input-mt.c 11 KB

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