Commit | Line | Data |
---|---|---|
47c78e89 HR |
1 | /* |
2 | * Input Multitouch Library | |
3 | * | |
4 | * Copyright (c) 2008-2010 Henrik Rydberg | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License version 2 as published by | |
8 | * the Free Software Foundation. | |
9 | */ | |
10 | ||
11 | #include <linux/input/mt.h> | |
15d0580f | 12 | #include <linux/export.h> |
47c78e89 HR |
13 | #include <linux/slab.h> |
14 | ||
c5f4dec1 HR |
15 | #define TRKID_SGN ((TRKID_MAX + 1) >> 1) |
16 | ||
55e49089 HR |
17 | static void copy_abs(struct input_dev *dev, unsigned int dst, unsigned int src) |
18 | { | |
19 | if (dev->absinfo && test_bit(src, dev->absbit)) { | |
20 | dev->absinfo[dst] = dev->absinfo[src]; | |
21 | dev->absbit[BIT_WORD(dst)] |= BIT_MASK(dst); | |
22 | } | |
23 | } | |
24 | ||
47c78e89 | 25 | /** |
8cde8100 | 26 | * input_mt_init_slots() - initialize MT input slots |
47c78e89 HR |
27 | * @dev: input device supporting MT events and finger tracking |
28 | * @num_slots: number of slots used by the device | |
29 | * | |
8cde8100 | 30 | * This function allocates all necessary memory for MT slot handling |
c5f4dec1 HR |
31 | * in the input device, prepares the ABS_MT_SLOT and |
32 | * ABS_MT_TRACKING_ID events for use and sets up appropriate buffers. | |
33 | * May be called repeatedly. Returns -EINVAL if attempting to | |
34 | * reinitialize with a different number of slots. | |
47c78e89 | 35 | */ |
b4adbbef HR |
36 | int input_mt_init_slots(struct input_dev *dev, unsigned int num_slots, |
37 | unsigned int flags) | |
47c78e89 | 38 | { |
8d18fba2 | 39 | struct input_mt *mt = dev->mt; |
47c78e89 HR |
40 | int i; |
41 | ||
42 | if (!num_slots) | |
43 | return 0; | |
8d18fba2 HR |
44 | if (mt) |
45 | return mt->num_slots != num_slots ? -EINVAL : 0; | |
47c78e89 | 46 | |
8d18fba2 HR |
47 | mt = kzalloc(sizeof(*mt) + num_slots * sizeof(*mt->slots), GFP_KERNEL); |
48 | if (!mt) | |
7c1a8789 | 49 | goto err_mem; |
47c78e89 | 50 | |
8d18fba2 | 51 | mt->num_slots = num_slots; |
b4adbbef | 52 | mt->flags = flags; |
47c78e89 | 53 | input_set_abs_params(dev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0); |
c5f4dec1 | 54 | input_set_abs_params(dev, ABS_MT_TRACKING_ID, 0, TRKID_MAX, 0, 0); |
47c78e89 | 55 | |
55e49089 HR |
56 | if (flags & (INPUT_MT_POINTER | INPUT_MT_DIRECT)) { |
57 | __set_bit(EV_KEY, dev->evbit); | |
58 | __set_bit(BTN_TOUCH, dev->keybit); | |
59 | ||
60 | copy_abs(dev, ABS_X, ABS_MT_POSITION_X); | |
61 | copy_abs(dev, ABS_Y, ABS_MT_POSITION_Y); | |
62 | copy_abs(dev, ABS_PRESSURE, ABS_MT_PRESSURE); | |
63 | } | |
64 | if (flags & INPUT_MT_POINTER) { | |
65 | __set_bit(BTN_TOOL_FINGER, dev->keybit); | |
66 | __set_bit(BTN_TOOL_DOUBLETAP, dev->keybit); | |
67 | if (num_slots >= 3) | |
68 | __set_bit(BTN_TOOL_TRIPLETAP, dev->keybit); | |
69 | if (num_slots >= 4) | |
70 | __set_bit(BTN_TOOL_QUADTAP, dev->keybit); | |
71 | if (num_slots >= 5) | |
72 | __set_bit(BTN_TOOL_QUINTTAP, dev->keybit); | |
73 | __set_bit(INPUT_PROP_POINTER, dev->propbit); | |
74 | } | |
75 | if (flags & INPUT_MT_DIRECT) | |
76 | __set_bit(INPUT_PROP_DIRECT, dev->propbit); | |
7c1a8789 HR |
77 | if (flags & INPUT_MT_TRACK) { |
78 | unsigned int n2 = num_slots * num_slots; | |
79 | mt->red = kcalloc(n2, sizeof(*mt->red), GFP_KERNEL); | |
80 | if (!mt->red) | |
81 | goto err_mem; | |
82 | } | |
55e49089 | 83 | |
47c78e89 HR |
84 | /* Mark slots as 'unused' */ |
85 | for (i = 0; i < num_slots; i++) | |
8d18fba2 | 86 | input_mt_set_value(&mt->slots[i], ABS_MT_TRACKING_ID, -1); |
47c78e89 | 87 | |
8d18fba2 | 88 | dev->mt = mt; |
47c78e89 | 89 | return 0; |
7c1a8789 HR |
90 | err_mem: |
91 | kfree(mt); | |
92 | return -ENOMEM; | |
47c78e89 | 93 | } |
8cde8100 | 94 | EXPORT_SYMBOL(input_mt_init_slots); |
47c78e89 HR |
95 | |
96 | /** | |
97 | * input_mt_destroy_slots() - frees the MT slots of the input device | |
98 | * @dev: input device with allocated MT slots | |
99 | * | |
100 | * This function is only needed in error path as the input core will | |
101 | * automatically free the MT slots when the device is destroyed. | |
102 | */ | |
103 | void input_mt_destroy_slots(struct input_dev *dev) | |
104 | { | |
7c1a8789 HR |
105 | if (dev->mt) { |
106 | kfree(dev->mt->red); | |
107 | kfree(dev->mt); | |
108 | } | |
47c78e89 | 109 | dev->mt = NULL; |
47c78e89 HR |
110 | } |
111 | EXPORT_SYMBOL(input_mt_destroy_slots); | |
c5f4dec1 HR |
112 | |
113 | /** | |
114 | * input_mt_report_slot_state() - report contact state | |
115 | * @dev: input device with allocated MT slots | |
116 | * @tool_type: the tool type to use in this slot | |
117 | * @active: true if contact is active, false otherwise | |
118 | * | |
119 | * Reports a contact via ABS_MT_TRACKING_ID, and optionally | |
120 | * ABS_MT_TOOL_TYPE. If active is true and the slot is currently | |
121 | * inactive, or if the tool type is changed, a new tracking id is | |
122 | * assigned to the slot. The tool type is only reported if the | |
123 | * corresponding absbit field is set. | |
124 | */ | |
125 | void input_mt_report_slot_state(struct input_dev *dev, | |
126 | unsigned int tool_type, bool active) | |
127 | { | |
8d18fba2 HR |
128 | struct input_mt *mt = dev->mt; |
129 | struct input_mt_slot *slot; | |
c5f4dec1 HR |
130 | int id; |
131 | ||
55e49089 HR |
132 | if (!mt) |
133 | return; | |
134 | ||
135 | slot = &mt->slots[mt->slot]; | |
136 | slot->frame = mt->frame; | |
137 | ||
138 | if (!active) { | |
c5f4dec1 HR |
139 | input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1); |
140 | return; | |
141 | } | |
142 | ||
8d18fba2 HR |
143 | id = input_mt_get_value(slot, ABS_MT_TRACKING_ID); |
144 | if (id < 0 || input_mt_get_value(slot, ABS_MT_TOOL_TYPE) != tool_type) | |
145 | id = input_mt_new_trkid(mt); | |
c5f4dec1 HR |
146 | |
147 | input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, id); | |
148 | input_event(dev, EV_ABS, ABS_MT_TOOL_TYPE, tool_type); | |
149 | } | |
150 | EXPORT_SYMBOL(input_mt_report_slot_state); | |
151 | ||
152 | /** | |
153 | * input_mt_report_finger_count() - report contact count | |
154 | * @dev: input device with allocated MT slots | |
155 | * @count: the number of contacts | |
156 | * | |
157 | * Reports the contact count via BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP, | |
158 | * BTN_TOOL_TRIPLETAP and BTN_TOOL_QUADTAP. | |
159 | * | |
160 | * The input core ensures only the KEY events already setup for | |
161 | * this device will produce output. | |
162 | */ | |
163 | void input_mt_report_finger_count(struct input_dev *dev, int count) | |
164 | { | |
165 | input_event(dev, EV_KEY, BTN_TOOL_FINGER, count == 1); | |
166 | input_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, count == 2); | |
167 | input_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, count == 3); | |
168 | input_event(dev, EV_KEY, BTN_TOOL_QUADTAP, count == 4); | |
d5051272 | 169 | input_event(dev, EV_KEY, BTN_TOOL_QUINTTAP, count == 5); |
c5f4dec1 HR |
170 | } |
171 | EXPORT_SYMBOL(input_mt_report_finger_count); | |
172 | ||
173 | /** | |
174 | * input_mt_report_pointer_emulation() - common pointer emulation | |
175 | * @dev: input device with allocated MT slots | |
176 | * @use_count: report number of active contacts as finger count | |
177 | * | |
178 | * Performs legacy pointer emulation via BTN_TOUCH, ABS_X, ABS_Y and | |
179 | * ABS_PRESSURE. Touchpad finger count is emulated if use_count is true. | |
180 | * | |
181 | * The input core ensures only the KEY and ABS axes already setup for | |
182 | * this device will produce output. | |
183 | */ | |
184 | void input_mt_report_pointer_emulation(struct input_dev *dev, bool use_count) | |
185 | { | |
8d18fba2 HR |
186 | struct input_mt *mt = dev->mt; |
187 | struct input_mt_slot *oldest; | |
188 | int oldid, count, i; | |
189 | ||
190 | if (!mt) | |
191 | return; | |
192 | ||
193 | oldest = 0; | |
194 | oldid = mt->trkid; | |
195 | count = 0; | |
c5f4dec1 | 196 | |
8d18fba2 HR |
197 | for (i = 0; i < mt->num_slots; ++i) { |
198 | struct input_mt_slot *ps = &mt->slots[i]; | |
c5f4dec1 HR |
199 | int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID); |
200 | ||
201 | if (id < 0) | |
202 | continue; | |
203 | if ((id - oldid) & TRKID_SGN) { | |
204 | oldest = ps; | |
205 | oldid = id; | |
206 | } | |
207 | count++; | |
208 | } | |
209 | ||
210 | input_event(dev, EV_KEY, BTN_TOUCH, count > 0); | |
211 | if (use_count) | |
212 | input_mt_report_finger_count(dev, count); | |
213 | ||
214 | if (oldest) { | |
215 | int x = input_mt_get_value(oldest, ABS_MT_POSITION_X); | |
216 | int y = input_mt_get_value(oldest, ABS_MT_POSITION_Y); | |
217 | int p = input_mt_get_value(oldest, ABS_MT_PRESSURE); | |
218 | ||
219 | input_event(dev, EV_ABS, ABS_X, x); | |
220 | input_event(dev, EV_ABS, ABS_Y, y); | |
221 | input_event(dev, EV_ABS, ABS_PRESSURE, p); | |
222 | } else { | |
223 | input_event(dev, EV_ABS, ABS_PRESSURE, 0); | |
224 | } | |
225 | } | |
226 | EXPORT_SYMBOL(input_mt_report_pointer_emulation); | |
55e49089 HR |
227 | |
228 | /** | |
229 | * input_mt_sync_frame() - synchronize mt frame | |
230 | * @dev: input device with allocated MT slots | |
231 | * | |
232 | * Close the frame and prepare the internal state for a new one. | |
233 | * Depending on the flags, marks unused slots as inactive and performs | |
234 | * pointer emulation. | |
235 | */ | |
236 | void input_mt_sync_frame(struct input_dev *dev) | |
237 | { | |
238 | struct input_mt *mt = dev->mt; | |
239 | struct input_mt_slot *s; | |
240 | ||
241 | if (!mt) | |
242 | return; | |
243 | ||
244 | if (mt->flags & INPUT_MT_DROP_UNUSED) { | |
245 | for (s = mt->slots; s != mt->slots + mt->num_slots; s++) { | |
246 | if (s->frame == mt->frame) | |
247 | continue; | |
248 | input_mt_slot(dev, s - mt->slots); | |
249 | input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1); | |
250 | } | |
251 | } | |
252 | ||
253 | input_mt_report_pointer_emulation(dev, (mt->flags & INPUT_MT_POINTER)); | |
254 | ||
255 | mt->frame++; | |
256 | } | |
257 | EXPORT_SYMBOL(input_mt_sync_frame); | |
7c1a8789 HR |
258 | |
259 | static int adjust_dual(int *begin, int step, int *end, int eq) | |
260 | { | |
261 | int f, *p, s, c; | |
262 | ||
263 | if (begin == end) | |
264 | return 0; | |
265 | ||
266 | f = *begin; | |
267 | p = begin + step; | |
268 | s = p == end ? f + 1 : *p; | |
269 | ||
270 | for (; p != end; p += step) | |
271 | if (*p < f) | |
272 | s = f, f = *p; | |
273 | else if (*p < s) | |
274 | s = *p; | |
275 | ||
276 | c = (f + s + 1) / 2; | |
277 | if (c == 0 || (c > 0 && !eq)) | |
278 | return 0; | |
279 | if (s < 0) | |
280 | c *= 2; | |
281 | ||
282 | for (p = begin; p != end; p += step) | |
283 | *p -= c; | |
284 | ||
285 | return (c < s && s <= 0) || (f >= 0 && f < c); | |
286 | } | |
287 | ||
288 | static void find_reduced_matrix(int *w, int nr, int nc, int nrc) | |
289 | { | |
290 | int i, k, sum; | |
291 | ||
292 | for (k = 0; k < nrc; k++) { | |
293 | for (i = 0; i < nr; i++) | |
294 | adjust_dual(w + i, nr, w + i + nrc, nr <= nc); | |
295 | sum = 0; | |
296 | for (i = 0; i < nrc; i += nr) | |
297 | sum += adjust_dual(w + i, 1, w + i + nr, nc <= nr); | |
298 | if (!sum) | |
299 | break; | |
300 | } | |
301 | } | |
302 | ||
303 | static int input_mt_set_matrix(struct input_mt *mt, | |
304 | const struct input_mt_pos *pos, int num_pos) | |
305 | { | |
306 | const struct input_mt_pos *p; | |
307 | struct input_mt_slot *s; | |
308 | int *w = mt->red; | |
309 | int x, y; | |
310 | ||
311 | for (s = mt->slots; s != mt->slots + mt->num_slots; s++) { | |
312 | if (!input_mt_is_active(s)) | |
313 | continue; | |
314 | x = input_mt_get_value(s, ABS_MT_POSITION_X); | |
315 | y = input_mt_get_value(s, ABS_MT_POSITION_Y); | |
316 | for (p = pos; p != pos + num_pos; p++) { | |
317 | int dx = x - p->x, dy = y - p->y; | |
318 | *w++ = dx * dx + dy * dy; | |
319 | } | |
320 | } | |
321 | ||
322 | return w - mt->red; | |
323 | } | |
324 | ||
325 | static void input_mt_set_slots(struct input_mt *mt, | |
326 | int *slots, int num_pos) | |
327 | { | |
328 | struct input_mt_slot *s; | |
329 | int *w = mt->red, *p; | |
330 | ||
331 | for (p = slots; p != slots + num_pos; p++) | |
332 | *p = -1; | |
333 | ||
334 | for (s = mt->slots; s != mt->slots + mt->num_slots; s++) { | |
335 | if (!input_mt_is_active(s)) | |
336 | continue; | |
337 | for (p = slots; p != slots + num_pos; p++) | |
338 | if (*w++ < 0) | |
339 | *p = s - mt->slots; | |
340 | } | |
341 | ||
342 | for (s = mt->slots; s != mt->slots + mt->num_slots; s++) { | |
343 | if (input_mt_is_active(s)) | |
344 | continue; | |
345 | for (p = slots; p != slots + num_pos; p++) | |
346 | if (*p < 0) { | |
347 | *p = s - mt->slots; | |
348 | break; | |
349 | } | |
350 | } | |
351 | } | |
352 | ||
353 | /** | |
354 | * input_mt_assign_slots() - perform a best-match assignment | |
355 | * @dev: input device with allocated MT slots | |
356 | * @slots: the slot assignment to be filled | |
357 | * @pos: the position array to match | |
358 | * @num_pos: number of positions | |
359 | * | |
360 | * Performs a best match against the current contacts and returns | |
361 | * the slot assignment list. New contacts are assigned to unused | |
362 | * slots. | |
363 | * | |
364 | * Returns zero on success, or negative error in case of failure. | |
365 | */ | |
366 | int input_mt_assign_slots(struct input_dev *dev, int *slots, | |
367 | const struct input_mt_pos *pos, int num_pos) | |
368 | { | |
369 | struct input_mt *mt = dev->mt; | |
370 | int nrc; | |
371 | ||
372 | if (!mt || !mt->red) | |
373 | return -ENXIO; | |
374 | if (num_pos > mt->num_slots) | |
375 | return -EINVAL; | |
376 | if (num_pos < 1) | |
377 | return 0; | |
378 | ||
379 | nrc = input_mt_set_matrix(mt, pos, num_pos); | |
380 | find_reduced_matrix(mt->red, num_pos, nrc / num_pos, nrc); | |
381 | input_mt_set_slots(mt, slots, num_pos); | |
382 | ||
383 | return 0; | |
384 | } | |
385 | EXPORT_SYMBOL(input_mt_assign_slots); |