summaryrefslogtreecommitdiff
path: root/test/test-touch.c
blob: 2bd1947e9d5797938f6df8537184fab0e08ab2f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
/*
 * Copyright © 2013 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <config.h>

#include <check.h>
#include <errno.h>
#include <fcntl.h>
#include <libinput.h>
#include <libevdev/libevdev.h>
#include <unistd.h>

#include "libinput-util.h"
#include "litest.h"

START_TEST(touch_frame_events)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
	int have_frame_event = 0;

	litest_drain_events(dev->libinput);

	litest_touch_down(dev, 0, 10, 10);
	libinput_dispatch(li);

	while ((event = libinput_get_event(li))) {
		if (libinput_event_get_type(event) == LIBINPUT_EVENT_TOUCH_FRAME)
			have_frame_event++;
		libinput_event_destroy(event);
	}
	ck_assert_int_eq(have_frame_event, 1);

	litest_touch_down(dev, 1, 10, 10);
	libinput_dispatch(li);

	while ((event = libinput_get_event(li))) {
		if (libinput_event_get_type(event) == LIBINPUT_EVENT_TOUCH_FRAME)
			have_frame_event++;
		libinput_event_destroy(event);
	}
	ck_assert_int_eq(have_frame_event, 2);
}
END_TEST

START_TEST(touch_abs_transform)
{
	struct litest_device *dev;
	struct libinput *libinput;
	struct libinput_event *ev;
	struct libinput_event_touch *tev;
	double fx, fy;
	bool tested = false;

	struct input_absinfo abs[] = {
		{ ABS_X, 0, 32767, 75, 0, 10 },
		{ ABS_Y, 0, 32767, 129, 0, 9 },
		{ ABS_MT_POSITION_X, 0, 32767, 0, 0, 10 },
		{ ABS_MT_POSITION_Y, 0, 32767, 0, 0, 9 },
		{ .value = -1 },
	};

	dev = litest_create_device_with_overrides(LITEST_WACOM_TOUCH,
						  "litest Highres touch device",
						  NULL, abs, NULL);

	libinput = dev->libinput;

	litest_touch_down(dev, 0, 100, 100);

	libinput_dispatch(libinput);

	while ((ev = libinput_get_event(libinput))) {
		if (libinput_event_get_type(ev) != LIBINPUT_EVENT_TOUCH_DOWN) {
			libinput_event_destroy(ev);
			continue;
		}

		tev = libinput_event_get_touch_event(ev);
		fx = libinput_event_touch_get_x_transformed(tev, 1920);
		ck_assert_int_eq(fx, 1919.0);
		fy = libinput_event_touch_get_y_transformed(tev, 720);
		ck_assert_int_eq(fy, 719.0);

		tested = true;

		libinput_event_destroy(ev);
	}

	ck_assert(tested);

	litest_delete_device(dev);
}
END_TEST

static inline void
touch_assert_seat_slot(struct libinput *li,
		       enum libinput_event_type type,
		       unsigned int slot,
		       unsigned int seat_slot)
{
	struct libinput_event *ev;
	struct libinput_event_touch *tev;

	libinput_dispatch(li);
	ev = libinput_get_event(li);
	tev = litest_is_touch_event(ev, type);
	slot = libinput_event_touch_get_slot(tev);
	ck_assert_int_eq(slot, slot);
	slot = libinput_event_touch_get_seat_slot(tev);
	ck_assert_int_eq(slot, seat_slot);
	libinput_event_destroy(ev);

	ev = libinput_get_event(li);
	litest_assert_event_type(ev, LIBINPUT_EVENT_TOUCH_FRAME);
	libinput_event_destroy(ev);
}

START_TEST(touch_seat_slot)
{
	struct litest_device *dev1 = litest_current_device();
	struct litest_device *dev2;
	struct libinput *li = dev1->libinput;

	dev2 = litest_add_device(li, LITEST_WACOM_TOUCH);

	litest_drain_events(li);

	litest_touch_down(dev1, 0, 50, 50);
	touch_assert_seat_slot(li, LIBINPUT_EVENT_TOUCH_DOWN, 0, 0);

	litest_touch_down(dev2, 0, 50, 50);
	touch_assert_seat_slot(li, LIBINPUT_EVENT_TOUCH_DOWN, 0, 1);

	litest_touch_down(dev2, 1, 60, 50);
	touch_assert_seat_slot(li, LIBINPUT_EVENT_TOUCH_DOWN, 1, 2);

	litest_touch_down(dev1, 1, 60, 50);
	touch_assert_seat_slot(li, LIBINPUT_EVENT_TOUCH_DOWN, 1, 3);

	litest_touch_move_to(dev1, 0, 50, 50, 60, 70, 10, 0);
	touch_assert_seat_slot(li, LIBINPUT_EVENT_TOUCH_MOTION, 0, 0);
	litest_drain_events(li);

	litest_touch_move_to(dev2, 1, 50, 50, 60, 70, 10, 0);
	touch_assert_seat_slot(li, LIBINPUT_EVENT_TOUCH_MOTION, 1, 2);
	litest_drain_events(li);

	litest_touch_up(dev1, 0);
	touch_assert_seat_slot(li, LIBINPUT_EVENT_TOUCH_UP, 0, 0);

	litest_touch_up(dev2, 0);
	touch_assert_seat_slot(li, LIBINPUT_EVENT_TOUCH_UP, 0, 1);

	litest_touch_up(dev2, 1);
	touch_assert_seat_slot(li, LIBINPUT_EVENT_TOUCH_UP, 1, 2);

	litest_touch_up(dev1, 1);
	touch_assert_seat_slot(li, LIBINPUT_EVENT_TOUCH_UP, 1, 3);

	litest_delete_device(dev2);
}
END_TEST

START_TEST(touch_many_slots)
{
	struct libinput *libinput;
	struct litest_device *dev;
	struct libinput_event *ev;
	int slot;
	const int num_tps = 100;
	int slot_count = 0;
	enum libinput_event_type type;

	struct input_absinfo abs[] = {
		{ ABS_MT_SLOT, 0, num_tps - 1, 0, 0, 0 },
		{ .value = -1 },
	};

	dev = litest_create_device_with_overrides(LITEST_WACOM_TOUCH,
						  "litest Multi-touch device",
						  NULL, abs, NULL);
	libinput = dev->libinput;

	for (slot = 0; slot < num_tps; ++slot)
		litest_touch_down(dev, slot, 0, 0);
	for (slot = 0; slot < num_tps; ++slot)
		litest_touch_up(dev, slot);

	libinput_dispatch(libinput);
	while ((ev = libinput_get_event(libinput))) {
		type = libinput_event_get_type(ev);

		if (type == LIBINPUT_EVENT_TOUCH_DOWN)
			slot_count++;
		else if (type == LIBINPUT_EVENT_TOUCH_UP)
			break;

		libinput_event_destroy(ev);
		libinput_dispatch(libinput);
	}

	ck_assert_notnull(ev);
	ck_assert_int_gt(slot_count, 0);

	libinput_dispatch(libinput);
	do {
		type = libinput_event_get_type(ev);
		ck_assert_int_ne(type, LIBINPUT_EVENT_TOUCH_DOWN);
		if (type == LIBINPUT_EVENT_TOUCH_UP)
			slot_count--;

		libinput_event_destroy(ev);
		libinput_dispatch(libinput);
	} while ((ev = libinput_get_event(libinput)));

	ck_assert_int_eq(slot_count, 0);

	litest_delete_device(dev);
}
END_TEST

START_TEST(touch_double_touch_down_up)
{
	struct libinput *libinput;
	struct litest_device *dev;
	struct libinput_event *ev;
	bool got_down = false;
	bool got_up = false;

	dev = litest_current_device();
	libinput = dev->libinput;

	/* note: this test is a false negative, libevdev will filter
	 * tracking IDs re-used in the same slot. */

	litest_touch_down(dev, 0, 0, 0);
	litest_touch_down(dev, 0, 0, 0);
	litest_touch_up(dev, 0);
	litest_touch_up(dev, 0);

	libinput_dispatch(libinput);

	while ((ev = libinput_get_event(libinput))) {
		switch (libinput_event_get_type(ev)) {
		case LIBINPUT_EVENT_TOUCH_DOWN:
			ck_assert(!got_down);
			got_down = true;
			break;
		case LIBINPUT_EVENT_TOUCH_UP:
			ck_assert(got_down);
			ck_assert(!got_up);
			got_up = true;
			break;
		default:
			break;
		}

		libinput_event_destroy(ev);
		libinput_dispatch(libinput);
	}

	ck_assert(got_down);
	ck_assert(got_up);
}
END_TEST

