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