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

#include "xwayland.h"

#include <linux/input.h>

#include <sys/mman.h>
#include <xkbsrv.h>
#include <xserver-properties.h>
#include <inpututils.h>
#include <mipointer.h>
#include <mipointrst.h>
#include <misc.h>
#include "tablet-unstable-v2-client-protocol.h"

struct axis_discrete_pending {
    struct xorg_list l;
    uint32_t axis;
    int32_t discrete;
};

struct sync_pending {
    struct xorg_list l;
    DeviceIntPtr pending_dev;
};

static void
xwl_pointer_warp_emulator_handle_motion(struct xwl_pointer_warp_emulator *warp_emulator,
                                        double dx,
                                        double dy,
                                        double dx_unaccel,
                                        double dy_unaccel);
static void
xwl_pointer_warp_emulator_maybe_lock(struct xwl_pointer_warp_emulator *warp_emulator,
                                     struct xwl_window *xwl_window,
                                     SpritePtr sprite,
                                     int x, int y);

static void
xwl_seat_destroy_confined_pointer(struct xwl_seat *xwl_seat);

static void
init_tablet_manager_seat(struct xwl_screen *xwl_screen,
                         struct xwl_seat *xwl_seat);
static void
release_tablet_manager_seat(struct xwl_seat *xwl_seat);

static void
xwl_pointer_control(DeviceIntPtr device, PtrCtrl *ctrl)
{
    /* Nothing to do, dix handles all settings */
}

static Bool
init_pointer_buttons(DeviceIntPtr device)
{
#define NBUTTONS 10
    BYTE map[NBUTTONS + 1];
    int i = 0;
    Atom btn_labels[NBUTTONS] = { 0 };

    for (i = 1; i <= NBUTTONS; i++)
        map[i] = i;

    btn_labels[0] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_LEFT);
    btn_labels[1] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_MIDDLE);
    btn_labels[2] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_RIGHT);
    btn_labels[3] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_UP);
    btn_labels[4] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_WHEEL_DOWN);
    btn_labels[5] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_LEFT);
    btn_labels[6] = XIGetKnownProperty(BTN_LABEL_PROP_BTN_HWHEEL_RIGHT);
    /* don't know about the rest */

    if (!InitButtonClassDeviceStruct(device, NBUTTONS, btn_labels, map))
        return FALSE;

    return TRUE;
}

static int
xwl_pointer_proc(DeviceIntPtr device, int what)
{
#define NAXES 4
    Atom axes_labels[NAXES] = { 0 };

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;

        if (!init_pointer_buttons(device))
            return BadValue;

        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
        axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_HWHEEL);
        axes_labels[3] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_WHEEL);

        if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
                                           GetMotionHistorySize(), Absolute))
            return BadValue;

        /* Valuators */
        InitValuatorAxisStruct(device, 0, axes_labels[0],
                               0, 0xFFFF, 10000, 0, 10000, Absolute);
        InitValuatorAxisStruct(device, 1, axes_labels[1],
                               0, 0xFFFF, 10000, 0, 10000, Absolute);
        InitValuatorAxisStruct(device, 2, axes_labels[2],
                               NO_AXIS_LIMITS, NO_AXIS_LIMITS, 0, 0, 0, Relative);
        InitValuatorAxisStruct(device, 3, axes_labels[3],
                               NO_AXIS_LIMITS, NO_AXIS_LIMITS, 0, 0, 0, Relative);

        SetScrollValuator(device, 2, SCROLL_TYPE_HORIZONTAL, 1.0, SCROLL_FLAG_NONE);
        SetScrollValuator(device, 3, SCROLL_TYPE_VERTICAL, 1.0, SCROLL_FLAG_PREFERRED);

        if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
            return BadValue;

        return Success;

    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;

#undef NBUTTONS
#undef NAXES
}

static int
xwl_pointer_proc_relative(DeviceIntPtr device, int what)
{
#define NAXES 2
    Atom axes_labels[NAXES] = { 0 };

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;

        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_X);
        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_REL_Y);

        /*
         * We'll never send buttons, but XGetPointerMapping might in certain
         * situations make the client think we have no buttons.
         */
        if (!init_pointer_buttons(device))
            return BadValue;

        if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
                                           GetMotionHistorySize(), Relative))
            return BadValue;

        /* Valuators */
        InitValuatorAxisStruct(device, 0, axes_labels[0],
                               NO_AXIS_LIMITS, NO_AXIS_LIMITS, 1, 0, 1, Relative);
        InitValuatorAxisStruct(device, 1, axes_labels[1],
                               NO_AXIS_LIMITS, NO_AXIS_LIMITS, 1, 0, 1, Relative);

        if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
            return BadValue;

        return Success;

    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;

#undef NAXES
}

static void
xwl_keyboard_control(DeviceIntPtr device, KeybdCtrl *ctrl)
{
}

static int
xwl_keyboard_proc(DeviceIntPtr device, int what)
{
    struct xwl_seat *xwl_seat = device->public.devicePrivate;
    int len;

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;
        if (xwl_seat->keymap)
            len = strnlen(xwl_seat->keymap, xwl_seat->keymap_size);
        else
            len = 0;
        if (!InitKeyboardDeviceStructFromString(device, xwl_seat->keymap,
                                                len,
                                                NULL, xwl_keyboard_control))
            return BadValue;

        return Success;
    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;
}

static int
xwl_touch_proc(DeviceIntPtr device, int what)
{
#define NTOUCHPOINTS 20
#define NBUTTONS 1
#define NAXES 2
    Atom btn_labels[NBUTTONS] = { 0 };
    Atom axes_labels[NAXES] = { 0 };
    BYTE map[NBUTTONS + 1] = { 0 };

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;

        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_MT_POSITION_X);
        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_MT_POSITION_Y);

        if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
                                           GetMotionHistorySize(), Absolute))
            return BadValue;

        if (!InitButtonClassDeviceStruct(device, NBUTTONS, btn_labels, map))
            return BadValue;

        if (!InitTouchClassDeviceStruct(device, NTOUCHPOINTS,
                                        XIDirectTouch, NAXES))
            return BadValue;

        /* Valuators */
        InitValuatorAxisStruct(device, 0, axes_labels[0],
                               0, 0xFFFF, 10000, 0, 10000, Absolute);
        InitValuatorAxisStruct(device, 1, axes_labels[1],
                               0, 0xFFFF, 10000, 0, 10000, Absolute);
        return Success;

    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;
#undef NAXES
#undef NBUTTONS
#undef NTOUCHPOINTS
}

static int
xwl_tablet_proc(DeviceIntPtr device, int what)
{
#define NBUTTONS 9
#define NAXES 6
    Atom btn_labels[NBUTTONS] = { 0 };
    Atom axes_labels[NAXES] = { 0 };
    BYTE map[NBUTTONS + 1] = { 0 };
    int i;

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;

        for (i = 1; i <= NBUTTONS; i++)
            map[i] = i;

        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
        axes_labels[2] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_PRESSURE);
        axes_labels[3] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_TILT_X);
        axes_labels[4] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_TILT_Y);
        axes_labels[5] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_WHEEL);

        if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
                                           GetMotionHistorySize(), Absolute))
            return BadValue;

        /* Valuators - match the xf86-input-wacom ranges */
        InitValuatorAxisStruct(device, 0, axes_labels[0],
                               0, 262143, 10000, 0, 10000, Absolute);
        InitValuatorAxisStruct(device, 1, axes_labels[1],
                               0, 262143, 10000, 0, 10000, Absolute);
        /* pressure */
        InitValuatorAxisStruct(device, 2, axes_labels[2],
                               0, 65535, 1, 0, 1, Absolute);
        /* tilt x */
        InitValuatorAxisStruct(device, 3, axes_labels[3],
                               -64, 63, 57, 0, 57, Absolute);
        /* tilt y */
        InitValuatorAxisStruct(device, 4, axes_labels[4],
                               -64, 63, 57, 0, 57, Absolute);
        /* abs wheel (airbrush) or rotation (artpen) */
        InitValuatorAxisStruct(device, 5, axes_labels[5],
                               -900, 899, 1, 0, 1, Absolute);

        if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
            return BadValue;

        if (!InitButtonClassDeviceStruct(device, NBUTTONS, btn_labels, map))
            return BadValue;

        return Success;

    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;
#undef NAXES
#undef NBUTTONS
}

static void
pointer_handle_enter(void *data, struct wl_pointer *pointer,
                     uint32_t serial, struct wl_surface *surface,
                     wl_fixed_t sx_w, wl_fixed_t sy_w)
{
    struct xwl_seat *xwl_seat = data;
    DeviceIntPtr dev = xwl_seat->pointer;
    DeviceIntPtr master;
    int i;
    int sx = wl_fixed_to_int(sx_w);
    int sy = wl_fixed_to_int(sy_w);
    int dx, dy;
    ScreenPtr pScreen = xwl_seat->xwl_screen->screen;
    ValuatorMask mask;

    /* There's a race here where if we create and then immediately
     * destroy a surface, we might end up in a state where the Wayland
     * compositor sends us an event for a surface that doesn't exist.
     *
     * Don't process enter events in this case.
     */
    if (surface == NULL)
        return;

    xwl_seat->xwl_screen->serial = serial;
    xwl_seat->pointer_enter_serial = serial;

    xwl_seat->focus_window = wl_surface_get_user_data(surface);
    dx = xwl_seat->focus_window->window->drawable.x;
    dy = xwl_seat->focus_window->window->drawable.y;

    /* We just entered a new xwindow, forget about the old last xwindow */
    xwl_seat->last_xwindow = NullWindow;

    master = GetMaster(dev, POINTER_OR_FLOAT);
    (*pScreen->SetCursorPosition) (dev, pScreen, dx + sx, dy + sy, TRUE);

    miPointerInvalidateSprite(master);

    CheckMotion(NULL, master);

    /* Ideally, X clients shouldn't see these button releases.  When
     * the pointer leaves a window with buttons down, it means that
     * the wayland compositor has grabbed the pointer.  The button
     * release event is consumed by whatever grab in the compositor
     * and won't be sent to clients (the X server is a client).
     * However, we need to reset X's idea of which buttons are up and
     * down, and they're all up (by definition) when the pointer
     * enters a window.  We should figure out a way to swallow these
     * events, perhaps using an X grab whenever the pointer is not in
     * any X window, but for now just send the events. */
    valuator_mask_zero(&mask);
    for (i = 0; i < dev->button->numButtons; i++)
        if (BitIsOn(dev->button->down, i))
            QueuePointerEvents(dev, ButtonRelease, i, 0, &mask);

    /* The last cursor frame we commited before the pointer left one
     * of our surfaces might not have been shown. In that case we'll
     * have a cursor surface frame callback pending which we need to
     * clear so that we can continue submitting new cursor frames. */
    if (xwl_seat->cursor.frame_cb) {
        wl_callback_destroy(xwl_seat->cursor.frame_cb);
        xwl_seat->cursor.frame_cb = NULL;
        xwl_seat_set_cursor(xwl_seat);
    }

    if (xwl_seat->pointer_warp_emulator) {
        xwl_pointer_warp_emulator_maybe_lock(xwl_seat->pointer_warp_emulator,
                                             xwl_seat->focus_window,
                                             NULL, 0, 0);
    }
}