START_TEST(touch_calibration_scale)
{
	struct libinput *li;
	struct litest_device *dev;
	struct libinput_event *ev;
	struct libinput_event_touch *tev;
	float matrix[6] = {
		1, 0, 0,
		0, 1, 0
	};

	float calibration;
	double x, y;
	const int width = 640, height = 480;

	dev = litest_current_device();
	li = dev->libinput;

	for (calibration = 0.1; calibration < 1; calibration += 0.1) {
		libinput_device_config_calibration_set_matrix(dev->libinput_device,
							      matrix);
		litest_drain_events(li);

		litest_touch_down(dev, 0, 100, 100);
		litest_touch_up(dev, 0);

		litest_wait_for_event(li);
		ev = libinput_get_event(li);
		tev = litest_is_touch_event(ev, LIBINPUT_EVENT_TOUCH_DOWN);

		x = libinput_event_touch_get_x_transformed(tev, width);
		y = libinput_event_touch_get_y_transformed(tev, height);

		ck_assert_int_eq(round(x), round(width * matrix[0]));
		ck_assert_int_eq(round(y), round(height * matrix[4]));

		libinput_event_destroy(ev);
		litest_drain_events(li);

		matrix[0] = calibration;
		matrix[4] = 1 - calibration;
	}
}
END_TEST

