Debian Codegraph @

test/test-trackpoint.c

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