2 * Copyright (c) 2016, 2020 Vladimir Kondratyev <wulf@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Copyright (c) 2015, 2016 Ulf Brosziewski
31 * Permission to use, copy, modify, and distribute this software for any
32 * purpose with or without fee is hereby granted, provided that the above
33 * copyright notice and this permission notice appear in all copies.
35 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
36 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
37 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
38 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
39 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
40 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
41 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 #include <sys/param.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/systm.h>
50 #include <dev/evdev/evdev.h>
51 #include <dev/evdev/evdev_private.h>
52 #include <dev/evdev/input.h>
55 #define debugf(fmt, args...) printf("evdev: " fmt "\n", ##args)
57 #define debugf(fmt, args...)
60 typedef u_int slotset_t;
62 _Static_assert(MAX_MT_SLOTS < sizeof(slotset_t) * 8, "MAX_MT_SLOTS too big");
64 #define FOREACHBIT(v, i) \
65 for ((i) = ffs(v) - 1; (i) != -1; (i) = ffs((v) & (~1 << (i))) - 1)
71 } static evdev_mtstmap[] = {
72 { ABS_MT_POSITION_X, ABS_X, 0 },
73 { ABS_MT_POSITION_Y, ABS_Y, 0 },
74 { ABS_MT_PRESSURE, ABS_PRESSURE, 255 },
75 { ABS_MT_TOUCH_MAJOR, ABS_TOOL_WIDTH, 15 },
79 int last_reported_slot;
81 int32_t tracking_ids[MAX_MT_SLOTS];
84 /* the set of slots with active touches */
86 /* the set of slots with unsynchronized state */
88 /* the set of slots to match with active touches */
89 slotset_t match_frame;
91 union evdev_mt_slot *match_slots;
93 union evdev_mt_slot slots[];
96 static void evdev_mt_support_st_compat(struct evdev_dev *);
97 static void evdev_mt_send_st_compat(struct evdev_dev *);
98 static void evdev_mt_send_autorel(struct evdev_dev *);
99 static void evdev_mt_replay_events(struct evdev_dev *);
102 ffc_slot(struct evdev_dev *evdev, slotset_t slots)
104 return (ffs(~slots & ((2U << MAXIMAL_MT_SLOT(evdev)) - 1)) - 1);
108 evdev_mt_init(struct evdev_dev *evdev)
111 size_t size = offsetof(struct evdev_mt, slots);
115 type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT);
117 /* Add events produced by MT type A to type B converter */
118 evdev_support_abs(evdev,
119 ABS_MT_SLOT, 0, MAX_MT_SLOTS - 1, 0, 0, 0);
120 evdev_support_abs(evdev,
121 ABS_MT_TRACKING_ID, -1, MAX_MT_SLOTS - 1, 0, 0, 0);
124 slots = MAXIMAL_MT_SLOT(evdev) + 1;
125 size += sizeof(mt->slots[0]) * slots;
126 if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) {
127 size += sizeof(mt->match_slots[0]) * slots;
128 size += sizeof(mt->matrix[0]) * (slots + 6) * slots;
131 mt = malloc(size, M_EVDEV, M_WAITOK | M_ZERO);
135 if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) {
136 mt->match_slots = mt->slots + slots;
137 mt->matrix = (int *)(mt->match_slots + slots);
140 /* Initialize multitouch protocol type B states */
141 for (slot = 0; slot < slots; slot++)
142 mt->slots[slot].id = -1;
144 if (!bit_test(evdev->ev_flags, EVDEV_FLAG_MT_KEEPID))
145 evdev_support_abs(evdev,
146 ABS_MT_TRACKING_ID, -1, UINT16_MAX, 0, 0, 0);
147 if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
148 evdev_mt_support_st_compat(evdev);
152 evdev_mt_free(struct evdev_dev *evdev)
154 free(evdev->ev_mt, M_EVDEV);
158 evdev_mt_sync_frame(struct evdev_dev *evdev)
160 if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK))
161 evdev_mt_replay_events(evdev);
162 if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_AUTOREL))
163 evdev_mt_send_autorel(evdev);
164 if (evdev->ev_report_opened &&
165 bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT))
166 evdev_mt_send_st_compat(evdev);
167 evdev->ev_mt->frame = 0;
171 evdev_mt_send_slot(struct evdev_dev *evdev, int slot,
172 union evdev_mt_slot *state)
175 bool type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT);
177 EVDEV_LOCK_ASSERT(evdev);
178 MPASS(type_a || (slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)));
179 MPASS(!type_a || state != NULL);
182 evdev_send_event(evdev, EV_ABS, ABS_MT_SLOT, slot);
184 evdev_send_event(evdev, EV_ABS, ABS_MT_TRACKING_ID, -1);
188 bit_foreach_at(evdev->ev_abs_flags, ABS_MT_FIRST, ABS_MT_LAST + 1, i)
189 evdev_send_event(evdev, EV_ABS, i,
190 state->val[ABS_MT_INDEX(i)]);
192 evdev_send_event(evdev, EV_SYN, SYN_MT_REPORT, 1);
196 evdev_mt_push_slot(struct evdev_dev *evdev, int slot,
197 union evdev_mt_slot *state)
199 struct evdev_mt *mt = evdev->ev_mt;
200 bool type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT);
202 if ((type_a || (mt != NULL && mt->type_a)) && state == NULL)
204 if (!type_a && (slot < 0 || slot > MAXIMAL_MT_SLOT(evdev)))
208 if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK) && mt->type_a) {
209 mt->match_slots[mt->match_slot] = *state;
210 evdev_mt_record_event(evdev, EV_SYN, SYN_MT_REPORT, 1);
211 } else if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) {
212 evdev_mt_record_event(evdev, EV_ABS, ABS_MT_SLOT, slot);
214 mt->match_slots[mt->match_slot] = *state;
216 evdev_mt_record_event(evdev, EV_ABS,
217 ABS_MT_TRACKING_ID, -1);
219 evdev_mt_send_slot(evdev, slot, state);
226 * Find a minimum-weight matching for an m-by-n matrix.
228 * m must be greater than or equal to n. The size of the buffer must be
231 * On return, the first m elements of the buffer contain the row-to-
232 * column mappings, i.e., buffer[i] is the column index for row i, or -1
233 * if there is no assignment for that row (which may happen if n < m).
235 * Wrong results because of overflows will not occur with input values
236 * in the range of 0 to INT_MAX / 2 inclusive.
238 * The function applies the Dinic-Kronrod algorithm. It is not modern or
239 * popular, but it seems to be a good choice for small matrices at least.
240 * The original form of the algorithm is modified as follows: There is no
241 * initial search for row minima, the initial assignments are in a
242 * "virtual" column with the index -1 and zero values. This permits inputs
243 * with n < m, and it simplifies the reassignments.
246 evdev_mt_matching(int *matrix, int m, int n, int *buffer)
248 int i, j, k, d, e, row, col, delta;
250 int *r2c = buffer; /* row-to-column assignments */
251 int *red = r2c + m; /* reduced values of the assignments */
252 int *mc = red + m; /* row-wise minimal elements of cs */
253 int *cs = mc + m; /* the column set */
254 int *c2r = cs + n; /* column-to-row assignments in cs */
255 int *cd = c2r + n; /* column deltas (reduction) */
257 for (p = r2c; p < red; *p++ = -1) {}
258 for (; p < mc; *p++ = 0) {}
259 for (col = 0; col < n; col++) {
261 for (i = 0, p = matrix + col; i < m; i++, p += n) {
263 if (d < delta || (d == delta && r2c[i] < 0)) {
273 for (p = mc; p < cs; *p++ = col) {}
274 for (k = 0; (j = r2c[row]) >= 0;) {
279 for (i = 0, p = matrix; i < m; i++, p += n)
281 d = p[mc[i]] - cd[mc[i]];
288 if (d < delta || (d == delta
295 for (i = 0; i < k; i++) {
297 red[c2r[cs[i]]] -= delta;
300 for (j = mc[row]; (r2c[row] = j) != col;) {
308 * Assign tracking IDs to the points in the pt array. The tracking ID
309 * assignment pairs the points with points of the previous frame in
310 * such a way that the sum of the squared distances is minimal. Using
311 * squares instead of simple distances favours assignments with more uniform
312 * distances, and it is faster.
313 * Set tracking id to -1 for unassigned (new) points.
316 evdev_mt_match_frame(struct evdev_dev *evdev, union evdev_mt_slot *pt,
319 struct evdev_mt *mt = evdev->ev_mt;
320 int i, j, m, n, dx, dy, slot, num_touches;
323 EVDEV_LOCK_ASSERT(evdev);
324 MPASS(mt->matrix != NULL);
325 MPASS(size >= 0 && size <= MAXIMAL_MT_SLOT(evdev) + 1);
331 num_touches = bitcount(mt->touches);
332 if (num_touches >= size) {
333 FOREACHBIT(mt->touches, slot)
334 for (i = 0; i < size; i++) {
335 dx = pt[i].x - mt->slots[slot].x;
336 dy = pt[i].y - mt->slots[slot].y;
337 *p++ = dx * dx + dy * dy;
342 for (i = 0; i < size; i++)
343 FOREACHBIT(mt->touches, slot) {
344 dx = pt[i].x - mt->slots[slot].x;
345 dy = pt[i].y - mt->slots[slot].y;
346 *p++ = dx * dx + dy * dy;
351 evdev_mt_matching(mt->matrix, m, n, p);
355 for (i = 0; i < m; i++)
356 if ((j = r2c[i]) >= 0)
359 p = (n == size ? c2r : r2c);
360 for (i = 0; i < size; i++)
364 p = (n == size ? r2c : c2r);
365 FOREACHBIT(mt->touches, slot)
367 pt[i].id = mt->tracking_ids[slot];
371 evdev_mt_send_frame(struct evdev_dev *evdev, union evdev_mt_slot *pt, int size)
373 struct evdev_mt *mt = evdev->ev_mt;
374 union evdev_mt_slot *slot;
376 EVDEV_LOCK_ASSERT(evdev);
377 MPASS(size >= 0 && size <= MAXIMAL_MT_SLOT(evdev) + 1);
380 * While MT-matching assign tracking IDs of new contacts to be equal
381 * to a slot number to make things simpler.
383 for (slot = pt; slot < pt + size; slot++) {
385 slot->id = ffc_slot(evdev, mt->touches | mt->frame);
387 evdev_mt_send_slot(evdev, slot->id, slot);
392 evdev_mt_push_frame(struct evdev_dev *evdev, union evdev_mt_slot *pt, int size)
394 if (size < 0 || size > MAXIMAL_MT_SLOT(evdev) + 1)
398 evdev_mt_send_frame(evdev, pt, size);
405 evdev_mt_record_event(struct evdev_dev *evdev, uint16_t type, uint16_t code,
408 struct evdev_mt *mt = evdev->ev_mt;
410 EVDEV_LOCK_ASSERT(evdev);
414 if (code == SYN_MT_REPORT) {
415 /* MT protocol type A support */
416 KASSERT(mt->type_a, ("Not a MT type A protocol"));
417 mt->match_frame |= 1U << mt->match_slot;
423 if (code == ABS_MT_SLOT) {
424 /* MT protocol type B support */
425 KASSERT(!mt->type_a, ("Not a MT type B protocol"));
426 KASSERT(value >= 0, ("Negative slot number"));
427 mt->match_slot = value;
428 mt->match_frame |= 1U << mt->match_slot;
430 } else if (code == ABS_MT_TRACKING_ID) {
431 KASSERT(!mt->type_a, ("Not a MT type B protocol"));
433 mt->match_frame &= ~(1U << mt->match_slot);
435 } else if (ABS_IS_MT(code)) {
436 KASSERT(mt->match_slot >= 0, ("Negative slot"));
437 KASSERT(mt->match_slot <= MAXIMAL_MT_SLOT(evdev),
438 ("Slot number too big"));
439 mt->match_slots[mt->match_slot].
440 val[ABS_MT_INDEX(code)] = value;
452 evdev_mt_replay_events(struct evdev_dev *evdev)
454 struct evdev_mt *mt = evdev->ev_mt;
457 EVDEV_LOCK_ASSERT(evdev);
459 FOREACHBIT(mt->match_frame, slot) {
461 mt->match_slots[size] = mt->match_slots[slot];
464 evdev_mt_match_frame(evdev, mt->match_slots, size);
465 evdev_mt_send_frame(evdev, mt->match_slots, size);
470 union evdev_mt_slot *
471 evdev_mt_get_match_slots(struct evdev_dev *evdev)
473 return (evdev->ev_mt->match_slots);
477 evdev_mt_get_last_slot(struct evdev_dev *evdev)
479 return (evdev->ev_mt->last_reported_slot);
483 evdev_mt_set_last_slot(struct evdev_dev *evdev, int slot)
485 struct evdev_mt *mt = evdev->ev_mt;
487 MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev));
489 mt->frame |= 1U << slot;
490 mt->last_reported_slot = slot;
494 evdev_mt_get_value(struct evdev_dev *evdev, int slot, int16_t code)
496 struct evdev_mt *mt = evdev->ev_mt;
498 MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev));
500 return (mt->slots[slot].val[ABS_MT_INDEX(code)]);
504 evdev_mt_set_value(struct evdev_dev *evdev, int slot, int16_t code,
507 struct evdev_mt *mt = evdev->ev_mt;
509 MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev));
511 if (code == ABS_MT_TRACKING_ID) {
513 mt->touches |= 1U << slot;
515 mt->touches &= ~(1U << slot);
517 mt->slots[slot].val[ABS_MT_INDEX(code)] = value;
521 evdev_mt_id_to_slot(struct evdev_dev *evdev, int32_t tracking_id)
523 struct evdev_mt *mt = evdev->ev_mt;
526 KASSERT(!mt->type_a, ("Not a MT type B protocol"));
529 * Ignore tracking_id if slot assignment is performed by evdev.
530 * Events are written sequentially to temporary matching buffer.
532 if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK))
533 return (ffc_slot(evdev, mt->match_frame));
535 FOREACHBIT(mt->touches, slot)
536 if (mt->tracking_ids[slot] == tracking_id)
539 * Do not allow allocation of new slot in a place of just
540 * released one within the same report.
542 return (ffc_slot(evdev, mt->touches | mt->frame));
546 evdev_mt_reassign_id(struct evdev_dev *evdev, int slot, int32_t id)
548 struct evdev_mt *mt = evdev->ev_mt;
551 if (id == -1 || bit_test(evdev->ev_flags, EVDEV_FLAG_MT_KEEPID)) {
552 mt->tracking_ids[slot] = id;
556 nid = evdev_mt_get_value(evdev, slot, ABS_MT_TRACKING_ID);
558 KASSERT(id == mt->tracking_ids[slot],
559 ("MT-slot tracking id has changed"));
563 mt->tracking_ids[slot] = id;
565 nid = mt->tracking_id++;
566 FOREACHBIT(mt->touches, slot)
567 if (evdev_mt_get_value(evdev, slot, ABS_MT_TRACKING_ID) == nid)
573 static inline int32_t
574 evdev_mt_normalize(int32_t value, int32_t mtmin, int32_t mtmax, int32_t stmax)
576 if (stmax != 0 && mtmax != mtmin) {
577 value = (value - mtmin) * stmax / (mtmax - mtmin);
578 value = MAX(MIN(value, stmax), 0);
584 evdev_mt_support_st_compat(struct evdev_dev *evdev)
586 struct input_absinfo *ai;
589 if (evdev->ev_absinfo == NULL)
592 evdev_support_event(evdev, EV_KEY);
593 evdev_support_key(evdev, BTN_TOUCH);
595 /* Touchscreens should not advertise tap tool capabilities */
596 if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT))
597 evdev_support_nfingers(evdev, MAXIMAL_MT_SLOT(evdev) + 1);
599 /* Echo 0-th MT-slot as ST-slot */
600 for (i = 0; i < nitems(evdev_mtstmap); i++) {
601 if (!bit_test(evdev->ev_abs_flags, evdev_mtstmap[i].mt) ||
602 bit_test(evdev->ev_abs_flags, evdev_mtstmap[i].st))
604 ai = evdev->ev_absinfo + evdev_mtstmap[i].mt;
605 evdev->ev_mt->mtst_events |= 1U << i;
606 if (evdev_mtstmap[i].max != 0)
607 evdev_support_abs(evdev, evdev_mtstmap[i].st,
609 evdev_mtstmap[i].max,
612 ai->flat, 0, ai->maximum, evdev_mtstmap[i].max),
615 evdev_support_abs(evdev, evdev_mtstmap[i].st,
625 evdev_mt_send_st_compat(struct evdev_dev *evdev)
627 struct evdev_mt *mt = evdev->ev_mt;
628 int nfingers, i, st_slot;
630 EVDEV_LOCK_ASSERT(evdev);
632 nfingers = bitcount(mt->touches);
633 evdev_send_event(evdev, EV_KEY, BTN_TOUCH, nfingers > 0);
635 /* Send first active MT-slot state as single touch report */
636 st_slot = ffs(mt->touches) - 1;
638 FOREACHBIT(mt->mtst_events, i)
639 evdev_send_event(evdev, EV_ABS, evdev_mtstmap[i].st,
640 evdev_mt_normalize(evdev_mt_get_value(evdev,
641 st_slot, evdev_mtstmap[i].mt),
642 evdev->ev_absinfo[evdev_mtstmap[i].mt].minimum,
643 evdev->ev_absinfo[evdev_mtstmap[i].mt].maximum,
644 evdev_mtstmap[i].max));
646 /* Touchscreens should not report tool taps */
647 if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT))
648 evdev_send_nfingers(evdev, nfingers);
651 evdev_send_event(evdev, EV_ABS, ABS_PRESSURE, 0);
655 evdev_mt_send_autorel(struct evdev_dev *evdev)
657 struct evdev_mt *mt = evdev->ev_mt;
660 EVDEV_LOCK_ASSERT(evdev);
661 KASSERT(mt->match_frame == 0, ("Unmatched events exist"));
663 FOREACHBIT(mt->touches & ~mt->frame, slot)
664 evdev_mt_send_slot(evdev, slot, NULL);
668 evdev_mt_push_autorel(struct evdev_dev *evdev)
671 evdev_mt_send_autorel(evdev);