2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Io/Key.cpp $
15 * <insert description of file here>
18 * Revision 1.12 2004/06/11 01:14:12 tigital
19 * OSX: switched to __APPLE__
21 * Revision 1.11 2003/05/18 03:57:08 taylor
22 * do not swap German z and y keys if they are already swapped
24 * Revision 1.9 2003/01/30 19:55:01 relnev
25 * add German keys (this is mostly a patch already sent in by someone else that hasn't made it into cvs yet) (Taylor Richards)
27 * Revision 1.8 2002/06/17 23:11:39 relnev
28 * enable sdl key repeating.
32 * Revision 1.7 2002/06/09 04:41:21 relnev
33 * added copyright header
35 * Revision 1.6 2002/06/05 04:03:32 relnev
36 * finished cfilesystem.
38 * removed some old code.
40 * fixed mouse save off-by-one.
44 * Revision 1.5 2002/05/31 03:34:02 theoddone33
48 * Revision 1.4 2002/05/30 23:46:29 theoddone33
49 * some minor key changes (not necessarily fixes)
51 * Revision 1.3 2002/05/30 16:50:24 theoddone33
52 * Keyboard partially fixed
54 * Revision 1.2 2002/05/29 23:17:50 theoddone33
55 * Non working text code and fixed keys
57 * Revision 1.1.1.1 2002/05/03 03:28:09 root
61 * 6 10/29/99 6:10p Jefff
62 * squashed the damned y/z german issues once and for all
64 * 5 6/07/99 1:21p Dave
65 * Fixed debug console scrolling problem. Thread related.
67 * 4 6/02/99 6:18p Dave
68 * Fixed TNT lockup problems! Wheeeee!
70 * 3 11/05/98 4:18p Dave
71 * First run nebula support. Beefed up localization a bit. Removed all
72 * conditional compiles for foreign versions. Modified mission file
75 * 2 10/07/98 10:53a Dave
78 * 1 10/07/98 10:49a Dave
80 * 37 6/19/98 3:50p Lawrance
81 * change GERMAN to GR_BUILD
83 * 36 6/17/98 11:05a Lawrance
84 * translate french and german keys
86 * 35 6/12/98 4:49p Hoffoss
87 * Added code to remap scancodes for german and french keyboards.
89 * 34 5/20/98 12:10a Mike
92 * 33 5/19/98 12:19p Mike
95 * 32 5/19/98 12:28a Mike
98 * 31 5/18/98 11:01p Mike
99 * Adding support for cheat system.
101 * 30 5/11/98 12:09a Lawrance
102 * Put in code to turn on/off NumLock key when running under 95
104 * 29 5/01/98 4:23p Lawrance
105 * Remap the scancode for the UK "\" key
107 * 28 4/18/98 12:42p John
108 * Added code to use DirectInput to read keyboard. Took out because it
109 * didn't differentiate btwn Pause and Numlock and sometimes Ctrl.
111 * 27 4/13/98 10:16a John
112 * Switched gettime back to timer_get_milliseconds, which is now thread
115 * 26 4/12/98 11:08p Lawrance
116 * switch back to using gettime() in separate threads
118 * 25 4/12/98 5:31p Lawrance
119 * use timer_get_milliseconds() instead of gettime()
121 * 24 3/25/98 8:08p John
122 * Restructured software rendering into two modules; One for windowed
123 * debug mode and one for DirectX fullscreen.
125 * 23 1/23/98 3:49p Mike
126 * Fix bug in negative time-down due to latency.
128 * 22 1/07/98 6:41p Lawrance
129 * Pass message latency to the keyboard lib.
131 * 21 11/17/97 10:42a John
132 * On Debug+Backsp, cleared out keys so that it looks like nothing ever
133 * happened, so they're not stuck down.
135 * 20 11/14/97 4:33p Mike
136 * Change Debug key to backquote (from F11).
137 * Balance a ton of subsystems in ships.tbl.
138 * Change "Heavy Laser" to "Disruptor".
140 * 19 9/13/97 9:30a Lawrance
141 * added ability to block certain keys from the keyboard
143 * 18 9/10/97 6:02p Hoffoss
144 * Added code to check for key-pressed sexp operator in FreeSpace as part
145 * of training mission stuff.
147 * 17 9/09/97 11:08a Sandeep
148 * fixed warning level 4
150 * 16 7/29/97 5:30p Lawrance
151 * move gettime() to timer module
153 * 15 4/22/97 10:56a John
154 * fixed some resource leaks.
156 * 14 2/03/97 4:23p Allender
157 * use F11 as debug key now
159 * 13 1/10/97 5:15p Mike
160 * Moved ship-specific parameters from obj_subsystem to ship_subsys.
162 * Added turret code to AI system.
167 #include <ctype.h> // for toupper
173 #include "localize.h"
175 #define KEY_BUFFER_SIZE 16
177 //-------- Variable accessed by outside functions ---------
178 ubyte keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
180 uint keyd_last_pressed;
181 uint keyd_last_released;
182 int keyd_time_when_last_pressed;
184 static bool keyd_pressed[SDL_NUM_SCANCODES];
186 typedef struct keyboard {
187 ushort keybuffer[KEY_BUFFER_SIZE];
188 uint time_pressed[KEY_BUFFER_SIZE];
189 uint TimeKeyWentDown[SDL_NUM_SCANCODES];
190 uint TimeKeyHeldDown[SDL_NUM_SCANCODES];
191 uint TimeKeyDownChecked[SDL_NUM_SCANCODES];
192 uint NumDowns[SDL_NUM_SCANCODES];
193 uint NumUps[SDL_NUM_SCANCODES];
194 int down_check[SDL_NUM_SCANCODES]; // nonzero if has been pressed yet this mission
195 uint keyhead, keytail;
202 CRITICAL_SECTION key_lock;
204 //int Backspace_debug=1; // global flag that will enable/disable the backspace key from stopping execution
205 // This flag was created since the backspace key is also used to correct mistakes
206 // when typing in your pilots callsign. This global flag is checked before execution
209 // used to limit the keypresses that are accepted from the keyboard
210 #define MAX_FILTER_KEYS 64
212 int Key_filter[MAX_FILTER_KEYS];
214 static int Key_numlock_was_on = 0; // Flag to indicate whether NumLock is on at start
216 int Cheats_enabled = 0;
217 int Key_normal_game = 0;
220 bool key_pressed(int keycode)
222 SDL_Scancode scancode = SDL_GetScancodeFromKey(keycode & KEY_MASK);
224 return keyd_pressed[scancode];
227 int key_numlock_is_on()
230 state = SDL_GetKeyboardState(NULL);
231 if ( state[SDL_SCANCODE_NUMLOCKCLEAR] ) {
238 void key_turn_off_numlock()
242 void key_turn_on_numlock()
246 // Convert a BIOS scancode to ASCII.
247 // If scancode >= 127, returns 255, meaning there is no corresponding ASCII code.
248 // Uses ascii_table and shifted_ascii_table to translate scancode to ASCII.
249 int key_to_ascii(int keycode, bool force_up)
253 // bail on non-printable keycodes
254 if (keycode & SDLK_SCANCODE_MASK) {
258 shifted = keycode & KEY_SHIFTED;
261 // this is definitely never come back to bite me in the ass
262 if ( ((keycode >= SDLK_SPACE) && (keycode <= SDLK_AT))
263 || ((keycode >= SDLK_LEFTBRACKET) && (keycode <= SDLK_z)) )
265 if ( (keycode >= SDLK_a) && (shifted || force_up) ) {
266 return toupper(keycode);
275 // Flush the keyboard buffer.
276 // Clear the keyboard array (keyd_pressed).
282 if ( !key_inited ) return;
284 ENTER_CRITICAL_SECTION(&key_lock);
286 key_data.keyhead = key_data.keytail = 0;
288 //Clear the keyboard buffer
289 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
290 key_data.keybuffer[i] = 0;
291 key_data.time_pressed[i] = 0;
294 //Clear the keyboard array
296 CurTime = timer_get_milliseconds();
299 for (i=0; i<SDL_NUM_SCANCODES; i++ ) {
300 keyd_pressed[i] = false;
301 key_data.TimeKeyDownChecked[i] = CurTime;
302 key_data.TimeKeyWentDown[i] = CurTime;
303 key_data.TimeKeyHeldDown[i] = 0;
304 key_data.NumDowns[i]=0;
305 key_data.NumUps[i]=0;
308 LEAVE_CRITICAL_SECTION(&key_lock);
311 // A nifty function which performs the function:
312 // n = (n+1) % KEY_BUFFER_SIZE
313 // (assuming positive values of n).
317 if ( n >= KEY_BUFFER_SIZE ) n=0;
321 // Returns 1 if character waiting... 0 otherwise
324 int is_one_waiting = 0;
326 if ( !key_inited ) return 0;
328 ENTER_CRITICAL_SECTION(&key_lock);
330 if (key_data.keytail != key_data.keyhead){
334 LEAVE_CRITICAL_SECTION(&key_lock);
336 return is_one_waiting;
339 // Return keycode if a key has been pressed,
341 // Reads keys out of the key buffer and updates keyhead.
344 SDL_Scancode scancode = SDL_SCANCODE_UNKNOWN;
350 if (key_data.keytail != key_data.keyhead) {
351 scancode = (SDL_Scancode)key_data.keybuffer[key_data.keyhead];
352 key_data.keyhead = add_one(key_data.keyhead);
357 // need to strip key mod state for keycode lookup
358 mod = (scancode & 0xf900);
359 keycode = SDL_GetKeyFromScancode((SDL_Scancode)(scancode & KEY_MASK));
361 return (keycode | mod);
364 // If not installed, uses BIOS and returns getch();
365 // Else returns pending key (or waits for one if none waiting).
371 if ( !key_inited ) return 0;
373 while (!key_checkch()){
383 // Set global shift_status with modifier results (shift, ctrl, alt).
384 uint key_get_shift_status()
386 unsigned int shift_status = 0;
391 SDL_Keymod kmod = SDL_GetModState();
393 if (kmod & KMOD_SHIFT)
394 shift_status |= KEY_SHIFTED;
397 shift_status |= KEY_ALTED;
399 if (kmod & KMOD_CTRL)
400 shift_status |= KEY_CTRLED;
403 if (key_pressed(KEY_DEBUG_KEY))
404 shift_status |= KEY_DEBUGGED;
406 if (key_pressed(KEY_DEBUG_KEY)) {
407 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
408 if ((Cheats_enabled) && Key_normal_game) {
409 mprintf(("Debug key\n"));
410 shift_status |= KEY_DEBUGGED1;
418 // Returns amount of time key (specified by "keycode") has been down since last call.
419 // Returns float, unlike key_down_time() which returns a fix.
420 float key_down_timef(int keycode)
422 uint time_down, time;
424 SDL_Scancode scancode;
429 scancode = SDL_GetScancodeFromKey(keycode & KEY_MASK);
431 if (scancode == SDL_SCANCODE_UNKNOWN)
434 ENTER_CRITICAL_SECTION(&key_lock);
436 time = timer_get_milliseconds();
437 delta_time = time - key_data.TimeKeyDownChecked[scancode];
438 key_data.TimeKeyDownChecked[scancode] = time;
440 if ( delta_time <= 1 ) {
441 key_data.TimeKeyWentDown[scancode] = time;
442 if (keyd_pressed[scancode]) {
443 LEAVE_CRITICAL_SECTION(&key_lock);
446 LEAVE_CRITICAL_SECTION(&key_lock);
451 if ( !keyd_pressed[scancode] ) {
452 time_down = key_data.TimeKeyHeldDown[scancode];
453 key_data.TimeKeyHeldDown[scancode] = 0;
455 time_down = time - key_data.TimeKeyWentDown[scancode];
456 key_data.TimeKeyWentDown[scancode] = time;
459 LEAVE_CRITICAL_SECTION(&key_lock);
461 return i2fl(time_down) / i2fl(delta_time);
464 // Returns number of times key has went from up to down since last call.
465 int key_down_count(int keycode)
468 SDL_Scancode scancode;
473 scancode = SDL_GetScancodeFromKey(keycode & KEY_MASK);
475 if (scancode == SDL_SCANCODE_UNKNOWN)
478 ENTER_CRITICAL_SECTION(&key_lock);
480 n = key_data.NumDowns[scancode];
481 key_data.NumDowns[scancode] = 0;
483 LEAVE_CRITICAL_SECTION(&key_lock);
489 // Returns number of times key has went from down to up since last call.
490 int key_up_count(int keycode)
493 SDL_Scancode scancode;
498 scancode = SDL_GetScancodeFromKey(keycode & KEY_MASK);
500 if (scancode == SDL_SCANCODE_UNKNOWN)
503 ENTER_CRITICAL_SECTION(&key_lock);
505 n = key_data.NumUps[scancode];
506 key_data.NumUps[scancode] = 0;
508 LEAVE_CRITICAL_SECTION(&key_lock);
513 int key_check(int keycode)
515 SDL_Scancode scancode = SDL_GetScancodeFromKey(keycode & KEY_MASK);
517 return key_data.down_check[scancode];
520 // Add a key up or down code to the key buffer. state=1 -> down, state=0 -> up
521 // latency => time difference in ms between when key was actually pressed and now
522 //void key_mark( uint code, int state )
523 void key_mark(SDL_Scancode scancode, int state, ushort kmod, uint latency )
525 uint breakbit, temp, event_time;
528 if ( !key_inited ) return;
530 ENTER_CRITICAL_SECTION(&key_lock);
532 Assert( scancode < SDL_NUM_SCANCODES );
534 event_time = timer_get_milliseconds() - latency;
535 // event_time = timeGetTime() - latency;
541 keyd_last_released = scancode;
542 keyd_pressed[scancode] = false;
543 key_data.NumUps[scancode]++;
545 if (event_time < key_data.TimeKeyWentDown[scancode])
546 key_data.TimeKeyHeldDown[scancode] = 0;
548 key_data.TimeKeyHeldDown[scancode] += event_time - key_data.TimeKeyWentDown[scancode];
551 keyd_last_pressed = scancode;
552 keyd_time_when_last_pressed = event_time;
553 if (!keyd_pressed[scancode]) {
555 key_data.TimeKeyWentDown[scancode] = event_time;
556 keyd_pressed[scancode] = true;
557 key_data.NumDowns[scancode]++;
558 key_data.down_check[scancode]++;
560 // mprintf(( "Scancode = %x\n", scancode ));
562 // if ( scancode == KEY_BREAK )
566 keycode = (unsigned short)scancode;
568 if (kmod & KMOD_SHIFT)
569 keycode |= KEY_SHIFTED;
572 keycode |= KEY_ALTED;
574 if (kmod & KMOD_CTRL)
575 keycode |= KEY_CTRLED;
578 if ( key_pressed(KEY_DEBUG_KEY) )
579 keycode |= KEY_DEBUGGED;
581 // if ( keycode == (KEY_BACKSP + KEY_DEBUGGED) ) {
583 // keyd_pressed[KEY_DEBUG_KEY] = 0;
584 // keyd_pressed[KEY_BACKSP] = 0;
588 if ( keyd_pressed(KEY_DEBUG_KEY) ) {
589 mprintf(("Cheats_enabled = %i, Key_normal_game = %i\n", Cheats_enabled, Key_normal_game));
590 if (Cheats_enabled && Key_normal_game) {
591 keycode |= KEY_DEBUGGED1;
598 temp = key_data.keytail+1;
599 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
601 if (temp!=key_data.keyhead) {
602 int i, accept_key = 1;
603 // Num_filter_keys will only be non-zero when a key filter has
604 // been explicity set up via key_set_filter()
605 for ( i = 0; i < Num_filter_keys; i++ ) {
607 if ( Key_filter[i] == keycode ) {
614 key_data.keybuffer[key_data.keytail] = keycode;
615 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
616 key_data.keytail = temp;
622 LEAVE_CRITICAL_SECTION(&key_lock);
630 if ( Key_numlock_was_on ) {
631 key_turn_on_numlock();
632 Key_numlock_was_on = 0;
646 keyd_time_when_last_pressed = timer_get_milliseconds();
647 keyd_buffer_type = 1;
649 // Clear the keyboard array
658 void key_level_init()
662 for (i=0; i<SDL_NUM_SCANCODES; i++)
663 key_data.down_check[i] = 0;
666 void key_lost_focus()
682 // Restricts the keys that are accepted from the keyboard
684 // filter_array => array of keys to act as a filter
685 // num => number of keys in filter_array
687 void key_set_filter(int *filter_array, int num)
691 if ( num >= MAX_FILTER_KEYS ) {
693 num = MAX_FILTER_KEYS;
696 Num_filter_keys = num;
698 for ( i = 0; i < num; i++ ) {
699 Key_filter[i] = filter_array[i];
703 // Clear the key filter, so all keypresses are accepted from keyboard
705 void key_clear_filter()
710 for ( i = 0; i < MAX_FILTER_KEYS; i++ ) {