Bug 1769547 - Do not MOZ_CRASH() on missing process r=nika
[gecko.git] / widget / gtk / nsGtkKeyUtils.cpp
blob212101262652622b5ad0a519e368f00920642481
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "mozilla/Logging.h"
10 #include "nsGtkKeyUtils.h"
12 #include <gdk/gdkkeysyms.h>
13 #include <algorithm>
14 #include <gdk/gdk.h>
15 #include <dlfcn.h>
16 #include <gdk/gdkkeysyms-compat.h>
17 #ifdef MOZ_X11
18 # include <gdk/gdkx.h>
19 # include <X11/XKBlib.h>
20 # include "X11UndefineNone.h"
21 #endif
22 #include "IMContextWrapper.h"
23 #include "WidgetUtils.h"
24 #include "WidgetUtilsGtk.h"
25 #include "x11/keysym2ucs.h"
26 #include "nsContentUtils.h"
27 #include "nsGtkUtils.h"
28 #include "nsIBidiKeyboard.h"
29 #include "nsPrintfCString.h"
30 #include "nsReadableUtils.h"
31 #include "nsServiceManagerUtils.h"
32 #include "nsWindow.h"
34 #include "mozilla/ArrayUtils.h"
35 #include "mozilla/MouseEvents.h"
36 #include "mozilla/TextEventDispatcher.h"
37 #include "mozilla/TextEvents.h"
39 #ifdef MOZ_WAYLAND
40 # include <sys/mman.h>
41 # include "nsWaylandDisplay.h"
42 #endif
44 // For collecting other people's log, tell them `MOZ_LOG=KeyboardHandler:4,sync`
45 // rather than `MOZ_LOG=KeyboardHandler:5,sync` since using `5` may create too
46 // big file.
47 // Therefore you shouldn't use `LogLevel::Verbose` for logging usual behavior.
48 mozilla::LazyLogModule gKeyLog("KeyboardHandler");
50 namespace mozilla {
51 namespace widget {
53 #define IS_ASCII_ALPHABETICAL(key) \
54 ((('a' <= key) && (key <= 'z')) || (('A' <= key) && (key <= 'Z')))
56 #define MOZ_MODIFIER_KEYS "MozKeymapWrapper"
58 KeymapWrapper* KeymapWrapper::sInstance = nullptr;
59 guint KeymapWrapper::sLastRepeatableHardwareKeyCode = 0;
60 #ifdef MOZ_X11
61 Time KeymapWrapper::sLastRepeatableKeyTime = 0;
62 #endif
63 KeymapWrapper::RepeatState KeymapWrapper::sRepeatState =
64 KeymapWrapper::NOT_PRESSED;
66 static const char* GetBoolName(bool aBool) { return aBool ? "TRUE" : "FALSE"; }
68 static const char* GetStatusName(nsEventStatus aStatus) {
69 switch (aStatus) {
70 case nsEventStatus_eConsumeDoDefault:
71 return "nsEventStatus_eConsumeDoDefault";
72 case nsEventStatus_eConsumeNoDefault:
73 return "nsEventStatus_eConsumeNoDefault";
74 case nsEventStatus_eIgnore:
75 return "nsEventStatus_eIgnore";
76 case nsEventStatus_eSentinel:
77 return "nsEventStatus_eSentinel";
78 default:
79 return "Illegal value";
83 static const nsCString GetKeyLocationName(uint32_t aLocation) {
84 switch (aLocation) {
85 case eKeyLocationLeft:
86 return "KEY_LOCATION_LEFT"_ns;
87 case eKeyLocationRight:
88 return "KEY_LOCATION_RIGHT"_ns;
89 case eKeyLocationStandard:
90 return "KEY_LOCATION_STANDARD"_ns;
91 case eKeyLocationNumpad:
92 return "KEY_LOCATION_NUMPAD"_ns;
93 default:
94 return nsPrintfCString("Unknown (0x%04X)", aLocation);
98 static const nsCString GetCharacterCodeName(char16_t aChar) {
99 switch (aChar) {
100 case 0x0000:
101 return "NULL (0x0000)"_ns;
102 case 0x0008:
103 return "BACKSPACE (0x0008)"_ns;
104 case 0x0009:
105 return "CHARACTER TABULATION (0x0009)"_ns;
106 case 0x000A:
107 return "LINE FEED (0x000A)"_ns;
108 case 0x000B:
109 return "LINE TABULATION (0x000B)"_ns;
110 case 0x000C:
111 return "FORM FEED (0x000C)"_ns;
112 case 0x000D:
113 return "CARRIAGE RETURN (0x000D)"_ns;
114 case 0x0018:
115 return "CANCEL (0x0018)"_ns;
116 case 0x001B:
117 return "ESCAPE (0x001B)"_ns;
118 case 0x0020:
119 return "SPACE (0x0020)"_ns;
120 case 0x007F:
121 return "DELETE (0x007F)"_ns;
122 case 0x00A0:
123 return "NO-BREAK SPACE (0x00A0)"_ns;
124 case 0x00AD:
125 return "SOFT HYPHEN (0x00AD)"_ns;
126 case 0x2000:
127 return "EN QUAD (0x2000)"_ns;
128 case 0x2001:
129 return "EM QUAD (0x2001)"_ns;
130 case 0x2002:
131 return "EN SPACE (0x2002)"_ns;
132 case 0x2003:
133 return "EM SPACE (0x2003)"_ns;
134 case 0x2004:
135 return "THREE-PER-EM SPACE (0x2004)"_ns;
136 case 0x2005:
137 return "FOUR-PER-EM SPACE (0x2005)"_ns;
138 case 0x2006:
139 return "SIX-PER-EM SPACE (0x2006)"_ns;
140 case 0x2007:
141 return "FIGURE SPACE (0x2007)"_ns;
142 case 0x2008:
143 return "PUNCTUATION SPACE (0x2008)"_ns;
144 case 0x2009:
145 return "THIN SPACE (0x2009)"_ns;
146 case 0x200A:
147 return "HAIR SPACE (0x200A)"_ns;
148 case 0x200B:
149 return "ZERO WIDTH SPACE (0x200B)"_ns;
150 case 0x200C:
151 return "ZERO WIDTH NON-JOINER (0x200C)"_ns;
152 case 0x200D:
153 return "ZERO WIDTH JOINER (0x200D)"_ns;
154 case 0x200E:
155 return "LEFT-TO-RIGHT MARK (0x200E)"_ns;
156 case 0x200F:
157 return "RIGHT-TO-LEFT MARK (0x200F)"_ns;
158 case 0x2029:
159 return "PARAGRAPH SEPARATOR (0x2029)"_ns;
160 case 0x202A:
161 return "LEFT-TO-RIGHT EMBEDDING (0x202A)"_ns;
162 case 0x202B:
163 return "RIGHT-TO-LEFT EMBEDDING (0x202B)"_ns;
164 case 0x202D:
165 return "LEFT-TO-RIGHT OVERRIDE (0x202D)"_ns;
166 case 0x202E:
167 return "RIGHT-TO-LEFT OVERRIDE (0x202E)"_ns;
168 case 0x202F:
169 return "NARROW NO-BREAK SPACE (0x202F)"_ns;
170 case 0x205F:
171 return "MEDIUM MATHEMATICAL SPACE (0x205F)"_ns;
172 case 0x2060:
173 return "WORD JOINER (0x2060)"_ns;
174 case 0x2066:
175 return "LEFT-TO-RIGHT ISOLATE (0x2066)"_ns;
176 case 0x2067:
177 return "RIGHT-TO-LEFT ISOLATE (0x2067)"_ns;
178 case 0x3000:
179 return "IDEOGRAPHIC SPACE (0x3000)"_ns;
180 case 0xFEFF:
181 return "ZERO WIDTH NO-BREAK SPACE (0xFEFF)"_ns;
182 default: {
183 if (aChar < ' ' || (aChar >= 0x80 && aChar < 0xA0)) {
184 return nsPrintfCString("control (0x%04X)", aChar);
186 if (NS_IS_HIGH_SURROGATE(aChar)) {
187 return nsPrintfCString("high surrogate (0x%04X)", aChar);
189 if (NS_IS_LOW_SURROGATE(aChar)) {
190 return nsPrintfCString("low surrogate (0x%04X)", aChar);
192 return nsPrintfCString("'%s' (0x%04X)",
193 NS_ConvertUTF16toUTF8(nsAutoString(aChar)).get(),
194 aChar);
199 static const nsCString GetCharacterCodeNames(const char16_t* aChars,
200 uint32_t aLength) {
201 if (!aLength) {
202 return "\"\""_ns;
204 nsCString result;
205 result.AssignLiteral("\"");
206 StringJoinAppend(result, ", "_ns, Span{aChars, aLength},
207 [](nsACString& dest, const char16_t charValue) {
208 dest.Append(GetCharacterCodeName(charValue));
210 result.AppendLiteral("\"");
211 return result;
214 static const nsCString GetCharacterCodeNames(const nsAString& aString) {
215 return GetCharacterCodeNames(aString.BeginReading(), aString.Length());
218 /* static */ const char* KeymapWrapper::GetModifierName(Modifier aModifier) {
219 switch (aModifier) {
220 case CAPS_LOCK:
221 return "CapsLock";
222 case NUM_LOCK:
223 return "NumLock";
224 case SCROLL_LOCK:
225 return "ScrollLock";
226 case SHIFT:
227 return "Shift";
228 case CTRL:
229 return "Ctrl";
230 case ALT:
231 return "Alt";
232 case SUPER:
233 return "Super";
234 case HYPER:
235 return "Hyper";
236 case META:
237 return "Meta";
238 case LEVEL3:
239 return "Level3";
240 case LEVEL5:
241 return "Level5";
242 case NOT_MODIFIER:
243 return "NotModifier";
244 default:
245 return "InvalidValue";
249 /* static */ KeymapWrapper::Modifier KeymapWrapper::GetModifierForGDKKeyval(
250 guint aGdkKeyval) {
251 switch (aGdkKeyval) {
252 case GDK_Caps_Lock:
253 return CAPS_LOCK;
254 case GDK_Num_Lock:
255 return NUM_LOCK;
256 case GDK_Scroll_Lock:
257 return SCROLL_LOCK;
258 case GDK_Shift_Lock:
259 case GDK_Shift_L:
260 case GDK_Shift_R:
261 return SHIFT;
262 case GDK_Control_L:
263 case GDK_Control_R:
264 return CTRL;
265 case GDK_Alt_L:
266 case GDK_Alt_R:
267 return ALT;
268 case GDK_Super_L:
269 case GDK_Super_R:
270 return SUPER;
271 case GDK_Hyper_L:
272 case GDK_Hyper_R:
273 return HYPER;
274 case GDK_Meta_L:
275 case GDK_Meta_R:
276 return META;
277 case GDK_ISO_Level3_Shift:
278 case GDK_Mode_switch:
279 return LEVEL3;
280 case GDK_ISO_Level5_Shift:
281 return LEVEL5;
282 default:
283 return NOT_MODIFIER;
287 guint KeymapWrapper::GetModifierMask(Modifier aModifier) const {
288 switch (aModifier) {
289 case CAPS_LOCK:
290 return GDK_LOCK_MASK;
291 case NUM_LOCK:
292 return mModifierMasks[INDEX_NUM_LOCK];
293 case SCROLL_LOCK:
294 return mModifierMasks[INDEX_SCROLL_LOCK];
295 case SHIFT:
296 return GDK_SHIFT_MASK;
297 case CTRL:
298 return GDK_CONTROL_MASK;
299 case ALT:
300 return mModifierMasks[INDEX_ALT];
301 case SUPER:
302 return mModifierMasks[INDEX_SUPER];
303 case HYPER:
304 return mModifierMasks[INDEX_HYPER];
305 case META:
306 return mModifierMasks[INDEX_META];
307 case LEVEL3:
308 return mModifierMasks[INDEX_LEVEL3];
309 case LEVEL5:
310 return mModifierMasks[INDEX_LEVEL5];
311 default:
312 return 0;
316 KeymapWrapper::ModifierKey* KeymapWrapper::GetModifierKey(
317 guint aHardwareKeycode) {
318 for (uint32_t i = 0; i < mModifierKeys.Length(); i++) {
319 ModifierKey& key = mModifierKeys[i];
320 if (key.mHardwareKeycode == aHardwareKeycode) {
321 return &key;
324 return nullptr;
327 /* static */
328 KeymapWrapper* KeymapWrapper::GetInstance() {
329 if (sInstance) {
330 sInstance->Init();
331 return sInstance;
334 sInstance = new KeymapWrapper();
335 return sInstance;
338 #ifdef MOZ_WAYLAND
339 void KeymapWrapper::EnsureInstance() { (void)GetInstance(); }
340 #endif
342 /* static */
343 void KeymapWrapper::Shutdown() {
344 if (sInstance) {
345 delete sInstance;
346 sInstance = nullptr;
350 KeymapWrapper::KeymapWrapper()
351 : mInitialized(false),
352 mGdkKeymap(gdk_keymap_get_default()),
353 mXKBBaseEventCode(0),
354 mOnKeysChangedSignalHandle(0),
355 mOnDirectionChangedSignalHandle(0) {
356 MOZ_LOG(gKeyLog, LogLevel::Info,
357 ("%p Constructor, mGdkKeymap=%p", this, mGdkKeymap));
359 g_object_ref(mGdkKeymap);
361 #ifdef MOZ_X11
362 if (GdkIsX11Display()) {
363 InitXKBExtension();
365 #endif
367 Init();
370 void KeymapWrapper::Init() {
371 if (mInitialized) {
372 return;
374 mInitialized = true;
376 MOZ_LOG(gKeyLog, LogLevel::Info,
377 ("%p Init, mGdkKeymap=%p", this, mGdkKeymap));
379 mModifierKeys.Clear();
380 memset(mModifierMasks, 0, sizeof(mModifierMasks));
382 #ifdef MOZ_X11
383 if (GdkIsX11Display()) {
384 InitBySystemSettingsX11();
386 #endif
387 #ifdef MOZ_WAYLAND
388 if (GdkIsWaylandDisplay()) {
389 InitBySystemSettingsWayland();
391 #endif
393 #ifdef MOZ_X11
394 gdk_window_add_filter(nullptr, FilterEvents, this);
395 #endif
397 MOZ_LOG(gKeyLog, LogLevel::Info,
398 ("%p Init, CapsLock=0x%X, NumLock=0x%X, "
399 "ScrollLock=0x%X, Level3=0x%X, Level5=0x%X, "
400 "Shift=0x%X, Ctrl=0x%X, Alt=0x%X, Meta=0x%X, Super=0x%X, Hyper=0x%X",
401 this, GetModifierMask(CAPS_LOCK), GetModifierMask(NUM_LOCK),
402 GetModifierMask(SCROLL_LOCK), GetModifierMask(LEVEL3),
403 GetModifierMask(LEVEL5), GetModifierMask(SHIFT),
404 GetModifierMask(CTRL), GetModifierMask(ALT), GetModifierMask(META),
405 GetModifierMask(SUPER), GetModifierMask(HYPER)));
408 #ifdef MOZ_X11
409 void KeymapWrapper::InitXKBExtension() {
410 PodZero(&mKeyboardState);
412 int xkbMajorVer = XkbMajorVersion;
413 int xkbMinorVer = XkbMinorVersion;
414 if (!XkbLibraryVersion(&xkbMajorVer, &xkbMinorVer)) {
415 MOZ_LOG(gKeyLog, LogLevel::Info,
416 ("%p InitXKBExtension failed due to failure of "
417 "XkbLibraryVersion()",
418 this));
419 return;
422 Display* display = gdk_x11_display_get_xdisplay(gdk_display_get_default());
424 // XkbLibraryVersion() set xkbMajorVer and xkbMinorVer to that of the
425 // library, which may be newer than what is required of the server in
426 // XkbQueryExtension(), so these variables should be reset to
427 // XkbMajorVersion and XkbMinorVersion before the XkbQueryExtension call.
428 xkbMajorVer = XkbMajorVersion;
429 xkbMinorVer = XkbMinorVersion;
430 int opcode, baseErrorCode;
431 if (!XkbQueryExtension(display, &opcode, &mXKBBaseEventCode, &baseErrorCode,
432 &xkbMajorVer, &xkbMinorVer)) {
433 MOZ_LOG(gKeyLog, LogLevel::Info,
434 ("%p InitXKBExtension failed due to failure of "
435 "XkbQueryExtension(), display=0x%p",
436 this, display));
437 return;
440 if (!XkbSelectEventDetails(display, XkbUseCoreKbd, XkbStateNotify,
441 XkbModifierStateMask, XkbModifierStateMask)) {
442 MOZ_LOG(gKeyLog, LogLevel::Info,
443 ("%p InitXKBExtension failed due to failure of "
444 "XkbSelectEventDetails() for XModifierStateMask, display=0x%p",
445 this, display));
446 return;
449 if (!XkbSelectEventDetails(display, XkbUseCoreKbd, XkbControlsNotify,
450 XkbPerKeyRepeatMask, XkbPerKeyRepeatMask)) {
451 MOZ_LOG(gKeyLog, LogLevel::Info,
452 ("%p InitXKBExtension failed due to failure of "
453 "XkbSelectEventDetails() for XkbControlsNotify, display=0x%p",
454 this, display));
455 return;
458 if (!XGetKeyboardControl(display, &mKeyboardState)) {
459 MOZ_LOG(gKeyLog, LogLevel::Info,
460 ("%p InitXKBExtension failed due to failure of "
461 "XGetKeyboardControl(), display=0x%p",
462 this, display));
463 return;
466 MOZ_LOG(gKeyLog, LogLevel::Info, ("%p InitXKBExtension, Succeeded", this));
469 void KeymapWrapper::InitBySystemSettingsX11() {
470 MOZ_LOG(gKeyLog, LogLevel::Info,
471 ("%p InitBySystemSettingsX11, mGdkKeymap=%p", this, mGdkKeymap));
473 if (!mOnKeysChangedSignalHandle) {
474 mOnKeysChangedSignalHandle = g_signal_connect(
475 mGdkKeymap, "keys-changed", (GCallback)OnKeysChanged, this);
477 if (!mOnDirectionChangedSignalHandle) {
478 mOnDirectionChangedSignalHandle = g_signal_connect(
479 mGdkKeymap, "direction-changed", (GCallback)OnDirectionChanged, this);
482 Display* display = gdk_x11_display_get_xdisplay(gdk_display_get_default());
484 int min_keycode = 0;
485 int max_keycode = 0;
486 XDisplayKeycodes(display, &min_keycode, &max_keycode);
488 int keysyms_per_keycode = 0;
489 KeySym* xkeymap =
490 XGetKeyboardMapping(display, min_keycode, max_keycode - min_keycode + 1,
491 &keysyms_per_keycode);
492 if (!xkeymap) {
493 MOZ_LOG(gKeyLog, LogLevel::Info,
494 ("%p InitBySystemSettings, "
495 "Failed due to null xkeymap",
496 this));
497 return;
500 XModifierKeymap* xmodmap = XGetModifierMapping(display);
501 if (!xmodmap) {
502 MOZ_LOG(gKeyLog, LogLevel::Info,
503 ("%p InitBySystemSettings, "
504 "Failed due to null xmodmap",
505 this));
506 XFree(xkeymap);
507 return;
509 MOZ_LOG(gKeyLog, LogLevel::Info,
510 ("%p InitBySystemSettings, min_keycode=%d, "
511 "max_keycode=%d, keysyms_per_keycode=%d, max_keypermod=%d",
512 this, min_keycode, max_keycode, keysyms_per_keycode,
513 xmodmap->max_keypermod));
515 // The modifiermap member of the XModifierKeymap structure contains 8 sets
516 // of max_keypermod KeyCodes, one for each modifier in the order Shift,
517 // Lock, Control, Mod1, Mod2, Mod3, Mod4, and Mod5.
518 // Only nonzero KeyCodes have meaning in each set, and zero KeyCodes are
519 // ignored.
521 // Note that two or more modifiers may use one modifier flag. E.g.,
522 // on Ubuntu 10.10, Alt and Meta share the Mod1 in default settings.
523 // And also Super and Hyper share the Mod4. In such cases, we need to
524 // decide which modifier flag means one of DOM modifiers.
526 // mod[0] is Modifier introduced by Mod1.
527 Modifier mod[5];
528 int32_t foundLevel[5];
529 for (uint32_t i = 0; i < ArrayLength(mod); i++) {
530 mod[i] = NOT_MODIFIER;
531 foundLevel[i] = INT32_MAX;
533 const uint32_t map_size = 8 * xmodmap->max_keypermod;
534 for (uint32_t i = 0; i < map_size; i++) {
535 KeyCode keycode = xmodmap->modifiermap[i];
536 MOZ_LOG(gKeyLog, LogLevel::Info,
537 ("%p InitBySystemSettings, "
538 " i=%d, keycode=0x%08X",
539 this, i, keycode));
540 if (!keycode || keycode < min_keycode || keycode > max_keycode) {
541 continue;
544 ModifierKey* modifierKey = GetModifierKey(keycode);
545 if (!modifierKey) {
546 modifierKey = mModifierKeys.AppendElement(ModifierKey(keycode));
549 const KeySym* syms =
550 xkeymap + (keycode - min_keycode) * keysyms_per_keycode;
551 const uint32_t bit = i / xmodmap->max_keypermod;
552 modifierKey->mMask |= 1 << bit;
554 // We need to know the meaning of Mod1, Mod2, Mod3, Mod4 and Mod5.
555 // Let's skip if current map is for others.
556 if (bit < 3) {
557 continue;
560 const int32_t modIndex = bit - 3;
561 for (int32_t j = 0; j < keysyms_per_keycode; j++) {
562 Modifier modifier = GetModifierForGDKKeyval(syms[j]);
563 MOZ_LOG(gKeyLog, LogLevel::Info,
564 ("%p InitBySystemSettings, "
565 " Mod%d, j=%d, syms[j]=%s(0x%lX), modifier=%s",
566 this, modIndex + 1, j, gdk_keyval_name(syms[j]), syms[j],
567 GetModifierName(modifier)));
569 switch (modifier) {
570 case NOT_MODIFIER:
571 // Don't overwrite the stored information with
572 // NOT_MODIFIER.
573 break;
574 case CAPS_LOCK:
575 case SHIFT:
576 case CTRL:
577 // Ignore the modifiers defined in GDK spec. They shouldn't
578 // be mapped to Mod1-5 because they must not work on native
579 // GTK applications.
580 break;
581 default:
582 // If new modifier is found in higher level than stored
583 // value, we don't need to overwrite it.
584 if (j > foundLevel[modIndex]) {
585 break;
587 // If new modifier is more important than stored value,
588 // we should overwrite it with new modifier.
589 if (j == foundLevel[modIndex]) {
590 mod[modIndex] = std::min(modifier, mod[modIndex]);
591 break;
593 foundLevel[modIndex] = j;
594 mod[modIndex] = modifier;
595 break;
600 for (uint32_t i = 0; i < COUNT_OF_MODIFIER_INDEX; i++) {
601 Modifier modifier;
602 switch (i) {
603 case INDEX_NUM_LOCK:
604 modifier = NUM_LOCK;
605 break;
606 case INDEX_SCROLL_LOCK:
607 modifier = SCROLL_LOCK;
608 break;
609 case INDEX_ALT:
610 modifier = ALT;
611 break;
612 case INDEX_META:
613 modifier = META;
614 break;
615 case INDEX_SUPER:
616 modifier = SUPER;
617 break;
618 case INDEX_HYPER:
619 modifier = HYPER;
620 break;
621 case INDEX_LEVEL3:
622 modifier = LEVEL3;
623 break;
624 case INDEX_LEVEL5:
625 modifier = LEVEL5;
626 break;
627 default:
628 MOZ_CRASH("All indexes must be handled here");
630 for (uint32_t j = 0; j < ArrayLength(mod); j++) {
631 if (modifier == mod[j]) {
632 mModifierMasks[i] |= 1 << (j + 3);
637 XFreeModifiermap(xmodmap);
638 XFree(xkeymap);
640 #endif
642 #ifdef MOZ_WAYLAND
643 void KeymapWrapper::SetModifierMask(xkb_keymap* aKeymap,
644 ModifierIndex aModifierIndex,
645 const char* aModifierName) {
646 static auto sXkbKeymapModGetIndex =
647 (xkb_mod_index_t(*)(struct xkb_keymap*, const char*))dlsym(
648 RTLD_DEFAULT, "xkb_keymap_mod_get_index");
650 xkb_mod_index_t index = sXkbKeymapModGetIndex(aKeymap, aModifierName);
651 if (index != XKB_MOD_INVALID) {
652 mModifierMasks[aModifierIndex] = (1 << index);
656 void KeymapWrapper::SetModifierMasks(xkb_keymap* aKeymap) {
657 KeymapWrapper* keymapWrapper = GetInstance();
659 // This mapping is derived from get_xkb_modifiers() at gdkkeys-wayland.c
660 keymapWrapper->SetModifierMask(aKeymap, INDEX_NUM_LOCK, XKB_MOD_NAME_NUM);
661 keymapWrapper->SetModifierMask(aKeymap, INDEX_ALT, XKB_MOD_NAME_ALT);
662 keymapWrapper->SetModifierMask(aKeymap, INDEX_META, "Meta");
663 keymapWrapper->SetModifierMask(aKeymap, INDEX_SUPER, "Super");
664 keymapWrapper->SetModifierMask(aKeymap, INDEX_HYPER, "Hyper");
666 keymapWrapper->SetModifierMask(aKeymap, INDEX_SCROLL_LOCK, "ScrollLock");
667 keymapWrapper->SetModifierMask(aKeymap, INDEX_LEVEL3, "Level3");
668 keymapWrapper->SetModifierMask(aKeymap, INDEX_LEVEL5, "Level5");
670 MOZ_LOG(gKeyLog, LogLevel::Info,
671 ("%p KeymapWrapper::SetModifierMasks, CapsLock=0x%X, NumLock=0x%X, "
672 "ScrollLock=0x%X, Level3=0x%X, Level5=0x%X, "
673 "Shift=0x%X, Ctrl=0x%X, Alt=0x%X, Meta=0x%X, Super=0x%X, Hyper=0x%X",
674 keymapWrapper, keymapWrapper->GetModifierMask(CAPS_LOCK),
675 keymapWrapper->GetModifierMask(NUM_LOCK),
676 keymapWrapper->GetModifierMask(SCROLL_LOCK),
677 keymapWrapper->GetModifierMask(LEVEL3),
678 keymapWrapper->GetModifierMask(LEVEL5),
679 keymapWrapper->GetModifierMask(SHIFT),
680 keymapWrapper->GetModifierMask(CTRL),
681 keymapWrapper->GetModifierMask(ALT),
682 keymapWrapper->GetModifierMask(META),
683 keymapWrapper->GetModifierMask(SUPER),
684 keymapWrapper->GetModifierMask(HYPER)));
687 /* This keymap routine is derived from weston-2.0.0/clients/simple-im.c
689 static void keyboard_handle_keymap(void* data, struct wl_keyboard* wl_keyboard,
690 uint32_t format, int fd, uint32_t size) {
691 KeymapWrapper::ResetKeyboard();
693 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
694 close(fd);
695 return;
698 char* mapString = (char*)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
699 if (mapString == MAP_FAILED) {
700 close(fd);
701 return;
704 static auto sXkbContextNew =
705 (struct xkb_context * (*)(enum xkb_context_flags))
706 dlsym(RTLD_DEFAULT, "xkb_context_new");
707 static auto sXkbKeymapNewFromString =
708 (struct xkb_keymap * (*)(struct xkb_context*, const char*,
709 enum xkb_keymap_format,
710 enum xkb_keymap_compile_flags))
711 dlsym(RTLD_DEFAULT, "xkb_keymap_new_from_string");
713 struct xkb_context* xkb_context = sXkbContextNew(XKB_CONTEXT_NO_FLAGS);
714 struct xkb_keymap* keymap =
715 sXkbKeymapNewFromString(xkb_context, mapString, XKB_KEYMAP_FORMAT_TEXT_V1,
716 XKB_KEYMAP_COMPILE_NO_FLAGS);
718 munmap(mapString, size);
719 close(fd);
721 if (!keymap) {
722 NS_WARNING("keyboard_handle_keymap(): Failed to compile keymap!\n");
723 return;
726 KeymapWrapper::SetModifierMasks(keymap);
728 static auto sXkbKeymapUnRef =
729 (void (*)(struct xkb_keymap*))dlsym(RTLD_DEFAULT, "xkb_keymap_unref");
730 sXkbKeymapUnRef(keymap);
732 static auto sXkbContextUnref =
733 (void (*)(struct xkb_context*))dlsym(RTLD_DEFAULT, "xkb_context_unref");
734 sXkbContextUnref(xkb_context);
737 static void keyboard_handle_enter(void* data, struct wl_keyboard* keyboard,
738 uint32_t serial, struct wl_surface* surface,
739 struct wl_array* keys) {
740 KeymapWrapper::SetFocusIn(surface, serial);
743 static void keyboard_handle_leave(void* data, struct wl_keyboard* keyboard,
744 uint32_t serial, struct wl_surface* surface) {
745 KeymapWrapper::SetFocusOut(surface);
748 static void keyboard_handle_key(void* data, struct wl_keyboard* keyboard,
749 uint32_t serial, uint32_t time, uint32_t key,
750 uint32_t state) {}
751 static void keyboard_handle_modifiers(void* data, struct wl_keyboard* keyboard,
752 uint32_t serial, uint32_t mods_depressed,
753 uint32_t mods_latched,
754 uint32_t mods_locked, uint32_t group) {}
756 static const struct wl_keyboard_listener keyboard_listener = {
757 keyboard_handle_keymap, keyboard_handle_enter, keyboard_handle_leave,
758 keyboard_handle_key, keyboard_handle_modifiers,
761 static void seat_handle_capabilities(void* data, struct wl_seat* seat,
762 unsigned int caps) {
763 static wl_keyboard* keyboard = nullptr;
765 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !keyboard) {
766 keyboard = wl_seat_get_keyboard(seat);
767 wl_keyboard_add_listener(keyboard, &keyboard_listener, nullptr);
768 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && keyboard) {
769 wl_keyboard_destroy(keyboard);
770 keyboard = nullptr;
774 static const struct wl_seat_listener seat_listener = {
775 seat_handle_capabilities,
778 static void gdk_registry_handle_global(void* data, struct wl_registry* registry,
779 uint32_t id, const char* interface,
780 uint32_t version) {
781 if (strcmp(interface, "wl_seat") == 0) {
782 auto* seat =
783 WaylandRegistryBind<wl_seat>(registry, id, &wl_seat_interface, 1);
784 KeymapWrapper::SetSeat(seat);
785 wl_seat_add_listener(seat, &seat_listener, data);
789 static void gdk_registry_handle_global_remove(void* data,
790 struct wl_registry* registry,
791 uint32_t id) {}
793 static const struct wl_registry_listener keyboard_registry_listener = {
794 gdk_registry_handle_global, gdk_registry_handle_global_remove};
796 void KeymapWrapper::InitBySystemSettingsWayland() {
797 wl_display* display = WaylandDisplayGetWLDisplay();
798 wl_registry_add_listener(wl_display_get_registry(display),
799 &keyboard_registry_listener, this);
801 #endif
803 KeymapWrapper::~KeymapWrapper() {
804 #ifdef MOZ_X11
805 gdk_window_remove_filter(nullptr, FilterEvents, this);
806 #endif
807 if (mOnKeysChangedSignalHandle) {
808 g_signal_handler_disconnect(mGdkKeymap, mOnKeysChangedSignalHandle);
810 if (mOnDirectionChangedSignalHandle) {
811 g_signal_handler_disconnect(mGdkKeymap, mOnDirectionChangedSignalHandle);
813 g_object_unref(mGdkKeymap);
814 MOZ_LOG(gKeyLog, LogLevel::Info, ("%p Destructor", this));
817 #ifdef MOZ_X11
818 /* static */
819 GdkFilterReturn KeymapWrapper::FilterEvents(GdkXEvent* aXEvent,
820 GdkEvent* aGdkEvent,
821 gpointer aData) {
822 XEvent* xEvent = static_cast<XEvent*>(aXEvent);
823 switch (xEvent->type) {
824 case KeyPress: {
825 // If the key doesn't support auto repeat, ignore the event because
826 // even if such key (e.g., Shift) is pressed during auto repeat of
827 // anoter key, it doesn't stop the auto repeat.
828 KeymapWrapper* self = static_cast<KeymapWrapper*>(aData);
829 if (!self->IsAutoRepeatableKey(xEvent->xkey.keycode)) {
830 break;
832 if (sRepeatState == NOT_PRESSED) {
833 sRepeatState = FIRST_PRESS;
834 MOZ_LOG(gKeyLog, LogLevel::Info,
835 ("FilterEvents(aXEvent={ type=KeyPress, "
836 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
837 "aGdkEvent={ state=0x%08X }), "
838 "detected first keypress",
839 xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time,
840 reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
841 } else if (sLastRepeatableHardwareKeyCode == xEvent->xkey.keycode) {
842 if (sLastRepeatableKeyTime == xEvent->xkey.time &&
843 sLastRepeatableHardwareKeyCode ==
844 IMContextWrapper::
845 GetWaitingSynthesizedKeyPressHardwareKeyCode()) {
846 // On some environment, IM may generate duplicated KeyPress event
847 // without any special state flags. In such case, we shouldn't
848 // treat the event as "repeated".
849 MOZ_LOG(gKeyLog, LogLevel::Info,
850 ("FilterEvents(aXEvent={ type=KeyPress, "
851 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
852 "aGdkEvent={ state=0x%08X }), "
853 "igored keypress since it must be synthesized by IME",
854 xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time,
855 reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
856 break;
858 sRepeatState = REPEATING;
859 MOZ_LOG(gKeyLog, LogLevel::Info,
860 ("FilterEvents(aXEvent={ type=KeyPress, "
861 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
862 "aGdkEvent={ state=0x%08X }), "
863 "detected repeating keypress",
864 xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time,
865 reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
866 } else {
867 // If a different key is pressed while another key is pressed,
868 // auto repeat system repeats only the last pressed key.
869 // So, setting new keycode and setting repeat state as first key
870 // press should work fine.
871 sRepeatState = FIRST_PRESS;
872 MOZ_LOG(gKeyLog, LogLevel::Info,
873 ("FilterEvents(aXEvent={ type=KeyPress, "
874 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
875 "aGdkEvent={ state=0x%08X }), "
876 "detected different keypress",
877 xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time,
878 reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
880 sLastRepeatableHardwareKeyCode = xEvent->xkey.keycode;
881 sLastRepeatableKeyTime = xEvent->xkey.time;
882 break;
884 case KeyRelease: {
885 if (sLastRepeatableHardwareKeyCode != xEvent->xkey.keycode) {
886 // This case means the key release event is caused by
887 // a non-repeatable key such as Shift or a repeatable key that
888 // was pressed before sLastRepeatableHardwareKeyCode was
889 // pressed.
890 break;
892 sRepeatState = NOT_PRESSED;
893 MOZ_LOG(gKeyLog, LogLevel::Info,
894 ("FilterEvents(aXEvent={ type=KeyRelease, "
895 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
896 "aGdkEvent={ state=0x%08X }), "
897 "detected key release",
898 xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time,
899 reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
900 break;
902 case FocusOut: {
903 // At moving focus, we should reset keyboard repeat state.
904 // Strictly, this causes incorrect behavior. However, this
905 // correctness must be enough for web applications.
906 sRepeatState = NOT_PRESSED;
907 break;
909 default: {
910 KeymapWrapper* self = static_cast<KeymapWrapper*>(aData);
911 if (xEvent->type != self->mXKBBaseEventCode) {
912 break;
914 XkbEvent* xkbEvent = (XkbEvent*)xEvent;
915 if (xkbEvent->any.xkb_type != XkbControlsNotify ||
916 !(xkbEvent->ctrls.changed_ctrls & XkbPerKeyRepeatMask)) {
917 break;
919 if (!XGetKeyboardControl(xkbEvent->any.display, &self->mKeyboardState)) {
920 MOZ_LOG(gKeyLog, LogLevel::Info,
921 ("%p FilterEvents failed due to failure "
922 "of XGetKeyboardControl(), display=0x%p",
923 self, xkbEvent->any.display));
925 break;
929 return GDK_FILTER_CONTINUE;
931 #endif
933 static void ResetBidiKeyboard() {
934 // Reset the bidi keyboard settings for the new GdkKeymap
935 nsCOMPtr<nsIBidiKeyboard> bidiKeyboard = nsContentUtils::GetBidiKeyboard();
936 if (bidiKeyboard) {
937 bidiKeyboard->Reset();
939 WidgetUtils::SendBidiKeyboardInfoToContent();
942 /* static */
943 void KeymapWrapper::ResetKeyboard() {
944 sInstance->mInitialized = false;
945 ResetBidiKeyboard();
948 /* static */
949 void KeymapWrapper::OnKeysChanged(GdkKeymap* aGdkKeymap,
950 KeymapWrapper* aKeymapWrapper) {
951 MOZ_LOG(gKeyLog, LogLevel::Info,
952 ("OnKeysChanged, aGdkKeymap=%p, aKeymapWrapper=%p", aGdkKeymap,
953 aKeymapWrapper));
955 MOZ_ASSERT(sInstance == aKeymapWrapper,
956 "This instance must be the singleton instance");
958 // We cannot reintialize here becasue we don't have GdkWindow which is using
959 // the GdkKeymap. We'll reinitialize it when next GetInstance() is called.
960 ResetKeyboard();
963 // static
964 void KeymapWrapper::OnDirectionChanged(GdkKeymap* aGdkKeymap,
965 KeymapWrapper* aKeymapWrapper) {
966 // XXX
967 // A lot of diretion-changed signal might be fired on switching bidi
968 // keyboard when using both ibus (with arabic layout) and fcitx (with IME).
969 // See https://github.com/fcitx/fcitx/issues/257
971 // Also, when using ibus, switching to IM might not cause this signal.
972 // See https://github.com/ibus/ibus/issues/1848
974 MOZ_LOG(gKeyLog, LogLevel::Info,
975 ("OnDirectionChanged, aGdkKeymap=%p, aKeymapWrapper=%p", aGdkKeymap,
976 aKeymapWrapper));
978 ResetBidiKeyboard();
981 /* static */
982 guint KeymapWrapper::GetCurrentModifierState() {
983 GdkModifierType modifiers;
984 GdkDisplay* display = gdk_display_get_default();
985 GdkScreen* screen = gdk_display_get_default_screen(display);
986 GdkWindow* window = gdk_screen_get_root_window(screen);
987 gdk_window_get_device_position(window, GdkGetPointer(), nullptr, nullptr,
988 &modifiers);
989 return static_cast<guint>(modifiers);
992 /* static */
993 bool KeymapWrapper::AreModifiersCurrentlyActive(Modifiers aModifiers) {
994 guint modifierState = GetCurrentModifierState();
995 return AreModifiersActive(aModifiers, modifierState);
998 /* static */
999 bool KeymapWrapper::AreModifiersActive(Modifiers aModifiers,
1000 guint aModifierState) {
1001 NS_ENSURE_TRUE(aModifiers, false);
1003 KeymapWrapper* keymapWrapper = GetInstance();
1004 for (uint32_t i = 0; i < sizeof(Modifier) * 8 && aModifiers; i++) {
1005 Modifier modifier = static_cast<Modifier>(1 << i);
1006 if (!(aModifiers & modifier)) {
1007 continue;
1009 if (!(aModifierState & keymapWrapper->GetModifierMask(modifier))) {
1010 return false;
1012 aModifiers &= ~modifier;
1014 return true;
1017 /* static */
1018 uint32_t KeymapWrapper::ComputeCurrentKeyModifiers() {
1019 return ComputeKeyModifiers(GetCurrentModifierState());
1022 /* static */
1023 uint32_t KeymapWrapper::ComputeKeyModifiers(guint aModifierState) {
1024 KeymapWrapper* keymapWrapper = GetInstance();
1026 uint32_t keyModifiers = 0;
1027 // DOM Meta key should be TRUE only on Mac. We need to discuss this
1028 // issue later.
1029 if (keymapWrapper->AreModifiersActive(SHIFT, aModifierState)) {
1030 keyModifiers |= MODIFIER_SHIFT;
1032 if (keymapWrapper->AreModifiersActive(CTRL, aModifierState)) {
1033 keyModifiers |= MODIFIER_CONTROL;
1035 if (keymapWrapper->AreModifiersActive(ALT, aModifierState)) {
1036 keyModifiers |= MODIFIER_ALT;
1038 if (keymapWrapper->AreModifiersActive(META, aModifierState)) {
1039 keyModifiers |= MODIFIER_META;
1041 if (keymapWrapper->AreModifiersActive(SUPER, aModifierState) ||
1042 keymapWrapper->AreModifiersActive(HYPER, aModifierState)) {
1043 keyModifiers |= MODIFIER_OS;
1045 if (keymapWrapper->AreModifiersActive(LEVEL3, aModifierState) ||
1046 keymapWrapper->AreModifiersActive(LEVEL5, aModifierState)) {
1047 keyModifiers |= MODIFIER_ALTGRAPH;
1049 if (keymapWrapper->AreModifiersActive(CAPS_LOCK, aModifierState)) {
1050 keyModifiers |= MODIFIER_CAPSLOCK;
1052 if (keymapWrapper->AreModifiersActive(NUM_LOCK, aModifierState)) {
1053 keyModifiers |= MODIFIER_NUMLOCK;
1055 if (keymapWrapper->AreModifiersActive(SCROLL_LOCK, aModifierState)) {
1056 keyModifiers |= MODIFIER_SCROLLLOCK;
1058 return keyModifiers;
1061 /* static */
1062 guint KeymapWrapper::ConvertWidgetModifierToGdkState(
1063 nsIWidget::Modifiers aNativeModifiers) {
1064 if (!aNativeModifiers) {
1065 return 0;
1067 struct ModifierMapEntry {
1068 nsIWidget::Modifiers mWidgetModifier;
1069 Modifier mModifier;
1071 // TODO: Currently, we don't treat L/R of each modifier on Linux.
1072 // TODO: No proper native modifier for Level5.
1073 static constexpr ModifierMapEntry sModifierMap[] = {
1074 {nsIWidget::CAPS_LOCK, Modifier::CAPS_LOCK},
1075 {nsIWidget::NUM_LOCK, Modifier::NUM_LOCK},
1076 {nsIWidget::SHIFT_L, Modifier::SHIFT},
1077 {nsIWidget::SHIFT_R, Modifier::SHIFT},
1078 {nsIWidget::CTRL_L, Modifier::CTRL},
1079 {nsIWidget::CTRL_R, Modifier::CTRL},
1080 {nsIWidget::ALT_L, Modifier::ALT},
1081 {nsIWidget::ALT_R, Modifier::ALT},
1082 {nsIWidget::ALTGRAPH, Modifier::LEVEL3},
1083 {nsIWidget::COMMAND_L, Modifier::SUPER},
1084 {nsIWidget::COMMAND_R, Modifier::SUPER}};
1086 guint state = 0;
1087 KeymapWrapper* instance = GetInstance();
1088 for (const ModifierMapEntry& entry : sModifierMap) {
1089 if (aNativeModifiers & entry.mWidgetModifier) {
1090 state |= instance->GetModifierMask(entry.mModifier);
1093 return state;
1096 /* static */
1097 void KeymapWrapper::InitInputEvent(WidgetInputEvent& aInputEvent,
1098 guint aModifierState) {
1099 KeymapWrapper* keymapWrapper = GetInstance();
1101 aInputEvent.mModifiers = ComputeKeyModifiers(aModifierState);
1103 // Don't log this method for non-important events because e.g., eMouseMove is
1104 // just noisy and there is no reason to log it.
1105 bool doLog = aInputEvent.mMessage != eMouseMove;
1106 if (doLog) {
1107 MOZ_LOG(gKeyLog, LogLevel::Debug,
1108 ("%p InitInputEvent, aModifierState=0x%08X, "
1109 "aInputEvent={ mMessage=%s, mModifiers=0x%04X (Shift: %s, "
1110 "Control: %s, Alt: %s, "
1111 "Meta: %s, OS: %s, AltGr: %s, "
1112 "CapsLock: %s, NumLock: %s, ScrollLock: %s })",
1113 keymapWrapper, aModifierState, ToChar(aInputEvent.mMessage),
1114 aInputEvent.mModifiers,
1115 GetBoolName(aInputEvent.mModifiers & MODIFIER_SHIFT),
1116 GetBoolName(aInputEvent.mModifiers & MODIFIER_CONTROL),
1117 GetBoolName(aInputEvent.mModifiers & MODIFIER_ALT),
1118 GetBoolName(aInputEvent.mModifiers & MODIFIER_META),
1119 GetBoolName(aInputEvent.mModifiers & MODIFIER_OS),
1120 GetBoolName(aInputEvent.mModifiers & MODIFIER_ALTGRAPH),
1121 GetBoolName(aInputEvent.mModifiers & MODIFIER_CAPSLOCK),
1122 GetBoolName(aInputEvent.mModifiers & MODIFIER_NUMLOCK),
1123 GetBoolName(aInputEvent.mModifiers & MODIFIER_SCROLLLOCK)));
1126 switch (aInputEvent.mClass) {
1127 case eMouseEventClass:
1128 case eMouseScrollEventClass:
1129 case eWheelEventClass:
1130 case eDragEventClass:
1131 case eSimpleGestureEventClass:
1132 break;
1133 default:
1134 return;
1137 WidgetMouseEventBase& mouseEvent = *aInputEvent.AsMouseEventBase();
1138 mouseEvent.mButtons = 0;
1139 if (aModifierState & GDK_BUTTON1_MASK) {
1140 mouseEvent.mButtons |= MouseButtonsFlag::ePrimaryFlag;
1142 if (aModifierState & GDK_BUTTON3_MASK) {
1143 mouseEvent.mButtons |= MouseButtonsFlag::eSecondaryFlag;
1145 if (aModifierState & GDK_BUTTON2_MASK) {
1146 mouseEvent.mButtons |= MouseButtonsFlag::eMiddleFlag;
1149 if (doLog) {
1150 MOZ_LOG(
1151 gKeyLog, LogLevel::Debug,
1152 ("%p InitInputEvent, aInputEvent has mButtons, "
1153 "aInputEvent.mButtons=0x%04X (Left: %s, Right: %s, Middle: %s, "
1154 "4th (BACK): %s, 5th (FORWARD): %s)",
1155 keymapWrapper, mouseEvent.mButtons,
1156 GetBoolName(mouseEvent.mButtons & MouseButtonsFlag::ePrimaryFlag),
1157 GetBoolName(mouseEvent.mButtons & MouseButtonsFlag::eSecondaryFlag),
1158 GetBoolName(mouseEvent.mButtons & MouseButtonsFlag::eMiddleFlag),
1159 GetBoolName(mouseEvent.mButtons & MouseButtonsFlag::e4thFlag),
1160 GetBoolName(mouseEvent.mButtons & MouseButtonsFlag::e5thFlag)));
1164 /* static */
1165 uint32_t KeymapWrapper::ComputeDOMKeyCode(const GdkEventKey* aGdkKeyEvent) {
1166 // If the keyval indicates it's a modifier key, we should use unshifted
1167 // key's modifier keyval.
1168 guint keyval = aGdkKeyEvent->keyval;
1169 if (GetModifierForGDKKeyval(keyval)) {
1170 // But if the keyval without modifiers isn't a modifier key, we
1171 // shouldn't use it. E.g., Japanese keyboard layout's
1172 // Shift + Eisu-Toggle key is CapsLock. This is an actual rare case,
1173 // Windows uses different keycode for a physical key for different
1174 // shift key state.
1175 guint keyvalWithoutModifier = GetGDKKeyvalWithoutModifier(aGdkKeyEvent);
1176 if (GetModifierForGDKKeyval(keyvalWithoutModifier)) {
1177 keyval = keyvalWithoutModifier;
1179 // Note that the modifier keycode and activating or deactivating
1180 // modifier flag may be mismatched, but it's okay. If a DOM key
1181 // event handler is testing a keydown event, it's more likely being
1182 // used to test which key is being pressed than to test which
1183 // modifier will become active. So, if we computed DOM keycode
1184 // from modifier flag which were changing by the physical key, then
1185 // there would be no other way for the user to generate the original
1186 // keycode.
1187 uint32_t DOMKeyCode = GetDOMKeyCodeFromKeyPairs(keyval);
1188 NS_ASSERTION(DOMKeyCode, "All modifier keys must have a DOM keycode");
1189 return DOMKeyCode;
1192 // If the key isn't printable, let's look at the key pairs.
1193 uint32_t charCode = GetCharCodeFor(aGdkKeyEvent);
1194 if (!charCode) {
1195 // Note that any key may be a function key because of some unusual keyboard
1196 // layouts. I.e., even if the pressed key is a printable key of en-US
1197 // keyboard layout, we should expose the function key's keyCode value to
1198 // web apps because web apps should handle the keydown/keyup events as
1199 // inputted by usual keyboard layout. For example, Hatchak keyboard
1200 // maps Tab key to "Digit3" key and Level3 Shift makes it "Backspace".
1201 // In this case, we should expose DOM_VK_BACK_SPACE (8).
1202 uint32_t DOMKeyCode = GetDOMKeyCodeFromKeyPairs(keyval);
1203 if (DOMKeyCode) {
1204 // XXX If DOMKeyCode is a function key's keyCode value, it might be
1205 // better to consume necessary modifiers. For example, if there is
1206 // no Control Pad section on keyboard like notebook, Delete key is
1207 // available only with Level3 Shift+"Backspace" key if using Hatchak.
1208 // If web apps accept Delete key operation only when no modifiers are
1209 // active, such users cannot use Delete key to do it. However,
1210 // Chromium doesn't consume such necessary modifiers. So, our default
1211 // behavior should keep not touching modifiers for compatibility, but
1212 // it might be better to add a pref to consume necessary modifiers.
1213 return DOMKeyCode;
1215 // If aGdkKeyEvent cannot be mapped to a DOM keyCode value, we should
1216 // refer keyCode value without modifiers because web apps should be
1217 // able to identify the key as far as possible.
1218 guint keyvalWithoutModifier = GetGDKKeyvalWithoutModifier(aGdkKeyEvent);
1219 return GetDOMKeyCodeFromKeyPairs(keyvalWithoutModifier);
1222 // printable numpad keys should be resolved here.
1223 switch (keyval) {
1224 case GDK_KP_Multiply:
1225 return NS_VK_MULTIPLY;
1226 case GDK_KP_Add:
1227 return NS_VK_ADD;
1228 case GDK_KP_Separator:
1229 return NS_VK_SEPARATOR;
1230 case GDK_KP_Subtract:
1231 return NS_VK_SUBTRACT;
1232 case GDK_KP_Decimal:
1233 return NS_VK_DECIMAL;
1234 case GDK_KP_Divide:
1235 return NS_VK_DIVIDE;
1236 case GDK_KP_0:
1237 return NS_VK_NUMPAD0;
1238 case GDK_KP_1:
1239 return NS_VK_NUMPAD1;
1240 case GDK_KP_2:
1241 return NS_VK_NUMPAD2;
1242 case GDK_KP_3:
1243 return NS_VK_NUMPAD3;
1244 case GDK_KP_4:
1245 return NS_VK_NUMPAD4;
1246 case GDK_KP_5:
1247 return NS_VK_NUMPAD5;
1248 case GDK_KP_6:
1249 return NS_VK_NUMPAD6;
1250 case GDK_KP_7:
1251 return NS_VK_NUMPAD7;
1252 case GDK_KP_8:
1253 return NS_VK_NUMPAD8;
1254 case GDK_KP_9:
1255 return NS_VK_NUMPAD9;
1258 KeymapWrapper* keymapWrapper = GetInstance();
1260 // Ignore all modifier state except NumLock.
1261 guint baseState =
1262 (aGdkKeyEvent->state & keymapWrapper->GetModifierMask(NUM_LOCK));
1264 // Basically, we should use unmodified character for deciding our keyCode.
1265 uint32_t unmodifiedChar = keymapWrapper->GetCharCodeFor(
1266 aGdkKeyEvent, baseState, aGdkKeyEvent->group);
1267 if (IsBasicLatinLetterOrNumeral(unmodifiedChar)) {
1268 // If the unmodified character is an ASCII alphabet or an ASCII
1269 // numeric, it's the best hint for deciding our keyCode.
1270 return WidgetUtils::ComputeKeyCodeFromChar(unmodifiedChar);
1273 // If the unmodified character is not an ASCII character, that means we
1274 // couldn't find the hint. We should reset it.
1275 if (!IsPrintableASCIICharacter(unmodifiedChar)) {
1276 unmodifiedChar = 0;
1279 // Retry with shifted keycode.
1280 guint shiftState = (baseState | keymapWrapper->GetModifierMask(SHIFT));
1281 uint32_t shiftedChar = keymapWrapper->GetCharCodeFor(aGdkKeyEvent, shiftState,
1282 aGdkKeyEvent->group);
1283 if (IsBasicLatinLetterOrNumeral(shiftedChar)) {
1284 // A shifted character can be an ASCII alphabet on Hebrew keyboard
1285 // layout. And also shifted character can be an ASCII numeric on
1286 // AZERTY keyboad layout. Then, it's a good hint for deciding our
1287 // keyCode.
1288 return WidgetUtils::ComputeKeyCodeFromChar(shiftedChar);
1291 // If the shifted unmodified character isn't an ASCII character, we should
1292 // discard it too.
1293 if (!IsPrintableASCIICharacter(shiftedChar)) {
1294 shiftedChar = 0;
1297 // If current keyboard layout isn't ASCII alphabet inputtable layout,
1298 // look for ASCII alphabet inputtable keyboard layout. If the key
1299 // inputs an ASCII alphabet or an ASCII numeric, we should use it
1300 // for deciding our keyCode.
1301 uint32_t unmodCharLatin = 0;
1302 uint32_t shiftedCharLatin = 0;
1303 if (!keymapWrapper->IsLatinGroup(aGdkKeyEvent->group)) {
1304 gint minGroup = keymapWrapper->GetFirstLatinGroup();
1305 if (minGroup >= 0) {
1306 unmodCharLatin =
1307 keymapWrapper->GetCharCodeFor(aGdkKeyEvent, baseState, minGroup);
1308 if (IsBasicLatinLetterOrNumeral(unmodCharLatin)) {
1309 // If the unmodified character is an ASCII alphabet or
1310 // an ASCII numeric, we should use it for the keyCode.
1311 return WidgetUtils::ComputeKeyCodeFromChar(unmodCharLatin);
1313 // If the unmodified character in the alternative ASCII capable
1314 // keyboard layout isn't an ASCII character, that means we couldn't
1315 // find the hint. We should reset it.
1316 if (!IsPrintableASCIICharacter(unmodCharLatin)) {
1317 unmodCharLatin = 0;
1319 shiftedCharLatin =
1320 keymapWrapper->GetCharCodeFor(aGdkKeyEvent, shiftState, minGroup);
1321 if (IsBasicLatinLetterOrNumeral(shiftedCharLatin)) {
1322 // If the shifted character is an ASCII alphabet or an ASCII
1323 // numeric, we should use it for the keyCode.
1324 return WidgetUtils::ComputeKeyCodeFromChar(shiftedCharLatin);
1326 // If the shifted unmodified character in the alternative ASCII
1327 // capable keyboard layout isn't an ASCII character, we should
1328 // discard it too.
1329 if (!IsPrintableASCIICharacter(shiftedCharLatin)) {
1330 shiftedCharLatin = 0;
1335 // If the key itself or with Shift state on active keyboard layout produces
1336 // an ASCII punctuation character, we should decide keyCode value with it.
1337 if (unmodifiedChar || shiftedChar) {
1338 return WidgetUtils::ComputeKeyCodeFromChar(unmodifiedChar ? unmodifiedChar
1339 : shiftedChar);
1342 // If the key itself or with Shift state on alternative ASCII capable
1343 // keyboard layout produces an ASCII punctuation character, we should
1344 // decide keyCode value with it. Note that We've returned 0 for long
1345 // time if keyCode isn't for an alphabet keys or a numeric key even in
1346 // alternative ASCII capable keyboard layout because we decided that we
1347 // should avoid setting same keyCode value to 2 or more keys since active
1348 // keyboard layout may have a key to input the punctuation with different
1349 // key. However, setting keyCode to 0 makes some web applications which
1350 // are aware of neither KeyboardEvent.key nor KeyboardEvent.code not work
1351 // with Firefox when user selects non-ASCII capable keyboard layout such
1352 // as Russian and Thai. So, if alternative ASCII capable keyboard layout
1353 // has keyCode value for the key, we should use it. In other words, this
1354 // behavior means that non-ASCII capable keyboard layout overrides some
1355 // keys' keyCode value only if the key produces ASCII character by itself
1356 // or with Shift key.
1357 if (unmodCharLatin || shiftedCharLatin) {
1358 return WidgetUtils::ComputeKeyCodeFromChar(
1359 unmodCharLatin ? unmodCharLatin : shiftedCharLatin);
1362 // Otherwise, let's decide keyCode value from the hardware_keycode
1363 // value on major keyboard layout.
1364 CodeNameIndex code = ComputeDOMCodeNameIndex(aGdkKeyEvent);
1365 return WidgetKeyboardEvent::GetFallbackKeyCodeOfPunctuationKey(code);
1368 KeyNameIndex KeymapWrapper::ComputeDOMKeyNameIndex(
1369 const GdkEventKey* aGdkKeyEvent) {
1370 switch (aGdkKeyEvent->keyval) {
1371 #define NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX(aNativeKey, aKeyNameIndex) \
1372 case aNativeKey: \
1373 return aKeyNameIndex;
1375 #include "NativeKeyToDOMKeyName.h"
1377 #undef NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX
1379 default:
1380 break;
1383 return KEY_NAME_INDEX_Unidentified;
1386 /* static */
1387 CodeNameIndex KeymapWrapper::ComputeDOMCodeNameIndex(
1388 const GdkEventKey* aGdkKeyEvent) {
1389 switch (aGdkKeyEvent->hardware_keycode) {
1390 #define NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX(aNativeKey, aCodeNameIndex) \
1391 case aNativeKey: \
1392 return aCodeNameIndex;
1394 #include "NativeKeyToDOMCodeName.h"
1396 #undef NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX
1398 default:
1399 break;
1402 return CODE_NAME_INDEX_UNKNOWN;
1405 /* static */
1406 bool KeymapWrapper::DispatchKeyDownOrKeyUpEvent(nsWindow* aWindow,
1407 GdkEventKey* aGdkKeyEvent,
1408 bool aIsProcessedByIME,
1409 bool* aIsCancelled) {
1410 MOZ_ASSERT(aIsCancelled, "aIsCancelled must not be nullptr");
1412 *aIsCancelled = false;
1414 if (aGdkKeyEvent->type == GDK_KEY_PRESS && aGdkKeyEvent->keyval == GDK_Tab &&
1415 AreModifiersActive(CTRL | ALT, aGdkKeyEvent->state)) {
1416 MOZ_LOG(gKeyLog, LogLevel::Info,
1417 (" DispatchKeyDownOrKeyUpEvent(), didn't dispatch keyboard events "
1418 "because it's Ctrl + Alt + Tab"));
1419 return false;
1422 EventMessage message =
1423 aGdkKeyEvent->type == GDK_KEY_PRESS ? eKeyDown : eKeyUp;
1424 WidgetKeyboardEvent keyEvent(true, message, aWindow);
1425 KeymapWrapper::InitKeyEvent(keyEvent, aGdkKeyEvent, aIsProcessedByIME);
1426 return DispatchKeyDownOrKeyUpEvent(aWindow, keyEvent, aIsCancelled);
1429 /* static */
1430 bool KeymapWrapper::DispatchKeyDownOrKeyUpEvent(
1431 nsWindow* aWindow, WidgetKeyboardEvent& aKeyboardEvent,
1432 bool* aIsCancelled) {
1433 MOZ_ASSERT(aIsCancelled, "aIsCancelled must not be nullptr");
1435 *aIsCancelled = false;
1437 RefPtr<TextEventDispatcher> dispatcher = aWindow->GetTextEventDispatcher();
1438 nsresult rv = dispatcher->BeginNativeInputTransaction();
1439 if (NS_WARN_IF(NS_FAILED(rv))) {
1440 MOZ_LOG(gKeyLog, LogLevel::Error,
1441 (" DispatchKeyDownOrKeyUpEvent(), stopped dispatching %s event "
1442 "because of failed to initialize TextEventDispatcher",
1443 ToChar(aKeyboardEvent.mMessage)));
1444 return FALSE;
1447 nsEventStatus status = nsEventStatus_eIgnore;
1448 bool dispatched = dispatcher->DispatchKeyboardEvent(
1449 aKeyboardEvent.mMessage, aKeyboardEvent, status, nullptr);
1450 *aIsCancelled = (status == nsEventStatus_eConsumeNoDefault);
1451 return dispatched;
1454 /* static */
1455 bool KeymapWrapper::MaybeDispatchContextMenuEvent(nsWindow* aWindow,
1456 const GdkEventKey* aEvent) {
1457 KeyNameIndex keyNameIndex = ComputeDOMKeyNameIndex(aEvent);
1459 // Shift+F10 and ContextMenu should cause eContextMenu event.
1460 if (keyNameIndex != KEY_NAME_INDEX_F10 &&
1461 keyNameIndex != KEY_NAME_INDEX_ContextMenu) {
1462 return false;
1465 WidgetMouseEvent contextMenuEvent(true, eContextMenu, aWindow,
1466 WidgetMouseEvent::eReal,
1467 WidgetMouseEvent::eContextMenuKey);
1469 contextMenuEvent.mRefPoint = LayoutDeviceIntPoint(0, 0);
1470 contextMenuEvent.AssignEventTime(aWindow->GetWidgetEventTime(aEvent->time));
1471 contextMenuEvent.mClickCount = 1;
1472 KeymapWrapper::InitInputEvent(contextMenuEvent, aEvent->state);
1474 if (contextMenuEvent.IsControl() || contextMenuEvent.IsMeta() ||
1475 contextMenuEvent.IsAlt()) {
1476 return false;
1479 // If the key is ContextMenu, then an eContextMenu mouse event is
1480 // dispatched regardless of the state of the Shift modifier. When it is
1481 // pressed without the Shift modifier, a web page can prevent the default
1482 // context menu action. When pressed with the Shift modifier, the web page
1483 // cannot prevent the default context menu action.
1484 // (PresShell::HandleEventInternal() sets mOnlyChromeDispatch to true.)
1486 // If the key is F10, it needs Shift state because Shift+F10 is well-known
1487 // shortcut key on Linux. However, eContextMenu with Shift state is
1488 // special. It won't fire "contextmenu" event in the web content for
1489 // blocking web page to prevent its default. Therefore, this combination
1490 // should work same as ContextMenu key.
1491 // XXX Should we allow to block web page to prevent its default with
1492 // Ctrl+Shift+F10 or Alt+Shift+F10 instead?
1493 if (keyNameIndex == KEY_NAME_INDEX_F10) {
1494 if (!contextMenuEvent.IsShift()) {
1495 return false;
1497 contextMenuEvent.mModifiers &= ~MODIFIER_SHIFT;
1500 aWindow->DispatchInputEvent(&contextMenuEvent);
1501 return true;
1504 /* static*/
1505 void KeymapWrapper::HandleKeyPressEvent(nsWindow* aWindow,
1506 GdkEventKey* aGdkKeyEvent) {
1507 MOZ_LOG(gKeyLog, LogLevel::Info,
1508 ("HandleKeyPressEvent(aWindow=%p, aGdkKeyEvent={ type=%s, "
1509 "keyval=%s(0x%X), state=0x%08X, hardware_keycode=0x%08X, "
1510 "time=%u, is_modifier=%s })",
1511 aWindow,
1512 ((aGdkKeyEvent->type == GDK_KEY_PRESS) ? "GDK_KEY_PRESS"
1513 : "GDK_KEY_RELEASE"),
1514 gdk_keyval_name(aGdkKeyEvent->keyval), aGdkKeyEvent->keyval,
1515 aGdkKeyEvent->state, aGdkKeyEvent->hardware_keycode,
1516 aGdkKeyEvent->time, GetBoolName(aGdkKeyEvent->is_modifier)));
1518 // if we are in the middle of composing text, XIM gets to see it
1519 // before mozilla does.
1520 // FYI: Don't dispatch keydown event before notifying IME of the event
1521 // because IME may send a key event synchronously and consume the
1522 // original event.
1523 bool IMEWasEnabled = false;
1524 KeyHandlingState handlingState = KeyHandlingState::eNotHandled;
1525 RefPtr<IMContextWrapper> imContext = aWindow->GetIMContext();
1526 if (imContext) {
1527 IMEWasEnabled = imContext->IsEnabled();
1528 handlingState = imContext->OnKeyEvent(aWindow, aGdkKeyEvent);
1529 if (handlingState == KeyHandlingState::eHandled) {
1530 MOZ_LOG(gKeyLog, LogLevel::Info,
1531 (" HandleKeyPressEvent(), the event was handled by "
1532 "IMContextWrapper"));
1533 return;
1537 // work around for annoying things.
1538 if (aGdkKeyEvent->keyval == GDK_Tab &&
1539 AreModifiersActive(CTRL | ALT, aGdkKeyEvent->state)) {
1540 MOZ_LOG(gKeyLog, LogLevel::Info,
1541 (" HandleKeyPressEvent(), didn't dispatch keyboard events "
1542 "because it's Ctrl + Alt + Tab"));
1543 return;
1546 // Dispatch keydown event always. At auto repeating, we should send
1547 // KEYDOWN -> KEYPRESS -> KEYDOWN -> KEYPRESS ... -> KEYUP
1548 // However, old distributions (e.g., Ubuntu 9.10) sent native key
1549 // release event, so, on such platform, the DOM events will be:
1550 // KEYDOWN -> KEYPRESS -> KEYUP -> KEYDOWN -> KEYPRESS -> KEYUP...
1552 bool isKeyDownCancelled = false;
1553 if (handlingState == KeyHandlingState::eNotHandled) {
1554 if (DispatchKeyDownOrKeyUpEvent(aWindow, aGdkKeyEvent, false,
1555 &isKeyDownCancelled) &&
1556 (MOZ_UNLIKELY(aWindow->IsDestroyed()) || isKeyDownCancelled)) {
1557 MOZ_LOG(gKeyLog, LogLevel::Info,
1558 (" HandleKeyPressEvent(), dispatched eKeyDown event and "
1559 "stopped handling the event because %s",
1560 aWindow->IsDestroyed() ? "the window has been destroyed"
1561 : "the event was consumed"));
1562 return;
1564 MOZ_LOG(gKeyLog, LogLevel::Info,
1565 (" HandleKeyPressEvent(), dispatched eKeyDown event and "
1566 "it wasn't consumed"));
1567 handlingState = KeyHandlingState::eNotHandledButEventDispatched;
1570 // If a keydown event handler causes to enable IME, i.e., it moves
1571 // focus from IME unusable content to IME usable editor, we should
1572 // send the native key event to IME for the first input on the editor.
1573 imContext = aWindow->GetIMContext();
1574 if (!IMEWasEnabled && imContext && imContext->IsEnabled()) {
1575 // Notice our keydown event was already dispatched. This prevents
1576 // unnecessary DOM keydown event in the editor.
1577 handlingState = imContext->OnKeyEvent(aWindow, aGdkKeyEvent, true);
1578 if (handlingState == KeyHandlingState::eHandled) {
1579 MOZ_LOG(gKeyLog, LogLevel::Info,
1580 (" HandleKeyPressEvent(), the event was handled by "
1581 "IMContextWrapper which was enabled by the preceding eKeyDown "
1582 "event"));
1583 return;
1587 // Look for specialized app-command keys
1588 switch (aGdkKeyEvent->keyval) {
1589 case GDK_Back:
1590 aWindow->DispatchCommandEvent(nsGkAtoms::Back);
1591 MOZ_LOG(gKeyLog, LogLevel::Info,
1592 (" HandleKeyPressEvent(), dispatched \"Back\" command event"));
1593 return;
1594 case GDK_Forward:
1595 aWindow->DispatchCommandEvent(nsGkAtoms::Forward);
1596 MOZ_LOG(gKeyLog, LogLevel::Info,
1597 (" HandleKeyPressEvent(), dispatched \"Forward\" command "
1598 "event"));
1599 return;
1600 case GDK_Reload:
1601 case GDK_Refresh:
1602 aWindow->DispatchCommandEvent(nsGkAtoms::Reload);
1603 return;
1604 case GDK_Stop:
1605 aWindow->DispatchCommandEvent(nsGkAtoms::Stop);
1606 MOZ_LOG(gKeyLog, LogLevel::Info,
1607 (" HandleKeyPressEvent(), dispatched \"Stop\" command event"));
1608 return;
1609 case GDK_Search:
1610 aWindow->DispatchCommandEvent(nsGkAtoms::Search);
1611 MOZ_LOG(gKeyLog, LogLevel::Info,
1612 (" HandleKeyPressEvent(), dispatched \"Search\" command event"));
1613 return;
1614 case GDK_Favorites:
1615 aWindow->DispatchCommandEvent(nsGkAtoms::Bookmarks);
1616 MOZ_LOG(gKeyLog, LogLevel::Info,
1617 (" HandleKeyPressEvent(), dispatched \"Bookmarks\" command "
1618 "event"));
1619 return;
1620 case GDK_HomePage:
1621 aWindow->DispatchCommandEvent(nsGkAtoms::Home);
1622 return;
1623 case GDK_Copy:
1624 case GDK_F16: // F16, F20, F18, F14 are old keysyms for Copy Cut Paste Undo
1625 aWindow->DispatchContentCommandEvent(eContentCommandCopy);
1626 MOZ_LOG(gKeyLog, LogLevel::Info,
1627 (" HandleKeyPressEvent(), dispatched \"Copy\" content command "
1628 "event"));
1629 return;
1630 case GDK_Cut:
1631 case GDK_F20:
1632 aWindow->DispatchContentCommandEvent(eContentCommandCut);
1633 MOZ_LOG(gKeyLog, LogLevel::Info,
1634 (" HandleKeyPressEvent(), dispatched \"Cut\" content command "
1635 "event"));
1636 return;
1637 case GDK_Paste:
1638 case GDK_F18:
1639 aWindow->DispatchContentCommandEvent(eContentCommandPaste);
1640 MOZ_LOG(gKeyLog, LogLevel::Info,
1641 (" HandleKeyPressEvent(), dispatched \"Paste\" content command "
1642 "event"));
1643 return;
1644 case GDK_Redo:
1645 aWindow->DispatchContentCommandEvent(eContentCommandRedo);
1646 return;
1647 case GDK_Undo:
1648 case GDK_F14:
1649 aWindow->DispatchContentCommandEvent(eContentCommandUndo);
1650 MOZ_LOG(gKeyLog, LogLevel::Info,
1651 (" HandleKeyPressEvent(), dispatched \"Undo\" content command "
1652 "event"));
1653 return;
1654 default:
1655 break;
1658 // before we dispatch a key, check if it's the context menu key.
1659 // If so, send a context menu key event instead.
1660 if (MaybeDispatchContextMenuEvent(aWindow, aGdkKeyEvent)) {
1661 MOZ_LOG(gKeyLog, LogLevel::Info,
1662 (" HandleKeyPressEvent(), stopped dispatching eKeyPress event "
1663 "because eContextMenu event was dispatched"));
1664 return;
1667 RefPtr<TextEventDispatcher> textEventDispatcher =
1668 aWindow->GetTextEventDispatcher();
1669 nsresult rv = textEventDispatcher->BeginNativeInputTransaction();
1670 if (NS_WARN_IF(NS_FAILED(rv))) {
1671 MOZ_LOG(gKeyLog, LogLevel::Error,
1672 (" HandleKeyPressEvent(), stopped dispatching eKeyPress event "
1673 "because of failed to initialize TextEventDispatcher"));
1674 return;
1677 // If the character code is in the BMP, send the key press event.
1678 // Otherwise, send a compositionchange event with the equivalent UTF-16
1679 // string.
1680 // TODO: Investigate other browser's behavior in this case because
1681 // this hack is odd for UI Events.
1682 WidgetKeyboardEvent keypressEvent(true, eKeyPress, aWindow);
1683 KeymapWrapper::InitKeyEvent(keypressEvent, aGdkKeyEvent, false);
1684 nsEventStatus status = nsEventStatus_eIgnore;
1685 if (keypressEvent.mKeyNameIndex != KEY_NAME_INDEX_USE_STRING ||
1686 keypressEvent.mKeyValue.Length() == 1) {
1687 if (textEventDispatcher->MaybeDispatchKeypressEvents(keypressEvent, status,
1688 aGdkKeyEvent)) {
1689 MOZ_LOG(gKeyLog, LogLevel::Info,
1690 (" HandleKeyPressEvent(), dispatched eKeyPress event "
1691 "(status=%s)",
1692 GetStatusName(status)));
1693 } else {
1694 MOZ_LOG(gKeyLog, LogLevel::Info,
1695 (" HandleKeyPressEvent(), didn't dispatch eKeyPress event "
1696 "(status=%s)",
1697 GetStatusName(status)));
1699 } else {
1700 WidgetEventTime eventTime = aWindow->GetWidgetEventTime(aGdkKeyEvent->time);
1701 textEventDispatcher->CommitComposition(status, &keypressEvent.mKeyValue,
1702 &eventTime);
1703 MOZ_LOG(gKeyLog, LogLevel::Info,
1704 (" HandleKeyPressEvent(), dispatched a set of composition "
1705 "events"));
1709 /* static */
1710 bool KeymapWrapper::HandleKeyReleaseEvent(nsWindow* aWindow,
1711 GdkEventKey* aGdkKeyEvent) {
1712 MOZ_LOG(gKeyLog, LogLevel::Info,
1713 ("HandleKeyReleaseEvent(aWindow=%p, aGdkKeyEvent={ type=%s, "
1714 "keyval=%s(0x%X), state=0x%08X, hardware_keycode=0x%08X, "
1715 "time=%u, is_modifier=%s })",
1716 aWindow,
1717 ((aGdkKeyEvent->type == GDK_KEY_PRESS) ? "GDK_KEY_PRESS"
1718 : "GDK_KEY_RELEASE"),
1719 gdk_keyval_name(aGdkKeyEvent->keyval), aGdkKeyEvent->keyval,
1720 aGdkKeyEvent->state, aGdkKeyEvent->hardware_keycode,
1721 aGdkKeyEvent->time, GetBoolName(aGdkKeyEvent->is_modifier)));
1723 RefPtr<IMContextWrapper> imContext = aWindow->GetIMContext();
1724 if (imContext) {
1725 KeyHandlingState handlingState =
1726 imContext->OnKeyEvent(aWindow, aGdkKeyEvent);
1727 if (handlingState != KeyHandlingState::eNotHandled) {
1728 MOZ_LOG(gKeyLog, LogLevel::Info,
1729 (" HandleKeyReleaseEvent(), the event was handled by "
1730 "IMContextWrapper"));
1731 return true;
1735 bool isCancelled = false;
1736 if (NS_WARN_IF(!DispatchKeyDownOrKeyUpEvent(aWindow, aGdkKeyEvent, false,
1737 &isCancelled))) {
1738 MOZ_LOG(gKeyLog, LogLevel::Error,
1739 (" HandleKeyReleaseEvent(), didn't dispatch eKeyUp event"));
1740 return false;
1743 MOZ_LOG(gKeyLog, LogLevel::Info,
1744 (" HandleKeyReleaseEvent(), dispatched eKeyUp event "
1745 "(isCancelled=%s)",
1746 GetBoolName(isCancelled)));
1747 return true;
1750 /* static */
1751 void KeymapWrapper::InitKeyEvent(WidgetKeyboardEvent& aKeyEvent,
1752 GdkEventKey* aGdkKeyEvent,
1753 bool aIsProcessedByIME) {
1754 MOZ_ASSERT(
1755 !aIsProcessedByIME || aKeyEvent.mMessage != eKeyPress,
1756 "If the key event is handled by IME, keypress event shouldn't be fired");
1758 KeymapWrapper* keymapWrapper = GetInstance();
1760 aKeyEvent.mCodeNameIndex = ComputeDOMCodeNameIndex(aGdkKeyEvent);
1761 MOZ_ASSERT(aKeyEvent.mCodeNameIndex != CODE_NAME_INDEX_USE_STRING);
1762 aKeyEvent.mKeyNameIndex =
1763 aIsProcessedByIME ? KEY_NAME_INDEX_Process
1764 : keymapWrapper->ComputeDOMKeyNameIndex(aGdkKeyEvent);
1765 if (aKeyEvent.mKeyNameIndex == KEY_NAME_INDEX_Unidentified) {
1766 uint32_t charCode = GetCharCodeFor(aGdkKeyEvent);
1767 if (!charCode) {
1768 charCode = keymapWrapper->GetUnmodifiedCharCodeFor(aGdkKeyEvent);
1770 if (charCode) {
1771 aKeyEvent.mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
1772 MOZ_ASSERT(aKeyEvent.mKeyValue.IsEmpty(),
1773 "Uninitialized mKeyValue must be empty");
1774 AppendUCS4ToUTF16(charCode, aKeyEvent.mKeyValue);
1778 if (aIsProcessedByIME) {
1779 aKeyEvent.mKeyCode = NS_VK_PROCESSKEY;
1780 } else if (aKeyEvent.mKeyNameIndex != KEY_NAME_INDEX_USE_STRING ||
1781 aKeyEvent.mMessage != eKeyPress) {
1782 aKeyEvent.mKeyCode = ComputeDOMKeyCode(aGdkKeyEvent);
1783 } else {
1784 aKeyEvent.mKeyCode = 0;
1787 // NOTE: The state of given key event indicates adjacent state of
1788 // modifier keys. E.g., even if the event is Shift key press event,
1789 // the bit for Shift is still false. By the same token, even if the
1790 // event is Shift key release event, the bit for Shift is still true.
1791 // Unfortunately, gdk_keyboard_get_modifiers() returns current modifier
1792 // state. It means if there're some pending modifier key press or
1793 // key release events, the result isn't what we want.
1794 guint modifierState = aGdkKeyEvent->state;
1795 GdkDisplay* gdkDisplay = gdk_display_get_default();
1796 #ifdef MOZ_X11
1797 if (aGdkKeyEvent->is_modifier && GdkIsX11Display(gdkDisplay)) {
1798 Display* display = gdk_x11_display_get_xdisplay(gdkDisplay);
1799 if (XEventsQueued(display, QueuedAfterReading)) {
1800 XEvent nextEvent;
1801 XPeekEvent(display, &nextEvent);
1802 if (nextEvent.type == keymapWrapper->mXKBBaseEventCode) {
1803 XkbEvent* XKBEvent = (XkbEvent*)&nextEvent;
1804 if (XKBEvent->any.xkb_type == XkbStateNotify) {
1805 XkbStateNotifyEvent* stateNotifyEvent =
1806 (XkbStateNotifyEvent*)XKBEvent;
1807 modifierState &= ~0xFF;
1808 modifierState |= stateNotifyEvent->lookup_mods;
1813 #endif
1814 InitInputEvent(aKeyEvent, modifierState);
1816 switch (aGdkKeyEvent->keyval) {
1817 case GDK_Shift_L:
1818 case GDK_Control_L:
1819 case GDK_Alt_L:
1820 case GDK_Super_L:
1821 case GDK_Hyper_L:
1822 case GDK_Meta_L:
1823 aKeyEvent.mLocation = eKeyLocationLeft;
1824 break;
1826 case GDK_Shift_R:
1827 case GDK_Control_R:
1828 case GDK_Alt_R:
1829 case GDK_Super_R:
1830 case GDK_Hyper_R:
1831 case GDK_Meta_R:
1832 aKeyEvent.mLocation = eKeyLocationRight;
1833 break;
1835 case GDK_KP_0:
1836 case GDK_KP_1:
1837 case GDK_KP_2:
1838 case GDK_KP_3:
1839 case GDK_KP_4:
1840 case GDK_KP_5:
1841 case GDK_KP_6:
1842 case GDK_KP_7:
1843 case GDK_KP_8:
1844 case GDK_KP_9:
1845 case GDK_KP_Space:
1846 case GDK_KP_Tab:
1847 case GDK_KP_Enter:
1848 case GDK_KP_F1:
1849 case GDK_KP_F2:
1850 case GDK_KP_F3:
1851 case GDK_KP_F4:
1852 case GDK_KP_Home:
1853 case GDK_KP_Left:
1854 case GDK_KP_Up:
1855 case GDK_KP_Right:
1856 case GDK_KP_Down:
1857 case GDK_KP_Prior: // same as GDK_KP_Page_Up
1858 case GDK_KP_Next: // same as GDK_KP_Page_Down
1859 case GDK_KP_End:
1860 case GDK_KP_Begin:
1861 case GDK_KP_Insert:
1862 case GDK_KP_Delete:
1863 case GDK_KP_Equal:
1864 case GDK_KP_Multiply:
1865 case GDK_KP_Add:
1866 case GDK_KP_Separator:
1867 case GDK_KP_Subtract:
1868 case GDK_KP_Decimal:
1869 case GDK_KP_Divide:
1870 aKeyEvent.mLocation = eKeyLocationNumpad;
1871 break;
1873 default:
1874 aKeyEvent.mLocation = eKeyLocationStandard;
1875 break;
1878 // The transformations above and in gdk for the keyval are not invertible
1879 // so link to the GdkEvent (which will vanish soon after return from the
1880 // event callback) to give plugins access to hardware_keycode and state.
1881 // (An XEvent would be nice but the GdkEvent is good enough.)
1882 aKeyEvent.mTime = aGdkKeyEvent->time;
1883 aKeyEvent.mNativeKeyEvent = static_cast<void*>(aGdkKeyEvent);
1884 aKeyEvent.mIsRepeat =
1885 sRepeatState == REPEATING &&
1886 aGdkKeyEvent->hardware_keycode == sLastRepeatableHardwareKeyCode;
1888 MOZ_LOG(
1889 gKeyLog, LogLevel::Info,
1890 ("%p InitKeyEvent, modifierState=0x%08X "
1891 "aKeyEvent={ mMessage=%s, isShift=%s, isControl=%s, "
1892 "isAlt=%s, isMeta=%s , mKeyCode=0x%02X, mCharCode=%s, "
1893 "mKeyNameIndex=%s, mKeyValue=%s, mCodeNameIndex=%s, mCodeValue=%s, "
1894 "mLocation=%s, mIsRepeat=%s }",
1895 keymapWrapper, modifierState, ToChar(aKeyEvent.mMessage),
1896 GetBoolName(aKeyEvent.IsShift()), GetBoolName(aKeyEvent.IsControl()),
1897 GetBoolName(aKeyEvent.IsAlt()), GetBoolName(aKeyEvent.IsMeta()),
1898 aKeyEvent.mKeyCode,
1899 GetCharacterCodeName(static_cast<char16_t>(aKeyEvent.mCharCode)).get(),
1900 ToString(aKeyEvent.mKeyNameIndex).get(),
1901 GetCharacterCodeNames(aKeyEvent.mKeyValue).get(),
1902 ToString(aKeyEvent.mCodeNameIndex).get(),
1903 GetCharacterCodeNames(aKeyEvent.mCodeValue).get(),
1904 GetKeyLocationName(aKeyEvent.mLocation).get(),
1905 GetBoolName(aKeyEvent.mIsRepeat)));
1908 /* static */
1909 uint32_t KeymapWrapper::GetCharCodeFor(const GdkEventKey* aGdkKeyEvent) {
1910 // Anything above 0xf000 is considered a non-printable
1911 // Exception: directly encoded UCS characters
1912 if (aGdkKeyEvent->keyval > 0xf000 &&
1913 (aGdkKeyEvent->keyval & 0xff000000) != 0x01000000) {
1914 // Keypad keys are an exception: they return a value different
1915 // from their non-keypad equivalents, but mozilla doesn't distinguish.
1916 switch (aGdkKeyEvent->keyval) {
1917 case GDK_KP_Space:
1918 return ' ';
1919 case GDK_KP_Equal:
1920 return '=';
1921 case GDK_KP_Multiply:
1922 return '*';
1923 case GDK_KP_Add:
1924 return '+';
1925 case GDK_KP_Separator:
1926 return ',';
1927 case GDK_KP_Subtract:
1928 return '-';
1929 case GDK_KP_Decimal:
1930 return '.';
1931 case GDK_KP_Divide:
1932 return '/';
1933 case GDK_KP_0:
1934 return '0';
1935 case GDK_KP_1:
1936 return '1';
1937 case GDK_KP_2:
1938 return '2';
1939 case GDK_KP_3:
1940 return '3';
1941 case GDK_KP_4:
1942 return '4';
1943 case GDK_KP_5:
1944 return '5';
1945 case GDK_KP_6:
1946 return '6';
1947 case GDK_KP_7:
1948 return '7';
1949 case GDK_KP_8:
1950 return '8';
1951 case GDK_KP_9:
1952 return '9';
1953 default:
1954 return 0; // non-printables
1958 static const long MAX_UNICODE = 0x10FFFF;
1960 // we're supposedly printable, let's try to convert
1961 long ucs = keysym2ucs(aGdkKeyEvent->keyval);
1962 if ((ucs != -1) && (ucs < MAX_UNICODE)) {
1963 return ucs;
1966 // I guess we couldn't convert
1967 return 0;
1970 uint32_t KeymapWrapper::GetCharCodeFor(const GdkEventKey* aGdkKeyEvent,
1971 guint aModifierState, gint aGroup) {
1972 guint keyval;
1973 if (!gdk_keymap_translate_keyboard_state(
1974 mGdkKeymap, aGdkKeyEvent->hardware_keycode,
1975 GdkModifierType(aModifierState), aGroup, &keyval, nullptr, nullptr,
1976 nullptr)) {
1977 return 0;
1979 GdkEventKey tmpEvent = *aGdkKeyEvent;
1980 tmpEvent.state = aModifierState;
1981 tmpEvent.keyval = keyval;
1982 tmpEvent.group = aGroup;
1983 return GetCharCodeFor(&tmpEvent);
1986 uint32_t KeymapWrapper::GetUnmodifiedCharCodeFor(
1987 const GdkEventKey* aGdkKeyEvent) {
1988 guint state = aGdkKeyEvent->state &
1989 (GetModifierMask(SHIFT) | GetModifierMask(CAPS_LOCK) |
1990 GetModifierMask(NUM_LOCK) | GetModifierMask(SCROLL_LOCK) |
1991 GetModifierMask(LEVEL3) | GetModifierMask(LEVEL5));
1992 uint32_t charCode =
1993 GetCharCodeFor(aGdkKeyEvent, GdkModifierType(state), aGdkKeyEvent->group);
1994 if (charCode) {
1995 return charCode;
1997 // If no character is mapped to the key when Level3 Shift or Level5 Shift
1998 // is active, let's return a character which is inputted by the key without
1999 // Level3 nor Level5 Shift.
2000 guint stateWithoutAltGraph =
2001 state & ~(GetModifierMask(LEVEL3) | GetModifierMask(LEVEL5));
2002 if (state == stateWithoutAltGraph) {
2003 return 0;
2005 return GetCharCodeFor(aGdkKeyEvent, GdkModifierType(stateWithoutAltGraph),
2006 aGdkKeyEvent->group);
2009 gint KeymapWrapper::GetKeyLevel(GdkEventKey* aGdkKeyEvent) {
2010 gint level;
2011 if (!gdk_keymap_translate_keyboard_state(
2012 mGdkKeymap, aGdkKeyEvent->hardware_keycode,
2013 GdkModifierType(aGdkKeyEvent->state), aGdkKeyEvent->group, nullptr,
2014 nullptr, &level, nullptr)) {
2015 return -1;
2017 return level;
2020 gint KeymapWrapper::GetFirstLatinGroup() {
2021 GdkKeymapKey* keys;
2022 gint count;
2023 gint minGroup = -1;
2024 if (gdk_keymap_get_entries_for_keyval(mGdkKeymap, GDK_a, &keys, &count)) {
2025 // find the minimum number group for latin inputtable layout
2026 for (gint i = 0; i < count && minGroup != 0; ++i) {
2027 if (keys[i].level != 0 && keys[i].level != 1) {
2028 continue;
2030 if (minGroup >= 0 && keys[i].group > minGroup) {
2031 continue;
2033 minGroup = keys[i].group;
2035 g_free(keys);
2037 return minGroup;
2040 bool KeymapWrapper::IsLatinGroup(guint8 aGroup) {
2041 GdkKeymapKey* keys;
2042 gint count;
2043 bool result = false;
2044 if (gdk_keymap_get_entries_for_keyval(mGdkKeymap, GDK_a, &keys, &count)) {
2045 for (gint i = 0; i < count; ++i) {
2046 if (keys[i].level != 0 && keys[i].level != 1) {
2047 continue;
2049 if (keys[i].group == aGroup) {
2050 result = true;
2051 break;
2054 g_free(keys);
2056 return result;
2059 bool KeymapWrapper::IsAutoRepeatableKey(guint aHardwareKeyCode) {
2060 #ifdef MOZ_X11
2061 uint8_t indexOfArray = aHardwareKeyCode / 8;
2062 MOZ_ASSERT(indexOfArray < ArrayLength(mKeyboardState.auto_repeats),
2063 "invalid index");
2064 char bitMask = 1 << (aHardwareKeyCode % 8);
2065 return (mKeyboardState.auto_repeats[indexOfArray] & bitMask) != 0;
2066 #else
2067 return false;
2068 #endif
2071 /* static */
2072 bool KeymapWrapper::IsBasicLatinLetterOrNumeral(uint32_t aCharCode) {
2073 return (aCharCode >= 'a' && aCharCode <= 'z') ||
2074 (aCharCode >= 'A' && aCharCode <= 'Z') ||
2075 (aCharCode >= '0' && aCharCode <= '9');
2078 /* static */
2079 guint KeymapWrapper::GetGDKKeyvalWithoutModifier(
2080 const GdkEventKey* aGdkKeyEvent) {
2081 KeymapWrapper* keymapWrapper = GetInstance();
2082 guint state =
2083 (aGdkKeyEvent->state & keymapWrapper->GetModifierMask(NUM_LOCK));
2084 guint keyval;
2085 if (!gdk_keymap_translate_keyboard_state(
2086 keymapWrapper->mGdkKeymap, aGdkKeyEvent->hardware_keycode,
2087 GdkModifierType(state), aGdkKeyEvent->group, &keyval, nullptr,
2088 nullptr, nullptr)) {
2089 return 0;
2091 return keyval;
2094 /* static */
2095 uint32_t KeymapWrapper::GetDOMKeyCodeFromKeyPairs(guint aGdkKeyval) {
2096 switch (aGdkKeyval) {
2097 case GDK_Cancel:
2098 return NS_VK_CANCEL;
2099 case GDK_BackSpace:
2100 return NS_VK_BACK;
2101 case GDK_Tab:
2102 case GDK_ISO_Left_Tab:
2103 return NS_VK_TAB;
2104 case GDK_Clear:
2105 return NS_VK_CLEAR;
2106 case GDK_Return:
2107 return NS_VK_RETURN;
2108 case GDK_Shift_L:
2109 case GDK_Shift_R:
2110 case GDK_Shift_Lock:
2111 return NS_VK_SHIFT;
2112 case GDK_Control_L:
2113 case GDK_Control_R:
2114 return NS_VK_CONTROL;
2115 case GDK_Alt_L:
2116 case GDK_Alt_R:
2117 return NS_VK_ALT;
2118 case GDK_Meta_L:
2119 case GDK_Meta_R:
2120 return NS_VK_META;
2122 // Assume that Super or Hyper is always mapped to physical Win key.
2123 case GDK_Super_L:
2124 case GDK_Super_R:
2125 case GDK_Hyper_L:
2126 case GDK_Hyper_R:
2127 return NS_VK_WIN;
2129 // GTK's AltGraph key is similar to Mac's Option (Alt) key. However,
2130 // unfortunately, browsers on Mac are using NS_VK_ALT for it even though
2131 // it's really different from Alt key on Windows.
2132 // On the other hand, GTK's AltGrapsh keys are really different from
2133 // Alt key. However, there is no AltGrapsh key on Windows. On Windows,
2134 // both Ctrl and Alt keys are pressed internally when AltGr key is
2135 // pressed. For some languages' users, AltGraph key is important, so,
2136 // web applications on such locale may want to know AltGraph key press.
2137 // Therefore, we should map AltGr keycode for them only on GTK.
2138 case GDK_ISO_Level3_Shift:
2139 case GDK_ISO_Level5_Shift:
2140 // We assume that Mode_switch is always used for level3 shift.
2141 case GDK_Mode_switch:
2142 return NS_VK_ALTGR;
2144 case GDK_Pause:
2145 return NS_VK_PAUSE;
2146 case GDK_Caps_Lock:
2147 return NS_VK_CAPS_LOCK;
2148 case GDK_Kana_Lock:
2149 case GDK_Kana_Shift:
2150 return NS_VK_KANA;
2151 case GDK_Hangul:
2152 return NS_VK_HANGUL;
2153 // case GDK_XXX: return NS_VK_JUNJA;
2154 // case GDK_XXX: return NS_VK_FINAL;
2155 case GDK_Hangul_Hanja:
2156 return NS_VK_HANJA;
2157 case GDK_Kanji:
2158 return NS_VK_KANJI;
2159 case GDK_Escape:
2160 return NS_VK_ESCAPE;
2161 case GDK_Henkan:
2162 return NS_VK_CONVERT;
2163 case GDK_Muhenkan:
2164 return NS_VK_NONCONVERT;
2165 // case GDK_XXX: return NS_VK_ACCEPT;
2166 // case GDK_XXX: return NS_VK_MODECHANGE;
2167 case GDK_Page_Up:
2168 return NS_VK_PAGE_UP;
2169 case GDK_Page_Down:
2170 return NS_VK_PAGE_DOWN;
2171 case GDK_End:
2172 return NS_VK_END;
2173 case GDK_Home:
2174 return NS_VK_HOME;
2175 case GDK_Left:
2176 return NS_VK_LEFT;
2177 case GDK_Up:
2178 return NS_VK_UP;
2179 case GDK_Right:
2180 return NS_VK_RIGHT;
2181 case GDK_Down:
2182 return NS_VK_DOWN;
2183 case GDK_Select:
2184 return NS_VK_SELECT;
2185 case GDK_Print:
2186 return NS_VK_PRINT;
2187 case GDK_Execute:
2188 return NS_VK_EXECUTE;
2189 case GDK_Insert:
2190 return NS_VK_INSERT;
2191 case GDK_Delete:
2192 return NS_VK_DELETE;
2193 case GDK_Help:
2194 return NS_VK_HELP;
2196 // keypad keys
2197 case GDK_KP_Left:
2198 return NS_VK_LEFT;
2199 case GDK_KP_Right:
2200 return NS_VK_RIGHT;
2201 case GDK_KP_Up:
2202 return NS_VK_UP;
2203 case GDK_KP_Down:
2204 return NS_VK_DOWN;
2205 case GDK_KP_Page_Up:
2206 return NS_VK_PAGE_UP;
2207 // Not sure what these are
2208 // case GDK_KP_Prior: return NS_VK_;
2209 // case GDK_KP_Next: return NS_VK_;
2210 case GDK_KP_Begin:
2211 return NS_VK_CLEAR; // Num-unlocked 5
2212 case GDK_KP_Page_Down:
2213 return NS_VK_PAGE_DOWN;
2214 case GDK_KP_Home:
2215 return NS_VK_HOME;
2216 case GDK_KP_End:
2217 return NS_VK_END;
2218 case GDK_KP_Insert:
2219 return NS_VK_INSERT;
2220 case GDK_KP_Delete:
2221 return NS_VK_DELETE;
2222 case GDK_KP_Enter:
2223 return NS_VK_RETURN;
2225 case GDK_Num_Lock:
2226 return NS_VK_NUM_LOCK;
2227 case GDK_Scroll_Lock:
2228 return NS_VK_SCROLL_LOCK;
2230 // Function keys
2231 case GDK_F1:
2232 return NS_VK_F1;
2233 case GDK_F2:
2234 return NS_VK_F2;
2235 case GDK_F3:
2236 return NS_VK_F3;
2237 case GDK_F4:
2238 return NS_VK_F4;
2239 case GDK_F5:
2240 return NS_VK_F5;
2241 case GDK_F6:
2242 return NS_VK_F6;
2243 case GDK_F7:
2244 return NS_VK_F7;
2245 case GDK_F8:
2246 return NS_VK_F8;
2247 case GDK_F9:
2248 return NS_VK_F9;
2249 case GDK_F10:
2250 return NS_VK_F10;
2251 case GDK_F11:
2252 return NS_VK_F11;
2253 case GDK_F12:
2254 return NS_VK_F12;
2255 case GDK_F13:
2256 return NS_VK_F13;
2257 case GDK_F14:
2258 return NS_VK_F14;
2259 case GDK_F15:
2260 return NS_VK_F15;
2261 case GDK_F16:
2262 return NS_VK_F16;
2263 case GDK_F17:
2264 return NS_VK_F17;
2265 case GDK_F18:
2266 return NS_VK_F18;
2267 case GDK_F19:
2268 return NS_VK_F19;
2269 case GDK_F20:
2270 return NS_VK_F20;
2271 case GDK_F21:
2272 return NS_VK_F21;
2273 case GDK_F22:
2274 return NS_VK_F22;
2275 case GDK_F23:
2276 return NS_VK_F23;
2277 case GDK_F24:
2278 return NS_VK_F24;
2280 // context menu key, keysym 0xff67, typically keycode 117 on 105-key
2281 // (Microsoft) x86 keyboards, located between right 'Windows' key and
2282 // right Ctrl key
2283 case GDK_Menu:
2284 return NS_VK_CONTEXT_MENU;
2285 case GDK_Sleep:
2286 return NS_VK_SLEEP;
2288 case GDK_3270_Attn:
2289 return NS_VK_ATTN;
2290 case GDK_3270_CursorSelect:
2291 return NS_VK_CRSEL;
2292 case GDK_3270_ExSelect:
2293 return NS_VK_EXSEL;
2294 case GDK_3270_EraseEOF:
2295 return NS_VK_EREOF;
2296 case GDK_3270_Play:
2297 return NS_VK_PLAY;
2298 // case GDK_XXX: return NS_VK_ZOOM;
2299 case GDK_3270_PA1:
2300 return NS_VK_PA1;
2302 // map Sun Keyboard special keysyms on to NS_VK keys
2304 // Sun F11 key generates SunF36(0x1005ff10) keysym
2305 case 0x1005ff10:
2306 return NS_VK_F11;
2307 // Sun F12 key generates SunF37(0x1005ff11) keysym
2308 case 0x1005ff11:
2309 return NS_VK_F12;
2310 default:
2311 return 0;
2315 void KeymapWrapper::WillDispatchKeyboardEvent(WidgetKeyboardEvent& aKeyEvent,
2316 GdkEventKey* aGdkKeyEvent) {
2317 GetInstance()->WillDispatchKeyboardEventInternal(aKeyEvent, aGdkKeyEvent);
2320 void KeymapWrapper::WillDispatchKeyboardEventInternal(
2321 WidgetKeyboardEvent& aKeyEvent, GdkEventKey* aGdkKeyEvent) {
2322 if (!aGdkKeyEvent) {
2323 // If aGdkKeyEvent is nullptr, we're trying to dispatch a fake keyboard
2324 // event in such case, we don't need to set alternative char codes.
2325 // So, we don't need to do nothing here. This case is typically we're
2326 // dispatching eKeyDown or eKeyUp event during composition.
2327 return;
2330 uint32_t charCode = GetCharCodeFor(aGdkKeyEvent);
2331 if (!charCode) {
2332 MOZ_LOG(gKeyLog, LogLevel::Info,
2333 ("%p WillDispatchKeyboardEventInternal, "
2334 "mKeyCode=0x%02X, charCode=0x%08X",
2335 this, aKeyEvent.mKeyCode, aKeyEvent.mCharCode));
2336 return;
2339 // The mCharCode was set from mKeyValue. However, for example, when Ctrl key
2340 // is pressed, its value should indicate an ASCII character for backward
2341 // compatibility rather than inputting character without the modifiers.
2342 // Therefore, we need to modify mCharCode value here.
2343 aKeyEvent.SetCharCode(charCode);
2345 gint level = GetKeyLevel(aGdkKeyEvent);
2346 if (level != 0 && level != 1) {
2347 MOZ_LOG(gKeyLog, LogLevel::Info,
2348 ("%p WillDispatchKeyboardEventInternal, "
2349 "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d",
2350 this, aKeyEvent.mKeyCode, aKeyEvent.mCharCode, level));
2351 return;
2354 guint baseState =
2355 aGdkKeyEvent->state & ~(GetModifierMask(SHIFT) | GetModifierMask(CTRL) |
2356 GetModifierMask(ALT) | GetModifierMask(META) |
2357 GetModifierMask(SUPER) | GetModifierMask(HYPER));
2359 // We shold send both shifted char and unshifted char, all keyboard layout
2360 // users can use all keys. Don't change event.mCharCode. On some keyboard
2361 // layouts, Ctrl/Alt/Meta keys are used for inputting some characters.
2362 AlternativeCharCode altCharCodes(0, 0);
2363 // unshifted charcode of current keyboard layout.
2364 altCharCodes.mUnshiftedCharCode =
2365 GetCharCodeFor(aGdkKeyEvent, baseState, aGdkKeyEvent->group);
2366 bool isLatin = (altCharCodes.mUnshiftedCharCode <= 0xFF);
2367 // shifted charcode of current keyboard layout.
2368 altCharCodes.mShiftedCharCode = GetCharCodeFor(
2369 aGdkKeyEvent, baseState | GetModifierMask(SHIFT), aGdkKeyEvent->group);
2370 isLatin = isLatin && (altCharCodes.mShiftedCharCode <= 0xFF);
2371 if (altCharCodes.mUnshiftedCharCode || altCharCodes.mShiftedCharCode) {
2372 aKeyEvent.mAlternativeCharCodes.AppendElement(altCharCodes);
2375 bool needLatinKeyCodes = !isLatin;
2376 if (!needLatinKeyCodes) {
2377 needLatinKeyCodes =
2378 (IS_ASCII_ALPHABETICAL(altCharCodes.mUnshiftedCharCode) !=
2379 IS_ASCII_ALPHABETICAL(altCharCodes.mShiftedCharCode));
2382 // If current keyboard layout can input Latin characters, we don't need
2383 // more information.
2384 if (!needLatinKeyCodes) {
2385 MOZ_LOG(gKeyLog, LogLevel::Info,
2386 ("%p WillDispatchKeyboardEventInternal, "
2387 "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d, altCharCodes={ "
2388 "mUnshiftedCharCode=0x%08X, mShiftedCharCode=0x%08X }",
2389 this, aKeyEvent.mKeyCode, aKeyEvent.mCharCode, level,
2390 altCharCodes.mUnshiftedCharCode, altCharCodes.mShiftedCharCode));
2391 return;
2394 // Next, find Latin inputtable keyboard layout.
2395 gint minGroup = GetFirstLatinGroup();
2396 if (minGroup < 0) {
2397 MOZ_LOG(gKeyLog, LogLevel::Info,
2398 ("%p WillDispatchKeyboardEventInternal, "
2399 "Latin keyboard layout isn't found: "
2400 "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d, "
2401 "altCharCodes={ mUnshiftedCharCode=0x%08X, "
2402 "mShiftedCharCode=0x%08X }",
2403 this, aKeyEvent.mKeyCode, aKeyEvent.mCharCode, level,
2404 altCharCodes.mUnshiftedCharCode, altCharCodes.mShiftedCharCode));
2405 return;
2408 AlternativeCharCode altLatinCharCodes(0, 0);
2409 uint32_t unmodifiedCh = aKeyEvent.IsShift() ? altCharCodes.mShiftedCharCode
2410 : altCharCodes.mUnshiftedCharCode;
2412 // unshifted charcode of found keyboard layout.
2413 uint32_t ch = GetCharCodeFor(aGdkKeyEvent, baseState, minGroup);
2414 altLatinCharCodes.mUnshiftedCharCode =
2415 IsBasicLatinLetterOrNumeral(ch) ? ch : 0;
2416 // shifted charcode of found keyboard layout.
2417 ch = GetCharCodeFor(aGdkKeyEvent, baseState | GetModifierMask(SHIFT),
2418 minGroup);
2419 altLatinCharCodes.mShiftedCharCode = IsBasicLatinLetterOrNumeral(ch) ? ch : 0;
2420 if (altLatinCharCodes.mUnshiftedCharCode ||
2421 altLatinCharCodes.mShiftedCharCode) {
2422 aKeyEvent.mAlternativeCharCodes.AppendElement(altLatinCharCodes);
2424 // If the mCharCode is not Latin, and the level is 0 or 1, we should
2425 // replace the mCharCode to Latin char if Alt and Meta keys are not
2426 // pressed. (Alt should be sent the localized char for accesskey
2427 // like handling of Web Applications.)
2428 ch = aKeyEvent.IsShift() ? altLatinCharCodes.mShiftedCharCode
2429 : altLatinCharCodes.mUnshiftedCharCode;
2430 if (ch && !(aKeyEvent.IsAlt() || aKeyEvent.IsMeta()) &&
2431 charCode == unmodifiedCh) {
2432 aKeyEvent.SetCharCode(ch);
2435 MOZ_LOG(gKeyLog, LogLevel::Info,
2436 ("%p WillDispatchKeyboardEventInternal, "
2437 "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d, minGroup=%d, "
2438 "altCharCodes={ mUnshiftedCharCode=0x%08X, "
2439 "mShiftedCharCode=0x%08X } "
2440 "altLatinCharCodes={ mUnshiftedCharCode=0x%08X, "
2441 "mShiftedCharCode=0x%08X }",
2442 this, aKeyEvent.mKeyCode, aKeyEvent.mCharCode, level, minGroup,
2443 altCharCodes.mUnshiftedCharCode, altCharCodes.mShiftedCharCode,
2444 altLatinCharCodes.mUnshiftedCharCode,
2445 altLatinCharCodes.mShiftedCharCode));
2448 #ifdef MOZ_WAYLAND
2449 void KeymapWrapper::SetFocusIn(wl_surface* aFocusSurface,
2450 uint32_t aFocusSerial) {
2451 LOGW("KeymapWrapper::SetFocusIn() surface %p ID %d serial %d", aFocusSurface,
2452 aFocusSurface ? wl_proxy_get_id((struct wl_proxy*)aFocusSurface) : 0,
2453 aFocusSerial);
2455 KeymapWrapper* keymapWrapper = KeymapWrapper::GetInstance();
2456 keymapWrapper->mFocusSurface = aFocusSurface;
2457 keymapWrapper->mFocusSerial = aFocusSerial;
2460 // aFocusSurface can be null in case that focused surface is already destroyed.
2461 void KeymapWrapper::SetFocusOut(wl_surface* aFocusSurface) {
2462 KeymapWrapper* keymapWrapper = KeymapWrapper::GetInstance();
2463 LOGW("KeymapWrapper::SetFocusOut surface %p ID %d", aFocusSurface,
2464 aFocusSurface ? wl_proxy_get_id((struct wl_proxy*)aFocusSurface) : 0);
2466 keymapWrapper->mFocusSurface = nullptr;
2467 keymapWrapper->mFocusSerial = 0;
2470 void KeymapWrapper::GetFocusInfo(wl_surface** aFocusSurface,
2471 uint32_t* aFocusSerial) {
2472 KeymapWrapper* keymapWrapper = KeymapWrapper::GetInstance();
2473 *aFocusSurface = keymapWrapper->mFocusSurface;
2474 *aFocusSerial = keymapWrapper->mFocusSerial;
2477 void KeymapWrapper::SetSeat(wl_seat* aSeat) {
2478 KeymapWrapper* keymapWrapper = KeymapWrapper::GetInstance();
2479 keymapWrapper->mSeat = aSeat;
2482 wl_seat* KeymapWrapper::GetSeat() {
2483 KeymapWrapper* keymapWrapper = KeymapWrapper::GetInstance();
2484 return keymapWrapper->mSeat;
2486 #endif
2488 } // namespace widget
2489 } // namespace mozilla