static void
pointer_handle_leave(void *data, struct wl_pointer *pointer,
                     uint32_t serial, struct wl_surface *surface)
{
    struct xwl_seat *xwl_seat = data;
    DeviceIntPtr dev = xwl_seat->pointer;

    xwl_seat->xwl_screen->serial = serial;

    /* The pointer has left a known xwindow, save it for a possible match
     * in sprite_check_lost_focus()
     */
    if (xwl_seat->focus_window) {
        xwl_seat->last_xwindow = xwl_seat->focus_window->window;
        xwl_seat->focus_window = NULL;
        CheckMotion(NULL, GetMaster(dev, POINTER_OR_FLOAT));
    }
}

static void
dispatch_pointer_motion_event(struct xwl_seat *xwl_seat)
{
    ValuatorMask mask;

    if (xwl_seat->pointer_warp_emulator &&
        xwl_seat->pending_pointer_event.has_relative) {
        double dx;
        double dy;
        double dx_unaccel;
        double dy_unaccel;

        dx = xwl_seat->pending_pointer_event.dx;
        dy = xwl_seat->pending_pointer_event.dy;
        dx_unaccel = xwl_seat->pending_pointer_event.dx_unaccel;
        dy_unaccel = xwl_seat->pending_pointer_event.dy_unaccel;
        xwl_pointer_warp_emulator_handle_motion(xwl_seat->pointer_warp_emulator,
                                                dx, dy,
                                                dx_unaccel, dy_unaccel);
    } else if (xwl_seat->pending_pointer_event.has_absolute ||
               xwl_seat->pending_pointer_event.has_relative) {
        int x;
        int y;

        if (xwl_seat->pending_pointer_event.has_absolute) {
            int sx = wl_fixed_to_int(xwl_seat->pending_pointer_event.x);
            int sy = wl_fixed_to_int(xwl_seat->pending_pointer_event.y);
            int dx = xwl_seat->focus_window->window->drawable.x;
            int dy = xwl_seat->focus_window->window->drawable.y;

            x = dx + sx;
            y = dy + sy;
        } else {
            miPointerGetPosition(xwl_seat->pointer, &x, &y);
        }

        valuator_mask_zero(&mask);
        if (xwl_seat->pending_pointer_event.has_relative) {
            double dx_unaccel;
            double dy_unaccel;

            dx_unaccel = xwl_seat->pending_pointer_event.dx_unaccel;
            dy_unaccel = xwl_seat->pending_pointer_event.dy_unaccel;
            valuator_mask_set_absolute_unaccelerated(&mask, 0, x, dx_unaccel);
            valuator_mask_set_absolute_unaccelerated(&mask, 1, y, dy_unaccel);
        } else {
            valuator_mask_set(&mask, 0, x);
            valuator_mask_set(&mask, 1, y);
        }

        QueuePointerEvents(xwl_seat->pointer, MotionNotify, 0,
                           POINTER_ABSOLUTE | POINTER_SCREEN, &mask);
    }

    xwl_seat->pending_pointer_event.has_absolute = FALSE;
    xwl_seat->pending_pointer_event.has_relative = FALSE;
}

static void
pointer_handle_motion(void *data, struct wl_pointer *pointer,
                      uint32_t time, wl_fixed_t sx_w, wl_fixed_t sy_w)
{
    struct xwl_seat *xwl_seat = data;

    if (!xwl_seat->focus_window)
        return;

    xwl_seat->pending_pointer_event.has_absolute = TRUE;
    xwl_seat->pending_pointer_event.x = sx_w;
    xwl_seat->pending_pointer_event.y = sy_w;

    if (wl_proxy_get_version((struct wl_proxy *) xwl_seat->wl_pointer) < 5)
        dispatch_pointer_motion_event(xwl_seat);
}

static void
pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
                      uint32_t time, uint32_t button, uint32_t state)
{
    struct xwl_seat *xwl_seat = data;
    int index;
    ValuatorMask mask;

    xwl_seat->xwl_screen->serial = serial;

    switch (button) {
    case BTN_LEFT:
        index = 1;
        break;
    case BTN_MIDDLE:
        index = 2;
        break;
    case BTN_RIGHT:
        index = 3;
        break;
    default:
        /* Skip indexes 4-7: they are used for vertical and horizontal scroll.
           The rest of the buttons go in order: BTN_SIDE becomes 8, etc. */
        index = 8 + button - BTN_SIDE;
        break;
    }

    valuator_mask_zero(&mask);
    QueuePointerEvents(xwl_seat->pointer,
                       state ? ButtonPress : ButtonRelease, index, 0, &mask);
}

static void
pointer_handle_axis(void *data, struct wl_pointer *pointer,
                    uint32_t time, uint32_t axis, wl_fixed_t value)
{
    struct xwl_seat *xwl_seat = data;
    int index;
    const int divisor = 10;
    ValuatorMask mask;
    struct axis_discrete_pending *pending = NULL;
    struct axis_discrete_pending *iter;

    switch (axis) {
    case WL_POINTER_AXIS_VERTICAL_SCROLL:
        index = 3;
        break;
    case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
        index = 2;
        break;
    default:
        return;
    }

    xorg_list_for_each_entry(iter, &xwl_seat->axis_discrete_pending, l) {
        if (iter->axis == axis) {
            pending = iter;
            break;
        }
    }

    valuator_mask_zero(&mask);

    if (pending) {
        valuator_mask_set(&mask, index, pending->discrete);
        xorg_list_del(&pending->l);
        free(pending);
    } else {
        valuator_mask_set_double(&mask, index, wl_fixed_to_double(value) / divisor);
    }
    QueuePointerEvents(xwl_seat->pointer, MotionNotify, 0, POINTER_RELATIVE, &mask);
}

static void
pointer_handle_frame(void *data, struct wl_pointer *wl_pointer)
{
    struct xwl_seat *xwl_seat = data;

    if (!xwl_seat->focus_window)
        return;

    dispatch_pointer_motion_event(xwl_seat);
}

static void
pointer_handle_axis_source(void *data, struct wl_pointer *wl_pointer, uint32_t axis_source)
{
}

static void
pointer_handle_axis_stop(void *data, struct wl_pointer *wl_pointer,
                         uint32_t time, uint32_t axis)
{
}

static void
pointer_handle_axis_discrete(void *data, struct wl_pointer *wl_pointer,
                             uint32_t axis, int32_t discrete)
{
    struct xwl_seat *xwl_seat = data;

    struct axis_discrete_pending *pending = malloc(sizeof *pending);
    if (!pending)
        return;

    pending->axis = axis;
    pending->discrete = discrete;

    xorg_list_add(&pending->l, &xwl_seat->axis_discrete_pending);
}

static const struct wl_pointer_listener pointer_listener = {
    pointer_handle_enter,
    pointer_handle_leave,
    pointer_handle_motion,
    pointer_handle_button,
    pointer_handle_axis,
    pointer_handle_frame,
    pointer_handle_axis_source,
    pointer_handle_axis_stop,
    pointer_handle_axis_discrete,
};

static void
relative_pointer_handle_relative_motion(void *data,
                                        struct zwp_relative_pointer_v1 *zwp_relative_pointer_v1,
                                        uint32_t utime_hi,
                                        uint32_t utime_lo,
                                        wl_fixed_t dxf,
                                        wl_fixed_t dyf,
                                        wl_fixed_t dx_unaccelf,
                                        wl_fixed_t dy_unaccelf)
{
    struct xwl_seat *xwl_seat = data;

    xwl_seat->pending_pointer_event.has_relative = TRUE;
    xwl_seat->pending_pointer_event.dx = wl_fixed_to_double(dxf);
    xwl_seat->pending_pointer_event.dy = wl_fixed_to_double(dyf);
    xwl_seat->pending_pointer_event.dx_unaccel = wl_fixed_to_double(dx_unaccelf);
    xwl_seat->pending_pointer_event.dy_unaccel = wl_fixed_to_double(dy_unaccelf);

    if (!xwl_seat->focus_window)
        return;

    if (wl_proxy_get_version((struct wl_proxy *) xwl_seat->wl_pointer) < 5)
        dispatch_pointer_motion_event(xwl_seat);
}

static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
    relative_pointer_handle_relative_motion,
};

static void
keyboard_handle_key(void *data, struct wl_keyboard *keyboard, uint32_t serial,
                    uint32_t time, uint32_t key, uint32_t state)
{
    struct xwl_seat *xwl_seat = data;
    uint32_t *k, *end;

    xwl_seat->xwl_screen->serial = serial;

    end = (uint32_t *) ((char *) xwl_seat->keys.data + xwl_seat->keys.size);
    for (k = xwl_seat->keys.data; k < end; k++) {
        if (*k == key)
            *k = *--end;
    }
    xwl_seat->keys.size = (char *) end - (char *) xwl_seat->keys.data;
    if (state) {
        k = wl_array_add(&xwl_seat->keys, sizeof *k);
        *k = key;
    }

    QueueKeyboardEvents(xwl_seat->keyboard,
                        state ? KeyPress : KeyRelease, key + 8);
}

static void
keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
                       uint32_t format, int fd, uint32_t size)
{
    struct xwl_seat *xwl_seat = data;
    DeviceIntPtr master;
    XkbDescPtr xkb;
    XkbChangesRec changes = { 0 };

    if (xwl_seat->keymap)
        munmap(xwl_seat->keymap, xwl_seat->keymap_size);

    xwl_seat->keymap_size = size;
    xwl_seat->keymap = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    if (xwl_seat->keymap == MAP_FAILED) {
        xwl_seat->keymap_size = 0;
        xwl_seat->keymap = NULL;
        goto out;
    }

    xkb = XkbCompileKeymapFromString(xwl_seat->keyboard, xwl_seat->keymap,
                                     strnlen(xwl_seat->keymap,
                                             xwl_seat->keymap_size));
    if (!xkb)
        goto out;

    XkbUpdateDescActions(xkb, xkb->min_key_code, XkbNumKeys(xkb), &changes);

    if (xwl_seat->keyboard->key)
        /* Keep the current controls */
        XkbCopyControls(xkb, xwl_seat->keyboard->key->xkbInfo->desc);

    XkbDeviceApplyKeymap(xwl_seat->keyboard, xkb);

    master = GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD);
    if (master)
        XkbDeviceApplyKeymap(master, xkb);

    XkbFreeKeyboard(xkb, XkbAllComponentsMask, TRUE);

 out:
    close(fd);
}

static void
keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
                      uint32_t serial,
                      struct wl_surface *surface, struct wl_array *keys)
{
    struct xwl_seat *xwl_seat = data;
    uint32_t *k;

    xwl_seat->xwl_screen->serial = serial;
    xwl_seat->keyboard_focus = surface;

    wl_array_copy(&xwl_seat->keys, keys);
    wl_array_for_each(k, &xwl_seat->keys)
        QueueKeyboardEvents(xwl_seat->keyboard, EnterNotify, *k + 8);
}

static void
keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
                      uint32_t serial, struct wl_surface *surface)
{
    struct xwl_seat *xwl_seat = data;
    uint32_t *k;

    xwl_seat->xwl_screen->serial = serial;

    wl_array_for_each(k, &xwl_seat->keys)
        QueueKeyboardEvents(xwl_seat->keyboard, LeaveNotify, *k + 8);

    xwl_seat->keyboard_focus = NULL;
}