START_TEST(touch_calibration_rotation)
{
	struct libinput *li;
	struct litest_device *dev;
	struct libinput_event *ev;
	struct libinput_event_touch *tev;
	float matrix[6];
	int i;
	double x, y;
	int width = 1024, height = 480;

	dev = litest_current_device();
	li = dev->libinput;

	for (i = 0; i < 4; i++) {
		float angle = i * M_PI/2;

		/* [ cos -sin  tx ]
		   [ sin  cos  ty ]
		   [  0    0   1  ] */
		matrix[0] = cos(angle);
		matrix[1] = -sin(angle);
		matrix[3] = sin(angle);
		matrix[4] = cos(angle);

		switch(i) {
		case 0: /* 0 deg */
			matrix[2] = 0;
			matrix[5] = 0;
			break;
		case 1: /* 90 deg cw */
			matrix[2] = 1;
			matrix[5] = 0;
			break;
		case 2: /* 180 deg cw */
			matrix[2] = 1;
			matrix[5] = 1;
			break;
		case 3: /* 270 deg cw */
			matrix[2] = 0;
			matrix[5] = 1;
			break;
		}

		libinput_device_config_calibration_set_matrix(dev->libinput_device,
							      matrix);
		litest_drain_events(li);

		litest_touch_down(dev, 0, 80, 20);
		litest_touch_up(dev, 0);
		litest_wait_for_event(li);
		ev = libinput_get_event(li);
		tev = litest_is_touch_event(ev, LIBINPUT_EVENT_TOUCH_DOWN);

		x = libinput_event_touch_get_x_transformed(tev, width);
		y = libinput_event_touch_get_y_transformed(tev, height);

		/* rounding errors... */
#define almost_equal(a_, b_) \
		{ ck_assert_int_ge((a_) + 0.5, (b_) - 1); \
		  ck_assert_int_le((a_) + 0.5, (b_) + 1); }
		switch(i) {
		case 0: /* 0 deg */
			almost_equal(x, width * 0.8);
			almost_equal(y, height * 0.2);
			break;
		case 1: /* 90 deg cw */
			almost_equal(x, width * 0.8);
			almost_equal(y, height * 0.8);
			break;
		case 2: /* 180 deg cw */
			almost_equal(x, width * 0.2);
			almost_equal(y, height * 0.8);
			break;
		case 3: /* 270 deg cw */
			almost_equal(x, width * 0.2);
			almost_equal(y, height * 0.2);
			break;
		}
#undef almost_equal

		libinput_event_destroy(ev);
		litest_drain_events(li);
	}
}
END_TEST

