summaryrefslogtreecommitdiffstats
path: root/widget/gtk/nsGtkKeyUtils.cpp
blob: 7157a09664186776ca32e61f0385ec3766ca3367 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:expandtab:shiftwidth=4:tabstop=4:
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/Logging.h"

#include "nsGtkKeyUtils.h"

#include <gdk/gdkkeysyms.h>
#include <algorithm>
#include <gdk/gdk.h>
#include <dlfcn.h>
#include <gdk/gdkkeysyms-compat.h>
#ifdef MOZ_X11
#  include <gdk/gdkx.h>
#  include <X11/XKBlib.h>
#  include "X11UndefineNone.h"
#endif
#include "IMContextWrapper.h"
#include "WidgetUtils.h"
#include "WidgetUtilsGtk.h"
#include "x11/keysym2ucs.h"
#include "nsContentUtils.h"
#include "nsGtkUtils.h"
#include "nsIBidiKeyboard.h"
#include "nsPrintfCString.h"
#include "nsReadableUtils.h"
#include "nsServiceManagerUtils.h"
#include "nsWindow.h"

#include "mozilla/ArrayUtils.h"
#include "mozilla/MouseEvents.h"
#include "mozilla/TextEventDispatcher.h"
#include "mozilla/TextEvents.h"

#ifdef MOZ_WAYLAND
#  include <sys/mman.h>
#  include "nsWaylandDisplay.h"
#endif

// For collecting other people's log, tell them `MOZ_LOG=KeyboardHandler:4,sync`
// rather than `MOZ_LOG=KeyboardHandler:5,sync` since using `5` may create too
// big file.
// Therefore you shouldn't use `LogLevel::Verbose` for logging usual behavior.
mozilla::LazyLogModule gKeyLog("KeyboardHandler");

namespace mozilla {
namespace widget {

#define IS_ASCII_ALPHABETICAL(key) \
  ((('a' <= key) && (key <= 'z')) || (('A' <= key) && (key <= 'Z')))

#define MOZ_MODIFIER_KEYS "MozKeymapWrapper"

KeymapWrapper* KeymapWrapper::sInstance = nullptr;
guint KeymapWrapper::sLastRepeatableHardwareKeyCode = 0;
#ifdef MOZ_X11
Time KeymapWrapper::sLastRepeatableKeyTime = 0;
#endif
KeymapWrapper::RepeatState KeymapWrapper::sRepeatState =
    KeymapWrapper::NOT_PRESSED;

#ifdef MOZ_WAYLAND
wl_seat* KeymapWrapper::sSeat = nullptr;
int KeymapWrapper::sSeatID = -1;
wl_keyboard* KeymapWrapper::sKeyboard = nullptr;
#endif

static const char* GetBoolName(bool aBool) { return aBool ? "TRUE" : "FALSE"; }

static const char* GetStatusName(nsEventStatus aStatus) {
  switch (aStatus) {
    case nsEventStatus_eConsumeDoDefault:
      return "nsEventStatus_eConsumeDoDefault";
    case nsEventStatus_eConsumeNoDefault:
      return "nsEventStatus_eConsumeNoDefault";
    case nsEventStatus_eIgnore:
      return "nsEventStatus_eIgnore";
    case nsEventStatus_eSentinel:
      return "nsEventStatus_eSentinel";
    default:
      return "Illegal value";
  }
}

static const nsCString GetKeyLocationName(uint32_t aLocation) {
  switch (aLocation) {
    case eKeyLocationLeft:
      return "KEY_LOCATION_LEFT"_ns;
    case eKeyLocationRight:
      return "KEY_LOCATION_RIGHT"_ns;
    case eKeyLocationStandard:
      return "KEY_LOCATION_STANDARD"_ns;
    case eKeyLocationNumpad:
      return "KEY_LOCATION_NUMPAD"_ns;
    default:
      return nsPrintfCString("Unknown (0x%04X)", aLocation);
  }
}

static const nsCString GetCharacterCodeName(char16_t aChar) {
  switch (aChar) {
    case 0x0000:
      return "NULL (0x0000)"_ns;
    case 0x0008:
      return "BACKSPACE (0x0008)"_ns;
    case 0x0009:
      return "CHARACTER TABULATION (0x0009)"_ns;
    case 0x000A:
      return "LINE FEED (0x000A)"_ns;
    case 0x000B:
      return "LINE TABULATION (0x000B)"_ns;
    case 0x000C:
      return "FORM FEED (0x000C)"_ns;
    case 0x000D:
      return "CARRIAGE RETURN (0x000D)"_ns;
    case 0x0018:
      return "CANCEL (0x0018)"_ns;
    case 0x001B:
      return "ESCAPE (0x001B)"_ns;
    case 0x0020:
      return "SPACE (0x0020)"_ns;
    case 0x007F:
      return "DELETE (0x007F)"_ns;
    case 0x00A0:
      return "NO-BREAK SPACE (0x00A0)"_ns;
    case 0x00AD:
      return "SOFT HYPHEN (0x00AD)"_ns;
    case 0x2000:
      return "EN QUAD (0x2000)"_ns;
    case 0x2001:
      return "EM QUAD (0x2001)"_ns;
    case 0x2002:
      return "EN SPACE (0x2002)"_ns;
    case 0x2003:
      return "EM SPACE (0x2003)"_ns;
    case 0x2004:
      return "THREE-PER-EM SPACE (0x2004)"_ns;
    case 0x2005:
      return "FOUR-PER-EM SPACE (0x2005)"_ns;
    case 0x2006:
      return "SIX-PER-EM SPACE (0x2006)"_ns;
    case 0x2007:
      return "FIGURE SPACE (0x2007)"_ns;
    case 0x2008:
      return "PUNCTUATION SPACE (0x2008)"_ns;
    case 0x2009:
      return "THIN SPACE (0x2009)"_ns;
    case 0x200A:
      return "HAIR SPACE (0x200A)"_ns;
    case 0x200B:
      return "ZERO WIDTH SPACE (0x200B)"_ns;
    case 0x200C:
      return "ZERO WIDTH NON-JOINER (0x200C)"_ns;
    case 0x200D:
      return "ZERO WIDTH JOINER (0x200D)"_ns;
    case 0x200E:
      return "LEFT-TO-RIGHT MARK (0x200E)"_ns;
    case 0x200F:
      return "RIGHT-TO-LEFT MARK (0x200F)"_ns;
    case 0x2029:
      return "PARAGRAPH SEPARATOR (0x2029)"_ns;
    case 0x202A:
      return "LEFT-TO-RIGHT EMBEDDING (0x202A)"_ns;
    case 0x202B:
      return "RIGHT-TO-LEFT EMBEDDING (0x202B)"_ns;
    case 0x202D:
      return "LEFT-TO-RIGHT OVERRIDE (0x202D)"_ns;
    case 0x202E:
      return "RIGHT-TO-LEFT OVERRIDE (0x202E)"_ns;
    case 0x202F:
      return "NARROW NO-BREAK SPACE (0x202F)"_ns;
    case 0x205F:
      return "MEDIUM MATHEMATICAL SPACE (0x205F)"_ns;
    case 0x2060:
      return "WORD JOINER (0x2060)"_ns;
    case 0x2066:
      return "LEFT-TO-RIGHT ISOLATE (0x2066)"_ns;
    case 0x2067:
      return "RIGHT-TO-LEFT ISOLATE (0x2067)"_ns;
    case 0x3000:
      return "IDEOGRAPHIC SPACE (0x3000)"_ns;
    case 0xFEFF:
      return "ZERO WIDTH NO-BREAK SPACE (0xFEFF)"_ns;
    default: {
      if (aChar < ' ' || (aChar >= 0x80 && aChar < 0xA0)) {
        return nsPrintfCString("control (0x%04X)", aChar);
      }
      if (NS_IS_HIGH_SURROGATE(aChar)) {
        return nsPrintfCString("high surrogate (0x%04X)", aChar);
      }
      if (NS_IS_LOW_SURROGATE(aChar)) {
        return nsPrintfCString("low surrogate (0x%04X)", aChar);
      }
      return nsPrintfCString("'%s' (0x%04X)",
                             NS_ConvertUTF16toUTF8(nsAutoString(aChar)).get(),
                             aChar);
    }
  }
}

static const nsCString GetCharacterCodeNames(const char16_t* aChars,
                                             uint32_t aLength) {
  if (!aLength) {
    return "\"\""_ns;
  }
  nsCString result;
  result.AssignLiteral("\"");
  StringJoinAppend(result, ", "_ns, Span{aChars, aLength},
                   [](nsACString& dest, const char16_t charValue) {
                     dest.Append(GetCharacterCodeName(charValue));
                   });
  result.AppendLiteral("\"");
  return result;
}

static const nsCString GetCharacterCodeNames(const nsAString& aString) {
  return GetCharacterCodeNames(aString.BeginReading(), aString.Length());
}

/* static */
const char* KeymapWrapper::GetModifierName(MappedModifier aModifier) {
  switch (aModifier) {
    case CAPS_LOCK:
      return "CapsLock";
    case NUM_LOCK:
      return "NumLock";
    case SCROLL_LOCK:
      return "ScrollLock";
    case SHIFT:
      return "Shift";
    case CTRL:
      return "Ctrl";
    case ALT:
      return "Alt";
    case SUPER:
      return "Super";
    case HYPER:
      return "Hyper";
    case META:
      return "Meta";
    case LEVEL3:
      return "Level3";
    case LEVEL5:
      return "Level5";
    case NOT_MODIFIER:
      return "NotModifier";
    default:
      return "InvalidValue";
  }
}

/* static */
KeymapWrapper::MappedModifier KeymapWrapper::GetModifierForGDKKeyval(
    guint aGdkKeyval) {
  switch (aGdkKeyval) {
    case GDK_Caps_Lock:
      return CAPS_LOCK;
    case GDK_Num_Lock:
      return NUM_LOCK;
    case GDK_Scroll_Lock:
      return SCROLL_LOCK;
    case GDK_Shift_Lock:
    case GDK_Shift_L:
    case GDK_Shift_R:
      return SHIFT;
    case GDK_Control_L:
    case GDK_Control_R:
      return CTRL;
    case GDK_Alt_L:
    case GDK_Alt_R:
      return ALT;
    case GDK_Super_L:
    case GDK_Super_R:
      return SUPER;
    case GDK_Hyper_L:
    case GDK_Hyper_R:
      return HYPER;
    case GDK_Meta_L:
    case GDK_Meta_R:
      return META;
    case GDK_ISO_Level3_Shift:
    case GDK_Mode_switch:
      return LEVEL3;
    case GDK_ISO_Level5_Shift:
      return LEVEL5;
    default:
      return NOT_MODIFIER;
  }
}

guint KeymapWrapper::GetGdkModifierMask(MappedModifier aModifier) const {
  switch (aModifier) {
    case CAPS_LOCK:
      return GDK_LOCK_MASK;
    case NUM_LOCK:
      return mModifierMasks[INDEX_NUM_LOCK];
    case SCROLL_LOCK:
      return mModifierMasks[INDEX_SCROLL_LOCK];
    case SHIFT:
      return GDK_SHIFT_MASK;
    case CTRL:
      return GDK_CONTROL_MASK;
    case ALT:
      return mModifierMasks[INDEX_ALT];
    case SUPER:
      return GDK_SUPER_MASK;
    case HYPER:
      return mModifierMasks[INDEX_HYPER];
    case META:
      return mModifierMasks[INDEX_META];
    case LEVEL3:
      return mModifierMasks[INDEX_LEVEL3];
    case LEVEL5:
      return mModifierMasks[INDEX_LEVEL5];
    default:
      return 0;
  }
}

KeymapWrapper::ModifierKey* KeymapWrapper::GetModifierKey(
    guint aHardwareKeycode) {
  for (uint32_t i = 0; i < mModifierKeys.Length(); i++) {
    ModifierKey& key = mModifierKeys[i];
    if (key.mHardwareKeycode == aHardwareKeycode) {
      return &key;
    }
  }
  return nullptr;
}

/* static */
KeymapWrapper* KeymapWrapper::GetInstance() {
  if (!sInstance) {
    sInstance = new KeymapWrapper();
    sInstance->Init();
  }
  return sInstance;
}

#ifdef MOZ_WAYLAND
void KeymapWrapper::EnsureInstance() { (void)GetInstance(); }

void KeymapWrapper::InitBySystemSettingsWayland() {
  MOZ_UNUSED(WaylandDisplayGet());
}
#endif

/* static */
void KeymapWrapper::Shutdown() {
  if (sInstance) {
    delete sInstance;
    sInstance = nullptr;
  }
}

KeymapWrapper::KeymapWrapper()
    : mInitialized(false),
      mGdkKeymap(gdk_keymap_get_default()),
      mXKBBaseEventCode(0),
      mOnKeysChangedSignalHandle(0),
      mOnDirectionChangedSignalHandle(0) {
  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("%p Constructor, mGdkKeymap=%p", this, mGdkKeymap));

