Lines 1-5
Link Here
|
1 |
/*- |
1 |
/*- |
2 |
* Copyright (c) 2016 Vladimir Kondratyev <wulf@FreeBSD.org> |
2 |
* Copyright (c) 2016, 2020 Vladimir Kondratyev <wulf@FreeBSD.org> |
3 |
* All rights reserved. |
3 |
* All rights reserved. |
4 |
* |
4 |
* |
5 |
* Redistribution and use in source and binary forms, with or without |
5 |
* Redistribution and use in source and binary forms, with or without |
Lines 25-30
Link Here
|
25 |
* |
25 |
* |
26 |
* $FreeBSD$ |
26 |
* $FreeBSD$ |
27 |
*/ |
27 |
*/ |
|
|
28 |
/*- |
29 |
* Copyright (c) 2015, 2016 Ulf Brosziewski |
30 |
* |
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. |
34 |
* |
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. |
42 |
*/ |
28 |
|
43 |
|
29 |
#include <sys/param.h> |
44 |
#include <sys/param.h> |
30 |
#include <sys/lock.h> |
45 |
#include <sys/lock.h> |
Lines 42-95
Link Here
|
42 |
#define debugf(fmt, args...) |
57 |
#define debugf(fmt, args...) |
43 |
#endif |
58 |
#endif |
44 |
|
59 |
|
45 |
static uint16_t evdev_fngmap[] = { |
60 |
typedef u_int slotset_t; |
46 |
BTN_TOOL_FINGER, |
|
|
47 |
BTN_TOOL_DOUBLETAP, |
48 |
BTN_TOOL_TRIPLETAP, |
49 |
BTN_TOOL_QUADTAP, |
50 |
BTN_TOOL_QUINTTAP, |
51 |
}; |
52 |
|
61 |
|
53 |
static uint16_t evdev_mtstmap[][2] = { |
62 |
_Static_assert(MAX_MT_SLOTS < sizeof(slotset_t) * 8, "MAX_MT_SLOTS too big"); |
54 |
{ ABS_MT_POSITION_X, ABS_X }, |
63 |
|
55 |
{ ABS_MT_POSITION_Y, ABS_Y }, |
64 |
#define FOREACHBIT(v, i) \ |
56 |
{ ABS_MT_PRESSURE, ABS_PRESSURE }, |
65 |
for ((i) = ffs(v) - 1; (i) != -1; (i) = ffs((v) & (~1 << (i))) - 1) |
57 |
{ ABS_MT_TOUCH_MAJOR, ABS_TOOL_WIDTH }, |
|
|
58 |
}; |
59 |
|
66 |
|
60 |
struct evdev_mt_slot { |
67 |
struct { |
61 |
uint64_t ev_report; |
68 |
uint16_t mt; |
62 |
int32_t ev_mt_states[MT_CNT]; |
69 |
uint16_t st; |
|
|
70 |
int32_t max; |
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 }, |
63 |
}; |
76 |
}; |
64 |
|
77 |
|
65 |
struct evdev_mt { |
78 |
struct evdev_mt { |
66 |
int32_t ev_mt_last_reported_slot; |
79 |
int last_reported_slot; |
67 |
struct evdev_mt_slot ev_mt_slots[]; |
80 |
uint16_t tracking_id; |
|
|
81 |
int32_t tracking_ids[MAX_MT_SLOTS]; |
82 |
bool type_a; |
83 |
u_int mtst_events; |
84 |
/* the set of slots with active touches */ |
85 |
slotset_t touches; |
86 |
/* the set of slots with unsynchronized state */ |
87 |
slotset_t frame; |
88 |
/* the set of slots to match with active touches */ |
89 |
slotset_t match_frame; |
90 |
int match_slot; |
91 |
union evdev_mt_slot *match_slots; |
92 |
int *matrix; |
93 |
union evdev_mt_slot slots[]; |
68 |
}; |
94 |
}; |
69 |
|
95 |
|
|
|
96 |
static void evdev_mt_send_st_compat(struct evdev_dev *); |
97 |
static void evdev_mt_send_autorel(struct evdev_dev *); |
98 |
static void evdev_mt_replay_events(struct evdev_dev *); |
99 |
|
100 |
static inline int |
101 |
ffc_slot(struct evdev_dev *evdev, slotset_t slots) |
102 |
{ |
103 |
return (ffs(~slots & (2U << MAXIMAL_MT_SLOT(evdev)) - 1) - 1); |
104 |
} |
105 |
|
70 |
void |
106 |
void |
71 |
evdev_mt_init(struct evdev_dev *evdev) |
107 |
evdev_mt_init(struct evdev_dev *evdev) |
72 |
{ |
108 |
{ |
73 |
int32_t slot, slots; |
109 |
struct evdev_mt *mt; |
|
|
110 |
size_t size = offsetof(struct evdev_mt, slots); |
111 |
int slot, slots; |
112 |
bool type_a; |
113 |
|
114 |
type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT); |
115 |
if (type_a) { |
116 |
/* Add events produced by MT type A to type B convertor */ |
117 |
evdev_support_abs(evdev, |
118 |
ABS_MT_SLOT, 0, MAX_MT_SLOTS - 1, 0, 0, 0); |
119 |
if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_KEEPID)) |
120 |
evdev_support_abs(evdev, |
121 |
ABS_MT_TRACKING_ID, -1, MAX_MT_SLOTS - 1, 0, 0, 0); |
122 |
} |
74 |
|
123 |
|
75 |
slots = MAXIMAL_MT_SLOT(evdev) + 1; |
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; |
129 |
} |
76 |
|
130 |
|
77 |
evdev->ev_mt = malloc(offsetof(struct evdev_mt, ev_mt_slots) + |
131 |
mt = malloc(size, M_EVDEV, M_WAITOK | M_ZERO); |
78 |
sizeof(struct evdev_mt_slot) * slots, M_EVDEV, M_WAITOK | M_ZERO); |
132 |
evdev->ev_mt = mt; |
|
|
133 |
mt->type_a = type_a; |
79 |
|
134 |
|
80 |
/* Initialize multitouch protocol type B states */ |
135 |
if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) { |
81 |
for (slot = 0; slot < slots; slot++) { |
136 |
mt->match_slots = mt->slots + slots; |
82 |
/* |
137 |
mt->matrix = (int *)(mt->match_slots + slots); |
83 |
* .ev_report should not be initialized to initial value of |
|
|
84 |
* report counter (0) as it brokes free slot detection in |
85 |
* evdev_get_mt_slot_by_tracking_id. So initialize it to -1 |
86 |
*/ |
87 |
evdev->ev_mt->ev_mt_slots[slot] = (struct evdev_mt_slot) { |
88 |
.ev_report = 0xFFFFFFFFFFFFFFFFULL, |
89 |
.ev_mt_states[ABS_MT_INDEX(ABS_MT_TRACKING_ID)] = -1, |
90 |
}; |
91 |
} |
138 |
} |
92 |
|
139 |
|
|
|
140 |
/* Initialize multitouch protocol type B states */ |
141 |
for (slot = 0; slot < slots; slot++) |
142 |
mt->slots[slot].id = -1; |
143 |
|
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); |
93 |
if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT)) |
147 |
if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_STCOMPAT)) |
94 |
evdev_support_mt_compat(evdev); |
148 |
evdev_support_mt_compat(evdev); |
95 |
} |
149 |
} |
Lines 97-251
evdev_mt_init(struct evdev_dev *evdev)
Link Here
|
97 |
void |
151 |
void |
98 |
evdev_mt_free(struct evdev_dev *evdev) |
152 |
evdev_mt_free(struct evdev_dev *evdev) |
99 |
{ |
153 |
{ |
100 |
|
|
|
101 |
free(evdev->ev_mt, M_EVDEV); |
154 |
free(evdev->ev_mt, M_EVDEV); |
102 |
} |
155 |
} |
103 |
|
156 |
|
104 |
int32_t |
157 |
void |
105 |
evdev_get_last_mt_slot(struct evdev_dev *evdev) |
158 |
evdev_mt_sync_frame(struct evdev_dev *evdev) |
|
|
159 |
{ |
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; |
168 |
} |
169 |
|
170 |
static void |
171 |
evdev_mt_send_slot(struct evdev_dev *evdev, int slot, |
172 |
union evdev_mt_slot *state) |
106 |
{ |
173 |
{ |
|
|
174 |
int i; |
175 |
bool type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT); |
176 |
|
177 |
EVDEV_LOCK_ASSERT(evdev); |
178 |
MPASS(type_a || (slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev))); |
179 |
MPASS(!type_a || state != NULL); |
180 |
|
181 |
if (!type_a) { |
182 |
evdev_send_event(evdev, EV_ABS, ABS_MT_SLOT, slot); |
183 |
if (state == NULL) { |
184 |
evdev_send_event(evdev, EV_ABS, ABS_MT_TRACKING_ID, -1); |
185 |
return; |
186 |
} |
187 |
} |
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)]); |
191 |
if (type_a) |
192 |
evdev_send_event(evdev, EV_SYN, SYN_MT_REPORT, 1); |
193 |
} |
194 |
|
195 |
int |
196 |
evdev_mt_push_slot(struct evdev_dev *evdev, int slot, |
197 |
union evdev_mt_slot *state) |
198 |
{ |
199 |
struct evdev_mt *mt = evdev->ev_mt; |
200 |
bool type_a = !bit_test(evdev->ev_abs_flags, ABS_MT_SLOT); |
201 |
|
202 |
if ((type_a || (mt != NULL && mt->type_a)) && state == NULL) |
203 |
return (EINVAL); |
204 |
if (!type_a && (slot < 0 || slot > MAXIMAL_MT_SLOT(evdev))) |
205 |
return (EINVAL); |
206 |
|
207 |
EVDEV_ENTER(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); |
213 |
if (state != NULL) |
214 |
mt->match_slots[mt->match_slot] = *state; |
215 |
else |
216 |
evdev_mt_record_event(evdev, EV_ABS, |
217 |
ABS_MT_TRACKING_ID, -1); |
218 |
} else |
219 |
evdev_mt_send_slot(evdev, slot, state); |
220 |
EVDEV_EXIT(evdev); |
221 |
|
222 |
return (0); |
223 |
} |
107 |
|
224 |
|
108 |
return (evdev->ev_mt->ev_mt_last_reported_slot); |
225 |
/* |
|
|
226 |
* Find a minimum-weight matching for an m-by-n matrix. |
227 |
* |
228 |
* m must be greater than or equal to n. The size of the buffer must be |
229 |
* at least 3m + 3n. |
230 |
* |
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). |
234 |
* |
235 |
* Wrong results because of overflows will not occur with input values |
236 |
* in the range of 0 to INT_MAX / 2 inclusive. |
237 |
* |
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. |
244 |
*/ |
245 |
static void |
246 |
evdev_mt_matching(int *matrix, int m, int n, int *buffer) |
247 |
{ |
248 |
int i, j, k, d, e, row, col, delta; |
249 |
int *p; |
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) */ |
256 |
|
257 |
for (p = r2c; p < red; *p++ = -1) {} |
258 |
for (; p < mc; *p++ = 0) {} |
259 |
for (col = 0; col < n; col++) { |
260 |
delta = INT_MAX; |
261 |
for (i = 0, p = matrix + col; i < m; i++, p += n) { |
262 |
d = *p - red[i]; |
263 |
if (d < delta || (d == delta && r2c[i] < 0)) { |
264 |
delta = d; |
265 |
row = i; |
266 |
} |
267 |
} |
268 |
cd[col] = delta; |
269 |
if (r2c[row] < 0) { |
270 |
r2c[row] = col; |
271 |
continue; |
272 |
} |
273 |
for (p = mc; p < cs; *p++ = col) {} |
274 |
for (k = 0; (j = r2c[row]) >= 0;) { |
275 |
cs[k++] = j; |
276 |
c2r[j] = row; |
277 |
mc[row] -= n; |
278 |
delta = INT_MAX; |
279 |
for (i = 0, p = matrix; i < m; i++, p += n) |
280 |
if (mc[i] >= 0) { |
281 |
d = p[mc[i]] - cd[mc[i]]; |
282 |
e = p[j] - cd[j]; |
283 |
if (e < d) { |
284 |
d = e; |
285 |
mc[i] = j; |
286 |
} |
287 |
d -= red[i]; |
288 |
if (d < delta || (d == delta |
289 |
&& r2c[i] < 0)) { |
290 |
delta = d; |
291 |
row = i; |
292 |
} |
293 |
} |
294 |
cd[col] += delta; |
295 |
for (i = 0; i < k; i++) { |
296 |
cd[cs[i]] += delta; |
297 |
red[c2r[cs[i]]] -= delta; |
298 |
} |
299 |
} |
300 |
for (j = mc[row]; (r2c[row] = j) != col;) { |
301 |
row = c2r[j]; |
302 |
j = mc[row] + n; |
303 |
} |
304 |
} |
109 |
} |
305 |
} |
110 |
|
306 |
|
|
|
307 |
/* |
308 |
* Assign slot numbers to the points in the pt array. The slot numbers are |
309 |
* passed to the caller in the pt->id fields. |
310 |
* |
311 |
* The slot assignment pairs the points with points of the previous frame in |
312 |
* such a way that the sum of the squared distances is minimal. Using |
313 |
* squares instead of simple distances favours assignments with more uniform |
314 |
* distances, and it is faster. |
315 |
*/ |
111 |
void |
316 |
void |
112 |
evdev_set_last_mt_slot(struct evdev_dev *evdev, int32_t slot) |
317 |
evdev_mt_match_frame(struct evdev_dev *evdev, union evdev_mt_slot *pt, |
|
|
318 |
int size) |
113 |
{ |
319 |
{ |
|
|
320 |
struct evdev_mt *mt = evdev->ev_mt; |
321 |
int i, j, m, n, dx, dy, slot, num_touches; |
322 |
int *p, *r2c, *c2r; |
323 |
u_int touches; |
324 |
|
325 |
EVDEV_LOCK_ASSERT(evdev); |
326 |
MPASS(mt->matrix != NULL); |
327 |
MPASS(size >= 0 && size <= MAXIMAL_MT_SLOT(evdev) + 1); |
114 |
|
328 |
|
115 |
evdev->ev_mt->ev_mt_slots[slot].ev_report = evdev->ev_report_count; |
329 |
if (size == 0) |
116 |
evdev->ev_mt->ev_mt_last_reported_slot = slot; |
330 |
return; |
|
|
331 |
|
332 |
p = mt->matrix; |
333 |
touches = mt->touches; |
334 |
num_touches = bitcount(touches); |
335 |
if (num_touches >= size) { |
336 |
FOREACHBIT(touches, slot) |
337 |
for (i = 0; i < size; i++) { |
338 |
dx = pt[i].x - mt->slots[slot].x; |
339 |
dy = pt[i].y - mt->slots[slot].y; |
340 |
*p++ = dx * dx + dy * dy; |
341 |
} |
342 |
m = num_touches; |
343 |
n = size; |
344 |
} else { |
345 |
for (i = 0; i < size; i++) |
346 |
FOREACHBIT(touches, slot) { |
347 |
dx = pt[i].x - mt->slots[slot].x; |
348 |
dy = pt[i].y - mt->slots[slot].y; |
349 |
*p++ = dx * dx + dy * dy; |
350 |
} |
351 |
m = size; |
352 |
n = num_touches; |
353 |
} |
354 |
evdev_mt_matching(mt->matrix, m, n, p); |
355 |
|
356 |
r2c = p; |
357 |
c2r = p + m; |
358 |
for (i = 0; i < m; i++) |
359 |
if ((j = r2c[i]) >= 0) |
360 |
c2r[j] = i; |
361 |
|
362 |
p = (n == size ? r2c : c2r); |
363 |
FOREACHBIT(touches, slot) |
364 |
if ((i = *p++) >= 0) |
365 |
pt[i].id = slot; |
366 |
|
367 |
p = (n == size ? c2r : r2c); |
368 |
for (i = 0; i < size; i++) |
369 |
if (*p++ < 0 && (pt[i].id = ffc_slot(evdev, touches)) >= 0) |
370 |
touches |= 1U << pt[i].id; |
117 |
} |
371 |
} |
118 |
|
372 |
|
119 |
inline int32_t |
373 |
static void |
120 |
evdev_get_mt_value(struct evdev_dev *evdev, int32_t slot, int16_t code) |
374 |
evdev_mt_send_frame(struct evdev_dev *evdev, union evdev_mt_slot *pt, int size) |
121 |
{ |
375 |
{ |
|
|
376 |
union evdev_mt_slot *slot; |
377 |
|
378 |
EVDEV_LOCK_ASSERT(evdev); |
379 |
MPASS(size >= 0 && size <= MAXIMAL_MT_SLOT(evdev) + 1); |
122 |
|
380 |
|
123 |
return (evdev->ev_mt-> |
381 |
for (slot = pt; slot < pt + size; slot++) |
124 |
ev_mt_slots[slot].ev_mt_states[ABS_MT_INDEX(code)]); |
382 |
if (slot->id >= 0) |
|
|
383 |
evdev_mt_send_slot(evdev, slot->id, slot); |
125 |
} |
384 |
} |
126 |
|
385 |
|
127 |
inline void |
386 |
int |
128 |
evdev_set_mt_value(struct evdev_dev *evdev, int32_t slot, int16_t code, |
387 |
evdev_mt_push_frame(struct evdev_dev *evdev, union evdev_mt_slot *pt, int size) |
|
|
388 |
{ |
389 |
if (size < 0 || size > MAXIMAL_MT_SLOT(evdev) + 1) |
390 |
return (EINVAL); |
391 |
|
392 |
EVDEV_ENTER(evdev); |
393 |
evdev_mt_send_frame(evdev, pt, size); |
394 |
EVDEV_EXIT(evdev); |
395 |
|
396 |
return (0); |
397 |
} |
398 |
|
399 |
bool |
400 |
evdev_mt_record_event(struct evdev_dev *evdev, uint16_t type, uint16_t code, |
129 |
int32_t value) |
401 |
int32_t value) |
130 |
{ |
402 |
{ |
|
|
403 |
struct evdev_mt *mt = evdev->ev_mt; |
404 |
|
405 |
EVDEV_LOCK_ASSERT(evdev); |
406 |
|
407 |
switch (type) { |
408 |
case EV_SYN: |
409 |
if (code == SYN_MT_REPORT) { |
410 |
/* MT protocol type A support */ |
411 |
KASSERT(mt->type_a, ("Not a MT type A protocol")); |
412 |
mt->match_frame |= 1U << mt->match_slot; |
413 |
mt->match_slot++; |
414 |
return (true); |
415 |
} |
416 |
break; |
417 |
case EV_ABS: |
418 |
if (code == ABS_MT_SLOT) { |
419 |
/* MT protocol type B support */ |
420 |
KASSERT(!mt->type_a, ("Not a MT type B protocol")); |
421 |
KASSERT(value >= 0, ("Negative slot number")); |
422 |
mt->match_slot = value; |
423 |
mt->match_frame |= 1U << mt->match_slot; |
424 |
return (true); |
425 |
} else if (code == ABS_MT_TRACKING_ID) { |
426 |
KASSERT(!mt->type_a, ("Not a MT type B protocol")); |
427 |
if (value == -1) |
428 |
mt->match_frame &= ~(1U << mt->match_slot); |
429 |
return (true); |
430 |
} else if (ABS_IS_MT(code)) { |
431 |
KASSERT(mt->match_slot >= 0, ("Negative slot")); |
432 |
KASSERT(mt->match_slot <= MAXIMAL_MT_SLOT(evdev), |
433 |
("Slot number too big")); |
434 |
mt->match_slots[mt->match_slot]. |
435 |
val[ABS_MT_INDEX(code)] = value; |
436 |
return (true); |
437 |
} |
438 |
break; |
439 |
default: |
440 |
break; |
441 |
} |
131 |
|
442 |
|
132 |
evdev->ev_mt->ev_mt_slots[slot].ev_mt_states[ABS_MT_INDEX(code)] = |
443 |
return (false); |
133 |
value; |
|
|
134 |
} |
444 |
} |
135 |
|
445 |
|
136 |
int32_t |
446 |
static void |
137 |
evdev_get_mt_slot_by_tracking_id(struct evdev_dev *evdev, int32_t tracking_id) |
447 |
evdev_mt_replay_events(struct evdev_dev *evdev) |
138 |
{ |
448 |
{ |
139 |
int32_t tr_id, slot, free_slot = -1; |
449 |
struct evdev_mt *mt = evdev->ev_mt; |
|
|
450 |
int slot, size = 0; |
140 |
|
451 |
|
141 |
for (slot = 0; slot <= MAXIMAL_MT_SLOT(evdev); slot++) { |
452 |
EVDEV_LOCK_ASSERT(evdev); |
142 |
tr_id = evdev_get_mt_value(evdev, slot, ABS_MT_TRACKING_ID); |
453 |
|
143 |
if (tr_id == tracking_id) |
454 |
FOREACHBIT(mt->match_frame, slot) { |
144 |
return (slot); |
455 |
if (slot != size) |
145 |
/* |
456 |
mt->match_slots[size] = mt->match_slots[slot]; |
146 |
* Its possible that slot will be reassigned in a place of just |
457 |
size++; |
147 |
* released one within the same report. To avoid this compare |
|
|
148 |
* report counter with slot`s report number updated with each |
149 |
* ABS_MT_TRACKING_ID change. |
150 |
*/ |
151 |
if (free_slot == -1 && tr_id == -1 && |
152 |
evdev->ev_mt->ev_mt_slots[slot].ev_report != |
153 |
evdev->ev_report_count) |
154 |
free_slot = slot; |
155 |
} |
458 |
} |
|
|
459 |
evdev_mt_match_frame(evdev, mt->match_slots, size); |
460 |
evdev_mt_send_frame(evdev, mt->match_slots, size); |
461 |
mt->match_slot = 0; |
462 |
mt->match_frame = 0; |
463 |
} |
464 |
|
465 |
union evdev_mt_slot * |
466 |
evdev_mt_get_match_slots(struct evdev_dev *evdev) |
467 |
{ |
468 |
return (evdev->ev_mt->match_slots); |
469 |
} |
156 |
|
470 |
|
157 |
return (free_slot); |
471 |
int |
|
|
472 |
evdev_mt_get_last_slot(struct evdev_dev *evdev) |
473 |
{ |
474 |
return (evdev->ev_mt->last_reported_slot); |
158 |
} |
475 |
} |
159 |
|
476 |
|
160 |
void |
477 |
void |
161 |
evdev_support_nfingers(struct evdev_dev *evdev, int32_t nfingers) |
478 |
evdev_mt_set_last_slot(struct evdev_dev *evdev, int slot) |
162 |
{ |
479 |
{ |
163 |
int32_t i; |
480 |
struct evdev_mt *mt = evdev->ev_mt; |
164 |
|
481 |
|
165 |
for (i = 0; i < MIN(nitems(evdev_fngmap), nfingers); i++) |
482 |
MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); |
166 |
evdev_support_key(evdev, evdev_fngmap[i]); |
483 |
|
|
|
484 |
mt->frame |= 1U << slot; |
485 |
mt->last_reported_slot = slot; |
167 |
} |
486 |
} |
168 |
|
487 |
|
169 |
void |
488 |
int32_t |
170 |
evdev_support_mt_compat(struct evdev_dev *evdev) |
489 |
evdev_mt_get_value(struct evdev_dev *evdev, int slot, int16_t code) |
171 |
{ |
490 |
{ |
172 |
int32_t i; |
491 |
struct evdev_mt *mt = evdev->ev_mt; |
173 |
|
492 |
|
174 |
if (evdev->ev_absinfo == NULL) |
493 |
MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); |
175 |
return; |
|
|
176 |
|
494 |
|
177 |
evdev_support_event(evdev, EV_KEY); |
495 |
return (mt->slots[slot].val[ABS_MT_INDEX(code)]); |
178 |
evdev_support_key(evdev, BTN_TOUCH); |
496 |
} |
179 |
|
497 |
|
180 |
/* Touchscreens should not advertise tap tool capabilities */ |
498 |
void |
181 |
if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT)) |
499 |
evdev_mt_set_value(struct evdev_dev *evdev, int slot, int16_t code, |
182 |
evdev_support_nfingers(evdev, MAXIMAL_MT_SLOT(evdev) + 1); |
500 |
int32_t value) |
|
|
501 |
{ |
502 |
struct evdev_mt *mt = evdev->ev_mt; |
183 |
|
503 |
|
184 |
/* Echo 0-th MT-slot as ST-slot */ |
504 |
MPASS(slot >= 0 && slot <= MAXIMAL_MT_SLOT(evdev)); |
185 |
for (i = 0; i < nitems(evdev_mtstmap); i++) |
505 |
|
186 |
if (bit_test(evdev->ev_abs_flags, evdev_mtstmap[i][0])) |
506 |
if (code == ABS_MT_TRACKING_ID) { |
187 |
evdev_support_abs(evdev, evdev_mtstmap[i][1], |
507 |
if (value != -1) |
188 |
evdev->ev_absinfo[evdev_mtstmap[i][0]].minimum, |
508 |
mt->touches |= 1U << slot; |
189 |
evdev->ev_absinfo[evdev_mtstmap[i][0]].maximum, |
509 |
else |
190 |
evdev->ev_absinfo[evdev_mtstmap[i][0]].fuzz, |
510 |
mt->touches &= ~(1U << slot); |
191 |
evdev->ev_absinfo[evdev_mtstmap[i][0]].flat, |
511 |
} |
192 |
evdev->ev_absinfo[evdev_mtstmap[i][0]].resolution); |
512 |
mt->slots[slot].val[ABS_MT_INDEX(code)] = value; |
193 |
} |
513 |
} |
194 |
|
514 |
|
195 |
static int32_t |
515 |
int |
196 |
evdev_count_fingers(struct evdev_dev *evdev) |
516 |
evdev_get_mt_slot_by_tracking_id(struct evdev_dev *evdev, int32_t tracking_id) |
197 |
{ |
517 |
{ |
198 |
int32_t nfingers = 0, i; |
518 |
struct evdev_mt *mt = evdev->ev_mt; |
|
|
519 |
int slot; |
199 |
|
520 |
|
200 |
for (i = 0; i <= MAXIMAL_MT_SLOT(evdev); i++) |
521 |
KASSERT(!mt->type_a, ("Not a MT type B protocol")); |
201 |
if (evdev_get_mt_value(evdev, i, ABS_MT_TRACKING_ID) != -1) |
|
|
202 |
nfingers++; |
203 |
|
522 |
|
204 |
return (nfingers); |
523 |
/* |
|
|
524 |
* Ignore tracking_id if slot assignment is performed by evdev. |
525 |
* Events are written sequentially to temporary matching buffer. |
526 |
*/ |
527 |
if (bit_test(evdev->ev_flags, EVDEV_FLAG_MT_TRACK)) |
528 |
return (ffc_slot(evdev, mt->match_frame)); |
529 |
|
530 |
FOREACHBIT(mt->touches, slot) |
531 |
if (mt->tracking_ids[slot] == tracking_id) |
532 |
return (slot); |
533 |
/* |
534 |
* Do not allow allocation of new slot in a place of just |
535 |
* released one within the same report. |
536 |
*/ |
537 |
return (ffc_slot(evdev, mt->touches | mt->frame)); |
205 |
} |
538 |
} |
206 |
|
539 |
|
207 |
static void |
540 |
int32_t |
208 |
evdev_send_nfingers(struct evdev_dev *evdev, int32_t nfingers) |
541 |
evdev_mt_reassign_id(struct evdev_dev *evdev, int slot, int32_t id) |
209 |
{ |
542 |
{ |
210 |
int32_t i; |
543 |
struct evdev_mt *mt = evdev->ev_mt; |
|
|
544 |
int32_t nid; |
211 |
|
545 |
|
212 |
EVDEV_LOCK_ASSERT(evdev); |
546 |
if (id == -1 || bit_test(evdev->ev_flags, EVDEV_FLAG_MT_KEEPID)) { |
|
|
547 |
mt->tracking_ids[slot] = id; |
548 |
return (id); |
549 |
} |
213 |
|
550 |
|
214 |
if (nfingers > nitems(evdev_fngmap)) |
551 |
nid = evdev_mt_get_value(evdev, slot, ABS_MT_TRACKING_ID); |
215 |
nfingers = nitems(evdev_fngmap); |
552 |
if (nid != -1) { |
|
|
553 |
KASSERT(id == mt->tracking_ids[slot], |
554 |
("MT-slot tracking id has changed")); |
555 |
return (nid); |
556 |
} |
216 |
|
557 |
|
217 |
for (i = 0; i < nitems(evdev_fngmap); i++) |
558 |
mt->tracking_ids[slot] = id; |
218 |
evdev_send_event(evdev, EV_KEY, evdev_fngmap[i], |
559 |
again: |
219 |
nfingers == i + 1); |
560 |
nid = mt->tracking_id++; |
|
|
561 |
FOREACHBIT(mt->touches, slot) |
562 |
if (evdev_mt_get_value(evdev, slot, ABS_MT_TRACKING_ID) == nid) |
563 |
goto again; |
564 |
|
565 |
return (nid); |
566 |
} |
567 |
|
568 |
static inline int32_t |
569 |
evdev_mt_normalize(int32_t value, int32_t mtmin, int32_t mtmax, int32_t stmax) |
570 |
{ |
571 |
if (stmax != 0 && mtmax != mtmin) { |
572 |
value = (value - mtmin) * stmax / (mtmax - mtmin); |
573 |
value = MAX(MIN(value, stmax), 0); |
574 |
} |
575 |
return (value); |
220 |
} |
576 |
} |
221 |
|
577 |
|
222 |
void |
578 |
void |
223 |
evdev_push_nfingers(struct evdev_dev *evdev, int32_t nfingers) |
579 |
evdev_support_mt_compat(struct evdev_dev *evdev) |
224 |
{ |
580 |
{ |
|
|
581 |
struct input_absinfo *ai; |
582 |
int i; |
225 |
|
583 |
|
226 |
EVDEV_ENTER(evdev); |
584 |
if (evdev->ev_absinfo == NULL) |
227 |
evdev_send_nfingers(evdev, nfingers); |
585 |
return; |
228 |
EVDEV_EXIT(evdev); |
586 |
|
|
|
587 |
evdev_support_event(evdev, EV_KEY); |
588 |
evdev_support_key(evdev, BTN_TOUCH); |
589 |
|
590 |
/* Touchscreens should not advertise tap tool capabilities */ |
591 |
if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT)) |
592 |
evdev_support_nfingers(evdev, MAXIMAL_MT_SLOT(evdev) + 1); |
593 |
|
594 |
/* Echo 0-th MT-slot as ST-slot */ |
595 |
for (i = 0; i < nitems(evdev_mtstmap); i++) { |
596 |
if (!bit_test(evdev->ev_abs_flags, evdev_mtstmap[i].mt) || |
597 |
bit_test(evdev->ev_abs_flags, evdev_mtstmap[i].st)) |
598 |
continue; |
599 |
ai = evdev->ev_absinfo + evdev_mtstmap[i].mt; |
600 |
evdev->ev_mt->mtst_events |= 1U << i; |
601 |
if (evdev_mtstmap[i].max != 0) |
602 |
evdev_support_abs(evdev, evdev_mtstmap[i].st, |
603 |
0, |
604 |
evdev_mtstmap[i].max, |
605 |
0, |
606 |
evdev_mt_normalize( |
607 |
ai->flat, 0, ai->maximum, evdev_mtstmap[i].max), |
608 |
0); |
609 |
else |
610 |
evdev_support_abs(evdev, evdev_mtstmap[i].st, |
611 |
ai->minimum, |
612 |
ai->maximum, |
613 |
0, |
614 |
ai->flat, |
615 |
ai->resolution); |
616 |
} |
229 |
} |
617 |
} |
230 |
|
618 |
|
231 |
void |
619 |
static void |
232 |
evdev_send_mt_compat(struct evdev_dev *evdev) |
620 |
evdev_mt_send_st_compat(struct evdev_dev *evdev) |
233 |
{ |
621 |
{ |
234 |
int32_t nfingers, i; |
622 |
struct evdev_mt *mt = evdev->ev_mt; |
|
|
623 |
int nfingers, i, st_slot; |
235 |
|
624 |
|
236 |
EVDEV_LOCK_ASSERT(evdev); |
625 |
EVDEV_LOCK_ASSERT(evdev); |
237 |
|
626 |
|
238 |
nfingers = evdev_count_fingers(evdev); |
627 |
nfingers = bitcount(mt->touches); |
239 |
evdev_send_event(evdev, EV_KEY, BTN_TOUCH, nfingers > 0); |
628 |
evdev_send_event(evdev, EV_KEY, BTN_TOUCH, nfingers > 0); |
240 |
|
629 |
|
241 |
if (evdev_get_mt_value(evdev, 0, ABS_MT_TRACKING_ID) != -1) |
630 |
/* Send first active MT-slot state as single touch report */ |
242 |
/* Echo 0-th MT-slot as ST-slot */ |
631 |
st_slot = ffs(mt->touches) - 1; |
243 |
for (i = 0; i < nitems(evdev_mtstmap); i++) |
632 |
if (st_slot != -1) |
244 |
if (bit_test(evdev->ev_abs_flags, evdev_mtstmap[i][1])) |
633 |
FOREACHBIT(mt->mtst_events, i) |
245 |
evdev_send_event(evdev, EV_ABS, |
634 |
evdev_send_event(evdev, EV_ABS, evdev_mtstmap[i].st, |
246 |
evdev_mtstmap[i][1], |
635 |
evdev_mt_normalize(evdev_mt_get_value(evdev, |
247 |
evdev_get_mt_value(evdev, 0, |
636 |
st_slot, evdev_mtstmap[i].mt), |
248 |
evdev_mtstmap[i][0])); |
637 |
evdev->ev_absinfo[evdev_mtstmap[i].mt].minimum, |
|
|
638 |
evdev->ev_absinfo[evdev_mtstmap[i].mt].maximum, |
639 |
evdev_mtstmap[i].max)); |
249 |
|
640 |
|
250 |
/* Touchscreens should not report tool taps */ |
641 |
/* Touchscreens should not report tool taps */ |
251 |
if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT)) |
642 |
if (!bit_test(evdev->ev_prop_flags, INPUT_PROP_DIRECT)) |
Lines 260-283
evdev_push_mt_compat(struct evdev_dev *evdev)
Link Here
|
260 |
{ |
651 |
{ |
261 |
|
652 |
|
262 |
EVDEV_ENTER(evdev); |
653 |
EVDEV_ENTER(evdev); |
263 |
evdev_send_mt_compat(evdev); |
654 |
evdev_mt_send_st_compat(evdev); |
264 |
EVDEV_EXIT(evdev); |
655 |
EVDEV_EXIT(evdev); |
265 |
} |
656 |
} |
266 |
|
657 |
|
267 |
void |
658 |
static void |
268 |
evdev_send_mt_autorel(struct evdev_dev *evdev) |
659 |
evdev_mt_send_autorel(struct evdev_dev *evdev) |
269 |
{ |
660 |
{ |
270 |
int32_t slot; |
661 |
struct evdev_mt *mt = evdev->ev_mt; |
|
|
662 |
int slot; |
271 |
|
663 |
|
272 |
EVDEV_LOCK_ASSERT(evdev); |
664 |
EVDEV_LOCK_ASSERT(evdev); |
|
|
665 |
KASSERT(mt->match_frame == 0, ("Unmatched events exist")); |
273 |
|
666 |
|
274 |
for (slot = 0; slot <= MAXIMAL_MT_SLOT(evdev); slot++) { |
667 |
FOREACHBIT(mt->touches & ~mt->frame, slot) |
275 |
if (evdev->ev_mt->ev_mt_slots[slot].ev_report != |
668 |
evdev_mt_send_slot(evdev, slot, NULL); |
276 |
evdev->ev_report_count && |
669 |
} |
277 |
evdev_get_mt_value(evdev, slot, ABS_MT_TRACKING_ID) != -1){ |
670 |
|
278 |
evdev_send_event(evdev, EV_ABS, ABS_MT_SLOT, slot); |
671 |
void |
279 |
evdev_send_event(evdev, EV_ABS, ABS_MT_TRACKING_ID, |
672 |
evdev_mt_push_autorel(struct evdev_dev *evdev) |
280 |
-1); |
673 |
{ |
281 |
} |
674 |
EVDEV_ENTER(evdev); |
282 |
} |
675 |
evdev_mt_send_autorel(evdev); |
|
|
676 |
EVDEV_EXIT(evdev); |
283 |
} |
677 |
} |