Debian Codegraph @

common/arch/sdl/key.cpp

raw
1 /*
2 * This file is part of the DXX-Rebirth project <https://www.dxx-rebirth.com/>.
3 * It is copyright by its individual contributors, as recorded in the
4 * project's Git history. See COPYING.txt at the top level for license
5 * terms and a link to the Git history.
6 */
7 /*
8 *
9 * SDL keyboard input support
10 *
11 *
12 */
13
14 #include "dxxsconf.h"
15 #include <algorithm>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <ranges>
19
20 #include <SDL.h>
21 #include <SDL_version.h>
22 #if SDL_MAJOR_VERSION == 2
23 #include <SDL_keycode.h>
24 #endif
25
26 #include "event.h"
27 #include "dxxerror.h"
28 #include "key.h"
29 #include "timer.h"
30 #include "window.h"
31 #include "console.h"
32 #include "args.h"
33
34 #include "dsx-ns.h"
35 #include "compiler-range_for.h"
36 #include <array>
37
38 namespace dcx {
39
40 //-------- Variable accessed by outside functions ---------
41 static bool keyd_repeat; // 1 = use repeats, 0 no repeats
42 pressed_keys keyd_pressed;
43 fix64 keyd_time_when_last_pressed;
44 std::array<unsigned char, KEY_BUFFER_SIZE> unicode_frame_buffer;
45
46 constexpr std::array<key_props, 256> key_properties = {{
47 { "", 255, SDLK_UNKNOWN }, // 0
48 { "ESC", 255, SDLK_ESCAPE },
49 { "1", '1', SDLK_1 },
50 { "2", '2', SDLK_2 },
51 { "3", '3', SDLK_3 },
52 { "4", '4', SDLK_4 },
53 { "5", '5', SDLK_5 },
54 { "6", '6', SDLK_6 },
55 { "7", '7', SDLK_7 },
56 { "8", '8', SDLK_8 },
57 { "9", '9', SDLK_9 }, // 10
58 { "0", '0', SDLK_0 },
59 { "-", '-', SDLK_MINUS },
60 { "=", '=', SDLK_EQUALS },
61 { "BSPC", 255, SDLK_BACKSPACE },
62 { "TAB", 255, SDLK_TAB },
63 { "Q", 'q', SDLK_q },
64 { "W", 'w', SDLK_w },
65 { "E", 'e', SDLK_e },
66 { "R", 'r', SDLK_r },
67 { "T", 't', SDLK_t }, // 20
68 { "Y", 'y', SDLK_y },
69 { "U", 'u', SDLK_u },
70 { "I", 'i', SDLK_i },
71 { "O", 'o', SDLK_o },
72 { "P", 'p', SDLK_p },
73 { "[", '[', SDLK_LEFTBRACKET },
74 { "]", ']', SDLK_RIGHTBRACKET },
75 { "ENTER", 255, SDLK_RETURN },
76 { "LCTRL", 255, SDLK_LCTRL },
77 { "A", 'a', SDLK_a }, // 30
78 { "S", 's', SDLK_s },
79 { "D", 'd', SDLK_d },
80 { "F", 'f', SDLK_f },
81 { "G", 'g', SDLK_g },
82 { "H", 'h', SDLK_h },
83 { "J", 'j', SDLK_j },
84 { "K", 'k', SDLK_k },
85 { "L", 'l', SDLK_l },
86 { ";", ';', SDLK_SEMICOLON },
87 { "'", '\'', SDLK_QUOTE }, // 40
88 { "`", '`', SDLK_BACKQUOTE },
89 { "LSHFT", 255, SDLK_LSHIFT },
90 { "\\", '\\', SDLK_BACKSLASH },
91 { "Z", 'z', SDLK_z },
92 { "X", 'x', SDLK_x },
93 { "C", 'c', SDLK_c },
94 { "V", 'v', SDLK_v },
95 { "B", 'b', SDLK_b },
96 { "N", 'n', SDLK_n },
97 { "M", 'm', SDLK_m }, // 50
98 { ",", ',', SDLK_COMMA },
99 { ".", '.', SDLK_PERIOD },
100 { "/", '/', SDLK_SLASH },
101 { "RSHFT", 255, SDLK_RSHIFT },
102 { "PAD*", '*', SDLK_KP_MULTIPLY },
103 { "LALT", 255, SDLK_LALT },
104 { "SPC", ' ', SDLK_SPACE },
105 { "CPSLK", 255, SDLK_CAPSLOCK },
106 { "F1", 255, SDLK_F1 },
107 { "F2", 255, SDLK_F2 }, // 60
108 { "F3", 255, SDLK_F3 },
109 { "F4", 255, SDLK_F4 },
110 { "F5", 255, SDLK_F5 },
111 { "F6", 255, SDLK_F6 },
112 { "F7", 255, SDLK_F7 },
113 { "F8", 255, SDLK_F8 },
114 { "F9", 255, SDLK_F9 },
115 { "F10", 255, SDLK_F10 },
116 #if SDL_MAJOR_VERSION == 1
117 #define SDLK_NUMLOCKCLEAR SDLK_NUMLOCK
118 #define SDLK_SCROLLLOCK SDLK_SCROLLOCK
119 #define SDLK_KP_0 SDLK_KP0
120 #define SDLK_KP_1 SDLK_KP1
121 #define SDLK_KP_2 SDLK_KP2
122 #define SDLK_KP_3 SDLK_KP3
123 #define SDLK_KP_4 SDLK_KP4
124 #define SDLK_KP_5 SDLK_KP5
125 #define SDLK_KP_6 SDLK_KP6
126 #define SDLK_KP_7 SDLK_KP7
127 #define SDLK_KP_8 SDLK_KP8
128 #define SDLK_KP_9 SDLK_KP9
129 #endif
130 { "NMLCK", 255, SDLK_NUMLOCKCLEAR },
131 { "SCLK", 255, SDLK_SCROLLLOCK }, // 70
132 { "PAD7", 255, SDLK_KP_7 },
133 { "PAD8", 255, SDLK_KP_8 },
134 { "PAD9", 255, SDLK_KP_9 },
135 { "PAD-", 255, SDLK_KP_MINUS },
136 { "PAD4", 255, SDLK_KP_4 },
137 { "PAD5", 255, SDLK_KP_5 },
138 { "PAD6", 255, SDLK_KP_6 },
139 { "PAD+", 255, SDLK_KP_PLUS },
140 { "PAD1", 255, SDLK_KP_1 },
141 { "PAD2", 255, SDLK_KP_2 }, // 80
142 { "PAD3", 255, SDLK_KP_3 },
143 { "PAD0", 255, SDLK_KP_0 },
144 { "PAD.", 255, SDLK_KP_PERIOD },
145 { "", 255, SDLK_UNKNOWN },
146 { "", 255, SDLK_UNKNOWN },
147 { "", 255, SDLK_UNKNOWN },
148 { "F11", 255, SDLK_F11 },
149 { "F12", 255, SDLK_F12 },
150 { "", 255, SDLK_UNKNOWN },
151 { "", 255, SDLK_UNKNOWN }, // 90
152 { "", 255, SDLK_UNKNOWN },
153 { "", 255, SDLK_UNKNOWN },
154 { "", 255, SDLK_UNKNOWN },
155 { "", 255, SDLK_UNKNOWN },
156 { "", 255, SDLK_UNKNOWN },
157 { "", 255, SDLK_UNKNOWN },
158 { "PAUSE", 255, SDLK_PAUSE },
159 #if SDL_MAJOR_VERSION == 2
160 #define SDLK_WORLD_0 SDLK_UNKNOWN
161 #define SDLK_WORLD_1 SDLK_UNKNOWN
162 #define SDLK_WORLD_2 SDLK_UNKNOWN
163 #define SDLK_WORLD_3 SDLK_UNKNOWN
164 #define SDLK_WORLD_4 SDLK_UNKNOWN
165 #define SDLK_WORLD_5 SDLK_UNKNOWN
166 #define SDLK_WORLD_6 SDLK_UNKNOWN
167 #define SDLK_WORLD_7 SDLK_UNKNOWN
168 #define SDLK_WORLD_8 SDLK_UNKNOWN
169 #define SDLK_WORLD_9 SDLK_UNKNOWN
170 #define SDLK_WORLD_10 SDLK_UNKNOWN
171 #define SDLK_WORLD_11 SDLK_UNKNOWN
172 #define SDLK_WORLD_12 SDLK_UNKNOWN
173 #define SDLK_WORLD_13 SDLK_UNKNOWN
174 #define SDLK_WORLD_14 SDLK_UNKNOWN
175 #define SDLK_WORLD_15 SDLK_UNKNOWN
176 #define SDLK_WORLD_16 SDLK_UNKNOWN
177 #define SDLK_WORLD_17 SDLK_UNKNOWN
178 #define SDLK_WORLD_18 SDLK_UNKNOWN
179 #define SDLK_WORLD_19 SDLK_UNKNOWN
180 #define SDLK_WORLD_20 SDLK_UNKNOWN
181 #define SDLK_WORLD_21 SDLK_UNKNOWN
182 #define SDLK_WORLD_22 SDLK_UNKNOWN
183 #define SDLK_WORLD_23 SDLK_UNKNOWN
184 #define SDLK_WORLD_24 SDLK_UNKNOWN
185 #define SDLK_WORLD_25 SDLK_UNKNOWN
186 #define SDLK_WORLD_26 SDLK_UNKNOWN
187 #define SDLK_WORLD_27 SDLK_UNKNOWN
188 #define SDLK_WORLD_28 SDLK_UNKNOWN
189 #define SDLK_WORLD_29 SDLK_UNKNOWN
190 #define SDLK_WORLD_30 SDLK_UNKNOWN
191 #define SDLK_WORLD_31 SDLK_UNKNOWN
192 #define SDLK_WORLD_32 SDLK_UNKNOWN
193 #define SDLK_WORLD_33 SDLK_UNKNOWN
194 #define SDLK_WORLD_34 SDLK_UNKNOWN
195 #define SDLK_WORLD_35 SDLK_UNKNOWN
196 #define SDLK_WORLD_36 SDLK_UNKNOWN
197 #define SDLK_WORLD_37 SDLK_UNKNOWN
198 #define SDLK_WORLD_38 SDLK_UNKNOWN
199 #define SDLK_WORLD_39 SDLK_UNKNOWN
200 #define SDLK_WORLD_40 SDLK_UNKNOWN
201 #define SDLK_WORLD_41 SDLK_UNKNOWN
202 #define SDLK_WORLD_42 SDLK_UNKNOWN
203 #define SDLK_WORLD_43 SDLK_UNKNOWN
204 #define SDLK_WORLD_44 SDLK_UNKNOWN
205 #define SDLK_WORLD_45 SDLK_UNKNOWN
206 #define SDLK_WORLD_46 SDLK_UNKNOWN
207 #define SDLK_WORLD_47 SDLK_UNKNOWN
208 #define SDLK_WORLD_48 SDLK_UNKNOWN
209 #define SDLK_WORLD_49 SDLK_UNKNOWN
210 #define SDLK_WORLD_50 SDLK_UNKNOWN
211 #define SDLK_WORLD_51 SDLK_UNKNOWN
212 #endif
213 { "W0", 255, SDLK_WORLD_0 },
214 { "W1", 255, SDLK_WORLD_1 },
215 { "W2", 255, SDLK_WORLD_2 }, // 100
216 { "W3", 255, SDLK_WORLD_3 },
217 { "W4", 255, SDLK_WORLD_4 },
218 { "W5", 255, SDLK_WORLD_5 },
219 { "W6", 255, SDLK_WORLD_6 },
220 { "W7", 255, SDLK_WORLD_7 },
221 { "W8", 255, SDLK_WORLD_8 },
222 { "W9", 255, SDLK_WORLD_9 },
223 { "W10", 255, SDLK_WORLD_10 },
224 { "W11", 255, SDLK_WORLD_11 },
225 { "W12", 255, SDLK_WORLD_12 }, // 110
226 { "W13", 255, SDLK_WORLD_13 },
227 { "W14", 255, SDLK_WORLD_14 },
228 { "W15", 255, SDLK_WORLD_15 },
229 { "W16", 255, SDLK_WORLD_16 },
230 { "W17", 255, SDLK_WORLD_17 },
231 { "W18", 255, SDLK_WORLD_18 },
232 { "W19", 255, SDLK_WORLD_19 },
233 { "W20", 255, SDLK_WORLD_20 },
234 { "W21", 255, SDLK_WORLD_21 },
235 { "W22", 255, SDLK_WORLD_22 }, // 120
236 { "W23", 255, SDLK_WORLD_23 },
237 { "W24", 255, SDLK_WORLD_24 },
238 { "W25", 255, SDLK_WORLD_25 },
239 { "W26", 255, SDLK_WORLD_26 },
240 { "W27", 255, SDLK_WORLD_27 },
241 { "W28", 255, SDLK_WORLD_28 },
242 { "W29", 255, SDLK_WORLD_29 },
243 { "W30", 255, SDLK_WORLD_30 },
244 { "W31", 255, SDLK_WORLD_31 },
245 { "W32", 255, SDLK_WORLD_32 }, // 130
246 { "W33", 255, SDLK_WORLD_33 },
247 { "W34", 255, SDLK_WORLD_34 },
248 { "W35", 255, SDLK_WORLD_35 },
249 { "W36", 255, SDLK_WORLD_36 },
250 { "W37", 255, SDLK_WORLD_37 },
251 { "W38", 255, SDLK_WORLD_38 },
252 { "W39", 255, SDLK_WORLD_39 },
253 { "W40", 255, SDLK_WORLD_40 },
254 { "W41", 255, SDLK_WORLD_41 },
255 { "W42", 255, SDLK_WORLD_42 }, // 140
256 { "W43", 255, SDLK_WORLD_43 },
257 { "W44", 255, SDLK_WORLD_44 },
258 { "W45", 255, SDLK_WORLD_45 },
259 { "W46", 255, SDLK_WORLD_46 },
260 { "W47", 255, SDLK_WORLD_47 },
261 { "W48", 255, SDLK_WORLD_48 },
262 { "W49", 255, SDLK_WORLD_49 },
263 { "W50", 255, SDLK_WORLD_50 },
264 { "W51", 255, SDLK_WORLD_51 },
265 { "", 255, SDLK_UNKNOWN }, // 150
266 { "", 255, SDLK_UNKNOWN },
267 { "", 255, SDLK_UNKNOWN },
268 { "", 255, SDLK_UNKNOWN },
269 { "", 255, SDLK_UNKNOWN },
270 { "", 255, SDLK_UNKNOWN },
271 { "PAD", 255, SDLK_KP_ENTER },
272 { "RCTRL", 255, SDLK_RCTRL },
273 #if SDL_MAJOR_VERSION == 2
274 #define SDLK_LMETA SDLK_UNKNOWN
275 #define SDLK_RMETA SDLK_UNKNOWN
276 #endif
277 { "LCMD", 255, SDLK_LMETA },
278 { "RCMD", 255, SDLK_RMETA },
279 { "", 255, SDLK_UNKNOWN }, // 160
280 { "", 255, SDLK_UNKNOWN },
281 { "", 255, SDLK_UNKNOWN },
282 { "", 255, SDLK_UNKNOWN },
283 { "", 255, SDLK_UNKNOWN },
284 { "", 255, SDLK_UNKNOWN },
285 { "", 255, SDLK_UNKNOWN },
286 { "", 255, SDLK_UNKNOWN },
287 { "", 255, SDLK_UNKNOWN },
288 { "", 255, SDLK_UNKNOWN },
289 { "", 255, SDLK_UNKNOWN }, // 170
290 { "", 255, SDLK_UNKNOWN },
291 { "", 255, SDLK_UNKNOWN },
292 { "", 255, SDLK_UNKNOWN },
293 { "", 255, SDLK_UNKNOWN },
294 { "", 255, SDLK_UNKNOWN },
295 { "", 255, SDLK_UNKNOWN },
296 { "", 255, SDLK_UNKNOWN },
297 { "", 255, SDLK_UNKNOWN },
298 { "", 255, SDLK_UNKNOWN },
299 { "", 255, SDLK_UNKNOWN }, // 180
300 { "PAD/", 255, SDLK_KP_DIVIDE },
301 { "", 255, SDLK_UNKNOWN },
302 #if SDL_MAJOR_VERSION == 1
303 #define SDLK_PRINTSCREEN SDLK_PRINT
304 #endif
305 { "PRSCR", 255, SDLK_PRINTSCREEN },
306 { "RALT", 255, SDLK_RALT },
307 { "", 255, SDLK_UNKNOWN },
308 { "", 255, SDLK_UNKNOWN },
309 { "", 255, SDLK_UNKNOWN },
310 { "", 255, SDLK_UNKNOWN },
311 { "", 255, SDLK_UNKNOWN },
312 { "", 255, SDLK_UNKNOWN }, // 190
313 { "", 255, SDLK_UNKNOWN },
314 { "", 255, SDLK_UNKNOWN },
315 { "", 255, SDLK_UNKNOWN },
316 { "", 255, SDLK_UNKNOWN },
317 { "", 255, SDLK_UNKNOWN },
318 { "", 255, SDLK_UNKNOWN },
319 { "", 255, SDLK_UNKNOWN },
320 { "", 255, SDLK_UNKNOWN },
321 { "HOME", 255, SDLK_HOME },
322 { "UP", 255, SDLK_UP }, // 200
323 { "PGUP", 255, SDLK_PAGEUP },
324 { "", 255, SDLK_UNKNOWN },
325 { "LEFT", 255, SDLK_LEFT },
326 { "", 255, SDLK_UNKNOWN },
327 { "RIGHT", 255, SDLK_RIGHT },
328 { "", 255, SDLK_UNKNOWN },
329 { "END", 255, SDLK_END },
330 { "DOWN", 255, SDLK_DOWN },
331 { "PGDN", 255, SDLK_PAGEDOWN },
332 { "INS", 255, SDLK_INSERT }, // 210
333 { "DEL", 255, SDLK_DELETE },
334 #if SDL_MAJOR_VERSION == 1
335 { "W52", 255, SDLK_WORLD_52 },
336 { "W53", 255, SDLK_WORLD_53 },
337 { "W54", 255, SDLK_WORLD_54 },
338 { "W55", 255, SDLK_WORLD_55 },
339 { "W56", 255, SDLK_WORLD_56 },
340 { "W57", 255, SDLK_WORLD_57 },
341 { "W58", 255, SDLK_WORLD_58 },
342 { "W59", 255, SDLK_WORLD_59 },
343 { "W60", 255, SDLK_WORLD_60 }, // 220
344 { "W61", 255, SDLK_WORLD_61 },
345 { "W62", 255, SDLK_WORLD_62 },
346 { "W63", 255, SDLK_WORLD_63 },
347 { "W64", 255, SDLK_WORLD_64 },
348 { "W65", 255, SDLK_WORLD_65 },
349 { "W66", 255, SDLK_WORLD_66 },
350 { "W67", 255, SDLK_WORLD_67 },
351 { "W68", 255, SDLK_WORLD_68 },
352 { "W69", 255, SDLK_WORLD_69 },
353 { "W70", 255, SDLK_WORLD_70 }, // 230
354 { "W71", 255, SDLK_WORLD_71 },
355 { "W72", 255, SDLK_WORLD_72 },
356 { "W73", 255, SDLK_WORLD_73 },
357 { "W74", 255, SDLK_WORLD_74 },
358 { "W75", 255, SDLK_WORLD_75 },
359 { "W76", 255, SDLK_WORLD_76 },
360 { "W77", 255, SDLK_WORLD_77 },
361 { "W78", 255, SDLK_WORLD_78 },
362 { "W79", 255, SDLK_WORLD_79 },
363 { "W80", 255, SDLK_WORLD_80 }, // 240
364 { "W81", 255, SDLK_WORLD_81 },
365 { "W82", 255, SDLK_WORLD_82 },
366 { "W83", 255, SDLK_WORLD_83 },
367 { "W84", 255, SDLK_WORLD_84 },
368 { "W85", 255, SDLK_WORLD_85 },
369 { "W86", 255, SDLK_WORLD_86 },
370 { "W87", 255, SDLK_WORLD_87 },
371 { "W88", 255, SDLK_WORLD_88 },
372 { "W89", 255, SDLK_WORLD_89 },
373 { "W90", 255, SDLK_WORLD_90 }, // 250
374 { "W91", 255, SDLK_WORLD_91 },
375 { "W92", 255, SDLK_WORLD_92 },
376 { "W93", 255, SDLK_WORLD_93 },
377 { "W94", 255, SDLK_WORLD_94 },
378 { "W95", 255, SDLK_WORLD_95 }, // 255
379 #endif
380 }};
381
382 namespace {
383
384 struct d_event_keycommand : d_event
385 {
386 const unsigned keycode;
387 constexpr d_event_keycommand(const event_type t, const unsigned k) :
388 d_event{t}, keycode{k}
389 {
390 }
391 };
392
393 static int key_ismodlck(int keycode)
394 {
395 switch (keycode)
396 {
397 case KEY_LSHIFT:
398 case KEY_RSHIFT:
399 case KEY_LALT:
400 case KEY_RALT:
401 case KEY_LCTRL:
402 case KEY_RCTRL:
403 case KEY_LMETA:
404 case KEY_RMETA:
405 return KEY_ISMOD;
406 case KEY_NUMLOCK:
407 case KEY_SCROLLOCK:
408 case KEY_CAPSLOCK:
409 return KEY_ISLCK;
410 default:
411 return 0;
412 }
413 }
414
415 }
416
417 unsigned char key_ascii()
418 {
419 using std::move;
420 using std::next;
421 static std::array<unsigned char, KEY_BUFFER_SIZE> unibuffer;
422 auto src = begin(unicode_frame_buffer);
423 auto dst = next(begin(unibuffer), strlen(reinterpret_cast<const char *>(&unibuffer[0])));
424
425 // move temporal chars from unicode_frame_buffer to empty space behind last unibuffer char (if any)
426 for (; dst != end(unibuffer); ++dst)
427 if (*src != '\0')
428 {
429 *dst = *src;
430 *src = '\0';
431 ++src;
432 }
433
434 // unibuffer is not empty. store first char, remove it, shift all chars one step left and then print our char
435 if (unibuffer[0] != '\0')
436 {
437 unsigned char retval = unibuffer[0];
438 *move(next(unibuffer.begin()), unibuffer.end(), unibuffer.begin()) = 0;
439 return retval;
440 }
441 else
442 return 255;
443 }
444
445 void pressed_keys::update(const std::size_t keycode, const uint8_t down)
446 {
447 constexpr unsigned all_modifiers_combined = KEY_SHIFTED | KEY_ALTED | KEY_CTRLED | KEY_DEBUGGED | KEY_METAED;
448 constexpr unsigned all_modifiers_shifted = all_modifiers_combined >> modifier_shift;
449 static_assert(all_modifiers_combined == all_modifiers_shifted << modifier_shift, "shift error");
450 static_assert(all_modifiers_shifted == static_cast<uint8_t>(all_modifiers_shifted), "truncation error");
451 uint8_t mask;
452 keyd_pressed.update_pressed(keycode, down);
453 switch (keycode)
454 {
455 case KEY_LSHIFT:
456 case KEY_RSHIFT:
457 mask = KEY_SHIFTED >> modifier_shift;
458 break;
459 case KEY_LALT:
460 case KEY_RALT:
461 mask = KEY_ALTED >> modifier_shift;
462 break;
463 case KEY_LCTRL:
464 case KEY_RCTRL:
465 mask = KEY_CTRLED >> modifier_shift;
466 break;
467 case KEY_DELETE:
468 mask = KEY_DEBUGGED >> modifier_shift;
469 break;
470 case KEY_LMETA:
471 case KEY_RMETA:
472 mask = KEY_METAED >> modifier_shift;
473 break;
474 default:
475 return;
476 }
477 if (down)
478 modifier_cache |= mask;
479 else
480 modifier_cache &= ~mask;
481 }
482
483 window_event_result key_handler(const SDL_KeyboardEvent *const kevent)
484 {
485 #if SDL_MAJOR_VERSION == 2
486 if (!keyd_repeat && kevent->repeat)
487 return window_event_result::ignored;
488 #endif
489 // Read SDLK symbol and state
490 const auto event_keysym = kevent->keysym.sym;
491 if (event_keysym == SDLK_UNKNOWN)
492 return window_event_result::ignored;
493 const auto key_state = (kevent->state != SDL_RELEASED);
494
495 // fill the unicode frame-related unicode buffer
496 if (key_state)
497 {
498 #if SDL_MAJOR_VERSION == 1
499 const auto sym = kevent->keysym.unicode;
500 #elif SDL_MAJOR_VERSION == 2
501 const auto sym = kevent->keysym.sym;
502 #endif
503 if (sym > 31 && sym < 255)
504 {
505 range_for (auto &i, unicode_frame_buffer)
506 if (i == '\0')
507 {
508 i = sym;
509 break;
510 }
511 }
512 }
513
514 //=====================================================
515 const auto re = key_properties.rend();
516 const auto &&fi{std::ranges::find(key_properties.rbegin(), re, event_keysym, &key_props::sym)};
517 if (fi == re)
518 return window_event_result::ignored;
519 unsigned keycode = std::distance(key_properties.begin(), std::next(fi).base());
520 if (keycode == 0)
521 return window_event_result::ignored;
522
523 /*
524 * process the key if:
525 * - it's a valid key AND
526 * - if the keystate has changed OR
527 * - key state same as last one and game accepts key repeats but keep out mod/lock keys
528 */
529 if (key_state != keyd_pressed[keycode] || (keyd_repeat && !key_ismodlck(keycode)))
530 {
531 // now update the key props
532 keyd_pressed.update(keycode, key_state);
533 const auto raw_keycode{keycode};
534 keycode |= keyd_pressed.get_modifiers();
535
536 // We allowed the key to be added to the queue for now,
537 // because there are still input loops without associated windows
538 const d_event_keycommand event{key_state ? event_type::key_command : event_type::key_release, keycode};
539 con_printf(CON_DEBUG, "Sending event %s: %s %s %s %s %s %s",
540 (key_state) ? "event_type::key_command": "event_type::key_release",
541 (keycode & KEY_METAED) ? "META" : "",
542 (keycode & KEY_DEBUGGED) ? "DEBUG" : "",
543 (keycode & KEY_CTRLED) ? "CTRL" : "",
544 (keycode & KEY_ALTED) ? "ALT" : "",
545 (keycode & KEY_SHIFTED) ? "SHIFT" : "",
546 key_properties[raw_keycode].key_text
547 );
548 return event_send(event);
549 }
550 return window_event_result::ignored;
551 }
552
553 void key_init()
554 {
555 #if SDL_MAJOR_VERSION == 1
556 SDL_EnableUNICODE(1);
557 #endif
558 key_toggle_repeat(1);
559
560 keyd_time_when_last_pressed = timer_query();
561 // Clear the keyboard array
562 key_flush();
563 }
564
565 namespace {
566
567 static void restore_sticky_key(const uint8_t *keystate, const unsigned i)
568 {
569 #if SDL_MAJOR_VERSION == 1
570 const auto ki = key_properties[i].sym;
571 #elif SDL_MAJOR_VERSION == 2
572 const auto ki{i};
573 #endif
574 const auto v = keystate[ki]; // do not flush status of sticky keys
575 keyd_pressed.update_pressed(i, v);
576 }
577
578 }
579
580 void key_flush()
581 {
582 //Clear the unicode buffer
583 unicode_frame_buffer = {};
584 keyd_pressed = {};
585 if (unlikely(CGameArg.CtlNoStickyKeys))
586 return;
587 #if SDL_MAJOR_VERSION == 1
588 const auto &keystate = SDL_GetKeyState(nullptr);
589 #define DXX_SDL_STICKY_KEYS {KEY_CAPSLOCK, KEY_NUMLOCK, KEY_SCROLLOCK}
590 #elif SDL_MAJOR_VERSION == 2
591 const auto &keystate = SDL_GetKeyboardState(nullptr);
592 #define DXX_SDL_STICKY_KEYS {SDL_SCANCODE_CAPSLOCK, SDL_SCANCODE_SCROLLLOCK, SDL_SCANCODE_NUMLOCKCLEAR}
593 #endif
594 range_for (const auto key, DXX_SDL_STICKY_KEYS)
595 #undef DXX_SDL_STICKY_KEYS
596 restore_sticky_key(keystate, key);
597 }
598
599 void event_keycommand_send(unsigned key) {
600 event_send(d_event_keycommand{event_type::key_command, key});
601 }
602
603 int event_key_get(const d_event &event)
604 {
605 auto &e = static_cast<const d_event_keycommand &>(event);
606 assert(e.type == event_type::key_command || e.type == event_type::key_release);
607 return e.keycode;
608 }
609
610 // same as above but without mod states
611 int event_key_get_raw(const d_event &event)
612 {
613 return event_key_get(event) & ~(KEY_SHIFTED | KEY_ALTED | KEY_CTRLED | KEY_DEBUGGED | KEY_METAED);
614 }
615
616 void key_toggle_repeat1()
617 {
618 #if SDL_MAJOR_VERSION == 1
619 if (SDL_EnableKeyRepeat(KEY_REPEAT_DELAY, KEY_REPEAT_INTERVAL) == 0)
620 #endif
621 keyd_repeat = 1;
622 key_flush();
623 }
624
625 void key_toggle_repeat0()
626 {
627 #if SDL_MAJOR_VERSION == 1
628 SDL_EnableKeyRepeat(0, 0);
629 #endif
630 keyd_repeat = 0;
631 key_flush();
632 }
633
634 }