1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
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>
16 #include <gdk/gdkkeysyms-compat.h>
18 # include <gdk/gdkx.h>
19 # include <X11/XKBlib.h>
20 # include "X11UndefineNone.h"
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"
34 #include "mozilla/ArrayUtils.h"
35 #include "mozilla/MouseEvents.h"
36 #include "mozilla/TextEventDispatcher.h"
37 #include "mozilla/TextEvents.h"
40 # include <sys/mman.h>
41 # include "nsWaylandDisplay.h"
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
47 // Therefore you shouldn't use `LogLevel::Verbose` for logging usual behavior.
48 mozilla::LazyLogModule
gKeyLog("KeyboardHandler");
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;
61 Time
KeymapWrapper::sLastRepeatableKeyTime
= 0;
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
) {
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";
79 return "Illegal value";
83 static const nsCString
GetKeyLocationName(uint32_t 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
;
94 return nsPrintfCString("Unknown (0x%04X)", aLocation
);
98 static const nsCString
GetCharacterCodeName(char16_t aChar
) {
101 return "NULL (0x0000)"_ns
;
103 return "BACKSPACE (0x0008)"_ns
;
105 return "CHARACTER TABULATION (0x0009)"_ns
;
107 return "LINE FEED (0x000A)"_ns
;
109 return "LINE TABULATION (0x000B)"_ns
;
111 return "FORM FEED (0x000C)"_ns
;
113 return "CARRIAGE RETURN (0x000D)"_ns
;
115 return "CANCEL (0x0018)"_ns
;
117 return "ESCAPE (0x001B)"_ns
;
119 return "SPACE (0x0020)"_ns
;
121 return "DELETE (0x007F)"_ns
;
123 return "NO-BREAK SPACE (0x00A0)"_ns
;
125 return "SOFT HYPHEN (0x00AD)"_ns
;
127 return "EN QUAD (0x2000)"_ns
;
129 return "EM QUAD (0x2001)"_ns
;
131 return "EN SPACE (0x2002)"_ns
;
133 return "EM SPACE (0x2003)"_ns
;
135 return "THREE-PER-EM SPACE (0x2004)"_ns
;
137 return "FOUR-PER-EM SPACE (0x2005)"_ns
;
139 return "SIX-PER-EM SPACE (0x2006)"_ns
;
141 return "FIGURE SPACE (0x2007)"_ns
;
143 return "PUNCTUATION SPACE (0x2008)"_ns
;
145 return "THIN SPACE (0x2009)"_ns
;
147 return "HAIR SPACE (0x200A)"_ns
;
149 return "ZERO WIDTH SPACE (0x200B)"_ns
;
151 return "ZERO WIDTH NON-JOINER (0x200C)"_ns
;
153 return "ZERO WIDTH JOINER (0x200D)"_ns
;
155 return "LEFT-TO-RIGHT MARK (0x200E)"_ns
;
157 return "RIGHT-TO-LEFT MARK (0x200F)"_ns
;
159 return "PARAGRAPH SEPARATOR (0x2029)"_ns
;
161 return "LEFT-TO-RIGHT EMBEDDING (0x202A)"_ns
;
163 return "RIGHT-TO-LEFT EMBEDDING (0x202B)"_ns
;
165 return "LEFT-TO-RIGHT OVERRIDE (0x202D)"_ns
;
167 return "RIGHT-TO-LEFT OVERRIDE (0x202E)"_ns
;
169 return "NARROW NO-BREAK SPACE (0x202F)"_ns
;
171 return "MEDIUM MATHEMATICAL SPACE (0x205F)"_ns
;
173 return "WORD JOINER (0x2060)"_ns
;
175 return "LEFT-TO-RIGHT ISOLATE (0x2066)"_ns
;
177 return "RIGHT-TO-LEFT ISOLATE (0x2067)"_ns
;
179 return "IDEOGRAPHIC SPACE (0x3000)"_ns
;
181 return "ZERO WIDTH NO-BREAK SPACE (0xFEFF)"_ns
;
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(),
199 static const nsCString
GetCharacterCodeNames(const char16_t
* aChars
,
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("\"");
214 static const nsCString
GetCharacterCodeNames(const nsAString
& aString
) {
215 return GetCharacterCodeNames(aString
.BeginReading(), aString
.Length());
218 /* static */ const char* KeymapWrapper::GetModifierName(Modifier aModifier
) {
243 return "NotModifier";
245 return "InvalidValue";
249 /* static */ KeymapWrapper::Modifier
KeymapWrapper::GetModifierForGDKKeyval(
251 switch (aGdkKeyval
) {
256 case GDK_Scroll_Lock
:
277 case GDK_ISO_Level3_Shift
:
278 case GDK_Mode_switch
:
280 case GDK_ISO_Level5_Shift
:
287 guint
KeymapWrapper::GetModifierMask(Modifier aModifier
) const {
290 return GDK_LOCK_MASK
;
292 return mModifierMasks
[INDEX_NUM_LOCK
];
294 return mModifierMasks
[INDEX_SCROLL_LOCK
];
296 return GDK_SHIFT_MASK
;
298 return GDK_CONTROL_MASK
;
300 return mModifierMasks
[INDEX_ALT
];
302 return mModifierMasks
[INDEX_SUPER
];
304 return mModifierMasks
[INDEX_HYPER
];
306 return mModifierMasks
[INDEX_META
];
308 return mModifierMasks
[INDEX_LEVEL3
];
310 return mModifierMasks
[INDEX_LEVEL5
];
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
) {
328 KeymapWrapper
* KeymapWrapper::GetInstance() {
334 sInstance
= new KeymapWrapper();
339 void KeymapWrapper::EnsureInstance() { (void)GetInstance(); }
343 void KeymapWrapper::Shutdown() {
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
);
362 if (GdkIsX11Display()) {
370 void KeymapWrapper::Init() {
376 MOZ_LOG(gKeyLog
, LogLevel::Info
,
377 ("%p Init, mGdkKeymap=%p", this, mGdkKeymap
));
379 mModifierKeys
.Clear();
380 memset(mModifierMasks
, 0, sizeof(mModifierMasks
));
383 if (GdkIsX11Display()) {
384 InitBySystemSettingsX11();
388 if (GdkIsWaylandDisplay()) {
389 InitBySystemSettingsWayland();
394 gdk_window_add_filter(nullptr, FilterEvents
, this);
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
)));
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()",
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",
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",
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",
458 if (!XGetKeyboardControl(display
, &mKeyboardState
)) {
459 MOZ_LOG(gKeyLog
, LogLevel::Info
,
460 ("%p InitXKBExtension failed due to failure of "
461 "XGetKeyboardControl(), display=0x%p",
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());
486 XDisplayKeycodes(display
, &min_keycode
, &max_keycode
);
488 int keysyms_per_keycode
= 0;
490 XGetKeyboardMapping(display
, min_keycode
, max_keycode
- min_keycode
+ 1,
491 &keysyms_per_keycode
);
493 MOZ_LOG(gKeyLog
, LogLevel::Info
,
494 ("%p InitBySystemSettings, "
495 "Failed due to null xkeymap",
500 XModifierKeymap
* xmodmap
= XGetModifierMapping(display
);
502 MOZ_LOG(gKeyLog
, LogLevel::Info
,
503 ("%p InitBySystemSettings, "
504 "Failed due to null xmodmap",
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
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.
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",
540 if (!keycode
|| keycode
< min_keycode
|| keycode
> max_keycode
) {
544 ModifierKey
* modifierKey
= GetModifierKey(keycode
);
546 modifierKey
= mModifierKeys
.AppendElement(ModifierKey(keycode
));
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.
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
)));
571 // Don't overwrite the stored information with
577 // Ignore the modifiers defined in GDK spec. They shouldn't
578 // be mapped to Mod1-5 because they must not work on native
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
]) {
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
]);
593 foundLevel
[modIndex
] = j
;
594 mod
[modIndex
] = modifier
;
600 for (uint32_t i
= 0; i
< COUNT_OF_MODIFIER_INDEX
; i
++) {
606 case INDEX_SCROLL_LOCK
:
607 modifier
= SCROLL_LOCK
;
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
);
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
) {
698 char* mapString
= (char*)mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0);
699 if (mapString
== MAP_FAILED
) {
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
);
722 NS_WARNING("keyboard_handle_keymap(): Failed to compile keymap!\n");
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
,
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
,
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
);
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
,
781 if (strcmp(interface
, "wl_seat") == 0) {
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
,
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);
803 KeymapWrapper::~KeymapWrapper() {
805 gdk_window_remove_filter(nullptr, FilterEvents
, this);
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));
819 GdkFilterReturn
KeymapWrapper::FilterEvents(GdkXEvent
* aXEvent
,
822 XEvent
* xEvent
= static_cast<XEvent
*>(aXEvent
);
823 switch (xEvent
->type
) {
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
)) {
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
==
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
));
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
));
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
;
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
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
));
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
;
910 KeymapWrapper
* self
= static_cast<KeymapWrapper
*>(aData
);
911 if (xEvent
->type
!= self
->mXKBBaseEventCode
) {
914 XkbEvent
* xkbEvent
= (XkbEvent
*)xEvent
;
915 if (xkbEvent
->any
.xkb_type
!= XkbControlsNotify
||
916 !(xkbEvent
->ctrls
.changed_ctrls
& XkbPerKeyRepeatMask
)) {
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
));
929 return GDK_FILTER_CONTINUE
;
933 static void ResetBidiKeyboard() {
934 // Reset the bidi keyboard settings for the new GdkKeymap
935 nsCOMPtr
<nsIBidiKeyboard
> bidiKeyboard
= nsContentUtils::GetBidiKeyboard();
937 bidiKeyboard
->Reset();
939 WidgetUtils::SendBidiKeyboardInfoToContent();
943 void KeymapWrapper::ResetKeyboard() {
944 sInstance
->mInitialized
= false;
949 void KeymapWrapper::OnKeysChanged(GdkKeymap
* aGdkKeymap
,
950 KeymapWrapper
* aKeymapWrapper
) {
951 MOZ_LOG(gKeyLog
, LogLevel::Info
,
952 ("OnKeysChanged, aGdkKeymap=%p, aKeymapWrapper=%p", aGdkKeymap
,
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.
964 void KeymapWrapper::OnDirectionChanged(GdkKeymap
* aGdkKeymap
,
965 KeymapWrapper
* aKeymapWrapper
) {
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
,
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,
989 return static_cast<guint
>(modifiers
);
993 bool KeymapWrapper::AreModifiersCurrentlyActive(Modifiers aModifiers
) {
994 guint modifierState
= GetCurrentModifierState();
995 return AreModifiersActive(aModifiers
, modifierState
);
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
)) {
1009 if (!(aModifierState
& keymapWrapper
->GetModifierMask(modifier
))) {
1012 aModifiers
&= ~modifier
;
1018 uint32_t KeymapWrapper::ComputeCurrentKeyModifiers() {
1019 return ComputeKeyModifiers(GetCurrentModifierState());
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
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
;
1062 guint
KeymapWrapper::ConvertWidgetModifierToGdkState(
1063 nsIWidget::Modifiers aNativeModifiers
) {
1064 if (!aNativeModifiers
) {
1067 struct ModifierMapEntry
{
1068 nsIWidget::Modifiers mWidgetModifier
;
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
}};
1087 KeymapWrapper
* instance
= GetInstance();
1088 for (const ModifierMapEntry
& entry
: sModifierMap
) {
1089 if (aNativeModifiers
& entry
.mWidgetModifier
) {
1090 state
|= instance
->GetModifierMask(entry
.mModifier
);
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
;
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
:
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
;
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
)));
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
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
1187 uint32_t DOMKeyCode
= GetDOMKeyCodeFromKeyPairs(keyval
);
1188 NS_ASSERTION(DOMKeyCode
, "All modifier keys must have a DOM keycode");
1192 // If the key isn't printable, let's look at the key pairs.
1193 uint32_t charCode
= GetCharCodeFor(aGdkKeyEvent
);
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
);
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.
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.
1224 case GDK_KP_Multiply
:
1225 return NS_VK_MULTIPLY
;
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
;
1235 return NS_VK_DIVIDE
;
1237 return NS_VK_NUMPAD0
;
1239 return NS_VK_NUMPAD1
;
1241 return NS_VK_NUMPAD2
;
1243 return NS_VK_NUMPAD3
;
1245 return NS_VK_NUMPAD4
;
1247 return NS_VK_NUMPAD5
;
1249 return NS_VK_NUMPAD6
;
1251 return NS_VK_NUMPAD7
;
1253 return NS_VK_NUMPAD8
;
1255 return NS_VK_NUMPAD9
;
1258 KeymapWrapper
* keymapWrapper
= GetInstance();
1260 // Ignore all modifier state except NumLock.
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
)) {
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
1288 return WidgetUtils::ComputeKeyCodeFromChar(shiftedChar
);
1291 // If the shifted unmodified character isn't an ASCII character, we should
1293 if (!IsPrintableASCIICharacter(shiftedChar
)) {
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) {
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
)) {
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
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
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) \
1373 return aKeyNameIndex;
1375 #include "NativeKeyToDOMKeyName.h"
1377 #undef NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX
1383 return KEY_NAME_INDEX_Unidentified
;
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) \
1392 return aCodeNameIndex;
1394 #include "NativeKeyToDOMCodeName.h"
1396 #undef NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX
1402 return CODE_NAME_INDEX_UNKNOWN
;
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"));
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
);
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
)));
1447 nsEventStatus status
= nsEventStatus_eIgnore
;
1448 bool dispatched
= dispatcher
->DispatchKeyboardEvent(
1449 aKeyboardEvent
.mMessage
, aKeyboardEvent
, status
, nullptr);
1450 *aIsCancelled
= (status
== nsEventStatus_eConsumeNoDefault
);
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
) {
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()) {
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()) {
1497 contextMenuEvent
.mModifiers
&= ~MODIFIER_SHIFT
;
1500 aWindow
->DispatchInputEvent(&contextMenuEvent
);
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 })",
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
1523 bool IMEWasEnabled
= false;
1524 KeyHandlingState handlingState
= KeyHandlingState::eNotHandled
;
1525 RefPtr
<IMContextWrapper
> imContext
= aWindow
->GetIMContext();
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"));
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"));
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"));
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 "
1587 // Look for specialized app-command keys
1588 switch (aGdkKeyEvent
->keyval
) {
1590 aWindow
->DispatchCommandEvent(nsGkAtoms::Back
);
1591 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1592 (" HandleKeyPressEvent(), dispatched \"Back\" command event"));
1595 aWindow
->DispatchCommandEvent(nsGkAtoms::Forward
);
1596 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1597 (" HandleKeyPressEvent(), dispatched \"Forward\" command "
1602 aWindow
->DispatchCommandEvent(nsGkAtoms::Reload
);
1605 aWindow
->DispatchCommandEvent(nsGkAtoms::Stop
);
1606 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1607 (" HandleKeyPressEvent(), dispatched \"Stop\" command event"));
1610 aWindow
->DispatchCommandEvent(nsGkAtoms::Search
);
1611 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1612 (" HandleKeyPressEvent(), dispatched \"Search\" command event"));
1615 aWindow
->DispatchCommandEvent(nsGkAtoms::Bookmarks
);
1616 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1617 (" HandleKeyPressEvent(), dispatched \"Bookmarks\" command "
1621 aWindow
->DispatchCommandEvent(nsGkAtoms::Home
);
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 "
1632 aWindow
->DispatchContentCommandEvent(eContentCommandCut
);
1633 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1634 (" HandleKeyPressEvent(), dispatched \"Cut\" content command "
1639 aWindow
->DispatchContentCommandEvent(eContentCommandPaste
);
1640 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1641 (" HandleKeyPressEvent(), dispatched \"Paste\" content command "
1645 aWindow
->DispatchContentCommandEvent(eContentCommandRedo
);
1649 aWindow
->DispatchContentCommandEvent(eContentCommandUndo
);
1650 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1651 (" HandleKeyPressEvent(), dispatched \"Undo\" content command "
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"));
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"));
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
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
,
1689 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1690 (" HandleKeyPressEvent(), dispatched eKeyPress event "
1692 GetStatusName(status
)));
1694 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1695 (" HandleKeyPressEvent(), didn't dispatch eKeyPress event "
1697 GetStatusName(status
)));
1700 WidgetEventTime eventTime
= aWindow
->GetWidgetEventTime(aGdkKeyEvent
->time
);
1701 textEventDispatcher
->CommitComposition(status
, &keypressEvent
.mKeyValue
,
1703 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1704 (" HandleKeyPressEvent(), dispatched a set of composition "
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 })",
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();
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"));
1735 bool isCancelled
= false;
1736 if (NS_WARN_IF(!DispatchKeyDownOrKeyUpEvent(aWindow
, aGdkKeyEvent
, false,
1738 MOZ_LOG(gKeyLog
, LogLevel::Error
,
1739 (" HandleKeyReleaseEvent(), didn't dispatch eKeyUp event"));
1743 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1744 (" HandleKeyReleaseEvent(), dispatched eKeyUp event "
1746 GetBoolName(isCancelled
)));
1751 void KeymapWrapper::InitKeyEvent(WidgetKeyboardEvent
& aKeyEvent
,
1752 GdkEventKey
* aGdkKeyEvent
,
1753 bool aIsProcessedByIME
) {
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
);
1768 charCode
= keymapWrapper
->GetUnmodifiedCharCodeFor(aGdkKeyEvent
);
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
);
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();
1797 if (aGdkKeyEvent
->is_modifier
&& GdkIsX11Display(gdkDisplay
)) {
1798 Display
* display
= gdk_x11_display_get_xdisplay(gdkDisplay
);
1799 if (XEventsQueued(display
, QueuedAfterReading
)) {
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
;
1814 InitInputEvent(aKeyEvent
, modifierState
);
1816 switch (aGdkKeyEvent
->keyval
) {
1823 aKeyEvent
.mLocation
= eKeyLocationLeft
;
1832 aKeyEvent
.mLocation
= eKeyLocationRight
;
1857 case GDK_KP_Prior
: // same as GDK_KP_Page_Up
1858 case GDK_KP_Next
: // same as GDK_KP_Page_Down
1864 case GDK_KP_Multiply
:
1866 case GDK_KP_Separator
:
1867 case GDK_KP_Subtract
:
1868 case GDK_KP_Decimal
:
1870 aKeyEvent
.mLocation
= eKeyLocationNumpad
;
1874 aKeyEvent
.mLocation
= eKeyLocationStandard
;
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
;
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()),
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
)));
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
) {
1921 case GDK_KP_Multiply
:
1925 case GDK_KP_Separator
:
1927 case GDK_KP_Subtract
:
1929 case GDK_KP_Decimal
:
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
)) {
1966 // I guess we couldn't convert
1970 uint32_t KeymapWrapper::GetCharCodeFor(const GdkEventKey
* aGdkKeyEvent
,
1971 guint aModifierState
, gint aGroup
) {
1973 if (!gdk_keymap_translate_keyboard_state(
1974 mGdkKeymap
, aGdkKeyEvent
->hardware_keycode
,
1975 GdkModifierType(aModifierState
), aGroup
, &keyval
, nullptr, nullptr,
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
));
1993 GetCharCodeFor(aGdkKeyEvent
, GdkModifierType(state
), aGdkKeyEvent
->group
);
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
) {
2005 return GetCharCodeFor(aGdkKeyEvent
, GdkModifierType(stateWithoutAltGraph
),
2006 aGdkKeyEvent
->group
);
2009 gint
KeymapWrapper::GetKeyLevel(GdkEventKey
* aGdkKeyEvent
) {
2011 if (!gdk_keymap_translate_keyboard_state(
2012 mGdkKeymap
, aGdkKeyEvent
->hardware_keycode
,
2013 GdkModifierType(aGdkKeyEvent
->state
), aGdkKeyEvent
->group
, nullptr,
2014 nullptr, &level
, nullptr)) {
2020 gint
KeymapWrapper::GetFirstLatinGroup() {
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) {
2030 if (minGroup
>= 0 && keys
[i
].group
> minGroup
) {
2033 minGroup
= keys
[i
].group
;
2040 bool KeymapWrapper::IsLatinGroup(guint8 aGroup
) {
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) {
2049 if (keys
[i
].group
== aGroup
) {
2059 bool KeymapWrapper::IsAutoRepeatableKey(guint aHardwareKeyCode
) {
2061 uint8_t indexOfArray
= aHardwareKeyCode
/ 8;
2062 MOZ_ASSERT(indexOfArray
< ArrayLength(mKeyboardState
.auto_repeats
),
2064 char bitMask
= 1 << (aHardwareKeyCode
% 8);
2065 return (mKeyboardState
.auto_repeats
[indexOfArray
] & bitMask
) != 0;
2072 bool KeymapWrapper::IsBasicLatinLetterOrNumeral(uint32_t aCharCode
) {
2073 return (aCharCode
>= 'a' && aCharCode
<= 'z') ||
2074 (aCharCode
>= 'A' && aCharCode
<= 'Z') ||
2075 (aCharCode
>= '0' && aCharCode
<= '9');
2079 guint
KeymapWrapper::GetGDKKeyvalWithoutModifier(
2080 const GdkEventKey
* aGdkKeyEvent
) {
2081 KeymapWrapper
* keymapWrapper
= GetInstance();
2083 (aGdkKeyEvent
->state
& keymapWrapper
->GetModifierMask(NUM_LOCK
));
2085 if (!gdk_keymap_translate_keyboard_state(
2086 keymapWrapper
->mGdkKeymap
, aGdkKeyEvent
->hardware_keycode
,
2087 GdkModifierType(state
), aGdkKeyEvent
->group
, &keyval
, nullptr,
2088 nullptr, nullptr)) {
2095 uint32_t KeymapWrapper::GetDOMKeyCodeFromKeyPairs(guint aGdkKeyval
) {
2096 switch (aGdkKeyval
) {
2098 return NS_VK_CANCEL
;
2102 case GDK_ISO_Left_Tab
:
2107 return NS_VK_RETURN
;
2110 case GDK_Shift_Lock
:
2114 return NS_VK_CONTROL
;
2122 // Assume that Super or Hyper is always mapped to physical Win key.
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
:
2147 return NS_VK_CAPS_LOCK
;
2149 case GDK_Kana_Shift
:
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
:
2160 return NS_VK_ESCAPE
;
2162 return NS_VK_CONVERT
;
2164 return NS_VK_NONCONVERT
;
2165 // case GDK_XXX: return NS_VK_ACCEPT;
2166 // case GDK_XXX: return NS_VK_MODECHANGE;
2168 return NS_VK_PAGE_UP
;
2170 return NS_VK_PAGE_DOWN
;
2184 return NS_VK_SELECT
;
2188 return NS_VK_EXECUTE
;
2190 return NS_VK_INSERT
;
2192 return NS_VK_DELETE
;
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_;
2211 return NS_VK_CLEAR
; // Num-unlocked 5
2212 case GDK_KP_Page_Down
:
2213 return NS_VK_PAGE_DOWN
;
2219 return NS_VK_INSERT
;
2221 return NS_VK_DELETE
;
2223 return NS_VK_RETURN
;
2226 return NS_VK_NUM_LOCK
;
2227 case GDK_Scroll_Lock
:
2228 return NS_VK_SCROLL_LOCK
;
2280 // context menu key, keysym 0xff67, typically keycode 117 on 105-key
2281 // (Microsoft) x86 keyboards, located between right 'Windows' key and
2284 return NS_VK_CONTEXT_MENU
;
2290 case GDK_3270_CursorSelect
:
2292 case GDK_3270_ExSelect
:
2294 case GDK_3270_EraseEOF
:
2298 // case GDK_XXX: return NS_VK_ZOOM;
2302 // map Sun Keyboard special keysyms on to NS_VK keys
2304 // Sun F11 key generates SunF36(0x1005ff10) keysym
2307 // Sun F12 key generates SunF37(0x1005ff11) keysym
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.
2330 uint32_t charCode
= GetCharCodeFor(aGdkKeyEvent
);
2332 MOZ_LOG(gKeyLog
, LogLevel::Info
,
2333 ("%p WillDispatchKeyboardEventInternal, "
2334 "mKeyCode=0x%02X, charCode=0x%08X",
2335 this, aKeyEvent
.mKeyCode
, aKeyEvent
.mCharCode
));
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
));
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
) {
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
));
2394 // Next, find Latin inputtable keyboard layout.
2395 gint minGroup
= GetFirstLatinGroup();
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
));
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
),
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
));
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,
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
;
2488 } // namespace widget
2489 } // namespace mozilla