litest-device-protocol-a-touch-screen.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright © 2015 Red Hat, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "config.h"
  24. #include "litest-int.h"
  25. #include "litest.h"
  26. #define PROTOCOL_A_MAX_SLOTS 10
  27. struct protocolA_device {
  28. struct slot {
  29. bool active;
  30. int x, y;
  31. int tracking_id;
  32. } slots[PROTOCOL_A_MAX_SLOTS];
  33. unsigned int nslots;
  34. };
  35. static bool
  36. protocolA_create(struct litest_device *d)
  37. {
  38. struct protocolA_device *dev = zalloc(sizeof(*dev));
  39. dev->nslots = PROTOCOL_A_MAX_SLOTS;
  40. d->private = dev;
  41. return true; /* we want litest to create our device */
  42. }
  43. static bool
  44. protocolA_down(struct litest_device *d, unsigned int slot, double x, double y)
  45. {
  46. struct protocolA_device *dev = d->private;
  47. static int tracking_id;
  48. bool first = true;
  49. assert(slot <= PROTOCOL_A_MAX_SLOTS);
  50. x = litest_scale(d, ABS_X, x);
  51. y = litest_scale(d, ABS_Y, y);
  52. for (unsigned int i = 0; i < dev->nslots; i++) {
  53. struct slot *s = &dev->slots[i];
  54. if (slot == i) {
  55. assert(!s->active);
  56. s->active = true;
  57. s->x = x;
  58. s->y = y;
  59. s->tracking_id = ++tracking_id;
  60. }
  61. if (!s->active)
  62. continue;
  63. if (first) {
  64. litest_event(d, EV_ABS, ABS_X, s->x);
  65. litest_event(d, EV_ABS, ABS_Y, s->y);
  66. first = false;
  67. }
  68. litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, s->tracking_id);
  69. litest_event(d, EV_ABS, ABS_MT_POSITION_X, s->x);
  70. litest_event(d, EV_ABS, ABS_MT_POSITION_Y, s->y);
  71. litest_event(d, EV_SYN, SYN_MT_REPORT, 0);
  72. }
  73. if (!first) {
  74. litest_event(d, EV_KEY, BTN_TOUCH, 1);
  75. litest_event(d, EV_SYN, SYN_REPORT, 0);
  76. }
  77. return true; /* we handled the event */
  78. }
  79. static bool
  80. protocolA_move(struct litest_device *d, unsigned int slot, double x, double y)
  81. {
  82. struct protocolA_device *dev = d->private;
  83. bool first = true;
  84. assert(slot <= PROTOCOL_A_MAX_SLOTS);
  85. x = litest_scale(d, ABS_X, x);
  86. y = litest_scale(d, ABS_Y, y);
  87. for (unsigned int i = 0; i < dev->nslots; i++) {
  88. struct slot *s = &dev->slots[i];
  89. if (slot == i) {
  90. assert(s->active);
  91. s->x = x;
  92. s->y = y;
  93. }
  94. if (!s->active)
  95. continue;
  96. if (first) {
  97. litest_event(d, EV_ABS, ABS_X, s->x);
  98. litest_event(d, EV_ABS, ABS_X, s->y);
  99. first = false;
  100. }
  101. litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, s->tracking_id);
  102. litest_event(d, EV_ABS, ABS_MT_POSITION_X, s->x);
  103. litest_event(d, EV_ABS, ABS_MT_POSITION_Y, s->y);
  104. litest_event(d, EV_SYN, SYN_MT_REPORT, 0);
  105. }
  106. if (!first)
  107. litest_event(d, EV_SYN, SYN_REPORT, 0);
  108. return true; /* we handled the event */
  109. }
  110. static bool
  111. protocolA_up(struct litest_device *d, unsigned int slot)
  112. {
  113. struct protocolA_device *dev = d->private;
  114. bool first = true;
  115. assert(slot <= PROTOCOL_A_MAX_SLOTS);
  116. for (unsigned int i = 0; i < dev->nslots; i++) {
  117. struct slot *s = &dev->slots[i];
  118. if (slot == i) {
  119. assert(s->active);
  120. s->active = false;
  121. litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, s->tracking_id);
  122. litest_event(d, EV_SYN, SYN_MT_REPORT, 0);
  123. }
  124. if (!s->active)
  125. continue;
  126. if (first) {
  127. litest_event(d, EV_ABS, ABS_X, s->x);
  128. litest_event(d, EV_ABS, ABS_X, s->y);
  129. first = false;
  130. }
  131. litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, s->tracking_id);
  132. litest_event(d, EV_ABS, ABS_MT_POSITION_X, s->x);
  133. litest_event(d, EV_ABS, ABS_MT_POSITION_Y, s->y);
  134. litest_event(d, EV_SYN, SYN_MT_REPORT, 0);
  135. }
  136. if (first)
  137. litest_event(d, EV_KEY, BTN_TOUCH, 0);
  138. litest_event(d, EV_SYN, SYN_REPORT, 0);
  139. return true; /* we handled the event */
  140. }
  141. static struct litest_device_interface interface = {
  142. .touch_down = protocolA_down,
  143. .touch_move = protocolA_move,
  144. .touch_up = protocolA_up,
  145. };
  146. /* clang-format off */
  147. static struct input_absinfo absinfo[] = {
  148. { ABS_X, 0, 32767, 0, 0, 0 },
  149. { ABS_Y, 0, 32767, 0, 0, 0 },
  150. { ABS_MT_POSITION_X, 0, 32767, 0, 0, 0 },
  151. { ABS_MT_POSITION_Y, 0, 32767, 0, 0, 0 },
  152. { ABS_MT_PRESSURE, 0, 1, 0, 0, 0 },
  153. { .value = -1 },
  154. };
  155. /* clang-format on */
  156. static struct input_id input_id = {
  157. .bustype = 0x18,
  158. .vendor = 0xeef,
  159. .product = 0x20,
  160. };
  161. /* clang-format off */
  162. static int events[] = {
  163. EV_KEY, BTN_TOUCH,
  164. INPUT_PROP_MAX, INPUT_PROP_DIRECT,
  165. -1, -1,
  166. };
  167. /* clang-format on */
  168. TEST_DEVICE(LITEST_PROTOCOL_A_SCREEN,
  169. .features = LITEST_PROTOCOL_A | LITEST_TOUCH,
  170. .create = protocolA_create,
  171. .interface = &interface,
  172. .name = "Protocol A touch screen",
  173. .id = &input_id,
  174. .events = events,
  175. .absinfo = absinfo,
  176. .udev_properties = {
  177. { "ID_INTEGRATION", "internal" },
  178. { NULL },
  179. }, )