2 * Input Multitouch Library
4 * Copyright (c) 2008-2010 Henrik Rydberg
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.
11 #include <linux/input/mt.h>
12 #include <linux/export.h>
13 #include <linux/slab.h>
15 #define TRKID_SGN ((TRKID_MAX + 1) >> 1)
17 static void copy_abs(struct input_dev
*dev
, unsigned int dst
, unsigned int src
)
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
);
26 * input_mt_init_slots() - initialize MT input slots
27 * @dev: input device supporting MT events and finger tracking
28 * @num_slots: number of slots used by the device
30 * This function allocates all necessary memory for MT slot handling
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.
36 int input_mt_init_slots(struct input_dev
*dev
, unsigned int num_slots
,
39 struct input_mt
*mt
= dev
->mt
;
45 return mt
->num_slots
!= num_slots
? -EINVAL
: 0;
47 mt
= kzalloc(sizeof(*mt
) + num_slots
* sizeof(*mt
->slots
), GFP_KERNEL
);
51 mt
->num_slots
= num_slots
;
53 input_set_abs_params(dev
, ABS_MT_SLOT
, 0, num_slots
- 1, 0, 0);
54 input_set_abs_params(dev
, ABS_MT_TRACKING_ID
, 0, TRKID_MAX
, 0, 0);
56 if (flags
& (INPUT_MT_POINTER
| INPUT_MT_DIRECT
)) {
57 __set_bit(EV_KEY
, dev
->evbit
);
58 __set_bit(BTN_TOUCH
, dev
->keybit
);
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
);
64 if (flags
& INPUT_MT_POINTER
) {
65 __set_bit(BTN_TOOL_FINGER
, dev
->keybit
);
66 __set_bit(BTN_TOOL_DOUBLETAP
, dev
->keybit
);
68 __set_bit(BTN_TOOL_TRIPLETAP
, dev
->keybit
);
70 __set_bit(BTN_TOOL_QUADTAP
, dev
->keybit
);
72 __set_bit(BTN_TOOL_QUINTTAP
, dev
->keybit
);
73 __set_bit(INPUT_PROP_POINTER
, dev
->propbit
);
75 if (flags
& INPUT_MT_DIRECT
)
76 __set_bit(INPUT_PROP_DIRECT
, dev
->propbit
);
77 if (flags
& INPUT_MT_TRACK
) {
78 unsigned int n2
= num_slots
* num_slots
;
79 mt
->red
= kcalloc(n2
, sizeof(*mt
->red
), GFP_KERNEL
);
84 /* Mark slots as 'unused' */
85 for (i
= 0; i
< num_slots
; i
++)
86 input_mt_set_value(&mt
->slots
[i
], ABS_MT_TRACKING_ID
, -1);
94 EXPORT_SYMBOL(input_mt_init_slots
);
97 * input_mt_destroy_slots() - frees the MT slots of the input device
98 * @dev: input device with allocated MT slots
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.
103 void input_mt_destroy_slots(struct input_dev
*dev
)
111 EXPORT_SYMBOL(input_mt_destroy_slots
);
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
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.
125 void input_mt_report_slot_state(struct input_dev
*dev
,
126 unsigned int tool_type
, bool active
)
128 struct input_mt
*mt
= dev
->mt
;
129 struct input_mt_slot
*slot
;
135 slot
= &mt
->slots
[mt
->slot
];
136 slot
->frame
= mt
->frame
;
139 input_event(dev
, EV_ABS
, ABS_MT_TRACKING_ID
, -1);
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
);
147 input_event(dev
, EV_ABS
, ABS_MT_TRACKING_ID
, id
);
148 input_event(dev
, EV_ABS
, ABS_MT_TOOL_TYPE
, tool_type
);
150 EXPORT_SYMBOL(input_mt_report_slot_state
);
153 * input_mt_report_finger_count() - report contact count
154 * @dev: input device with allocated MT slots
155 * @count: the number of contacts
157 * Reports the contact count via BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP,
158 * BTN_TOOL_TRIPLETAP and BTN_TOOL_QUADTAP.
160 * The input core ensures only the KEY events already setup for
161 * this device will produce output.
163 void input_mt_report_finger_count(struct input_dev
*dev
, int count
)
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);
169 input_event(dev
, EV_KEY
, BTN_TOOL_QUINTTAP
, count
== 5);
171 EXPORT_SYMBOL(input_mt_report_finger_count
);
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
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.
181 * The input core ensures only the KEY and ABS axes already setup for
182 * this device will produce output.
184 void input_mt_report_pointer_emulation(struct input_dev
*dev
, bool use_count
)
186 struct input_mt
*mt
= dev
->mt
;
187 struct input_mt_slot
*oldest
;
197 for (i
= 0; i
< mt
->num_slots
; ++i
) {
198 struct input_mt_slot
*ps
= &mt
->slots
[i
];
199 int id
= input_mt_get_value(ps
, ABS_MT_TRACKING_ID
);
203 if ((id
- oldid
) & TRKID_SGN
) {
210 input_event(dev
, EV_KEY
, BTN_TOUCH
, count
> 0);
212 input_mt_report_finger_count(dev
, count
);
215 int x
= input_mt_get_value(oldest
, ABS_MT_POSITION_X
);
216 int y
= input_mt_get_value(oldest
, ABS_MT_POSITION_Y
);
218 input_event(dev
, EV_ABS
, ABS_X
, x
);
219 input_event(dev
, EV_ABS
, ABS_Y
, y
);
221 if (test_bit(ABS_MT_PRESSURE
, dev
->absbit
)) {
222 int p
= input_mt_get_value(oldest
, ABS_MT_PRESSURE
);
223 input_event(dev
, EV_ABS
, ABS_PRESSURE
, p
);
226 if (test_bit(ABS_MT_PRESSURE
, dev
->absbit
))
227 input_event(dev
, EV_ABS
, ABS_PRESSURE
, 0);
230 EXPORT_SYMBOL(input_mt_report_pointer_emulation
);
233 * input_mt_sync_frame() - synchronize mt frame
234 * @dev: input device with allocated MT slots
236 * Close the frame and prepare the internal state for a new one.
237 * Depending on the flags, marks unused slots as inactive and performs
240 void input_mt_sync_frame(struct input_dev
*dev
)
242 struct input_mt
*mt
= dev
->mt
;
243 struct input_mt_slot
*s
;
248 if (mt
->flags
& INPUT_MT_DROP_UNUSED
) {
249 for (s
= mt
->slots
; s
!= mt
->slots
+ mt
->num_slots
; s
++) {
250 if (s
->frame
== mt
->frame
)
252 input_mt_slot(dev
, s
- mt
->slots
);
253 input_event(dev
, EV_ABS
, ABS_MT_TRACKING_ID
, -1);
257 input_mt_report_pointer_emulation(dev
, (mt
->flags
& INPUT_MT_POINTER
));
261 EXPORT_SYMBOL(input_mt_sync_frame
);
263 static int adjust_dual(int *begin
, int step
, int *end
, int eq
)
272 s
= p
== end
? f
+ 1 : *p
;
274 for (; p
!= end
; p
+= step
)
281 if (c
== 0 || (c
> 0 && !eq
))
286 for (p
= begin
; p
!= end
; p
+= step
)
289 return (c
< s
&& s
<= 0) || (f
>= 0 && f
< c
);
292 static void find_reduced_matrix(int *w
, int nr
, int nc
, int nrc
)
296 for (k
= 0; k
< nrc
; k
++) {
297 for (i
= 0; i
< nr
; i
++)
298 adjust_dual(w
+ i
, nr
, w
+ i
+ nrc
, nr
<= nc
);
300 for (i
= 0; i
< nrc
; i
+= nr
)
301 sum
+= adjust_dual(w
+ i
, 1, w
+ i
+ nr
, nc
<= nr
);
307 static int input_mt_set_matrix(struct input_mt
*mt
,
308 const struct input_mt_pos
*pos
, int num_pos
)
310 const struct input_mt_pos
*p
;
311 struct input_mt_slot
*s
;
315 for (s
= mt
->slots
; s
!= mt
->slots
+ mt
->num_slots
; s
++) {
316 if (!input_mt_is_active(s
))
318 x
= input_mt_get_value(s
, ABS_MT_POSITION_X
);
319 y
= input_mt_get_value(s
, ABS_MT_POSITION_Y
);
320 for (p
= pos
; p
!= pos
+ num_pos
; p
++) {
321 int dx
= x
- p
->x
, dy
= y
- p
->y
;
322 *w
++ = dx
* dx
+ dy
* dy
;
329 static void input_mt_set_slots(struct input_mt
*mt
,
330 int *slots
, int num_pos
)
332 struct input_mt_slot
*s
;
333 int *w
= mt
->red
, *p
;
335 for (p
= slots
; p
!= slots
+ num_pos
; p
++)
338 for (s
= mt
->slots
; s
!= mt
->slots
+ mt
->num_slots
; s
++) {
339 if (!input_mt_is_active(s
))
341 for (p
= slots
; p
!= slots
+ num_pos
; p
++)
346 for (s
= mt
->slots
; s
!= mt
->slots
+ mt
->num_slots
; s
++) {
347 if (input_mt_is_active(s
))
349 for (p
= slots
; p
!= slots
+ num_pos
; p
++)
358 * input_mt_assign_slots() - perform a best-match assignment
359 * @dev: input device with allocated MT slots
360 * @slots: the slot assignment to be filled
361 * @pos: the position array to match
362 * @num_pos: number of positions
364 * Performs a best match against the current contacts and returns
365 * the slot assignment list. New contacts are assigned to unused
368 * Returns zero on success, or negative error in case of failure.
370 int input_mt_assign_slots(struct input_dev
*dev
, int *slots
,
371 const struct input_mt_pos
*pos
, int num_pos
)
373 struct input_mt
*mt
= dev
->mt
;
378 if (num_pos
> mt
->num_slots
)
383 nrc
= input_mt_set_matrix(mt
, pos
, num_pos
);
384 find_reduced_matrix(mt
->red
, num_pos
, nrc
/ num_pos
, nrc
);
385 input_mt_set_slots(mt
, slots
, num_pos
);
389 EXPORT_SYMBOL(input_mt_assign_slots
);
392 * input_mt_get_slot_by_key() - return slot matching key
393 * @dev: input device with allocated MT slots
394 * @key: the key of the sought slot
396 * Returns the slot of the given key, if it exists, otherwise
397 * set the key on the first unused slot and return.
399 * If no available slot can be found, -1 is returned.
401 int input_mt_get_slot_by_key(struct input_dev
*dev
, int key
)
403 struct input_mt
*mt
= dev
->mt
;
404 struct input_mt_slot
*s
;
409 for (s
= mt
->slots
; s
!= mt
->slots
+ mt
->num_slots
; s
++)
410 if (input_mt_is_active(s
) && s
->key
== key
)
411 return s
- mt
->slots
;
413 for (s
= mt
->slots
; s
!= mt
->slots
+ mt
->num_slots
; s
++)
414 if (!input_mt_is_active(s
)) {
416 return s
- mt
->slots
;
421 EXPORT_SYMBOL(input_mt_get_slot_by_key
);