START_TEST(touch_calibration_translation)
{
	struct libinput *li;
	struct litest_device *dev;
	struct libinput_event *ev;
	struct libinput_event_touch *tev;
	float matrix[6] = {
		1, 0, 0,
		0, 1, 0
	};

	float translate;
	double x, y;
	const int width = 640, height = 480;

	dev = litest_current_device();
	li = dev->libinput;

	/* translating from 0 up to 1 device width/height */
	for (translate = 0.1; translate <= 1; translate += 0.1) {
		libinput_device_config_calibration_set_matrix(dev->libinput_device,
							      matrix);
		litest_drain_events(li);

		litest_touch_down(dev, 0, 100, 100);
		litest_touch_up(dev, 0);

		litest_wait_for_event(li);
		ev = libinput_get_event(li);
		tev = litest_is_touch_event(ev, LIBINPUT_EVENT_TOUCH_DOWN);

		x = libinput_event_touch_get_x_transformed(tev, width);
		y = libinput_event_touch_get_y_transformed(tev, height);

		/* sigh. rounding errors */
		ck_assert_int_ge(round(x), width + round(width * matrix[2]) - 1);
		ck_assert_int_ge(round(y), height + round(height * matrix[5]) - 1);
		ck_assert_int_le(round(x), width + round(width * matrix[2]) + 1);
		ck_assert_int_le(round(y), height + round(height * matrix[5]) + 1);

		libinput_event_destroy(ev);
		litest_drain_events(li);

		matrix[2] = translate;
		matrix[5] = 1 - translate;
	}
}
END_TEST

START_TEST(touch_calibrated_screen_path)
{
	struct litest_device *dev = litest_current_device();
	float matrix[6];
	int rc;

	rc = libinput_device_config_calibration_has_matrix(dev->libinput_device);
	ck_assert_int_eq(rc, 1);

	rc = libinput_device_config_calibration_get_matrix(dev->libinput_device,
							   matrix);
	ck_assert_int_eq(rc, 1);

	ck_assert_double_eq(matrix[0], 1.2);
	ck_assert_double_eq(matrix[1], 3.4);
	ck_assert_double_eq(matrix[2], 5.6);
	ck_assert_double_eq(matrix[3], 7.8);
	ck_assert_double_eq(matrix[4], 9.10);
	ck_assert_double_eq(matrix[5], 11.12);
}
END_TEST

static int open_restricted(const char *path, int flags, void *data)
{
	int fd;
	fd = open(path, flags);
	return fd < 0 ? -errno : fd;
}
static void close_restricted(int fd, void *data)
{
	close(fd);
}

static const struct libinput_interface simple_interface = {
	.open_restricted = open_restricted,
	.close_restricted = close_restricted,
};

START_TEST(touch_calibrated_screen_udev)
{
	struct libinput *li;
	struct libinput_event *ev;
	struct libinput_device *device = NULL;
	struct udev *udev;
	float matrix[6];
	int rc;

	udev = udev_new();
	ck_assert(udev != NULL);

	li = libinput_udev_create_context(&simple_interface, NULL, udev);
	ck_assert(li != NULL);
	ck_assert_int_eq(libinput_udev_assign_seat(li, "seat0"), 0);

	libinput_dispatch(li);

	while ((ev = libinput_get_event(li))) {
		struct libinput_device *d;

		if (libinput_event_get_type(ev) !=
		    LIBINPUT_EVENT_DEVICE_ADDED) {
			libinput_event_destroy(ev);
			continue;
		}

		d = libinput_event_get_device(ev);

		if (libinput_device_get_id_vendor(d) == 0x22 &&
		    libinput_device_get_id_product(d) == 0x33) {
			device = libinput_device_ref(d);
			litest_drain_events(li);
		}
		libinput_event_destroy(ev);
	}

	litest_drain_events(li);

	ck_assert_notnull(device);
	rc = libinput_device_config_calibration_has_matrix(device);
	ck_assert_int_eq(rc, 1);

	rc = libinput_device_config_calibration_get_matrix(device, matrix);
	ck_assert_int_eq(rc, 1);

	ck_assert_double_eq(matrix[0], 1.2);
	ck_assert_double_eq(matrix[1], 3.4);
	ck_assert_double_eq(matrix[2], 5.6);
	ck_assert_double_eq(matrix[3], 7.8);
	ck_assert_double_eq(matrix[4], 9.10);
	ck_assert_double_eq(matrix[5], 11.12);

	libinput_device_unref(device);

	libinput_unref(li);
	udev_unref(udev);

}
END_TEST

