touchpad: disable tapping for fingers exceeding the timeout/motion threshold
[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_no_2fg_tap_after_move)
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, move past threshold,
331            second finger down, first finger up
332            -> no event
333          */
334         litest_touch_down(dev, 0, 50, 50);
335         litest_touch_move_to(dev, 0, 50, 50, 90, 90, 10);
336         litest_drain_events(dev->libinput);
337
338         litest_touch_down(dev, 1, 70, 50);
339         litest_touch_up(dev, 0);
340
341         litest_assert_empty_queue(li);
342 }
343 END_TEST
344
345 START_TEST(touchpad_no_2fg_tap_after_timeout)
346 {
347         struct litest_device *dev = litest_current_device();
348         struct libinput *li = dev->libinput;
349
350         litest_drain_events(dev->libinput);
351
352         /* one finger down, wait past tap timeout,
353            second finger down, first finger up
354            -> no event
355          */
356         litest_touch_down(dev, 0, 50, 50);
357         libinput_dispatch(dev->libinput);
358         msleep(300);
359         libinput_dispatch(dev->libinput);
360         litest_drain_events(dev->libinput);
361
362         litest_touch_down(dev, 1, 70, 50);
363         litest_touch_up(dev, 0);
364
365         litest_assert_empty_queue(li);
366 }
367 END_TEST
368
369 START_TEST(touchpad_no_first_fg_tap_after_move)
370 {
371         struct litest_device *dev = litest_current_device();
372         struct libinput *li = dev->libinput;
373         struct libinput_event *event;
374
375         litest_drain_events(dev->libinput);
376
377         /* one finger down, second finger down,
378            second finger moves beyond threshold,
379            first finger up
380            -> no event
381          */
382         litest_touch_down(dev, 0, 50, 50);
383         litest_touch_down(dev, 1, 70, 50);
384         libinput_dispatch(dev->libinput);
385         litest_touch_move_to(dev, 1, 70, 50, 90, 90, 10);
386         libinput_dispatch(dev->libinput);
387         litest_touch_up(dev, 0);
388         litest_touch_up(dev, 1);
389         libinput_dispatch(dev->libinput);
390
391         while ((event = libinput_get_event(li))) {
392                 ck_assert_int_ne(libinput_event_get_type(event),
393                                  LIBINPUT_EVENT_POINTER_BUTTON);
394                 libinput_event_destroy(event);
395         }
396 }
397 END_TEST
398
399 START_TEST(touchpad_1fg_double_tap_click)
400 {
401         struct litest_device *dev = litest_current_device();
402         struct libinput *li = dev->libinput;
403
404         litest_drain_events(dev->libinput);
405
406         /* one finger down, up, down, button click, finger up
407            -> two button left event pairs */
408         litest_touch_down(dev, 0, 50, 50);
409         litest_touch_up(dev, 0);
410         litest_touch_down(dev, 0, 50, 50);
411         litest_event(dev, EV_KEY, BTN_LEFT, 1);
412         litest_event(dev, EV_SYN, SYN_REPORT, 0);
413         litest_event(dev, EV_KEY, BTN_LEFT, 0);
414         litest_event(dev, EV_SYN, SYN_REPORT, 0);
415         litest_touch_up(dev, 0);
416
417         libinput_dispatch(li);
418
419         assert_button_event(li, BTN_LEFT,
420                             LIBINPUT_BUTTON_STATE_PRESSED);
421         assert_button_event(li, BTN_LEFT,
422                             LIBINPUT_BUTTON_STATE_RELEASED);
423         assert_button_event(li, BTN_LEFT,
424                             LIBINPUT_BUTTON_STATE_PRESSED);
425         assert_button_event(li, BTN_LEFT,
426                             LIBINPUT_BUTTON_STATE_RELEASED);
427
428         litest_assert_empty_queue(li);
429 }
430 END_TEST
431
432 START_TEST(touchpad_1fg_tap_n_drag_click)
433 {
434         struct litest_device *dev = litest_current_device();
435         struct libinput *li = dev->libinput;
436         struct libinput_event *event;
437
438         litest_drain_events(dev->libinput);
439
440         /* one finger down, up, down, move, button click, finger up
441            -> two button left event pairs, motion allowed */
442         litest_touch_down(dev, 0, 50, 50);
443         litest_touch_up(dev, 0);
444         litest_touch_down(dev, 0, 50, 50);
445         litest_touch_move_to(dev, 0, 50, 50, 80, 50, 10);
446
447         assert_button_event(li, BTN_LEFT,
448                             LIBINPUT_BUTTON_STATE_PRESSED);
449
450         libinput_dispatch(li);
451
452         ck_assert_int_eq(libinput_next_event_type(li),
453                          LIBINPUT_EVENT_POINTER_MOTION);
454         while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION) {
455                 event = libinput_get_event(li);
456                 libinput_event_destroy(event);
457                 libinput_dispatch(li);
458         }
459
460         litest_event(dev, EV_KEY, BTN_LEFT, 1);
461         litest_event(dev, EV_SYN, SYN_REPORT, 0);
462
463         assert_button_event(li, BTN_LEFT,
464                             LIBINPUT_BUTTON_STATE_RELEASED);
465         assert_button_event(li, BTN_LEFT,
466                             LIBINPUT_BUTTON_STATE_PRESSED);
467
468         litest_event(dev, EV_KEY, BTN_LEFT, 0);
469         litest_event(dev, EV_SYN, SYN_REPORT, 0);
470         litest_touch_up(dev, 0);
471
472         libinput_dispatch(li);
473
474         assert_button_event(li, BTN_LEFT,
475                             LIBINPUT_BUTTON_STATE_RELEASED);
476
477         litest_assert_empty_queue(li);
478 }
479 END_TEST
480
481 START_TEST(touchpad_1fg_clickfinger)
482 {
483         struct litest_device *dev = litest_create_device(LITEST_BCM5974);
484         struct libinput *li = dev->libinput;
485
486         litest_drain_events(li);
487
488         litest_touch_down(dev, 0, 50, 50);
489         litest_event(dev, EV_KEY, BTN_LEFT, 1);
490         litest_event(dev, EV_SYN, SYN_REPORT, 0);
491         litest_event(dev, EV_KEY, BTN_LEFT, 0);
492         litest_event(dev, EV_SYN, SYN_REPORT, 0);
493         litest_touch_up(dev, 0);
494
495         libinput_dispatch(li);
496
497         assert_button_event(li, BTN_LEFT,
498                             LIBINPUT_BUTTON_STATE_PRESSED);
499         assert_button_event(li, BTN_LEFT,
500                             LIBINPUT_BUTTON_STATE_RELEASED);
501
502         litest_delete_device(dev);
503 }
504 END_TEST
505
506 START_TEST(touchpad_2fg_clickfinger)
507 {
508         struct litest_device *dev = litest_create_device(LITEST_BCM5974);
509         struct libinput *li = dev->libinput;
510
511         litest_drain_events(li);
512
513         litest_touch_down(dev, 0, 50, 50);
514         litest_touch_down(dev, 1, 70, 70);
515         litest_event(dev, EV_KEY, BTN_LEFT, 1);
516         litest_event(dev, EV_SYN, SYN_REPORT, 0);
517         litest_event(dev, EV_KEY, BTN_LEFT, 0);
518         litest_event(dev, EV_SYN, SYN_REPORT, 0);
519         litest_touch_up(dev, 0);
520         litest_touch_up(dev, 1);
521
522         libinput_dispatch(li);
523
524         assert_button_event(li, BTN_RIGHT,
525                             LIBINPUT_BUTTON_STATE_PRESSED);
526         assert_button_event(li, BTN_RIGHT,
527                             LIBINPUT_BUTTON_STATE_RELEASED);
528
529         litest_delete_device(dev);
530 }
531 END_TEST
532
533 START_TEST(touchpad_btn_left)
534 {
535         struct litest_device *dev = litest_current_device();
536         struct libinput *li = dev->libinput;
537
538         litest_drain_events(li);
539
540         litest_event(dev, EV_KEY, BTN_LEFT, 1);
541         litest_event(dev, EV_SYN, SYN_REPORT, 0);
542         litest_event(dev, EV_KEY, BTN_LEFT, 0);
543         litest_event(dev, EV_SYN, SYN_REPORT, 0);
544
545         libinput_dispatch(li);
546
547         assert_button_event(li, BTN_LEFT,
548                             LIBINPUT_BUTTON_STATE_PRESSED);
549         assert_button_event(li, BTN_LEFT,
550                             LIBINPUT_BUTTON_STATE_RELEASED);
551 }
552 END_TEST
553
554 START_TEST(clickpad_btn_left)
555 {
556         struct litest_device *dev = litest_current_device();
557         struct libinput *li = dev->libinput;
558
559         litest_drain_events(li);
560
561         /* A clickpad always needs a finger down to tell where the
562            click happens */
563         litest_event(dev, EV_KEY, BTN_LEFT, 1);
564         litest_event(dev, EV_SYN, SYN_REPORT, 0);
565         litest_event(dev, EV_KEY, BTN_LEFT, 0);
566         litest_event(dev, EV_SYN, SYN_REPORT, 0);
567
568         libinput_dispatch(li);
569         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
570 }
571 END_TEST
572
573 START_TEST(clickpad_click_n_drag)
574 {
575         struct litest_device *dev = litest_current_device();
576         struct libinput *li = dev->libinput;
577         struct libinput_event *event;
578
579         litest_drain_events(li);
580
581         litest_touch_down(dev, 0, 50, 50);
582         litest_event(dev, EV_KEY, BTN_LEFT, 1);
583         litest_event(dev, EV_SYN, SYN_REPORT, 0);
584
585         libinput_dispatch(li);
586         assert_button_event(li, BTN_LEFT,
587                             LIBINPUT_BUTTON_STATE_PRESSED);
588
589         libinput_dispatch(li);
590         ck_assert_int_eq(libinput_next_event_type(li), LIBINPUT_EVENT_NONE);
591
592         /* now put a second finger down */
593         litest_touch_down(dev, 1, 70, 70);
594         litest_touch_move_to(dev, 1, 70, 70, 80, 50, 5);
595         litest_touch_up(dev, 1);
596
597         libinput_dispatch(li);
598         ck_assert_int_eq(libinput_next_event_type(li),
599                          LIBINPUT_EVENT_POINTER_MOTION);
600         do {
601                 event = libinput_get_event(li);
602                 libinput_event_destroy(event);
603                 libinput_dispatch(li);
604         } while (libinput_next_event_type(li) == LIBINPUT_EVENT_POINTER_MOTION);
605
606         litest_event(dev, EV_KEY, BTN_LEFT, 0);
607         litest_event(dev, EV_SYN, SYN_REPORT, 0);
608         litest_touch_up(dev, 0);
609
610         assert_button_event(li, BTN_LEFT,
611                             LIBINPUT_BUTTON_STATE_RELEASED);
612 }
613 END_TEST
614
615 START_TEST(clickpad_softbutton_left)
616 {
617         struct litest_device *dev = litest_current_device();
618         struct libinput *li = dev->libinput;
619
620         litest_drain_events(li);
621
622         litest_touch_down(dev, 0, 10, 90);
623         litest_event(dev, EV_KEY, BTN_LEFT, 1);
624         litest_event(dev, EV_SYN, SYN_REPORT, 0);
625
626         assert_button_event(li,
627                             BTN_LEFT,
628                             LIBINPUT_BUTTON_STATE_PRESSED);
629
630         litest_event(dev, EV_KEY, BTN_LEFT, 0);
631         litest_event(dev, EV_SYN, SYN_REPORT, 0);
632         litest_touch_up(dev, 0);
633
634         assert_button_event(li,
635                             BTN_LEFT,
636                             LIBINPUT_BUTTON_STATE_RELEASED);
637
638         libinput_dispatch(li);
639
640         litest_assert_empty_queue(li);
641 }
642 END_TEST
643
644 START_TEST(clickpad_softbutton_right)
645 {
646         struct litest_device *dev = litest_current_device();
647         struct libinput *li = dev->libinput;
648
649         litest_drain_events(li);
650
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_RIGHT,
657                             LIBINPUT_BUTTON_STATE_PRESSED);
658
659         litest_event(dev, EV_KEY, BTN_LEFT, 0);
660         litest_event(dev, EV_SYN, SYN_REPORT, 0);
661         litest_touch_up(dev, 0);
662
663         assert_button_event(li,
664                             BTN_RIGHT,
665                             LIBINPUT_BUTTON_STATE_RELEASED);
666
667         libinput_dispatch(li);
668
669         litest_assert_empty_queue(li);
670 }
671 END_TEST
672
673 START_TEST(clickpad_softbutton_left_tap_n_drag)
674 {
675         struct litest_device *dev = litest_current_device();
676         struct libinput *li = dev->libinput;
677
678         litest_drain_events(li);
679
680         /* Tap in left button area, then finger down, button click
681                 -> expect left button press/release and left button press
682            Release button, finger up
683                 -> expect right button release
684          */
685         litest_touch_down(dev, 0, 20, 90);
686         litest_touch_up(dev, 0);
687         litest_touch_down(dev, 0, 20, 90);
688         litest_event(dev, EV_KEY, BTN_LEFT, 1);
689         litest_event(dev, EV_SYN, SYN_REPORT, 0);
690
691         assert_button_event(li,
692                             BTN_LEFT,
693                             LIBINPUT_BUTTON_STATE_PRESSED);
694         assert_button_event(li,
695                             BTN_LEFT,
696                             LIBINPUT_BUTTON_STATE_RELEASED);
697         assert_button_event(li,
698                             BTN_LEFT,
699                             LIBINPUT_BUTTON_STATE_PRESSED);
700         litest_assert_empty_queue(li);
701
702         litest_event(dev, EV_KEY, BTN_LEFT, 0);
703         litest_event(dev, EV_SYN, SYN_REPORT, 0);
704         litest_touch_up(dev, 0);
705
706         assert_button_event(li,
707                             BTN_LEFT,
708                             LIBINPUT_BUTTON_STATE_RELEASED);
709         litest_assert_empty_queue(li);
710 }
711 END_TEST
712
713 START_TEST(clickpad_softbutton_right_tap_n_drag)
714 {
715         struct litest_device *dev = litest_current_device();
716         struct libinput *li = dev->libinput;
717
718         litest_drain_events(li);
719
720         /* Tap in right button area, then finger down, button click
721                 -> expect left button press/release and right button press
722            Release button, finger up
723                 -> expect right button release
724          */
725         litest_touch_down(dev, 0, 90, 90);
726         litest_touch_up(dev, 0);
727         litest_touch_down(dev, 0, 90, 90);
728         litest_event(dev, EV_KEY, BTN_LEFT, 1);
729         litest_event(dev, EV_SYN, SYN_REPORT, 0);
730
731         assert_button_event(li,
732                             BTN_LEFT,
733                             LIBINPUT_BUTTON_STATE_PRESSED);
734         assert_button_event(li,
735                             BTN_LEFT,
736                             LIBINPUT_BUTTON_STATE_RELEASED);
737         assert_button_event(li,
738                             BTN_RIGHT,
739                             LIBINPUT_BUTTON_STATE_PRESSED);
740         litest_assert_empty_queue(li);
741
742         litest_event(dev, EV_KEY, BTN_LEFT, 0);
743         litest_event(dev, EV_SYN, SYN_REPORT, 0);
744         litest_touch_up(dev, 0);
745
746         assert_button_event(li,
747                             BTN_RIGHT,
748                             LIBINPUT_BUTTON_STATE_RELEASED);
749         litest_assert_empty_queue(li);
750 }
751 END_TEST
752
753 START_TEST(clickpad_softbutton_left_1st_fg_move)
754 {
755         struct litest_device *dev = litest_current_device();
756         struct libinput *li = dev->libinput;
757         struct libinput_event *event;
758         double x = 0, y = 0;
759         int nevents = 0;
760
761         litest_drain_events(li);
762
763         /* One finger down in the left button area, button press
764                 -> expect a button event
765            Move finger up out of the area, wait for timeout
766            Move finger around diagonally down left
767                 -> expect motion events down left
768            Release finger
769                 -> expect a button event */
770
771         /* finger down, press in left button */
772         litest_touch_down(dev, 0, 20, 90);
773         litest_event(dev, EV_KEY, BTN_LEFT, 1);
774         litest_event(dev, EV_SYN, SYN_REPORT, 0);
775
776         assert_button_event(li,
777                             BTN_LEFT,
778                             LIBINPUT_BUTTON_STATE_PRESSED);
779         litest_assert_empty_queue(li);
780
781         /* move out of the area, then wait for softbutton timer */
782         litest_touch_move_to(dev, 0, 20, 90, 90, 20, 10);
783         libinput_dispatch(li);
784         msleep(400);
785         libinput_dispatch(li);
786         litest_drain_events(li);
787
788         /* move down left, expect motion */
789         litest_touch_move_to(dev, 0, 90, 20, 20, 90, 10);
790
791         libinput_dispatch(li);
792         event = libinput_get_event(li);
793         ck_assert(event != NULL);
794         while (event) {
795                 struct libinput_event_pointer *p;
796
797                 ck_assert_int_eq(libinput_event_get_type(event),
798                                  LIBINPUT_EVENT_POINTER_MOTION);
799                 p = libinput_event_get_pointer_event(event);
800
801                 /* we moved up/right, now down/left so the pointer accel
802                    code may lag behind with the dx/dy vectors. Hence, add up
803                    the x/y movements and expect that on average we moved
804                    left and down */
805                 x += libinput_event_pointer_get_dx(p);
806                 y += libinput_event_pointer_get_dy(p);
807                 nevents++;
808
809                 libinput_event_destroy(event);
810                 libinput_dispatch(li);
811                 event = libinput_get_event(li);
812         }
813
814         ck_assert(x/nevents < 0);
815         ck_assert(y/nevents > 0);
816
817         litest_event(dev, EV_KEY, BTN_LEFT, 0);
818         litest_event(dev, EV_SYN, SYN_REPORT, 0);
819         litest_touch_up(dev, 0);
820
821         assert_button_event(li,
822                             BTN_LEFT,
823                             LIBINPUT_BUTTON_STATE_RELEASED);
824
825         litest_assert_empty_queue(li);
826 }
827 END_TEST
828
829 START_TEST(clickpad_softbutton_left_2nd_fg_move)
830 {
831         struct litest_device *dev = litest_current_device();
832         struct libinput *li = dev->libinput;
833         struct libinput_event *event;
834
835         litest_drain_events(li);
836
837         /* One finger down in the left button area, button press
838                 -> expect a button event
839            Put a second finger down in the area, move it right, release
840                 -> expect motion events right
841            Put a second finger down in the area, move it down, release
842                 -> expect motion events down
843            Release second finger, release first finger
844                 -> expect a button event */
845         litest_touch_down(dev, 0, 20, 90);
846         litest_event(dev, EV_KEY, BTN_LEFT, 1);
847         litest_event(dev, EV_SYN, SYN_REPORT, 0);
848
849         assert_button_event(li,
850                             BTN_LEFT,
851                             LIBINPUT_BUTTON_STATE_PRESSED);
852         litest_assert_empty_queue(li);
853
854         litest_touch_down(dev, 1, 20, 20);
855         litest_touch_move_to(dev, 1, 20, 20, 80, 20, 10);
856
857         libinput_dispatch(li);
858         event = libinput_get_event(li);
859         ck_assert(event != NULL);
860         while (event) {
861                 struct libinput_event_pointer *p;
862                 double x, y;
863
864                 ck_assert_int_eq(libinput_event_get_type(event),
865                                  LIBINPUT_EVENT_POINTER_MOTION);
866                 p = libinput_event_get_pointer_event(event);
867
868                 x = libinput_event_pointer_get_dx(p);
869                 y = libinput_event_pointer_get_dy(p);
870
871                 ck_assert(x > 0);
872                 ck_assert(y == 0);
873
874                 libinput_event_destroy(event);
875                 libinput_dispatch(li);
876                 event = libinput_get_event(li);
877         }
878         litest_touch_up(dev, 1);
879
880         /* second finger down */
881         litest_touch_down(dev, 1, 20, 20);
882         litest_touch_move_to(dev, 1, 20, 20, 20, 80, 10);
883
884         libinput_dispatch(li);
885         event = libinput_get_event(li);
886         ck_assert(event != NULL);
887         while (event) {
888                 struct libinput_event_pointer *p;
889                 double x, y;
890
891                 ck_assert_int_eq(libinput_event_get_type(event),
892                                  LIBINPUT_EVENT_POINTER_MOTION);
893                 p = libinput_event_get_pointer_event(event);
894
895                 x = libinput_event_pointer_get_dx(p);
896                 y = libinput_event_pointer_get_dy(p);
897
898                 ck_assert(x == 0);
899                 ck_assert(y > 0);
900
901                 libinput_event_destroy(event);
902                 libinput_dispatch(li);
903                 event = libinput_get_event(li);
904         }
905
906         litest_event(dev, EV_KEY, BTN_LEFT, 0);
907         litest_event(dev, EV_SYN, SYN_REPORT, 0);
908         litest_touch_up(dev, 0);
909
910         assert_button_event(li,
911                             BTN_LEFT,
912                             LIBINPUT_BUTTON_STATE_RELEASED);
913
914         litest_assert_empty_queue(li);
915 }
916 END_TEST
917
918 START_TEST(clickpad_softbutton_left_to_right)
919 {
920         struct litest_device *dev = litest_current_device();
921         struct libinput *li = dev->libinput;
922
923         litest_drain_events(li);
924
925         /* One finger down in left software button area,
926            move to right button area immediately, click
927                 -> expect right button event
928         */
929
930         litest_touch_down(dev, 0, 20, 90);
931         litest_touch_move_to(dev, 0, 20, 90, 90, 90, 10);
932         litest_event(dev, EV_KEY, BTN_LEFT, 1);
933         litest_event(dev, EV_SYN, SYN_REPORT, 0);
934
935         assert_button_event(li,
936                             BTN_RIGHT,
937                             LIBINPUT_BUTTON_STATE_PRESSED);
938         litest_assert_empty_queue(li);
939
940         litest_event(dev, EV_KEY, BTN_LEFT, 0);
941         litest_event(dev, EV_SYN, SYN_REPORT, 0);
942         litest_touch_up(dev, 0);
943
944         assert_button_event(li,
945                             BTN_RIGHT,
946                             LIBINPUT_BUTTON_STATE_RELEASED);
947
948         litest_assert_empty_queue(li);
949 }
950 END_TEST
951
952 START_TEST(clickpad_softbutton_right_to_left)
953 {
954         struct litest_device *dev = litest_current_device();
955         struct libinput *li = dev->libinput;
956
957         litest_drain_events(li);
958
959         /* One finger down in right software button area,
960            move to left button area immediately, click
961                 -> expect left button event
962         */
963
964         litest_touch_down(dev, 0, 90, 90);
965         litest_touch_move_to(dev, 0, 90, 90, 20, 90, 10);
966         litest_event(dev, EV_KEY, BTN_LEFT, 1);
967         litest_event(dev, EV_SYN, SYN_REPORT, 0);
968
969         assert_button_event(li,
970                             BTN_LEFT,
971                             LIBINPUT_BUTTON_STATE_PRESSED);
972         litest_assert_empty_queue(li);
973
974         litest_event(dev, EV_KEY, BTN_LEFT, 0);
975         litest_event(dev, EV_SYN, SYN_REPORT, 0);
976         litest_touch_up(dev, 0);
977
978         assert_button_event(li,
979                             BTN_LEFT,
980                             LIBINPUT_BUTTON_STATE_RELEASED);
981
982         litest_assert_empty_queue(li);
983 }
984 END_TEST
985
986 START_TEST(clickpad_topsoftbuttons_left)
987 {
988         struct litest_device *dev = litest_current_device();
989         struct libinput *li = dev->libinput;
990
991         litest_drain_events(li);
992
993         litest_touch_down(dev, 0, 10, 5);
994         litest_event(dev, EV_KEY, BTN_LEFT, 1);
995         litest_event(dev, EV_SYN, SYN_REPORT, 0);
996
997         assert_button_event(li,
998                             BTN_LEFT,
999                             LIBINPUT_BUTTON_STATE_PRESSED);
1000         litest_assert_empty_queue(li);
1001
1002         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1003         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1004         litest_touch_up(dev, 0);
1005
1006         assert_button_event(li,
1007                             BTN_LEFT,
1008                             LIBINPUT_BUTTON_STATE_RELEASED);
1009
1010         litest_assert_empty_queue(li);
1011 }
1012 END_TEST
1013
1014 START_TEST(clickpad_topsoftbuttons_right)
1015 {
1016         struct litest_device *dev = litest_current_device();
1017         struct libinput *li = dev->libinput;
1018
1019         litest_drain_events(li);
1020
1021         litest_touch_down(dev, 0, 90, 5);
1022         litest_event(dev, EV_KEY, BTN_LEFT, 1);
1023         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1024
1025         assert_button_event(li,
1026                             BTN_RIGHT,
1027                             LIBINPUT_BUTTON_STATE_PRESSED);
1028         litest_assert_empty_queue(li);
1029
1030         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1031         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1032         litest_touch_up(dev, 0);
1033
1034         assert_button_event(li,
1035                             BTN_RIGHT,
1036                             LIBINPUT_BUTTON_STATE_RELEASED);
1037
1038         litest_assert_empty_queue(li);
1039 }
1040 END_TEST
1041
1042 START_TEST(clickpad_topsoftbuttons_middle)
1043 {
1044         struct litest_device *dev = litest_current_device();
1045         struct libinput *li = dev->libinput;
1046
1047         litest_drain_events(li);
1048
1049         litest_touch_down(dev, 0, 50, 5);
1050         litest_event(dev, EV_KEY, BTN_LEFT, 1);
1051         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1052
1053         assert_button_event(li,
1054                             BTN_MIDDLE,
1055                             LIBINPUT_BUTTON_STATE_PRESSED);
1056         litest_assert_empty_queue(li);
1057
1058         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1059         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1060         litest_touch_up(dev, 0);
1061
1062         assert_button_event(li,
1063                             BTN_MIDDLE,
1064                             LIBINPUT_BUTTON_STATE_RELEASED);
1065
1066         litest_assert_empty_queue(li);
1067 }
1068 END_TEST
1069
1070 START_TEST(clickpad_topsoftbuttons_move_out_ignore)
1071 {
1072         struct litest_device *dev = litest_current_device();
1073         struct libinput *li = dev->libinput;
1074
1075         /* Finger down in top button area, wait past enter timeout
1076            Move into main area, wait past leave timeout
1077            Click
1078              -> expect no events
1079          */
1080
1081         litest_drain_events(li);
1082
1083         litest_touch_down(dev, 0, 50, 5);
1084         libinput_dispatch(li);
1085         msleep(200);
1086         libinput_dispatch(li);
1087         litest_assert_empty_queue(li);
1088
1089         litest_touch_move_to(dev, 0, 50, 5, 80, 90, 20);
1090         libinput_dispatch(li);
1091         msleep(400);
1092         libinput_dispatch(li);
1093
1094         litest_event(dev, EV_KEY, BTN_LEFT, 1);
1095         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1096         litest_event(dev, EV_KEY, BTN_LEFT, 0);
1097         litest_event(dev, EV_SYN, SYN_REPORT, 0);
1098
1099         litest_touch_up(dev, 0);
1100
1101         litest_assert_empty_queue(li);
1102 }
1103 END_TEST
1104
1105 static void
1106 test_2fg_scroll(struct litest_device *dev, int dx, int dy, int sleep)
1107 {
1108         struct libinput *li = dev->libinput;
1109
1110         litest_touch_down(dev, 0, 47, 50);
1111         litest_touch_down(dev, 1, 53, 50);
1112
1113         litest_touch_move_to(dev, 0, 47, 50, 47 + dx, 50 + dy, 5);
1114         litest_touch_move_to(dev, 1, 53, 50, 53 + dx, 50 + dy, 5);
1115
1116         /* Avoid a small scroll being seen as a tap */
1117         if (sleep) {
1118                 libinput_dispatch(li);
1119                 msleep(sleep);
1120                 libinput_dispatch(li);
1121         }
1122
1123         litest_touch_up(dev, 1);
1124         litest_touch_up(dev, 0);
1125
1126         libinput_dispatch(li);
1127 }
1128
1129 static void
1130 check_2fg_scroll(struct litest_device *dev, int axis, int dir)
1131 {
1132         struct libinput *li = dev->libinput;
1133         struct libinput_event *event, *next_event;
1134         struct libinput_event_pointer *ptrev;
1135
1136         event = libinput_get_event(li);
1137         next_event = libinput_get_event(li);
1138         ck_assert(next_event != NULL); /* At least 1 scroll + stop scroll */
1139
1140         while (event) {
1141                 ck_assert_int_eq(libinput_event_get_type(event),
1142                                  LIBINPUT_EVENT_POINTER_AXIS);
1143                 ptrev = libinput_event_get_pointer_event(event);
1144                 ck_assert(ptrev != NULL);
1145                 ck_assert_int_eq(libinput_event_pointer_get_axis(ptrev), axis);
1146
1147                 if (next_event) {
1148                         /* Normal scroll event, check dir */
1149                         if (dir > 0) {
1150                                 ck_assert_int_ge(
1151                                         libinput_event_pointer_get_axis_value(ptrev),
1152                                         dir);
1153                         } else {
1154                                 ck_assert_int_le(
1155                                         libinput_event_pointer_get_axis_value(ptrev),
1156                                         dir);
1157                         }
1158                 } else {
1159                         /* Last scroll event, must be 0 */
1160                         ck_assert_int_eq(
1161                                 libinput_event_pointer_get_axis_value(ptrev),
1162                                 0);
1163                 }
1164                 libinput_event_destroy(event);
1165                 event = next_event;
1166                 next_event = libinput_get_event(li);
1167         }
1168 }
1169
1170 START_TEST(touchpad_2fg_scroll)
1171 {
1172         struct litest_device *dev = litest_current_device();
1173         struct libinput *li = dev->libinput;
1174
1175         litest_drain_events(li);
1176
1177         /* Note this mixes in a tiny amount of movement in the wrong direction,
1178            which should be ignored */
1179         test_2fg_scroll(dev, 1, 40, 0);
1180         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL, 10);
1181         test_2fg_scroll(dev, 1, -40, 0);
1182         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL, -10);
1183         test_2fg_scroll(dev, 40, 1, 0);
1184         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL, 10);
1185         test_2fg_scroll(dev, -40, 1, 0);
1186         check_2fg_scroll(dev, LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL, -10);
1187
1188         /* 2fg scroll smaller than the threshold should not generate events */
1189         test_2fg_scroll(dev, 1, 1, 200);
1190         litest_assert_empty_queue(li);
1191 }
1192 END_TEST
1193
1194 int main(int argc, char **argv) {
1195
1196         litest_add("touchpad:motion", touchpad_1fg_motion, LITEST_TOUCHPAD, LITEST_ANY);
1197         litest_add("touchpad:motion", touchpad_2fg_no_motion, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1198
1199         litest_add("touchpad:tap", touchpad_1fg_tap, LITEST_TOUCHPAD, LITEST_ANY);
1200         litest_add("touchpad:tap", touchpad_1fg_tap_n_drag, LITEST_TOUCHPAD, LITEST_ANY);
1201         litest_add("touchpad:tap", touchpad_2fg_tap, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1202         litest_add("touchpad:tap", touchpad_2fg_tap_inverted, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1203         litest_add("touchpad:tap", touchpad_1fg_tap_click, LITEST_TOUCHPAD, LITEST_ANY);
1204         litest_add("touchpad:tap", touchpad_2fg_tap_click, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH|LITEST_APPLE_CLICKPAD);
1205         litest_add("touchpad:tap", touchpad_2fg_tap_click_apple, LITEST_APPLE_CLICKPAD, LITEST_ANY);
1206         litest_add("touchpad:tap", touchpad_no_2fg_tap_after_move, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1207         litest_add("touchpad:tap", touchpad_no_2fg_tap_after_timeout, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1208         litest_add("touchpad:tap", touchpad_no_first_fg_tap_after_move, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1209         litest_add("touchpad:tap", touchpad_no_first_fg_tap_after_move, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1210
1211         /* Real buttons don't interfere with tapping, so don't run those for
1212            pads with buttons */
1213         litest_add("touchpad:tap", touchpad_1fg_double_tap_click, LITEST_CLICKPAD, LITEST_ANY);
1214         litest_add("touchpad:tap", touchpad_1fg_tap_n_drag_click, LITEST_CLICKPAD, LITEST_ANY);
1215
1216         litest_add_no_device("touchpad:clickfinger", touchpad_1fg_clickfinger);
1217         litest_add_no_device("touchpad:clickfinger", touchpad_2fg_clickfinger);
1218
1219         litest_add("touchpad:click", touchpad_btn_left, LITEST_TOUCHPAD, LITEST_CLICKPAD);
1220         litest_add("touchpad:click", clickpad_btn_left, LITEST_CLICKPAD, LITEST_ANY);
1221         litest_add("touchpad:click", clickpad_click_n_drag, LITEST_CLICKPAD, LITEST_SINGLE_TOUCH);
1222
1223         litest_add("touchpad:softbutton", clickpad_softbutton_left, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1224         litest_add("touchpad:softbutton", clickpad_softbutton_right, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1225         litest_add("touchpad:softbutton", clickpad_softbutton_left_tap_n_drag, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1226         litest_add("touchpad:softbutton", clickpad_softbutton_right_tap_n_drag, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1227         litest_add("touchpad:softbutton", clickpad_softbutton_left_1st_fg_move, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1228         litest_add("touchpad:softbutton", clickpad_softbutton_left_2nd_fg_move, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1229         litest_add("touchpad:softbutton", clickpad_softbutton_left_to_right, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1230         litest_add("touchpad:softbutton", clickpad_softbutton_right_to_left, LITEST_CLICKPAD, LITEST_APPLE_CLICKPAD);
1231
1232         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_left, LITEST_TOPBUTTONPAD, LITEST_ANY);
1233         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_right, LITEST_TOPBUTTONPAD, LITEST_ANY);
1234         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_middle, LITEST_TOPBUTTONPAD, LITEST_ANY);
1235         litest_add("touchpad:topsoftbuttons", clickpad_topsoftbuttons_move_out_ignore, LITEST_TOPBUTTONPAD, LITEST_ANY);
1236
1237         litest_add("touchpad:scroll", touchpad_2fg_scroll, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1238
1239         return litest_run(argc, argv);
1240 }