Merge branch 'master' of git+ssh://git.freedesktop.org/git/wayland/libinput
[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, 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         litest_drain_events(li);
120
121         litest_touch_down(dev, 0, 50, 50);
122         litest_touch_up(dev, 0);
123
124         libinput_dispatch(li);
125
126         assert_button_event(li, BTN_LEFT,
127                             LIBINPUT_BUTTON_STATE_PRESSED);
128         msleep(300); /* tap-n-drag timeout */
129         assert_button_event(li, BTN_LEFT,
130                             LIBINPUT_BUTTON_STATE_RELEASED);
131
132         libinput_dispatch(li);
133         event = libinput_get_event(li);
134         ck_assert(event == NULL);
135 }
136 END_TEST
137
138 START_TEST(touchpad_1fg_tap_n_drag)
139 {
140         struct litest_device *dev = litest_current_device();
141         struct libinput *li = dev->libinput;
142         struct libinput_event *event;
143
144         litest_drain_events(li);
145
146         litest_touch_down(dev, 0, 50, 50);
147         litest_touch_up(dev, 0);
148         litest_touch_down(dev, 0, 50, 50);
149         litest_touch_move_to(dev, 0, 50, 50, 80, 80, 5);
150         litest_touch_up(dev, 0);
151
152         libinput_dispatch(li);
153
154         assert_button_event(li, BTN_LEFT,
155                             LIBINPUT_BUTTON_STATE_PRESSED);
156
157         libinput_dispatch(li);
158         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
159                 event = libinput_get_event(li);
160                 libinput_event_destroy(event);
161                 libinput_dispatch(li);
162         }
163
164         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
165
166         /* lift finger, set down again, should continue dragging */
167         litest_touch_down(dev, 0, 50, 50);
168         litest_touch_move_to(dev, 0, 50, 50, 80, 80, 5);
169         litest_touch_up(dev, 0);
170
171         libinput_dispatch(li);
172         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
173                 event = libinput_get_event(li);
174                 libinput_event_destroy(event);
175                 libinput_dispatch(li);
176         }
177
178         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
179
180         msleep(300); /* tap-n-drag timeout */
181
182         assert_button_event(li, BTN_LEFT,
183                             LIBINPUT_BUTTON_STATE_RELEASED);
184
185         litest_assert_empty_queue(li);
186 }
187 END_TEST
188
189 START_TEST(touchpad_2fg_tap)
190 {
191         struct litest_device *dev = litest_current_device();
192         struct libinput *li = dev->libinput;
193
194         litest_drain_events(dev->libinput);
195
196         litest_touch_down(dev, 0, 50, 50);
197         litest_touch_down(dev, 1, 70, 70);
198         litest_touch_up(dev, 0);
199         litest_touch_up(dev, 1);
200
201         libinput_dispatch(li);
202
203         assert_button_event(li, BTN_RIGHT,
204                             LIBINPUT_BUTTON_STATE_PRESSED);
205         msleep(300); /* tap-n-drag timeout */
206         assert_button_event(li, BTN_RIGHT,
207                             LIBINPUT_BUTTON_STATE_RELEASED);
208
209         litest_assert_empty_queue(li);
210 }
211 END_TEST
212
213 START_TEST(touchpad_2fg_tap_inverted)
214 {
215         struct litest_device *dev = litest_current_device();
216         struct libinput *li = dev->libinput;
217
218         litest_drain_events(dev->libinput);
219
220         litest_touch_down(dev, 0, 50, 50);
221         litest_touch_down(dev, 1, 70, 70);
222         litest_touch_up(dev, 1);
223         litest_touch_up(dev, 0);
224
225         libinput_dispatch(li);
226
227         assert_button_event(li, BTN_RIGHT,
228                             LIBINPUT_BUTTON_STATE_PRESSED);
229         msleep(300); /* tap-n-drag timeout */
230         assert_button_event(li, BTN_RIGHT,
231                             LIBINPUT_BUTTON_STATE_RELEASED);
232
233         litest_assert_empty_queue(li);
234 }
235 END_TEST
236
237 START_TEST(touchpad_1fg_tap_click)
238 {
239         struct litest_device *dev = litest_current_device();
240         struct libinput *li = dev->libinput;
241
242         litest_drain_events(dev->libinput);
243
244         /* finger down, button click, finger up
245            -> only one button left event pair */
246         litest_touch_down(dev, 0, 50, 50);
247         litest_event(dev, EV_KEY, BTN_LEFT, 1);
248         litest_event(dev, EV_SYN, SYN_REPORT, 0);
249         litest_event(dev, EV_KEY, BTN_LEFT, 0);
250         litest_event(dev, EV_SYN, SYN_REPORT, 0);
251         litest_touch_up(dev, 0);
252
253         libinput_dispatch(li);
254
255         assert_button_event(li, BTN_LEFT,
256                             LIBINPUT_BUTTON_STATE_PRESSED);
257         assert_button_event(li, BTN_LEFT,
258                             LIBINPUT_BUTTON_STATE_RELEASED);
259
260         litest_assert_empty_queue(li);
261 }
262 END_TEST
263
264 START_TEST(touchpad_2fg_tap_click)
265 {
266         struct litest_device *dev = litest_current_device();
267         struct libinput *li = dev->libinput;
268
269         litest_drain_events(dev->libinput);
270
271         /* two fingers down, button click, fingers up
272            -> only one button left event pair */
273         litest_touch_down(dev, 0, 50, 50);
274         litest_touch_down(dev, 1, 70, 50);
275         litest_event(dev, EV_KEY, BTN_LEFT, 1);
276         litest_event(dev, EV_SYN, SYN_REPORT, 0);
277         litest_event(dev, EV_KEY, BTN_LEFT, 0);
278         litest_event(dev, EV_SYN, SYN_REPORT, 0);
279         litest_touch_up(dev, 1);
280         litest_touch_up(dev, 0);
281
282         libinput_dispatch(li);
283
284         assert_button_event(li, BTN_LEFT,
285                             LIBINPUT_BUTTON_STATE_PRESSED);
286         assert_button_event(li, BTN_LEFT,
287                             LIBINPUT_BUTTON_STATE_RELEASED);
288
289         litest_assert_empty_queue(li);
290 }
291 END_TEST
292
293 START_TEST(touchpad_2fg_tap_click_apple)
294 {
295         struct litest_device *dev = litest_current_device();
296         struct libinput *li = dev->libinput;
297
298         litest_drain_events(dev->libinput);
299
300         /* two fingers down, button click, fingers up
301            -> only one button right event pair
302            (apple have clickfinger enabled by default) */
303         litest_touch_down(dev, 0, 50, 50);
304         litest_touch_down(dev, 1, 70, 50);
305         litest_event(dev, EV_KEY, BTN_LEFT, 1);
306         litest_event(dev, EV_SYN, SYN_REPORT, 0);
307         litest_event(dev, EV_KEY, BTN_LEFT, 0);
308         litest_event(dev, EV_SYN, SYN_REPORT, 0);
309         litest_touch_up(dev, 1);
310         litest_touch_up(dev, 0);
311
312         libinput_dispatch(li);
313
314         assert_button_event(li, BTN_RIGHT,
315                             LIBINPUT_BUTTON_STATE_PRESSED);
316         assert_button_event(li, BTN_RIGHT,
317                             LIBINPUT_BUTTON_STATE_RELEASED);
318
319         litest_assert_empty_queue(li);
320 }
321 END_TEST
322
323 START_TEST(touchpad_1fg_double_tap_click)
324 {
325         struct litest_device *dev = litest_current_device();
326         struct libinput *li = dev->libinput;
327
328         litest_drain_events(dev->libinput);
329
330         /* one finger down, up, down, button click, finger up
331            -> two button left event pairs */
332         litest_touch_down(dev, 0, 50, 50);
333         litest_touch_up(dev, 0);
334         litest_touch_down(dev, 0, 50, 50);
335         litest_event(dev, EV_KEY, BTN_LEFT, 1);
336         litest_event(dev, EV_SYN, SYN_REPORT, 0);
337         litest_event(dev, EV_KEY, BTN_LEFT, 0);
338         litest_event(dev, EV_SYN, SYN_REPORT, 0);
339         litest_touch_up(dev, 0);
340
341         libinput_dispatch(li);
342
343         assert_button_event(li, BTN_LEFT,
344                             LIBINPUT_BUTTON_STATE_PRESSED);
345         assert_button_event(li, BTN_LEFT,
346                             LIBINPUT_BUTTON_STATE_RELEASED);
347         assert_button_event(li, BTN_LEFT,
348                             LIBINPUT_BUTTON_STATE_PRESSED);
349         assert_button_event(li, BTN_LEFT,
350                             LIBINPUT_BUTTON_STATE_RELEASED);
351
352         litest_assert_empty_queue(li);
353 }
354 END_TEST
355
356 START_TEST(touchpad_1fg_tap_n_drag_click)
357 {
358         struct litest_device *dev = litest_current_device();
359         struct libinput *li = dev->libinput;
360         struct libinput_event *event;
361
362         litest_drain_events(dev->libinput);
363
364         /* one finger down, up, down, move, button click, finger up
365            -> two button left event pairs, motion allowed */
366         litest_touch_down(dev, 0, 50, 50);
367         litest_touch_up(dev, 0);
368         litest_touch_down(dev, 0, 50, 50);
369         litest_touch_move_to(dev, 0, 50, 50, 80, 50, 10);
370
371         assert_button_event(li, BTN_LEFT,
372                             LIBINPUT_BUTTON_STATE_PRESSED);
373
374         libinput_dispatch(li);
375
376         ck_assert_int_eq(libinput_next_event_type(li),
377                          LIBINPUT_EVENT_POINTER_MOTION);
378         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
379                 event = libinput_get_event(li);
380                 libinput_event_destroy(event);
381                 libinput_dispatch(li);
382         }
383
384         litest_event(dev, EV_KEY, BTN_LEFT, 1);
385         litest_event(dev, EV_SYN, SYN_REPORT, 0);
386
387         assert_button_event(li, BTN_LEFT,
388                             LIBINPUT_BUTTON_STATE_RELEASED);
389         assert_button_event(li, BTN_LEFT,
390                             LIBINPUT_BUTTON_STATE_PRESSED);
391
392         litest_event(dev, EV_KEY, BTN_LEFT, 0);
393         litest_event(dev, EV_SYN, SYN_REPORT, 0);
394         litest_touch_up(dev, 0);
395
396         libinput_dispatch(li);
397
398         assert_button_event(li, BTN_LEFT,
399                             LIBINPUT_BUTTON_STATE_RELEASED);
400
401         litest_assert_empty_queue(li);
402 }
403 END_TEST
404
405 START_TEST(touchpad_1fg_clickfinger)
406 {
407         struct litest_device *dev = litest_create_device(LITEST_BCM5974);
408         struct libinput *li = dev->libinput;
409
410         litest_drain_events(li);
411
412         litest_touch_down(dev, 0, 50, 50);
413         litest_event(dev, EV_KEY, BTN_LEFT, 1);
414         litest_event(dev, EV_SYN, SYN_REPORT, 0);
415         litest_event(dev, EV_KEY, BTN_LEFT, 0);
416         litest_event(dev, EV_SYN, SYN_REPORT, 0);
417         litest_touch_up(dev, 0);
418
419         libinput_dispatch(li);
420
421         assert_button_event(li, BTN_LEFT,
422                             LIBINPUT_BUTTON_STATE_PRESSED);
423         assert_button_event(li, BTN_LEFT,
424                             LIBINPUT_BUTTON_STATE_RELEASED);
425
426         litest_delete_device(dev);
427 }
428 END_TEST
429
430 START_TEST(touchpad_2fg_clickfinger)
431 {
432         struct litest_device *dev = litest_create_device(LITEST_BCM5974);
433         struct libinput *li = dev->libinput;
434
435         litest_drain_events(li);
436
437         litest_touch_down(dev, 0, 50, 50);
438         litest_touch_down(dev, 1, 70, 70);
439         litest_event(dev, EV_KEY, BTN_LEFT, 1);
440         litest_event(dev, EV_SYN, SYN_REPORT, 0);
441         litest_event(dev, EV_KEY, BTN_LEFT, 0);
442         litest_event(dev, EV_SYN, SYN_REPORT, 0);
443         litest_touch_up(dev, 0);
444         litest_touch_up(dev, 1);
445
446         libinput_dispatch(li);
447
448         assert_button_event(li, BTN_RIGHT,
449                             LIBINPUT_BUTTON_STATE_PRESSED);
450         assert_button_event(li, BTN_RIGHT,
451                             LIBINPUT_BUTTON_STATE_RELEASED);
452
453         litest_delete_device(dev);
454 }
455 END_TEST
456
457 START_TEST(touchpad_btn_left)
458 {
459         struct litest_device *dev = litest_current_device();
460         struct libinput *li = dev->libinput;
461
462         litest_drain_events(li);
463
464         litest_event(dev, EV_KEY, BTN_LEFT, 1);
465         litest_event(dev, EV_SYN, SYN_REPORT, 0);
466         litest_event(dev, EV_KEY, BTN_LEFT, 0);
467         litest_event(dev, EV_SYN, SYN_REPORT, 0);
468
469         libinput_dispatch(li);
470
471         assert_button_event(li, BTN_LEFT,
472                             LIBINPUT_BUTTON_STATE_PRESSED);
473         assert_button_event(li, BTN_LEFT,
474                             LIBINPUT_BUTTON_STATE_RELEASED);
475 }
476 END_TEST
477
478 START_TEST(clickpad_btn_left)
479 {
480         struct litest_device *dev = litest_current_device();
481         struct libinput *li = dev->libinput;
482
483         litest_drain_events(li);
484
485         /* A clickpad always needs a finger down to tell where the
486            click happens */
487         litest_event(dev, EV_KEY, BTN_LEFT, 1);
488         litest_event(dev, EV_SYN, SYN_REPORT, 0);
489         litest_event(dev, EV_KEY, BTN_LEFT, 0);
490         litest_event(dev, EV_SYN, SYN_REPORT, 0);
491
492         libinput_dispatch(li);
493         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
494 }
495 END_TEST
496
497 START_TEST(clickpad_click_n_drag)
498 {
499         struct litest_device *dev = litest_current_device();
500         struct libinput *li = dev->libinput;
501         struct libinput_event *event;
502
503         litest_drain_events(li);
504
505         litest_touch_down(dev, 0, 50, 50);
506         litest_event(dev, EV_KEY, BTN_LEFT, 1);
507         litest_event(dev, EV_SYN, SYN_REPORT, 0);
508
509         libinput_dispatch(li);
510         assert_button_event(li, BTN_LEFT,
511                             LIBINPUT_BUTTON_STATE_PRESSED);
512
513         libinput_dispatch(li);
514         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
515
516         /* now put a second finger down */
517         litest_touch_down(dev, 1, 70, 70);
518         litest_touch_move_to(dev, 1, 70, 70, 80, 50, 5);
519         litest_touch_up(dev, 1);
520
521         libinput_dispatch(li);
522         ck_assert_int_eq(libinput_next_event_type(li),
523                          LIBINPUT_EVENT_POINTER_MOTION);
524         do {
525                 event = libinput_get_event(li);
526                 libinput_event_destroy(event);
527                 libinput_dispatch(li);
528         } while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION);
529
530         litest_event(dev, EV_KEY, BTN_LEFT, 0);
531         litest_event(dev, EV_SYN, SYN_REPORT, 0);
532         litest_touch_up(dev, 0);
533
534         assert_button_event(li, BTN_LEFT,
535                             LIBINPUT_BUTTON_STATE_RELEASED);
536 }
537 END_TEST
538
539 START_TEST(clickpad_softbutton_left)
540 {
541         struct litest_device *dev = litest_current_device();
542         struct libinput *li = dev->libinput;
543
544         litest_drain_events(li);
545
546         litest_touch_down(dev, 0, 10, 90);
547         litest_event(dev, EV_KEY, BTN_LEFT, 1);
548         litest_event(dev, EV_SYN, SYN_REPORT, 0);
549
550         assert_button_event(li,
551                             BTN_LEFT,
552                             LIBINPUT_BUTTON_STATE_PRESSED);
553
554         litest_event(dev, EV_KEY, BTN_LEFT, 0);
555         litest_event(dev, EV_SYN, SYN_REPORT, 0);
556         litest_touch_up(dev, 0);
557
558         assert_button_event(li,
559                             BTN_LEFT,
560                             LIBINPUT_BUTTON_STATE_RELEASED);
561
562         libinput_dispatch(li);
563
564         litest_assert_empty_queue(li);
565 }
566 END_TEST
567
568 START_TEST(clickpad_softbutton_right)
569 {
570         struct litest_device *dev = litest_current_device();
571         struct libinput *li = dev->libinput;
572
573         litest_drain_events(li);
574
575         litest_touch_down(dev, 0, 90, 90);
576         litest_event(dev, EV_KEY, BTN_LEFT, 1);
577         litest_event(dev, EV_SYN, SYN_REPORT, 0);
578
579         assert_button_event(li,
580                             BTN_RIGHT,
581                             LIBINPUT_BUTTON_STATE_PRESSED);
582
583         litest_event(dev, EV_KEY, BTN_LEFT, 0);
584         litest_event(dev, EV_SYN, SYN_REPORT, 0);
585         litest_touch_up(dev, 0);
586
587         assert_button_event(li,
588                             BTN_RIGHT,
589                             LIBINPUT_BUTTON_STATE_RELEASED);
590
591         libinput_dispatch(li);
592
593         litest_assert_empty_queue(li);
594 }
595 END_TEST
596
597 START_TEST(clickpad_softbutton_left_tap_n_drag)
598 {
599         struct litest_device *dev = litest_current_device();
600         struct libinput *li = dev->libinput;
601
602         litest_drain_events(li);
603
604         /* Tap in left button area, then finger down, button click
605                 -> expect left button press/release and left button press
606            Release button, finger up
607                 -> expect right button release
608          */
609         litest_touch_down(dev, 0, 20, 90);
610         litest_touch_up(dev, 0);
611         litest_touch_down(dev, 0, 20, 90);
612         litest_event(dev, EV_KEY, BTN_LEFT, 1);
613         litest_event(dev, EV_SYN, SYN_REPORT, 0);
614
615         assert_button_event(li,
616                             BTN_LEFT,
617                             LIBINPUT_BUTTON_STATE_PRESSED);
618         assert_button_event(li,
619                             BTN_LEFT,
620                             LIBINPUT_BUTTON_STATE_RELEASED);
621         assert_button_event(li,
622                             BTN_LEFT,
623                             LIBINPUT_BUTTON_STATE_PRESSED);
624         litest_assert_empty_queue(li);
625
626         litest_event(dev, EV_KEY, BTN_LEFT, 0);
627         litest_event(dev, EV_SYN, SYN_REPORT, 0);
628         litest_touch_up(dev, 0);
629
630         assert_button_event(li,
631                             BTN_LEFT,
632                             LIBINPUT_BUTTON_STATE_RELEASED);
633         litest_assert_empty_queue(li);
634 }
635 END_TEST
636
637 START_TEST(clickpad_softbutton_right_tap_n_drag)
638 {
639         struct litest_device *dev = litest_current_device();
640         struct libinput *li = dev->libinput;
641
642         litest_drain_events(li);
643
644         /* Tap in right button area, then finger down, button click
645                 -> expect left button press/release and right button press
646            Release button, finger up
647                 -> expect right button release
648          */
649         litest_touch_down(dev, 0, 90, 90);
650         litest_touch_up(dev, 0);
651         litest_touch_down(dev, 0, 90, 90);
652         litest_event(dev, EV_KEY, BTN_LEFT, 1);
653         litest_event(dev, EV_SYN, SYN_REPORT, 0);
654
655         assert_button_event(li,
656                             BTN_LEFT,
657                             LIBINPUT_BUTTON_STATE_PRESSED);
658         assert_button_event(li,
659                             BTN_LEFT,
660                             LIBINPUT_BUTTON_STATE_RELEASED);
661         assert_button_event(li,
662                             BTN_RIGHT,
663                             LIBINPUT_BUTTON_STATE_PRESSED);
664         litest_assert_empty_queue(li);
665
666         litest_event(dev, EV_KEY, BTN_LEFT, 0);
667         litest_event(dev, EV_SYN, SYN_REPORT, 0);
668         litest_touch_up(dev, 0);
669
670         assert_button_event(li,
671                             BTN_RIGHT,
672                             LIBINPUT_BUTTON_STATE_RELEASED);
673         litest_assert_empty_queue(li);
674 }
675 END_TEST
676
677 START_TEST(clickpad_softbutton_left_1st_fg_move)
678 {
679         struct litest_device *dev = litest_current_device();
680         struct libinput *li = dev->libinput;
681         struct libinput_event *event;
682         double x = 0, y = 0;
683         int nevents = 0;
684
685         litest_drain_events(li);
686
687         /* One finger down in the left button area, button press
688                 -> expect a button event
689            Move finger up out of the area, wait for timeout
690            Move finger around diagonally down left
691                 -> expect motion events down left
692            Release finger
693                 -> expect a button event */
694
695         /* finger down, press in left button */
696         litest_touch_down(dev, 0, 20, 90);
697         litest_event(dev, EV_KEY, BTN_LEFT, 1);
698         litest_event(dev, EV_SYN, SYN_REPORT, 0);
699
700         assert_button_event(li,
701                             BTN_LEFT,
702                             LIBINPUT_BUTTON_STATE_PRESSED);
703         litest_assert_empty_queue(li);
704
705         /* move out of the area, then wait for softbutton timer */
706         litest_touch_move_to(dev, 0, 20, 90, 90, 20, 10);
707         libinput_dispatch(li);
708         msleep(400);
709         libinput_dispatch(li);
710         litest_drain_events(li);
711
712         /* move down left, expect motion */
713         litest_touch_move_to(dev, 0, 90, 20, 20, 90, 10);
714
715         libinput_dispatch(li);
716         event = libinput_get_event(li);
717         ck_assert(event != NULL);
718         while (event) {
719                 struct libinput_event_pointer *p;
720
721                 ck_assert_int_eq(libinput_event_get_type(event),
722                                  LIBINPUT_EVENT_POINTER_MOTION);
723                 p = libinput_event_get_pointer_event(event);
724
725                 /* we moved up/right, now down/left so the pointer accel
726                    code may lag behind with the dx/dy vectors. Hence, add up
727                    the x/y movements and expect that on average we moved
728                    left and down */
729                 x += libinput_event_pointer_get_dx(p);
730                 y += libinput_event_pointer_get_dy(p);
731                 nevents++;
732
733                 libinput_event_destroy(event);
734                 libinput_dispatch(li);
735                 event = libinput_get_event(li);
736         }
737
738         ck_assert(x/nevents < 0);
739         ck_assert(y/nevents > 0);
740
741         litest_event(dev, EV_KEY, BTN_LEFT, 0);
742         litest_event(dev, EV_SYN, SYN_REPORT, 0);
743         litest_touch_up(dev, 0);
744
745         assert_button_event(li,
746                             BTN_LEFT,
747                             LIBINPUT_BUTTON_STATE_RELEASED);
748
749         litest_assert_empty_queue(li);
750 }
751 END_TEST
752
753 START_TEST(clickpad_softbutton_left_2nd_fg_move)
754 {
755         struct litest_device *dev = litest_current_device();
756         struct libinput *li = dev->libinput;
757         struct libinput_event *event;
758
759         litest_drain_events(li);
760
761         /* One finger down in the left button area, button press
762                 -> expect a button event
763            Put a second finger down in the area, move it right, release
764                 -> expect motion events right
765            Put a second finger down in the area, move it down, release
766                 -> expect motion events down
767            Release second finger, release first finger
768                 -> expect a button event */
769         litest_touch_down(dev, 0, 20, 90);
770         litest_event(dev, EV_KEY, BTN_LEFT, 1);
771         litest_event(dev, EV_SYN, SYN_REPORT, 0);
772
773         assert_button_event(li,
774                             BTN_LEFT,
775                             LIBINPUT_BUTTON_STATE_PRESSED);
776         litest_assert_empty_queue(li);
777
778         litest_touch_down(dev, 1, 20, 20);
779         litest_touch_move_to(dev, 1, 20, 20, 80, 20, 10);
780
781         libinput_dispatch(li);
782         event = libinput_get_event(li);
783         ck_assert(event != NULL);
784         while (event) {
785                 struct libinput_event_pointer *p;
786                 double x, y;
787
788                 ck_assert_int_eq(libinput_event_get_type(event),
789                                  LIBINPUT_EVENT_POINTER_MOTION);
790                 p = libinput_event_get_pointer_event(event);
791
792                 x = libinput_event_pointer_get_dx(p);
793                 y = libinput_event_pointer_get_dy(p);
794
795                 ck_assert(x > 0);
796                 ck_assert(y == 0);
797
798                 libinput_event_destroy(event);
799                 libinput_dispatch(li);
800                 event = libinput_get_event(li);
801         }
802         litest_touch_up(dev, 1);
803
804         /* second finger down */
805         litest_touch_down(dev, 1, 20, 20);
806         litest_touch_move_to(dev, 1, 20, 20, 20, 80, 10);
807
808         libinput_dispatch(li);
809         event = libinput_get_event(li);
810         ck_assert(event != NULL);
811         while (event) {
812                 struct libinput_event_pointer *p;
813                 double x, y;
814
815                 ck_assert_int_eq(libinput_event_get_type(event),
816                                  LIBINPUT_EVENT_POINTER_MOTION);
817                 p = libinput_event_get_pointer_event(event);
818
819                 x = libinput_event_pointer_get_dx(p);
820                 y = libinput_event_pointer_get_dy(p);
821
822                 ck_assert(x == 0);
823                 ck_assert(y > 0);
824
825                 libinput_event_destroy(event);
826                 libinput_dispatch(li);
827                 event = libinput_get_event(li);
828         }
829
830         litest_event(dev, EV_KEY, BTN_LEFT, 0);
831         litest_event(dev, EV_SYN, SYN_REPORT, 0);
832         litest_touch_up(dev, 0);
833
834         assert_button_event(li,
835                             BTN_LEFT,
836                             LIBINPUT_BUTTON_STATE_RELEASED);
837
838         litest_assert_empty_queue(li);
839 }
840 END_TEST
841
842 START_TEST(clickpad_softbutton_left_to_right)
843 {
844         struct litest_device *dev = litest_current_device();
845         struct libinput *li = dev->libinput;
846
847         litest_drain_events(li);
848
849         /* One finger down in left software button area,
850            move to right button area immediately, click
851                 -> expect right button event
852         */
853
854         litest_touch_down(dev, 0, 20, 90);
855         litest_touch_move_to(dev, 0, 20, 90, 90, 90, 10);
856         litest_event(dev, EV_KEY, BTN_LEFT, 1);
857         litest_event(dev, EV_SYN, SYN_REPORT, 0);
858
859         assert_button_event(li,
860                             BTN_RIGHT,
861                             LIBINPUT_BUTTON_STATE_PRESSED);
862         litest_assert_empty_queue(li);
863
864         litest_event(dev, EV_KEY, BTN_LEFT, 0);
865         litest_event(dev, EV_SYN, SYN_REPORT, 0);
866         litest_touch_up(dev, 0);
867
868         assert_button_event(li,
869                             BTN_RIGHT,
870                             LIBINPUT_BUTTON_STATE_RELEASED);
871
872         litest_assert_empty_queue(li);
873 }
874 END_TEST
875
876 START_TEST(clickpad_softbutton_right_to_left)
877 {
878         struct litest_device *dev = litest_current_device();
879         struct libinput *li = dev->libinput;
880
881         litest_drain_events(li);
882
883         /* One finger down in right software button area,
884            move to left button area immediately, click
885                 -> expect left button event
886         */
887
888         litest_touch_down(dev, 0, 90, 90);
889         litest_touch_move_to(dev, 0, 90, 90, 20, 90, 10);
890         litest_event(dev, EV_KEY, BTN_LEFT, 1);
891         litest_event(dev, EV_SYN, SYN_REPORT, 0);
892
893         assert_button_event(li,
894                             BTN_LEFT,
895                             LIBINPUT_BUTTON_STATE_PRESSED);
896         litest_assert_empty_queue(li);
897
898         litest_event(dev, EV_KEY, BTN_LEFT, 0);
899         litest_event(dev, EV_SYN, SYN_REPORT, 0);
900         litest_touch_up(dev, 0);
901
902         assert_button_event(li,
903                             BTN_LEFT,
904                             LIBINPUT_BUTTON_STATE_RELEASED);
905
906         litest_assert_empty_queue(li);
907 }
908 END_TEST
909
910 START_TEST(clickpad_topsoftbuttons_left)
911 {
912         struct litest_device *dev = litest_current_device();
913         struct libinput *li = dev->libinput;
914
915         litest_drain_events(li);
916
917         litest_touch_down(dev, 0, 10, 5);
918         litest_event(dev, EV_KEY, BTN_LEFT, 1);
919         litest_event(dev, EV_SYN, SYN_REPORT, 0);
920
921         assert_button_event(li,
922                             BTN_LEFT,
923                             LIBINPUT_BUTTON_STATE_PRESSED);
924         litest_assert_empty_queue(li);
925
926         litest_event(dev, EV_KEY, BTN_LEFT, 0);
927         litest_event(dev, EV_SYN, SYN_REPORT, 0);
928         litest_touch_up(dev, 0);
929
930         assert_button_event(li,
931                             BTN_LEFT,
932                             LIBINPUT_BUTTON_STATE_RELEASED);
933
934         litest_assert_empty_queue(li);
935 }
936 END_TEST
937
938 START_TEST(clickpad_topsoftbuttons_right)
939 {
940         struct litest_device *dev = litest_current_device();
941         struct libinput *li = dev->libinput;
942
943         litest_drain_events(li);
944
945         litest_touch_down(dev, 0, 90, 5);
946         litest_event(dev, EV_KEY, BTN_LEFT, 1);
947         litest_event(dev, EV_SYN, SYN_REPORT, 0);
948
949         assert_button_event(li,
950                             BTN_RIGHT,
951                             LIBINPUT_BUTTON_STATE_PRESSED);
952         litest_assert_empty_queue(li);
953
954         litest_event(dev, EV_KEY, BTN_LEFT, 0);
955         litest_event(dev, EV_SYN, SYN_REPORT, 0);
956         litest_touch_up(dev, 0);
957
958         assert_button_event(li,
959                             BTN_RIGHT,
960                             LIBINPUT_BUTTON_STATE_RELEASED);
961
962         litest_assert_empty_queue(li);
963 }
964 END_TEST
965
966 START_TEST(clickpad_topsoftbuttons_middle)
967 {
968         struct litest_device *dev = litest_current_device();
969         struct libinput *li = dev->libinput;
970
971         litest_drain_events(li);
972
973         litest_touch_down(dev, 0, 50, 5);
974         litest_event(dev, EV_KEY, BTN_LEFT, 1);
975         litest_event(dev, EV_SYN, SYN_REPORT, 0);
976
977         assert_button_event(li,
978                             BTN_MIDDLE,
979                             LIBINPUT_BUTTON_STATE_PRESSED);
980         litest_assert_empty_queue(li);
981
982         litest_event(dev, EV_KEY, BTN_LEFT, 0);
983         litest_event(dev, EV_SYN, SYN_REPORT, 0);
984         litest_touch_up(dev, 0);
985
986         assert_button_event(li,
987                             BTN_MIDDLE,
988                             LIBINPUT_BUTTON_STATE_RELEASED);
989
990         litest_assert_empty_queue(li);
991 }
992 END_TEST
993
994 START_TEST(clickpad_topsoftbuttons_move_out_ignore)
995 {
996         struct litest_device *dev = litest_current_device();
997         struct libinput *li = dev->libinput;
998
999         /* Finger down in top button area, wait past enter timeout
1000            Move into main area, wait past leave timeout
1001            Click
1002              -> expect no events
1003          */
1004
1005         litest_drain_events(li);
1006
1007         litest_touch_down(dev, 0, 50, 5);
1008         libinput_dispatch(li);
1009         msleep(200);
1010         libinput_dispatch(li);
1011         litest_assert_empty_queue(li);
1012
1013         litest_touch_move_to(dev, 0, 50, 5, 80, 90, 20);
1014         libinput_dispatch(li);
1015         msleep(400);
1016         libinput_dispatch(li);
1017
1018         litest_event(dev, EV_KEY, BTN_LEFT, 1);
1019         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1020         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1021         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1022
1023         litest_touch_up(dev, 0);
1024
1025         litest_assert_empty_queue(li);
1026 }
1027 END_TEST
1028
1029 static void
1030 test_2fg_scroll(struct litest_device *dev, int dx, int dy, int sleep)
1031 {
1032         struct libinput *li = dev->libinput;
1033
1034         litest_touch_down(dev, 0, 47, 50);
1035         litest_touch_down(dev, 1, 53, 50);
1036
1037         litest_touch_move_to(dev, 0, 47, 50, 47 + dx, 50 + dy, 5);
1038         litest_touch_move_to(dev, 1, 53, 50, 53 + dx, 50 + dy, 5);
1039
1040         /* Avoid a small scroll being seen as a tap */
1041         if (sleep) {
1042                 libinput_dispatch(li);
1043                 msleep(sleep);
1044                 libinput_dispatch(li);
1045         }
1046
1047         litest_touch_up(dev, 1);
1048         litest_touch_up(dev, 0);
1049
1050         libinput_dispatch(li);
1051 }
1052
1053 static void
1054 check_2fg_scroll(struct litest_device *dev, int axis, int dir)
1055 {
1056         struct libinput *li = dev->libinput;
1057         struct libinput_event *event, *next_event;
1058         struct libinput_event_pointer *ptrev;
1059
1060         event = libinput_get_event(li);
1061         next_event = libinput_get_event(li);
1062         ck_assert(next_event != NULL); /* At least 1 scroll + stop scroll */
1063
1064         while (event) {
1065                 ck_assert_int_eq(libinput_event_get_type(event),
1066                                  LIBINPUT_EVENT_POINTER_AXIS);
1067                 ptrev = libinput_event_get_pointer_event(event);
1068                 ck_assert(ptrev != NULL);
1069                 ck_assert_int_eq(libinput_event_pointer_get_axis(ptrev), axis);
1070
1071                 if (next_event) {
1072                         /* Normal scroll event, check dir */
1073                         if (dir > 0) {
1074                                 ck_assert_int_ge(
1075                                         libinput_event_pointer_get_axis_value(ptrev),
1076                                         dir);
1077                         } else {
1078                                 ck_assert_int_le(
1079                                         libinput_event_pointer_get_axis_value(ptrev),
1080                                         dir);
1081                         }
1082                 } else {
1083                         /* Last scroll event, must be 0 */
1084                         ck_assert_int_eq(
1085                                 libinput_event_pointer_get_axis_value(ptrev),
1086                                 0);
1087                 }
1088                 libinput_event_destroy(event);
1089                 event = next_event;
1090                 next_event = libinput_get_event(li);
1091         }
1092 }
1093
1094 START_TEST(touchpad_2fg_scroll)
1095 {
1096         struct litest_device *dev = litest_current_device();
1097         struct libinput *li = dev->libinput;
1098
1099         litest_drain_events(li);
1100
1101         /* Note this mixes in a tiny amount of movement in the wrong direction,
1102            which should be ignored */
1103         test_2fg_scroll(dev, 1, 40, 0);
1104         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL, 10);
1105         test_2fg_scroll(dev, 1, -40, 0);
1106         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL, -10);
1107         test_2fg_scroll(dev, 40, 1, 0);
1108         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL, 10);
1109         test_2fg_scroll(dev, -40, 1, 0);
1110         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL, -10);
1111
1112         /* 2fg scroll smaller than the threshold should not generate events */
1113         test_2fg_scroll(dev, 1, 1, 200);
1114         litest_assert_empty_queue(li);
1115 }
1116 END_TEST
1117
1118 int main(int argc, char **argv) {
1119
1120         litest_add("touchpad:motion", touchpad_1fg_motion, LITEST_TOUCHPAD, LITEST_ANY);
1121         litest_add("touchpad:motion", touchpad_2fg_no_motion, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1122
1123         litest_add("touchpad:tap", touchpad_1fg_tap, LITEST_TOUCHPAD, LITEST_ANY);
1124         litest_add("touchpad:tap", touchpad_1fg_tap_n_drag, LITEST_TOUCHPAD, LITEST_ANY);
1125         litest_add("touchpad:tap", touchpad_2fg_tap, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1126         litest_add("touchpad:tap", touchpad_2fg_tap_inverted, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1127         litest_add("touchpad:tap", touchpad_1fg_tap_click, LITEST_TOUCHPAD, LITEST_ANY);
1128         litest_add("touchpad:tap", touchpad_2fg_tap_click, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH|LITEST_APPLE_CLICKPAD);
1129         litest_add("touchpad:tap", touchpad_2fg_tap_click_apple, LITEST_APPLE_CLICKPAD, LITEST_ANY);
1130         /* Real buttons don't interfere with tapping, so don't run those for
1131            pads with buttons */
1132         litest_add("touchpad:tap", touchpad_1fg_double_tap_click, LITEST_CLICKPAD, LITEST_ANY);
1133         litest_add("touchpad:tap", touchpad_1fg_tap_n_drag_click, LITEST_CLICKPAD, LITEST_ANY);
1134
1135         litest_add_no_device("touchpad:clickfinger", touchpad_1fg_clickfinger);
1136         litest_add_no_device("touchpad:clickfinger", touchpad_2fg_clickfinger);
1137
1138         litest_add("touchpad:click", touchpad_btn_left, LITEST_TOUCHPAD, LITEST_CLICKPAD);
1139         litest_add("touchpad:click", clickpad_btn_left, LITEST_CLICKPAD, LITEST_ANY);
1140         litest_add("touchpad:click", clickpad_click_n_drag, LITEST_CLICKPAD, LITEST_SINGLE_TOUCH);
1141
1142         litest_add("touchpad:softbutton", clickpad_softbutton_left, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1143         litest_add("touchpad:softbutton", clickpad_softbutton_right, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1144         litest_add("touchpad:softbutton", clickpad_softbutton_left_tap_n_drag, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1145         litest_add("touchpad:softbutton", clickpad_softbutton_right_tap_n_drag, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1146         litest_add("touchpad:softbutton", clickpad_softbutton_left_1st_fg_move, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1147         litest_add("touchpad:softbutton", clickpad_softbutton_left_2nd_fg_move, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1148         litest_add("touchpad:softbutton", clickpad_softbutton_left_to_right, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1149         litest_add("touchpad:softbutton", clickpad_softbutton_right_to_left, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1150
1151         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_left, LITEST_TOPBUTTONPAD, LITEST_ANY);
1152         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_right, LITEST_TOPBUTTONPAD, LITEST_ANY);
1153         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_middle, LITEST_TOPBUTTONPAD, LITEST_ANY);
1154         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_move_out_ignore, LITEST_TOPBUTTONPAD, LITEST_ANY);
1155
1156         litest_add("touchpad:scroll", touchpad_2fg_scroll, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1157
1158         return litest_run(argc, argv);
1159 }