This commit was generated by cvs2svn to compensate for changes in r2,
[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  * $Source: /cvs/cvsroot/d2x/arch/dos/key.c,v $
15  * $Revision: 1.1.1.1 $
16  * $Author: bradleyb $
17  * $Date: 2001-01-19 03:30:15 $
18  * 
19  * Functions for keyboard handler.
20  * 
21  * $Log: not supported by cvs2svn $
22  * Revision 1.1.1.1  1999/06/14 21:58:32  donut
23  * Import of d1x 1.37 source.
24  *
25  * Revision 1.35  1995/01/25  20:13:30  john
26  * Took out not passing keys to debugger if w10.
27  * 
28  * Revision 1.34  1995/01/14  19:19:31  john
29  * Made so when you press Shift+Baskspace, it release keys autmatically.
30  * 
31  * Revision 1.33  1994/12/13  09:21:48  john
32  * Took out keyd_editor_mode, and KEY_DEBUGGED stuff for NDEBUG versions.
33  * 
34  * Revision 1.32  1994/11/12  13:52:01  john
35  * Fixed bug with code that cleared bios buffer.
36  * 
37  * Revision 1.31  1994/10/24  15:16:16  john
38  * Added code to detect KEY_PAUSE.
39  * 
40  * Revision 1.30  1994/10/24  13:57:53  john
41  * Hacked in support for pause key onto code 0x61.
42  * 
43  * Revision 1.29  1994/10/21  15:18:13  john
44  * *** empty log message ***
45  * 
46  * Revision 1.28  1994/10/21  15:17:24  john
47  * Made LSHIFT+BACKSPACE do what PrtScr used to.
48  * 
49  * Revision 1.27  1994/09/22  16:09:18  john
50  * Fixed some virtual memory lockdown problems with timer and
51  * joystick.
52  * 
53  * Revision 1.26  1994/09/15  21:32:47  john
54  * Added bounds checking for down_count scancode
55  * parameter.
56  * 
57  * Revision 1.25  1994/08/31  12:22:20  john
58  * Added KEY_DEBUGGED
59  * 
60  * Revision 1.24  1994/08/24  18:53:48  john
61  * Made Cyberman read like normal mouse; added dpmi module; moved
62  * mouse from assembly to c. Made mouse buttons return time_down.
63  * 
64  * Revision 1.23  1994/08/18  15:17:51  john
65  * *** empty log message ***
66  * 
67  * Revision 1.22  1994/08/18  15:16:38  john
68  * fixed some bugs with clear_key_times and then
69  * removed it because i fixed key_flush to do the
70  * same.
71  * 
72  * Revision 1.21  1994/08/17  19:01:25  john
73  * Attempted to fix a bug with a key being held down
74  * key_flush called, then the key released having too 
75  * long of a time.
76  * 
77  * Revision 1.20  1994/08/08  10:43:48  john
78  * Recorded when a key was pressed for key_inkey_time.
79  * 
80  * Revision 1.19  1994/06/22  15:00:03  john
81  * Made keyboard close automatically on exit.
82  * 
83  * Revision 1.18  1994/06/21  09:16:29  john
84  * *** empty log message ***
85  * 
86  * Revision 1.17  1994/06/21  09:08:23  john
87  * *** empty log message ***
88  * 
89  * Revision 1.16  1994/06/21  09:05:01  john
90  * *** empty log message ***
91  * 
92  * Revision 1.15  1994/06/21  09:04:24  john
93  * Made PrtScreen do an int5
94  * 
95  * Revision 1.14  1994/06/17  17:17:06  john
96  * Added keyd_time_last_key_was_pressed or something like that.
97  * 
98  * Revision 1.13  1994/05/14  13:55:16  matt
99  * Added #define to control key passing to bios
100  * 
101  * Revision 1.12  1994/05/05  18:09:39  john
102  * Took out BIOS to prevent stuck keys.
103  * 
104  * Revision 1.11  1994/05/03  17:39:12  john
105  * *** empty log message ***
106  * 
107  * Revision 1.10  1994/04/29  12:14:20  john
108  * Locked all memory used during interrupts so that program
109  * won't hang when using virtual memory.
110  * 
111  * Revision 1.9  1994/04/28  23:49:41  john
112  * Made key_flush flush more keys and also did something else but i forget what.
113  * 
114  * Revision 1.8  1994/04/22  12:52:12  john
115  * *** empty log message ***
116  * 
117  * Revision 1.7  1994/04/01  10:44:59  mike
118  * Change key_getch() to call getch() if our interrupt hasn't been installed.
119  * 
120  * Revision 1.6  1994/03/09  10:45:48  john
121  * Neatend code a bit.
122  * 
123  * Revision 1.5  1994/02/17  17:24:16  john
124  * Neatened up a bit.
125  * 
126  * Revision 1.4  1994/02/17  16:30:29  john
127  * Put in code to pass keys when in debugger.
128  * 
129  * Revision 1.3  1994/02/17  15:57:59  john
130  * Made handler not chain to BIOS handler.
131  * 
132  * Revision 1.2  1994/02/17  15:56:06  john
133  * Initial version.
134  * 
135  * Revision 1.1  1994/02/17  15:54:07  john
136  * Initial revision
137  * 
138  * 
139  */
140
141 //#define PASS_KEYS_TO_BIOS     1                       //if set, bios gets keys
142
143 #ifdef RCS
144 static char rcsid[] = "$Id: key.c,v 1.1.1.1 2001-01-19 03:30:15 bradleyb Exp $";
145 #endif
146
147 #include <stdlib.h>
148 #include <stdio.h>
149 #include <conio.h>
150 #include <dos.h>
151 #include <i86.h>
152
153 //#define WATCOM_10
154 #ifdef __DJGPP__
155 #include <dpmi.h>
156 #define _far
157 #define __far
158 #define __interrupt
159 #define near
160 _go32_dpmi_seginfo kbd_hand_info;
161 #endif
162 #include "error.h"
163 #include "key.h"
164 #include "timer.h"
165 #include "u_dpmi.h"
166
167 #define KEY_BUFFER_SIZE 16
168
169
170 //-------- Variable accessed by outside functions ---------
171 unsigned char                           keyd_buffer_type;               // 0=No buffer, 1=buffer ASCII, 2=buffer scans
172 unsigned char                           keyd_repeat;
173 unsigned char                           keyd_editor_mode;
174 volatile unsigned char  keyd_last_pressed;
175 volatile unsigned char  keyd_last_released;
176 volatile unsigned char  keyd_pressed[256];
177 volatile int                            keyd_time_when_last_pressed;
178
179 typedef struct keyboard {
180         unsigned short          keybuffer[KEY_BUFFER_SIZE];
181         fix                                     time_pressed[KEY_BUFFER_SIZE];
182         fix                                     TimeKeyWentDown[256];
183         fix                                     TimeKeyHeldDown[256];
184         unsigned int            NumDowns[256];
185         unsigned int            NumUps[256];
186         unsigned int            keyhead, keytail;
187         unsigned char           E0Flag;
188         unsigned char           E1Flag;
189         int                                     in_key_handler;
190 #ifdef __DJGPP__
191         _go32_dpmi_seginfo prev_int_9;
192 #else
193         void (__interrupt __far *prev_int_9)();
194 #endif
195 } keyboard;
196
197 static volatile keyboard key_data;
198
199 static unsigned char Installed=0;
200
201 unsigned char ascii_table[128] = 
202 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
203   'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
204   'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
205   255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
206   255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
207   255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
208   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
209   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
210   255,255,255,255,255,255,255,255 };
211
212 unsigned char shifted_ascii_table[128] = 
213 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
214   'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
215   'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~', 
216   255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
217   255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
218   255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
219   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
220   255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
221   255,255,255,255,255,255,255,255 };
222
223 char * key_text[256] = {
224 "","ESC","1","2","3","4","5","6","7","8","9","0","-",
225 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",
226 "P","[","]","\83","LCTRL","A","S","D","F",
227 "G","H","J","K","L",";","'","`",
228 "LSHFT","\\","Z","X","C","V","B","N","M",",",
229 ".","/","RSHFT","PAD*","LALT","SPC",
230 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",
231 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",
232 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0",
233 "PAD.","","","","F11","F12","","","","","","","","","",
234 "","","","","","","","","","","","","","","","","","","","",
235 "","","","","","","","","","","","","","","","","","","","",
236 "","","","","","","","","","","","","","","","","","",
237 "PAD\83","RCTRL","","","","","","","","","","","","","",
238 "","","","","","","","","","","PAD/","","","RALT","",
239 "","","","","","","","","","","","","","HOME","\82","PGUP",
240 "","\81","","\7f","","END","\80","PGDN","INS",
241 "DEL","","","","","","","","","","","","","","","","","",
242 "","","","","","","","","","","","","","","","","","","","",
243 "","","","","","","" };
244
245 unsigned char key_to_ascii(int keycode )
246 {
247         int shifted;
248
249         shifted = keycode & KEY_SHIFTED;
250         keycode &= 0xFF;
251
252         if ( keycode>=127 )
253                 return 255;
254
255         if (shifted)
256                 return shifted_ascii_table[keycode];
257         else
258                 return ascii_table[keycode];
259 }
260
261 void key_clear_bios_buffer_all()
262 {
263 #ifdef __WATCOMC__
264         // Clear keyboard buffer...
265         *(ushort *)0x41a=*(ushort *)0x41c;
266         // Clear the status bits...
267         *(ubyte *)0x417 = 0;
268         *(ubyte *)0x418 = 0;
269 #else
270         _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
271         _farpokeb(_dos_ds,0x417, 0);
272         _farpokeb(_dos_ds,0x418, 0);
273 #endif
274 }
275
276 void key_clear_bios_buffer()
277 {
278 #ifdef __WATCOMC__
279         // Clear keyboard buffer...
280         *(ushort *)0x41a=*(ushort *)0x41c;
281 #else
282         _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
283 #endif
284 }
285
286 void key_flush()
287 {
288         int i;
289         fix CurTime;
290
291         _disable();
292
293         // Clear the BIOS buffer
294         key_clear_bios_buffer();
295
296         key_data.keyhead = key_data.keytail = 0;
297
298         //Clear the keyboard buffer
299         for (i=0; i<KEY_BUFFER_SIZE; i++ )      {
300                 key_data.keybuffer[i] = 0;
301                 key_data.time_pressed[i] = 0;
302         }
303         
304         //Clear the keyboard array
305
306         CurTime =timer_get_fixed_secondsX();
307
308         for (i=0; i<256; i++ )  {
309                 keyd_pressed[i] = 0;
310                 key_data.TimeKeyWentDown[i] = CurTime;
311                 key_data.TimeKeyHeldDown[i] = 0;
312                 key_data.NumDowns[i]=0;
313                 key_data.NumUps[i]=0;
314         }
315         _enable();
316 }
317
318 int add_one( int n )
319 {
320         n++;
321         if ( n >= KEY_BUFFER_SIZE ) n=0;
322         return n;
323 }
324
325 // Returns 1 if character waiting... 0 otherwise
326 int key_checkch()
327 {
328         int is_one_waiting = 0;
329
330         _disable();
331
332         key_clear_bios_buffer();
333
334         if (key_data.keytail!=key_data.keyhead)
335                 is_one_waiting = 1;
336         _enable();
337         return is_one_waiting;
338 }
339
340 int key_inkey()
341 {
342         int key = 0;
343
344         _disable();
345
346         key_clear_bios_buffer();
347
348         if (key_data.keytail!=key_data.keyhead) {
349                 key = key_data.keybuffer[key_data.keyhead];
350                 key_data.keyhead = add_one(key_data.keyhead);
351         }
352         _enable();
353         return key;
354 }
355
356 int key_inkey_time(fix * time)
357 {
358         int key = 0;
359
360         _disable();
361
362         key_clear_bios_buffer();
363
364         if (key_data.keytail!=key_data.keyhead) {
365                 key = key_data.keybuffer[key_data.keyhead];
366                 *time = key_data.time_pressed[key_data.keyhead];
367                 key_data.keyhead = add_one(key_data.keyhead);
368         }
369         _enable();
370         return key;
371 }
372
373
374
375 int key_peekkey()
376 {
377         int key = 0;
378
379         _disable();
380
381         key_clear_bios_buffer();
382
383         if (key_data.keytail!=key_data.keyhead) {
384                 key = key_data.keybuffer[key_data.keyhead];
385         }
386         _enable();
387         return key;
388 }
389
390 // If not installed, uses BIOS and returns getch();
391 //      Else returns pending key (or waits for one if none waiting).
392 int key_getch()
393 {
394         int dummy=0;
395         
396         if (!Installed)
397                 return getch();
398
399         while (!key_checkch())
400                 dummy++;
401         return key_inkey();
402 }
403
404 unsigned int key_get_shift_status()
405 {
406         unsigned int shift_status = 0;
407
408         _disable();
409
410         key_clear_bios_buffer();
411
412         if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
413                 shift_status |= KEY_SHIFTED;
414
415         if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
416                 shift_status |= KEY_ALTED;
417
418         if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
419                 shift_status |= KEY_CTRLED;
420
421 #ifndef NDEBUG
422         if (keyd_pressed[KEY_DELETE])
423                 shift_status |=KEY_DEBUGGED;
424 #endif
425
426         _enable();
427
428         return shift_status;
429 }
430
431 // Returns the number of seconds this key has been down since last call.
432 fix key_down_time(int scancode) {
433         fix time_down, time;
434
435         if ((scancode<0)|| (scancode>255)) return 0;
436
437 #ifndef NDEBUG
438         if (keyd_editor_mode && key_get_shift_status() )
439                 return 0;  
440 #endif
441
442         _disable();
443
444         if ( !keyd_pressed[scancode] )  {
445                 time_down = key_data.TimeKeyHeldDown[scancode];
446                 key_data.TimeKeyHeldDown[scancode] = 0;
447         } else  {
448                 time = timer_get_fixed_secondsX();
449                 time_down =  time - key_data.TimeKeyWentDown[scancode];
450                 key_data.TimeKeyWentDown[scancode] = time;
451         }
452         _enable();
453
454         return time_down;
455 }
456
457 // Returns number of times key has went from up to down since last call.
458 unsigned int key_down_count(int scancode)       {
459         int n;
460
461         if ((scancode<0)|| (scancode>255)) return 0;
462
463         _disable();
464         n = key_data.NumDowns[scancode];
465         key_data.NumDowns[scancode] = 0;
466         _enable();
467
468         return n;
469 }
470
471
472 // Returns number of times key has went from down to up since last call.
473 unsigned int key_up_count(int scancode) {
474         int n;
475
476         if ((scancode<0)|| (scancode>255)) return 0;
477
478         _disable();
479         n = key_data.NumUps[scancode];
480         key_data.NumUps[scancode] = 0;
481         _enable();
482
483         return n;
484 }
485
486 // Use intrinsic forms so that we stay in the locked interrup code.
487
488 #ifdef __WATCOMC__
489 void Int5();
490 #pragma aux Int5 = "int 5";
491 #else
492 #ifdef __GNUC__
493 #define Int5() asm volatile("int $5")
494 #endif
495 #endif
496
497 #pragma off (check_stack)
498 void __interrupt __far key_handler()
499 {
500         unsigned char scancode, breakbit, temp;
501         unsigned short keycode;
502
503 #ifndef WATCOM_10
504 #ifndef NDEBUG
505 #ifdef __WATCOMC__ /* must have _chain_intr */
506         ubyte * MONO = (ubyte *)(0x0b0000+24*80*2);
507         if (  ((MONO[0]=='D') && (MONO[2]=='B') && (MONO[4]=='G') && (MONO[6]=='>')) ||
508                         ((MONO[14]=='<') && (MONO[16]=='i') && (MONO[18]=='>') && (MONO[20]==' ') && (MONO[22]=='-')) ||
509                         ((MONO[0]==200 ) && (MONO[2]==27) && (MONO[4]==17) )
510                 )
511                 _chain_intr( key_data.prev_int_9 );
512 #endif
513 #endif
514 #endif
515
516         // Read in scancode
517         scancode = inp( 0x60 );
518
519         switch( scancode )      {
520         case 0xE0:
521                 key_data.E0Flag = 0x80;
522                 break;
523         default:
524                 // Parse scancode and break bit
525                 if (key_data.E1Flag > 0 )       {               // Special code for Pause, which is E1 1D 45 E1 9D C5
526                         key_data.E1Flag--;
527                         if ( scancode == 0x1D ) {
528                                 scancode        = KEY_PAUSE;
529                                 breakbit        = 0;
530                         } else if ( scancode == 0x9d ) {
531                                 scancode        = KEY_PAUSE;
532                                 breakbit        = 1;
533                         } else {
534                                 break;          // skip this keycode
535                         }
536                 } else if ( scancode==0xE1 )    {
537                         key_data.E1Flag = 2;
538                         break;
539                 } else {
540                         breakbit        = scancode & 0x80;              // Get make/break bit
541                         scancode &= 0x7f;                                               // Strip make/break bit off of scancode
542                         scancode |= key_data.E0Flag;                                    // Add in extended key code
543                 }
544                 key_data.E0Flag = 0;                                                            // Clear extended key code
545
546                 if (breakbit)   {
547                         // Key going up
548                         keyd_last_released = scancode;
549                         keyd_pressed[scancode] = 0;
550                         key_data.NumUps[scancode]++;
551                         temp = 0;
552                         temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
553                         temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
554                         temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
555 #ifndef NDEBUG
556                         temp |= keyd_pressed[KEY_DELETE];
557                         if ( !(keyd_editor_mode && temp) )
558 #endif          // NOTICE LINK TO ABOVE IF!!!!
559                                 key_data.TimeKeyHeldDown[scancode] += timer_get_fixed_secondsX() - key_data.TimeKeyWentDown[scancode];
560                 } else {
561                         // Key going down
562                         keyd_last_pressed = scancode;
563                         keyd_time_when_last_pressed = timer_get_fixed_secondsX();
564                         if (!keyd_pressed[scancode])    {
565                                 // First time down
566                                 key_data.TimeKeyWentDown[scancode] = timer_get_fixed_secondsX();
567                                 keyd_pressed[scancode] = 1;
568                                 key_data.NumDowns[scancode]++;
569 #ifndef NDEBUG
570                                 if ( (keyd_pressed[KEY_LSHIFT]) && (scancode == KEY_BACKSP) )   {
571                                         keyd_pressed[KEY_LSHIFT] = 0;
572                                         Int5();
573                                 }
574 #endif
575                         } else if (!keyd_repeat) {
576                                 // Don't buffer repeating key if repeat mode is off
577                                 scancode = 0xAA;
578                         }
579
580                         if ( scancode!=0xAA ) {
581                                 keycode = scancode;
582
583                                 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
584                                         keycode |= KEY_SHIFTED;
585
586                                 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
587                                         keycode |= KEY_ALTED;
588
589                                 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
590                                         keycode |= KEY_CTRLED;
591
592 #ifndef NDEBUG
593                                 if ( keyd_pressed[KEY_DELETE] )
594                                         keycode |= KEY_DEBUGGED;
595 #endif
596
597                                 temp = key_data.keytail+1;
598                                 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
599
600                                 if (temp!=key_data.keyhead)     {
601                                         key_data.keybuffer[key_data.keytail] = keycode;
602                                         key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
603                                         key_data.keytail = temp;
604                                 }
605                         }
606                 }
607         }
608
609 #ifndef NDEBUG
610 #ifdef PASS_KEYS_TO_BIOS
611         _chain_intr( key_data.prev_int_9 );
612 #endif
613 #endif
614
615         temp = inp(0x61);               // Get current port 61h state
616         temp |= 0x80;                   // Turn on bit 7 to signal clear keybrd
617         outp( 0x61, temp );     // Send to port
618         temp &= 0x7f;                   // Turn off bit 7 to signal break
619         outp( 0x61, temp );     // Send to port
620         outp( 0x20, 0x20 );     // Reset interrupt controller
621 }
622
623 #pragma on (check_stack)
624
625 void key_handler_end()  {               // Dummy function to help calculate size of keyboard handler function
626 }
627
628 void key_init()
629 {
630         // Initialize queue
631
632         keyd_time_when_last_pressed = timer_get_fixed_seconds();
633         keyd_buffer_type = 1;
634         keyd_repeat = 1;
635         key_data.in_key_handler = 0;
636         key_data.E0Flag = 0;
637         key_data.E1Flag = 0;
638
639         // Clear the keyboard array
640         key_flush();
641
642         if (Installed) return;
643         Installed = 1;
644
645         //--------------- lock everything for the virtal memory ----------------------------------
646         if (!dpmi_lock_region ((void near *)key_handler, (char *)key_handler_end - (char near *)key_handler))   {
647                 printf( "Error locking keyboard handler!\n" );
648                 exit(1);
649         }
650         if (!dpmi_lock_region ((void *)&key_data, sizeof(keyboard)))    {
651                 printf( "Error locking keyboard handler's data1!\n" );
652                 exit(1);
653         }
654         if (!dpmi_lock_region (&keyd_buffer_type, sizeof(char)))        {
655                 printf( "Error locking keyboard handler's data2!\n" );
656                 exit(1);
657         }
658         if (!dpmi_lock_region (&keyd_repeat, sizeof(char)))     {
659                 printf( "Error locking keyboard handler's data3!\n" );
660                 exit(1);
661         }
662         if (!dpmi_lock_region (&keyd_editor_mode, sizeof(char)))        {
663                 printf( "Error locking keyboard handler's data4!\n" );
664                 exit(1);
665         }
666         if (!dpmi_lock_region ((void *)&keyd_last_pressed, sizeof(char)))       {
667                 printf( "Error locking keyboard handler's data5!\n" );
668                 exit(1);
669         }
670         if (!dpmi_lock_region ((void *)&keyd_last_released, sizeof(char)))      {
671                 printf( "Error locking keyboard handler's data6!\n" );
672                 exit(1);
673         }
674         if (!dpmi_lock_region ((void *)&keyd_pressed, sizeof(char)*256))        {
675                 printf( "Error locking keyboard handler's data7!\n" );
676                 exit(1);
677         }
678         if (!dpmi_lock_region ((void *)&keyd_time_when_last_pressed, sizeof(int)))      {
679                 printf( "Error locking keyboard handler's data8!\n" );
680                 exit(1);
681         }
682
683 #ifndef __DJGPP__
684         key_data.prev_int_9 = (void *)_dos_getvect( 9 );
685     _dos_setvect( 9, key_handler );
686 #else
687         _go32_dpmi_get_protected_mode_interrupt_vector(9,
688          (_go32_dpmi_seginfo *)&key_data.prev_int_9);
689         kbd_hand_info.pm_offset = (int)key_handler;
690         kbd_hand_info.pm_selector = _my_cs();
691         _go32_dpmi_allocate_iret_wrapper(&kbd_hand_info);
692         _go32_dpmi_set_protected_mode_interrupt_vector(9, &kbd_hand_info);
693 #endif
694
695         atexit( key_close );
696 }
697
698 void key_close()
699 {
700         if (!Installed) return;
701         Installed = 0;
702         
703 #ifndef __DJGPP__
704         _dos_setvect( 9, key_data.prev_int_9 );
705 #else
706         _go32_dpmi_set_protected_mode_interrupt_vector(9,
707          (_go32_dpmi_seginfo *)&key_data.prev_int_9);
708 #endif
709
710         _disable();
711         key_clear_bios_buffer_all();
712         _enable();
713
714 }