This commit was generated by cvs2svn to compensate for changes in r2,
[btb/d2x.git] / input / sdl / key.c
1 // SDL keyboard input support
2
3 #include <conf.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6
7 #include <SDL/SDL.h>
8
9 #include "event.h"
10 #include "error.h"
11 #include "key.h"
12 #include "timer.h"
13
14 //added on 9/3/98 by Matt Mueller to free some cpu instead of hogging during menus and such
15 #include "d_delay.h"
16 //end this section addition - Matt Mueller
17
18 #define KEY_BUFFER_SIZE 16
19
20 static unsigned char Installed = 0;
21
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;
30
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
39 } Key_info;
40
41 typedef struct keyboard {
42         unsigned short          keybuffer[KEY_BUFFER_SIZE];
43         Key_info                keys[256];
44         fix                     time_pressed[KEY_BUFFER_SIZE];
45         unsigned int            keyhead, keytail;
46 } keyboard;
47
48 static keyboard key_data;
49
50 typedef struct key_props {
51         char *key_text;
52         unsigned char ascii_value;
53         unsigned char shifted_ascii_value;
54         SDLKey sym;
55 } key_props;
56
57 key_props key_properties[256] = {
58 { "",       255,    255,    -1                 },
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        },
88 //end edit -MM
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         },
101 //end edit -MM
102 { "'",      '\'',   '"',    SDLK_QUOTE         },
103 //edited 06/08/99 Matt Mueller - set to correct sym
104 { "`",      '`',    '~',    SDLK_BACKQUOTE     },
105 //end edit -MM
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  },
119 //end edit -MM
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                 },
165 //end edit -MM
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      },
226 //end edit -MM
227 //edited 06/08/99 Matt Mueller - set to correct sym
228 { "RCTRL",  255,    255,    SDLK_RCTRL            },
229 //end edit -MM
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                 },
257 //end edit -MM
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            },
276 //end edit -MM
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          },
281 //end edit -MM
282 { "",       255,    255,    -1                 },
283 //edited 06/08/99 Matt Mueller - set to correct key_text
284 { "RIGHT",      255,    255,    SDLK_RIGHT         },
285 //end edit -MM
286 { "",       255,    255,    -1                 },
287 //edited 06/08/99 Matt Mueller - set to correct key_text
288 { "END",    255,    255,    SDLK_END           },
289 //end edit -MM
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                 },
338 };
339
340 char *key_text[256];
341
342 void key_buid_key_text(void)
343 {
344 }
345
346 unsigned char key_to_ascii(int keycode )
347 {
348         int shifted;
349
350         shifted = keycode & KEY_SHIFTED;
351         keycode &= 0xFF;
352
353         if (shifted)
354                 return key_properties[keycode].shifted_ascii_value;
355         else
356                 return key_properties[keycode].ascii_value;
357 }
358
359 void key_handler(SDL_KeyboardEvent *event)
360 {
361         ubyte state;
362         int i, keycode, event_key, key_state;
363         Key_info *key;
364         unsigned char temp;
365
366         event_key = event->keysym.sym;
367
368         key_state = (event->state == SDL_PRESSED); //  !(wInfo & KF_UP);
369         //=====================================================
370         //Here a translation from win keycodes to mac keycodes!
371         //=====================================================
372
373         for (i = 255; i >= 0; i--) {
374
375                 keycode = i;
376                 key = &(key_data.keys[keycode]);
377                 if (key_properties[i].sym == event_key)
378                         state = key_state;
379                 else
380                         state = key->last_state;
381                         
382                 if ( key->last_state == state ) {
383                         if (state) {
384                                 key->counter++;
385                                 keyd_last_pressed = keycode;
386                                 keyd_time_when_last_pressed = timer_get_fixed_seconds();
387                         }
388                 } else {
389                         if (state)      {
390                                 keyd_last_pressed = keycode;
391                                 keyd_pressed[keycode] = 1;
392                                 key->downcount += state;
393                                 key->state = 1;
394                                 key->timewentdown = keyd_time_when_last_pressed = timer_get_fixed_seconds();
395                                 key->counter++;
396                         } else {        
397                                 keyd_pressed[keycode] = 0;
398                                 keyd_last_released = keycode;
399                                 key->upcount += key->state;
400                                 key->state = 0;
401                                 key->counter = 0;
402                                 key->timehelddown += timer_get_fixed_seconds() - key->timewentdown;
403                         }
404                 }
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;
420                         }
421                 }
422                 key->last_state = state;
423         }
424 }
425
426 void key_close()
427 {
428  Installed = 0;
429 }
430
431 void key_init()
432 {
433   int i;
434   
435   if (Installed) return;
436
437   Installed=1;
438
439   keyd_time_when_last_pressed = timer_get_fixed_seconds();
440   keyd_buffer_type = 1;
441   keyd_repeat = 1;
442   
443   for(i=0; i<256; i++)
444      key_text[i] = key_properties[i].key_text;
445      
446   // Clear the keyboard array
447   key_flush();
448   atexit(key_close);
449 }
450
451 void key_flush()
452 {
453         int i;
454         fix curtime;
455
456         if (!Installed)
457                 key_init();
458
459         key_data.keyhead = key_data.keytail = 0;
460
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;
465         }
466
467 //use gettimeofday here:
468         curtime = timer_get_fixed_seconds();
469
470         for (i=0; i<256; i++ )  {
471                 keyd_pressed[i] = 0;
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;
479         }
480 }
481
482 int add_one(int n)
483 {
484  n++;
485  if ( n >= KEY_BUFFER_SIZE ) n=0;
486  return n;
487 }
488
489 int key_checkch()
490 {
491         int is_one_waiting = 0;
492         event_poll();
493         if (key_data.keytail!=key_data.keyhead)
494                 is_one_waiting = 1;
495         return is_one_waiting;
496 }
497
498 int key_inkey()
499 {
500         int key = 0;
501         if (!Installed)
502                 key_init();
503         event_poll();
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);
507         }
508 //added 9/3/98 by Matt Mueller to free cpu time instead of hogging during menus and such
509         else d_delay(1);
510 //end addition - Matt Mueller
511              
512         return key;
513 }
514
515 int key_inkey_time(fix * time)
516 {
517         int key = 0;
518
519         if (!Installed)
520                 key_init();
521         event_poll();
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);
526         }
527         return key;
528 }
529
530 int key_peekkey()
531 {
532         int key = 0;
533         event_poll();
534         if (key_data.keytail!=key_data.keyhead)
535                 key = key_data.keybuffer[key_data.keyhead];
536
537         return key;
538 }
539
540 int key_getch()
541 {
542         int dummy=0;
543
544         if (!Installed)
545                 return 0;
546 //              return getch();
547
548         while (!key_checkch())
549                 dummy++;
550         return key_inkey();
551 }
552
553 unsigned int key_get_shift_status()
554 {
555         unsigned int shift_status = 0;
556
557         if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
558                 shift_status |= KEY_SHIFTED;
559
560         if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
561                 shift_status |= KEY_ALTED;
562
563         if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
564                 shift_status |= KEY_CTRLED;
565
566 #ifndef NDEBUG
567         if (keyd_pressed[KEY_DELETE])
568                 shift_status |=KEY_DEBUGGED;
569 #endif
570
571         return shift_status;
572 }
573
574 // Returns the number of seconds this key has been down since last call.
575 fix key_down_time(int scancode)
576 {
577         fix time_down, time;
578
579         event_poll();
580         if ((scancode<0)|| (scancode>255)) return 0;
581
582         if (!keyd_pressed[scancode]) {
583                 time_down = key_data.keys[scancode].timehelddown;
584                 key_data.keys[scancode].timehelddown = 0;
585         } else {
586                 time = timer_get_fixed_seconds();
587                 time_down = time - key_data.keys[scancode].timewentdown;
588                 key_data.keys[scancode].timewentdown = time;
589         }
590
591         return time_down;
592 }
593
594 unsigned int key_down_count(int scancode)
595 {
596         int n;
597         event_poll();
598         if ((scancode<0)|| (scancode>255)) return 0;
599
600         n = key_data.keys[scancode].downcount;
601         key_data.keys[scancode].downcount = 0;
602
603         return n;
604 }
605
606 unsigned int key_up_count(int scancode)
607 {
608         int n;
609         event_poll();
610         if ((scancode<0)|| (scancode>255)) return 0;
611
612         n = key_data.keys[scancode].upcount;
613         key_data.keys[scancode].upcount = 0;
614
615         return n;
616 }