  g_object_ref(mGdkKeymap);

#ifdef MOZ_X11
  if (GdkIsX11Display()) {
    InitXKBExtension();
  }
#endif
}

void KeymapWrapper::Init() {
  if (mInitialized) {
    return;
  }
  mInitialized = true;

  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("%p Init, mGdkKeymap=%p", this, mGdkKeymap));

  mModifierKeys.Clear();
  memset(mModifierMasks, 0, sizeof(mModifierMasks));

#ifdef MOZ_X11
  if (GdkIsX11Display()) {
    InitBySystemSettingsX11();
  }
#endif
#ifdef MOZ_WAYLAND
  if (GdkIsWaylandDisplay()) {
    InitBySystemSettingsWayland();
  }
#endif

#ifdef MOZ_X11
  gdk_window_add_filter(nullptr, FilterEvents, this);
#endif

  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("%p Init, CapsLock=0x%X, NumLock=0x%X, "
           "ScrollLock=0x%X, Level3=0x%X, Level5=0x%X, "
           "Shift=0x%X, Ctrl=0x%X, Alt=0x%X, Meta=0x%X, Super=0x%X, Hyper=0x%X",
           this, GetGdkModifierMask(CAPS_LOCK), GetGdkModifierMask(NUM_LOCK),
           GetGdkModifierMask(SCROLL_LOCK), GetGdkModifierMask(LEVEL3),
           GetGdkModifierMask(LEVEL5), GetGdkModifierMask(SHIFT),
           GetGdkModifierMask(CTRL), GetGdkModifierMask(ALT),
           GetGdkModifierMask(META), GetGdkModifierMask(SUPER),
           GetGdkModifierMask(HYPER)));
}

#ifdef MOZ_X11
void KeymapWrapper::InitXKBExtension() {
  PodZero(&mKeyboardState);

  int xkbMajorVer = XkbMajorVersion;
  int xkbMinorVer = XkbMinorVersion;
  if (!XkbLibraryVersion(&xkbMajorVer, &xkbMinorVer)) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p InitXKBExtension failed due to failure of "
             "XkbLibraryVersion()",
             this));
    return;
  }

  Display* display = gdk_x11_display_get_xdisplay(gdk_display_get_default());

  // XkbLibraryVersion() set xkbMajorVer and xkbMinorVer to that of the
  // library, which may be newer than what is required of the server in
  // XkbQueryExtension(), so these variables should be reset to
  // XkbMajorVersion and XkbMinorVersion before the XkbQueryExtension call.
  xkbMajorVer = XkbMajorVersion;
  xkbMinorVer = XkbMinorVersion;
  int opcode, baseErrorCode;
  if (!XkbQueryExtension(display, &opcode, &mXKBBaseEventCode, &baseErrorCode,
                         &xkbMajorVer, &xkbMinorVer)) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p   InitXKBExtension failed due to failure of "
             "XkbQueryExtension(), display=0x%p",
             this, display));
    return;
  }

  if (!XkbSelectEventDetails(display, XkbUseCoreKbd, XkbStateNotify,
                             XkbModifierStateMask, XkbModifierStateMask)) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p   InitXKBExtension failed due to failure of "
             "XkbSelectEventDetails() for XModifierStateMask, display=0x%p",
             this, display));
    return;
  }

  if (!XkbSelectEventDetails(display, XkbUseCoreKbd, XkbControlsNotify,
                             XkbPerKeyRepeatMask, XkbPerKeyRepeatMask)) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p   InitXKBExtension failed due to failure of "
             "XkbSelectEventDetails() for XkbControlsNotify, display=0x%p",
             this, display));
    return;
  }

  if (!XGetKeyboardControl(display, &mKeyboardState)) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p   InitXKBExtension failed due to failure of "
             "XGetKeyboardControl(), display=0x%p",
             this, display));
    return;
  }

  MOZ_LOG(gKeyLog, LogLevel::Info, ("%p   InitXKBExtension, Succeeded", this));
}

void KeymapWrapper::InitBySystemSettingsX11() {
  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("%p InitBySystemSettingsX11, mGdkKeymap=%p", this, mGdkKeymap));

  if (!mOnKeysChangedSignalHandle) {
    mOnKeysChangedSignalHandle = g_signal_connect(
        mGdkKeymap, "keys-changed", (GCallback)OnKeysChanged, this);
  }
  if (!mOnDirectionChangedSignalHandle) {
    mOnDirectionChangedSignalHandle = g_signal_connect(
        mGdkKeymap, "direction-changed", (GCallback)OnDirectionChanged, this);
  }

  Display* display = gdk_x11_display_get_xdisplay(gdk_display_get_default());

  int min_keycode = 0;
  int max_keycode = 0;
  XDisplayKeycodes(display, &min_keycode, &max_keycode);

  int keysyms_per_keycode = 0;
  KeySym* xkeymap =
      XGetKeyboardMapping(display, min_keycode, max_keycode - min_keycode + 1,
                          &keysyms_per_keycode);
  if (!xkeymap) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p   InitBySystemSettings, "
             "Failed due to null xkeymap",
             this));
    return;
  }

  XModifierKeymap* xmodmap = XGetModifierMapping(display);
  if (!xmodmap) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p   InitBySystemSettings, "
             "Failed due to null xmodmap",
             this));
    XFree(xkeymap);
    return;
  }
  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("%p   InitBySystemSettings, min_keycode=%d, "
           "max_keycode=%d, keysyms_per_keycode=%d, max_keypermod=%d",
           this, min_keycode, max_keycode, keysyms_per_keycode,
           xmodmap->max_keypermod));

  // The modifiermap member of the XModifierKeymap structure contains 8 sets
  // of max_keypermod KeyCodes, one for each modifier in the order Shift,
  // Lock, Control, Mod1, Mod2, Mod3, Mod4, and Mod5.
  // Only nonzero KeyCodes have meaning in each set, and zero KeyCodes are
  // ignored.

  // Note that two or more modifiers may use one modifier flag.  E.g.,
  // on Ubuntu 10.10, Alt and Meta share the Mod1 in default settings.
  // And also Super and Hyper share the Mod4. In such cases, we need to
  // decide which modifier flag means one of DOM modifiers.

  // mod[0] is Modifier introduced by Mod1.
  MappedModifier mod[5];
  int32_t foundLevel[5];
  for (uint32_t i = 0; i < ArrayLength(mod); i++) {
    mod[i] = NOT_MODIFIER;
    foundLevel[i] = INT32_MAX;
  }
  const uint32_t map_size = 8 * xmodmap->max_keypermod;
  for (uint32_t i = 0; i < map_size; i++) {
    KeyCode keycode = xmodmap->modifiermap[i];
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p   InitBySystemSettings, "
             "  i=%d, keycode=0x%08X",
             this, i, keycode));
    if (!keycode || keycode < min_keycode || keycode > max_keycode) {
      continue;
    }

    ModifierKey* modifierKey = GetModifierKey(keycode);
    if (!modifierKey) {
      modifierKey = mModifierKeys.AppendElement(ModifierKey(keycode));
    }

    const KeySym* syms =
        xkeymap + (keycode - min_keycode) * keysyms_per_keycode;
    const uint32_t bit = i / xmodmap->max_keypermod;
    modifierKey->mMask |= 1 << bit;

    // We need to know the meaning of Mod1, Mod2, Mod3, Mod4 and Mod5.
    // Let's skip if current map is for others.
    if (bit < 3) {
      continue;
    }

    const int32_t modIndex = bit - 3;
    for (int32_t j = 0; j < keysyms_per_keycode; j++) {
      MappedModifier modifier = GetModifierForGDKKeyval(syms[j]);
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("%p   InitBySystemSettings, "
               "    Mod%d, j=%d, syms[j]=%s(0x%lX), modifier=%s",
               this, modIndex + 1, j, gdk_keyval_name(syms[j]), syms[j],
               GetModifierName(modifier)));

      switch (modifier) {
        case NOT_MODIFIER:
          // Don't overwrite the stored information with
          // NOT_MODIFIER.
          break;
        case CAPS_LOCK:
        case SHIFT:
        case CTRL:
        case SUPER:
          // Ignore the modifiers defined in GDK spec. They shouldn't
          // be mapped to Mod1-5 because they must not work on native
          // GTK applications.
          break;
        default:
          // If new modifier is found in higher level than stored
          // value, we don't need to overwrite it.
          if (j > foundLevel[modIndex]) {
            break;
          }
          // If new modifier is more important than stored value,
          // we should overwrite it with new modifier.
          if (j == foundLevel[modIndex]) {
            mod[modIndex] = std::min(modifier, mod[modIndex]);
            break;
          }
          foundLevel[modIndex] = j;
          mod[modIndex] = modifier;
          break;
      }
    }
  }

  for (uint32_t i = 0; i < COUNT_OF_MODIFIER_INDEX; i++) {
    MappedModifier modifier;
    switch (i) {
      case INDEX_NUM_LOCK:
        modifier = NUM_LOCK;
        break;
      case INDEX_SCROLL_LOCK:
        modifier = SCROLL_LOCK;
        break;
      case INDEX_ALT:
        modifier = ALT;
        break;
      case INDEX_META:
        modifier = META;
        break;
      case INDEX_HYPER:
        modifier = HYPER;
        break;
      case INDEX_LEVEL3:
        modifier = LEVEL3;
        break;
      case INDEX_LEVEL5:
        modifier = LEVEL5;
        break;
      default:
        MOZ_CRASH("All indexes must be handled here");
    }
    for (uint32_t j = 0; j < ArrayLength(mod); j++) {
      if (modifier == mod[j]) {
        mModifierMasks[i] |= 1 << (j + 3);
      }
    }
  }

  XFreeModifiermap(xmodmap);
  XFree(xkeymap);
}
#endif

#ifdef MOZ_WAYLAND
void KeymapWrapper::SetModifierMask(xkb_keymap* aKeymap,
                                    ModifierIndex aModifierIndex,
                                    const char* aModifierName) {
  static auto sXkbKeymapModGetIndex =
      (xkb_mod_index_t(*)(struct xkb_keymap*, const char*))dlsym(
          RTLD_DEFAULT, "xkb_keymap_mod_get_index");

  xkb_mod_index_t index = sXkbKeymapModGetIndex(aKeymap, aModifierName);
  if (index != XKB_MOD_INVALID) {
    mModifierMasks[aModifierIndex] = (1 << index);
  }
}

void KeymapWrapper::SetModifierMasks(xkb_keymap* aKeymap) {
  KeymapWrapper* keymapWrapper = GetInstance();

  // This mapping is derived from get_xkb_modifiers() at gdkkeys-wayland.c
  keymapWrapper->SetModifierMask(aKeymap, INDEX_NUM_LOCK, XKB_MOD_NAME_NUM);
  keymapWrapper->SetModifierMask(aKeymap, INDEX_ALT, XKB_MOD_NAME_ALT);
  keymapWrapper->SetModifierMask(aKeymap, INDEX_META, "Meta");
  keymapWrapper->SetModifierMask(aKeymap, INDEX_HYPER, "Hyper");

  keymapWrapper->SetModifierMask(aKeymap, INDEX_SCROLL_LOCK, "ScrollLock");
  keymapWrapper->SetModifierMask(aKeymap, INDEX_LEVEL3, "Level3");
  keymapWrapper->SetModifierMask(aKeymap, INDEX_LEVEL5, "Level5");

  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("%p KeymapWrapper::SetModifierMasks, CapsLock=0x%X, NumLock=0x%X, "
           "ScrollLock=0x%X, Level3=0x%X, Level5=0x%X, "
           "Shift=0x%X, Ctrl=0x%X, Alt=0x%X, Meta=0x%X, Super=0x%X, Hyper=0x%X",
           keymapWrapper, keymapWrapper->GetGdkModifierMask(CAPS_LOCK),
           keymapWrapper->GetGdkModifierMask(NUM_LOCK),
           keymapWrapper->GetGdkModifierMask(SCROLL_LOCK),
           keymapWrapper->GetGdkModifierMask(LEVEL3),
           keymapWrapper->GetGdkModifierMask(LEVEL5),
           keymapWrapper->GetGdkModifierMask(SHIFT),
           keymapWrapper->GetGdkModifierMask(CTRL),
           keymapWrapper->GetGdkModifierMask(ALT),
           keymapWrapper->GetGdkModifierMask(META),
           keymapWrapper->GetGdkModifierMask(SUPER),
           keymapWrapper->GetGdkModifierMask(HYPER)));
}

/* This keymap routine is derived from weston-2.0.0/clients/simple-im.c
 */
