]> icculus.org git repositories - btb/d2x.git/blob - input/svgalib_key.c
svgalib support
[btb/d2x.git] / input / svgalib_key.c
1 /* SVGALib keyboard input support */
2
3 #include <conf.h>
4
5 #ifdef SVGALIB_INPUT
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include <vgakeyboard.h> 
11
12 #include "event.h"
13 #include "error.h"
14 #include "key.h"
15 #include "timer.h"
16
17 //added on 9/3/98 by Matt Mueller to free some cpu instead of hogging during menus and such
18 #include "d_delay.h"
19 //end this section addition - Matt Mueller
20
21 #define KEY_BUFFER_SIZE 16
22
23 static unsigned char Installed = 0;
24
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;
33
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
42 } Key_info;
43
44 typedef struct keyboard {
45         unsigned short          keybuffer[KEY_BUFFER_SIZE];
46         Key_info                keys[256];
47         fix                     time_pressed[KEY_BUFFER_SIZE];
48         unsigned int            keyhead, keytail;
49 } keyboard;
50
51 static /*volatile*/ keyboard key_data;
52
53 char *key_text[256];
54
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 };
65
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 };
76
77 //killed on 10/03/98 by Matt Mueller
78 //unsigned char key_to_ascii(int a)
79 //{
80 // if (!isprint(a)) return 255;
81 // if (a & KEY_SHIFTED) {
82 //  return (toupper((unsigned char) a));
83 // } else {
84 //  return ((unsigned char) a);
85 // }
86 //}
87 //end kill -MM
88
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)
91 {
92         int shifted;
93
94         shifted = keycode & KEY_SHIFTED;
95         keycode &= 0xFF;
96
97         if ( keycode>=127 )
98                 return 255;
99
100         if (shifted)
101                 return shifted_ascii_table[keycode];
102         else
103                 return ascii_table[keycode];
104 }
105 //end addition -MM
106
107 void key_handler(int scancode, int press)
108 {
109         ubyte state, key_state;
110         int i, keycode, event_key;
111         Key_info *key;
112         unsigned char temp;
113
114         if (press == KEY_EVENTPRESS)
115                 key_state = 1;
116         else if (press == KEY_EVENTRELEASE)
117                 key_state = 0;
118         else
119                 return;
120
121         event_key = scancode;
122
123         //=====================================================
124         //Here a translation from win keycodes to mac keycodes!
125         //=====================================================
126
127         for (i = 255; i >= 0; i--) {
128
129                 keycode = i;
130                 key = &(key_data.keys[keycode]);
131                 if (i == event_key)
132                         state = key_state;
133                 else
134                         state = key->last_state;
135                         
136                 if ( key->last_state == state ) {
137                         if (state) {
138                                 key->counter++;
139                                 keyd_last_pressed = keycode;
140                                 keyd_time_when_last_pressed = timer_get_fixed_seconds();
141                         }
142                 } else {
143                         if (state)      {
144                                 keyd_last_pressed = keycode;
145                                 keyd_pressed[keycode] = 1;
146                                 key->downcount += state;
147                                 key->state = 1;
148                                 key->timewentdown = keyd_time_when_last_pressed = timer_get_fixed_seconds();
149                                 key->counter++;
150                         } else {        
151                                 keyd_pressed[keycode] = 0;
152                                 keyd_last_released = keycode;
153                                 key->upcount += key->state;
154                                 key->state = 0;
155                                 key->counter = 0;
156                                 key->timehelddown += timer_get_fixed_seconds() - key->timewentdown;
157                         }
158                 }
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;
174                         }
175                 }
176                 key->last_state = state;
177         }
178 }
179
180 void key_close()
181 {
182         Installed = 0;
183         keyboard_close();
184 }
185
186 void key_init()
187 {
188         if (keyboard_init())
189                 Error ("SVGAlib Keyboard Init Failed");
190         Installed=1;
191
192         keyboard_seteventhandler (key_handler);
193         keyd_time_when_last_pressed = timer_get_fixed_seconds();
194         keyd_buffer_type = 1;
195         keyd_repeat = 1;
196
197 // Clear the keyboard array
198         key_flush();
199         atexit(key_close);
200 }
201
202 void key_flush()
203 {
204         int i;
205         fix curtime;
206
207         if (!Installed)
208                 key_init();
209
210         key_data.keyhead = key_data.keytail = 0;
211
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;
216         }
217
218 //use gettimeofday here:
219         curtime = timer_get_fixed_seconds();
220
221         for (i=0; i<256; i++ )  {
222                 keyd_pressed[i] = 0;
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;
230         }
231 }
232
233 int add_one(int n)
234 {
235  n++;
236  if ( n >= KEY_BUFFER_SIZE ) n=0;
237  return n;
238 }
239
240 int key_checkch()
241 {
242         int is_one_waiting = 0;
243         event_poll();
244         if (key_data.keytail!=key_data.keyhead)
245                 is_one_waiting = 1;
246         return is_one_waiting;
247 }
248
249 int key_inkey()
250 {
251         int key = 0;
252         if (!Installed)
253                 key_init();
254         event_poll();
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);
258         }
259 //added 9/3/98 by Matt Mueller to free cpu time instead of hogging during menus and such
260 //      else d_delay(1);
261 //end addition - Matt Mueller
262         return key;
263 }
264
265 int key_inkey_time(fix * time)
266 {
267         int key = 0;
268
269         if (!Installed)
270                 key_init();
271         event_poll();
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);
276         }
277         return key;
278 }
279
280 int key_peekkey()
281 {
282         int key = 0;
283         event_poll();
284         if (key_data.keytail!=key_data.keyhead)
285                 key = key_data.keybuffer[key_data.keyhead];
286
287         return key;
288 }
289
290 int key_getch()
291 {
292         int dummy=0;
293
294         if (!Installed)
295                 return 0;
296 //              return getch();
297
298         while (!key_checkch())
299                 dummy++;
300         return key_inkey();
301 }
302
303 unsigned int key_get_shift_status()
304 {
305         unsigned int shift_status = 0;
306
307         if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
308                 shift_status |= KEY_SHIFTED;
309
310         if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
311                 shift_status |= KEY_ALTED;
312
313         if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
314                 shift_status |= KEY_CTRLED;
315
316 #ifndef NDEBUG
317         if (keyd_pressed[KEY_DELETE])
318                 shift_status |=KEY_DEBUGGED;
319 #endif
320
321         return shift_status;
322 }
323
324 // Returns the number of seconds this key has been down since last call.
325 fix key_down_time(int scancode)
326 {
327         fix time_down, time;
328
329         event_poll();
330         if ((scancode<0)|| (scancode>255)) return 0;
331
332         if (!keyd_pressed[scancode]) {
333                 time_down = key_data.keys[scancode].timehelddown;
334                 key_data.keys[scancode].timehelddown = 0;
335         } else {
336                 time = timer_get_fixed_seconds();
337                 time_down = time - key_data.keys[scancode].timewentdown;
338                 key_data.keys[scancode].timewentdown = time;
339         }
340
341         return time_down;
342 }
343
344 unsigned int key_down_count(int scancode)
345 {
346         int n;
347         event_poll();
348         if ((scancode<0)|| (scancode>255)) return 0;
349
350         n = key_data.keys[scancode].downcount;
351         key_data.keys[scancode].downcount = 0;
352
353         return n;
354 }
355
356 unsigned int key_up_count(int scancode)
357 {
358         int n;
359         event_poll();
360         if ((scancode<0)|| (scancode>255)) return 0;
361
362         n = key_data.keys[scancode].upcount;
363         key_data.keys[scancode].upcount = 0;
364
365         return n;
366 }
367
368 #endif /* SVGALIB_INPUT */