forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsGtkKeyUtils.cpp
2488 lines (2269 loc) · 86 KB
/
nsGtkKeyUtils.cpp
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
/* -*- 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;
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(Modifier 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::Modifier 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::GetModifierMask(Modifier 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 mModifierMasks[INDEX_SUPER];
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->Init();
return sInstance;
}
sInstance = new KeymapWrapper();
return sInstance;
}
#ifdef MOZ_WAYLAND
void KeymapWrapper::EnsureInstance() { (void)GetInstance(); }
#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
Init();
}
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, GetModifierMask(CAPS_LOCK), GetModifierMask(NUM_LOCK),
GetModifierMask(SCROLL_LOCK), GetModifierMask(LEVEL3),
GetModifierMask(LEVEL5), GetModifierMask(SHIFT),
GetModifierMask(CTRL), GetModifierMask(ALT), GetModifierMask(META),
GetModifierMask(SUPER), GetModifierMask(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.
Modifier 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++) {
Modifier 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:
// 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++) {
Modifier 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_SUPER:
modifier = SUPER;
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_SUPER, "Super");
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->GetModifierMask(CAPS_LOCK),
keymapWrapper->GetModifierMask(NUM_LOCK),
keymapWrapper->GetModifierMask(SCROLL_LOCK),
keymapWrapper->GetModifierMask(LEVEL3),
keymapWrapper->GetModifierMask(LEVEL5),
keymapWrapper->GetModifierMask(SHIFT),
keymapWrapper->GetModifierMask(CTRL),
keymapWrapper->GetModifierMask(ALT),
keymapWrapper->GetModifierMask(META),
keymapWrapper->GetModifierMask(SUPER),
keymapWrapper->GetModifierMask(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 const struct wl_keyboard_listener keyboard_listener = {
keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave,
keyboard_handle_key, keyboard_handle_modifiers,
};
static void seat_handle_capabilities(void* data, struct wl_seat* seat,
unsigned int caps) {
static wl_keyboard* keyboard = nullptr;
if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !keyboard) {
keyboard = wl_seat_get_keyboard(seat);
wl_keyboard_add_listener(keyboard, &keyboard_listener, nullptr);
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && keyboard) {
wl_keyboard_destroy(keyboard);
keyboard = nullptr;
}
}
static const struct wl_seat_listener seat_listener = {
seat_handle_capabilities,
};
static void gdk_registry_handle_global(void* data, struct wl_registry* registry,
uint32_t id, const char* interface,
uint32_t version) {
if (strcmp(interface, "wl_seat") == 0) {
auto* seat =
WaylandRegistryBind<wl_seat>(registry, id, &wl_seat_interface, 1);
KeymapWrapper::SetSeat(seat);
wl_seat_add_listener(seat, &seat_listener, data);
}
}
static void gdk_registry_handle_global_remove(void* data,
struct wl_registry* registry,
uint32_t id) {}
static const struct wl_registry_listener keyboard_registry_listener = {
gdk_registry_handle_global, gdk_registry_handle_global_remove};
void KeymapWrapper::InitBySystemSettingsWayland() {
wl_display* display = WaylandDisplayGetWLDisplay();
wl_registry_add_listener(wl_display_get_registry(display),
&keyboard_registry_listener, this);
}
#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() {
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::AreModifiersCurrentlyActive(Modifiers aModifiers) {
guint modifierState = GetCurrentModifierState();
return AreModifiersActive(aModifiers, modifierState);
}
/* static */
bool KeymapWrapper::AreModifiersActive(Modifiers aModifiers,
guint aModifierState) {