START_TEST(touch_no_left_handed)
{
	struct litest_device *dev = litest_current_device();
	struct libinput_device *d = dev->libinput_device;
	enum libinput_config_status status;
	int rc;

	rc = libinput_device_config_left_handed_is_available(d);
	ck_assert_int_eq(rc, 0);

	rc = libinput_device_config_left_handed_get(d);
	ck_assert_int_eq(rc, 0);

	rc = libinput_device_config_left_handed_get_default(d);
	ck_assert_int_eq(rc, 0);

	status = libinput_device_config_left_handed_set(d, 0);
	ck_assert_int_eq(status, LIBINPUT_CONFIG_STATUS_UNSUPPORTED);
}
END_TEST

START_TEST(fake_mt_exists)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
	struct libinput_device *device;

	litest_wait_for_event_of_type(li, LIBINPUT_EVENT_DEVICE_ADDED, -1);
	event = libinput_get_event(li);
	device = libinput_event_get_device(event);

	ck_assert(!libinput_device_has_capability(device,
						  LIBINPUT_DEVICE_CAP_TOUCH));

	/* This test may need fixing if we add other fake-mt devices that
	 * have different capabilities */
	ck_assert(libinput_device_has_capability(device,
						 LIBINPUT_DEVICE_CAP_POINTER));

	libinput_event_destroy(event);
}
END_TEST

START_TEST(fake_mt_no_touch_events)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;

	litest_drain_events(li);

	litest_touch_down(dev, 0, 50, 50);
	litest_touch_move_to(dev, 0, 50, 50, 70, 70, 5, 10);
	litest_touch_up(dev, 0);

	litest_touch_down(dev, 0, 50, 50);
	litest_touch_down(dev, 1, 70, 70);
	litest_touch_move_to(dev, 0, 50, 50, 90, 40, 10, 10);
	litest_touch_move_to(dev, 0, 70, 70, 40, 50, 10, 10);
	litest_touch_up(dev, 0);
	litest_touch_up(dev, 1);

	litest_assert_only_typed_events(li,
					LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE);
}
END_TEST

START_TEST(touch_protocol_a_init)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_device *device = dev->libinput_device;

	ck_assert_int_ne(libinput_next_event_type(li),
			 LIBINPUT_EVENT_NONE);

	ck_assert(libinput_device_has_capability(device,
						 LIBINPUT_DEVICE_CAP_TOUCH));
}
END_TEST

START_TEST(touch_protocol_a_touch)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *ev;
	struct libinput_event_touch *tev;
	double x, y, oldx, oldy;

	litest_drain_events(li);

	litest_touch_down(dev, 0, 5, 95);

	litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TOUCH_DOWN, -1);

	ev = libinput_get_event(li);
	tev = litest_is_touch_event(ev, LIBINPUT_EVENT_TOUCH_DOWN);

	oldx = libinput_event_touch_get_x(tev);
	oldy = libinput_event_touch_get_y(tev);

	libinput_event_destroy(ev);

	litest_touch_move_to(dev, 0, 10, 90, 90, 10, 20, 1);
	litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TOUCH_MOTION, -1);

	while ((ev = libinput_get_event(li))) {
		if (libinput_event_get_type(ev) ==
		    LIBINPUT_EVENT_TOUCH_FRAME) {
			libinput_event_destroy(ev);
			continue;
		}
		ck_assert_int_eq(libinput_event_get_type(ev),
				 LIBINPUT_EVENT_TOUCH_MOTION);

		tev = libinput_event_get_touch_event(ev);
		x = libinput_event_touch_get_x(tev);
		y = libinput_event_touch_get_y(tev);

		ck_assert_int_gt(x, oldx);
		ck_assert_int_lt(y, oldy);

		oldx = x;
		oldy = y;

		libinput_event_destroy(ev);
	}

	litest_touch_up(dev, 0);
	litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TOUCH_UP, -1);
}
END_TEST

