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