test-trackpoint.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Copyright © 2014 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. static inline bool
  31. has_disable_while_trackpointing(struct litest_device *device)
  32. {
  33. return libinput_device_config_dwtp_is_available(device->libinput_device);
  34. }
  35. START_TEST(trackpoint_middlebutton)
  36. {
  37. struct litest_device *dev = litest_current_device();
  38. struct libinput *li = dev->libinput;
  39. struct libinput_event *event;
  40. struct libinput_event_pointer *ptrev;
  41. uint64_t ptime, rtime;
  42. litest_drain_events(li);
  43. /* A quick middle button click should get reported normally */
  44. litest_button_click_debounced(dev, li, BTN_MIDDLE, 1);
  45. msleep(2);
  46. litest_button_click_debounced(dev, li, BTN_MIDDLE, 0);
  47. litest_wait_for_event(li);
  48. event = libinput_get_event(li);
  49. ptrev = litest_is_button_event(event,
  50. BTN_MIDDLE,
  51. LIBINPUT_BUTTON_STATE_PRESSED);
  52. ptime = libinput_event_pointer_get_time(ptrev);
  53. libinput_event_destroy(event);
  54. event = libinput_get_event(li);
  55. ptrev = litest_is_button_event(event,
  56. BTN_MIDDLE,
  57. LIBINPUT_BUTTON_STATE_RELEASED);
  58. rtime = libinput_event_pointer_get_time(ptrev);
  59. libinput_event_destroy(event);
  60. litest_assert_int_lt(ptime, rtime);
  61. litest_assert_empty_queue(li);
  62. }
  63. END_TEST
  64. START_TEST(trackpoint_scroll)
  65. {
  66. struct litest_device *dev = litest_current_device();
  67. struct libinput *li = dev->libinput;
  68. litest_drain_events(li);
  69. litest_button_scroll(dev, BTN_MIDDLE, 1, 6);
  70. litest_assert_scroll(li,
  71. LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS,
  72. LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
  73. 6);
  74. litest_button_scroll(dev, BTN_MIDDLE, 1, -7);
  75. litest_assert_scroll(li,
  76. LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS,
  77. LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
  78. -7);
  79. litest_button_scroll(dev, BTN_MIDDLE, 8, 1);
  80. litest_assert_scroll(li,
  81. LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS,
  82. LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
  83. 8);
  84. litest_button_scroll(dev, BTN_MIDDLE, -9, 1);
  85. litest_assert_scroll(li,
  86. LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS,
  87. LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL,
  88. -9);
  89. /* scroll smaller than the threshold should not generate axis events */
  90. litest_button_scroll(dev, BTN_MIDDLE, 1, 1);
  91. litest_button_scroll(dev, BTN_MIDDLE, 0, 0);
  92. litest_assert_button_event(li, BTN_MIDDLE, LIBINPUT_BUTTON_STATE_PRESSED);
  93. litest_assert_button_event(li, BTN_MIDDLE, LIBINPUT_BUTTON_STATE_RELEASED);
  94. litest_assert_empty_queue(li);
  95. }
  96. END_TEST
  97. START_TEST(trackpoint_middlebutton_noscroll)
  98. {
  99. struct litest_device *dev = litest_current_device();
  100. struct libinput *li = dev->libinput;
  101. struct libinput_event *event;
  102. /* Disable middle button scrolling */
  103. libinput_device_config_scroll_set_method(dev->libinput_device,
  104. LIBINPUT_CONFIG_SCROLL_NO_SCROLL);
  105. litest_drain_events(li);
  106. /* A long middle button click + motion should get reported normally now */
  107. litest_button_scroll(dev, BTN_MIDDLE, 0, 10);
  108. litest_assert_button_event(li, BTN_MIDDLE, 1);
  109. event = libinput_get_event(li);
  110. litest_assert_notnull(event);
  111. litest_assert_event_type(event, LIBINPUT_EVENT_POINTER_MOTION);
  112. libinput_event_destroy(event);
  113. litest_assert_button_event(li, BTN_MIDDLE, 0);
  114. litest_assert_empty_queue(li);
  115. /* Restore default scroll behavior */
  116. libinput_device_config_scroll_set_method(
  117. dev->libinput_device,
  118. libinput_device_config_scroll_get_default_method(dev->libinput_device));
  119. }
  120. END_TEST
  121. START_TEST(trackpoint_scroll_source)
  122. {
  123. struct litest_device *dev = litest_current_device();
  124. struct libinput *li = dev->libinput;
  125. struct libinput_event *event;
  126. struct libinput_event_pointer *ptrev;
  127. litest_drain_events(li);
  128. litest_button_scroll(dev, BTN_MIDDLE, 0, 6);
  129. litest_wait_for_event_of_type(li, LIBINPUT_EVENT_POINTER_AXIS);
  130. while ((event = libinput_get_event(li))) {
  131. ptrev = libinput_event_get_pointer_event(event);
  132. litest_assert_enum_eq(litest_event_pointer_get_axis_source(ptrev),
  133. LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS);
  134. libinput_event_destroy(event);
  135. }
  136. }
  137. END_TEST
  138. START_TEST(trackpoint_topsoftbuttons_left_handed_trackpoint)
  139. {
  140. struct litest_device *touchpad = litest_current_device();
  141. struct libinput *li = touchpad->libinput;
  142. enum libinput_config_status status;
  143. struct libinput_event *event;
  144. struct libinput_device *device;
  145. litest_disable_hold_gestures(touchpad->libinput_device);
  146. _destroy_(litest_device) *trackpoint = litest_add_device(li, LITEST_TRACKPOINT);
  147. litest_drain_events(li);
  148. /* touchpad right-handed, trackpoint left-handed */
  149. status = libinput_device_config_left_handed_set(trackpoint->libinput_device, 1);
  150. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
  151. litest_touch_down(touchpad, 0, 5, 5);
  152. litest_dispatch(li);
  153. litest_button_click_debounced(touchpad, li, BTN_LEFT, true);
  154. litest_dispatch(li);
  155. event = libinput_get_event(li);
  156. litest_is_button_event(event, BTN_RIGHT, LIBINPUT_BUTTON_STATE_PRESSED);
  157. device = libinput_event_get_device(event);
  158. litest_assert(device == trackpoint->libinput_device);
  159. libinput_event_destroy(event);
  160. litest_button_click_debounced(touchpad, li, BTN_LEFT, false);
  161. litest_dispatch(li);
  162. event = libinput_get_event(li);
  163. litest_is_button_event(event, BTN_RIGHT, LIBINPUT_BUTTON_STATE_RELEASED);
  164. device = libinput_event_get_device(event);
  165. litest_assert(device == trackpoint->libinput_device);
  166. libinput_event_destroy(event);
  167. }
  168. END_TEST
  169. START_TEST(trackpoint_topsoftbuttons_left_handed_touchpad)
  170. {
  171. struct litest_device *touchpad = litest_current_device();
  172. struct libinput *li = touchpad->libinput;
  173. enum libinput_config_status status;
  174. struct libinput_event *event;
  175. struct libinput_device *device;
  176. litest_disable_hold_gestures(touchpad->libinput_device);
  177. _destroy_(litest_device) *trackpoint = litest_add_device(li, LITEST_TRACKPOINT);
  178. litest_drain_events(li);
  179. /* touchpad left-handed, trackpoint right-handed */
  180. status = libinput_device_config_left_handed_set(touchpad->libinput_device, 1);
  181. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
  182. litest_touch_down(touchpad, 0, 5, 5);
  183. litest_dispatch(li);
  184. litest_button_click_debounced(touchpad, li, BTN_LEFT, true);
  185. litest_dispatch(li);
  186. event = libinput_get_event(li);
  187. litest_is_button_event(event, BTN_LEFT, LIBINPUT_BUTTON_STATE_PRESSED);
  188. device = libinput_event_get_device(event);
  189. litest_assert(device == trackpoint->libinput_device);
  190. libinput_event_destroy(event);
  191. litest_button_click_debounced(touchpad, li, BTN_LEFT, false);
  192. litest_dispatch(li);
  193. event = libinput_get_event(li);
  194. litest_is_button_event(event, BTN_LEFT, LIBINPUT_BUTTON_STATE_RELEASED);
  195. device = libinput_event_get_device(event);
  196. litest_assert(device == trackpoint->libinput_device);
  197. libinput_event_destroy(event);
  198. }
  199. END_TEST
  200. START_TEST(trackpoint_topsoftbuttons_left_handed_both)
  201. {
  202. struct litest_device *touchpad = litest_current_device();
  203. struct libinput *li = touchpad->libinput;
  204. enum libinput_config_status status;
  205. struct libinput_event *event;
  206. struct libinput_device *device;
  207. litest_disable_hold_gestures(touchpad->libinput_device);
  208. _destroy_(litest_device) *trackpoint = litest_add_device(li, LITEST_TRACKPOINT);
  209. litest_drain_events(li);
  210. /* touchpad left-handed, trackpoint left-handed */
  211. status = libinput_device_config_left_handed_set(touchpad->libinput_device, 1);
  212. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
  213. status = libinput_device_config_left_handed_set(trackpoint->libinput_device, 1);
  214. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
  215. litest_touch_down(touchpad, 0, 5, 5);
  216. litest_dispatch(li);
  217. litest_button_click_debounced(touchpad, li, BTN_LEFT, true);
  218. litest_dispatch(li);
  219. event = libinput_get_event(li);
  220. litest_is_button_event(event, BTN_RIGHT, LIBINPUT_BUTTON_STATE_PRESSED);
  221. device = libinput_event_get_device(event);
  222. litest_assert(device == trackpoint->libinput_device);
  223. libinput_event_destroy(event);
  224. litest_button_click_debounced(touchpad, li, BTN_LEFT, false);
  225. litest_dispatch(li);
  226. event = libinput_get_event(li);
  227. litest_is_button_event(event, BTN_RIGHT, LIBINPUT_BUTTON_STATE_RELEASED);
  228. device = libinput_event_get_device(event);
  229. litest_assert(device == trackpoint->libinput_device);
  230. libinput_event_destroy(event);
  231. }
  232. END_TEST
  233. static inline void
  234. enable_dwtp_with_timeout(struct litest_device *dev, uint32_t timeout)
  235. {
  236. enum libinput_config_status status, expected = LIBINPUT_CONFIG_STATUS_SUCCESS;
  237. status = libinput_device_config_dwtp_set_enabled(dev->libinput_device,
  238. LIBINPUT_CONFIG_DWTP_ENABLED);
  239. litest_assert_enum_eq(status, expected);
  240. if (timeout) {
  241. auto status =
  242. libinput_device_config_dwtp_set_timeout(dev->libinput_device,
  243. timeout);
  244. litest_assert_enum_eq(status, LIBINPUT_CONFIG_STATUS_SUCCESS);
  245. }
  246. }
  247. static inline void
  248. enable_dwtp(struct litest_device *dev)
  249. {
  250. enable_dwtp_with_timeout(dev, 0);
  251. }
  252. static inline void
  253. disable_dwtp(struct litest_device *dev)
  254. {
  255. enum libinput_config_status status, expected = LIBINPUT_CONFIG_STATUS_SUCCESS;
  256. status = libinput_device_config_dwtp_set_enabled(dev->libinput_device,
  257. LIBINPUT_CONFIG_DWTP_DISABLED);
  258. litest_assert_enum_eq(status, expected);
  259. }
  260. START_TEST(trackpoint_palmdetect)
  261. {
  262. struct litest_device *trackpoint = litest_current_device();
  263. struct libinput *li = trackpoint->libinput;
  264. int i;
  265. uint32_t timeout = litest_test_param_get_u32(test_env->params, "timeout");
  266. _destroy_(litest_device) *touchpad =
  267. litest_add_device(li, LITEST_SYNAPTICS_I2C);
  268. if (has_disable_while_trackpointing(touchpad))
  269. enable_dwtp_with_timeout(touchpad, timeout);
  270. litest_disable_hold_gestures(touchpad->libinput_device);
  271. litest_drain_events(li);
  272. for (i = 0; i < 10; i++) {
  273. litest_event(trackpoint, EV_REL, REL_X, 1);
  274. litest_event(trackpoint, EV_REL, REL_Y, 1);
  275. litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
  276. litest_dispatch(li);
  277. }
  278. litest_drain_events(li);
  279. litest_touch_down(touchpad, 0, 30, 30);
  280. litest_touch_move_to(touchpad, 0, 30, 30, 80, 80, 10);
  281. litest_touch_up(touchpad, 0);
  282. litest_assert_empty_queue(li);
  283. if (timeout) {
  284. litest_timeout(li, timeout);
  285. } else {
  286. litest_timeout_trackpoint(li);
  287. }
  288. litest_touch_down(touchpad, 0, 30, 30);
  289. litest_touch_move_to(touchpad, 0, 30, 30, 80, 80, 10);
  290. litest_touch_up(touchpad, 0);
  291. litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
  292. }
  293. END_TEST
  294. START_TEST(trackpoint_palmdetect_dwtp_disabled)
  295. {
  296. struct litest_device *trackpoint = litest_current_device();
  297. struct libinput *li = trackpoint->libinput;
  298. int i;
  299. _destroy_(litest_device) *touchpad =
  300. litest_add_device(li, LITEST_SYNAPTICS_I2C);
  301. if (has_disable_while_trackpointing(touchpad))
  302. disable_dwtp(touchpad);
  303. litest_disable_hold_gestures(touchpad->libinput_device);
  304. litest_drain_events(li);
  305. for (i = 0; i < 10; i++) {
  306. litest_event(trackpoint, EV_REL, REL_X, 1);
  307. litest_event(trackpoint, EV_REL, REL_Y, 1);
  308. litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
  309. litest_dispatch(li);
  310. }
  311. litest_drain_events(li);
  312. litest_touch_down(touchpad, 0, 30, 30);
  313. litest_touch_move_to(touchpad, 0, 30, 30, 80, 80, 10);
  314. litest_touch_up(touchpad, 0);
  315. litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
  316. }
  317. END_TEST
  318. START_TEST(trackpoint_palmdetect_resume_touch)
  319. {
  320. struct litest_device *trackpoint = litest_current_device();
  321. struct libinput *li = trackpoint->libinput;
  322. int i;
  323. _destroy_(litest_device) *touchpad =
  324. litest_add_device(li, LITEST_SYNAPTICS_I2C);
  325. if (has_disable_while_trackpointing(touchpad))
  326. enable_dwtp(touchpad);
  327. litest_disable_hold_gestures(touchpad->libinput_device);
  328. litest_drain_events(li);
  329. for (i = 0; i < 10; i++) {
  330. litest_event(trackpoint, EV_REL, REL_X, 1);
  331. litest_event(trackpoint, EV_REL, REL_Y, 1);
  332. litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
  333. litest_dispatch(li);
  334. }
  335. litest_drain_events(li);
  336. litest_touch_down(touchpad, 0, 30, 30);
  337. litest_touch_move_to(touchpad, 0, 30, 30, 80, 80, 10);
  338. litest_assert_empty_queue(li);
  339. litest_timeout_trackpoint(li);
  340. /* touch started after last tp event, expect resume */
  341. litest_touch_move_to(touchpad, 0, 80, 80, 30, 30, 10);
  342. litest_touch_up(touchpad, 0);
  343. litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
  344. }
  345. END_TEST
  346. START_TEST(trackpoint_palmdetect_require_min_events)
  347. {
  348. struct litest_device *trackpoint = litest_current_device();
  349. struct libinput *li = trackpoint->libinput;
  350. _destroy_(litest_device) *touchpad =
  351. litest_add_device(li, LITEST_SYNAPTICS_I2C);
  352. if (has_disable_while_trackpointing(touchpad))
  353. enable_dwtp(touchpad);
  354. litest_disable_hold_gestures(touchpad->libinput_device);
  355. litest_drain_events(li);
  356. /* A single event does not trigger palm detection */
  357. litest_event(trackpoint, EV_REL, REL_X, 1);
  358. litest_event(trackpoint, EV_REL, REL_Y, 1);
  359. litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
  360. litest_dispatch(li);
  361. litest_drain_events(li);
  362. litest_touch_down(touchpad, 0, 30, 30);
  363. litest_touch_move_to(touchpad, 0, 30, 30, 80, 80, 10);
  364. litest_touch_up(touchpad, 0);
  365. litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
  366. }
  367. END_TEST
  368. START_TEST(trackpoint_palmdetect_require_min_events_timeout)
  369. {
  370. struct litest_device *trackpoint = litest_current_device();
  371. struct libinput *li = trackpoint->libinput;
  372. _destroy_(litest_device) *touchpad =
  373. litest_add_device(li, LITEST_SYNAPTICS_I2C);
  374. if (has_disable_while_trackpointing(touchpad))
  375. enable_dwtp(touchpad);
  376. litest_disable_hold_gestures(touchpad->libinput_device);
  377. litest_drain_events(li);
  378. for (int i = 0; i < 10; i++) {
  379. /* A single event does not trigger palm detection */
  380. litest_event(trackpoint, EV_REL, REL_X, 1);
  381. litest_event(trackpoint, EV_REL, REL_Y, 1);
  382. litest_event(trackpoint, EV_SYN, SYN_REPORT, 0);
  383. litest_dispatch(li);
  384. litest_drain_events(li);
  385. litest_touch_down(touchpad, 0, 30, 30);
  386. litest_touch_move_to(touchpad, 0, 30, 30, 80, 80, 10);
  387. litest_touch_up(touchpad, 0);
  388. litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
  389. litest_timeout_trackpoint(li);
  390. }
  391. }
  392. END_TEST
  393. TEST_COLLECTION(trackpoint)
  394. {
  395. /* clang-format off */
  396. litest_add(trackpoint_middlebutton, LITEST_POINTINGSTICK, LITEST_ANY);
  397. litest_add(trackpoint_middlebutton_noscroll, LITEST_POINTINGSTICK, LITEST_ANY);
  398. litest_add(trackpoint_scroll, LITEST_POINTINGSTICK, LITEST_ANY);
  399. litest_add(trackpoint_scroll_source, LITEST_POINTINGSTICK, LITEST_ANY);
  400. litest_add(trackpoint_topsoftbuttons_left_handed_trackpoint, LITEST_TOPBUTTONPAD, LITEST_ANY);
  401. litest_add(trackpoint_topsoftbuttons_left_handed_touchpad, LITEST_TOPBUTTONPAD, LITEST_ANY);
  402. litest_add(trackpoint_topsoftbuttons_left_handed_both, LITEST_TOPBUTTONPAD, LITEST_ANY);
  403. litest_with_parameters(params, "timeout", 'u', 4, 0, 120, 300, 900) {
  404. litest_add_parametrized(trackpoint_palmdetect, LITEST_POINTINGSTICK, LITEST_ANY, params);
  405. }
  406. litest_add(trackpoint_palmdetect_dwtp_disabled, LITEST_POINTINGSTICK, LITEST_ANY);
  407. litest_add(trackpoint_palmdetect_resume_touch, LITEST_POINTINGSTICK, LITEST_ANY);
  408. litest_add(trackpoint_palmdetect_require_min_events, LITEST_POINTINGSTICK, LITEST_ANY);
  409. litest_add(trackpoint_palmdetect_require_min_events_timeout, LITEST_POINTINGSTICK, LITEST_ANY);
  410. /* clang-format on */
  411. }