static void
keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
                          uint32_t serial, uint32_t mods_depressed,
                          uint32_t mods_latched, uint32_t mods_locked,
                          uint32_t group)
{
    struct xwl_seat *xwl_seat = data;
    DeviceIntPtr dev;
    XkbStateRec old_state, *new_state;
    xkbStateNotify sn;
    CARD16 changed;

    mieqProcessInputEvents();

    for (dev = inputInfo.devices; dev; dev = dev->next) {
        if (dev != xwl_seat->keyboard &&
            dev != GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD))
            continue;

        old_state = dev->key->xkbInfo->state;
        new_state = &dev->key->xkbInfo->state;

        new_state->locked_group = group & XkbAllGroupsMask;
        new_state->base_mods = mods_depressed & XkbAllModifiersMask;
        new_state->locked_mods = mods_locked & XkbAllModifiersMask;
        XkbLatchModifiers(dev, XkbAllModifiersMask,
                          mods_latched & XkbAllModifiersMask);

        XkbComputeDerivedState(dev->key->xkbInfo);

        changed = XkbStateChangedFlags(&old_state, new_state);
        if (!changed)
            continue;

        sn.keycode = 0;
        sn.eventType = 0;
        sn.requestMajor = XkbReqCode;
        sn.requestMinor = X_kbLatchLockState;   /* close enough */
        sn.changed = changed;
        XkbSendStateNotify(dev, &sn);
    }
}

static void
remove_sync_pending(DeviceIntPtr dev)
{
    struct xwl_seat *xwl_seat = dev->public.devicePrivate;
    struct sync_pending *p, *npd;

    xorg_list_for_each_entry_safe(p, npd, &xwl_seat->sync_pending, l) {
        if (p->pending_dev == dev) {
            xorg_list_del(&xwl_seat->sync_pending);
            free (p);
            return;
        }
    }
}

static void
sync_callback(void *data, struct wl_callback *callback, uint32_t serial)
{
    DeviceIntPtr dev = (DeviceIntPtr) data;

    remove_sync_pending(dev);
    wl_callback_destroy(callback);
}

static const struct wl_callback_listener sync_listener = {
   sync_callback
};

static Bool
keyboard_check_repeat (DeviceIntPtr dev, XkbSrvInfoPtr xkbi, unsigned key)
{
    struct xwl_seat *xwl_seat = dev->public.devicePrivate;
    struct xwl_screen *xwl_screen = xwl_seat->xwl_screen;
    struct wl_callback *callback;
    struct sync_pending *p;

    /* Make sure we didn't miss a possible reply from the compositor */
    xwl_sync_events (xwl_screen);

    xorg_list_for_each_entry(p, &xwl_seat->sync_pending, l) {
        if (p->pending_dev == dev) {
            ErrorF("Key repeat discarded, Wayland compositor doesn't "
                   "seem to be processing events fast enough!\n");

            return FALSE;
        }
    }

    p = xnfalloc(sizeof(struct sync_pending));
    p->pending_dev = dev;
    callback = wl_display_sync (xwl_screen->display);
    xorg_list_add(&p->l, &xwl_seat->sync_pending);

    wl_callback_add_listener(callback, &sync_listener, dev);

    return TRUE;
}

static void
keyboard_handle_repeat_info (void *data, struct wl_keyboard *keyboard,
                             int32_t rate, int32_t delay)
{
    struct xwl_seat *xwl_seat = data;
    DeviceIntPtr dev;
    XkbControlsPtr ctrl;

    if (rate < 0 || delay < 0) {
        ErrorF("Wrong rate/delay: %d, %d\n", rate, delay);
        return;
    }

    for (dev = inputInfo.devices; dev; dev = dev->next) {
        if (dev != xwl_seat->keyboard &&
            dev != GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD))
            continue;

        if (rate != 0) {
            ctrl = dev->key->xkbInfo->desc->ctrls;
            ctrl->repeat_delay = delay;
            /* rate is number of keys per second */
            ctrl->repeat_interval = 1000 / rate;

            XkbSetRepeatKeys(dev, -1, AutoRepeatModeOn);
        } else
            XkbSetRepeatKeys(dev, -1, AutoRepeatModeOff);
    }
}

static const struct wl_keyboard_listener keyboard_listener = {
    keyboard_handle_keymap,
    keyboard_handle_enter,
    keyboard_handle_leave,
    keyboard_handle_key,
    keyboard_handle_modifiers,
    keyboard_handle_repeat_info,
};

static struct xwl_touch *
xwl_seat_lookup_touch(struct xwl_seat *xwl_seat, int32_t id)
{
    struct xwl_touch *xwl_touch, *next_xwl_touch;

    xorg_list_for_each_entry_safe(xwl_touch, next_xwl_touch,
                                  &xwl_seat->touches, link_touch) {
        if (xwl_touch->id == id)
            return xwl_touch;
    }

    return NULL;
}

static void
xwl_touch_send_event(struct xwl_touch *xwl_touch,
                     struct xwl_seat *xwl_seat, int type)
{
    double dx, dy, x, y;
    ValuatorMask mask;

    dx = xwl_touch->window->window->drawable.x;
    dy = xwl_touch->window->window->drawable.y;

    x = (dx + xwl_touch->x) * 0xFFFF / xwl_seat->xwl_screen->width;
    y = (dy + xwl_touch->y) * 0xFFFF / xwl_seat->xwl_screen->height;

    valuator_mask_zero(&mask);
    valuator_mask_set_double(&mask, 0, x);
    valuator_mask_set_double(&mask, 1, y);
    QueueTouchEvents(xwl_seat->touch, type, xwl_touch->id, 0, &mask);
}

static void
touch_handle_down(void *data, struct wl_touch *wl_touch,
                  uint32_t serial, uint32_t time,
                  struct wl_surface *surface,
                  int32_t id, wl_fixed_t sx_w, wl_fixed_t sy_w)
{
    struct xwl_seat *xwl_seat = data;
    struct xwl_touch *xwl_touch;

    if (surface == NULL)
        return;

    xwl_touch = calloc(1, sizeof *xwl_touch);
    if (xwl_touch == NULL) {
        ErrorF("%s: ENOMEM\n", __func__);
        return;
    }

    xwl_touch->window = wl_surface_get_user_data(surface);
    xwl_touch->id = id;
    xwl_touch->x = wl_fixed_to_int(sx_w);
    xwl_touch->y = wl_fixed_to_int(sy_w);
    xorg_list_add(&xwl_touch->link_touch, &xwl_seat->touches);

    xwl_touch_send_event(xwl_touch, xwl_seat, XI_TouchBegin);
}

static void
touch_handle_up(void *data, struct wl_touch *wl_touch,
                uint32_t serial, uint32_t time, int32_t id)
{
    struct xwl_touch *xwl_touch;
    struct xwl_seat *xwl_seat = data;

    xwl_touch = xwl_seat_lookup_touch(xwl_seat, id);

    if (!xwl_touch)
        return;

    xwl_touch_send_event(xwl_touch, xwl_seat, XI_TouchEnd);
    xorg_list_del(&xwl_touch->link_touch);
    free(xwl_touch);
}

static void
touch_handle_motion(void *data, struct wl_touch *wl_touch,
                    uint32_t time, int32_t id,
                    wl_fixed_t sx_w, wl_fixed_t sy_w)
{
    struct xwl_seat *xwl_seat = data;
    struct xwl_touch *xwl_touch;

    xwl_touch = xwl_seat_lookup_touch(xwl_seat, id);

    if (!xwl_touch)
        return;

    xwl_touch->x = wl_fixed_to_int(sx_w);
    xwl_touch->y = wl_fixed_to_int(sy_w);
    xwl_touch_send_event(xwl_touch, xwl_seat, XI_TouchUpdate);
}

static void
touch_handle_frame(void *data, struct wl_touch *wl_touch)
{
}

static void
touch_handle_cancel(void *data, struct wl_touch *wl_touch)
{
    struct xwl_seat *xwl_seat = data;
    struct xwl_touch *xwl_touch, *next_xwl_touch;

    xorg_list_for_each_entry_safe(xwl_touch, next_xwl_touch,
                                  &xwl_seat->touches, link_touch) {
        /* We can't properly notify of cancellation to the X client
         * once it thinks it has the ownership, send at least a
         * TouchEnd event.
         */
        xwl_touch_send_event(xwl_touch, xwl_seat, XI_TouchEnd);
        xorg_list_del(&xwl_touch->link_touch);
        free(xwl_touch);
    }
}

static const struct wl_touch_listener touch_listener = {
    touch_handle_down,
    touch_handle_up,
    touch_handle_motion,
    touch_handle_frame,
    touch_handle_cancel
};

static struct xwl_seat *
find_matching_seat(DeviceIntPtr device)
{
    DeviceIntPtr dev;

    for (dev = inputInfo.devices; dev; dev = dev->next)
        if (dev->deviceProc == xwl_keyboard_proc &&
            device == GetMaster(dev, MASTER_KEYBOARD))
                return (struct xwl_seat *) dev->public.devicePrivate;

    return NULL;
}

static void
release_grab(struct xwl_seat *xwl_seat)
{
    if (xwl_seat->keyboard_grab)
        zwp_xwayland_keyboard_grab_v1_destroy(xwl_seat->keyboard_grab);
    xwl_seat->keyboard_grab = NULL;
}

static void
set_grab(struct xwl_seat *xwl_seat, struct xwl_window *xwl_window)
{
    struct xwl_screen *xwl_screen;

    if (!xwl_window)
        return;

    /* We already have a grab */
    if (xwl_seat->keyboard_grab)
        release_grab (xwl_seat);

    xwl_screen = xwl_seat->xwl_screen;
    xwl_seat->keyboard_grab =
        zwp_xwayland_keyboard_grab_manager_v1_grab_keyboard(xwl_screen->wp_grab,
                                                            xwl_window->surface,
                                                            xwl_seat->seat);
}

static void
xwl_keyboard_activate_grab(DeviceIntPtr device, GrabPtr grab, TimeStamp time, Bool passive)
{
    struct xwl_seat *xwl_seat = device->public.devicePrivate;

    /* We are not interested in passive grabs */
    if (!passive) {
        /* If the device is the MASTER_KEYBOARD, we don't have an xwl_seat */
        if (xwl_seat == NULL)
            xwl_seat = find_matching_seat(device);
        if (xwl_seat)
            set_grab(xwl_seat, xwl_window_from_window(grab->window));
    }

    ActivateKeyboardGrab(device, grab, time, passive);
}

static void
xwl_keyboard_deactivate_grab(DeviceIntPtr device)
{
    struct xwl_seat *xwl_seat = device->public.devicePrivate;

    /* If the device is the MASTER_KEYBOARD, we don't have an xwl_seat */
    if (xwl_seat == NULL)
        xwl_seat = find_matching_seat(device);
    if (xwl_seat)
        release_grab (xwl_seat);

    DeactivateKeyboardGrab(device);
}

static void
setup_keyboard_grab_handler (DeviceIntPtr device)
{
    device->deviceGrab.ActivateGrab = xwl_keyboard_activate_grab;
    device->deviceGrab.DeactivateGrab = xwl_keyboard_deactivate_grab;
}

static DeviceIntPtr
add_device(struct xwl_seat *xwl_seat,
           const char *driver, DeviceProc device_proc)
{
    DeviceIntPtr dev = NULL;
    static Atom type_atom;
    char name[32];

    dev = AddInputDevice(serverClient, device_proc, TRUE);
    if (dev == NULL)
        return NULL;

    if (type_atom == None)
        type_atom = MakeAtom(driver, strlen(driver), TRUE);
    snprintf(name, sizeof name, "%s:%d", driver, xwl_seat->id);
    AssignTypeAndName(dev, type_atom, name);
    dev->public.devicePrivate = xwl_seat;
    dev->type = SLAVE;
    dev->spriteInfo->spriteOwner = FALSE;

    return dev;
}