static void keyboard_handle_keymap(void* data, struct wl_keyboard* wl_keyboard,
                                   uint32_t format, int fd, uint32_t size) {
  KeymapWrapper::ResetKeyboard();

  if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
    close(fd);
    return;
  }

  char* mapString = (char*)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
  if (mapString == MAP_FAILED) {
    close(fd);
    return;
  }

  static auto sXkbContextNew =
      (struct xkb_context * (*)(enum xkb_context_flags))
          dlsym(RTLD_DEFAULT, "xkb_context_new");
  static auto sXkbKeymapNewFromString =
      (struct xkb_keymap * (*)(struct xkb_context*, const char*,
                               enum xkb_keymap_format,
                               enum xkb_keymap_compile_flags))
          dlsym(RTLD_DEFAULT, "xkb_keymap_new_from_string");

  struct xkb_context* xkb_context = sXkbContextNew(XKB_CONTEXT_NO_FLAGS);
  struct xkb_keymap* keymap =
      sXkbKeymapNewFromString(xkb_context, mapString, XKB_KEYMAP_FORMAT_TEXT_V1,
                              XKB_KEYMAP_COMPILE_NO_FLAGS);

  munmap(mapString, size);
  close(fd);

  if (!keymap) {
    NS_WARNING("keyboard_handle_keymap(): Failed to compile keymap!\n");
    return;
  }

  KeymapWrapper::SetModifierMasks(keymap);

  static auto sXkbKeymapUnRef =
      (void (*)(struct xkb_keymap*))dlsym(RTLD_DEFAULT, "xkb_keymap_unref");
  sXkbKeymapUnRef(keymap);

  static auto sXkbContextUnref =
      (void (*)(struct xkb_context*))dlsym(RTLD_DEFAULT, "xkb_context_unref");
  sXkbContextUnref(xkb_context);
}

static void keyboard_handle_enter(void* data, struct wl_keyboard* keyboard,
                                  uint32_t serial, struct wl_surface* surface,
                                  struct wl_array* keys) {
  KeymapWrapper::SetFocusIn(surface, serial);
}

static void keyboard_handle_leave(void* data, struct wl_keyboard* keyboard,
                                  uint32_t serial, struct wl_surface* surface) {
  KeymapWrapper::SetFocusOut(surface);
}

static void keyboard_handle_key(void* data, struct wl_keyboard* keyboard,
                                uint32_t serial, uint32_t time, uint32_t key,
                                uint32_t state) {}
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) {}
static void keyboard_handle_repeat_info(void* data,
                                        struct wl_keyboard* keyboard,
                                        int32_t rate, int32_t delay) {}

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

static void seat_handle_capabilities(void* data, struct wl_seat* seat,
                                     unsigned int caps) {
  wl_keyboard* keyboard = KeymapWrapper::GetKeyboard();
  if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !keyboard) {
    keyboard = wl_seat_get_keyboard(seat);
    wl_keyboard_add_listener(keyboard, &keyboard_listener, nullptr);
    KeymapWrapper::SetKeyboard(keyboard);
  } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && keyboard) {
    KeymapWrapper::ClearKeyboard();
  }
}

static const struct wl_seat_listener seat_listener = {
    seat_handle_capabilities,
};

#endif

KeymapWrapper::~KeymapWrapper() {
#ifdef MOZ_X11
  gdk_window_remove_filter(nullptr, FilterEvents, this);
#endif
  if (mOnKeysChangedSignalHandle) {
    g_signal_handler_disconnect(mGdkKeymap, mOnKeysChangedSignalHandle);
  }
  if (mOnDirectionChangedSignalHandle) {
    g_signal_handler_disconnect(mGdkKeymap, mOnDirectionChangedSignalHandle);
  }
  g_object_unref(mGdkKeymap);
  MOZ_LOG(gKeyLog, LogLevel::Info, ("%p Destructor", this));
}

