input-mt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. {
  27. int i;
  28. if (!num_slots)
  29. return 0;
  30. if (dev->mt)
  31. return dev->mtsize != num_slots ? -EINVAL : 0;
  32. dev->mt = kcalloc(num_slots, sizeof(struct input_mt_slot), GFP_KERNEL);
  33. if (!dev->mt)
  34. return -ENOMEM;
  35. dev->mtsize = num_slots;
  36. input_set_abs_params(dev, ABS_MT_SLOT, 0, num_slots - 1, 0, 0);
  37. input_set_abs_params(dev, ABS_MT_TRACKING_ID, 0, TRKID_MAX, 0, 0);
  38. input_set_events_per_packet(dev, 6 * num_slots);
  39. /* Mark slots as 'unused' */
  40. for (i = 0; i < num_slots; i++)
  41. input_mt_set_value(&dev->mt[i], ABS_MT_TRACKING_ID, -1);
  42. return 0;
  43. }
  44. EXPORT_SYMBOL(input_mt_init_slots);
  45. /**
  46. * input_mt_destroy_slots() - frees the MT slots of the input device
  47. * @dev: input device with allocated MT slots
  48. *
  49. * This function is only needed in error path as the input core will
  50. * automatically free the MT slots when the device is destroyed.
  51. */
  52. void input_mt_destroy_slots(struct input_dev *dev)
  53. {
  54. kfree(dev->mt);
  55. dev->mt = NULL;
  56. dev->mtsize = 0;
  57. dev->slot = 0;
  58. dev->trkid = 0;
  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_slot *mt;
  77. int id;
  78. if (!dev->mt || !active) {
  79. input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, -1);
  80. return;
  81. }
  82. mt = &dev->mt[dev->slot];
  83. id = input_mt_get_value(mt, ABS_MT_TRACKING_ID);
  84. if (id < 0 || input_mt_get_value(mt, ABS_MT_TOOL_TYPE) != tool_type)
  85. id = input_mt_new_trkid(dev);
  86. input_event(dev, EV_ABS, ABS_MT_TRACKING_ID, id);
  87. input_event(dev, EV_ABS, ABS_MT_TOOL_TYPE, tool_type);
  88. }
  89. EXPORT_SYMBOL(input_mt_report_slot_state);
  90. /**
  91. * input_mt_report_finger_count() - report contact count
  92. * @dev: input device with allocated MT slots
  93. * @count: the number of contacts
  94. *
  95. * Reports the contact count via BTN_TOOL_FINGER, BTN_TOOL_DOUBLETAP,
  96. * BTN_TOOL_TRIPLETAP and BTN_TOOL_QUADTAP.
  97. *
  98. * The input core ensures only the KEY events already setup for
  99. * this device will produce output.
  100. */
  101. void input_mt_report_finger_count(struct input_dev *dev, int count)
  102. {
  103. input_event(dev, EV_KEY, BTN_TOOL_FINGER, count == 1);
  104. input_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, count == 2);
  105. input_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, count == 3);
  106. input_event(dev, EV_KEY, BTN_TOOL_QUADTAP, count == 4);
  107. input_event(dev, EV_KEY, BTN_TOOL_QUINTTAP, count == 5);
  108. }
  109. EXPORT_SYMBOL(input_mt_report_finger_count);
  110. /**
  111. * input_mt_report_pointer_emulation() - common pointer emulation
  112. * @dev: input device with allocated MT slots
  113. * @use_count: report number of active contacts as finger count
  114. *
  115. * Performs legacy pointer emulation via BTN_TOUCH, ABS_X, ABS_Y and
  116. * ABS_PRESSURE. Touchpad finger count is emulated if use_count is true.
  117. *
  118. * The input core ensures only the KEY and ABS axes already setup for
  119. * this device will produce output.
  120. */
  121. void input_mt_report_pointer_emulation(struct input_dev *dev, bool use_count)
  122. {
  123. struct input_mt_slot *oldest = 0;
  124. int oldid = dev->trkid;
  125. int count = 0;
  126. int i;
  127. for (i = 0; i < dev->mtsize; ++i) {
  128. struct input_mt_slot *ps = &dev->mt[i];
  129. int id = input_mt_get_value(ps, ABS_MT_TRACKING_ID);
  130. if (id < 0)
  131. continue;
  132. if ((id - oldid) & TRKID_SGN) {
  133. oldest = ps;
  134. oldid = id;
  135. }
  136. count++;
  137. }
  138. input_event(dev, EV_KEY, BTN_TOUCH, count > 0);
  139. if (use_count)
  140. input_mt_report_finger_count(dev, count);
  141. if (oldest) {
  142. int x = input_mt_get_value(oldest, ABS_MT_POSITION_X);
  143. int y = input_mt_get_value(oldest, ABS_MT_POSITION_Y);
  144. int p = input_mt_get_value(oldest, ABS_MT_PRESSURE);
  145. input_event(dev, EV_ABS, ABS_X, x);
  146. input_event(dev, EV_ABS, ABS_Y, y);
  147. input_event(dev, EV_ABS, ABS_PRESSURE, p);
  148. } else {
  149. input_event(dev, EV_ABS, ABS_PRESSURE, 0);
  150. }
  151. }
  152. EXPORT_SYMBOL(input_mt_report_pointer_emulation);