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