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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
|
/*
* Copyright © 2013 Red Hat, Inc.
* Copyright © 2013 Marcin Slusarz <marcin.slusarz@gmail.com>
*
* 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 <dirent.h>
#include <errno.h>
#include <libgen.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <getopt.h>
#include <poll.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include "linux/input.h"
#include <sys/ptrace.h>
#include <sys/resource.h>
#include <sys/timerfd.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <libudev.h>
#if HAVE_LIBSYSTEMD
#include <systemd/sd-bus.h>
#endif
#ifdef __FreeBSD__
#include <termios.h>
#endif
#include <valgrind/valgrind.h>
#include "litest.h"
#include "litest-int.h"
#include "libinput-util.h"
#include "quirks.h"
#include "builddir.h"
#include <linux/kd.h>
#define evbit(t, c) ((t) << 16U | (c & 0xffff))
#define UDEV_RULES_D "/run/udev/rules.d"
#define UDEV_FUZZ_OVERRIDE_RULE_FILE UDEV_RULES_D \
"/91-litest-fuzz-override-REMOVEME-XXXXXX.rules"
#define UDEV_TEST_DEVICE_RULE_FILE UDEV_RULES_D \
"/91-litest-test-device-REMOVEME-XXXXXXX.rules"
#define UDEV_DEVICE_GROUPS_FILE UDEV_RULES_D \
"/80-libinput-device-groups-litest-XXXXXX.rules"
static int jobs;
static bool in_debugger = false;
static bool verbose = false;
static bool run_deviceless = false;
static bool use_system_rules_quirks = false;
const char *filter_test = NULL;
const char *filter_device = NULL;
const char *filter_group = NULL;
const char *xml_prefix = NULL;
static struct quirks_context *quirks_context;
struct created_file {
struct list link;
char *path;
};
struct list created_files_list; /* list of all files to remove at the end of
the test run */
static void litest_init_udev_rules(struct list *created_files_list);
static void litest_remove_udev_rules(struct list *created_files_list);
enum quirks_setup_mode {
QUIRKS_SETUP_USE_SRCDIR,
QUIRKS_SETUP_ONLY_DEVICE,
QUIRKS_SETUP_FULL,
};
static void litest_setup_quirks(struct list *created_files_list,
enum quirks_setup_mode mode);
/* defined for the litest selftest */
#ifndef LITEST_DISABLE_BACKTRACE_LOGGING
#define litest_log(...) fprintf(stderr, __VA_ARGS__)
#define litest_vlog(format_, args_) vfprintf(stderr, format_, args_)
#else
#define litest_log(...) { /* __VA_ARGS__ */ }
#define litest_vlog(...) { /* __VA_ARGS__ */ }
#endif
static void
litest_backtrace(void)
{
#if HAVE_GSTACK
pid_t parent, child;
int pipefd[2];
if (RUNNING_ON_VALGRIND) {
litest_log(" Using valgrind, omitting backtrace\n");
return;
}
if (pipe(pipefd) == -1)
return;
parent = getpid();
child = fork();
if (child == 0) {
char pid[8];
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
sprintf(pid, "%d", parent);
execlp("gstack", "gstack", pid, NULL);
exit(errno);
}
/* parent */
char buf[1024];
int status, nread;
close(pipefd[1]);
waitpid(child, &status, 0);
status = WEXITSTATUS(status);
if (status != 0) {
litest_log("ERROR: gstack failed, no backtrace available: %s\n",
strerror(status));
} else {
litest_log("\nBacktrace:\n");
while ((nread = read(pipefd[0], buf, sizeof(buf) - 1)) > 0) {
buf[nread] = '\0';
litest_log("%s", buf);
}
litest_log("\n");
}
close(pipefd[0]);
#endif
}
LIBINPUT_ATTRIBUTE_PRINTF(5, 6)
__attribute__((noreturn))
void
litest_fail_condition(const char *file,
int line,
const char *func,
const char *condition,
const char *message,
...)
{
litest_log("FAILED: %s\n", condition);
if (message) {
va_list args;
va_start(args, message);
litest_vlog(message, args);
va_end(args);
}
litest_log("in %s() (%s:%d)\n", func, file, line);
litest_backtrace();
abort();
}
__attribute__((noreturn))
void
litest_fail_comparison_int(const char *file,
int line,
const char *func,
const char *operator,
int a,
int b,
const char *astr,
const char *bstr)
{
litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr);
litest_log("Resolved to: %d %s %d\n", a, operator, b);
litest_log("in %s() (%s:%d)\n", func, file, line);
litest_backtrace();
abort();
}
__attribute__((noreturn))
void
litest_fail_comparison_double(const char *file,
int line,
const char *func,
const char *operator,
double a,
double b,
const char *astr,
const char *bstr)
{
litest_log("FAILED COMPARISON: %s %s %s\n", astr, operator, bstr);
litest_log("Resolved to: %.3f %s %.3f\n", a, operator, b);
litest_log("in %s() (%s:%d)\n", func, file, line);
litest_backtrace();
abort();
}
__attribute__((noreturn))
void
litest_fail_comparison_ptr(const char *file,
int line,
const char *func,
const char *comparison)
{
litest_log("FAILED COMPARISON: %s\n", comparison);
litest_log("in %s() (%s:%d)\n", func, file, line);
litest_backtrace();
abort();
}
struct test {
struct list node;
char *name;
char *devname;
const void *func;
void *setup;
void *teardown;
struct range range;
bool deviceless;
};
struct suite {
struct list node;
struct list tests;
char *name;
};
static struct litest_device *current_device;
struct litest_device *litest_current_device(void)
{
return current_device;
}
static void
grab_device(struct litest_device *device, bool mode)
{
struct libinput *li = libinput_device_get_context(device->libinput_device);
struct litest_context *ctx = libinput_get_user_data(li);
struct udev_device *udev_device;
const char *devnode;
struct path *p;
udev_device = libinput_device_get_udev_device(device->libinput_device);
litest_assert_ptr_notnull(udev_device);
devnode = udev_device_get_devnode(udev_device);
/* Note: in some tests we create multiple devices for the same path.
* This will only grab the first device in the list but we're using
* list_insert() so the first device is the latest that was
* initialized, so we should be good.
*/
list_for_each(p, &ctx->paths, link) {
if (streq(p->path, devnode)) {
int rc = ioctl(p->fd, EVIOCGRAB, (void*)mode ? 1 : 0);
ck_assert_int_gt(rc, -1);
udev_device_unref(udev_device);
return;
}
}
litest_abort_msg("Failed to find device %s to %sgrab\n",
devnode, mode ? "" : "un");
}
void
litest_grab_device(struct litest_device *device)
{
grab_device(device, true);
}
void
litest_ungrab_device(struct litest_device *device)
{
grab_device(device, false);
}
void litest_set_current_device(struct litest_device *device)
{
current_device = device;
}
void litest_generic_device_teardown(void)
{
litest_delete_device(current_device);
current_device = NULL;
}
struct list devices;
static struct list all_tests;
static inline void
litest_system(const char *command)
{
int ret;
ret = system(command);
if (ret == -1) {
litest_abort_msg("Failed to execute: %s", command);
} else if (WIFEXITED(ret)) {
if (WEXITSTATUS(ret))
litest_abort_msg("'%s' failed with %d",
command,
WEXITSTATUS(ret));
} else if (WIFSIGNALED(ret)) {
litest_abort_msg("'%s' terminated with signal %d",
command,
WTERMSIG(ret));
}
}
static void
litest_reload_udev_rules(void)
{
litest_system("udevadm control --reload-rules");
}
static void
litest_add_tcase_for_device(struct suite *suite,
const char *funcname,
const void *func,
const struct litest_test_device *dev,
const struct range *range)
{
struct test *t;
t = zalloc(sizeof(*t));
t->name = safe_strdup(funcname);
t->devname = safe_strdup(dev->shortname);
t->func = func;
t->setup = dev->setup;
t->teardown = dev->teardown ?
dev->teardown : litest_generic_device_teardown;
if (range)
t->range = *range;
list_insert(&suite->tests, &t->node);
}
static void
litest_add_tcase_no_device(struct suite *suite,
const void *func,
const char *funcname,
const struct range *range)
{
struct test *t;
const char *test_name = funcname;
if (filter_device &&
strstr(test_name, filter_device) == NULL &&
fnmatch(filter_device, test_name, 0) != 0)
return;
t = zalloc(sizeof(*t));
t->name = safe_strdup(test_name);
t->devname = safe_strdup("no device");
t->func = func;
if (range)
t->range = *range;
t->setup = NULL;
t->teardown = NULL;
list_insert(&suite->tests, &t->node);
}
static void
litest_add_tcase_deviceless(struct suite *suite,
const void *func,
const char *funcname,
const struct range *range)
{
struct test *t;
const char *test_name = funcname;
if (filter_device &&
strstr(test_name, filter_device) == NULL &&
fnmatch(filter_device, test_name, 0) != 0)
return;
t = zalloc(sizeof(*t));
t->deviceless = true;
t->name = safe_strdup(test_name);
t->devname = safe_strdup("deviceless");
t->func = func;
if (range)
t->range = *range;
t->setup = NULL;
t->teardown = NULL;
list_insert(&suite->tests, &t->node);
}
static struct suite *
get_suite(const char *name)
{
struct suite *s;
list_for_each(s, &all_tests, node) {
if (streq(s->name, name))
return s;
}
s = zalloc(sizeof(*s));
s->name = safe_strdup(name);
list_init(&s->tests);
list_insert(&all_tests, &s->node);
return s;
}
static void
create_suite_name(const char *filename, char suitename[64])
{
char *trunk = trunkname(filename);
char *p = trunk;
/* strip the test- prefix */
if (strstartswith(trunk, "test-"))
p += 5;
snprintf(suitename, 64, "%s", p);
free(trunk);
}
static void
litest_add_tcase(const char *filename,
const char *funcname,
const void *func,
int64_t required,
int64_t excluded,
const struct range *range)
{
char suite_name[65];
struct suite *suite;
bool added = false;
litest_assert(required >= LITEST_DEVICELESS);
litest_assert(excluded >= LITEST_DEVICELESS);
if (filter_test &&
strstr(funcname, filter_test) == NULL &&
fnmatch(filter_test, funcname, 0) != 0)
return;
create_suite_name(filename, suite_name);
if (filter_group && fnmatch(filter_group, suite_name, 0) != 0)
return;
suite = get_suite(suite_name);
if (required == LITEST_DEVICELESS &&
excluded == LITEST_DEVICELESS) {
litest_add_tcase_deviceless(suite, func, funcname, range);
added = true;
} else if (required == LITEST_DISABLE_DEVICE &&
excluded == LITEST_DISABLE_DEVICE) {
litest_add_tcase_no_device(suite, func, funcname, range);
added = true;
} else if (required != LITEST_ANY || excluded != LITEST_ANY) {
struct litest_test_device *dev;
list_for_each(dev, &devices, node) {
if (dev->features & LITEST_IGNORED)
continue;
if (filter_device &&
strstr(dev->shortname, filter_device) == NULL &&
fnmatch(filter_device, dev->shortname, 0) != 0)
continue;
if ((dev->features & required) != required ||
(dev->features & excluded) != 0)
continue;
litest_add_tcase_for_device(suite,
funcname,
func,
dev,
range);
added = true;
}
} else {
struct litest_test_device *dev;
list_for_each(dev, &devices, node) {
if (dev->features & LITEST_IGNORED)
continue;
if (filter_device &&
strstr(dev->shortname, filter_device) == NULL &&
fnmatch(filter_device, dev->shortname, 0) != 0)
continue;
litest_add_tcase_for_device(suite,
funcname,
func,
dev,
range);
added = true;
}
}
if (!added &&
filter_test == NULL &&
filter_device == NULL &&
filter_group == NULL) {
fprintf(stderr, "Test '%s' does not match any devices. Aborting.\n", funcname);
abort();
}
}
void
_litest_add_no_device(const char *name, const char *funcname, const void *func)
{
_litest_add(name, funcname, func, LITEST_DISABLE_DEVICE, LITEST_DISABLE_DEVICE);
}
void
_litest_add_ranged_no_device(const char *name,
const char *funcname,
const void *func,
const struct range *range)
{
_litest_add_ranged(name,
funcname,
func,
LITEST_DISABLE_DEVICE,
LITEST_DISABLE_DEVICE,
range);
}
void
_litest_add_deviceless(const char *name,
const char *funcname,
const void *func)
{
_litest_add_ranged(name,
funcname,
func,
LITEST_DEVICELESS,
LITEST_DEVICELESS,
NULL);
}
void
_litest_add(const char *name,
const char *funcname,
const void *func,
int64_t required,
int64_t excluded)
{
_litest_add_ranged(name,
funcname,
func,
required,
excluded,
NULL);
}
void
_litest_add_ranged(const char *name,
const char *funcname,
const void *func,
int64_t required,
int64_t excluded,
const struct range *range)
{
litest_add_tcase(name, funcname, func, required, excluded, range);
}
void
_litest_add_for_device(const char *name,
const char *funcname,
const void *func,
enum litest_device_type type)
{
_litest_add_ranged_for_device(name, funcname, func, type, NULL);
}
void
_litest_add_ranged_for_device(const char *filename,
const char *funcname,
const void *func,
enum litest_device_type type,
const struct range *range)
{
struct suite *s;
struct litest_test_device *dev;
bool device_filtered = false;
char suite_name[64];
litest_assert(type < LITEST_NO_DEVICE);
if (filter_test &&
strstr(funcname, filter_test) == NULL &&
fnmatch(filter_test, funcname, 0) != 0)
return;
create_suite_name(filename, suite_name);
if (filter_group && fnmatch(filter_group, suite_name, 0) != 0)
return;
s = get_suite(suite_name);
list_for_each(dev, &devices, node) {
if (filter_device &&
strstr(dev->shortname, filter_device) == NULL &&
fnmatch(filter_device, dev->shortname, 0) != 0) {
device_filtered = true;
continue;
}
if (dev->type == type) {
litest_add_tcase_for_device(s,
funcname,
func,
dev,
range);
return;
}
}
/* only abort if no filter was set, that's a bug */
if (!device_filtered)
litest_abort_msg("Invalid test device type\n");
}
LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
static void
litest_log_handler(struct libinput *libinput,
enum libinput_log_priority pri,
const char *format,
va_list args)
{
static int is_tty = -1;
const char *priority = NULL;
const char *color;
if (is_tty == -1)
is_tty = isatty(STDERR_FILENO);
switch(pri) {
case LIBINPUT_LOG_PRIORITY_INFO:
priority = "info ";
color = ANSI_HIGHLIGHT;
break;
case LIBINPUT_LOG_PRIORITY_ERROR:
priority = "error";
color = ANSI_BRIGHT_RED;
break;
case LIBINPUT_LOG_PRIORITY_DEBUG:
priority = "debug";
color = ANSI_NORMAL;
break;
default:
abort();
}
if (!is_tty)
color = "";
else if (strstr(format, "tap:"))
color = ANSI_BLUE;
else if (strstr(format, "thumb state:"))
color = ANSI_YELLOW;
else if (strstr(format, "button state:"))
color = ANSI_MAGENTA;
else if (strstr(format, "touch-size:") ||
strstr(format, "pressure:"))
color = ANSI_GREEN;
else if (strstr(format, "palm:") ||
strstr(format, "thumb:"))
color = ANSI_CYAN;
else if (strstr(format, "edge-scroll:"))
color = ANSI_BRIGHT_GREEN;
fprintf(stderr, "%slitest %s ", color, priority);
vfprintf(stderr, format, args);
if (is_tty)
fprintf(stderr, ANSI_NORMAL);
if (strstr(format, "client bug: ") ||
strstr(format, "libinput bug: ")) {
/* valgrind is too slow and some of our offsets are too
* short, don't abort if during a valgrind run we get a
* negative offset */
if ((RUNNING_ON_VALGRIND && in_debugger) ||
!strstr(format, "scheduled expiry is in the past")) {
/* noop */
} else if (!strstr(format, "event processing lagging behind")) {
/* noop */
} else {
litest_abort_msg("libinput bug triggered, aborting.\n");
}
}
if (strstr(format, "Touch jump detected and discarded")) {
litest_abort_msg("libinput touch jump triggered, aborting.\n");
}
}
static void
litest_init_device_udev_rules(struct litest_test_device *dev, FILE *f)
{
const struct key_value_str *kv;
static int count;
bool need_keyboard_builtin = false;
if (dev->udev_properties[0].key == NULL)
return;
count++;
fprintf(f, "# %s\n", dev->shortname);
fprintf(f, "ACTION==\"remove\", GOTO=\"rule%d_end\"\n", count);
fprintf(f, "KERNEL!=\"event*\", GOTO=\"rule%d_end\"\n", count);
fprintf(f, "ATTRS{name}==\"litest %s*\"", dev->name);
kv = dev->udev_properties;
while (kv->key) {
fprintf(f, ", \\\n\tENV{%s}=\"%s\"", kv->key, kv->value);
if (strneq(kv->key, "EVDEV_ABS_", 10))
need_keyboard_builtin = true;
kv++;
}
fprintf(f, "\n");
/* Special case: the udev keyboard builtin is only run for hwdb
* matches but we don't set any up in litest. So instead scan the
* device's udev properties for any EVDEV_ABS properties and where
* they exist, force a (re-)run of the keyboard builtin to set up
* the evdev device correctly.
* This needs to be done as separate rule apparently, otherwise the
* ENV variables aren't set yet by the time the builtin runs.
*/
if (need_keyboard_builtin) {
fprintf(f, ""
"ATTRS{name}==\"litest %s*\","
" IMPORT{builtin}=\"keyboard\"\n",
dev->name);
}
fprintf(f, "LABEL=\"rule%d_end\"\n\n", count);;
}
static void
litest_init_all_device_udev_rules(struct list *created_files)
{
struct created_file *file = zalloc(sizeof(*file));
struct litest_test_device *dev;
char *path = NULL;
FILE *f;
int rc;
int fd;
rc = xasprintf(&path,
"%s/99-litest-XXXXXX.rules",
UDEV_RULES_D);
litest_assert_int_gt(rc, 0);
fd = mkstemps(path, 6);
litest_assert_int_ne(fd, -1);
f = fdopen(fd, "w");
litest_assert_notnull(f);
list_for_each(dev, &devices, node)
litest_init_device_udev_rules(dev, f);
fclose(f);
file->path = path;
list_insert(created_files, &file->link);
}
static int
open_restricted(const char *path, int flags, void *userdata)
{
const char prefix[] = "/dev/input/event";
struct litest_context *ctx = userdata;
struct path *p;
int fd;
litest_assert_ptr_notnull(ctx);
fd = open(path, flags);
if (fd < 0)
return -errno;
if (strneq(path, prefix, strlen(prefix))) {
p = zalloc(sizeof *p);
p->path = safe_strdup(path);
p->fd = fd;
/* We specifically insert here so that the most-recently
* opened path is the first one in the list. This helps when
* we have multiple test devices with the same device path,
* the fd of the most recent device is the first one to get
* grabbed
*/
list_insert(&ctx->paths, &p->link);
}
return fd;
}
static void
close_restricted(int fd, void *userdata)
{
struct litest_context *ctx = userdata;
struct path *p;
list_for_each_safe(p, &ctx->paths, link) {
if (p->fd != fd)
continue;
list_remove(&p->link);
free(p->path);
free(p);
}
close(fd);
}
struct libinput_interface interface = {
.open_restricted = open_restricted,
.close_restricted = close_restricted,
};
static void
litest_signal(int sig)
{
struct created_file *f;
list_for_each_safe(f, &created_files_list, link) {
list_remove(&f->link);
unlink(f->path);
rmdir(f->path);
/* in the sighandler, we can't free */
}
if (fork() == 0) {
/* child, we can run system() */
litest_reload_udev_rules();
exit(0);
}
exit(1);
}
static inline void
litest_setup_sighandler(int sig)
{
struct sigaction act, oact;
int rc;
sigemptyset(&act.sa_mask);
sigaddset(&act.sa_mask, sig);
act.sa_flags = 0;
act.sa_handler = litest_signal;
rc = sigaction(sig, &act, &oact);
litest_assert_int_ne(rc, -1);
}
static void
litest_free_test_list(struct list *tests)
{
struct suite *s;
list_for_each_safe(s, tests, node) {
struct test *t;
list_for_each_safe(t, &s->tests, node) {
free(t->name);
free(t->devname);
list_remove(&t->node);
free(t);
}
list_remove(&s->node);
free(s->name);
free(s);
}
}
LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
static inline void
quirk_log_handler(struct libinput *unused,
enum libinput_log_priority priority,
const char *format,
va_list args)
{
if (priority < LIBINPUT_LOG_PRIORITY_ERROR)
return;
vfprintf(stderr, format, args);
}
static void
litest_export_xml(SRunner *sr, const char *xml_prefix)
{
TestResult **results;
int nresults, nfailed;
char *filename;
int fd;
/* This is the minimum-effort implementation here because its only
* real purpose is to make test logs look pretty in the gitlab CI.
*
* Which means:
* - there's no filename validation, if you supply a filename that
* mkstemps doesn't like, things go boom.
* - every fork writes out a separate junit.xml file. gitlab is better
* at collecting lots of files than I am at writing code to collect
* this across forks to write out only one file.
* - most of the content is pretty useless because libcheck only gives
* us minimal information. the libcheck XML file has more info like
* the duration of each test but it's more complicated to extract
* and we don't need it for now.
*/
filename = safe_strdup(xml_prefix);
fd = mkstemps(filename, 4);
results = srunner_results(sr);
nresults = srunner_ntests_run(sr);
nfailed = srunner_ntests_failed(sr);
dprintf(fd, "<?xml version=\"1.0\"?>\n");
dprintf(fd, "<testsuites id=\"%s\" tests=\"%d\" failures=\"%d\">\n",
filename,
nresults,
nfailed);
dprintf(fd, " <testsuite>\n");
for (int i = 0; i < nresults; i++) {
TestResult *r = results[i];
dprintf(fd, " <testcase id=\"%s\" name=\"%s\" %s>\n",
tr_tcname(r),
tr_tcname(r),
tr_rtype(r) == CK_PASS ? "/" : "");
if (tr_rtype(r) != CK_PASS) {
dprintf(fd, " <failure message=\"%s:%d\">\n",
tr_lfile(r),
tr_lno(r));
dprintf(fd, " %s:%d\n", tr_lfile(r), tr_lno(r));
dprintf(fd, " %s\n", tr_tcname(r));
dprintf(fd, "\n");
dprintf(fd, " %s\n", tr_msg(r));
dprintf(fd, " </failure>\n");
dprintf(fd, " </testcase>\n");
}
}
dprintf(fd, " </testsuite>\n");
dprintf(fd, "</testsuites>\n");
free(results);
close(fd);
free(filename);
}
static int
litest_run_suite(struct list *tests, int which, int max, int error_fd)
{
int failed = 0;
SRunner *sr = NULL;
struct suite *s;
struct test *t;
int count = -1;
struct name {
struct list node;
char *name;
};
struct name *n;
struct list testnames;
const char *data_path;
data_path = getenv("LIBINPUT_QUIRKS_DIR");
if (!data_path)
data_path = LIBINPUT_QUIRKS_DIR;
quirks_context = quirks_init_subsystem(data_path,
NULL,
quirk_log_handler,
NULL,
QLOG_LIBINPUT_LOGGING);
/* Check just takes the suite/test name pointers but doesn't strdup
* them - we have to keep them around */
list_init(&testnames);
/* For each test, create one test suite with one test case, then
add it to the test runner. The only benefit suites give us in
check is that we can filter them, but our test runner has a
--filter-group anyway. */
list_for_each(s, tests, node) {
list_for_each(t, &s->tests, node) {
Suite *suite;
TCase *tc;
char *sname, *tname;
/* We run deviceless tests as part of the normal
* test suite runner, just in case. Filtering
* all the other ones out just for the case where
* we can't run the full runner.
*/
if (run_deviceless && !t->deviceless)
continue;
count = (count + 1) % max;
if (max != 1 && (count % max) != which)
continue;
xasprintf(&sname,
"%s:%s:%s",
s->name,
t->name,
t->devname);
litest_assert_ptr_notnull(sname);
n = zalloc(sizeof(*n));
n->name = sname;
list_insert(&testnames, &n->node);
xasprintf(&tname,
"%s:%s",
t->name,
t->devname);
litest_assert_ptr_notnull(tname);
n = zalloc(sizeof(*n));
n->name = tname;
list_insert(&testnames, &n->node);
tc = tcase_create(tname);
tcase_add_checked_fixture(tc,
t->setup,
t->teardown);
if (t->range.upper != t->range.lower)
tcase_add_loop_test(tc,
t->func,
t->range.lower,
t->range.upper);
else
tcase_add_test(tc, t->func);
suite = suite_create(sname);
suite_add_tcase(suite, tc);
if (!sr)
sr = srunner_create(suite);
else
srunner_add_suite(sr, suite);
}
}
if (!sr)
goto out;
srunner_run_all(sr, CK_ENV);
if (xml_prefix)
litest_export_xml(sr, xml_prefix);
failed = srunner_ntests_failed(sr);
if (failed) {
TestResult **trs;
trs = srunner_failures(sr);
for (int i = 0; i < failed; i++) {
char tname[256];
char *c = tname;
/* tr_tcname is in the form "suite:testcase", let's
* convert this to "suite(testcase)" to make
* double-click selection in the terminal a bit
* easier. */
snprintf(tname, sizeof(tname), "%s)", tr_tcname(trs[i]));
if ((c = index(c, ':')))
*c = '(';
dprintf(error_fd,
":: Failure: %s:%d: %s\n",
tr_lfile(trs[i]),
tr_lno(trs[i]),
tname);
}
free(trs);
}
srunner_free(sr);
out:
list_for_each_safe(n, &testnames, node) {
free(n->name);
free(n);
}
quirks_context_unref(quirks_context);
return failed;
}
static int
litest_fork_subtests(struct list *tests, int max_forks)
{
int failed = 0;
int status;
pid_t pid;
int f;
int pipes[max_forks];
for (f = 0; f < max_forks; f++) {
int rc;
int pipefd[2];
rc = pipe2(pipefd, O_NONBLOCK);
assert(rc != -1);
pid = fork();
if (pid == 0) {
close(pipefd[0]);
failed = litest_run_suite(tests,
f,
max_forks,
pipefd[1]);
litest_free_test_list(&all_tests);
exit(failed);
/* child always exits here */
} else {
pipes[f] = pipefd[0];
close(pipefd[1]);
}
}
/* parent process only */
while (wait(&status) != -1 && errno != ECHILD) {
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
failed = 1;
}
for (f = 0; f < max_forks; f++) {
char buf[1024] = {0};
int rc;
while ((rc = read(pipes[f], buf, sizeof(buf) - 1)) > 0) {
buf[rc] = '\0';
fprintf(stderr, "%s", buf);
}
close(pipes[f]);
}
return failed;
}
static inline int
inhibit(void)
{
int lock_fd = -1;
#if HAVE_LIBSYSTEMD
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *m = NULL;
sd_bus *bus = NULL;
int rc;
if (run_deviceless)
return -1;
rc = sd_bus_open_system(&bus);
if (rc != 0) {
fprintf(stderr, "Warning: inhibit failed: %s\n", strerror(-rc));
goto out;
}
rc = sd_bus_call_method(bus,
"org.freedesktop.login1",
"/org/freedesktop/login1",
"org.freedesktop.login1.Manager",
"Inhibit",
&error,
&m,
"ssss",
"sleep:shutdown:handle-lid-switch:handle-power-key:handle-suspend-key:handle-hibernate-key",
"libinput test-suite runner",
"testing in progress",
"block");
if (rc < 0) {
fprintf(stderr, "Warning: inhibit failed: %s\n", error.message);
goto out;
}
rc = sd_bus_message_read(m, "h", &lock_fd);
if (rc < 0) {
fprintf(stderr, "Warning: inhibit failed: %s\n", strerror(-rc));
goto out;
}
lock_fd = dup(lock_fd);
out:
sd_bus_error_free(&error);
sd_bus_message_unref(m);
sd_bus_close(bus);
sd_bus_unref(bus);
#endif
return lock_fd;
}
static inline int
litest_run(int argc, char **argv)
{
int failed = 0;
int inhibit_lock_fd;
list_init(&created_files_list);
if (list_empty(&all_tests)) {
fprintf(stderr,
"Error: filters are too strict, no tests to run.\n");
return 1;
}
if (getenv("LITEST_VERBOSE"))
verbose = true;
if (run_deviceless) {
litest_setup_quirks(&created_files_list,
QUIRKS_SETUP_USE_SRCDIR);
} else {
enum quirks_setup_mode mode;
litest_init_udev_rules(&created_files_list);
mode = use_system_rules_quirks ?
QUIRKS_SETUP_ONLY_DEVICE :
QUIRKS_SETUP_FULL;
litest_setup_quirks(&created_files_list, mode);
}
litest_setup_sighandler(SIGINT);
inhibit_lock_fd = inhibit();
if (jobs == 1)
failed = litest_run_suite(&all_tests, 1, 1, STDERR_FILENO);
else
failed = litest_fork_subtests(&all_tests, jobs);
close(inhibit_lock_fd);
litest_free_test_list(&all_tests);
litest_remove_udev_rules(&created_files_list);
return failed;
}
static struct input_absinfo *
merge_absinfo(const struct input_absinfo *orig,
const struct input_absinfo *override)
{
struct input_absinfo *abs;
unsigned int nelem, i;
size_t sz = ABS_MAX + 1;
if (!orig)
return NULL;
abs = zalloc(sz * sizeof(*abs));
litest_assert_ptr_notnull(abs);
nelem = 0;
while (orig[nelem].value != -1) {
abs[nelem] = orig[nelem];
nelem++;
litest_assert_int_lt(nelem, sz);
}
/* just append, if the same axis is present twice, libevdev will
only use the last value anyway */
i = 0;
while (override && override[i].value != -1) {
abs[nelem++] = override[i++];
litest_assert_int_lt(nelem, sz);
}
litest_assert_int_lt(nelem, sz);
abs[nelem].value = -1;
return abs;
}
static int*
merge_events(const int *orig, const int *override)
{
int *events;
unsigned int nelem, i;
size_t sz = KEY_MAX * 3;
if (!orig)
return NULL;
events = zalloc(sz * sizeof(int));
litest_assert_ptr_notnull(events);
nelem = 0;
while (orig[nelem] != -1) {
events[nelem] = orig[nelem];
nelem++;
litest_assert_int_lt(nelem, sz);
}
/* just append, if the same axis is present twice, libevdev will
* ignore the double definition anyway */
i = 0;
while (override && override[i] != -1) {
events[nelem++] = override[i++];
litest_assert_int_le(nelem, sz);
}
litest_assert_int_lt(nelem, sz);
events[nelem] = -1;
return events;
}
static inline struct created_file *
litest_copy_file(const char *dest, const char *src, const char *header, bool is_file)
{
int in, out, length;
struct created_file *file;
file = zalloc(sizeof(*file));
file->path = safe_strdup(dest);
if (strstr(dest, "XXXXXX")) {
int suffixlen;
suffixlen = file->path +
strlen(file->path) -
rindex(file->path, '.');
out = mkstemps(file->path, suffixlen);
} else {
out = open(file->path, O_CREAT|O_WRONLY, 0644);
}
if (out == -1)
litest_abort_msg("Failed to write to file %s (%s)\n",
file->path,
strerror(errno));
litest_assert_int_ne(chmod(file->path, 0644), -1);
if (header) {
length = strlen(header);
litest_assert_int_eq(write(out, header, length), length);
}
if (is_file) {
in = open(src, O_RDONLY);
if (in == -1)
litest_abort_msg("Failed to open file %s (%s)\n",
src,
strerror(errno));
/* lazy, just check for error and empty file copy */
litest_assert_int_gt(litest_send_file(out, in), 0);
close(in);
} else {
size_t written = write(out, src, strlen(src));
litest_assert_int_eq(written, strlen(src));
}
close(out);
return file;
}
static inline void
litest_install_model_quirks(struct list *created_files_list)
{
const char *warning =
"#################################################################\n"
"# WARNING: REMOVE THIS FILE\n"
"# This is a run-time file for the libinput test suite and\n"
"# should be removed on exit. If the test-suite is not currently \n"
"# running, remove this file\n"
"#################################################################\n\n";
struct created_file *file;
const char *test_device_udev_rule = "KERNELS==\"*input*\", "
"ATTRS{name}==\"litest *\", "
"ENV{LIBINPUT_TEST_DEVICE}=\"1\"";
file = litest_copy_file(UDEV_TEST_DEVICE_RULE_FILE,
test_device_udev_rule,
warning,
false);
list_insert(created_files_list, &file->link);
/* Only install the litest device rule when we're running as system
* test suite, we expect the others to be in place already */
if (use_system_rules_quirks)
return;
file = litest_copy_file(UDEV_DEVICE_GROUPS_FILE,
LIBINPUT_DEVICE_GROUPS_RULES_FILE,
warning,
true);
list_insert(created_files_list, &file->link);
file = litest_copy_file(UDEV_FUZZ_OVERRIDE_RULE_FILE,
LIBINPUT_FUZZ_OVERRIDE_UDEV_RULES_FILE,
warning,
true);
list_insert(created_files_list, &file->link);
}
static char *
litest_init_device_quirk_file(const char *data_dir,
struct litest_test_device *dev)
{
int fd;
FILE *f;
char path[PATH_MAX];
static int count;
if (!dev->quirk_file)
return NULL;
snprintf(path, sizeof(path),
"%s/99-%03d-%s.quirks",
data_dir,
++count,
dev->shortname);
fd = open(path, O_CREAT|O_WRONLY, 0644);
litest_assert_int_ne(fd, -1);
f = fdopen(fd, "w");
litest_assert_notnull(f);
litest_assert_int_ge(fputs(dev->quirk_file, f), 0);
fclose(f);
return safe_strdup(path);
}
static int is_quirks_file(const struct dirent *dir) {
return strendswith(dir->d_name, ".quirks");
}
/**
* Install the quirks from the quirks/ source directory.
*/
static void
litest_install_source_quirks(struct list *created_files_list,
const char *dirname)
{
struct dirent **namelist;
int ndev;
ndev = scandir(LIBINPUT_QUIRKS_SRCDIR,
&namelist,
is_quirks_file,
versionsort);
litest_assert_int_ge(ndev, 0);
for (int idx = 0; idx < ndev; idx++) {
struct created_file *file;
char *filename;
char dest[PATH_MAX];
char src[PATH_MAX];
filename = namelist[idx]->d_name;
snprintf(src, sizeof(src), "%s/%s",
LIBINPUT_QUIRKS_SRCDIR, filename);
snprintf(dest, sizeof(dest), "%s/%s", dirname, filename);
file = litest_copy_file(dest, src, NULL, true);
list_append(created_files_list, &file->link);
free(namelist[idx]);
}
free(namelist);
}
/**
* Install the quirks from the various litest test devices
*/
static void
litest_install_device_quirks(struct list *created_files_list,
const char *dirname)
{
struct litest_test_device *dev;
list_for_each(dev, &devices, node) {
char *path;
path = litest_init_device_quirk_file(dirname, dev);
if (path) {
struct created_file *file = zalloc(sizeof(*file));
file->path = path;
list_insert(created_files_list, &file->link);
}
}
}
static void
litest_setup_quirks(struct list *created_files_list,
enum quirks_setup_mode mode)
{
struct created_file *file = NULL;
const char *dirname;
char tmpdir[] = "/run/litest-XXXXXX";
switch (mode) {
case QUIRKS_SETUP_USE_SRCDIR:
dirname = LIBINPUT_QUIRKS_SRCDIR;
break;
case QUIRKS_SETUP_ONLY_DEVICE:
dirname = LIBINPUT_QUIRKS_DIR;
litest_install_device_quirks(created_files_list, dirname);
break;
case QUIRKS_SETUP_FULL:
litest_assert_notnull(mkdtemp(tmpdir));
litest_assert_int_ne(chmod(tmpdir, 0755), -1);
file = zalloc(sizeof *file);
file->path = safe_strdup(tmpdir);
dirname = tmpdir;
litest_install_source_quirks(created_files_list, dirname);
litest_install_device_quirks(created_files_list, dirname);
list_append(created_files_list, &file->link);
break;
}
setenv("LIBINPUT_QUIRKS_DIR", dirname, 1);
}
static inline void
mkdir_p(const char *dir)
{
char *path, *parent;
int rc;
if (streq(dir, "/"))
return;
path = safe_strdup(dir);
parent = dirname(path);
mkdir_p(parent);
rc = mkdir(dir, 0755);
if (rc == -1 && errno != EEXIST) {
litest_abort_msg("Failed to create directory %s (%s)\n",
dir,
strerror(errno));
}
free(path);
}
static inline void
litest_init_udev_rules(struct list *created_files)
{
mkdir_p(UDEV_RULES_D);
litest_install_model_quirks(created_files);
litest_init_all_device_udev_rules(created_files);
litest_reload_udev_rules();
}
static inline void
litest_remove_udev_rules(struct list *created_files_list)
{
struct created_file *f;
bool reload_udev;
reload_udev = !list_empty(created_files_list);
list_for_each_safe(f, created_files_list, link) {
list_remove(&f->link);
unlink(f->path);
rmdir(f->path);
free(f->path);
free(f);
}
if (reload_udev)
litest_reload_udev_rules();
}
/**
* Creates a uinput device but does not add it to a libinput context
*/
struct litest_device *
litest_create(enum litest_device_type which,
const char *name_override,
struct input_id *id_override,
const struct input_absinfo *abs_override,
const int *events_override)
{
struct litest_device *d = NULL;
struct litest_test_device *dev;
const char *name;
const struct input_id *id;
struct input_absinfo *abs;
int *events, *e;
const char *path;
int fd, rc;
bool found = false;
bool create_device = true;
list_for_each(dev, &devices, node) {
if (dev->type == which) {
found = true;
break;
}
}
if (!found)
ck_abort_msg("Invalid device type %d\n", which);
d = zalloc(sizeof(*d));
d->which = which;
/* device has custom create method */
if (dev->create) {
create_device = dev->create(d);
if (abs_override || events_override) {
litest_abort_msg("Custom create cannot be overridden");
}
}
abs = merge_absinfo(dev->absinfo, abs_override);
events = merge_events(dev->events, events_override);
name = name_override ? name_override : dev->name;
id = id_override ? id_override : dev->id;
if (create_device) {
d->uinput = litest_create_uinput_device_from_description(name,
id,
abs,
events);
d->interface = dev->interface;
for (e = events; *e != -1; e += 2) {
unsigned int type = *e,
code = *(e + 1);
if (type == INPUT_PROP_MAX &&
code == INPUT_PROP_SEMI_MT) {
d->semi_mt.is_semi_mt = true;
break;
}
}
}
free(abs);
free(events);
path = libevdev_uinput_get_devnode(d->uinput);
litest_assert_ptr_notnull(path);
fd = open(path, O_RDWR|O_NONBLOCK);
litest_assert_int_ne(fd, -1);
rc = libevdev_new_from_fd(fd, &d->evdev);
litest_assert_int_eq(rc, 0);
return d;
}
struct libinput *
litest_create_context(void)
{
struct libinput *libinput;
struct litest_context *ctx;
ctx = zalloc(sizeof *ctx);
list_init(&ctx->paths);
libinput = libinput_path_create_context(&interface, ctx);
litest_assert_notnull(libinput);
libinput_log_set_handler(libinput, litest_log_handler);
if (verbose)
libinput_log_set_priority(libinput, LIBINPUT_LOG_PRIORITY_DEBUG);
return libinput;
}
void
litest_destroy_context(struct libinput *li)
{
struct path *p;
struct litest_context *ctx;
ctx = libinput_get_user_data(li);
litest_assert_ptr_notnull(ctx);
libinput_unref(li);
list_for_each_safe(p, &ctx->paths, link) {
litest_abort_msg("Device paths should be removed by now");
}
free(ctx);
}
void
litest_disable_log_handler(struct libinput *libinput)
{
libinput_log_set_handler(libinput, NULL);
}
void
litest_restore_log_handler(struct libinput *libinput)
{
libinput_log_set_handler(libinput, litest_log_handler);
if (verbose)
libinput_log_set_priority(libinput, LIBINPUT_LOG_PRIORITY_DEBUG);
}
LIBINPUT_ATTRIBUTE_PRINTF(3, 0)
static void
litest_bug_log_handler(struct libinput *libinput,
enum libinput_log_priority pri,
const char *format,
va_list args)
{
if (strstr(format, "client bug: ") ||
strstr(format, "libinput bug: ") ||
strstr(format, "kernel bug: "))
return;
litest_abort_msg("Expected bug statement in log msg, aborting.\n");
}
void
litest_set_log_handler_bug(struct libinput *libinput)
{
libinput_log_set_handler(libinput, litest_bug_log_handler);
}
struct litest_device *
litest_add_device_with_overrides(struct libinput *libinput,
enum litest_device_type which,
const char *name_override,
struct input_id *id_override,
const struct input_absinfo *abs_override,
const int *events_override)
{
struct udev_device *ud;
struct litest_device *d;
const char *path;
d = litest_create(which,
name_override,
id_override,
abs_override,
events_override);
path = libevdev_uinput_get_devnode(d->uinput);
litest_assert_ptr_notnull(path);
d->libinput = libinput;
d->libinput_device = libinput_path_add_device(d->libinput, path);
litest_assert_ptr_notnull(d->libinput_device);
ud = libinput_device_get_udev_device(d->libinput_device);
d->quirks = quirks_fetch_for_device(quirks_context, ud);
udev_device_unref(ud);
libinput_device_ref(d->libinput_device);
if (d->interface) {
unsigned int code;
code = ABS_X;
if (!libevdev_has_event_code(d->evdev, EV_ABS, code))
code = ABS_MT_POSITION_X;
if (libevdev_has_event_code(d->evdev, EV_ABS, code)) {
d->interface->min[ABS_X] = libevdev_get_abs_minimum(d->evdev, code);
d->interface->max[ABS_X] = libevdev_get_abs_maximum(d->evdev, code);
}
code = ABS_Y;
if (!libevdev_has_event_code(d->evdev, EV_ABS, code))
code = ABS_MT_POSITION_Y;
if (libevdev_has_event_code(d->evdev, EV_ABS, code)) {
d->interface->min[ABS_Y] = libevdev_get_abs_minimum(d->evdev, code);
d->interface->max[ABS_Y] = libevdev_get_abs_maximum(d->evdev, code);
}
d->interface->tool_type = BTN_TOOL_PEN;
}
return d;
}
struct litest_device *
litest_add_device(struct libinput *libinput,
enum litest_device_type which)
{
return litest_add_device_with_overrides(libinput,
which,
NULL,
NULL,
NULL,
NULL);
}
struct litest_device *
litest_create_device_with_overrides(enum litest_device_type which,
const char *name_override,
struct input_id *id_override,
const struct input_absinfo *abs_override,
const int *events_override)
{
struct litest_device *dev =
litest_add_device_with_overrides(litest_create_context(),
which,
name_override,
id_override,
abs_override,
events_override);
dev->owns_context = true;
return dev;
}
struct litest_device *
litest_create_device(enum litest_device_type which)
{
return litest_create_device_with_overrides(which, NULL, NULL, NULL, NULL);
}
static struct udev_monitor *
udev_setup_monitor(void)
{
struct udev *udev;
struct udev_monitor *udev_monitor;
int rc;
udev = udev_new();
litest_assert_notnull(udev);
udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
litest_assert_notnull(udev_monitor);
udev_monitor_filter_add_match_subsystem_devtype(udev_monitor, "input",
NULL);
/* remove O_NONBLOCK */
rc = fcntl(udev_monitor_get_fd(udev_monitor), F_SETFL, 0);
litest_assert_int_ne(rc, -1);
litest_assert_int_eq(udev_monitor_enable_receiving(udev_monitor),
0);
udev_unref(udev);
return udev_monitor;
}
static struct udev_device *
udev_wait_for_device_event(struct udev_monitor *udev_monitor,
const char *udev_event,
const char *syspath)
{
struct udev_device *udev_device = NULL;
/* blocking, we don't want to continue until udev is ready */
while (1) {
const char *udev_syspath = NULL;
const char *udev_action;
udev_device = udev_monitor_receive_device(udev_monitor);
litest_assert_notnull(udev_device);
udev_action = udev_device_get_action(udev_device);
if (!udev_action || !streq(udev_action, udev_event)) {
udev_device_unref(udev_device);
continue;
}
udev_syspath = udev_device_get_syspath(udev_device);
if (udev_syspath && strstartswith(udev_syspath, syspath))
break;
udev_device_unref(udev_device);
}
return udev_device;
}
void
litest_delete_device(struct litest_device *d)
{
struct udev_monitor *udev_monitor;
struct udev_device *udev_device;
char path[PATH_MAX];
if (!d)
return;
udev_monitor = udev_setup_monitor();
snprintf(path, sizeof(path),
"%s/event",
libevdev_uinput_get_syspath(d->uinput));
litest_assert_int_eq(d->skip_ev_syn, 0);
quirks_unref(d->quirks);
if (d->libinput_device) {
libinput_path_remove_device(d->libinput_device);
libinput_device_unref(d->libinput_device);
}
if (d->owns_context) {
libinput_dispatch(d->libinput);
litest_destroy_context(d->libinput);
}
close(libevdev_get_fd(d->evdev));
libevdev_free(d->evdev);
libevdev_uinput_destroy(d->uinput);
free(d->private);
memset(d,0, sizeof(*d));
free(d);
udev_device = udev_wait_for_device_event(udev_monitor,
"remove",
path);
udev_device_unref(udev_device);
udev_monitor_unref(udev_monitor);
}
void
litest_event(struct litest_device *d, unsigned int type,
unsigned int code, int value)
{
int ret;
if (!libevdev_has_event_code(d->evdev, type, code))
return;
if (d->skip_ev_syn && type == EV_SYN && code == SYN_REPORT)
return;
ret = libevdev_uinput_write_event(d->uinput, type, code, value);
litest_assert_int_eq(ret, 0);
}
static bool
axis_replacement_value(struct litest_device *d,
struct axis_replacement *axes,
int32_t evcode,
int32_t *value)
{
struct axis_replacement *axis = axes;
if (!axes)
return false;
while (axis->evcode != -1) {
if (axis->evcode == evcode) {
switch (evcode) {
case ABS_MT_SLOT:
case ABS_MT_TRACKING_ID:
case ABS_MT_TOOL_TYPE:
*value = axis->value;
break;
default:
*value = litest_scale(d, evcode, axis->value);
break;
}
return true;
}
axis++;
}
return false;
}
int
litest_auto_assign_value(struct litest_device *d,
const struct input_event *ev,
int slot, double x, double y,
struct axis_replacement *axes,
bool touching)
{
static int tracking_id;
int value = ev->value;
if (value != LITEST_AUTO_ASSIGN || ev->type != EV_ABS)
return value;
switch (ev->code) {
case ABS_X:
case ABS_MT_POSITION_X:
value = litest_scale(d, ABS_X, x);
break;
case ABS_Y:
case ABS_MT_POSITION_Y:
value = litest_scale(d, ABS_Y, y);
break;
case ABS_MT_TRACKING_ID:
value = ++tracking_id;
break;
case ABS_MT_SLOT:
value = slot;
break;
case ABS_MT_DISTANCE:
value = touching ? 0 : 1;
break;
case ABS_MT_TOOL_TYPE:
if (!axis_replacement_value(d, axes, ev->code, &value))
value = MT_TOOL_FINGER;
break;
default:
if (!axis_replacement_value(d, axes, ev->code, &value) &&
d->interface->get_axis_default) {
int error = d->interface->get_axis_default(d,
ev->code,
&value);
if (error) {
litest_abort_msg("Failed to get default axis value for %s (%d)\n",
libevdev_event_code_get_name(EV_ABS, ev->code),
ev->code);
}
}
break;
}
return value;
}
static void
send_btntool(struct litest_device *d, bool hover)
{
litest_event(d, EV_KEY, BTN_TOUCH, d->ntouches_down != 0 && !hover);
litest_event(d, EV_KEY, BTN_TOOL_FINGER, d->ntouches_down == 1);
litest_event(d, EV_KEY, BTN_TOOL_DOUBLETAP, d->ntouches_down == 2);
litest_event(d, EV_KEY, BTN_TOOL_TRIPLETAP, d->ntouches_down == 3);
litest_event(d, EV_KEY, BTN_TOOL_QUADTAP, d->ntouches_down == 4);
litest_event(d, EV_KEY, BTN_TOOL_QUINTTAP, d->ntouches_down == 5);
}
static void
slot_start(struct litest_device *d,
unsigned int slot,
double x,
double y,
struct axis_replacement *axes,
bool touching,
bool filter_abs_xy)
{
struct input_event *ev;
litest_assert_int_ge(d->ntouches_down, 0);
d->ntouches_down++;
send_btntool(d, !touching);
/* If the test device overrides touch_down and says it didn't
* handle the event, let's continue normally */
if (d->interface->touch_down &&
d->interface->touch_down(d, slot, x, y))
return;
for (ev = d->interface->touch_down_events;
ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1;
ev++) {
int value = litest_auto_assign_value(d,
ev,
slot,
x,
y,
axes,
touching);
if (value == LITEST_AUTO_ASSIGN)
continue;
if (filter_abs_xy && ev->type == EV_ABS &&
(ev->code == ABS_X || ev->code == ABS_Y))
continue;
litest_event(d, ev->type, ev->code, value);
}
}
static void
slot_move(struct litest_device *d,
unsigned int slot,
double x,
double y,
struct axis_replacement *axes,
bool touching,
bool filter_abs_xy)
{
struct input_event *ev;
if (d->interface->touch_move &&
d->interface->touch_move(d, slot, x, y))
return;
for (ev = d->interface->touch_move_events;
ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1;
ev++) {
int value = litest_auto_assign_value(d,
ev,
slot,
x,
y,
axes,
touching);
if (value == LITEST_AUTO_ASSIGN)
continue;
if (filter_abs_xy && ev->type == EV_ABS &&
(ev->code == ABS_X || ev->code == ABS_Y))
continue;
litest_event(d, ev->type, ev->code, value);
}
}
static void
touch_up(struct litest_device *d, unsigned int slot)
{
struct input_event *ev;
struct input_event up[] = {
{ .type = EV_ABS, .code = ABS_MT_SLOT, .value = LITEST_AUTO_ASSIGN },
{ .type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = -1 },
{ .type = EV_ABS, .code = ABS_MT_PRESSURE, .value = 0 },
{ .type = EV_ABS, .code = ABS_MT_TOUCH_MAJOR, .value = 0 },
{ .type = EV_ABS, .code = ABS_MT_TOUCH_MINOR, .value = 0 },
{ .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
{ .type = -1, .code = -1 }
};
litest_assert_int_gt(d->ntouches_down, 0);
d->ntouches_down--;
send_btntool(d, false);
if (d->interface->touch_up &&
d->interface->touch_up(d, slot)) {
return;
} else if (d->interface->touch_up_events) {
ev = d->interface->touch_up_events;
} else
ev = up;
for ( /* */;
ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1;
ev++) {
int value = litest_auto_assign_value(d,
ev,
slot,
0,
0,
NULL,
false);
litest_event(d, ev->type, ev->code, value);
}
}
static void
litest_slot_start(struct litest_device *d,
unsigned int slot,
double x,
double y,
struct axis_replacement *axes,
bool touching)
{
double t, l, r = 0, b = 0; /* top, left, right, bottom */
bool filter_abs_xy = false;
if (!d->semi_mt.is_semi_mt) {
slot_start(d, slot, x, y, axes, touching, filter_abs_xy);
return;
}
if (d->ntouches_down >= 2 || slot > 1)
return;
slot = d->ntouches_down;
if (d->ntouches_down == 0) {
l = x;
t = y;
} else {
int other = (slot + 1) % 2;
l = min(x, d->semi_mt.touches[other].x);
t = min(y, d->semi_mt.touches[other].y);
r = max(x, d->semi_mt.touches[other].x);
b = max(y, d->semi_mt.touches[other].y);
}
litest_push_event_frame(d);
if (d->ntouches_down == 0)
slot_start(d, 0, l, t, axes, touching, filter_abs_xy);
else
slot_move(d, 0, l, t, axes, touching, filter_abs_xy);
if (slot == 1) {
filter_abs_xy = true;
slot_start(d, 1, r, b, axes, touching, filter_abs_xy);
}
litest_pop_event_frame(d);
d->semi_mt.touches[slot].x = x;
d->semi_mt.touches[slot].y = y;
}
void
litest_touch_sequence(struct litest_device *d,
unsigned int slot,
double x_from,
double y_from,
double x_to,
double y_to,
int steps)
{
litest_touch_down(d, slot, x_from, y_from);
litest_touch_move_to(d, slot,
x_from, y_from,
x_to, y_to,
steps);
litest_touch_up(d, slot);
}
void
litest_touch_down(struct litest_device *d,
unsigned int slot,
double x,
double y)
{
litest_slot_start(d, slot, x, y, NULL, true);
}
void
litest_touch_down_extended(struct litest_device *d,
unsigned int slot,
double x,
double y,
struct axis_replacement *axes)
{
litest_slot_start(d, slot, x, y, axes, true);
}
static void
litest_slot_move(struct litest_device *d,
unsigned int slot,
double x,
double y,
struct axis_replacement *axes,
bool touching)
{
double t, l, r = 0, b = 0; /* top, left, right, bottom */
bool filter_abs_xy = false;
if (!d->semi_mt.is_semi_mt) {
slot_move(d, slot, x, y, axes, touching, filter_abs_xy);
return;
}
if (d->ntouches_down > 2 || slot > 1)
return;
if (d->ntouches_down == 1) {
l = x;
t = y;
} else {
int other = (slot + 1) % 2;
l = min(x, d->semi_mt.touches[other].x);
t = min(y, d->semi_mt.touches[other].y);
r = max(x, d->semi_mt.touches[other].x);
b = max(y, d->semi_mt.touches[other].y);
}
litest_push_event_frame(d);
slot_move(d, 0, l, t, axes, touching, filter_abs_xy);
if (d->ntouches_down == 2) {
filter_abs_xy = true;
slot_move(d, 1, r, b, axes, touching, filter_abs_xy);
}
litest_pop_event_frame(d);
d->semi_mt.touches[slot].x = x;
d->semi_mt.touches[slot].y = y;
}
void
litest_touch_up(struct litest_device *d, unsigned int slot)
{
if (!d->semi_mt.is_semi_mt) {
touch_up(d, slot);
return;
}
if (d->ntouches_down > 2 || slot > 1)
return;
litest_push_event_frame(d);
touch_up(d, d->ntouches_down - 1);
/* if we have one finger left, send x/y coords for that finger left.
this is likely to happen with a real touchpad */
if (d->ntouches_down == 1) {
bool touching = true;
bool filter_abs_xy = false;
int other = (slot + 1) % 2;
slot_move(d,
0,
d->semi_mt.touches[other].x,
d->semi_mt.touches[other].y,
NULL,
touching,
filter_abs_xy);
}
litest_pop_event_frame(d);
}
void
litest_touch_move(struct litest_device *d,
unsigned int slot,
double x,
double y)
{
litest_slot_move(d, slot, x, y, NULL, true);
}
void
litest_touch_move_extended(struct litest_device *d,
unsigned int slot,
double x,
double y,
struct axis_replacement *axes)
{
litest_slot_move(d, slot, x, y, axes, true);
}
void
litest_touch_move_to(struct litest_device *d,
unsigned int slot,
double x_from, double y_from,
double x_to, double y_to,
int steps)
{
litest_touch_move_to_extended(d, slot,
x_from, y_from,
x_to, y_to,
NULL,
steps);
}
void
litest_touch_move_to_extended(struct litest_device *d,
unsigned int slot,
double x_from, double y_from,
double x_to, double y_to,
struct axis_replacement *axes,
int steps)
{
int sleep_ms = 10;
for (int i = 1; i < steps; i++) {
litest_touch_move_extended(d, slot,
x_from + (x_to - x_from)/steps * i,
y_from + (y_to - y_from)/steps * i,
axes);
libinput_dispatch(d->libinput);
msleep(sleep_ms);
libinput_dispatch(d->libinput);
}
litest_touch_move_extended(d, slot, x_to, y_to, axes);
}
static int
auto_assign_tablet_value(struct litest_device *d,
const struct input_event *ev,
int x, int y,
struct axis_replacement *axes)
{
static int tracking_id;
int value = ev->value;
if (value != LITEST_AUTO_ASSIGN || ev->type != EV_ABS)
return value;
switch (ev->code) {
case ABS_MT_TRACKING_ID:
value = ++tracking_id;
break;
case ABS_X:
case ABS_MT_POSITION_X:
value = litest_scale(d, ABS_X, x);
break;
case ABS_Y:
case ABS_MT_POSITION_Y:
value = litest_scale(d, ABS_Y, y);
break;
default:
if (!axis_replacement_value(d, axes, ev->code, &value) &&
d->interface->get_axis_default) {
int error = d->interface->get_axis_default(d, ev->code, &value);
if (error) {
litest_abort_msg("Failed to get default axis value for %s (%d)\n",
libevdev_event_code_get_name(EV_ABS, ev->code),
ev->code);
}
}
break;
}
return value;
}
static int
tablet_ignore_event(const struct input_event *ev, int value)
{
return value == -1 && (ev->code == ABS_PRESSURE || ev->code == ABS_DISTANCE);
}
void
litest_tablet_set_tool_type(struct litest_device *d, unsigned int code)
{
switch (code) {
case BTN_TOOL_PEN:
case BTN_TOOL_RUBBER:
case BTN_TOOL_BRUSH:
case BTN_TOOL_PENCIL:
case BTN_TOOL_AIRBRUSH:
case BTN_TOOL_MOUSE:
case BTN_TOOL_LENS:
break;
default:
abort();
}
d->interface->tool_type = code;
}
static void
litest_tool_event(struct litest_device *d, int value)
{
unsigned int tool = d->interface->tool_type;
litest_event(d, EV_KEY, tool, value);
}
void
litest_tablet_proximity_in(struct litest_device *d, int x, int y, struct axis_replacement *axes)
{
struct input_event *ev;
/* If the test device overrides proximity_in and says it didn't
* handle the event, let's continue normally */
if (d->interface->tablet_proximity_in &&
d->interface->tablet_proximity_in(d, d->interface->tool_type, x, y, axes))
return;
ev = d->interface->tablet_proximity_in_events;
while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
int value;
switch (evbit(ev->type, ev->code)) {
case evbit(EV_KEY, LITEST_BTN_TOOL_AUTO):
litest_tool_event(d, ev->value);
break;
default:
value = auto_assign_tablet_value(d, ev, x, y, axes);
if (!tablet_ignore_event(ev, value))
litest_event(d, ev->type, ev->code, value);
}
ev++;
}
}
void
litest_tablet_proximity_out(struct litest_device *d)
{
struct input_event *ev;
/* If the test device overrides proximity_out and says it didn't
* handle the event, let's continue normally */
if (d->interface->tablet_proximity_out &&
d->interface->tablet_proximity_out(d, d->interface->tool_type))
return;
ev = d->interface->tablet_proximity_out_events;
while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
int value;
switch (evbit(ev->type, ev->code)) {
case evbit(EV_KEY, LITEST_BTN_TOOL_AUTO):
litest_tool_event(d, ev->value);
break;
default:
value = auto_assign_tablet_value(d, ev, -1, -1, NULL);
if (!tablet_ignore_event(ev, value))
litest_event(d, ev->type, ev->code, value);
break;
}
ev++;
}
}
void
litest_tablet_motion(struct litest_device *d, int x, int y, struct axis_replacement *axes)
{
struct input_event *ev;
ev = d->interface->tablet_motion_events;
while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
int value = auto_assign_tablet_value(d, ev, x, y, axes);
if (!tablet_ignore_event(ev, value))
litest_event(d, ev->type, ev->code, value);
ev++;
}
}
void
litest_touch_move_two_touches(struct litest_device *d,
double x0, double y0,
double x1, double y1,
double dx, double dy,
int steps)
{
int sleep_ms = 10;
for (int i = 1; i < steps; i++) {
litest_push_event_frame(d);
litest_touch_move(d, 0, x0 + dx / steps * i,
y0 + dy / steps * i);
litest_touch_move(d, 1, x1 + dx / steps * i,
y1 + dy / steps * i);
litest_pop_event_frame(d);
libinput_dispatch(d->libinput);
msleep(sleep_ms);
libinput_dispatch(d->libinput);
}
litest_push_event_frame(d);
litest_touch_move(d, 0, x0 + dx, y0 + dy);
litest_touch_move(d, 1, x1 + dx, y1 + dy);
litest_pop_event_frame(d);
}
void
litest_touch_move_three_touches(struct litest_device *d,
double x0, double y0,
double x1, double y1,
double x2, double y2,
double dx, double dy,
int steps)
{
int sleep_ms = 10;
for (int i = 1; i <= steps; i++) {
double step_x = dx / steps * i;
double step_y = dy / steps * i;
litest_push_event_frame(d);
litest_touch_move(d, 0, x0 + step_x, y0 + step_y);
litest_touch_move(d, 1, x1 + step_x, y1 + step_y);
litest_touch_move(d, 2, x2 + step_x, y2 + step_y);
litest_pop_event_frame(d);
libinput_dispatch(d->libinput);
msleep(sleep_ms);
}
libinput_dispatch(d->libinput);
}
void
litest_hover_start(struct litest_device *d,
unsigned int slot,
double x,
double y)
{
struct axis_replacement axes[] = {
{ABS_MT_PRESSURE, 0 },
{ABS_PRESSURE, 0 },
{-1, -1 },
};
litest_slot_start(d, slot, x, y, axes, 0);
}
void
litest_hover_end(struct litest_device *d, unsigned int slot)
{
struct input_event *ev;
struct input_event up[] = {
{ .type = EV_ABS, .code = ABS_MT_SLOT, .value = LITEST_AUTO_ASSIGN },
{ .type = EV_ABS, .code = ABS_MT_DISTANCE, .value = 1 },
{ .type = EV_ABS, .code = ABS_MT_TRACKING_ID, .value = -1 },
{ .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
{ .type = -1, .code = -1 }
};
litest_assert_int_gt(d->ntouches_down, 0);
d->ntouches_down--;
send_btntool(d, true);
if (d->interface->touch_up) {
d->interface->touch_up(d, slot);
return;
} else if (d->interface->touch_up_events) {
ev = d->interface->touch_up_events;
} else
ev = up;
while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
int value = litest_auto_assign_value(d, ev, slot, 0, 0, NULL, false);
litest_event(d, ev->type, ev->code, value);
ev++;
}
}
void
litest_hover_move(struct litest_device *d, unsigned int slot,
double x, double y)
{
struct axis_replacement axes[] = {
{ABS_MT_PRESSURE, 0 },
{ABS_PRESSURE, 0 },
{-1, -1 },
};
litest_slot_move(d, slot, x, y, axes, false);
}
void
litest_hover_move_to(struct litest_device *d,
unsigned int slot,
double x_from, double y_from,
double x_to, double y_to,
int steps)
{
int sleep_ms = 10;
for (int i = 0; i < steps - 1; i++) {
litest_hover_move(d, slot,
x_from + (x_to - x_from)/steps * i,
y_from + (y_to - y_from)/steps * i);
libinput_dispatch(d->libinput);
msleep(sleep_ms);
libinput_dispatch(d->libinput);
}
litest_hover_move(d, slot, x_to, y_to);
}
void
litest_hover_move_two_touches(struct litest_device *d,
double x0, double y0,
double x1, double y1,
double dx, double dy,
int steps)
{
int sleep_ms = 10;
for (int i = 0; i < steps - 1; i++) {
litest_push_event_frame(d);
litest_hover_move(d, 0, x0 + dx / steps * i,
y0 + dy / steps * i);
litest_hover_move(d, 1, x1 + dx / steps * i,
y1 + dy / steps * i);
litest_pop_event_frame(d);
libinput_dispatch(d->libinput);
msleep(sleep_ms);
libinput_dispatch(d->libinput);
}
litest_push_event_frame(d);
litest_hover_move(d, 0, x0 + dx, y0 + dy);
litest_hover_move(d, 1, x1 + dx, y1 + dy);
litest_pop_event_frame(d);
}
void
litest_button_click(struct litest_device *d,
unsigned int button,
bool is_press)
{
struct input_event *ev;
struct input_event click[] = {
{ .type = EV_KEY, .code = button, .value = is_press ? 1 : 0 },
{ .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
};
ARRAY_FOR_EACH(click, ev)
litest_event(d, ev->type, ev->code, ev->value);
}
void
litest_button_click_debounced(struct litest_device *d,
struct libinput *li,
unsigned int button,
bool is_press)
{
litest_button_click(d, button, is_press);
libinput_dispatch(li);
litest_timeout_debounce();
libinput_dispatch(li);
}
void
litest_button_scroll(struct litest_device *dev,
unsigned int button,
double dx, double dy)
{
struct libinput *li = dev->libinput;
litest_button_click_debounced(dev, li, button, 1);
libinput_dispatch(li);
litest_timeout_buttonscroll();
libinput_dispatch(li);
litest_event(dev, EV_REL, REL_X, dx);
litest_event(dev, EV_REL, REL_Y, dy);
litest_event(dev, EV_SYN, SYN_REPORT, 0);
litest_button_click_debounced(dev, li, button, 0);
libinput_dispatch(li);
}
void
litest_button_scroll_locked(struct litest_device *dev,
unsigned int button,
double dx, double dy)
{
struct libinput *li = dev->libinput;
litest_button_click_debounced(dev, li, button, 1);
litest_button_click_debounced(dev, li, button, 0);
libinput_dispatch(li);
litest_timeout_buttonscroll();
libinput_dispatch(li);
litest_event(dev, EV_REL, REL_X, dx);
litest_event(dev, EV_REL, REL_Y, dy);
litest_event(dev, EV_SYN, SYN_REPORT, 0);
libinput_dispatch(li);
}
void
litest_keyboard_key(struct litest_device *d, unsigned int key, bool is_press)
{
struct input_event *ev;
struct input_event click[] = {
{ .type = EV_KEY, .code = key, .value = is_press ? 1 : 0 },
{ .type = EV_SYN, .code = SYN_REPORT, .value = 0 },
};
ARRAY_FOR_EACH(click, ev)
litest_event(d, ev->type, ev->code, ev->value);
}
void
litest_switch_action(struct litest_device *dev,
enum libinput_switch sw,
enum libinput_switch_state state)
{
unsigned int code;
switch (sw) {
case LIBINPUT_SWITCH_LID:
code = SW_LID;
break;
case LIBINPUT_SWITCH_TABLET_MODE:
code = SW_TABLET_MODE;
break;
default:
litest_abort_msg("Invalid switch %d", sw);
break;
}
litest_event(dev, EV_SW, code, state);
litest_event(dev, EV_SYN, SYN_REPORT, 0);
}
static int
litest_scale_axis(const struct litest_device *d,
unsigned int axis,
double val)
{
const struct input_absinfo *abs;
litest_assert_double_ge(val, 0.0);
/* major/minor must be able to beyond 100% for large fingers */
if (axis != ABS_MT_TOUCH_MAJOR &&
axis != ABS_MT_TOUCH_MINOR) {
litest_assert_double_le(val, 100.0);
}
abs = libevdev_get_abs_info(d->evdev, axis);
litest_assert_notnull(abs);
return (abs->maximum - abs->minimum) * val/100.0 + abs->minimum;
}
static inline int
litest_scale_range(int min, int max, double val)
{
litest_assert_int_ge((int)val, 0);
litest_assert_int_le((int)val, 100);
return (max - min) * val/100.0 + min;
}
int
litest_scale(const struct litest_device *d, unsigned int axis, double val)
{
int min, max;
litest_assert_double_ge(val, 0.0);
/* major/minor must be able to beyond 100% for large fingers */
if (axis != ABS_MT_TOUCH_MAJOR &&
axis != ABS_MT_TOUCH_MINOR)
litest_assert_double_le(val, 100.0);
if (axis <= ABS_Y) {
min = d->interface->min[axis];
max = d->interface->max[axis];
return litest_scale_range(min, max, val);
} else {
return litest_scale_axis(d, axis, val);
}
}
static inline int
auto_assign_pad_value(struct litest_device *dev,
struct input_event *ev,
double value)
{
const struct input_absinfo *abs;
if (ev->value != LITEST_AUTO_ASSIGN ||
ev->type != EV_ABS)
return value;
abs = libevdev_get_abs_info(dev->evdev, ev->code);
litest_assert_notnull(abs);
if (ev->code == ABS_RX || ev->code == ABS_RY) {
double min = abs->minimum != 0 ? log2(abs->minimum) : 0,
max = abs->maximum != 0 ? log2(abs->maximum) : 0;
/* Value 0 is reserved for finger up, so a value of 0% is
* actually 1 */
if (value == 0.0) {
return 1;
} else {
value = litest_scale_range(min, max, value);
return pow(2, value);
}
} else {
return litest_scale_range(abs->minimum, abs->maximum, value);
}
}
void
litest_pad_ring_start(struct litest_device *d, double value)
{
struct input_event *ev;
ev = d->interface->pad_ring_start_events;
while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
value = auto_assign_pad_value(d, ev, value);
litest_event(d, ev->type, ev->code, value);
ev++;
}
}
void
litest_pad_ring_change(struct litest_device *d, double value)
{
struct input_event *ev;
ev = d->interface->pad_ring_change_events;
while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
value = auto_assign_pad_value(d, ev, value);
litest_event(d, ev->type, ev->code, value);
ev++;
}
}
void
litest_pad_ring_end(struct litest_device *d)
{
struct input_event *ev;
ev = d->interface->pad_ring_end_events;
while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
litest_event(d, ev->type, ev->code, ev->value);
ev++;
}
}
void
litest_pad_strip_start(struct litest_device *d, double value)
{
struct input_event *ev;
ev = d->interface->pad_strip_start_events;
while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
value = auto_assign_pad_value(d, ev, value);
litest_event(d, ev->type, ev->code, value);
ev++;
}
}
void
litest_pad_strip_change(struct litest_device *d, double value)
{
struct input_event *ev;
ev = d->interface->pad_strip_change_events;
while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
value = auto_assign_pad_value(d, ev, value);
litest_event(d, ev->type, ev->code, value);
ev++;
}
}
void
litest_pad_strip_end(struct litest_device *d)
{
struct input_event *ev;
ev = d->interface->pad_strip_end_events;
while (ev && (int16_t)ev->type != -1 && (int16_t)ev->code != -1) {
litest_event(d, ev->type, ev->code, ev->value);
ev++;
}
}
void
litest_wait_for_event(struct libinput *li)
{
return litest_wait_for_event_of_type(li, -1);
}
void
litest_wait_for_event_of_type(struct libinput *li, ...)
{
va_list args;
enum libinput_event_type types[32] = {LIBINPUT_EVENT_NONE};
size_t ntypes = 0;
enum libinput_event_type type;
struct pollfd fds;
va_start(args, li);
type = va_arg(args, int);
while ((int)type != -1) {
litest_assert_int_gt(type, 0U);
litest_assert_int_lt(ntypes, ARRAY_LENGTH(types));
types[ntypes++] = type;
type = va_arg(args, int);
}
va_end(args);
fds.fd = libinput_get_fd(li);
fds.events = POLLIN;
fds.revents = 0;
while (1) {
size_t i;
struct libinput_event *event;
while ((type = libinput_next_event_type(li)) == LIBINPUT_EVENT_NONE) {
int rc = poll(&fds, 1, 2000);
litest_assert_int_gt(rc, 0);
libinput_dispatch(li);
}
/* no event mask means wait for any event */
if (ntypes == 0)
return;
for (i = 0; i < ntypes; i++) {
if (type == types[i])
return;
}
event = libinput_get_event(li);
libinput_event_destroy(event);
}
}
void
litest_drain_events(struct libinput *li)
{
struct libinput_event *event;
libinput_dispatch(li);
while ((event = libinput_get_event(li))) {
libinput_event_destroy(event);
libinput_dispatch(li);
}
}
void
litest_drain_events_of_type(struct libinput *li, ...)
{
enum libinput_event_type type;
enum libinput_event_type types[32] = {LIBINPUT_EVENT_NONE};
size_t ntypes = 0;
va_list args;
va_start(args, li);
type = va_arg(args, int);
while ((int)type != -1) {
litest_assert_int_gt(type, 0U);
litest_assert_int_lt(ntypes, ARRAY_LENGTH(types));
types[ntypes++] = type;
type = va_arg(args, int);
}
va_end(args);
libinput_dispatch(li);
type = libinput_next_event_type(li);
while (type != LIBINPUT_EVENT_NONE) {
struct libinput_event *event;
bool found = false;
type = libinput_next_event_type(li);
for (size_t i = 0; i < ntypes; i++) {
if (type == types[i]) {
found = true;
break;
}
}
if (!found)
return;
event = libinput_get_event(li);
libinput_event_destroy(event);
libinput_dispatch(li);
}
}
static const char *
litest_event_type_str(enum libinput_event_type type)
{
const char *str = NULL;
switch (type) {
case LIBINPUT_EVENT_NONE:
abort();
case LIBINPUT_EVENT_DEVICE_ADDED:
str = "ADDED";
break;
case LIBINPUT_EVENT_DEVICE_REMOVED:
str = "REMOVED";
break;
case LIBINPUT_EVENT_KEYBOARD_KEY:
str = "KEY";
break;
case LIBINPUT_EVENT_POINTER_MOTION:
str = "MOTION";
break;
case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
str = "ABSOLUTE";
break;
case LIBINPUT_EVENT_POINTER_BUTTON:
str = "BUTTON";
break;
case LIBINPUT_EVENT_POINTER_AXIS:
str = "AXIS";
break;
case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
str = "SCROLL_WHEEL";
break;
case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
str = "SCROLL_FINGER";
break;
case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
str = "SCROLL_CONTINUOUS";
break;
case LIBINPUT_EVENT_TOUCH_DOWN:
str = "TOUCH DOWN";
break;
case LIBINPUT_EVENT_TOUCH_UP:
str = "TOUCH UP";
break;
case LIBINPUT_EVENT_TOUCH_MOTION:
str = "TOUCH MOTION";
break;
case LIBINPUT_EVENT_TOUCH_CANCEL:
str = "TOUCH CANCEL";
break;
case LIBINPUT_EVENT_TOUCH_FRAME:
str = "TOUCH FRAME";
break;
case LIBINPUT_EVENT_GESTURE_SWIPE_BEGIN:
str = "GESTURE SWIPE BEGIN";
break;
case LIBINPUT_EVENT_GESTURE_SWIPE_UPDATE:
str = "GESTURE SWIPE UPDATE";
break;
case LIBINPUT_EVENT_GESTURE_SWIPE_END:
str = "GESTURE SWIPE END";
break;
case LIBINPUT_EVENT_GESTURE_PINCH_BEGIN:
str = "GESTURE PINCH BEGIN";
break;
case LIBINPUT_EVENT_GESTURE_PINCH_UPDATE:
str = "GESTURE PINCH UPDATE";
break;
case LIBINPUT_EVENT_GESTURE_PINCH_END:
str = "GESTURE PINCH END";
break;
case LIBINPUT_EVENT_GESTURE_HOLD_BEGIN:
str = "GESTURE HOLD BEGIN";
break;
case LIBINPUT_EVENT_GESTURE_HOLD_END:
str = "GESTURE HOLD END";
break;
case LIBINPUT_EVENT_TABLET_TOOL_AXIS:
str = "TABLET TOOL AXIS";
break;
case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
str = "TABLET TOOL PROX";
break;
case LIBINPUT_EVENT_TABLET_TOOL_TIP:
str = "TABLET TOOL TIP";
break;
case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
str = "TABLET TOOL BUTTON";
break;
case LIBINPUT_EVENT_TABLET_PAD_BUTTON:
str = "TABLET PAD BUTTON";
break;
case LIBINPUT_EVENT_TABLET_PAD_RING:
str = "TABLET PAD RING";
break;
case LIBINPUT_EVENT_TABLET_PAD_STRIP:
str = "TABLET PAD STRIP";
break;
case LIBINPUT_EVENT_TABLET_PAD_KEY:
str = "TABLET PAD KEY";
break;
case LIBINPUT_EVENT_SWITCH_TOGGLE:
str = "SWITCH TOGGLE";
break;
}
return str;
}
static const char *
litest_event_get_type_str(struct libinput_event *event)
{
return litest_event_type_str(libinput_event_get_type(event));
}
static void
litest_print_event(struct libinput_event *event)
{
struct libinput_event_pointer *p;
struct libinput_event_tablet_tool *t;
struct libinput_event_tablet_pad *pad;
struct libinput_device *dev;
enum libinput_event_type type;
double x, y;
dev = libinput_event_get_device(event);
type = libinput_event_get_type(event);
fprintf(stderr,
"device %s (%s) type %s ",
libinput_device_get_sysname(dev),
libinput_device_get_name(dev),
litest_event_get_type_str(event));
switch (type) {
case LIBINPUT_EVENT_POINTER_MOTION:
p = libinput_event_get_pointer_event(event);
x = libinput_event_pointer_get_dx(p);
y = libinput_event_pointer_get_dy(p);
fprintf(stderr, "%.2f/%.2f", x, y);
break;
case LIBINPUT_EVENT_POINTER_MOTION_ABSOLUTE:
p = libinput_event_get_pointer_event(event);
x = libinput_event_pointer_get_absolute_x(p);
y = libinput_event_pointer_get_absolute_y(p);
fprintf(stderr, "%.2f/%.2f", x, y);
break;
case LIBINPUT_EVENT_POINTER_BUTTON:
p = libinput_event_get_pointer_event(event);
fprintf(stderr,
"button %d state %d",
libinput_event_pointer_get_button(p),
libinput_event_pointer_get_button_state(p));
break;
case LIBINPUT_EVENT_POINTER_AXIS:
p = libinput_event_get_pointer_event(event);
x = 0.0;
y = 0.0;
if (libinput_event_pointer_has_axis(p,
LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL))
y = libinput_event_pointer_get_axis_value(p,
LIBINPUT_POINTER_AXIS_SCROLL_VERTICAL);
if (libinput_event_pointer_has_axis(p,
LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL))
x = libinput_event_pointer_get_axis_value(p,
LIBINPUT_POINTER_AXIS_SCROLL_HORIZONTAL);
fprintf(stderr, "vert %.2f horiz %.2f", y, x);
break;
case LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY:
t = libinput_event_get_tablet_tool_event(event);
fprintf(stderr, "proximity %d",
libinput_event_tablet_tool_get_proximity_state(t));
break;
case LIBINPUT_EVENT_TABLET_TOOL_TIP:
t = libinput_event_get_tablet_tool_event(event);
fprintf(stderr, "tip %d",
libinput_event_tablet_tool_get_tip_state(t));
break;
case LIBINPUT_EVENT_TABLET_TOOL_BUTTON:
t = libinput_event_get_tablet_tool_event(event);
fprintf(stderr, "button %d state %d",
libinput_event_tablet_tool_get_button(t),
libinput_event_tablet_tool_get_button_state(t));
break;
case LIBINPUT_EVENT_TABLET_PAD_BUTTON:
pad = libinput_event_get_tablet_pad_event(event);
fprintf(stderr, "button %d state %d",
libinput_event_tablet_pad_get_button_number(pad),
libinput_event_tablet_pad_get_button_state(pad));
break;
case LIBINPUT_EVENT_TABLET_PAD_RING:
pad = libinput_event_get_tablet_pad_event(event);
fprintf(stderr, "ring %d position %.2f source %d",
libinput_event_tablet_pad_get_ring_number(pad),
libinput_event_tablet_pad_get_ring_position(pad),
libinput_event_tablet_pad_get_ring_source(pad));
break;
case LIBINPUT_EVENT_TABLET_PAD_STRIP:
pad = libinput_event_get_tablet_pad_event(event);
fprintf(stderr, "strip %d position %.2f source %d",
libinput_event_tablet_pad_get_ring_number(pad),
libinput_event_tablet_pad_get_ring_position(pad),
libinput_event_tablet_pad_get_ring_source(pad));
break;
default:
break;
}
fprintf(stderr, "\n");
}
#define litest_assert_event_type_is_one_of(...) \
_litest_assert_event_type_is_one_of(__VA_ARGS__, -1)
static void
_litest_assert_event_type_is_one_of(struct libinput_event *event, ...)
{
va_list args;
enum libinput_event_type expected_type;
enum libinput_event_type actual_type = libinput_event_get_type(event);
bool match = false;
va_start(args, event);
expected_type = va_arg(args, int);
while ((int)expected_type != -1 && !match) {
match = (actual_type == expected_type);
expected_type = va_arg(args, int);
}
va_end(args);
if (match)
return;
fprintf(stderr,
"FAILED EVENT TYPE: %s: have %s (%d) but want ",
libinput_device_get_name(libinput_event_get_device(event)),
litest_event_get_type_str(event),
libinput_event_get_type(event));
va_start(args, event);
expected_type = va_arg(args, int);
while ((int)expected_type != -1) {
fprintf(stderr,
"%s (%d)",
litest_event_type_str(expected_type),
expected_type);
expected_type = va_arg(args, int);
if ((int)expected_type != -1)
fprintf(stderr, " || ");
}
fprintf(stderr, "\nWrong event is: ");
litest_print_event(event);
litest_backtrace();
abort();
}
void
litest_assert_event_type(struct libinput_event *event,
enum libinput_event_type want)
{
litest_assert_event_type_is_one_of(event, want);
}
void
litest_assert_empty_queue(struct libinput *li)
{
bool empty_queue = true;
struct libinput_event *event;
libinput_dispatch(li);
while ((event = libinput_get_event(li))) {
empty_queue = false;
fprintf(stderr,
"Unexpected event: ");
litest_print_event(event);
libinput_event_destroy(event);
libinput_dispatch(li);
}
litest_assert(empty_queue);
}
static struct libevdev_uinput *
litest_create_uinput(const char *name,
const struct input_id *id,
const struct input_absinfo *abs_info,
const int *events)
{
struct libevdev_uinput *uinput;
struct libevdev *dev;
int type, code;
int rc, fd;
const struct input_absinfo *abs;
const struct input_absinfo default_abs = {
.value = 0,
.minimum = 0,
.maximum = 100,
.fuzz = 0,
.flat = 0,
.resolution = 100
};
char buf[512];
const char *devnode;
dev = libevdev_new();
litest_assert_ptr_notnull(dev);
snprintf(buf, sizeof(buf), "litest %s", name);
libevdev_set_name(dev, buf);
if (id) {
libevdev_set_id_bustype(dev, id->bustype);
libevdev_set_id_vendor(dev, id->vendor);
libevdev_set_id_product(dev, id->product);
libevdev_set_id_version(dev, id->version);
}
abs = abs_info;
while (abs && abs->value != -1) {
struct input_absinfo a = *abs;
/* abs_info->value is used for the code and may be outside
of [min, max] */
a.value = abs->minimum;
rc = libevdev_enable_event_code(dev, EV_ABS, abs->value, &a);
litest_assert_int_eq(rc, 0);
abs++;
}
while (events &&
(type = *events++) != -1 &&
(code = *events++) != -1) {
if (type == INPUT_PROP_MAX) {
rc = libevdev_enable_property(dev, code);
} else {
rc = libevdev_enable_event_code(dev, type, code,
type == EV_ABS ? &default_abs : NULL);
}
litest_assert_int_eq(rc, 0);
}
rc = libevdev_uinput_create_from_device(dev,
LIBEVDEV_UINPUT_OPEN_MANAGED,
&uinput);
/* workaround for a bug in libevdev pre-1.3
http://cgit.freedesktop.org/libevdev/commit/?id=debe9b030c8069cdf78307888ef3b65830b25122 */
if (rc == -EBADF)
rc = -EACCES;
litest_assert_msg(rc == 0, "Failed to create uinput device: %s\n", strerror(-rc));
libevdev_free(dev);
devnode = libevdev_uinput_get_devnode(uinput);
litest_assert_notnull(devnode);
fd = open(devnode, O_RDONLY);
litest_assert_int_gt(fd, -1);
rc = libevdev_new_from_fd(fd, &dev);
litest_assert_int_eq(rc, 0);
/* uinput before kernel 4.5 + libevdev 1.5.0 does not support
* setting the resolution, so we set it afterwards. This is of
* course racy as hell but the way we _generally_ use this function
* by the time libinput uses the device, we're finished here.
*
* If you have kernel 4.5 and libevdev 1.5.0 or later, this code
* just keeps the room warm.
*/
abs = abs_info;
while (abs && abs->value != -1) {
if (abs->resolution != 0) {
if (libevdev_get_abs_resolution(dev, abs->value) ==
abs->resolution)
break;
rc = libevdev_kernel_set_abs_info(dev,
abs->value,
abs);
litest_assert_int_eq(rc, 0);
}
abs++;
}
close(fd);
libevdev_free(dev);
return uinput;
}
struct libevdev_uinput *
litest_create_uinput_device_from_description(const char *name,
const struct input_id *id,
const struct input_absinfo *abs_info,
const int *events)
{
struct libevdev_uinput *uinput;
const char *syspath;
char path[PATH_MAX];
struct udev_monitor *udev_monitor;
struct udev_device *udev_device;
udev_monitor = udev_setup_monitor();
uinput = litest_create_uinput(name, id, abs_info, events);
syspath = libevdev_uinput_get_syspath(uinput);
snprintf(path, sizeof(path), "%s/event", syspath);
udev_device = udev_wait_for_device_event(udev_monitor, "add", path);
litest_assert(udev_device_get_property_value(udev_device, "ID_INPUT"));
udev_device_unref(udev_device);
udev_monitor_unref(udev_monitor);
return uinput;
}
static struct libevdev_uinput *
litest_create_uinput_abs_device_v(const char *name,
struct input_id *id,
const struct input_absinfo *abs,
va_list args)
{
int events[KEY_MAX * 2 + 2]; /* increase this if not sufficient */
int *event = events;
int type, code;
while ((type = va_arg(args, int)) != -1 &&
(code = va_arg(args, int)) != -1) {
*event++ = type;
*event++ = code;
litest_assert(event < &events[ARRAY_LENGTH(events) - 2]);
}
*event++ = -1;
*event++ = -1;
return litest_create_uinput_device_from_description(name, id,
abs, events);
}
struct libevdev_uinput *
litest_create_uinput_abs_device(const char *name,
struct input_id *id,
const struct input_absinfo *abs,
...)
{
struct libevdev_uinput *uinput;
va_list args;
va_start(args, abs);
uinput = litest_create_uinput_abs_device_v(name, id, abs, args);
va_end(args);
return uinput;
}
struct libevdev_uinput *
litest_create_uinput_device(const char *name, struct input_id *id, ...)
{
struct libevdev_uinput *uinput;
va_list args;
va_start(args, id);
uinput = litest_create_uinput_abs_device_v(name, id, NULL, args);
va_end(args);
return uinput;
}
struct libinput_event_pointer*
litest_is_button_event(struct libinput_event *event,
unsigned int button,
enum libinput_button_state state)
{
struct libinput_event_pointer *ptrev;
enum libinput_event_type type = LIBINPUT_EVENT_POINTER_BUTTON;
litest_assert_ptr_notnull(event);
litest_assert_event_type(event, type);
ptrev = libinput_event_get_pointer_event(event);
litest_assert_int_eq(libinput_event_pointer_get_button(ptrev),
button);
litest_assert_int_eq(libinput_event_pointer_get_button_state(ptrev),
state);
return ptrev;
}
struct libinput_event_pointer *
litest_is_axis_event(struct libinput_event *event,
enum libinput_event_type axis_type,
enum libinput_pointer_axis axis,
enum libinput_pointer_axis_source source)
{
struct libinput_event_pointer *ptrev;
litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
litest_assert_ptr_notnull(event);
litest_assert_event_type_is_one_of(event,
LIBINPUT_EVENT_POINTER_AXIS,
axis_type);
ptrev = libinput_event_get_pointer_event(event);
litest_assert(libinput_event_pointer_has_axis(ptrev, axis));
if (source != 0)
litest_assert_int_eq(litest_event_pointer_get_axis_source(ptrev),
source);
return ptrev;
}
bool
litest_is_high_res_axis_event(struct libinput_event *event)
{
litest_assert_event_type_is_one_of(event,
LIBINPUT_EVENT_POINTER_AXIS,
LIBINPUT_EVENT_POINTER_SCROLL_WHEEL,
LIBINPUT_EVENT_POINTER_SCROLL_FINGER,
LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
return (libinput_event_get_type(event) != LIBINPUT_EVENT_POINTER_AXIS);
}
struct libinput_event_pointer *
litest_is_motion_event(struct libinput_event *event)
{
struct libinput_event_pointer *ptrev;
enum libinput_event_type type = LIBINPUT_EVENT_POINTER_MOTION;
double x, y, ux, uy;
litest_assert_ptr_notnull(event);
litest_assert_event_type(event, type);
ptrev = libinput_event_get_pointer_event(event);
x = libinput_event_pointer_get_dx(ptrev);
y = libinput_event_pointer_get_dy(ptrev);
ux = libinput_event_pointer_get_dx_unaccelerated(ptrev);
uy = libinput_event_pointer_get_dy_unaccelerated(ptrev);
/* No 0 delta motion events */
litest_assert(x != 0.0 || y != 0.0 ||
ux != 0.0 || uy != 0.0);
return ptrev;
}
void
litest_assert_key_event(struct libinput *li, unsigned int key,
enum libinput_key_state state)
{
struct libinput_event *event;
litest_wait_for_event(li);
event = libinput_get_event(li);
litest_is_keyboard_event(event, key, state);
libinput_event_destroy(event);
}
void
litest_assert_button_event(struct libinput *li, unsigned int button,
enum libinput_button_state state)
{
struct libinput_event *event;
litest_wait_for_event(li);
event = libinput_get_event(li);
litest_is_button_event(event, button, state);
libinput_event_destroy(event);
}
struct libinput_event_touch *
litest_is_touch_event(struct libinput_event *event,
enum libinput_event_type type)
{
struct libinput_event_touch *touch;
litest_assert_ptr_notnull(event);
if (type == 0)
type = libinput_event_get_type(event);
switch (type) {
case LIBINPUT_EVENT_TOUCH_DOWN:
case LIBINPUT_EVENT_TOUCH_UP:
case LIBINPUT_EVENT_TOUCH_MOTION:
case LIBINPUT_EVENT_TOUCH_FRAME:
case LIBINPUT_EVENT_TOUCH_CANCEL:
litest_assert_event_type(event, type);
break;
default:
ck_abort_msg("%s: invalid touch type %d\n", __func__, type);
}
touch = libinput_event_get_touch_event(event);
return touch;
}
struct libinput_event_keyboard *
litest_is_keyboard_event(struct libinput_event *event,
unsigned int key,
enum libinput_key_state state)
{
struct libinput_event_keyboard *kevent;
enum libinput_event_type type = LIBINPUT_EVENT_KEYBOARD_KEY;
litest_assert_ptr_notnull(event);
litest_assert_event_type(event, type);
kevent = libinput_event_get_keyboard_event(event);
litest_assert_ptr_notnull(kevent);
litest_assert_int_eq(libinput_event_keyboard_get_key(kevent), key);
litest_assert_int_eq(libinput_event_keyboard_get_key_state(kevent),
state);
return kevent;
}
struct libinput_event_gesture *
litest_is_gesture_event(struct libinput_event *event,
enum libinput_event_type type,
int nfingers)
{
struct libinput_event_gesture *gevent;
litest_assert_ptr_notnull(event);
litest_assert_event_type(event, type);
gevent = libinput_event_get_gesture_event(event);
litest_assert_ptr_notnull(gevent);
if (nfingers != -1)
litest_assert_int_eq(libinput_event_gesture_get_finger_count(gevent),
nfingers);
return gevent;
}
void
litest_assert_gesture_event(struct libinput *li,
enum libinput_event_type type,
int nfingers)
{
struct libinput_event *event;
litest_wait_for_event(li);
event = libinput_get_event(li);
litest_is_gesture_event(event, type, nfingers);
libinput_event_destroy(event);
}
struct libinput_event_tablet_tool *
litest_is_tablet_event(struct libinput_event *event,
enum libinput_event_type type)
{
struct libinput_event_tablet_tool *tevent;
litest_assert_ptr_notnull(event);
litest_assert_event_type(event, type);
tevent = libinput_event_get_tablet_tool_event(event);
litest_assert_ptr_notnull(tevent);
return tevent;
}
void
litest_assert_tablet_button_event(struct libinput *li, unsigned int button,
enum libinput_button_state state)
{
struct libinput_event *event;
struct libinput_event_tablet_tool *tev;
enum libinput_event_type type = LIBINPUT_EVENT_TABLET_TOOL_BUTTON;
litest_wait_for_event(li);
event = libinput_get_event(li);
litest_assert_notnull(event);
litest_assert_event_type(event, type);
tev = libinput_event_get_tablet_tool_event(event);
litest_assert_int_eq(libinput_event_tablet_tool_get_button(tev),
button);
litest_assert_int_eq(libinput_event_tablet_tool_get_button_state(tev),
state);
libinput_event_destroy(event);
}
struct libinput_event_tablet_tool *
litest_is_proximity_event(struct libinput_event *event,
enum libinput_tablet_tool_proximity_state state)
{
struct libinput_event_tablet_tool *tev;
enum libinput_event_type type = LIBINPUT_EVENT_TABLET_TOOL_PROXIMITY;
litest_assert_notnull(event);
litest_assert_event_type(event, type);
tev = libinput_event_get_tablet_tool_event(event);
litest_assert_int_eq(libinput_event_tablet_tool_get_proximity_state(tev),
state);
return tev;
}
double
litest_event_pointer_get_value(struct libinput_event_pointer *ptrev,
enum libinput_pointer_axis axis)
{
struct libinput_event *event;
enum libinput_event_type type;
event = libinput_event_pointer_get_base_event(ptrev);
type = libinput_event_get_type(event);
switch (type) {
case LIBINPUT_EVENT_POINTER_AXIS:
return libinput_event_pointer_get_axis_value(ptrev, axis);
case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
return libinput_event_pointer_get_scroll_value_v120(ptrev, axis);
case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
return libinput_event_pointer_get_scroll_value(ptrev, axis);
default:
abort();
}
}
enum libinput_pointer_axis_source
litest_event_pointer_get_axis_source(struct libinput_event_pointer *ptrev)
{
struct libinput_event *event;
enum libinput_event_type type;
event = libinput_event_pointer_get_base_event(ptrev);
type = libinput_event_get_type(event);
if (type == LIBINPUT_EVENT_POINTER_AXIS)
return libinput_event_pointer_get_axis_source(ptrev);
switch (type) {
case LIBINPUT_EVENT_POINTER_SCROLL_WHEEL:
return LIBINPUT_POINTER_AXIS_SOURCE_WHEEL;
case LIBINPUT_EVENT_POINTER_SCROLL_FINGER:
return LIBINPUT_POINTER_AXIS_SOURCE_FINGER;
case LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS:
return LIBINPUT_POINTER_AXIS_SOURCE_CONTINUOUS;
default:
abort();
}
}
void litest_assert_tablet_proximity_event(struct libinput *li,
enum libinput_tablet_tool_proximity_state state)
{
struct libinput_event *event;
litest_wait_for_event(li);
event = libinput_get_event(li);
litest_is_proximity_event(event, state);
libinput_event_destroy(event);
}
void litest_assert_tablet_tip_event(struct libinput *li,
enum libinput_tablet_tool_tip_state state)
{
struct libinput_event *event;
struct libinput_event_tablet_tool *tev;
enum libinput_event_type type = LIBINPUT_EVENT_TABLET_TOOL_TIP;
litest_wait_for_event(li);
event = libinput_get_event(li);
litest_assert_notnull(event);
litest_assert_event_type(event, type);
tev = libinput_event_get_tablet_tool_event(event);
litest_assert_int_eq(libinput_event_tablet_tool_get_tip_state(tev),
state);
libinput_event_destroy(event);
}
struct libinput_event_tablet_pad *
litest_is_pad_button_event(struct libinput_event *event,
unsigned int button,
enum libinput_button_state state)
{
struct libinput_event_tablet_pad *p;
enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_BUTTON;
litest_assert_ptr_notnull(event);
litest_assert_event_type(event, type);
p = libinput_event_get_tablet_pad_event(event);
litest_assert_ptr_notnull(p);
litest_assert_int_eq(libinput_event_tablet_pad_get_button_number(p),
button);
litest_assert_int_eq(libinput_event_tablet_pad_get_button_state(p),
state);
return p;
}
struct libinput_event_tablet_pad *
litest_is_pad_ring_event(struct libinput_event *event,
unsigned int number,
enum libinput_tablet_pad_ring_axis_source source)
{
struct libinput_event_tablet_pad *p;
enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_RING;
litest_assert_ptr_notnull(event);
litest_assert_event_type(event, type);
p = libinput_event_get_tablet_pad_event(event);
litest_assert_int_eq(libinput_event_tablet_pad_get_ring_number(p),
number);
litest_assert_int_eq(libinput_event_tablet_pad_get_ring_source(p),
source);
return p;
}
struct libinput_event_tablet_pad *
litest_is_pad_strip_event(struct libinput_event *event,
unsigned int number,
enum libinput_tablet_pad_strip_axis_source source)
{
struct libinput_event_tablet_pad *p;
enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_STRIP;
litest_assert_ptr_notnull(event);
litest_assert_event_type(event, type);
p = libinput_event_get_tablet_pad_event(event);
litest_assert_int_eq(libinput_event_tablet_pad_get_strip_number(p),
number);
litest_assert_int_eq(libinput_event_tablet_pad_get_strip_source(p),
source);
return p;
}
struct libinput_event_tablet_pad *
litest_is_pad_key_event(struct libinput_event *event,
unsigned int key,
enum libinput_key_state state)
{
struct libinput_event_tablet_pad *p;
enum libinput_event_type type = LIBINPUT_EVENT_TABLET_PAD_KEY;
litest_assert(event != NULL);
litest_assert_event_type(event, type);
p = libinput_event_get_tablet_pad_event(event);
litest_assert(p != NULL);
litest_assert_int_eq(libinput_event_tablet_pad_get_key(p), key);
litest_assert_int_eq(libinput_event_tablet_pad_get_key_state(p),
state);
return p;
}
struct libinput_event_switch *
litest_is_switch_event(struct libinput_event *event,
enum libinput_switch sw,
enum libinput_switch_state state)
{
struct libinput_event_switch *swev;
enum libinput_event_type type = LIBINPUT_EVENT_SWITCH_TOGGLE;
litest_assert_notnull(event);
litest_assert_event_type(event, type);
swev = libinput_event_get_switch_event(event);
litest_assert_int_eq(libinput_event_switch_get_switch(swev), sw);
litest_assert_int_eq(libinput_event_switch_get_switch_state(swev),
state);
return swev;
}
void
litest_assert_pad_button_event(struct libinput *li,
unsigned int button,
enum libinput_button_state state)
{
struct libinput_event *event;
litest_wait_for_event(li);
event = libinput_get_event(li);
litest_is_pad_button_event(event, button, state);
libinput_event_destroy(event);
}
void
litest_assert_pad_key_event(struct libinput *li,
unsigned int key,
enum libinput_key_state state)
{
struct libinput_event *event;
litest_wait_for_event(li);
event = libinput_get_event(li);
litest_is_pad_key_event(event, key, state);
libinput_event_destroy(event);
}
void
litest_assert_scroll(struct libinput *li,
enum libinput_event_type axis_type,
enum libinput_pointer_axis axis,
int minimum_movement)
{
struct libinput_event *event;
struct libinput_event_pointer *ptrev;
bool last_hi_res_event_found, last_low_res_event_found;
int value;
int nevents = 0;
litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
last_hi_res_event_found = false;
last_low_res_event_found = false;
event = libinput_get_event(li);
litest_assert_ptr_notnull(event);
while (event) {
int min = minimum_movement;
ptrev = litest_is_axis_event(event, axis_type, axis, 0);
nevents++;
/* Due to how the hysteresis works on touchpad
* events, the first event is reduced by the
* hysteresis margin that can cause the first event
* go under the minimum we expect for all other
* events */
if (nevents == 1)
min = minimum_movement/2;
value = litest_event_pointer_get_value(ptrev, axis);
if (litest_is_high_res_axis_event(event)) {
litest_assert(!last_hi_res_event_found);
if (axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL)
min *= 120;
if (value == 0)
last_hi_res_event_found = true;
} else {
litest_assert(!last_low_res_event_found);
if (value == 0)
last_low_res_event_found = true;
}
if (value != 0) {
if (minimum_movement > 0)
litest_assert_int_ge(value, min);
else
litest_assert_int_le(value, min);
}
libinput_event_destroy(event);
event = libinput_get_event(li);
}
litest_assert(last_low_res_event_found);
litest_assert(last_hi_res_event_found);
}
void
litest_assert_axis_end_sequence(struct libinput *li,
enum libinput_event_type axis_type,
enum libinput_pointer_axis axis,
enum libinput_pointer_axis_source source)
{
struct libinput_event *event;
struct libinput_event_pointer *ptrev;
bool last_hi_res_event_found, last_low_res_event_found;
double val;
int i;
litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
last_hi_res_event_found = false;
last_low_res_event_found = false;
/* both high and low scroll end events must be sent */
for (i = 0; i < 2; i++) {
event = libinput_get_event(li);
ptrev = litest_is_axis_event(event, axis_type, axis, source);
val = litest_event_pointer_get_value(ptrev, axis);
ck_assert(val == 0.0);
if (litest_is_high_res_axis_event(event)) {
litest_assert(!last_hi_res_event_found);
last_hi_res_event_found = true;
} else {
litest_assert(!last_low_res_event_found);
last_low_res_event_found = true;
}
libinput_event_destroy(event);
}
litest_assert(last_low_res_event_found);
litest_assert(last_hi_res_event_found);
}
void
litest_assert_only_typed_events(struct libinput *li,
enum libinput_event_type type)
{
struct libinput_event *event;
litest_assert(type != LIBINPUT_EVENT_NONE);
libinput_dispatch(li);
event = libinput_get_event(li);
litest_assert_notnull(event);
while (event) {
litest_assert_event_type(event, type);
libinput_event_destroy(event);
libinput_dispatch(li);
event = libinput_get_event(li);
}
}
void
litest_assert_only_axis_events(struct libinput *li,
enum libinput_event_type axis_type)
{
struct libinput_event *event;
litest_assert(axis_type == LIBINPUT_EVENT_POINTER_SCROLL_WHEEL ||
axis_type == LIBINPUT_EVENT_POINTER_SCROLL_FINGER ||
axis_type == LIBINPUT_EVENT_POINTER_SCROLL_CONTINUOUS);
libinput_dispatch(li);
event = libinput_get_event(li);
litest_assert_notnull(event);
while (event) {
litest_assert_event_type_is_one_of(event,
LIBINPUT_EVENT_POINTER_AXIS,
axis_type);
libinput_event_destroy(event);
libinput_dispatch(li);
event = libinput_get_event(li);
}
}
void
litest_assert_no_typed_events(struct libinput *li,
enum libinput_event_type type)
{
struct libinput_event *event;
litest_assert(type != LIBINPUT_EVENT_NONE);
libinput_dispatch(li);
event = libinput_get_event(li);
while (event) {
litest_assert_int_ne(libinput_event_get_type(event),
type);
libinput_event_destroy(event);
libinput_dispatch(li);
event = libinput_get_event(li);
}
}
void
litest_assert_touch_sequence(struct libinput *li)
{
struct libinput_event *event;
struct libinput_event_touch *tev;
int slot;
event = libinput_get_event(li);
tev = litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_DOWN);
slot = libinput_event_touch_get_slot(tev);
libinput_event_destroy(event);
event = libinput_get_event(li);
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
libinput_event_destroy(event);
event = libinput_get_event(li);
do {
tev = litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_MOTION);
litest_assert_int_eq(slot, libinput_event_touch_get_slot(tev));
libinput_event_destroy(event);
event = libinput_get_event(li);
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
libinput_event_destroy(event);
event = libinput_get_event(li);
litest_assert_notnull(event);
} while (libinput_event_get_type(event) != LIBINPUT_EVENT_TOUCH_UP);
tev = litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_UP);
litest_assert_int_eq(slot, libinput_event_touch_get_slot(tev));
libinput_event_destroy(event);
event = libinput_get_event(li);
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
libinput_event_destroy(event);
}
void
litest_assert_touch_motion_frame(struct libinput *li)
{
struct libinput_event *event;
/* expect at least one, but maybe more */
event = libinput_get_event(li);
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_MOTION);
libinput_event_destroy(event);
event = libinput_get_event(li);
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
libinput_event_destroy(event);
event = libinput_get_event(li);
while (event) {
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_MOTION);
libinput_event_destroy(event);
event = libinput_get_event(li);
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
libinput_event_destroy(event);
event = libinput_get_event(li);
}
}
void
litest_assert_touch_down_frame(struct libinput *li)
{
struct libinput_event *event;
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);
}
void
litest_assert_touch_up_frame(struct libinput *li)
{
struct libinput_event *event;
event = libinput_get_event(li);
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_UP);
libinput_event_destroy(event);
event = libinput_get_event(li);
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
libinput_event_destroy(event);
}
void
litest_assert_touch_cancel(struct libinput *li)
{
struct libinput_event *event;
event = libinput_get_event(li);
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_CANCEL);
libinput_event_destroy(event);
event = libinput_get_event(li);
litest_is_touch_event(event, LIBINPUT_EVENT_TOUCH_FRAME);
libinput_event_destroy(event);
}
void
litest_timeout_tap(void)
{
msleep(300);
}
void
litest_timeout_tapndrag(void)
{
msleep(520);
}
void
litest_timeout_debounce(void)
{
msleep(30);
}
void
litest_timeout_softbuttons(void)
{
msleep(300);
}
void
litest_timeout_buttonscroll(void)
{
msleep(300);
}
void
litest_timeout_finger_switch(void)
{
msleep(120);
}
void
litest_timeout_edgescroll(void)
{
msleep(300);
}
void
litest_timeout_middlebutton(void)
{
msleep(70);
}
void
litest_timeout_dwt_short(void)
{
msleep(220);
}
void
litest_timeout_dwt_long(void)
{
msleep(520);
}
void
litest_timeout_gesture(void)
{
msleep(120);
}
void
litest_timeout_gesture_scroll(void)
{
msleep(180);
}
void
litest_timeout_gesture_hold(void)
{
msleep(300);
}
void
litest_timeout_gesture_quick_hold(void)
{
msleep(60);
}
void
litest_timeout_trackpoint(void)
{
msleep(320);
}
void
litest_timeout_tablet_proxout(void)
{
msleep(170);
}
void
litest_timeout_touch_arbitration(void)
{
msleep(100);
}
void
litest_timeout_hysteresis(void)
{
msleep(90);
}
void
litest_push_event_frame(struct litest_device *dev)
{
litest_assert_int_ge(dev->skip_ev_syn, 0);
dev->skip_ev_syn++;
}
void
litest_pop_event_frame(struct litest_device *dev)
{
litest_assert_int_gt(dev->skip_ev_syn, 0);
dev->skip_ev_syn--;
if (dev->skip_ev_syn == 0)
litest_event(dev, EV_SYN, SYN_REPORT, 0);
}
void
litest_filter_event(struct litest_device *dev,
unsigned int type,
unsigned int code)
{
libevdev_disable_event_code(dev->evdev, type, code);
}
void
litest_unfilter_event(struct litest_device *dev,
unsigned int type,
unsigned int code)
{
/* would need an non-NULL argument for re-enabling, so simply abort
* until we need to be more sophisticated */
litest_assert_int_ne(type, (unsigned int)EV_ABS);
libevdev_enable_event_code(dev->evdev, type, code, NULL);
}
static void
send_abs_xy(struct litest_device *d, double x, double y)
{
struct input_event e;
int val;
e.type = EV_ABS;
e.code = ABS_X;
e.value = LITEST_AUTO_ASSIGN;
val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
litest_event(d, EV_ABS, ABS_X, val);
e.code = ABS_Y;
val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
litest_event(d, EV_ABS, ABS_Y, val);
}
static void
send_abs_mt_xy(struct litest_device *d, double x, double y)
{
struct input_event e;
int val;
e.type = EV_ABS;
e.code = ABS_MT_POSITION_X;
e.value = LITEST_AUTO_ASSIGN;
val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
litest_event(d, EV_ABS, ABS_MT_POSITION_X, val);
e.code = ABS_MT_POSITION_Y;
e.value = LITEST_AUTO_ASSIGN;
val = litest_auto_assign_value(d, &e, 0, x, y, NULL, true);
litest_event(d, EV_ABS, ABS_MT_POSITION_Y, val);
}
void
litest_semi_mt_touch_down(struct litest_device *d,
struct litest_semi_mt *semi_mt,
unsigned int slot,
double x, double y)
{
double t, l, r = 0, b = 0; /* top, left, right, bottom */
if (d->ntouches_down > 2 || slot > 1)
return;
if (d->ntouches_down == 1) {
l = x;
t = y;
} else {
int other = (slot + 1) % 2;
l = min(x, semi_mt->touches[other].x);
t = min(y, semi_mt->touches[other].y);
r = max(x, semi_mt->touches[other].x);
b = max(y, semi_mt->touches[other].y);
}
send_abs_xy(d, l, t);
litest_event(d, EV_ABS, ABS_MT_SLOT, 0);
if (d->ntouches_down == 1)
litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, ++semi_mt->tracking_id);
send_abs_mt_xy(d, l, t);
if (d->ntouches_down == 2) {
litest_event(d, EV_ABS, ABS_MT_SLOT, 1);
litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, ++semi_mt->tracking_id);
send_abs_mt_xy(d, r, b);
}
litest_event(d, EV_SYN, SYN_REPORT, 0);
semi_mt->touches[slot].x = x;
semi_mt->touches[slot].y = y;
}
void
litest_semi_mt_touch_move(struct litest_device *d,
struct litest_semi_mt *semi_mt,
unsigned int slot,
double x, double y)
{
double t, l, r = 0, b = 0; /* top, left, right, bottom */
if (d->ntouches_down > 2 || slot > 1)
return;
if (d->ntouches_down == 1) {
l = x;
t = y;
} else {
int other = (slot + 1) % 2;
l = min(x, semi_mt->touches[other].x);
t = min(y, semi_mt->touches[other].y);
r = max(x, semi_mt->touches[other].x);
b = max(y, semi_mt->touches[other].y);
}
send_abs_xy(d, l, t);
litest_event(d, EV_ABS, ABS_MT_SLOT, 0);
send_abs_mt_xy(d, l, t);
if (d->ntouches_down == 2) {
litest_event(d, EV_ABS, ABS_MT_SLOT, 1);
send_abs_mt_xy(d, r, b);
}
litest_event(d, EV_SYN, SYN_REPORT, 0);
semi_mt->touches[slot].x = x;
semi_mt->touches[slot].y = y;
}
void
litest_semi_mt_touch_up(struct litest_device *d,
struct litest_semi_mt *semi_mt,
unsigned int slot)
{
/* note: ntouches_down is decreased before we get here */
if (d->ntouches_down >= 2 || slot > 1)
return;
litest_event(d, EV_ABS, ABS_MT_SLOT, d->ntouches_down);
litest_event(d, EV_ABS, ABS_MT_TRACKING_ID, -1);
/* if we have one finger left, send x/y coords for that finger left.
this is likely to happen with a real touchpad */
if (d->ntouches_down == 1) {
int other = (slot + 1) % 2;
send_abs_xy(d, semi_mt->touches[other].x, semi_mt->touches[other].y);
litest_event(d, EV_ABS, ABS_MT_SLOT, 0);
send_abs_mt_xy(d, semi_mt->touches[other].x, semi_mt->touches[other].y);
}
litest_event(d, EV_SYN, SYN_REPORT, 0);
}
enum litest_mode {
LITEST_MODE_ERROR,
LITEST_MODE_TEST,
LITEST_MODE_LIST,
};
static inline enum litest_mode
litest_parse_argv(int argc, char **argv)
{
enum {
OPT_FILTER_TEST,
OPT_FILTER_DEVICE,
OPT_FILTER_GROUP,
OPT_FILTER_DEVICELESS,
OPT_XML_PREFIX,
OPT_JOBS,
OPT_LIST,
OPT_VERBOSE,
};
static const struct option opts[] = {
{ "filter-test", 1, 0, OPT_FILTER_TEST },
{ "filter-device", 1, 0, OPT_FILTER_DEVICE },
{ "filter-group", 1, 0, OPT_FILTER_GROUP },
{ "filter-deviceless", 0, 0, OPT_FILTER_DEVICELESS },
{ "xml-output", 1, 0, OPT_XML_PREFIX },
{ "jobs", 1, 0, OPT_JOBS },
{ "list", 0, 0, OPT_LIST },
{ "verbose", 0, 0, OPT_VERBOSE },
{ "help", 0, 0, 'h'},
{ 0, 0, 0, 0}
};
enum {
JOBS_DEFAULT,
JOBS_SINGLE,
JOBS_CUSTOM
} want_jobs = JOBS_DEFAULT;
char *builddir;
char *jobs_env;
/* If we are not running from the builddir, we assume we're running
* against the system as installed */
builddir = builddir_lookup();
if (!builddir)
use_system_rules_quirks = true;
free(builddir);
if (in_debugger)
want_jobs = JOBS_SINGLE;
if ((jobs_env = getenv("LITEST_JOBS"))) {
if (!safe_atoi(jobs_env, &jobs)) {
fprintf(stderr, "LITEST_JOBS environment variable must be positive integer\n");
exit(EXIT_FAILURE);
}
}
while(1) {
int c;
int option_index = 0;
c = getopt_long(argc, argv, "j:", opts, &option_index);
if (c == -1)
break;
switch(c) {
default:
case 'h':
printf("Usage: %s [--verbose] [--jobs] [--filter-...]\n"
"\n"
"Options:\n"
" --filter-test=.... \n"
" Glob to filter on test names\n"
" --filter-device=.... \n"
" Glob to filter on device names\n"
" --filter-group=.... \n"
" Glob to filter on test groups\n"
" --filter-deviceless=.... \n"
" Glob to filter on tests that do not create test devices\n"
" --xml-output=/path/to/file-XXXXXXX.xml\n"
" Write test output in libcheck's XML format\n"
" to the given files. The file must match the format\n"
" prefix-XXXXXX.xml and only the prefix is your choice.\n"
" --verbose\n"
" Enable verbose output\n"
" --jobs 8\n"
" Number of parallel test suites to run (default: 8).\n"
" This overrides the LITEST_JOBS environment variable.\n"
" --list\n"
" List all tests\n"
"\n"
"See the libinput-test-suite(1) man page for details.\n",
program_invocation_short_name);
exit(c != 'h');
break;
case OPT_FILTER_TEST:
filter_test = optarg;
if (want_jobs == JOBS_DEFAULT)
want_jobs = JOBS_SINGLE;
break;
case OPT_FILTER_DEVICE:
filter_device = optarg;
if (want_jobs == JOBS_DEFAULT)
want_jobs = JOBS_SINGLE;
break;
case OPT_FILTER_GROUP:
filter_group = optarg;
break;
case OPT_XML_PREFIX:
xml_prefix = optarg;
break;
case 'j':
case OPT_JOBS:
jobs = atoi(optarg);
want_jobs = JOBS_CUSTOM;
break;
case OPT_LIST:
return LITEST_MODE_LIST;
case OPT_VERBOSE:
verbose = true;
break;
case OPT_FILTER_DEVICELESS:
run_deviceless = true;
break;
}
}
if (want_jobs == JOBS_SINGLE)
jobs = 1;
return LITEST_MODE_TEST;
}
#ifndef LITEST_NO_MAIN
static bool
is_debugger_attached(void)
{
int status;
bool rc;
int pid = fork();
if (pid == -1)
return 0;
if (pid == 0) {
int ppid = getppid();
if (ptrace(PTRACE_ATTACH, ppid, NULL, 0) == 0) {
waitpid(ppid, NULL, 0);
ptrace(PTRACE_CONT, ppid, NULL, 0);
ptrace(PTRACE_DETACH, ppid, NULL, 0);
rc = false;
} else {
rc = true;
}
_exit(rc);
} else {
waitpid(pid, &status, 0);
rc = WEXITSTATUS(status);
}
return !!rc;
}
static void
litest_list_tests(struct list *tests)
{
struct suite *s;
const char *last_test_name = NULL;
list_for_each(s, tests, node) {
struct test *t;
printf("%s:\n", s->name);
list_for_each(t, &s->tests, node) {
if (!last_test_name ||
!streq(last_test_name, t->name))
printf(" %s:\n", t->name);
last_test_name = t->name;
printf(" %s\n", t->devname);
}
}
}
extern const struct test_device __start_test_section, __stop_test_section;
static void
litest_init_test_devices(void)
{
const struct test_device *t;
list_init(&devices);
for (t = &__start_test_section; t < &__stop_test_section; t++)
list_append(&devices, &t->device->node);
}
extern const struct test_collection __start_test_collection_section,
__stop_test_collection_section;
static void
setup_tests(void)
{
const struct test_collection *c;
for (c = &__start_test_collection_section;
c < &__stop_test_collection_section;
c++) {
c->setup();
}
}
static int
check_device_access(void)
{
if (getuid() != 0) {
fprintf(stderr,
"%s must be run as root.\n",
program_invocation_short_name);
return 77;
}
if (access("/dev/uinput", F_OK) == -1 &&
access("/dev/input/uinput", F_OK) == -1) {
fprintf(stderr,
"uinput device is missing, skipping tests.\n");
return 77;
}
return 0;
}
static int
disable_tty(void)
{
int tty_mode = -1;
/* If we're running 'normally' on the VT, disable the keyboard to
* avoid messing up our host. But if we're inside gdb or running
* without forking, leave it as-is.
*/
if (!run_deviceless &&
jobs > 1 &&
!in_debugger &&
getenv("CK_FORK") == NULL &&
isatty(STDIN_FILENO) &&
ioctl(STDIN_FILENO, KDGKBMODE, &tty_mode) == 0) {
#ifdef __linux__
ioctl(STDIN_FILENO, KDSKBMODE, K_OFF);
#elif __FreeBSD__
ioctl(STDIN_FILENO, KDSKBMODE, K_RAW);
/* Put the tty into raw mode */
struct termios tios;
if (tcgetattr(STDIN_FILENO, &tios))
fprintf(stderr, "Failed to get terminal attribute: %d - %s\n", errno, strerror(errno));
cfmakeraw(&tios);
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios))
fprintf(stderr, "Failed to set terminal attribute: %d - %s\n", errno, strerror(errno));
#endif
}
return tty_mode;
}
int
main(int argc, char **argv)
{
const struct rlimit corelimit = { 0, 0 };
enum litest_mode mode;
int tty_mode = -1;
int failed_tests;
int rc;
const char *meson_testthreads;
in_debugger = is_debugger_attached();
if (in_debugger || RUNNING_ON_VALGRIND)
setenv("CK_FORK", "no", 0);
if ((meson_testthreads = getenv("MESON_TESTTHREADS")) == NULL ||
!safe_atoi(meson_testthreads, &jobs)) {
jobs = get_nprocs();
if (!RUNNING_ON_VALGRIND)
jobs *= 2;
}
mode = litest_parse_argv(argc, argv);
if (mode == LITEST_MODE_ERROR)
return EXIT_FAILURE;
litest_init_test_devices();
list_init(&all_tests);
setup_tests();
if (mode == LITEST_MODE_LIST) {
litest_list_tests(&all_tests);
return EXIT_SUCCESS;
}
if (!run_deviceless && (rc = check_device_access()) != 0)
return rc;
setenv("CK_DEFAULT_TIMEOUT", "30", 0);
setenv("LIBINPUT_RUNNING_TEST_SUITE", "1", 1);
if (setrlimit(RLIMIT_CORE, &corelimit) != 0)
perror("WARNING: Core dumps not disabled");
tty_mode = disable_tty();
failed_tests = litest_run(argc, argv);
if (tty_mode != -1) {
ioctl(STDIN_FILENO, KDSKBMODE, tty_mode);
#ifdef __FreeBSD__
/* Put the tty into "sane" mode */
struct termios tios;
if (tcgetattr(STDIN_FILENO, &tios))
fprintf(stderr, "Failed to get terminal attribute: %d - %s\n", errno, strerror(errno));
cfmakesane(&tios);
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios))
fprintf(stderr, "Failed to set terminal attribute: %d - %s\n", errno, strerror(errno));
#endif
}
return min(failed_tests, 255);
}
#endif
|