touch.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**
  2. * Copyright © 2011 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. #ifdef HAVE_DIX_CONFIG_H
  24. #include <dix-config.h>
  25. #endif
  26. #include <stdint.h>
  27. #include "inputstr.h"
  28. #include "assert.h"
  29. #include "scrnintstr.h"
  30. static void
  31. touch_grow_queue(void)
  32. {
  33. DeviceIntRec dev;
  34. ValuatorClassRec val;
  35. TouchClassRec touch;
  36. size_t size, new_size;
  37. int i;
  38. memset(&dev, 0, sizeof(dev));
  39. dev.name = xnfstrdup("test device");
  40. dev.id = 2;
  41. dev.valuator = &val;
  42. val.numAxes = 5;
  43. dev.touch = &touch;
  44. inputInfo.devices = &dev;
  45. size = 5;
  46. dev.last.num_touches = size;
  47. dev.last.touches = calloc(dev.last.num_touches, sizeof(*dev.last.touches));
  48. assert(dev.last.touches);
  49. for (i = 0; i < size; i++) {
  50. dev.last.touches[i].active = TRUE;
  51. dev.last.touches[i].ddx_id = i;
  52. dev.last.touches[i].client_id = i * 2;
  53. }
  54. /* no more space, should've scheduled a workproc */
  55. assert(TouchBeginDDXTouch(&dev, 1234) == NULL);
  56. ProcessWorkQueue();
  57. new_size = size + size / 2 + 1;
  58. assert(dev.last.num_touches == new_size);
  59. /* make sure we haven't touched those */
  60. for (i = 0; i < size; i++) {
  61. DDXTouchPointInfoPtr t = &dev.last.touches[i];
  62. assert(t->active == TRUE);
  63. assert(t->ddx_id == i);
  64. assert(t->client_id == i * 2);
  65. }
  66. /* make sure those are zero-initialized */
  67. for (i = size; i < new_size; i++) {
  68. DDXTouchPointInfoPtr t = &dev.last.touches[i];
  69. assert(t->active == FALSE);
  70. assert(t->client_id == 0);
  71. assert(t->ddx_id == 0);
  72. }
  73. free(dev.name);
  74. }
  75. static void
  76. touch_find_ddxid(void)
  77. {
  78. DeviceIntRec dev;
  79. DDXTouchPointInfoPtr ti;
  80. ValuatorClassRec val;
  81. TouchClassRec touch;
  82. int size = 5;
  83. int i;
  84. memset(&dev, 0, sizeof(dev));
  85. dev.name = xnfstrdup("test device");
  86. dev.id = 2;
  87. dev.valuator = &val;
  88. val.numAxes = 5;
  89. dev.touch = &touch;
  90. dev.last.num_touches = size;
  91. dev.last.touches = calloc(dev.last.num_touches, sizeof(*dev.last.touches));
  92. inputInfo.devices = &dev;
  93. assert(dev.last.touches);
  94. dev.last.touches[0].active = TRUE;
  95. dev.last.touches[0].ddx_id = 10;
  96. dev.last.touches[0].client_id = 20;
  97. /* existing */
  98. ti = TouchFindByDDXID(&dev, 10, FALSE);
  99. assert(ti == &dev.last.touches[0]);
  100. /* non-existing */
  101. ti = TouchFindByDDXID(&dev, 20, FALSE);
  102. assert(ti == NULL);
  103. /* Non-active */
  104. dev.last.touches[0].active = FALSE;
  105. ti = TouchFindByDDXID(&dev, 10, FALSE);
  106. assert(ti == NULL);
  107. /* create on number 2 */
  108. dev.last.touches[0].active = TRUE;
  109. ti = TouchFindByDDXID(&dev, 20, TRUE);
  110. assert(ti == &dev.last.touches[1]);
  111. assert(ti->active);
  112. assert(ti->ddx_id == 20);
  113. /* set all to active */
  114. for (i = 0; i < size; i++)
  115. dev.last.touches[i].active = TRUE;
  116. /* Try to create more, fail */
  117. ti = TouchFindByDDXID(&dev, 30, TRUE);
  118. assert(ti == NULL);
  119. ti = TouchFindByDDXID(&dev, 30, TRUE);
  120. assert(ti == NULL);
  121. /* make sure we haven't resized, we're in the signal handler */
  122. assert(dev.last.num_touches == size);
  123. /* stop one touchpoint, try to create, succeed */
  124. dev.last.touches[2].active = FALSE;
  125. ti = TouchFindByDDXID(&dev, 30, TRUE);
  126. assert(ti == &dev.last.touches[2]);
  127. /* but still grow anyway */
  128. ProcessWorkQueue();
  129. ti = TouchFindByDDXID(&dev, 40, TRUE);
  130. assert(ti == &dev.last.touches[size]);
  131. free(dev.name);
  132. }
  133. static void
  134. touch_begin_ddxtouch(void)
  135. {
  136. DeviceIntRec dev;
  137. DDXTouchPointInfoPtr ti;
  138. ValuatorClassRec val;
  139. TouchClassRec touch;
  140. int ddx_id = 123;
  141. unsigned int last_client_id = 0;
  142. int size = 5;
  143. memset(&dev, 0, sizeof(dev));
  144. dev.name = xnfstrdup("test device");
  145. dev.id = 2;
  146. dev.valuator = &val;
  147. val.numAxes = 5;
  148. touch.mode = XIDirectTouch;
  149. dev.touch = &touch;
  150. dev.last.num_touches = size;
  151. dev.last.touches = calloc(dev.last.num_touches, sizeof(*dev.last.touches));
  152. inputInfo.devices = &dev;
  153. assert(dev.last.touches);
  154. ti = TouchBeginDDXTouch(&dev, ddx_id);
  155. assert(ti);
  156. assert(ti->ddx_id == ddx_id);
  157. /* client_id == ddx_id can happen in real life, but not in this test */
  158. assert(ti->client_id != ddx_id);
  159. assert(ti->active);
  160. assert(ti->client_id > last_client_id);
  161. assert(ti->emulate_pointer);
  162. last_client_id = ti->client_id;
  163. ddx_id += 10;
  164. ti = TouchBeginDDXTouch(&dev, ddx_id);
  165. assert(ti);
  166. assert(ti->ddx_id == ddx_id);
  167. /* client_id == ddx_id can happen in real life, but not in this test */
  168. assert(ti->client_id != ddx_id);
  169. assert(ti->active);
  170. assert(ti->client_id > last_client_id);
  171. assert(!ti->emulate_pointer);
  172. last_client_id = ti->client_id;
  173. free(dev.name);
  174. }
  175. static void
  176. touch_begin_touch(void)
  177. {
  178. DeviceIntRec dev;
  179. TouchClassRec touch;
  180. ValuatorClassRec val;
  181. TouchPointInfoPtr ti;
  182. int touchid = 12434;
  183. int sourceid = 23;
  184. SpriteInfoRec sprite;
  185. ScreenRec screen;
  186. screenInfo.screens[0] = &screen;
  187. memset(&dev, 0, sizeof(dev));
  188. dev.name = xnfstrdup("test device");
  189. dev.id = 2;
  190. memset(&sprite, 0, sizeof(sprite));
  191. dev.spriteInfo = &sprite;
  192. memset(&touch, 0, sizeof(touch));
  193. touch.num_touches = 0;
  194. memset(&val, 0, sizeof(val));
  195. dev.valuator = &val;
  196. val.numAxes = 2;
  197. ti = TouchBeginTouch(&dev, sourceid, touchid, TRUE);
  198. assert(!ti);
  199. dev.touch = &touch;
  200. ti = TouchBeginTouch(&dev, sourceid, touchid, TRUE);
  201. assert(ti);
  202. assert(ti->client_id == touchid);
  203. assert(ti->active);
  204. assert(ti->sourceid == sourceid);
  205. assert(ti->emulate_pointer);
  206. assert(touch.num_touches == 1);
  207. free(dev.name);
  208. }
  209. static void
  210. touch_init(void)
  211. {
  212. DeviceIntRec dev;
  213. Atom labels[2] = { 0 };
  214. int rc;
  215. SpriteInfoRec sprite;
  216. ScreenRec screen;
  217. screenInfo.screens[0] = &screen;
  218. memset(&dev, 0, sizeof(dev));
  219. dev.name = xnfstrdup("test device");
  220. memset(&sprite, 0, sizeof(sprite));
  221. dev.spriteInfo = &sprite;
  222. InitAtoms();
  223. rc = InitTouchClassDeviceStruct(&dev, 1, XIDirectTouch, 2);
  224. assert(rc == FALSE);
  225. InitValuatorClassDeviceStruct(&dev, 2, labels, 10, Absolute);
  226. rc = InitTouchClassDeviceStruct(&dev, 1, XIDirectTouch, 2);
  227. assert(rc == TRUE);
  228. assert(dev.touch);
  229. free(dev.name);
  230. }
  231. int
  232. main(int argc, char **argv)
  233. {
  234. touch_grow_queue();
  235. touch_find_ddxid();
  236. touch_begin_ddxtouch();
  237. touch_init();
  238. touch_begin_touch();
  239. return 0;
  240. }