334a59c32ca49e74aa5692f1b0cfb7734ce860b4
[platform/upstream/libinput.git] / test / touchpad.c
1 /*
2  * Copyright © 2014 Red Hat, Inc.
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24
25 #include <check.h>
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 START_TEST(touchpad_1fg_motion)
35 {
36         struct litest_device *dev = litest_current_device();
37         struct libinput *li = dev->libinput;
38         struct libinput_event *event;
39         struct libinput_event_pointer *ptrev;
40
41         litest_drain_events(li);
42
43         litest_touch_down(dev, 0, 50, 50);
44         litest_touch_move_to(dev, 0, 50, 50, 80, 50, 5);
45         litest_touch_up(dev, 0);
46
47         libinput_dispatch(li);
48
49         event = libinput_get_event(li);
50         ck_assert(event != NULL);
51
52         while (event) {
53                 ck_assert_int_eq(libinput_event_get_type(event),
54                                  LIBINPUT_EVENT_POINTER_MOTION);
55
56                 ptrev = libinput_event_get_pointer_event(event);
57                 ck_assert_int_ge(libinput_event_pointer_get_dx(ptrev), 0);
58                 ck_assert_int_eq(libinput_event_pointer_get_dy(ptrev), 0);
59                 libinput_event_destroy(event);
60                 event = libinput_get_event(li);
61         }
62 }
63 END_TEST
64
65 START_TEST(touchpad_2fg_no_motion)
66 {
67         struct litest_device *dev = litest_current_device();
68         struct libinput *li = dev->libinput;
69         struct libinput_event *event;
70
71         litest_drain_events(li);
72
73         litest_touch_down(dev, 0, 20, 20);
74         litest_touch_down(dev, 1, 70, 20);
75         litest_touch_move_to(dev, 0, 20, 20, 80, 80, 5);
76         litest_touch_move_to(dev, 1, 70, 20, 80, 50, 5);
77         litest_touch_up(dev, 1);
78         litest_touch_up(dev, 0);
79
80         libinput_dispatch(li);
81
82         event = libinput_get_event(li);
83         while (event) {
84                 ck_assert_int_ne(libinput_event_get_type(event),
85                                  LIBINPUT_EVENT_POINTER_MOTION);
86                 libinput_event_destroy(event);
87                 event = libinput_get_event(li);
88         }
89 }
90 END_TEST
91
92 static void
93 assert_button_event(struct libinput *li, unsigned int button,
94                     enum libinput_button_state state)
95 {
96         struct libinput_event *event;
97         struct libinput_event_pointer *ptrev;
98
99         libinput_dispatch(li);
100         event = libinput_get_event(li);
101
102         ck_assert(event != NULL);
103         ck_assert_int_eq(libinput_event_get_type(event),
104                          LIBINPUT_EVENT_POINTER_BUTTON);
105         ptrev = libinput_event_get_pointer_event(event);
106         ck_assert_int_eq(libinput_event_pointer_get_button(ptrev),
107                          button);
108         ck_assert_int_eq(libinput_event_pointer_get_button_state(ptrev),
109                          state);
110         libinput_event_destroy(event);
111 }
112
113 START_TEST(touchpad_1fg_tap)
114 {
115         struct litest_device *dev = litest_current_device();
116         struct libinput *li = dev->libinput;
117         struct libinput_event *event;
118
119         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
120
121         litest_drain_events(li);
122
123         litest_touch_down(dev, 0, 50, 50);
124         litest_touch_up(dev, 0);
125
126         libinput_dispatch(li);
127
128         assert_button_event(li, BTN_LEFT,
129                             LIBINPUT_BUTTON_STATE_PRESSED);
130         msleep(300); /* tap-n-drag timeout */
131         assert_button_event(li, BTN_LEFT,
132                             LIBINPUT_BUTTON_STATE_RELEASED);
133
134         libinput_dispatch(li);
135         event = libinput_get_event(li);
136         ck_assert(event == NULL);
137 }
138 END_TEST
139
140 START_TEST(touchpad_1fg_tap_n_drag)
141 {
142         struct litest_device *dev = litest_current_device();
143         struct libinput *li = dev->libinput;
144         struct libinput_event *event;
145
146         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
147
148         litest_drain_events(li);
149
150         litest_touch_down(dev, 0, 50, 50);
151         litest_touch_up(dev, 0);
152         litest_touch_down(dev, 0, 50, 50);
153         litest_touch_move_to(dev, 0, 50, 50, 80, 80, 5);
154         litest_touch_up(dev, 0);
155
156         libinput_dispatch(li);
157
158         assert_button_event(li, BTN_LEFT,
159                             LIBINPUT_BUTTON_STATE_PRESSED);
160
161         libinput_dispatch(li);
162         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
163                 event = libinput_get_event(li);
164                 libinput_event_destroy(event);
165                 libinput_dispatch(li);
166         }
167
168         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
169
170         /* lift finger, set down again, should continue dragging */
171         litest_touch_down(dev, 0, 50, 50);
172         litest_touch_move_to(dev, 0, 50, 50, 80, 80, 5);
173         litest_touch_up(dev, 0);
174
175         libinput_dispatch(li);
176         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
177                 event = libinput_get_event(li);
178                 libinput_event_destroy(event);
179                 libinput_dispatch(li);
180         }
181
182         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
183
184         msleep(300); /* tap-n-drag timeout */
185
186         assert_button_event(li, BTN_LEFT,
187                             LIBINPUT_BUTTON_STATE_RELEASED);
188
189         litest_assert_empty_queue(li);
190 }
191 END_TEST
192
193 START_TEST(touchpad_2fg_tap)
194 {
195         struct litest_device *dev = litest_current_device();
196         struct libinput *li = dev->libinput;
197
198         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
199
200         litest_drain_events(dev->libinput);
201
202         litest_touch_down(dev, 0, 50, 50);
203         litest_touch_down(dev, 1, 70, 70);
204         litest_touch_up(dev, 0);
205         litest_touch_up(dev, 1);
206
207         libinput_dispatch(li);
208
209         assert_button_event(li, BTN_RIGHT,
210                             LIBINPUT_BUTTON_STATE_PRESSED);
211         msleep(300); /* tap-n-drag timeout */
212         assert_button_event(li, BTN_RIGHT,
213                             LIBINPUT_BUTTON_STATE_RELEASED);
214
215         litest_assert_empty_queue(li);
216 }
217 END_TEST
218
219 START_TEST(touchpad_2fg_tap_inverted)
220 {
221         struct litest_device *dev = litest_current_device();
222         struct libinput *li = dev->libinput;
223
224         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
225
226         litest_drain_events(dev->libinput);
227
228         litest_touch_down(dev, 0, 50, 50);
229         litest_touch_down(dev, 1, 70, 70);
230         litest_touch_up(dev, 1);
231         litest_touch_up(dev, 0);
232
233         libinput_dispatch(li);
234
235         assert_button_event(li, BTN_RIGHT,
236                             LIBINPUT_BUTTON_STATE_PRESSED);
237         msleep(300); /* tap-n-drag timeout */
238         assert_button_event(li, BTN_RIGHT,
239                             LIBINPUT_BUTTON_STATE_RELEASED);
240
241         litest_assert_empty_queue(li);
242 }
243 END_TEST
244
245 START_TEST(touchpad_1fg_tap_click)
246 {
247         struct litest_device *dev = litest_current_device();
248         struct libinput *li = dev->libinput;
249
250         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
251
252         litest_drain_events(dev->libinput);
253
254         /* finger down, button click, finger up
255            -> only one button left event pair */
256         litest_touch_down(dev, 0, 50, 50);
257         litest_event(dev, EV_KEY, BTN_LEFT, 1);
258         litest_event(dev, EV_SYN, SYN_REPORT, 0);
259         litest_event(dev, EV_KEY, BTN_LEFT, 0);
260         litest_event(dev, EV_SYN, SYN_REPORT, 0);
261         litest_touch_up(dev, 0);
262
263         libinput_dispatch(li);
264
265         assert_button_event(li, BTN_LEFT,
266                             LIBINPUT_BUTTON_STATE_PRESSED);
267         assert_button_event(li, BTN_LEFT,
268                             LIBINPUT_BUTTON_STATE_RELEASED);
269
270         litest_assert_empty_queue(li);
271 }
272 END_TEST
273
274 START_TEST(touchpad_2fg_tap_click)
275 {
276         struct litest_device *dev = litest_current_device();
277         struct libinput *li = dev->libinput;
278
279         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
280
281         litest_drain_events(dev->libinput);
282
283         /* two fingers down, button click, fingers up
284            -> only one button left event pair */
285         litest_touch_down(dev, 0, 50, 50);
286         litest_touch_down(dev, 1, 70, 50);
287         litest_event(dev, EV_KEY, BTN_LEFT, 1);
288         litest_event(dev, EV_SYN, SYN_REPORT, 0);
289         litest_event(dev, EV_KEY, BTN_LEFT, 0);
290         litest_event(dev, EV_SYN, SYN_REPORT, 0);
291         litest_touch_up(dev, 1);
292         litest_touch_up(dev, 0);
293
294         libinput_dispatch(li);
295
296         assert_button_event(li, BTN_LEFT,
297                             LIBINPUT_BUTTON_STATE_PRESSED);
298         assert_button_event(li, BTN_LEFT,
299                             LIBINPUT_BUTTON_STATE_RELEASED);
300
301         litest_assert_empty_queue(li);
302 }
303 END_TEST
304
305 START_TEST(touchpad_2fg_tap_click_apple)
306 {
307         struct litest_device *dev = litest_current_device();
308         struct libinput *li = dev->libinput;
309
310         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
311
312         litest_drain_events(dev->libinput);
313
314         /* two fingers down, button click, fingers up
315            -> only one button right event pair
316            (apple have clickfinger enabled by default) */
317         litest_touch_down(dev, 0, 50, 50);
318         litest_touch_down(dev, 1, 70, 50);
319         litest_event(dev, EV_KEY, BTN_LEFT, 1);
320         litest_event(dev, EV_SYN, SYN_REPORT, 0);
321         litest_event(dev, EV_KEY, BTN_LEFT, 0);
322         litest_event(dev, EV_SYN, SYN_REPORT, 0);
323         litest_touch_up(dev, 1);
324         litest_touch_up(dev, 0);
325
326         libinput_dispatch(li);
327
328         assert_button_event(li, BTN_RIGHT,
329                             LIBINPUT_BUTTON_STATE_PRESSED);
330         assert_button_event(li, BTN_RIGHT,
331                             LIBINPUT_BUTTON_STATE_RELEASED);
332
333         litest_assert_empty_queue(li);
334 }
335 END_TEST
336
337 START_TEST(touchpad_no_2fg_tap_after_move)
338 {
339         struct litest_device *dev = litest_current_device();
340         struct libinput *li = dev->libinput;
341
342         litest_drain_events(dev->libinput);
343
344         /* one finger down, move past threshold,
345            second finger down, first finger up
346            -> no event
347          */
348         litest_touch_down(dev, 0, 50, 50);
349         litest_touch_move_to(dev, 0, 50, 50, 90, 90, 10);
350         litest_drain_events(dev->libinput);
351
352         litest_touch_down(dev, 1, 70, 50);
353         litest_touch_up(dev, 0);
354
355         litest_assert_empty_queue(li);
356 }
357 END_TEST
358
359 START_TEST(touchpad_no_2fg_tap_after_timeout)
360 {
361         struct litest_device *dev = litest_current_device();
362         struct libinput *li = dev->libinput;
363
364         litest_drain_events(dev->libinput);
365
366         /* one finger down, wait past tap timeout,
367            second finger down, first finger up
368            -> no event
369          */
370         litest_touch_down(dev, 0, 50, 50);
371         libinput_dispatch(dev->libinput);
372         msleep(300);
373         libinput_dispatch(dev->libinput);
374         litest_drain_events(dev->libinput);
375
376         litest_touch_down(dev, 1, 70, 50);
377         litest_touch_up(dev, 0);
378
379         litest_assert_empty_queue(li);
380 }
381 END_TEST
382
383 START_TEST(touchpad_no_first_fg_tap_after_move)
384 {
385         struct litest_device *dev = litest_current_device();
386         struct libinput *li = dev->libinput;
387         struct libinput_event *event;
388
389         litest_drain_events(dev->libinput);
390
391         /* one finger down, second finger down,
392            second finger moves beyond threshold,
393            first finger up
394            -> no event
395          */
396         litest_touch_down(dev, 0, 50, 50);
397         litest_touch_down(dev, 1, 70, 50);
398         libinput_dispatch(dev->libinput);
399         litest_touch_move_to(dev, 1, 70, 50, 90, 90, 10);
400         libinput_dispatch(dev->libinput);
401         litest_touch_up(dev, 0);
402         litest_touch_up(dev, 1);
403         libinput_dispatch(dev->libinput);
404
405         while ((event = libinput_get_event(li))) {
406                 ck_assert_int_ne(libinput_event_get_type(event),
407                                  LIBINPUT_EVENT_POINTER_BUTTON);
408                 libinput_event_destroy(event);
409         }
410 }
411 END_TEST
412
413 START_TEST(touchpad_1fg_double_tap_click)
414 {
415         struct litest_device *dev = litest_current_device();
416         struct libinput *li = dev->libinput;
417
418         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
419
420         litest_drain_events(dev->libinput);
421
422         /* one finger down, up, down, button click, finger up
423            -> two button left event pairs */
424         litest_touch_down(dev, 0, 50, 50);
425         litest_touch_up(dev, 0);
426         litest_touch_down(dev, 0, 50, 50);
427         litest_event(dev, EV_KEY, BTN_LEFT, 1);
428         litest_event(dev, EV_SYN, SYN_REPORT, 0);
429         litest_event(dev, EV_KEY, BTN_LEFT, 0);
430         litest_event(dev, EV_SYN, SYN_REPORT, 0);
431         litest_touch_up(dev, 0);
432
433         libinput_dispatch(li);
434
435         assert_button_event(li, BTN_LEFT,
436                             LIBINPUT_BUTTON_STATE_PRESSED);
437         assert_button_event(li, BTN_LEFT,
438                             LIBINPUT_BUTTON_STATE_RELEASED);
439         assert_button_event(li, BTN_LEFT,
440                             LIBINPUT_BUTTON_STATE_PRESSED);
441         assert_button_event(li, BTN_LEFT,
442                             LIBINPUT_BUTTON_STATE_RELEASED);
443
444         litest_assert_empty_queue(li);
445 }
446 END_TEST
447
448 START_TEST(touchpad_1fg_tap_n_drag_click)
449 {
450         struct litest_device *dev = litest_current_device();
451         struct libinput *li = dev->libinput;
452         struct libinput_event *event;
453
454         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
455
456         litest_drain_events(dev->libinput);
457
458         /* one finger down, up, down, move, button click, finger up
459            -> two button left event pairs, motion allowed */
460         litest_touch_down(dev, 0, 50, 50);
461         litest_touch_up(dev, 0);
462         litest_touch_down(dev, 0, 50, 50);
463         litest_touch_move_to(dev, 0, 50, 50, 80, 50, 10);
464
465         assert_button_event(li, BTN_LEFT,
466                             LIBINPUT_BUTTON_STATE_PRESSED);
467
468         libinput_dispatch(li);
469
470         ck_assert_int_eq(libinput_next_event_type(li),
471                          LIBINPUT_EVENT_POINTER_MOTION);
472         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
473                 event = libinput_get_event(li);
474                 libinput_event_destroy(event);
475                 libinput_dispatch(li);
476         }
477
478         litest_event(dev, EV_KEY, BTN_LEFT, 1);
479         litest_event(dev, EV_SYN, SYN_REPORT, 0);
480
481         assert_button_event(li, BTN_LEFT,
482                             LIBINPUT_BUTTON_STATE_RELEASED);
483         assert_button_event(li, BTN_LEFT,
484                             LIBINPUT_BUTTON_STATE_PRESSED);
485
486         litest_event(dev, EV_KEY, BTN_LEFT, 0);
487         litest_event(dev, EV_SYN, SYN_REPORT, 0);
488         litest_touch_up(dev, 0);
489
490         libinput_dispatch(li);
491
492         assert_button_event(li, BTN_LEFT,
493                             LIBINPUT_BUTTON_STATE_RELEASED);
494
495         litest_assert_empty_queue(li);
496 }
497 END_TEST
498
499 START_TEST(touchpad_3fg_tap)
500 {
501         struct litest_device *dev = litest_current_device();
502         struct libinput *li = dev->libinput;
503         struct libinput_event *event;
504         int i;
505
506         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
507
508         for (i = 0; i < 3; i++) {
509                 litest_drain_events(li);
510
511                 litest_touch_down(dev, 0, 50, 50);
512                 litest_touch_down(dev, 1, 70, 50);
513                 litest_touch_down(dev, 2, 80, 50);
514
515                 litest_touch_up(dev, (i + 2) % 3);
516                 litest_touch_up(dev, (i + 1) % 3);
517                 litest_touch_up(dev, (i + 0) % 3);
518
519                 libinput_dispatch(li);
520
521                 assert_button_event(li, BTN_MIDDLE,
522                                     LIBINPUT_BUTTON_STATE_PRESSED);
523                 msleep(300); /* tap-n-drag timeout */
524                 assert_button_event(li, BTN_MIDDLE,
525                                     LIBINPUT_BUTTON_STATE_RELEASED);
526
527                 libinput_dispatch(li);
528                 event = libinput_get_event(li);
529                 ck_assert(event == NULL);
530         }
531 }
532 END_TEST
533
534 START_TEST(touchpad_3fg_tap_btntool)
535 {
536         struct litest_device *dev = litest_current_device();
537         struct libinput *li = dev->libinput;
538         struct libinput_event *event;
539
540         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
541
542         litest_drain_events(li);
543
544         litest_touch_down(dev, 0, 50, 50);
545         litest_touch_down(dev, 1, 70, 50);
546         litest_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, 1);
547         litest_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, 0);
548         litest_event(dev, EV_SYN, SYN_REPORT, 0);
549         litest_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, 0);
550         litest_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, 1);
551         litest_event(dev, EV_SYN, SYN_REPORT, 0);
552         litest_touch_up(dev, 1);
553         litest_touch_up(dev, 0);
554
555         libinput_dispatch(li);
556
557         assert_button_event(li, BTN_MIDDLE,
558                             LIBINPUT_BUTTON_STATE_PRESSED);
559         msleep(300); /* tap-n-drag timeout */
560         assert_button_event(li, BTN_MIDDLE,
561                             LIBINPUT_BUTTON_STATE_RELEASED);
562
563         libinput_dispatch(li);
564         event = libinput_get_event(li);
565         ck_assert(event == NULL);
566 }
567 END_TEST
568
569 START_TEST(touchpad_3fg_tap_btntool_inverted)
570 {
571         struct litest_device *dev = litest_current_device();
572         struct libinput *li = dev->libinput;
573         struct libinput_event *event;
574
575         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
576
577         litest_drain_events(li);
578
579         litest_touch_down(dev, 0, 50, 50);
580         litest_touch_down(dev, 1, 70, 50);
581         litest_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, 1);
582         litest_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, 0);
583         litest_event(dev, EV_SYN, SYN_REPORT, 0);
584         litest_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, 0);
585         litest_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, 1);
586         litest_event(dev, EV_SYN, SYN_REPORT, 0);
587         litest_touch_up(dev, 0);
588         litest_touch_up(dev, 1);
589
590         libinput_dispatch(li);
591
592         assert_button_event(li, BTN_MIDDLE,
593                             LIBINPUT_BUTTON_STATE_PRESSED);
594         msleep(300); /* tap-n-drag timeout */
595         assert_button_event(li, BTN_MIDDLE,
596                             LIBINPUT_BUTTON_STATE_RELEASED);
597
598         libinput_dispatch(li);
599         event = libinput_get_event(li);
600         ck_assert(event == NULL);
601 }
602 END_TEST
603
604 START_TEST(touchpad_1fg_clickfinger)
605 {
606         struct litest_device *dev = litest_create_device(LITEST_BCM5974);
607         struct libinput *li = dev->libinput;
608
609         litest_drain_events(li);
610
611         litest_touch_down(dev, 0, 50, 50);
612         litest_event(dev, EV_KEY, BTN_LEFT, 1);
613         litest_event(dev, EV_SYN, SYN_REPORT, 0);
614         litest_event(dev, EV_KEY, BTN_LEFT, 0);
615         litest_event(dev, EV_SYN, SYN_REPORT, 0);
616         litest_touch_up(dev, 0);
617
618         libinput_dispatch(li);
619
620         assert_button_event(li, BTN_LEFT,
621                             LIBINPUT_BUTTON_STATE_PRESSED);
622         assert_button_event(li, BTN_LEFT,
623                             LIBINPUT_BUTTON_STATE_RELEASED);
624
625         litest_delete_device(dev);
626 }
627 END_TEST
628
629 START_TEST(touchpad_2fg_clickfinger)
630 {
631         struct litest_device *dev = litest_create_device(LITEST_BCM5974);
632         struct libinput *li = dev->libinput;
633
634         litest_drain_events(li);
635
636         litest_touch_down(dev, 0, 50, 50);
637         litest_touch_down(dev, 1, 70, 70);
638         litest_event(dev, EV_KEY, BTN_LEFT, 1);
639         litest_event(dev, EV_SYN, SYN_REPORT, 0);
640         litest_event(dev, EV_KEY, BTN_LEFT, 0);
641         litest_event(dev, EV_SYN, SYN_REPORT, 0);
642         litest_touch_up(dev, 0);
643         litest_touch_up(dev, 1);
644
645         libinput_dispatch(li);
646
647         assert_button_event(li, BTN_RIGHT,
648                             LIBINPUT_BUTTON_STATE_PRESSED);
649         assert_button_event(li, BTN_RIGHT,
650                             LIBINPUT_BUTTON_STATE_RELEASED);
651
652         litest_delete_device(dev);
653 }
654 END_TEST
655
656 START_TEST(touchpad_btn_left)
657 {
658         struct litest_device *dev = litest_current_device();
659         struct libinput *li = dev->libinput;
660
661         litest_drain_events(li);
662
663         litest_event(dev, EV_KEY, BTN_LEFT, 1);
664         litest_event(dev, EV_SYN, SYN_REPORT, 0);
665         litest_event(dev, EV_KEY, BTN_LEFT, 0);
666         litest_event(dev, EV_SYN, SYN_REPORT, 0);
667
668         libinput_dispatch(li);
669
670         assert_button_event(li, BTN_LEFT,
671                             LIBINPUT_BUTTON_STATE_PRESSED);
672         assert_button_event(li, BTN_LEFT,
673                             LIBINPUT_BUTTON_STATE_RELEASED);
674 }
675 END_TEST
676
677 START_TEST(clickpad_btn_left)
678 {
679         struct litest_device *dev = litest_current_device();
680         struct libinput *li = dev->libinput;
681
682         litest_drain_events(li);
683
684         /* A clickpad always needs a finger down to tell where the
685            click happens */
686         litest_event(dev, EV_KEY, BTN_LEFT, 1);
687         litest_event(dev, EV_SYN, SYN_REPORT, 0);
688         litest_event(dev, EV_KEY, BTN_LEFT, 0);
689         litest_event(dev, EV_SYN, SYN_REPORT, 0);
690
691         libinput_dispatch(li);
692         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
693 }
694 END_TEST
695
696 START_TEST(clickpad_click_n_drag)
697 {
698         struct litest_device *dev = litest_current_device();
699         struct libinput *li = dev->libinput;
700         struct libinput_event *event;
701
702         litest_drain_events(li);
703
704         litest_touch_down(dev, 0, 50, 50);
705         litest_event(dev, EV_KEY, BTN_LEFT, 1);
706         litest_event(dev, EV_SYN, SYN_REPORT, 0);
707
708         libinput_dispatch(li);
709         assert_button_event(li, BTN_LEFT,
710                             LIBINPUT_BUTTON_STATE_PRESSED);
711
712         libinput_dispatch(li);
713         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
714
715         /* now put a second finger down */
716         litest_touch_down(dev, 1, 70, 70);
717         litest_touch_move_to(dev, 1, 70, 70, 80, 50, 5);
718         litest_touch_up(dev, 1);
719
720         libinput_dispatch(li);
721         ck_assert_int_eq(libinput_next_event_type(li),
722                          LIBINPUT_EVENT_POINTER_MOTION);
723         do {
724                 event = libinput_get_event(li);
725                 libinput_event_destroy(event);
726                 libinput_dispatch(li);
727         } while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION);
728
729         litest_event(dev, EV_KEY, BTN_LEFT, 0);
730         litest_event(dev, EV_SYN, SYN_REPORT, 0);
731         litest_touch_up(dev, 0);
732
733         assert_button_event(li, BTN_LEFT,
734                             LIBINPUT_BUTTON_STATE_RELEASED);
735 }
736 END_TEST
737
738 START_TEST(clickpad_softbutton_left)
739 {
740         struct litest_device *dev = litest_current_device();
741         struct libinput *li = dev->libinput;
742
743         litest_drain_events(li);
744
745         litest_touch_down(dev, 0, 10, 90);
746         litest_event(dev, EV_KEY, BTN_LEFT, 1);
747         litest_event(dev, EV_SYN, SYN_REPORT, 0);
748
749         assert_button_event(li,
750                             BTN_LEFT,
751                             LIBINPUT_BUTTON_STATE_PRESSED);
752
753         litest_event(dev, EV_KEY, BTN_LEFT, 0);
754         litest_event(dev, EV_SYN, SYN_REPORT, 0);
755         litest_touch_up(dev, 0);
756
757         assert_button_event(li,
758                             BTN_LEFT,
759                             LIBINPUT_BUTTON_STATE_RELEASED);
760
761         libinput_dispatch(li);
762
763         litest_assert_empty_queue(li);
764 }
765 END_TEST
766
767 START_TEST(clickpad_softbutton_right)
768 {
769         struct litest_device *dev = litest_current_device();
770         struct libinput *li = dev->libinput;
771
772         litest_drain_events(li);
773
774         litest_touch_down(dev, 0, 90, 90);
775         litest_event(dev, EV_KEY, BTN_LEFT, 1);
776         litest_event(dev, EV_SYN, SYN_REPORT, 0);
777
778         assert_button_event(li,
779                             BTN_RIGHT,
780                             LIBINPUT_BUTTON_STATE_PRESSED);
781
782         litest_event(dev, EV_KEY, BTN_LEFT, 0);
783         litest_event(dev, EV_SYN, SYN_REPORT, 0);
784         litest_touch_up(dev, 0);
785
786         assert_button_event(li,
787                             BTN_RIGHT,
788                             LIBINPUT_BUTTON_STATE_RELEASED);
789
790         libinput_dispatch(li);
791
792         litest_assert_empty_queue(li);
793 }
794 END_TEST
795
796 START_TEST(clickpad_softbutton_left_tap_n_drag)
797 {
798         struct litest_device *dev = litest_current_device();
799         struct libinput *li = dev->libinput;
800
801         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
802
803         litest_drain_events(li);
804
805         /* Tap in left button area, then finger down, button click
806                 -> expect left button press/release and left button press
807            Release button, finger up
808                 -> expect right button release
809          */
810         litest_touch_down(dev, 0, 20, 90);
811         litest_touch_up(dev, 0);
812         litest_touch_down(dev, 0, 20, 90);
813         litest_event(dev, EV_KEY, BTN_LEFT, 1);
814         litest_event(dev, EV_SYN, SYN_REPORT, 0);
815
816         assert_button_event(li,
817                             BTN_LEFT,
818                             LIBINPUT_BUTTON_STATE_PRESSED);
819         assert_button_event(li,
820                             BTN_LEFT,
821                             LIBINPUT_BUTTON_STATE_RELEASED);
822         assert_button_event(li,
823                             BTN_LEFT,
824                             LIBINPUT_BUTTON_STATE_PRESSED);
825         litest_assert_empty_queue(li);
826
827         litest_event(dev, EV_KEY, BTN_LEFT, 0);
828         litest_event(dev, EV_SYN, SYN_REPORT, 0);
829         litest_touch_up(dev, 0);
830
831         assert_button_event(li,
832                             BTN_LEFT,
833                             LIBINPUT_BUTTON_STATE_RELEASED);
834         litest_assert_empty_queue(li);
835 }
836 END_TEST
837
838 START_TEST(clickpad_softbutton_right_tap_n_drag)
839 {
840         struct litest_device *dev = litest_current_device();
841         struct libinput *li = dev->libinput;
842
843         libinput_device_config_tap_set_enabled(dev->libinput_device, 1);
844
845         litest_drain_events(li);
846
847         /* Tap in right button area, then finger down, button click
848                 -> expect left button press/release and right button press
849            Release button, finger up
850                 -> expect right button release
851          */
852         litest_touch_down(dev, 0, 90, 90);
853         litest_touch_up(dev, 0);
854         litest_touch_down(dev, 0, 90, 90);
855         litest_event(dev, EV_KEY, BTN_LEFT, 1);
856         litest_event(dev, EV_SYN, SYN_REPORT, 0);
857
858         assert_button_event(li,
859                             BTN_LEFT,
860                             LIBINPUT_BUTTON_STATE_PRESSED);
861         assert_button_event(li,
862                             BTN_LEFT,
863                             LIBINPUT_BUTTON_STATE_RELEASED);
864         assert_button_event(li,
865                             BTN_RIGHT,
866                             LIBINPUT_BUTTON_STATE_PRESSED);
867         litest_assert_empty_queue(li);
868
869         litest_event(dev, EV_KEY, BTN_LEFT, 0);
870         litest_event(dev, EV_SYN, SYN_REPORT, 0);
871         litest_touch_up(dev, 0);
872
873         assert_button_event(li,
874                             BTN_RIGHT,
875                             LIBINPUT_BUTTON_STATE_RELEASED);
876         litest_assert_empty_queue(li);
877 }
878 END_TEST
879
880 START_TEST(clickpad_softbutton_left_1st_fg_move)
881 {
882         struct litest_device *dev = litest_current_device();
883         struct libinput *li = dev->libinput;
884         struct libinput_event *event;
885         double x = 0, y = 0;
886         int nevents = 0;
887
888         litest_drain_events(li);
889
890         /* One finger down in the left button area, button press
891                 -> expect a button event
892            Move finger up out of the area, wait for timeout
893            Move finger around diagonally down left
894                 -> expect motion events down left
895            Release finger
896                 -> expect a button event */
897
898         /* finger down, press in left button */
899         litest_touch_down(dev, 0, 20, 90);
900         litest_event(dev, EV_KEY, BTN_LEFT, 1);
901         litest_event(dev, EV_SYN, SYN_REPORT, 0);
902
903         assert_button_event(li,
904                             BTN_LEFT,
905                             LIBINPUT_BUTTON_STATE_PRESSED);
906         litest_assert_empty_queue(li);
907
908         /* move out of the area, then wait for softbutton timer */
909         litest_touch_move_to(dev, 0, 20, 90, 90, 20, 10);
910         libinput_dispatch(li);
911         msleep(400);
912         libinput_dispatch(li);
913         litest_drain_events(li);
914
915         /* move down left, expect motion */
916         litest_touch_move_to(dev, 0, 90, 20, 20, 90, 10);
917
918         libinput_dispatch(li);
919         event = libinput_get_event(li);
920         ck_assert(event != NULL);
921         while (event) {
922                 struct libinput_event_pointer *p;
923
924                 ck_assert_int_eq(libinput_event_get_type(event),
925                                  LIBINPUT_EVENT_POINTER_MOTION);
926                 p = libinput_event_get_pointer_event(event);
927
928                 /* we moved up/right, now down/left so the pointer accel
929                    code may lag behind with the dx/dy vectors. Hence, add up
930                    the x/y movements and expect that on average we moved
931                    left and down */
932                 x += libinput_event_pointer_get_dx(p);
933                 y += libinput_event_pointer_get_dy(p);
934                 nevents++;
935
936                 libinput_event_destroy(event);
937                 libinput_dispatch(li);
938                 event = libinput_get_event(li);
939         }
940
941         ck_assert(x/nevents < 0);
942         ck_assert(y/nevents > 0);
943
944         litest_event(dev, EV_KEY, BTN_LEFT, 0);
945         litest_event(dev, EV_SYN, SYN_REPORT, 0);
946         litest_touch_up(dev, 0);
947
948         assert_button_event(li,
949                             BTN_LEFT,
950                             LIBINPUT_BUTTON_STATE_RELEASED);
951
952         litest_assert_empty_queue(li);
953 }
954 END_TEST
955
956 START_TEST(clickpad_softbutton_left_2nd_fg_move)
957 {
958         struct litest_device *dev = litest_current_device();
959         struct libinput *li = dev->libinput;
960         struct libinput_event *event;
961
962         litest_drain_events(li);
963
964         /* One finger down in the left button area, button press
965                 -> expect a button event
966            Put a second finger down in the area, move it right, release
967                 -> expect motion events right
968            Put a second finger down in the area, move it down, release
969                 -> expect motion events down
970            Release second finger, release first finger
971                 -> expect a button event */
972         litest_touch_down(dev, 0, 20, 90);
973         litest_event(dev, EV_KEY, BTN_LEFT, 1);
974         litest_event(dev, EV_SYN, SYN_REPORT, 0);
975
976         assert_button_event(li,
977                             BTN_LEFT,
978                             LIBINPUT_BUTTON_STATE_PRESSED);
979         litest_assert_empty_queue(li);
980
981         litest_touch_down(dev, 1, 20, 20);
982         litest_touch_move_to(dev, 1, 20, 20, 80, 20, 10);
983
984         libinput_dispatch(li);
985         event = libinput_get_event(li);
986         ck_assert(event != NULL);
987         while (event) {
988                 struct libinput_event_pointer *p;
989                 double x, y;
990
991                 ck_assert_int_eq(libinput_event_get_type(event),
992                                  LIBINPUT_EVENT_POINTER_MOTION);
993                 p = libinput_event_get_pointer_event(event);
994
995                 x = libinput_event_pointer_get_dx(p);
996                 y = libinput_event_pointer_get_dy(p);
997
998                 ck_assert(x > 0);
999                 ck_assert(y == 0);
1000
1001                 libinput_event_destroy(event);
1002                 libinput_dispatch(li);
1003                 event = libinput_get_event(li);
1004         }
1005         litest_touch_up(dev, 1);
1006
1007         /* second finger down */
1008         litest_touch_down(dev, 1, 20, 20);
1009         litest_touch_move_to(dev, 1, 20, 20, 20, 80, 10);
1010
1011         libinput_dispatch(li);
1012         event = libinput_get_event(li);
1013         ck_assert(event != NULL);
1014         while (event) {
1015                 struct libinput_event_pointer *p;
1016                 double x, y;
1017
1018                 ck_assert_int_eq(libinput_event_get_type(event),
1019                                  LIBINPUT_EVENT_POINTER_MOTION);
1020                 p = libinput_event_get_pointer_event(event);
1021
1022                 x = libinput_event_pointer_get_dx(p);
1023                 y = libinput_event_pointer_get_dy(p);
1024
1025                 ck_assert(x == 0);
1026                 ck_assert(y > 0);
1027
1028                 libinput_event_destroy(event);
1029                 libinput_dispatch(li);
1030                 event = libinput_get_event(li);
1031         }
1032
1033         litest_touch_up(dev, 1);
1034
1035         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1036         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1037         litest_touch_up(dev, 0);
1038
1039         assert_button_event(li,
1040                             BTN_LEFT,
1041                             LIBINPUT_BUTTON_STATE_RELEASED);
1042
1043         litest_assert_empty_queue(li);
1044 }
1045 END_TEST
1046
1047 START_TEST(clickpad_softbutton_left_to_right)
1048 {
1049         struct litest_device *dev = litest_current_device();
1050         struct libinput *li = dev->libinput;
1051
1052         litest_drain_events(li);
1053
1054         /* One finger down in left software button area,
1055            move to right button area immediately, click
1056                 -> expect right button event
1057         */
1058
1059         litest_touch_down(dev, 0, 20, 90);
1060         litest_touch_move_to(dev, 0, 20, 90, 90, 90, 10);
1061         litest_event(dev, EV_KEY, BTN_LEFT, 1);
1062         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1063
1064         assert_button_event(li,
1065                             BTN_RIGHT,
1066                             LIBINPUT_BUTTON_STATE_PRESSED);
1067         litest_assert_empty_queue(li);
1068
1069         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1070         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1071         litest_touch_up(dev, 0);
1072
1073         assert_button_event(li,
1074                             BTN_RIGHT,
1075                             LIBINPUT_BUTTON_STATE_RELEASED);
1076
1077         litest_assert_empty_queue(li);
1078 }
1079 END_TEST
1080
1081 START_TEST(clickpad_softbutton_right_to_left)
1082 {
1083         struct litest_device *dev = litest_current_device();
1084         struct libinput *li = dev->libinput;
1085
1086         litest_drain_events(li);
1087
1088         /* One finger down in right software button area,
1089            move to left button area immediately, click
1090                 -> expect left button event
1091         */
1092
1093         litest_touch_down(dev, 0, 90, 90);
1094         litest_touch_move_to(dev, 0, 90, 90, 20, 90, 10);
1095         litest_event(dev, EV_KEY, BTN_LEFT, 1);
1096         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1097
1098         assert_button_event(li,
1099                             BTN_LEFT,
1100                             LIBINPUT_BUTTON_STATE_PRESSED);
1101         litest_assert_empty_queue(li);
1102
1103         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1104         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1105         litest_touch_up(dev, 0);
1106
1107         assert_button_event(li,
1108                             BTN_LEFT,
1109                             LIBINPUT_BUTTON_STATE_RELEASED);
1110
1111         litest_assert_empty_queue(li);
1112 }
1113 END_TEST
1114
1115 START_TEST(clickpad_topsoftbuttons_left)
1116 {
1117         struct litest_device *dev = litest_current_device();
1118         struct libinput *li = dev->libinput;
1119
1120         litest_drain_events(li);
1121
1122         litest_touch_down(dev, 0, 10, 5);
1123         litest_event(dev, EV_KEY, BTN_LEFT, 1);
1124         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1125
1126         assert_button_event(li,
1127                             BTN_LEFT,
1128                             LIBINPUT_BUTTON_STATE_PRESSED);
1129         litest_assert_empty_queue(li);
1130
1131         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1132         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1133         litest_touch_up(dev, 0);
1134
1135         assert_button_event(li,
1136                             BTN_LEFT,
1137                             LIBINPUT_BUTTON_STATE_RELEASED);
1138
1139         litest_assert_empty_queue(li);
1140 }
1141 END_TEST
1142
1143 START_TEST(clickpad_topsoftbuttons_right)
1144 {
1145         struct litest_device *dev = litest_current_device();
1146         struct libinput *li = dev->libinput;
1147
1148         litest_drain_events(li);
1149
1150         litest_touch_down(dev, 0, 90, 5);
1151         litest_event(dev, EV_KEY, BTN_LEFT, 1);
1152         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1153
1154         assert_button_event(li,
1155                             BTN_RIGHT,
1156                             LIBINPUT_BUTTON_STATE_PRESSED);
1157         litest_assert_empty_queue(li);
1158
1159         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1160         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1161         litest_touch_up(dev, 0);
1162
1163         assert_button_event(li,
1164                             BTN_RIGHT,
1165                             LIBINPUT_BUTTON_STATE_RELEASED);
1166
1167         litest_assert_empty_queue(li);
1168 }
1169 END_TEST
1170
1171 START_TEST(clickpad_topsoftbuttons_middle)
1172 {
1173         struct litest_device *dev = litest_current_device();
1174         struct libinput *li = dev->libinput;
1175
1176         litest_drain_events(li);
1177
1178         litest_touch_down(dev, 0, 50, 5);
1179         litest_event(dev, EV_KEY, BTN_LEFT, 1);
1180         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1181
1182         assert_button_event(li,
1183                             BTN_MIDDLE,
1184                             LIBINPUT_BUTTON_STATE_PRESSED);
1185         litest_assert_empty_queue(li);
1186
1187         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1188         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1189         litest_touch_up(dev, 0);
1190
1191         assert_button_event(li,
1192                             BTN_MIDDLE,
1193                             LIBINPUT_BUTTON_STATE_RELEASED);
1194
1195         litest_assert_empty_queue(li);
1196 }
1197 END_TEST
1198
1199 START_TEST(clickpad_topsoftbuttons_move_out_ignore)
1200 {
1201         struct litest_device *dev = litest_current_device();
1202         struct libinput *li = dev->libinput;
1203
1204         /* Finger down in top button area, wait past enter timeout
1205            Move into main area, wait past leave timeout
1206            Click
1207              -> expect no events
1208          */
1209
1210         litest_drain_events(li);
1211
1212         litest_touch_down(dev, 0, 50, 5);
1213         libinput_dispatch(li);
1214         msleep(200);
1215         libinput_dispatch(li);
1216         litest_assert_empty_queue(li);
1217
1218         litest_touch_move_to(dev, 0, 50, 5, 80, 90, 20);
1219         libinput_dispatch(li);
1220         msleep(400);
1221         libinput_dispatch(li);
1222
1223         litest_event(dev, EV_KEY, BTN_LEFT, 1);
1224         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1225         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1226         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1227
1228         litest_touch_up(dev, 0);
1229
1230         litest_assert_empty_queue(li);
1231 }
1232 END_TEST
1233
1234 static void
1235 test_2fg_scroll(struct litest_device *dev, double dx, double dy, int sleep)
1236 {
1237         struct libinput *li = dev->libinput;
1238
1239         litest_touch_down(dev, 0, 47, 50);
1240         litest_touch_down(dev, 1, 53, 50);
1241
1242         litest_touch_move_to(dev, 0, 47, 50, 47 + dx, 50 + dy, 5);
1243         litest_touch_move_to(dev, 1, 53, 50, 53 + dx, 50 + dy, 5);
1244
1245         /* Avoid a small scroll being seen as a tap */
1246         if (sleep) {
1247                 libinput_dispatch(li);
1248                 msleep(sleep);
1249                 libinput_dispatch(li);
1250         }
1251
1252         litest_touch_up(dev, 1);
1253         litest_touch_up(dev, 0);
1254
1255         libinput_dispatch(li);
1256 }
1257
1258 static void
1259 check_2fg_scroll(struct litest_device *dev, unsigned int axis, int dir)
1260 {
1261         struct libinput *li = dev->libinput;
1262         struct libinput_event *event, *next_event;
1263         struct libinput_event_pointer *ptrev;
1264
1265         event = libinput_get_event(li);
1266         next_event = libinput_get_event(li);
1267         ck_assert(next_event != NULL); /* At least 1 scroll + stop scroll */
1268
1269         while (event) {
1270                 ck_assert_int_eq(libinput_event_get_type(event),
1271                                  LIBINPUT_EVENT_POINTER_AXIS);
1272                 ptrev = libinput_event_get_pointer_event(event);
1273                 ck_assert(ptrev != NULL);
1274                 ck_assert_int_eq(libinput_event_pointer_get_axis(ptrev), axis);
1275
1276                 if (next_event) {
1277                         /* Normal scroll event, check dir */
1278                         if (dir > 0) {
1279                                 ck_assert_int_ge(
1280                                         libinput_event_pointer_get_axis_value(ptrev),
1281                                         dir);
1282                         } else {
1283                                 ck_assert_int_le(
1284                                         libinput_event_pointer_get_axis_value(ptrev),
1285                                         dir);
1286                         }
1287                 } else {
1288                         /* Last scroll event, must be 0 */
1289                         ck_assert_int_eq(
1290                                 libinput_event_pointer_get_axis_value(ptrev),
1291                                 0);
1292                 }
1293                 libinput_event_destroy(event);
1294                 event = next_event;
1295                 next_event = libinput_get_event(li);
1296         }
1297 }
1298
1299 START_TEST(touchpad_2fg_scroll)
1300 {
1301         struct litest_device *dev = litest_current_device();
1302         struct libinput *li = dev->libinput;
1303
1304         litest_drain_events(li);
1305
1306         test_2fg_scroll(dev, 0.1, 40, 0);
1307         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL, 10);
1308         test_2fg_scroll(dev, 0.1, -40, 0);
1309         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL, -10);
1310         test_2fg_scroll(dev, 40, 0.1, 0);
1311         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL, 10);
1312         test_2fg_scroll(dev, -40, 0.1, 0);
1313         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL, -10);
1314
1315         /* 2fg scroll smaller than the threshold should not generate events */
1316         test_2fg_scroll(dev, 0.1, 0.1, 200);
1317         litest_assert_empty_queue(li);
1318 }
1319 END_TEST
1320
1321 START_TEST(touchpad_tap_is_available)
1322 {
1323         struct litest_device *dev = litest_current_device();
1324
1325         ck_assert_int_ge(libinput_device_config_tap_get_finger_count(dev->libinput_device), 1);
1326         ck_assert_int_eq(libinput_device_config_tap_get_enabled(dev->libinput_device), 0);
1327 }
1328 END_TEST
1329
1330 START_TEST(touchpad_tap_is_not_available)
1331 {
1332         struct litest_device *dev = litest_current_device();
1333
1334         ck_assert_int_eq(libinput_device_config_tap_get_finger_count(dev->libinput_device), 0);
1335         ck_assert_int_eq(libinput_device_config_tap_get_enabled(dev->libinput_device), 0);
1336         ck_assert_int_eq(libinput_device_config_tap_set_enabled(dev->libinput_device, 1),
1337                          LIBINPUT_CONFIG_STATUS_UNSUPPORTED);
1338 }
1339 END_TEST
1340
1341 START_TEST(touchpad_tap_default)
1342 {
1343         struct litest_device *dev = litest_current_device();
1344
1345         ck_assert_int_eq(libinput_device_config_tap_get_default_enabled(dev->libinput_device), 0);
1346 }
1347 END_TEST
1348
1349 static int
1350 touchpad_has_palm_detect_size(struct litest_device *dev)
1351 {
1352         double width, height;
1353         int rc;
1354
1355         if (libinput_device_get_id_vendor(dev->libinput_device) == 0x5ac) /* Apple */
1356                 return 1;
1357
1358         rc = libinput_device_get_size(dev->libinput_device, &width, &height);
1359
1360         return rc == 0 && width >= 80;
1361 }
1362
1363 START_TEST(touchpad_palm_detect_at_edge)
1364 {
1365         struct litest_device *dev = litest_current_device();
1366         struct libinput *li = dev->libinput;
1367
1368         if (!touchpad_has_palm_detect_size(dev))
1369                 return;
1370
1371         litest_drain_events(li);
1372
1373         litest_touch_down(dev, 0, 99, 50);
1374         litest_touch_move_to(dev, 0, 99, 50, 99, 70, 5);
1375         litest_touch_up(dev, 0);
1376
1377         litest_assert_empty_queue(li);
1378
1379         litest_touch_down(dev, 0, 5, 50);
1380         litest_touch_move_to(dev, 0, 5, 50, 5, 70, 5);
1381         litest_touch_up(dev, 0);
1382 }
1383 END_TEST
1384
1385 START_TEST(touchpad_palm_detect_at_bottom_corners)
1386 {
1387         struct litest_device *dev = litest_current_device();
1388         struct libinput *li = dev->libinput;
1389
1390         if (!touchpad_has_palm_detect_size(dev))
1391                 return;
1392
1393         /* Run for non-clickpads only: make sure the bottom corners trigger
1394            palm detection too */
1395         litest_drain_events(li);
1396
1397         litest_touch_down(dev, 0, 99, 95);
1398         litest_touch_move_to(dev, 0, 99, 95, 99, 99, 10);
1399         litest_touch_up(dev, 0);
1400
1401         litest_assert_empty_queue(li);
1402
1403         litest_touch_down(dev, 0, 5, 95);
1404         litest_touch_move_to(dev, 0, 5, 95, 5, 99, 5);
1405         litest_touch_up(dev, 0);
1406 }
1407 END_TEST
1408
1409 START_TEST(touchpad_palm_detect_at_top_corners)
1410 {
1411         struct litest_device *dev = litest_current_device();
1412         struct libinput *li = dev->libinput;
1413
1414         if (!touchpad_has_palm_detect_size(dev))
1415                 return;
1416
1417         /* Run for non-clickpads only: make sure the bottom corners trigger
1418            palm detection too */
1419         litest_drain_events(li);
1420
1421         litest_touch_down(dev, 0, 99, 5);
1422         litest_touch_move_to(dev, 0, 99, 5, 99, 9, 10);
1423         litest_touch_up(dev, 0);
1424
1425         litest_assert_empty_queue(li);
1426
1427         litest_touch_down(dev, 0, 5, 5);
1428         litest_touch_move_to(dev, 0, 5, 5, 5, 9, 5);
1429         litest_touch_up(dev, 0);
1430 }
1431 END_TEST
1432
1433 START_TEST(touchpad_palm_detect_palm_stays_palm)
1434 {
1435         struct litest_device *dev = litest_current_device();
1436         struct libinput *li = dev->libinput;
1437
1438         if (!touchpad_has_palm_detect_size(dev))
1439                 return;
1440
1441         litest_drain_events(li);
1442
1443         litest_touch_down(dev, 0, 99, 20);
1444         litest_touch_move_to(dev, 0, 99, 20, 75, 99, 5);
1445         litest_touch_up(dev, 0);
1446         litest_assert_empty_queue(li);
1447 }
1448 END_TEST
1449
1450 START_TEST(touchpad_palm_detect_palm_becomes_pointer)
1451 {
1452         struct litest_device *dev = litest_current_device();
1453         struct libinput *li = dev->libinput;
1454         struct libinput_event *ev;
1455         enum libinput_event_type type;
1456
1457         if (!touchpad_has_palm_detect_size(dev))
1458                 return;
1459
1460         litest_drain_events(li);
1461
1462         litest_touch_down(dev, 0, 99, 50);
1463         litest_touch_move_to(dev, 0, 99, 70, 0, 70, 5);
1464         litest_touch_up(dev, 0);
1465
1466         libinput_dispatch(li);
1467
1468         ev = libinput_get_event(li);
1469         ck_assert_notnull(ev);
1470         do {
1471                 type = libinput_event_get_type(ev);
1472                 ck_assert_int_eq(type, LIBINPUT_EVENT_POINTER_MOTION);
1473
1474                 libinput_event_destroy(ev);
1475                 ev = libinput_get_event(li);
1476         } while (ev);
1477
1478         litest_assert_empty_queue(li);
1479 }
1480 END_TEST
1481
1482 START_TEST(touchpad_palm_detect_no_palm_moving_into_edges)
1483 {
1484         struct litest_device *dev = litest_current_device();
1485         struct libinput *li = dev->libinput;
1486         struct libinput_event *ev;
1487         enum libinput_event_type type;
1488
1489         if (!touchpad_has_palm_detect_size(dev))
1490                 return;
1491
1492         /* moving non-palm into the edge does not label it as palm */
1493         litest_drain_events(li);
1494
1495         litest_touch_down(dev, 0, 50, 50);
1496         litest_touch_move_to(dev, 0, 50, 50, 99, 50, 5);
1497
1498         litest_drain_events(li);
1499
1500         litest_touch_move_to(dev, 0, 99, 50, 99, 90, 5);
1501         libinput_dispatch(li);
1502
1503         type = libinput_next_event_type(li);
1504         do {
1505
1506                 ck_assert_int_eq(type, LIBINPUT_EVENT_POINTER_MOTION);
1507                 ev = libinput_get_event(li);
1508                 libinput_event_destroy(ev);
1509
1510                 type = libinput_next_event_type(li);
1511                 libinput_dispatch(li);
1512         } while (type != LIBINPUT_EVENT_NONE);
1513
1514         litest_touch_up(dev, 0);
1515         libinput_dispatch(li);
1516         litest_assert_empty_queue(li);
1517 }
1518 END_TEST
1519
1520 int main(int argc, char **argv) {
1521
1522         litest_add("touchpad:motion", touchpad_1fg_motion, LITEST_TOUCHPAD, LITEST_ANY);
1523         litest_add("touchpad:motion", touchpad_2fg_no_motion, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1524
1525         litest_add("touchpad:tap", touchpad_1fg_tap, LITEST_TOUCHPAD, LITEST_ANY);
1526         litest_add("touchpad:tap", touchpad_1fg_tap_n_drag, LITEST_TOUCHPAD, LITEST_ANY);
1527         litest_add("touchpad:tap", touchpad_2fg_tap, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1528         litest_add("touchpad:tap", touchpad_2fg_tap_inverted, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1529         litest_add("touchpad:tap", touchpad_1fg_tap_click, LITEST_TOUCHPAD, LITEST_ANY);
1530         litest_add("touchpad:tap", touchpad_2fg_tap_click, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH|LITEST_APPLE_CLICKPAD);
1531         litest_add("touchpad:tap", touchpad_2fg_tap_click_apple, LITEST_APPLE_CLICKPAD, LITEST_ANY);
1532         litest_add("touchpad:tap", touchpad_no_2fg_tap_after_move, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1533         litest_add("touchpad:tap", touchpad_no_2fg_tap_after_timeout, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1534         litest_add("touchpad:tap", touchpad_no_first_fg_tap_after_move, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1535         litest_add("touchpad:tap", touchpad_no_first_fg_tap_after_move, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1536         /* apple is the only one with real 3-finger support */
1537         litest_add("touchpad:tap", touchpad_3fg_tap_btntool, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH|LITEST_APPLE_CLICKPAD);
1538         litest_add("touchpad:tap", touchpad_3fg_tap_btntool_inverted, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH|LITEST_APPLE_CLICKPAD);
1539         litest_add("touchpad:tap", touchpad_3fg_tap, LITEST_APPLE_CLICKPAD, LITEST_ANY);
1540
1541         /* Real buttons don't interfere with tapping, so don't run those for
1542            pads with buttons */
1543         litest_add("touchpad:tap", touchpad_1fg_double_tap_click, LITEST_CLICKPAD, LITEST_ANY);
1544         litest_add("touchpad:tap", touchpad_1fg_tap_n_drag_click, LITEST_CLICKPAD, LITEST_ANY);
1545
1546         litest_add("touchpad:tap", touchpad_tap_default, LITEST_TOUCHPAD, LITEST_ANY);
1547         litest_add("touchpad:tap", touchpad_tap_is_available, LITEST_TOUCHPAD, LITEST_ANY);
1548         litest_add("touchpad:tap", touchpad_tap_is_not_available, LITEST_ANY, LITEST_TOUCHPAD);
1549
1550         litest_add_no_device("touchpad:clickfinger", touchpad_1fg_clickfinger);
1551         litest_add_no_device("touchpad:clickfinger", touchpad_2fg_clickfinger);
1552
1553         litest_add("touchpad:click", touchpad_btn_left, LITEST_TOUCHPAD, LITEST_CLICKPAD);
1554         litest_add("touchpad:click", clickpad_btn_left, LITEST_CLICKPAD, LITEST_ANY);
1555         litest_add("touchpad:click", clickpad_click_n_drag, LITEST_CLICKPAD, LITEST_SINGLE_TOUCH);
1556
1557         litest_add("touchpad:softbutton", clickpad_softbutton_left, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1558         litest_add("touchpad:softbutton", clickpad_softbutton_right, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1559         litest_add("touchpad:softbutton", clickpad_softbutton_left_tap_n_drag, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1560         litest_add("touchpad:softbutton", clickpad_softbutton_right_tap_n_drag, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1561         litest_add("touchpad:softbutton", clickpad_softbutton_left_1st_fg_move, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1562         litest_add("touchpad:softbutton", clickpad_softbutton_left_2nd_fg_move, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1563         litest_add("touchpad:softbutton", clickpad_softbutton_left_to_right, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1564         litest_add("touchpad:softbutton", clickpad_softbutton_right_to_left, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1565
1566         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_left, LITEST_TOPBUTTONPAD, LITEST_ANY);
1567         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_right, LITEST_TOPBUTTONPAD, LITEST_ANY);
1568         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_middle, LITEST_TOPBUTTONPAD, LITEST_ANY);
1569         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_move_out_ignore, LITEST_TOPBUTTONPAD, LITEST_ANY);
1570
1571         litest_add("touchpad:scroll", touchpad_2fg_scroll, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1572
1573         litest_add("touchpad:palm", touchpad_palm_detect_at_edge, LITEST_TOUCHPAD, LITEST_ANY);
1574         litest_add("touchpad:palm", touchpad_palm_detect_at_bottom_corners, LITEST_TOUCHPAD, LITEST_CLICKPAD);
1575         litest_add("touchpad:palm", touchpad_palm_detect_at_top_corners, LITEST_TOUCHPAD, LITEST_TOPBUTTONPAD);
1576         litest_add("touchpad:palm", touchpad_palm_detect_palm_becomes_pointer, LITEST_TOUCHPAD, LITEST_ANY);
1577         litest_add("touchpad:palm", touchpad_palm_detect_palm_stays_palm, LITEST_TOUCHPAD, LITEST_ANY);
1578         litest_add("touchpad:palm", touchpad_palm_detect_no_palm_moving_into_edges, LITEST_TOUCHPAD, LITEST_ANY);
1579
1580         return litest_run(argc, argv);
1581 }