input-mt.c 12 KB

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