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.
15 * $Source: /cvs/cvsroot/d2x/input/dos_key.c,v $
18 * $Date: 2001-01-29 14:03:57 $
20 * Functions for keyboard handler.
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
26 * Revision 1.1.1.1 1999/06/14 21:58:32 donut
27 * Import of d1x 1.37 source.
29 * Revision 1.35 1995/01/25 20:13:30 john
30 * Took out not passing keys to debugger if w10.
32 * Revision 1.34 1995/01/14 19:19:31 john
33 * Made so when you press Shift+Baskspace, it release keys autmatically.
35 * Revision 1.33 1994/12/13 09:21:48 john
36 * Took out keyd_editor_mode, and KEY_DEBUGGED stuff for NDEBUG versions.
38 * Revision 1.32 1994/11/12 13:52:01 john
39 * Fixed bug with code that cleared bios buffer.
41 * Revision 1.31 1994/10/24 15:16:16 john
42 * Added code to detect KEY_PAUSE.
44 * Revision 1.30 1994/10/24 13:57:53 john
45 * Hacked in support for pause key onto code 0x61.
47 * Revision 1.29 1994/10/21 15:18:13 john
48 * *** empty log message ***
50 * Revision 1.28 1994/10/21 15:17:24 john
51 * Made LSHIFT+BACKSPACE do what PrtScr used to.
53 * Revision 1.27 1994/09/22 16:09:18 john
54 * Fixed some virtual memory lockdown problems with timer and
57 * Revision 1.26 1994/09/15 21:32:47 john
58 * Added bounds checking for down_count scancode
61 * Revision 1.25 1994/08/31 12:22:20 john
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.
68 * Revision 1.23 1994/08/18 15:17:51 john
69 * *** empty log message ***
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
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
81 * Revision 1.20 1994/08/08 10:43:48 john
82 * Recorded when a key was pressed for key_inkey_time.
84 * Revision 1.19 1994/06/22 15:00:03 john
85 * Made keyboard close automatically on exit.
87 * Revision 1.18 1994/06/21 09:16:29 john
88 * *** empty log message ***
90 * Revision 1.17 1994/06/21 09:08:23 john
91 * *** empty log message ***
93 * Revision 1.16 1994/06/21 09:05:01 john
94 * *** empty log message ***
96 * Revision 1.15 1994/06/21 09:04:24 john
97 * Made PrtScreen do an int5
99 * Revision 1.14 1994/06/17 17:17:06 john
100 * Added keyd_time_last_key_was_pressed or something like that.
102 * Revision 1.13 1994/05/14 13:55:16 matt
103 * Added #define to control key passing to bios
105 * Revision 1.12 1994/05/05 18:09:39 john
106 * Took out BIOS to prevent stuck keys.
108 * Revision 1.11 1994/05/03 17:39:12 john
109 * *** empty log message ***
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.
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.
118 * Revision 1.8 1994/04/22 12:52:12 john
119 * *** empty log message ***
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.
124 * Revision 1.6 1994/03/09 10:45:48 john
125 * Neatend code a bit.
127 * Revision 1.5 1994/02/17 17:24:16 john
130 * Revision 1.4 1994/02/17 16:30:29 john
131 * Put in code to pass keys when in debugger.
133 * Revision 1.3 1994/02/17 15:57:59 john
134 * Made handler not chain to BIOS handler.
136 * Revision 1.2 1994/02/17 15:56:06 john
139 * Revision 1.1 1994/02/17 15:54:07 john
145 //#define PASS_KEYS_TO_BIOS 1 //if set, bios gets keys
162 _go32_dpmi_seginfo kbd_hand_info;
169 #define KEY_BUFFER_SIZE 16
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;
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;
193 _go32_dpmi_seginfo prev_int_9;
195 void (__interrupt __far *prev_int_9)();
199 static volatile keyboard key_data;
201 static unsigned char Installed=0;
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 };
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 };
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 "","","","","","","" };
247 unsigned char key_to_ascii(int keycode )
251 shifted = keycode & KEY_SHIFTED;
258 return shifted_ascii_table[keycode];
260 return ascii_table[keycode];
263 void key_clear_bios_buffer_all()
266 // Clear keyboard buffer...
267 *(ushort *)0x41a=*(ushort *)0x41c;
268 // Clear the status bits...
272 _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
273 _farpokeb(_dos_ds,0x417, 0);
274 _farpokeb(_dos_ds,0x418, 0);
278 void key_clear_bios_buffer()
281 // Clear keyboard buffer...
282 *(ushort *)0x41a=*(ushort *)0x41c;
284 _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
295 // Clear the BIOS buffer
296 key_clear_bios_buffer();
298 key_data.keyhead = key_data.keytail = 0;
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;
306 //Clear the keyboard array
308 CurTime =timer_get_fixed_secondsX();
310 for (i=0; i<256; i++ ) {
312 key_data.TimeKeyWentDown[i] = CurTime;
313 key_data.TimeKeyHeldDown[i] = 0;
314 key_data.NumDowns[i]=0;
315 key_data.NumUps[i]=0;
323 if ( n >= KEY_BUFFER_SIZE ) n=0;
327 // Returns 1 if character waiting... 0 otherwise
330 int is_one_waiting = 0;
334 key_clear_bios_buffer();
336 if (key_data.keytail!=key_data.keyhead)
339 return is_one_waiting;
348 key_clear_bios_buffer();
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);
358 int key_inkey_time(fix * time)
364 key_clear_bios_buffer();
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);
383 key_clear_bios_buffer();
385 if (key_data.keytail!=key_data.keyhead) {
386 key = key_data.keybuffer[key_data.keyhead];
392 // If not installed, uses BIOS and returns getch();
393 // Else returns pending key (or waits for one if none waiting).
401 while (!key_checkch())
406 unsigned int key_get_shift_status()
408 unsigned int shift_status = 0;
412 key_clear_bios_buffer();
414 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
415 shift_status |= KEY_SHIFTED;
417 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
418 shift_status |= KEY_ALTED;
420 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
421 shift_status |= KEY_CTRLED;
424 if (keyd_pressed[KEY_DELETE])
425 shift_status |=KEY_DEBUGGED;
433 // Returns the number of seconds this key has been down since last call.
434 fix key_down_time(int scancode) {
437 if ((scancode<0)|| (scancode>255)) return 0;
440 if (keyd_editor_mode && key_get_shift_status() )
446 if ( !keyd_pressed[scancode] ) {
447 time_down = key_data.TimeKeyHeldDown[scancode];
448 key_data.TimeKeyHeldDown[scancode] = 0;
450 time = timer_get_fixed_secondsX();
451 time_down = time - key_data.TimeKeyWentDown[scancode];
452 key_data.TimeKeyWentDown[scancode] = time;
459 // Returns number of times key has went from up to down since last call.
460 unsigned int key_down_count(int scancode) {
463 if ((scancode<0)|| (scancode>255)) return 0;
466 n = key_data.NumDowns[scancode];
467 key_data.NumDowns[scancode] = 0;
474 // Returns number of times key has went from down to up since last call.
475 unsigned int key_up_count(int scancode) {
478 if ((scancode<0)|| (scancode>255)) return 0;
481 n = key_data.NumUps[scancode];
482 key_data.NumUps[scancode] = 0;
488 // Use intrinsic forms so that we stay in the locked interrup code.
492 #pragma aux Int5 = "int 5";
495 #define Int5() asm volatile("int $5")
499 void __interrupt __far key_handler()
501 unsigned char scancode, breakbit, temp;
502 unsigned short keycode;
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) )
512 _chain_intr( key_data.prev_int_9 );
518 scancode = inp( 0x60 );
522 key_data.E0Flag = 0x80;
525 // Parse scancode and break bit
526 if (key_data.E1Flag > 0 ) { // Special code for Pause, which is E1 1D 45 E1 9D C5
528 if ( scancode == 0x1D ) {
529 scancode = KEY_PAUSE;
531 } else if ( scancode == 0x9d ) {
532 scancode = KEY_PAUSE;
535 break; // skip this keycode
537 } else if ( scancode==0xE1 ) {
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
545 key_data.E0Flag = 0; // Clear extended key code
549 keyd_last_released = scancode;
550 keyd_pressed[scancode] = 0;
551 key_data.NumUps[scancode]++;
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];
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];
563 keyd_last_pressed = scancode;
564 keyd_time_when_last_pressed = timer_get_fixed_secondsX();
565 if (!keyd_pressed[scancode]) {
567 key_data.TimeKeyWentDown[scancode] = timer_get_fixed_secondsX();
568 keyd_pressed[scancode] = 1;
569 key_data.NumDowns[scancode]++;
571 if ( (keyd_pressed[KEY_LSHIFT]) && (scancode == KEY_BACKSP) ) {
572 keyd_pressed[KEY_LSHIFT] = 0;
576 } else if (!keyd_repeat) {
577 // Don't buffer repeating key if repeat mode is off
581 if ( scancode!=0xAA ) {
584 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
585 keycode |= KEY_SHIFTED;
587 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
588 keycode |= KEY_ALTED;
590 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
591 keycode |= KEY_CTRLED;
594 if ( keyd_pressed[KEY_DELETE] )
595 keycode |= KEY_DEBUGGED;
598 temp = key_data.keytail+1;
599 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
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;
611 #ifdef PASS_KEYS_TO_BIOS
612 _chain_intr( key_data.prev_int_9 );
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
625 void key_handler_end() { // Dummy function to help calculate size of keyboard handler function
632 keyd_time_when_last_pressed = timer_get_fixed_seconds();
633 keyd_buffer_type = 1;
635 key_data.in_key_handler = 0;
639 // Clear the keyboard array
642 if (Installed) return;
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" );
650 if (!dpmi_lock_region ((void *)&key_data, sizeof(keyboard))) {
651 printf( "Error locking keyboard handler's data1!\n" );
654 if (!dpmi_lock_region (&keyd_buffer_type, sizeof(char))) {
655 printf( "Error locking keyboard handler's data2!\n" );
658 if (!dpmi_lock_region (&keyd_repeat, sizeof(char))) {
659 printf( "Error locking keyboard handler's data3!\n" );
662 if (!dpmi_lock_region (&keyd_editor_mode, sizeof(char))) {
663 printf( "Error locking keyboard handler's data4!\n" );
666 if (!dpmi_lock_region ((void *)&keyd_last_pressed, sizeof(char))) {
667 printf( "Error locking keyboard handler's data5!\n" );
670 if (!dpmi_lock_region ((void *)&keyd_last_released, sizeof(char))) {
671 printf( "Error locking keyboard handler's data6!\n" );
674 if (!dpmi_lock_region ((void *)&keyd_pressed, sizeof(char)*256)) {
675 printf( "Error locking keyboard handler's data7!\n" );
678 if (!dpmi_lock_region ((void *)&keyd_time_when_last_pressed, sizeof(int))) {
679 printf( "Error locking keyboard handler's data8!\n" );
684 key_data.prev_int_9 = (void *)_dos_getvect( 9 );
685 _dos_setvect( 9, key_handler );
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);
700 if (!Installed) return;
704 _dos_setvect( 9, key_data.prev_int_9 );
706 _go32_dpmi_set_protected_mode_interrupt_vector(9,
707 (_go32_dpmi_seginfo *)&key_data.prev_int_9);
711 key_clear_bios_buffer_all();