static void
init_pointer(struct xwl_seat *xwl_seat)
{
    xwl_seat->wl_pointer = wl_seat_get_pointer(xwl_seat->seat);
    wl_pointer_add_listener(xwl_seat->wl_pointer,
                            &pointer_listener, xwl_seat);

    if (xwl_seat->pointer == NULL) {
        xwl_seat_set_cursor(xwl_seat);
        xwl_seat->pointer =
            add_device(xwl_seat, "xwayland-pointer", xwl_pointer_proc);
        ActivateDevice(xwl_seat->pointer, TRUE);
    }
    EnableDevice(xwl_seat->pointer, TRUE);
}

static void
release_pointer(struct xwl_seat *xwl_seat)
{
    wl_pointer_release(xwl_seat->wl_pointer);
    xwl_seat->wl_pointer = NULL;

    if (xwl_seat->pointer)
        DisableDevice(xwl_seat->pointer, TRUE);
}

static void
init_relative_pointer(struct xwl_seat *xwl_seat)
{
    struct zwp_relative_pointer_manager_v1 *relative_pointer_manager =
        xwl_seat->xwl_screen->relative_pointer_manager;

    if (relative_pointer_manager) {
        xwl_seat->wp_relative_pointer =
            zwp_relative_pointer_manager_v1_get_relative_pointer(
                relative_pointer_manager, xwl_seat->wl_pointer);
        zwp_relative_pointer_v1_add_listener(xwl_seat->wp_relative_pointer,
                                             &relative_pointer_listener,
                                             xwl_seat);
    }

    if (xwl_seat->relative_pointer == NULL) {
        xwl_seat->relative_pointer =
            add_device(xwl_seat, "xwayland-relative-pointer",
                       xwl_pointer_proc_relative);
        ActivateDevice(xwl_seat->relative_pointer, TRUE);
    }
    EnableDevice(xwl_seat->relative_pointer, TRUE);
}

static void
release_relative_pointer(struct xwl_seat *xwl_seat)
{
    if (xwl_seat->wp_relative_pointer) {
        zwp_relative_pointer_v1_destroy(xwl_seat->wp_relative_pointer);
        xwl_seat->wp_relative_pointer = NULL;
    }

    if (xwl_seat->relative_pointer)
        DisableDevice(xwl_seat->relative_pointer, TRUE);
}

static void
init_keyboard(struct xwl_seat *xwl_seat)
{
    DeviceIntPtr master;

    xwl_seat->wl_keyboard = wl_seat_get_keyboard(xwl_seat->seat);
    wl_keyboard_add_listener(xwl_seat->wl_keyboard,
                             &keyboard_listener, xwl_seat);

    if (xwl_seat->keyboard == NULL) {
        xwl_seat->keyboard =
            add_device(xwl_seat, "xwayland-keyboard", xwl_keyboard_proc);
        ActivateDevice(xwl_seat->keyboard, TRUE);
    }
    EnableDevice(xwl_seat->keyboard, TRUE);
    xwl_seat->keyboard->key->xkbInfo->checkRepeat = keyboard_check_repeat;

    if (xwl_seat->xwl_screen->wp_grab) {
        /* We have Xwayland grab protocol supported by the compositor */
        master = GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD);
        if (master)
            setup_keyboard_grab_handler(master);
    }
}

static void
release_keyboard(struct xwl_seat *xwl_seat)
{
    release_grab(xwl_seat);
    wl_keyboard_release(xwl_seat->wl_keyboard);
    xwl_seat->wl_keyboard = NULL;

    if (xwl_seat->keyboard) {
        remove_sync_pending(xwl_seat->keyboard);
        DisableDevice(xwl_seat->keyboard, TRUE);
    }
}

static void
init_touch(struct xwl_seat *xwl_seat)
{
    xwl_seat->wl_touch = wl_seat_get_touch(xwl_seat->seat);
    wl_touch_add_listener(xwl_seat->wl_touch,
                          &touch_listener, xwl_seat);

    if (xwl_seat->touch == NULL) {
        xwl_seat->touch =
            add_device(xwl_seat, "xwayland-touch", xwl_touch_proc);
        ActivateDevice(xwl_seat->touch, TRUE);
    }
    EnableDevice(xwl_seat->touch, TRUE);

}

static void
release_touch(struct xwl_seat *xwl_seat)
{
    wl_touch_release(xwl_seat->wl_touch);
    xwl_seat->wl_touch = NULL;

    if (xwl_seat->touch)
        DisableDevice(xwl_seat->touch, TRUE);
}

static void
seat_handle_capabilities(void *data, struct wl_seat *seat,
                         enum wl_seat_capability caps)
{
    struct xwl_seat *xwl_seat = data;

    if (caps & WL_SEAT_CAPABILITY_POINTER && xwl_seat->wl_pointer == NULL) {
        init_pointer(xwl_seat);
        init_relative_pointer(xwl_seat);
    } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && xwl_seat->wl_pointer) {
        release_pointer(xwl_seat);
        release_relative_pointer(xwl_seat);
    }

    if (caps & WL_SEAT_CAPABILITY_KEYBOARD && xwl_seat->wl_keyboard == NULL) {
        init_keyboard(xwl_seat);
    } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && xwl_seat->wl_keyboard) {
        release_keyboard(xwl_seat);
    }

    if (caps & WL_SEAT_CAPABILITY_TOUCH && xwl_seat->wl_touch == NULL) {
        init_touch(xwl_seat);
    } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && xwl_seat->wl_touch) {
        release_touch(xwl_seat);
    }

    xwl_seat->xwl_screen->expecting_event--;
}

static void
seat_handle_name(void *data, struct wl_seat *seat,
                 const char *name)
{

}

static const struct wl_seat_listener seat_listener = {
    seat_handle_capabilities,
    seat_handle_name
};

static void
xwl_cursor_init(struct xwl_cursor *xwl_cursor, struct xwl_screen *xwl_screen,
                void (* update_proc)(struct xwl_cursor *))
{
    xwl_cursor->surface = wl_compositor_create_surface(xwl_screen->compositor);
    xwl_cursor->update_proc = update_proc;
    xwl_cursor->frame_cb = NULL;
    xwl_cursor->needs_update = FALSE;
}

static void
xwl_cursor_release(struct xwl_cursor *xwl_cursor)
{
    wl_surface_destroy(xwl_cursor->surface);
    if (xwl_cursor->frame_cb)
        wl_callback_destroy(xwl_cursor->frame_cb);
}

static void
xwl_seat_update_cursor(struct xwl_cursor *xwl_cursor)
{
    struct xwl_seat *xwl_seat = wl_container_of(xwl_cursor, xwl_seat, cursor);
    xwl_seat_set_cursor(xwl_seat);
}

static void
create_input_device(struct xwl_screen *xwl_screen, uint32_t id, uint32_t version)
{
    struct xwl_seat *xwl_seat;

    xwl_seat = calloc(1, sizeof *xwl_seat);
    if (xwl_seat == NULL) {
        ErrorF("%s: ENOMEM\n", __func__);
        return;
    }

    xwl_seat->xwl_screen = xwl_screen;
    xorg_list_add(&xwl_seat->link, &xwl_screen->seat_list);

    xwl_seat->seat =
        wl_registry_bind(xwl_screen->registry, id,
                         &wl_seat_interface, min(version, 5));
    xwl_seat->id = id;

    xwl_cursor_init(&xwl_seat->cursor, xwl_seat->xwl_screen,
                    xwl_seat_update_cursor);
    wl_seat_add_listener(xwl_seat->seat, &seat_listener, xwl_seat);

    init_tablet_manager_seat(xwl_screen, xwl_seat);

    wl_array_init(&xwl_seat->keys);

    xorg_list_init(&xwl_seat->touches);
    xorg_list_init(&xwl_seat->axis_discrete_pending);
    xorg_list_init(&xwl_seat->sync_pending);
}

void
xwl_seat_destroy(struct xwl_seat *xwl_seat)
{
    struct xwl_touch *xwl_touch, *next_xwl_touch;
    struct sync_pending *p, *npd;
    struct axis_discrete_pending *ad, *ad_next;

    xorg_list_for_each_entry_safe(xwl_touch, next_xwl_touch,
                                  &xwl_seat->touches, link_touch) {
        xorg_list_del(&xwl_touch->link_touch);
        free(xwl_touch);
    }

    xorg_list_for_each_entry_safe(p, npd, &xwl_seat->sync_pending, l) {
        xorg_list_del(&xwl_seat->sync_pending);
        free (p);
    }

    xorg_list_for_each_entry_safe(ad, ad_next, &xwl_seat->axis_discrete_pending, l) {
        xorg_list_del(&ad->l);
        free(ad);
    }

    release_tablet_manager_seat(xwl_seat);

    release_grab(xwl_seat);
    wl_seat_destroy(xwl_seat->seat);
    xwl_cursor_release(&xwl_seat->cursor);
    wl_array_release(&xwl_seat->keys);
    free(xwl_seat);
}

static void
tablet_handle_name(void *data, struct zwp_tablet_v2 *tablet, const char *name)
{
}

static void
tablet_handle_id(void *data, struct zwp_tablet_v2 *tablet, uint32_t vid,
                  uint32_t pid)
{
}

static void
tablet_handle_path(void *data, struct zwp_tablet_v2 *tablet, const char *path)
{
}

static void
tablet_handle_done(void *data, struct zwp_tablet_v2 *tablet)
{
    struct xwl_tablet *xwl_tablet = data;
    struct xwl_seat *xwl_seat = xwl_tablet->seat;

    if (xwl_seat->stylus == NULL) {
        xwl_seat->stylus = add_device(xwl_seat, "xwayland-stylus", xwl_tablet_proc);
        ActivateDevice(xwl_seat->stylus, TRUE);
    }
    EnableDevice(xwl_seat->stylus, TRUE);

    if (xwl_seat->eraser == NULL) {
        xwl_seat->eraser = add_device(xwl_seat, "xwayland-eraser", xwl_tablet_proc);
        ActivateDevice(xwl_seat->eraser, TRUE);
    }
    EnableDevice(xwl_seat->eraser, TRUE);

    if (xwl_seat->puck == NULL) {
        xwl_seat->puck = add_device(xwl_seat, "xwayland-cursor", xwl_tablet_proc);
        ActivateDevice(xwl_seat->puck, TRUE);
    }
    EnableDevice(xwl_seat->puck, TRUE);
}

static void
tablet_handle_removed(void *data, struct zwp_tablet_v2 *tablet)
{
    struct xwl_tablet *xwl_tablet = data;
    struct xwl_seat *xwl_seat = xwl_tablet->seat;

    xorg_list_del(&xwl_tablet->link);

    /* The tablet is merely disabled, not removed. The next tablet
       will re-use the same X devices */
    if (xorg_list_is_empty(&xwl_seat->tablets)) {
        if (xwl_seat->stylus)
            DisableDevice(xwl_seat->stylus, TRUE);
        if (xwl_seat->eraser)
            DisableDevice(xwl_seat->eraser, TRUE);
        if (xwl_seat->puck)
            DisableDevice(xwl_seat->puck, TRUE);
        /* pads are removed separately */
    }

    zwp_tablet_v2_destroy(tablet);
    free(xwl_tablet);
}