#ifdef MOZ_X11
/* static */
GdkFilterReturn KeymapWrapper::FilterEvents(GdkXEvent* aXEvent,
                                            GdkEvent* aGdkEvent,
                                            gpointer aData) {
  XEvent* xEvent = static_cast<XEvent*>(aXEvent);
  switch (xEvent->type) {
    case KeyPress: {
      // If the key doesn't support auto repeat, ignore the event because
      // even if such key (e.g., Shift) is pressed during auto repeat of
      // anoter key, it doesn't stop the auto repeat.
      KeymapWrapper* self = static_cast<KeymapWrapper*>(aData);
      if (!self->IsAutoRepeatableKey(xEvent->xkey.keycode)) {
        break;
      }
      if (sRepeatState == NOT_PRESSED) {
        sRepeatState = FIRST_PRESS;
        MOZ_LOG(gKeyLog, LogLevel::Info,
                ("FilterEvents(aXEvent={ type=KeyPress, "
                 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
                 "aGdkEvent={ state=0x%08X }), "
                 "detected first keypress",
                 xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time,
                 reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
      } else if (sLastRepeatableHardwareKeyCode == xEvent->xkey.keycode) {
        if (sLastRepeatableKeyTime == xEvent->xkey.time &&
            sLastRepeatableHardwareKeyCode ==
                IMContextWrapper::
                    GetWaitingSynthesizedKeyPressHardwareKeyCode()) {
          // On some environment, IM may generate duplicated KeyPress event
          // without any special state flags.  In such case, we shouldn't
          // treat the event as "repeated".
          MOZ_LOG(gKeyLog, LogLevel::Info,
                  ("FilterEvents(aXEvent={ type=KeyPress, "
                   "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
                   "aGdkEvent={ state=0x%08X }), "
                   "igored keypress since it must be synthesized by IME",
                   xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time,
                   reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
          break;
        }
        sRepeatState = REPEATING;
        MOZ_LOG(gKeyLog, LogLevel::Info,
                ("FilterEvents(aXEvent={ type=KeyPress, "
                 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
                 "aGdkEvent={ state=0x%08X }), "
                 "detected repeating keypress",
                 xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time,
                 reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
      } else {
        // If a different key is pressed while another key is pressed,
        // auto repeat system repeats only the last pressed key.
        // So, setting new keycode and setting repeat state as first key
        // press should work fine.
        sRepeatState = FIRST_PRESS;
        MOZ_LOG(gKeyLog, LogLevel::Info,
                ("FilterEvents(aXEvent={ type=KeyPress, "
                 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
                 "aGdkEvent={ state=0x%08X }), "
                 "detected different keypress",
                 xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time,
                 reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
      }
      sLastRepeatableHardwareKeyCode = xEvent->xkey.keycode;
      sLastRepeatableKeyTime = xEvent->xkey.time;
      break;
    }
    case KeyRelease: {
      if (sLastRepeatableHardwareKeyCode != xEvent->xkey.keycode) {
        // This case means the key release event is caused by
        // a non-repeatable key such as Shift or a repeatable key that
        // was pressed before sLastRepeatableHardwareKeyCode was
        // pressed.
        break;
      }
      sRepeatState = NOT_PRESSED;
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("FilterEvents(aXEvent={ type=KeyRelease, "
               "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
               "aGdkEvent={ state=0x%08X }), "
               "detected key release",
               xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time,
               reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
      break;
    }
    case FocusOut: {
      // At moving focus, we should reset keyboard repeat state.
      // Strictly, this causes incorrect behavior.  However, this
      // correctness must be enough for web applications.
      sRepeatState = NOT_PRESSED;
      break;
    }
    default: {
      KeymapWrapper* self = static_cast<KeymapWrapper*>(aData);
      if (xEvent->type != self->mXKBBaseEventCode) {
        break;
      }
      XkbEvent* xkbEvent = (XkbEvent*)xEvent;
      if (xkbEvent->any.xkb_type != XkbControlsNotify ||
          !(xkbEvent->ctrls.changed_ctrls & XkbPerKeyRepeatMask)) {
        break;
      }
      if (!XGetKeyboardControl(xkbEvent->any.display, &self->mKeyboardState)) {
        MOZ_LOG(gKeyLog, LogLevel::Info,
                ("%p FilterEvents failed due to failure "
                 "of XGetKeyboardControl(), display=0x%p",
                 self, xkbEvent->any.display));
      }
      break;
    }
  }

  return GDK_FILTER_CONTINUE;
}
#endif

static void ResetBidiKeyboard() {
  // Reset the bidi keyboard settings for the new GdkKeymap
  nsCOMPtr<nsIBidiKeyboard> bidiKeyboard = nsContentUtils::GetBidiKeyboard();
  if (bidiKeyboard) {
    bidiKeyboard->Reset();
  }
  WidgetUtils::SendBidiKeyboardInfoToContent();
}

/* static */
void KeymapWrapper::ResetKeyboard() {
  if (sInstance) {
    sInstance->mInitialized = false;
    ResetBidiKeyboard();
  }
}

/* static */
void KeymapWrapper::OnKeysChanged(GdkKeymap* aGdkKeymap,
                                  KeymapWrapper* aKeymapWrapper) {
  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("OnKeysChanged, aGdkKeymap=%p, aKeymapWrapper=%p", aGdkKeymap,
           aKeymapWrapper));

  MOZ_ASSERT(sInstance == aKeymapWrapper,
             "This instance must be the singleton instance");

  // We cannot reintialize here becasue we don't have GdkWindow which is using
  // the GdkKeymap.  We'll reinitialize it when next GetInstance() is called.
  ResetKeyboard();
}

// static
void KeymapWrapper::OnDirectionChanged(GdkKeymap* aGdkKeymap,
                                       KeymapWrapper* aKeymapWrapper) {
  // XXX
  // A lot of diretion-changed signal might be fired on switching bidi
  // keyboard when using both ibus (with arabic layout) and fcitx (with IME).
  // See https://github.com/fcitx/fcitx/issues/257
  //
  // Also, when using ibus, switching to IM might not cause this signal.
  // See https://github.com/ibus/ibus/issues/1848

  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("OnDirectionChanged, aGdkKeymap=%p, aKeymapWrapper=%p", aGdkKeymap,
           aKeymapWrapper));

  ResetBidiKeyboard();
}

/* static */
guint KeymapWrapper::GetCurrentModifierState() {
  GdkModifierType modifiers;
  GdkDisplay* display = gdk_display_get_default();
  GdkScreen* screen = gdk_display_get_default_screen(display);
  GdkWindow* window = gdk_screen_get_root_window(screen);
  gdk_window_get_device_position(window, GdkGetPointer(), nullptr, nullptr,
                                 &modifiers);
  return static_cast<guint>(modifiers);
}

/* static */
bool KeymapWrapper::AreModifiersActive(MappedModifiers aModifiers,
                                       guint aGdkModifierState) {
  NS_ENSURE_TRUE(aModifiers, false);

  KeymapWrapper* keymapWrapper = GetInstance();
  for (uint32_t i = 0; i < sizeof(MappedModifier) * 8 && aModifiers; i++) {
    MappedModifier modifier = static_cast<MappedModifier>(1 << i);
    // Is the binary position used by modifier?
    if (!(aModifiers & modifier)) {
      continue;
    }
    // Is the modifier active?
    if (!(aGdkModifierState & keymapWrapper->GetGdkModifierMask(modifier))) {
      return false;
    }
    aModifiers &= ~modifier;
  }
  return true;
}

/* static */
uint32_t KeymapWrapper::ComputeCurrentKeyModifiers() {
  return ComputeKeyModifiers(GetCurrentModifierState());
}

/* static */
uint32_t KeymapWrapper::ComputeKeyModifiers(guint aGdkModifierState) {
  uint32_t keyModifiers = 0;
  if (!aGdkModifierState) {
    return keyModifiers;
  }

  // DOM Meta key should be TRUE only on Mac.  We need to discuss this
  // issue later.
  KeymapWrapper* keymapWrapper = GetInstance();
  if (keymapWrapper->AreModifiersActive(SHIFT, aGdkModifierState)) {
    keyModifiers |= MODIFIER_SHIFT;
  }
  if (keymapWrapper->AreModifiersActive(CTRL, aGdkModifierState)) {
    keyModifiers |= MODIFIER_CONTROL;
  }
  if (keymapWrapper->AreModifiersActive(ALT, aGdkModifierState)) {
    keyModifiers |= MODIFIER_ALT;
  }
  if (keymapWrapper->AreModifiersActive(SUPER, aGdkModifierState) ||
      keymapWrapper->AreModifiersActive(HYPER, aGdkModifierState) ||
      // "Meta" state is typically mapped to `Alt` + `Shift`, but we ignore the
      // state if `Alt` is mapped to "Alt" state.  Additionally it's mapped to
      // `Win` in Sun/Solaris keyboard layout.  In this case, we want to treat
      // them as DOM Meta modifier keys like "Super" state in the major Linux
      // environments.
      keymapWrapper->AreModifiersActive(META, aGdkModifierState)) {
    keyModifiers |= MODIFIER_META;
  }
  if (keymapWrapper->AreModifiersActive(LEVEL3, aGdkModifierState) ||
      keymapWrapper->AreModifiersActive(LEVEL5, aGdkModifierState)) {
    keyModifiers |= MODIFIER_ALTGRAPH;
  }
  if (keymapWrapper->AreModifiersActive(CAPS_LOCK, aGdkModifierState)) {
    keyModifiers |= MODIFIER_CAPSLOCK;
  }
  if (keymapWrapper->AreModifiersActive(NUM_LOCK, aGdkModifierState)) {
    keyModifiers |= MODIFIER_NUMLOCK;
  }
  if (keymapWrapper->AreModifiersActive(SCROLL_LOCK, aGdkModifierState)) {
    keyModifiers |= MODIFIER_SCROLLLOCK;
  }
  return keyModifiers;
}

/* static */
guint KeymapWrapper::ConvertWidgetModifierToGdkState(
    nsIWidget::Modifiers aNativeModifiers) {
  if (!aNativeModifiers) {
    return 0;
  }
  struct ModifierMapEntry {
    nsIWidget::Modifiers mWidgetModifier;
    MappedModifier mModifier;
  };
  // TODO: Currently, we don't treat L/R of each modifier on Linux.
  // TODO: No proper native modifier for Level5.
  static constexpr ModifierMapEntry sModifierMap[] = {
      {nsIWidget::CAPS_LOCK, MappedModifier::CAPS_LOCK},
      {nsIWidget::NUM_LOCK, MappedModifier::NUM_LOCK},
      {nsIWidget::SHIFT_L, MappedModifier::SHIFT},
      {nsIWidget::SHIFT_R, MappedModifier::SHIFT},
      {nsIWidget::CTRL_L, MappedModifier::CTRL},
      {nsIWidget::CTRL_R, MappedModifier::CTRL},
      {nsIWidget::ALT_L, MappedModifier::ALT},
      {nsIWidget::ALT_R, MappedModifier::ALT},
      {nsIWidget::ALTGRAPH, MappedModifier::LEVEL3},
      {nsIWidget::COMMAND_L, MappedModifier::SUPER},
      {nsIWidget::COMMAND_R, MappedModifier::SUPER}};

  guint state = 0;
  KeymapWrapper* instance = GetInstance();
  for (const ModifierMapEntry& entry : sModifierMap) {
    if (aNativeModifiers & entry.mWidgetModifier) {
      state |= instance->GetGdkModifierMask(entry.mModifier);
    }
  }
  return state;
}

/* static */
void KeymapWrapper::InitInputEvent(WidgetInputEvent& aInputEvent,
                                   guint aGdkModifierState) {
  KeymapWrapper* keymapWrapper = GetInstance();

  aInputEvent.mModifiers = ComputeKeyModifiers(aGdkModifierState);

  // Don't log this method for non-important events because e.g., eMouseMove is
  // just noisy and there is no reason to log it.
  bool doLog = aInputEvent.mMessage != eMouseMove;
  if (doLog) {
    MOZ_LOG(gKeyLog, LogLevel::Debug,
            ("%p InitInputEvent, aGdkModifierState=0x%08X, "
             "aInputEvent={ mMessage=%s, mModifiers=0x%04X (Shift: %s, "
             "Control: %s, Alt: %s, Meta: %s, AltGr: %s, "
             "CapsLock: %s, NumLock: %s, ScrollLock: %s })",
             keymapWrapper, aGdkModifierState, ToChar(aInputEvent.mMessage),
             aInputEvent.mModifiers,
             GetBoolName(aInputEvent.mModifiers & MODIFIER_SHIFT),
             GetBoolName(aInputEvent.mModifiers & MODIFIER_CONTROL),
             GetBoolName(aInputEvent.mModifiers & MODIFIER_ALT),
             GetBoolName(aInputEvent.mModifiers & MODIFIER_META),
             GetBoolName(aInputEvent.mModifiers & MODIFIER_ALTGRAPH),
             GetBoolName(aInputEvent.mModifiers & MODIFIER_CAPSLOCK),
             GetBoolName(aInputEvent.mModifiers & MODIFIER_NUMLOCK),
             GetBoolName(aInputEvent.mModifiers & MODIFIER_SCROLLLOCK)));
  }

  switch (aInputEvent.mClass) {
    case eMouseEventClass:
    case eMouseScrollEventClass:
    case eWheelEventClass:
    case eDragEventClass:
    case eSimpleGestureEventClass:
      break;
    default:
      return;
  }

  WidgetMouseEventBase& mouseEvent = *aInputEvent.AsMouseEventBase();
  mouseEvent.mButtons = 0;
  if (aGdkModifierState & GDK_BUTTON1_MASK) {
    mouseEvent.mButtons |= MouseButtonsFlag::ePrimaryFlag;
  }
  if (aGdkModifierState & GDK_BUTTON3_MASK) {
    mouseEvent.mButtons |= MouseButtonsFlag::eSecondaryFlag;
  }
  if (aGdkModifierState & GDK_BUTTON2_MASK) {
    mouseEvent.mButtons |= MouseButtonsFlag::eMiddleFlag;
  }

  if (doLog) {
    MOZ_LOG(
        gKeyLog, LogLevel::Debug,
        ("%p   InitInputEvent, aInputEvent has mButtons, "
         "aInputEvent.mButtons=0x%04X (Left: %s, Right: %s, Middle: %s, "
         "4th (BACK): %s, 5th (FORWARD): %s)",
         keymapWrapper, mouseEvent.mButtons,
         GetBoolName(mouseEvent.mButtons & MouseButtonsFlag::ePrimaryFlag),
         GetBoolName(mouseEvent.mButtons & MouseButtonsFlag::eSecondaryFlag),
         GetBoolName(mouseEvent.mButtons & MouseButtonsFlag::eMiddleFlag),
         GetBoolName(mouseEvent.mButtons & MouseButtonsFlag::e4thFlag),
         GetBoolName(mouseEvent.mButtons & MouseButtonsFlag::e5thFlag)));
  }
}

/* static */
uint32_t KeymapWrapper::ComputeDOMKeyCode(const GdkEventKey* aGdkKeyEvent) {
  // If the keyval indicates it's a modifier key, we should use unshifted
  // key's modifier keyval.
  guint keyval = aGdkKeyEvent->keyval;
  if (GetModifierForGDKKeyval(keyval)) {
    // But if the keyval without modifiers isn't a modifier key, we
    // shouldn't use it.  E.g., Japanese keyboard layout's
    // Shift + Eisu-Toggle key is CapsLock.  This is an actual rare case,
    // Windows uses different keycode for a physical key for different
    // shift key state.
    guint keyvalWithoutModifier = GetGDKKeyvalWithoutModifier(aGdkKeyEvent);
    if (GetModifierForGDKKeyval(keyvalWithoutModifier)) {
      keyval = keyvalWithoutModifier;
    }
    // Note that the modifier keycode and activating or deactivating
    // modifier flag may be mismatched, but it's okay.  If a DOM key
    // event handler is testing a keydown event, it's more likely being
    // used to test which key is being pressed than to test which
    // modifier will become active.  So, if we computed DOM keycode
    // from modifier flag which were changing by the physical key, then
    // there would be no other way for the user to generate the original
    // keycode.
    uint32_t DOMKeyCode = GetDOMKeyCodeFromKeyPairs(keyval);
    NS_ASSERTION(DOMKeyCode, "All modifier keys must have a DOM keycode");
    return DOMKeyCode;
  }

  // If the key isn't printable, let's look at the key pairs.
  uint32_t charCode = GetCharCodeFor(aGdkKeyEvent);
  if (!charCode) {
    // Note that any key may be a function key because of some unusual keyboard
    // layouts.  I.e., even if the pressed key is a printable key of en-US
    // keyboard layout, we should expose the function key's keyCode value to
    // web apps because web apps should handle the keydown/keyup events as
    // inputted by usual keyboard layout.  For example, Hatchak keyboard
    // maps Tab key to "Digit3" key and Level3 Shift makes it "Backspace".
    // In this case, we should expose DOM_VK_BACK_SPACE (8).
    uint32_t DOMKeyCode = GetDOMKeyCodeFromKeyPairs(keyval);
    if (DOMKeyCode) {
      // XXX If DOMKeyCode is a function key's keyCode value, it might be
      //     better to consume necessary modifiers.  For example, if there is
      //     no Control Pad section on keyboard like notebook, Delete key is
      //     available only with Level3 Shift+"Backspace" key if using Hatchak.
      //     If web apps accept Delete key operation only when no modifiers are
      //     active, such users cannot use Delete key to do it.  However,
      //     Chromium doesn't consume such necessary modifiers.  So, our default
      //     behavior should keep not touching modifiers for compatibility, but
      //     it might be better to add a pref to consume necessary modifiers.
      return DOMKeyCode;
    }
    // If aGdkKeyEvent cannot be mapped to a DOM keyCode value, we should
    // refer keyCode value without modifiers because web apps should be
    // able to identify the key as far as possible.
    guint keyvalWithoutModifier = GetGDKKeyvalWithoutModifier(aGdkKeyEvent);
    return GetDOMKeyCodeFromKeyPairs(keyvalWithoutModifier);
  }

  // printable numpad keys should be resolved here.
  switch (keyval) {
    case GDK_KP_Multiply:
      return NS_VK_MULTIPLY;
    case GDK_KP_Add:
      return NS_VK_ADD;
    case GDK_KP_Separator:
      return NS_VK_SEPARATOR;
    case GDK_KP_Subtract:
      return NS_VK_SUBTRACT;
    case GDK_KP_Decimal:
      return NS_VK_DECIMAL;
    case GDK_KP_Divide:
      return NS_VK_DIVIDE;
    case GDK_KP_0:
      return NS_VK_NUMPAD0;
    case GDK_KP_1:
      return NS_VK_NUMPAD1;
    case GDK_KP_2:
      return NS_VK_NUMPAD2;
    case GDK_KP_3:
      return NS_VK_NUMPAD3;
    case GDK_KP_4:
      return NS_VK_NUMPAD4;
    case GDK_KP_5:
      return NS_VK_NUMPAD5;
    case GDK_KP_6:
      return NS_VK_NUMPAD6;
    case GDK_KP_7:
      return NS_VK_NUMPAD7;
    case GDK_KP_8:
      return NS_VK_NUMPAD8;
    case GDK_KP_9:
      return NS_VK_NUMPAD9;
  }

  KeymapWrapper* keymapWrapper = GetInstance();

  // Ignore all modifier state except NumLock.
  guint baseState =
      (aGdkKeyEvent->state & keymapWrapper->GetGdkModifierMask(NUM_LOCK));

  // Basically, we should use unmodified character for deciding our keyCode.
  uint32_t unmodifiedChar = keymapWrapper->GetCharCodeFor(
      aGdkKeyEvent, baseState, aGdkKeyEvent->group);
  if (IsBasicLatinLetterOrNumeral(unmodifiedChar)) {
    // If the unmodified character is an ASCII alphabet or an ASCII
    // numeric, it's the best hint for deciding our keyCode.
    return WidgetUtils::ComputeKeyCodeFromChar(unmodifiedChar);
  }

  // If the unmodified character is not an ASCII character, that means we
  // couldn't find the hint. We should reset it.
  if (!IsPrintableASCIICharacter(unmodifiedChar)) {
    unmodifiedChar = 0;
  }

  // Retry with shifted keycode.
  guint shiftState = (baseState | keymapWrapper->GetGdkModifierMask(SHIFT));
  uint32_t shiftedChar = keymapWrapper->GetCharCodeFor(aGdkKeyEvent, shiftState,
                                                       aGdkKeyEvent->group);
  if (IsBasicLatinLetterOrNumeral(shiftedChar)) {
    // A shifted character can be an ASCII alphabet on Hebrew keyboard
    // layout. And also shifted character can be an ASCII numeric on
    // AZERTY keyboad layout.  Then, it's a good hint for deciding our
    // keyCode.
    return WidgetUtils::ComputeKeyCodeFromChar(shiftedChar);
  }

  // If the shifted unmodified character isn't an ASCII character, we should
  // discard it too.
  if (!IsPrintableASCIICharacter(shiftedChar)) {
    shiftedChar = 0;
  }

  // If current keyboard layout isn't ASCII alphabet inputtable layout,
  // look for ASCII alphabet inputtable keyboard layout.  If the key
  // inputs an ASCII alphabet or an ASCII numeric, we should use it
  // for deciding our keyCode.
  uint32_t unmodCharLatin = 0;
  uint32_t shiftedCharLatin = 0;
  if (!keymapWrapper->IsLatinGroup(aGdkKeyEvent->group)) {
    gint minGroup = keymapWrapper->GetFirstLatinGroup();
    if (minGroup >= 0) {
      unmodCharLatin =
          keymapWrapper->GetCharCodeFor(aGdkKeyEvent, baseState, minGroup);
      if (IsBasicLatinLetterOrNumeral(unmodCharLatin)) {
        // If the unmodified character is an ASCII alphabet or
        // an ASCII numeric, we should use it for the keyCode.
        return WidgetUtils::ComputeKeyCodeFromChar(unmodCharLatin);
      }
      // If the unmodified character in the alternative ASCII capable
      // keyboard layout isn't an ASCII character, that means we couldn't
      // find the hint. We should reset it.
      if (!IsPrintableASCIICharacter(unmodCharLatin)) {
        unmodCharLatin = 0;
      }
      shiftedCharLatin =
          keymapWrapper->GetCharCodeFor(aGdkKeyEvent, shiftState, minGroup);
      if (IsBasicLatinLetterOrNumeral(shiftedCharLatin)) {
        // If the shifted character is an ASCII alphabet or an ASCII
        // numeric, we should use it for the keyCode.
        return WidgetUtils::ComputeKeyCodeFromChar(shiftedCharLatin);
      }
      // If the shifted unmodified character in the alternative ASCII
      // capable keyboard layout isn't an ASCII character, we should
      // discard it too.
      if (!IsPrintableASCIICharacter(shiftedCharLatin)) {
        shiftedCharLatin = 0;
      }
    }
  }

  // If the key itself or with Shift state on active keyboard layout produces
  // an ASCII punctuation character, we should decide keyCode value with it.
  if (unmodifiedChar || shiftedChar) {
    return WidgetUtils::ComputeKeyCodeFromChar(unmodifiedChar ? unmodifiedChar
                                                              : shiftedChar);
  }

  // If the key itself or with Shift state on alternative ASCII capable
  // keyboard layout produces an ASCII punctuation character, we should
  // decide keyCode value with it.  Note that We've returned 0 for long
  // time if keyCode isn't for an alphabet keys or a numeric key even in
  // alternative ASCII capable keyboard layout because we decided that we
  // should avoid setting same keyCode value to 2 or more keys since active
  // keyboard layout may have a key to input the punctuation with different
  // key.  However, setting keyCode to 0 makes some web applications which
  // are aware of neither KeyboardEvent.key nor KeyboardEvent.code not work
  // with Firefox when user selects non-ASCII capable keyboard layout such
  // as Russian and Thai.  So, if alternative ASCII capable keyboard layout
  // has keyCode value for the key, we should use it.  In other words, this
  // behavior means that non-ASCII capable keyboard layout overrides some
  // keys' keyCode value only if the key produces ASCII character by itself
  // or with Shift key.
  if (unmodCharLatin || shiftedCharLatin) {
    return WidgetUtils::ComputeKeyCodeFromChar(
        unmodCharLatin ? unmodCharLatin : shiftedCharLatin);
  }

  // Otherwise, let's decide keyCode value from the hardware_keycode
  // value on major keyboard layout.
  CodeNameIndex code = ComputeDOMCodeNameIndex(aGdkKeyEvent);
  return WidgetKeyboardEvent::GetFallbackKeyCodeOfPunctuationKey(code);
}

KeyNameIndex KeymapWrapper::ComputeDOMKeyNameIndex(
    const GdkEventKey* aGdkKeyEvent) {
  switch (aGdkKeyEvent->keyval) {
#define NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX(aNativeKey, aKeyNameIndex) \
  case aNativeKey:                                                     \
    return aKeyNameIndex;

#include "NativeKeyToDOMKeyName.h"

#undef NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX

    default:
      break;
  }

  return KEY_NAME_INDEX_Unidentified;
}

/* static */
CodeNameIndex KeymapWrapper::ComputeDOMCodeNameIndex(
    const GdkEventKey* aGdkKeyEvent) {
  switch (aGdkKeyEvent->hardware_keycode) {
#define NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX(aNativeKey, aCodeNameIndex) \
  case aNativeKey:                                                       \
    return aCodeNameIndex;

#include "NativeKeyToDOMCodeName.h"

#undef NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX

    default:
      break;
  }

  return CODE_NAME_INDEX_UNKNOWN;
}

/* static */
bool KeymapWrapper::DispatchKeyDownOrKeyUpEvent(nsWindow* aWindow,
                                                GdkEventKey* aGdkKeyEvent,
                                                bool aIsProcessedByIME,
                                                bool* aIsCancelled) {
  MOZ_ASSERT(aIsCancelled, "aIsCancelled must not be nullptr");

  *aIsCancelled = false;

  if (aGdkKeyEvent->type == GDK_KEY_PRESS && aGdkKeyEvent->keyval == GDK_Tab &&
      AreModifiersActive(CTRL | ALT, aGdkKeyEvent->state)) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("  DispatchKeyDownOrKeyUpEvent(), didn't dispatch keyboard events "
             "because it's Ctrl + Alt + Tab"));
    return false;
  }

  EventMessage message =
      aGdkKeyEvent->type == GDK_KEY_PRESS ? eKeyDown : eKeyUp;
  WidgetKeyboardEvent keyEvent(true, message, aWindow);
  KeymapWrapper::InitKeyEvent(keyEvent, aGdkKeyEvent, aIsProcessedByIME);
  return DispatchKeyDownOrKeyUpEvent(aWindow, keyEvent, aIsCancelled);
}

/* static */
bool KeymapWrapper::DispatchKeyDownOrKeyUpEvent(
    nsWindow* aWindow, WidgetKeyboardEvent& aKeyboardEvent,
    bool* aIsCancelled) {
  MOZ_ASSERT(aIsCancelled, "aIsCancelled must not be nullptr");

  *aIsCancelled = false;

  RefPtr<TextEventDispatcher> dispatcher = aWindow->GetTextEventDispatcher();
  nsresult rv = dispatcher->BeginNativeInputTransaction();
  if (NS_WARN_IF(NS_FAILED(rv))) {
    MOZ_LOG(gKeyLog, LogLevel::Error,
            ("  DispatchKeyDownOrKeyUpEvent(), stopped dispatching %s event "
             "because of failed to initialize TextEventDispatcher",
             ToChar(aKeyboardEvent.mMessage)));
    return FALSE;
  }

  nsEventStatus status = nsEventStatus_eIgnore;
  bool dispatched = dispatcher->DispatchKeyboardEvent(
      aKeyboardEvent.mMessage, aKeyboardEvent, status, nullptr);
  *aIsCancelled = (status == nsEventStatus_eConsumeNoDefault);
  return dispatched;
}

/* static */
bool KeymapWrapper::MaybeDispatchContextMenuEvent(nsWindow* aWindow,
                                                  const GdkEventKey* aEvent) {
  KeyNameIndex keyNameIndex = ComputeDOMKeyNameIndex(aEvent);

  // Shift+F10 and ContextMenu should cause eContextMenu event.
  if (keyNameIndex != KEY_NAME_INDEX_F10 &&
      keyNameIndex != KEY_NAME_INDEX_ContextMenu) {
    return false;
  }

  WidgetMouseEvent contextMenuEvent(true, eContextMenu, aWindow,
                                    WidgetMouseEvent::eReal,
                                    WidgetMouseEvent::eContextMenuKey);

  contextMenuEvent.mRefPoint = LayoutDeviceIntPoint(0, 0);
  contextMenuEvent.AssignEventTime(aWindow->GetWidgetEventTime(aEvent->time));
  contextMenuEvent.mClickCount = 1;
  KeymapWrapper::InitInputEvent(contextMenuEvent, aEvent->state);

  if (contextMenuEvent.IsControl() || contextMenuEvent.IsMeta() ||
      contextMenuEvent.IsAlt()) {
    return false;
  }

  // If the key is ContextMenu, then an eContextMenu mouse event is
  // dispatched regardless of the state of the Shift modifier.  When it is
  // pressed without the Shift modifier, a web page can prevent the default
  // context menu action.  When pressed with the Shift modifier, the web page
  // cannot prevent the default context menu action.
  // (PresShell::HandleEventInternal() sets mOnlyChromeDispatch to true.)

  // If the key is F10, it needs Shift state because Shift+F10 is well-known
  // shortcut key on Linux.  However, eContextMenu with Shift state is
  // special.  It won't fire "contextmenu" event in the web content for
  // blocking web page to prevent its default.  Therefore, this combination
  // should work same as ContextMenu key.
  // XXX Should we allow to block web page to prevent its default with
  //     Ctrl+Shift+F10 or Alt+Shift+F10 instead?
  if (keyNameIndex == KEY_NAME_INDEX_F10) {
    if (!contextMenuEvent.IsShift()) {
      return false;
    }
    contextMenuEvent.mModifiers &= ~MODIFIER_SHIFT;
  }

  aWindow->DispatchInputEvent(&contextMenuEvent);
  return true;
}

/* static*/
void KeymapWrapper::HandleKeyPressEvent(nsWindow* aWindow,
                                        GdkEventKey* aGdkKeyEvent) {
  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("HandleKeyPressEvent(aWindow=%p, aGdkKeyEvent={ type=%s, "
           "keyval=%s(0x%X), state=0x%08X, hardware_keycode=0x%08X, "
           "time=%u, is_modifier=%s })",
           aWindow,
           ((aGdkKeyEvent->type == GDK_KEY_PRESS) ? "GDK_KEY_PRESS"
                                                  : "GDK_KEY_RELEASE"),
           gdk_keyval_name(aGdkKeyEvent->keyval), aGdkKeyEvent->keyval,
           aGdkKeyEvent->state, aGdkKeyEvent->hardware_keycode,
           aGdkKeyEvent->time, GetBoolName(aGdkKeyEvent->is_modifier)));

  // if we are in the middle of composing text, XIM gets to see it
  // before mozilla does.
  // FYI: Don't dispatch keydown event before notifying IME of the event
  //      because IME may send a key event synchronously and consume the
  //      original event.
  bool IMEWasEnabled = false;
  KeyHandlingState handlingState = KeyHandlingState::eNotHandled;
  RefPtr<IMContextWrapper> imContext = aWindow->GetIMContext();
  if (imContext) {
    IMEWasEnabled = imContext->IsEnabled();
    handlingState = imContext->OnKeyEvent(aWindow, aGdkKeyEvent);
    if (handlingState == KeyHandlingState::eHandled) {
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), the event was handled by "
               "IMContextWrapper"));
      return;
    }
  }

  // work around for annoying things.
  if (aGdkKeyEvent->keyval == GDK_Tab &&
      AreModifiersActive(CTRL | ALT, aGdkKeyEvent->state)) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("  HandleKeyPressEvent(), didn't dispatch keyboard events "
             "because it's Ctrl + Alt + Tab"));
    return;
  }

  // Dispatch keydown event always.  At auto repeating, we should send
  // KEYDOWN -> KEYPRESS -> KEYDOWN -> KEYPRESS ... -> KEYUP
  // However, old distributions (e.g., Ubuntu 9.10) sent native key
  // release event, so, on such platform, the DOM events will be:
  // KEYDOWN -> KEYPRESS -> KEYUP -> KEYDOWN -> KEYPRESS -> KEYUP...

  bool isKeyDownCancelled = false;
  if (handlingState == KeyHandlingState::eNotHandled) {
    if (DispatchKeyDownOrKeyUpEvent(aWindow, aGdkKeyEvent, false,
                                    &isKeyDownCancelled) &&
        (MOZ_UNLIKELY(aWindow->IsDestroyed()) || isKeyDownCancelled)) {
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched eKeyDown event and "
               "stopped handling the event because %s",
               aWindow->IsDestroyed() ? "the window has been destroyed"
                                      : "the event was consumed"));
      return;
    }
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("  HandleKeyPressEvent(), dispatched eKeyDown event and "
             "it wasn't consumed"));
    handlingState = KeyHandlingState::eNotHandledButEventDispatched;
  }

  // If a keydown event handler causes to enable IME, i.e., it moves
  // focus from IME unusable content to IME usable editor, we should
  // send the native key event to IME for the first input on the editor.
  imContext = aWindow->GetIMContext();
  if (!IMEWasEnabled && imContext && imContext->IsEnabled()) {
    // Notice our keydown event was already dispatched.  This prevents
    // unnecessary DOM keydown event in the editor.
    handlingState = imContext->OnKeyEvent(aWindow, aGdkKeyEvent, true);
    if (handlingState == KeyHandlingState::eHandled) {
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), the event was handled by "
               "IMContextWrapper which was enabled by the preceding eKeyDown "
               "event"));
      return;
    }
  }

  // Look for specialized app-command keys
  switch (aGdkKeyEvent->keyval) {
    case GDK_Back:
      aWindow->DispatchCommandEvent(nsGkAtoms::Back);
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched \"Back\" command event"));
      return;
    case GDK_Forward:
      aWindow->DispatchCommandEvent(nsGkAtoms::Forward);
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched \"Forward\" command "
               "event"));
      return;
    case GDK_Reload:
    case GDK_Refresh:
      aWindow->DispatchCommandEvent(nsGkAtoms::Reload);
      return;
    case GDK_Stop:
      aWindow->DispatchCommandEvent(nsGkAtoms::Stop);
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched \"Stop\" command event"));
      return;
    case GDK_Search:
      aWindow->DispatchCommandEvent(nsGkAtoms::Search);
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched \"Search\" command event"));
      return;
    case GDK_Favorites:
      aWindow->DispatchCommandEvent(nsGkAtoms::Bookmarks);
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched \"Bookmarks\" command "
               "event"));
      return;
    case GDK_HomePage:
      aWindow->DispatchCommandEvent(nsGkAtoms::Home);
      return;
    case GDK_Copy:
    case GDK_F16:  // F16, F20, F18, F14 are old keysyms for Copy Cut Paste Undo
      aWindow->DispatchContentCommandEvent(eContentCommandCopy);
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched \"Copy\" content command "
               "event"));
      return;
    case GDK_Cut:
    case GDK_F20:
      aWindow->DispatchContentCommandEvent(eContentCommandCut);
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched \"Cut\" content command "
               "event"));
      return;
    case GDK_Paste:
    case GDK_F18:
      aWindow->DispatchContentCommandEvent(eContentCommandPaste);
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched \"Paste\" content command "
               "event"));
      return;
    case GDK_Redo:
      aWindow->DispatchContentCommandEvent(eContentCommandRedo);
      return;
    case GDK_Undo:
    case GDK_F14:
      aWindow->DispatchContentCommandEvent(eContentCommandUndo);
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched \"Undo\" content command "
               "event"));
      return;
    default:
      break;
  }

  // before we dispatch a key, check if it's the context menu key.
  // If so, send a context menu key event instead.
  if (MaybeDispatchContextMenuEvent(aWindow, aGdkKeyEvent)) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("  HandleKeyPressEvent(), stopped dispatching eKeyPress event "
             "because eContextMenu event was dispatched"));
    return;
  }

  RefPtr<TextEventDispatcher> textEventDispatcher =
      aWindow->GetTextEventDispatcher();
  nsresult rv = textEventDispatcher->BeginNativeInputTransaction();
  if (NS_WARN_IF(NS_FAILED(rv))) {
    MOZ_LOG(gKeyLog, LogLevel::Error,
            ("  HandleKeyPressEvent(), stopped dispatching eKeyPress event "
             "because of failed to initialize TextEventDispatcher"));
    return;
  }

  // If the character code is in the BMP, send the key press event.
  // Otherwise, send a compositionchange event with the equivalent UTF-16
  // string.
  // TODO: Investigate other browser's behavior in this case because
  //       this hack is odd for UI Events.
  WidgetKeyboardEvent keypressEvent(true, eKeyPress, aWindow);
  KeymapWrapper::InitKeyEvent(keypressEvent, aGdkKeyEvent, false);
  nsEventStatus status = nsEventStatus_eIgnore;
  if (keypressEvent.mKeyNameIndex != KEY_NAME_INDEX_USE_STRING ||
      keypressEvent.mKeyValue.Length() == 1) {
    if (textEventDispatcher->MaybeDispatchKeypressEvents(keypressEvent, status,
                                                         aGdkKeyEvent)) {
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), dispatched eKeyPress event "
               "(status=%s)",
               GetStatusName(status)));
    } else {
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyPressEvent(), didn't dispatch eKeyPress event "
               "(status=%s)",
               GetStatusName(status)));
    }
  } else {
    WidgetEventTime eventTime = aWindow->GetWidgetEventTime(aGdkKeyEvent->time);
    textEventDispatcher->CommitComposition(status, &keypressEvent.mKeyValue,
                                           &eventTime);
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("  HandleKeyPressEvent(), dispatched a set of composition "
             "events"));
  }
}

