1 /* SVGALib keyboard input support */
10 #include <vgakeyboard.h>
17 //added on 9/3/98 by Matt Mueller to free some cpu instead of hogging during menus and such
19 //end this section addition - Matt Mueller
21 #define KEY_BUFFER_SIZE 16
23 static unsigned char Installed = 0;
25 //-------- Variable accessed by outside functions ---------
26 unsigned char keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
27 unsigned char keyd_repeat;
28 unsigned char keyd_editor_mode;
29 volatile unsigned char keyd_last_pressed;
30 volatile unsigned char keyd_last_released;
31 volatile unsigned char keyd_pressed[256];
32 volatile int keyd_time_when_last_pressed;
34 typedef struct Key_info {
35 ubyte state; // state of key 1 == down, 0 == up
36 ubyte last_state; // previous state of key
37 int counter; // incremented each time key is down in handler
38 fix timewentdown; // simple counter incremented each time in interrupt and key is down
39 fix timehelddown; // counter to tell how long key is down -- gets reset to 0 by key routines
40 ubyte downcount; // number of key counts key was down
41 ubyte upcount; // number of times key was released
44 typedef struct keyboard {
45 unsigned short keybuffer[KEY_BUFFER_SIZE];
47 fix time_pressed[KEY_BUFFER_SIZE];
48 unsigned int keyhead, keytail;
51 static /*volatile*/ keyboard key_data;
55 unsigned char ascii_table[128] =
56 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
57 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
58 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
59 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
60 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
61 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
62 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
63 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
64 255,255,255,255,255,255,255,255 };
66 unsigned char shifted_ascii_table[128] =
67 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
68 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
69 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
70 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
71 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,255,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,
75 255,255,255,255,255,255,255,255 };
77 //killed on 10/03/98 by Matt Mueller
78 //unsigned char key_to_ascii(int a)
80 // if (!isprint(a)) return 255;
81 // if (a & KEY_SHIFTED) {
82 // return (toupper((unsigned char) a));
84 // return ((unsigned char) a);
89 //added on 10/03/98 by Matt Mueller to fix shifted keys (copied from dos/key.c)
90 unsigned char key_to_ascii(int keycode)
94 shifted = keycode & KEY_SHIFTED;
101 return shifted_ascii_table[keycode];
103 return ascii_table[keycode];
107 void key_handler(int scancode, int press)
109 ubyte state, key_state;
110 int i, keycode, event_key;
114 if (press == KEY_EVENTPRESS)
116 else if (press == KEY_EVENTRELEASE)
121 event_key = scancode;
123 //=====================================================
124 //Here a translation from win keycodes to mac keycodes!
125 //=====================================================
127 for (i = 255; i >= 0; i--) {
130 key = &(key_data.keys[keycode]);
134 state = key->last_state;
136 if ( key->last_state == state ) {
139 keyd_last_pressed = keycode;
140 keyd_time_when_last_pressed = timer_get_fixed_seconds();
144 keyd_last_pressed = keycode;
145 keyd_pressed[keycode] = 1;
146 key->downcount += state;
148 key->timewentdown = keyd_time_when_last_pressed = timer_get_fixed_seconds();
151 keyd_pressed[keycode] = 0;
152 keyd_last_released = keycode;
153 key->upcount += key->state;
156 key->timehelddown += timer_get_fixed_seconds() - key->timewentdown;
159 if ( (state && !key->last_state) || (state && key->last_state && (key->counter > 30) && (key->counter & 0x01)) ) {
160 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT])
161 keycode |= KEY_SHIFTED;
162 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT])
163 keycode |= KEY_ALTED;
164 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL])
165 keycode |= KEY_CTRLED;
166 if ( keyd_pressed[KEY_DELETE] )
167 keycode |= KEY_DEBUGGED;
168 temp = key_data.keytail+1;
169 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
170 if (temp!=key_data.keyhead) {
171 key_data.keybuffer[key_data.keytail] = keycode;
172 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
173 key_data.keytail = temp;
176 key->last_state = state;
189 Error ("SVGAlib Keyboard Init Failed");
192 keyboard_seteventhandler (key_handler);
193 keyd_time_when_last_pressed = timer_get_fixed_seconds();
194 keyd_buffer_type = 1;
197 // Clear the keyboard array
210 key_data.keyhead = key_data.keytail = 0;
212 //Clear the keyboard buffer
213 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
214 key_data.keybuffer[i] = 0;
215 key_data.time_pressed[i] = 0;
218 //use gettimeofday here:
219 curtime = timer_get_fixed_seconds();
221 for (i=0; i<256; i++ ) {
223 key_data.keys[i].state = 1;
224 key_data.keys[i].last_state = 0;
225 key_data.keys[i].timewentdown = curtime;
226 key_data.keys[i].downcount=0;
227 key_data.keys[i].upcount=0;
228 key_data.keys[i].timehelddown = 0;
229 key_data.keys[i].counter = 0;
236 if ( n >= KEY_BUFFER_SIZE ) n=0;
242 int is_one_waiting = 0;
244 if (key_data.keytail!=key_data.keyhead)
246 return is_one_waiting;
255 if (key_data.keytail!=key_data.keyhead) {
256 key = key_data.keybuffer[key_data.keyhead];
257 key_data.keyhead = add_one(key_data.keyhead);
259 //added 9/3/98 by Matt Mueller to free cpu time instead of hogging during menus and such
261 //end addition - Matt Mueller
265 int key_inkey_time(fix * time)
272 if (key_data.keytail!=key_data.keyhead) {
273 key = key_data.keybuffer[key_data.keyhead];
274 *time = key_data.time_pressed[key_data.keyhead];
275 key_data.keyhead = add_one(key_data.keyhead);
284 if (key_data.keytail!=key_data.keyhead)
285 key = key_data.keybuffer[key_data.keyhead];
298 while (!key_checkch())
303 unsigned int key_get_shift_status()
305 unsigned int shift_status = 0;
307 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
308 shift_status |= KEY_SHIFTED;
310 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
311 shift_status |= KEY_ALTED;
313 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
314 shift_status |= KEY_CTRLED;
317 if (keyd_pressed[KEY_DELETE])
318 shift_status |=KEY_DEBUGGED;
324 // Returns the number of seconds this key has been down since last call.
325 fix key_down_time(int scancode)
330 if ((scancode<0)|| (scancode>255)) return 0;
332 if (!keyd_pressed[scancode]) {
333 time_down = key_data.keys[scancode].timehelddown;
334 key_data.keys[scancode].timehelddown = 0;
336 time = timer_get_fixed_seconds();
337 time_down = time - key_data.keys[scancode].timewentdown;
338 key_data.keys[scancode].timewentdown = time;
344 unsigned int key_down_count(int scancode)
348 if ((scancode<0)|| (scancode>255)) return 0;
350 n = key_data.keys[scancode].downcount;
351 key_data.keys[scancode].downcount = 0;
356 unsigned int key_up_count(int scancode)
360 if ((scancode<0)|| (scancode>255)) return 0;
362 n = key_data.keys[scancode].upcount;
363 key_data.keys[scancode].upcount = 0;
368 #endif /* SVGALIB_INPUT */