static const struct zwp_tablet_v2_listener tablet_listener = {
    tablet_handle_name,
    tablet_handle_id,
    tablet_handle_path,
    tablet_handle_done,
    tablet_handle_removed
};

static void
tablet_tool_receive_type(void *data, struct zwp_tablet_tool_v2 *tool,
                         uint32_t type)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;

    switch (type) {
        case ZWP_TABLET_TOOL_V2_TYPE_ERASER:
            xwl_tablet_tool->xdevice = xwl_seat->eraser;
            break;
        case ZWP_TABLET_TOOL_V2_TYPE_MOUSE:
        case ZWP_TABLET_TOOL_V2_TYPE_LENS:
            xwl_tablet_tool->xdevice = xwl_seat->puck;
            break;
        default:
            xwl_tablet_tool->xdevice = xwl_seat->stylus;
            break;
    }
}

static void
tablet_tool_receive_hardware_serial(void *data, struct zwp_tablet_tool_v2 *tool,
                                    uint32_t hi, uint32_t low)
{
}

static void
tablet_tool_receive_hardware_id_wacom(void *data, struct zwp_tablet_tool_v2 *tool,
                                      uint32_t hi, uint32_t low)
{
}

static void
tablet_tool_receive_capability(void *data, struct zwp_tablet_tool_v2 *tool,
                               uint32_t capability)
{
}

static void
tablet_tool_receive_done(void *data, struct zwp_tablet_tool_v2 *tool)
{
}

static void
tablet_tool_receive_removed(void *data, struct zwp_tablet_tool_v2 *tool)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;

    xorg_list_del(&xwl_tablet_tool->link);
    xwl_cursor_release(&xwl_tablet_tool->cursor);
    zwp_tablet_tool_v2_destroy(tool);
    free(xwl_tablet_tool);
}

static void
tablet_tool_proximity_in(void *data, struct zwp_tablet_tool_v2 *tool,
                         uint32_t serial, struct zwp_tablet_v2 *tablet,
                         struct wl_surface *wl_surface)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;

    /* There's a race here where if we create and then immediately
     * destroy a surface, we might end up in a state where the Wayland
     * compositor sends us an event for a surface that doesn't exist.
     *
     * Don't process enter events in this case.
     *
     * see pointer_handle_enter()
     */
    if (wl_surface == NULL)
        return;

    xwl_tablet_tool->proximity_in_serial = serial;
    xwl_seat->tablet_focus_window = wl_surface_get_user_data(wl_surface);

    xwl_tablet_tool_set_cursor(xwl_tablet_tool);
}

static void
tablet_tool_proximity_out(void *data, struct zwp_tablet_tool_v2 *tool)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;

    xwl_tablet_tool->proximity_in_serial = 0;
    xwl_seat->tablet_focus_window = NULL;

    xwl_tablet_tool->pressure = 0;
    xwl_tablet_tool->tilt_x = 0;
    xwl_tablet_tool->tilt_y = 0;
    xwl_tablet_tool->rotation = 0;
    xwl_tablet_tool->slider = 0;
}

static void
tablet_tool_down(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t serial)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
    ValuatorMask mask;

    xwl_seat->xwl_screen->serial = serial;

    valuator_mask_zero(&mask);
    QueuePointerEvents(xwl_tablet_tool->xdevice, ButtonPress, 1, 0, &mask);
}

static void
tablet_tool_up(void *data, struct zwp_tablet_tool_v2 *tool)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    ValuatorMask mask;

    valuator_mask_zero(&mask);
    QueuePointerEvents(xwl_tablet_tool->xdevice, ButtonRelease, 1, 0, &mask);
}

static void
tablet_tool_motion(void *data, struct zwp_tablet_tool_v2 *tool,
                   wl_fixed_t x, wl_fixed_t y)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
    int32_t dx, dy;
    double sx = wl_fixed_to_double(x);
    double sy = wl_fixed_to_double(y);

    if (!xwl_seat->tablet_focus_window)
        return;

    dx = xwl_seat->tablet_focus_window->window->drawable.x;
    dy = xwl_seat->tablet_focus_window->window->drawable.y;

    xwl_tablet_tool->x = (double) dx + sx;
    xwl_tablet_tool->y = (double) dy + sy;
}

static void
tablet_tool_pressure(void *data, struct zwp_tablet_tool_v2 *tool,
                     uint32_t pressure)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;

    if (!xwl_seat->tablet_focus_window)
        return;

    /* normalized to 65535 already */
    xwl_tablet_tool->pressure = pressure;
}

static void
tablet_tool_distance(void *data, struct zwp_tablet_tool_v2 *tool,
                     uint32_t distance_raw)
{
}

static void
tablet_tool_tilt(void *data, struct zwp_tablet_tool_v2 *tool,
                 wl_fixed_t tilt_x, wl_fixed_t tilt_y)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;

    if (!xwl_seat->tablet_focus_window)
        return;

    xwl_tablet_tool->tilt_x = wl_fixed_to_double(tilt_x);
    xwl_tablet_tool->tilt_y = wl_fixed_to_double(tilt_y);
}

static void
tablet_tool_rotation(void *data, struct zwp_tablet_tool_v2 *tool,
                     wl_fixed_t angle)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
    double rotation = wl_fixed_to_double(angle);

    if (!xwl_seat->tablet_focus_window)
        return;

    /* change origin (buttons facing right [libinput +90 degrees]) and
     * scaling (5 points per degree) to match wacom driver behavior
     */
    rotation = remainderf(rotation + 90.0f, 360.0f);
    rotation *= 5.0f;
    xwl_tablet_tool->rotation = rotation;
}

static void
tablet_tool_slider(void *data, struct zwp_tablet_tool_v2 *tool,
                   int32_t position_raw)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
    float position = position_raw / 65535.0;

    if (!xwl_seat->tablet_focus_window)
        return;

    xwl_tablet_tool->slider = (position * 1799.0f) - 900.0f;
}

static void
tablet_tool_wheel(void *data, struct zwp_tablet_tool_v2 *tool,
                  wl_fixed_t degrees, int32_t clicks)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;

    if (!xwl_seat->tablet_focus_window)
        return;

    xwl_tablet_tool->wheel_clicks = clicks;
}

static void
tablet_tool_button_state(void *data, struct zwp_tablet_tool_v2 *tool,
                         uint32_t serial, uint32_t button, uint32_t state)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    struct xwl_seat *xwl_seat = xwl_tablet_tool->seat;
    uint32_t *mask = &xwl_tablet_tool->buttons_now;
    int xbtn = 0;

    /* BTN_0 .. BTN_9 */
    if (button >= 0x100 && button <= 0x109) {
        xbtn = button - 0x100 + 1;
    }
    /* BTN_A .. BTN_Z */
    else if (button >= 0x130 && button <= 0x135) {
        xbtn = button - 0x130 + 10;
    }
    /* BTN_BASE .. BTN_BASE6 */
    else if (button >= 0x126 && button <= 0x12b) {
        xbtn = button - 0x126 + 16;
    }
    else {
        switch (button) {
        case 0x110: /* BTN_LEFT    */
        case 0x14a: /* BTN_TOUCH   */
            xbtn = 1;
            break;

        case 0x112: /* BTN_MIDDLE  */
        case 0x14b: /* BTN_STYLUS  */
            xbtn = 2;
            break;

        case 0x111: /* BTN_RIGHT   */
        case 0x14c: /* BTN_STYLUS2 */
            xbtn = 3;
            break;

        case 0x113: /* BTN_SIDE    */
        case 0x116: /* BTN_BACK    */
        case 0x149: /* BTN_STYLUS3 */
            xbtn = 8;
            break;

        case 0x114: /* BTN_EXTRA   */
        case 0x115: /* BTN_FORWARD */
            xbtn = 9;
            break;
        }
    }

    if (!xbtn) {
        ErrorF("unknown tablet button number %d\n", button);
        return;
    }

    BUG_RETURN(xbtn >= 8 * sizeof(*mask));

    if (state)
        SetBit(mask, xbtn - 1);
    else
        ClearBit(mask, xbtn - 1);

    xwl_seat->xwl_screen->serial = serial;
}

static void
tablet_tool_frame(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t time)
{
    struct xwl_tablet_tool *xwl_tablet_tool = data;
    ValuatorMask mask;
    uint32_t released, pressed, diff;
    int button;

    valuator_mask_zero(&mask);
    valuator_mask_set_double(&mask, 0, xwl_tablet_tool->x);
    valuator_mask_set_double(&mask, 1, xwl_tablet_tool->y);
    valuator_mask_set(&mask, 2, xwl_tablet_tool->pressure);
    valuator_mask_set_double(&mask, 3, xwl_tablet_tool->tilt_x);
    valuator_mask_set_double(&mask, 4, xwl_tablet_tool->tilt_y);
    valuator_mask_set_double(&mask, 5, xwl_tablet_tool->rotation + xwl_tablet_tool->slider);

    QueuePointerEvents(xwl_tablet_tool->xdevice, MotionNotify, 0,
               POINTER_ABSOLUTE | POINTER_DESKTOP, &mask);

    valuator_mask_zero(&mask);

    diff = xwl_tablet_tool->buttons_prev ^ xwl_tablet_tool->buttons_now;
    released = diff & ~xwl_tablet_tool->buttons_now;
    pressed = diff & xwl_tablet_tool->buttons_now;

    button = 1;
    while (released) {
        if (released & 0x1)
            QueuePointerEvents(xwl_tablet_tool->xdevice,
                               ButtonRelease, button, 0, &mask);
        button++;
        released >>= 1;
    }

    button = 1;
    while (pressed) {
        if (pressed & 0x1)
            QueuePointerEvents(xwl_tablet_tool->xdevice,
                               ButtonPress, button, 0, &mask);
        button++;
        pressed >>= 1;
    }

    xwl_tablet_tool->buttons_prev = xwl_tablet_tool->buttons_now;

    while (xwl_tablet_tool->wheel_clicks) {
            if (xwl_tablet_tool->wheel_clicks < 0) {
                button = 4;
                xwl_tablet_tool->wheel_clicks++;
            }
            else {
                button = 5;
                xwl_tablet_tool->wheel_clicks--;
            }

            QueuePointerEvents(xwl_tablet_tool->xdevice,
                               ButtonPress, button, 0, &mask);
            QueuePointerEvents(xwl_tablet_tool->xdevice,
                               ButtonRelease, button, 0, &mask);

    }
}

static const struct zwp_tablet_tool_v2_listener tablet_tool_listener = {
    tablet_tool_receive_type,
    tablet_tool_receive_hardware_serial,
    tablet_tool_receive_hardware_id_wacom,
    tablet_tool_receive_capability,
    tablet_tool_receive_done,
    tablet_tool_receive_removed,
    tablet_tool_proximity_in,
    tablet_tool_proximity_out,
    tablet_tool_down,
    tablet_tool_up,
    tablet_tool_motion,
    tablet_tool_pressure,
    tablet_tool_distance,
    tablet_tool_tilt,
    tablet_tool_rotation,
    tablet_tool_slider,
    tablet_tool_wheel,
    tablet_tool_button_state,
    tablet_tool_frame
};

