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