input-mt.c 5.0 KB

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