/* static */
bool KeymapWrapper::HandleKeyReleaseEvent(nsWindow* aWindow,
                                          GdkEventKey* aGdkKeyEvent) {
  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("HandleKeyReleaseEvent(aWindow=%p, aGdkKeyEvent={ type=%s, "
           "keyval=%s(0x%X), state=0x%08X, hardware_keycode=0x%08X, "
           "time=%u, is_modifier=%s })",
           aWindow,
           ((aGdkKeyEvent->type == GDK_KEY_PRESS) ? "GDK_KEY_PRESS"
                                                  : "GDK_KEY_RELEASE"),
           gdk_keyval_name(aGdkKeyEvent->keyval), aGdkKeyEvent->keyval,
           aGdkKeyEvent->state, aGdkKeyEvent->hardware_keycode,
           aGdkKeyEvent->time, GetBoolName(aGdkKeyEvent->is_modifier)));

  RefPtr<IMContextWrapper> imContext = aWindow->GetIMContext();
  if (imContext) {
    KeyHandlingState handlingState =
        imContext->OnKeyEvent(aWindow, aGdkKeyEvent);
    if (handlingState != KeyHandlingState::eNotHandled) {
      MOZ_LOG(gKeyLog, LogLevel::Info,
              ("  HandleKeyReleaseEvent(), the event was handled by "
               "IMContextWrapper"));
      return true;
    }
  }

  bool isCancelled = false;
  if (NS_WARN_IF(!DispatchKeyDownOrKeyUpEvent(aWindow, aGdkKeyEvent, false,
                                              &isCancelled))) {
    MOZ_LOG(gKeyLog, LogLevel::Error,
            ("  HandleKeyReleaseEvent(), didn't dispatch eKeyUp event"));
    return false;
  }

  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("  HandleKeyReleaseEvent(), dispatched eKeyUp event "
           "(isCancelled=%s)",
           GetBoolName(isCancelled)));
  return true;
}

