1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "host/frontend/webrtc/lib/keyboard.h"
18
19 #include <linux/input.h>
20
21 #include <map>
22
23 static const std::map<std::string, uint16_t> kDomToLinuxMapping = {
24 {"Backquote", KEY_GRAVE},
25 {"Backslash", KEY_BACKSLASH},
26 {"Backspace", KEY_BACKSPACE},
27 {"BracketLeft", KEY_LEFTBRACE},
28 {"BracketRight", KEY_RIGHTBRACE},
29 {"Comma", KEY_COMMA},
30 {"Digit0", KEY_0},
31 {"Digit1", KEY_1},
32 {"Digit2", KEY_2},
33 {"Digit3", KEY_3},
34 {"Digit4", KEY_4},
35 {"Digit5", KEY_5},
36 {"Digit6", KEY_6},
37 {"Digit7", KEY_7},
38 {"Digit8", KEY_8},
39 {"Digit9", KEY_9},
40 {"Equal", KEY_EQUAL},
41 {"IntlBackslash", KEY_BACKSLASH},
42 {"IntlRo", KEY_RO},
43 {"IntlYen", KEY_BACKSLASH},
44 {"KeyA", KEY_A},
45 {"KeyB", KEY_B},
46 {"KeyC", KEY_C},
47 {"KeyD", KEY_D},
48 {"KeyE", KEY_E},
49 {"KeyF", KEY_F},
50 {"KeyG", KEY_G},
51 {"KeyH", KEY_H},
52 {"KeyI", KEY_I},
53 {"KeyJ", KEY_J},
54 {"KeyK", KEY_K},
55 {"KeyL", KEY_L},
56 {"KeyM", KEY_M},
57 {"KeyN", KEY_N},
58 {"KeyO", KEY_O},
59 {"KeyP", KEY_P},
60 {"KeyQ", KEY_Q},
61 {"KeyR", KEY_R},
62 {"KeyS", KEY_S},
63 {"KeyT", KEY_T},
64 {"KeyU", KEY_U},
65 {"KeyV", KEY_V},
66 {"KeyW", KEY_W},
67 {"KeyX", KEY_X},
68 {"KeyY", KEY_Y},
69 {"KeyZ", KEY_Z},
70 {"Minus", KEY_MINUS},
71 {"Period", KEY_DOT},
72 {"Quote", KEY_APOSTROPHE},
73 {"Semicolon", KEY_SEMICOLON},
74 {"Slash", KEY_SLASH},
75 {"AltLeft", KEY_LEFTALT},
76 {"AltRight", KEY_RIGHTALT},
77 {"CapsLock", KEY_CAPSLOCK},
78 {"ContextMenu", KEY_CONTEXT_MENU},
79 {"ControlLeft", KEY_LEFTCTRL},
80 {"ControlRight", KEY_RIGHTCTRL},
81 {"Enter", KEY_ENTER},
82 {"MetaLeft", KEY_LEFTMETA},
83 {"MetaRight", KEY_RIGHTMETA},
84 {"ShiftLeft", KEY_LEFTSHIFT},
85 {"ShiftRight", KEY_RIGHTSHIFT},
86 {"Space", KEY_SPACE},
87 {"Tab", KEY_TAB},
88 {"Delete", KEY_DELETE},
89 {"End", KEY_END},
90 {"Help", KEY_HELP},
91 {"Home", KEY_HOME},
92 {"Insert", KEY_INSERT},
93 {"PageDown", KEY_PAGEDOWN},
94 {"PageUp", KEY_PAGEUP},
95 {"ArrowDown", KEY_DOWN},
96 {"ArrowLeft", KEY_LEFT},
97 {"ArrowRight", KEY_RIGHT},
98 {"ArrowUp", KEY_UP},
99
100 {"NumLock", KEY_NUMLOCK},
101 {"Numpad0", KEY_KP0},
102 {"Numpad1", KEY_KP1},
103 {"Numpad2", KEY_KP2},
104 {"Numpad3", KEY_KP3},
105 {"Numpad4", KEY_KP4},
106 {"Numpad5", KEY_KP5},
107 {"Numpad6", KEY_KP6},
108 {"Numpad7", KEY_KP7},
109 {"Numpad8", KEY_KP8},
110 {"Numpad9", KEY_KP9},
111 {"NumpadAdd", KEY_KPPLUS},
112 {"NumpadBackspace", KEY_BACKSPACE},
113 {"NumpadClear", KEY_CLEAR},
114 {"NumpadComma", KEY_KPCOMMA},
115 {"NumpadDecimal", KEY_KPDOT},
116 {"NumpadDivide", KEY_KPSLASH},
117 {"NumpadEnter", KEY_KPENTER},
118 {"NumpadEqual", KEY_KPEQUAL},
119 /*
120 {"NumpadClearEntry", },
121 {"NumpadHash", },
122 {"NumpadMemoryAdd", },
123 {"NumpadMemoryClear", },
124 {"NumpadMemoryRecall", },
125 {"NumpadMemoryStore", },
126 {"NumpadMemorySubtract", },
127 */
128 {"NumpadMultiply", KEY_KPASTERISK},
129 {"NumpadParenLeft", KEY_KPLEFTPAREN},
130 {"NumpadParenRight", KEY_KPRIGHTPAREN},
131 {"NumpadStar", KEY_KPASTERISK},
132 {"NumpadSubtract", KEY_KPMINUS},
133
134 {"Escape", KEY_ESC},
135 {"F1", KEY_F1},
136 {"F2", KEY_F2},
137 {"F3", KEY_F3},
138 {"F4", KEY_F4},
139 {"F5", KEY_F5},
140 {"F6", KEY_F6},
141 {"F7", KEY_F7},
142 {"F8", KEY_F8},
143 {"F9", KEY_F9},
144 {"F10", KEY_F10},
145 {"F11", KEY_F11},
146 {"F12", KEY_F12},
147 {"Fn", KEY_FN},
148 /*{"FnLock", },*/
149 {"PrintScreen", KEY_SYSRQ},
150 {"ScrollLock", KEY_SCROLLLOCK},
151 {"Pause", KEY_PAUSE}};
152
DomKeyCodeToLinux(const std::string & dom_KEY_code)153 uint16_t DomKeyCodeToLinux(const std::string& dom_KEY_code) {
154 const auto it = kDomToLinuxMapping.find(dom_KEY_code);
155 if (it == kDomToLinuxMapping.end()) {
156 return 0;
157 }
158 return it->second;
159 }
160