OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/base/ime/chromeos/ime_keymap.h" | 5 #include "ui/base/ime/chromeos/ime_keymap.h" |
6 | 6 |
7 #define XK_MISCELLANY | 7 #include <map> |
8 #include <X11/keysymdef.h> | 8 |
9 #include <X11/XF86keysym.h> | 9 #include "base/lazy_instance.h" |
10 | 10 |
11 namespace ui { | 11 namespace ui { |
12 | 12 |
13 std::string FromXKeycodeToKeyValue(int keyval) { | 13 namespace { |
14 // TODO: Ensure all keys are supported. | 14 |
15 switch (keyval) { | 15 const struct KeyCodeTable { |
16 case XK_Escape: | 16 KeyboardCode keyboard_code; |
17 return "Esc"; | 17 const char* dom_code; |
18 case XK_F1: | 18 } kKeyCodeTable[] = { |
19 case XF86XK_Back: | 19 {VKEY_BACK, "Backspace"}, |
20 return "HistoryBack"; | 20 {VKEY_TAB, "Tab"}, |
21 case XK_F2: | 21 {VKEY_RETURN, "Enter"}, |
22 case XF86XK_Forward: | 22 {VKEY_MENU, "ContextMenu"}, |
23 return "HistoryForward"; | 23 {VKEY_PAUSE, "Pause"}, |
24 case XK_F3: | 24 {VKEY_CAPITAL, "CapsLock"}, |
25 case XF86XK_Reload: | 25 {VKEY_KANA, "KanaMode"}, |
26 return "BrowserRefresh"; | 26 {VKEY_HANGUL, "HangulMode"}, |
27 case XK_F4: | 27 {VKEY_HANJA, "Hanja"}, |
28 case XF86XK_LaunchB: | 28 {VKEY_KANJI, "Kanji"}, |
29 return "ChromeOSFullscreen"; // TODO: Check this value | 29 {VKEY_ESCAPE, "Escape"}, |
30 case XK_F5: | 30 {VKEY_CONVERT, "Convert"}, |
31 case XF86XK_LaunchA: | 31 {VKEY_NONCONVERT, "NoConvert"}, |
32 return "ChromeOSSwitchWindow"; // TODO: Check this value | 32 {VKEY_SPACE, "Space"}, |
33 case XK_F6: | 33 {VKEY_END, "End"}, |
34 case XF86XK_MonBrightnessDown: | 34 {VKEY_HOME, "Home"}, |
35 return "BrightnessDown"; | 35 {VKEY_LEFT, "ArrowLeft"}, |
36 case XK_F7: | 36 {VKEY_UP, "ArrowUp"}, |
37 case XF86XK_MonBrightnessUp: | 37 {VKEY_RIGHT, "ArrowRight"}, |
38 return "BrightnessUp"; | 38 {VKEY_DOWN, "ArrowDown"}, |
39 case XK_F8: | 39 {VKEY_PRINT, "PrintScreen"}, |
40 case XF86XK_AudioMute: | 40 {VKEY_INSERT, "Insert"}, |
41 return "AudioVolumeMute"; | 41 {VKEY_DELETE, "Delete"}, |
42 case XK_F9: | 42 {VKEY_HELP, "Help"}, |
43 case XF86XK_AudioLowerVolume: | 43 {VKEY_0, "Digit0"}, |
44 return "AudioVolumeDown"; | 44 {VKEY_1, "Digit1"}, |
45 case XK_F10: | 45 {VKEY_2, "Digit2"}, |
46 case XF86XK_AudioRaiseVolume: | 46 {VKEY_3, "Digit3"}, |
47 return "AudioVolumeUp"; | 47 {VKEY_4, "Digit4"}, |
48 case XK_BackSpace: | 48 {VKEY_5, "Digit5"}, |
49 return "Backspace"; | 49 {VKEY_6, "Digit6"}, |
50 case XK_Delete: | 50 {VKEY_7, "Digit7"}, |
51 case XK_KP_Delete: | 51 {VKEY_8, "Digit8"}, |
52 return "Delete"; | 52 {VKEY_9, "Digit9"}, |
53 case XK_Tab: | 53 {VKEY_A, "KeyA"}, |
54 return "Tab"; | 54 {VKEY_B, "KeyB"}, |
55 case XK_KP_Enter: | 55 {VKEY_C, "KeyC"}, |
56 case XK_Return: | 56 {VKEY_D, "KeyD"}, |
57 return "Enter"; | 57 {VKEY_E, "KeyE"}, |
58 case XK_Meta_L: | 58 {VKEY_F, "KeyF"}, |
59 return "BrowserSearch"; | 59 {VKEY_G, "KeyG"}, |
60 case XK_Up: | 60 {VKEY_H, "KeyH"}, |
61 case XK_KP_Up: | 61 {VKEY_I, "KeyI"}, |
62 return "Up"; | 62 {VKEY_J, "KeyJ"}, |
63 case XK_Down: | 63 {VKEY_K, "KeyK"}, |
64 case XK_KP_Down: | 64 {VKEY_L, "KeyL"}, |
65 return "Down"; | 65 {VKEY_M, "KeyM"}, |
66 case XK_Left: | 66 {VKEY_N, "KeyN"}, |
67 case XK_KP_Left: | 67 {VKEY_O, "KeyO"}, |
68 return "Left"; | 68 {VKEY_P, "KeyP"}, |
69 case XK_Right: | 69 {VKEY_Q, "KeyQ"}, |
70 case XK_KP_Right: | 70 {VKEY_R, "KeyR"}, |
71 return "Right"; | 71 {VKEY_S, "KeyS"}, |
72 case XK_Page_Up: | 72 {VKEY_T, "KeyT"}, |
73 return "PageUp"; | 73 {VKEY_U, "KeyU"}, |
74 case XK_Page_Down: | 74 {VKEY_V, "KeyV"}, |
75 return "PageDown"; | 75 {VKEY_W, "KeyW"}, |
76 case XK_Home: | 76 {VKEY_X, "KeyX"}, |
77 return "Home"; | 77 {VKEY_Y, "KeyY"}, |
78 case XK_End: | 78 {VKEY_Z, "KeyZ"}, |
79 return "End"; | 79 {VKEY_LWIN, "OSLeft"}, |
80 case XK_Shift_L: | 80 {VKEY_RWIN, "OSRight"}, |
81 case XK_Shift_R: | 81 {VKEY_NUMPAD0, "Numpad0"}, |
82 return "Shift"; | 82 {VKEY_NUMPAD1, "Numpad1"}, |
83 case XK_Alt_L: | 83 {VKEY_NUMPAD2, "Numpad2"}, |
84 case XK_Alt_R: | 84 {VKEY_NUMPAD3, "Numpad3"}, |
85 return "Alt"; | 85 {VKEY_NUMPAD4, "Numpad4"}, |
86 case XK_Control_L: | 86 {VKEY_NUMPAD5, "Numpad5"}, |
87 case XK_Control_R: | 87 {VKEY_NUMPAD6, "Numpad6"}, |
88 return "Ctrl"; | 88 {VKEY_NUMPAD7, "Numpad7"}, |
89 case XK_Caps_Lock: | 89 {VKEY_NUMPAD8, "Numpad8"}, |
90 return "CapsLock"; | 90 {VKEY_NUMPAD9, "Numpad9"}, |
91 default: { | 91 {VKEY_MULTIPLY, "NumpadMultiply"}, |
92 // TODO: Properly support unicode characters. | 92 {VKEY_ADD, "NumpadAdd"}, |
93 char value[2]; | 93 {VKEY_SUBTRACT, "NumpadSubtract"}, |
94 value[0] = keyval; | 94 {VKEY_DECIMAL, "NumpadDecimal"}, |
95 value[1] = '\0'; | 95 {VKEY_DIVIDE, "NumpadDivide"}, |
96 return value; | 96 {VKEY_F1, "F1"}, |
97 } | 97 {VKEY_F2, "F2"}, |
| 98 {VKEY_F3, "F3"}, |
| 99 {VKEY_F4, "F4"}, |
| 100 {VKEY_F5, "F5"}, |
| 101 {VKEY_F6, "F6"}, |
| 102 {VKEY_F7, "F7"}, |
| 103 {VKEY_F8, "F8"}, |
| 104 {VKEY_F9, "F9"}, |
| 105 {VKEY_F10, "F10"}, |
| 106 {VKEY_F11, "F11"}, |
| 107 {VKEY_F12, "F12"}, |
| 108 {VKEY_F13, "F13"}, |
| 109 {VKEY_F14, "F14"}, |
| 110 {VKEY_F15, "F15"}, |
| 111 {VKEY_F16, "F16"}, |
| 112 {VKEY_F17, "F17"}, |
| 113 {VKEY_F18, "F18"}, |
| 114 {VKEY_F19, "F19"}, |
| 115 {VKEY_F20, "F20"}, |
| 116 {VKEY_F21, "F21"}, |
| 117 {VKEY_F22, "F22"}, |
| 118 {VKEY_F23, "F23"}, |
| 119 {VKEY_F24, "F24"}, |
| 120 {VKEY_NUMLOCK, "NumLock"}, |
| 121 {VKEY_SCROLL, "ScrollLock"}, |
| 122 {VKEY_LSHIFT, "ShiftLeft"}, |
| 123 {VKEY_RSHIFT, "ShiftRight"}, |
| 124 {VKEY_LCONTROL, "CtrlLeft"}, |
| 125 {VKEY_RCONTROL, "CtrlRight"}, |
| 126 {VKEY_LMENU, "AltLeft"}, |
| 127 {VKEY_RMENU, "AltRight"}, |
| 128 {VKEY_BROWSER_BACK, "BrowserBack"}, |
| 129 {VKEY_BROWSER_FORWARD, "BrowserForward"}, |
| 130 {VKEY_BROWSER_REFRESH, "BrowserRefresh"}, |
| 131 {VKEY_BROWSER_STOP, "BrowserStop"}, |
| 132 {VKEY_BROWSER_SEARCH, "BrowserSearch"}, |
| 133 {VKEY_BROWSER_HOME, "BrowserHome"}, |
| 134 {VKEY_VOLUME_MUTE, "VolumeMute"}, |
| 135 {VKEY_VOLUME_DOWN, "VolumeDown"}, |
| 136 {VKEY_VOLUME_UP, "VolumeUp"}, |
| 137 {VKEY_MEDIA_NEXT_TRACK, "MediaTrackNext"}, |
| 138 {VKEY_MEDIA_PREV_TRACK, "MediaTrackPrevious"}, |
| 139 {VKEY_MEDIA_STOP, "MediaStop"}, |
| 140 {VKEY_MEDIA_PLAY_PAUSE, "MediaPlayPause"}, |
| 141 {VKEY_MEDIA_LAUNCH_MAIL, "LaunchMail"}, |
| 142 {VKEY_OEM_1, "Semicolon"}, |
| 143 {VKEY_OEM_PLUS, "Equal"}, |
| 144 {VKEY_OEM_COMMA, "Comma"}, |
| 145 {VKEY_OEM_MINUS, "Minus"}, |
| 146 {VKEY_OEM_PERIOD, "Period"}, |
| 147 {VKEY_OEM_2, "Slash"}, |
| 148 {VKEY_OEM_3, "Backquote"}, |
| 149 {VKEY_OEM_4, "BracketLeft"}, |
| 150 {VKEY_OEM_5, "Backslash"}, |
| 151 {VKEY_OEM_6, "BracketRight"}, |
| 152 {VKEY_OEM_7, "Quote"}}; |
| 153 |
| 154 class KeyCodeMap { |
| 155 public: |
| 156 KeyCodeMap() { |
| 157 for (size_t i = 0; i < arraysize(kKeyCodeTable); ++i) |
| 158 map_[kKeyCodeTable[i].dom_code] = kKeyCodeTable[i].keyboard_code; |
98 } | 159 } |
| 160 |
| 161 KeyboardCode GetKeyboardCode(const std::string& dom_code) const { |
| 162 std::map<std::string, KeyboardCode>::const_iterator it = |
| 163 map_.find(dom_code); |
| 164 return (it == map_.end()) ? VKEY_UNKNOWN : it->second; |
| 165 } |
| 166 |
| 167 private: |
| 168 std::map<std::string, KeyboardCode> map_; |
| 169 }; |
| 170 |
| 171 base::LazyInstance<KeyCodeMap>::Leaky g_keycode_map = |
| 172 LAZY_INSTANCE_INITIALIZER; |
| 173 |
| 174 } // namespace |
| 175 |
| 176 KeyboardCode DomKeycodeToKeyboardCode(const std::string& code) { |
| 177 return g_keycode_map.Get().GetKeyboardCode(code); |
99 } | 178 } |
100 | 179 |
101 } // namespace ui | 180 } // namespace ui |
OLD | NEW |