/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim:expandtab:shiftwidth=4:tabstop=4:
*/ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifdef MOZ_WAYLAND # include <sys/mman.h> # include "nsWaylandDisplay.h" #endif
// For collecting other people's log, tell them `MOZ_LOG=KeyboardHandler:4,sync` // rather than `MOZ_LOG=KeyboardHandler:5,sync` since using `5` may create too // big file. // Therefore you shouldn't use `LogLevel::Verbose` for logging usual behavior.
mozilla::LazyLogModule gKeyLog("KeyboardHandler");
staticconstchar* GetStatusName(nsEventStatus aStatus) { switch (aStatus) { case nsEventStatus_eConsumeDoDefault: return"nsEventStatus_eConsumeDoDefault"; case nsEventStatus_eConsumeNoDefault: return"nsEventStatus_eConsumeNoDefault"; case nsEventStatus_eIgnore: return"nsEventStatus_eIgnore"; case nsEventStatus_eSentinel: return"nsEventStatus_eSentinel"; default: return"Illegal value";
}
}
staticconst nsCString GetKeyLocationName(uint32_t aLocation) { switch (aLocation) { case eKeyLocationLeft: return"KEY_LOCATION_LEFT"_ns; case eKeyLocationRight: return"KEY_LOCATION_RIGHT"_ns; case eKeyLocationStandard: return"KEY_LOCATION_STANDARD"_ns; case eKeyLocationNumpad: return"KEY_LOCATION_NUMPAD"_ns; default: return nsPrintfCString("Unknown (0x%04X)", aLocation);
}
}
staticconst nsCString GetCharacterCodeName(char16_t aChar) { switch (aChar) { case 0x0000: return"NULL (0x0000)"_ns; case 0x0008: return"BACKSPACE (0x0008)"_ns; case 0x0009: return"CHARACTER TABULATION (0x0009)"_ns; case 0x000A: return"LINE FEED (0x000A)"_ns; case 0x000B: return"LINE TABULATION (0x000B)"_ns; case 0x000C: return"FORM FEED (0x000C)"_ns; case 0x000D: return"CARRIAGE RETURN (0x000D)"_ns; case 0x0018: return"CANCEL (0x0018)"_ns; case 0x001B: return"ESCAPE (0x001B)"_ns; case 0x0020: return"SPACE (0x0020)"_ns; case 0x007F: return"DELETE (0x007F)"_ns; case 0x00A0: return"NO-BREAK SPACE (0x00A0)"_ns; case 0x00AD: return"SOFT HYPHEN (0x00AD)"_ns; case 0x2000: return"EN QUAD (0x2000)"_ns; case 0x2001: return"EM QUAD (0x2001)"_ns; case 0x2002: return"EN SPACE (0x2002)"_ns; case 0x2003: return"EM SPACE (0x2003)"_ns; case 0x2004: return"THREE-PER-EM SPACE (0x2004)"_ns; case 0x2005: return"FOUR-PER-EM SPACE (0x2005)"_ns; case 0x2006: return"SIX-PER-EM SPACE (0x2006)"_ns; case 0x2007: return"FIGURE SPACE (0x2007)"_ns; case 0x2008: return"PUNCTUATION SPACE (0x2008)"_ns; case 0x2009: return"THIN SPACE (0x2009)"_ns; case 0x200A: return"HAIR SPACE (0x200A)"_ns; case 0x200B: return"ZERO WIDTH SPACE (0x200B)"_ns; case 0x200C: return"ZERO WIDTH NON-JOINER (0x200C)"_ns; case 0x200D: return"ZERO WIDTH JOINER (0x200D)"_ns; case 0x200E: return"LEFT-TO-RIGHT MARK (0x200E)"_ns; case 0x200F: return"RIGHT-TO-LEFT MARK (0x200F)"_ns; case 0x2029: return"PARAGRAPH SEPARATOR (0x2029)"_ns; case 0x202A: return"LEFT-TO-RIGHT EMBEDDING (0x202A)"_ns; case 0x202B: return"RIGHT-TO-LEFT EMBEDDING (0x202B)"_ns; case 0x202D: return"LEFT-TO-RIGHT OVERRIDE (0x202D)"_ns; case 0x202E: return"RIGHT-TO-LEFT OVERRIDE (0x202E)"_ns; case 0x202F: return"NARROW NO-BREAK SPACE (0x202F)"_ns; case 0x205F: return"MEDIUM MATHEMATICAL SPACE (0x205F)"_ns; case 0x2060: return"WORD JOINER (0x2060)"_ns; case 0x2066: return"LEFT-TO-RIGHT ISOLATE (0x2066)"_ns; case 0x2067: return"RIGHT-TO-LEFT ISOLATE (0x2067)"_ns; case 0x3000: return"IDEOGRAPHIC SPACE (0x3000)"_ns; case 0xFEFF: return"ZERO WIDTH NO-BREAK SPACE (0xFEFF)"_ns; default: { if (aChar < ' ' || (aChar >= 0x80 && aChar < 0xA0)) { return nsPrintfCString("control (0x%04X)", aChar);
} if (NS_IS_HIGH_SURROGATE(aChar)) { return nsPrintfCString("high surrogate (0x%04X)", aChar);
} if (NS_IS_LOW_SURROGATE(aChar)) { return nsPrintfCString("low surrogate (0x%04X)", aChar);
} return nsPrintfCString("'%s' (0x%04X)",
NS_ConvertUTF16toUTF8(nsAutoString(aChar)).get(),
aChar);
}
}
}
/* static */ constchar* KeymapWrapper::GetModifierName(MappedModifier aModifier) { switch (aModifier) { case CAPS_LOCK: return"CapsLock"; case NUM_LOCK: return"NumLock"; case SCROLL_LOCK: return"ScrollLock"; case SHIFT: return"Shift"; case CTRL: return"Ctrl"; case ALT: return"Alt"; case SUPER: return"Super"; case HYPER: return"Hyper"; case META: return"Meta"; case LEVEL3: return"Level3"; case LEVEL5: return"Level5"; case NOT_MODIFIER: return"NotModifier"; default: return"InvalidValue";
}
}
/* static */
KeymapWrapper::MappedModifier KeymapWrapper::GetModifierForGDKKeyval(
guint aGdkKeyval) { switch (aGdkKeyval) { case GDK_Caps_Lock: return CAPS_LOCK; case GDK_Num_Lock: return NUM_LOCK; case GDK_Scroll_Lock: return SCROLL_LOCK; case GDK_Shift_Lock: case GDK_Shift_L: case GDK_Shift_R: return SHIFT; case GDK_Control_L: case GDK_Control_R: return CTRL; case GDK_Alt_L: case GDK_Alt_R: return ALT; case GDK_Super_L: case GDK_Super_R: return SUPER; case GDK_Hyper_L: case GDK_Hyper_R: return HYPER; case GDK_Meta_L: case GDK_Meta_R: return META; case GDK_ISO_Level3_Shift: case GDK_Mode_switch: return LEVEL3; case GDK_ISO_Level5_Shift: return LEVEL5; default: return NOT_MODIFIER;
}
}
guint KeymapWrapper::GetGdkModifierMask(MappedModifier aModifier) const { switch (aModifier) { case CAPS_LOCK: return GDK_LOCK_MASK; case NUM_LOCK: return mModifierMasks[INDEX_NUM_LOCK]; case SCROLL_LOCK: return mModifierMasks[INDEX_SCROLL_LOCK]; case SHIFT: return GDK_SHIFT_MASK; case CTRL: return GDK_CONTROL_MASK; case ALT: return mModifierMasks[INDEX_ALT]; case SUPER: return GDK_SUPER_MASK; case HYPER: return mModifierMasks[INDEX_HYPER]; case META: return mModifierMasks[INDEX_META]; case LEVEL3: return mModifierMasks[INDEX_LEVEL3]; case LEVEL5: return mModifierMasks[INDEX_LEVEL5]; default: return 0;
}
}
KeymapWrapper::ModifierKey* KeymapWrapper::GetModifierKey(
guint aHardwareKeycode) { for (uint32_t i = 0; i < mModifierKeys.Length(); i++) {
ModifierKey& key = mModifierKeys[i]; if (key.mHardwareKeycode == aHardwareKeycode) { return &key;
}
} return nullptr;
}
/* static */
KeymapWrapper* KeymapWrapper::GetInstance() { if (!sInstance) {
sInstance = new KeymapWrapper();
sInstance->Init();
} return sInstance;
}
int xkbMajorVer = XkbMajorVersion; int xkbMinorVer = XkbMinorVersion; if (!XkbLibraryVersion(&xkbMajorVer, &xkbMinorVer)) {
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p InitXKBExtension failed due to failure of " "XkbLibraryVersion()", this)); return;
}
// XkbLibraryVersion() set xkbMajorVer and xkbMinorVer to that of the // library, which may be newer than what is required of the server in // XkbQueryExtension(), so these variables should be reset to // XkbMajorVersion and XkbMinorVersion before the XkbQueryExtension call.
xkbMajorVer = XkbMajorVersion;
xkbMinorVer = XkbMinorVersion; int opcode, baseErrorCode; if (!XkbQueryExtension(display, &opcode, &mXKBBaseEventCode, &baseErrorCode,
&xkbMajorVer, &xkbMinorVer)) {
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p InitXKBExtension failed due to failure of " "XkbQueryExtension(), display=0x%p", this, display)); return;
}
if (!XkbSelectEventDetails(display, XkbUseCoreKbd, XkbStateNotify,
XkbModifierStateMask, XkbModifierStateMask)) {
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p InitXKBExtension failed due to failure of " "XkbSelectEventDetails() for XModifierStateMask, display=0x%p", this, display)); return;
}
if (!XkbSelectEventDetails(display, XkbUseCoreKbd, XkbControlsNotify,
XkbPerKeyRepeatMask, XkbPerKeyRepeatMask)) {
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p InitXKBExtension failed due to failure of " "XkbSelectEventDetails() for XkbControlsNotify, display=0x%p", this, display)); return;
}
if (!XGetKeyboardControl(display, &mKeyboardState)) {
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p InitXKBExtension failed due to failure of " "XGetKeyboardControl(), display=0x%p", this, display)); return;
}
// The modifiermap member of the XModifierKeymap structure contains 8 sets // of max_keypermod KeyCodes, one for each modifier in the order Shift, // Lock, Control, Mod1, Mod2, Mod3, Mod4, and Mod5. // Only nonzero KeyCodes have meaning in each set, and zero KeyCodes are // ignored.
// Note that two or more modifiers may use one modifier flag. E.g., // on Ubuntu 10.10, Alt and Meta share the Mod1 in default settings. // And also Super and Hyper share the Mod4. In such cases, we need to // decide which modifier flag means one of DOM modifiers.
// mod[0] is Modifier introduced by Mod1.
MappedModifier mod[5];
int32_t foundLevel[5]; for (uint32_t i = 0; i < std::size(mod); i++) {
mod[i] = NOT_MODIFIER;
foundLevel[i] = INT32_MAX;
} const uint32_t map_size = 8 * xmodmap->max_keypermod; for (uint32_t i = 0; i < map_size; i++) {
KeyCode keycode = xmodmap->modifiermap[i];
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p InitBySystemSettings, " " i=%d, keycode=0x%08X", this, i, keycode)); if (!keycode || keycode < min_keycode || keycode > max_keycode) { continue;
}
switch (modifier) { case NOT_MODIFIER: // Don't overwrite the stored information with // NOT_MODIFIER. break; case CAPS_LOCK: case SHIFT: case CTRL: case SUPER: // Ignore the modifiers defined in GDK spec. They shouldn't // be mapped to Mod1-5 because they must not work on native // GTK applications. break; default: // If new modifier is found in higher level than stored // value, we don't need to overwrite it. if (j > foundLevel[modIndex]) { break;
} // If new modifier is more important than stored value, // we should overwrite it with new modifier. if (j == foundLevel[modIndex]) {
mod[modIndex] = std::min(modifier, mod[modIndex]); break;
}
foundLevel[modIndex] = j;
mod[modIndex] = modifier; break;
}
}
}
for (uint32_t i = 0; i < COUNT_OF_MODIFIER_INDEX; i++) {
MappedModifier modifier; switch (i) { case INDEX_NUM_LOCK:
modifier = NUM_LOCK; break; case INDEX_SCROLL_LOCK:
modifier = SCROLL_LOCK; break; case INDEX_ALT:
modifier = ALT; break; case INDEX_META:
modifier = META; break; case INDEX_HYPER:
modifier = HYPER; break; case INDEX_LEVEL3:
modifier = LEVEL3; break; case INDEX_LEVEL5:
modifier = LEVEL5; break; default:
MOZ_CRASH("All indexes must be handled here");
} for (uint32_t j = 0; j < std::size(mod); j++) { if (modifier == mod[j]) {
mModifierMasks[i] |= 1 << (j + 3);
}
}
}
/* This keymap routine is derived from weston-2.0.0/clients/simple-im.c
*/ void KeymapWrapper::HandleKeymap(uint32_t format, int fd, uint32_t size) {
MOZ_LOG(gKeyLog, LogLevel::Info,
("KeymapWrapper::HandleKeymap() format %d fd %d size %d", format, fd,
size));
KeymapWrapper::ResetKeyboard();
if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
MOZ_LOG(gKeyLog, LogLevel::Info,
("KeymapWrapper::HandleKeymap(): format is not " "WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1!"));
close(fd); return;
}
#ifdef MOZ_X11 /* static */
GdkFilterReturn KeymapWrapper::FilterEvents(GdkXEvent* aXEvent,
GdkEvent* aGdkEvent,
gpointer aData) {
XEvent* xEvent = static_cast<XEvent*>(aXEvent); switch (xEvent->type) { case KeyPress: { // If the key doesn't support auto repeat, ignore the event because // even if such key (e.g., Shift) is pressed during auto repeat of // anoter key, it doesn't stop the auto repeat.
KeymapWrapper* self = static_cast<KeymapWrapper*>(aData); if (!self->IsAutoRepeatableKey(xEvent->xkey.keycode)) { break;
} if (sRepeatState == NOT_PRESSED) {
sRepeatState = FIRST_PRESS;
MOZ_LOG(gKeyLog, LogLevel::Info,
("FilterEvents(aXEvent={ type=KeyPress, " "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, " "aGdkEvent={ state=0x%08X }), " "detected first keypress",
xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time, reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
} elseif (sLastRepeatableHardwareKeyCode == xEvent->xkey.keycode) { if (sLastRepeatableKeyTime == xEvent->xkey.time &&
sLastRepeatableHardwareKeyCode ==
IMContextWrapper::
GetWaitingSynthesizedKeyPressHardwareKeyCode()) { // On some environment, IM may generate duplicated KeyPress event // without any special state flags. In such case, we shouldn't // treat the event as "repeated".
MOZ_LOG(gKeyLog, LogLevel::Info,
("FilterEvents(aXEvent={ type=KeyPress, " "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, " "aGdkEvent={ state=0x%08X }), " "igored keypress since it must be synthesized by IME",
xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time, reinterpret_cast<GdkEventKey*>(aGdkEvent)->state)); break;
}
sRepeatState = REPEATING;
MOZ_LOG(gKeyLog, LogLevel::Info,
("FilterEvents(aXEvent={ type=KeyPress, " "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, " "aGdkEvent={ state=0x%08X }), " "detected repeating keypress",
xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time, reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
} else { // If a different key is pressed while another key is pressed, // auto repeat system repeats only the last pressed key. // So, setting new keycode and setting repeat state as first key // press should work fine.
sRepeatState = FIRST_PRESS;
MOZ_LOG(gKeyLog, LogLevel::Info,
("FilterEvents(aXEvent={ type=KeyPress, " "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, " "aGdkEvent={ state=0x%08X }), " "detected different keypress",
xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time, reinterpret_cast<GdkEventKey*>(aGdkEvent)->state));
}
sLastRepeatableHardwareKeyCode = xEvent->xkey.keycode;
sLastRepeatableKeyTime = xEvent->xkey.time; break;
} case KeyRelease: { if (sLastRepeatableHardwareKeyCode != xEvent->xkey.keycode) { // This case means the key release event is caused by // a non-repeatable key such as Shift or a repeatable key that // was pressed before sLastRepeatableHardwareKeyCode was // pressed. break;
}
sRepeatState = NOT_PRESSED;
MOZ_LOG(gKeyLog, LogLevel::Info,
("FilterEvents(aXEvent={ type=KeyRelease, " "xkey={ keycode=0x%08X, state=0x%08X, time=%lu } }, " "aGdkEvent={ state=0x%08X }), " "detected key release",
xEvent->xkey.keycode, xEvent->xkey.state, xEvent->xkey.time, reinterpret_cast<GdkEventKey*>(aGdkEvent)->state)); break;
} case FocusOut: { // At moving focus, we should reset keyboard repeat state. // Strictly, this causes incorrect behavior. However, this // correctness must be enough for web applications.
sRepeatState = NOT_PRESSED; break;
} default: {
KeymapWrapper* self = static_cast<KeymapWrapper*>(aData); if (xEvent->type != self->mXKBBaseEventCode) { break;
}
XkbEvent* xkbEvent = (XkbEvent*)xEvent; if (xkbEvent->any.xkb_type != XkbControlsNotify ||
!(xkbEvent->ctrls.changed_ctrls & XkbPerKeyRepeatMask)) { break;
} if (!XGetKeyboardControl(xkbEvent->any.display, &self->mKeyboardState)) {
MOZ_LOG(gKeyLog, LogLevel::Info,
("%p FilterEvents failed due to failure " "of XGetKeyboardControl(), display=0x%p",
self, xkbEvent->any.display));
} break;
}
}
return GDK_FILTER_CONTINUE;
} #endif
staticvoid ResetBidiKeyboard() { // Reset the bidi keyboard settings for the new GdkKeymap
nsCOMPtr<nsIBidiKeyboard> bidiKeyboard = nsContentUtils::GetBidiKeyboard(); if (bidiKeyboard) {
bidiKeyboard->Reset();
}
WidgetUtils::SendBidiKeyboardInfoToContent();
}
MOZ_ASSERT(sInstance == aKeymapWrapper, "This instance must be the singleton instance");
// We cannot reintialize here becasue we don't have GdkWindow which is using // the GdkKeymap. We'll reinitialize it when next GetInstance() is called.
ResetKeyboard();
}
// static void KeymapWrapper::OnDirectionChanged(GdkKeymap* aGdkKeymap,
KeymapWrapper* aKeymapWrapper) { // XXX // A lot of diretion-changed signal might be fired on switching bidi // keyboard when using both ibus (with arabic layout) and fcitx (with IME). // See https://github.com/fcitx/fcitx/issues/257 // // Also, when using ibus, switching to IM might not cause this signal. // See https://github.com/ibus/ibus/issues/1848
KeymapWrapper* keymapWrapper = GetInstance(); for (uint32_t i = 0; i < sizeof(MappedModifier) * 8 && aModifiers; i++) {
MappedModifier modifier = static_cast<MappedModifier>(1 << i); // Is the binary position used by modifier? if (!(aModifiers & modifier)) { continue;
} // Is the modifier active? if (!(aGdkModifierState & keymapWrapper->GetGdkModifierMask(modifier))) { returnfalse;
}
aModifiers &= ~modifier;
} returntrue;
}
// DOM Meta key should be TRUE only on Mac. We need to discuss this // issue later.
KeymapWrapper* keymapWrapper = GetInstance(); if (keymapWrapper->AreModifiersActive(SHIFT, aGdkModifierState)) {
keyModifiers |= MODIFIER_SHIFT;
} if (keymapWrapper->AreModifiersActive(CTRL, aGdkModifierState)) {
keyModifiers |= MODIFIER_CONTROL;
} if (keymapWrapper->AreModifiersActive(ALT, aGdkModifierState)) {
keyModifiers |= MODIFIER_ALT;
} if (keymapWrapper->AreModifiersActive(SUPER, aGdkModifierState) ||
keymapWrapper->AreModifiersActive(HYPER, aGdkModifierState) || // "Meta" state is typically mapped to `Alt` + `Shift`, but we ignore the // state if `Alt` is mapped to "Alt" state. Additionally it's mapped to // `Win` in Sun/Solaris keyboard layout. In this case, we want to treat // them as DOM Meta modifier keys like "Super" state in the major Linux // environments.
keymapWrapper->AreModifiersActive(META, aGdkModifierState)) {
keyModifiers |= MODIFIER_META;
} if (keymapWrapper->AreModifiersActive(LEVEL3, aGdkModifierState) ||
keymapWrapper->AreModifiersActive(LEVEL5, aGdkModifierState)) {
keyModifiers |= MODIFIER_ALTGRAPH;
} if (keymapWrapper->AreModifiersActive(CAPS_LOCK, aGdkModifierState)) {
keyModifiers |= MODIFIER_CAPSLOCK;
} if (keymapWrapper->AreModifiersActive(NUM_LOCK, aGdkModifierState)) {
keyModifiers |= MODIFIER_NUMLOCK;
} if (keymapWrapper->AreModifiersActive(SCROLL_LOCK, aGdkModifierState)) {
keyModifiers |= MODIFIER_SCROLLLOCK;
} return keyModifiers;
}
/* static */
guint KeymapWrapper::ConvertWidgetModifierToGdkState(
nsIWidget::Modifiers aNativeModifiers) { if (!aNativeModifiers) { return 0;
} struct ModifierMapEntry {
nsIWidget::Modifiers mWidgetModifier;
MappedModifier mModifier;
}; // TODO: Currently, we don't treat L/R of each modifier on Linux. // TODO: No proper native modifier for Level5. static constexpr ModifierMapEntry sModifierMap[] = {
{nsIWidget::CAPS_LOCK, MappedModifier::CAPS_LOCK},
{nsIWidget::NUM_LOCK, MappedModifier::NUM_LOCK},
{nsIWidget::SHIFT_L, MappedModifier::SHIFT},
{nsIWidget::SHIFT_R, MappedModifier::SHIFT},
{nsIWidget::CTRL_L, MappedModifier::CTRL},
{nsIWidget::CTRL_R, MappedModifier::CTRL},
{nsIWidget::ALT_L, MappedModifier::ALT},
{nsIWidget::ALT_R, MappedModifier::ALT},
{nsIWidget::ALTGRAPH, MappedModifier::LEVEL3},
{nsIWidget::COMMAND_L, MappedModifier::SUPER},
{nsIWidget::COMMAND_R, MappedModifier::SUPER}};
guint state = 0;
KeymapWrapper* instance = GetInstance(); for (const ModifierMapEntry& entry : sModifierMap) { if (aNativeModifiers & entry.mWidgetModifier) {
state |= instance->GetGdkModifierMask(entry.mModifier);
}
} return state;
}
// Don't log this method for non-important events because e.g., eMouseMove is // just noisy and there is no reason to log it. bool doLog = aInputEvent.mMessage != eMouseMove; if (doLog) {
MOZ_LOG(gKeyLog, LogLevel::Debug,
("%p InitInputEvent, aGdkModifierState=0x%08X, " "aInputEvent={ mMessage=%s, mModifiers=0x%04X (Shift: %s, " "Control: %s, Alt: %s, Meta: %s, AltGr: %s, " "CapsLock: %s, NumLock: %s, ScrollLock: %s })",
keymapWrapper, aGdkModifierState, ToChar(aInputEvent.mMessage),
aInputEvent.mModifiers,
GetBoolName(aInputEvent.mModifiers & MODIFIER_SHIFT),
GetBoolName(aInputEvent.mModifiers & MODIFIER_CONTROL),
GetBoolName(aInputEvent.mModifiers & MODIFIER_ALT),
GetBoolName(aInputEvent.mModifiers & MODIFIER_META),
GetBoolName(aInputEvent.mModifiers & MODIFIER_ALTGRAPH),
GetBoolName(aInputEvent.mModifiers & MODIFIER_CAPSLOCK),
GetBoolName(aInputEvent.mModifiers & MODIFIER_NUMLOCK),
GetBoolName(aInputEvent.mModifiers & MODIFIER_SCROLLLOCK)));
}
switch (aInputEvent.mClass) { case eMouseEventClass: case ePointerEventClass: case eMouseScrollEventClass: case eWheelEventClass: case eDragEventClass: case eSimpleGestureEventClass: break; default: return;
}
/* static */
uint32_t KeymapWrapper::ComputeDOMKeyCode(const GdkEventKey* aGdkKeyEvent) { // If the keyval indicates it's a modifier key, we should use unshifted // key's modifier keyval.
guint keyval = aGdkKeyEvent->keyval; if (GetModifierForGDKKeyval(keyval)) { // But if the keyval without modifiers isn't a modifier key, we // shouldn't use it. E.g., Japanese keyboard layout's // Shift + Eisu-Toggle key is CapsLock. This is an actual rare case, // Windows uses different keycode for a physical key for different // shift key state.
guint keyvalWithoutModifier = GetGDKKeyvalWithoutModifier(aGdkKeyEvent); if (GetModifierForGDKKeyval(keyvalWithoutModifier)) {
keyval = keyvalWithoutModifier;
} // Note that the modifier keycode and activating or deactivating // modifier flag may be mismatched, but it's okay. If a DOM key // event handler is testing a keydown event, it's more likely being // used to test which key is being pressed than to test which // modifier will become active. So, if we computed DOM keycode // from modifier flag which were changing by the physical key, then // there would be no other way for the user to generate the original // keycode.
uint32_t DOMKeyCode = GetDOMKeyCodeFromKeyPairs(keyval);
NS_ASSERTION(DOMKeyCode, "All modifier keys must have a DOM keycode"); return DOMKeyCode;
}
// If the key isn't printable, let's look at the key pairs.
uint32_t charCode = GetCharCodeFor(aGdkKeyEvent); if (!charCode) { // Note that any key may be a function key because of some unusual keyboard // layouts. I.e., even if the pressed key is a printable key of en-US // keyboard layout, we should expose the function key's keyCode value to // web apps because web apps should handle the keydown/keyup events as // inputted by usual keyboard layout. For example, Hatchak keyboard // maps Tab key to "Digit3" key and Level3 Shift makes it "Backspace". // In this case, we should expose DOM_VK_BACK_SPACE (8).
uint32_t DOMKeyCode = GetDOMKeyCodeFromKeyPairs(keyval); if (DOMKeyCode) { // XXX If DOMKeyCode is a function key's keyCode value, it might be // better to consume necessary modifiers. For example, if there is // no Control Pad section on keyboard like notebook, Delete key is // available only with Level3 Shift+"Backspace" key if using Hatchak. // If web apps accept Delete key operation only when no modifiers are // active, such users cannot use Delete key to do it. However, // Chromium doesn't consume such necessary modifiers. So, our default // behavior should keep not touching modifiers for compatibility, but // it might be better to add a pref to consume necessary modifiers. return DOMKeyCode;
} // If aGdkKeyEvent cannot be mapped to a DOM keyCode value, we should // refer keyCode value without modifiers because web apps should be // able to identify the key as far as possible.
guint keyvalWithoutModifier = GetGDKKeyvalWithoutModifier(aGdkKeyEvent); return GetDOMKeyCodeFromKeyPairs(keyvalWithoutModifier);
}
// printable numpad keys should be resolved here. switch (keyval) { case GDK_KP_Multiply: return NS_VK_MULTIPLY; case GDK_KP_Add: return NS_VK_ADD; case GDK_KP_Separator: return NS_VK_SEPARATOR; case GDK_KP_Subtract: return NS_VK_SUBTRACT; case GDK_KP_Decimal: return NS_VK_DECIMAL; case GDK_KP_Divide: return NS_VK_DIVIDE; case GDK_KP_0: return NS_VK_NUMPAD0; case GDK_KP_1: return NS_VK_NUMPAD1; case GDK_KP_2: return NS_VK_NUMPAD2; case GDK_KP_3: return NS_VK_NUMPAD3; case GDK_KP_4: return NS_VK_NUMPAD4; case GDK_KP_5: return NS_VK_NUMPAD5; case GDK_KP_6: return NS_VK_NUMPAD6; case GDK_KP_7: return NS_VK_NUMPAD7; case GDK_KP_8: return NS_VK_NUMPAD8; case GDK_KP_9: return NS_VK_NUMPAD9;
}
KeymapWrapper* keymapWrapper = GetInstance();
// Ignore all modifier state except NumLock.
guint baseState =
(aGdkKeyEvent->state & keymapWrapper->GetGdkModifierMask(NUM_LOCK));
// Basically, we should use unmodified character for deciding our keyCode.
uint32_t unmodifiedChar = keymapWrapper->GetCharCodeFor(
aGdkKeyEvent, baseState, aGdkKeyEvent->group); if (IsBasicLatinLetterOrNumeral(unmodifiedChar)) { // If the unmodified character is an ASCII alphabet or an ASCII // numeric, it's the best hint for deciding our keyCode. return WidgetUtils::ComputeKeyCodeFromChar(unmodifiedChar);
}
// If the unmodified character is not an ASCII character, that means we // couldn't find the hint. We should reset it. if (!IsPrintableASCIICharacter(unmodifiedChar)) {
unmodifiedChar = 0;
}
// Retry with shifted keycode.
guint shiftState = (baseState | keymapWrapper->GetGdkModifierMask(SHIFT));
uint32_t shiftedChar = keymapWrapper->GetCharCodeFor(aGdkKeyEvent, shiftState,
aGdkKeyEvent->group); if (IsBasicLatinLetterOrNumeral(shiftedChar)) { // A shifted character can be an ASCII alphabet on Hebrew keyboard // layout. And also shifted character can be an ASCII numeric on // AZERTY keyboad layout. Then, it's a good hint for deciding our // keyCode. return WidgetUtils::ComputeKeyCodeFromChar(shiftedChar);
}
// If the shifted unmodified character isn't an ASCII character, we should // discard it too. if (!IsPrintableASCIICharacter(shiftedChar)) {
shiftedChar = 0;
}
// If current keyboard layout isn't ASCII alphabet inputtable layout, // look for ASCII alphabet inputtable keyboard layout. If the key // inputs an ASCII alphabet or an ASCII numeric, we should use it // for deciding our keyCode.
uint32_t unmodCharLatin = 0;
uint32_t shiftedCharLatin = 0; if (!keymapWrapper->IsLatinGroup(aGdkKeyEvent->group)) {
gint minGroup = keymapWrapper->GetFirstLatinGroup(); if (minGroup >= 0) {
unmodCharLatin =
keymapWrapper->GetCharCodeFor(aGdkKeyEvent, baseState, minGroup); if (IsBasicLatinLetterOrNumeral(unmodCharLatin)) { // If the unmodified character is an ASCII alphabet or // an ASCII numeric, we should use it for the keyCode. return WidgetUtils::ComputeKeyCodeFromChar(unmodCharLatin);
} // If the unmodified character in the alternative ASCII capable // keyboard layout isn't an ASCII character, that means we couldn't // find the hint. We should reset it. if (!IsPrintableASCIICharacter(unmodCharLatin)) {
unmodCharLatin = 0;
}
shiftedCharLatin =
keymapWrapper->GetCharCodeFor(aGdkKeyEvent, shiftState, minGroup); if (IsBasicLatinLetterOrNumeral(shiftedCharLatin)) { // If the shifted character is an ASCII alphabet or an ASCII // numeric, we should use it for the keyCode. return WidgetUtils::ComputeKeyCodeFromChar(shiftedCharLatin);
} // If the shifted unmodified character in the alternative ASCII // capable keyboard layout isn't an ASCII character, we should // discard it too. if (!IsPrintableASCIICharacter(shiftedCharLatin)) {
shiftedCharLatin = 0;
}
}
}
// If the key itself or with Shift state on active keyboard layout produces // an ASCII punctuation character, we should decide keyCode value with it. if (unmodifiedChar || shiftedChar) { return WidgetUtils::ComputeKeyCodeFromChar(unmodifiedChar ? unmodifiedChar
: shiftedChar);
}
// If the key itself or with Shift state on alternative ASCII capable // keyboard layout produces an ASCII punctuation character, we should // decide keyCode value with it. Note that We've returned 0 for long // time if keyCode isn't for an alphabet keys or a numeric key even in // alternative ASCII capable keyboard layout because we decided that we // should avoid setting same keyCode value to 2 or more keys since active // keyboard layout may have a key to input the punctuation with different // key. However, setting keyCode to 0 makes some web applications which // are aware of neither KeyboardEvent.key nor KeyboardEvent.code not work // with Firefox when user selects non-ASCII capable keyboard layout such // as Russian and Thai. So, if alternative ASCII capable keyboard layout // has keyCode value for the key, we should use it. In other words, this // behavior means that non-ASCII capable keyboard layout overrides some // keys' keyCode value only if the key produces ASCII character by itself // or with Shift key. if (unmodCharLatin || shiftedCharLatin) { return WidgetUtils::ComputeKeyCodeFromChar(
unmodCharLatin ? unmodCharLatin : shiftedCharLatin);
}
// Otherwise, let's decide keyCode value from the hardware_keycode // value on major keyboard layout.
CodeNameIndex code = ComputeDOMCodeNameIndex(aGdkKeyEvent); return WidgetKeyboardEvent::GetFallbackKeyCodeOfPunctuationKey(code);
}
// Shift+F10 and ContextMenu should cause eContextMenu event. if (keyNameIndex != KEY_NAME_INDEX_F10 &&
keyNameIndex != KEY_NAME_INDEX_ContextMenu) { returnfalse;
}
if (contextMenuEvent.IsControl() || contextMenuEvent.IsMeta() ||
contextMenuEvent.IsAlt()) { returnfalse;
}
// If the key is ContextMenu, then an eContextMenu mouse event is // dispatched regardless of the state of the Shift modifier. When it is // pressed without the Shift modifier, a web page can prevent the default // context menu action. When pressed with the Shift modifier, the web page // cannot prevent the default context menu action. // (PresShell::HandleEventInternal() sets mOnlyChromeDispatch to true.)
// If the key is F10, it needs Shift state because Shift+F10 is well-known // shortcut key on Linux. However, eContextMenu with Shift state is // special. It won't fire "contextmenu" event in the web content for // blocking web page to prevent its default. Therefore, this combination // should work same as ContextMenu key. // XXX Should we allow to block web page to prevent its default with // Ctrl+Shift+F10 or Alt+Shift+F10 instead? if (keyNameIndex == KEY_NAME_INDEX_F10) { if (!contextMenuEvent.IsShift()) { returnfalse;
}
contextMenuEvent.mModifiers &= ~MODIFIER_SHIFT;
}
// if we are in the middle of composing text, XIM gets to see it // before mozilla does. // FYI: Don't dispatch keydown event before notifying IME of the event // because IME may send a key event synchronously and consume the // original event. bool IMEWasEnabled = false;
KeyHandlingState handlingState = KeyHandlingState::eNotHandled;
RefPtr<IMContextWrapper> imContext = aWindow->GetIMContext(); if (imContext) {
IMEWasEnabled = imContext->IsEnabled();
handlingState = imContext->OnKeyEvent(aWindow, aGdkKeyEvent); if (handlingState == KeyHandlingState::eHandled) {
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), the event was handled by " "IMContextWrapper")); return;
}
}
// work around for annoying things. if (aGdkKeyEvent->keyval == GDK_Tab &&
AreModifiersActive(CTRL | ALT, aGdkKeyEvent->state)) {
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), didn't dispatch keyboard events " "because it's Ctrl + Alt + Tab")); return;
}
// Dispatch keydown event always. At auto repeating, we should send // KEYDOWN -> KEYPRESS -> KEYDOWN -> KEYPRESS ... -> KEYUP // However, old distributions (e.g., Ubuntu 9.10) sent native key // release event, so, on such platform, the DOM events will be: // KEYDOWN -> KEYPRESS -> KEYUP -> KEYDOWN -> KEYPRESS -> KEYUP...
bool isKeyDownCancelled = false; if (handlingState == KeyHandlingState::eNotHandled) { if (DispatchKeyDownOrKeyUpEvent(aWindow, aGdkKeyEvent, false,
&isKeyDownCancelled) &&
(MOZ_UNLIKELY(aWindow->IsDestroyed()) || isKeyDownCancelled)) {
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched eKeyDown event and " "stopped handling the event because %s",
aWindow->IsDestroyed() ? "the window has been destroyed"
: "the event was consumed")); return;
}
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched eKeyDown event and " "it wasn't consumed"));
handlingState = KeyHandlingState::eNotHandledButEventDispatched;
}
// If a keydown event handler causes to enable IME, i.e., it moves // focus from IME unusable content to IME usable editor, we should // send the native key event to IME for the first input on the editor.
imContext = aWindow->GetIMContext(); if (!IMEWasEnabled && imContext && imContext->IsEnabled()) { // Notice our keydown event was already dispatched. This prevents // unnecessary DOM keydown event in the editor.
handlingState = imContext->OnKeyEvent(aWindow, aGdkKeyEvent, true); if (handlingState == KeyHandlingState::eHandled) {
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), the event was handled by " "IMContextWrapper which was enabled by the preceding eKeyDown " "event")); return;
}
}
// Look for specialized app-command keys switch (aGdkKeyEvent->keyval) { case GDK_Back:
aWindow->DispatchCommandEvent(nsGkAtoms::Back);
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched \"Back\" command event")); return; case GDK_Forward:
aWindow->DispatchCommandEvent(nsGkAtoms::Forward);
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched \"Forward\" command " "event")); return; case GDK_Reload: case GDK_Refresh:
aWindow->DispatchCommandEvent(nsGkAtoms::Reload); return; case GDK_Stop:
aWindow->DispatchCommandEvent(nsGkAtoms::Stop);
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched \"Stop\" command event")); return; case GDK_Search:
aWindow->DispatchCommandEvent(nsGkAtoms::Search);
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched \"Search\" command event")); return; case GDK_Favorites:
aWindow->DispatchCommandEvent(nsGkAtoms::Bookmarks);
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched \"Bookmarks\" command " "event")); return; case GDK_HomePage:
aWindow->DispatchCommandEvent(nsGkAtoms::Home); return; case GDK_Copy: case GDK_F16: // F16, F20, F18, F14 are old keysyms for Copy Cut Paste Undo
aWindow->DispatchContentCommandEvent(eContentCommandCopy);
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched \"Copy\" content command " "event")); return; case GDK_Cut: case GDK_F20:
aWindow->DispatchContentCommandEvent(eContentCommandCut);
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched \"Cut\" content command " "event")); return; case GDK_Paste: case GDK_F18:
aWindow->DispatchContentCommandEvent(eContentCommandPaste);
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched \"Paste\" content command " "event")); return; case GDK_Redo:
aWindow->DispatchContentCommandEvent(eContentCommandRedo); return; case GDK_Undo: case GDK_F14:
aWindow->DispatchContentCommandEvent(eContentCommandUndo);
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched \"Undo\" content command " "event")); return; default: break;
}
// before we dispatch a key, check if it's the context menu key. // If so, send a context menu key event instead. if (MaybeDispatchContextMenuEvent(aWindow, aGdkKeyEvent)) {
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), stopped dispatching eKeyPress event " "because eContextMenu event was dispatched")); return;
}
RefPtr<TextEventDispatcher> textEventDispatcher =
aWindow->GetTextEventDispatcher();
nsresult rv = textEventDispatcher->BeginNativeInputTransaction(); if (NS_WARN_IF(NS_FAILED(rv))) {
MOZ_LOG(gKeyLog, LogLevel::Error,
(" HandleKeyPressEvent(), stopped dispatching eKeyPress event " "because of failed to initialize TextEventDispatcher")); return;
}
// If the character code is in the BMP, send the key press event. // Otherwise, send a compositionchange event with the equivalent UTF-16 // string. // TODO: Investigate other browser's behavior in this case because // this hack is odd for UI Events.
WidgetKeyboardEvent keypressEvent(true, eKeyPress, aWindow);
KeymapWrapper::InitKeyEvent(keypressEvent, aGdkKeyEvent, false);
nsEventStatus status = nsEventStatus_eIgnore; if (keypressEvent.mKeyNameIndex != KEY_NAME_INDEX_USE_STRING ||
keypressEvent.mKeyValue.Length() == 1) { if (textEventDispatcher->MaybeDispatchKeypressEvents(keypressEvent, status,
aGdkKeyEvent)) {
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched eKeyPress event " "(status=%s)",
GetStatusName(status)));
} else {
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), didn't dispatch eKeyPress event " "(status=%s)",
GetStatusName(status)));
}
} else {
WidgetEventTime eventTime = aWindow->GetWidgetEventTime(aGdkKeyEvent->time);
textEventDispatcher->CommitComposition(status, &keypressEvent.mKeyValue,
&eventTime);
MOZ_LOG(gKeyLog, LogLevel::Info,
(" HandleKeyPressEvent(), dispatched a set of composition " "events"));
}
}
guint KeymapWrapper::GetModifierState(GdkEventKey* aGdkKeyEvent,
KeymapWrapper* aWrapper) {
guint state = aGdkKeyEvent->state; if (!aGdkKeyEvent->is_modifier) { return state;
} #ifdef MOZ_X11 // NOTE: The state of given key event indicates adjacent state of // modifier keys. E.g., even if the event is Shift key press event, // the bit for Shift is still false. By the same token, even if the // event is Shift key release event, the bit for Shift is still true. // Unfortunately, gdk_keyboard_get_modifiers() returns current modifier // state. It means if there're some pending modifier key press or // key release events, the result isn't what we want.
GdkDisplay* gdkDisplay = gdk_display_get_default(); if (GdkIsX11Display(gdkDisplay)) {
GdkDisplay* gdkDisplay = gdk_display_get_default();
Display* display = gdk_x11_display_get_xdisplay(gdkDisplay); if (XEventsQueued(display, QueuedAfterReading)) {
XEvent nextEvent;
XPeekEvent(display, &nextEvent); if (nextEvent.type == aWrapper->mXKBBaseEventCode) {
XkbEvent* XKBEvent = (XkbEvent*)&nextEvent; if (XKBEvent->any.xkb_type == XkbStateNotify) {
XkbStateNotifyEvent* stateNotifyEvent =
(XkbStateNotifyEvent*)XKBEvent;
state &= ~0xFF;
state |= stateNotifyEvent->lookup_mods;
}
}
} return state;
} #endif #ifdef MOZ_WAYLAND int mask = 0; switch (aGdkKeyEvent->keyval) { case GDK_Shift_L: case GDK_Shift_R:
mask = aWrapper->GetGdkModifierMask(SHIFT); break; case GDK_Control_L: case GDK_Control_R:
mask = aWrapper->GetGdkModifierMask(CTRL); break; case GDK_Alt_L: case GDK_Alt_R:
mask = aWrapper->GetGdkModifierMask(ALT); break; case GDK_Super_L: case GDK_Super_R:
mask = aWrapper->GetGdkModifierMask(SUPER); break; case GDK_Hyper_L: case GDK_Hyper_R:
mask = aWrapper->GetGdkModifierMask(HYPER); break; case GDK_Meta_L: case GDK_Meta_R:
mask = aWrapper->GetGdkModifierMask(META); break; default: break;
} if (aGdkKeyEvent->type == GDK_KEY_PRESS) {
state |= mask;
} else {
state &= ~mask;
} #endif return state;
}
/* static */ void KeymapWrapper::InitKeyEvent(WidgetKeyboardEvent& aKeyEvent,
GdkEventKey* aGdkKeyEvent, bool aIsProcessedByIME) {
MOZ_ASSERT(
!aIsProcessedByIME || aKeyEvent.mMessage != eKeyPress, "If the key event is handled by IME, keypress event shouldn't be fired");
KeymapWrapper* keymapWrapper = GetInstance();
aKeyEvent.mCodeNameIndex = ComputeDOMCodeNameIndex(aGdkKeyEvent);
MOZ_ASSERT(aKeyEvent.mCodeNameIndex != CODE_NAME_INDEX_USE_STRING);
aKeyEvent.mKeyNameIndex =
aIsProcessedByIME ? KEY_NAME_INDEX_Process
: keymapWrapper->ComputeDOMKeyNameIndex(aGdkKeyEvent); if (aKeyEvent.mKeyNameIndex == KEY_NAME_INDEX_Unidentified) {
uint32_t charCode = GetCharCodeFor(aGdkKeyEvent); if (!charCode) {
charCode = keymapWrapper->GetUnmodifiedCharCodeFor(aGdkKeyEvent);
} if (charCode) {
aKeyEvent.mKeyNameIndex = KEY_NAME_INDEX_USE_STRING;
MOZ_ASSERT(aKeyEvent.mKeyValue.IsEmpty(), "Uninitialized mKeyValue must be empty");
AppendUCS4ToUTF16(charCode, aKeyEvent.mKeyValue);
}
}
switch (aGdkKeyEvent->keyval) { case GDK_Shift_L: case GDK_Control_L: case GDK_Alt_L: case GDK_Super_L: case GDK_Hyper_L: case GDK_Meta_L:
aKeyEvent.mLocation = eKeyLocationLeft; break;
case GDK_Shift_R: case GDK_Control_R: case GDK_Alt_R: case GDK_Super_R: case GDK_Hyper_R: case GDK_Meta_R:
aKeyEvent.mLocation = eKeyLocationRight; break;
case GDK_KP_0: case GDK_KP_1: case GDK_KP_2: case GDK_KP_3: case GDK_KP_4: case GDK_KP_5: case GDK_KP_6: case GDK_KP_7: case GDK_KP_8: case GDK_KP_9: case GDK_KP_Space: case GDK_KP_Tab: case GDK_KP_Enter: case GDK_KP_F1: case GDK_KP_F2: case GDK_KP_F3:
--> --------------------
--> maximum size reached
--> --------------------
¤ Dauer der Verarbeitung: 0.36 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.