fixes.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 <stdio.h>
  27. #include <stdlib.h>
  28. #include <X11/X.h>
  29. #include <xfixesint.h>
  30. #include <X11/extensions/xfixeswire.h>
  31. static void
  32. _fixes_test_direction(struct PointerBarrier *barrier, int d[4], int permitted)
  33. {
  34. BOOL blocking;
  35. int i, j;
  36. int dir = barrier_get_direction(d[0], d[1], d[2], d[3]);
  37. barrier->directions = 0;
  38. blocking = barrier_is_blocking_direction(barrier, dir);
  39. assert(blocking);
  40. for (j = 0; j <= BarrierNegativeY; j++) {
  41. for (i = 0; i <= BarrierNegativeY; i++) {
  42. barrier->directions |= 1 << i;
  43. blocking = barrier_is_blocking_direction(barrier, dir);
  44. assert((barrier->directions & permitted) ==
  45. permitted ? !blocking : blocking);
  46. }
  47. }
  48. }
  49. static void
  50. fixes_pointer_barrier_direction_test(void)
  51. {
  52. struct PointerBarrier barrier;
  53. int x = 100;
  54. int y = 100;
  55. int directions[8][4] = {
  56. {x, y, x, y + 100}, /* S */
  57. {x + 50, y, x - 50, y + 100}, /* SW */
  58. {x + 100, y, x, y}, /* W */
  59. {x + 100, y + 50, x, y - 50}, /* NW */
  60. {x, y + 100, x, y}, /* N */
  61. {x - 50, y + 100, x + 50, y}, /* NE */
  62. {x, y, x + 100, y}, /* E */
  63. {x, y - 50, x + 100, y + 50}, /* SE */
  64. };
  65. barrier.x1 = x;
  66. barrier.x2 = x;
  67. barrier.y1 = y - 50;
  68. barrier.y2 = y + 49;
  69. _fixes_test_direction(&barrier, directions[0], BarrierPositiveY);
  70. _fixes_test_direction(&barrier, directions[1],
  71. BarrierPositiveY | BarrierNegativeX);
  72. _fixes_test_direction(&barrier, directions[2], BarrierNegativeX);
  73. _fixes_test_direction(&barrier, directions[3],
  74. BarrierNegativeY | BarrierNegativeX);
  75. _fixes_test_direction(&barrier, directions[4], BarrierNegativeY);
  76. _fixes_test_direction(&barrier, directions[5],
  77. BarrierPositiveX | BarrierNegativeY);
  78. _fixes_test_direction(&barrier, directions[6], BarrierPositiveX);
  79. _fixes_test_direction(&barrier, directions[7],
  80. BarrierPositiveY | BarrierPositiveX);
  81. }
  82. static void
  83. fixes_pointer_barriers_test(void)
  84. {
  85. struct PointerBarrier barrier;
  86. int x1, y1, x2, y2;
  87. double distance;
  88. int x = 100;
  89. int y = 100;
  90. /* vert barrier */
  91. barrier.x1 = x;
  92. barrier.x2 = x;
  93. barrier.y1 = y - 50;
  94. barrier.y2 = y + 50;
  95. /* across at half-way */
  96. x1 = x + 1;
  97. x2 = x - 1;
  98. y1 = y;
  99. y2 = y;
  100. assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  101. assert(distance == 1);
  102. /* definitely not across */
  103. x1 = x + 10;
  104. x2 = x + 5;
  105. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  106. /* across, but outside of y range */
  107. x1 = x + 1;
  108. x2 = x - 1;
  109. y1 = y + 100;
  110. y2 = y + 100;
  111. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  112. /* across, diagonally */
  113. x1 = x + 5;
  114. x2 = x - 5;
  115. y1 = y + 5;
  116. y2 = y - 5;
  117. assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  118. /* across but outside boundary, diagonally */
  119. x1 = x + 5;
  120. x2 = x - 5;
  121. y1 = y + 100;
  122. y2 = y + 50;
  123. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  124. /* edge case: startpoint of movement on barrier → blocking */
  125. x1 = x;
  126. x2 = x - 1;
  127. y1 = y;
  128. y2 = y;
  129. assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  130. /* edge case: startpoint of movement on barrier → not blocking, positive */
  131. x1 = x;
  132. x2 = x + 1;
  133. y1 = y;
  134. y2 = y;
  135. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  136. /* edge case: startpoint of movement on barrier → not blocking, negative */
  137. x1 = x - 1;
  138. x2 = x - 2;
  139. y1 = y;
  140. y2 = y;
  141. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  142. /* edge case: endpoint of movement on barrier → blocking */
  143. x1 = x + 1;
  144. x2 = x;
  145. y1 = y;
  146. y2 = y;
  147. assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  148. /* startpoint on barrier but outside y range */
  149. x1 = x;
  150. x2 = x - 1;
  151. y1 = y + 100;
  152. y2 = y + 100;
  153. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  154. /* endpoint on barrier but outside y range */
  155. x1 = x + 1;
  156. x2 = x;
  157. y1 = y + 100;
  158. y2 = y + 100;
  159. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  160. /* horizontal barrier */
  161. barrier.x1 = x - 50;
  162. barrier.x2 = x + 50;
  163. barrier.y1 = y;
  164. barrier.y2 = y;
  165. /* across at half-way */
  166. x1 = x;
  167. x2 = x;
  168. y1 = y - 1;
  169. y2 = y + 1;
  170. assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  171. /* definitely not across */
  172. y1 = y + 10;
  173. y2 = y + 5;
  174. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  175. /* across, but outside of y range */
  176. x1 = x + 100;
  177. x2 = x + 100;
  178. y1 = y + 1;
  179. y2 = y - 1;
  180. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  181. /* across, diagonally */
  182. y1 = y + 5;
  183. y2 = y - 5;
  184. x1 = x + 5;
  185. x2 = x - 5;
  186. assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  187. /* across but outside boundary, diagonally */
  188. y1 = y + 5;
  189. y2 = y - 5;
  190. x1 = x + 100;
  191. x2 = x + 50;
  192. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  193. /* edge case: startpoint of movement on barrier → blocking */
  194. y1 = y;
  195. y2 = y - 1;
  196. x1 = x;
  197. x2 = x;
  198. assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  199. /* edge case: startpoint of movement on barrier → not blocking, positive */
  200. y1 = y;
  201. y2 = y + 1;
  202. x1 = x;
  203. x2 = x;
  204. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  205. /* edge case: startpoint of movement on barrier → not blocking, negative */
  206. y1 = y - 1;
  207. y2 = y - 2;
  208. x1 = x;
  209. x2 = x;
  210. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  211. /* edge case: endpoint of movement on barrier → blocking */
  212. y1 = y + 1;
  213. y2 = y;
  214. x1 = x;
  215. x2 = x;
  216. assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  217. /* startpoint on barrier but outside y range */
  218. y1 = y;
  219. y2 = y - 1;
  220. x1 = x + 100;
  221. x2 = x + 100;
  222. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  223. /* endpoint on barrier but outside y range */
  224. y1 = y + 1;
  225. y2 = y;
  226. x1 = x + 100;
  227. x2 = x + 100;
  228. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  229. /* ray vert barrier */
  230. barrier.x1 = x;
  231. barrier.x2 = x;
  232. barrier.y1 = -1;
  233. barrier.y2 = y + 100;
  234. /* ray barrier simple case */
  235. y1 = y;
  236. y2 = y;
  237. x1 = x + 50;
  238. x2 = x - 50;
  239. assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  240. /* endpoint outside y range; should be blocked */
  241. y1 = y - 1000;
  242. y2 = y - 1000;
  243. x1 = x + 50;
  244. x2 = x - 50;
  245. assert(barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  246. /* endpoint outside y range */
  247. y1 = y + 150;
  248. y2 = y + 150;
  249. x1 = x + 50;
  250. x2 = x - 50;
  251. assert(!barrier_is_blocking(&barrier, x1, y1, x2, y2, &distance));
  252. }
  253. static void
  254. fixes_pointer_barrier_clamp_test(void)
  255. {
  256. struct PointerBarrier barrier;
  257. int x = 100;
  258. int y = 100;
  259. int cx, cy; /* clamped */
  260. /* vert barrier */
  261. barrier.x1 = x;
  262. barrier.x2 = x;
  263. barrier.y1 = y - 50;
  264. barrier.y2 = y + 49;
  265. barrier.directions = 0;
  266. cx = INT_MAX;
  267. cy = INT_MAX;
  268. barrier_clamp_to_barrier(&barrier, BarrierPositiveX, &cx, &cy);
  269. assert(cx == barrier.x1 - 1);
  270. assert(cy == INT_MAX);
  271. cx = 0;
  272. cy = INT_MAX;
  273. barrier_clamp_to_barrier(&barrier, BarrierNegativeX, &cx, &cy);
  274. assert(cx == barrier.x1);
  275. assert(cy == INT_MAX);
  276. /* horiz barrier */
  277. barrier.x1 = x - 50;
  278. barrier.x2 = x + 49;
  279. barrier.y1 = y;
  280. barrier.y2 = y;
  281. barrier.directions = 0;
  282. cx = INT_MAX;
  283. cy = INT_MAX;
  284. barrier_clamp_to_barrier(&barrier, BarrierPositiveY, &cx, &cy);
  285. assert(cx == INT_MAX);
  286. assert(cy == barrier.y1 - 1);
  287. cx = INT_MAX;
  288. cy = 0;
  289. barrier_clamp_to_barrier(&barrier, BarrierNegativeY, &cx, &cy);
  290. assert(cx == INT_MAX);
  291. assert(cy == barrier.y1);
  292. }
  293. int
  294. main(int argc, char **argv)
  295. {
  296. fixes_pointer_barriers_test();
  297. fixes_pointer_barrier_direction_test();
  298. fixes_pointer_barrier_clamp_test();
  299. return 0;
  300. }