]> icculus.org git repositories - btb/d2x.git/blob - input/ggi/key.c
Got rid of all compiler warnings, for non-OpenGL on linux, anyway...
[btb/d2x.git] / input / ggi / key.c
1 #include <conf.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include <ggi/gii.h> 
6
7 #include "event.h"
8 #include "error.h"
9 #include "key.h"
10 #include "timer.h"
11 #include "mono.h"
12
13 //added on 9/3/98 by Matt Mueller to free some cpu instead of hogging during menus and such
14 #include "d_delay.h"
15 //end this section addition - Matt Mueller
16
17 #define KEY_BUFFER_SIZE 16
18
19 static unsigned char Installed = 0;
20
21 //-------- Variable accessed by outside functions ---------
22 unsigned char           keyd_buffer_type;               // 0=No buffer, 1=buffer ASCII, 2=buffer scans
23 unsigned char           keyd_repeat;
24 unsigned char           keyd_editor_mode;
25 volatile unsigned char  keyd_last_pressed;
26 volatile unsigned char  keyd_last_released;
27 volatile unsigned char  keyd_pressed[256];
28 volatile int            keyd_time_when_last_pressed;
29
30 typedef struct Key_info {
31         ubyte           state;                  // state of key 1 == down, 0 == up
32         ubyte           last_state;             // previous state of key
33         int             counter;                // incremented each time key is down in handler
34         fix             timewentdown;   // simple counter incremented each time in interrupt and key is down
35         fix             timehelddown;   // counter to tell how long key is down -- gets reset to 0 by key routines
36         ubyte           downcount;              // number of key counts key was down
37         ubyte           upcount;                // number of times key was released
38 } Key_info;
39
40 typedef struct keyboard {
41         unsigned short          keybuffer[KEY_BUFFER_SIZE];
42         Key_info                keys[256];
43         fix                     time_pressed[KEY_BUFFER_SIZE];
44         unsigned int            keyhead, keytail;
45 } keyboard;
46
47 static /*volatile*/ keyboard key_data;
48
49 char * key_text[256] = {
50 "","ESC","1","2","3","4","5","6","7","8","9","0","-",
51 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",
52 "P","[","]","\83","LCTRL","A","S","D","F",
53 "G","H","J","K","L",";","'","`",
54 "LSHFT","\\","Z","X","C","V","B","N","M",",",
55 ".","/","RSHFT","PAD*","LALT","SPC",
56 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",
57 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",
58 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0",
59 "PAD.","","","","F11","F12","","","","","","","","","",
60 "","","","","","","","","","","","","","","","","","","","",
61 "","","","","","","","","","","","","","","","","","","","",
62 "","","","","","","","","","","","","","","","","","",
63 "PAD\83","RCTRL","","","","","","","","","","","","","",
64 "","","","","","","","","","","PAD/","","","RALT","",
65 "","","","","","","","","","","","","","HOME","\82","PGUP",
66 "","\81","","\7f","","END","\80","PGDN","INS",
67 "DEL","","","","","","","","","","","","","","","","","",
68 "","","","","","","","","","","","","","","","","","","","",
69 "","","","","","","" };
70
71 unsigned char ascii_table[128] = 
72 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
73   'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
74   'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
75   255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
76   255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
77   255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
78   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
79   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
80   255,255,255,255,255,255,255,255 };
81
82 unsigned char shifted_ascii_table[128] = 
83 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
84   'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
85   'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 
86   255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
87   255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
88   255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
89   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
90   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
91   255,255,255,255,255,255,255,255 };
92
93 int giiKeyTranslate (int keylabel) {
94  switch (keylabel)
95  {
96   case GIIUC_0: return KEY_0;
97   case GIIUC_1: return KEY_1;
98   case GIIUC_2: return KEY_2;
99   case GIIUC_3: return KEY_3;
100   case GIIUC_4: return KEY_4;
101   case GIIUC_5: return KEY_5;
102   case GIIUC_6: return KEY_6;
103   case GIIUC_7: return KEY_7;
104   case GIIUC_8: return KEY_8;
105   case GIIUC_9: return KEY_9;
106
107   case GIIUC_A: return KEY_A;
108   case GIIUC_B: return KEY_B;
109   case GIIUC_C: return KEY_C;
110   case GIIUC_D: return KEY_D;
111   case GIIUC_E: return KEY_E;
112   case GIIUC_F: return KEY_F;
113   case GIIUC_G: return KEY_G;
114   case GIIUC_H: return KEY_H;
115   case GIIUC_I: return KEY_I;
116   case GIIUC_J: return KEY_J;
117   case GIIUC_K: return KEY_K;
118   case GIIUC_L: return KEY_L;
119   case GIIUC_M: return KEY_M;
120   case GIIUC_N: return KEY_N;
121   case GIIUC_O: return KEY_O;
122   case GIIUC_P: return KEY_P;
123   case GIIUC_Q: return KEY_Q;
124   case GIIUC_R: return KEY_R;
125   case GIIUC_S: return KEY_S;
126   case GIIUC_T: return KEY_T;
127   case GIIUC_U: return KEY_U;
128   case GIIUC_V: return KEY_V;
129   case GIIUC_W: return KEY_W;
130   case GIIUC_X: return KEY_X;
131   case GIIUC_Y: return KEY_Y;
132   case GIIUC_Z: return KEY_Z;
133
134   case GIIUC_Minus: return KEY_MINUS;
135   case GIIUC_Equal: return KEY_EQUAL;
136   case GIIUC_Slash: return KEY_DIVIDE;
137   case GIIUC_BackSlash: return KEY_SLASH;
138   case GIIUC_Comma: return KEY_COMMA;
139   case GIIUC_Period: return KEY_PERIOD;
140   case GIIUC_Semicolon: return KEY_SEMICOL;
141
142   case GIIUC_BracketLeft: return KEY_LBRACKET;
143   case GIIUC_BracketRight: return KEY_RBRACKET;
144  
145   case GIIUC_Apostrophe: return KEY_RAPOSTRO;
146   case GIIUC_Grave:  return KEY_LAPOSTRO;
147
148   case GIIUC_Escape: return KEY_ESC;
149   case GIIK_Enter: return KEY_ENTER;
150   case GIIUC_BackSpace: return KEY_BACKSP;
151   case GIIUC_Tab: return KEY_TAB;
152   case GIIUC_Space: return KEY_SPACEBAR;
153
154   case GIIK_NumLock: return KEY_NUMLOCK;
155   case GIIK_ScrollLock: return KEY_SCROLLOCK;
156   case GIIK_CapsLock: return KEY_CAPSLOCK;
157
158   case GIIK_ShiftL: return KEY_LSHIFT;
159   case GIIK_ShiftR: return KEY_RSHIFT;
160
161   case GIIK_AltL: return KEY_LALT;
162   case GIIK_AltR: return KEY_RALT;
163
164   case GIIK_CtrlL: return KEY_LCTRL;
165   case GIIK_CtrlR: return KEY_RCTRL;
166
167   case GIIK_F1: return KEY_F1;
168   case GIIK_F2: return KEY_F2;
169   case GIIK_F3: return KEY_F3;
170   case GIIK_F4: return KEY_F4;
171   case GIIK_F5: return KEY_F5;
172   case GIIK_F6: return KEY_F6;
173   case GIIK_F7: return KEY_F7;
174   case GIIK_F8: return KEY_F8;
175   case GIIK_F9: return KEY_F9;
176   case GIIK_F10: return KEY_F10;
177   case GIIK_F11: return KEY_F11;
178   case GIIK_F12: return KEY_F12;
179
180   case GIIK_P0: return KEY_PAD0;
181   case GIIK_P1: return KEY_PAD1;
182   case GIIK_P2: return KEY_PAD2;
183   case GIIK_P3: return KEY_PAD3;
184   case GIIK_P4: return KEY_PAD4;
185   case GIIK_P5: return KEY_PAD5;
186   case GIIK_P6: return KEY_PAD6;
187   case GIIK_P7: return KEY_PAD7;
188   case GIIK_P8: return KEY_PAD8;
189   case GIIK_P9: return KEY_PAD9;
190   case GIIK_PMinus: return KEY_PADMINUS;
191   case GIIK_PPlus: return KEY_PADPLUS;
192   case GIIK_PDecimal: return KEY_PADPERIOD;
193   case GIIK_PSlash: return KEY_PADDIVIDE;
194   case GIIK_PAsterisk: return KEY_PADMULTIPLY;
195   case GIIK_PEnter: return KEY_PADENTER;
196
197   case GIIK_Insert: return KEY_INSERT;
198   case GIIK_Home: return KEY_HOME;
199   case GIIK_PageUp: return KEY_PAGEUP;
200   case GIIK_Delete: return KEY_DELETE;
201   case GIIK_End: return KEY_END;
202   case GIIK_PageDown: return KEY_PAGEDOWN;
203   case GIIK_Up: return KEY_UP;
204   case GIIK_Down: return KEY_DOWN;
205   case GIIK_Left: return KEY_LEFT;
206   case GIIK_Right: return KEY_RIGHT;
207
208   case GIIK_PrintScreen: return KEY_PRINT_SCREEN;
209   case GIIK_Pause: return KEY_PAUSE;
210  }
211  return 0;
212 }
213
214 //killed on 10/03/98 by Matt Mueller
215 //unsigned char key_to_ascii(int a)
216 //{
217 // if (!isprint(a)) return 255;
218 // if (a & KEY_SHIFTED) {
219 //  return (toupper((unsigned char) a));
220 // } else {
221 //  return ((unsigned char) a);
222 // }
223 //}
224 //end kill -MM
225
226 //added on 10/03/98 by Matt Mueller to fix shifted keys (copied from dos/key.c)
227 unsigned char key_to_ascii(int keycode)
228 {
229         int shifted;
230
231         shifted = keycode & KEY_SHIFTED;
232         keycode &= 0xFF;
233
234         if ( keycode>=127 )
235                 return 255;
236
237         if (shifted)
238                 return shifted_ascii_table[keycode];
239         else
240                 return ascii_table[keycode];
241 }
242 //end addition -MM
243
244 void keyboard_handler(int button, ubyte state)
245 {
246         ubyte key_state;
247         int i, keycode;
248         unsigned short event_key;
249         Key_info *key;
250         unsigned char temp;
251
252         key_state = state;
253         event_key = giiKeyTranslate(button);
254         //mprintf((0,"keyboard_handler(%i,%i):%i\n",button,state,event_key));
255
256         //=====================================================
257         //Here a translation from win keycodes to mac keycodes!
258         //=====================================================
259
260         for (i = 255; i >= 0; i--) {
261
262                 keycode = i;
263                 key = &(key_data.keys[keycode]);
264                 if (i == event_key)
265                         state = key_state;
266                 else
267                         state = key->last_state;
268                         
269                 if ( key->last_state == state ) {
270                         if (state) {
271                                 key->counter++;
272                                 keyd_last_pressed = keycode;
273                                 keyd_time_when_last_pressed = timer_get_fixed_seconds();
274                         }
275                 } else {
276                         if (state)      {
277                                 keyd_last_pressed = keycode;
278                                 keyd_pressed[keycode] = 1;
279                                 key->downcount += state;
280                                 key->state = 1;
281                                 key->timewentdown = keyd_time_when_last_pressed = timer_get_fixed_seconds();
282                                 key->counter++;
283                         } else {        
284                                 keyd_pressed[keycode] = 0;
285                                 keyd_last_released = keycode;
286                                 key->upcount += key->state;
287                                 key->state = 0;
288                                 key->counter = 0;
289                                 key->timehelddown += timer_get_fixed_seconds() - key->timewentdown;
290                         }
291                 }
292                 if ( (state && !key->last_state) || (state && key->last_state && (key->counter > 30) && (key->counter & 0x01)) ) {
293                         if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT])
294                                 keycode |= KEY_SHIFTED;
295                         if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT])
296                                 keycode |= KEY_ALTED;
297                         if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL])
298                                 keycode |= KEY_CTRLED;
299                         if ( keyd_pressed[KEY_DELETE] )
300                                 keycode |= KEY_DEBUGGED;
301                         temp = key_data.keytail+1;
302                         if ( temp >= KEY_BUFFER_SIZE ) temp=0;
303                         if (temp!=key_data.keyhead)     {
304                                 key_data.keybuffer[key_data.keytail] = keycode;
305                                 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
306                                 key_data.keytail = temp;
307                         }
308                 }
309                 key->last_state = state;
310         }
311 }
312
313 void key_close()
314 {
315         Installed = 0;
316 }
317
318 void key_init()
319 {
320         Installed=1;
321
322         keyd_time_when_last_pressed = timer_get_fixed_seconds();
323         keyd_buffer_type = 1;
324         keyd_repeat = 1;
325
326 // Clear the keyboard array
327         key_flush();
328         atexit(key_close);
329 }
330
331 void key_flush()
332 {
333         int i;
334         fix curtime;
335
336         if (!Installed)
337                 key_init();
338
339         key_data.keyhead = key_data.keytail = 0;
340
341         //Clear the keyboard buffer
342         for (i=0; i<KEY_BUFFER_SIZE; i++ )      {
343                 key_data.keybuffer[i] = 0;
344                 key_data.time_pressed[i] = 0;
345         }
346
347         curtime = timer_get_fixed_seconds();
348
349         for (i=0; i<256; i++ )  {
350                 keyd_pressed[i] = 0;
351                 key_data.keys[i].state = 1;
352                 key_data.keys[i].last_state = 0;
353                 key_data.keys[i].timewentdown = curtime;
354                 key_data.keys[i].downcount=0;
355                 key_data.keys[i].upcount=0;
356                 key_data.keys[i].timehelddown = 0;
357                 key_data.keys[i].counter = 0;
358         }
359 }
360
361 int add_one(int n)
362 {
363  n++;
364  if ( n >= KEY_BUFFER_SIZE ) n=0;
365  return n;
366 }
367
368 int key_checkch()
369 {
370         int is_one_waiting = 0;
371         event_poll();
372         if (key_data.keytail!=key_data.keyhead)
373                 is_one_waiting = 1;
374         return is_one_waiting;
375 }
376
377 int key_inkey()
378 {
379         int key = 0;
380         if (!Installed)
381                 key_init();
382         event_poll();
383         if (key_data.keytail!=key_data.keyhead) {
384                 key = key_data.keybuffer[key_data.keyhead];
385                 key_data.keyhead = add_one(key_data.keyhead);
386         }
387 //added 9/3/98 by Matt Mueller to free cpu time instead of hogging during menus and such
388 //      else d_delay(1);
389 //end addition - Matt Mueller
390         return key;
391 }
392
393 int key_inkey_time(fix * time)
394 {
395         int key = 0;
396
397         if (!Installed)
398                 key_init();
399         event_poll();
400         if (key_data.keytail!=key_data.keyhead) {
401                 key = key_data.keybuffer[key_data.keyhead];
402                 *time = key_data.time_pressed[key_data.keyhead];
403                 key_data.keyhead = add_one(key_data.keyhead);
404         }
405         return key;
406 }
407
408 int key_peekkey()
409 {
410         int key = 0;
411         event_poll();
412         if (key_data.keytail!=key_data.keyhead)
413                 key = key_data.keybuffer[key_data.keyhead];
414
415         return key;
416 }
417
418 int key_getch()
419 {
420         int dummy=0;
421
422         if (!Installed)
423                 return 0;
424 //              return getch();
425
426         while (!key_checkch())
427                 dummy++;
428         return key_inkey();
429 }
430
431 unsigned int key_get_shift_status()
432 {
433         unsigned int shift_status = 0;
434
435         if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
436                 shift_status |= KEY_SHIFTED;
437
438         if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
439                 shift_status |= KEY_ALTED;
440
441         if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
442                 shift_status |= KEY_CTRLED;
443
444 #ifndef NDEBUG
445         if (keyd_pressed[KEY_DELETE])
446                 shift_status |=KEY_DEBUGGED;
447 #endif
448
449         return shift_status;
450 }
451
452 // Returns the number of seconds this key has been down since last call.
453 fix key_down_time(int scancode)
454 {
455         fix time_down, time;
456
457         event_poll();
458         if ((scancode<0)|| (scancode>255)) return 0;
459
460         if (!keyd_pressed[scancode]) {
461                 time_down = key_data.keys[scancode].timehelddown;
462                 key_data.keys[scancode].timehelddown = 0;
463         } else {
464                 time = timer_get_fixed_seconds();
465                 time_down = time - key_data.keys[scancode].timewentdown;
466                 key_data.keys[scancode].timewentdown = time;
467         }
468
469         return time_down;
470 }
471
472 unsigned int key_down_count(int scancode)
473 {
474         int n;
475         event_poll();
476         if ((scancode<0)|| (scancode>255)) return 0;
477
478         n = key_data.keys[scancode].downcount;
479         key_data.keys[scancode].downcount = 0;
480
481         return n;
482 }
483
484 unsigned int key_up_count(int scancode)
485 {
486         int n;
487         event_poll();
488         if ((scancode<0)|| (scancode>255)) return 0;
489
490         n = key_data.keys[scancode].upcount;
491         key_data.keys[scancode].upcount = 0;
492
493         return n;
494 }
495