xref: /third_party/libinput/test/test-gestures.c (revision a46c0ec8)
1/*
2 * Copyright © 2015 Red Hat, Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24#include <config.h>
25
26#include <check.h>
27#include <libinput.h>
28#include <valgrind/valgrind.h>
29
30#include "libinput-util.h"
31#include "litest.h"
32
33enum cardinal {
34	N, NE, E, SE, S, SW, W, NW, NCARDINALS
35};
36
37enum hold_gesture_behaviour {
38   HOLD_GESTURE_IGNORE,
39   HOLD_GESTURE_REQUIRE,
40};
41
42static void
43test_gesture_swipe_3fg(int cardinal, enum hold_gesture_behaviour hold)
44{
45	struct litest_device *dev = litest_current_device();
46	struct libinput *li = dev->libinput;
47	struct libinput_event *event;
48	struct libinput_event_gesture *gevent;
49	double dx, dy;
50	double dir_x, dir_y;
51	int cardinals[NCARDINALS][2] = {
52		{ 0, 30 },
53		{ 30, 30 },
54		{ 30, 0 },
55		{ 30, -30 },
56		{ 0, -30 },
57		{ -30, -30 },
58		{ -30, 0 },
59		{ -30, 30 },
60	};
61
62	if (litest_slot_count(dev) < 3)
63		return;
64
65	dir_x = cardinals[cardinal][0];
66	dir_y = cardinals[cardinal][1];
67
68	litest_drain_events(li);
69
70	litest_touch_down(dev, 0, 40, 40);
71	litest_touch_down(dev, 1, 50, 40);
72	litest_touch_down(dev, 2, 60, 40);
73	libinput_dispatch(li);
74
75	if (hold == HOLD_GESTURE_REQUIRE)
76		litest_timeout_gesture_hold();
77
78	litest_touch_move_three_touches(dev, 40, 40, 50, 40, 60, 40, dir_x,
79					dir_y, 10);
80	libinput_dispatch(li);
81
82	if (hold == HOLD_GESTURE_REQUIRE) {
83		litest_assert_gesture_event(li,
84					    LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
85					    3);
86		litest_assert_gesture_event(li,
87					    LIBINPUT_EVENT_GESTURE_HOLD_END,
88					    3);
89	}
90
91	event = libinput_get_event(li);
92	gevent = litest_is_gesture_event(event,
93					 LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
94					 3);
95	dx = libinput_event_gesture_get_dx(gevent);
96	dy = libinput_event_gesture_get_dy(gevent);
97	ck_assert(dx == 0.0);
98	ck_assert(dy == 0.0);
99	libinput_event_destroy(event);
100
101	while ((event = libinput_get_event(li)) != NULL) {
102		gevent = litest_is_gesture_event(event,
103						 LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
104						 3);
105
106		dx = libinput_event_gesture_get_dx(gevent);
107		dy = libinput_event_gesture_get_dy(gevent);
108		if (dir_x == 0.0)
109			ck_assert(dx == 0.0);
110		else if (dir_x < 0.0)
111			ck_assert(dx < 0.0);
112		else if (dir_x > 0.0)
113			ck_assert(dx > 0.0);
114
115		if (dir_y == 0.0)
116			ck_assert(dy == 0.0);
117		else if (dir_y < 0.0)
118			ck_assert(dy < 0.0);
119		else if (dir_y > 0.0)
120			ck_assert(dy > 0.0);
121
122		dx = libinput_event_gesture_get_dx_unaccelerated(gevent);
123		dy = libinput_event_gesture_get_dy_unaccelerated(gevent);
124		if (dir_x == 0.0)
125			ck_assert(dx == 0.0);
126		else if (dir_x < 0.0)
127			ck_assert(dx < 0.0);
128		else if (dir_x > 0.0)
129			ck_assert(dx > 0.0);
130
131		if (dir_y == 0.0)
132			ck_assert(dy == 0.0);
133		else if (dir_y < 0.0)
134			ck_assert(dy < 0.0);
135		else if (dir_y > 0.0)
136			ck_assert(dy > 0.0);
137
138		libinput_event_destroy(event);
139	}
140
141	litest_touch_up(dev, 0);
142	litest_touch_up(dev, 1);
143	litest_touch_up(dev, 2);
144	libinput_dispatch(li);
145	event = libinput_get_event(li);
146	gevent = litest_is_gesture_event(event,
147					 LIBINPUT_EVENT_GESTURE_SWIPE_END,
148					 3);
149	ck_assert(!libinput_event_gesture_get_cancelled(gevent));
150	libinput_event_destroy(event);
151}
152
153static void
154test_gesture_swipe_4fg(int cardinal, enum hold_gesture_behaviour hold)
155{
156	struct litest_device *dev = litest_current_device();
157	struct libinput *li = dev->libinput;
158	struct libinput_event *event;
159	struct libinput_event_gesture *gevent;
160	double dx, dy;
161	double dir_x, dir_y;
162	int cardinals[NCARDINALS][2] = {
163		{ 0, 3 },
164		{ 3, 3 },
165		{ 3, 0 },
166		{ 3, -3 },
167		{ 0, -3 },
168		{ -3, -3 },
169		{ -3, 0 },
170		{ -3, 3 },
171	};
172	int i;
173
174	if (litest_slot_count(dev) < 4)
175		return;
176
177	dir_x = cardinals[cardinal][0];
178	dir_y = cardinals[cardinal][1];
179
180	litest_drain_events(li);
181
182	litest_touch_down(dev, 0, 40, 40);
183	litest_touch_down(dev, 1, 50, 40);
184	litest_touch_down(dev, 2, 60, 40);
185	litest_touch_down(dev, 3, 70, 40);
186	libinput_dispatch(li);
187
188	if (hold == HOLD_GESTURE_REQUIRE)
189		litest_timeout_gesture_hold();
190
191	for (i = 0; i < 8; i++) {
192		litest_push_event_frame(dev);
193
194		dir_x += cardinals[cardinal][0];
195		dir_y += cardinals[cardinal][1];
196
197		litest_touch_move(dev,
198				  0,
199				  40 + dir_x,
200				  40 + dir_y);
201		litest_touch_move(dev,
202				  1,
203				  50 + dir_x,
204				  40 + dir_y);
205		litest_touch_move(dev,
206				  2,
207				  60 + dir_x,
208				  40 + dir_y);
209		litest_touch_move(dev,
210				  3,
211				  70 + dir_x,
212				  40 + dir_y);
213		litest_pop_event_frame(dev);
214		libinput_dispatch(li);
215	}
216
217	libinput_dispatch(li);
218
219	if (hold == HOLD_GESTURE_REQUIRE) {
220		litest_assert_gesture_event(li,
221					    LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
222					    4);
223		litest_assert_gesture_event(li,
224					    LIBINPUT_EVENT_GESTURE_HOLD_END,
225					    4);
226	}
227
228	event = libinput_get_event(li);
229	gevent = litest_is_gesture_event(event,
230					 LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
231					 4);
232	dx = libinput_event_gesture_get_dx(gevent);
233	dy = libinput_event_gesture_get_dy(gevent);
234	ck_assert(dx == 0.0);
235	ck_assert(dy == 0.0);
236	libinput_event_destroy(event);
237
238	while ((event = libinput_get_event(li)) != NULL) {
239		gevent = litest_is_gesture_event(event,
240						 LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
241						 4);
242
243		dx = libinput_event_gesture_get_dx(gevent);
244		dy = libinput_event_gesture_get_dy(gevent);
245		if (dir_x == 0.0)
246			ck_assert(dx == 0.0);
247		else if (dir_x < 0.0)
248			ck_assert(dx < 0.0);
249		else if (dir_x > 0.0)
250			ck_assert(dx > 0.0);
251
252		if (dir_y == 0.0)
253			ck_assert(dy == 0.0);
254		else if (dir_y < 0.0)
255			ck_assert(dy < 0.0);
256		else if (dir_y > 0.0)
257			ck_assert(dy > 0.0);
258
259		dx = libinput_event_gesture_get_dx_unaccelerated(gevent);
260		dy = libinput_event_gesture_get_dy_unaccelerated(gevent);
261		if (dir_x == 0.0)
262			ck_assert(dx == 0.0);
263		else if (dir_x < 0.0)
264			ck_assert(dx < 0.0);
265		else if (dir_x > 0.0)
266			ck_assert(dx > 0.0);
267
268		if (dir_y == 0.0)
269			ck_assert(dy == 0.0);
270		else if (dir_y < 0.0)
271			ck_assert(dy < 0.0);
272		else if (dir_y > 0.0)
273			ck_assert(dy > 0.0);
274
275		libinput_event_destroy(event);
276	}
277
278	litest_touch_up(dev, 0);
279	litest_touch_up(dev, 1);
280	litest_touch_up(dev, 2);
281	litest_touch_up(dev, 3);
282	libinput_dispatch(li);
283	event = libinput_get_event(li);
284	gevent = litest_is_gesture_event(event,
285					 LIBINPUT_EVENT_GESTURE_SWIPE_END,
286					 4);
287	ck_assert(!libinput_event_gesture_get_cancelled(gevent));
288	libinput_event_destroy(event);
289}
290
291static void
292test_gesture_pinch_2fg(int cardinal, enum hold_gesture_behaviour hold)
293{
294	struct litest_device *dev = litest_current_device();
295	struct libinput *li = dev->libinput;
296	struct libinput_event *event;
297	struct libinput_event_gesture *gevent;
298	double dx, dy;
299	double dir_x, dir_y;
300	int i;
301	double scale, oldscale;
302	double angle;
303	int cardinals[NCARDINALS][2] = {
304		{ 0, 30 },
305		{ 30, 30 },
306		{ 30, 0 },
307		{ 30, -30 },
308		{ 0, -30 },
309		{ -30, -30 },
310		{ -30, 0 },
311		{ -30, 30 },
312	};
313
314	if (litest_slot_count(dev) < 2 ||
315	    !libinput_device_has_capability(dev->libinput_device,
316					    LIBINPUT_DEVICE_CAP_GESTURE))
317		return;
318
319	/* If the device is too small to provide a finger spread wide enough
320	 * to avoid the scroll bias, skip the test */
321	if (cardinal == E || cardinal == W) {
322		double w = 0, h = 0;
323		libinput_device_get_size(dev->libinput_device, &w, &h);
324		/* 0.6 because the code below gives us points like 20/y and
325		 * 80/y. 45 because the threshold in the code is 40mm */
326		if (w * 0.6 < 45)
327			return;
328	}
329
330	dir_x = cardinals[cardinal][0];
331	dir_y = cardinals[cardinal][1];
332
333	litest_drain_events(li);
334
335	litest_touch_down(dev, 0, 50 + dir_x, 50 + dir_y);
336	litest_touch_down(dev, 1, 50 - dir_x, 50 - dir_y);
337	libinput_dispatch(li);
338
339	if (hold == HOLD_GESTURE_REQUIRE)
340		litest_timeout_gesture_hold();
341
342	for (i = 0; i < 8; i++) {
343		litest_push_event_frame(dev);
344		if (dir_x > 0.0)
345			dir_x -= 2;
346		else if (dir_x < 0.0)
347			dir_x += 2;
348		if (dir_y > 0.0)
349			dir_y -= 2;
350		else if (dir_y < 0.0)
351			dir_y += 2;
352		litest_touch_move(dev,
353				  0,
354				  50 + dir_x,
355				  50 + dir_y);
356		litest_touch_move(dev,
357				  1,
358				  50 - dir_x,
359				  50 - dir_y);
360		litest_pop_event_frame(dev);
361		libinput_dispatch(li);
362	}
363
364	if (hold == HOLD_GESTURE_REQUIRE) {
365		litest_assert_gesture_event(li,
366					LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
367					2);
368		litest_assert_gesture_event(li,
369					LIBINPUT_EVENT_GESTURE_HOLD_END,
370					2);
371	}
372
373	event = libinput_get_event(li);
374	gevent = litest_is_gesture_event(event,
375					 LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
376					 2);
377	dx = libinput_event_gesture_get_dx(gevent);
378	dy = libinput_event_gesture_get_dy(gevent);
379	scale = libinput_event_gesture_get_scale(gevent);
380	ck_assert(dx == 0.0);
381	ck_assert(dy == 0.0);
382	ck_assert(scale == 1.0);
383
384	libinput_event_destroy(event);
385
386	while ((event = libinput_get_event(li)) != NULL) {
387		gevent = litest_is_gesture_event(event,
388						 LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
389						 2);
390
391		oldscale = scale;
392		scale = libinput_event_gesture_get_scale(gevent);
393
394		ck_assert(scale < oldscale);
395
396		angle = libinput_event_gesture_get_angle_delta(gevent);
397		ck_assert_double_le(fabs(angle), 1.0);
398
399		libinput_event_destroy(event);
400		libinput_dispatch(li);
401	}
402
403	litest_touch_up(dev, 0);
404	litest_touch_up(dev, 1);
405	libinput_dispatch(li);
406	event = libinput_get_event(li);
407	gevent = litest_is_gesture_event(event,
408					 LIBINPUT_EVENT_GESTURE_PINCH_END,
409					 2);
410	ck_assert(!libinput_event_gesture_get_cancelled(gevent));
411	libinput_event_destroy(event);
412}
413
414static void
415test_gesture_pinch_3fg(int cardinal, enum hold_gesture_behaviour hold)
416{
417	struct litest_device *dev = litest_current_device();
418	struct libinput *li = dev->libinput;
419	struct libinput_event *event;
420	struct libinput_event_gesture *gevent;
421	double dx, dy;
422	double dir_x, dir_y;
423	int i;
424	double scale, oldscale;
425	double angle;
426	int cardinals[NCARDINALS][2] = {
427		{ 0, 30 },
428		{ 30, 30 },
429		{ 30, 0 },
430		{ 30, -30 },
431		{ 0, -30 },
432		{ -30, -30 },
433		{ -30, 0 },
434		{ -30, 30 },
435	};
436
437	if (litest_slot_count(dev) < 3)
438		return;
439
440	dir_x = cardinals[cardinal][0];
441	dir_y = cardinals[cardinal][1];
442
443	litest_drain_events(li);
444
445	litest_touch_down(dev, 0, 50 + dir_x, 50 + dir_y);
446	litest_touch_down(dev, 1, 50 - dir_x, 50 - dir_y);
447	litest_touch_down(dev, 2, 51 - dir_x, 51 - dir_y);
448	libinput_dispatch(li);
449
450	if (hold == HOLD_GESTURE_REQUIRE)
451		litest_timeout_gesture_hold();
452
453	for (i = 0; i < 8; i++) {
454		litest_push_event_frame(dev);
455		if (dir_x > 0.0)
456			dir_x -= 2;
457		else if (dir_x < 0.0)
458			dir_x += 2;
459		if (dir_y > 0.0)
460			dir_y -= 2;
461		else if (dir_y < 0.0)
462			dir_y += 2;
463		litest_touch_move(dev,
464				  0,
465				  50 + dir_x,
466				  50 + dir_y);
467		litest_touch_move(dev,
468				  1,
469				  50 - dir_x,
470				  50 - dir_y);
471		litest_touch_move(dev,
472				  2,
473				  51 - dir_x,
474				  51 - dir_y);
475		litest_pop_event_frame(dev);
476		libinput_dispatch(li);
477	}
478
479	if (hold == HOLD_GESTURE_REQUIRE) {
480		litest_assert_gesture_event(li,
481					LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
482					3);
483		litest_assert_gesture_event(li,
484					LIBINPUT_EVENT_GESTURE_HOLD_END,
485					3);
486	}
487	event = libinput_get_event(li);
488	gevent = litest_is_gesture_event(event,
489					 LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
490					 3);
491	dx = libinput_event_gesture_get_dx(gevent);
492	dy = libinput_event_gesture_get_dy(gevent);
493	scale = libinput_event_gesture_get_scale(gevent);
494	ck_assert(dx == 0.0);
495	ck_assert(dy == 0.0);
496	ck_assert(scale == 1.0);
497
498	libinput_event_destroy(event);
499
500	while ((event = libinput_get_event(li)) != NULL) {
501		gevent = litest_is_gesture_event(event,
502						 LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
503						 3);
504
505		oldscale = scale;
506		scale = libinput_event_gesture_get_scale(gevent);
507
508		ck_assert(scale < oldscale);
509
510		angle = libinput_event_gesture_get_angle_delta(gevent);
511		ck_assert_double_le(fabs(angle), 1.0);
512
513		libinput_event_destroy(event);
514		libinput_dispatch(li);
515	}
516
517	litest_touch_up(dev, 0);
518	litest_touch_up(dev, 1);
519	litest_touch_up(dev, 2);
520	libinput_dispatch(li);
521	event = libinput_get_event(li);
522	gevent = litest_is_gesture_event(event,
523					 LIBINPUT_EVENT_GESTURE_PINCH_END,
524					 3);
525	ck_assert(!libinput_event_gesture_get_cancelled(gevent));
526	libinput_event_destroy(event);
527}
528
529static void
530test_gesture_pinch_4fg(int cardinal, enum hold_gesture_behaviour hold)
531{
532	struct litest_device *dev = litest_current_device();
533	struct libinput *li = dev->libinput;
534	struct libinput_event *event;
535	struct libinput_event_gesture *gevent;
536	double dx, dy;
537	double dir_x, dir_y;
538	int i;
539	double scale, oldscale;
540	double angle;
541	int cardinals[NCARDINALS][2] = {
542		{ 0, 30 },
543		{ 30, 30 },
544		{ 30, 0 },
545		{ 30, -30 },
546		{ 0, -30 },
547		{ -30, -30 },
548		{ -30, 0 },
549		{ -30, 30 },
550	};
551
552	if (litest_slot_count(dev) < 4)
553		return;
554
555	dir_x = cardinals[cardinal][0];
556	dir_y = cardinals[cardinal][1];
557
558	litest_drain_events(li);
559
560	litest_touch_down(dev, 0, 50 + dir_x, 50 + dir_y);
561	litest_touch_down(dev, 1, 50 - dir_x, 50 - dir_y);
562	litest_touch_down(dev, 2, 51 - dir_x, 51 - dir_y);
563	litest_touch_down(dev, 3, 52 - dir_x, 52 - dir_y);
564	libinput_dispatch(li);
565
566	if (hold == HOLD_GESTURE_REQUIRE)
567		litest_timeout_gesture_hold();
568
569	for (i = 0; i < 7; i++) {
570		litest_push_event_frame(dev);
571		if (dir_x > 0.0)
572			dir_x -= 2;
573		else if (dir_x < 0.0)
574			dir_x += 2;
575		if (dir_y > 0.0)
576			dir_y -= 2;
577		else if (dir_y < 0.0)
578			dir_y += 2;
579		litest_touch_move(dev,
580				  0,
581				  50 + dir_x,
582				  50 + dir_y);
583		litest_touch_move(dev,
584				  1,
585				  50 - dir_x,
586				  50 - dir_y);
587		litest_touch_move(dev,
588				  2,
589				  51 - dir_x,
590				  51 - dir_y);
591		litest_touch_move(dev,
592				  3,
593				  52 - dir_x,
594				  52 - dir_y);
595		litest_pop_event_frame(dev);
596		libinput_dispatch(li);
597	}
598
599	if (hold == HOLD_GESTURE_REQUIRE) {
600		litest_assert_gesture_event(li,
601					LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
602					4);
603		litest_assert_gesture_event(li,
604					LIBINPUT_EVENT_GESTURE_HOLD_END,
605					4);
606	}
607
608	event = libinput_get_event(li);
609	gevent = litest_is_gesture_event(event,
610					 LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
611					 4);
612	dx = libinput_event_gesture_get_dx(gevent);
613	dy = libinput_event_gesture_get_dy(gevent);
614	scale = libinput_event_gesture_get_scale(gevent);
615	ck_assert(dx == 0.0);
616	ck_assert(dy == 0.0);
617	ck_assert(scale == 1.0);
618
619	libinput_event_destroy(event);
620
621	while ((event = libinput_get_event(li)) != NULL) {
622		gevent = litest_is_gesture_event(event,
623						 LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
624						 4);
625
626		oldscale = scale;
627		scale = libinput_event_gesture_get_scale(gevent);
628
629		ck_assert(scale < oldscale);
630
631		angle = libinput_event_gesture_get_angle_delta(gevent);
632		ck_assert_double_le(fabs(angle), 1.0);
633
634		libinput_event_destroy(event);
635		libinput_dispatch(li);
636	}
637
638	litest_touch_up(dev, 0);
639	litest_touch_up(dev, 1);
640	litest_touch_up(dev, 2);
641	litest_touch_up(dev, 3);
642	libinput_dispatch(li);
643	event = libinput_get_event(li);
644	gevent = litest_is_gesture_event(event,
645					 LIBINPUT_EVENT_GESTURE_PINCH_END,
646					 4);
647	ck_assert(!libinput_event_gesture_get_cancelled(gevent));
648	libinput_event_destroy(event);
649}
650
651static void
652test_gesture_spread(int cardinal, enum hold_gesture_behaviour hold)
653{
654	struct litest_device *dev = litest_current_device();
655	struct libinput *li = dev->libinput;
656	struct libinput_event *event;
657	struct libinput_event_gesture *gevent;
658	double dx, dy;
659	double dir_x, dir_y;
660	int i;
661	double scale, oldscale;
662	double angle;
663	int cardinals[NCARDINALS][2] = {
664		{ 0, 30 },
665		{ 30, 30 },
666		{ 30, 0 },
667		{ 30, -30 },
668		{ 0, -30 },
669		{ -30, -30 },
670		{ -30, 0 },
671		{ -30, 30 },
672	};
673
674	if (litest_slot_count(dev) < 2 ||
675	    !libinput_device_has_capability(dev->libinput_device,
676					    LIBINPUT_DEVICE_CAP_GESTURE))
677		return;
678
679	/* If the device is too small to provide a finger spread wide enough
680	 * to avoid the scroll bias, skip the test */
681	if (cardinal == E || cardinal == W) {
682		double w = 0, h = 0;
683		libinput_device_get_size(dev->libinput_device, &w, &h);
684		/* 0.6 because the code below gives us points like 20/y and
685		 * 80/y. 45 because the threshold in the code is 40mm */
686		if (w * 0.6 < 45)
687			return;
688	}
689
690	dir_x = cardinals[cardinal][0];
691	dir_y = cardinals[cardinal][1];
692
693	litest_drain_events(li);
694
695	litest_touch_down(dev, 0, 50 + dir_x, 50 + dir_y);
696	litest_touch_down(dev, 1, 50 - dir_x, 50 - dir_y);
697	libinput_dispatch(li);
698
699	if (hold == HOLD_GESTURE_REQUIRE)
700		litest_timeout_gesture_hold();
701
702	for (i = 0; i < 15; i++) {
703		litest_push_event_frame(dev);
704		if (dir_x > 0.0)
705			dir_x += 1;
706		else if (dir_x < 0.0)
707			dir_x -= 1;
708		if (dir_y > 0.0)
709			dir_y += 1;
710		else if (dir_y < 0.0)
711			dir_y -= 1;
712		litest_touch_move(dev,
713				  0,
714				  50 + dir_x,
715				  50 + dir_y);
716		litest_touch_move(dev,
717				  1,
718				  50 - dir_x,
719				  50 - dir_y);
720		litest_pop_event_frame(dev);
721		libinput_dispatch(li);
722	}
723
724	if (hold == HOLD_GESTURE_REQUIRE) {
725		litest_assert_gesture_event(li,
726					LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
727					2);
728		litest_assert_gesture_event(li,
729					LIBINPUT_EVENT_GESTURE_HOLD_END,
730					2);
731	}
732
733	event = libinput_get_event(li);
734	gevent = litest_is_gesture_event(event,
735					 LIBINPUT_EVENT_GESTURE_PINCH_BEGIN,
736					 2);
737	dx = libinput_event_gesture_get_dx(gevent);
738	dy = libinput_event_gesture_get_dy(gevent);
739	scale = libinput_event_gesture_get_scale(gevent);
740	ck_assert(dx == 0.0);
741	ck_assert(dy == 0.0);
742	ck_assert(scale == 1.0);
743
744	libinput_event_destroy(event);
745
746	while ((event = libinput_get_event(li)) != NULL) {
747		gevent = litest_is_gesture_event(event,
748						 LIBINPUT_EVENT_GESTURE_PINCH_UPDATE,
749						 2);
750		oldscale = scale;
751		scale = libinput_event_gesture_get_scale(gevent);
752		ck_assert(scale > oldscale);
753
754		angle = libinput_event_gesture_get_angle_delta(gevent);
755		ck_assert_double_le(fabs(angle), 1.0);
756
757		libinput_event_destroy(event);
758		libinput_dispatch(li);
759	}
760
761	litest_touch_up(dev, 0);
762	litest_touch_up(dev, 1);
763	libinput_dispatch(li);
764	event = libinput_get_event(li);
765	gevent = litest_is_gesture_event(event,
766					 LIBINPUT_EVENT_GESTURE_PINCH_END,
767					 2);
768	ck_assert(!libinput_event_gesture_get_cancelled(gevent));
769	libinput_event_destroy(event);
770}
771
772static void
773test_gesture_3fg_buttonarea_scroll(enum hold_gesture_behaviour hold)
774{
775	struct litest_device *dev = litest_current_device();
776	struct libinput *li = dev->libinput;
777
778	if (litest_slot_count(dev) < 3)
779		return;
780
781	litest_enable_buttonareas(dev);
782	litest_enable_2fg_scroll(dev);
783	litest_drain_events(li);
784
785	litest_touch_down(dev, 0, 40, 20);
786	litest_touch_down(dev, 1, 30, 20);
787	/* third finger in btnarea */
788	litest_touch_down(dev, 2, 50, 99);
789	libinput_dispatch(li);
790
791	if (hold == HOLD_GESTURE_REQUIRE)
792		litest_timeout_gesture_hold();
793
794	litest_touch_move_two_touches(dev, 40, 20, 30, 20, 0, 40, 10);
795
796	litest_touch_up(dev, 0);
797	litest_touch_up(dev, 1);
798	libinput_dispatch(li);
799
800	if (hold == HOLD_GESTURE_REQUIRE) {
801		litest_assert_gesture_event(li,
802					LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
803					2);
804		litest_assert_gesture_event(li,
805					LIBINPUT_EVENT_GESTURE_HOLD_END,
806					2);
807	}
808
809	litest_assert_scroll(li,
810			     LIBINPUT_EVENT_POINTER_SCROLL_FINGER,
811			     LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
812			     4);
813}
814
815static void
816test_gesture_hold(int nfingers)
817{
818	struct litest_device *dev = litest_current_device();
819	struct libinput *li = dev->libinput;
820
821	if (litest_slot_count(dev) < nfingers)
822		return;
823
824	litest_drain_events(li);
825
826	switch (nfingers) {
827	case 4:
828		litest_touch_down(dev, 3, 70, 30);
829		_fallthrough_;
830	case 3:
831		litest_touch_down(dev, 2, 60, 30);
832		_fallthrough_;
833	case 2:
834		litest_touch_down(dev, 1, 50, 30);
835		_fallthrough_;
836	case 1:
837		litest_touch_down(dev, 0, 40, 30);
838		break;
839	}
840
841	libinput_dispatch(li);
842	litest_timeout_gesture_hold();
843
844	if (libinput_device_has_capability(dev->libinput_device,
845					   LIBINPUT_DEVICE_CAP_GESTURE)) {
846		litest_assert_gesture_event(li,
847					    LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
848					    nfingers);
849	} else {
850		litest_assert_empty_queue(li);
851	}
852
853	switch (nfingers) {
854	case 4:
855		litest_touch_up(dev, 3);
856		_fallthrough_;
857	case 3:
858		litest_touch_up(dev, 2);
859		_fallthrough_;
860	case 2:
861		litest_touch_up(dev, 1);
862		_fallthrough_;
863	case 1:
864		litest_touch_up(dev, 0);
865		break;
866	}
867
868	libinput_dispatch(li);
869	if (libinput_device_has_capability(dev->libinput_device,
870					   LIBINPUT_DEVICE_CAP_GESTURE)) {
871		litest_assert_gesture_event(li,
872					    LIBINPUT_EVENT_GESTURE_HOLD_END,
873					    nfingers);
874	}
875
876	litest_assert_empty_queue(li);
877}
878
879static void
880test_gesture_hold_cancel(int nfingers)
881{
882	struct litest_device *dev = litest_current_device();
883	struct libinput *li = dev->libinput;
884	int last_finger = (nfingers - 1);
885
886	if (litest_slot_count(dev) < nfingers)
887		return;
888
889	litest_drain_events(li);
890
891	switch (nfingers) {
892	case 4:
893		litest_touch_down(dev, 3, 70, 30);
894		_fallthrough_;
895	case 3:
896		litest_touch_down(dev, 2, 60, 30);
897		_fallthrough_;
898	case 2:
899		litest_touch_down(dev, 1, 50, 30);
900		_fallthrough_;
901	case 1:
902		litest_touch_down(dev, 0, 40, 30);
903		break;
904	}
905
906	libinput_dispatch(li);
907	litest_timeout_gesture_hold();
908
909	litest_touch_up(dev, last_finger);
910
911	if (libinput_device_has_capability(dev->libinput_device,
912					   LIBINPUT_DEVICE_CAP_GESTURE)) {
913		litest_assert_gesture_event(li,
914					    LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
915					    nfingers);
916		litest_assert_gesture_event(li,
917					    LIBINPUT_EVENT_GESTURE_HOLD_END,
918					    nfingers);
919	}
920
921	litest_assert_empty_queue(li);
922}
923
924START_TEST(gestures_cap)
925{
926	struct litest_device *dev = litest_current_device();
927	struct libinput_device *device = dev->libinput_device;
928
929	if (libevdev_has_property(dev->evdev, INPUT_PROP_SEMI_MT))
930		ck_assert(!libinput_device_has_capability(device,
931					  LIBINPUT_DEVICE_CAP_GESTURE));
932	else
933		ck_assert(libinput_device_has_capability(device,
934					 LIBINPUT_DEVICE_CAP_GESTURE));
935}
936END_TEST
937
938START_TEST(gestures_nocap)
939{
940	struct litest_device *dev = litest_current_device();
941	struct libinput_device *device = dev->libinput_device;
942
943	ck_assert(!libinput_device_has_capability(device,
944						  LIBINPUT_DEVICE_CAP_GESTURE));
945}
946END_TEST
947
948START_TEST(gestures_swipe_3fg)
949{
950	int cardinal = _i; /* ranged test */
951	test_gesture_swipe_3fg(cardinal, HOLD_GESTURE_IGNORE);
952}
953END_TEST
954
955START_TEST(gestures_swipe_3fg_btntool)
956{
957	struct litest_device *dev = litest_current_device();
958	struct libinput *li = dev->libinput;
959	struct libinput_event *event;
960	struct libinput_event_gesture *gevent;
961	double dx, dy;
962	int cardinal = _i; /* ranged test */
963	double dir_x, dir_y;
964	int cardinals[NCARDINALS][2] = {
965		{ 0, 30 },
966		{ 30, 30 },
967		{ 30, 0 },
968		{ 30, -30 },
969		{ 0, -30 },
970		{ -30, -30 },
971		{ -30, 0 },
972		{ -30, 30 },
973	};
974
975	if (litest_slot_count(dev) > 2 ||
976	    !libevdev_has_event_code(dev->evdev, EV_KEY, BTN_TOOL_TRIPLETAP) ||
977	    !libinput_device_has_capability(dev->libinput_device,
978					    LIBINPUT_DEVICE_CAP_GESTURE))
979		return;
980
981	dir_x = cardinals[cardinal][0];
982	dir_y = cardinals[cardinal][1];
983
984	litest_drain_events(li);
985
986	litest_touch_down(dev, 0, 40, 40);
987	litest_touch_down(dev, 1, 50, 40);
988	litest_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, 0);
989	litest_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, 1);
990	litest_event(dev, EV_SYN, SYN_REPORT, 0);
991
992	libinput_dispatch(li);
993	litest_touch_move_two_touches(dev, 40, 40, 50, 40, dir_x, dir_y, 10);
994	libinput_dispatch(li);
995
996	event = libinput_get_event(li);
997	gevent = litest_is_gesture_event(event,
998					 LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
999					 3);
1000	dx = libinput_event_gesture_get_dx(gevent);
1001	dy = libinput_event_gesture_get_dy(gevent);
1002	ck_assert(dx == 0.0);
1003	ck_assert(dy == 0.0);
1004	libinput_event_destroy(event);
1005
1006	while ((event = libinput_get_event(li)) != NULL) {
1007		gevent = litest_is_gesture_event(event,
1008						 LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
1009						 3);
1010
1011		dx = libinput_event_gesture_get_dx(gevent);
1012		dy = libinput_event_gesture_get_dy(gevent);
1013		if (dir_x == 0.0)
1014			ck_assert(dx == 0.0);
1015		else if (dir_x < 0.0)
1016			ck_assert(dx < 0.0);
1017		else if (dir_x > 0.0)
1018			ck_assert(dx > 0.0);
1019
1020		if (dir_y == 0.0)
1021			ck_assert(dy == 0.0);
1022		else if (dir_y < 0.0)
1023			ck_assert(dy < 0.0);
1024		else if (dir_y > 0.0)
1025			ck_assert(dy > 0.0);
1026
1027		dx = libinput_event_gesture_get_dx_unaccelerated(gevent);
1028		dy = libinput_event_gesture_get_dy_unaccelerated(gevent);
1029		if (dir_x == 0.0)
1030			ck_assert(dx == 0.0);
1031		else if (dir_x < 0.0)
1032			ck_assert(dx < 0.0);
1033		else if (dir_x > 0.0)
1034			ck_assert(dx > 0.0);
1035
1036		if (dir_y == 0.0)
1037			ck_assert(dy == 0.0);
1038		else if (dir_y < 0.0)
1039			ck_assert(dy < 0.0);
1040		else if (dir_y > 0.0)
1041			ck_assert(dy > 0.0);
1042
1043		libinput_event_destroy(event);
1044	}
1045
1046	litest_touch_up(dev, 0);
1047	litest_touch_up(dev, 1);
1048	libinput_dispatch(li);
1049	event = libinput_get_event(li);
1050	gevent = litest_is_gesture_event(event,
1051					 LIBINPUT_EVENT_GESTURE_SWIPE_END,
1052					 3);
1053	ck_assert(!libinput_event_gesture_get_cancelled(gevent));
1054	libinput_event_destroy(event);
1055}
1056END_TEST
1057
1058START_TEST(gestures_swipe_3fg_btntool_pinch_like)
1059{
1060	struct litest_device *dev = litest_current_device();
1061	struct libinput *li = dev->libinput;
1062	struct libinput_event *event;
1063	struct libinput_event_gesture *gevent;
1064
1065	if (litest_slot_count(dev) > 2 ||
1066	    !libevdev_has_event_code(dev->evdev, EV_KEY, BTN_TOOL_TRIPLETAP) ||
1067	    !libinput_device_has_capability(dev->libinput_device,
1068					    LIBINPUT_DEVICE_CAP_GESTURE))
1069		return;
1070
1071	litest_drain_events(li);
1072
1073	/* Technically a pinch position + pinch movement, but expect swipe
1074	 * for nfingers > nslots */
1075	litest_touch_down(dev, 0, 20, 60);
1076	litest_touch_down(dev, 1, 50, 20);
1077	litest_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, 0);
1078	litest_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, 1);
1079	litest_event(dev, EV_SYN, SYN_REPORT, 0);
1080
1081	libinput_dispatch(li);
1082	litest_touch_move_to(dev, 0, 20, 60, 10, 80, 20);
1083	libinput_dispatch(li);
1084
1085	event = libinput_get_event(li);
1086	litest_is_gesture_event(event, LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN, 3);
1087	libinput_event_destroy(event);
1088
1089	while ((event = libinput_get_event(li)) != NULL) {
1090		litest_is_gesture_event(event,
1091					LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
1092					3);
1093		libinput_event_destroy(event);
1094	}
1095
1096	litest_touch_up(dev, 0);
1097	litest_touch_up(dev, 1);
1098	libinput_dispatch(li);
1099	event = libinput_get_event(li);
1100	gevent = litest_is_gesture_event(event,
1101					 LIBINPUT_EVENT_GESTURE_SWIPE_END,
1102					 3);
1103	ck_assert(!libinput_event_gesture_get_cancelled(gevent));
1104	libinput_event_destroy(event);
1105}
1106END_TEST
1107
1108START_TEST(gestures_swipe_4fg)
1109{
1110	int cardinal = _i; /* ranged test */
1111	test_gesture_swipe_4fg(cardinal, HOLD_GESTURE_IGNORE);
1112}
1113END_TEST
1114
1115START_TEST(gestures_swipe_4fg_btntool)
1116{
1117	struct litest_device *dev = litest_current_device();
1118	struct libinput *li = dev->libinput;
1119	struct libinput_event *event;
1120	struct libinput_event_gesture *gevent;
1121	double dx, dy;
1122	int cardinal = _i; /* ranged test */
1123	double dir_x, dir_y;
1124	int cardinals[NCARDINALS][2] = {
1125		{ 0, 30 },
1126		{ 30, 30 },
1127		{ 30, 0 },
1128		{ 30, -30 },
1129		{ 0, -30 },
1130		{ -30, -30 },
1131		{ -30, 0 },
1132		{ -30, 30 },
1133	};
1134
1135	if (litest_slot_count(dev) > 2 ||
1136	    !libevdev_has_event_code(dev->evdev, EV_KEY, BTN_TOOL_QUADTAP) ||
1137	    !libinput_device_has_capability(dev->libinput_device,
1138					    LIBINPUT_DEVICE_CAP_GESTURE))
1139		return;
1140
1141	dir_x = cardinals[cardinal][0];
1142	dir_y = cardinals[cardinal][1];
1143
1144	litest_drain_events(li);
1145
1146	litest_touch_down(dev, 0, 40, 40);
1147	litest_touch_down(dev, 1, 50, 40);
1148	litest_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, 0);
1149	litest_event(dev, EV_KEY, BTN_TOOL_QUADTAP, 1);
1150	litest_event(dev, EV_SYN, SYN_REPORT, 0);
1151
1152	libinput_dispatch(li);
1153	litest_touch_move_two_touches(dev, 40, 40, 50, 40, dir_x, dir_y, 10);
1154	libinput_dispatch(li);
1155
1156	event = libinput_get_event(li);
1157	gevent = litest_is_gesture_event(event,
1158					 LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
1159					 4);
1160	dx = libinput_event_gesture_get_dx(gevent);
1161	dy = libinput_event_gesture_get_dy(gevent);
1162	ck_assert(dx == 0.0);
1163	ck_assert(dy == 0.0);
1164	libinput_event_destroy(event);
1165
1166	while ((event = libinput_get_event(li)) != NULL) {
1167		gevent = litest_is_gesture_event(event,
1168						 LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
1169						 4);
1170
1171		dx = libinput_event_gesture_get_dx(gevent);
1172		dy = libinput_event_gesture_get_dy(gevent);
1173		if (dir_x == 0.0)
1174			ck_assert(dx == 0.0);
1175		else if (dir_x < 0.0)
1176			ck_assert(dx < 0.0);
1177		else if (dir_x > 0.0)
1178			ck_assert(dx > 0.0);
1179
1180		if (dir_y == 0.0)
1181			ck_assert(dy == 0.0);
1182		else if (dir_y < 0.0)
1183			ck_assert(dy < 0.0);
1184		else if (dir_y > 0.0)
1185			ck_assert(dy > 0.0);
1186
1187		dx = libinput_event_gesture_get_dx_unaccelerated(gevent);
1188		dy = libinput_event_gesture_get_dy_unaccelerated(gevent);
1189		if (dir_x == 0.0)
1190			ck_assert(dx == 0.0);
1191		else if (dir_x < 0.0)
1192			ck_assert(dx < 0.0);
1193		else if (dir_x > 0.0)
1194			ck_assert(dx > 0.0);
1195
1196		if (dir_y == 0.0)
1197			ck_assert(dy == 0.0);
1198		else if (dir_y < 0.0)
1199			ck_assert(dy < 0.0);
1200		else if (dir_y > 0.0)
1201			ck_assert(dy > 0.0);
1202
1203		libinput_event_destroy(event);
1204	}
1205
1206	litest_touch_up(dev, 0);
1207	litest_touch_up(dev, 1);
1208	libinput_dispatch(li);
1209	event = libinput_get_event(li);
1210	gevent = litest_is_gesture_event(event,
1211					 LIBINPUT_EVENT_GESTURE_SWIPE_END,
1212					 4);
1213	ck_assert(!libinput_event_gesture_get_cancelled(gevent));
1214	libinput_event_destroy(event);
1215}
1216END_TEST
1217
1218START_TEST(gestures_pinch)
1219{
1220	int cardinal = _i; /* ranged test */
1221	test_gesture_pinch_2fg(cardinal, HOLD_GESTURE_IGNORE);
1222}
1223END_TEST
1224
1225START_TEST(gestures_pinch_3fg)
1226{
1227	int cardinal = _i; /* ranged test */
1228	test_gesture_pinch_3fg(cardinal, HOLD_GESTURE_IGNORE);
1229}
1230END_TEST
1231
1232START_TEST(gestures_pinch_4fg)
1233{
1234	int cardinal = _i; /* ranged test */
1235	test_gesture_pinch_4fg(cardinal, HOLD_GESTURE_IGNORE);
1236}
1237END_TEST
1238
1239START_TEST(gestures_spread)
1240{
1241	int cardinal = _i; /* ranged test */
1242	test_gesture_spread(cardinal, HOLD_GESTURE_IGNORE);
1243}
1244END_TEST
1245
1246START_TEST(gestures_time_usec)
1247{
1248	struct litest_device *dev = litest_current_device();
1249	struct libinput *li = dev->libinput;
1250	struct libinput_event *event;
1251	struct libinput_event_gesture *gevent;
1252	uint64_t time_usec;
1253
1254	if (litest_slot_count(dev) < 3)
1255		return;
1256
1257	litest_drain_events(li);
1258
1259	litest_touch_down(dev, 0, 40, 40);
1260	litest_touch_down(dev, 1, 50, 40);
1261	litest_touch_down(dev, 2, 60, 40);
1262	libinput_dispatch(li);
1263	litest_touch_move_three_touches(dev, 40, 40, 50, 40, 60, 40, 0, 30,
1264					30);
1265
1266	libinput_dispatch(li);
1267	event = libinput_get_event(li);
1268	gevent = litest_is_gesture_event(event,
1269					 LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
1270					 3);
1271	time_usec = libinput_event_gesture_get_time_usec(gevent);
1272	ck_assert_int_eq(libinput_event_gesture_get_time(gevent),
1273			 (uint32_t) (time_usec / 1000));
1274	libinput_event_destroy(event);
1275}
1276END_TEST
1277
1278START_TEST(gestures_3fg_buttonarea_scroll)
1279{
1280	test_gesture_3fg_buttonarea_scroll(HOLD_GESTURE_IGNORE);
1281}
1282END_TEST
1283
1284START_TEST(gestures_3fg_buttonarea_scroll_btntool)
1285{
1286	struct litest_device *dev = litest_current_device();
1287	struct libinput *li = dev->libinput;
1288
1289	if (litest_slot_count(dev) > 2)
1290		return;
1291
1292	litest_enable_buttonareas(dev);
1293	litest_enable_2fg_scroll(dev);
1294	litest_drain_events(li);
1295
1296	/* first finger in btnarea */
1297	litest_touch_down(dev, 0, 20, 99);
1298	litest_touch_down(dev, 1, 30, 20);
1299	litest_event(dev, EV_KEY, BTN_TOOL_DOUBLETAP, 0);
1300	litest_event(dev, EV_KEY, BTN_TOOL_TRIPLETAP, 1);
1301	litest_event(dev, EV_SYN, SYN_REPORT, 0);
1302	libinput_dispatch(li);
1303	litest_touch_move_to(dev, 1, 30, 20, 30, 70, 10);
1304
1305	litest_touch_up(dev, 1);
1306	libinput_dispatch(li);
1307	litest_assert_scroll(li,
1308			     LIBINPUT_EVENT_POINTER_SCROLL_FINGER,
1309			     LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL,
1310			     4);
1311}
1312END_TEST
1313
1314START_TEST(gestures_swipe_3fg_unaccel)
1315{
1316	struct litest_device *dev = litest_current_device();
1317	struct libinput *li = dev->libinput;
1318	struct libinput_event *event;
1319	double reference_ux = 0, reference_uy = 0;
1320
1321	/**
1322	 * This magic number is an artifact of the acceleration code.
1323	 * The maximum factor in the touchpad accel profile is 4.8 times the
1324	 * speed setting (1.000875 at default setting 0). The factor
1325	 * applied to the const acceleration is the 0.9 baseline.
1326	 * So our two sets of coordinates are:
1327	 * accel = 4.8 * delta * normalize_magic
1328	 * unaccel = 0.9 * delta * normalize_magic
1329	 *
1330	 * Since delta and the normalization magic are the same for both,
1331	 * our accelerated deltas can be a maximum of 4.8/0.9 bigger than
1332	 * the unaccelerated deltas.
1333	 *
1334	 * If any of the accel methods numbers change, this will have to
1335	 * change here too.
1336	 */
1337	const double max_factor = 5.34;
1338
1339	if (litest_slot_count(dev) < 3)
1340		return;
1341
1342	litest_drain_events(li);
1343	litest_touch_down(dev, 0, 40, 20);
1344	litest_touch_down(dev, 1, 50, 20);
1345	litest_touch_down(dev, 2, 60, 20);
1346	libinput_dispatch(li);
1347	litest_touch_move_three_touches(dev,
1348					40, 20,
1349					50, 20,
1350					60, 20,
1351					30, 40,
1352					10);
1353	libinput_dispatch(li);
1354
1355	event = libinput_get_event(li);
1356	litest_is_gesture_event(event,
1357				LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN,
1358				3);
1359	libinput_event_destroy(event);
1360	event = libinput_get_event(li);
1361	do {
1362		struct libinput_event_gesture *gevent;
1363		double dx, dy;
1364		double ux, uy;
1365
1366		gevent = litest_is_gesture_event(event,
1367						 LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE,
1368						 3);
1369		dx = libinput_event_gesture_get_dx(gevent);
1370		dy = libinput_event_gesture_get_dy(gevent);
1371		ux = libinput_event_gesture_get_dx_unaccelerated(gevent);
1372		uy = libinput_event_gesture_get_dy_unaccelerated(gevent);
1373
1374		ck_assert_double_ne(ux, 0.0);
1375		ck_assert_double_ne(uy, 0.0);
1376
1377		if (!reference_ux)
1378			reference_ux = ux;
1379		if (!reference_uy)
1380			reference_uy = uy;
1381
1382		/* The unaccelerated delta should be the same for every
1383		 * event, but we have rounding errors since we only control
1384		 * input data as percentage of the touchpad size.
1385		 * so we just eyeball it */
1386		ck_assert_double_gt(ux, reference_ux - 2);
1387		ck_assert_double_lt(ux, reference_ux + 2);
1388		ck_assert_double_gt(uy, reference_uy - 2);
1389		ck_assert_double_lt(uy, reference_uy + 2);
1390
1391		/* All our touchpads are large enough to make this is a fast
1392		 * swipe, we don't expect deceleration, unaccel should
1393		 * always be less than accel delta */
1394		ck_assert_double_lt(ux, dx);
1395		ck_assert_double_lt(ux, dx);
1396
1397		/* Check our accelerated delta is within the expected
1398		 * maximum. */
1399		ck_assert_double_lt(dx, ux * max_factor);
1400		ck_assert_double_lt(dy, uy * max_factor);
1401
1402		libinput_event_destroy(event);
1403	} while ((event = libinput_get_event(li)));
1404
1405	litest_touch_up(dev, 0);
1406	litest_touch_up(dev, 1);
1407	litest_touch_up(dev, 2);
1408}
1409END_TEST
1410
1411START_TEST(gestures_hold_config_default_disabled)
1412{
1413	struct litest_device *dev = litest_current_device();
1414	struct libinput_device *device = dev->libinput_device;
1415
1416	ck_assert_int_eq(libinput_device_config_gesture_hold_is_available(device),
1417			 0);
1418	ck_assert_int_eq(libinput_device_config_gesture_get_hold_default_enabled(device),
1419			 LIBINPUT_CONFIG_HOLD_DISABLED);
1420	ck_assert_int_eq(libinput_device_config_gesture_get_hold_default_enabled(device),
1421			 LIBINPUT_CONFIG_HOLD_DISABLED);
1422}
1423END_TEST
1424
1425START_TEST(gestures_hold_config_default_enabled)
1426{
1427	struct litest_device *dev = litest_current_device();
1428	struct libinput_device *device = dev->libinput_device;
1429
1430	ck_assert_int_eq(libinput_device_config_gesture_hold_is_available(device),
1431			 1);
1432	ck_assert_int_eq(libinput_device_config_gesture_get_hold_default_enabled(device),
1433			 LIBINPUT_CONFIG_HOLD_ENABLED);
1434	ck_assert_int_eq(libinput_device_config_gesture_get_hold_enabled(device),
1435			 LIBINPUT_CONFIG_HOLD_ENABLED);
1436}
1437END_TEST
1438
1439START_TEST(gestures_hold_config_set_invalid)
1440{
1441	struct litest_device *dev = litest_current_device();
1442	struct libinput_device *device = dev->libinput_device;
1443
1444	ck_assert_int_eq(libinput_device_config_gesture_set_hold_enabled(device, -1),
1445			 LIBINPUT_CONFIG_STATUS_INVALID);
1446	ck_assert_int_eq(libinput_device_config_gesture_set_hold_enabled(device, 2),
1447			 LIBINPUT_CONFIG_STATUS_INVALID);
1448}
1449END_TEST
1450
1451START_TEST(gestures_hold_config_is_available)
1452{
1453	struct litest_device *dev = litest_current_device();
1454	struct libinput_device *device = dev->libinput_device;
1455
1456	ck_assert_int_eq(libinput_device_config_gesture_hold_is_available(device),
1457			 1);
1458	ck_assert_int_eq(libinput_device_config_gesture_get_hold_enabled(device),
1459			 LIBINPUT_CONFIG_HOLD_ENABLED);
1460	ck_assert_int_eq(libinput_device_config_gesture_set_hold_enabled(device, LIBINPUT_CONFIG_HOLD_DISABLED),
1461			 LIBINPUT_CONFIG_STATUS_SUCCESS);
1462	ck_assert_int_eq(libinput_device_config_gesture_get_hold_enabled(device),
1463			 LIBINPUT_CONFIG_HOLD_DISABLED);
1464}
1465END_TEST
1466
1467START_TEST(gestures_hold_config_is_not_available)
1468{
1469	struct litest_device *dev = litest_current_device();
1470	struct libinput_device *device = dev->libinput_device;
1471
1472	ck_assert_int_eq(libinput_device_config_gesture_hold_is_available(device),
1473			 0);
1474	ck_assert_int_eq(libinput_device_config_gesture_get_hold_enabled(device),
1475			 LIBINPUT_CONFIG_HOLD_DISABLED);
1476	ck_assert_int_eq(libinput_device_config_gesture_set_hold_enabled(device, LIBINPUT_CONFIG_HOLD_ENABLED),
1477			 LIBINPUT_CONFIG_STATUS_UNSUPPORTED);
1478	ck_assert_int_eq(libinput_device_config_gesture_set_hold_enabled(device, LIBINPUT_CONFIG_HOLD_DISABLED),
1479			 LIBINPUT_CONFIG_STATUS_SUCCESS);
1480}
1481END_TEST
1482
1483START_TEST(gestures_hold)
1484{
1485	struct litest_device *dev = litest_current_device();
1486	struct libinput *li = dev->libinput;
1487	int nfingers = _i; /* ranged test */
1488
1489	litest_disable_tap(dev->libinput_device);
1490	litest_drain_events(li);
1491
1492	test_gesture_hold(nfingers);
1493}
1494END_TEST
1495
1496START_TEST(gestures_hold_tap_enabled)
1497{
1498	struct litest_device *dev = litest_current_device();
1499	struct libinput *li = dev->libinput;
1500	int nfingers = _i; /* ranged test */
1501
1502	litest_enable_tap(dev->libinput_device);
1503	litest_drain_events(li);
1504
1505	test_gesture_hold(nfingers);
1506}
1507END_TEST
1508
1509START_TEST(gestures_hold_cancel)
1510{
1511	struct litest_device *dev = litest_current_device();
1512	struct libinput *li = dev->libinput;
1513	int nfingers = _i; /* ranged test */
1514
1515	litest_disable_tap(dev->libinput_device);
1516	litest_drain_events(li);
1517
1518	test_gesture_hold_cancel(nfingers);
1519}
1520END_TEST
1521
1522START_TEST(gestures_hold_cancel_tap_enabled)
1523{
1524	struct litest_device *dev = litest_current_device();
1525	struct libinput *li = dev->libinput;
1526	int nfingers = _i; /* ranged test */
1527
1528	litest_enable_tap(dev->libinput_device);
1529	litest_drain_events(li);
1530
1531	test_gesture_hold_cancel(nfingers);
1532}
1533END_TEST
1534
1535START_TEST(gestures_hold_then_swipe_3fg)
1536{
1537	int cardinal = _i; /* ranged test */
1538	test_gesture_swipe_3fg(cardinal, HOLD_GESTURE_REQUIRE);
1539}
1540END_TEST
1541
1542START_TEST(gestures_hold_then_swipe_4fg)
1543{
1544	int cardinal = _i; /* ranged test */
1545	test_gesture_swipe_4fg(cardinal, HOLD_GESTURE_REQUIRE);
1546}
1547END_TEST
1548
1549START_TEST(gestures_hold_then_pinch_2fg)
1550{
1551	int cardinal = _i; /* ranged test */
1552	test_gesture_pinch_2fg(cardinal, HOLD_GESTURE_REQUIRE);
1553}
1554END_TEST
1555
1556START_TEST(gestures_hold_then_pinch_3fg)
1557{
1558	int cardinal = _i; /* ranged test */
1559	test_gesture_pinch_3fg(cardinal, HOLD_GESTURE_REQUIRE);
1560}
1561END_TEST
1562
1563START_TEST(gestures_hold_then_pinch_4fg)
1564{
1565	int cardinal = _i; /* ranged test */
1566	test_gesture_pinch_4fg(cardinal, HOLD_GESTURE_REQUIRE);
1567}
1568END_TEST
1569
1570START_TEST(gestures_hold_then_spread)
1571{
1572	int cardinal = _i; /* ranged test */
1573	test_gesture_spread(cardinal, HOLD_GESTURE_REQUIRE);
1574}
1575END_TEST
1576
1577START_TEST(gestures_hold_then_3fg_buttonarea_scroll)
1578{
1579	test_gesture_3fg_buttonarea_scroll(HOLD_GESTURE_REQUIRE);
1580}
1581END_TEST
1582
1583START_TEST(gestures_hold_once_on_double_tap)
1584{
1585	struct litest_device *dev = litest_current_device();
1586	struct libinput *li = dev->libinput;
1587
1588	if (!libinput_device_has_capability(dev->libinput_device,
1589					    LIBINPUT_DEVICE_CAP_GESTURE))
1590		return;
1591
1592	litest_enable_tap(dev->libinput_device);
1593	litest_drain_events(li);
1594
1595	/* First tap, a hold gesture must be generated */
1596	litest_touch_down(dev, 0, 50, 50);
1597	libinput_dispatch(li);
1598	litest_timeout_gesture_quick_hold();
1599	litest_touch_up(dev, 0);
1600	libinput_dispatch(li);
1601
1602	litest_assert_gesture_event(li,
1603				    LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
1604				    1);
1605	litest_assert_gesture_event(li,
1606				    LIBINPUT_EVENT_GESTURE_HOLD_END,
1607				    1);
1608	litest_assert_button_event(li, BTN_LEFT,
1609				   LIBINPUT_BUTTON_STATE_PRESSED);
1610	litest_assert_button_event(li, BTN_LEFT,
1611				   LIBINPUT_BUTTON_STATE_RELEASED);
1612	litest_assert_empty_queue(li);
1613
1614	/* Double tap, don't generate an extra hold gesture */
1615	litest_touch_down(dev, 0, 50, 50);
1616	litest_touch_up(dev, 0);
1617	libinput_dispatch(li);
1618	litest_timeout_gesture_quick_hold();
1619
1620	litest_assert_button_event(li, BTN_LEFT,
1621				   LIBINPUT_BUTTON_STATE_PRESSED);
1622	litest_assert_button_event(li, BTN_LEFT,
1623				   LIBINPUT_BUTTON_STATE_RELEASED);
1624
1625	litest_assert_empty_queue(li);
1626}
1627END_TEST
1628
1629START_TEST(gestures_hold_once_tap_n_drag)
1630{
1631	struct litest_device *dev = litest_current_device();
1632	struct libinput *li = dev->libinput;
1633	int nfingers = _i; /* ranged test */
1634	unsigned int button = 0;
1635
1636	if (nfingers > litest_slot_count(dev))
1637		return;
1638
1639	if (!libinput_device_has_capability(dev->libinput_device,
1640					    LIBINPUT_DEVICE_CAP_GESTURE))
1641		return;
1642
1643	litest_enable_tap(dev->libinput_device);
1644	litest_disable_drag_lock(dev->libinput_device);
1645	litest_drain_events(li);
1646
1647	switch (nfingers) {
1648	case 1:
1649		button = BTN_LEFT;
1650		break;
1651	case 2:
1652		button = BTN_RIGHT;
1653		break;
1654	case 3:
1655		button = BTN_MIDDLE;
1656		break;
1657	default:
1658		abort();
1659	}
1660
1661	switch (nfingers) {
1662	case 3:
1663		litest_touch_down(dev, 2, 60, 30);
1664		_fallthrough_;
1665	case 2:
1666		litest_touch_down(dev, 1, 50, 30);
1667		_fallthrough_;
1668	case 1:
1669		litest_touch_down(dev, 0, 40, 30);
1670		break;
1671	}
1672	libinput_dispatch(li);
1673	litest_timeout_gesture_quick_hold();
1674
1675	switch (nfingers) {
1676	case 3:
1677		litest_touch_up(dev, 2);
1678		_fallthrough_;
1679	case 2:
1680		litest_touch_up(dev, 1);
1681		_fallthrough_;
1682	case 1:
1683		litest_touch_up(dev, 0);
1684		break;
1685	}
1686	libinput_dispatch(li);
1687
1688	/* "Quick" hold gestures are only generated when using 1 or 2 fingers */
1689	if (nfingers == 1 || nfingers == 2) {
1690		litest_assert_gesture_event(li,
1691					    LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
1692					    nfingers);
1693		litest_assert_gesture_event(li,
1694					    LIBINPUT_EVENT_GESTURE_HOLD_END,
1695					    nfingers);
1696	}
1697
1698	/* Tap and drag, don't generate an extra hold gesture */
1699	litest_touch_down(dev, 0, 50, 50);
1700	litest_touch_move_to(dev, 0, 50, 50, 80, 80, 20);
1701	libinput_dispatch(li);
1702
1703	litest_assert_button_event(li, button,
1704				   LIBINPUT_BUTTON_STATE_PRESSED);
1705	litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
1706
1707	litest_touch_up(dev, 0);
1708	libinput_dispatch(li);
1709
1710	litest_assert_button_event(li, button,
1711				   LIBINPUT_BUTTON_STATE_RELEASED);
1712	litest_assert_empty_queue(li);
1713}
1714END_TEST
1715
1716START_TEST(gestures_hold_and_motion_before_timeout)
1717{
1718	struct litest_device *dev = litest_current_device();
1719	struct libinput *li = dev->libinput;
1720
1721	if (!libinput_device_has_capability(dev->libinput_device,
1722					    LIBINPUT_DEVICE_CAP_GESTURE))
1723		return;
1724
1725	litest_drain_events(li);
1726
1727	litest_touch_down(dev, 0, 50, 50);
1728	libinput_dispatch(li);
1729
1730	litest_touch_move_to(dev, 0, 50, 50, 51, 51, 1);
1731	litest_touch_move_to(dev, 0, 51, 51, 50, 50, 1);
1732	libinput_dispatch(li);
1733
1734	litest_timeout_gesture_quick_hold();
1735
1736	litest_drain_events_of_type(li, LIBINPUT_EVENT_POINTER_MOTION, -1);
1737
1738	litest_assert_gesture_event(li,
1739				    LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
1740				    1);
1741
1742	litest_touch_up(dev, 0);
1743	libinput_dispatch(li);
1744
1745	litest_assert_gesture_event(li,
1746				    LIBINPUT_EVENT_GESTURE_HOLD_END,
1747				    1);
1748	litest_assert_empty_queue(li);
1749}
1750END_TEST
1751
1752START_TEST(gestures_hold_and_motion_after_timeout)
1753{
1754	struct litest_device *dev = litest_current_device();
1755	struct libinput *li = dev->libinput;
1756
1757	if (!libinput_device_has_capability(dev->libinput_device,
1758					    LIBINPUT_DEVICE_CAP_GESTURE))
1759		return;
1760
1761	litest_drain_events(li);
1762
1763	litest_touch_down(dev, 0, 50, 50);
1764	libinput_dispatch(li);
1765	litest_timeout_gesture_quick_hold();
1766
1767	litest_assert_gesture_event(li,
1768				    LIBINPUT_EVENT_GESTURE_HOLD_BEGIN,
1769				    1);
1770
1771	litest_touch_move_to(dev, 0, 50, 50, 51, 51, 1);
1772	litest_touch_move_to(dev, 0, 51, 51, 50, 50, 1);
1773	libinput_dispatch(li);
1774	litest_assert_only_typed_events(li, LIBINPUT_EVENT_POINTER_MOTION);
1775
1776	litest_touch_up(dev, 0);
1777	libinput_dispatch(li);
1778
1779	litest_assert_gesture_event(li,
1780				    LIBINPUT_EVENT_GESTURE_HOLD_END,
1781				    1);
1782	litest_assert_empty_queue(li);
1783}
1784END_TEST
1785
1786TEST_COLLECTION(gestures)
1787{
1788	struct range cardinals = { N, N + NCARDINALS };
1789	struct range range_hold = { 1, 5 };
1790	struct range range_multifinger_tap = {1, 4};
1791
1792	litest_add(gestures_cap, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1793	litest_add(gestures_nocap, LITEST_ANY, LITEST_TOUCHPAD);
1794
1795	litest_add_ranged(gestures_swipe_3fg, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1796	litest_add_ranged(gestures_swipe_3fg_btntool, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1797	litest_add(gestures_swipe_3fg_btntool_pinch_like, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1798	litest_add_ranged(gestures_swipe_4fg, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1799	litest_add_ranged(gestures_swipe_4fg_btntool, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1800	litest_add_ranged(gestures_pinch, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1801	litest_add_ranged(gestures_pinch_3fg, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1802	litest_add_ranged(gestures_pinch_4fg, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1803	litest_add_ranged(gestures_spread, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1804
1805	litest_add(gestures_3fg_buttonarea_scroll, LITEST_CLICKPAD, LITEST_SINGLE_TOUCH);
1806	litest_add(gestures_3fg_buttonarea_scroll_btntool, LITEST_CLICKPAD, LITEST_SINGLE_TOUCH);
1807
1808	litest_add(gestures_time_usec, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1809
1810	litest_add(gestures_hold_config_default_disabled, LITEST_TOUCHPAD|LITEST_SEMI_MT, LITEST_ANY);
1811	litest_add(gestures_hold_config_default_enabled, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH|LITEST_SEMI_MT);
1812	litest_add(gestures_hold_config_set_invalid, LITEST_TOUCHPAD, LITEST_ANY);
1813	litest_add(gestures_hold_config_is_available, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH|LITEST_SEMI_MT);
1814	litest_add(gestures_hold_config_is_not_available, LITEST_TOUCHPAD|LITEST_SEMI_MT, LITEST_ANY);
1815
1816	litest_add_ranged(gestures_hold, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &range_hold);
1817	litest_add_ranged(gestures_hold_tap_enabled, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &range_hold);
1818	litest_add_ranged(gestures_hold_cancel, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &range_hold);
1819	litest_add_ranged(gestures_hold_cancel_tap_enabled, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &range_hold);
1820	litest_add_ranged(gestures_hold_then_swipe_3fg, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1821	litest_add_ranged(gestures_hold_then_swipe_4fg, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1822	litest_add_ranged(gestures_hold_then_pinch_2fg, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1823	litest_add_ranged(gestures_hold_then_pinch_3fg, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1824	litest_add_ranged(gestures_hold_then_pinch_4fg, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1825	litest_add_ranged(gestures_hold_then_spread, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH, &cardinals);
1826	litest_add(gestures_hold_then_3fg_buttonarea_scroll, LITEST_CLICKPAD, LITEST_SINGLE_TOUCH);
1827
1828	litest_add(gestures_hold_once_on_double_tap, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1829	litest_add_ranged(gestures_hold_once_tap_n_drag, LITEST_TOUCHPAD, LITEST_ANY, &range_multifinger_tap);
1830
1831	litest_add(gestures_hold_and_motion_before_timeout, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1832	litest_add(gestures_hold_and_motion_after_timeout, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1833
1834	/* Timing-sensitive test, valgrind is too slow */
1835	if (!RUNNING_ON_VALGRIND)
1836		litest_add(gestures_swipe_3fg_unaccel, LITEST_TOUCHPAD, LITEST_SINGLE_TOUCH);
1837}
1838