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/Maybe.h"
36 #include "mozilla/MouseEvents.h"
37 #include "mozilla/StaticPrefs_dom.h"
38 #include "mozilla/TextEventDispatcher.h"
39 #include "mozilla/TextEvents.h"
42 # include <sys/mman.h>
43 # include "nsWaylandDisplay.h"
46 // For collecting other people's log, tell them `MOZ_LOG=KeyboardHandler:4,sync`
47 // rather than `MOZ_LOG=KeyboardHandler:5,sync` since using `5` may create too
49 // Therefore you shouldn't use `LogLevel::Verbose` for logging usual behavior.
50 mozilla::LazyLogModule
gKeyLog("KeyboardHandler");
55 #define IS_ASCII_ALPHABETICAL(key) \
56 ((('a' <= key) && (key <= 'z')) || (('A' <= key) && (key <= 'Z')))
58 #define MOZ_MODIFIER_KEYS "MozKeymapWrapper"
60 KeymapWrapper
* KeymapWrapper::sInstance
= nullptr;
61 guint
KeymapWrapper::sLastRepeatableHardwareKeyCode
= 0;
63 Time
KeymapWrapper::sLastRepeatableKeyTime
= 0;
65 KeymapWrapper::RepeatState
KeymapWrapper::sRepeatState
=
66 KeymapWrapper::NOT_PRESSED
;
68 static const char* GetBoolName(bool aBool
) { return aBool
? "TRUE" : "FALSE"; }
70 static const char* GetStatusName(nsEventStatus aStatus
) {
72 case nsEventStatus_eConsumeDoDefault
:
73 return "nsEventStatus_eConsumeDoDefault";
74 case nsEventStatus_eConsumeNoDefault
:
75 return "nsEventStatus_eConsumeNoDefault";
76 case nsEventStatus_eIgnore
:
77 return "nsEventStatus_eIgnore";
78 case nsEventStatus_eSentinel
:
79 return "nsEventStatus_eSentinel";
81 return "Illegal value";
85 static const nsCString
GetKeyLocationName(uint32_t aLocation
) {
87 case eKeyLocationLeft
:
88 return "KEY_LOCATION_LEFT"_ns
;
89 case eKeyLocationRight
:
90 return "KEY_LOCATION_RIGHT"_ns
;
91 case eKeyLocationStandard
:
92 return "KEY_LOCATION_STANDARD"_ns
;
93 case eKeyLocationNumpad
:
94 return "KEY_LOCATION_NUMPAD"_ns
;
96 return nsPrintfCString("Unknown (0x%04X)", aLocation
);
100 static const nsCString
GetCharacterCodeName(char16_t aChar
) {
103 return "NULL (0x0000)"_ns
;
105 return "BACKSPACE (0x0008)"_ns
;
107 return "CHARACTER TABULATION (0x0009)"_ns
;
109 return "LINE FEED (0x000A)"_ns
;
111 return "LINE TABULATION (0x000B)"_ns
;
113 return "FORM FEED (0x000C)"_ns
;
115 return "CARRIAGE RETURN (0x000D)"_ns
;
117 return "CANCEL (0x0018)"_ns
;
119 return "ESCAPE (0x001B)"_ns
;
121 return "SPACE (0x0020)"_ns
;
123 return "DELETE (0x007F)"_ns
;
125 return "NO-BREAK SPACE (0x00A0)"_ns
;
127 return "SOFT HYPHEN (0x00AD)"_ns
;
129 return "EN QUAD (0x2000)"_ns
;
131 return "EM QUAD (0x2001)"_ns
;
133 return "EN SPACE (0x2002)"_ns
;
135 return "EM SPACE (0x2003)"_ns
;
137 return "THREE-PER-EM SPACE (0x2004)"_ns
;
139 return "FOUR-PER-EM SPACE (0x2005)"_ns
;
141 return "SIX-PER-EM SPACE (0x2006)"_ns
;
143 return "FIGURE SPACE (0x2007)"_ns
;
145 return "PUNCTUATION SPACE (0x2008)"_ns
;
147 return "THIN SPACE (0x2009)"_ns
;
149 return "HAIR SPACE (0x200A)"_ns
;
151 return "ZERO WIDTH SPACE (0x200B)"_ns
;
153 return "ZERO WIDTH NON-JOINER (0x200C)"_ns
;
155 return "ZERO WIDTH JOINER (0x200D)"_ns
;
157 return "LEFT-TO-RIGHT MARK (0x200E)"_ns
;
159 return "RIGHT-TO-LEFT MARK (0x200F)"_ns
;
161 return "PARAGRAPH SEPARATOR (0x2029)"_ns
;
163 return "LEFT-TO-RIGHT EMBEDDING (0x202A)"_ns
;
165 return "RIGHT-TO-LEFT EMBEDDING (0x202B)"_ns
;
167 return "LEFT-TO-RIGHT OVERRIDE (0x202D)"_ns
;
169 return "RIGHT-TO-LEFT OVERRIDE (0x202E)"_ns
;
171 return "NARROW NO-BREAK SPACE (0x202F)"_ns
;
173 return "MEDIUM MATHEMATICAL SPACE (0x205F)"_ns
;
175 return "WORD JOINER (0x2060)"_ns
;
177 return "LEFT-TO-RIGHT ISOLATE (0x2066)"_ns
;
179 return "RIGHT-TO-LEFT ISOLATE (0x2067)"_ns
;
181 return "IDEOGRAPHIC SPACE (0x3000)"_ns
;
183 return "ZERO WIDTH NO-BREAK SPACE (0xFEFF)"_ns
;
185 if (aChar
< ' ' || (aChar
>= 0x80 && aChar
< 0xA0)) {
186 return nsPrintfCString("control (0x%04X)", aChar
);
188 if (NS_IS_HIGH_SURROGATE(aChar
)) {
189 return nsPrintfCString("high surrogate (0x%04X)", aChar
);
191 if (NS_IS_LOW_SURROGATE(aChar
)) {
192 return nsPrintfCString("low surrogate (0x%04X)", aChar
);
194 return nsPrintfCString("'%s' (0x%04X)",
195 NS_ConvertUTF16toUTF8(nsAutoString(aChar
)).get(),
201 static const nsCString
GetCharacterCodeNames(const char16_t
* aChars
,
207 result
.AssignLiteral("\"");
208 StringJoinAppend(result
, ", "_ns
, Span
{aChars
, aLength
},
209 [](nsACString
& dest
, const char16_t charValue
) {
210 dest
.Append(GetCharacterCodeName(charValue
));
212 result
.AppendLiteral("\"");
216 static const nsCString
GetCharacterCodeNames(const nsAString
& aString
) {
217 return GetCharacterCodeNames(aString
.BeginReading(), aString
.Length());
221 const char* KeymapWrapper::GetModifierName(MappedModifier aModifier
) {
246 return "NotModifier";
248 return "InvalidValue";
253 KeymapWrapper::MappedModifier
KeymapWrapper::GetModifierForGDKKeyval(
255 switch (aGdkKeyval
) {
260 case GDK_Scroll_Lock
:
281 case GDK_ISO_Level3_Shift
:
282 case GDK_Mode_switch
:
284 case GDK_ISO_Level5_Shift
:
291 guint
KeymapWrapper::GetGdkModifierMask(MappedModifier aModifier
) const {
294 return GDK_LOCK_MASK
;
296 return mModifierMasks
[INDEX_NUM_LOCK
];
298 return mModifierMasks
[INDEX_SCROLL_LOCK
];
300 return GDK_SHIFT_MASK
;
302 return GDK_CONTROL_MASK
;
304 return mModifierMasks
[INDEX_ALT
];
306 return GDK_SUPER_MASK
;
308 return mModifierMasks
[INDEX_HYPER
];
310 return mModifierMasks
[INDEX_META
];
312 return mModifierMasks
[INDEX_LEVEL3
];
314 return mModifierMasks
[INDEX_LEVEL5
];
320 KeymapWrapper::ModifierKey
* KeymapWrapper::GetModifierKey(
321 guint aHardwareKeycode
) {
322 for (uint32_t i
= 0; i
< mModifierKeys
.Length(); i
++) {
323 ModifierKey
& key
= mModifierKeys
[i
];
324 if (key
.mHardwareKeycode
== aHardwareKeycode
) {
332 KeymapWrapper
* KeymapWrapper::GetInstance() {
334 sInstance
= new KeymapWrapper();
341 void KeymapWrapper::EnsureInstance() { (void)GetInstance(); }
343 void KeymapWrapper::InitBySystemSettingsWayland() {
344 MOZ_UNUSED(WaylandDisplayGet());
349 void KeymapWrapper::Shutdown() {
356 KeymapWrapper::KeymapWrapper()
357 : mInitialized(false),
358 mGdkKeymap(gdk_keymap_get_default()),
359 mXKBBaseEventCode(0),
360 mOnKeysChangedSignalHandle(0),
361 mOnDirectionChangedSignalHandle(0) {
362 MOZ_LOG(gKeyLog
, LogLevel::Info
,
363 ("%p Constructor, mGdkKeymap=%p", this, mGdkKeymap
));
365 g_object_ref(mGdkKeymap
);
368 if (GdkIsX11Display()) {
374 void KeymapWrapper::Init() {
380 MOZ_LOG(gKeyLog
, LogLevel::Info
,
381 ("%p Init, mGdkKeymap=%p", this, mGdkKeymap
));
383 mModifierKeys
.Clear();
384 memset(mModifierMasks
, 0, sizeof(mModifierMasks
));
387 if (GdkIsX11Display()) {
388 InitBySystemSettingsX11();
392 if (GdkIsWaylandDisplay()) {
393 InitBySystemSettingsWayland();
398 gdk_window_add_filter(nullptr, FilterEvents
, this);
401 MOZ_LOG(gKeyLog
, LogLevel::Info
,
402 ("%p Init, CapsLock=0x%X, NumLock=0x%X, "
403 "ScrollLock=0x%X, Level3=0x%X, Level5=0x%X, "
404 "Shift=0x%X, Ctrl=0x%X, Alt=0x%X, Meta=0x%X, Super=0x%X, Hyper=0x%X",
405 this, GetGdkModifierMask(CAPS_LOCK
), GetGdkModifierMask(NUM_LOCK
),
406 GetGdkModifierMask(SCROLL_LOCK
), GetGdkModifierMask(LEVEL3
),
407 GetGdkModifierMask(LEVEL5
), GetGdkModifierMask(SHIFT
),
408 GetGdkModifierMask(CTRL
), GetGdkModifierMask(ALT
),
409 GetGdkModifierMask(META
), GetGdkModifierMask(SUPER
),
410 GetGdkModifierMask(HYPER
)));
414 void KeymapWrapper::InitXKBExtension() {
415 PodZero(&mKeyboardState
);
417 int xkbMajorVer
= XkbMajorVersion
;
418 int xkbMinorVer
= XkbMinorVersion
;
419 if (!XkbLibraryVersion(&xkbMajorVer
, &xkbMinorVer
)) {
420 MOZ_LOG(gKeyLog
, LogLevel::Info
,
421 ("%p InitXKBExtension failed due to failure of "
422 "XkbLibraryVersion()",
427 Display
* display
= gdk_x11_display_get_xdisplay(gdk_display_get_default());
429 // XkbLibraryVersion() set xkbMajorVer and xkbMinorVer to that of the
430 // library, which may be newer than what is required of the server in
431 // XkbQueryExtension(), so these variables should be reset to
432 // XkbMajorVersion and XkbMinorVersion before the XkbQueryExtension call.
433 xkbMajorVer
= XkbMajorVersion
;
434 xkbMinorVer
= XkbMinorVersion
;
435 int opcode
, baseErrorCode
;
436 if (!XkbQueryExtension(display
, &opcode
, &mXKBBaseEventCode
, &baseErrorCode
,
437 &xkbMajorVer
, &xkbMinorVer
)) {
438 MOZ_LOG(gKeyLog
, LogLevel::Info
,
439 ("%p InitXKBExtension failed due to failure of "
440 "XkbQueryExtension(), display=0x%p",
445 if (!XkbSelectEventDetails(display
, XkbUseCoreKbd
, XkbStateNotify
,
446 XkbModifierStateMask
, XkbModifierStateMask
)) {
447 MOZ_LOG(gKeyLog
, LogLevel::Info
,
448 ("%p InitXKBExtension failed due to failure of "
449 "XkbSelectEventDetails() for XModifierStateMask, display=0x%p",
454 if (!XkbSelectEventDetails(display
, XkbUseCoreKbd
, XkbControlsNotify
,
455 XkbPerKeyRepeatMask
, XkbPerKeyRepeatMask
)) {
456 MOZ_LOG(gKeyLog
, LogLevel::Info
,
457 ("%p InitXKBExtension failed due to failure of "
458 "XkbSelectEventDetails() for XkbControlsNotify, display=0x%p",
463 if (!XGetKeyboardControl(display
, &mKeyboardState
)) {
464 MOZ_LOG(gKeyLog
, LogLevel::Info
,
465 ("%p InitXKBExtension failed due to failure of "
466 "XGetKeyboardControl(), display=0x%p",
471 MOZ_LOG(gKeyLog
, LogLevel::Info
, ("%p InitXKBExtension, Succeeded", this));
474 void KeymapWrapper::InitBySystemSettingsX11() {
475 MOZ_LOG(gKeyLog
, LogLevel::Info
,
476 ("%p InitBySystemSettingsX11, mGdkKeymap=%p", this, mGdkKeymap
));
478 if (!mOnKeysChangedSignalHandle
) {
479 mOnKeysChangedSignalHandle
= g_signal_connect(
480 mGdkKeymap
, "keys-changed", (GCallback
)OnKeysChanged
, this);
482 if (!mOnDirectionChangedSignalHandle
) {
483 mOnDirectionChangedSignalHandle
= g_signal_connect(
484 mGdkKeymap
, "direction-changed", (GCallback
)OnDirectionChanged
, this);
487 Display
* display
= gdk_x11_display_get_xdisplay(gdk_display_get_default());
491 XDisplayKeycodes(display
, &min_keycode
, &max_keycode
);
493 int keysyms_per_keycode
= 0;
495 XGetKeyboardMapping(display
, min_keycode
, max_keycode
- min_keycode
+ 1,
496 &keysyms_per_keycode
);
498 MOZ_LOG(gKeyLog
, LogLevel::Info
,
499 ("%p InitBySystemSettings, "
500 "Failed due to null xkeymap",
505 XModifierKeymap
* xmodmap
= XGetModifierMapping(display
);
507 MOZ_LOG(gKeyLog
, LogLevel::Info
,
508 ("%p InitBySystemSettings, "
509 "Failed due to null xmodmap",
514 MOZ_LOG(gKeyLog
, LogLevel::Info
,
515 ("%p InitBySystemSettings, min_keycode=%d, "
516 "max_keycode=%d, keysyms_per_keycode=%d, max_keypermod=%d",
517 this, min_keycode
, max_keycode
, keysyms_per_keycode
,
518 xmodmap
->max_keypermod
));
520 // The modifiermap member of the XModifierKeymap structure contains 8 sets
521 // of max_keypermod KeyCodes, one for each modifier in the order Shift,
522 // Lock, Control, Mod1, Mod2, Mod3, Mod4, and Mod5.
523 // Only nonzero KeyCodes have meaning in each set, and zero KeyCodes are
526 // Note that two or more modifiers may use one modifier flag. E.g.,
527 // on Ubuntu 10.10, Alt and Meta share the Mod1 in default settings.
528 // And also Super and Hyper share the Mod4. In such cases, we need to
529 // decide which modifier flag means one of DOM modifiers.
531 // mod[0] is Modifier introduced by Mod1.
532 MappedModifier mod
[5];
533 int32_t foundLevel
[5];
534 for (uint32_t i
= 0; i
< std::size(mod
); i
++) {
535 mod
[i
] = NOT_MODIFIER
;
536 foundLevel
[i
] = INT32_MAX
;
538 const uint32_t map_size
= 8 * xmodmap
->max_keypermod
;
539 for (uint32_t i
= 0; i
< map_size
; i
++) {
540 KeyCode keycode
= xmodmap
->modifiermap
[i
];
541 MOZ_LOG(gKeyLog
, LogLevel::Info
,
542 ("%p InitBySystemSettings, "
543 " i=%d, keycode=0x%08X",
545 if (!keycode
|| keycode
< min_keycode
|| keycode
> max_keycode
) {
549 ModifierKey
* modifierKey
= GetModifierKey(keycode
);
551 modifierKey
= mModifierKeys
.AppendElement(ModifierKey(keycode
));
555 xkeymap
+ (keycode
- min_keycode
) * keysyms_per_keycode
;
556 const uint32_t bit
= i
/ xmodmap
->max_keypermod
;
557 modifierKey
->mMask
|= 1 << bit
;
559 // We need to know the meaning of Mod1, Mod2, Mod3, Mod4 and Mod5.
560 // Let's skip if current map is for others.
565 const int32_t modIndex
= bit
- 3;
566 for (int32_t j
= 0; j
< keysyms_per_keycode
; j
++) {
567 MappedModifier modifier
= GetModifierForGDKKeyval(syms
[j
]);
568 MOZ_LOG(gKeyLog
, LogLevel::Info
,
569 ("%p InitBySystemSettings, "
570 " Mod%d, j=%d, syms[j]=%s(0x%lX), modifier=%s",
571 this, modIndex
+ 1, j
, gdk_keyval_name(syms
[j
]), syms
[j
],
572 GetModifierName(modifier
)));
576 // Don't overwrite the stored information with
583 // Ignore the modifiers defined in GDK spec. They shouldn't
584 // be mapped to Mod1-5 because they must not work on native
588 // If new modifier is found in higher level than stored
589 // value, we don't need to overwrite it.
590 if (j
> foundLevel
[modIndex
]) {
593 // If new modifier is more important than stored value,
594 // we should overwrite it with new modifier.
595 if (j
== foundLevel
[modIndex
]) {
596 mod
[modIndex
] = std::min(modifier
, mod
[modIndex
]);
599 foundLevel
[modIndex
] = j
;
600 mod
[modIndex
] = modifier
;
606 for (uint32_t i
= 0; i
< COUNT_OF_MODIFIER_INDEX
; i
++) {
607 MappedModifier modifier
;
612 case INDEX_SCROLL_LOCK
:
613 modifier
= SCROLL_LOCK
;
631 MOZ_CRASH("All indexes must be handled here");
633 for (uint32_t j
= 0; j
< std::size(mod
); j
++) {
634 if (modifier
== mod
[j
]) {
635 mModifierMasks
[i
] |= 1 << (j
+ 3);
640 XFreeModifiermap(xmodmap
);
646 void KeymapWrapper::SetModifierMask(xkb_keymap
* aKeymap
,
647 ModifierIndex aModifierIndex
,
648 const char* aModifierName
) {
649 xkb_mod_index_t index
= xkb_keymap_mod_get_index(aKeymap
, aModifierName
);
650 if (index
!= XKB_MOD_INVALID
) {
651 mModifierMasks
[aModifierIndex
] = (1 << index
);
655 void KeymapWrapper::SetModifierMasks(xkb_keymap
* aKeymap
) {
656 KeymapWrapper
* keymapWrapper
= GetInstance();
658 // This mapping is derived from get_xkb_modifiers() at gdkkeys-wayland.c
659 keymapWrapper
->SetModifierMask(aKeymap
, INDEX_NUM_LOCK
, XKB_MOD_NAME_NUM
);
660 keymapWrapper
->SetModifierMask(aKeymap
, INDEX_ALT
, XKB_MOD_NAME_ALT
);
661 keymapWrapper
->SetModifierMask(aKeymap
, INDEX_META
, "Meta");
662 keymapWrapper
->SetModifierMask(aKeymap
, INDEX_HYPER
, "Hyper");
664 keymapWrapper
->SetModifierMask(aKeymap
, INDEX_SCROLL_LOCK
, "ScrollLock");
665 keymapWrapper
->SetModifierMask(aKeymap
, INDEX_LEVEL3
, "Level3");
666 keymapWrapper
->SetModifierMask(aKeymap
, INDEX_LEVEL5
, "Level5");
668 MOZ_LOG(gKeyLog
, LogLevel::Info
,
669 ("%p KeymapWrapper::SetModifierMasks, CapsLock=0x%X, NumLock=0x%X, "
670 "ScrollLock=0x%X, Level3=0x%X, Level5=0x%X, "
671 "Shift=0x%X, Ctrl=0x%X, Alt=0x%X, Meta=0x%X, Super=0x%X, Hyper=0x%X",
672 keymapWrapper
, keymapWrapper
->GetGdkModifierMask(CAPS_LOCK
),
673 keymapWrapper
->GetGdkModifierMask(NUM_LOCK
),
674 keymapWrapper
->GetGdkModifierMask(SCROLL_LOCK
),
675 keymapWrapper
->GetGdkModifierMask(LEVEL3
),
676 keymapWrapper
->GetGdkModifierMask(LEVEL5
),
677 keymapWrapper
->GetGdkModifierMask(SHIFT
),
678 keymapWrapper
->GetGdkModifierMask(CTRL
),
679 keymapWrapper
->GetGdkModifierMask(ALT
),
680 keymapWrapper
->GetGdkModifierMask(META
),
681 keymapWrapper
->GetGdkModifierMask(SUPER
),
682 keymapWrapper
->GetGdkModifierMask(HYPER
)));
685 /* This keymap routine is derived from weston-2.0.0/clients/simple-im.c
687 void KeymapWrapper::HandleKeymap(uint32_t format
, int fd
, uint32_t size
) {
688 MOZ_LOG(gKeyLog
, LogLevel::Info
,
689 ("KeymapWrapper::HandleKeymap() format %d fd %d size %d", format
, fd
,
691 KeymapWrapper::ResetKeyboard();
693 if (format
!= WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1
) {
694 MOZ_LOG(gKeyLog
, LogLevel::Info
,
695 ("KeymapWrapper::HandleKeymap(): format is not "
696 "WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1!"));
701 char* mapString
= (char*)mmap(NULL
, size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
702 if (mapString
== MAP_FAILED
) {
703 MOZ_LOG(gKeyLog
, LogLevel::Info
,
704 ("KeymapWrapper::HandleKeymap(): failed to allocate shm!"));
709 struct xkb_context
* xkb_context
= xkb_context_new(XKB_CONTEXT_NO_FLAGS
);
710 struct xkb_keymap
* keymap
= xkb_keymap_new_from_string(
711 xkb_context
, mapString
, XKB_KEYMAP_FORMAT_TEXT_V1
,
712 XKB_KEYMAP_COMPILE_NO_FLAGS
);
714 munmap(mapString
, size
);
718 MOZ_LOG(gKeyLog
, LogLevel::Info
,
719 ("KeymapWrapper::HandleKeymap(): Failed to compile keymap!"));
723 KeymapWrapper::SetModifierMasks(keymap
);
725 xkb_keymap_unref(keymap
);
727 xkb_context_unref(xkb_context
);
731 KeymapWrapper::~KeymapWrapper() {
733 gdk_window_remove_filter(nullptr, FilterEvents
, this);
735 if (mOnKeysChangedSignalHandle
) {
736 g_signal_handler_disconnect(mGdkKeymap
, mOnKeysChangedSignalHandle
);
738 if (mOnDirectionChangedSignalHandle
) {
739 g_signal_handler_disconnect(mGdkKeymap
, mOnDirectionChangedSignalHandle
);
741 g_object_unref(mGdkKeymap
);
742 MOZ_LOG(gKeyLog
, LogLevel::Info
, ("%p Destructor", this));
747 GdkFilterReturn
KeymapWrapper::FilterEvents(GdkXEvent
* aXEvent
,
750 XEvent
* xEvent
= static_cast<XEvent
*>(aXEvent
);
751 switch (xEvent
->type
) {
753 // If the key doesn't support auto repeat, ignore the event because
754 // even if such key (e.g., Shift) is pressed during auto repeat of
755 // anoter key, it doesn't stop the auto repeat.
756 KeymapWrapper
* self
= static_cast<KeymapWrapper
*>(aData
);
757 if (!self
->IsAutoRepeatableKey(xEvent
->xkey
.keycode
)) {
760 if (sRepeatState
== NOT_PRESSED
) {
761 sRepeatState
= FIRST_PRESS
;
762 MOZ_LOG(gKeyLog
, LogLevel::Info
,
763 ("FilterEvents(aXEvent={ type=KeyPress, "
764 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
765 "aGdkEvent={ state=0x%08X }), "
766 "detected first keypress",
767 xEvent
->xkey
.keycode
, xEvent
->xkey
.state
, xEvent
->xkey
.time
,
768 reinterpret_cast<GdkEventKey
*>(aGdkEvent
)->state
));
769 } else if (sLastRepeatableHardwareKeyCode
== xEvent
->xkey
.keycode
) {
770 if (sLastRepeatableKeyTime
== xEvent
->xkey
.time
&&
771 sLastRepeatableHardwareKeyCode
==
773 GetWaitingSynthesizedKeyPressHardwareKeyCode()) {
774 // On some environment, IM may generate duplicated KeyPress event
775 // without any special state flags. In such case, we shouldn't
776 // treat the event as "repeated".
777 MOZ_LOG(gKeyLog
, LogLevel::Info
,
778 ("FilterEvents(aXEvent={ type=KeyPress, "
779 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
780 "aGdkEvent={ state=0x%08X }), "
781 "igored keypress since it must be synthesized by IME",
782 xEvent
->xkey
.keycode
, xEvent
->xkey
.state
, xEvent
->xkey
.time
,
783 reinterpret_cast<GdkEventKey
*>(aGdkEvent
)->state
));
786 sRepeatState
= REPEATING
;
787 MOZ_LOG(gKeyLog
, LogLevel::Info
,
788 ("FilterEvents(aXEvent={ type=KeyPress, "
789 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
790 "aGdkEvent={ state=0x%08X }), "
791 "detected repeating keypress",
792 xEvent
->xkey
.keycode
, xEvent
->xkey
.state
, xEvent
->xkey
.time
,
793 reinterpret_cast<GdkEventKey
*>(aGdkEvent
)->state
));
795 // If a different key is pressed while another key is pressed,
796 // auto repeat system repeats only the last pressed key.
797 // So, setting new keycode and setting repeat state as first key
798 // press should work fine.
799 sRepeatState
= FIRST_PRESS
;
800 MOZ_LOG(gKeyLog
, LogLevel::Info
,
801 ("FilterEvents(aXEvent={ type=KeyPress, "
802 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
803 "aGdkEvent={ state=0x%08X }), "
804 "detected different keypress",
805 xEvent
->xkey
.keycode
, xEvent
->xkey
.state
, xEvent
->xkey
.time
,
806 reinterpret_cast<GdkEventKey
*>(aGdkEvent
)->state
));
808 sLastRepeatableHardwareKeyCode
= xEvent
->xkey
.keycode
;
809 sLastRepeatableKeyTime
= xEvent
->xkey
.time
;
813 if (sLastRepeatableHardwareKeyCode
!= xEvent
->xkey
.keycode
) {
814 // This case means the key release event is caused by
815 // a non-repeatable key such as Shift or a repeatable key that
816 // was pressed before sLastRepeatableHardwareKeyCode was
820 sRepeatState
= NOT_PRESSED
;
821 MOZ_LOG(gKeyLog
, LogLevel::Info
,
822 ("FilterEvents(aXEvent={ type=KeyRelease, "
823 "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, "
824 "aGdkEvent={ state=0x%08X }), "
825 "detected key release",
826 xEvent
->xkey
.keycode
, xEvent
->xkey
.state
, xEvent
->xkey
.time
,
827 reinterpret_cast<GdkEventKey
*>(aGdkEvent
)->state
));
831 // At moving focus, we should reset keyboard repeat state.
832 // Strictly, this causes incorrect behavior. However, this
833 // correctness must be enough for web applications.
834 sRepeatState
= NOT_PRESSED
;
838 KeymapWrapper
* self
= static_cast<KeymapWrapper
*>(aData
);
839 if (xEvent
->type
!= self
->mXKBBaseEventCode
) {
842 XkbEvent
* xkbEvent
= (XkbEvent
*)xEvent
;
843 if (xkbEvent
->any
.xkb_type
!= XkbControlsNotify
||
844 !(xkbEvent
->ctrls
.changed_ctrls
& XkbPerKeyRepeatMask
)) {
847 if (!XGetKeyboardControl(xkbEvent
->any
.display
, &self
->mKeyboardState
)) {
848 MOZ_LOG(gKeyLog
, LogLevel::Info
,
849 ("%p FilterEvents failed due to failure "
850 "of XGetKeyboardControl(), display=0x%p",
851 self
, xkbEvent
->any
.display
));
857 return GDK_FILTER_CONTINUE
;
861 static void ResetBidiKeyboard() {
862 // Reset the bidi keyboard settings for the new GdkKeymap
863 nsCOMPtr
<nsIBidiKeyboard
> bidiKeyboard
= nsContentUtils::GetBidiKeyboard();
865 bidiKeyboard
->Reset();
867 WidgetUtils::SendBidiKeyboardInfoToContent();
871 void KeymapWrapper::ResetKeyboard() {
873 sInstance
->mInitialized
= false;
879 void KeymapWrapper::OnKeysChanged(GdkKeymap
* aGdkKeymap
,
880 KeymapWrapper
* aKeymapWrapper
) {
881 MOZ_LOG(gKeyLog
, LogLevel::Info
,
882 ("OnKeysChanged, aGdkKeymap=%p, aKeymapWrapper=%p", aGdkKeymap
,
885 MOZ_ASSERT(sInstance
== aKeymapWrapper
,
886 "This instance must be the singleton instance");
888 // We cannot reintialize here becasue we don't have GdkWindow which is using
889 // the GdkKeymap. We'll reinitialize it when next GetInstance() is called.
894 void KeymapWrapper::OnDirectionChanged(GdkKeymap
* aGdkKeymap
,
895 KeymapWrapper
* aKeymapWrapper
) {
897 // A lot of diretion-changed signal might be fired on switching bidi
898 // keyboard when using both ibus (with arabic layout) and fcitx (with IME).
899 // See https://github.com/fcitx/fcitx/issues/257
901 // Also, when using ibus, switching to IM might not cause this signal.
902 // See https://github.com/ibus/ibus/issues/1848
904 MOZ_LOG(gKeyLog
, LogLevel::Info
,
905 ("OnDirectionChanged, aGdkKeymap=%p, aKeymapWrapper=%p", aGdkKeymap
,
912 guint
KeymapWrapper::GetCurrentModifierState() {
913 GdkModifierType modifiers
;
914 GdkDisplay
* display
= gdk_display_get_default();
915 GdkScreen
* screen
= gdk_display_get_default_screen(display
);
916 GdkWindow
* window
= gdk_screen_get_root_window(screen
);
917 gdk_window_get_device_position(window
, GdkGetPointer(), nullptr, nullptr,
919 return static_cast<guint
>(modifiers
);
923 bool KeymapWrapper::AreModifiersActive(MappedModifiers aModifiers
,
924 guint aGdkModifierState
) {
925 NS_ENSURE_TRUE(aModifiers
, false);
927 KeymapWrapper
* keymapWrapper
= GetInstance();
928 for (uint32_t i
= 0; i
< sizeof(MappedModifier
) * 8 && aModifiers
; i
++) {
929 MappedModifier modifier
= static_cast<MappedModifier
>(1 << i
);
930 // Is the binary position used by modifier?
931 if (!(aModifiers
& modifier
)) {
934 // Is the modifier active?
935 if (!(aGdkModifierState
& keymapWrapper
->GetGdkModifierMask(modifier
))) {
938 aModifiers
&= ~modifier
;
944 uint32_t KeymapWrapper::ComputeCurrentKeyModifiers() {
945 return ComputeKeyModifiers(GetCurrentModifierState());
949 uint32_t KeymapWrapper::ComputeKeyModifiers(guint aGdkModifierState
) {
950 uint32_t keyModifiers
= 0;
951 if (!aGdkModifierState
) {
955 // DOM Meta key should be TRUE only on Mac. We need to discuss this
957 KeymapWrapper
* keymapWrapper
= GetInstance();
958 if (keymapWrapper
->AreModifiersActive(SHIFT
, aGdkModifierState
)) {
959 keyModifiers
|= MODIFIER_SHIFT
;
961 if (keymapWrapper
->AreModifiersActive(CTRL
, aGdkModifierState
)) {
962 keyModifiers
|= MODIFIER_CONTROL
;
964 if (keymapWrapper
->AreModifiersActive(ALT
, aGdkModifierState
)) {
965 keyModifiers
|= MODIFIER_ALT
;
967 if (keymapWrapper
->AreModifiersActive(SUPER
, aGdkModifierState
) ||
968 keymapWrapper
->AreModifiersActive(HYPER
, aGdkModifierState
) ||
969 // "Meta" state is typically mapped to `Alt` + `Shift`, but we ignore the
970 // state if `Alt` is mapped to "Alt" state. Additionally it's mapped to
971 // `Win` in Sun/Solaris keyboard layout. In this case, we want to treat
972 // them as DOM Meta modifier keys like "Super" state in the major Linux
974 keymapWrapper
->AreModifiersActive(META
, aGdkModifierState
)) {
975 keyModifiers
|= MODIFIER_META
;
977 if (keymapWrapper
->AreModifiersActive(LEVEL3
, aGdkModifierState
) ||
978 keymapWrapper
->AreModifiersActive(LEVEL5
, aGdkModifierState
)) {
979 keyModifiers
|= MODIFIER_ALTGRAPH
;
981 if (keymapWrapper
->AreModifiersActive(CAPS_LOCK
, aGdkModifierState
)) {
982 keyModifiers
|= MODIFIER_CAPSLOCK
;
984 if (keymapWrapper
->AreModifiersActive(NUM_LOCK
, aGdkModifierState
)) {
985 keyModifiers
|= MODIFIER_NUMLOCK
;
987 if (keymapWrapper
->AreModifiersActive(SCROLL_LOCK
, aGdkModifierState
)) {
988 keyModifiers
|= MODIFIER_SCROLLLOCK
;
994 guint
KeymapWrapper::ConvertWidgetModifierToGdkState(
995 nsIWidget::Modifiers aNativeModifiers
) {
996 if (!aNativeModifiers
) {
999 struct ModifierMapEntry
{
1000 nsIWidget::Modifiers mWidgetModifier
;
1001 MappedModifier mModifier
;
1003 // TODO: Currently, we don't treat L/R of each modifier on Linux.
1004 // TODO: No proper native modifier for Level5.
1005 static constexpr ModifierMapEntry sModifierMap
[] = {
1006 {nsIWidget::CAPS_LOCK
, MappedModifier::CAPS_LOCK
},
1007 {nsIWidget::NUM_LOCK
, MappedModifier::NUM_LOCK
},
1008 {nsIWidget::SHIFT_L
, MappedModifier::SHIFT
},
1009 {nsIWidget::SHIFT_R
, MappedModifier::SHIFT
},
1010 {nsIWidget::CTRL_L
, MappedModifier::CTRL
},
1011 {nsIWidget::CTRL_R
, MappedModifier::CTRL
},
1012 {nsIWidget::ALT_L
, MappedModifier::ALT
},
1013 {nsIWidget::ALT_R
, MappedModifier::ALT
},
1014 {nsIWidget::ALTGRAPH
, MappedModifier::LEVEL3
},
1015 {nsIWidget::COMMAND_L
, MappedModifier::SUPER
},
1016 {nsIWidget::COMMAND_R
, MappedModifier::SUPER
}};
1019 KeymapWrapper
* instance
= GetInstance();
1020 for (const ModifierMapEntry
& entry
: sModifierMap
) {
1021 if (aNativeModifiers
& entry
.mWidgetModifier
) {
1022 state
|= instance
->GetGdkModifierMask(entry
.mModifier
);
1029 void KeymapWrapper::InitInputEvent(WidgetInputEvent
& aInputEvent
,
1030 guint aGdkModifierState
) {
1031 KeymapWrapper
* keymapWrapper
= GetInstance();
1033 aInputEvent
.mModifiers
= ComputeKeyModifiers(aGdkModifierState
);
1035 // Don't log this method for non-important events because e.g., eMouseMove is
1036 // just noisy and there is no reason to log it.
1037 bool doLog
= aInputEvent
.mMessage
!= eMouseMove
;
1039 MOZ_LOG(gKeyLog
, LogLevel::Debug
,
1040 ("%p InitInputEvent, aGdkModifierState=0x%08X, "
1041 "aInputEvent={ mMessage=%s, mModifiers=0x%04X (Shift: %s, "
1042 "Control: %s, Alt: %s, Meta: %s, AltGr: %s, "
1043 "CapsLock: %s, NumLock: %s, ScrollLock: %s })",
1044 keymapWrapper
, aGdkModifierState
, ToChar(aInputEvent
.mMessage
),
1045 aInputEvent
.mModifiers
,
1046 GetBoolName(aInputEvent
.mModifiers
& MODIFIER_SHIFT
),
1047 GetBoolName(aInputEvent
.mModifiers
& MODIFIER_CONTROL
),
1048 GetBoolName(aInputEvent
.mModifiers
& MODIFIER_ALT
),
1049 GetBoolName(aInputEvent
.mModifiers
& MODIFIER_META
),
1050 GetBoolName(aInputEvent
.mModifiers
& MODIFIER_ALTGRAPH
),
1051 GetBoolName(aInputEvent
.mModifiers
& MODIFIER_CAPSLOCK
),
1052 GetBoolName(aInputEvent
.mModifiers
& MODIFIER_NUMLOCK
),
1053 GetBoolName(aInputEvent
.mModifiers
& MODIFIER_SCROLLLOCK
)));
1056 switch (aInputEvent
.mClass
) {
1057 case eMouseEventClass
:
1058 case ePointerEventClass
:
1059 case eMouseScrollEventClass
:
1060 case eWheelEventClass
:
1061 case eDragEventClass
:
1062 case eSimpleGestureEventClass
:
1068 WidgetMouseEventBase
& mouseEvent
= *aInputEvent
.AsMouseEventBase();
1069 mouseEvent
.mButtons
= 0;
1070 if (aGdkModifierState
& GDK_BUTTON1_MASK
) {
1071 mouseEvent
.mButtons
|= MouseButtonsFlag::ePrimaryFlag
;
1073 if (aGdkModifierState
& GDK_BUTTON3_MASK
) {
1074 mouseEvent
.mButtons
|= MouseButtonsFlag::eSecondaryFlag
;
1076 if (aGdkModifierState
& GDK_BUTTON2_MASK
) {
1077 mouseEvent
.mButtons
|= MouseButtonsFlag::eMiddleFlag
;
1082 gKeyLog
, LogLevel::Debug
,
1083 ("%p InitInputEvent, aInputEvent has mButtons, "
1084 "aInputEvent.mButtons=0x%04X (Left: %s, Right: %s, Middle: %s, "
1085 "4th (BACK): %s, 5th (FORWARD): %s)",
1086 keymapWrapper
, mouseEvent
.mButtons
,
1087 GetBoolName(mouseEvent
.mButtons
& MouseButtonsFlag::ePrimaryFlag
),
1088 GetBoolName(mouseEvent
.mButtons
& MouseButtonsFlag::eSecondaryFlag
),
1089 GetBoolName(mouseEvent
.mButtons
& MouseButtonsFlag::eMiddleFlag
),
1090 GetBoolName(mouseEvent
.mButtons
& MouseButtonsFlag::e4thFlag
),
1091 GetBoolName(mouseEvent
.mButtons
& MouseButtonsFlag::e5thFlag
)));
1096 uint32_t KeymapWrapper::ComputeDOMKeyCode(const GdkEventKey
* aGdkKeyEvent
) {
1097 // If the keyval indicates it's a modifier key, we should use unshifted
1098 // key's modifier keyval.
1099 guint keyval
= aGdkKeyEvent
->keyval
;
1100 if (GetModifierForGDKKeyval(keyval
)) {
1101 // But if the keyval without modifiers isn't a modifier key, we
1102 // shouldn't use it. E.g., Japanese keyboard layout's
1103 // Shift + Eisu-Toggle key is CapsLock. This is an actual rare case,
1104 // Windows uses different keycode for a physical key for different
1106 guint keyvalWithoutModifier
= GetGDKKeyvalWithoutModifier(aGdkKeyEvent
);
1107 if (GetModifierForGDKKeyval(keyvalWithoutModifier
)) {
1108 keyval
= keyvalWithoutModifier
;
1110 // Note that the modifier keycode and activating or deactivating
1111 // modifier flag may be mismatched, but it's okay. If a DOM key
1112 // event handler is testing a keydown event, it's more likely being
1113 // used to test which key is being pressed than to test which
1114 // modifier will become active. So, if we computed DOM keycode
1115 // from modifier flag which were changing by the physical key, then
1116 // there would be no other way for the user to generate the original
1118 uint32_t DOMKeyCode
= GetDOMKeyCodeFromKeyPairs(keyval
);
1119 NS_ASSERTION(DOMKeyCode
, "All modifier keys must have a DOM keycode");
1123 // If the key isn't printable, let's look at the key pairs.
1124 uint32_t charCode
= GetCharCodeFor(aGdkKeyEvent
);
1126 // Note that any key may be a function key because of some unusual keyboard
1127 // layouts. I.e., even if the pressed key is a printable key of en-US
1128 // keyboard layout, we should expose the function key's keyCode value to
1129 // web apps because web apps should handle the keydown/keyup events as
1130 // inputted by usual keyboard layout. For example, Hatchak keyboard
1131 // maps Tab key to "Digit3" key and Level3 Shift makes it "Backspace".
1132 // In this case, we should expose DOM_VK_BACK_SPACE (8).
1133 uint32_t DOMKeyCode
= GetDOMKeyCodeFromKeyPairs(keyval
);
1135 // XXX If DOMKeyCode is a function key's keyCode value, it might be
1136 // better to consume necessary modifiers. For example, if there is
1137 // no Control Pad section on keyboard like notebook, Delete key is
1138 // available only with Level3 Shift+"Backspace" key if using Hatchak.
1139 // If web apps accept Delete key operation only when no modifiers are
1140 // active, such users cannot use Delete key to do it. However,
1141 // Chromium doesn't consume such necessary modifiers. So, our default
1142 // behavior should keep not touching modifiers for compatibility, but
1143 // it might be better to add a pref to consume necessary modifiers.
1146 // If aGdkKeyEvent cannot be mapped to a DOM keyCode value, we should
1147 // refer keyCode value without modifiers because web apps should be
1148 // able to identify the key as far as possible.
1149 guint keyvalWithoutModifier
= GetGDKKeyvalWithoutModifier(aGdkKeyEvent
);
1150 return GetDOMKeyCodeFromKeyPairs(keyvalWithoutModifier
);
1153 // printable numpad keys should be resolved here.
1155 case GDK_KP_Multiply
:
1156 return NS_VK_MULTIPLY
;
1159 case GDK_KP_Separator
:
1160 return NS_VK_SEPARATOR
;
1161 case GDK_KP_Subtract
:
1162 return NS_VK_SUBTRACT
;
1163 case GDK_KP_Decimal
:
1164 return NS_VK_DECIMAL
;
1166 return NS_VK_DIVIDE
;
1168 return NS_VK_NUMPAD0
;
1170 return NS_VK_NUMPAD1
;
1172 return NS_VK_NUMPAD2
;
1174 return NS_VK_NUMPAD3
;
1176 return NS_VK_NUMPAD4
;
1178 return NS_VK_NUMPAD5
;
1180 return NS_VK_NUMPAD6
;
1182 return NS_VK_NUMPAD7
;
1184 return NS_VK_NUMPAD8
;
1186 return NS_VK_NUMPAD9
;
1189 KeymapWrapper
* keymapWrapper
= GetInstance();
1191 // Ignore all modifier state except NumLock.
1193 (aGdkKeyEvent
->state
& keymapWrapper
->GetGdkModifierMask(NUM_LOCK
));
1195 // Basically, we should use unmodified character for deciding our keyCode.
1196 uint32_t unmodifiedChar
= keymapWrapper
->GetCharCodeFor(
1197 aGdkKeyEvent
, baseState
, aGdkKeyEvent
->group
);
1198 if (IsBasicLatinLetterOrNumeral(unmodifiedChar
)) {
1199 // If the unmodified character is an ASCII alphabet or an ASCII
1200 // numeric, it's the best hint for deciding our keyCode.
1201 return WidgetUtils::ComputeKeyCodeFromChar(unmodifiedChar
);
1204 // If the unmodified character is not an ASCII character, that means we
1205 // couldn't find the hint. We should reset it.
1206 if (!IsPrintableASCIICharacter(unmodifiedChar
)) {
1210 // Retry with shifted keycode.
1211 guint shiftState
= (baseState
| keymapWrapper
->GetGdkModifierMask(SHIFT
));
1212 uint32_t shiftedChar
= keymapWrapper
->GetCharCodeFor(aGdkKeyEvent
, shiftState
,
1213 aGdkKeyEvent
->group
);
1214 if (IsBasicLatinLetterOrNumeral(shiftedChar
)) {
1215 // A shifted character can be an ASCII alphabet on Hebrew keyboard
1216 // layout. And also shifted character can be an ASCII numeric on
1217 // AZERTY keyboad layout. Then, it's a good hint for deciding our
1219 return WidgetUtils::ComputeKeyCodeFromChar(shiftedChar
);
1222 // If the shifted unmodified character isn't an ASCII character, we should
1224 if (!IsPrintableASCIICharacter(shiftedChar
)) {
1228 // If current keyboard layout isn't ASCII alphabet inputtable layout,
1229 // look for ASCII alphabet inputtable keyboard layout. If the key
1230 // inputs an ASCII alphabet or an ASCII numeric, we should use it
1231 // for deciding our keyCode.
1232 uint32_t unmodCharLatin
= 0;
1233 uint32_t shiftedCharLatin
= 0;
1234 if (!keymapWrapper
->IsLatinGroup(aGdkKeyEvent
->group
)) {
1235 gint minGroup
= keymapWrapper
->GetFirstLatinGroup();
1236 if (minGroup
>= 0) {
1238 keymapWrapper
->GetCharCodeFor(aGdkKeyEvent
, baseState
, minGroup
);
1239 if (IsBasicLatinLetterOrNumeral(unmodCharLatin
)) {
1240 // If the unmodified character is an ASCII alphabet or
1241 // an ASCII numeric, we should use it for the keyCode.
1242 return WidgetUtils::ComputeKeyCodeFromChar(unmodCharLatin
);
1244 // If the unmodified character in the alternative ASCII capable
1245 // keyboard layout isn't an ASCII character, that means we couldn't
1246 // find the hint. We should reset it.
1247 if (!IsPrintableASCIICharacter(unmodCharLatin
)) {
1251 keymapWrapper
->GetCharCodeFor(aGdkKeyEvent
, shiftState
, minGroup
);
1252 if (IsBasicLatinLetterOrNumeral(shiftedCharLatin
)) {
1253 // If the shifted character is an ASCII alphabet or an ASCII
1254 // numeric, we should use it for the keyCode.
1255 return WidgetUtils::ComputeKeyCodeFromChar(shiftedCharLatin
);
1257 // If the shifted unmodified character in the alternative ASCII
1258 // capable keyboard layout isn't an ASCII character, we should
1260 if (!IsPrintableASCIICharacter(shiftedCharLatin
)) {
1261 shiftedCharLatin
= 0;
1266 // If the key itself or with Shift state on active keyboard layout produces
1267 // an ASCII punctuation character, we should decide keyCode value with it.
1268 if (unmodifiedChar
|| shiftedChar
) {
1269 return WidgetUtils::ComputeKeyCodeFromChar(unmodifiedChar
? unmodifiedChar
1273 // If the key itself or with Shift state on alternative ASCII capable
1274 // keyboard layout produces an ASCII punctuation character, we should
1275 // decide keyCode value with it. Note that We've returned 0 for long
1276 // time if keyCode isn't for an alphabet keys or a numeric key even in
1277 // alternative ASCII capable keyboard layout because we decided that we
1278 // should avoid setting same keyCode value to 2 or more keys since active
1279 // keyboard layout may have a key to input the punctuation with different
1280 // key. However, setting keyCode to 0 makes some web applications which
1281 // are aware of neither KeyboardEvent.key nor KeyboardEvent.code not work
1282 // with Firefox when user selects non-ASCII capable keyboard layout such
1283 // as Russian and Thai. So, if alternative ASCII capable keyboard layout
1284 // has keyCode value for the key, we should use it. In other words, this
1285 // behavior means that non-ASCII capable keyboard layout overrides some
1286 // keys' keyCode value only if the key produces ASCII character by itself
1287 // or with Shift key.
1288 if (unmodCharLatin
|| shiftedCharLatin
) {
1289 return WidgetUtils::ComputeKeyCodeFromChar(
1290 unmodCharLatin
? unmodCharLatin
: shiftedCharLatin
);
1293 // Otherwise, let's decide keyCode value from the hardware_keycode
1294 // value on major keyboard layout.
1295 CodeNameIndex code
= ComputeDOMCodeNameIndex(aGdkKeyEvent
);
1296 return WidgetKeyboardEvent::GetFallbackKeyCodeOfPunctuationKey(code
);
1299 KeyNameIndex
KeymapWrapper::ComputeDOMKeyNameIndex(
1300 const GdkEventKey
* aGdkKeyEvent
) {
1301 switch (aGdkKeyEvent
->keyval
) {
1302 #define NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX(aNativeKey, aKeyNameIndex) \
1304 return aKeyNameIndex;
1306 #include "NativeKeyToDOMKeyName.h"
1308 #undef NS_NATIVE_KEY_TO_DOM_KEY_NAME_INDEX
1314 return KEY_NAME_INDEX_Unidentified
;
1318 CodeNameIndex
KeymapWrapper::ComputeDOMCodeNameIndex(
1319 const GdkEventKey
* aGdkKeyEvent
) {
1320 switch (aGdkKeyEvent
->hardware_keycode
) {
1321 #define NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX(aNativeKey, aCodeNameIndex) \
1323 return aCodeNameIndex;
1325 #include "NativeKeyToDOMCodeName.h"
1327 #undef NS_NATIVE_KEY_TO_DOM_CODE_NAME_INDEX
1333 return CODE_NAME_INDEX_UNKNOWN
;
1337 bool KeymapWrapper::DispatchKeyDownOrKeyUpEvent(nsWindow
* aWindow
,
1338 GdkEventKey
* aGdkKeyEvent
,
1339 bool aIsProcessedByIME
,
1340 bool* aIsCancelled
) {
1341 MOZ_ASSERT(aIsCancelled
, "aIsCancelled must not be nullptr");
1343 *aIsCancelled
= false;
1345 if (aGdkKeyEvent
->type
== GDK_KEY_PRESS
&& aGdkKeyEvent
->keyval
== GDK_Tab
&&
1346 AreModifiersActive(CTRL
| ALT
, aGdkKeyEvent
->state
)) {
1347 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1348 (" DispatchKeyDownOrKeyUpEvent(), didn't dispatch keyboard events "
1349 "because it's Ctrl + Alt + Tab"));
1353 EventMessage message
=
1354 aGdkKeyEvent
->type
== GDK_KEY_PRESS
? eKeyDown
: eKeyUp
;
1355 WidgetKeyboardEvent
keyEvent(true, message
, aWindow
);
1356 KeymapWrapper::InitKeyEvent(keyEvent
, aGdkKeyEvent
, aIsProcessedByIME
);
1357 return DispatchKeyDownOrKeyUpEvent(aWindow
, keyEvent
, aIsCancelled
);
1361 bool KeymapWrapper::DispatchKeyDownOrKeyUpEvent(
1362 nsWindow
* aWindow
, WidgetKeyboardEvent
& aKeyboardEvent
,
1363 bool* aIsCancelled
) {
1364 MOZ_ASSERT(aIsCancelled
, "aIsCancelled must not be nullptr");
1366 *aIsCancelled
= false;
1368 RefPtr
<TextEventDispatcher
> dispatcher
= aWindow
->GetTextEventDispatcher();
1369 nsresult rv
= dispatcher
->BeginNativeInputTransaction();
1370 if (NS_WARN_IF(NS_FAILED(rv
))) {
1371 MOZ_LOG(gKeyLog
, LogLevel::Error
,
1372 (" DispatchKeyDownOrKeyUpEvent(), stopped dispatching %s event "
1373 "because of failed to initialize TextEventDispatcher",
1374 ToChar(aKeyboardEvent
.mMessage
)));
1378 nsEventStatus status
= nsEventStatus_eIgnore
;
1379 bool dispatched
= dispatcher
->DispatchKeyboardEvent(
1380 aKeyboardEvent
.mMessage
, aKeyboardEvent
, status
, nullptr);
1381 *aIsCancelled
= (status
== nsEventStatus_eConsumeNoDefault
);
1386 bool KeymapWrapper::MaybeDispatchContextMenuEvent(nsWindow
* aWindow
,
1387 const GdkEventKey
* aEvent
) {
1388 KeyNameIndex keyNameIndex
= ComputeDOMKeyNameIndex(aEvent
);
1390 // Shift+F10 and ContextMenu should cause eContextMenu event.
1391 if (keyNameIndex
!= KEY_NAME_INDEX_F10
&&
1392 keyNameIndex
!= KEY_NAME_INDEX_ContextMenu
) {
1396 WidgetPointerEvent
contextMenuEvent(true, eContextMenu
, aWindow
,
1397 WidgetMouseEvent::eContextMenuKey
);
1398 contextMenuEvent
.mRefPoint
= LayoutDeviceIntPoint(0, 0);
1399 contextMenuEvent
.AssignEventTime(aWindow
->GetWidgetEventTime(aEvent
->time
));
1400 contextMenuEvent
.mClickCount
= 1;
1401 KeymapWrapper::InitInputEvent(contextMenuEvent
, aEvent
->state
);
1403 if (contextMenuEvent
.IsControl() || contextMenuEvent
.IsMeta() ||
1404 contextMenuEvent
.IsAlt()) {
1408 // If the key is ContextMenu, then an eContextMenu mouse event is
1409 // dispatched regardless of the state of the Shift modifier. When it is
1410 // pressed without the Shift modifier, a web page can prevent the default
1411 // context menu action. When pressed with the Shift modifier, the web page
1412 // cannot prevent the default context menu action.
1413 // (PresShell::HandleEventInternal() sets mOnlyChromeDispatch to true.)
1415 // If the key is F10, it needs Shift state because Shift+F10 is well-known
1416 // shortcut key on Linux. However, eContextMenu with Shift state is
1417 // special. It won't fire "contextmenu" event in the web content for
1418 // blocking web page to prevent its default. Therefore, this combination
1419 // should work same as ContextMenu key.
1420 // XXX Should we allow to block web page to prevent its default with
1421 // Ctrl+Shift+F10 or Alt+Shift+F10 instead?
1422 if (keyNameIndex
== KEY_NAME_INDEX_F10
) {
1423 if (!contextMenuEvent
.IsShift()) {
1426 contextMenuEvent
.mModifiers
&= ~MODIFIER_SHIFT
;
1429 aWindow
->DispatchInputEvent(&contextMenuEvent
);
1434 void KeymapWrapper::HandleKeyPressEvent(nsWindow
* aWindow
,
1435 GdkEventKey
* aGdkKeyEvent
) {
1436 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1437 ("HandleKeyPressEvent(aWindow=%p, aGdkKeyEvent={ type=%s, "
1438 "keyval=%s(0x%X), state=0x%08X, hardware_keycode=0x%08X, "
1439 "time=%u, is_modifier=%s })",
1441 ((aGdkKeyEvent
->type
== GDK_KEY_PRESS
) ? "GDK_KEY_PRESS"
1442 : "GDK_KEY_RELEASE"),
1443 gdk_keyval_name(aGdkKeyEvent
->keyval
), aGdkKeyEvent
->keyval
,
1444 aGdkKeyEvent
->state
, aGdkKeyEvent
->hardware_keycode
,
1445 aGdkKeyEvent
->time
, GetBoolName(aGdkKeyEvent
->is_modifier
)));
1447 // if we are in the middle of composing text, XIM gets to see it
1448 // before mozilla does.
1449 // FYI: Don't dispatch keydown event before notifying IME of the event
1450 // because IME may send a key event synchronously and consume the
1452 bool IMEWasEnabled
= false;
1453 KeyHandlingState handlingState
= KeyHandlingState::eNotHandled
;
1454 RefPtr
<IMContextWrapper
> imContext
= aWindow
->GetIMContext();
1456 IMEWasEnabled
= imContext
->IsEnabled();
1457 handlingState
= imContext
->OnKeyEvent(aWindow
, aGdkKeyEvent
);
1458 if (handlingState
== KeyHandlingState::eHandled
) {
1459 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1460 (" HandleKeyPressEvent(), the event was handled by "
1461 "IMContextWrapper"));
1466 // work around for annoying things.
1467 if (aGdkKeyEvent
->keyval
== GDK_Tab
&&
1468 AreModifiersActive(CTRL
| ALT
, aGdkKeyEvent
->state
)) {
1469 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1470 (" HandleKeyPressEvent(), didn't dispatch keyboard events "
1471 "because it's Ctrl + Alt + Tab"));
1475 // Dispatch keydown event always. At auto repeating, we should send
1476 // KEYDOWN -> KEYPRESS -> KEYDOWN -> KEYPRESS ... -> KEYUP
1477 // However, old distributions (e.g., Ubuntu 9.10) sent native key
1478 // release event, so, on such platform, the DOM events will be:
1479 // KEYDOWN -> KEYPRESS -> KEYUP -> KEYDOWN -> KEYPRESS -> KEYUP...
1481 bool isKeyDownCancelled
= false;
1482 if (handlingState
== KeyHandlingState::eNotHandled
) {
1483 if (DispatchKeyDownOrKeyUpEvent(aWindow
, aGdkKeyEvent
, false,
1484 &isKeyDownCancelled
) &&
1485 (MOZ_UNLIKELY(aWindow
->IsDestroyed()) || isKeyDownCancelled
)) {
1486 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1487 (" HandleKeyPressEvent(), dispatched eKeyDown event and "
1488 "stopped handling the event because %s",
1489 aWindow
->IsDestroyed() ? "the window has been destroyed"
1490 : "the event was consumed"));
1493 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1494 (" HandleKeyPressEvent(), dispatched eKeyDown event and "
1495 "it wasn't consumed"));
1496 handlingState
= KeyHandlingState::eNotHandledButEventDispatched
;
1499 // If a keydown event handler causes to enable IME, i.e., it moves
1500 // focus from IME unusable content to IME usable editor, we should
1501 // send the native key event to IME for the first input on the editor.
1502 imContext
= aWindow
->GetIMContext();
1503 if (!IMEWasEnabled
&& imContext
&& imContext
->IsEnabled()) {
1504 // Notice our keydown event was already dispatched. This prevents
1505 // unnecessary DOM keydown event in the editor.
1506 handlingState
= imContext
->OnKeyEvent(aWindow
, aGdkKeyEvent
, true);
1507 if (handlingState
== KeyHandlingState::eHandled
) {
1508 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1509 (" HandleKeyPressEvent(), the event was handled by "
1510 "IMContextWrapper which was enabled by the preceding eKeyDown "
1516 // Look for specialized app-command keys
1517 switch (aGdkKeyEvent
->keyval
) {
1519 aWindow
->DispatchCommandEvent(nsGkAtoms::Back
);
1520 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1521 (" HandleKeyPressEvent(), dispatched \"Back\" command event"));
1524 aWindow
->DispatchCommandEvent(nsGkAtoms::Forward
);
1525 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1526 (" HandleKeyPressEvent(), dispatched \"Forward\" command "
1531 aWindow
->DispatchCommandEvent(nsGkAtoms::Reload
);
1534 aWindow
->DispatchCommandEvent(nsGkAtoms::Stop
);
1535 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1536 (" HandleKeyPressEvent(), dispatched \"Stop\" command event"));
1539 aWindow
->DispatchCommandEvent(nsGkAtoms::Search
);
1540 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1541 (" HandleKeyPressEvent(), dispatched \"Search\" command event"));
1544 aWindow
->DispatchCommandEvent(nsGkAtoms::Bookmarks
);
1545 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1546 (" HandleKeyPressEvent(), dispatched \"Bookmarks\" command "
1550 aWindow
->DispatchCommandEvent(nsGkAtoms::Home
);
1553 case GDK_F16
: // F16, F20, F18, F14 are old keysyms for Copy Cut Paste Undo
1554 aWindow
->DispatchContentCommandEvent(eContentCommandCopy
);
1555 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1556 (" HandleKeyPressEvent(), dispatched \"Copy\" content command "
1561 aWindow
->DispatchContentCommandEvent(eContentCommandCut
);
1562 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1563 (" HandleKeyPressEvent(), dispatched \"Cut\" content command "
1568 aWindow
->DispatchContentCommandEvent(eContentCommandPaste
);
1569 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1570 (" HandleKeyPressEvent(), dispatched \"Paste\" content command "
1574 aWindow
->DispatchContentCommandEvent(eContentCommandRedo
);
1578 aWindow
->DispatchContentCommandEvent(eContentCommandUndo
);
1579 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1580 (" HandleKeyPressEvent(), dispatched \"Undo\" content command "
1587 // before we dispatch a key, check if it's the context menu key.
1588 // If so, send a context menu key event instead.
1589 if (MaybeDispatchContextMenuEvent(aWindow
, aGdkKeyEvent
)) {
1590 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1591 (" HandleKeyPressEvent(), stopped dispatching eKeyPress event "
1592 "because eContextMenu event was dispatched"));
1596 RefPtr
<TextEventDispatcher
> textEventDispatcher
=
1597 aWindow
->GetTextEventDispatcher();
1598 nsresult rv
= textEventDispatcher
->BeginNativeInputTransaction();
1599 if (NS_WARN_IF(NS_FAILED(rv
))) {
1600 MOZ_LOG(gKeyLog
, LogLevel::Error
,
1601 (" HandleKeyPressEvent(), stopped dispatching eKeyPress event "
1602 "because of failed to initialize TextEventDispatcher"));
1606 // If the character code is in the BMP, send the key press event.
1607 // Otherwise, send a compositionchange event with the equivalent UTF-16
1609 // TODO: Investigate other browser's behavior in this case because
1610 // this hack is odd for UI Events.
1611 WidgetKeyboardEvent
keypressEvent(true, eKeyPress
, aWindow
);
1612 KeymapWrapper::InitKeyEvent(keypressEvent
, aGdkKeyEvent
, false);
1613 nsEventStatus status
= nsEventStatus_eIgnore
;
1614 if (keypressEvent
.mKeyNameIndex
!= KEY_NAME_INDEX_USE_STRING
||
1615 keypressEvent
.mKeyValue
.Length() == 1) {
1616 if (textEventDispatcher
->MaybeDispatchKeypressEvents(keypressEvent
, status
,
1618 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1619 (" HandleKeyPressEvent(), dispatched eKeyPress event "
1621 GetStatusName(status
)));
1623 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1624 (" HandleKeyPressEvent(), didn't dispatch eKeyPress event "
1626 GetStatusName(status
)));
1629 WidgetEventTime eventTime
= aWindow
->GetWidgetEventTime(aGdkKeyEvent
->time
);
1630 textEventDispatcher
->CommitComposition(status
, &keypressEvent
.mKeyValue
,
1632 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1633 (" HandleKeyPressEvent(), dispatched a set of composition "
1639 bool KeymapWrapper::HandleKeyReleaseEvent(nsWindow
* aWindow
,
1640 GdkEventKey
* aGdkKeyEvent
) {
1641 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1642 ("HandleKeyReleaseEvent(aWindow=%p, aGdkKeyEvent={ type=%s, "
1643 "keyval=%s(0x%X), state=0x%08X, hardware_keycode=0x%08X, "
1644 "time=%u, is_modifier=%s })",
1646 ((aGdkKeyEvent
->type
== GDK_KEY_PRESS
) ? "GDK_KEY_PRESS"
1647 : "GDK_KEY_RELEASE"),
1648 gdk_keyval_name(aGdkKeyEvent
->keyval
), aGdkKeyEvent
->keyval
,
1649 aGdkKeyEvent
->state
, aGdkKeyEvent
->hardware_keycode
,
1650 aGdkKeyEvent
->time
, GetBoolName(aGdkKeyEvent
->is_modifier
)));
1652 RefPtr
<IMContextWrapper
> imContext
= aWindow
->GetIMContext();
1654 KeyHandlingState handlingState
=
1655 imContext
->OnKeyEvent(aWindow
, aGdkKeyEvent
);
1656 if (handlingState
!= KeyHandlingState::eNotHandled
) {
1657 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1658 (" HandleKeyReleaseEvent(), the event was handled by "
1659 "IMContextWrapper"));
1664 bool isCancelled
= false;
1665 if (NS_WARN_IF(!DispatchKeyDownOrKeyUpEvent(aWindow
, aGdkKeyEvent
, false,
1667 MOZ_LOG(gKeyLog
, LogLevel::Error
,
1668 (" HandleKeyReleaseEvent(), didn't dispatch eKeyUp event"));
1672 MOZ_LOG(gKeyLog
, LogLevel::Info
,
1673 (" HandleKeyReleaseEvent(), dispatched eKeyUp event "
1675 GetBoolName(isCancelled
)));
1679 guint
KeymapWrapper::GetModifierState(GdkEventKey
* aGdkKeyEvent
,
1680 KeymapWrapper
* aWrapper
) {
1681 guint state
= aGdkKeyEvent
->state
;
1682 if (!aGdkKeyEvent
->is_modifier
) {
1686 // NOTE: The state of given key event indicates adjacent state of
1687 // modifier keys. E.g., even if the event is Shift key press event,
1688 // the bit for Shift is still false. By the same token, even if the
1689 // event is Shift key release event, the bit for Shift is still true.
1690 // Unfortunately, gdk_keyboard_get_modifiers() returns current modifier
1691 // state. It means if there're some pending modifier key press or
1692 // key release events, the result isn't what we want.
1693 GdkDisplay
* gdkDisplay
= gdk_display_get_default();
1694 if (GdkIsX11Display(gdkDisplay
)) {
1695 GdkDisplay
* gdkDisplay
= gdk_display_get_default();
1696 Display
* display
= gdk_x11_display_get_xdisplay(gdkDisplay
);
1697 if (XEventsQueued(display
, QueuedAfterReading
)) {
1699 XPeekEvent(display
, &nextEvent
);
1700 if (nextEvent
.type
== aWrapper
->mXKBBaseEventCode
) {
1701 XkbEvent
* XKBEvent
= (XkbEvent
*)&nextEvent
;
1702 if (XKBEvent
->any
.xkb_type
== XkbStateNotify
) {
1703 XkbStateNotifyEvent
* stateNotifyEvent
=
1704 (XkbStateNotifyEvent
*)XKBEvent
;
1706 state
|= stateNotifyEvent
->lookup_mods
;
1715 switch (aGdkKeyEvent
->keyval
) {
1718 mask
= aWrapper
->GetGdkModifierMask(SHIFT
);
1722 mask
= aWrapper
->GetGdkModifierMask(CTRL
);
1726 mask
= aWrapper
->GetGdkModifierMask(ALT
);
1730 mask
= aWrapper
->GetGdkModifierMask(SUPER
);
1734 mask
= aWrapper
->GetGdkModifierMask(HYPER
);
1738 mask
= aWrapper
->GetGdkModifierMask(META
);
1743 if (aGdkKeyEvent
->type
== GDK_KEY_PRESS
) {
1753 void KeymapWrapper::InitKeyEvent(WidgetKeyboardEvent
& aKeyEvent
,
1754 GdkEventKey
* aGdkKeyEvent
,
1755 bool aIsProcessedByIME
) {
1757 !aIsProcessedByIME
|| aKeyEvent
.mMessage
!= eKeyPress
,
1758 "If the key event is handled by IME, keypress event shouldn't be fired");
1760 KeymapWrapper
* keymapWrapper
= GetInstance();
1762 aKeyEvent
.mCodeNameIndex
= ComputeDOMCodeNameIndex(aGdkKeyEvent
);
1763 MOZ_ASSERT(aKeyEvent
.mCodeNameIndex
!= CODE_NAME_INDEX_USE_STRING
);
1764 aKeyEvent
.mKeyNameIndex
=
1765 aIsProcessedByIME
? KEY_NAME_INDEX_Process
1766 : keymapWrapper
->ComputeDOMKeyNameIndex(aGdkKeyEvent
);
1767 if (aKeyEvent
.mKeyNameIndex
== KEY_NAME_INDEX_Unidentified
) {
1768 uint32_t charCode
= GetCharCodeFor(aGdkKeyEvent
);
1770 charCode
= keymapWrapper
->GetUnmodifiedCharCodeFor(aGdkKeyEvent
);
1773 aKeyEvent
.mKeyNameIndex
= KEY_NAME_INDEX_USE_STRING
;
1774 MOZ_ASSERT(aKeyEvent
.mKeyValue
.IsEmpty(),
1775 "Uninitialized mKeyValue must be empty");
1776 AppendUCS4ToUTF16(charCode
, aKeyEvent
.mKeyValue
);
1780 if (aIsProcessedByIME
) {
1781 aKeyEvent
.mKeyCode
= NS_VK_PROCESSKEY
;
1782 } else if (aKeyEvent
.mKeyNameIndex
!= KEY_NAME_INDEX_USE_STRING
||
1783 aKeyEvent
.mMessage
!= eKeyPress
) {
1784 aKeyEvent
.mKeyCode
= ComputeDOMKeyCode(aGdkKeyEvent
);
1786 aKeyEvent
.mKeyCode
= 0;
1789 const guint modifierState
= GetModifierState(aGdkKeyEvent
, keymapWrapper
);
1790 InitInputEvent(aKeyEvent
, modifierState
);
1792 switch (aGdkKeyEvent
->keyval
) {
1799 aKeyEvent
.mLocation
= eKeyLocationLeft
;
1808 aKeyEvent
.mLocation
= eKeyLocationRight
;
1833 case GDK_KP_Prior
: // same as GDK_KP_Page_Up
1834 case GDK_KP_Next
: // same as GDK_KP_Page_Down
1840 case GDK_KP_Multiply
:
1842 case GDK_KP_Separator
:
1843 case GDK_KP_Subtract
:
1844 case GDK_KP_Decimal
:
1846 aKeyEvent
.mLocation
= eKeyLocationNumpad
;
1850 aKeyEvent
.mLocation
= eKeyLocationStandard
;
1854 // The transformations above and in gdk for the keyval are not invertible
1855 // so link to the GdkEvent (which will vanish soon after return from the
1856 // event callback) to give plugins access to hardware_keycode and state.
1857 // (An XEvent would be nice but the GdkEvent is good enough.)
1858 aKeyEvent
.mNativeKeyEvent
= static_cast<void*>(aGdkKeyEvent
);
1859 aKeyEvent
.mIsRepeat
=
1860 sRepeatState
== REPEATING
&&
1861 aGdkKeyEvent
->hardware_keycode
== sLastRepeatableHardwareKeyCode
;
1864 gKeyLog
, LogLevel::Info
,
1865 ("%p InitKeyEvent, modifierState=0x%08X "
1866 "aKeyEvent={ mMessage=%s, isShift=%s, isControl=%s, "
1867 "isAlt=%s, isMeta=%s , mKeyCode=0x%02X, mCharCode=%s, "
1868 "mKeyNameIndex=%s, mKeyValue=%s, mCodeNameIndex=%s, mCodeValue=%s, "
1869 "mLocation=%s, mIsRepeat=%s }",
1870 keymapWrapper
, modifierState
, ToChar(aKeyEvent
.mMessage
),
1871 GetBoolName(aKeyEvent
.IsShift()), GetBoolName(aKeyEvent
.IsControl()),
1872 GetBoolName(aKeyEvent
.IsAlt()), GetBoolName(aKeyEvent
.IsMeta()),
1874 GetCharacterCodeName(static_cast<char16_t
>(aKeyEvent
.mCharCode
)).get(),
1875 ToString(aKeyEvent
.mKeyNameIndex
).get(),
1876 GetCharacterCodeNames(aKeyEvent
.mKeyValue
).get(),
1877 ToString(aKeyEvent
.mCodeNameIndex
).get(),
1878 GetCharacterCodeNames(aKeyEvent
.mCodeValue
).get(),
1879 GetKeyLocationName(aKeyEvent
.mLocation
).get(),
1880 GetBoolName(aKeyEvent
.mIsRepeat
)));
1884 uint32_t KeymapWrapper::GetCharCodeFor(const GdkEventKey
* aGdkKeyEvent
) {
1885 // Anything above 0xf000 is considered a non-printable
1886 // Exception: directly encoded UCS characters
1887 if (aGdkKeyEvent
->keyval
> 0xf000 &&
1888 (aGdkKeyEvent
->keyval
& 0xff000000) != 0x01000000) {
1889 // Keypad keys are an exception: they return a value different
1890 // from their non-keypad equivalents, but mozilla doesn't distinguish.
1891 switch (aGdkKeyEvent
->keyval
) {
1896 case GDK_KP_Multiply
:
1900 case GDK_KP_Separator
:
1902 case GDK_KP_Subtract
:
1904 case GDK_KP_Decimal
:
1929 return 0; // non-printables
1933 static const long MAX_UNICODE
= 0x10FFFF;
1935 // we're supposedly printable, let's try to convert
1936 long ucs
= keysym2ucs(aGdkKeyEvent
->keyval
);
1937 if ((ucs
!= -1) && (ucs
< MAX_UNICODE
)) {
1941 // I guess we couldn't convert
1945 uint32_t KeymapWrapper::GetCharCodeFor(const GdkEventKey
* aGdkKeyEvent
,
1946 guint aGdkModifierState
, gint aGroup
) {
1948 if (!gdk_keymap_translate_keyboard_state(
1949 mGdkKeymap
, aGdkKeyEvent
->hardware_keycode
,
1950 GdkModifierType(aGdkModifierState
), aGroup
, &keyval
, nullptr, nullptr,
1954 GdkEventKey tmpEvent
= *aGdkKeyEvent
;
1955 tmpEvent
.state
= aGdkModifierState
;
1956 tmpEvent
.keyval
= keyval
;
1957 tmpEvent
.group
= aGroup
;
1958 return GetCharCodeFor(&tmpEvent
);
1961 uint32_t KeymapWrapper::GetUnmodifiedCharCodeFor(
1962 const GdkEventKey
* aGdkKeyEvent
) {
1964 aGdkKeyEvent
->state
&
1965 (GetGdkModifierMask(SHIFT
) | GetGdkModifierMask(CAPS_LOCK
) |
1966 GetGdkModifierMask(NUM_LOCK
) | GetGdkModifierMask(SCROLL_LOCK
) |
1967 GetGdkModifierMask(LEVEL3
) | GetGdkModifierMask(LEVEL5
));
1969 GetCharCodeFor(aGdkKeyEvent
, GdkModifierType(state
), aGdkKeyEvent
->group
);
1973 // If no character is mapped to the key when Level3 Shift or Level5 Shift
1974 // is active, let's return a character which is inputted by the key without
1975 // Level3 nor Level5 Shift.
1976 guint stateWithoutAltGraph
=
1977 state
& ~(GetGdkModifierMask(LEVEL3
) | GetGdkModifierMask(LEVEL5
));
1978 if (state
== stateWithoutAltGraph
) {
1981 return GetCharCodeFor(aGdkKeyEvent
, GdkModifierType(stateWithoutAltGraph
),
1982 aGdkKeyEvent
->group
);
1985 gint
KeymapWrapper::GetKeyLevel(GdkEventKey
* aGdkKeyEvent
) {
1987 if (!gdk_keymap_translate_keyboard_state(
1988 mGdkKeymap
, aGdkKeyEvent
->hardware_keycode
,
1989 GdkModifierType(aGdkKeyEvent
->state
), aGdkKeyEvent
->group
, nullptr,
1990 nullptr, &level
, nullptr)) {
1996 gint
KeymapWrapper::GetFirstLatinGroup() {
2000 if (gdk_keymap_get_entries_for_keyval(mGdkKeymap
, GDK_a
, &keys
, &count
)) {
2001 // find the minimum number group for latin inputtable layout
2002 for (gint i
= 0; i
< count
&& minGroup
!= 0; ++i
) {
2003 if (keys
[i
].level
!= 0 && keys
[i
].level
!= 1) {
2006 if (minGroup
>= 0 && keys
[i
].group
> minGroup
) {
2009 minGroup
= keys
[i
].group
;
2016 bool KeymapWrapper::IsLatinGroup(guint8 aGroup
) {
2019 bool result
= false;
2020 if (gdk_keymap_get_entries_for_keyval(mGdkKeymap
, GDK_a
, &keys
, &count
)) {
2021 for (gint i
= 0; i
< count
; ++i
) {
2022 if (keys
[i
].level
!= 0 && keys
[i
].level
!= 1) {
2025 if (keys
[i
].group
== aGroup
) {
2035 bool KeymapWrapper::IsAutoRepeatableKey(guint aHardwareKeyCode
) {
2037 uint8_t indexOfArray
= aHardwareKeyCode
/ 8;
2038 MOZ_ASSERT(indexOfArray
< std::size(mKeyboardState
.auto_repeats
),
2040 char bitMask
= 1 << (aHardwareKeyCode
% 8);
2041 return (mKeyboardState
.auto_repeats
[indexOfArray
] & bitMask
) != 0;
2048 bool KeymapWrapper::IsBasicLatinLetterOrNumeral(uint32_t aCharCode
) {
2049 return (aCharCode
>= 'a' && aCharCode
<= 'z') ||
2050 (aCharCode
>= 'A' && aCharCode
<= 'Z') ||
2051 (aCharCode
>= '0' && aCharCode
<= '9');
2055 guint
KeymapWrapper::GetGDKKeyvalWithoutModifier(
2056 const GdkEventKey
* aGdkKeyEvent
) {
2057 KeymapWrapper
* keymapWrapper
= GetInstance();
2059 (aGdkKeyEvent
->state
& keymapWrapper
->GetGdkModifierMask(NUM_LOCK
));
2061 if (!gdk_keymap_translate_keyboard_state(
2062 keymapWrapper
->mGdkKeymap
, aGdkKeyEvent
->hardware_keycode
,
2063 GdkModifierType(state
), aGdkKeyEvent
->group
, &keyval
, nullptr,
2064 nullptr, nullptr)) {
2070 struct KeyCodeData
{
2076 static struct KeyCodeData gKeyCodes
[] = {
2077 #define NS_DEFINE_VK(aDOMKeyName, aDOMKeyCode) \
2078 {#aDOMKeyName, sizeof(#aDOMKeyName) - 1, aDOMKeyCode},
2079 #include "mozilla/VirtualKeyCodeList.h"
2084 uint32_t DOMKeyCode
;
2089 // Netscape keycodes are defined in widget/public/nsGUIEvent.h
2090 // GTK keycodes are defined in <gdk/gdkkeysyms.h>
2092 static const KeyPair gKeyPairs
[] = {
2093 {NS_VK_CANCEL
, GDK_Cancel
},
2094 {NS_VK_BACK
, GDK_BackSpace
},
2095 {NS_VK_TAB
, GDK_Tab
},
2096 {NS_VK_CLEAR
, GDK_Clear
},
2097 {NS_VK_RETURN
, GDK_Return
},
2098 {NS_VK_SHIFT
, GDK_Shift_L
},
2099 {NS_VK_CONTROL
, GDK_Control_L
},
2100 {NS_VK_ALT
, GDK_Alt_L
},
2101 {NS_VK_META
, GDK_Meta_L
},
2103 // Assume that Super or Hyper is always mapped to physical Win key.
2104 {NS_VK_WIN
, GDK_Super_L
},
2106 // GTK's AltGraph key is similar to Mac's Option (Alt) key. However,
2107 // unfortunately, browsers on Mac are using NS_VK_ALT for it even though
2108 // it's really different from Alt key on Windows.
2109 // On the other hand, GTK's AltGrapsh keys are really different from
2110 // Alt key. However, there is no AltGrapsh key on Windows. On Windows,
2111 // both Ctrl and Alt keys are pressed internally when AltGr key is pressed.
2112 // For some languages' users, AltGraph key is important, so, web
2113 // applications on such locale may want to know AltGraph key press.
2114 // Therefore, we should map AltGr keycode for them only on GTK.
2115 {NS_VK_ALTGR
, GDK_ISO_Level3_Shift
},
2117 {NS_VK_PAUSE
, GDK_Pause
},
2118 {NS_VK_CAPS_LOCK
, GDK_Caps_Lock
},
2119 {NS_VK_ESCAPE
, GDK_Escape
},
2120 // { NS_VK_ACCEPT, GDK_XXX },
2121 // { NS_VK_MODECHANGE, GDK_XXX },
2122 {NS_VK_SPACE
, GDK_space
},
2123 {NS_VK_PAGE_UP
, GDK_Page_Up
},
2124 {NS_VK_PAGE_DOWN
, GDK_Page_Down
},
2125 {NS_VK_END
, GDK_End
},
2126 {NS_VK_HOME
, GDK_Home
},
2127 {NS_VK_LEFT
, GDK_Left
},
2129 {NS_VK_RIGHT
, GDK_Right
},
2130 {NS_VK_DOWN
, GDK_Down
},
2131 {NS_VK_SELECT
, GDK_Select
},
2132 {NS_VK_PRINT
, GDK_Print
},
2133 {NS_VK_EXECUTE
, GDK_Execute
},
2134 {NS_VK_PRINTSCREEN
, GDK_Print
},
2135 {NS_VK_INSERT
, GDK_Insert
},
2136 {NS_VK_DELETE
, GDK_Delete
},
2137 {NS_VK_HELP
, GDK_Help
},
2139 {NS_VK_NUM_LOCK
, GDK_Num_Lock
},
2140 {NS_VK_SCROLL_LOCK
, GDK_Scroll_Lock
},
2152 {NS_VK_F10
, GDK_F10
},
2153 {NS_VK_F11
, GDK_F11
},
2154 {NS_VK_F12
, GDK_F12
},
2155 {NS_VK_F13
, GDK_F13
},
2156 {NS_VK_F14
, GDK_F14
},
2157 {NS_VK_F15
, GDK_F15
},
2158 {NS_VK_F16
, GDK_F16
},
2159 {NS_VK_F17
, GDK_F17
},
2160 {NS_VK_F18
, GDK_F18
},
2161 {NS_VK_F19
, GDK_F19
},
2162 {NS_VK_F20
, GDK_F20
},
2163 {NS_VK_F21
, GDK_F21
},
2164 {NS_VK_F22
, GDK_F22
},
2165 {NS_VK_F23
, GDK_F23
},
2166 {NS_VK_F24
, GDK_F24
},
2168 // context menu key, keysym 0xff67, typically keycode 117 on 105-key
2169 // (Microsoft) x86 keyboards, located between right 'Windows' key and right
2171 {NS_VK_CONTEXT_MENU
, GDK_Menu
},
2172 {NS_VK_SLEEP
, GDK_Sleep
},
2174 {NS_VK_ATTN
, GDK_3270_Attn
},
2175 {NS_VK_CRSEL
, GDK_3270_CursorSelect
},
2176 {NS_VK_EXSEL
, GDK_3270_ExSelect
},
2177 {NS_VK_EREOF
, GDK_3270_EraseEOF
},
2178 {NS_VK_PLAY
, GDK_3270_Play
},
2179 //{ NS_VK_ZOOM, GDK_XXX },
2180 {NS_VK_PA1
, GDK_3270_PA1
},
2182 {NS_VK_MULTIPLY
, GDK_KP_Multiply
},
2183 {NS_VK_ADD
, GDK_KP_Add
},
2184 {NS_VK_SEPARATOR
, GDK_KP_Separator
},
2185 {NS_VK_SUBTRACT
, GDK_KP_Subtract
},
2186 {NS_VK_DECIMAL
, GDK_KP_Decimal
},
2187 {NS_VK_DIVIDE
, GDK_KP_Divide
},
2188 {NS_VK_NUMPAD0
, GDK_KP_0
},
2189 {NS_VK_NUMPAD1
, GDK_KP_1
},
2190 {NS_VK_NUMPAD2
, GDK_KP_2
},
2191 {NS_VK_NUMPAD3
, GDK_KP_3
},
2192 {NS_VK_NUMPAD4
, GDK_KP_4
},
2193 {NS_VK_NUMPAD5
, GDK_KP_5
},
2194 {NS_VK_NUMPAD6
, GDK_KP_6
},
2195 {NS_VK_NUMPAD7
, GDK_KP_7
},
2196 {NS_VK_NUMPAD8
, GDK_KP_8
},
2197 {NS_VK_NUMPAD9
, GDK_KP_9
},
2198 {NS_VK_SPACE
, GDK_space
},
2199 {NS_VK_COLON
, GDK_colon
},
2200 {NS_VK_SEMICOLON
, GDK_semicolon
},
2201 {NS_VK_LESS_THAN
, GDK_less
},
2202 {NS_VK_EQUALS
, GDK_equal
},
2203 {NS_VK_GREATER_THAN
, GDK_greater
},
2204 {NS_VK_QUESTION_MARK
, GDK_question
},
2206 {NS_VK_CIRCUMFLEX
, GDK_asciicircum
},
2207 {NS_VK_EXCLAMATION
, GDK_exclam
},
2208 {NS_VK_DOUBLE_QUOTE
, GDK_quotedbl
},
2209 {NS_VK_HASH
, GDK_numbersign
},
2210 {NS_VK_DOLLAR
, GDK_dollar
},
2211 {NS_VK_PERCENT
, GDK_percent
},
2212 {NS_VK_AMPERSAND
, GDK_ampersand
},
2213 {NS_VK_UNDERSCORE
, GDK_underscore
},
2214 {NS_VK_OPEN_PAREN
, GDK_parenleft
},
2215 {NS_VK_CLOSE_PAREN
, GDK_parenright
},
2216 {NS_VK_ASTERISK
, GDK_asterisk
},
2217 {NS_VK_PLUS
, GDK_plus
},
2218 {NS_VK_PIPE
, GDK_bar
},
2219 {NS_VK_HYPHEN_MINUS
, GDK_minus
},
2220 {NS_VK_OPEN_CURLY_BRACKET
, GDK_braceleft
},
2221 {NS_VK_CLOSE_CURLY_BRACKET
, GDK_braceright
},
2222 {NS_VK_TILDE
, GDK_asciitilde
},
2223 {NS_VK_COMMA
, GDK_comma
},
2224 {NS_VK_PERIOD
, GDK_period
},
2225 {NS_VK_SLASH
, GDK_slash
},
2226 {NS_VK_BACK_QUOTE
, GDK_grave
},
2227 {NS_VK_OPEN_BRACKET
, GDK_bracketleft
},
2228 {NS_VK_BACK_SLASH
, GDK_backslash
},
2229 {NS_VK_CLOSE_BRACKET
, GDK_bracketright
},
2230 {NS_VK_QUOTE
, GDK_apostrophe
},
2234 guint
KeymapWrapper::ConvertGeckoKeyCodeToGDKKeyval(const nsAString
& aKeyCode
) {
2235 NS_ConvertUTF16toUTF8
keyName(aKeyCode
);
2236 ToUpperCase(keyName
); // We want case-insensitive comparison with data
2237 // stored as uppercase.
2239 uint32_t keyCode
= 0;
2241 uint32_t keyNameLength
= keyName
.Length();
2242 const char* keyNameStr
= keyName
.get();
2243 for (const auto& code
: gKeyCodes
) {
2244 if (keyNameLength
== code
.strlength
&&
2245 !nsCRT::strcmp(code
.str
, keyNameStr
)) {
2246 keyCode
= code
.keycode
;
2251 // First, try to handle alphanumeric input, not listed in nsKeycodes:
2252 // most likely, more letters will be getting typed in than things in
2253 // the key list, so we will look through these first.
2255 if (keyCode
>= NS_VK_A
&& keyCode
<= NS_VK_Z
) {
2256 // gdk and DOM both use the ASCII codes for these keys.
2261 if (keyCode
>= NS_VK_0
&& keyCode
<= NS_VK_9
) {
2262 // gdk and DOM both use the ASCII codes for these keys.
2263 return keyCode
- NS_VK_0
+ GDK_0
;
2266 // misc other things
2267 for (const auto& pair
: gKeyPairs
) {
2268 if (pair
.DOMKeyCode
== keyCode
) {
2269 return pair
.GDKKeyval
;
2277 uint32_t KeymapWrapper::GetDOMKeyCodeFromKeyPairs(guint aGdkKeyval
) {
2278 switch (aGdkKeyval
) {
2280 return NS_VK_CANCEL
;
2284 case GDK_ISO_Left_Tab
:
2289 return NS_VK_RETURN
;
2292 case GDK_Shift_Lock
:
2296 return NS_VK_CONTROL
;
2304 // Assume that Super or Hyper is always mapped to physical Win key.
2311 // GTK's AltGraph key is similar to Mac's Option (Alt) key. However,
2312 // unfortunately, browsers on Mac are using NS_VK_ALT for it even though
2313 // it's really different from Alt key on Windows.
2314 // On the other hand, GTK's AltGrapsh keys are really different from
2315 // Alt key. However, there is no AltGrapsh key on Windows. On Windows,
2316 // both Ctrl and Alt keys are pressed internally when AltGr key is
2317 // pressed. For some languages' users, AltGraph key is important, so,
2318 // web applications on such locale may want to know AltGraph key press.
2319 // Therefore, we should map AltGr keycode for them only on GTK.
2320 case GDK_ISO_Level3_Shift
:
2321 case GDK_ISO_Level5_Shift
:
2322 // We assume that Mode_switch is always used for level3 shift.
2323 case GDK_Mode_switch
:
2329 return NS_VK_CAPS_LOCK
;
2331 case GDK_Kana_Shift
:
2334 return NS_VK_HANGUL
;
2335 // case GDK_XXX: return NS_VK_JUNJA;
2336 // case GDK_XXX: return NS_VK_FINAL;
2337 case GDK_Hangul_Hanja
:
2342 return NS_VK_ESCAPE
;
2344 return NS_VK_CONVERT
;
2346 return NS_VK_NONCONVERT
;
2347 // case GDK_XXX: return NS_VK_ACCEPT;
2348 // case GDK_XXX: return NS_VK_MODECHANGE;
2350 return NS_VK_PAGE_UP
;
2352 return NS_VK_PAGE_DOWN
;
2366 return NS_VK_SELECT
;
2370 return NS_VK_EXECUTE
;
2372 return NS_VK_INSERT
;
2374 return NS_VK_DELETE
;
2387 case GDK_KP_Page_Up
:
2388 return NS_VK_PAGE_UP
;
2389 // Not sure what these are
2390 // case GDK_KP_Prior: return NS_VK_;
2391 // case GDK_KP_Next: return NS_VK_;
2393 return NS_VK_CLEAR
; // Num-unlocked 5
2394 case GDK_KP_Page_Down
:
2395 return NS_VK_PAGE_DOWN
;
2401 return NS_VK_INSERT
;
2403 return NS_VK_DELETE
;
2405 return NS_VK_RETURN
;
2408 return NS_VK_NUM_LOCK
;
2409 case GDK_Scroll_Lock
:
2410 return NS_VK_SCROLL_LOCK
;
2462 // context menu key, keysym 0xff67, typically keycode 117 on 105-key
2463 // (Microsoft) x86 keyboards, located between right 'Windows' key and
2466 return NS_VK_CONTEXT_MENU
;
2472 case GDK_3270_CursorSelect
:
2474 case GDK_3270_ExSelect
:
2476 case GDK_3270_EraseEOF
:
2480 // case GDK_XXX: return NS_VK_ZOOM;
2484 // map Sun Keyboard special keysyms on to NS_VK keys
2486 // Sun F11 key generates SunF36(0x1005ff10) keysym
2489 // Sun F12 key generates SunF37(0x1005ff11) keysym
2497 void KeymapWrapper::WillDispatchKeyboardEvent(WidgetKeyboardEvent
& aKeyEvent
,
2498 GdkEventKey
* aGdkKeyEvent
) {
2499 GetInstance()->WillDispatchKeyboardEventInternal(aKeyEvent
, aGdkKeyEvent
);
2502 void KeymapWrapper::WillDispatchKeyboardEventInternal(
2503 WidgetKeyboardEvent
& aKeyEvent
, GdkEventKey
* aGdkKeyEvent
) {
2504 if (!aGdkKeyEvent
) {
2505 // If aGdkKeyEvent is nullptr, we're trying to dispatch a fake keyboard
2506 // event in such case, we don't need to set alternative char codes.
2507 // So, we don't need to do nothing here. This case is typically we're
2508 // dispatching eKeyDown or eKeyUp event during composition.
2512 uint32_t charCode
= GetCharCodeFor(aGdkKeyEvent
);
2514 MOZ_LOG(gKeyLog
, LogLevel::Info
,
2515 ("%p WillDispatchKeyboardEventInternal, "
2516 "mKeyCode=0x%02X, charCode=0x%08X",
2517 this, aKeyEvent
.mKeyCode
, aKeyEvent
.mCharCode
));
2521 // The mCharCode was set from mKeyValue. However, for example, when Ctrl key
2522 // is pressed, its value should indicate an ASCII character for backward
2523 // compatibility rather than inputting character without the modifiers.
2524 // Therefore, we need to modify mCharCode value here.
2525 aKeyEvent
.SetCharCode(charCode
);
2527 gint level
= GetKeyLevel(aGdkKeyEvent
);
2528 if (level
!= 0 && level
!= 1) {
2529 MOZ_LOG(gKeyLog
, LogLevel::Info
,
2530 ("%p WillDispatchKeyboardEventInternal, "
2531 "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d",
2532 this, aKeyEvent
.mKeyCode
, aKeyEvent
.mCharCode
, level
));
2536 guint baseState
= aGdkKeyEvent
->state
&
2537 ~(GetGdkModifierMask(SHIFT
) | GetGdkModifierMask(CTRL
) |
2538 GetGdkModifierMask(ALT
) | GetGdkModifierMask(META
) |
2539 GetGdkModifierMask(SUPER
) | GetGdkModifierMask(HYPER
));
2541 // We shold send both shifted char and unshifted char, all keyboard layout
2542 // users can use all keys. Don't change event.mCharCode. On some keyboard
2543 // layouts, Ctrl/Alt/Meta keys are used for inputting some characters.
2544 AlternativeCharCode
altCharCodes(0, 0);
2545 // unshifted charcode of current keyboard layout.
2546 altCharCodes
.mUnshiftedCharCode
=
2547 GetCharCodeFor(aGdkKeyEvent
, baseState
, aGdkKeyEvent
->group
);
2548 bool isLatin
= (altCharCodes
.mUnshiftedCharCode
<= 0xFF);
2549 // shifted charcode of current keyboard layout.
2550 altCharCodes
.mShiftedCharCode
= GetCharCodeFor(
2551 aGdkKeyEvent
, baseState
| GetGdkModifierMask(SHIFT
), aGdkKeyEvent
->group
);
2552 isLatin
= isLatin
&& (altCharCodes
.mShiftedCharCode
<= 0xFF);
2553 if (altCharCodes
.mUnshiftedCharCode
|| altCharCodes
.mShiftedCharCode
) {
2554 aKeyEvent
.mAlternativeCharCodes
.AppendElement(altCharCodes
);
2557 bool needLatinKeyCodes
= !isLatin
;
2558 if (!needLatinKeyCodes
) {
2560 (IS_ASCII_ALPHABETICAL(altCharCodes
.mUnshiftedCharCode
) !=
2561 IS_ASCII_ALPHABETICAL(altCharCodes
.mShiftedCharCode
));
2564 // If current keyboard layout can input Latin characters, we don't need
2565 // more information.
2566 if (!needLatinKeyCodes
) {
2567 MOZ_LOG(gKeyLog
, LogLevel::Info
,
2568 ("%p WillDispatchKeyboardEventInternal, "
2569 "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d, altCharCodes={ "
2570 "mUnshiftedCharCode=0x%08X, mShiftedCharCode=0x%08X }",
2571 this, aKeyEvent
.mKeyCode
, aKeyEvent
.mCharCode
, level
,
2572 altCharCodes
.mUnshiftedCharCode
, altCharCodes
.mShiftedCharCode
));
2576 // Next, find Latin inputtable keyboard layout.
2577 gint minGroup
= GetFirstLatinGroup();
2579 MOZ_LOG(gKeyLog
, LogLevel::Info
,
2580 ("%p WillDispatchKeyboardEventInternal, "
2581 "Latin keyboard layout isn't found: "
2582 "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d, "
2583 "altCharCodes={ mUnshiftedCharCode=0x%08X, "
2584 "mShiftedCharCode=0x%08X }",
2585 this, aKeyEvent
.mKeyCode
, aKeyEvent
.mCharCode
, level
,
2586 altCharCodes
.mUnshiftedCharCode
, altCharCodes
.mShiftedCharCode
));
2590 AlternativeCharCode
altLatinCharCodes(0, 0);
2591 uint32_t unmodifiedCh
= aKeyEvent
.IsShift() ? altCharCodes
.mShiftedCharCode
2592 : altCharCodes
.mUnshiftedCharCode
;
2594 // unshifted charcode of found keyboard layout.
2595 uint32_t ch
= GetCharCodeFor(aGdkKeyEvent
, baseState
, minGroup
);
2596 altLatinCharCodes
.mUnshiftedCharCode
=
2597 IsBasicLatinLetterOrNumeral(ch
) ? ch
: 0;
2598 // shifted charcode of found keyboard layout.
2599 ch
= GetCharCodeFor(aGdkKeyEvent
, baseState
| GetGdkModifierMask(SHIFT
),
2601 altLatinCharCodes
.mShiftedCharCode
= IsBasicLatinLetterOrNumeral(ch
) ? ch
: 0;
2602 if (altLatinCharCodes
.mUnshiftedCharCode
||
2603 altLatinCharCodes
.mShiftedCharCode
) {
2604 aKeyEvent
.mAlternativeCharCodes
.AppendElement(altLatinCharCodes
);
2606 // If the mCharCode is not Latin, and the level is 0 or 1, we should
2607 // replace the mCharCode to Latin char if Alt keys are not pressed. (Alt
2608 // should be sent the localized char for accesskey like handling of Web
2610 ch
= aKeyEvent
.IsShift() ? altLatinCharCodes
.mShiftedCharCode
2611 : altLatinCharCodes
.mUnshiftedCharCode
;
2612 if (ch
&& !aKeyEvent
.IsAlt() && charCode
== unmodifiedCh
) {
2613 aKeyEvent
.SetCharCode(ch
);
2616 MOZ_LOG(gKeyLog
, LogLevel::Info
,
2617 ("%p WillDispatchKeyboardEventInternal, "
2618 "mKeyCode=0x%02X, mCharCode=0x%08X, level=%d, minGroup=%d, "
2619 "altCharCodes={ mUnshiftedCharCode=0x%08X, "
2620 "mShiftedCharCode=0x%08X } "
2621 "altLatinCharCodes={ mUnshiftedCharCode=0x%08X, "
2622 "mShiftedCharCode=0x%08X }",
2623 this, aKeyEvent
.mKeyCode
, aKeyEvent
.mCharCode
, level
, minGroup
,
2624 altCharCodes
.mUnshiftedCharCode
, altCharCodes
.mShiftedCharCode
,
2625 altLatinCharCodes
.mUnshiftedCharCode
,
2626 altLatinCharCodes
.mShiftedCharCode
));
2630 void KeymapWrapper::SetFocusIn(wl_surface
* aFocusSurface
,
2631 uint32_t aFocusSerial
) {
2632 LOGW("KeymapWrapper::SetFocusIn() surface %p ID %d serial %d", aFocusSurface
,
2633 aFocusSurface
? wl_proxy_get_id((struct wl_proxy
*)aFocusSurface
) : 0,
2636 KeymapWrapper
* keymapWrapper
= KeymapWrapper::GetInstance();
2637 keymapWrapper
->mFocusSurface
= aFocusSurface
;
2638 keymapWrapper
->mFocusSerial
= aFocusSerial
;
2641 // aFocusSurface can be null in case that focused surface is already destroyed.
2642 void KeymapWrapper::SetFocusOut(wl_surface
* aFocusSurface
) {
2643 KeymapWrapper
* keymapWrapper
= KeymapWrapper::GetInstance();
2644 LOGW("KeymapWrapper::SetFocusOut surface %p ID %d", aFocusSurface
,
2645 aFocusSurface
? wl_proxy_get_id((struct wl_proxy
*)aFocusSurface
) : 0);
2647 keymapWrapper
->mFocusSurface
= nullptr;
2648 keymapWrapper
->mFocusSerial
= 0;
2651 void KeymapWrapper::GetFocusInfo(wl_surface
** aFocusSurface
,
2652 uint32_t* aFocusSerial
) {
2653 KeymapWrapper
* keymapWrapper
= KeymapWrapper::GetInstance();
2654 *aFocusSurface
= keymapWrapper
->mFocusSurface
;
2655 *aFocusSerial
= keymapWrapper
->mFocusSerial
;
2659 } // namespace widget
2660 } // namespace mozilla