input-mt.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /**
  15. * input_mt_init_slots() - initialize MT input slots
  16. * @dev: input device supporting MT events and finger tracking
  17. * @num_slots: number of slots used by the device
  18. *
  19. * This function allocates all necessary memory for MT slot handling
  20. * in the input device, prepares the ABS_MT_SLOT and
  21. * ABS_MT_TRACKING_ID events for use and sets up appropriate buffers.
  22. * May be called repeatedly. Returns -EINVAL if attempting to
  23. * reinitialize with a different number of slots.
  24. */
  25. int input_mt_init_slots(struct input_dev *dev, unsigned int num_slots,
  26. unsigned int flags)
  27. {
  28. struct input_mt *mt = dev->mt;
  29. int i;
  30. if (!num_slots)
  31. return 0;
  32. if (mt)
  33. return mt->num_slots != num_slots ? -EINVAL : 0;
  34. mt = kzalloc(sizeof(*mt) + num_slots * sizeof(*mt->slots), GFP_KERNEL);
  35. if (!mt)
  36. return -ENOMEM;
  37. mt->num_slots = num_slots;
  38. mt->flags = flags;
  39. input_set_abs_params(dev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0);
  40. input_set_abs_params(dev, ABS_MT_TRACKING_ID, 0, TRKID_MAX, 0, 0);
  41. /* Mark slots as 'unused' */
  42. for (i = 0; i < num_slots; i++)
  43. input_mt_set_value(&mt->slots[i], ABS_MT_TRACKING_ID, -1);
  44. dev->mt = mt;
  45. return 0;
  46. }
  47. EXPORT_SYMBOL(input_mt_init_slots);
  48. /**
  49. * input_mt_destroy_slots() - frees the MT slots of the input device
  50. * @dev: input device with allocated MT slots
  51. *
  52. * This function is only needed in error path as the input core will
  53. * automatically free the MT slots when the device is destroyed.
  54. */
  55. void input_mt_destroy_slots(struct input_dev *dev)
  56. {
  57. kfree(dev->mt);
  58. dev->mt = NULL;
  59. }
  60. EXPORT_SYMBOL(input_mt_destroy_slots);
  61. /**
  62. * input_mt_report_slot_state() - report contact state
  63. * @dev: input device with allocated MT slots
  64. * @tool_type: the tool type to use in this slot
  65. * @active: true if contact is active, false otherwise
  66. *
  67. * Reports a contact via ABS_MT_TRACKING_ID, and optionally
  68. * ABS_MT_TOOL_TYPE. If active is true and the slot is currently
  69. * inactive, or if the tool type is changed, a new tracking id is
  70. * assigned to the slot. The tool type is only reported if the
  71. * corresponding absbit field is set.
  72. */
  73. void input_mt_report_slot_state(struct input_dev *dev,
  74. unsigned int tool_type, bool active)
  75. {
  76. struct input_mt *mt = dev->mt;
  77. struct input_mt_slot *slot;
  78. int id;
  79. if (!mt || !active) {
  80. input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
  81. return;
  82. }
  83. slot = &mt->slots[mt->slot];
  84. id = input_mt_get_value(slot, ABS_MT_TRACKING_ID);
  85. if (id < 0 || input_mt_get_value(slot, ABS_MT_TOOL_TYPE) != tool_type)
  86. id = input_mt_new_trkid(mt);
  87. input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, id);
  88. input_event(dev, EV_ABS, ABS_MT_TOOL_TYPE, tool_type);
  89. }
  90. EXPORT_SYMBOL(input_mt_report_slot_state);
  91. /**
  92. * input_mt_report_finger_count() - report contact count
  93. * @dev: input device with allocated MT slots
  94. * @count: the number of contacts
  95. *
  96. * Reports the contact count via BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP,
  97. * BTN_TOOL_TRIPLETAP and BTN_TOOL_QUADTAP.
  98. *
  99. * The input core ensures only the KEY events already setup for
  100. * this device will produce output.
  101. */
  102. void input_mt_report_finger_count(struct input_dev *dev, int count)
  103. {
  104. input_event(dev, EV_KEY, BTN_TOOL_FINGER, count == 1);
  105. input_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, count == 2);
  106. input_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, count == 3);
  107. input_event(dev, EV_KEY, BTN_TOOL_QUADTAP, count == 4);
  108. input_event(dev, EV_KEY, BTN_TOOL_QUINTTAP, count == 5);
  109. }
  110. EXPORT_SYMBOL(input_mt_report_finger_count);
  111. /**
  112. * input_mt_report_pointer_emulation() - common pointer emulation
  113. * @dev: input device with allocated MT slots
  114. * @use_count: report number of active contacts as finger count
  115. *
  116. * Performs legacy pointer emulation via BTN_TOUCH, ABS_X, ABS_Y and
  117. * ABS_PRESSURE. Touchpad finger count is emulated if use_count is true.
  118. *
  119. * The input core ensures only the KEY and ABS axes already setup for
  120. * this device will produce output.
  121. */
  122. void input_mt_report_pointer_emulation(struct input_dev *dev, bool use_count)
  123. {
  124. struct input_mt *mt = dev->mt;
  125. struct input_mt_slot *oldest;
  126. int oldid, count, i;
  127. if (!mt)
  128. return;
  129. oldest = 0;
  130. oldid = mt->trkid;
  131. count = 0;
  132. for (i = 0; i < mt->num_slots; ++i) {
  133. struct input_mt_slot *ps = &mt->slots[i];
  134. int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
  135. if (id < 0)
  136. continue;
  137. if ((id - oldid) & TRKID_SGN) {
  138. oldest = ps;
  139. oldid = id;
  140. }
  141. count++;
  142. }
  143. input_event(dev, EV_KEY, BTN_TOUCH, count > 0);
  144. if (use_count)
  145. input_mt_report_finger_count(dev, count);
  146. if (oldest) {
  147. int x = input_mt_get_value(oldest, ABS_MT_POSITION_X);
  148. int y = input_mt_get_value(oldest, ABS_MT_POSITION_Y);
  149. int p = input_mt_get_value(oldest, ABS_MT_PRESSURE);
  150. input_event(dev, EV_ABS, ABS_X, x);
  151. input_event(dev, EV_ABS, ABS_Y, y);
  152. input_event(dev, EV_ABS, ABS_PRESSURE, p);
  153. } else {
  154. input_event(dev, EV_ABS, ABS_PRESSURE, 0);
  155. }
  156. }
  157. EXPORT_SYMBOL(input_mt_report_pointer_emulation);