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