]> icculus.org git repositories - btb/d2x.git/blob - arch/svgalib/key.c
fixed the config bug where it'd always put in some weird axis (d1x r1.3)
[btb/d2x.git] / arch / svgalib / key.c
1 /*
2  * $Source: /cvs/cvsroot/d2x/arch/svgalib/key.c,v $
3  * $Revision: 1.2 $
4  * $Author: btb $
5  * $Date: 2003-02-27 22:07:21 $
6  *
7  * SVGALib keyboard input support
8  *
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.
12  *
13  * Revision 1.2  2001/01/29 14:03:57  bradleyb
14  * Fixed build, minor fixes
15  *
16  */
17
18 #ifdef HAVE_CONFIG_H
19 #include <conf.h>
20 #endif
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <vgakeyboard.h> 
26
27 #include "event.h"
28 #include "error.h"
29 #include "key.h"
30 #include "timer.h"
31
32
33 #define KEY_BUFFER_SIZE 16
34
35 static unsigned char Installed = 0;
36
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;
45
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
54 } Key_info;
55
56 typedef struct keyboard {
57         unsigned short          keybuffer[KEY_BUFFER_SIZE];
58         Key_info                keys[256];
59         fix                     time_pressed[KEY_BUFFER_SIZE];
60         unsigned int            keyhead, keytail;
61 } keyboard;
62
63 static /*volatile*/ keyboard key_data;
64
65 char *key_text[256];
66
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 };
77
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 };
88
89 //killed on 10/03/98 by Matt Mueller
90 //unsigned char key_to_ascii(int a)
91 //{
92 // if (!isprint(a)) return 255;
93 // if (a & KEY_SHIFTED) {
94 //  return (toupper((unsigned char) a));
95 // } else {
96 //  return ((unsigned char) a);
97 // }
98 //}
99 //end kill -MM
100
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)
103 {
104         int shifted;
105
106         shifted = keycode & KEY_SHIFTED;
107         keycode &= 0xFF;
108
109         if ( keycode>=127 )
110                 return 255;
111
112         if (shifted)
113                 return shifted_ascii_table[keycode];
114         else
115                 return ascii_table[keycode];
116 }
117 //end addition -MM
118
119 void key_handler(int scancode, int press)
120 {
121         ubyte state, key_state;
122         int i, keycode, event_key;
123         Key_info *key;
124         unsigned char temp;
125
126         if (press == KEY_EVENTPRESS)
127                 key_state = 1;
128         else if (press == KEY_EVENTRELEASE)
129                 key_state = 0;
130         else
131                 return;
132
133         event_key = scancode;
134
135         //=====================================================
136         //Here a translation from win keycodes to mac keycodes!
137         //=====================================================
138
139         for (i = 255; i >= 0; i--) {
140
141                 keycode = i;
142                 key = &(key_data.keys[keycode]);
143                 if (i == event_key)
144                         state = key_state;
145                 else
146                         state = key->last_state;
147                         
148                 if ( key->last_state == state ) {
149                         if (state) {
150                                 key->counter++;
151                                 keyd_last_pressed = keycode;
152                                 keyd_time_when_last_pressed = timer_get_fixed_seconds();
153                         }
154                 } else {
155                         if (state)      {
156                                 keyd_last_pressed = keycode;
157                                 keyd_pressed[keycode] = 1;
158                                 key->downcount += state;
159                                 key->state = 1;
160                                 key->timewentdown = keyd_time_when_last_pressed = timer_get_fixed_seconds();
161                                 key->counter++;
162                         } else {        
163                                 keyd_pressed[keycode] = 0;
164                                 keyd_last_released = keycode;
165                                 key->upcount += key->state;
166                                 key->state = 0;
167                                 key->counter = 0;
168                                 key->timehelddown += timer_get_fixed_seconds() - key->timewentdown;
169                         }
170                 }
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;
186                         }
187                 }
188                 key->last_state = state;
189         }
190 }
191
192 void key_close()
193 {
194         Installed = 0;
195         keyboard_close();
196 }
197
198 void key_init()
199 {
200         if (keyboard_init())
201                 Error ("SVGAlib Keyboard Init Failed");
202         Installed=1;
203
204         keyboard_seteventhandler (key_handler);
205         keyd_time_when_last_pressed = timer_get_fixed_seconds();
206         keyd_buffer_type = 1;
207         keyd_repeat = 1;
208
209 // Clear the keyboard array
210         key_flush();
211         atexit(key_close);
212 }
213
214 void key_flush()
215 {
216         int i;
217         fix curtime;
218
219         if (!Installed)
220                 key_init();
221
222         key_data.keyhead = key_data.keytail = 0;
223
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;
228         }
229
230 //use gettimeofday here:
231         curtime = timer_get_fixed_seconds();
232
233         for (i=0; i<256; i++ )  {
234                 keyd_pressed[i] = 0;
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;
242         }
243 }
244
245 int add_one(int n)
246 {
247  n++;
248  if ( n >= KEY_BUFFER_SIZE ) n=0;
249  return n;
250 }
251
252 int key_checkch()
253 {
254         int is_one_waiting = 0;
255         event_poll();
256         if (key_data.keytail!=key_data.keyhead)
257                 is_one_waiting = 1;
258         return is_one_waiting;
259 }
260
261 int key_inkey()
262 {
263         int key = 0;
264         if (!Installed)
265                 key_init();
266         event_poll();
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);
270         }
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
274         return key;
275 }
276
277 int key_inkey_time(fix * time)
278 {
279         int key = 0;
280
281         if (!Installed)
282                 key_init();
283         event_poll();
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);
288         }
289         return key;
290 }
291
292 int key_peekkey()
293 {
294         int key = 0;
295         event_poll();
296         if (key_data.keytail!=key_data.keyhead)
297                 key = key_data.keybuffer[key_data.keyhead];
298
299         return key;
300 }
301
302 int key_getch()
303 {
304         int dummy=0;
305
306         if (!Installed)
307                 return 0;
308 //              return getch();
309
310         while (!key_checkch())
311                 dummy++;
312         return key_inkey();
313 }
314
315 unsigned int key_get_shift_status()
316 {
317         unsigned int shift_status = 0;
318
319         if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
320                 shift_status |= KEY_SHIFTED;
321
322         if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
323                 shift_status |= KEY_ALTED;
324
325         if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
326                 shift_status |= KEY_CTRLED;
327
328 #ifndef NDEBUG
329         if (keyd_pressed[KEY_DELETE])
330                 shift_status |=KEY_DEBUGGED;
331 #endif
332
333         return shift_status;
334 }
335
336 // Returns the number of seconds this key has been down since last call.
337 fix key_down_time(int scancode)
338 {
339         fix time_down, time;
340
341         event_poll();
342         if ((scancode<0)|| (scancode>255)) return 0;
343
344         if (!keyd_pressed[scancode]) {
345                 time_down = key_data.keys[scancode].timehelddown;
346                 key_data.keys[scancode].timehelddown = 0;
347         } else {
348                 time = timer_get_fixed_seconds();
349                 time_down = time - key_data.keys[scancode].timewentdown;
350                 key_data.keys[scancode].timewentdown = time;
351         }
352
353         return time_down;
354 }
355
356 unsigned int key_down_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].downcount;
363         key_data.keys[scancode].downcount = 0;
364
365         return n;
366 }
367
368 unsigned int key_up_count(int scancode)
369 {
370         int n;
371         event_poll();
372         if ((scancode<0)|| (scancode>255)) return 0;
373
374         n = key_data.keys[scancode].upcount;
375         key_data.keys[scancode].upcount = 0;
376
377         return n;
378 }