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