static void
tablet_pad_ring_destroy(struct xwl_tablet_pad_ring *ring)
{
    zwp_tablet_pad_ring_v2_destroy(ring->ring);
    xorg_list_del(&ring->link);
    free(ring);
}

static void
tablet_pad_ring_source(void *data,
                       struct zwp_tablet_pad_ring_v2 *zwp_tablet_pad_ring_v2,
                       uint32_t source)
{
}

static void
tablet_pad_ring_angle(void *data,
                      struct zwp_tablet_pad_ring_v2 *zwp_tablet_pad_ring_v2,
                      wl_fixed_t degrees)
{
    struct xwl_tablet_pad_ring *ring = data;
    struct xwl_tablet_pad *pad = ring->group->pad;
    double deg = wl_fixed_to_double(degrees);
    ValuatorMask mask;

    valuator_mask_zero(&mask);
    valuator_mask_set(&mask, 5 + ring->index, deg/360.0  * 71);
    QueuePointerEvents(pad->xdevice, MotionNotify, 0, 0, &mask);
}

static void
tablet_pad_ring_stop(void *data,
                     struct zwp_tablet_pad_ring_v2 *zwp_tablet_pad_ring_v2)
{
}

static void
tablet_pad_ring_frame(void *data,
                      struct zwp_tablet_pad_ring_v2 *zwp_tablet_pad_ring_v2,
                      uint32_t time)
{
}

static const struct zwp_tablet_pad_ring_v2_listener tablet_pad_ring_listener = {
    tablet_pad_ring_source,
    tablet_pad_ring_angle,
    tablet_pad_ring_stop,
    tablet_pad_ring_frame,
};


static void
tablet_pad_strip_destroy(struct xwl_tablet_pad_strip *strip)
{
    zwp_tablet_pad_strip_v2_destroy(strip->strip);
    xorg_list_del(&strip->link);
    free(strip);
}

static void
tablet_pad_strip_source(void *data,
                        struct zwp_tablet_pad_strip_v2 *zwp_tablet_pad_strip_v2,
                        uint32_t source)
{
}

static void
tablet_pad_strip_position(void *data,
                          struct zwp_tablet_pad_strip_v2 *zwp_tablet_pad_strip_v2,
                          uint32_t position)
{
    struct xwl_tablet_pad_strip *strip = data;
    struct xwl_tablet_pad *pad = strip->group->pad;
    ValuatorMask mask;

    valuator_mask_zero(&mask);
    valuator_mask_set(&mask, 3 + strip->index, position/65535.0 * 2048);
    QueuePointerEvents(pad->xdevice, MotionNotify, 0, 0, &mask);
}

static void
tablet_pad_strip_stop(void *data,
                      struct zwp_tablet_pad_strip_v2 *zwp_tablet_pad_strip_v2)
{
}

static void
tablet_pad_strip_frame(void *data,
                       struct zwp_tablet_pad_strip_v2 *zwp_tablet_pad_strip_v2,
                       uint32_t time)
{
}

static const struct zwp_tablet_pad_strip_v2_listener tablet_pad_strip_listener = {
    tablet_pad_strip_source,
    tablet_pad_strip_position,
    tablet_pad_strip_stop,
    tablet_pad_strip_frame,
};

static void
tablet_pad_group_destroy(struct xwl_tablet_pad_group *group)
{
    struct xwl_tablet_pad_ring *r, *tr;
    struct xwl_tablet_pad_strip *s, *ts;

    xorg_list_for_each_entry_safe(r, tr,
                                  &group->pad_group_ring_list,
                                  link)
        tablet_pad_ring_destroy(r);

    xorg_list_for_each_entry_safe(s, ts,
                                  &group->pad_group_strip_list,
                                  link)
        tablet_pad_strip_destroy(s);

    zwp_tablet_pad_group_v2_destroy(group->group);
    xorg_list_del(&group->link);
    free(group);
}

static void
tablet_pad_group_buttons(void *data,
                         struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
                         struct wl_array *buttons)
{

}

static void
tablet_pad_group_ring(void *data,
                      struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
                      struct zwp_tablet_pad_ring_v2 *wp_ring)
{
    static unsigned int ring_index = 0;
    struct xwl_tablet_pad_group *group = data;
    struct xwl_tablet_pad_ring *ring;

    ring = calloc(1, sizeof *ring);
    if (ring == NULL) {
        ErrorF("%s ENOMEM\n", __func__);
        return;
    }

    ring->index = ring_index++;
    ring->group = group;
    ring->ring = wp_ring;

    xorg_list_add(&ring->link, &group->pad_group_ring_list);

    zwp_tablet_pad_ring_v2_add_listener(wp_ring, &tablet_pad_ring_listener,
                                        ring);
}

static void
tablet_pad_group_strip(void *data,
                       struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
                       struct zwp_tablet_pad_strip_v2 *wp_strip)
{
    static unsigned int strip_index = 0;
    struct xwl_tablet_pad_group *group = data;
    struct xwl_tablet_pad_strip *strip;

    strip = calloc(1, sizeof *strip);
    if (strip == NULL) {
        ErrorF("%s ENOMEM\n", __func__);
        return;
    }

    strip->index = strip_index++;
    strip->group = group;
    strip->strip = wp_strip;

    xorg_list_add(&strip->link, &group->pad_group_strip_list);

    zwp_tablet_pad_strip_v2_add_listener(wp_strip, &tablet_pad_strip_listener,
                                         strip);
}

static void
tablet_pad_group_modes(void *data,
                       struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
                       uint32_t modes)
{

}

static void
tablet_pad_group_done(void *data,
                      struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2)
{

}

static void
tablet_pad_group_mode_switch(void *data,
                             struct zwp_tablet_pad_group_v2 *zwp_tablet_pad_group_v2,
                             uint32_t time,
                             uint32_t serial,
                             uint32_t mode)
{

}

static struct zwp_tablet_pad_group_v2_listener tablet_pad_group_listener = {
    tablet_pad_group_buttons,
    tablet_pad_group_ring,
    tablet_pad_group_strip,
    tablet_pad_group_modes,
    tablet_pad_group_done,
    tablet_pad_group_mode_switch,
};

static int
xwl_tablet_pad_proc(DeviceIntPtr device, int what)
{
    struct xwl_tablet_pad *pad = device->public.devicePrivate;
    /* Axis layout mirrors that of xf86-input-wacom to have better
       compatibility with existing clients */
#define NAXES 7
    Atom axes_labels[NAXES] = { 0 };
    BYTE map[MAX_BUTTONS + 1];
    int i = 0;
    Atom btn_labels[MAX_BUTTONS] = { 0 }; /* btn labels are meaningless */
    int nbuttons;

    switch (what) {
    case DEVICE_INIT:
        device->public.on = FALSE;

        axes_labels[0] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_X);
        axes_labels[1] = XIGetKnownProperty(AXIS_LABEL_PROP_ABS_Y);
        /* The others have no good mapping */

        if (!InitValuatorClassDeviceStruct(device, NAXES, axes_labels,
                                           GetMotionHistorySize(), Absolute))
            return BadValue;

        for (i = 1; i <= MAX_BUTTONS; i++)
            map[i] = i;

        /* We need at least 7 buttons to allow scrolling */
        nbuttons = min(max(pad->nbuttons + 4, 7), MAX_BUTTONS);

        if (!InitButtonClassDeviceStruct(device, nbuttons,
                                         btn_labels, map))
            return BadValue;

        /* Valuators */
        InitValuatorAxisStruct(device, 0, axes_labels[0],
                               0, 100, 1, 0, 1, Absolute);
        InitValuatorAxisStruct(device, 1, axes_labels[1],
                               0, 100, 1, 0, 1, Absolute);
        /* Pressure - unused, for backwards compat only */
        InitValuatorAxisStruct(device, 2, axes_labels[2],
                               0, 2048, 1, 0, 1, Absolute);
        /* strip x */
        InitValuatorAxisStruct(device, 3, axes_labels[3],
                               0, 2048, 1, 0, 1, Absolute);
        /* strip y */
        InitValuatorAxisStruct(device, 4, axes_labels[4],
                               0, 2048, 1, 0, 1, Absolute);
        /* ring */
        InitValuatorAxisStruct(device, 5, axes_labels[5],
                               0, 71, 1, 0, 1, Absolute);
        /* ring2 */
        InitValuatorAxisStruct(device, 6, axes_labels[6],
                               0, 71, 1, 0, 1, Absolute);

        if (!InitPtrFeedbackClassDeviceStruct(device, xwl_pointer_control))
            return BadValue;

        return Success;

    case DEVICE_ON:
        device->public.on = TRUE;
        return Success;

    case DEVICE_OFF:
    case DEVICE_CLOSE:
        device->public.on = FALSE;
        return Success;
    }

    return BadMatch;
#undef NAXES
}

static void
tablet_pad_group(void *data,
                 struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
                 struct zwp_tablet_pad_group_v2 *pad_group)
{
    struct xwl_tablet_pad *pad = data;
    struct xwl_tablet_pad_group *group;

    group = calloc(1, sizeof *group);
    if (pad == NULL) {
        ErrorF("%s ENOMEM\n", __func__);
        return;
    }

    group->pad = pad;
    group->group = pad_group;
    xorg_list_init(&group->pad_group_ring_list);
    xorg_list_init(&group->pad_group_strip_list);

    xorg_list_add(&group->link, &pad->pad_group_list);

    zwp_tablet_pad_group_v2_add_listener(pad_group,
                                         &tablet_pad_group_listener,
                                         group);
}

static void
tablet_pad_path(void *data,
                struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
                const char *path)
{

}

static void
tablet_pad_buttons(void *data,
                   struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
                   uint32_t buttons)
{
    struct xwl_tablet_pad *pad = data;

    pad->nbuttons = buttons;
}

static void
tablet_pad_done(void *data,
                struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2)
{
    struct xwl_tablet_pad *pad = data;

    pad->xdevice = add_device(pad->seat, "xwayland-pad",
                              xwl_tablet_pad_proc);
    pad->xdevice->public.devicePrivate = pad;
    ActivateDevice(pad->xdevice, TRUE);
    EnableDevice(pad->xdevice, TRUE);
}

static void
tablet_pad_button(void *data,
                  struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
                  uint32_t time,
                  uint32_t button,
                  uint32_t state)
{
    struct xwl_tablet_pad *pad = data;
    ValuatorMask mask;

    button++; /* wayland index vs X's 1-offset */
    /* skip scroll wheel buttons 4-7 */
    button = button > 3 ? button + 4 : button;

    valuator_mask_zero(&mask);
    QueuePointerEvents(pad->xdevice,
                       state ? ButtonPress : ButtonRelease, button, 0, &mask);
}

static void
tablet_pad_enter(void *data,
                 struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
                 uint32_t serial,
                 struct zwp_tablet_v2 *tablet,
                 struct wl_surface *surface)
{
    /* pairs the pad with the tablet but also to set the focus. We
     * don't care about the pairing and always use X's focus */
}

static void
tablet_pad_leave(void *data,
                 struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2,
                 uint32_t serial,
                 struct wl_surface *surface)
{
    /* pairs the pad with the tablet but also to set the focus. We
     * don't care about the pairing and always use X's focus */
}

static void
tablet_pad_removed(void *data,
                   struct zwp_tablet_pad_v2 *zwp_tablet_pad_v2)
{
    struct xwl_tablet_pad *pad = data;
    struct xwl_tablet_pad_group *g, *tg;

