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