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