guint KeymapWrapper::GetModifierState(GdkEventKey* aGdkKeyEvent,
                                      KeymapWrapper* aWrapper) {
  guint state = aGdkKeyEvent->state;
  if (!aGdkKeyEvent->is_modifier) {
    return state;
  }
#ifdef MOZ_X11
  // NOTE: The state of given key event indicates adjacent state of
  // modifier keys.  E.g., even if the event is Shift key press event,
  // the bit for Shift is still false.  By the same token, even if the
  // event is Shift key release event, the bit for Shift is still true.
  // Unfortunately, gdk_keyboard_get_modifiers() returns current modifier
  // state.  It means if there're some pending modifier key press or
  // key release events, the result isn't what we want.
  GdkDisplay* gdkDisplay = gdk_display_get_default();
  if (GdkIsX11Display(gdkDisplay)) {
    GdkDisplay* gdkDisplay = gdk_display_get_default();
    Display* display = gdk_x11_display_get_xdisplay(gdkDisplay);
    if (XEventsQueued(display, QueuedAfterReading)) {
      XEvent nextEvent;
      XPeekEvent(display, &nextEvent);
      if (nextEvent.type == aWrapper->mXKBBaseEventCode) {
        XkbEvent* XKBEvent = (XkbEvent*)&nextEvent;
        if (XKBEvent->any.xkb_type == XkbStateNotify) {
          XkbStateNotifyEvent* stateNotifyEvent =
              (XkbStateNotifyEvent*)XKBEvent;
          state &= ~0xFF;
          state |= stateNotifyEvent->lookup_mods;
        }
      }
    }
    return state;
  }
#endif
#ifdef MOZ_WAYLAND
  int mask = 0;
  switch (aGdkKeyEvent->keyval) {
    case GDK_Shift_L:
    case GDK_Shift_R:
      mask = aWrapper->GetGdkModifierMask(SHIFT);
      break;
    case GDK_Control_L:
    case GDK_Control_R:
      mask = aWrapper->GetGdkModifierMask(CTRL);
      break;
    case GDK_Alt_L:
    case GDK_Alt_R:
      mask = aWrapper->GetGdkModifierMask(ALT);
      break;
    case GDK_Super_L:
    case GDK_Super_R:
      mask = aWrapper->GetGdkModifierMask(SUPER);
      break;
    case GDK_Hyper_L:
    case GDK_Hyper_R:
      mask = aWrapper->GetGdkModifierMask(HYPER);
      break;
    case GDK_Meta_L:
    case GDK_Meta_R:
      mask = aWrapper->GetGdkModifierMask(META);
      break;
    default:
      break;
  }
  if (aGdkKeyEvent->type == GDK_KEY_PRESS) {
    state |= mask;
  } else {
    state &= ~mask;
  }
#endif
  return state;
}

/* static */
void KeymapWrapper::InitKeyEvent(WidgetKeyboardEvent& aKeyEvent,
                                 GdkEventKey* aGdkKeyEvent,
                                 bool aIsProcessedByIME) {
  MOZ_ASSERT(
      !aIsProcessedByIME || aKeyEvent.mMessage != eKeyPress,
      "If the key event is handled by IME, keypress event shouldn't be fired");

  KeymapWrapper* keymapWrapper = GetInstance();

  aKeyEvent.mCodeNameIndex = ComputeDOMCodeNameIndex(aGdkKeyEvent);
  MOZ_ASSERT(aKeyEvent.mCodeNameIndex != CODE_NAME_INDEX_USE_STRING);
  aKeyEvent.mKeyNameIndex =
      aIsProcessedByIME ? KEY_NAME_INDEX_Process
                        : keymapWrapper->ComputeDOMKeyNameIndex(aGdkKeyEvent);
  if (aKeyEvent.mKeyNameIndex == KEY_NAME_INDEX_Unidentified) {
    uint32_t charCode = GetCharCodeFor(aGdkKeyEvent);
    if (!charCode) {
      charCode = keymapWrapper->GetUnmodifiedCharCodeFor(aGdkKeyEvent);
    }
    if (charCode) {
      aKeyEvent.mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
      MOZ_ASSERT(aKeyEvent.mKeyValue.IsEmpty(),
                 "Uninitialized mKeyValue must be empty");
      AppendUCS4ToUTF16(charCode, aKeyEvent.mKeyValue);
    }
  }

  if (aIsProcessedByIME) {
    aKeyEvent.mKeyCode = NS_VK_PROCESSKEY;
  } else if (aKeyEvent.mKeyNameIndex != KEY_NAME_INDEX_USE_STRING ||
             aKeyEvent.mMessage != eKeyPress) {
    aKeyEvent.mKeyCode = ComputeDOMKeyCode(aGdkKeyEvent);
  } else {
    aKeyEvent.mKeyCode = 0;
  }

  const guint modifierState = GetModifierState(aGdkKeyEvent, keymapWrapper);
  InitInputEvent(aKeyEvent, modifierState);

  switch (aGdkKeyEvent->keyval) {
    case GDK_Shift_L:
    case GDK_Control_L:
    case GDK_Alt_L:
    case GDK_Super_L:
    case GDK_Hyper_L:
    case GDK_Meta_L:
      aKeyEvent.mLocation = eKeyLocationLeft;
      break;

    case GDK_Shift_R:
    case GDK_Control_R:
    case GDK_Alt_R:
    case GDK_Super_R:
    case GDK_Hyper_R:
    case GDK_Meta_R:
      aKeyEvent.mLocation = eKeyLocationRight;
      break;

    case GDK_KP_0:
    case GDK_KP_1:
    case GDK_KP_2:
    case GDK_KP_3:
    case GDK_KP_4:
    case GDK_KP_5:
    case GDK_KP_6:
    case GDK_KP_7:
    case GDK_KP_8:
    case GDK_KP_9:
    case GDK_KP_Space:
    case GDK_KP_Tab:
    case GDK_KP_Enter:
    case GDK_KP_F1:
    case GDK_KP_F2:
    case GDK_KP_F3:
    case GDK_KP_F4:
    case GDK_KP_Home:
    case GDK_KP_Left:
    case GDK_KP_Up:
    case GDK_KP_Right:
    case GDK_KP_Down:
    case GDK_KP_Prior:  // same as GDK_KP_Page_Up
    case GDK_KP_Next:   // same as GDK_KP_Page_Down
    case GDK_KP_End:
    case GDK_KP_Begin:
    case GDK_KP_Insert:
    case GDK_KP_Delete:
    case GDK_KP_Equal:
    case GDK_KP_Multiply:
    case GDK_KP_Add:
    case GDK_KP_Separator:
    case GDK_KP_Subtract:
    case GDK_KP_Decimal:
    case GDK_KP_Divide:
      aKeyEvent.mLocation = eKeyLocationNumpad;
      break;

    default:
      aKeyEvent.mLocation = eKeyLocationStandard;
      break;
  }

  // The transformations above and in gdk for the keyval are not invertible
  // so link to the GdkEvent (which will vanish soon after return from the
  // event callback) to give plugins access to hardware_keycode and state.
  // (An XEvent would be nice but the GdkEvent is good enough.)
  aKeyEvent.mNativeKeyEvent = static_cast<void*>(aGdkKeyEvent);
  aKeyEvent.mIsRepeat =
      sRepeatState == REPEATING &&
      aGdkKeyEvent->hardware_keycode == sLastRepeatableHardwareKeyCode;

  MOZ_LOG(
      gKeyLog, LogLevel::Info,
      ("%p InitKeyEvent, modifierState=0x%08X "
       "aKeyEvent={ mMessage=%s, isShift=%s, isControl=%s, "
       "isAlt=%s, isMeta=%s , mKeyCode=0x%02X, mCharCode=%s, "
       "mKeyNameIndex=%s, mKeyValue=%s, mCodeNameIndex=%s, mCodeValue=%s, "
       "mLocation=%s, mIsRepeat=%s }",
       keymapWrapper, modifierState, ToChar(aKeyEvent.mMessage),
       GetBoolName(aKeyEvent.IsShift()), GetBoolName(aKeyEvent.IsControl()),
       GetBoolName(aKeyEvent.IsAlt()), GetBoolName(aKeyEvent.IsMeta()),
       aKeyEvent.mKeyCode,
       GetCharacterCodeName(static_cast<char16_t>(aKeyEvent.mCharCode)).get(),
       ToString(aKeyEvent.mKeyNameIndex).get(),
       GetCharacterCodeNames(aKeyEvent.mKeyValue).get(),
       ToString(aKeyEvent.mCodeNameIndex).get(),
       GetCharacterCodeNames(aKeyEvent.mCodeValue).get(),
       GetKeyLocationName(aKeyEvent.mLocation).get(),
       GetBoolName(aKeyEvent.mIsRepeat)));
}

