// Copyright 2012 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifdef UNSAFE_BUFFERS_BUILD // TODO(crbug.com/351564777): Remove this and convert code to safer constructs. #pragma allow_unsafe_buffers #endif #include "ui/events/keycodes/keyboard_code_conversion_x.h" #include <stddef.h> #include <algorithm> #include "base/logging.h" #include "base/strings/stringprintf.h" #include "base/strings/sys_string_conversions.h" #include "base/strings/utf_string_conversions.h" #include "ui/events/keycodes/dom/keycode_converter.h" #include "ui/events/keycodes/keyboard_code_conversion_xkb.h" #include "ui/events/keycodes/keyboard_codes_posix.h" #include "ui/events/keycodes/keysym_to_unicode.h" #include "ui/events/keycodes/xkb_keysym.h" #include "ui/gfx/x/keysyms/keysyms.h" #include "ui/gfx/x/xinput.h" #include "ui/gfx/x/xproto.h" #include "ui/gfx/x/xproto_types.h" #define VKEY_UNSUPPORTED … namespace ui { namespace { // MAP0 - MAP3: // These are the generated VKEY code maps from these layout datas on Windows: // KBDA1.DLL // KBDAL.DLL // KBDARME.DLL // KBDARMW.DLL // KBDAZEL.DLL // KBDBE.DLL // KBDBENE.DLL // KBDBR.DLL // KBDCA.DLL // KBDCAN.DLL // KBDCR.DLL // KBDCZ.DLL // KBDCZ1.DLL // KBDCZ2.DLL // KBDDA.DLL // KBDDV.DLL // KBDES.DLL // KBDEST.DLL // KBDFC.DLL // KBDFI.DLL // KBDFI1.DLL // KBDFO.DLL // KBDFR.DLL // KBDHEB.DLL // KBDGAE.DLL // KBDGEO.DLL // kbdgeoer.dll // kbdgeoqw.dll // KBDGR.DLL // KBDGR1.DLL // KBDGRLND.DLL // KBDHAU.DLL // KBDHU.DLL // KBDHU1.DLL // KBDIBO.DLL // KBDIC.DLL // KBDIR.DLL // KBDIT.DLL // KBDIT142.DLL // KBDLA.DLL // KBDLT.DLL // KBDLT1.DLL // KBDLT2.DLL // KBDLV.DLL // KBDLV1.DLL // KBDMLT47.DLL // KBDMLT48.DLL // KBDNE.DLL // KBDNO.DLL // KBDNO1.DLL // KBDPL.DLL // KBDPL1.DLL // KBDPO.DLL // KBDRO.DLL // KBDROPR.DLL // KBDROST.DLL // KBDRU.DLL // KBDSF.DLL // KBDSG.DLL // KBDSL.DLL // KBDSL1.DLL // KBDSOREX.DLL // KBDSORS1.DLL // KBDSORST.DLL // KBDSP.DLL // KBDSW.DLL // KBDSW09.DLL // KBDUK.DLL // KBDUKX.DLL // KBDUS.DLL // KBDUSL.DLL // KBDUSR.DLL // KBDUSX.DLL // KBDVNTC.DLL // KBDYBA.DLL // And the maps are only for special letter keys excluding [a-z] & [0-9]. // // ch0: the keysym without modifier states. // ch1: the keysym with shift state. // ch2: the keysym with altgr state. // sc: the hardware keycode (in Windows, it's called scan code). // vk: the VKEY code. // // MAP0: maps from ch0 to vk. // MAP1: maps from ch0+sc to vk. // MAP2: maps from ch0+ch1+sc to vk. // MAP3: maps from ch0+ch1+ch2+sc to vk. // MAP0 - MAP3 are all sorted, so that finding VK can be binary search. // // Reason for creating these maps is because a hard-coded mapping in // KeyboardCodeFromXKeysym() doesn't support non-US keyboard layouts. // e.g. in UK keyboard, the key between Quote and Enter keys has the VKEY code // VKEY_OEM_5 instead of VKEY_3. // // The key symbols which are not [a-zA-Z0-9] and functional/extend keys (e.g. // TAB, ENTER, BS, Arrow keys, modifier keys, F1-F12, media/app keys, etc.) // should go through these maps for correct VKEY codes. // // Please refer to crbug.com/386066. // const struct MAP0 { … } map0[] = …; const struct MAP1 { … } map1[] = …; const struct MAP2 { … } map2[] = …; const struct MAP3 { … } map3[] = …; template <class T_MAP> KeyboardCode FindVK(const T_MAP& key, const T_MAP* map, size_t size) { … } // Check for TTY function keys or space key which should always be mapped // based on KeySym, and never fall back to MAP0~MAP3, since some layouts // generate them by applying the Control/AltGr modifier to some other key. // e.g. in de(neo), AltGr+V generates XK_Enter. bool IsTtyFunctionOrSpaceKey(uint32_t keysym) { … } uint32_t TranslateKey(uint32_t keycode, uint32_t modifiers) { … } void GetKeycodeAndModifiers(const x11::Event& event, uint32_t* keycode, uint32_t* modifiers) { … } bool IsKeypadKey(uint32_t keysym) { … } bool IsPrivateKeypadKey(uint32_t keysym) { … } bool IsCursorKey(uint32_t keysym) { … } bool IsPFKey(uint32_t keysym) { … } bool IsFunctionKey(uint32_t keysym) { … } bool IsModifierKey(uint32_t keysym) { … } } // namespace // Get an ui::KeyboardCode from an X keyevent KeyboardCode KeyboardCodeFromXKeyEvent(const x11::Event& xev) { … } KeyboardCode KeyboardCodeFromXKeysym(unsigned int keysym) { … } DomCode CodeFromXEvent(const x11::Event& xev) { … } uint16_t GetCharacterFromXEvent(const x11::Event& xev) { … } DomKey GetDomKeyFromXEvent(const x11::Event& xev) { … } KeyboardCode DefaultKeyboardCodeFromHardwareKeycode( unsigned int hardware_code) { … } // TODO(jcampan): this method might be incomplete. int XKeysymForWindowsKeyCode(KeyboardCode keycode, bool shift) { … } unsigned int XKeyCodeForWindowsKeyCode(ui::KeyboardCode key_code, int flags, x11::Connection* connection) { … } } // namespace ui