2 * $Source: /cvs/cvsroot/d2x/arch/svgalib/key.c,v $
5 * $Date: 2003-02-27 22:07:21 $
7 * SVGALib keyboard input support
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.
13 * Revision 1.2 2001/01/29 14:03:57 bradleyb
14 * Fixed build, minor fixes
25 #include <vgakeyboard.h>
33 #define KEY_BUFFER_SIZE 16
35 static unsigned char Installed = 0;
37 //-------- Variable accessed by outside functions ---------
38 unsigned char keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
39 unsigned char keyd_repeat;
40 unsigned char keyd_editor_mode;
41 volatile unsigned char keyd_last_pressed;
42 volatile unsigned char keyd_last_released;
43 volatile unsigned char keyd_pressed[256];
44 volatile int keyd_time_when_last_pressed;
46 typedef struct Key_info {
47 ubyte state; // state of key 1 == down, 0 == up
48 ubyte last_state; // previous state of key
49 int counter; // incremented each time key is down in handler
50 fix timewentdown; // simple counter incremented each time in interrupt and key is down
51 fix timehelddown; // counter to tell how long key is down -- gets reset to 0 by key routines
52 ubyte downcount; // number of key counts key was down
53 ubyte upcount; // number of times key was released
56 typedef struct keyboard {
57 unsigned short keybuffer[KEY_BUFFER_SIZE];
59 fix time_pressed[KEY_BUFFER_SIZE];
60 unsigned int keyhead, keytail;
63 static /*volatile*/ keyboard key_data;
67 unsigned char ascii_table[128] =
68 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
69 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
70 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
71 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 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,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,
76 255,255,255,255,255,255,255,255 };
78 unsigned char shifted_ascii_table[128] =
79 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
80 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
81 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
82 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 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,255,
85 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,255,
87 255,255,255,255,255,255,255,255 };
89 //killed on 10/03/98 by Matt Mueller
90 //unsigned char key_to_ascii(int a)
92 // if (!isprint(a)) return 255;
93 // if (a & KEY_SHIFTED) {
94 // return (toupper((unsigned char) a));
96 // return ((unsigned char) a);
101 //added on 10/03/98 by Matt Mueller to fix shifted keys (copied from dos/key.c)
102 unsigned char key_to_ascii(int keycode)
106 shifted = keycode & KEY_SHIFTED;
113 return shifted_ascii_table[keycode];
115 return ascii_table[keycode];
119 void key_handler(int scancode, int press)
121 ubyte state, key_state;
122 int i, keycode, event_key;
126 if (press == KEY_EVENTPRESS)
128 else if (press == KEY_EVENTRELEASE)
133 event_key = scancode;
135 //=====================================================
136 //Here a translation from win keycodes to mac keycodes!
137 //=====================================================
139 for (i = 255; i >= 0; i--) {
142 key = &(key_data.keys[keycode]);
146 state = key->last_state;
148 if ( key->last_state == state ) {
151 keyd_last_pressed = keycode;
152 keyd_time_when_last_pressed = timer_get_fixed_seconds();
156 keyd_last_pressed = keycode;
157 keyd_pressed[keycode] = 1;
158 key->downcount += state;
160 key->timewentdown = keyd_time_when_last_pressed = timer_get_fixed_seconds();
163 keyd_pressed[keycode] = 0;
164 keyd_last_released = keycode;
165 key->upcount += key->state;
168 key->timehelddown += timer_get_fixed_seconds() - key->timewentdown;
171 if ( (state && !key->last_state) || (state && key->last_state && (key->counter > 30) && (key->counter & 0x01)) ) {
172 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT])
173 keycode |= KEY_SHIFTED;
174 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT])
175 keycode |= KEY_ALTED;
176 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL])
177 keycode |= KEY_CTRLED;
178 if ( keyd_pressed[KEY_DELETE] )
179 keycode |= KEY_DEBUGGED;
180 temp = key_data.keytail+1;
181 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
182 if (temp!=key_data.keyhead) {
183 key_data.keybuffer[key_data.keytail] = keycode;
184 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
185 key_data.keytail = temp;
188 key->last_state = state;
201 Error ("SVGAlib Keyboard Init Failed");
204 keyboard_seteventhandler (key_handler);
205 keyd_time_when_last_pressed = timer_get_fixed_seconds();
206 keyd_buffer_type = 1;
209 // Clear the keyboard array
222 key_data.keyhead = key_data.keytail = 0;
224 //Clear the keyboard buffer
225 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
226 key_data.keybuffer[i] = 0;
227 key_data.time_pressed[i] = 0;
230 //use gettimeofday here:
231 curtime = timer_get_fixed_seconds();
233 for (i=0; i<256; i++ ) {
235 key_data.keys[i].state = 1;
236 key_data.keys[i].last_state = 0;
237 key_data.keys[i].timewentdown = curtime;
238 key_data.keys[i].downcount=0;
239 key_data.keys[i].upcount=0;
240 key_data.keys[i].timehelddown = 0;
241 key_data.keys[i].counter = 0;
248 if ( n >= KEY_BUFFER_SIZE ) n=0;
254 int is_one_waiting = 0;
256 if (key_data.keytail!=key_data.keyhead)
258 return is_one_waiting;
267 if (key_data.keytail!=key_data.keyhead) {
268 key = key_data.keybuffer[key_data.keyhead];
269 key_data.keyhead = add_one(key_data.keyhead);
271 //added 9/3/98 by Matt Mueller to free cpu time instead of hogging during menus and such
272 // else timer_delay(1);
273 //end addition - Matt Mueller
277 int key_inkey_time(fix * time)
284 if (key_data.keytail!=key_data.keyhead) {
285 key = key_data.keybuffer[key_data.keyhead];
286 *time = key_data.time_pressed[key_data.keyhead];
287 key_data.keyhead = add_one(key_data.keyhead);
296 if (key_data.keytail!=key_data.keyhead)
297 key = key_data.keybuffer[key_data.keyhead];
310 while (!key_checkch())
315 unsigned int key_get_shift_status()
317 unsigned int shift_status = 0;
319 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
320 shift_status |= KEY_SHIFTED;
322 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
323 shift_status |= KEY_ALTED;
325 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
326 shift_status |= KEY_CTRLED;
329 if (keyd_pressed[KEY_DELETE])
330 shift_status |=KEY_DEBUGGED;
336 // Returns the number of seconds this key has been down since last call.
337 fix key_down_time(int scancode)
342 if ((scancode<0)|| (scancode>255)) return 0;
344 if (!keyd_pressed[scancode]) {
345 time_down = key_data.keys[scancode].timehelddown;
346 key_data.keys[scancode].timehelddown = 0;
348 time = timer_get_fixed_seconds();
349 time_down = time - key_data.keys[scancode].timewentdown;
350 key_data.keys[scancode].timewentdown = time;
356 unsigned int key_down_count(int scancode)
360 if ((scancode<0)|| (scancode>255)) return 0;
362 n = key_data.keys[scancode].downcount;
363 key_data.keys[scancode].downcount = 0;
368 unsigned int key_up_count(int scancode)
372 if ((scancode<0)|| (scancode>255)) return 0;
374 n = key_data.keys[scancode].upcount;
375 key_data.keys[scancode].upcount = 0;