/* static */
uint32_t KeymapWrapper::GetCharCodeFor(const GdkEventKey* aGdkKeyEvent) {
  // Anything above 0xf000 is considered a non-printable
  // Exception: directly encoded UCS characters
  if (aGdkKeyEvent->keyval > 0xf000 &&
      (aGdkKeyEvent->keyval & 0xff000000) != 0x01000000) {
    // Keypad keys are an exception: they return a value different
    // from their non-keypad equivalents, but mozilla doesn't distinguish.
    switch (aGdkKeyEvent->keyval) {
      case GDK_KP_Space:
        return ' ';
      case GDK_KP_Equal:
        return '=';
      case GDK_KP_Multiply:
        return '*';
      case GDK_KP_Add:
        return '+';
      case GDK_KP_Separator:
        return ',';
      case GDK_KP_Subtract:
        return '-';
      case GDK_KP_Decimal:
        return '.';
      case GDK_KP_Divide:
        return '/';
      case GDK_KP_0:
        return '0';
      case GDK_KP_1:
        return '1';
      case GDK_KP_2:
        return '2';
      case GDK_KP_3:
        return '3';
      case GDK_KP_4:
        return '4';
      case GDK_KP_5:
        return '5';
      case GDK_KP_6:
        return '6';
      case GDK_KP_7:
        return '7';
      case GDK_KP_8:
        return '8';
      case GDK_KP_9:
        return '9';
      default:
        return 0;  // non-printables
    }
  }

  static const long MAX_UNICODE = 0x10FFFF;

  // we're supposedly printable, let's try to convert
  long ucs = keysym2ucs(aGdkKeyEvent->keyval);
  if ((ucs != -1) && (ucs < MAX_UNICODE)) {
    return ucs;
  }

  // I guess we couldn't convert
  return 0;
}

uint32_t KeymapWrapper::GetCharCodeFor(const GdkEventKey* aGdkKeyEvent,
                                       guint aGdkModifierState, gint aGroup) {
  guint keyval;
  if (!gdk_keymap_translate_keyboard_state(
          mGdkKeymap, aGdkKeyEvent->hardware_keycode,
          GdkModifierType(aGdkModifierState), aGroup, &keyval, nullptr, nullptr,
          nullptr)) {
    return 0;
  }
  GdkEventKey tmpEvent = *aGdkKeyEvent;
  tmpEvent.state = aGdkModifierState;
  tmpEvent.keyval = keyval;
  tmpEvent.group = aGroup;
  return GetCharCodeFor(&tmpEvent);
}

uint32_t KeymapWrapper::GetUnmodifiedCharCodeFor(
    const GdkEventKey* aGdkKeyEvent) {
  guint state =
      aGdkKeyEvent->state &
      (GetGdkModifierMask(SHIFT) | GetGdkModifierMask(CAPS_LOCK) |
       GetGdkModifierMask(NUM_LOCK) | GetGdkModifierMask(SCROLL_LOCK) |
       GetGdkModifierMask(LEVEL3) | GetGdkModifierMask(LEVEL5));
  uint32_t charCode =
      GetCharCodeFor(aGdkKeyEvent, GdkModifierType(state), aGdkKeyEvent->group);
  if (charCode) {
    return charCode;
  }
  // If no character is mapped to the key when Level3 Shift or Level5 Shift
  // is active, let's return a character which is inputted by the key without
  // Level3 nor Level5 Shift.
  guint stateWithoutAltGraph =
      state & ~(GetGdkModifierMask(LEVEL3) | GetGdkModifierMask(LEVEL5));
  if (state == stateWithoutAltGraph) {
    return 0;
  }
  return GetCharCodeFor(aGdkKeyEvent, GdkModifierType(stateWithoutAltGraph),
                        aGdkKeyEvent->group);
}

gint KeymapWrapper::GetKeyLevel(GdkEventKey* aGdkKeyEvent) {
  gint level;
  if (!gdk_keymap_translate_keyboard_state(
          mGdkKeymap, aGdkKeyEvent->hardware_keycode,
          GdkModifierType(aGdkKeyEvent->state), aGdkKeyEvent->group, nullptr,
          nullptr, &level, nullptr)) {
    return -1;
  }
  return level;
}

gint KeymapWrapper::GetFirstLatinGroup() {
  GdkKeymapKey* keys;
  gint count;
  gint minGroup = -1;
  if (gdk_keymap_get_entries_for_keyval(mGdkKeymap, GDK_a, &keys, &count)) {
    // find the minimum number group for latin inputtable layout
    for (gint i = 0; i < count && minGroup != 0; ++i) {
      if (keys[i].level != 0 && keys[i].level != 1) {
        continue;
      }
      if (minGroup >= 0 && keys[i].group > minGroup) {
        continue;
      }
      minGroup = keys[i].group;
    }
    g_free(keys);
  }
  return minGroup;
}

bool KeymapWrapper::IsLatinGroup(guint8 aGroup) {
  GdkKeymapKey* keys;
  gint count;
  bool result = false;
  if (gdk_keymap_get_entries_for_keyval(mGdkKeymap, GDK_a, &keys, &count)) {
    for (gint i = 0; i < count; ++i) {
      if (keys[i].level != 0 && keys[i].level != 1) {
        continue;
      }
      if (keys[i].group == aGroup) {
        result = true;
        break;
      }
    }
    g_free(keys);
  }
  return result;
}

bool KeymapWrapper::IsAutoRepeatableKey(guint aHardwareKeyCode) {
#ifdef MOZ_X11
  uint8_t indexOfArray = aHardwareKeyCode / 8;
  MOZ_ASSERT(indexOfArray < ArrayLength(mKeyboardState.auto_repeats),
             "invalid index");
  char bitMask = 1 << (aHardwareKeyCode % 8);
  return (mKeyboardState.auto_repeats[indexOfArray] & bitMask) != 0;
#else
  return false;
#endif
}

/* static */
bool KeymapWrapper::IsBasicLatinLetterOrNumeral(uint32_t aCharCode) {
  return (aCharCode >= 'a' && aCharCode <= 'z') ||
         (aCharCode >= 'A' && aCharCode <= 'Z') ||
         (aCharCode >= '0' && aCharCode <= '9');
}

/* static */
guint KeymapWrapper::GetGDKKeyvalWithoutModifier(
    const GdkEventKey* aGdkKeyEvent) {
  KeymapWrapper* keymapWrapper = GetInstance();
  guint state =
      (aGdkKeyEvent->state & keymapWrapper->GetGdkModifierMask(NUM_LOCK));
  guint keyval;
  if (!gdk_keymap_translate_keyboard_state(
          keymapWrapper->mGdkKeymap, aGdkKeyEvent->hardware_keycode,
          GdkModifierType(state), aGdkKeyEvent->group, &keyval, nullptr,
          nullptr, nullptr)) {
    return 0;
  }
  return keyval;
}

/* static */
uint32_t KeymapWrapper::GetDOMKeyCodeFromKeyPairs(guint aGdkKeyval) {
  switch (aGdkKeyval) {
    case GDK_Cancel:
      return NS_VK_CANCEL;
    case GDK_BackSpace:
      return NS_VK_BACK;
    case GDK_Tab:
    case GDK_ISO_Left_Tab:
      return NS_VK_TAB;
    case GDK_Clear:
      return NS_VK_CLEAR;
    case GDK_Return:
      return NS_VK_RETURN;
    case GDK_Shift_L:
    case GDK_Shift_R:
    case GDK_Shift_Lock:
      return NS_VK_SHIFT;
    case GDK_Control_L:
    case GDK_Control_R:
      return NS_VK_CONTROL;
    case GDK_Alt_L:
    case GDK_Alt_R:
      return NS_VK_ALT;
    case GDK_Meta_L:
    case GDK_Meta_R:
      return NS_VK_META;

    // Assume that Super or Hyper is always mapped to physical Win key.
    case GDK_Super_L:
    case GDK_Super_R:
    case GDK_Hyper_L:
    case GDK_Hyper_R:
      return NS_VK_WIN;

    // GTK's AltGraph key is similar to Mac's Option (Alt) key.  However,
    // unfortunately, browsers on Mac are using NS_VK_ALT for it even though
    // it's really different from Alt key on Windows.
    // On the other hand, GTK's AltGrapsh keys are really different from
    // Alt key.  However, there is no AltGrapsh key on Windows.  On Windows,
    // both Ctrl and Alt keys are pressed internally when AltGr key is
    // pressed.  For some languages' users, AltGraph key is important, so,
    // web applications on such locale may want to know AltGraph key press.
    // Therefore, we should map AltGr keycode for them only on GTK.
    case GDK_ISO_Level3_Shift:
    case GDK_ISO_Level5_Shift:
    // We assume that Mode_switch is always used for level3 shift.
    case GDK_Mode_switch:
      return NS_VK_ALTGR;

    case GDK_Pause:
      return NS_VK_PAUSE;
    case GDK_Caps_Lock:
      return NS_VK_CAPS_LOCK;
    case GDK_Kana_Lock:
    case GDK_Kana_Shift:
      return NS_VK_KANA;
    case GDK_Hangul:
      return NS_VK_HANGUL;
    // case GDK_XXX:                   return NS_VK_JUNJA;
    // case GDK_XXX:                   return NS_VK_FINAL;
    case GDK_Hangul_Hanja:
      return NS_VK_HANJA;
    case GDK_Kanji:
      return NS_VK_KANJI;
    case GDK_Escape:
      return NS_VK_ESCAPE;
    case GDK_Henkan:
      return NS_VK_CONVERT;
    case GDK_Muhenkan:
      return NS_VK_NONCONVERT;
    // case GDK_XXX:                   return NS_VK_ACCEPT;
    // case GDK_XXX:                   return NS_VK_MODECHANGE;
    case GDK_Page_Up:
      return NS_VK_PAGE_UP;
    case GDK_Page_Down:
      return NS_VK_PAGE_DOWN;
    case GDK_End:
      return NS_VK_END;
    case GDK_Home:
      return NS_VK_HOME;
    case GDK_Left:
      return NS_VK_LEFT;
    case GDK_Up:
      return NS_VK_UP;
    case GDK_Right:
      return NS_VK_RIGHT;
    case GDK_Down:
      return NS_VK_DOWN;
    case GDK_Select:
      return NS_VK_SELECT;
    case GDK_Print:
      return NS_VK_PRINT;
    case GDK_Execute:
      return NS_VK_EXECUTE;
    case GDK_Insert:
      return NS_VK_INSERT;
    case GDK_Delete:
      return NS_VK_DELETE;
    case GDK_Help:
      return NS_VK_HELP;

    // keypad keys
    case GDK_KP_Left:
      return NS_VK_LEFT;
    case GDK_KP_Right:
      return NS_VK_RIGHT;
    case GDK_KP_Up:
      return NS_VK_UP;
    case GDK_KP_Down:
      return NS_VK_DOWN;
    case GDK_KP_Page_Up:
      return NS_VK_PAGE_UP;
    // Not sure what these are
    // case GDK_KP_Prior:              return NS_VK_;
    // case GDK_KP_Next:               return NS_VK_;
    case GDK_KP_Begin:
      return NS_VK_CLEAR;  // Num-unlocked 5
    case GDK_KP_Page_Down:
      return NS_VK_PAGE_DOWN;
    case GDK_KP_Home:
      return NS_VK_HOME;
    case GDK_KP_End:
      return NS_VK_END;
    case GDK_KP_Insert:
      return NS_VK_INSERT;
    case GDK_KP_Delete:
      return NS_VK_DELETE;
    case GDK_KP_Enter:
      return NS_VK_RETURN;

    case GDK_Num_Lock:
      return NS_VK_NUM_LOCK;
    case GDK_Scroll_Lock:
      return NS_VK_SCROLL_LOCK;

    // Function keys
    case GDK_F1:
      return NS_VK_F1;
    case GDK_F2:
      return NS_VK_F2;
    case GDK_F3:
      return NS_VK_F3;
    case GDK_F4:
      return NS_VK_F4;
    case GDK_F5:
      return NS_VK_F5;
    case GDK_F6:
      return NS_VK_F6;
    case GDK_F7:
      return NS_VK_F7;
    case GDK_F8:
      return NS_VK_F8;
    case GDK_F9:
      return NS_VK_F9;
    case GDK_F10:
      return NS_VK_F10;
    case GDK_F11:
      return NS_VK_F11;
    case GDK_F12:
      return NS_VK_F12;
    case GDK_F13:
      return NS_VK_F13;
    case GDK_F14:
      return NS_VK_F14;
    case GDK_F15:
      return NS_VK_F15;
    case GDK_F16:
      return NS_VK_F16;
    case GDK_F17:
      return NS_VK_F17;
    case GDK_F18:
      return NS_VK_F18;
    case GDK_F19:
      return NS_VK_F19;
    case GDK_F20:
      return NS_VK_F20;
    case GDK_F21:
      return NS_VK_F21;
    case GDK_F22:
      return NS_VK_F22;
    case GDK_F23:
      return NS_VK_F23;
    case GDK_F24:
      return NS_VK_F24;

    // context menu key, keysym 0xff67, typically keycode 117 on 105-key
    // (Microsoft) x86 keyboards, located between right 'Windows' key and
    // right Ctrl key
    case GDK_Menu:
      return NS_VK_CONTEXT_MENU;
    case GDK_Sleep:
      return NS_VK_SLEEP;

    case GDK_3270_Attn:
      return NS_VK_ATTN;
    case GDK_3270_CursorSelect:
      return NS_VK_CRSEL;
    case GDK_3270_ExSelect:
      return NS_VK_EXSEL;
    case GDK_3270_EraseEOF:
      return NS_VK_EREOF;
    case GDK_3270_Play:
      return NS_VK_PLAY;
    // case GDK_XXX:                   return NS_VK_ZOOM;
    case GDK_3270_PA1:
      return NS_VK_PA1;

    // map Sun Keyboard special keysyms on to NS_VK keys

    // Sun F11 key generates SunF36(0x1005ff10) keysym
    case 0x1005ff10:
      return NS_VK_F11;
    // Sun F12 key generates SunF37(0x1005ff11) keysym
    case 0x1005ff11:
      return NS_VK_F12;
    default:
      return 0;
  }
}

