input-mt.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. return -ENOMEM;
  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. /* Mark slots as 'unused' */
  69. for (i = 0; i < num_slots; i++)
  70. input_mt_set_value(&mt->slots[i], ABS_MT_TRACKING_ID, -1);
  71. dev->mt = mt;
  72. return 0;
  73. }
  74. EXPORT_SYMBOL(input_mt_init_slots);
  75. /**
  76. * input_mt_destroy_slots() - frees the MT slots of the input device
  77. * @dev: input device with allocated MT slots
  78. *
  79. * This function is only needed in error path as the input core will
  80. * automatically free the MT slots when the device is destroyed.
  81. */
  82. void input_mt_destroy_slots(struct input_dev *dev)
  83. {
  84. kfree(dev->mt);
  85. dev->mt = NULL;
  86. }
  87. EXPORT_SYMBOL(input_mt_destroy_slots);
  88. /**
  89. * input_mt_report_slot_state() - report contact state
  90. * @dev: input device with allocated MT slots
  91. * @tool_type: the tool type to use in this slot
  92. * @active: true if contact is active, false otherwise
  93. *
  94. * Reports a contact via ABS_MT_TRACKING_ID, and optionally
  95. * ABS_MT_TOOL_TYPE. If active is true and the slot is currently
  96. * inactive, or if the tool type is changed, a new tracking id is
  97. * assigned to the slot. The tool type is only reported if the
  98. * corresponding absbit field is set.
  99. */
  100. void input_mt_report_slot_state(struct input_dev *dev,
  101. unsigned int tool_type, bool active)
  102. {
  103. struct input_mt *mt = dev->mt;
  104. struct input_mt_slot *slot;
  105. int id;
  106. if (!mt)
  107. return;
  108. slot = &mt->slots[mt->slot];
  109. slot->frame = mt->frame;
  110. if (!active) {
  111. input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
  112. return;
  113. }
  114. id = input_mt_get_value(slot, ABS_MT_TRACKING_ID);
  115. if (id < 0 || input_mt_get_value(slot, ABS_MT_TOOL_TYPE) != tool_type)
  116. id = input_mt_new_trkid(mt);
  117. input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, id);
  118. input_event(dev, EV_ABS, ABS_MT_TOOL_TYPE, tool_type);
  119. }
  120. EXPORT_SYMBOL(input_mt_report_slot_state);
  121. /**
  122. * input_mt_report_finger_count() - report contact count
  123. * @dev: input device with allocated MT slots
  124. * @count: the number of contacts
  125. *
  126. * Reports the contact count via BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP,
  127. * BTN_TOOL_TRIPLETAP and BTN_TOOL_QUADTAP.
  128. *
  129. * The input core ensures only the KEY events already setup for
  130. * this device will produce output.
  131. */
  132. void input_mt_report_finger_count(struct input_dev *dev, int count)
  133. {
  134. input_event(dev, EV_KEY, BTN_TOOL_FINGER, count == 1);
  135. input_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, count == 2);
  136. input_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, count == 3);
  137. input_event(dev, EV_KEY, BTN_TOOL_QUADTAP, count == 4);
  138. input_event(dev, EV_KEY, BTN_TOOL_QUINTTAP, count == 5);
  139. }
  140. EXPORT_SYMBOL(input_mt_report_finger_count);
  141. /**
  142. * input_mt_report_pointer_emulation() - common pointer emulation
  143. * @dev: input device with allocated MT slots
  144. * @use_count: report number of active contacts as finger count
  145. *
  146. * Performs legacy pointer emulation via BTN_TOUCH, ABS_X, ABS_Y and
  147. * ABS_PRESSURE. Touchpad finger count is emulated if use_count is true.
  148. *
  149. * The input core ensures only the KEY and ABS axes already setup for
  150. * this device will produce output.
  151. */
  152. void input_mt_report_pointer_emulation(struct input_dev *dev, bool use_count)
  153. {
  154. struct input_mt *mt = dev->mt;
  155. struct input_mt_slot *oldest;
  156. int oldid, count, i;
  157. if (!mt)
  158. return;
  159. oldest = 0;
  160. oldid = mt->trkid;
  161. count = 0;
  162. for (i = 0; i < mt->num_slots; ++i) {
  163. struct input_mt_slot *ps = &mt->slots[i];
  164. int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
  165. if (id < 0)
  166. continue;
  167. if ((id - oldid) & TRKID_SGN) {
  168. oldest = ps;
  169. oldid = id;
  170. }
  171. count++;
  172. }
  173. input_event(dev, EV_KEY, BTN_TOUCH, count > 0);
  174. if (use_count)
  175. input_mt_report_finger_count(dev, count);
  176. if (oldest) {
  177. int x = input_mt_get_value(oldest, ABS_MT_POSITION_X);
  178. int y = input_mt_get_value(oldest, ABS_MT_POSITION_Y);
  179. int p = input_mt_get_value(oldest, ABS_MT_PRESSURE);
  180. input_event(dev, EV_ABS, ABS_X, x);
  181. input_event(dev, EV_ABS, ABS_Y, y);
  182. input_event(dev, EV_ABS, ABS_PRESSURE, p);
  183. } else {
  184. input_event(dev, EV_ABS, ABS_PRESSURE, 0);
  185. }
  186. }
  187. EXPORT_SYMBOL(input_mt_report_pointer_emulation);
  188. /**
  189. * input_mt_sync_frame() - synchronize mt frame
  190. * @dev: input device with allocated MT slots
  191. *
  192. * Close the frame and prepare the internal state for a new one.
  193. * Depending on the flags, marks unused slots as inactive and performs
  194. * pointer emulation.
  195. */
  196. void input_mt_sync_frame(struct input_dev *dev)
  197. {
  198. struct input_mt *mt = dev->mt;
  199. struct input_mt_slot *s;
  200. if (!mt)
  201. return;
  202. if (mt->flags & INPUT_MT_DROP_UNUSED) {
  203. for (s = mt->slots; s != mt->slots + mt->num_slots; s++) {
  204. if (s->frame == mt->frame)
  205. continue;
  206. input_mt_slot(dev, s - mt->slots);
  207. input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
  208. }
  209. }
  210. input_mt_report_pointer_emulation(dev, (mt->flags & INPUT_MT_POINTER));
  211. mt->frame++;
  212. }
  213. EXPORT_SYMBOL(input_mt_sync_frame);