1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
|
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#ifdef SDL_VIDEO_DRIVER_WAYLAND
#include "../../core/unix/SDL_poll.h"
#include "../../events/SDL_events_c.h"
#include "../../events/SDL_scancode_tables_c.h"
#include "../../events/SDL_keysym_to_keycode_c.h"
#include "../../core/linux/SDL_system_theme.h"
#include "../SDL_sysvideo.h"
#include "SDL_waylandvideo.h"
#include "SDL_waylandevents_c.h"
#include "SDL_waylandwindow.h"
#include "SDL_waylandmouse.h"
#include "pointer-constraints-unstable-v1-client-protocol.h"
#include "relative-pointer-unstable-v1-client-protocol.h"
#include "xdg-shell-client-protocol.h"
#include "keyboard-shortcuts-inhibit-unstable-v1-client-protocol.h"
#include "text-input-unstable-v3-client-protocol.h"
#include "tablet-v2-client-protocol.h"
#include "primary-selection-unstable-v1-client-protocol.h"
#include "input-timestamps-unstable-v1-client-protocol.h"
#ifdef HAVE_LIBDECOR_H
#include <libdecor.h>
#endif
#ifdef SDL_INPUT_LINUXEV
#include <linux/input.h>
#else
#define BTN_LEFT (0x110)
#define BTN_RIGHT (0x111)
#define BTN_MIDDLE (0x112)
#define BTN_SIDE (0x113)
#define BTN_EXTRA (0x114)
#endif
#include "../../events/SDL_keysym_to_scancode_c.h"
#include "../../events/imKStoUCS.h"
#include <errno.h>
#include <sys/mman.h>
#include <unistd.h>
#include <xkbcommon/xkbcommon-compose.h>
#include <xkbcommon/xkbcommon.h>
#include "cursor-shape-v1-client-protocol.h"
// Weston uses a ratio of 10 units per scroll tick
#define WAYLAND_WHEEL_AXIS_UNIT 10
#ifndef XKB_MOD_NAME_MOD3
#define XKB_MOD_NAME_MOD3 "Mod3"
#endif
#ifndef XKB_MOD_NAME_MOD5
#define XKB_MOD_NAME_MOD5 "Mod5"
#endif
// Keyboard and mouse names to match XWayland
#define WAYLAND_DEFAULT_KEYBOARD_NAME "Virtual core keyboard"
#define WAYLAND_DEFAULT_POINTER_NAME "Virtual core pointer"
// Focus clickthrough timeout
#define WAYLAND_FOCUS_CLICK_TIMEOUT_NS SDL_MS_TO_NS(10)
struct SDL_WaylandTouchPoint
{
SDL_TouchID id;
wl_fixed_t fx;
wl_fixed_t fy;
struct wl_surface *surface;
struct wl_list link;
};
static struct wl_list touch_points;
static void touch_add(SDL_TouchID id, wl_fixed_t fx, wl_fixed_t fy, struct wl_surface *surface)
{
struct SDL_WaylandTouchPoint *tp = SDL_malloc(sizeof(struct SDL_WaylandTouchPoint));
SDL_zerop(tp);
tp->id = id;
tp->fx = fx;
tp->fy = fy;
tp->surface = surface;
WAYLAND_wl_list_insert(&touch_points, &tp->link);
}
static void touch_update(SDL_TouchID id, wl_fixed_t fx, wl_fixed_t fy, struct wl_surface **surface)
{
struct SDL_WaylandTouchPoint *tp;
wl_list_for_each (tp, &touch_points, link) {
if (tp->id == id) {
tp->fx = fx;
tp->fy = fy;
if (surface) {
*surface = tp->surface;
}
break;
}
}
}
static void touch_del(SDL_TouchID id, wl_fixed_t *fx, wl_fixed_t *fy, struct wl_surface **surface)
{
struct SDL_WaylandTouchPoint *tp;
wl_list_for_each (tp, &touch_points, link) {
if (tp->id == id) {
if (fx) {
*fx = tp->fx;
}
if (fy) {
*fy = tp->fy;
}
if (surface) {
*surface = tp->surface;
}
WAYLAND_wl_list_remove(&tp->link);
SDL_free(tp);
break;
}
}
}
static bool Wayland_SurfaceHasActiveTouches(struct wl_surface *surface)
{
struct SDL_WaylandTouchPoint *tp;
wl_list_for_each (tp, &touch_points, link) {
if (tp->surface == surface) {
return true;
}
}
return false;
}
static Uint64 Wayland_GetEventTimestamp(Uint64 nsTimestamp)
{
static Uint64 last;
static Uint64 timestamp_offset;
const Uint64 now = SDL_GetTicksNS();
if (nsTimestamp < last) {
// 32-bit timer rollover, bump the offset
timestamp_offset += SDL_MS_TO_NS(0x100000000LLU);
}
last = nsTimestamp;
if (!timestamp_offset) {
timestamp_offset = (now - nsTimestamp);
}
nsTimestamp += timestamp_offset;
if (nsTimestamp > now) {
timestamp_offset -= (nsTimestamp - now);
nsTimestamp = now;
}
return nsTimestamp;
}
static void Wayland_input_timestamp_listener(void *data, struct zwp_input_timestamps_v1 *zwp_input_timestamps_v1,
uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec)
{
*((Uint64 *)data) = ((((Uint64)tv_sec_hi << 32) | (Uint64)tv_sec_lo) * SDL_NS_PER_SECOND) + tv_nsec;
}
static const struct zwp_input_timestamps_v1_listener timestamp_listener = {
Wayland_input_timestamp_listener
};
static Uint64 Wayland_GetKeyboardTimestamp(struct SDL_WaylandInput *input, Uint32 wl_timestamp_ms)
{
if (wl_timestamp_ms) {
return Wayland_GetEventTimestamp(input->keyboard_timestamp_ns ? input->keyboard_timestamp_ns : SDL_MS_TO_NS(wl_timestamp_ms));
}
return 0;
}
static Uint64 Wayland_GetKeyboardTimestampRaw(struct SDL_WaylandInput *input, Uint32 wl_timestamp_ms)
{
if (wl_timestamp_ms) {
return input->keyboard_timestamp_ns ? input->keyboard_timestamp_ns : SDL_MS_TO_NS(wl_timestamp_ms);
}
return 0;
}
static Uint64 Wayland_GetPointerTimestamp(struct SDL_WaylandInput *input, Uint32 wl_timestamp_ms)
{
if (wl_timestamp_ms) {
return Wayland_GetEventTimestamp(input->pointer_timestamp_ns ? input->pointer_timestamp_ns : SDL_MS_TO_NS(wl_timestamp_ms));
}
return 0;
}
Uint64 Wayland_GetTouchTimestamp(struct SDL_WaylandInput *input, Uint32 wl_timestamp_ms)
{
if (wl_timestamp_ms) {
return Wayland_GetEventTimestamp(input->touch_timestamp_ns ? input->touch_timestamp_ns : SDL_MS_TO_NS(wl_timestamp_ms));
}
return 0;
}
void Wayland_RegisterTimestampListeners(struct SDL_WaylandInput *input)
{
SDL_VideoData *viddata = input->display;
if (viddata->input_timestamps_manager) {
if (input->keyboard && !input->keyboard_timestamps) {
input->keyboard_timestamps = zwp_input_timestamps_manager_v1_get_keyboard_timestamps(viddata->input_timestamps_manager, input->keyboard);
zwp_input_timestamps_v1_add_listener(input->keyboard_timestamps, ×tamp_listener, &input->keyboard_timestamp_ns);
}
if (input->pointer && !input->pointer_timestamps) {
input->pointer_timestamps = zwp_input_timestamps_manager_v1_get_pointer_timestamps(viddata->input_timestamps_manager, input->pointer);
zwp_input_timestamps_v1_add_listener(input->pointer_timestamps, ×tamp_listener, &input->pointer_timestamp_ns);
}
if (input->touch && !input->touch_timestamps) {
input->touch_timestamps = zwp_input_timestamps_manager_v1_get_touch_timestamps(viddata->input_timestamps_manager, input->touch);
zwp_input_timestamps_v1_add_listener(input->touch_timestamps, ×tamp_listener, &input->touch_timestamp_ns);
}
}
}
void Wayland_CreateCursorShapeDevice(struct SDL_WaylandInput *input)
{
SDL_VideoData *viddata = input->display;
if (viddata->cursor_shape_manager) {
if (input->pointer && !input->cursor_shape) {
input->cursor_shape = wp_cursor_shape_manager_v1_get_pointer(viddata->cursor_shape_manager, input->pointer);
}
}
}
// Returns true if a key repeat event was due
static bool keyboard_repeat_handle(SDL_WaylandKeyboardRepeat *repeat_info, Uint64 elapsed)
{
bool ret = false;
while (elapsed >= repeat_info->next_repeat_ns) {
if (repeat_info->scancode != SDL_SCANCODE_UNKNOWN) {
const Uint64 timestamp = repeat_info->wl_press_time_ns + repeat_info->next_repeat_ns;
SDL_SendKeyboardKeyIgnoreModifiers(Wayland_GetEventTimestamp(timestamp), repeat_info->keyboard_id, repeat_info->key, repeat_info->scancode, true);
}
if (repeat_info->text[0]) {
SDL_SendKeyboardText(repeat_info->text);
}
repeat_info->next_repeat_ns += SDL_NS_PER_SECOND / (Uint64)repeat_info->repeat_rate;
ret = true;
}
return ret;
}
static void keyboard_repeat_clear(SDL_WaylandKeyboardRepeat *repeat_info)
{
if (!repeat_info->is_initialized) {
return;
}
repeat_info->is_key_down = false;
}
static void keyboard_repeat_set(SDL_WaylandKeyboardRepeat *repeat_info, Uint32 keyboard_id, uint32_t key, Uint64 wl_press_time_ns,
uint32_t scancode, bool has_text, char text[8])
{
if (!repeat_info->is_initialized || !repeat_info->repeat_rate) {
return;
}
repeat_info->is_key_down = true;
repeat_info->keyboard_id = keyboard_id;
repeat_info->key = key;
repeat_info->wl_press_time_ns = wl_press_time_ns;
repeat_info->sdl_press_time_ns = SDL_GetTicksNS();
repeat_info->next_repeat_ns = SDL_MS_TO_NS(repeat_info->repeat_delay_ms);
repeat_info->scancode = scancode;
if (has_text) {
SDL_copyp(repeat_info->text, text);
} else {
repeat_info->text[0] = '\0';
}
}
static uint32_t keyboard_repeat_get_key(SDL_WaylandKeyboardRepeat *repeat_info)
{
if (repeat_info->is_initialized && repeat_info->is_key_down) {
return repeat_info->key;
}
return 0;
}
static void keyboard_repeat_set_text(SDL_WaylandKeyboardRepeat *repeat_info, const char text[8])
{
if (repeat_info->is_initialized) {
SDL_copyp(repeat_info->text, text);
}
}
static bool keyboard_repeat_is_set(SDL_WaylandKeyboardRepeat *repeat_info)
{
return repeat_info->is_initialized && repeat_info->is_key_down;
}
static bool keyboard_repeat_key_is_set(SDL_WaylandKeyboardRepeat *repeat_info, uint32_t key)
{
return repeat_info->is_initialized && repeat_info->is_key_down && key == repeat_info->key;
}
static void sync_done_handler(void *data, struct wl_callback *callback, uint32_t callback_data)
{
// Nothing to do, just destroy the callback
wl_callback_destroy(callback);
}
static struct wl_callback_listener sync_listener = {
sync_done_handler
};
void Wayland_SendWakeupEvent(SDL_VideoDevice *_this, SDL_Window *window)
{
SDL_VideoData *d = _this->internal;
/* Queue a sync event to unblock the event queue fd if it's empty and being waited on.
* TODO: Maybe use a pipe to avoid the compositor roundtrip?
*/
struct wl_callback *cb = wl_display_sync(d->display);
wl_callback_add_listener(cb, &sync_listener, NULL);
WAYLAND_wl_display_flush(d->display);
}
static int dispatch_queued_events(SDL_VideoData *viddata)
{
int rc;
/*
* NOTE: When reconnection is implemented, check if libdecor needs to be
* involved in the reconnection process.
*/
#ifdef HAVE_LIBDECOR_H
if (viddata->shell.libdecor) {
libdecor_dispatch(viddata->shell.libdecor, 0);
}
#endif
rc = WAYLAND_wl_display_dispatch_pending(viddata->display);
return rc >= 0 ? 1 : rc;
}
int Wayland_WaitEventTimeout(SDL_VideoDevice *_this, Sint64 timeoutNS)
{
SDL_VideoData *d = _this->internal;
struct SDL_WaylandInput *input = d->input;
bool key_repeat_active = false;
WAYLAND_wl_display_flush(d->display);
#ifdef SDL_USE_IME
SDL_Window *keyboard_focus = SDL_GetKeyboardFocus();
if (!d->text_input_manager && keyboard_focus && SDL_TextInputActive(keyboard_focus)) {
SDL_IME_PumpEvents();
}
#endif
#ifdef SDL_USE_LIBDBUS
SDL_DBus_PumpEvents();
#endif
// If key repeat is active, we'll need to cap our maximum wait time to handle repeats
if (input && keyboard_repeat_is_set(&input->keyboard_repeat)) {
const Uint64 elapsed = SDL_GetTicksNS() - input->keyboard_repeat.sdl_press_time_ns;
if (keyboard_repeat_handle(&input->keyboard_repeat, elapsed)) {
// A repeat key event was already due
return 1;
} else {
const Uint64 next_repeat_wait_time = (input->keyboard_repeat.next_repeat_ns - elapsed) + 1;
if (timeoutNS >= 0) {
timeoutNS = SDL_min(timeoutNS, next_repeat_wait_time);
} else {
timeoutNS = next_repeat_wait_time;
}
key_repeat_active = true;
}
}
/* wl_display_prepare_read() will return -1 if the default queue is not empty.
* If the default queue is empty, it will prepare us for our SDL_IOReady() call. */
if (WAYLAND_wl_display_prepare_read(d->display) == 0) {
// Use SDL_IOR_NO_RETRY to ensure SIGINT will break us out of our wait
int err = SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_IOR_READ | SDL_IOR_NO_RETRY, timeoutNS);
if (err > 0) {
// There are new events available to read
WAYLAND_wl_display_read_events(d->display);
return dispatch_queued_events(d);
} else if (err == 0) {
// No events available within the timeout
WAYLAND_wl_display_cancel_read(d->display);
// If key repeat is active, we might have woken up to generate a key event
if (key_repeat_active) {
const Uint64 elapsed = SDL_GetTicksNS() - input->keyboard_repeat.sdl_press_time_ns;
if (keyboard_repeat_handle(&input->keyboard_repeat, elapsed)) {
return 1;
}
}
return 0;
} else {
// Error returned from poll()/select()
WAYLAND_wl_display_cancel_read(d->display);
if (errno == EINTR) {
/* If the wait was interrupted by a signal, we may have generated a
* SDL_EVENT_QUIT event. Let the caller know to call SDL_PumpEvents(). */
return 1;
} else {
return err;
}
}
} else {
// We already had pending events
return dispatch_queued_events(d);
}
}
void Wayland_PumpEvents(SDL_VideoDevice *_this)
{
SDL_VideoData *d = _this->internal;
struct SDL_WaylandInput *input = d->input;
int err;
#ifdef SDL_USE_IME
SDL_Window *keyboard_focus = SDL_GetKeyboardFocus();
if (!d->text_input_manager && keyboard_focus && SDL_TextInputActive(keyboard_focus)) {
SDL_IME_PumpEvents();
}
#endif
#ifdef SDL_USE_LIBDBUS
SDL_DBus_PumpEvents();
#endif
#ifdef HAVE_LIBDECOR_H
if (d->shell.libdecor) {
libdecor_dispatch(d->shell.libdecor, 0);
}
#endif
WAYLAND_wl_display_flush(d->display);
/* wl_display_prepare_read() will return -1 if the default queue is not empty.
* If the default queue is empty, it will prepare us for our SDL_IOReady() call. */
if (WAYLAND_wl_display_prepare_read(d->display) == 0) {
if (SDL_IOReady(WAYLAND_wl_display_get_fd(d->display), SDL_IOR_READ, 0) > 0) {
WAYLAND_wl_display_read_events(d->display);
} else {
WAYLAND_wl_display_cancel_read(d->display);
}
}
// Dispatch any pre-existing pending events or new events we may have read
err = WAYLAND_wl_display_dispatch_pending(d->display);
if (input && keyboard_repeat_is_set(&input->keyboard_repeat)) {
const Uint64 elapsed = SDL_GetTicksNS() - input->keyboard_repeat.sdl_press_time_ns;
keyboard_repeat_handle(&input->keyboard_repeat, elapsed);
}
if (err < 0 && !d->display_disconnected) {
/* Something has failed with the Wayland connection -- for example,
* the compositor may have shut down and closed its end of the socket,
* or there is a library-specific error.
*
* Try to recover once, then quit.
*/
if (!Wayland_VideoReconnect(_this)) {
d->display_disconnected = 1;
SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Wayland display connection closed by server (fatal)");
/* Only send a single quit message, as application shutdown might call
* SDL_PumpEvents
*/
SDL_SendQuit();
}
}
}
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 SDL_WaylandInput *input = data;
SDL_WindowData *window_data = input->pointer_focus;
SDL_Window *window = window_data ? window_data->sdlwindow : NULL;
input->sx_w = sx_w;
input->sy_w = sy_w;
if (input->pointer_focus) {
const float sx = (float)(wl_fixed_to_double(sx_w) * window_data->pointer_scale.x);
const float sy = (float)(wl_fixed_to_double(sy_w) * window_data->pointer_scale.y);
SDL_SendMouseMotion(Wayland_GetPointerTimestamp(input, time), window_data->sdlwindow, input->pointer_id, false, sx, sy);
}
if (window && window->hit_test) {
const SDL_Point point = { (int)SDL_floor(wl_fixed_to_double(sx_w) * window_data->pointer_scale.x),
(int)SDL_floor(wl_fixed_to_double(sy_w) * window_data->pointer_scale.y) };
SDL_HitTestResult rc = window->hit_test(window, &point, window->hit_test_data);
if (rc == window_data->hit_test_result) {
return;
}
Wayland_SetHitTestCursor(rc);
window_data->hit_test_result = rc;
}
}
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 SDL_WaylandInput *input = data;
SDL_WindowData *window;
if (!surface) {
// enter event for a window we've just destroyed
return;
}
/* This handler will be called twice in Wayland 1.4
* Once for the window surface which has valid user data
* and again for the mouse cursor surface which does not have valid user data
* We ignore the later
*/
window = Wayland_GetWindowDataForOwnedSurface(surface);
if (window) {
input->pointer_focus = window;
input->pointer_enter_serial = serial;
SDL_SetMouseFocus(window->sdlwindow);
/* In the case of e.g. a pointer confine warp, we may receive an enter
* event with no following motion event, but with the new coordinates
* as part of the enter event.
*
* FIXME: This causes a movement event with an anomalous timestamp when
* the cursor enters the window.
*/
pointer_handle_motion(data, pointer, 0, sx_w, sy_w);
/* If the cursor was changed while our window didn't have pointer
* focus, we might need to trigger another call to
* wl_pointer_set_cursor() for the new cursor to be displayed. */
Wayland_SetHitTestCursor(window->hit_test_result);
}
}
static void pointer_handle_leave(void *data, struct wl_pointer *pointer,
uint32_t serial, struct wl_surface *surface)
{
struct SDL_WaylandInput *input = data;
if (!surface) {
return;
}
if (input->pointer_focus) {
SDL_WindowData *wind = Wayland_GetWindowDataForOwnedSurface(surface);
if (wind) {
// Clear the capture flag and raise all buttons
wind->sdlwindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
input->buttons_pressed = 0;
SDL_SendMouseButton(Wayland_GetPointerTimestamp(input, 0), wind->sdlwindow, input->pointer_id, SDL_BUTTON_LEFT, false);
SDL_SendMouseButton(Wayland_GetPointerTimestamp(input, 0), wind->sdlwindow, input->pointer_id, SDL_BUTTON_RIGHT, false);
SDL_SendMouseButton(Wayland_GetPointerTimestamp(input, 0), wind->sdlwindow, input->pointer_id, SDL_BUTTON_MIDDLE, false);
SDL_SendMouseButton(Wayland_GetPointerTimestamp(input, 0), wind->sdlwindow, input->pointer_id, SDL_BUTTON_X1, false);
SDL_SendMouseButton(Wayland_GetPointerTimestamp(input, 0), wind->sdlwindow, input->pointer_id, SDL_BUTTON_X2, false);
}
/* A pointer leave event may be emitted if the compositor hides the pointer in response to receiving a touch event.
* Don't relinquish focus if the surface has active touches, as the compositor is just transitioning from mouse to touch mode.
*/
if (!Wayland_SurfaceHasActiveTouches(surface)) {
SDL_SetMouseFocus(NULL);
}
input->pointer_focus = NULL;
}
}
static bool ProcessHitTest(SDL_WindowData *window_data,
struct wl_seat *seat,
wl_fixed_t sx_w, wl_fixed_t sy_w,
uint32_t serial)
{
SDL_Window *window = window_data->sdlwindow;
if (window->hit_test) {
static const uint32_t directions[] = {
XDG_TOPLEVEL_RESIZE_EDGE_TOP_LEFT, XDG_TOPLEVEL_RESIZE_EDGE_TOP,
XDG_TOPLEVEL_RESIZE_EDGE_TOP_RIGHT, XDG_TOPLEVEL_RESIZE_EDGE_RIGHT,
XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_RIGHT, XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM,
XDG_TOPLEVEL_RESIZE_EDGE_BOTTOM_LEFT, XDG_TOPLEVEL_RESIZE_EDGE_LEFT
};
#ifdef HAVE_LIBDECOR_H
static const uint32_t directions_libdecor[] = {
LIBDECOR_RESIZE_EDGE_TOP_LEFT, LIBDECOR_RESIZE_EDGE_TOP,
LIBDECOR_RESIZE_EDGE_TOP_RIGHT, LIBDECOR_RESIZE_EDGE_RIGHT,
LIBDECOR_RESIZE_EDGE_BOTTOM_RIGHT, LIBDECOR_RESIZE_EDGE_BOTTOM,
LIBDECOR_RESIZE_EDGE_BOTTOM_LEFT, LIBDECOR_RESIZE_EDGE_LEFT
};
#endif
switch (window_data->hit_test_result) {
case SDL_HITTEST_DRAGGABLE:
#ifdef HAVE_LIBDECOR_H
if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR) {
if (window_data->shell_surface.libdecor.frame) {
libdecor_frame_move(window_data->shell_surface.libdecor.frame,
seat,
serial);
}
} else
#endif
if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_TOPLEVEL) {
if (window_data->shell_surface.xdg.toplevel.xdg_toplevel) {
xdg_toplevel_move(window_data->shell_surface.xdg.toplevel.xdg_toplevel,
seat,
serial);
}
}
return true;
case SDL_HITTEST_RESIZE_TOPLEFT:
case SDL_HITTEST_RESIZE_TOP:
case SDL_HITTEST_RESIZE_TOPRIGHT:
case SDL_HITTEST_RESIZE_RIGHT:
case SDL_HITTEST_RESIZE_BOTTOMRIGHT:
case SDL_HITTEST_RESIZE_BOTTOM:
case SDL_HITTEST_RESIZE_BOTTOMLEFT:
case SDL_HITTEST_RESIZE_LEFT:
#ifdef HAVE_LIBDECOR_H
if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_LIBDECOR) {
if (window_data->shell_surface.libdecor.frame) {
libdecor_frame_resize(window_data->shell_surface.libdecor.frame,
seat,
serial,
directions_libdecor[window_data->hit_test_result - SDL_HITTEST_RESIZE_TOPLEFT]);
}
} else
#endif
if (window_data->shell_surface_type == WAYLAND_SHELL_SURFACE_TYPE_XDG_TOPLEVEL) {
if (window_data->shell_surface.xdg.toplevel.xdg_toplevel) {
xdg_toplevel_resize(window_data->shell_surface.xdg.toplevel.xdg_toplevel,
seat,
serial,
directions[window_data->hit_test_result - SDL_HITTEST_RESIZE_TOPLEFT]);
}
}
return true;
default:
return false;
}
}
return false;
}
static void pointer_handle_button_common(struct SDL_WaylandInput *input, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state_w)
{
SDL_WindowData *window = input->pointer_focus;
enum wl_pointer_button_state state = state_w;
Uint64 timestamp = Wayland_GetPointerTimestamp(input, time);
Uint8 sdl_button;
const bool down = (state != 0);
switch (button) {
case BTN_LEFT:
sdl_button = SDL_BUTTON_LEFT;
break;
case BTN_MIDDLE:
sdl_button = SDL_BUTTON_MIDDLE;
break;
case BTN_RIGHT:
sdl_button = SDL_BUTTON_RIGHT;
break;
case BTN_SIDE:
sdl_button = SDL_BUTTON_X1;
break;
case BTN_EXTRA:
sdl_button = SDL_BUTTON_X2;
break;
default:
return;
}
if (window) {
SDL_VideoData *viddata = window->waylandData;
bool ignore_click = false;
if (state) {
Wayland_UpdateImplicitGrabSerial(input, serial);
input->buttons_pressed |= SDL_BUTTON_MASK(sdl_button);
} else {
input->buttons_pressed &= ~(SDL_BUTTON_MASK(sdl_button));
}
if (sdl_button == SDL_BUTTON_LEFT &&
ProcessHitTest(input->pointer_focus, input->seat, input->sx_w, input->sy_w, serial)) {
return; // don't pass this event on to app.
}
// Possibly ignore this click if it was to gain focus.
if (window->last_focus_event_time_ns) {
if (state == WL_POINTER_BUTTON_STATE_PRESSED &&
(SDL_GetTicksNS() - window->last_focus_event_time_ns) < WAYLAND_FOCUS_CLICK_TIMEOUT_NS) {
ignore_click = !SDL_GetHintBoolean(SDL_HINT_MOUSE_FOCUS_CLICKTHROUGH, false);
}
window->last_focus_event_time_ns = 0;
}
/* Wayland won't let you "capture" the mouse, but it will automatically track
* the mouse outside the window if you drag outside of it, until you let go
* of all buttons (even if you add or remove presses outside the window, as
* long as any button is still down, the capture remains).
*
* The mouse is not captured in relative mode.
*/
if (!viddata->relative_mouse_mode) {
if (input->buttons_pressed != 0) {
window->sdlwindow->flags |= SDL_WINDOW_MOUSE_CAPTURE;
} else {
window->sdlwindow->flags &= ~SDL_WINDOW_MOUSE_CAPTURE;
}
}
if (!ignore_click) {
SDL_SendMouseButton(timestamp, window->sdlwindow, input->pointer_id, sdl_button, down);
}
}
}
static void pointer_handle_button(void *data, struct wl_pointer *pointer, uint32_t serial,
uint32_t time, uint32_t button, uint32_t state_w)
{
struct SDL_WaylandInput *input = data;
pointer_handle_button_common(input, serial, time, button, state_w);
}
static void pointer_handle_axis_common_v1(struct SDL_WaylandInput *input,
uint32_t time, uint32_t axis, wl_fixed_t value)
{
SDL_WindowData *window = input->pointer_focus;
const Uint64 timestamp = Wayland_GetPointerTimestamp(input, time);
const enum wl_pointer_axis a = axis;
if (input->pointer_focus) {
float x, y;
switch (a) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
x = 0;
y = 0 - (float)wl_fixed_to_double(value);
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
x = (float)wl_fixed_to_double(value);
y = 0;
break;
default:
return;
}
x /= WAYLAND_WHEEL_AXIS_UNIT;
y /= WAYLAND_WHEEL_AXIS_UNIT;
SDL_SendMouseWheel(timestamp, window->sdlwindow, input->pointer_id, x, y, SDL_MOUSEWHEEL_NORMAL);
}
}
static void pointer_handle_axis_common(struct SDL_WaylandInput *input, enum SDL_WaylandAxisEvent type,
uint32_t axis, wl_fixed_t value)
{
const enum wl_pointer_axis a = axis;
if (input->pointer_focus) {
switch (a) {
case WL_POINTER_AXIS_VERTICAL_SCROLL:
switch (type) {
case AXIS_EVENT_VALUE120:
/*
* High resolution scroll event. The spec doesn't state that axis_value120
* events are limited to one per frame, so the values are accumulated.
*/
if (input->pointer_curr_axis_info.y_axis_type != AXIS_EVENT_VALUE120) {
input->pointer_curr_axis_info.y_axis_type = AXIS_EVENT_VALUE120;
input->pointer_curr_axis_info.y = 0.0f;
}
input->pointer_curr_axis_info.y += 0 - (float)wl_fixed_to_double(value);
break;
case AXIS_EVENT_DISCRETE:
/*
* This is a discrete axis event, so we process it and set the
* flag to ignore future continuous axis events in this frame.
*/
if (input->pointer_curr_axis_info.y_axis_type != AXIS_EVENT_DISCRETE) {
input->pointer_curr_axis_info.y_axis_type = AXIS_EVENT_DISCRETE;
input->pointer_curr_axis_info.y = 0 - (float)wl_fixed_to_double(value);
}
break;
case AXIS_EVENT_CONTINUOUS:
// Only process continuous events if no discrete events have been received.
if (input->pointer_curr_axis_info.y_axis_type == AXIS_EVENT_CONTINUOUS) {
input->pointer_curr_axis_info.y = 0 - (float)wl_fixed_to_double(value);
}
break;
}
break;
case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
switch (type) {
case AXIS_EVENT_VALUE120:
/*
* High resolution scroll event. The spec doesn't state that axis_value120
* events are limited to one per frame, so the values are accumulated.
*/
if (input->pointer_curr_axis_info.x_axis_type != AXIS_EVENT_VALUE120) {
input->pointer_curr_axis_info.x_axis_type = AXIS_EVENT_VALUE120;
input->pointer_curr_axis_info.x = 0.0f;
}
input->pointer_curr_axis_info.x += (float)wl_fixed_to_double(value);
break;
case AXIS_EVENT_DISCRETE:
/*
* This is a discrete axis event, so we process it and set the
* flag to ignore future continuous axis events in this frame.
*/
if (input->pointer_curr_axis_info.x_axis_type != AXIS_EVENT_DISCRETE) {
input->pointer_curr_axis_info.x_axis_type = AXIS_EVENT_DISCRETE;
input->pointer_curr_axis_info.x = (float)wl_fixed_to_double(value);
}
break;
case AXIS_EVENT_CONTINUOUS:
// Only process continuous events if no discrete events have been received.
if (input->pointer_curr_axis_info.x_axis_type == AXIS_EVENT_CONTINUOUS) {
input->pointer_curr_axis_info.x = (float)wl_fixed_to_double(value);
}
break;
}
break;
}
}
}
static void pointer_handle_axis(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis, wl_fixed_t value)
{
struct SDL_WaylandInput *input = data;
if (wl_seat_get_version(input->seat) >= WL_POINTER_FRAME_SINCE_VERSION) {
input->pointer_curr_axis_info.timestamp_ns = Wayland_GetPointerTimestamp(input, time);
pointer_handle_axis_common(input, AXIS_EVENT_CONTINUOUS, axis, value);
} else {
pointer_handle_axis_common_v1(input, time, axis, value);
}
}
static void pointer_handle_axis_relative_direction(void *data, struct wl_pointer *pointer,
uint32_t axis, uint32_t axis_relative_direction)
{
struct SDL_WaylandInput *input = data;
if (axis != WL_POINTER_AXIS_VERTICAL_SCROLL) {
return;
}
switch (axis_relative_direction) {
case WL_POINTER_AXIS_RELATIVE_DIRECTION_IDENTICAL:
input->pointer_curr_axis_info.direction = SDL_MOUSEWHEEL_NORMAL;
break;
case WL_POINTER_AXIS_RELATIVE_DIRECTION_INVERTED:
input->pointer_curr_axis_info.direction = SDL_MOUSEWHEEL_FLIPPED;
break;
}
}
static void pointer_handle_frame(void *data, struct wl_pointer *pointer)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window = input->pointer_focus;
float x, y;
SDL_MouseWheelDirection direction = input->pointer_curr_axis_info.direction;
switch (input->pointer_curr_axis_info.x_axis_type) {
case AXIS_EVENT_CONTINUOUS:
x = input->pointer_curr_axis_info.x / WAYLAND_WHEEL_AXIS_UNIT;
break;
case AXIS_EVENT_DISCRETE:
x = input->pointer_curr_axis_info.x;
break;
case AXIS_EVENT_VALUE120:
x = input->pointer_curr_axis_info.x / 120.0f;
break;
default:
x = 0.0f;
break;
}
switch (input->pointer_curr_axis_info.y_axis_type) {
case AXIS_EVENT_CONTINUOUS:
y = input->pointer_curr_axis_info.y / WAYLAND_WHEEL_AXIS_UNIT;
break;
case AXIS_EVENT_DISCRETE:
y = input->pointer_curr_axis_info.y;
break;
case AXIS_EVENT_VALUE120:
y = input->pointer_curr_axis_info.y / 120.0f;
break;
default:
y = 0.0f;
break;
}
// clear pointer_curr_axis_info for next frame
SDL_memset(&input->pointer_curr_axis_info, 0, sizeof(input->pointer_curr_axis_info));
if (x != 0.0f || y != 0.0f) {
SDL_SendMouseWheel(input->pointer_curr_axis_info.timestamp_ns,
window->sdlwindow, input->pointer_id, x, y, direction);
}
}
static void pointer_handle_axis_source(void *data, struct wl_pointer *pointer,
uint32_t axis_source)
{
// unimplemented
}
static void pointer_handle_axis_stop(void *data, struct wl_pointer *pointer,
uint32_t time, uint32_t axis)
{
// unimplemented
}
static void pointer_handle_axis_discrete(void *data, struct wl_pointer *pointer,
uint32_t axis, int32_t discrete)
{
struct SDL_WaylandInput *input = data;
pointer_handle_axis_common(input, AXIS_EVENT_DISCRETE, axis, wl_fixed_from_int(discrete));
}
static void pointer_handle_axis_value120(void *data, struct wl_pointer *pointer,
uint32_t axis, int32_t value120)
{
struct SDL_WaylandInput *input = data;
pointer_handle_axis_common(input, AXIS_EVENT_VALUE120, axis, wl_fixed_from_int(value120));
}
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, // Version 5
pointer_handle_axis_source, // Version 5
pointer_handle_axis_stop, // Version 5
pointer_handle_axis_discrete, // Version 5
pointer_handle_axis_value120, // Version 8
pointer_handle_axis_relative_direction // Version 9
};
static void relative_pointer_handle_relative_motion(void *data,
struct zwp_relative_pointer_v1 *pointer,
uint32_t time_hi,
uint32_t time_lo,
wl_fixed_t dx_w,
wl_fixed_t dy_w,
wl_fixed_t dx_unaccel_w,
wl_fixed_t dy_unaccel_w)
{
struct SDL_WaylandInput *input = data;
SDL_VideoData *d = input->display;
SDL_WindowData *window = input->pointer_focus;
// Relative pointer event times are in microsecond granularity.
const Uint64 timestamp = Wayland_GetEventTimestamp(SDL_US_TO_NS(((Uint64)time_hi << 32) | (Uint64)time_lo));
if (input->pointer_focus && d->relative_mouse_mode) {
double dx;
double dy;
if (!SDL_GetMouse()->enable_relative_system_scale) {
dx = wl_fixed_to_double(dx_unaccel_w);
dy = wl_fixed_to_double(dy_unaccel_w);
} else {
dx = wl_fixed_to_double(dx_w);
dy = wl_fixed_to_double(dy_w);
}
SDL_SendMouseMotion(timestamp, window->sdlwindow, input->pointer_id, true, (float)dx, (float)dy);
}
}
static const struct zwp_relative_pointer_v1_listener relative_pointer_listener = {
relative_pointer_handle_relative_motion,
};
static void locked_pointer_locked(void *data,
struct zwp_locked_pointer_v1 *locked_pointer)
{
}
static void locked_pointer_unlocked(void *data,
struct zwp_locked_pointer_v1 *locked_pointer)
{
}
static const struct zwp_locked_pointer_v1_listener locked_pointer_listener = {
locked_pointer_locked,
locked_pointer_unlocked,
};
bool Wayland_input_lock_pointer(struct SDL_WaylandInput *input, SDL_Window *window)
{
SDL_WindowData *w = window->internal;
SDL_VideoData *d = input->display;
if (!d->pointer_constraints || !input->pointer) {
return false;
}
if (!w->locked_pointer) {
if (w->confined_pointer) {
// If the pointer is already confined to the surface, the lock will fail with a protocol error.
Wayland_input_unconfine_pointer(input, window);
}
w->locked_pointer = zwp_pointer_constraints_v1_lock_pointer(d->pointer_constraints,
w->surface,
input->pointer,
NULL,
ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
zwp_locked_pointer_v1_add_listener(w->locked_pointer,
&locked_pointer_listener,
window);
}
return true;
}
bool Wayland_input_unlock_pointer(struct SDL_WaylandInput *input, SDL_Window *window)
{
SDL_WindowData *w = window->internal;
if (w->locked_pointer) {
zwp_locked_pointer_v1_destroy(w->locked_pointer);
w->locked_pointer = NULL;
}
// Restore existing pointer confinement.
Wayland_input_confine_pointer(input, window);
return true;
}
static void pointer_confine_destroy(SDL_Window *window)
{
SDL_WindowData *w = window->internal;
if (w->confined_pointer) {
zwp_confined_pointer_v1_destroy(w->confined_pointer);
w->confined_pointer = NULL;
}
}
static void confined_pointer_confined(void *data,
struct zwp_confined_pointer_v1 *confined_pointer)
{
}
static void confined_pointer_unconfined(void *data,
struct zwp_confined_pointer_v1 *confined_pointer)
{
}
static const struct zwp_confined_pointer_v1_listener confined_pointer_listener = {
confined_pointer_confined,
confined_pointer_unconfined,
};
static void touch_handler_down(void *data, struct wl_touch *touch, uint32_t serial,
uint32_t timestamp, struct wl_surface *surface,
int id, wl_fixed_t fx, wl_fixed_t fy)
{
struct SDL_WaylandInput *input = (struct SDL_WaylandInput *)data;
SDL_WindowData *window_data;
// Check that this surface is valid.
if (!surface) {
return;
}
touch_add(id, fx, fy, surface);
Wayland_UpdateImplicitGrabSerial(input, serial);
window_data = Wayland_GetWindowDataForOwnedSurface(surface);
if (window_data) {
float x, y;
if (window_data->current.logical_width <= 1) {
x = 0.5f;
} else {
x = (float)wl_fixed_to_double(fx) / (window_data->current.logical_width - 1);
}
if (window_data->current.logical_height <= 1) {
y = 0.5f;
} else {
y = (float)wl_fixed_to_double(fy) / (window_data->current.logical_height - 1);
}
SDL_SetMouseFocus(window_data->sdlwindow);
SDL_SendTouch(Wayland_GetTouchTimestamp(input, timestamp), (SDL_TouchID)(uintptr_t)touch,
(SDL_FingerID)(id + 1), window_data->sdlwindow, SDL_EVENT_FINGER_DOWN, x, y, 1.0f);
}
}
static void touch_handler_up(void *data, struct wl_touch *touch, uint32_t serial,
uint32_t timestamp, int id)
{
struct SDL_WaylandInput *input = (struct SDL_WaylandInput *)data;
wl_fixed_t fx = 0, fy = 0;
struct wl_surface *surface = NULL;
touch_del(id, &fx, &fy, &surface);
if (surface) {
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
if (window_data) {
const float x = (float)wl_fixed_to_double(fx) / window_data->current.logical_width;
const float y = (float)wl_fixed_to_double(fy) / window_data->current.logical_height;
SDL_SendTouch(Wayland_GetTouchTimestamp(input, timestamp), (SDL_TouchID)(uintptr_t)touch,
(SDL_FingerID)(id + 1), window_data->sdlwindow, SDL_EVENT_FINGER_UP, x, y, 0.0f);
/* If the seat lacks pointer focus, the seat's keyboard focus is another window or NULL, this window currently
* has mouse focus, and the surface has no active touch events, consider mouse focus to be lost.
*/
if (!input->pointer_focus && input->keyboard_focus != window_data &&
SDL_GetMouseFocus() == window_data->sdlwindow && !Wayland_SurfaceHasActiveTouches(surface)) {
SDL_SetMouseFocus(NULL);
}
}
}
}
static void touch_handler_motion(void *data, struct wl_touch *touch, uint32_t timestamp,
int id, wl_fixed_t fx, wl_fixed_t fy)
{
struct SDL_WaylandInput *input = (struct SDL_WaylandInput *)data;
struct wl_surface *surface = NULL;
touch_update(id, fx, fy, &surface);
if (surface) {
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(surface);
if (window_data) {
const float x = (float)wl_fixed_to_double(fx) / window_data->current.logical_width;
const float y = (float)wl_fixed_to_double(fy) / window_data->current.logical_height;
SDL_SendTouchMotion(Wayland_GetPointerTimestamp(input, timestamp), (SDL_TouchID)(uintptr_t)touch,
(SDL_FingerID)(id + 1), window_data->sdlwindow, x, y, 1.0f);
}
}
}
static void touch_handler_frame(void *data, struct wl_touch *touch)
{
}
static void touch_handler_cancel(void *data, struct wl_touch *touch)
{
struct SDL_WaylandInput *input = (struct SDL_WaylandInput *)data;
struct SDL_WaylandTouchPoint *tp, *temp;
wl_list_for_each_safe (tp, temp, &touch_points, link) {
bool removed = false;
if (tp->surface) {
SDL_WindowData *window_data = (SDL_WindowData *)wl_surface_get_user_data(tp->surface);
if (window_data) {
const float x = (float)(wl_fixed_to_double(tp->fx) / window_data->current.logical_width);
const float y = (float)(wl_fixed_to_double(tp->fy) / window_data->current.logical_height);
SDL_SendTouch(0, (SDL_TouchID)(uintptr_t)touch,
(SDL_FingerID)(tp->id + 1), window_data->sdlwindow, SDL_EVENT_FINGER_CANCELED, x, y, 0.0f);
// Remove the touch from the list before checking for still-active touches on the surface.
WAYLAND_wl_list_remove(&tp->link);
removed = true;
/* If the seat lacks pointer focus, the seat's keyboard focus is another window or NULL, this window currently
* has mouse focus, and the surface has no active touch events, consider mouse focus to be lost.
*/
if (!input->pointer_focus && input->keyboard_focus != window_data &&
SDL_GetMouseFocus() == window_data->sdlwindow && !Wayland_SurfaceHasActiveTouches(tp->surface)) {
SDL_SetMouseFocus(NULL);
}
}
}
if (!removed) {
WAYLAND_wl_list_remove(&tp->link);
}
SDL_free(tp);
}
}
static const struct wl_touch_listener touch_listener = {
touch_handler_down,
touch_handler_up,
touch_handler_motion,
touch_handler_frame,
touch_handler_cancel,
NULL, // shape
NULL, // orientation
};
typedef struct Wayland_Keymap
{
SDL_Keymap *keymap;
struct xkb_state *state;
SDL_Keymod modstate;
} Wayland_Keymap;
static void Wayland_keymap_iter(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
{
Wayland_Keymap *sdlKeymap = (Wayland_Keymap *)data;
const xkb_keysym_t *syms;
const SDL_Scancode scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_XFREE86_2, (key - 8));
if (scancode == SDL_SCANCODE_UNKNOWN) {
return;
}
if (WAYLAND_xkb_state_key_get_syms(sdlKeymap->state, key, &syms) > 0) {
SDL_Keycode keycode = SDL_GetKeyCodeFromKeySym(syms[0], key, sdlKeymap->modstate);
if (!keycode) {
switch (scancode) {
case SDL_SCANCODE_RETURN:
keycode = SDLK_RETURN;
break;
case SDL_SCANCODE_ESCAPE:
keycode = SDLK_ESCAPE;
break;
case SDL_SCANCODE_BACKSPACE:
keycode = SDLK_BACKSPACE;
break;
case SDL_SCANCODE_DELETE:
keycode = SDLK_DELETE;
break;
default:
keycode = SDL_SCANCODE_TO_KEYCODE(scancode);
break;
}
}
SDL_SetKeymapEntry(sdlKeymap->keymap, scancode, sdlKeymap->modstate, keycode);
}
}
static void Wayland_UpdateKeymap(struct SDL_WaylandInput *input)
{
struct Keymod_masks
{
SDL_Keymod sdl_mask;
xkb_mod_mask_t xkb_mask;
} const keymod_masks[] = {
{ SDL_KMOD_NONE, 0 },
{ SDL_KMOD_SHIFT, input->xkb.idx_shift },
{ SDL_KMOD_CAPS, input->xkb.idx_caps },
{ SDL_KMOD_SHIFT | SDL_KMOD_CAPS, input->xkb.idx_shift | input->xkb.idx_caps },
{ SDL_KMOD_MODE, input->xkb.idx_mod5 },
{ SDL_KMOD_MODE | SDL_KMOD_SHIFT, input->xkb.idx_mod5 | input->xkb.idx_shift },
{ SDL_KMOD_MODE | SDL_KMOD_CAPS, input->xkb.idx_mod5 | input->xkb.idx_caps },
{ SDL_KMOD_MODE | SDL_KMOD_SHIFT | SDL_KMOD_CAPS, input->xkb.idx_mod5 | input->xkb.idx_shift | input->xkb.idx_caps },
{ SDL_KMOD_LEVEL5, input->xkb.idx_mod3 },
{ SDL_KMOD_LEVEL5 | SDL_KMOD_SHIFT, input->xkb.idx_mod3 | input->xkb.idx_shift },
{ SDL_KMOD_LEVEL5 | SDL_KMOD_CAPS, input->xkb.idx_mod3 | input->xkb.idx_caps },
{ SDL_KMOD_LEVEL5 | SDL_KMOD_SHIFT | SDL_KMOD_CAPS, input->xkb.idx_mod3 | input->xkb.idx_shift | input->xkb.idx_caps },
{ SDL_KMOD_LEVEL5 | SDL_KMOD_MODE, input->xkb.idx_mod3 | input->xkb.idx_mod5 },
{ SDL_KMOD_LEVEL5 | SDL_KMOD_MODE | SDL_KMOD_SHIFT, input->xkb.idx_mod3 | input->xkb.idx_mod5 | input->xkb.idx_shift },
{ SDL_KMOD_LEVEL5 | SDL_KMOD_MODE | SDL_KMOD_CAPS, input->xkb.idx_mod3 | input->xkb.idx_mod5 | input->xkb.idx_caps },
{ SDL_KMOD_LEVEL5 | SDL_KMOD_MODE | SDL_KMOD_SHIFT | SDL_KMOD_CAPS, input->xkb.idx_mod3 | input->xkb.idx_mod5 | input->xkb.idx_shift | input->xkb.idx_caps },
};
if (!input->keyboard_is_virtual) {
Wayland_Keymap keymap;
keymap.keymap = SDL_CreateKeymap();
if (!keymap.keymap) {
return;
}
keymap.state = WAYLAND_xkb_state_new(input->xkb.keymap);
if (!keymap.state) {
SDL_SetError("failed to create XKB state");
SDL_DestroyKeymap(keymap.keymap);
return;
}
for (int i = 0; i < SDL_arraysize(keymod_masks); ++i) {
keymap.modstate = keymod_masks[i].sdl_mask;
WAYLAND_xkb_state_update_mask(keymap.state,
keymod_masks[i].xkb_mask & (input->xkb.idx_shift | input->xkb.idx_mod5 | input->xkb.idx_mod3), 0, keymod_masks[i].xkb_mask & input->xkb.idx_caps,
0, 0, input->xkb.current_group);
WAYLAND_xkb_keymap_key_for_each(input->xkb.keymap,
Wayland_keymap_iter,
&keymap);
}
WAYLAND_xkb_state_unref(keymap.state);
SDL_SetKeymap(keymap.keymap, true);
} else {
// Virtual keyboards use the default keymap.
SDL_SetKeymap(NULL, true);
}
}
static void keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
uint32_t format, int fd, uint32_t size)
{
struct SDL_WaylandInput *input = data;
char *map_str;
const char *locale;
if (!data) {
close(fd);
return;
}
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
close(fd);
return;
}
map_str = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
if (map_str == MAP_FAILED) {
close(fd);
return;
}
if (input->xkb.keymap != NULL) {
/* if there's already a keymap loaded, throw it away rather than leaking it before
* parsing the new one
*/
WAYLAND_xkb_keymap_unref(input->xkb.keymap);
input->xkb.keymap = NULL;
}
input->xkb.keymap = WAYLAND_xkb_keymap_new_from_string(input->display->xkb_context,
map_str,
XKB_KEYMAP_FORMAT_TEXT_V1,
0);
munmap(map_str, size);
close(fd);
if (!input->xkb.keymap) {
SDL_SetError("failed to compile keymap");
return;
}
#define GET_MOD_INDEX(mod) \
WAYLAND_xkb_keymap_mod_get_index(input->xkb.keymap, XKB_MOD_NAME_##mod)
input->xkb.idx_shift = 1 << GET_MOD_INDEX(SHIFT);
input->xkb.idx_ctrl = 1 << GET_MOD_INDEX(CTRL);
input->xkb.idx_alt = 1 << GET_MOD_INDEX(ALT);
input->xkb.idx_gui = 1 << GET_MOD_INDEX(LOGO);
input->xkb.idx_mod3 = 1 << GET_MOD_INDEX(MOD3);
input->xkb.idx_mod5 = 1 << GET_MOD_INDEX(MOD5);
input->xkb.idx_num = 1 << GET_MOD_INDEX(NUM);
input->xkb.idx_caps = 1 << GET_MOD_INDEX(CAPS);
#undef GET_MOD_INDEX
if (input->xkb.state != NULL) {
/* if there's already a state, throw it away rather than leaking it before
* trying to create a new one with the new keymap.
*/
WAYLAND_xkb_state_unref(input->xkb.state);
input->xkb.state = NULL;
}
input->xkb.state = WAYLAND_xkb_state_new(input->xkb.keymap);
if (!input->xkb.state) {
SDL_SetError("failed to create XKB state");
WAYLAND_xkb_keymap_unref(input->xkb.keymap);
input->xkb.keymap = NULL;
return;
}
/*
* Assume that a nameless layout implies a virtual keyboard with an arbitrary layout.
* TODO: Use a better method of detection?
*/
input->keyboard_is_virtual = WAYLAND_xkb_keymap_layout_get_name(input->xkb.keymap, 0) == NULL;
// Update the keymap if changed.
if (input->xkb.current_group != XKB_GROUP_INVALID) {
Wayland_UpdateKeymap(input);
}
/*
* See https://blogs.s-osg.org/compose-key-support-weston/
* for further explanation on dead keys in Wayland.
*/
// Look up the preferred locale, falling back to "C" as default
locale = SDL_getenv("LC_ALL");
if (!locale) {
locale = SDL_getenv("LC_CTYPE");
if (!locale) {
locale = SDL_getenv("LANG");
if (!locale) {
locale = "C";
}
}
}
// Set up XKB compose table
if (input->xkb.compose_table != NULL) {
WAYLAND_xkb_compose_table_unref(input->xkb.compose_table);
input->xkb.compose_table = NULL;
}
input->xkb.compose_table = WAYLAND_xkb_compose_table_new_from_locale(input->display->xkb_context,
locale, XKB_COMPOSE_COMPILE_NO_FLAGS);
if (input->xkb.compose_table) {
// Set up XKB compose state
if (input->xkb.compose_state != NULL) {
WAYLAND_xkb_compose_state_unref(input->xkb.compose_state);
input->xkb.compose_state = NULL;
}
input->xkb.compose_state = WAYLAND_xkb_compose_state_new(input->xkb.compose_table,
XKB_COMPOSE_STATE_NO_FLAGS);
if (!input->xkb.compose_state) {
SDL_SetError("could not create XKB compose state");
WAYLAND_xkb_compose_table_unref(input->xkb.compose_table);
input->xkb.compose_table = NULL;
}
}
}
/*
* Virtual keyboards can have arbitrary layouts, arbitrary scancodes/keycodes, etc...
* Key presses from these devices must be looked up by their keysym value.
*/
static SDL_Scancode Wayland_GetScancodeForKey(struct SDL_WaylandInput *input, uint32_t key)
{
SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
if (!input->keyboard_is_virtual) {
scancode = SDL_GetScancodeFromTable(SDL_SCANCODE_TABLE_XFREE86_2, key);
} else {
const xkb_keysym_t *syms;
if (WAYLAND_xkb_keymap_key_get_syms_by_level(input->xkb.keymap, key + 8, input->xkb.current_group, 0, &syms) > 0) {
scancode = SDL_GetScancodeFromKeySym(syms[0], key);
}
}
return scancode;
}
static void Wayland_ReconcileModifiers(struct SDL_WaylandInput *input, bool key_pressed)
{
/* Handle explicit pressed modifier state. This will correct the modifier state
* if common modifier keys were remapped and the modifiers presumed to be set
* during a key press event were incorrect, or if the modifier was set to the
* pressed state via means other than pressing the physical key.
*/
if (!key_pressed) {
if (input->xkb.wl_pressed_modifiers & input->xkb.idx_shift) {
if (!(input->pressed_modifiers & SDL_KMOD_SHIFT)) {
input->pressed_modifiers |= SDL_KMOD_SHIFT;
}
} else {
input->pressed_modifiers &= ~SDL_KMOD_SHIFT;
}
if (input->xkb.wl_pressed_modifiers & input->xkb.idx_ctrl) {
if (!(input->pressed_modifiers & SDL_KMOD_CTRL)) {
input->pressed_modifiers |= SDL_KMOD_CTRL;
}
} else {
input->pressed_modifiers &= ~SDL_KMOD_CTRL;
}
if (input->xkb.wl_pressed_modifiers & input->xkb.idx_alt) {
if (!(input->pressed_modifiers & SDL_KMOD_ALT)) {
input->pressed_modifiers |= SDL_KMOD_ALT;
}
} else {
input->pressed_modifiers &= ~SDL_KMOD_ALT;
}
if (input->xkb.wl_pressed_modifiers & input->xkb.idx_gui) {
if (!(input->pressed_modifiers & SDL_KMOD_GUI)) {
input->pressed_modifiers |= SDL_KMOD_GUI;
}
} else {
input->pressed_modifiers &= ~SDL_KMOD_GUI;
}
/* Note: This is not backwards: in the default keymap, Mod5 is typically
* level 3 shift, and Mod3 is typically level 5 shift.
*/
if (input->xkb.wl_pressed_modifiers & input->xkb.idx_mod3) {
if (!(input->pressed_modifiers & SDL_KMOD_LEVEL5)) {
input->pressed_modifiers |= SDL_KMOD_LEVEL5;
}
} else {
input->pressed_modifiers &= ~SDL_KMOD_LEVEL5;
}
if (input->xkb.wl_pressed_modifiers & input->xkb.idx_mod5) {
if (!(input->pressed_modifiers & SDL_KMOD_MODE)) {
input->pressed_modifiers |= SDL_KMOD_MODE;
}
} else {
input->pressed_modifiers &= ~SDL_KMOD_MODE;
}
}
/* If a latch or lock was activated by a keypress, the latch/lock will
* be tied to the specific left/right key that initiated it. Otherwise,
* the ambiguous left/right combo is used.
*
* The modifier will remain active until the latch/lock is released by
* the system.
*/
if (input->xkb.wl_locked_modifiers & input->xkb.idx_shift) {
if (input->pressed_modifiers & SDL_KMOD_SHIFT) {
input->locked_modifiers &= ~SDL_KMOD_SHIFT;
input->locked_modifiers |= (input->pressed_modifiers & SDL_KMOD_SHIFT);
} else if (!(input->locked_modifiers & SDL_KMOD_SHIFT)) {
input->locked_modifiers |= SDL_KMOD_SHIFT;
}
} else {
input->locked_modifiers &= ~SDL_KMOD_SHIFT;
}
if (input->xkb.wl_locked_modifiers & input->xkb.idx_ctrl) {
if (input->pressed_modifiers & SDL_KMOD_CTRL) {
input->locked_modifiers &= ~SDL_KMOD_CTRL;
input->locked_modifiers |= (input->pressed_modifiers & SDL_KMOD_CTRL);
} else if (!(input->locked_modifiers & SDL_KMOD_CTRL)) {
input->locked_modifiers |= SDL_KMOD_CTRL;
}
} else {
input->locked_modifiers &= ~SDL_KMOD_CTRL;
}
if (input->xkb.wl_locked_modifiers & input->xkb.idx_alt) {
if (input->pressed_modifiers & SDL_KMOD_ALT) {
input->locked_modifiers &= ~SDL_KMOD_ALT;
input->locked_modifiers |= (input->pressed_modifiers & SDL_KMOD_ALT);
} else if (!(input->locked_modifiers & SDL_KMOD_ALT)) {
input->locked_modifiers |= SDL_KMOD_ALT;
}
} else {
input->locked_modifiers &= ~SDL_KMOD_ALT;
}
if (input->xkb.wl_locked_modifiers & input->xkb.idx_gui) {
if (input->pressed_modifiers & SDL_KMOD_GUI) {
input->locked_modifiers &= ~SDL_KMOD_GUI;
input->locked_modifiers |= (input->pressed_modifiers & SDL_KMOD_GUI);
} else if (!(input->locked_modifiers & SDL_KMOD_GUI)) {
input->locked_modifiers |= SDL_KMOD_GUI;
}
} else {
input->locked_modifiers &= ~SDL_KMOD_GUI;
}
// As above, this is correct: Mod3 is typically level 5 shift, and Mod5 is typically level 3 shift.
if (input->xkb.wl_locked_modifiers & input->xkb.idx_mod3) {
input->locked_modifiers |= SDL_KMOD_LEVEL5;
} else {
input->locked_modifiers &= ~SDL_KMOD_LEVEL5;
}
if (input->xkb.wl_locked_modifiers & input->xkb.idx_mod5) {
input->locked_modifiers |= SDL_KMOD_MODE;
} else {
input->locked_modifiers &= ~SDL_KMOD_MODE;
}
// Capslock and Numlock can only be locked, not pressed.
if (input->xkb.wl_locked_modifiers & input->xkb.idx_caps) {
input->locked_modifiers |= SDL_KMOD_CAPS;
} else {
input->locked_modifiers &= ~SDL_KMOD_CAPS;
}
if (input->xkb.wl_locked_modifiers & input->xkb.idx_num) {
input->locked_modifiers |= SDL_KMOD_NUM;
} else {
input->locked_modifiers &= ~SDL_KMOD_NUM;
}
SDL_SetModState(input->pressed_modifiers | input->locked_modifiers);
}
static void Wayland_HandleModifierKeys(struct SDL_WaylandInput *input, SDL_Scancode scancode, bool pressed)
{
const SDL_Keycode keycode = SDL_GetKeyFromScancode(scancode, SDL_KMOD_NONE, false);
SDL_Keymod mod;
/* SDL clients expect modifier state to be activated at the same time as the
* source keypress, so we set pressed modifier state with the usual modifier
* keys here, as the explicit modifier event won't arrive until after the
* keypress event. If this is wrong, it will be corrected when the explicit
* modifier state is sent at a later time.
*/
switch (keycode) {
case SDLK_LSHIFT:
mod = SDL_KMOD_LSHIFT;
break;
case SDLK_RSHIFT:
mod = SDL_KMOD_RSHIFT;
break;
case SDLK_LCTRL:
mod = SDL_KMOD_LCTRL;
break;
case SDLK_RCTRL:
mod = SDL_KMOD_RCTRL;
break;
case SDLK_LALT:
mod = SDL_KMOD_LALT;
break;
case SDLK_RALT:
mod = SDL_KMOD_RALT;
break;
case SDLK_LGUI:
mod = SDL_KMOD_LGUI;
break;
case SDLK_RGUI:
mod = SDL_KMOD_RGUI;
break;
case SDLK_MODE:
mod = SDL_KMOD_MODE;
break;
case SDLK_LEVEL5_SHIFT:
mod = SDL_KMOD_LEVEL5;
break;
default:
return;
}
if (pressed) {
input->pressed_modifiers |= mod;
} else {
input->pressed_modifiers &= ~mod;
}
Wayland_ReconcileModifiers(input, true);
}
static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface,
struct wl_array *keys)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *window;
uint32_t *key;
if (!surface) {
// enter event for a window we've just destroyed
return;
}
window = Wayland_GetWindowDataForOwnedSurface(surface);
if (!window) {
return;
}
input->keyboard_focus = window;
window->keyboard_device = input;
// Restore the keyboard focus to the child popup that was holding it
SDL_SetKeyboardFocus(window->keyboard_focus ? window->keyboard_focus : window->sdlwindow);
#ifdef SDL_USE_IME
if (!input->text_input) {
SDL_IME_SetFocus(true);
}
#endif
Uint64 timestamp = SDL_GetTicksNS();
window->last_focus_event_time_ns = timestamp;
wl_array_for_each (key, keys) {
const SDL_Scancode scancode = Wayland_GetScancodeForKey(input, *key);
const SDL_Keycode keycode = SDL_GetKeyFromScancode(scancode, SDL_KMOD_NONE, false);
switch (keycode) {
case SDLK_LSHIFT:
case SDLK_RSHIFT:
case SDLK_LCTRL:
case SDLK_RCTRL:
case SDLK_LALT:
case SDLK_RALT:
case SDLK_LGUI:
case SDLK_RGUI:
case SDLK_MODE:
case SDLK_LEVEL5_SHIFT:
Wayland_HandleModifierKeys(input, scancode, true);
SDL_SendKeyboardKeyIgnoreModifiers(timestamp, input->keyboard_id, *key, scancode, true);
break;
default:
break;
}
}
}
static void keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface)
{
struct SDL_WaylandInput *input = data;
SDL_WindowData *wind;
SDL_Window *window = NULL;
if (!surface) {
return;
}
wind = Wayland_GetWindowDataForOwnedSurface(surface);
if (!wind) {
return;
}
wind->keyboard_device = NULL;
window = wind->sdlwindow;
// Stop key repeat before clearing keyboard focus
keyboard_repeat_clear(&input->keyboard_repeat);
// This will release any keys still pressed
SDL_SetKeyboardFocus(NULL);
input->keyboard_focus = NULL;
// Clear the pressed modifiers.
input->pressed_modifiers = SDL_KMOD_NONE;
#ifdef SDL_USE_IME
if (!input->text_input) {
SDL_IME_SetFocus(false);
}
#endif
/* If the surface had a pointer leave event while still having active touch events, it retained mouse focus.
* Clear it now if all touch events are raised.
*/
if (!input->pointer_focus && SDL_GetMouseFocus() == window && !Wayland_SurfaceHasActiveTouches(surface)) {
SDL_SetMouseFocus(NULL);
}
}
static bool keyboard_input_get_text(char text[8], const struct SDL_WaylandInput *input, uint32_t key, bool down, bool *handled_by_ime)
{
SDL_WindowData *window = input->keyboard_focus;
const xkb_keysym_t *syms;
xkb_keysym_t sym;
if (!window || window->keyboard_device != input || !input->xkb.state) {
return false;
}
// TODO: Can this happen?
if (WAYLAND_xkb_state_key_get_syms(input->xkb.state, key + 8, &syms) != 1) {
return false;
}
sym = syms[0];
#ifdef SDL_USE_IME
if (SDL_IME_ProcessKeyEvent(sym, key + 8, down)) {
if (handled_by_ime) {
*handled_by_ime = true;
}
return true;
}
#endif
if (!down) {
return false;
}
if (input->xkb.compose_state && WAYLAND_xkb_compose_state_feed(input->xkb.compose_state, sym) == XKB_COMPOSE_FEED_ACCEPTED) {
switch (WAYLAND_xkb_compose_state_get_status(input->xkb.compose_state)) {
case XKB_COMPOSE_COMPOSING:
if (handled_by_ime) {
*handled_by_ime = true;
}
return true;
case XKB_COMPOSE_CANCELLED:
default:
sym = XKB_KEY_NoSymbol;
break;
case XKB_COMPOSE_NOTHING:
break;
case XKB_COMPOSE_COMPOSED:
sym = WAYLAND_xkb_compose_state_get_one_sym(input->xkb.compose_state);
break;
}
}
return WAYLAND_xkb_keysym_to_utf8(sym, text, 8) > 0;
}
static void keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
uint32_t serial, uint32_t time, uint32_t key,
uint32_t state_w)
{
struct SDL_WaylandInput *input = data;
enum wl_keyboard_key_state state = state_w;
char text[8];
bool has_text = false;
bool handled_by_ime = false;
const Uint64 timestamp_raw_ns = Wayland_GetKeyboardTimestampRaw(input, time);
Wayland_UpdateImplicitGrabSerial(input, serial);
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
SDL_Window *keyboard_focus = SDL_GetKeyboardFocus();
if (keyboard_focus && SDL_TextInputActive(keyboard_focus)) {
has_text = keyboard_input_get_text(text, input, key, true, &handled_by_ime);
}
} else {
if (keyboard_repeat_key_is_set(&input->keyboard_repeat, key)) {
/* Send any due key repeat events before stopping the repeat and generating the key up event.
* Compute time based on the Wayland time, as it reports when the release event happened.
* Using SDL_GetTicks would be wrong, as it would report when the release event is processed,
* which may be off if the application hasn't pumped events for a while.
*/
keyboard_repeat_handle(&input->keyboard_repeat, timestamp_raw_ns - input->keyboard_repeat.wl_press_time_ns);
keyboard_repeat_clear(&input->keyboard_repeat);
}
keyboard_input_get_text(text, input, key, false, &handled_by_ime);
}
const SDL_Scancode scancode = Wayland_GetScancodeForKey(input, key);
Wayland_HandleModifierKeys(input, scancode, state == WL_KEYBOARD_KEY_STATE_PRESSED);
Uint64 timestamp = Wayland_GetKeyboardTimestamp(input, time);
SDL_SendKeyboardKeyIgnoreModifiers(timestamp, input->keyboard_id, key, scancode, state == WL_KEYBOARD_KEY_STATE_PRESSED);
if (state == WL_KEYBOARD_KEY_STATE_PRESSED) {
if (has_text && !(SDL_GetModState() & SDL_KMOD_CTRL)) {
if (!handled_by_ime) {
SDL_SendKeyboardText(text);
}
}
if (input->xkb.keymap && WAYLAND_xkb_keymap_key_repeats(input->xkb.keymap, key + 8)) {
keyboard_repeat_set(&input->keyboard_repeat, input->keyboard_id, key, timestamp_raw_ns, scancode, has_text, text);
}
}
}
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 SDL_WaylandInput *input = data;
if (input->xkb.state == NULL) {
/* if we get a modifier notification before the keymap, there's nothing we can do with the information
*/
return;
}
WAYLAND_xkb_state_update_mask(input->xkb.state, mods_depressed, mods_latched,
mods_locked, 0, 0, group);
input->xkb.wl_pressed_modifiers = mods_depressed;
input->xkb.wl_locked_modifiers = mods_latched | mods_locked;
Wayland_ReconcileModifiers(input, false);
// If a key is repeating, update the text to apply the modifier.
if (keyboard_repeat_is_set(&input->keyboard_repeat)) {
char text[8];
const uint32_t key = keyboard_repeat_get_key(&input->keyboard_repeat);
if (keyboard_input_get_text(text, input, key, true, NULL)) {
keyboard_repeat_set_text(&input->keyboard_repeat, text);
}
}
if (group == input->xkb.current_group) {
return;
}
// The layout changed, remap and fire an event. Virtual keyboards use the default keymap.
input->xkb.current_group = group;
Wayland_UpdateKeymap(input);
}
static void keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
int32_t rate, int32_t delay)
{
struct SDL_WaylandInput *input = data;
input->keyboard_repeat.repeat_rate = SDL_clamp(rate, 0, 1000);
input->keyboard_repeat.repeat_delay_ms = delay;
input->keyboard_repeat.is_initialized = true;
}
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, // Version 4
};
void Wayland_input_init_relative_pointer(SDL_VideoData *d)
{
struct SDL_WaylandInput *input = d->input;
if (!d->relative_pointer_manager) {
return;
}
if (input->pointer && !input->relative_pointer) {
input->relative_pointer = zwp_relative_pointer_manager_v1_get_relative_pointer(input->display->relative_pointer_manager, input->pointer);
zwp_relative_pointer_v1_add_listener(input->relative_pointer,
&relative_pointer_listener,
input);
}
}
static void seat_handle_capabilities(void *data, struct wl_seat *seat,
enum wl_seat_capability caps)
{
struct SDL_WaylandInput *input = data;
if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
input->pointer = wl_seat_get_pointer(seat);
SDL_memset(&input->pointer_curr_axis_info, 0, sizeof(input->pointer_curr_axis_info));
input->display->pointer = input->pointer;
Wayland_CreateCursorShapeDevice(input);
wl_pointer_set_user_data(input->pointer, input);
wl_pointer_add_listener(input->pointer, &pointer_listener, input);
Wayland_input_init_relative_pointer(input->display);
input->pointer_id = SDL_GetNextObjectID();
SDL_AddMouse(input->pointer_id, WAYLAND_DEFAULT_POINTER_NAME, !input->display->initializing);
} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
if (input->relative_pointer) {
zwp_relative_pointer_v1_destroy(input->relative_pointer);
input->relative_pointer = NULL;
}
if (input->cursor_shape) {
wp_cursor_shape_device_v1_destroy(input->cursor_shape);
input->cursor_shape = NULL;
}
if (wl_pointer_get_version(input->pointer) >= WL_POINTER_RELEASE_SINCE_VERSION) {
wl_pointer_release(input->pointer);
} else {
wl_pointer_destroy(input->pointer);
}
input->pointer = NULL;
input->display->pointer = NULL;
SDL_RemoveMouse(input->pointer_id, true);
input->pointer_id = 0;
}
if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
input->touch = wl_seat_get_touch(seat);
SDL_AddTouch((SDL_TouchID)(uintptr_t)input->touch, SDL_TOUCH_DEVICE_DIRECT, "wayland_touch");
wl_touch_set_user_data(input->touch, input);
wl_touch_add_listener(input->touch, &touch_listener,
input);
} else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
SDL_DelTouch((SDL_TouchID)(intptr_t)input->touch);
wl_touch_destroy(input->touch);
input->touch = NULL;
}
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
input->keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_set_user_data(input->keyboard, input);
wl_keyboard_add_listener(input->keyboard, &keyboard_listener,
input);
input->keyboard_id = SDL_GetNextObjectID();
SDL_AddKeyboard(input->keyboard_id, WAYLAND_DEFAULT_KEYBOARD_NAME, !input->display->initializing);
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
wl_keyboard_destroy(input->keyboard);
input->keyboard = NULL;
SDL_RemoveKeyboard(input->keyboard_id, true);
input->keyboard_id = 0;
}
Wayland_RegisterTimestampListeners(input);
}
static void seat_handle_name(void *data, struct wl_seat *wl_seat, const char *name)
{
// unimplemented
}
static const struct wl_seat_listener seat_listener = {
seat_handle_capabilities,
seat_handle_name, // Version 2
};
static void data_source_handle_target(void *data, struct wl_data_source *wl_data_source,
const char *mime_type)
{
}
static void data_source_handle_send(void *data, struct wl_data_source *wl_data_source,
const char *mime_type, int32_t fd)
{
Wayland_data_source_send((SDL_WaylandDataSource *)data, mime_type, fd);
}
static void data_source_handle_cancelled(void *data, struct wl_data_source *wl_data_source)
{
SDL_WaylandDataSource *source = data;
if (source) {
Wayland_data_source_destroy(source);
}
}
static void data_source_handle_dnd_drop_performed(void *data, struct wl_data_source *wl_data_source)
{
}
static void data_source_handle_dnd_finished(void *data, struct wl_data_source *wl_data_source)
{
}
static void data_source_handle_action(void *data, struct wl_data_source *wl_data_source,
uint32_t dnd_action)
{
}
static const struct wl_data_source_listener data_source_listener = {
data_source_handle_target,
data_source_handle_send,
data_source_handle_cancelled,
data_source_handle_dnd_drop_performed, // Version 3
data_source_handle_dnd_finished, // Version 3
data_source_handle_action, // Version 3
};
static void primary_selection_source_send(void *data, struct zwp_primary_selection_source_v1 *zwp_primary_selection_source_v1,
const char *mime_type, int32_t fd)
{
Wayland_primary_selection_source_send((SDL_WaylandPrimarySelectionSource *)data,
mime_type, fd);
}
static void primary_selection_source_cancelled(void *data, struct zwp_primary_selection_source_v1 *zwp_primary_selection_source_v1)
{
Wayland_primary_selection_source_destroy(data);
}
static const struct zwp_primary_selection_source_v1_listener primary_selection_source_listener = {
primary_selection_source_send,
primary_selection_source_cancelled,
};
SDL_WaylandDataSource *Wayland_data_source_create(SDL_VideoDevice *_this)
{
SDL_WaylandDataSource *data_source = NULL;
SDL_VideoData *driver_data = NULL;
struct wl_data_source *id = NULL;
if (!_this || !_this->internal) {
SDL_SetError("Video driver uninitialized");
} else {
driver_data = _this->internal;
if (driver_data->data_device_manager) {
id = wl_data_device_manager_create_data_source(
driver_data->data_device_manager);
}
if (!id) {
SDL_SetError("Wayland unable to create data source");
} else {
data_source = SDL_calloc(1, sizeof(*data_source));
if (!data_source) {
wl_data_source_destroy(id);
} else {
data_source->source = id;
wl_data_source_set_user_data(id, data_source);
wl_data_source_add_listener(id, &data_source_listener,
data_source);
}
}
}
return data_source;
}
SDL_WaylandPrimarySelectionSource *Wayland_primary_selection_source_create(SDL_VideoDevice *_this)
{
SDL_WaylandPrimarySelectionSource *primary_selection_source = NULL;
SDL_VideoData *driver_data = NULL;
struct zwp_primary_selection_source_v1 *id = NULL;
if (!_this || !_this->internal) {
SDL_SetError("Video driver uninitialized");
} else {
driver_data = _this->internal;
if (driver_data->primary_selection_device_manager) {
id = zwp_primary_selection_device_manager_v1_create_source(
driver_data->primary_selection_device_manager);
}
if (!id) {
SDL_SetError("Wayland unable to create primary selection source");
} else {
primary_selection_source = SDL_calloc(1, sizeof(*primary_selection_source));
if (!primary_selection_source) {
zwp_primary_selection_source_v1_destroy(id);
} else {
primary_selection_source->source = id;
zwp_primary_selection_source_v1_add_listener(id, &primary_selection_source_listener,
primary_selection_source);
}
}
}
return primary_selection_source;
}
static void data_offer_handle_offer(void *data, struct wl_data_offer *wl_data_offer,
const char *mime_type)
{
SDL_WaylandDataOffer *offer = data;
Wayland_data_offer_add_mime(offer, mime_type);
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_offer_listener . data_offer_handle_offer on data_offer 0x%08x for MIME '%s'",
(wl_data_offer ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)wl_data_offer) : -1),
mime_type);
}
static void data_offer_handle_source_actions(void *data, struct wl_data_offer *wl_data_offer,
uint32_t source_actions)
{
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_offer_listener . data_offer_handle_source_actions on data_offer 0x%08x for Source Actions '%d'",
(wl_data_offer ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)wl_data_offer) : -1),
source_actions);
}
static void data_offer_handle_actions(void *data, struct wl_data_offer *wl_data_offer,
uint32_t dnd_action)
{
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_offer_listener . data_offer_handle_actions on data_offer 0x%08x for DND Actions '%d'",
(wl_data_offer ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)wl_data_offer) : -1),
dnd_action);
}
static const struct wl_data_offer_listener data_offer_listener = {
data_offer_handle_offer,
data_offer_handle_source_actions, // Version 3
data_offer_handle_actions, // Version 3
};
static void primary_selection_offer_handle_offer(void *data, struct zwp_primary_selection_offer_v1 *zwp_primary_selection_offer_v1,
const char *mime_type)
{
SDL_WaylandPrimarySelectionOffer *offer = data;
Wayland_primary_selection_offer_add_mime(offer, mime_type);
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In zwp_primary_selection_offer_v1_listener . primary_selection_offer_handle_offer on primary_selection_offer 0x%08x for MIME '%s'",
(zwp_primary_selection_offer_v1 ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)zwp_primary_selection_offer_v1) : -1),
mime_type);
}
static const struct zwp_primary_selection_offer_v1_listener primary_selection_offer_listener = {
primary_selection_offer_handle_offer,
};
static void data_device_handle_data_offer(void *data, struct wl_data_device *wl_data_device,
struct wl_data_offer *id)
{
SDL_WaylandDataOffer *data_offer = SDL_calloc(1, sizeof(*data_offer));
if (data_offer) {
data_offer->offer = id;
data_offer->data_device = data;
WAYLAND_wl_list_init(&(data_offer->mimes));
wl_data_offer_set_user_data(id, data_offer);
wl_data_offer_add_listener(id, &data_offer_listener, data_offer);
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_data_offer on data_offer 0x%08x",
(id ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)id) : -1));
}
}
static void data_device_handle_enter(void *data, struct wl_data_device *wl_data_device,
uint32_t serial, struct wl_surface *surface,
wl_fixed_t x, wl_fixed_t y, struct wl_data_offer *id)
{
SDL_WaylandDataDevice *data_device = data;
data_device->has_mime_file = false;
data_device->has_mime_text = false;
uint32_t dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_NONE;
data_device->drag_serial = serial;
if (id) {
data_device->drag_offer = wl_data_offer_get_user_data(id);
// TODO: SDL Support more mime types
#ifdef SDL_USE_LIBDBUS
if (Wayland_data_offer_has_mime(data_device->drag_offer, FILE_PORTAL_MIME)) {
data_device->has_mime_file = true;
wl_data_offer_accept(id, serial, FILE_PORTAL_MIME);
}
#endif
if (Wayland_data_offer_has_mime(data_device->drag_offer, FILE_MIME)) {
data_device->has_mime_file = true;
wl_data_offer_accept(id, serial, FILE_MIME);
}
if (Wayland_data_offer_has_mime(data_device->drag_offer, TEXT_MIME)) {
data_device->has_mime_text = true;
wl_data_offer_accept(id, serial, TEXT_MIME);
}
// SDL only supports "copy" style drag and drop
if (data_device->has_mime_file || data_device->has_mime_text) {
dnd_action = WL_DATA_DEVICE_MANAGER_DND_ACTION_COPY;
} else {
// drag_mime is NULL this will decline the offer
wl_data_offer_accept(id, serial, NULL);
}
if (wl_data_offer_get_version(data_device->drag_offer->offer) >=
WL_DATA_OFFER_SET_ACTIONS_SINCE_VERSION) {
wl_data_offer_set_actions(data_device->drag_offer->offer,
dnd_action, dnd_action);
}
// find the current window
if (surface) {
SDL_WindowData *window = Wayland_GetWindowDataForOwnedSurface(surface);
if (window) {
data_device->dnd_window = window->sdlwindow;
const float dx = (float)wl_fixed_to_double(x);
const float dy = (float)wl_fixed_to_double(y);
SDL_SendDropPosition(data_device->dnd_window, dx, dy);
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_enter on data_offer 0x%08x at %d x %d into window %d for serial %d",
WAYLAND_wl_proxy_get_id((struct wl_proxy *)id),
wl_fixed_to_int(x), wl_fixed_to_int(y), SDL_GetWindowID(data_device->dnd_window), serial);
} else {
data_device->dnd_window = NULL;
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_enter on data_offer 0x%08x at %d x %d for serial %d",
WAYLAND_wl_proxy_get_id((struct wl_proxy *)id),
wl_fixed_to_int(x), wl_fixed_to_int(y), serial);
}
} else {
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_enter on data_offer 0x%08x at %d x %d for serial %d",
WAYLAND_wl_proxy_get_id((struct wl_proxy *)id),
wl_fixed_to_int(x), wl_fixed_to_int(y), serial);
}
} else {
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_enter on data_offer 0x%08x at %d x %d for serial %d",
-1, wl_fixed_to_int(x), wl_fixed_to_int(y), serial);
}
}
static void data_device_handle_leave(void *data, struct wl_data_device *wl_data_device)
{
SDL_WaylandDataDevice *data_device = data;
if (data_device->drag_offer) {
if (data_device->dnd_window) {
SDL_SendDropComplete(data_device->dnd_window);
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_leave on data_offer 0x%08x from window %d for serial %d",
WAYLAND_wl_proxy_get_id((struct wl_proxy *)data_device->drag_offer->offer),
SDL_GetWindowID(data_device->dnd_window), data_device->drag_serial);
} else {
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_leave on data_offer 0x%08x for serial %d",
WAYLAND_wl_proxy_get_id((struct wl_proxy *)data_device->drag_offer->offer),
data_device->drag_serial);
}
Wayland_data_offer_destroy(data_device->drag_offer);
data_device->drag_offer = NULL;
} else {
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_leave on data_offer 0x%08x for serial %d",
-1, -1);
}
data_device->has_mime_file = false;
data_device->has_mime_text = false;
}
static void data_device_handle_motion(void *data, struct wl_data_device *wl_data_device,
uint32_t time, wl_fixed_t x, wl_fixed_t y)
{
SDL_WaylandDataDevice *data_device = data;
if (data_device->drag_offer && data_device->dnd_window && (data_device->has_mime_file || data_device->has_mime_text)) {
const float dx = (float)wl_fixed_to_double(x);
const float dy = (float)wl_fixed_to_double(y);
/* XXX: Send the filename here if the event system ever starts passing it though.
* Any future implementation should cache the filenames, as otherwise this could
* hammer the DBus interface hundreds or even thousands of times per second.
*/
SDL_SendDropPosition(data_device->dnd_window, dx, dy);
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_motion on data_offer 0x%08x at %d x %d in window %d serial %d",
WAYLAND_wl_proxy_get_id((struct wl_proxy *)data_device->drag_offer->offer),
wl_fixed_to_int(x), wl_fixed_to_int(y),
SDL_GetWindowID(data_device->dnd_window), data_device->drag_serial);
} else {
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_motion on data_offer 0x%08x at %d x %d serial %d",
-1, wl_fixed_to_int(x), wl_fixed_to_int(y), -1);
}
}
static void data_device_handle_drop(void *data, struct wl_data_device *wl_data_device)
{
SDL_WaylandDataDevice *data_device = data;
if (data_device->drag_offer && data_device->dnd_window && (data_device->has_mime_file || data_device->has_mime_text)) {
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_drop on data_offer 0x%08x in window %d serial %d",
WAYLAND_wl_proxy_get_id((struct wl_proxy *)data_device->drag_offer->offer),
SDL_GetWindowID(data_device->dnd_window), data_device->drag_serial);
// TODO: SDL Support more mime types
size_t length;
bool drop_handled = false;
#ifdef SDL_USE_LIBDBUS
if (Wayland_data_offer_has_mime(data_device->drag_offer, FILE_PORTAL_MIME)) {
void *buffer = Wayland_data_offer_receive(data_device->drag_offer,
FILE_PORTAL_MIME, &length);
if (buffer) {
SDL_DBusContext *dbus = SDL_DBus_GetContext();
if (dbus) {
int path_count = 0;
char **paths = SDL_DBus_DocumentsPortalRetrieveFiles(buffer, &path_count);
// If dropped files contain a directory the list is empty
if (paths && path_count > 0) {
int i;
for (i = 0; i < path_count; i++) {
SDL_SendDropFile(data_device->dnd_window, NULL, paths[i]);
}
dbus->free_string_array(paths);
SDL_SendDropComplete(data_device->dnd_window);
drop_handled = true;
}
}
SDL_free(buffer);
}
}
#endif
/* If XDG document portal fails fallback.
* When running a flatpak sandbox this will most likely be a list of
* non paths that are not visible to the application
*/
if (!drop_handled) {
const char *mime_type = data_device->has_mime_file ? FILE_MIME : (data_device->has_mime_text ? TEXT_MIME : "");
void *buffer = Wayland_data_offer_receive(data_device->drag_offer,
mime_type, &length);
if (data_device->has_mime_file) {
if (buffer) {
char *saveptr = NULL;
char *token = SDL_strtok_r((char *)buffer, "\r\n", &saveptr);
while (token) {
if (SDL_URIToLocal(token, token) >= 0) {
SDL_SendDropFile(data_device->dnd_window, NULL, token);
}
token = SDL_strtok_r(NULL, "\r\n", &saveptr);
}
SDL_free(buffer);
SDL_SendDropComplete(data_device->dnd_window);
} else {
SDL_SendDropComplete(data_device->dnd_window);
}
drop_handled = true;
} else if (data_device->has_mime_text) {
if (buffer) {
char *saveptr = NULL;
char *token = SDL_strtok_r((char *)buffer, "\r\n", &saveptr);
while (token) {
SDL_SendDropText(data_device->dnd_window, token);
token = SDL_strtok_r(NULL, "\r\n", &saveptr);
}
SDL_free(buffer);
SDL_SendDropComplete(data_device->dnd_window);
} else {
/* Even though there has been a valid data offer,
* and there have been valid Enter, Motion, and Drop callbacks,
* Wayland_data_offer_receive may return an empty buffer,
* because the data is actually in the primary selection device,
* not in the data device.
*/
SDL_SendDropComplete(data_device->dnd_window);
}
drop_handled = true;
}
}
if (drop_handled && wl_data_offer_get_version(data_device->drag_offer->offer) >= WL_DATA_OFFER_FINISH_SINCE_VERSION) {
wl_data_offer_finish(data_device->drag_offer->offer);
}
} else {
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In wl_data_device_listener . data_device_handle_drop on data_offer 0x%08x serial %d",
-1, -1);
}
Wayland_data_offer_destroy(data_device->drag_offer);
data_device->drag_offer = NULL;
}
static void notifyFromMimes(struct wl_list *mimes)
{
int nformats = 0;
char **new_mime_types = NULL;
if (mimes) {
nformats = WAYLAND_wl_list_length(mimes);
size_t alloc_size = (nformats + 1) * sizeof(char *);
/* do a first pass to compute allocation size */
SDL_MimeDataList *item = NULL;
wl_list_for_each(item, mimes, link) {
alloc_size += SDL_strlen(item->mime_type) + 1;
}
new_mime_types = SDL_AllocateTemporaryMemory(alloc_size);
if (!new_mime_types) {
SDL_LogError(SDL_LOG_CATEGORY_INPUT, "unable to allocate new_mime_types");
return;
}
/* second pass to fill*/
char *strPtr = (char *)(new_mime_types + nformats + 1);
item = NULL;
int i = 0;
wl_list_for_each(item, mimes, link) {
new_mime_types[i] = strPtr;
strPtr = stpcpy(strPtr, item->mime_type) + 1;
i++;
}
new_mime_types[nformats] = NULL;
}
SDL_SendClipboardUpdate(false, new_mime_types, nformats);
}
static void data_device_handle_selection(void *data, struct wl_data_device *wl_data_device,
struct wl_data_offer *id)
{
SDL_WaylandDataDevice *data_device = data;
SDL_WaylandDataOffer *offer = NULL;
if (id) {
offer = wl_data_offer_get_user_data(id);
}
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In data_device_listener . data_device_handle_selection on data_offer 0x%08x",
(id ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)id) : -1));
if (data_device->selection_offer != offer) {
Wayland_data_offer_destroy(data_device->selection_offer);
data_device->selection_offer = offer;
}
notifyFromMimes(offer ? &offer->mimes : NULL);
}
static const struct wl_data_device_listener data_device_listener = {
data_device_handle_data_offer,
data_device_handle_enter,
data_device_handle_leave,
data_device_handle_motion,
data_device_handle_drop,
data_device_handle_selection
};
static void primary_selection_device_handle_offer(void *data, struct zwp_primary_selection_device_v1 *zwp_primary_selection_device_v1,
struct zwp_primary_selection_offer_v1 *id)
{
SDL_WaylandPrimarySelectionOffer *primary_selection_offer = SDL_calloc(1, sizeof(*primary_selection_offer));
if (primary_selection_offer) {
primary_selection_offer->offer = id;
primary_selection_offer->primary_selection_device = data;
WAYLAND_wl_list_init(&(primary_selection_offer->mimes));
zwp_primary_selection_offer_v1_set_user_data(id, primary_selection_offer);
zwp_primary_selection_offer_v1_add_listener(id, &primary_selection_offer_listener, primary_selection_offer);
}
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In zwp_primary_selection_device_v1_listener . primary_selection_device_handle_offer on primary_selection_offer 0x%08x",
(id ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)id) : -1));
}
static void primary_selection_device_handle_selection(void *data, struct zwp_primary_selection_device_v1 *zwp_primary_selection_device_v1,
struct zwp_primary_selection_offer_v1 *id)
{
SDL_WaylandPrimarySelectionDevice *primary_selection_device = data;
SDL_WaylandPrimarySelectionOffer *offer = NULL;
if (id) {
offer = zwp_primary_selection_offer_v1_get_user_data(id);
}
if (primary_selection_device->selection_offer != offer) {
Wayland_primary_selection_offer_destroy(primary_selection_device->selection_offer);
primary_selection_device->selection_offer = offer;
}
SDL_LogTrace(SDL_LOG_CATEGORY_INPUT,
". In zwp_primary_selection_device_v1_listener . primary_selection_device_handle_selection on primary_selection_offer 0x%08x",
(id ? WAYLAND_wl_proxy_get_id((struct wl_proxy *)id) : -1));
}
static const struct zwp_primary_selection_device_v1_listener primary_selection_device_listener = {
primary_selection_device_handle_offer,
primary_selection_device_handle_selection
};
static void text_input_enter(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
struct wl_surface *surface)
{
// No-op
}
static void text_input_leave(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
struct wl_surface *surface)
{
// No-op
}
static void text_input_preedit_string(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
const char *text,
int32_t cursor_begin,
int32_t cursor_end)
{
SDL_WaylandTextInput *text_input = data;
text_input->has_preedit = true;
if (text) {
int cursor_begin_utf8 = cursor_begin >= 0 ? (int)SDL_utf8strnlen(text, cursor_begin) : -1;
int cursor_end_utf8 = cursor_end >= 0 ? (int)SDL_utf8strnlen(text, cursor_end) : -1;
int cursor_size_utf8;
if (cursor_end_utf8 >= 0) {
if (cursor_begin_utf8 >= 0) {
cursor_size_utf8 = cursor_end_utf8 - cursor_begin_utf8;
} else {
cursor_size_utf8 = cursor_end_utf8;
}
} else {
cursor_size_utf8 = -1;
}
SDL_SendEditingText(text, cursor_begin_utf8, cursor_size_utf8);
} else {
SDL_SendEditingText("", 0, 0);
}
}
static void text_input_commit_string(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
const char *text)
{
SDL_SendKeyboardText(text);
}
static void text_input_delete_surrounding_text(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
uint32_t before_length,
uint32_t after_length)
{
// FIXME: Do we care about this event?
}
static void text_input_done(void *data,
struct zwp_text_input_v3 *zwp_text_input_v3,
uint32_t serial)
{
SDL_WaylandTextInput *text_input = data;
if (!text_input->has_preedit) {
SDL_SendEditingText("", 0, 0);
}
text_input->has_preedit = false;
}
static const struct zwp_text_input_v3_listener text_input_listener = {
text_input_enter,
text_input_leave,
text_input_preedit_string,
text_input_commit_string,
text_input_delete_surrounding_text,
text_input_done
};
void Wayland_create_data_device(SDL_VideoData *d)
{
SDL_WaylandDataDevice *data_device = NULL;
if (!d->input->seat) {
// No seat yet, will be initialized later.
return;
}
data_device = SDL_calloc(1, sizeof(*data_device));
if (!data_device) {
return;
}
data_device->data_device = wl_data_device_manager_get_data_device(
d->data_device_manager, d->input->seat);
data_device->video_data = d;
if (!data_device->data_device) {
SDL_free(data_device);
} else {
wl_data_device_set_user_data(data_device->data_device, data_device);
wl_data_device_add_listener(data_device->data_device,
&data_device_listener, data_device);
d->input->data_device = data_device;
}
}
void Wayland_create_primary_selection_device(SDL_VideoData *d)
{
SDL_WaylandPrimarySelectionDevice *primary_selection_device = NULL;
if (!d->input->seat) {
// No seat yet, will be initialized later.
return;
}
primary_selection_device = SDL_calloc(1, sizeof(*primary_selection_device));
if (!primary_selection_device) {
return;
}
primary_selection_device->primary_selection_device = zwp_primary_selection_device_manager_v1_get_device(
d->primary_selection_device_manager, d->input->seat);
primary_selection_device->video_data = d;
if (!primary_selection_device->primary_selection_device) {
SDL_free(primary_selection_device);
} else {
zwp_primary_selection_device_v1_set_user_data(primary_selection_device->primary_selection_device,
primary_selection_device);
zwp_primary_selection_device_v1_add_listener(primary_selection_device->primary_selection_device,
&primary_selection_device_listener, primary_selection_device);
d->input->primary_selection_device = primary_selection_device;
}
}
static void Wayland_create_text_input(SDL_VideoData *d)
{
SDL_WaylandTextInput *text_input = NULL;
if (!d->input->seat) {
// No seat yet, will be initialized later.
return;
}
text_input = SDL_calloc(1, sizeof(*text_input));
if (!text_input) {
return;
}
text_input->text_input = zwp_text_input_manager_v3_get_text_input(
d->text_input_manager, d->input->seat);
if (!text_input->text_input) {
SDL_free(text_input);
} else {
zwp_text_input_v3_set_user_data(text_input->text_input, text_input);
zwp_text_input_v3_add_listener(text_input->text_input,
&text_input_listener, text_input);
d->input->text_input = text_input;
}
}
void Wayland_create_text_input_manager(SDL_VideoData *d, uint32_t id)
{
#ifdef HAVE_FCITX
const char *im_module = SDL_getenv("SDL_IM_MODULE");
if (im_module && SDL_strcmp(im_module, "fcitx") == 0) {
/* Override the Wayland text-input protocol when Fcitx is enabled, like how GTK_IM_MODULE does.
*
* The Fcitx wiki discourages enabling it under Wayland via SDL_IM_MODULE, so its presence must
* be intentional, and this workaround is needed for fixing key repeat detection.
*/
return;
}
#endif
d->text_input_manager = wl_registry_bind(d->registry, id, &zwp_text_input_manager_v3_interface, 1);
Wayland_create_text_input(d);
}
// Pen/Tablet support...
typedef struct SDL_WaylandPenTool // a stylus, etc, on a tablet.
{
SDL_PenID instance_id;
SDL_PenInfo info;
SDL_Window *tool_focus;
struct zwp_tablet_tool_v2 *wltool;
float x;
float y;
bool frame_motion_set;
float frame_axes[SDL_PEN_AXIS_COUNT];
Uint32 frame_axes_set;
int frame_pen_down;
int frame_buttons[3];
} SDL_WaylandPenTool;
static void tablet_tool_handle_type(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t type)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
switch (type) {
#define CASE(typ) case ZWP_TABLET_TOOL_V2_TYPE_##typ: sdltool->info.subtype = SDL_PEN_TYPE_##typ; return
CASE(ERASER);
CASE(PEN);
CASE(PENCIL);
CASE(AIRBRUSH);
CASE(BRUSH);
#undef CASE
default: sdltool->info.subtype = SDL_PEN_TYPE_UNKNOWN; // we'll decline to add this when the `done` event comes through.
}
}
static void tablet_tool_handle_hardware_serial(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t serial_hi, uint32_t serial_lo)
{
// don't care about this atm.
}
static void tablet_tool_handle_hardware_id_wacom(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t id_hi, uint32_t id_lo)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
sdltool->info.wacom_id = id_lo;
}
static void tablet_tool_handle_capability(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t capability)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
switch (capability) {
#define CASE(wltyp,sdltyp) case ZWP_TABLET_TOOL_V2_CAPABILITY_##wltyp: sdltool->info.capabilities |= sdltyp; return
CASE(TILT, SDL_PEN_CAPABILITY_XTILT | SDL_PEN_CAPABILITY_YTILT);
CASE(PRESSURE, SDL_PEN_CAPABILITY_PRESSURE);
CASE(DISTANCE, SDL_PEN_CAPABILITY_DISTANCE);
CASE(ROTATION, SDL_PEN_CAPABILITY_ROTATION);
CASE(SLIDER, SDL_PEN_CAPABILITY_SLIDER);
#undef CASE
default: break; // unsupported here.
}
}
static void tablet_tool_handle_done(void *data, struct zwp_tablet_tool_v2 *tool)
{
}
static void tablet_tool_handle_removed(void *data, struct zwp_tablet_tool_v2 *tool)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
if (sdltool->instance_id) {
SDL_RemovePenDevice(0, sdltool->instance_id);
}
zwp_tablet_tool_v2_destroy(tool);
SDL_free(sdltool);
}
static void tablet_tool_handle_proximity_in(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t serial, struct zwp_tablet_v2 *tablet, struct wl_surface *surface)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
SDL_WindowData *windowdata = surface ? Wayland_GetWindowDataForOwnedSurface(surface) : NULL;
sdltool->tool_focus = windowdata ? windowdata->sdlwindow : NULL;
SDL_assert(sdltool->instance_id == 0); // shouldn't be added at this point.
if (sdltool->info.subtype != SDL_PEN_TYPE_UNKNOWN) { // don't tell SDL about it if we don't know its role.
sdltool->instance_id = SDL_AddPenDevice(0, NULL, &sdltool->info, sdltool);
}
// According to the docs, this should be followed by a motion event, where we'll send our SDL events.
}
static void tablet_tool_handle_proximity_out(void *data, struct zwp_tablet_tool_v2 *tool)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
sdltool->tool_focus = NULL;
if (sdltool->instance_id) {
SDL_RemovePenDevice(0, sdltool->instance_id);
sdltool->instance_id = 0;
}
}
static void tablet_tool_handle_down(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t serial)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
sdltool->frame_pen_down = 1;
}
static void tablet_tool_handle_up(void *data, struct zwp_tablet_tool_v2 *tool)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
sdltool->frame_pen_down = 0;
}
static void tablet_tool_handle_motion(void *data, struct zwp_tablet_tool_v2 *tool, wl_fixed_t sx_w, wl_fixed_t sy_w)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
SDL_Window *window = sdltool->tool_focus;
if (window) {
const SDL_WindowData *windowdata = window->internal;
sdltool->x = (float)(wl_fixed_to_double(sx_w) * windowdata->pointer_scale.x);
sdltool->y = (float)(wl_fixed_to_double(sy_w) * windowdata->pointer_scale.y);
sdltool->frame_motion_set = true;
}
}
static void tablet_tool_handle_pressure(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t pressure)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
sdltool->frame_axes[SDL_PEN_AXIS_PRESSURE] = ((float) pressure) / 65535.0f;
sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_PRESSURE);
if (pressure) {
sdltool->frame_axes[SDL_PEN_AXIS_DISTANCE] = 0.0f;
sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_DISTANCE);
}
}
static void tablet_tool_handle_distance(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t distance)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
sdltool->frame_axes[SDL_PEN_AXIS_DISTANCE] = ((float) distance) / 65535.0f;
sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_DISTANCE);
if (distance) {
sdltool->frame_axes[SDL_PEN_AXIS_PRESSURE] = 0.0f;
sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_PRESSURE);
}
}
static void tablet_tool_handle_tilt(void *data, struct zwp_tablet_tool_v2 *tool, wl_fixed_t xtilt, wl_fixed_t ytilt)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
sdltool->frame_axes[SDL_PEN_AXIS_XTILT] = (float)(wl_fixed_to_double(xtilt));
sdltool->frame_axes[SDL_PEN_AXIS_YTILT] = (float)(wl_fixed_to_double(ytilt));
sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_XTILT) | (1u << SDL_PEN_AXIS_YTILT);
}
static void tablet_tool_handle_button(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t serial, uint32_t button, uint32_t state)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
int sdlbutton;
switch (button) {
// see %{_includedir}/linux/input-event-codes.h
case 0x14b: // BTN_STYLUS
sdlbutton = 1;
break;
case 0x14c: // BTN_STYLUS2
sdlbutton = 2;
break;
case 0x149: // BTN_STYLUS3
sdlbutton = 3;
break;
default:
return; // don't care about this button, I guess.
}
SDL_assert((sdlbutton >= 1) && (sdlbutton <= SDL_arraysize(sdltool->frame_buttons)));
sdltool->frame_buttons[sdlbutton-1] = (state == ZWP_TABLET_PAD_V2_BUTTON_STATE_PRESSED) ? 1 : 0;
}
static void tablet_tool_handle_rotation(void *data, struct zwp_tablet_tool_v2 *tool, wl_fixed_t degrees)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
const float rotation = (float)(wl_fixed_to_double(degrees));
sdltool->frame_axes[SDL_PEN_AXIS_ROTATION] = (rotation > 180.0f) ? (rotation - 360.0f) : rotation; // map to -180.0f ... 179.0f range
sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_ROTATION);
}
static void tablet_tool_handle_slider(void *data, struct zwp_tablet_tool_v2 *tool, int32_t position)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
sdltool->frame_axes[SDL_PEN_AXIS_SLIDER] = position / 65535.f;
sdltool->frame_axes_set |= (1u << SDL_PEN_AXIS_SLIDER);
}
static void tablet_tool_handle_wheel(void *data, struct zwp_tablet_tool_v2 *tool, int32_t degrees, int32_t clicks)
{
// not supported at the moment
}
static void tablet_tool_handle_frame(void *data, struct zwp_tablet_tool_v2 *tool, uint32_t time)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) data;
if (!sdltool->instance_id) {
return; // Not a pen we report on.
}
const Uint64 timestamp = Wayland_GetEventTimestamp(SDL_MS_TO_NS(time));
const SDL_PenID instance_id = sdltool->instance_id;
SDL_Window *window = sdltool->tool_focus;
// I don't know if this is necessary (or makes sense), but send motion before pen downs, but after pen ups, so you don't get unexpected lines drawn.
if (sdltool->frame_motion_set && (sdltool->frame_pen_down != -1)) {
if (sdltool->frame_pen_down) {
SDL_SendPenMotion(timestamp, instance_id, window, sdltool->x, sdltool->y);
SDL_SendPenTouch(timestamp, instance_id, window, false, true); // !!! FIXME: how do we know what tip is in use?
} else {
SDL_SendPenTouch(timestamp, instance_id, window, false, false); // !!! FIXME: how do we know what tip is in use?
SDL_SendPenMotion(timestamp, instance_id, window, sdltool->x, sdltool->y);
}
} else {
if (sdltool->frame_pen_down != -1) {
SDL_SendPenTouch(timestamp, instance_id, window, false, (sdltool->frame_pen_down != 0)); // !!! FIXME: how do we know what tip is in use?
}
if (sdltool->frame_motion_set) {
SDL_SendPenMotion(timestamp, instance_id, window, sdltool->x, sdltool->y);
}
}
for (SDL_PenAxis i = 0; i < SDL_PEN_AXIS_COUNT; i++) {
if (sdltool->frame_axes_set & (1u << i)) {
SDL_SendPenAxis(timestamp, instance_id, window, i, sdltool->frame_axes[i]);
}
}
for (int i = 0; i < SDL_arraysize(sdltool->frame_buttons); i++) {
const int state = sdltool->frame_buttons[i];
if (state != -1) {
SDL_SendPenButton(timestamp, instance_id, window, (Uint8)(i + 1), (state != 0));
sdltool->frame_buttons[i] = -1;
}
}
// reset for next frame.
sdltool->frame_pen_down = -1;
sdltool->frame_motion_set = false;
sdltool->frame_axes_set = 0;
}
static const struct zwp_tablet_tool_v2_listener tablet_tool_listener = {
tablet_tool_handle_type,
tablet_tool_handle_hardware_serial,
tablet_tool_handle_hardware_id_wacom,
tablet_tool_handle_capability,
tablet_tool_handle_done,
tablet_tool_handle_removed,
tablet_tool_handle_proximity_in,
tablet_tool_handle_proximity_out,
tablet_tool_handle_down,
tablet_tool_handle_up,
tablet_tool_handle_motion,
tablet_tool_handle_pressure,
tablet_tool_handle_distance,
tablet_tool_handle_tilt,
tablet_tool_handle_rotation,
tablet_tool_handle_slider,
tablet_tool_handle_wheel,
tablet_tool_handle_button,
tablet_tool_handle_frame
};
static void tablet_seat_handle_tablet_added(void *data, struct zwp_tablet_seat_v2 *seat, struct zwp_tablet_v2 *tablet)
{
// don't care atm.
}
static void tablet_seat_handle_tool_added(void *data, struct zwp_tablet_seat_v2 *seat, struct zwp_tablet_tool_v2 *tool)
{
SDL_WaylandPenTool *sdltool = SDL_calloc(1, sizeof(*sdltool));
if (sdltool) { // if allocation failed, oh well, we won't report this device.
sdltool->wltool = tool;
sdltool->info.max_tilt = -1.0f;
sdltool->info.num_buttons = -1;
sdltool->frame_pen_down = -1;
for (int i = 0; i < SDL_arraysize(sdltool->frame_buttons); i++) {
sdltool->frame_buttons[i] = -1;
}
// this will send a bunch of zwp_tablet_tool_v2 events right up front to tell
// us device details, with a "done" event to let us know we have everything.
zwp_tablet_tool_v2_add_listener(tool, &tablet_tool_listener, sdltool);
}
}
static void tablet_seat_handle_pad_added(void *data, struct zwp_tablet_seat_v2 *seat, struct zwp_tablet_pad_v2 *pad)
{
// we don't care atm.
}
static const struct zwp_tablet_seat_v2_listener tablet_seat_listener = {
tablet_seat_handle_tablet_added,
tablet_seat_handle_tool_added,
tablet_seat_handle_pad_added
};
void Wayland_input_init_tablet_support(struct SDL_WaylandInput *input, struct zwp_tablet_manager_v2 *tablet_manager)
{
if (!tablet_manager || !input->seat) {
return;
}
SDL_WaylandTabletInput *tablet_input = SDL_calloc(1, sizeof(*tablet_input));
if (!tablet_input) {
return;
}
tablet_input->input = input;
tablet_input->seat = zwp_tablet_manager_v2_get_tablet_seat(tablet_manager, input->seat);
zwp_tablet_seat_v2_add_listener(tablet_input->seat, &tablet_seat_listener, tablet_input);
input->tablet_input = tablet_input;
}
static void Wayland_remove_all_pens_callback(SDL_PenID instance_id, void *handle, void *userdata)
{
SDL_WaylandPenTool *sdltool = (SDL_WaylandPenTool *) handle;
zwp_tablet_tool_v2_destroy(sdltool->wltool);
SDL_free(sdltool);
}
void Wayland_input_quit_tablet_support(struct SDL_WaylandInput *input)
{
SDL_RemoveAllPenDevices(Wayland_remove_all_pens_callback, NULL);
if (input && input->tablet_input) {
zwp_tablet_seat_v2_destroy(input->tablet_input->seat);
SDL_free(input->tablet_input);
input->tablet_input = NULL;
}
}
void Wayland_input_initialize_seat(SDL_VideoData *d)
{
struct SDL_WaylandInput *input = d->input;
WAYLAND_wl_list_init(&touch_points);
if (d->data_device_manager) {
Wayland_create_data_device(d);
}
if (d->primary_selection_device_manager) {
Wayland_create_primary_selection_device(d);
}
if (d->text_input_manager) {
Wayland_create_text_input(d);
}
wl_seat_add_listener(input->seat, &seat_listener, input);
wl_seat_set_user_data(input->seat, input);
if (d->tablet_manager) {
Wayland_input_init_tablet_support(d->input, d->tablet_manager);
}
WAYLAND_wl_display_flush(d->display);
}
void Wayland_display_destroy_input(SDL_VideoData *d)
{
struct SDL_WaylandInput *input = d->input;
if (input->keyboard_timestamps) {
zwp_input_timestamps_v1_destroy(input->keyboard_timestamps);
}
if (input->pointer_timestamps) {
zwp_input_timestamps_v1_destroy(input->pointer_timestamps);
}
if (input->touch_timestamps) {
zwp_input_timestamps_v1_destroy(input->touch_timestamps);
}
if (input->data_device) {
Wayland_data_device_clear_selection(input->data_device);
if (input->data_device->selection_offer) {
Wayland_data_offer_destroy(input->data_device->selection_offer);
}
if (input->data_device->drag_offer) {
Wayland_data_offer_destroy(input->data_device->drag_offer);
}
if (input->data_device->data_device) {
if (wl_data_device_get_version(input->data_device->data_device) >= WL_DATA_DEVICE_RELEASE_SINCE_VERSION) {
wl_data_device_release(input->data_device->data_device);
} else {
wl_data_device_destroy(input->data_device->data_device);
}
}
SDL_free(input->data_device);
}
if (input->primary_selection_device) {
if (input->primary_selection_device->selection_offer) {
Wayland_primary_selection_offer_destroy(input->primary_selection_device->selection_offer);
}
if (input->primary_selection_device->selection_source) {
Wayland_primary_selection_source_destroy(input->primary_selection_device->selection_source);
}
if (input->primary_selection_device->primary_selection_device) {
zwp_primary_selection_device_v1_destroy(input->primary_selection_device->primary_selection_device);
}
SDL_free(input->primary_selection_device);
}
if (input->text_input) {
zwp_text_input_v3_destroy(input->text_input->text_input);
SDL_free(input->text_input);
}
if (input->keyboard) {
if (wl_keyboard_get_version(input->keyboard) >= WL_KEYBOARD_RELEASE_SINCE_VERSION) {
wl_keyboard_release(input->keyboard);
} else {
wl_keyboard_destroy(input->keyboard);
}
}
if (input->relative_pointer) {
zwp_relative_pointer_v1_destroy(input->relative_pointer);
}
if (input->cursor_shape) {
wp_cursor_shape_device_v1_destroy(input->cursor_shape);
}
if (input->pointer) {
if (wl_pointer_get_version(input->pointer) >= WL_POINTER_RELEASE_SINCE_VERSION) {
wl_pointer_release(input->pointer);
} else {
wl_pointer_destroy(input->pointer);
}
}
if (input->touch) {
struct SDL_WaylandTouchPoint *tp, *tmp;
SDL_DelTouch(1);
if (wl_touch_get_version(input->touch) >= WL_TOUCH_RELEASE_SINCE_VERSION) {
wl_touch_release(input->touch);
} else {
wl_touch_destroy(input->touch);
}
wl_list_for_each_safe (tp, tmp, &touch_points, link) {
WAYLAND_wl_list_remove(&tp->link);
SDL_free(tp);
}
}
if (input->tablet_input) {
Wayland_input_quit_tablet_support(input);
}
if (input->seat) {
if (wl_seat_get_version(input->seat) >= WL_SEAT_RELEASE_SINCE_VERSION) {
wl_seat_release(input->seat);
} else {
wl_seat_destroy(input->seat);
}
}
if (input->xkb.compose_state) {
WAYLAND_xkb_compose_state_unref(input->xkb.compose_state);
}
if (input->xkb.compose_table) {
WAYLAND_xkb_compose_table_unref(input->xkb.compose_table);
}
if (input->xkb.state) {
WAYLAND_xkb_state_unref(input->xkb.state);
}
if (input->xkb.keymap) {
WAYLAND_xkb_keymap_unref(input->xkb.keymap);
}
SDL_free(input);
d->input = NULL;
}
bool Wayland_input_enable_relative_pointer(struct SDL_WaylandInput *input)
{
SDL_VideoDevice *vd = SDL_GetVideoDevice();
SDL_VideoData *d = input->display;
SDL_Window *window;
if (!d->relative_pointer_manager) {
return false;
}
if (!d->pointer_constraints) {
return false;
}
if (!input->pointer) {
return false;
}
/* If we have a pointer confine active, we must destroy it here because
* creating a locked pointer otherwise would be a protocol error.
*/
for (window = vd->windows; window; window = window->next) {
pointer_confine_destroy(window);
}
for (window = vd->windows; window; window = window->next) {
Wayland_input_lock_pointer(input, window);
}
d->relative_mouse_mode = 1;
return true;
}
bool Wayland_input_disable_relative_pointer(struct SDL_WaylandInput *input)
{
SDL_VideoDevice *vd = SDL_GetVideoDevice();
SDL_VideoData *d = input->display;
SDL_Window *window;
for (window = vd->windows; window; window = window->next) {
Wayland_input_unlock_pointer(input, window);
}
d->relative_mouse_mode = 0;
for (window = vd->windows; window; window = window->next) {
Wayland_input_confine_pointer(input, window);
}
return true;
}
bool Wayland_input_confine_pointer(struct SDL_WaylandInput *input, SDL_Window *window)
{
SDL_WindowData *w = window->internal;
SDL_VideoData *d = input->display;
struct wl_region *confine_rect;
if (!d->pointer_constraints) {
return SDL_SetError("Failed to confine pointer: compositor lacks support for the required zwp_pointer_constraints_v1 protocol");
}
if (!input->pointer) {
return SDL_SetError("No pointer to confine");
}
/* A confine may already be active, in which case we should destroy it and
* create a new one.
*/
pointer_confine_destroy(window);
/* We cannot create a confine if the pointer is already locked. Defer until
* the pointer is unlocked.
*/
if (d->relative_mouse_mode) {
return true;
}
// Don't confine the pointer if it shouldn't be confined.
if (SDL_RectEmpty(&window->mouse_rect) && !(window->flags & SDL_WINDOW_MOUSE_GRABBED)) {
return true;
}
if (SDL_RectEmpty(&window->mouse_rect)) {
confine_rect = NULL;
} else {
SDL_Rect scaled_mouse_rect;
scaled_mouse_rect.x = (int)SDL_floor(window->mouse_rect.x / w->pointer_scale.x);
scaled_mouse_rect.y = (int)SDL_floor(window->mouse_rect.y / w->pointer_scale.y);
scaled_mouse_rect.w = (int)SDL_ceil(window->mouse_rect.w / w->pointer_scale.x);
scaled_mouse_rect.h = (int)SDL_ceil(window->mouse_rect.h / w->pointer_scale.y);
confine_rect = wl_compositor_create_region(d->compositor);
wl_region_add(confine_rect,
scaled_mouse_rect.x,
scaled_mouse_rect.y,
scaled_mouse_rect.w,
scaled_mouse_rect.h);
}
struct zwp_confined_pointer_v1 *confined_pointer =
zwp_pointer_constraints_v1_confine_pointer(d->pointer_constraints,
w->surface,
input->pointer,
confine_rect,
ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
zwp_confined_pointer_v1_add_listener(confined_pointer,
&confined_pointer_listener,
window);
if (confine_rect) {
wl_region_destroy(confine_rect);
}
w->confined_pointer = confined_pointer;
return true;
}
bool Wayland_input_unconfine_pointer(struct SDL_WaylandInput *input, SDL_Window *window)
{
pointer_confine_destroy(window);
return true;
}
bool Wayland_input_grab_keyboard(SDL_Window *window, struct SDL_WaylandInput *input)
{
SDL_WindowData *w = window->internal;
SDL_VideoData *d = input->display;
if (!d->key_inhibitor_manager) {
return SDL_SetError("Failed to grab keyboard: compositor lacks support for the required zwp_keyboard_shortcuts_inhibit_manager_v1 protocol");
}
if (w->key_inhibitor) {
return true;
}
w->key_inhibitor =
zwp_keyboard_shortcuts_inhibit_manager_v1_inhibit_shortcuts(d->key_inhibitor_manager,
w->surface,
input->seat);
return true;
}
bool Wayland_input_ungrab_keyboard(SDL_Window *window)
{
SDL_WindowData *w = window->internal;
if (w->key_inhibitor) {
zwp_keyboard_shortcuts_inhibitor_v1_destroy(w->key_inhibitor);
w->key_inhibitor = NULL;
}
return true;
}
void Wayland_UpdateImplicitGrabSerial(struct SDL_WaylandInput *input, Uint32 serial)
{
if (serial > input->last_implicit_grab_serial) {
input->last_implicit_grab_serial = serial;
Wayland_data_device_set_serial(input->data_device, serial);
Wayland_primary_selection_device_set_serial(input->primary_selection_device, serial);
}
}
#endif // SDL_VIDEO_DRIVER_WAYLAND
|