Debian Codegraph @

ddio/key.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ddio_common/key.cpp $
21 * $Revision: 27 $
22 * $Date: 10/21/99 9:28p $
23 * $Author: Jeff $
24 *
25 * Keyboard common interface
26 *
27 * $Log: /DescentIII/Main/ddio_common/key.cpp $
28 *
29 * 27 10/21/99 9:28p Jeff
30 * B.A. Macintosh code merge
31 *
32 * 26 8/31/99 5:30p Jason
33 * network statistics code
34 *
35 * 25 4/24/99 8:40p Samir
36 * debug key for mono screens moved into key handler.
37 *
38 * 24 4/24/99 5:41p Samir
39 * moved key to ascii, ascii to key to the ddio_common library.
40 *
41 * 23 4/22/99 2:00a Jeff
42 * pass in ddio_init_info
43 *
44 * 22 2/21/99 6:38p Samir
45 * mouse and key input better. buffered mouse.
46 *
47 * 21 2/05/99 1:16p Samir
48 * reset low level keys when flushing keyboard in the high level. added a
49 * function to the low level to reset the status of a key, called from
50 * high level key flush.
51 *
52 * 20 1/25/99 7:27p Samir
53 * fixed timing issues with emulated keyboard key down times. (Win32 sends
54 * multiple keydown messages, so ignore them.)
55 *
56 * 19 1/25/99 6:47p Samir
57 * allow slow keyboard
58 *
59 * 18 1/25/99 11:02a Samir
60 * revamped mouse and key controls.
61 *
62 * 17 10/21/98 6:51p Samir
63 * took out dynamic key updating.
64 *
65 * 16 10/21/98 12:02p Samir
66 * properly update odd keys when they are released throug
67 * ddio_KeyGetDownTime.
68 *
69 * 15 10/16/98 12:16p Samir
70 * more accurate key timing broke ddio_KeyDownTime, so key down time is
71 * rounded off 5 decimal places.
72 *
73 * 14 9/17/98 12:50p Samir
74 * added ddio_KeyFlushKey.
75 *
76 * 13 7/01/98 4:56p Samir
77 * changed some deinit stuff.
78 *
79 * 12 3/31/98 12:46p Samir
80 * keyboard IO system better. uses getmsgproc windows hook.
81 *
82 * 11 3/24/98 4:26p Samir
83 * added function to return the state of an adjusted key.
84 *
85 * 10 2/25/98 6:11p Samir
86 * Added functions to better deal with key flushing.
87 *
88 * 9 2/03/98 5:16p Matt
89 * Changed key buffer to be ushorts so shift,ctrl,etc. bits don't get
90 * lost.
91 * Added support for KEY_DEBUGGED.
92 *
93 * 8 1/21/98 6:47p Samir
94 * Added debugging info.
95 *
96 * 7 12/10/97 1:12p Samir
97 * Pass time to ddio_UpdateKeyState
98 *
99 * 6 10/29/97 4:14p Samir
100 * Keep record of key down count per key.
101 *
102 * 5 10/16/97 2:28p Samir
103 * move keyboard init into ddio_init and added preemptive select for
104 * keyboard.
105 *
106 * 4 8/29/97 11:54a Samir
107 * Fixed some problem with keycodes that were greater than 0x80, being
108 * sign extended upon return from KeyInKey.
109 *
110 * 3 8/13/97 5:41p Samir
111 * Took out annoying mprintf of keycode.
112 *
113 * 2 7/25/97 6:31p Samir
114 * Fixed some really stupid key queue buffer check bugs that could cause
115 * instability.
116 *
117 * 3 5/09/97 6:45p Samir
118 * Common keystate update function
119 *
120 * 2 5/08/97 1:54p Samir
121 * Took out prototype to ddio_Close
122 *
123 * 1 5/08/97 12:22p Samir
124 * Moved common keyboard code from ddio_win to ddio_common.
125 *
126 * $NoKeywords: $
127 */
128
129 // Handles keyboard queue management
130
131 #include "ddio.h"
132 #include "pserror.h"
133
134 #define KEY_QUEUE_SIZE 16
135
136 // This is the key used as a debugging modifier
137 #define KEY_DEBUG KEY_DELETE
138
139 // ----------------------------------------------------------------------------
140 // Data for ddio_common keyboard code.
141 // ----------------------------------------------------------------------------
142
143 static bool DDIO_key_init = 0;
144 volatile uint8_t DDIO_key_state[DDIO_MAX_KEYS];
145 volatile int16_t DDIO_key_down_count[DDIO_MAX_KEYS];
146
147 static struct DDIO_key_queue {
148 uint16_t buffer[KEY_QUEUE_SIZE]; // Keyboard buffer queue
149 int tail;
150 int head;
151 } DDIO_key_queue;
152
153 // ----------------------------------------------------------------------------
154 // Common initialization
155 // ----------------------------------------------------------------------------
156
157 bool ddio_KeyInit(ddio_init_info *init_info) {
158 if (ddio_InternalKeyInit(init_info)) {
159 DDIO_key_init = 1;
160 ddio_KeyFlush();
161 return 1;
162 }
163
164 return 0;
165 }
166
167 void ddio_KeyClose() {
168 if (DDIO_key_init) {
169 ddio_InternalKeyClose();
170 DDIO_key_init = 0;
171 }
172 }
173
174 // ----------------------------------------------------------------------------
175 // Keyboard accessor functions
176 // ----------------------------------------------------------------------------
177
178 float ddio_KeyDownTime(int key) {
179 ASSERT(DDIO_key_init);
180
181 // snap off shift states, etc.
182 return ddio_InternalKeyDownTime((uint8_t)(key & 0xff));
183 }
184
185 // return number of times a key's been down since last call.
186 int ddio_KeyDownCount(int key) {
187 int count = DDIO_key_down_count[key];
188 DDIO_key_down_count[key] = 0;
189
190 return count;
191 }
192
193 // returns the state of an ADJUSTED KEY VALUE
194 bool ddio_GetAdjKeyState(int adj_key) {
195 uint8_t key_raw = adj_key & 0xff;
196 int key_test = 0;
197
198 if (adj_key & KEY_SHIFTED) {
199 if (KEY_STATE(KEY_LSHIFT) || KEY_STATE(KEY_RSHIFT))
200 key_test |= KEY_SHIFTED;
201 }
202 if (adj_key & KEY_ALTED) {
203 if (KEY_STATE(KEY_LALT) || KEY_STATE(KEY_RALT))
204 key_test |= KEY_ALTED;
205 }
206 if (adj_key & KEY_CTRLED) {
207 if (KEY_STATE(KEY_LCTRL) || KEY_STATE(KEY_RCTRL))
208 key_test |= KEY_CTRLED;
209 }
210 if (adj_key & KEY_DEBUGGED) {
211 if (KEY_STATE(KEY_DEBUG))
212 key_test |= KEY_DEBUGGED;
213 }
214
215 if (KEY_STATE(key_raw))
216 key_test |= key_raw;
217
218 if (key_test == adj_key)
219 return true;
220 else
221 return false;
222 }
223
224 // ----------------------------------------------------------------------------
225 // Keyboard Queue functions
226 // ----------------------------------------------------------------------------
227
228 void ddio_KeyFlush() {
229 int i;
230
231 if (!DDIO_key_init)
232 return;
233
234 // flush out internal key input system by gathering all data in buffer.
235 DDIO_key_queue.head = DDIO_key_queue.tail = 0;
236
237 for (i = 0; i < KEY_QUEUE_SIZE; i++) {
238 DDIO_key_queue.buffer[i] = 0;
239 }
240
241 // flush keyboard array too
242 for (i = 0; i < DDIO_MAX_KEYS; i++) {
243 DDIO_key_down_count[i] = 0;
244 DDIO_key_state[i] = 0;
245 ddio_InternalResetKey(i);
246 }
247 }
248
249 void ddio_KeyFlushKey(int key) {
250 DDIO_key_down_count[key] = 0;
251 DDIO_key_state[key] = 0;
252 }
253
254 int ddio_KeyInKey() {
255 unsigned key = 0;
256
257 if (DDIO_key_queue.head != DDIO_key_queue.tail) {
258 key = (unsigned)DDIO_key_queue.buffer[DDIO_key_queue.head];
259 // mprintf(1, "%x ", key);
260 DDIO_key_queue.head++;
261 if (DDIO_key_queue.head >= KEY_QUEUE_SIZE)
262 DDIO_key_queue.head = 0;
263 }
264
265 return (int)key;
266 }
267
268 int ddio_KeyPeek() {
269 if (DDIO_key_queue.head != DDIO_key_queue.tail) {
270 return (int)DDIO_key_queue.buffer[DDIO_key_queue.head];
271 }
272 return 0;
273 }
274
275 void ddio_AddKeyToQueue(int key) {
276 int keycode = key;
277 int temp;
278
279 if (DDIO_key_state[KEY_LSHIFT] || DDIO_key_state[KEY_RSHIFT])
280 keycode |= KEY_SHIFTED;
281 if (DDIO_key_state[KEY_LCTRL] || DDIO_key_state[KEY_RCTRL])
282 keycode |= KEY_CTRLED;
283 if (DDIO_key_state[KEY_LALT] || DDIO_key_state[KEY_RALT])
284 keycode |= KEY_ALTED;
285 if (DDIO_key_state[KEY_CMD])
286 keycode |= KEY_CMD; // DAJ
287
288 #ifdef _DEBUG
289 if (DDIO_key_state[KEY_DEBUG])
290 keycode |= KEY_DEBUGGED;
291 #endif
292
293 temp = DDIO_key_queue.tail + 1;
294 if (temp >= KEY_QUEUE_SIZE)
295 temp = 0;
296
297 if (temp != DDIO_key_queue.head) {
298 DDIO_key_queue.buffer[DDIO_key_queue.tail] = (uint16_t)keycode;
299 DDIO_key_queue.tail = temp;
300 // mprintf(1, "%d ", keycode);
301 }
302 }
303
304 // Updates timing info for keys
305 void ddio_UpdateKeyState(int key, bool isdown) {
306 if (isdown) {
307 if (!DDIO_key_state[key]) {
308 ddio_AddKeyToQueue(key);
309 DDIO_key_down_count[key]++;
310 }
311 DDIO_key_state[key] = 1;
312 } else {
313 DDIO_key_state[key] = 0;
314 }
315 }
316
317 ////////////////////////////////////////////////////////////////////////////////
318
319 static uint8_t ascii_table[128] = {
320 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 255, 255, 'q', 'w', 'e', 'r', 't', 'y',
321 'u', 'i', 'o', 'p', '[', ']', 255, 255, 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`', 255, '\\',
322 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255, '*', 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255,
323 255, 255, 255, 255, 255, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 255, 255, 255, 255,
324 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
325 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
326
327 static uint8_t shifted_ascii_table[128] = {
328 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', 255, 255, 'Q', 'W', 'E', 'R', 'T', 'Y',
329 'U', 'I', 'O', 'P', '{', '}', 255, 255, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 255, '|',
330 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255, 255, 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255,
331 255, 255, 255, 255, 255, '7', '8', '9', '-', '4', '5', '6', '+', '1', '2', '3', '0', '.', 255, 255, 255, 255,
332 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
333 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255};
334
335 #define SHK KEY_SHIFTED
336
337 // keycode value for each ascii.
338 static int keycode_table[128] = {
339 0,
340 0,
341 0,
342 0,
343 0,
344 0,
345 0,
346 0,
347 0,
348 KEY_TAB,
349 0,
350 0,
351 0,
352 KEY_ENTER,
353 0,
354 0, // 0-15
355 0,
356 0,
357 0,
358 0,
359 0,
360 0,
361 0,
362 0,
363 0,
364 0,
365 0,
366 KEY_ESC,
367 0,
368 0,
369 0,
370 0, // 16-31
371 KEY_SPACEBAR,
372 SHK + KEY_1,
373 SHK + KEY_RAPOSTRO,
374 SHK + KEY_3,
375 SHK + KEY_4,
376 SHK + KEY_5, // 32-37
377 SHK + KEY_7,
378 KEY_RAPOSTRO,
379 SHK + KEY_9,
380 SHK + KEY_0,
381 SHK + KEY_8,
382 SHK + KEY_EQUAL, // 38-43
383 KEY_COMMA,
384 KEY_MINUS,
385 KEY_PERIOD,
386 KEY_SLASH,
387 KEY_0,
388 KEY_1,
389 KEY_2,
390 KEY_3, // 44-51
391 KEY_4,
392 KEY_5,
393 KEY_6,
394 KEY_7,
395 KEY_8,
396 KEY_9,
397 SHK + KEY_SEMICOL,
398 KEY_SEMICOL, // 52-59
399 SHK + KEY_COMMA,
400 KEY_EQUAL,
401 SHK + KEY_PERIOD,
402 SHK + KEY_SLASH,
403 SHK + KEY_2, // 60-64
404 KEY_A,
405 KEY_B,
406 KEY_C,
407 KEY_D,
408 KEY_E,
409 KEY_F,
410 KEY_G,
411 KEY_H,
412 KEY_I,
413 KEY_J,
414 KEY_K, // 65-75
415 KEY_L,
416 KEY_M,
417 KEY_N,
418 KEY_O,
419 KEY_P,
420 KEY_Q,
421 KEY_R,
422 KEY_S,
423 KEY_T,
424 KEY_U,
425 KEY_V, // 76-86
426 KEY_W,
427 KEY_X,
428 KEY_Y,
429 KEY_Z,
430 KEY_LBRACKET,
431 KEY_BACKSLASH,
432 KEY_RBRACKET, // 87-93
433 SHK + KEY_6,
434 SHK + KEY_MINUS,
435 KEY_LAPOSTRO, // 94-96
436 KEY_A,
437 KEY_B,
438 KEY_C,
439 KEY_D,
440 KEY_E,
441 KEY_F,
442 KEY_G,
443 KEY_H,
444 KEY_I,
445 KEY_J,
446 KEY_K, // 97-107
447 KEY_L,
448 KEY_M,
449 KEY_N,
450 KEY_O,
451 KEY_P,
452 KEY_Q,
453 KEY_R,
454 KEY_S,
455 KEY_T,
456 KEY_U,
457 KEY_V, // 108-118
458 KEY_W,
459 KEY_X,
460 KEY_Y,
461 KEY_Z,
462 SHK + KEY_LBRACKET,
463 SHK + KEY_BACKSLASH, // 119-124
464 SHK + KEY_RBRACKET,
465 SHK + KEY_LAPOSTRO,
466 0, // 125-127
467 };
468
469 // converts keycode to ASCII
470 int ddio_KeyToAscii(int code) {
471 int shifted;
472
473 shifted = code & KEY_SHIFTED;
474 code &= 0xFF;
475
476 if (code >= 127)
477 return 255;
478
479 if (shifted)
480 return (int)shifted_ascii_table[code];
481 else
482 return (int)ascii_table[code];
483 }
484
485 // converts ascii code to key
486 int ddio_AsciiToKey(int ascii) {
487 if (ascii < 0 || ascii >= 128)
488 return 0;
489
490 if (ascii >= 'A' && ascii <= 'Z') {
491 // I'm lazy
492 return (SHK + keycode_table[ascii]);
493 } else {
494 return keycode_table[ascii];
495 }
496 }