    xorg_list_for_each_entry_safe(g, tg, &pad->pad_group_list, link)
        tablet_pad_group_destroy(g);

    RemoveDevice(pad->xdevice, TRUE);
    xorg_list_del(&pad->link);
    zwp_tablet_pad_v2_destroy(pad->pad);
    free(pad);
}

static const struct zwp_tablet_pad_v2_listener tablet_pad_listener = {
    tablet_pad_group,
    tablet_pad_path,
    tablet_pad_buttons,
    tablet_pad_done,
    tablet_pad_button,
    tablet_pad_enter,
    tablet_pad_leave,
    tablet_pad_removed,
};

static void
tablet_seat_handle_add_tablet(void *data, struct zwp_tablet_seat_v2 *tablet_seat,
                              struct zwp_tablet_v2 *tablet)
{
    struct xwl_seat *xwl_seat = data;
    struct xwl_tablet *xwl_tablet;

    xwl_tablet = calloc(sizeof *xwl_tablet, 1);
    if (xwl_tablet == NULL) {
        ErrorF("%s ENOMEM\n", __func__);
        return;
    }

    xwl_tablet->tablet = tablet;
    xwl_tablet->seat = xwl_seat;

    xorg_list_add(&xwl_tablet->link, &xwl_seat->tablets);

    zwp_tablet_v2_add_listener(tablet, &tablet_listener, xwl_tablet);
}

static void
xwl_tablet_tool_update_cursor(struct xwl_cursor *xwl_cursor)
{
    struct xwl_tablet_tool *xwl_tablet_tool = wl_container_of(xwl_cursor,
                                                              xwl_tablet_tool,
                                                              cursor);
    xwl_tablet_tool_set_cursor(xwl_tablet_tool);
}

static void
tablet_seat_handle_add_tool(void *data, struct zwp_tablet_seat_v2 *tablet_seat,
                            struct zwp_tablet_tool_v2 *tool)
{
    struct xwl_seat *xwl_seat = data;
    struct xwl_screen *xwl_screen = xwl_seat->xwl_screen;
    struct xwl_tablet_tool *xwl_tablet_tool;

    xwl_tablet_tool = calloc(sizeof *xwl_tablet_tool, 1);
    if (xwl_tablet_tool == NULL) {
        ErrorF("%s ENOMEM\n", __func__);
        return;
    }

    xwl_tablet_tool->tool = tool;
    xwl_tablet_tool->seat = xwl_seat;
    xwl_cursor_init(&xwl_tablet_tool->cursor, xwl_screen,
                    xwl_tablet_tool_update_cursor);

    xorg_list_add(&xwl_tablet_tool->link, &xwl_seat->tablet_tools);

    zwp_tablet_tool_v2_add_listener(tool, &tablet_tool_listener, xwl_tablet_tool);
}

static void
tablet_seat_handle_add_pad(void *data, struct zwp_tablet_seat_v2 *tablet_seat,
                           struct zwp_tablet_pad_v2 *pad)
{
    struct xwl_seat *xwl_seat = data;
    struct xwl_tablet_pad *xwl_tablet_pad;

    xwl_tablet_pad = calloc(sizeof *xwl_tablet_pad, 1);
    if (xwl_tablet_pad == NULL) {
        ErrorF("%s ENOMEM\n", __func__);
        return;
    }

    xwl_tablet_pad->pad = pad;
    xwl_tablet_pad->seat = xwl_seat;
    xorg_list_init(&xwl_tablet_pad->pad_group_list);

    xorg_list_add(&xwl_tablet_pad->link, &xwl_seat->tablet_pads);

    zwp_tablet_pad_v2_add_listener(pad, &tablet_pad_listener,
                                   xwl_tablet_pad);
}

static const struct zwp_tablet_seat_v2_listener tablet_seat_listener = {
    tablet_seat_handle_add_tablet,
    tablet_seat_handle_add_tool,
    tablet_seat_handle_add_pad
};

static void
init_tablet_manager_seat(struct xwl_screen *xwl_screen,
                         struct xwl_seat *xwl_seat)
{
    xorg_list_init(&xwl_seat->tablets);
    xorg_list_init(&xwl_seat->tablet_tools);
    xorg_list_init(&xwl_seat->tablet_pads);

    if (!xwl_screen->tablet_manager)
        return;

    xwl_seat->tablet_seat =
        zwp_tablet_manager_v2_get_tablet_seat(xwl_screen->tablet_manager,
                                              xwl_seat->seat);

    zwp_tablet_seat_v2_add_listener(xwl_seat->tablet_seat, &tablet_seat_listener, xwl_seat);
}

static void
release_tablet_manager_seat(struct xwl_seat *xwl_seat)
{
    struct xwl_tablet *xwl_tablet, *next_xwl_tablet;
    struct xwl_tablet_tool *xwl_tablet_tool, *next_xwl_tablet_tool;
    struct xwl_tablet_pad *xwl_tablet_pad, *next_xwl_tablet_pad;

    xorg_list_for_each_entry_safe(xwl_tablet_pad, next_xwl_tablet_pad,
                                  &xwl_seat->tablet_pads, link) {
        xorg_list_del(&xwl_tablet_pad->link);
        zwp_tablet_pad_v2_destroy(xwl_tablet_pad->pad);
        free(xwl_tablet_pad);
    }

    xorg_list_for_each_entry_safe(xwl_tablet_tool, next_xwl_tablet_tool,
                                  &xwl_seat->tablet_tools, link) {
        xorg_list_del(&xwl_tablet_tool->link);
        zwp_tablet_tool_v2_destroy(xwl_tablet_tool->tool);
        free(xwl_tablet_tool);
    }

    xorg_list_for_each_entry_safe(xwl_tablet, next_xwl_tablet,
                                  &xwl_seat->tablets, link) {
        xorg_list_del(&xwl_tablet->link);
        zwp_tablet_v2_destroy(xwl_tablet->tablet);
        free(xwl_tablet);
    }

    if (xwl_seat->tablet_seat) {
        zwp_tablet_seat_v2_destroy(xwl_seat->tablet_seat);
        xwl_seat->tablet_seat = NULL;
    }
}

static void
init_tablet_manager(struct xwl_screen *xwl_screen, uint32_t id, uint32_t version)
{
    struct xwl_seat *xwl_seat;

    xwl_screen->tablet_manager = wl_registry_bind(xwl_screen->registry,
                                                  id,
                                                  &zwp_tablet_manager_v2_interface,
                                                  min(version,1));

    xorg_list_for_each_entry(xwl_seat, &xwl_screen->seat_list, link) {
        init_tablet_manager_seat(xwl_screen, xwl_seat);
    }
}

void
xwl_screen_release_tablet_manager(struct xwl_screen *xwl_screen)
{
    if (xwl_screen->tablet_manager) {
        zwp_tablet_manager_v2_destroy(xwl_screen->tablet_manager);
        xwl_screen->tablet_manager = NULL;
    }
}

static void
init_relative_pointer_manager(struct xwl_screen *xwl_screen,
                              uint32_t id, uint32_t version)
{
    xwl_screen->relative_pointer_manager =
        wl_registry_bind(xwl_screen->registry, id,
                         &zwp_relative_pointer_manager_v1_interface,
                         1);
}

static void
init_pointer_constraints(struct xwl_screen *xwl_screen,
                         uint32_t id, uint32_t version)
{
    xwl_screen->pointer_constraints =
        wl_registry_bind(xwl_screen->registry, id,
                         &zwp_pointer_constraints_v1_interface,
                         1);
}

static void
init_keyboard_grab(struct xwl_screen *xwl_screen,
                   uint32_t id, uint32_t version)
{
    struct xwl_seat *xwl_seat;
    DeviceIntPtr master;

    xwl_screen->wp_grab =
         wl_registry_bind(xwl_screen->registry, id,
                          &zwp_xwayland_keyboard_grab_manager_v1_interface,
                          1);

    xorg_list_for_each_entry(xwl_seat, &xwl_screen->seat_list, link) {
        if (xwl_seat->keyboard) {
            master = GetMaster(xwl_seat->keyboard, MASTER_KEYBOARD);
            if (master)
                setup_keyboard_grab_handler(master);
        }
    }
}

static void
input_handler(void *data, struct wl_registry *registry, uint32_t id,
              const char *interface, uint32_t version)
{
    struct xwl_screen *xwl_screen = data;

    if (strcmp(interface, "wl_seat") == 0 && version >= 3) {
        create_input_device(xwl_screen, id, version);
        xwl_screen->expecting_event++;
    } else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0) {
        init_relative_pointer_manager(xwl_screen, id, version);
    } else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0) {
        init_pointer_constraints(xwl_screen, id, version);
    } else if (strcmp(interface, "zwp_tablet_manager_v2") == 0) {
        init_tablet_manager(xwl_screen, id, version);
    } else if (strcmp(interface, "zwp_xwayland_keyboard_grab_manager_v1") == 0) {
        init_keyboard_grab(xwl_screen, id, version);
    }
}

static void
global_remove(void *data, struct wl_registry *registry, uint32_t name)
{
}

static const struct wl_registry_listener input_listener = {
    input_handler,
    global_remove,
};

Bool
LegalModifier(unsigned int key, DeviceIntPtr pDev)
{
    return TRUE;
}

void
ProcessInputEvents(void)
{
    mieqProcessInputEvents();
}

void
DDXRingBell(int volume, int pitch, int duration)
{
}

static Bool
sprite_check_lost_focus(SpritePtr sprite, WindowPtr window)
{
    DeviceIntPtr device, master;
    struct xwl_seat *xwl_seat;

    for (device = inputInfo.devices; device; device = device->next) {
        /* Ignore non-wayland devices */
        if (device->deviceProc == xwl_pointer_proc &&
            device->spriteInfo->sprite == sprite)
            break;
    }

    if (!device)
        return FALSE;

    xwl_seat = device->public.devicePrivate;

    master = GetMaster(device, POINTER_OR_FLOAT);
    if (!master || !master->lastSlave)
        return FALSE;

    /* We do want the last active slave, we only check on slave xwayland
     * devices so we can find out the xwl_seat, but those don't actually own
     * their sprite, so the match doesn't mean a lot.
     */
    if (master->lastSlave == xwl_seat->pointer &&
        xwl_seat->focus_window == NULL &&
        xwl_seat->last_xwindow != NullWindow &&
        IsParent(xwl_seat->last_xwindow, window))
        return TRUE;

    return FALSE;
}

static WindowPtr
xwl_xy_to_window(ScreenPtr screen, SpritePtr sprite, int x, int y)
{
    struct xwl_screen *xwl_screen;
    WindowPtr ret;

    xwl_screen = xwl_screen_get(screen);

    screen->XYToWindow = xwl_screen->XYToWindow;
    ret = screen->XYToWindow(screen, sprite, x, y);
    xwl_screen->XYToWindow = screen->XYToWindow;
    screen->XYToWindow = xwl_xy_to_window;

    /* If the device controlling the sprite has left the Wayland surface but
     * the DIX still finds the pointer within the X11 window, it means that
     * the pointer has crossed to another native Wayland window, in this
     * case, pretend we entered the root window so that a LeaveNotify
     * event is emitted.
     */
    if (sprite_check_lost_focus(sprite, ret)) {
        sprite->spriteTraceGood = 1;
        return sprite->spriteTrace[0];
    }

    return ret;
}

