test-trackball.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * Copyright © 2016 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 <errno.h>
  25. #include <fcntl.h>
  26. #include <libinput.h>
  27. #include <unistd.h>
  28. #include "libinput-util.h"
  29. #include "litest.h"
  30. START_TEST(trackball_rotation_config_defaults)
  31. {
  32. struct litest_device *dev = litest_current_device();
  33. struct libinput_device *device = dev->libinput_device;
  34. int angle;
  35. litest_assert(libinput_device_config_rotation_is_available(device));
  36. angle = libinput_device_config_rotation_get_angle(device);
  37. litest_assert_int_eq(angle, 0);
  38. angle = libinput_device_config_rotation_get_default_angle(device);
  39. litest_assert_int_eq(angle, 0);
  40. }
  41. END_TEST
  42. START_TEST(trackball_rotation_config_invalid_range)
  43. {
  44. struct litest_device *dev = litest_current_device();
  45. struct libinput_device *device = dev->libinput_device;
  46. enum libinput_config_status status;
  47. status = libinput_device_config_rotation_set_angle(device, 360);
  48. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_INVALID);
  49. status = libinput_device_config_rotation_set_angle(device, 361);
  50. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_INVALID);
  51. status = libinput_device_config_rotation_set_angle(device, -1);
  52. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_INVALID);
  53. }
  54. END_TEST
  55. START_TEST(trackball_rotation_config_no_rotation)
  56. {
  57. struct litest_device *dev = litest_current_device();
  58. struct libinput_device *device = dev->libinput_device;
  59. enum libinput_config_status status;
  60. int angle;
  61. litest_assert(!libinput_device_config_rotation_is_available(device));
  62. angle = libinput_device_config_rotation_get_angle(device);
  63. litest_assert_int_eq(angle, 0);
  64. angle = libinput_device_config_rotation_get_default_angle(device);
  65. litest_assert_int_eq(angle, 0);
  66. /* 0 always succeeds */
  67. status = libinput_device_config_rotation_set_angle(device, 0);
  68. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
  69. for (angle = 1; angle < 360; angle++) {
  70. status = libinput_device_config_rotation_set_angle(device, angle);
  71. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_UNSUPPORTED);
  72. }
  73. }
  74. END_TEST
  75. START_TEST(trackball_rotation_config_right_angle)
  76. {
  77. struct litest_device *dev = litest_current_device();
  78. struct libinput_device *device = dev->libinput_device;
  79. enum libinput_config_status status;
  80. int angle;
  81. litest_assert(libinput_device_config_rotation_is_available(device));
  82. for (angle = 0; angle < 360; angle += 90) {
  83. status = libinput_device_config_rotation_set_angle(device, angle);
  84. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
  85. }
  86. }
  87. END_TEST
  88. START_TEST(trackball_rotation_config_odd_angle)
  89. {
  90. struct litest_device *dev = litest_current_device();
  91. struct libinput_device *device = dev->libinput_device;
  92. enum libinput_config_status status;
  93. int angle;
  94. litest_assert(libinput_device_config_rotation_is_available(device));
  95. for (angle = 0; angle < 360; angle++) {
  96. status = libinput_device_config_rotation_set_angle(device, angle);
  97. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
  98. }
  99. }
  100. END_TEST
  101. START_TEST(trackball_rotation_x)
  102. {
  103. struct litest_device *dev = litest_current_device();
  104. struct libinput *li = dev->libinput;
  105. struct libinput_device *device = dev->libinput_device;
  106. struct libinput_event *event;
  107. struct libinput_event_pointer *ptrev;
  108. int angle;
  109. double dx, dy;
  110. litest_drain_events(li);
  111. for (angle = 0; angle < 360; angle++) {
  112. libinput_device_config_rotation_set_angle(device, angle);
  113. litest_event(dev, EV_REL, REL_X, 1);
  114. litest_event(dev, EV_SYN, SYN_REPORT, 0);
  115. litest_dispatch(li);
  116. event = libinput_get_event(li);
  117. ptrev = litest_is_motion_event(event);
  118. /* Test unaccelerated because pointer accel may mangle the
  119. other coords */
  120. dx = libinput_event_pointer_get_dx_unaccelerated(ptrev);
  121. dy = libinput_event_pointer_get_dy_unaccelerated(ptrev);
  122. switch (angle) {
  123. case 0:
  124. litest_assert_double_eq(dx, 1.0);
  125. litest_assert_double_eq(dy, 0.0);
  126. break;
  127. case 90:
  128. litest_assert_double_eq(dx, 0.0);
  129. litest_assert_double_eq(dy, 1.0);
  130. break;
  131. case 180:
  132. litest_assert_double_eq(dx, -1.0);
  133. litest_assert_double_eq(dy, 0.0);
  134. break;
  135. case 270:
  136. litest_assert_double_eq(dx, 0.0);
  137. litest_assert_double_eq(dy, -1.0);
  138. break;
  139. }
  140. libinput_event_destroy(event);
  141. }
  142. }
  143. END_TEST
  144. START_TEST(trackball_rotation_y)
  145. {
  146. struct litest_device *dev = litest_current_device();
  147. struct libinput *li = dev->libinput;
  148. struct libinput_device *device = dev->libinput_device;
  149. struct libinput_event *event;
  150. struct libinput_event_pointer *ptrev;
  151. int angle;
  152. double dx, dy;
  153. litest_drain_events(li);
  154. for (angle = 0; angle < 360; angle++) {
  155. libinput_device_config_rotation_set_angle(device, angle);
  156. litest_event(dev, EV_REL, REL_Y, 1);
  157. litest_event(dev, EV_SYN, SYN_REPORT, 0);
  158. litest_dispatch(li);
  159. event = libinput_get_event(li);
  160. ptrev = litest_is_motion_event(event);
  161. /* Test unaccelerated because pointer accel may mangle the
  162. other coords */
  163. dx = libinput_event_pointer_get_dx_unaccelerated(ptrev);
  164. dy = libinput_event_pointer_get_dy_unaccelerated(ptrev);
  165. switch (angle) {
  166. case 0:
  167. litest_assert_double_eq(dx, 0.0);
  168. litest_assert_double_eq(dy, 1.0);
  169. break;
  170. case 90:
  171. litest_assert_double_eq(dx, -1.0);
  172. litest_assert_double_eq(dy, 0.0);
  173. break;
  174. case 180:
  175. litest_assert_double_eq(dx, 0.0);
  176. litest_assert_double_eq(dy, -1.0);
  177. break;
  178. case 270:
  179. litest_assert_double_eq(dx, 1.0);
  180. litest_assert_double_eq(dy, 0.0);
  181. break;
  182. }
  183. libinput_event_destroy(event);
  184. }
  185. }
  186. END_TEST
  187. START_TEST(trackball_rotation_accel)
  188. {
  189. struct litest_device *dev = litest_current_device();
  190. struct libinput *li = dev->libinput;
  191. struct libinput_device *device = dev->libinput_device;
  192. struct libinput_event *event;
  193. struct libinput_event_pointer *ptrev;
  194. double dx, dy;
  195. litest_drain_events(li);
  196. /* Pointer accel mangles the coordinates, so we only test one angle
  197. * and rely on the unaccelerated tests above to warn us when
  198. * something's off */
  199. libinput_device_config_rotation_set_angle(device, 90);
  200. litest_event(dev, EV_REL, REL_Y, 1);
  201. litest_event(dev, EV_REL, REL_X, 1);
  202. litest_event(dev, EV_SYN, SYN_REPORT, 0);
  203. litest_dispatch(li);
  204. event = libinput_get_event(li);
  205. ptrev = litest_is_motion_event(event);
  206. dx = libinput_event_pointer_get_dx(ptrev);
  207. dy = libinput_event_pointer_get_dy(ptrev);
  208. litest_assert_double_lt(dx, 0.0);
  209. litest_assert_double_gt(dy, 0.0);
  210. libinput_event_destroy(event);
  211. }
  212. END_TEST
  213. TEST_COLLECTION(trackball)
  214. {
  215. /* clang-format off */
  216. litest_add(trackball_rotation_config_defaults, LITEST_TRACKBALL, LITEST_ANY);
  217. litest_add(trackball_rotation_config_invalid_range, LITEST_TRACKBALL, LITEST_ANY);
  218. litest_add(trackball_rotation_config_no_rotation, LITEST_POINTINGSTICK, LITEST_ANY);
  219. litest_add(trackball_rotation_config_right_angle, LITEST_TRACKBALL, LITEST_ANY);
  220. litest_add(trackball_rotation_config_odd_angle, LITEST_TRACKBALL, LITEST_ANY);
  221. litest_add(trackball_rotation_x, LITEST_TRACKBALL, LITEST_ANY);
  222. litest_add(trackball_rotation_y, LITEST_TRACKBALL, LITEST_ANY);
  223. litest_add(trackball_rotation_accel, LITEST_TRACKBALL, LITEST_ANY);
  224. /* clang-format on */
  225. }