void KeymapWrapper::WillDispatchKeyboardEvent(WidgetKeyboardEvent& aKeyEvent,
                                              GdkEventKey* aGdkKeyEvent) {
  GetInstance()->WillDispatchKeyboardEventInternal(aKeyEvent, aGdkKeyEvent);
}

void KeymapWrapper::WillDispatchKeyboardEventInternal(
    WidgetKeyboardEvent& aKeyEvent, GdkEventKey* aGdkKeyEvent) {
  if (!aGdkKeyEvent) {
    // If aGdkKeyEvent is nullptr, we're trying to dispatch a fake keyboard
    // event in such case, we don't need to set alternative char codes.
    // So, we don't need to do nothing here.  This case is typically we're
    // dispatching eKeyDown or eKeyUp event during composition.
    return;
  }

  uint32_t charCode = GetCharCodeFor(aGdkKeyEvent);
  if (!charCode) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p WillDispatchKeyboardEventInternal, "
             "mKeyCode=0x%02X, charCode=0x%08X",
             this, aKeyEvent.mKeyCode, aKeyEvent.mCharCode));
    return;
  }

  // The mCharCode was set from mKeyValue. However, for example, when Ctrl key
  // is pressed, its value should indicate an ASCII character for backward
  // compatibility rather than inputting character without the modifiers.
  // Therefore, we need to modify mCharCode value here.
  aKeyEvent.SetCharCode(charCode);

  gint level = GetKeyLevel(aGdkKeyEvent);
  if (level != 0 && level != 1) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p WillDispatchKeyboardEventInternal, "
             "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d",
             this, aKeyEvent.mKeyCode, aKeyEvent.mCharCode, level));
    return;
  }

  guint baseState = aGdkKeyEvent->state &
                    ~(GetGdkModifierMask(SHIFT) | GetGdkModifierMask(CTRL) |
                      GetGdkModifierMask(ALT) | GetGdkModifierMask(META) |
                      GetGdkModifierMask(SUPER) | GetGdkModifierMask(HYPER));

  // We shold send both shifted char and unshifted char, all keyboard layout
  // users can use all keys.  Don't change event.mCharCode. On some keyboard
  // layouts, Ctrl/Alt/Meta keys are used for inputting some characters.
  AlternativeCharCode altCharCodes(0, 0);
  // unshifted charcode of current keyboard layout.
  altCharCodes.mUnshiftedCharCode =
      GetCharCodeFor(aGdkKeyEvent, baseState, aGdkKeyEvent->group);
  bool isLatin = (altCharCodes.mUnshiftedCharCode <= 0xFF);
  // shifted charcode of current keyboard layout.
  altCharCodes.mShiftedCharCode = GetCharCodeFor(
      aGdkKeyEvent, baseState | GetGdkModifierMask(SHIFT), aGdkKeyEvent->group);
  isLatin = isLatin && (altCharCodes.mShiftedCharCode <= 0xFF);
  if (altCharCodes.mUnshiftedCharCode || altCharCodes.mShiftedCharCode) {
    aKeyEvent.mAlternativeCharCodes.AppendElement(altCharCodes);
  }

  bool needLatinKeyCodes = !isLatin;
  if (!needLatinKeyCodes) {
    needLatinKeyCodes =
        (IS_ASCII_ALPHABETICAL(altCharCodes.mUnshiftedCharCode) !=
         IS_ASCII_ALPHABETICAL(altCharCodes.mShiftedCharCode));
  }

  // If current keyboard layout can input Latin characters, we don't need
  // more information.
  if (!needLatinKeyCodes) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p WillDispatchKeyboardEventInternal, "
             "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d, altCharCodes={ "
             "mUnshiftedCharCode=0x%08X, mShiftedCharCode=0x%08X }",
             this, aKeyEvent.mKeyCode, aKeyEvent.mCharCode, level,
             altCharCodes.mUnshiftedCharCode, altCharCodes.mShiftedCharCode));
    return;
  }

  // Next, find Latin inputtable keyboard layout.
  gint minGroup = GetFirstLatinGroup();
  if (minGroup < 0) {
    MOZ_LOG(gKeyLog, LogLevel::Info,
            ("%p WillDispatchKeyboardEventInternal, "
             "Latin keyboard layout isn't found: "
             "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d, "
             "altCharCodes={ mUnshiftedCharCode=0x%08X, "
             "mShiftedCharCode=0x%08X }",
             this, aKeyEvent.mKeyCode, aKeyEvent.mCharCode, level,
             altCharCodes.mUnshiftedCharCode, altCharCodes.mShiftedCharCode));
    return;
  }

  AlternativeCharCode altLatinCharCodes(0, 0);
  uint32_t unmodifiedCh = aKeyEvent.IsShift() ? altCharCodes.mShiftedCharCode
                                              : altCharCodes.mUnshiftedCharCode;

  // unshifted charcode of found keyboard layout.
  uint32_t ch = GetCharCodeFor(aGdkKeyEvent, baseState, minGroup);
  altLatinCharCodes.mUnshiftedCharCode =
      IsBasicLatinLetterOrNumeral(ch) ? ch : 0;
  // shifted charcode of found keyboard layout.
  ch = GetCharCodeFor(aGdkKeyEvent, baseState | GetGdkModifierMask(SHIFT),
                      minGroup);
  altLatinCharCodes.mShiftedCharCode = IsBasicLatinLetterOrNumeral(ch) ? ch : 0;
  if (altLatinCharCodes.mUnshiftedCharCode ||
      altLatinCharCodes.mShiftedCharCode) {
    aKeyEvent.mAlternativeCharCodes.AppendElement(altLatinCharCodes);
  }
  // If the mCharCode is not Latin, and the level is 0 or 1, we should
  // replace the mCharCode to Latin char if Alt keys are not pressed. (Alt
  // should be sent the localized char for accesskey like handling of Web
  // Applications.)
  ch = aKeyEvent.IsShift() ? altLatinCharCodes.mShiftedCharCode
                           : altLatinCharCodes.mUnshiftedCharCode;
  if (ch && !aKeyEvent.IsAlt() && charCode == unmodifiedCh) {
    aKeyEvent.SetCharCode(ch);
  }

  MOZ_LOG(gKeyLog, LogLevel::Info,
          ("%p WillDispatchKeyboardEventInternal, "
           "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d, minGroup=%d, "
           "altCharCodes={ mUnshiftedCharCode=0x%08X, "
           "mShiftedCharCode=0x%08X } "
           "altLatinCharCodes={ mUnshiftedCharCode=0x%08X, "
           "mShiftedCharCode=0x%08X }",
           this, aKeyEvent.mKeyCode, aKeyEvent.mCharCode, level, minGroup,
           altCharCodes.mUnshiftedCharCode, altCharCodes.mShiftedCharCode,
           altLatinCharCodes.mUnshiftedCharCode,
           altLatinCharCodes.mShiftedCharCode));
}

#ifdef MOZ_WAYLAND
void KeymapWrapper::SetFocusIn(wl_surface* aFocusSurface,
                               uint32_t aFocusSerial) {
  LOGW("KeymapWrapper::SetFocusIn() surface %p ID %d serial %d", aFocusSurface,
       aFocusSurface ? wl_proxy_get_id((struct wl_proxy*)aFocusSurface) : 0,
       aFocusSerial);

  KeymapWrapper* keymapWrapper = KeymapWrapper::GetInstance();
  keymapWrapper->mFocusSurface = aFocusSurface;
  keymapWrapper->mFocusSerial = aFocusSerial;
}

// aFocusSurface can be null in case that focused surface is already destroyed.
void KeymapWrapper::SetFocusOut(wl_surface* aFocusSurface) {
  KeymapWrapper* keymapWrapper = KeymapWrapper::GetInstance();
  LOGW("KeymapWrapper::SetFocusOut surface %p ID %d", aFocusSurface,
       aFocusSurface ? wl_proxy_get_id((struct wl_proxy*)aFocusSurface) : 0);

  keymapWrapper->mFocusSurface = nullptr;
  keymapWrapper->mFocusSerial = 0;
}

void KeymapWrapper::GetFocusInfo(wl_surface** aFocusSurface,
                                 uint32_t* aFocusSerial) {
  KeymapWrapper* keymapWrapper = KeymapWrapper::GetInstance();
  *aFocusSurface = keymapWrapper->mFocusSurface;
  *aFocusSerial = keymapWrapper->mFocusSerial;
}

void KeymapWrapper::SetSeat(wl_seat* aSeat, int aId) {
  sSeat = aSeat;
  sSeatID = aId;
  wl_seat_add_listener(aSeat, &seat_listener, nullptr);
}

void KeymapWrapper::ClearSeat(int aId) {
  if (sSeatID == aId) {
    ClearKeyboard();
    sSeat = nullptr;
    sSeatID = -1;
  }
}

wl_seat* KeymapWrapper::GetSeat() { return sSeat; }

void KeymapWrapper::SetKeyboard(wl_keyboard* aKeyboard) {
  sKeyboard = aKeyboard;
}

wl_keyboard* KeymapWrapper::GetKeyboard() { return sKeyboard; }

void KeymapWrapper::ClearKeyboard() {
  if (sKeyboard) {
    wl_keyboard_destroy(sKeyboard);
    sKeyboard = nullptr;
  }
}
#endif

}  // namespace widget
}  // namespace mozilla