START_TEST(touch_protocol_a_2fg_touch)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *ev;
	struct libinput_event_touch *tev;
	int pos;

	litest_drain_events(li);

	litest_push_event_frame(dev);
	litest_touch_down(dev, 0, 5, 95);
	litest_touch_down(dev, 0, 95, 5);
	litest_pop_event_frame(dev);

	litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TOUCH_DOWN, -1);

	ev = libinput_get_event(li);
	libinput_event_destroy(ev);

	litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TOUCH_DOWN, -1);

	ev = libinput_get_event(li);
	libinput_event_destroy(ev);

	for (pos = 10; pos < 100; pos += 10) {
		litest_push_event_frame(dev);
		litest_touch_move_to(dev, 0, pos, 100 - pos, pos, 100 - pos, 1, 1);
		litest_touch_move_to(dev, 0, 100 - pos, pos, 100 - pos, pos, 1, 1);
		litest_pop_event_frame(dev);
		litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TOUCH_MOTION, -1);
		ev = libinput_get_event(li);
		tev = libinput_event_get_touch_event(ev);
		ck_assert_int_eq(libinput_event_touch_get_slot(tev),
				0);
		libinput_event_destroy(ev);

		litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TOUCH_MOTION, -1);
		ev = libinput_get_event(li);
		tev = libinput_event_get_touch_event(ev);
		ck_assert_int_eq(libinput_event_touch_get_slot(tev),
				1);
		libinput_event_destroy(ev);
	}

	litest_event(dev, EV_SYN, SYN_MT_REPORT, 0);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TOUCH_UP, -1);
	litest_wait_for_event_of_type(li, LIBINPUT_EVENT_TOUCH_UP, -1);
}
END_TEST

START_TEST(touch_initial_state)
{
	struct litest_device *dev;
	struct libinput *libinput1, *libinput2;
	struct libinput_event *ev1 = NULL;
	struct libinput_event *ev2 = NULL;
	struct libinput_event_touch *t1, *t2;
	struct libinput_device *device1, *device2;
	int axis = _i; /* looped test */

	dev = litest_current_device();
	device1 = dev->libinput_device;
	libinput_device_config_tap_set_enabled(device1,
					       LIBINPUT_CONFIG_TAP_DISABLED);

	libinput1 = dev->libinput;
	litest_touch_down(dev, 0, 40, 60);
	litest_touch_up(dev, 0);

	/* device is now on some x/y value */
	litest_drain_events(libinput1);

	libinput2 = litest_create_context();
	device2 = libinput_path_add_device(libinput2,
					   libevdev_uinput_get_devnode(
							       dev->uinput));
	libinput_device_config_tap_set_enabled(device2,
					       LIBINPUT_CONFIG_TAP_DISABLED);
	litest_drain_events(libinput2);

	if (axis == ABS_X)
		litest_touch_down(dev, 0, 40, 70);
	else
		litest_touch_down(dev, 0, 70, 60);
	litest_touch_up(dev, 0);

	litest_wait_for_event(libinput1);
	litest_wait_for_event(libinput2);

	while (libinput_next_event_type(libinput1)) {
		ev1 = libinput_get_event(libinput1);
		ev2 = libinput_get_event(libinput2);

		t1 = litest_is_touch_event(ev1, 0);
		t2 = litest_is_touch_event(ev2, 0);

		ck_assert_int_eq(libinput_event_get_type(ev1),
				 libinput_event_get_type(ev2));

		if (libinput_event_get_type(ev1) == LIBINPUT_EVENT_TOUCH_UP ||
		    libinput_event_get_type(ev1) == LIBINPUT_EVENT_TOUCH_FRAME)
			break;

		ck_assert_int_eq(libinput_event_touch_get_x(t1),
				 libinput_event_touch_get_x(t2));
		ck_assert_int_eq(libinput_event_touch_get_y(t1),
				 libinput_event_touch_get_y(t2));

		libinput_event_destroy(ev1);
		libinput_event_destroy(ev2);
	}

	libinput_event_destroy(ev1);
	libinput_event_destroy(ev2);

	libinput_unref(libinput2);
}
END_TEST

START_TEST(touch_time_usec)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
	struct libinput_event_touch *tev;
	uint64_t time_usec;

	litest_drain_events(dev->libinput);

	litest_touch_down(dev, 0, 10, 10);

	litest_wait_for_event(li);

	event = libinput_get_event(li);
	tev = litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_DOWN);
	time_usec = libinput_event_touch_get_time_usec(tev);
	ck_assert_int_eq(libinput_event_touch_get_time(tev),
			 (uint32_t) (time_usec / 1000));
	libinput_event_destroy(event);
}
END_TEST

