Debian Codegraph @

platform/web/dom_keys.inc

raw
1 /**************************************************************************/
2 /* dom_keys.inc */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "core/os/keyboard.h"
32
33 // See https://w3c.github.io/uievents-code/#code-value-tables
34 Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], bool p_physical) {
35 #define DOM2GODOT(p_str, p_godot_code) \
36 if (memcmp((const void *)p_str, (void *)(p_physical ? p_code : p_key), strlen(p_str) + 1) == 0) { \
37 return Key::p_godot_code; \
38 }
39
40 // Numpad section.
41 DOM2GODOT("NumLock", NUMLOCK);
42 DOM2GODOT("Numpad0", KP_0);
43 DOM2GODOT("Numpad1", KP_1);
44 DOM2GODOT("Numpad2", KP_2);
45 DOM2GODOT("Numpad3", KP_3);
46 DOM2GODOT("Numpad4", KP_4);
47 DOM2GODOT("Numpad5", KP_5);
48 DOM2GODOT("Numpad6", KP_6);
49 DOM2GODOT("Numpad7", KP_7);
50 DOM2GODOT("Numpad8", KP_8);
51 DOM2GODOT("Numpad9", KP_9);
52 DOM2GODOT("NumpadAdd", KP_ADD);
53 DOM2GODOT("NumpadBackspace", BACKSPACE);
54 DOM2GODOT("Clear", CLEAR); // NumLock on macOS.
55 DOM2GODOT("NumpadClear", CLEAR);
56 DOM2GODOT("NumpadClearEntry", CLEAR);
57 //DOM2GODOT("NumpadComma", UNKNOWN);
58 DOM2GODOT("NumpadDecimal", KP_PERIOD);
59 DOM2GODOT("NumpadDivide", KP_DIVIDE);
60 DOM2GODOT("NumpadEnter", KP_ENTER);
61 DOM2GODOT("NumpadEqual", EQUAL);
62 //DOM2GODOT("NumpadHash", UNKNOWN);
63 //DOM2GODOT("NumpadMemoryAdd", UNKNOWN);
64 //DOM2GODOT("NumpadMemoryClear", UNKNOWN);
65 //DOM2GODOT("NumpadMemoryRecall", UNKNOWN);
66 //DOM2GODOT("NumpadMemoryStore", UNKNOWN);
67 //DOM2GODOT("NumpadMemorySubtract", UNKNOWN);
68 DOM2GODOT("NumpadMultiply", KP_MULTIPLY);
69 DOM2GODOT("NumpadParenLeft", PARENLEFT);
70 DOM2GODOT("NumpadParenRight", PARENRIGHT);
71 DOM2GODOT("NumpadStar", KP_MULTIPLY); // or ASTERISK ?
72 DOM2GODOT("NumpadSubtract", KP_SUBTRACT);
73
74 // Alphanumeric section.
75 DOM2GODOT("Backquote", QUOTELEFT);
76 DOM2GODOT("Backslash", BACKSLASH);
77 DOM2GODOT("BracketLeft", BRACKETLEFT);
78 DOM2GODOT("BracketRight", BRACKETRIGHT);
79 DOM2GODOT("Comma", COMMA);
80 DOM2GODOT("Digit0", KEY_0);
81 DOM2GODOT("Digit1", KEY_1);
82 DOM2GODOT("Digit2", KEY_2);
83 DOM2GODOT("Digit3", KEY_3);
84 DOM2GODOT("Digit4", KEY_4);
85 DOM2GODOT("Digit5", KEY_5);
86 DOM2GODOT("Digit6", KEY_6);
87 DOM2GODOT("Digit7", KEY_7);
88 DOM2GODOT("Digit8", KEY_8);
89 DOM2GODOT("Digit9", KEY_9);
90 DOM2GODOT("Equal", EQUAL);
91 DOM2GODOT("IntlBackslash", BACKSLASH);
92 //DOM2GODOT("IntlRo", UNKNOWN);
93 DOM2GODOT("IntlYen", YEN);
94
95 DOM2GODOT("KeyA", A);
96 DOM2GODOT("KeyB", B);
97 DOM2GODOT("KeyC", C);
98 DOM2GODOT("KeyD", D);
99 DOM2GODOT("KeyE", E);
100 DOM2GODOT("KeyF", F);
101 DOM2GODOT("KeyG", G);
102 DOM2GODOT("KeyH", H);
103 DOM2GODOT("KeyI", I);
104 DOM2GODOT("KeyJ", J);
105 DOM2GODOT("KeyK", K);
106 DOM2GODOT("KeyL", L);
107 DOM2GODOT("KeyM", M);
108 DOM2GODOT("KeyN", N);
109 DOM2GODOT("KeyO", O);
110 DOM2GODOT("KeyP", P);
111 DOM2GODOT("KeyQ", Q);
112 DOM2GODOT("KeyR", R);
113 DOM2GODOT("KeyS", S);
114 DOM2GODOT("KeyT", T);
115 DOM2GODOT("KeyU", U);
116 DOM2GODOT("KeyV", V);
117 DOM2GODOT("KeyW", W);
118 DOM2GODOT("KeyX", X);
119 DOM2GODOT("KeyY", Y);
120 DOM2GODOT("KeyZ", Z);
121
122 DOM2GODOT("Minus", MINUS);
123 DOM2GODOT("Period", PERIOD);
124 DOM2GODOT("Quote", APOSTROPHE);
125 DOM2GODOT("Semicolon", SEMICOLON);
126 DOM2GODOT("Slash", SLASH);
127
128 // Functional keys in the Alphanumeric section.
129 DOM2GODOT("Alt", ALT);
130 DOM2GODOT("AltLeft", ALT);
131 DOM2GODOT("AltRight", ALT);
132 DOM2GODOT("Backspace", BACKSPACE);
133 DOM2GODOT("CapsLock", CAPSLOCK);
134 DOM2GODOT("ContextMenu", MENU);
135 DOM2GODOT("Control", CTRL);
136 DOM2GODOT("ControlLeft", CTRL);
137 DOM2GODOT("ControlRight", CTRL);
138 DOM2GODOT("Enter", ENTER);
139 DOM2GODOT("Meta", META);
140 DOM2GODOT("MetaLeft", META);
141 DOM2GODOT("MetaRight", META);
142 DOM2GODOT("OSLeft", META); // Command on macOS.
143 DOM2GODOT("OSRight", META); // Command on macOS.
144 DOM2GODOT("Shift", SHIFT);
145 DOM2GODOT("ShiftLeft", SHIFT);
146 DOM2GODOT("ShiftRight", SHIFT);
147 DOM2GODOT("Space", SPACE);
148 DOM2GODOT("Tab", TAB);
149
150 // ControlPad section.
151 DOM2GODOT("Delete", KEY_DELETE);
152 DOM2GODOT("End", END);
153 DOM2GODOT("Help", HELP);
154 DOM2GODOT("Home", HOME);
155 DOM2GODOT("Insert", INSERT);
156 DOM2GODOT("PageDown", PAGEDOWN);
157 DOM2GODOT("PageUp", PAGEUP);
158
159 // ArrowPad section.
160 DOM2GODOT("ArrowDown", DOWN);
161 DOM2GODOT("ArrowLeft", LEFT);
162 DOM2GODOT("ArrowRight", RIGHT);
163 DOM2GODOT("ArrowUp", UP);
164
165 // Function section.
166 DOM2GODOT("Escape", ESCAPE);
167 DOM2GODOT("F1", F1);
168 DOM2GODOT("F2", F2);
169 DOM2GODOT("F3", F3);
170 DOM2GODOT("F4", F4);
171 DOM2GODOT("F5", F5);
172 DOM2GODOT("F6", F6);
173 DOM2GODOT("F7", F7);
174 DOM2GODOT("F8", F8);
175 DOM2GODOT("F9", F9);
176 DOM2GODOT("F10", F10);
177 DOM2GODOT("F11", F11);
178 DOM2GODOT("F12", F12);
179 //DOM2GODOT("Fn", UNKNOWN); // never actually fired, but included in the standard draft.
180 //DOM2GODOT("FnLock", UNKNOWN);
181 DOM2GODOT("PrintScreen", PRINT);
182 DOM2GODOT("ScrollLock", SCROLLLOCK);
183 DOM2GODOT("Pause", PAUSE);
184
185 // Media keys section.
186 DOM2GODOT("BrowserBack", BACK);
187 DOM2GODOT("BrowserFavorites", FAVORITES);
188 DOM2GODOT("BrowserForward", FORWARD);
189 DOM2GODOT("BrowserHome", OPENURL);
190 DOM2GODOT("BrowserRefresh", REFRESH);
191 DOM2GODOT("BrowserSearch", SEARCH);
192 DOM2GODOT("BrowserStop", STOP);
193 //DOM2GODOT("Eject", UNKNOWN);
194 DOM2GODOT("LaunchApp1", LAUNCH0);
195 DOM2GODOT("LaunchApp2", LAUNCH1);
196 DOM2GODOT("LaunchMail", LAUNCHMAIL);
197 DOM2GODOT("MediaPlayPause", MEDIAPLAY);
198 DOM2GODOT("MediaSelect", LAUNCHMEDIA);
199 DOM2GODOT("MediaStop", MEDIASTOP);
200 DOM2GODOT("MediaTrackNext", MEDIANEXT);
201 DOM2GODOT("MediaTrackPrevious", MEDIAPREVIOUS);
202 //DOM2GODOT("Power", UNKNOWN);
203 //DOM2GODOT("Sleep", UNKNOWN);
204 DOM2GODOT("AudioVolumeDown", VOLUMEDOWN);
205 DOM2GODOT("AudioVolumeMute", VOLUMEMUTE);
206 DOM2GODOT("AudioVolumeUp", VOLUMEUP);
207 //DOM2GODOT("WakeUp", UNKNOWN);
208
209 // Printable ASCII.
210 uint8_t b0 = (uint8_t)p_key[0];
211 uint8_t b1 = (uint8_t)p_key[1];
212 if (b0 >= 0x20 && b0 < 0x7F) { // ASCII.
213 if (b0 > 0x60 && b0 < 0x7B) { // Lowercase ASCII.
214 b0 -= 32;
215 }
216 return (Key)b0;
217 } else if (b0 == 0xC2 && b1 == 0xA5) {
218 return Key::YEN;
219 } else if (b0 == 0xC2 && b1 == 0xA7) {
220 return Key::SECTION;
221 }
222
223 return Key::NONE;
224 #undef DOM2GODOT
225 }
226
227 KeyLocation dom_code2godot_key_location(EM_UTF8 const p_code[32]) {
228 #define DOM2GODOT(m_str, m_godot_code) \
229 if (memcmp((const void *)m_str, (void *)p_code, strlen(m_str) + 1) == 0) { \
230 return KeyLocation::m_godot_code; \
231 }
232
233 DOM2GODOT("AltLeft", LEFT);
234 DOM2GODOT("AltRight", RIGHT);
235 DOM2GODOT("ControlLeft", LEFT);
236 DOM2GODOT("ControlRight", RIGHT);
237 DOM2GODOT("MetaLeft", LEFT);
238 DOM2GODOT("MetaRight", RIGHT);
239 DOM2GODOT("OSLeft", LEFT);
240 DOM2GODOT("OSRight", RIGHT);
241 DOM2GODOT("ShiftLeft", LEFT);
242 DOM2GODOT("ShiftRight", RIGHT);
243
244 return KeyLocation::UNSPECIFIED;
245 #undef DOM2GODOT
246 }