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