]> icculus.org git repositories - btb/d2x.git/blob - arch/dos/key.c
use the orientation parameter of g3_draw_bitmap
[btb/d2x.git] / arch / dos / key.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  * 
16  * Functions for keyboard handler.
17  * 
18  */
19
20 //#define PASS_KEYS_TO_BIOS     1                       //if set, bios gets keys
21
22 #ifdef HAVE_CONFIG_H
23 #include <conf.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <dos.h>
29 #include <conio.h> /* for getch */
30
31 //#define WATCOM_10
32 #ifdef __DJGPP__
33 #include <dpmi.h>
34 #define _far
35 #define __far
36 #define __interrupt
37 #define near
38 _go32_dpmi_seginfo kbd_hand_info;
39 #endif
40 #include "dxxerror.h"
41 #include "key.h"
42 #include "timer.h"
43 #include "u_dpmi.h"
44
45 #define KEY_BUFFER_SIZE 16
46
47
48 //-------- Variable accessed by outside functions ---------
49 unsigned char                           keyd_buffer_type;               // 0=No buffer, 1=buffer ASCII, 2=buffer scans
50 unsigned char                           keyd_repeat;
51 unsigned char                           keyd_editor_mode;
52 volatile unsigned char  keyd_last_pressed;
53 volatile unsigned char  keyd_last_released;
54 volatile unsigned char  keyd_pressed[256];
55 volatile int                            keyd_time_when_last_pressed;
56
57 typedef struct keyboard {
58         unsigned short          keybuffer[KEY_BUFFER_SIZE];
59         fix                                     time_pressed[KEY_BUFFER_SIZE];
60         fix                                     TimeKeyWentDown[256];
61         fix                                     TimeKeyHeldDown[256];
62         unsigned int            NumDowns[256];
63         unsigned int            NumUps[256];
64         unsigned int            keyhead, keytail;
65         unsigned char           E0Flag;
66         unsigned char           E1Flag;
67         int                                     in_key_handler;
68 #ifdef __DJGPP__
69         _go32_dpmi_seginfo prev_int_9;
70 #else
71         void (__interrupt __far *prev_int_9)();
72 #endif
73 } keyboard;
74
75 static volatile keyboard key_data;
76
77 static unsigned char Installed=0;
78
79 unsigned char ascii_table[128] = 
80 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
81   'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
82   'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
83   255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
84   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,255,255,
86   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
87   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
88   255,255,255,255,255,255,255,255 };
89
90 unsigned char shifted_ascii_table[128] = 
91 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
92   'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
93   'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 
94   255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
95   255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
96   255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
97   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
98   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
99   255,255,255,255,255,255,255,255 };
100 /*
101 char * key_text[256] = {
102 "","ESC","1","2","3","4","5","6","7","8","9","0","-",
103 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",
104 "P","[","]","\83","LCTRL","A","S","D","F",
105 "G","H","J","K","L",";","'","`",
106 "LSHFT","\\","Z","X","C","V","B","N","M",",",
107 ".","/","RSHFT","PAD*","LALT","SPC",
108 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",
109 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",
110 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0",
111 "PAD.","","","","F11","F12","","","","","","","","","",
112 "","","","","","","","","","","","","","","","","","","","",
113 "","","","","","","","","","","","","","","","","","","","",
114 "","","","","","","","","","","","","","","","","","",
115 "PAD\83","RCTRL","","","","","","","","","","","","","",
116 "","","","","","","","","","","PAD/","","","RALT","",
117 "","","","","","","","","","","","","","HOME","\82","PGUP",
118 "","\81","","\7f","","END","\80","PGDN","INS",
119 "DEL","","","","","","","","","","","","","","","","","",
120 "","","","","","","","","","","","","","","","","","","","",
121 "","","","","","","" };
122 */
123 unsigned char key_to_ascii(int keycode )
124 {
125         int shifted;
126
127         shifted = keycode & KEY_SHIFTED;
128         keycode &= 0xFF;
129
130         if ( keycode>=127 )
131                 return 255;
132
133         if (shifted)
134                 return shifted_ascii_table[keycode];
135         else
136                 return ascii_table[keycode];
137 }
138
139 void key_clear_bios_buffer_all()
140 {
141 #ifdef __WATCOMC__
142         // Clear keyboard buffer...
143         *(ushort *)0x41a=*(ushort *)0x41c;
144         // Clear the status bits...
145         *(ubyte *)0x417 = 0;
146         *(ubyte *)0x418 = 0;
147 #else
148         _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
149         _farpokeb(_dos_ds,0x417, 0);
150         _farpokeb(_dos_ds,0x418, 0);
151 #endif
152 }
153
154 void key_clear_bios_buffer()
155 {
156 #ifdef __WATCOMC__
157         // Clear keyboard buffer...
158         *(ushort *)0x41a=*(ushort *)0x41c;
159 #else
160         _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
161 #endif
162 }
163
164 void key_flush()
165 {
166         int i;
167         fix CurTime;
168
169         _disable();
170
171         // Clear the BIOS buffer
172         key_clear_bios_buffer();
173
174         key_data.keyhead = key_data.keytail = 0;
175
176         //Clear the keyboard buffer
177         for (i=0; i<KEY_BUFFER_SIZE; i++ )      {
178                 key_data.keybuffer[i] = 0;
179                 key_data.time_pressed[i] = 0;
180         }
181         
182         //Clear the keyboard array
183
184         CurTime =timer_get_fixed_secondsX();
185
186         for (i=0; i<256; i++ )  {
187                 keyd_pressed[i] = 0;
188                 key_data.TimeKeyWentDown[i] = CurTime;
189                 key_data.TimeKeyHeldDown[i] = 0;
190                 key_data.NumDowns[i]=0;
191                 key_data.NumUps[i]=0;
192         }
193         _enable();
194 }
195
196 int add_one( int n )
197 {
198         n++;
199         if ( n >= KEY_BUFFER_SIZE ) n=0;
200         return n;
201 }
202
203 // Returns 1 if character waiting... 0 otherwise
204 int key_checkch()
205 {
206         int is_one_waiting = 0;
207
208         _disable();
209
210         key_clear_bios_buffer();
211
212         if (key_data.keytail!=key_data.keyhead)
213                 is_one_waiting = 1;
214         _enable();
215         return is_one_waiting;
216 }
217
218 int key_inkey()
219 {
220         int key = 0;
221
222         _disable();
223
224         key_clear_bios_buffer();
225
226         if (key_data.keytail!=key_data.keyhead) {
227                 key = key_data.keybuffer[key_data.keyhead];
228                 key_data.keyhead = add_one(key_data.keyhead);
229         }
230         _enable();
231         return key;
232 }
233
234 int key_inkey_time(fix * time)
235 {
236         int key = 0;
237
238         _disable();
239
240         key_clear_bios_buffer();
241
242         if (key_data.keytail!=key_data.keyhead) {
243                 key = key_data.keybuffer[key_data.keyhead];
244                 *time = key_data.time_pressed[key_data.keyhead];
245                 key_data.keyhead = add_one(key_data.keyhead);
246         }
247         _enable();
248         return key;
249 }
250
251
252
253 int key_peekkey()
254 {
255         int key = 0;
256
257         _disable();
258
259         key_clear_bios_buffer();
260
261         if (key_data.keytail!=key_data.keyhead) {
262                 key = key_data.keybuffer[key_data.keyhead];
263         }
264         _enable();
265         return key;
266 }
267
268 // If not installed, uses BIOS and returns getch();
269 //      Else returns pending key (or waits for one if none waiting).
270 int key_getch()
271 {
272         int dummy=0;
273         
274         if (!Installed)
275                 return getch();
276
277         while (!key_checkch())
278                 dummy++;
279         return key_inkey();
280 }
281
282 unsigned int key_get_shift_status()
283 {
284         unsigned int shift_status = 0;
285
286         _disable();
287
288         key_clear_bios_buffer();
289
290         if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
291                 shift_status |= KEY_SHIFTED;
292
293         if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
294                 shift_status |= KEY_ALTED;
295
296         if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
297                 shift_status |= KEY_CTRLED;
298
299 #ifndef NDEBUG
300         if (keyd_pressed[KEY_DELETE])
301                 shift_status |=KEY_DEBUGGED;
302 #endif
303
304         _enable();
305
306         return shift_status;
307 }
308
309 // Returns the number of seconds this key has been down since last call.
310 fix key_down_time(int scancode) {
311         fix time_down, time;
312
313         if ((scancode<0)|| (scancode>255)) return 0;
314
315 #ifndef NDEBUG
316         if (keyd_editor_mode && key_get_shift_status() )
317                 return 0;  
318 #endif
319
320         _disable();
321
322         if ( !keyd_pressed[scancode] )  {
323                 time_down = key_data.TimeKeyHeldDown[scancode];
324                 key_data.TimeKeyHeldDown[scancode] = 0;
325         } else  {
326                 time = timer_get_fixed_secondsX();
327                 time_down =  time - key_data.TimeKeyWentDown[scancode];
328                 key_data.TimeKeyWentDown[scancode] = time;
329         }
330         _enable();
331
332         return time_down;
333 }
334
335 // Returns number of times key has went from up to down since last call.
336 unsigned int key_down_count(int scancode)       {
337         int n;
338
339         if ((scancode<0)|| (scancode>255)) return 0;
340
341         _disable();
342         n = key_data.NumDowns[scancode];
343         key_data.NumDowns[scancode] = 0;
344         _enable();
345
346         return n;
347 }
348
349
350 // Returns number of times key has went from down to up since last call.
351 unsigned int key_up_count(int scancode) {
352         int n;
353
354         if ((scancode<0)|| (scancode>255)) return 0;
355
356         _disable();
357         n = key_data.NumUps[scancode];
358         key_data.NumUps[scancode] = 0;
359         _enable();
360
361         return n;
362 }
363
364 // Use intrinsic forms so that we stay in the locked interrup code.
365
366 #ifdef __WATCOMC__
367 void Int5();
368 #pragma aux Int5 = "int 5";
369 #else
370 #ifdef __GNUC__
371 #define Int5() asm volatile("int $5")
372 #endif
373 #endif
374
375 void __interrupt __far key_handler()
376 {
377         unsigned char scancode, breakbit, temp;
378         unsigned short keycode;
379
380 #ifndef WATCOM_10
381 #ifndef NDEBUG
382 #ifdef __WATCOMC__ /* must have _chain_intr */
383         ubyte * MONO = (ubyte *)(0x0b0000+24*80*2);
384         if (  ((MONO[0]=='D') && (MONO[2]=='B') && (MONO[4]=='G') && (MONO[6]=='>')) ||
385                         ((MONO[14]=='<') && (MONO[16]=='i') && (MONO[18]=='>') && (MONO[20]==' ') && (MONO[22]=='-')) ||
386                         ((MONO[0]==200 ) && (MONO[2]==27) && (MONO[4]==17) )
387                 )
388                 _chain_intr( key_data.prev_int_9 );
389 #endif
390 #endif
391 #endif
392
393         // Read in scancode
394         scancode = inp( 0x60 );
395
396         switch( scancode )      {
397         case 0xE0:
398                 key_data.E0Flag = 0x80;
399                 break;
400         default:
401                 // Parse scancode and break bit
402                 if (key_data.E1Flag > 0 )       {               // Special code for Pause, which is E1 1D 45 E1 9D C5
403                         key_data.E1Flag--;
404                         if ( scancode == 0x1D ) {
405                                 scancode        = KEY_PAUSE;
406                                 breakbit        = 0;
407                         } else if ( scancode == 0x9d ) {
408                                 scancode        = KEY_PAUSE;
409                                 breakbit        = 1;
410                         } else {
411                                 break;          // skip this keycode
412                         }
413                 } else if ( scancode==0xE1 )    {
414                         key_data.E1Flag = 2;
415                         break;
416                 } else {
417                         breakbit        = scancode & 0x80;              // Get make/break bit
418                         scancode &= 0x7f;                                               // Strip make/break bit off of scancode
419                         scancode |= key_data.E0Flag;                                    // Add in extended key code
420                 }
421                 key_data.E0Flag = 0;                                                            // Clear extended key code
422
423                 if (breakbit)   {
424                         // Key going up
425                         keyd_last_released = scancode;
426                         keyd_pressed[scancode] = 0;
427                         key_data.NumUps[scancode]++;
428                         temp = 0;
429                         temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
430                         temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
431                         temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
432 #ifndef NDEBUG
433                         temp |= keyd_pressed[KEY_DELETE];
434                         if ( !(keyd_editor_mode && temp) )
435 #endif          // NOTICE LINK TO ABOVE IF!!!!
436                                 key_data.TimeKeyHeldDown[scancode] += timer_get_fixed_secondsX() - key_data.TimeKeyWentDown[scancode];
437                 } else {
438                         // Key going down
439                         keyd_last_pressed = scancode;
440                         keyd_time_when_last_pressed = timer_get_fixed_secondsX();
441                         if (!keyd_pressed[scancode])    {
442                                 // First time down
443                                 key_data.TimeKeyWentDown[scancode] = timer_get_fixed_secondsX();
444                                 keyd_pressed[scancode] = 1;
445                                 key_data.NumDowns[scancode]++;
446 #ifndef NDEBUG
447                                 if ( (keyd_pressed[KEY_LSHIFT]) && (scancode == KEY_BACKSP) )   {
448                                         keyd_pressed[KEY_LSHIFT] = 0;
449                                         Int5();
450                                 }
451 #endif
452                         } else if (!keyd_repeat) {
453                                 // Don't buffer repeating key if repeat mode is off
454                                 scancode = 0xAA;
455                         }
456
457                         if ( scancode!=0xAA ) {
458                                 keycode = scancode;
459
460                                 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
461                                         keycode |= KEY_SHIFTED;
462
463                                 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
464                                         keycode |= KEY_ALTED;
465
466                                 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
467                                         keycode |= KEY_CTRLED;
468
469 #ifndef NDEBUG
470                                 if ( keyd_pressed[KEY_DELETE] )
471                                         keycode |= KEY_DEBUGGED;
472 #endif
473
474                                 temp = key_data.keytail+1;
475                                 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
476
477                                 if (temp!=key_data.keyhead)     {
478                                         key_data.keybuffer[key_data.keytail] = keycode;
479                                         key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
480                                         key_data.keytail = temp;
481                                 }
482                         }
483                 }
484         }
485
486 #ifndef NDEBUG
487 #ifdef PASS_KEYS_TO_BIOS
488         _chain_intr( key_data.prev_int_9 );
489 #endif
490 #endif
491
492         temp = inp(0x61);               // Get current port 61h state
493         temp |= 0x80;                   // Turn on bit 7 to signal clear keybrd
494         outp( 0x61, temp );     // Send to port
495         temp &= 0x7f;                   // Turn off bit 7 to signal break
496         outp( 0x61, temp );     // Send to port
497         outp( 0x20, 0x20 );     // Reset interrupt controller
498 }
499
500
501 void key_handler_end()  {               // Dummy function to help calculate size of keyboard handler function
502 }
503
504 void key_init()
505 {
506         // Initialize queue
507
508         keyd_time_when_last_pressed = timer_get_fixed_seconds();
509         keyd_buffer_type = 1;
510         keyd_repeat = 1;
511         key_data.in_key_handler = 0;
512         key_data.E0Flag = 0;
513         key_data.E1Flag = 0;
514
515         // Clear the keyboard array
516         key_flush();
517
518         if (Installed) return;
519         Installed = 1;
520
521         //--------------- lock everything for the virtal memory ----------------------------------
522         if (!dpmi_lock_region ((void near *)key_handler, (char *)key_handler_end - (char near *)key_handler))   {
523                 printf( "Error locking keyboard handler!\n" );
524                 exit(1);
525         }
526         if (!dpmi_lock_region ((void *)&key_data, sizeof(keyboard)))    {
527                 printf( "Error locking keyboard handler's data1!\n" );
528                 exit(1);
529         }
530         if (!dpmi_lock_region (&keyd_buffer_type, sizeof(char)))        {
531                 printf( "Error locking keyboard handler's data2!\n" );
532                 exit(1);
533         }
534         if (!dpmi_lock_region (&keyd_repeat, sizeof(char)))     {
535                 printf( "Error locking keyboard handler's data3!\n" );
536                 exit(1);
537         }
538         if (!dpmi_lock_region (&keyd_editor_mode, sizeof(char)))        {
539                 printf( "Error locking keyboard handler's data4!\n" );
540                 exit(1);
541         }
542         if (!dpmi_lock_region ((void *)&keyd_last_pressed, sizeof(char)))       {
543                 printf( "Error locking keyboard handler's data5!\n" );
544                 exit(1);
545         }
546         if (!dpmi_lock_region ((void *)&keyd_last_released, sizeof(char)))      {
547                 printf( "Error locking keyboard handler's data6!\n" );
548                 exit(1);
549         }
550         if (!dpmi_lock_region ((void *)&keyd_pressed, sizeof(char)*256))        {
551                 printf( "Error locking keyboard handler's data7!\n" );
552                 exit(1);
553         }
554         if (!dpmi_lock_region ((void *)&keyd_time_when_last_pressed, sizeof(int)))      {
555                 printf( "Error locking keyboard handler's data8!\n" );
556                 exit(1);
557         }
558
559 #ifndef __DJGPP__
560         key_data.prev_int_9 = (void *)_dos_getvect( 9 );
561     _dos_setvect( 9, key_handler );
562 #else
563         _go32_dpmi_get_protected_mode_interrupt_vector(9,
564          (_go32_dpmi_seginfo *)&key_data.prev_int_9);
565         kbd_hand_info.pm_offset = (int)key_handler;
566         kbd_hand_info.pm_selector = _my_cs();
567         _go32_dpmi_allocate_iret_wrapper(&kbd_hand_info);
568         _go32_dpmi_set_protected_mode_interrupt_vector(9, &kbd_hand_info);
569 #endif
570
571         atexit( key_close );
572 }
573
574 void key_close()
575 {
576         if (!Installed) return;
577         Installed = 0;
578         
579 #ifndef __DJGPP__
580         _dos_setvect( 9, key_data.prev_int_9 );
581 #else
582         _go32_dpmi_set_protected_mode_interrupt_vector(9,
583          (_go32_dpmi_seginfo *)&key_data.prev_int_9);
584 #endif
585
586         _disable();
587         key_clear_bios_buffer_all();
588         _enable();
589
590 }