3 * GGI keyboard input support
24 #define KEY_BUFFER_SIZE 16
26 static unsigned char Installed = 0;
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;
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
47 typedef struct keyboard {
48 unsigned short keybuffer[KEY_BUFFER_SIZE];
50 fix time_pressed[KEY_BUFFER_SIZE];
51 unsigned int keyhead, keytail;
54 static /*volatile*/ keyboard key_data;
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 };
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 };
80 int giiKeyTranslate (int keylabel) {
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;
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;
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;
129 case GIIUC_BracketLeft: return KEY_LBRACKET;
130 case GIIUC_BracketRight: return KEY_RBRACKET;
132 case GIIUC_Apostrophe: return KEY_RAPOSTRO;
133 case GIIUC_Grave: return KEY_LAPOSTRO;
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;
141 case GIIK_NumLock: return KEY_NUMLOCK;
142 case GIIK_ScrollLock: return KEY_SCROLLOCK;
143 case GIIK_CapsLock: return KEY_CAPSLOCK;
145 case GIIK_ShiftL: return KEY_LSHIFT;
146 case GIIK_ShiftR: return KEY_RSHIFT;
148 case GIIK_AltL: return KEY_LALT;
149 case GIIK_AltR: return KEY_RALT;
151 case GIIK_CtrlL: return KEY_LCTRL;
152 case GIIK_CtrlR: return KEY_RCTRL;
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;
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;
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;
195 case GIIK_PrintScreen: return KEY_PRINT_SCREEN;
196 case GIIK_Pause: return KEY_PAUSE;
201 //killed on 10/03/98 by Matt Mueller
202 //unsigned char key_to_ascii(int a)
204 // if (!isprint(a)) return 255;
205 // if (a & KEY_SHIFTED) {
206 // return (toupper((unsigned char) a));
208 // return ((unsigned char) a);
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)
218 shifted = keycode & KEY_SHIFTED;
225 return shifted_ascii_table[keycode];
227 return ascii_table[keycode];
231 void keyboard_handler(int button, ubyte state)
235 unsigned short event_key;
240 event_key = giiKeyTranslate(button);
241 //mprintf((0,"keyboard_handler(%i,%i):%i\n",button,state,event_key));
243 //=====================================================
244 //Here a translation from win keycodes to mac keycodes!
245 //=====================================================
247 for (i = 255; i >= 0; i--) {
250 key = &(key_data.keys[keycode]);
254 state = key->last_state;
256 if ( key->last_state == state ) {
259 keyd_last_pressed = keycode;
260 keyd_time_when_last_pressed = timer_get_fixed_seconds();
264 keyd_last_pressed = keycode;
265 keyd_pressed[keycode] = 1;
266 key->downcount += state;
268 key->timewentdown = keyd_time_when_last_pressed = timer_get_fixed_seconds();
271 keyd_pressed[keycode] = 0;
272 keyd_last_released = keycode;
273 key->upcount += key->state;
276 key->timehelddown += timer_get_fixed_seconds() - key->timewentdown;
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;
296 key->last_state = state;
309 keyd_time_when_last_pressed = timer_get_fixed_seconds();
310 keyd_buffer_type = 1;
313 // Clear the keyboard array
326 key_data.keyhead = key_data.keytail = 0;
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;
334 curtime = timer_get_fixed_seconds();
336 for (i=0; i<256; i++ ) {
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;
351 if ( n >= KEY_BUFFER_SIZE ) n=0;
357 int is_one_waiting = 0;
359 if (key_data.keytail!=key_data.keyhead)
361 return is_one_waiting;
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);
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
380 int key_inkey_time(fix * time)
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);
399 if (key_data.keytail!=key_data.keyhead)
400 key = key_data.keybuffer[key_data.keyhead];
413 while (!key_checkch())
418 unsigned int key_get_shift_status()
420 unsigned int shift_status = 0;
422 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
423 shift_status |= KEY_SHIFTED;
425 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
426 shift_status |= KEY_ALTED;
428 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
429 shift_status |= KEY_CTRLED;
432 if (keyd_pressed[KEY_DELETE])
433 shift_status |=KEY_DEBUGGED;
439 // Returns the number of seconds this key has been down since last call.
440 fix key_down_time(int scancode)
445 if ((scancode<0)|| (scancode>255)) return 0;
447 if (!keyd_pressed[scancode]) {
448 time_down = key_data.keys[scancode].timehelddown;
449 key_data.keys[scancode].timehelddown = 0;
451 time = timer_get_fixed_seconds();
452 time_down = time - key_data.keys[scancode].timewentdown;
453 key_data.keys[scancode].timewentdown = time;
459 unsigned int key_down_count(int scancode)
463 if ((scancode<0)|| (scancode>255)) return 0;
465 n = key_data.keys[scancode].downcount;
466 key_data.keys[scancode].downcount = 0;
471 unsigned int key_up_count(int scancode)
475 if ((scancode<0)|| (scancode>255)) return 0;
477 n = key_data.keys[scancode].upcount;
478 key_data.keys[scancode].upcount = 0;