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