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
|
/*
* Copyright © 2004-2008 Red Hat, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software
* and its documentation for any purpose is hereby granted without
* fee, provided that the above copyright notice appear in all copies
* and that both that copyright notice and this permission notice
* appear in supporting documentation, and that the name of Red Hat
* not be used in advertising or publicity pertaining to distribution
* of the software without specific, written prior permission. Red
* Hat makes no representations about the suitability of this software
* for any purpose. It is provided "as is" without express or implied
* warranty.
*
* THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
* NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Authors:
* Kristian Høgsberg (krh@redhat.com)
* Adam Jackson (ajax@redhat.com)
* Peter Hutterer (peter.hutterer@redhat.com)
* Oliver McFadden (oliver.mcfadden@nokia.com)
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "evdev.h"
#include "axis_labels.h"
#include <X11/keysym.h>
#include <X11/extensions/XI.h>
#include <linux/version.h>
#include <sys/stat.h>
#include <libudev.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <xf86.h>
#include <xf86Xinput.h>
#include <exevents.h>
#include <xorgVersion.h>
#include <xkbsrv.h>
#include <X11/Xatom.h>
#include <evdev-properties.h>
#include <xserver-properties.h>
#ifndef XI_PROP_PRODUCT_ID
#define XI_PROP_PRODUCT_ID "Device Product ID"
#endif
#ifndef XI_PROP_VIRTUAL_DEVICE
#define XI_PROP_VIRTUAL_DEVICE "Virtual Device"
#endif
/* removed from server, purge when dropping support for server 1.10 */
#define XI86_SEND_DRAG_EVENTS 0x08
#ifndef MAXDEVICES
#include <inputstr.h> /* for MAX_DEVICES */
#define MAXDEVICES MAX_DEVICES
#endif
#define ArrayLength(a) (sizeof(a) / (sizeof((a)[0])))
#define MIN_KEYCODE 8
#define GLYPHS_PER_KEY 2
#define AltMask Mod1Mask
#define NumLockMask Mod2Mask
#define AltLangMask Mod3Mask
#define KanaMask Mod4Mask
#define ScrollLockMask Mod5Mask
#define CAPSFLAG 1
#define NUMFLAG 2
#define SCROLLFLAG 4
#define MODEFLAG 8
#define COMPOSEFLAG 16
#ifndef ABS_MT_SLOT
#define ABS_MT_SLOT 0x2f
#endif
#ifndef ABS_MT_TRACKING_ID
#define ABS_MT_TRACKING_ID 0x39
#endif
static const char *evdevDefaults[] = {
"XkbRules", "evdev",
"XkbModel", "pc104", /* the right model for 'us' */
"XkbLayout", "us",
NULL
};
/* Any of those triggers a proximity event */
static int proximity_bits[] = {
BTN_TOOL_PEN,
BTN_TOOL_RUBBER,
BTN_TOOL_BRUSH,
BTN_TOOL_PENCIL,
BTN_TOOL_AIRBRUSH,
BTN_TOOL_FINGER,
BTN_TOOL_MOUSE,
BTN_TOOL_LENS,
};
static int EvdevOn(DeviceIntPtr);
static int EvdevCache(InputInfoPtr pInfo);
static void EvdevKbdCtrl(DeviceIntPtr device, KeybdCtrl *ctrl);
static int EvdevSwitchMode(ClientPtr client, DeviceIntPtr device, int mode);
static BOOL EvdevGrabDevice(InputInfoPtr pInfo, int grab, int ungrab);
static void EvdevSetCalibration(InputInfoPtr pInfo, int num_calibration, int calibration[4]);
static int EvdevOpenDevice(InputInfoPtr pInfo);
static void EvdevCloseDevice(InputInfoPtr pInfo);
static void EvdevInitAxesLabels(EvdevPtr pEvdev, int mode, int natoms, Atom *atoms);
static void EvdevInitOneAxisLabel(EvdevPtr pEvdev, int mapped_axis,
const char **labels, int label_idx, Atom *atoms);
static void EvdevInitButtonLabels(EvdevPtr pEvdev, int natoms, Atom *atoms);
static void EvdevInitProperty(DeviceIntPtr dev);
static int EvdevSetProperty(DeviceIntPtr dev, Atom atom,
XIPropertyValuePtr val, BOOL checkonly);
static Atom prop_product_id;
static Atom prop_invert;
static Atom prop_calibration;
static Atom prop_swap;
static Atom prop_axis_label;
static Atom prop_btn_label;
static Atom prop_device;
static Atom prop_virtual;
/* All devices the evdev driver has allocated and knows about.
* MAXDEVICES is safe as null-terminated array, as two devices (VCP and VCK)
* cannot be used by evdev, leaving us with a space of 2 at the end. */
static EvdevPtr evdev_devices[MAXDEVICES] = {NULL};
static int EvdevSwitchMode(ClientPtr client, DeviceIntPtr device, int mode)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
if (pEvdev->flags & EVDEV_RELATIVE_EVENTS)
{
if (mode == Relative)
return Success;
else
return XI_BadMode;
}
switch (mode) {
case Absolute:
pEvdev->flags &= ~EVDEV_RELATIVE_MODE;
break;
case Relative:
pEvdev->flags |= EVDEV_RELATIVE_MODE;
break;
default:
return XI_BadMode;
}
return Success;
}
static size_t EvdevCountBits(unsigned long *array, size_t nlongs)
{
unsigned int i;
size_t count = 0;
for (i = 0; i < nlongs; i++) {
unsigned long x = array[i];
while (x > 0)
{
count += (x & 0x1);
x >>= 1;
}
}
return count;
}
static inline int EvdevBitIsSet(const unsigned long *array, int bit)
{
return !!(array[bit / LONG_BITS] & (1LL << (bit % LONG_BITS)));
}
static inline void EvdevSetBit(unsigned long *array, int bit)
{
array[bit / LONG_BITS] |= (1LL << (bit % LONG_BITS));
}
static int
EvdevGetMajorMinor(InputInfoPtr pInfo)
{
struct stat st;
if (fstat(pInfo->fd, &st) == -1)
{
xf86IDrvMsg(pInfo, X_ERROR, "stat failed (%s). cannot check for duplicates.\n",
strerror(errno));
return 0;
}
return st.st_rdev;
}
/**
* Return TRUE if one of the devices we know about has the same min/maj
* number.
*/
static BOOL
EvdevIsDuplicate(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
EvdevPtr* dev = evdev_devices;
if (pEvdev->min_maj)
{
while(*dev)
{
if ((*dev) != pEvdev &&
(*dev)->min_maj &&
(*dev)->min_maj == pEvdev->min_maj)
return TRUE;
dev++;
}
}
return FALSE;
}
/**
* Add to internal device list.
*/
static void
EvdevAddDevice(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
EvdevPtr* dev = evdev_devices;
while(*dev)
dev++;
*dev = pEvdev;
}
/**
* Remove from internal device list.
*/
static void
EvdevRemoveDevice(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
EvdevPtr *dev = evdev_devices;
int count = 0;
while(*dev)
{
count++;
if (*dev == pEvdev)
{
memmove(dev, dev + 1,
sizeof(evdev_devices) - (count * sizeof(EvdevPtr)));
break;
}
dev++;
}
}
static BOOL
EvdevDeviceIsVirtual(const char* devicenode)
{
struct udev *udev = NULL;
struct udev_device *device = NULL;
struct stat st;
int rc = FALSE;
const char *devpath;
udev = udev_new();
if (!udev)
goto out;
if (stat(devicenode, &st) == -1)
goto out;
device = udev_device_new_from_devnum(udev, 'c', st.st_rdev);
if (!device)
goto out;
devpath = udev_device_get_devpath(device);
if (!devpath)
goto out;
if (strstr(devpath, "LNXSYSTM"))
rc = TRUE;
out:
udev_device_unref(device);
udev_unref(udev);
return rc;
}
#ifndef HAVE_SMOOTH_SCROLLING
static int wheel_up_button = 4;
static int wheel_down_button = 5;
static int wheel_left_button = 6;
static int wheel_right_button = 7;
#endif
static EventQueuePtr
EvdevNextInQueue(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
if (pEvdev->num_queue >= EVDEV_MAXQUEUE)
{
LogMessageVerbSigSafe(X_WARNING, 0, "dropping event due to full queue!\n");
return NULL;
}
pEvdev->num_queue++;
return &pEvdev->queue[pEvdev->num_queue - 1];
}
void
EvdevQueueKbdEvent(InputInfoPtr pInfo, struct input_event *ev, int value)
{
int code = ev->code + MIN_KEYCODE;
EventQueuePtr pQueue;
/* Filter all repeated events from device.
We'll do softrepeat in the server, but only since 1.6 */
if (value == 2)
return;
if ((pQueue = EvdevNextInQueue(pInfo)))
{
pQueue->type = EV_QUEUE_KEY;
pQueue->detail.key = code;
pQueue->val = value;
}
}
void
EvdevQueueButtonEvent(InputInfoPtr pInfo, int button, int value)
{
EventQueuePtr pQueue;
if ((pQueue = EvdevNextInQueue(pInfo)))
{
pQueue->type = EV_QUEUE_BTN;
pQueue->detail.key = button;
pQueue->val = value;
}
}
void
EvdevQueueProximityEvent(InputInfoPtr pInfo, int value)
{
EventQueuePtr pQueue;
if ((pQueue = EvdevNextInQueue(pInfo)))
{
pQueue->type = EV_QUEUE_PROXIMITY;
pQueue->detail.key = 0;
pQueue->val = value;
}
}
#ifdef MULTITOUCH
void
EvdevQueueTouchEvent(InputInfoPtr pInfo, unsigned int touch, ValuatorMask *mask,
uint16_t evtype)
{
EventQueuePtr pQueue;
if ((pQueue = EvdevNextInQueue(pInfo)))
{
pQueue->type = EV_QUEUE_TOUCH;
pQueue->detail.touch = touch;
valuator_mask_copy(pQueue->touchMask, mask);
pQueue->val = evtype;
}
}
#endif
/**
* Post button event right here, right now.
* Interface for MB emulation since these need to post immediately.
*/
void
EvdevPostButtonEvent(InputInfoPtr pInfo, int button, enum ButtonAction act)
{
xf86PostButtonEvent(pInfo->dev, Relative, button,
(act == BUTTON_PRESS) ? 1 : 0, 0, 0);
}
void
EvdevQueueButtonClicks(InputInfoPtr pInfo, int button, int count)
{
int i;
for (i = 0; i < count; i++) {
EvdevQueueButtonEvent(pInfo, button, 1);
EvdevQueueButtonEvent(pInfo, button, 0);
}
}
static void
EvdevSwapAbsValuators(EvdevPtr pEvdev, ValuatorMask *mask)
{
int i;
int swapped_isset[2] = {0, 0};
int swapped_values[2];
if (!pEvdev->swap_axes)
return;
for(i = 0; i <= 1; i++) {
if (valuator_mask_isset(mask, i)) {
swapped_isset[1 - i] = 1;
swapped_values[1 - i] =
xf86ScaleAxis(valuator_mask_get(mask, i),
pEvdev->absinfo[1 - i].maximum,
pEvdev->absinfo[1 - i].minimum,
pEvdev->absinfo[i].maximum,
pEvdev->absinfo[i].minimum);
}
}
for (i = 0; i <= 1; i++) {
if (swapped_isset[i])
valuator_mask_set(mask, i, swapped_values[i]);
else
valuator_mask_unset(mask, i);
}
}
static void
EvdevApplyCalibration(EvdevPtr pEvdev, ValuatorMask *mask)
{
int i;
for (i = 0; i <= 1; i++) {
int val;
int calib_min;
int calib_max;
if (!valuator_mask_isset(mask, i))
continue;
val = valuator_mask_get(mask, i);
if (i == 0) {
calib_min = pEvdev->calibration.min_x;
calib_max = pEvdev->calibration.max_x;
} else {
calib_min = pEvdev->calibration.min_y;
calib_max = pEvdev->calibration.max_y;
}
if (pEvdev->flags & EVDEV_CALIBRATED)
val = xf86ScaleAxis(val, pEvdev->absinfo[i].maximum,
pEvdev->absinfo[i].minimum, calib_max,
calib_min);
if ((i == 0 && pEvdev->invert_x) || (i == 1 && pEvdev->invert_y))
val = (pEvdev->absinfo[i].maximum - val +
pEvdev->absinfo[i].minimum);
valuator_mask_set(mask, i, val);
}
}
/**
* Take the valuators and process them accordingly.
*/
static void
EvdevProcessValuators(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
int *delta = pEvdev->delta;
/* convert to relative motion for touchpads */
if (pEvdev->abs_queued && (pEvdev->flags & EVDEV_RELATIVE_MODE)) {
if (pEvdev->in_proximity) {
if (valuator_mask_isset(pEvdev->vals, 0))
{
if (valuator_mask_isset(pEvdev->old_vals, 0))
delta[REL_X] = valuator_mask_get(pEvdev->vals, 0) -
valuator_mask_get(pEvdev->old_vals, 0);
valuator_mask_set(pEvdev->old_vals, 0,
valuator_mask_get(pEvdev->vals, 0));
}
if (valuator_mask_isset(pEvdev->vals, 1))
{
if (valuator_mask_isset(pEvdev->old_vals, 1))
delta[REL_Y] = valuator_mask_get(pEvdev->vals, 1) -
valuator_mask_get(pEvdev->old_vals, 1);
valuator_mask_set(pEvdev->old_vals, 1,
valuator_mask_get(pEvdev->vals, 1));
}
} else {
valuator_mask_zero(pEvdev->old_vals);
}
valuator_mask_zero(pEvdev->vals);
pEvdev->abs_queued = 0;
pEvdev->rel_queued = 1;
}
if (pEvdev->rel_queued) {
int tmp;
int i;
if (pEvdev->swap_axes) {
tmp = pEvdev->delta[REL_X];
pEvdev->delta[REL_X] = pEvdev->delta[REL_Y];
pEvdev->delta[REL_Y] = tmp;
if (pEvdev->delta[REL_X] == 0)
valuator_mask_unset(pEvdev->vals, REL_X);
if (pEvdev->delta[REL_Y] == 0)
valuator_mask_unset(pEvdev->vals, REL_Y);
}
if (pEvdev->invert_x)
pEvdev->delta[REL_X] *= -1;
if (pEvdev->invert_y)
pEvdev->delta[REL_Y] *= -1;
Evdev3BEmuProcessRelMotion(pInfo,
pEvdev->delta[REL_X],
pEvdev->delta[REL_Y]);
for (i = 0; i < REL_CNT; i++)
{
int map = pEvdev->rel_axis_map[i];
if (pEvdev->delta[i] && map != -1)
valuator_mask_set(pEvdev->vals, map, pEvdev->delta[i]);
}
}
/*
* Some devices only generate valid abs coords when BTN_TOOL_PEN is
* pressed. On wacom tablets, this means that the pen is in
* proximity of the tablet. After the pen is removed, BTN_TOOL_PEN is
* released, and a (0, 0) absolute event is generated. Checking
* pEvdev->in_proximity here lets us ignore that event. pEvdev is
* initialized to 1 so devices that don't use this scheme still
* just works.
*/
else if (pEvdev->abs_queued && pEvdev->in_proximity) {
EvdevSwapAbsValuators(pEvdev, pEvdev->vals);
EvdevApplyCalibration(pEvdev, pEvdev->vals);
Evdev3BEmuProcessAbsMotion(pInfo, pEvdev->vals);
}
}
static void
EvdevProcessProximityEvent(InputInfoPtr pInfo, struct input_event *ev)
{
EvdevPtr pEvdev = pInfo->private;
if (!pEvdev->use_proximity)
return;
pEvdev->prox_queued = 1;
EvdevQueueProximityEvent(pInfo, ev->value);
}
/**
* Proximity handling is rather weird because of tablet-specific issues.
* Some tablets, notably Wacoms, send a 0/0 coordinate in the same EV_SYN as
* the out-of-proximity notify. We need to ignore those, hence we only
* actually post valuator events when we're in proximity.
*
* Other tablets send the x/y coordinates, then EV_SYN, then the proximity
* event. For those, we need to remember x/y to post it when the proximity
* comes.
*
* If we're not in proximity and we get valuator events, remember that, they
* won't be posted though. If we move into proximity without valuators, use
* the last ones we got and let the rest of the code post them.
*/
static int
EvdevProcessProximityState(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
int prox_state = 0;
int i;
/* Does this device have any proximity axes? */
if (!pEvdev->prox)
return 0;
/* no proximity change in the queue */
if (!pEvdev->prox_queued)
{
if (pEvdev->abs_queued && !pEvdev->in_proximity)
for (i = 0; i < valuator_mask_size(pEvdev->vals); i++)
if (valuator_mask_isset(pEvdev->vals, i))
valuator_mask_set(pEvdev->prox, i,
valuator_mask_get(pEvdev->vals, i));
return 0;
}
for (i = 0; i < pEvdev->num_queue; i++)
{
if (pEvdev->queue[i].type == EV_QUEUE_PROXIMITY)
{
prox_state = pEvdev->queue[i].val;
break;
}
}
if ((prox_state && !pEvdev->in_proximity) ||
(!prox_state && pEvdev->in_proximity))
{
/* We're about to go into/out of proximity but have no abs events
* within the EV_SYN. Use the last coordinates we have. */
for (i = 0; i < valuator_mask_size(pEvdev->prox); i++)
if (!valuator_mask_isset(pEvdev->vals, i) &&
valuator_mask_isset(pEvdev->prox, i))
valuator_mask_set(pEvdev->vals, i,
valuator_mask_get(pEvdev->prox, i));
valuator_mask_zero(pEvdev->prox);
pEvdev->abs_queued = valuator_mask_size(pEvdev->vals);
}
pEvdev->in_proximity = prox_state;
return 1;
}
/**
* Take a button input event and process it accordingly.
*/
static void
EvdevProcessButtonEvent(InputInfoPtr pInfo, struct input_event *ev)
{
unsigned int button;
int value;
EvdevPtr pEvdev = pInfo->private;
button = EvdevUtilButtonEventToButtonNumber(pEvdev, ev->code);
/* Get the signed value, earlier kernels had this as unsigned */
value = ev->value;
/* Handle drag lock */
if (EvdevDragLockFilterEvent(pInfo, button, value))
return;
if (EvdevWheelEmuFilterButton(pInfo, button, value))
return;
if (EvdevMBEmuFilterEvent(pInfo, button, value))
return;
if (button)
EvdevQueueButtonEvent(pInfo, button, value);
else
EvdevQueueKbdEvent(pInfo, ev, value);
}
/**
* Take the relative motion input event and process it accordingly.
*/
static void
EvdevProcessRelativeMotionEvent(InputInfoPtr pInfo, struct input_event *ev)
{
int value;
EvdevPtr pEvdev = pInfo->private;
int map;
/* Get the signed value, earlier kernels had this as unsigned */
value = ev->value;
switch (ev->code) {
#ifndef HAVE_SMOOTH_SCROLLING
case REL_WHEEL:
if (value > 0)
EvdevQueueButtonClicks(pInfo, wheel_up_button, value);
else if (value < 0)
EvdevQueueButtonClicks(pInfo, wheel_down_button, -value);
break;
case REL_DIAL:
case REL_HWHEEL:
if (value > 0)
EvdevQueueButtonClicks(pInfo, wheel_right_button, value);
else if (value < 0)
EvdevQueueButtonClicks(pInfo, wheel_left_button, -value);
break;
/* We don't post wheel events as axis motion. */
#endif
default:
/* Ignore EV_REL events if we never set up for them. */
if (!(pEvdev->flags & EVDEV_RELATIVE_EVENTS) &&
ev->code != REL_WHEEL && ev->code != REL_DIAL &&
ev->code != REL_HWHEEL)
return;
/* Handle mouse wheel emulation */
if (EvdevWheelEmuFilterMotion(pInfo, ev))
return;
pEvdev->rel_queued = 1;
pEvdev->delta[ev->code] += value;
map = pEvdev->rel_axis_map[ev->code];
valuator_mask_set(pEvdev->vals, map, value);
break;
}
}
#ifdef MULTITOUCH
static void
EvdevProcessTouch(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
int type;
if (pEvdev->cur_slot < 0 || !pEvdev->mt_mask)
return;
/* If the ABS_MT_SLOT is the first event we get after EV_SYN, skip this */
if (pEvdev->slot_state == SLOTSTATE_EMPTY)
return;
if (pEvdev->slot_state == SLOTSTATE_CLOSE)
type = XI_TouchEnd;
else if (pEvdev->slot_state == SLOTSTATE_OPEN)
type = XI_TouchBegin;
else
type = XI_TouchUpdate;
EvdevSwapAbsValuators(pEvdev, pEvdev->mt_mask);
EvdevApplyCalibration(pEvdev, pEvdev->mt_mask);
EvdevQueueTouchEvent(pInfo, pEvdev->cur_slot, pEvdev->mt_mask, type);
pEvdev->slot_state = SLOTSTATE_EMPTY;
valuator_mask_zero(pEvdev->mt_mask);
}
static int
num_slots(EvdevPtr pEvdev)
{
int value = pEvdev->absinfo[ABS_MT_SLOT].maximum -
pEvdev->absinfo[ABS_MT_SLOT].minimum + 1;
/* If we don't know how many slots there are, assume at least 10 */
return value > 1 ? value : 10;
}
static int
last_mt_vals_slot(EvdevPtr pEvdev)
{
int value = pEvdev->cur_slot - pEvdev->absinfo[ABS_MT_SLOT].minimum;
return value < num_slots(pEvdev) ? value : -1;
}
static void
EvdevProcessTouchEvent(InputInfoPtr pInfo, struct input_event *ev)
{
EvdevPtr pEvdev = pInfo->private;
int map;
if (!pEvdev->mtdev)
return;
if (ev->code == ABS_MT_SLOT) {
EvdevProcessTouch(pInfo);
pEvdev->cur_slot = ev->value;
} else
{
int slot_index = last_mt_vals_slot(pEvdev);
if (pEvdev->slot_state == SLOTSTATE_EMPTY)
pEvdev->slot_state = SLOTSTATE_UPDATE;
if (ev->code == ABS_MT_TRACKING_ID) {
if (ev->value >= 0) {
pEvdev->slot_state = SLOTSTATE_OPEN;
if (slot_index >= 0)
valuator_mask_copy(pEvdev->mt_mask,
pEvdev->last_mt_vals[slot_index]);
else
LogMessageVerbSigSafe(X_WARNING, 0,
"%s: Attempted to copy values from out-of-range "
"slot, touch events may be incorrect.\n",
pInfo->name);
} else
pEvdev->slot_state = SLOTSTATE_CLOSE;
} else {
map = pEvdev->abs_axis_map[ev->code];
valuator_mask_set(pEvdev->mt_mask, map, ev->value);
if (slot_index >= 0)
valuator_mask_set(pEvdev->last_mt_vals[slot_index], map,
ev->value);
}
}
}
#else
#define EvdevProcessTouch(pInfo)
#define EvdevProcessTouchEvent(pInfo, ev)
#endif /* MULTITOUCH */
/**
* Take the absolute motion input event and process it accordingly.
*/
static void
EvdevProcessAbsoluteMotionEvent(InputInfoPtr pInfo, struct input_event *ev)
{
int value;
EvdevPtr pEvdev = pInfo->private;
int map;
/* Get the signed value, earlier kernels had this as unsigned */
value = ev->value;
/* Ignore EV_ABS events if we never set up for them. */
if (!(pEvdev->flags & EVDEV_ABSOLUTE_EVENTS))
return;
if (ev->code > ABS_MAX)
return;
if (EvdevWheelEmuFilterMotion(pInfo, ev))
return;
if (ev->code >= ABS_MT_SLOT) {
EvdevProcessTouchEvent(pInfo, ev);
pEvdev->abs_queued = 1;
} else if (!pEvdev->mt_mask) {
map = pEvdev->abs_axis_map[ev->code];
valuator_mask_set(pEvdev->vals, map, value);
pEvdev->abs_queued = 1;
}
}
/**
* Take the key press/release input event and process it accordingly.
*/
static void
EvdevProcessKeyEvent(InputInfoPtr pInfo, struct input_event *ev)
{
int value, i;
EvdevPtr pEvdev = pInfo->private;
/* Get the signed value, earlier kernels had this as unsigned */
value = ev->value;
/* don't repeat mouse buttons */
if (ev->code >= BTN_MOUSE && ev->code < KEY_OK)
if (value == 2)
return;
for (i = 0; i < ArrayLength(proximity_bits); i++)
{
if (ev->code == proximity_bits[i])
{
EvdevProcessProximityEvent(pInfo, ev);
return;
}
}
switch (ev->code) {
case BTN_TOUCH:
/* For devices that have but don't use proximity, use
* BTN_TOUCH as the proximity notifier */
if (!pEvdev->use_proximity)
pEvdev->in_proximity = value ? ev->code : 0;
if (!(pEvdev->flags & (EVDEV_TOUCHSCREEN | EVDEV_TABLET)) ||
pEvdev->mt_mask)
break;
/* Treat BTN_TOUCH from devices that only have BTN_TOUCH as
* BTN_LEFT. */
ev->code = BTN_LEFT;
/* Intentional fallthrough! */
default:
EvdevProcessButtonEvent(pInfo, ev);
break;
}
}
/**
* Post the relative motion events.
*/
void
EvdevPostRelativeMotionEvents(InputInfoPtr pInfo, int num_v, int first_v,
int v[MAX_VALUATORS])
{
EvdevPtr pEvdev = pInfo->private;
if (pEvdev->rel_queued) {
xf86PostMotionEventM(pInfo->dev, Relative, pEvdev->vals);
}
}
/**
* Post the absolute motion events.
*/
void
EvdevPostAbsoluteMotionEvents(InputInfoPtr pInfo, int num_v, int first_v,
int v[MAX_VALUATORS])
{
EvdevPtr pEvdev = pInfo->private;
/*
* Some devices only generate valid abs coords when BTN_TOOL_PEN is
* pressed. On wacom tablets, this means that the pen is in
* proximity of the tablet. After the pen is removed, BTN_TOOL_PEN is
* released, and a (0, 0) absolute event is generated. Checking
* pEvdev->in_proximity here lets us ignore that event.
* pEvdev->in_proximity is initialized to 1 so devices that don't use
* this scheme still just work.
*/
if (pEvdev->abs_queued && pEvdev->in_proximity) {
xf86PostMotionEventM(pInfo->dev, Absolute, pEvdev->vals);
}
}
static void
EvdevPostProximityEvents(InputInfoPtr pInfo, int which, int num_v, int first_v,
int v[MAX_VALUATORS])
{
int i;
EvdevPtr pEvdev = pInfo->private;
for (i = 0; pEvdev->prox_queued && i < pEvdev->num_queue; i++) {
switch (pEvdev->queue[i].type) {
case EV_QUEUE_KEY:
case EV_QUEUE_BTN:
#ifdef MULTITOUCH
case EV_QUEUE_TOUCH:
#endif
break;
case EV_QUEUE_PROXIMITY:
if (pEvdev->queue[i].val == which)
xf86PostProximityEventP(pInfo->dev, which, first_v, num_v,
v + first_v);
break;
}
}
}
/**
* Post the queued key/button events.
*/
static void EvdevPostQueuedEvents(InputInfoPtr pInfo, int num_v, int first_v,
int v[MAX_VALUATORS])
{
int i;
EvdevPtr pEvdev = pInfo->private;
for (i = 0; i < pEvdev->num_queue; i++) {
switch (pEvdev->queue[i].type) {
case EV_QUEUE_KEY:
xf86PostKeyboardEvent(pInfo->dev, pEvdev->queue[i].detail.key,
pEvdev->queue[i].val);
break;
case EV_QUEUE_BTN:
if (Evdev3BEmuFilterEvent(pInfo,
pEvdev->queue[i].detail.key,
pEvdev->queue[i].val))
break;
if (pEvdev->abs_queued && pEvdev->in_proximity) {
xf86PostButtonEventP(pInfo->dev, Absolute, pEvdev->queue[i].detail.key,
pEvdev->queue[i].val, first_v, num_v,
v + first_v);
} else
xf86PostButtonEvent(pInfo->dev, Relative, pEvdev->queue[i].detail.key,
pEvdev->queue[i].val, 0, 0);
break;
case EV_QUEUE_PROXIMITY:
break;
#ifdef MULTITOUCH
case EV_QUEUE_TOUCH:
xf86PostTouchEvent(pInfo->dev, pEvdev->queue[i].detail.touch,
pEvdev->queue[i].val, 0,
pEvdev->queue[i].touchMask);
break;
#endif
}
}
}
/**
* Take the synchronization input event and process it accordingly; the motion
* notify events are sent first, then any button/key press/release events.
*/
static void
EvdevProcessSyncEvent(InputInfoPtr pInfo, struct input_event *ev)
{
int i;
int num_v = 0, first_v = 0;
int v[MAX_VALUATORS] = {};
EvdevPtr pEvdev = pInfo->private;
EvdevProcessProximityState(pInfo);
EvdevProcessValuators(pInfo);
EvdevProcessTouch(pInfo);
EvdevPostProximityEvents(pInfo, TRUE, num_v, first_v, v);
EvdevPostRelativeMotionEvents(pInfo, num_v, first_v, v);
EvdevPostAbsoluteMotionEvents(pInfo, num_v, first_v, v);
EvdevPostQueuedEvents(pInfo, num_v, first_v, v);
EvdevPostProximityEvents(pInfo, FALSE, num_v, first_v, v);
memset(pEvdev->delta, 0, sizeof(pEvdev->delta));
for (i = 0; i < ArrayLength(pEvdev->queue); i++)
{
EventQueuePtr queue = &pEvdev->queue[i];
queue->detail.key = 0;
queue->type = 0;
queue->val = 0;
/* don't reset the touchMask */
}
if (pEvdev->vals)
valuator_mask_zero(pEvdev->vals);
pEvdev->num_queue = 0;
pEvdev->abs_queued = 0;
pEvdev->rel_queued = 0;
pEvdev->prox_queued = 0;
}
/**
* Process the events from the device; nothing is actually posted to the server
* until an EV_SYN event is received.
*/
static void
EvdevProcessEvent(InputInfoPtr pInfo, struct input_event *ev)
{
switch (ev->type) {
case EV_REL:
EvdevProcessRelativeMotionEvent(pInfo, ev);
break;
case EV_ABS:
EvdevProcessAbsoluteMotionEvent(pInfo, ev);
break;
case EV_KEY:
EvdevProcessKeyEvent(pInfo, ev);
break;
case EV_SYN:
EvdevProcessSyncEvent(pInfo, ev);
break;
}
}
#undef ABS_X_VALUE
#undef ABS_Y_VALUE
#undef ABS_VALUE
static void
EvdevFreeMasks(EvdevPtr pEvdev)
{
#ifdef MULTITOUCH
int i;
#endif
valuator_mask_free(&pEvdev->vals);
valuator_mask_free(&pEvdev->old_vals);
valuator_mask_free(&pEvdev->prox);
#ifdef MULTITOUCH
valuator_mask_free(&pEvdev->mt_mask);
if (pEvdev->last_mt_vals)
{
for (i = 0; i < num_slots(pEvdev); i++)
valuator_mask_free(&pEvdev->last_mt_vals[i]);
free(pEvdev->last_mt_vals);
pEvdev->last_mt_vals = NULL;
}
for (i = 0; i < EVDEV_MAXQUEUE; i++)
valuator_mask_free(&pEvdev->queue[i].touchMask);
#endif
}
/* just a magic number to reduce the number of reads */
#define NUM_EVENTS 16
static void
EvdevReadInput(InputInfoPtr pInfo)
{
struct input_event ev[NUM_EVENTS];
int i, len = sizeof(ev);
while (len == sizeof(ev))
{
#ifdef MULTITOUCH
EvdevPtr pEvdev = pInfo->private;
if (pEvdev->mtdev)
len = mtdev_get(pEvdev->mtdev, pInfo->fd, ev, NUM_EVENTS) *
sizeof(struct input_event);
else
#endif
len = read(pInfo->fd, &ev, sizeof(ev));
if (len <= 0)
{
if (errno == ENODEV) /* May happen after resume */
xf86RemoveEnabledDevice(pInfo);
else if (errno != EAGAIN)
LogMessageVerbSigSafe(X_ERROR, 0, "%s: Read error: %s\n", pInfo->name,
strerror(errno));
break;
}
/* The kernel promises that we always only read a complete
* event, so len != sizeof ev is an error. */
if (len % sizeof(ev[0])) {
LogMessageVerbSigSafe(X_ERROR, 0, "%s: Read error: %s\n", pInfo->name, strerror(errno));
break;
}
for (i = 0; i < len/sizeof(ev[0]); i++)
EvdevProcessEvent(pInfo, &ev[i]);
}
}
static void
EvdevPtrCtrlProc(DeviceIntPtr device, PtrCtrl *ctrl)
{
/* Nothing to do, dix handles all settings */
}
static void
EvdevKbdCtrl(DeviceIntPtr device, KeybdCtrl *ctrl)
{
static struct { int xbit, code; } bits[] = {
{ CAPSFLAG, LED_CAPSL },
{ NUMFLAG, LED_NUML },
{ SCROLLFLAG, LED_SCROLLL },
{ MODEFLAG, LED_KANA },
{ COMPOSEFLAG, LED_COMPOSE }
};
InputInfoPtr pInfo;
struct input_event ev[ArrayLength(bits)];
int i;
memset(ev, 0, sizeof(ev));
pInfo = device->public.devicePrivate;
for (i = 0; i < ArrayLength(bits); i++) {
ev[i].type = EV_LED;
ev[i].code = bits[i].code;
ev[i].value = (ctrl->leds & bits[i].xbit) > 0;
}
write(pInfo->fd, ev, sizeof ev);
}
static int
EvdevAddKeyClass(DeviceIntPtr device)
{
int rc = Success;
XkbRMLVOSet rmlvo = {0};
InputInfoPtr pInfo;
pInfo = device->public.devicePrivate;
/* sorry, no rules change allowed for you */
xf86ReplaceStrOption(pInfo->options, "xkb_rules", "evdev");
rmlvo.rules = xf86SetStrOption(pInfo->options, "xkb_rules", NULL);
rmlvo.model = xf86SetStrOption(pInfo->options, "xkb_model", NULL);
rmlvo.layout = xf86SetStrOption(pInfo->options, "xkb_layout", NULL);
rmlvo.variant = xf86SetStrOption(pInfo->options, "xkb_variant", NULL);
rmlvo.options = xf86SetStrOption(pInfo->options, "xkb_options", NULL);
if (!InitKeyboardDeviceStruct(device, &rmlvo, NULL, EvdevKbdCtrl))
rc = !Success;
XkbFreeRMLVOSet(&rmlvo, FALSE);
return rc;
}
#ifdef MULTITOUCH
/* MT axes are counted twice - once as ABS_X (which the kernel keeps for
* backwards compatibility), once as ABS_MT_POSITION_X. So we need to keep a
* mapping of those axes to make sure we only count them once
*/
struct mt_axis_mappings {
int mt_code;
int code;
Bool needs_mapping; /* TRUE if both code and mt_code are present */
int mapping; /* Logical mapping of 'code' axis */
};
static struct mt_axis_mappings mt_axis_mappings[] = {
{ABS_MT_POSITION_X, ABS_X},
{ABS_MT_POSITION_Y, ABS_Y},
{ABS_MT_PRESSURE, ABS_PRESSURE},
{ABS_MT_DISTANCE, ABS_DISTANCE},
};
#endif
/**
* return TRUE if the axis is not one we should count as true axis
*/
static int
is_blacklisted_axis(int axis)
{
switch(axis)
{
case ABS_MT_SLOT:
case ABS_MT_TRACKING_ID:
return TRUE;
default:
return FALSE;
}
}
static int
EvdevAddAbsValuatorClass(DeviceIntPtr device, int want_scroll_axes)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
int num_axes, axis, i = 0;
int num_mt_axes = 0, /* number of MT-only axes */
num_mt_axes_total = 0; /* total number of MT axes, including
double-counted ones, excluding blacklisted */
Atom *atoms;
int mapping = 0;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
if (!EvdevBitIsSet(pEvdev->bitmask, EV_ABS))
goto out;
num_axes = EvdevCountBits(pEvdev->abs_bitmask, NLONGS(ABS_MAX));
if (num_axes < 1)
goto out;
#ifdef MULTITOUCH
for (axis = ABS_MT_SLOT; axis < ABS_MAX; axis++)
{
if (EvdevBitIsSet(pEvdev->abs_bitmask, axis))
{
int j;
Bool skip = FALSE;
for (j = 0; j < ArrayLength(mt_axis_mappings); j++)
{
if (mt_axis_mappings[j].mt_code == axis &&
EvdevBitIsSet(pEvdev->abs_bitmask, mt_axis_mappings[j].code))
{
mt_axis_mappings[j].needs_mapping = TRUE;
skip = TRUE;
}
}
if (!is_blacklisted_axis(axis))
{
num_mt_axes_total++;
if (!skip)
num_mt_axes++;
}
num_axes--;
}
}
/* device only has mt-axes. the kernel should give us ABS_X etc for
backwards compat but some devices don't have it. */
if (num_axes == 0 && num_mt_axes > 0) {
xf86IDrvMsg(pInfo, X_ERROR,
"found only multitouch-axes. That shouldn't happen.\n");
goto out;
}
#endif
#ifdef HAVE_SMOOTH_SCROLLING
if (want_scroll_axes && EvdevBitIsSet(pEvdev->bitmask, EV_REL))
{
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_WHEEL))
num_axes++;
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_HWHEEL))
num_axes++;
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_DIAL))
num_axes++;
}
#endif
if (num_axes + num_mt_axes > MAX_VALUATORS) {
xf86IDrvMsg(pInfo, X_WARNING, "found %d axes, limiting to %d.\n", num_axes, MAX_VALUATORS);
num_axes = MAX_VALUATORS;
}
if (num_axes < 1 && num_mt_axes_total < 1) {
xf86Msg(X_WARNING, "%s: no absolute or touch axes found.\n",
device->name);
return !Success;
}
pEvdev->num_vals = num_axes;
if (num_axes > 0) {
pEvdev->vals = valuator_mask_new(num_axes);
pEvdev->old_vals = valuator_mask_new(num_axes);
if (!pEvdev->vals || !pEvdev->old_vals) {
xf86IDrvMsg(pInfo, X_ERROR, "failed to allocate valuator masks.\n");
goto out;
}
}
#ifdef MULTITOUCH
if (num_mt_axes_total > 0) {
pEvdev->num_mt_vals = num_mt_axes_total;
pEvdev->mt_mask = valuator_mask_new(num_mt_axes_total);
if (!pEvdev->mt_mask) {
xf86Msg(X_ERROR, "%s: failed to allocate MT valuator mask.\n",
device->name);
goto out;
}
pEvdev->last_mt_vals = calloc(num_slots(pEvdev), sizeof(ValuatorMask *));
if (!pEvdev->last_mt_vals) {
xf86IDrvMsg(pInfo, X_ERROR,
"%s: failed to allocate MT last values mask array.\n",
device->name);
goto out;
}
for (i = 0; i < num_slots(pEvdev); i++) {
pEvdev->last_mt_vals[i] = valuator_mask_new(num_mt_axes_total);
if (!pEvdev->last_mt_vals[i]) {
xf86IDrvMsg(pInfo, X_ERROR,
"%s: failed to allocate MT last values mask.\n",
device->name);
goto out;
}
}
for (i = 0; i < EVDEV_MAXQUEUE; i++) {
pEvdev->queue[i].touchMask =
valuator_mask_new(num_mt_axes_total);
if (!pEvdev->queue[i].touchMask) {
xf86Msg(X_ERROR, "%s: failed to allocate MT valuator masks for "
"evdev event queue.\n", device->name);
goto out;
}
}
}
#endif
atoms = malloc((pEvdev->num_vals + num_mt_axes) * sizeof(Atom));
i = 0;
for (axis = ABS_X; i < MAX_VALUATORS && axis <= ABS_MAX; axis++) {
#ifdef MULTITOUCH
int j;
#endif
pEvdev->abs_axis_map[axis] = -1;
if (!EvdevBitIsSet(pEvdev->abs_bitmask, axis) ||
is_blacklisted_axis(axis))
continue;
mapping = i;
#ifdef MULTITOUCH
for (j = 0; j < ArrayLength(mt_axis_mappings); j++)
{
if (mt_axis_mappings[j].code == axis)
mt_axis_mappings[j].mapping = mapping;
else if (mt_axis_mappings[j].mt_code == axis &&
mt_axis_mappings[j].needs_mapping)
mapping = mt_axis_mappings[j].mapping;
}
#endif
pEvdev->abs_axis_map[axis] = mapping;
if (mapping == i)
i++;
}
#ifdef HAVE_SMOOTH_SCROLLING
if (want_scroll_axes)
{
mapping++; /* continue from abs axis mapping */
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_HWHEEL))
pEvdev->rel_axis_map[REL_HWHEEL] = mapping++;
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_DIAL))
pEvdev->rel_axis_map[REL_DIAL] = mapping++;
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_WHEEL))
pEvdev->rel_axis_map[REL_WHEEL] = mapping++;
}
#endif
EvdevInitAxesLabels(pEvdev, Absolute, pEvdev->num_vals + num_mt_axes, atoms);
if (!InitValuatorClassDeviceStruct(device, num_axes + num_mt_axes, atoms,
GetMotionHistorySize(), Absolute)) {
xf86IDrvMsg(pInfo, X_ERROR, "failed to initialize valuator class device.\n");
goto out;
}
#ifdef MULTITOUCH
if (pEvdev->mtdev && num_mt_axes_total > 0)
{
int num_touches = 0;
int mode = pEvdev->flags & EVDEV_TOUCHPAD ?
XIDependentTouch : XIDirectTouch;
if (pEvdev->mtdev->caps.slot.maximum > 0)
num_touches = pEvdev->mtdev->caps.slot.maximum -
pEvdev->mtdev->caps.slot.minimum + 1;
if (!InitTouchClassDeviceStruct(device, num_touches, mode,
num_mt_axes_total)) {
xf86Msg(X_ERROR, "%s: failed to initialize touch class device.\n",
device->name);
goto out;
}
for (i = 0; i < num_slots(pEvdev); i++) {
for (axis = ABS_MT_TOUCH_MAJOR; axis < ABS_MAX; axis++) {
if (pEvdev->abs_axis_map[axis] >= 0) {
/* XXX: read initial values from mtdev when it adds support
* for doing so. */
valuator_mask_set(pEvdev->last_mt_vals[i],
pEvdev->abs_axis_map[axis], 0);
}
}
}
}
#endif
for (axis = ABS_X; axis < ABS_MT_SLOT; axis++) {
int axnum = pEvdev->abs_axis_map[axis];
int resolution = 0;
if (axnum == -1)
continue;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 30)
/* Kernel provides units/mm, X wants units/m */
if (pEvdev->absinfo[axis].resolution)
resolution = pEvdev->absinfo[axis].resolution * 1000;
#endif
xf86InitValuatorAxisStruct(device, axnum,
atoms[axnum],
pEvdev->absinfo[axis].minimum,
pEvdev->absinfo[axis].maximum,
resolution, 0, resolution, Absolute);
xf86InitValuatorDefaults(device, axnum);
}
#ifdef MULTITOUCH
for (axis = ABS_MT_TOUCH_MAJOR; axis <= ABS_MAX; axis++) {
int axnum = pEvdev->abs_axis_map[axis];
int resolution = 0;
int j;
BOOL skip = FALSE;
if (axnum < 0)
continue;
for (j = 0; j < ArrayLength(mt_axis_mappings); j++)
if (mt_axis_mappings[j].mt_code == axis &&
mt_axis_mappings[j].needs_mapping)
{
skip = TRUE;
break;
}
/* MT axis is mapped, don't set up twice */
if (skip)
continue;
if (pEvdev->absinfo[axis].resolution)
resolution = pEvdev->absinfo[axis].resolution * 1000;
xf86InitValuatorAxisStruct(device, axnum,
atoms[axnum],
pEvdev->absinfo[axis].minimum,
pEvdev->absinfo[axis].maximum,
resolution, 0, resolution,
Absolute);
}
#endif
#ifdef HAVE_SMOOTH_SCROLLING
if (want_scroll_axes)
{
int idx;
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_WHEEL))
{
idx = REL_WHEEL;
xf86InitValuatorAxisStruct(device,
pEvdev->rel_axis_map[idx],
atoms[pEvdev->rel_axis_map[idx]],
NO_AXIS_LIMITS, NO_AXIS_LIMITS,
0, 0, 0, Relative);
SetScrollValuator(device, pEvdev->rel_axis_map[idx],
SCROLL_TYPE_VERTICAL, -1.0,
SCROLL_FLAG_PREFERRED);
}
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_HWHEEL))
{
idx = REL_HWHEEL;
xf86InitValuatorAxisStruct(device,
pEvdev->rel_axis_map[idx],
atoms[pEvdev->rel_axis_map[idx]],
NO_AXIS_LIMITS, NO_AXIS_LIMITS,
0, 0, 0, Relative);
SetScrollValuator(device, pEvdev->rel_axis_map[idx],
SCROLL_TYPE_HORIZONTAL, 1.0,
SCROLL_FLAG_NONE);
}
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_DIAL))
{
idx = REL_DIAL;
xf86InitValuatorAxisStruct(device,
pEvdev->rel_axis_map[idx],
atoms[pEvdev->rel_axis_map[idx]],
NO_AXIS_LIMITS, NO_AXIS_LIMITS,
0, 0, 0, Relative);
SetScrollValuator(device, pEvdev->rel_axis_map[idx],
SCROLL_TYPE_HORIZONTAL, 1.0,
SCROLL_FLAG_NONE);
}
}
#endif
free(atoms);
for (i = 0; i < ArrayLength(proximity_bits); i++)
{
if (!pEvdev->use_proximity)
break;
if (EvdevBitIsSet(pEvdev->key_bitmask, proximity_bits[i]))
{
InitProximityClassDeviceStruct(device);
pEvdev->prox = valuator_mask_new(num_axes);
if (!pEvdev->prox) {
xf86IDrvMsg(pInfo, X_ERROR,
"failed to allocate proximity valuator " "mask.\n");
goto out;
}
break;
}
}
if (!InitPtrFeedbackClassDeviceStruct(device, EvdevPtrCtrlProc)) {
xf86IDrvMsg(pInfo, X_ERROR,
"failed to initialize pointer feedback class device.\n");
goto out;
}
if (pEvdev->flags & EVDEV_TOUCHPAD)
pEvdev->flags |= EVDEV_RELATIVE_MODE;
else
pEvdev->flags &= ~EVDEV_RELATIVE_MODE;
if (xf86FindOption(pInfo->options, "Mode"))
{
char *mode;
mode = xf86SetStrOption(pInfo->options, "Mode", NULL);
if (!strcasecmp("absolute", mode))
pEvdev->flags &= ~EVDEV_RELATIVE_MODE;
else if (!strcasecmp("relative", mode))
pEvdev->flags |= EVDEV_RELATIVE_MODE;
else
xf86IDrvMsg(pInfo, X_INFO, "unknown mode, use default\n");
free(mode);
}
return Success;
out:
EvdevFreeMasks(pEvdev);
return !Success;
}
static int
EvdevAddRelValuatorClass(DeviceIntPtr device)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
int num_axes, axis, i = 0;
Atom *atoms;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
if (!EvdevBitIsSet(pEvdev->bitmask, EV_REL))
goto out;
num_axes = EvdevCountBits(pEvdev->rel_bitmask, NLONGS(REL_MAX));
if (num_axes < 1)
goto out;
#ifndef HAVE_SMOOTH_SCROLLING
/* Wheels are special, we post them as button events. So let's ignore them
* in the axes list too */
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_WHEEL))
num_axes--;
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_HWHEEL))
num_axes--;
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_DIAL))
num_axes--;
if (num_axes <= 0)
goto out;
#endif
if (num_axes > MAX_VALUATORS) {
xf86IDrvMsg(pInfo, X_WARNING, "found %d axes, limiting to %d.\n", num_axes, MAX_VALUATORS);
num_axes = MAX_VALUATORS;
}
pEvdev->num_vals = num_axes;
if (num_axes > 0) {
pEvdev->vals = valuator_mask_new(num_axes);
if (!pEvdev->vals)
goto out;
}
atoms = malloc(pEvdev->num_vals * sizeof(Atom));
for (axis = REL_X; i < MAX_VALUATORS && axis <= REL_MAX; axis++)
{
pEvdev->rel_axis_map[axis] = -1;
#ifndef HAVE_SMOOTH_SCROLLING
/* We don't post wheel events, so ignore them here too */
if (axis == REL_WHEEL || axis == REL_HWHEEL || axis == REL_DIAL)
continue;
#endif
if (!EvdevBitIsSet(pEvdev->rel_bitmask, axis))
continue;
pEvdev->rel_axis_map[axis] = i;
i++;
}
EvdevInitAxesLabels(pEvdev, Relative, pEvdev->num_vals, atoms);
if (!InitValuatorClassDeviceStruct(device, num_axes, atoms,
GetMotionHistorySize(), Relative)) {
xf86IDrvMsg(pInfo, X_ERROR, "failed to initialize valuator class device.\n");
goto out;
}
if (!InitPtrFeedbackClassDeviceStruct(device, EvdevPtrCtrlProc)) {
xf86IDrvMsg(pInfo, X_ERROR, "failed to initialize pointer feedback class "
"device.\n");
goto out;
}
for (axis = REL_X; axis <= REL_MAX; axis++)
{
int axnum = pEvdev->rel_axis_map[axis];
if (axnum == -1)
continue;
xf86InitValuatorAxisStruct(device, axnum, atoms[axnum], -1, -1, 1, 0, 1,
Relative);
xf86InitValuatorDefaults(device, axnum);
#ifdef HAVE_SMOOTH_SCROLLING
if (axis == REL_WHEEL)
SetScrollValuator(device, axnum, SCROLL_TYPE_VERTICAL, -1.0, SCROLL_FLAG_PREFERRED);
else if (axis == REL_DIAL)
SetScrollValuator(device, axnum, SCROLL_TYPE_VERTICAL, -1.0, SCROLL_FLAG_NONE);
else if (axis == REL_HWHEEL)
SetScrollValuator(device, axnum, SCROLL_TYPE_HORIZONTAL, 1.0, SCROLL_FLAG_NONE);
#endif
}
free(atoms);
return Success;
out:
valuator_mask_free(&pEvdev->vals);
return !Success;
}
static int
EvdevAddButtonClass(DeviceIntPtr device)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
Atom *labels;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
labels = malloc(pEvdev->num_buttons * sizeof(Atom));
EvdevInitButtonLabels(pEvdev, pEvdev->num_buttons, labels);
if (!InitButtonClassDeviceStruct(device, pEvdev->num_buttons, labels,
pEvdev->btnmap))
return !Success;
free(labels);
return Success;
}
/**
* Init the button mapping for the device. By default, this is a 1:1 mapping,
* i.e. Button 1 maps to Button 1, Button 2 to 2, etc.
*
* If a mapping has been specified, the mapping is the default, with the
* user-defined ones overwriting the defaults.
* i.e. a user-defined mapping of "3 2 1" results in a mapping of 3 2 1 4 5 6 ...
*
* Invalid button mappings revert to the default.
*
* Note that index 0 is unused, button 0 does not exist.
* This mapping is initialised for all devices, but only applied if the device
* has buttons (in EvdevAddButtonClass).
*/
static void
EvdevInitButtonMapping(InputInfoPtr pInfo)
{
int i, nbuttons = 1;
char *mapping = NULL;
EvdevPtr pEvdev = pInfo->private;
/* Check for user-defined button mapping */
if ((mapping = xf86CheckStrOption(pInfo->options, "ButtonMapping", NULL)))
{
char *map, *s = NULL;
int btn = 0;
xf86IDrvMsg(pInfo, X_CONFIG, "ButtonMapping '%s'\n", mapping);
map = mapping;
do
{
btn = strtol(map, &s, 10);
if (s == map || btn < 0 || btn > EVDEV_MAXBUTTONS)
{
xf86IDrvMsg(pInfo, X_ERROR,
"... Invalid button mapping. Using defaults\n");
nbuttons = 1; /* ensure defaults start at 1 */
break;
}
pEvdev->btnmap[nbuttons++] = btn;
map = s;
} while (s && *s != '\0' && nbuttons < EVDEV_MAXBUTTONS);
free(mapping);
}
for (i = nbuttons; i < ArrayLength(pEvdev->btnmap); i++)
pEvdev->btnmap[i] = i;
}
static void
EvdevInitAnyValuators(DeviceIntPtr device, EvdevPtr pEvdev)
{
InputInfoPtr pInfo = device->public.devicePrivate;
int rel_success = FALSE;
if (pEvdev->flags & EVDEV_RELATIVE_EVENTS &&
EvdevAddRelValuatorClass(device) == Success)
{
rel_success = TRUE;
xf86IDrvMsg(pInfo, X_INFO, "initialized for relative axes.\n");
}
if (pEvdev->flags & EVDEV_ABSOLUTE_EVENTS &&
EvdevAddAbsValuatorClass(device, !rel_success) == Success)
xf86IDrvMsg(pInfo, X_INFO, "initialized for absolute axes.\n");
}
static void
EvdevInitAbsValuators(DeviceIntPtr device, EvdevPtr pEvdev)
{
InputInfoPtr pInfo = device->public.devicePrivate;
if (EvdevAddAbsValuatorClass(device, TRUE) == Success) {
xf86IDrvMsg(pInfo, X_INFO,"initialized for absolute axes.\n");
} else {
xf86IDrvMsg(pInfo, X_ERROR,"failed to initialize for absolute axes.\n");
pEvdev->flags &= ~EVDEV_ABSOLUTE_EVENTS;
}
}
static void
EvdevInitRelValuators(DeviceIntPtr device, EvdevPtr pEvdev)
{
InputInfoPtr pInfo = device->public.devicePrivate;
int has_abs_axes = pEvdev->flags & EVDEV_ABSOLUTE_EVENTS;
if (EvdevAddRelValuatorClass(device) == Success) {
xf86IDrvMsg(pInfo, X_INFO,"initialized for relative axes.\n");
if (has_abs_axes) {
xf86IDrvMsg(pInfo, X_WARNING,"ignoring absolute axes.\n");
pEvdev->flags &= ~EVDEV_ABSOLUTE_EVENTS;
}
} else {
xf86IDrvMsg(pInfo, X_ERROR,"failed to initialize for relative axes.\n");
pEvdev->flags &= ~EVDEV_RELATIVE_EVENTS;
if (has_abs_axes)
EvdevInitAbsValuators(device, pEvdev);
}
}
static void
EvdevInitTouchDevice(DeviceIntPtr device, EvdevPtr pEvdev)
{
InputInfoPtr pInfo = device->public.devicePrivate;
if (pEvdev->flags & EVDEV_RELATIVE_EVENTS) {
xf86IDrvMsg(pInfo, X_WARNING, "touchpads, tablets and touchscreens "
"ignore relative axes.\n");
pEvdev->flags &= ~EVDEV_RELATIVE_EVENTS;
}
EvdevInitAbsValuators(device, pEvdev);
}
static int
EvdevInit(DeviceIntPtr device)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
if (pEvdev->flags & EVDEV_KEYBOARD_EVENTS)
EvdevAddKeyClass(device);
if (pEvdev->flags & EVDEV_BUTTON_EVENTS)
EvdevAddButtonClass(device);
/* We don't allow relative and absolute axes on the same device. The
* reason is that some devices (MS Optical Desktop 2000) register both
* rel and abs axes for x/y.
*
* The abs axes register min/max; this min/max then also applies to the
* relative device (the mouse) and caps it at 0..255 for both axes.
* So, unless you have a small screen, you won't be enjoying it much;
* consequently, absolute axes are generally ignored.
*
* However, currenly only a device with absolute axes can be registered
* as a touch{pad,screen}. Thus, given such a device, absolute axes are
* used and relative axes are ignored.
*/
if (pEvdev->flags & (EVDEV_UNIGNORE_RELATIVE | EVDEV_UNIGNORE_ABSOLUTE))
EvdevInitAnyValuators(device, pEvdev);
else if (pEvdev->flags & (EVDEV_TOUCHPAD | EVDEV_TOUCHSCREEN | EVDEV_TABLET))
EvdevInitTouchDevice(device, pEvdev);
else if (pEvdev->flags & EVDEV_RELATIVE_EVENTS)
EvdevInitRelValuators(device, pEvdev);
else if (pEvdev->flags & EVDEV_ABSOLUTE_EVENTS)
EvdevInitAbsValuators(device, pEvdev);
/* We drop the return value, the only time we ever want the handlers to
* unregister is when the device dies. In which case we don't have to
* unregister anyway */
EvdevInitProperty(device);
XIRegisterPropertyHandler(device, EvdevSetProperty, NULL, NULL);
EvdevMBEmuInitProperty(device);
Evdev3BEmuInitProperty(device);
EvdevWheelEmuInitProperty(device);
EvdevDragLockInitProperty(device);
EvdevAppleInitProperty(device);
return Success;
}
/**
* Init all extras (wheel emulation, etc.) and grab the device.
*/
static int
EvdevOn(DeviceIntPtr device)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
int rc = Success;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
/* after PreInit fd is still open */
rc = EvdevOpenDevice(pInfo);
if (rc != Success)
return rc;
EvdevGrabDevice(pInfo, 1, 0);
xf86FlushInput(pInfo->fd);
xf86AddEnabledDevice(pInfo);
EvdevMBEmuOn(pInfo);
Evdev3BEmuOn(pInfo);
pEvdev->flags |= EVDEV_INITIALIZED;
device->public.on = TRUE;
return Success;
}
static int
EvdevProc(DeviceIntPtr device, int what)
{
InputInfoPtr pInfo;
EvdevPtr pEvdev;
pInfo = device->public.devicePrivate;
pEvdev = pInfo->private;
switch (what)
{
case DEVICE_INIT:
return EvdevInit(device);
case DEVICE_ON:
return EvdevOn(device);
case DEVICE_OFF:
if (pEvdev->flags & EVDEV_INITIALIZED)
{
EvdevMBEmuFinalize(pInfo);
Evdev3BEmuFinalize(pInfo);
}
if (pInfo->fd != -1)
{
EvdevGrabDevice(pInfo, 0, 1);
xf86RemoveEnabledDevice(pInfo);
EvdevCloseDevice(pInfo);
}
pEvdev->min_maj = 0;
pEvdev->flags &= ~EVDEV_INITIALIZED;
device->public.on = FALSE;
break;
case DEVICE_CLOSE:
xf86IDrvMsg(pInfo, X_INFO, "Close\n");
EvdevCloseDevice(pInfo);
EvdevFreeMasks(pEvdev);
EvdevRemoveDevice(pInfo);
pEvdev->min_maj = 0;
break;
default:
return BadValue;
}
return Success;
}
/**
* Get as much information as we can from the fd and cache it.
*
* @return Success if the information was cached, or !Success otherwise.
*/
static int
EvdevCache(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
int i, len;
struct input_id id;
unsigned long bitmask[NLONGS(EV_CNT)] = {0};
unsigned long key_bitmask[NLONGS(KEY_CNT)] = {0};
unsigned long rel_bitmask[NLONGS(REL_CNT)] = {0};
unsigned long abs_bitmask[NLONGS(ABS_CNT)] = {0};
if (ioctl(pInfo->fd, EVIOCGID, &id) < 0)
{
xf86IDrvMsg(pInfo, X_ERROR, "ioctl EVIOCGID failed: %s\n", strerror(errno));
goto error;
}
pEvdev->id_vendor = id.vendor;
pEvdev->id_product = id.product;
len = ioctl(pInfo->fd, EVIOCGBIT(0, sizeof(bitmask)), bitmask);
if (len < 0) {
xf86IDrvMsg(pInfo, X_ERROR, "ioctl EVIOCGBIT for bitmask failed: %s\n",
strerror(errno));
goto error;
}
memcpy(pEvdev->bitmask, bitmask, len);
len = ioctl(pInfo->fd, EVIOCGBIT(EV_REL, sizeof(rel_bitmask)), rel_bitmask);
if (len < 0) {
xf86IDrvMsg(pInfo, X_ERROR, "ioctl EVIOCGBIT for EV_REL failed: %s\n",
strerror(errno));
goto error;
}
memcpy(pEvdev->rel_bitmask, rel_bitmask, len);
len = ioctl(pInfo->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);
if (len < 0) {
xf86IDrvMsg(pInfo, X_ERROR, "ioctl EVIOCGBIT for EV_ABS failed: %s\n",
strerror(errno));
goto error;
}
memcpy(pEvdev->abs_bitmask, abs_bitmask, len);
/*
* Do not try to validate absinfo data since it is not expected
* to be static, always refresh it in evdev structure.
*/
for (i = ABS_X; i <= ABS_MAX; i++) {
if (EvdevBitIsSet(abs_bitmask, i)) {
len = ioctl(pInfo->fd, EVIOCGABS(i), &pEvdev->absinfo[i]);
if (len < 0) {
xf86IDrvMsg(pInfo, X_ERROR, "ioctl EVIOCGABSi(%d) failed: %s\n",
i, strerror(errno));
goto error;
}
xf86IDrvMsgVerb(pInfo, X_PROBED, 6, "absolute axis %#x [%d..%d]\n",
i, pEvdev->absinfo[i].maximum, pEvdev->absinfo[i].minimum);
}
}
len = ioctl(pInfo->fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask);
if (len < 0) {
xf86IDrvMsg(pInfo, X_ERROR, "ioctl EVIOCGBIT for EV_KEY failed: %s\n",
strerror(errno));
goto error;
}
/* Copy the data so we have reasonably up-to-date info */
memcpy(pEvdev->key_bitmask, key_bitmask, len);
return Success;
error:
return !Success;
}
/**
* Issue an EVIOCGRAB on the device file, either as a grab or to ungrab, or
* both. Return TRUE on success, otherwise FALSE. Failing the release is a
* still considered a success, because it's not as if you could do anything
* about it.
*/
static BOOL
EvdevGrabDevice(InputInfoPtr pInfo, int grab, int ungrab)
{
EvdevPtr pEvdev = pInfo->private;
if (pEvdev->grabDevice)
{
if (grab && ioctl(pInfo->fd, EVIOCGRAB, (void *)1)) {
xf86IDrvMsg(pInfo, X_WARNING, "Grab failed (%s)\n",
strerror(errno));
return FALSE;
} else if (ungrab && ioctl(pInfo->fd, EVIOCGRAB, (void *)0))
xf86IDrvMsg(pInfo, X_WARNING, "Release failed (%s)\n",
strerror(errno));
}
return TRUE;
}
/**
* Some devices only have other axes (e.g. wheels), but we
* still need x/y for these. The server relies on devices having
* x/y as axes 0/1 and core/XI 1.x clients expect it too (#44655)
*/
static void
EvdevForceXY(InputInfoPtr pInfo, int mode)
{
EvdevPtr pEvdev = pInfo->private;
xf86IDrvMsg(pInfo, X_INFO, "Forcing %s x/y axes to exist.\n",
(mode == Relative) ? "relative" : "absolute");
if (mode == Relative)
{
EvdevSetBit(pEvdev->rel_bitmask, REL_X);
EvdevSetBit(pEvdev->rel_bitmask, REL_Y);
} else if (mode == Absolute)
{
EvdevSetBit(pEvdev->abs_bitmask, ABS_X);
EvdevSetBit(pEvdev->abs_bitmask, ABS_Y);
pEvdev->absinfo[ABS_X].minimum = 0;
pEvdev->absinfo[ABS_X].maximum = 1000;
pEvdev->absinfo[ABS_X].value = 0;
pEvdev->absinfo[ABS_X].resolution = 0;
pEvdev->absinfo[ABS_Y].minimum = 0;
pEvdev->absinfo[ABS_Y].maximum = 1000;
pEvdev->absinfo[ABS_Y].value = 0;
pEvdev->absinfo[ABS_Y].resolution = 0;
}
}
static int
EvdevProbe(InputInfoPtr pInfo)
{
int i, has_rel_axes, has_abs_axes, has_keys, num_buttons, has_scroll;
int has_lmr; /* left middle right */
int has_mt; /* multitouch */
int ignore_abs = 0, ignore_rel = 0;
EvdevPtr pEvdev = pInfo->private;
int rc = 1;
xf86IDrvMsg(pInfo, X_PROBED, "Vendor %#hx Product %#hx\n",
pEvdev->id_vendor, pEvdev->id_product);
/* Trinary state for ignoring axes:
- unset: do the normal thing.
- TRUE: explicitly ignore them.
- FALSE: unignore axes, use them at all cost if they're present.
*/
if (xf86FindOption(pInfo->options, "IgnoreRelativeAxes"))
{
if (xf86SetBoolOption(pInfo->options, "IgnoreRelativeAxes", FALSE))
ignore_rel = TRUE;
else
pEvdev->flags |= EVDEV_UNIGNORE_RELATIVE;
}
if (xf86FindOption(pInfo->options, "IgnoreAbsoluteAxes"))
{
if (xf86SetBoolOption(pInfo->options, "IgnoreAbsoluteAxes", FALSE))
ignore_abs = TRUE;
else
pEvdev->flags |= EVDEV_UNIGNORE_ABSOLUTE;
}
has_rel_axes = FALSE;
has_abs_axes = FALSE;
has_keys = FALSE;
has_scroll = FALSE;
has_lmr = FALSE;
has_mt = FALSE;
num_buttons = 0;
/* count all buttons */
for (i = BTN_MISC; i < BTN_JOYSTICK; i++)
{
int mapping = 0;
if (EvdevBitIsSet(pEvdev->key_bitmask, i))
{
mapping = EvdevUtilButtonEventToButtonNumber(pEvdev, i);
if (mapping > num_buttons)
num_buttons = mapping;
}
}
has_lmr = EvdevBitIsSet(pEvdev->key_bitmask, BTN_LEFT) ||
EvdevBitIsSet(pEvdev->key_bitmask, BTN_MIDDLE) ||
EvdevBitIsSet(pEvdev->key_bitmask, BTN_RIGHT);
if (num_buttons)
{
pEvdev->flags |= EVDEV_BUTTON_EVENTS;
pEvdev->num_buttons = num_buttons;
xf86IDrvMsg(pInfo, X_PROBED, "Found %d mouse buttons\n", num_buttons);
}
for (i = 0; i < REL_MAX; i++) {
if (EvdevBitIsSet(pEvdev->rel_bitmask, i)) {
has_rel_axes = TRUE;
break;
}
}
if (has_rel_axes) {
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_WHEEL) ||
EvdevBitIsSet(pEvdev->rel_bitmask, REL_HWHEEL) ||
EvdevBitIsSet(pEvdev->rel_bitmask, REL_DIAL)) {
xf86IDrvMsg(pInfo, X_PROBED, "Found scroll wheel(s)\n");
has_scroll = TRUE;
if (!num_buttons)
xf86IDrvMsg(pInfo, X_INFO,
"Forcing buttons for scroll wheel(s)\n");
num_buttons = (num_buttons < 3) ? 7 : num_buttons + 4;
pEvdev->num_buttons = num_buttons;
}
if (!ignore_rel)
{
xf86IDrvMsg(pInfo, X_PROBED, "Found relative axes\n");
pEvdev->flags |= EVDEV_RELATIVE_EVENTS;
if (EvdevBitIsSet(pEvdev->rel_bitmask, REL_X) &&
EvdevBitIsSet(pEvdev->rel_bitmask, REL_Y)) {
xf86IDrvMsg(pInfo, X_PROBED, "Found x and y relative axes\n");
} else if (!EvdevBitIsSet(pEvdev->abs_bitmask, ABS_X) ||
!EvdevBitIsSet(pEvdev->abs_bitmask, ABS_Y))
EvdevForceXY(pInfo, Relative);
} else {
xf86IDrvMsg(pInfo, X_INFO, "Relative axes present but ignored.\n");
has_rel_axes = FALSE;
}
}
for (i = 0; i < ABS_MAX; i++) {
if (EvdevBitIsSet(pEvdev->abs_bitmask, i)) {
has_abs_axes = TRUE;
break;
}
}
#ifdef MULTITOUCH
for (i = ABS_MT_SLOT; i < ABS_MAX; i++) {
if (EvdevBitIsSet(pEvdev->abs_bitmask, i)) {
has_mt = TRUE;
break;
}
}
#endif
if (ignore_abs && has_abs_axes)
{
xf86IDrvMsg(pInfo, X_INFO, "Absolute axes present but ignored.\n");
has_abs_axes = FALSE;
} else if (has_abs_axes) {
xf86IDrvMsg(pInfo, X_PROBED, "Found absolute axes\n");
pEvdev->flags |= EVDEV_ABSOLUTE_EVENTS;
if (has_mt) {
xf86IDrvMsg(pInfo, X_PROBED, "Found absolute multitouch axes\n");
if (num_buttons == 0) {
if (EvdevBitIsSet(pEvdev->key_bitmask, BTN_JOYSTICK)) {
xf86IDrvMsg(pInfo, X_INFO, "Device is a Joystick with MT without buttons. Ignoring it.\n");
goto out;
} else {
xf86IDrvMsg(pInfo, X_INFO, "No buttons found, faking one.\n");
num_buttons = 1;
pEvdev->num_buttons = num_buttons;
pEvdev->flags |= EVDEV_BUTTON_EVENTS;
}
}
}
if ((EvdevBitIsSet(pEvdev->abs_bitmask, ABS_X) &&
EvdevBitIsSet(pEvdev->abs_bitmask, ABS_Y))) {
xf86IDrvMsg(pInfo, X_PROBED, "Found x and y absolute axes\n");
if (EvdevBitIsSet(pEvdev->key_bitmask, BTN_TOOL_PEN) ||
EvdevBitIsSet(pEvdev->key_bitmask, BTN_STYLUS) ||
EvdevBitIsSet(pEvdev->key_bitmask, BTN_STYLUS2))
{
xf86IDrvMsg(pInfo, X_PROBED, "Found absolute tablet.\n");
pEvdev->flags |= EVDEV_TABLET;
if (!pEvdev->num_buttons)
{
pEvdev->num_buttons = 7; /* LMR + scroll wheels */
pEvdev->flags |= EVDEV_BUTTON_EVENTS;
}
} else if (EvdevBitIsSet(pEvdev->abs_bitmask, ABS_PRESSURE) ||
EvdevBitIsSet(pEvdev->key_bitmask, BTN_TOUCH)) {
if (has_lmr || EvdevBitIsSet(pEvdev->key_bitmask, BTN_TOOL_FINGER)) {
xf86IDrvMsg(pInfo, X_PROBED, "Found absolute touchpad.\n");
pEvdev->flags |= EVDEV_TOUCHPAD;
} else {
xf86IDrvMsg(pInfo, X_PROBED, "Found absolute touchscreen\n");
pEvdev->flags |= EVDEV_TOUCHSCREEN;
pEvdev->flags |= EVDEV_BUTTON_EVENTS;
}
} else if (!(EvdevBitIsSet(pEvdev->rel_bitmask, REL_X) &&
EvdevBitIsSet(pEvdev->rel_bitmask, REL_Y)) && has_lmr) {
/* some touchscreens use BTN_LEFT rather than BTN_TOUCH */
xf86IDrvMsg(pInfo, X_PROBED, "Found absolute touchscreen\n");
pEvdev->flags |= EVDEV_TOUCHSCREEN;
pEvdev->flags |= EVDEV_BUTTON_EVENTS;
}
} else {
#ifdef MULTITOUCH
if (!EvdevBitIsSet(pEvdev->abs_bitmask, ABS_MT_POSITION_X) ||
!EvdevBitIsSet(pEvdev->abs_bitmask, ABS_MT_POSITION_Y))
#endif
EvdevForceXY(pInfo, Absolute);
}
}
for (i = 0; i < BTN_MISC; i++) {
if (EvdevBitIsSet(pEvdev->key_bitmask, i)) {
xf86IDrvMsg(pInfo, X_PROBED, "Found keys\n");
pEvdev->flags |= EVDEV_KEYBOARD_EVENTS;
has_keys = TRUE;
break;
}
}
if (has_rel_axes || has_abs_axes)
{
char *str;
int num_calibration = 0, calibration[4] = { 0, 0, 0, 0 };
pEvdev->invert_x = xf86SetBoolOption(pInfo->options, "InvertX", FALSE);
pEvdev->invert_y = xf86SetBoolOption(pInfo->options, "InvertY", FALSE);
pEvdev->swap_axes = xf86SetBoolOption(pInfo->options, "SwapAxes", FALSE);
str = xf86CheckStrOption(pInfo->options, "Calibration", NULL);
if (str) {
num_calibration = sscanf(str, "%d %d %d %d",
&calibration[0], &calibration[1],
&calibration[2], &calibration[3]);
free(str);
if (num_calibration == 4)
EvdevSetCalibration(pInfo, num_calibration, calibration);
else
xf86IDrvMsg(pInfo, X_ERROR,
"Insufficient calibration factors (%d). Ignoring calibration\n",
num_calibration);
}
}
if (has_rel_axes || has_abs_axes || num_buttons) {
pInfo->flags |= XI86_SEND_DRAG_EVENTS;
if (pEvdev->flags & EVDEV_TOUCHPAD) {
xf86IDrvMsg(pInfo, X_INFO, "Configuring as touchpad\n");
pInfo->type_name = XI_TOUCHPAD;
pEvdev->use_proximity = 0;
} else if (pEvdev->flags & EVDEV_TABLET) {
xf86IDrvMsg(pInfo, X_INFO, "Configuring as tablet\n");
pInfo->type_name = XI_TABLET;
} else if (pEvdev->flags & EVDEV_TOUCHSCREEN) {
xf86IDrvMsg(pInfo, X_INFO, "Configuring as touchscreen\n");
pInfo->type_name = XI_TOUCHSCREEN;
} else {
if (!EvdevBitIsSet(pEvdev->rel_bitmask, REL_X) ||
!EvdevBitIsSet(pEvdev->rel_bitmask, REL_Y))
EvdevForceXY(pInfo, Relative);
xf86IDrvMsg(pInfo, X_INFO, "Configuring as mouse\n");
pInfo->type_name = XI_MOUSE;
}
rc = 0;
}
if (has_keys) {
xf86IDrvMsg(pInfo, X_INFO, "Configuring as keyboard\n");
pInfo->type_name = XI_KEYBOARD;
rc = 0;
}
if (has_scroll &&
(has_rel_axes || has_abs_axes || num_buttons || has_keys))
{
xf86IDrvMsg(pInfo, X_INFO, "Adding scrollwheel support\n");
pEvdev->flags |= EVDEV_BUTTON_EVENTS;
pEvdev->flags |= EVDEV_RELATIVE_EVENTS;
}
out:
if (rc)
xf86IDrvMsg(pInfo, X_WARNING, "Don't know how to use device\n");
return rc;
}
static void
EvdevSetCalibration(InputInfoPtr pInfo, int num_calibration, int calibration[4])
{
EvdevPtr pEvdev = pInfo->private;
if (num_calibration == 0) {
pEvdev->flags &= ~EVDEV_CALIBRATED;
pEvdev->calibration.min_x = 0;
pEvdev->calibration.max_x = 0;
pEvdev->calibration.min_y = 0;
pEvdev->calibration.max_y = 0;
} else if (num_calibration == 4) {
pEvdev->flags |= EVDEV_CALIBRATED;
pEvdev->calibration.min_x = calibration[0];
pEvdev->calibration.max_x = calibration[1];
pEvdev->calibration.min_y = calibration[2];
pEvdev->calibration.max_y = calibration[3];
}
}
#ifdef MULTITOUCH
/**
* Open an mtdev device for this device. mtdev is a bit too generous with
* memory usage, so only do so for devices with multitouch bits set.
*
* @return FALSE on error, TRUE if mtdev was initiated or the device doesn't
* need it
*/
static Bool
EvdevOpenMTDev(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
unsigned long bitmask[NLONGS(EV_CNT)] = {0};
unsigned long abs_bitmask[NLONGS(ABS_CNT)] = {0};
int len;
if (pEvdev->mtdev) {
pEvdev->cur_slot = pEvdev->mtdev->caps.slot.value;
return TRUE;
}
if (pInfo->fd < 0) {
xf86Msg(X_ERROR, "%s: Bug. fd < 0\n", pInfo->name);
return FALSE;
}
/* Use ioctl here, this may be called before EvdevCache */
len = ioctl(pInfo->fd, EVIOCGBIT(0, sizeof(bitmask)), bitmask);
if (len < 0) {
xf86IDrvMsg(pInfo, X_ERROR, "ioctl EVIOCGBIT for bitmask in %s failed: %s\n",
__func__, strerror(errno));
return FALSE;
}
if (!EvdevBitIsSet(bitmask, EV_ABS))
return TRUE;
len = ioctl(pInfo->fd, EVIOCGBIT(EV_ABS, sizeof(abs_bitmask)), abs_bitmask);
if (len < 0) {
xf86IDrvMsg(pInfo, X_ERROR, "ioctl EVIOCGBIT for EV_ABS in %s failed: %s\n",
__func__, strerror(errno));
return FALSE;
}
if (!EvdevBitIsSet(abs_bitmask, ABS_MT_POSITION_X) ||
!EvdevBitIsSet(abs_bitmask, ABS_MT_POSITION_Y))
return TRUE;
xf86IDrvMsg(pInfo, X_INFO, "Using mtdev for this device\n");
pEvdev->mtdev = mtdev_new_open(pInfo->fd);
if (pEvdev->mtdev)
pEvdev->cur_slot = pEvdev->mtdev->caps.slot.value;
else {
xf86Msg(X_ERROR, "%s: Couldn't open mtdev device\n", pInfo->name);
EvdevCloseDevice(pInfo);
return FALSE;
}
return TRUE;
}
#endif
static int
EvdevOpenDevice(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
char *device = pEvdev->device;
if (!device)
{
device = xf86CheckStrOption(pInfo->options, "Device", NULL);
if (!device) {
xf86IDrvMsg(pInfo, X_ERROR, "No device specified.\n");
return BadValue;
}
pEvdev->device = device;
xf86IDrvMsg(pInfo, X_CONFIG, "Device: \"%s\"\n", device);
}
if (pInfo->fd < 0)
{
do {
pInfo->fd = open(device, O_RDWR | O_NONBLOCK, 0);
} while (pInfo->fd < 0 && errno == EINTR);
if (pInfo->fd < 0) {
xf86IDrvMsg(pInfo, X_ERROR, "Unable to open evdev device \"%s\".\n", device);
return BadValue;
}
}
/* Check major/minor of device node to avoid adding duplicate devices. */
pEvdev->min_maj = EvdevGetMajorMinor(pInfo);
if (EvdevIsDuplicate(pInfo))
{
xf86IDrvMsg(pInfo, X_WARNING, "device file is duplicate. Ignoring.\n");
EvdevCloseDevice(pInfo);
return BadMatch;
}
#ifdef MULTITOUCH
if (!EvdevOpenMTDev(pInfo)) {
xf86Msg(X_ERROR, "%s: Couldn't open mtdev device\n", pInfo->name);
EvdevCloseDevice(pInfo);
return BadValue;
}
#endif
return Success;
}
static void
EvdevCloseDevice(InputInfoPtr pInfo)
{
EvdevPtr pEvdev = pInfo->private;
if (pInfo->fd >= 0)
{
close(pInfo->fd);
pInfo->fd = -1;
}
#ifdef MULTITOUCH
if (pEvdev->mtdev)
{
mtdev_close_delete(pEvdev->mtdev);
pEvdev->mtdev = NULL;
}
#endif
}
static void
EvdevUnInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
{
EvdevPtr pEvdev = pInfo ? pInfo->private : NULL;
if (pEvdev)
{
/* Release string allocated in EvdevOpenDevice. */
free(pEvdev->device);
pEvdev->device = NULL;
free(pEvdev->type_name);
pEvdev->type_name = NULL;
}
xf86DeleteInput(pInfo, flags);
}
static EvdevPtr
EvdevAlloc(void)
{
int i;
EvdevPtr pEvdev = calloc(sizeof(EvdevRec), 1);
if (!pEvdev)
return NULL;
/*
* We initialize pEvdev->in_proximity to 1 so that device that doesn't use
* proximity will still report events.
*/
pEvdev->in_proximity = 1;
pEvdev->use_proximity = 1;
#ifdef MULTITOUCH
pEvdev->cur_slot = -1;
#endif
for (i = 0; i < ArrayLength(pEvdev->rel_axis_map); i++)
pEvdev->rel_axis_map[i] = -1;
for (i = 0; i < ArrayLength(pEvdev->abs_axis_map); i++)
pEvdev->abs_axis_map[i] = -1;
pEvdev->rel_axis_map[0] = 0;
pEvdev->rel_axis_map[1] = 1;
pEvdev->type_name = NULL;
return pEvdev;
}
static int
EvdevPreInit(InputDriverPtr drv, InputInfoPtr pInfo, int flags)
{
EvdevPtr pEvdev;
int rc = BadAlloc;
if (!(pEvdev = EvdevAlloc()))
goto error;
pInfo->private = pEvdev;
pInfo->type_name = "UNKNOWN";
pInfo->device_control = EvdevProc;
pInfo->read_input = EvdevReadInput;
pInfo->switch_mode = EvdevSwitchMode;
rc = EvdevOpenDevice(pInfo);
if (rc != Success)
goto error;
/* Grabbing the event device stops in-kernel event forwarding. In other
words, it disables rfkill and the "Macintosh mouse button emulation".
Note that this needs a server that sets the console to RAW mode. */
pEvdev->grabDevice = xf86CheckBoolOption(pInfo->options, "GrabDevice", 0);
/* If grabDevice is set, ungrab immediately since we only want to grab
* between DEVICE_ON and DEVICE_OFF. If we never get DEVICE_ON, don't
* hold a grab. */
if (!EvdevGrabDevice(pInfo, 1, 1))
{
xf86IDrvMsg(pInfo, X_WARNING, "Device may already be configured.\n");
rc = BadMatch;
goto error;
}
EvdevInitButtonMapping(pInfo);
if (EvdevCache(pInfo) || EvdevProbe(pInfo)) {
rc = BadMatch;
goto error;
}
/* Overwrite type_name with custom-defined one (#62831).
Note: pInfo->type_name isn't freed so we need to manually do this
*/
pEvdev->type_name = xf86SetStrOption(pInfo->options,
"TypeName",
pInfo->type_name);
pInfo->type_name = pEvdev->type_name;
EvdevAddDevice(pInfo);
if (pEvdev->flags & EVDEV_BUTTON_EVENTS)
{
EvdevMBEmuPreInit(pInfo);
Evdev3BEmuPreInit(pInfo);
EvdevWheelEmuPreInit(pInfo);
EvdevDragLockPreInit(pInfo);
}
return Success;
error:
EvdevCloseDevice(pInfo);
return rc;
}
_X_EXPORT InputDriverRec EVDEV = {
1,
"evdev",
NULL,
EvdevPreInit,
EvdevUnInit,
NULL,
evdevDefaults
};
static void
EvdevUnplug(pointer p)
{
}
static pointer
EvdevPlug(pointer module,
pointer options,
int *errmaj,
int *errmin)
{
xf86AddInputDriver(&EVDEV, module, 0);
return module;
}
static XF86ModuleVersionInfo EvdevVersionRec =
{
"evdev",
MODULEVENDORSTRING,
MODINFOSTRING1,
MODINFOSTRING2,
XORG_VERSION_CURRENT,
PACKAGE_VERSION_MAJOR, PACKAGE_VERSION_MINOR, PACKAGE_VERSION_PATCHLEVEL,
ABI_CLASS_XINPUT,
ABI_XINPUT_VERSION,
MOD_CLASS_XINPUT,
{0, 0, 0, 0}
};
_X_EXPORT XF86ModuleData evdevModuleData =
{
&EvdevVersionRec,
EvdevPlug,
EvdevUnplug
};
/* Return an index value for a given button event code
* returns 0 on non-button event.
*/
unsigned int
EvdevUtilButtonEventToButtonNumber(EvdevPtr pEvdev, int code)
{
switch (code)
{
/* Mouse buttons */
case BTN_LEFT:
return 1;
case BTN_MIDDLE:
return 2;
case BTN_RIGHT:
return 3;
case BTN_SIDE ... BTN_JOYSTICK - 1:
return 8 + code - BTN_SIDE;
/* Generic buttons */
case BTN_0 ... BTN_2:
return 1 + code - BTN_0;
case BTN_3 ... BTN_MOUSE - 1:
return 8 + code - BTN_3;
/* Tablet stylus buttons */
case BTN_TOUCH ... BTN_STYLUS2:
return 1 + code - BTN_TOUCH;
/* The rest */
default:
/* Ignore */
return 0;
}
}
static void EvdevInitOneAxisLabel(EvdevPtr pEvdev, int mapped_axis,
const char **labels, int label_idx, Atom *atoms)
{
Atom atom;
if (mapped_axis == -1)
return;
atom = XIGetKnownProperty(labels[label_idx]);
if (!atom) /* Should not happen */
return;
atoms[mapped_axis] = atom;
}
static void EvdevInitAxesLabels(EvdevPtr pEvdev, int mode, int natoms, Atom *atoms)
{
int axis;
memset(atoms, 0, natoms * sizeof(Atom));
/* rel[0] and [1] are always mapped, so we get the rel labels. if we
have abs x/y, the labels will be overwritten with the right one */
for (axis = 0; axis < ArrayLength(rel_labels); axis++)
EvdevInitOneAxisLabel(pEvdev, pEvdev->rel_axis_map[axis], rel_labels, axis, atoms);
for (axis = 0; axis < ArrayLength(abs_labels); axis++)
EvdevInitOneAxisLabel(pEvdev, pEvdev->abs_axis_map[axis], abs_labels, axis, atoms);
}
static void EvdevInitButtonLabels(EvdevPtr pEvdev, int natoms, Atom *atoms)
{
Atom atom;
int button, bmap;
/* First, make sure all atoms are initialized */
atom = XIGetKnownProperty(BTN_LABEL_PROP_BTN_UNKNOWN);
for (button = 0; button < natoms; button++)
atoms[button] = atom;
for (button = BTN_MISC; button < BTN_JOYSTICK; button++)
{
if (EvdevBitIsSet(pEvdev->key_bitmask, button))
{
int group = (button % 0x100)/16;
int idx = button - ((button/16) * 16);
if (!btn_labels[group][idx])
continue;
atom = XIGetKnownProperty(btn_labels[group][idx]);
if (!atom)
continue;
/* Props are 0-indexed, button numbers start with 1 */
bmap = EvdevUtilButtonEventToButtonNumber(pEvdev, button) - 1;
atoms[bmap] = atom;
}
}
/* wheel buttons, hardcoded anyway */
if (natoms > 3)
atoms[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP);
if (natoms > 4)
atoms[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN);
if (natoms > 5)
atoms[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT);
if (natoms > 6)
atoms[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT);
}
static void
EvdevInitProperty(DeviceIntPtr dev)
{
InputInfoPtr pInfo = dev->public.devicePrivate;
EvdevPtr pEvdev = pInfo->private;
int rc;
char *device_node;
CARD32 product[2];
prop_product_id = MakeAtom(XI_PROP_PRODUCT_ID, strlen(XI_PROP_PRODUCT_ID), TRUE);
product[0] = pEvdev->id_vendor;
product[1] = pEvdev->id_product;
rc = XIChangeDeviceProperty(dev, prop_product_id, XA_INTEGER, 32,
PropModeReplace, 2, product, FALSE);
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_product_id, FALSE);
/* Device node property */
device_node = strdup(pEvdev->device);
prop_device = MakeAtom(XI_PROP_DEVICE_NODE,
strlen(XI_PROP_DEVICE_NODE), TRUE);
rc = XIChangeDeviceProperty(dev, prop_device, XA_STRING, 8,
PropModeReplace,
strlen(device_node), device_node,
FALSE);
free(device_node);
if (rc != Success)
return;
if (EvdevDeviceIsVirtual(pEvdev->device))
{
BOOL virtual = 1;
prop_virtual = MakeAtom(XI_PROP_VIRTUAL_DEVICE,
strlen(XI_PROP_VIRTUAL_DEVICE), TRUE);
rc = XIChangeDeviceProperty(dev, prop_virtual, XA_INTEGER, 8,
PropModeReplace, 1, &virtual, FALSE);
XISetDevicePropertyDeletable(dev, prop_virtual, FALSE);
}
XISetDevicePropertyDeletable(dev, prop_device, FALSE);
if (pEvdev->flags & (EVDEV_RELATIVE_EVENTS | EVDEV_ABSOLUTE_EVENTS))
{
BOOL invert[2];
invert[0] = pEvdev->invert_x;
invert[1] = pEvdev->invert_y;
prop_invert = MakeAtom(EVDEV_PROP_INVERT_AXES, strlen(EVDEV_PROP_INVERT_AXES), TRUE);
rc = XIChangeDeviceProperty(dev, prop_invert, XA_INTEGER, 8,
PropModeReplace, 2,
invert, FALSE);
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_invert, FALSE);
prop_calibration = MakeAtom(EVDEV_PROP_CALIBRATION,
strlen(EVDEV_PROP_CALIBRATION), TRUE);
if (pEvdev->flags & EVDEV_CALIBRATED) {
int calibration[4];
calibration[0] = pEvdev->calibration.min_x;
calibration[1] = pEvdev->calibration.max_x;
calibration[2] = pEvdev->calibration.min_y;
calibration[3] = pEvdev->calibration.max_y;
rc = XIChangeDeviceProperty(dev, prop_calibration, XA_INTEGER,
32, PropModeReplace, 4, calibration,
FALSE);
} else if (pEvdev->flags & EVDEV_ABSOLUTE_EVENTS) {
rc = XIChangeDeviceProperty(dev, prop_calibration, XA_INTEGER,
32, PropModeReplace, 0, NULL,
FALSE);
}
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_calibration, FALSE);
prop_swap = MakeAtom(EVDEV_PROP_SWAP_AXES,
strlen(EVDEV_PROP_SWAP_AXES), TRUE);
rc = XIChangeDeviceProperty(dev, prop_swap, XA_INTEGER, 8,
PropModeReplace, 1, &pEvdev->swap_axes, FALSE);
if (rc != Success)
return;
XISetDevicePropertyDeletable(dev, prop_swap, FALSE);
/* Axis labelling */
if ((pEvdev->num_vals > 0) && (prop_axis_label = XIGetKnownProperty(AXIS_LABEL_PROP)))
{
int mode;
int num_axes = pEvdev->num_vals + pEvdev->num_mt_vals;
Atom atoms[num_axes];
if (pEvdev->flags & EVDEV_ABSOLUTE_EVENTS)
mode = Absolute;
else if (pEvdev->flags & EVDEV_RELATIVE_EVENTS)
mode = Relative;
else {
xf86IDrvMsg(pInfo, X_ERROR, "BUG: mode is neither absolute nor relative\n");
mode = Absolute;
}
EvdevInitAxesLabels(pEvdev, mode, num_axes, atoms);
XIChangeDeviceProperty(dev, prop_axis_label, XA_ATOM, 32,
PropModeReplace, num_axes, atoms, FALSE);
XISetDevicePropertyDeletable(dev, prop_axis_label, FALSE);
}
/* Button labelling */
if ((pEvdev->num_buttons > 0) && (prop_btn_label = XIGetKnownProperty(BTN_LABEL_PROP)))
{
Atom atoms[EVDEV_MAXBUTTONS];
EvdevInitButtonLabels(pEvdev, EVDEV_MAXBUTTONS, atoms);
XIChangeDeviceProperty(dev, prop_btn_label, XA_ATOM, 32,
PropModeReplace, pEvdev->num_buttons, atoms, FALSE);
XISetDevicePropertyDeletable(dev, prop_btn_label, FALSE);
}
}
}
static int
EvdevSetProperty(DeviceIntPtr dev, Atom atom, XIPropertyValuePtr val,
BOOL checkonly)
{
InputInfoPtr pInfo = dev->public.devicePrivate;
EvdevPtr pEvdev = pInfo->private;
if (atom == prop_invert)
{
BOOL* data;
if (val->format != 8 || val->size != 2 || val->type != XA_INTEGER)
return BadMatch;
if (!checkonly)
{
data = (BOOL*)val->data;
pEvdev->invert_x = data[0];
pEvdev->invert_y = data[1];
}
} else if (atom == prop_calibration)
{
if (val->format != 32 || val->type != XA_INTEGER)
return BadMatch;
if (val->size != 4 && val->size != 0)
return BadMatch;
if (!checkonly)
EvdevSetCalibration(pInfo, val->size, val->data);
} else if (atom == prop_swap)
{
if (val->format != 8 || val->type != XA_INTEGER || val->size != 1)
return BadMatch;
if (!checkonly)
pEvdev->swap_axes = *((BOOL*)val->data);
} else if (atom == prop_axis_label || atom == prop_btn_label ||
atom == prop_product_id || atom == prop_device ||
atom == prop_virtual)
return BadAccess; /* Read-only properties */
return Success;
}
|