1 // SDL keyboard input support
14 //added on 9/3/98 by Matt Mueller to free some cpu instead of hogging during menus and such
16 //end this section addition - Matt Mueller
18 #define KEY_BUFFER_SIZE 16
20 static unsigned char Installed = 0;
22 //-------- Variable accessed by outside functions ---------
23 unsigned char keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
24 unsigned char keyd_repeat;
25 unsigned char keyd_editor_mode;
26 volatile unsigned char keyd_last_pressed;
27 volatile unsigned char keyd_last_released;
28 volatile unsigned char keyd_pressed[256];
29 volatile int keyd_time_when_last_pressed;
31 typedef struct Key_info {
32 ubyte state; // state of key 1 == down, 0 == up
33 ubyte last_state; // previous state of key
34 int counter; // incremented each time key is down in handler
35 fix timewentdown; // simple counter incremented each time in interrupt and key is down
36 fix timehelddown; // counter to tell how long key is down -- gets reset to 0 by key routines
37 ubyte downcount; // number of key counts key was down
38 ubyte upcount; // number of times key was released
41 typedef struct keyboard {
42 unsigned short keybuffer[KEY_BUFFER_SIZE];
44 fix time_pressed[KEY_BUFFER_SIZE];
45 unsigned int keyhead, keytail;
48 static keyboard key_data;
50 typedef struct key_props {
52 unsigned char ascii_value;
53 unsigned char shifted_ascii_value;
57 key_props key_properties[256] = {
59 { "ESC", 255, 255, SDLK_ESCAPE },
60 { "1", '1', '!', SDLK_1 },
61 { "2", '2', '@', SDLK_2 },
62 { "3", '3', '#', SDLK_3 },
63 { "4", '4', '$', SDLK_4 },
64 { "5", '5', '%', SDLK_5 },
65 { "6", '6', '^', SDLK_6 },
66 { "7", '7', '&', SDLK_7 },
67 { "8", '8', '*', SDLK_8 },
68 { "9", '9', '(', SDLK_9 },
69 { "0", '0', ')', SDLK_0 },
70 { "-", '-', '_', SDLK_MINUS },
71 { "=", '=', '+', SDLK_EQUALS },
72 { "BSPC", 255, 255, SDLK_BACKSPACE },
73 { "TAB", 255, 255, SDLK_TAB },
74 { "Q", 'q', 'Q', SDLK_q },
75 { "W", 'w', 'W', SDLK_w },
76 { "E", 'e', 'E', SDLK_e },
77 { "R", 'r', 'R', SDLK_r },
78 { "T", 't', 'T', SDLK_t },
79 { "Y", 'y', 'Y', SDLK_y },
80 { "U", 'u', 'U', SDLK_u },
81 { "I", 'i', 'I', SDLK_i },
82 { "O", 'o', 'O', SDLK_o },
83 { "P", 'p', 'P', SDLK_p },
84 { "[", '[', '{', SDLK_LEFTBRACKET },
85 { "]", ']', '}', SDLK_RIGHTBRACKET },
86 //edited 06/08/99 Matt Mueller - set to correct key_text
87 { "
\83", 255, 255, SDLK_RETURN },
89 { "LCTRL", 255, 255, SDLK_LCTRL },
90 { "A", 'a', 'A', SDLK_a },
91 { "S", 's', 'S', SDLK_s },
92 { "D", 'd', 'D', SDLK_d },
93 { "F", 'f', 'F', SDLK_f },
94 { "G", 'g', 'G', SDLK_g },
95 { "H", 'h', 'H', SDLK_h },
96 { "J", 'j', 'J', SDLK_j },
97 { "K", 'k', 'K', SDLK_k },
98 { "L", 'l', 'L', SDLK_l },
99 //edited 06/08/99 Matt Mueller - set to correct sym
100 { ";", ';', ':', SDLK_SEMICOLON },
102 { "'", '\'', '"', SDLK_QUOTE },
103 //edited 06/08/99 Matt Mueller - set to correct sym
104 { "`", '`', '~', SDLK_BACKQUOTE },
106 { "LSHFT", 255, 255, SDLK_LSHIFT },
107 { "\\", '\\', '|', SDLK_BACKSLASH },
108 { "Z", 'z', 'Z', SDLK_z },
109 { "X", 'x', 'X', SDLK_x },
110 { "C", 'c', 'C', SDLK_c },
111 { "V", 'v', 'V', SDLK_v },
112 { "B", 'b', 'B', SDLK_b },
113 { "N", 'n', 'N', SDLK_n },
114 { "M", 'm', 'M', SDLK_m },
115 //edited 06/08/99 Matt Mueller - set to correct syms
116 { ",", ',', '<', SDLK_COMMA },
117 { ".", '.', '>', SDLK_PERIOD },
118 { "/", '/', '?', SDLK_SLASH },
120 { "RSHFT", 255, 255, SDLK_RSHIFT },
121 { "PAD*", '*', 255, SDLK_KP_MULTIPLY },
122 { "LALT", 255, 255, SDLK_LALT },
123 { "SPC", ' ', ' ', SDLK_SPACE },
124 { "CPSLK", 255, 255, SDLK_CAPSLOCK },
125 { "F1", 255, 255, SDLK_F1 },
126 { "F2", 255, 255, SDLK_F2 },
127 { "F3", 255, 255, SDLK_F3 },
128 { "F4", 255, 255, SDLK_F4 },
129 { "F5", 255, 255, SDLK_F5 },
130 { "F6", 255, 255, SDLK_F6 },
131 { "F7", 255, 255, SDLK_F7 },
132 { "F8", 255, 255, SDLK_F8 },
133 { "F9", 255, 255, SDLK_F9 },
134 { "F10", 255, 255, SDLK_F10 },
135 { "NMLCK", 255, 255, SDLK_NUMLOCK },
136 { "SCLK", 255, 255, SDLK_SCROLLOCK },
137 { "PAD7", 255, 255, SDLK_KP7 },
138 { "PAD8", 255, 255, SDLK_KP8 },
139 { "PAD9", 255, 255, SDLK_KP9 },
140 { "PAD-", 255, 255, SDLK_KP_MINUS },
141 { "PAD4", 255, 255, SDLK_KP4 },
142 { "PAD5", 255, 255, SDLK_KP5 },
143 { "PAD6", 255, 255, SDLK_KP6 },
144 { "PAD+", 255, 255, SDLK_KP_PLUS },
145 { "PAD1", 255, 255, SDLK_KP1 },
146 { "PAD2", 255, 255, SDLK_KP2 },
147 { "PAD3", 255, 255, SDLK_KP3 },
148 { "PAD0", 255, 255, SDLK_KP0 },
149 { "PAD.", 255, 255, SDLK_KP_PERIOD },
150 { "", 255, 255, -1 },
151 { "", 255, 255, -1 },
152 { "", 255, 255, -1 },
153 { "F11", 255, 255, SDLK_F11 },
154 { "F12", 255, 255, SDLK_F12 },
155 { "", 255, 255, -1 },
156 { "", 255, 255, -1 },
157 { "", 255, 255, -1 },
158 { "", 255, 255, -1 },
159 { "", 255, 255, -1 },
160 { "", 255, 255, -1 },
161 { "", 255, 255, -1 },
162 { "", 255, 255, -1 },
163 //edited 06/08/99 Matt Mueller - add pause ability
164 { "PAUSE", 255, 255, SDLK_PAUSE },
166 { "", 255, 255, -1 },
167 { "", 255, 255, -1 },
168 { "", 255, 255, -1 },
169 { "", 255, 255, -1 },
170 { "", 255, 255, -1 },
171 { "", 255, 255, -1 },
172 { "", 255, 255, -1 },
173 { "", 255, 255, -1 },
174 { "", 255, 255, -1 },
175 { "", 255, 255, -1 },
176 { "", 255, 255, -1 },
177 { "", 255, 255, -1 },
178 { "", 255, 255, -1 },
179 { "", 255, 255, -1 },
180 { "", 255, 255, -1 },
181 { "", 255, 255, -1 },
182 { "", 255, 255, -1 },
183 { "", 255, 255, -1 },
184 { "", 255, 255, -1 },
185 { "", 255, 255, -1 },
186 { "", 255, 255, -1 },
187 { "", 255, 255, -1 },
188 { "", 255, 255, -1 },
189 { "", 255, 255, -1 },
190 { "", 255, 255, -1 },
191 { "", 255, 255, -1 },
192 { "", 255, 255, -1 },
193 { "", 255, 255, -1 },
194 { "", 255, 255, -1 },
195 { "", 255, 255, -1 },
196 { "", 255, 255, -1 },
197 { "", 255, 255, -1 },
198 { "", 255, 255, -1 },
199 { "", 255, 255, -1 },
200 { "", 255, 255, -1 },
201 { "", 255, 255, -1 },
202 { "", 255, 255, -1 },
203 { "", 255, 255, -1 },
204 { "", 255, 255, -1 },
205 { "", 255, 255, -1 },
206 { "", 255, 255, -1 },
207 { "", 255, 255, -1 },
208 { "", 255, 255, -1 },
209 { "", 255, 255, -1 },
210 { "", 255, 255, -1 },
211 { "", 255, 255, -1 },
212 { "", 255, 255, -1 },
213 { "", 255, 255, -1 },
214 { "", 255, 255, -1 },
215 { "", 255, 255, -1 },
216 { "", 255, 255, -1 },
217 { "", 255, 255, -1 },
218 { "", 255, 255, -1 },
219 { "", 255, 255, -1 },
220 { "", 255, 255, -1 },
221 { "", 255, 255, -1 },
222 { "", 255, 255, -1 },
223 { "", 255, 255, -1 },
224 //edited 06/08/99 Matt Mueller - set to correct key_text
225 { "PAD
\83", 255, 255, SDLK_KP_ENTER },
227 //edited 06/08/99 Matt Mueller - set to correct sym
228 { "RCTRL", 255, 255, SDLK_RCTRL },
230 { "", 255, 255, -1 },
231 { "", 255, 255, -1 },
232 { "", 255, 255, -1 },
233 { "", 255, 255, -1 },
234 { "", 255, 255, -1 },
235 { "", 255, 255, -1 },
236 { "", 255, 255, -1 },
237 { "", 255, 255, -1 },
238 { "", 255, 255, -1 },
239 { "", 255, 255, -1 },
240 { "", 255, 255, -1 },
241 { "", 255, 255, -1 },
242 { "", 255, 255, -1 },
243 { "", 255, 255, -1 },
244 { "", 255, 255, -1 },
245 { "", 255, 255, -1 },
246 { "", 255, 255, -1 },
247 { "", 255, 255, -1 },
248 { "", 255, 255, -1 },
249 { "", 255, 255, -1 },
250 { "", 255, 255, -1 },
251 { "", 255, 255, -1 },
252 { "", 255, 255, -1 },
253 { "PAD/", 255, 255, SDLK_KP_DIVIDE },
254 { "", 255, 255, -1 },
255 //edited 06/08/99 Matt Mueller - add printscreen ability
256 { "PRSCR", 255, 255, SDLK_PRINT },
258 { "RALT", 255, 255, SDLK_RALT },
259 { "", 255, 255, -1 },
260 { "", 255, 255, -1 },
261 { "", 255, 255, -1 },
262 { "", 255, 255, -1 },
263 { "", 255, 255, -1 },
264 { "", 255, 255, -1 },
265 { "", 255, 255, -1 },
266 { "", 255, 255, -1 },
267 { "", 255, 255, -1 },
268 { "", 255, 255, -1 },
269 { "", 255, 255, -1 },
270 { "", 255, 255, -1 },
271 { "", 255, 255, -1 },
272 { "", 255, 255, -1 },
273 { "HOME", 255, 255, SDLK_HOME },
274 //edited 06/08/99 Matt Mueller - set to correct key_text
275 { "UP", 255, 255, SDLK_UP },
277 { "PGUP", 255, 255, SDLK_PAGEUP },
278 { "", 255, 255, -1 },
279 //edited 06/08/99 Matt Mueller - set to correct key_text
280 { "LEFT", 255, 255, SDLK_LEFT },
282 { "", 255, 255, -1 },
283 //edited 06/08/99 Matt Mueller - set to correct key_text
284 { "RIGHT", 255, 255, SDLK_RIGHT },
286 { "", 255, 255, -1 },
287 //edited 06/08/99 Matt Mueller - set to correct key_text
288 { "END", 255, 255, SDLK_END },
290 { "DOWN", 255, 255, SDLK_DOWN },
291 { "PGDN", 255, 255, SDLK_PAGEDOWN },
292 { "INS", 255, 255, SDLK_INSERT },
293 { "DEL", 255, 255, SDLK_DELETE },
294 { "", 255, 255, -1 },
295 { "", 255, 255, -1 },
296 { "", 255, 255, -1 },
297 { "", 255, 255, -1 },
298 { "", 255, 255, -1 },
299 { "", 255, 255, -1 },
300 { "", 255, 255, -1 },
301 { "", 255, 255, -1 },
302 { "", 255, 255, -1 },
303 { "", 255, 255, -1 },
304 { "", 255, 255, -1 },
305 { "", 255, 255, -1 },
306 { "", 255, 255, -1 },
307 { "", 255, 255, -1 },
308 { "", 255, 255, -1 },
309 { "", 255, 255, -1 },
310 { "", 255, 255, -1 },
311 { "", 255, 255, -1 },
312 { "", 255, 255, -1 },
313 { "", 255, 255, -1 },
314 { "", 255, 255, -1 },
315 { "", 255, 255, -1 },
316 { "", 255, 255, -1 },
317 { "", 255, 255, -1 },
318 { "", 255, 255, -1 },
319 { "", 255, 255, -1 },
320 { "", 255, 255, -1 },
321 { "", 255, 255, -1 },
322 { "", 255, 255, -1 },
323 { "", 255, 255, -1 },
324 { "", 255, 255, -1 },
325 { "", 255, 255, -1 },
326 { "", 255, 255, -1 },
327 { "", 255, 255, -1 },
328 { "", 255, 255, -1 },
329 { "", 255, 255, -1 },
330 { "", 255, 255, -1 },
331 { "", 255, 255, -1 },
332 { "", 255, 255, -1 },
333 { "", 255, 255, -1 },
334 { "", 255, 255, -1 },
335 { "", 255, 255, -1 },
336 { "", 255, 255, -1 },
337 { "", 255, 255, -1 },
342 void key_buid_key_text(void)
346 unsigned char key_to_ascii(int keycode )
350 shifted = keycode & KEY_SHIFTED;
354 return key_properties[keycode].shifted_ascii_value;
356 return key_properties[keycode].ascii_value;
359 void key_handler(SDL_KeyboardEvent *event)
362 int i, keycode, event_key, key_state;
366 event_key = event->keysym.sym;
368 key_state = (event->state == SDL_PRESSED); // !(wInfo & KF_UP);
369 //=====================================================
370 //Here a translation from win keycodes to mac keycodes!
371 //=====================================================
373 for (i = 255; i >= 0; i--) {
376 key = &(key_data.keys[keycode]);
377 if (key_properties[i].sym == event_key)
380 state = key->last_state;
382 if ( key->last_state == state ) {
385 keyd_last_pressed = keycode;
386 keyd_time_when_last_pressed = timer_get_fixed_seconds();
390 keyd_last_pressed = keycode;
391 keyd_pressed[keycode] = 1;
392 key->downcount += state;
394 key->timewentdown = keyd_time_when_last_pressed = timer_get_fixed_seconds();
397 keyd_pressed[keycode] = 0;
398 keyd_last_released = keycode;
399 key->upcount += key->state;
402 key->timehelddown += timer_get_fixed_seconds() - key->timewentdown;
405 if ( (state && !key->last_state) || (state && key->last_state && (key->counter > 30) && (key->counter & 0x01)) ) {
406 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT])
407 keycode |= KEY_SHIFTED;
408 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT])
409 keycode |= KEY_ALTED;
410 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL])
411 keycode |= KEY_CTRLED;
412 if ( keyd_pressed[KEY_DELETE] )
413 keycode |= KEY_DEBUGGED;
414 temp = key_data.keytail+1;
415 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
416 if (temp!=key_data.keyhead) {
417 key_data.keybuffer[key_data.keytail] = keycode;
418 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
419 key_data.keytail = temp;
422 key->last_state = state;
435 if (Installed) return;
439 keyd_time_when_last_pressed = timer_get_fixed_seconds();
440 keyd_buffer_type = 1;
444 key_text[i] = key_properties[i].key_text;
446 // Clear the keyboard array
459 key_data.keyhead = key_data.keytail = 0;
461 //Clear the keyboard buffer
462 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
463 key_data.keybuffer[i] = 0;
464 key_data.time_pressed[i] = 0;
467 //use gettimeofday here:
468 curtime = timer_get_fixed_seconds();
470 for (i=0; i<256; i++ ) {
472 key_data.keys[i].state = 1;
473 key_data.keys[i].last_state = 0;
474 key_data.keys[i].timewentdown = curtime;
475 key_data.keys[i].downcount=0;
476 key_data.keys[i].upcount=0;
477 key_data.keys[i].timehelddown = 0;
478 key_data.keys[i].counter = 0;
485 if ( n >= KEY_BUFFER_SIZE ) n=0;
491 int is_one_waiting = 0;
493 if (key_data.keytail!=key_data.keyhead)
495 return is_one_waiting;
504 if (key_data.keytail!=key_data.keyhead) {
505 key = key_data.keybuffer[key_data.keyhead];
506 key_data.keyhead = add_one(key_data.keyhead);
508 //added 9/3/98 by Matt Mueller to free cpu time instead of hogging during menus and such
510 //end addition - Matt Mueller
515 int key_inkey_time(fix * time)
522 if (key_data.keytail!=key_data.keyhead) {
523 key = key_data.keybuffer[key_data.keyhead];
524 *time = key_data.time_pressed[key_data.keyhead];
525 key_data.keyhead = add_one(key_data.keyhead);
534 if (key_data.keytail!=key_data.keyhead)
535 key = key_data.keybuffer[key_data.keyhead];
548 while (!key_checkch())
553 unsigned int key_get_shift_status()
555 unsigned int shift_status = 0;
557 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
558 shift_status |= KEY_SHIFTED;
560 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
561 shift_status |= KEY_ALTED;
563 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
564 shift_status |= KEY_CTRLED;
567 if (keyd_pressed[KEY_DELETE])
568 shift_status |=KEY_DEBUGGED;
574 // Returns the number of seconds this key has been down since last call.
575 fix key_down_time(int scancode)
580 if ((scancode<0)|| (scancode>255)) return 0;
582 if (!keyd_pressed[scancode]) {
583 time_down = key_data.keys[scancode].timehelddown;
584 key_data.keys[scancode].timehelddown = 0;
586 time = timer_get_fixed_seconds();
587 time_down = time - key_data.keys[scancode].timewentdown;
588 key_data.keys[scancode].timewentdown = time;
594 unsigned int key_down_count(int scancode)
598 if ((scancode<0)|| (scancode>255)) return 0;
600 n = key_data.keys[scancode].downcount;
601 key_data.keys[scancode].downcount = 0;
606 unsigned int key_up_count(int scancode)
610 if ((scancode<0)|| (scancode>255)) return 0;
612 n = key_data.keys[scancode].upcount;
613 key_data.keys[scancode].upcount = 0;