void
xwl_seat_clear_touch(struct xwl_seat *xwl_seat, WindowPtr window)
{
    struct xwl_touch *xwl_touch, *next_xwl_touch;

    xorg_list_for_each_entry_safe(xwl_touch, next_xwl_touch,
                                  &xwl_seat->touches, link_touch) {
        if (xwl_touch->window->window == window) {
            xorg_list_del(&xwl_touch->link_touch);
            free(xwl_touch);
        }
    }
}

static void
xwl_pointer_warp_emulator_set_fake_pos(struct xwl_pointer_warp_emulator *warp_emulator,
                                       int x,
                                       int y)
{
    struct zwp_locked_pointer_v1 *locked_pointer =
        warp_emulator->locked_pointer;
    WindowPtr window;
    int sx, sy;

    if (!warp_emulator->locked_pointer)
        return;

    if (!warp_emulator->xwl_seat->focus_window)
        return;

    window = warp_emulator->xwl_seat->focus_window->window;
    if (x >= window->drawable.x ||
        y >= window->drawable.y ||
        x < (window->drawable.x + window->drawable.width) ||
        y < (window->drawable.y + window->drawable.height)) {
        sx = x - window->drawable.x;
        sy = y - window->drawable.y;
        zwp_locked_pointer_v1_set_cursor_position_hint(locked_pointer,
                                                       wl_fixed_from_int(sx),
                                                       wl_fixed_from_int(sy));
        wl_surface_commit(warp_emulator->xwl_seat->focus_window->surface);
    }
}

static Bool
xwl_pointer_warp_emulator_is_locked(struct xwl_pointer_warp_emulator *warp_emulator)
{
    if (warp_emulator->locked_pointer)
        return TRUE;
    else
        return FALSE;
}

static void
xwl_pointer_warp_emulator_lock(struct xwl_pointer_warp_emulator *warp_emulator)
{
    struct xwl_seat *xwl_seat = warp_emulator->xwl_seat;
    struct xwl_screen *xwl_screen = xwl_seat->xwl_screen;
    struct zwp_pointer_constraints_v1 *pointer_constraints =
        xwl_screen->pointer_constraints;
    struct xwl_window *lock_window = xwl_seat->focus_window;

    warp_emulator->locked_window = lock_window;

    warp_emulator->locked_pointer =
        zwp_pointer_constraints_v1_lock_pointer(pointer_constraints,
                                                lock_window->surface,
                                                xwl_seat->wl_pointer,
                                                NULL,
                                                ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
}

static void
xwl_pointer_warp_emulator_maybe_lock(struct xwl_pointer_warp_emulator *warp_emulator,
                                     struct xwl_window *xwl_window,
                                     SpritePtr sprite,
                                     int x, int y)
{
    struct xwl_seat *xwl_seat = warp_emulator->xwl_seat;
    GrabPtr pointer_grab = xwl_seat->pointer->deviceGrab.grab;

    if (warp_emulator->locked_pointer)
        return;

    /*
     * If there is no grab, and the window doesn't have pointer focus, ignore
     * the warp, as under Wayland it won't receive input anyway.
     */
    if (!pointer_grab && xwl_seat->focus_window != xwl_window)
        return;

    /*
     * If there is a grab, but it's not an ownerEvents grab and the destination
     * is not the pointer focus, ignore it, as events wouldn't be delivered
     * there anyway.
     */
    if (pointer_grab &&
        !pointer_grab->ownerEvents &&
        XYToWindow(sprite, x, y) != xwl_seat->focus_window->window)
        return;

    xwl_pointer_warp_emulator_lock(warp_emulator);
}

static void
xwl_pointer_warp_emulator_warp(struct xwl_pointer_warp_emulator *warp_emulator,
                               struct xwl_window *xwl_window,
                               SpritePtr sprite,
                               int x, int y)
{
    xwl_pointer_warp_emulator_maybe_lock(warp_emulator,
                                         xwl_window,
                                         sprite,
                                         x, y);
    xwl_pointer_warp_emulator_set_fake_pos(warp_emulator, x, y);
}

static void
xwl_pointer_warp_emulator_handle_motion(struct xwl_pointer_warp_emulator *warp_emulator,
                                        double dx,
                                        double dy,
                                        double dx_unaccel,
                                        double dy_unaccel)
{
    struct xwl_seat *xwl_seat = warp_emulator->xwl_seat;
    ValuatorMask mask;
    WindowPtr window;
    int x, y;

    valuator_mask_zero(&mask);
    valuator_mask_set_unaccelerated(&mask, 0, dx, dx_unaccel);
    valuator_mask_set_unaccelerated(&mask, 1, dy, dy_unaccel);

    QueuePointerEvents(xwl_seat->relative_pointer, MotionNotify, 0,
                       POINTER_RELATIVE, &mask);

    window = xwl_seat->focus_window->window;
    miPointerGetPosition(xwl_seat->pointer, &x, &y);

    if (xwl_pointer_warp_emulator_is_locked(warp_emulator) &&
        xwl_seat->cursor_confinement_window != warp_emulator->locked_window &&
        (x < window->drawable.x ||
         y < window->drawable.y ||
         x >= (window->drawable.x + window->drawable.width) ||
         y >= (window->drawable.y + window->drawable.height)))
        xwl_seat_destroy_pointer_warp_emulator(xwl_seat);
    else
        xwl_pointer_warp_emulator_set_fake_pos(warp_emulator, x, y);
}

static struct xwl_pointer_warp_emulator *
xwl_pointer_warp_emulator_create(struct xwl_seat *xwl_seat)
{
    struct xwl_pointer_warp_emulator *warp_emulator;

    warp_emulator = calloc(1, sizeof *warp_emulator);
    if (!warp_emulator) {
        ErrorF("%s: ENOMEM\n", __func__);
        return NULL;
    }

    warp_emulator->xwl_seat = xwl_seat;

    return warp_emulator;
}

static void
xwl_pointer_warp_emulator_destroy(struct xwl_pointer_warp_emulator *warp_emulator)
{
    if (warp_emulator->locked_pointer)
        zwp_locked_pointer_v1_destroy(warp_emulator->locked_pointer);
    free(warp_emulator);
}

static void
xwl_seat_create_pointer_warp_emulator(struct xwl_seat *xwl_seat)
{
    if (xwl_seat->confined_pointer)
        xwl_seat_destroy_confined_pointer(xwl_seat);

    xwl_seat->pointer_warp_emulator =
        xwl_pointer_warp_emulator_create(xwl_seat);
}

static Bool
xwl_seat_can_emulate_pointer_warp(struct xwl_seat *xwl_seat)
{
    struct xwl_screen *xwl_screen = xwl_seat->xwl_screen;

    if (!xwl_screen->relative_pointer_manager)
        return FALSE;

    if (!xwl_screen->pointer_constraints)
        return FALSE;

    return TRUE;
}

void
xwl_seat_emulate_pointer_warp(struct xwl_seat *xwl_seat,
                              struct xwl_window *xwl_window,
                              SpritePtr sprite,
                              int x, int y)
{
    if (!xwl_seat_can_emulate_pointer_warp(xwl_seat))
        return;

    if (xwl_seat->x_cursor != NULL)
        return;

    if (!xwl_seat->pointer_warp_emulator)
        xwl_seat_create_pointer_warp_emulator(xwl_seat);

    if (!xwl_seat->pointer_warp_emulator)
        return;

    xwl_pointer_warp_emulator_warp(xwl_seat->pointer_warp_emulator,
                                   xwl_window,
                                   sprite,
                                   x, y);
}

static Bool
xwl_seat_maybe_lock_on_hidden_cursor(struct xwl_seat *xwl_seat)
{
    /* Some clients use hidden cursor+confineTo+relative motion
     * to implement infinite panning (eg. 3D views), lock the
     * pointer for so the relative pointer is used.
     */
    if (xwl_seat->x_cursor ||
        !xwl_seat->cursor_confinement_window)
        return FALSE;

    if (!xwl_seat->focus_window)
        return FALSE;

    if (xwl_seat->confined_pointer)
        xwl_seat_destroy_confined_pointer(xwl_seat);

    xwl_seat_create_pointer_warp_emulator(xwl_seat);
    xwl_pointer_warp_emulator_lock(xwl_seat->pointer_warp_emulator);
    return TRUE;
}

void
xwl_seat_cursor_visibility_changed(struct xwl_seat *xwl_seat)
{
    if (xwl_seat->pointer_warp_emulator && xwl_seat->x_cursor != NULL) {
        xwl_seat_destroy_pointer_warp_emulator(xwl_seat);
    } else if (!xwl_seat->x_cursor && xwl_seat->cursor_confinement_window) {
        /* If the cursor goes hidden as is confined, lock it for
         * relative motion to work. */
        xwl_seat_maybe_lock_on_hidden_cursor(xwl_seat);
    }
}

void
xwl_seat_destroy_pointer_warp_emulator(struct xwl_seat *xwl_seat)
{
    if (!xwl_seat->pointer_warp_emulator)
        return;

    xwl_pointer_warp_emulator_destroy(xwl_seat->pointer_warp_emulator);
    xwl_seat->pointer_warp_emulator = NULL;

    if (xwl_seat->cursor_confinement_window) {
        xwl_seat_confine_pointer(xwl_seat,
                                 xwl_seat->cursor_confinement_window);
    }
}

void
xwl_seat_confine_pointer(struct xwl_seat *xwl_seat,
                         struct xwl_window *xwl_window)
{
    struct zwp_pointer_constraints_v1 *pointer_constraints =
        xwl_seat->xwl_screen->pointer_constraints;

    if (!pointer_constraints)
        return;

    if (xwl_seat->cursor_confinement_window == xwl_window &&
        xwl_seat->confined_pointer)
        return;

    xwl_seat_unconfine_pointer(xwl_seat);

    xwl_seat->cursor_confinement_window = xwl_window;

    if (xwl_seat->pointer_warp_emulator)
        return;

    if (xwl_seat_maybe_lock_on_hidden_cursor(xwl_seat))
        return;

    xwl_seat->confined_pointer =
        zwp_pointer_constraints_v1_confine_pointer(pointer_constraints,
                                                   xwl_window->surface,
                                                   xwl_seat->wl_pointer,
                                                   NULL,
                                                   ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
}

static void
xwl_seat_destroy_confined_pointer(struct xwl_seat *xwl_seat)
{
    zwp_confined_pointer_v1_destroy(xwl_seat->confined_pointer);
    xwl_seat->confined_pointer = NULL;
}

void
xwl_seat_unconfine_pointer(struct xwl_seat *xwl_seat)
{
    xwl_seat->cursor_confinement_window = NULL;

    if (xwl_seat->confined_pointer)
        xwl_seat_destroy_confined_pointer(xwl_seat);
}

void
InitInput(int argc, char *argv[])
{
    ScreenPtr pScreen = screenInfo.screens[0];
    struct xwl_screen *xwl_screen = xwl_screen_get(pScreen);

    mieqInit();

    xwl_screen->input_registry = wl_display_get_registry(xwl_screen->display);
    wl_registry_add_listener(xwl_screen->input_registry, &input_listener,
                             xwl_screen);

    xwl_screen->XYToWindow = pScreen->XYToWindow;
    pScreen->XYToWindow = xwl_xy_to_window;

    wl_display_roundtrip(xwl_screen->display);
    while (xwl_screen->expecting_event)
        wl_display_roundtrip(xwl_screen->display);
}

void
CloseInput(void)
{
    mieqFini();
}