START_TEST(touch_fuzz)
{
	struct litest_device *dev = litest_current_device();
	struct libinput *li = dev->libinput;
	struct libinput_event *event;
	int i;
	int x = 700, y = 300;

	litest_drain_events(dev->libinput);

	litest_event(dev, EV_ABS, ABS_MT_TRACKING_ID, 30);
	litest_event(dev, EV_ABS, ABS_MT_SLOT, 0);
	litest_event(dev, EV_ABS, ABS_MT_POSITION_X, x);
	litest_event(dev, EV_ABS, ABS_MT_POSITION_Y, y);
	litest_event(dev, EV_KEY, BTN_TOUCH, 1);
	litest_event(dev, EV_SYN, SYN_REPORT, 0);
	libinput_dispatch(li);

	event = libinput_get_event(li);
	litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_DOWN);
	libinput_event_destroy(event);
	event = libinput_get_event(li);
	litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
	libinput_event_destroy(event);

	litest_drain_events(li);

	for (i = 0; i < 50; i++) {
		if (i % 2) {
			x++;
			y--;
		} else {
			x--;
			y++;
		}
		litest_event(dev, EV_ABS, ABS_MT_POSITION_X, x);
		litest_event(dev, EV_ABS, ABS_MT_POSITION_Y, y);
		litest_event(dev, EV_SYN, SYN_REPORT, 0);
		libinput_dispatch(li);
		litest_assert_empty_queue(li);
	}
}
END_TEST

void
litest_setup_tests_touch(void)
{
	struct range axes = { ABS_X, ABS_Y + 1};

	litest_add("touch:frame", touch_frame_events, LITEST_TOUCH, LITEST_ANY);
	litest_add_no_device("touch:abs-transform", touch_abs_transform);
	litest_add("touch:slots", touch_seat_slot, LITEST_TOUCH, LITEST_TOUCHPAD);
	litest_add_no_device("touch:slots", touch_many_slots);
	litest_add("touch:double-touch-down-up", touch_double_touch_down_up, LITEST_TOUCH, LITEST_ANY);
	litest_add("touch:calibration", touch_calibration_scale, LITEST_TOUCH, LITEST_TOUCHPAD);
	litest_add("touch:calibration", touch_calibration_scale, LITEST_SINGLE_TOUCH, LITEST_TOUCHPAD);
	litest_add("touch:calibration", touch_calibration_rotation, LITEST_TOUCH, LITEST_TOUCHPAD);
	litest_add("touch:calibration", touch_calibration_rotation, LITEST_SINGLE_TOUCH, LITEST_TOUCHPAD);
	litest_add("touch:calibration", touch_calibration_translation, LITEST_TOUCH, LITEST_TOUCHPAD);
	litest_add("touch:calibration", touch_calibration_translation, LITEST_SINGLE_TOUCH, LITEST_TOUCHPAD);
	litest_add_for_device("touch:calibration", touch_calibrated_screen_path, LITEST_CALIBRATED_TOUCHSCREEN);
	litest_add_for_device("touch:calibration", touch_calibrated_screen_udev, LITEST_CALIBRATED_TOUCHSCREEN);

	litest_add("touch:left-handed", touch_no_left_handed, LITEST_TOUCH, LITEST_ANY);

	litest_add("touch:fake-mt", fake_mt_exists, LITEST_FAKE_MT, LITEST_ANY);
	litest_add("touch:fake-mt", fake_mt_no_touch_events, LITEST_FAKE_MT, LITEST_ANY);

	litest_add("touch:protocol a", touch_protocol_a_init, LITEST_PROTOCOL_A, LITEST_ANY);
	litest_add("touch:protocol a", touch_protocol_a_touch, LITEST_PROTOCOL_A, LITEST_ANY);
	litest_add("touch:protocol a", touch_protocol_a_2fg_touch, LITEST_PROTOCOL_A, LITEST_ANY);

	litest_add_ranged("touch:state", touch_initial_state, LITEST_TOUCH, LITEST_PROTOCOL_A, &axes);

	litest_add("touch:time", touch_time_usec, LITEST_TOUCH, LITEST_TOUCHPAD);

	litest_add_for_device("touch:fuzz", touch_fuzz, LITEST_MULTITOUCH_FUZZ_SCREEN);
}