1 /// SDL keyboard input support
6 #include <vgakeyboard.h>
13 //added on 9/3/98 by Matt Mueller to free some cpu instead of hogging during menus and such
15 //end this section addition - Matt Mueller
17 #define KEY_BUFFER_SIZE 16
19 static unsigned char Installed = 0;
21 //-------- Variable accessed by outside functions ---------
22 unsigned char keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
23 unsigned char keyd_repeat;
24 unsigned char keyd_editor_mode;
25 volatile unsigned char keyd_last_pressed;
26 volatile unsigned char keyd_last_released;
27 volatile unsigned char keyd_pressed[256];
28 volatile int keyd_time_when_last_pressed;
30 typedef struct Key_info {
31 ubyte state; // state of key 1 == down, 0 == up
32 ubyte last_state; // previous state of key
33 int counter; // incremented each time key is down in handler
34 fix timewentdown; // simple counter incremented each time in interrupt and key is down
35 fix timehelddown; // counter to tell how long key is down -- gets reset to 0 by key routines
36 ubyte downcount; // number of key counts key was down
37 ubyte upcount; // number of times key was released
40 typedef struct keyboard {
41 unsigned short keybuffer[KEY_BUFFER_SIZE];
43 fix time_pressed[KEY_BUFFER_SIZE];
44 unsigned int keyhead, keytail;
47 static /*volatile*/ keyboard key_data;
49 char * key_text[256] = {
50 "","ESC","1","2","3","4","5","6","7","8","9","0","-",
51 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",
52 "P","[","]","
\83","LCTRL","A","S","D","F",
53 "G","H","J","K","L",";","'","`",
54 "LSHFT","\\","Z","X","C","V","B","N","M",",",
55 ".","/","RSHFT","PAD*","LALT","SPC",
56 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",
57 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",
58 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0",
59 "PAD.","","","","F11","F12","","","","","","","","","",
60 "","","","","","","","","","","","","","","","","","","","",
61 "","","","","","","","","","","","","","","","","","","","",
62 "","","","","","","","","","","","","","","","","","",
63 "PAD
\83","RCTRL","","","","","","","","","","","","","",
64 "","","","","","","","","","","PAD/","","","RALT","",
65 "","","","","","","","","","","","","","HOME","
\82","PGUP",
66 "","
\81","","
\7f","","END","
\80","PGDN","INS",
67 "DEL","","","","","","","","","","","","","","","","","",
68 "","","","","","","","","","","","","","","","","","","","",
69 "","","","","","","" };
71 unsigned char ascii_table[128] =
72 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
73 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
74 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
75 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 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,255,
78 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
79 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
80 255,255,255,255,255,255,255,255 };
82 unsigned char shifted_ascii_table[128] =
83 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
84 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
85 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
86 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
87 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
88 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
89 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
90 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
91 255,255,255,255,255,255,255,255 };
93 //killed on 10/03/98 by Matt Mueller
94 //unsigned char key_to_ascii(int a)
96 // if (!isprint(a)) return 255;
97 // if (a & KEY_SHIFTED) {
98 // return (toupper((unsigned char) a));
100 // return ((unsigned char) a);
105 //added on 10/03/98 by Matt Mueller to fix shifted keys (copied from dos/key.c)
106 unsigned char key_to_ascii(int keycode)
110 shifted = keycode & KEY_SHIFTED;
117 return shifted_ascii_table[keycode];
119 return ascii_table[keycode];
123 void key_handler(int scancode, int press)
125 ubyte state, key_state;
126 int i, keycode, event_key;
130 if (press == KEY_EVENTPRESS)
132 else if (press == KEY_EVENTRELEASE)
137 event_key = scancode;
139 //=====================================================
140 //Here a translation from win keycodes to mac keycodes!
141 //=====================================================
143 for (i = 255; i >= 0; i--) {
146 key = &(key_data.keys[keycode]);
150 state = key->last_state;
152 if ( key->last_state == state ) {
155 keyd_last_pressed = keycode;
156 keyd_time_when_last_pressed = timer_get_fixed_seconds();
160 keyd_last_pressed = keycode;
161 keyd_pressed[keycode] = 1;
162 key->downcount += state;
164 key->timewentdown = keyd_time_when_last_pressed = timer_get_fixed_seconds();
167 keyd_pressed[keycode] = 0;
168 keyd_last_released = keycode;
169 key->upcount += key->state;
172 key->timehelddown += timer_get_fixed_seconds() - key->timewentdown;
175 if ( (state && !key->last_state) || (state && key->last_state && (key->counter > 30) && (key->counter & 0x01)) ) {
176 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT])
177 keycode |= KEY_SHIFTED;
178 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT])
179 keycode |= KEY_ALTED;
180 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL])
181 keycode |= KEY_CTRLED;
182 if ( keyd_pressed[KEY_DELETE] )
183 keycode |= KEY_DEBUGGED;
184 temp = key_data.keytail+1;
185 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
186 if (temp!=key_data.keyhead) {
187 key_data.keybuffer[key_data.keytail] = keycode;
188 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
189 key_data.keytail = temp;
192 key->last_state = state;
205 Error ("SVGAlib Keyboard Init Failed");
208 keyboard_seteventhandler (key_handler);
209 keyd_time_when_last_pressed = timer_get_fixed_seconds();
210 keyd_buffer_type = 1;
213 // Clear the keyboard array
226 key_data.keyhead = key_data.keytail = 0;
228 //Clear the keyboard buffer
229 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
230 key_data.keybuffer[i] = 0;
231 key_data.time_pressed[i] = 0;
234 //use gettimeofday here:
235 curtime = timer_get_fixed_seconds();
237 for (i=0; i<256; i++ ) {
239 key_data.keys[i].state = 1;
240 key_data.keys[i].last_state = 0;
241 key_data.keys[i].timewentdown = curtime;
242 key_data.keys[i].downcount=0;
243 key_data.keys[i].upcount=0;
244 key_data.keys[i].timehelddown = 0;
245 key_data.keys[i].counter = 0;
252 if ( n >= KEY_BUFFER_SIZE ) n=0;
258 int is_one_waiting = 0;
260 if (key_data.keytail!=key_data.keyhead)
262 return is_one_waiting;
271 if (key_data.keytail!=key_data.keyhead) {
272 key = key_data.keybuffer[key_data.keyhead];
273 key_data.keyhead = add_one(key_data.keyhead);
275 //added 9/3/98 by Matt Mueller to free cpu time instead of hogging during menus and such
277 //end addition - Matt Mueller
281 int key_inkey_time(fix * time)
288 if (key_data.keytail!=key_data.keyhead) {
289 key = key_data.keybuffer[key_data.keyhead];
290 *time = key_data.time_pressed[key_data.keyhead];
291 key_data.keyhead = add_one(key_data.keyhead);
300 if (key_data.keytail!=key_data.keyhead)
301 key = key_data.keybuffer[key_data.keyhead];
314 while (!key_checkch())
319 unsigned int key_get_shift_status()
321 unsigned int shift_status = 0;
323 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
324 shift_status |= KEY_SHIFTED;
326 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
327 shift_status |= KEY_ALTED;
329 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
330 shift_status |= KEY_CTRLED;
333 if (keyd_pressed[KEY_DELETE])
334 shift_status |=KEY_DEBUGGED;
340 // Returns the number of seconds this key has been down since last call.
341 fix key_down_time(int scancode)
346 if ((scancode<0)|| (scancode>255)) return 0;
348 if (!keyd_pressed[scancode]) {
349 time_down = key_data.keys[scancode].timehelddown;
350 key_data.keys[scancode].timehelddown = 0;
352 time = timer_get_fixed_seconds();
353 time_down = time - key_data.keys[scancode].timewentdown;
354 key_data.keys[scancode].timewentdown = time;
360 unsigned int key_down_count(int scancode)
364 if ((scancode<0)|| (scancode>255)) return 0;
366 n = key_data.keys[scancode].downcount;
367 key_data.keys[scancode].downcount = 0;
372 unsigned int key_up_count(int scancode)
376 if ((scancode<0)|| (scancode>255)) return 0;
378 n = key_data.keys[scancode].upcount;
379 key_data.keys[scancode].upcount = 0;