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/arch/dos/key.c,v $
17 * $Author: schaffner $
18 * $Date: 2004-08-28 23:17:45 $
20 * Functions for keyboard handler.
24 //#define PASS_KEYS_TO_BIOS 1 //if set, bios gets keys
33 #include <conio.h> /* for getch */
42 _go32_dpmi_seginfo kbd_hand_info;
49 #define KEY_BUFFER_SIZE 16
52 //-------- Variable accessed by outside functions ---------
53 unsigned char keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
54 unsigned char keyd_repeat;
55 unsigned char keyd_editor_mode;
56 volatile unsigned char keyd_last_pressed;
57 volatile unsigned char keyd_last_released;
58 volatile unsigned char keyd_pressed[256];
59 volatile int keyd_time_when_last_pressed;
61 typedef struct keyboard {
62 unsigned short keybuffer[KEY_BUFFER_SIZE];
63 fix time_pressed[KEY_BUFFER_SIZE];
64 fix TimeKeyWentDown[256];
65 fix TimeKeyHeldDown[256];
66 unsigned int NumDowns[256];
67 unsigned int NumUps[256];
68 unsigned int keyhead, keytail;
73 _go32_dpmi_seginfo prev_int_9;
75 void (__interrupt __far *prev_int_9)();
79 static volatile keyboard key_data;
81 static unsigned char Installed=0;
83 unsigned char ascii_table[128] =
84 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
85 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
86 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
87 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
88 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
89 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
90 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
91 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
92 255,255,255,255,255,255,255,255 };
94 unsigned char shifted_ascii_table[128] =
95 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
96 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
97 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
98 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
99 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
100 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
101 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
102 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
103 255,255,255,255,255,255,255,255 };
105 char * key_text[256] = {
106 "","ESC","1","2","3","4","5","6","7","8","9","0","-",
107 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",
108 "P","[","]","
\83","LCTRL","A","S","D","F",
109 "G","H","J","K","L",";","'","`",
110 "LSHFT","\\","Z","X","C","V","B","N","M",",",
111 ".","/","RSHFT","PAD*","LALT","SPC",
112 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",
113 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",
114 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0",
115 "PAD.","","","","F11","F12","","","","","","","","","",
116 "","","","","","","","","","","","","","","","","","","","",
117 "","","","","","","","","","","","","","","","","","","","",
118 "","","","","","","","","","","","","","","","","","",
119 "PAD
\83","RCTRL","","","","","","","","","","","","","",
120 "","","","","","","","","","","PAD/","","","RALT","",
121 "","","","","","","","","","","","","","HOME","
\82","PGUP",
122 "","
\81","","
\7f","","END","
\80","PGDN","INS",
123 "DEL","","","","","","","","","","","","","","","","","",
124 "","","","","","","","","","","","","","","","","","","","",
125 "","","","","","","" };
127 unsigned char key_to_ascii(int keycode )
131 shifted = keycode & KEY_SHIFTED;
138 return shifted_ascii_table[keycode];
140 return ascii_table[keycode];
143 void key_clear_bios_buffer_all()
146 // Clear keyboard buffer...
147 *(ushort *)0x41a=*(ushort *)0x41c;
148 // Clear the status bits...
152 _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
153 _farpokeb(_dos_ds,0x417, 0);
154 _farpokeb(_dos_ds,0x418, 0);
158 void key_clear_bios_buffer()
161 // Clear keyboard buffer...
162 *(ushort *)0x41a=*(ushort *)0x41c;
164 _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
175 // Clear the BIOS buffer
176 key_clear_bios_buffer();
178 key_data.keyhead = key_data.keytail = 0;
180 //Clear the keyboard buffer
181 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
182 key_data.keybuffer[i] = 0;
183 key_data.time_pressed[i] = 0;
186 //Clear the keyboard array
188 CurTime =timer_get_fixed_secondsX();
190 for (i=0; i<256; i++ ) {
192 key_data.TimeKeyWentDown[i] = CurTime;
193 key_data.TimeKeyHeldDown[i] = 0;
194 key_data.NumDowns[i]=0;
195 key_data.NumUps[i]=0;
203 if ( n >= KEY_BUFFER_SIZE ) n=0;
207 // Returns 1 if character waiting... 0 otherwise
210 int is_one_waiting = 0;
214 key_clear_bios_buffer();
216 if (key_data.keytail!=key_data.keyhead)
219 return is_one_waiting;
228 key_clear_bios_buffer();
230 if (key_data.keytail!=key_data.keyhead) {
231 key = key_data.keybuffer[key_data.keyhead];
232 key_data.keyhead = add_one(key_data.keyhead);
238 int key_inkey_time(fix * time)
244 key_clear_bios_buffer();
246 if (key_data.keytail!=key_data.keyhead) {
247 key = key_data.keybuffer[key_data.keyhead];
248 *time = key_data.time_pressed[key_data.keyhead];
249 key_data.keyhead = add_one(key_data.keyhead);
263 key_clear_bios_buffer();
265 if (key_data.keytail!=key_data.keyhead) {
266 key = key_data.keybuffer[key_data.keyhead];
272 // If not installed, uses BIOS and returns getch();
273 // Else returns pending key (or waits for one if none waiting).
281 while (!key_checkch())
286 unsigned int key_get_shift_status()
288 unsigned int shift_status = 0;
292 key_clear_bios_buffer();
294 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
295 shift_status |= KEY_SHIFTED;
297 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
298 shift_status |= KEY_ALTED;
300 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
301 shift_status |= KEY_CTRLED;
304 if (keyd_pressed[KEY_DELETE])
305 shift_status |=KEY_DEBUGGED;
313 // Returns the number of seconds this key has been down since last call.
314 fix key_down_time(int scancode) {
317 if ((scancode<0)|| (scancode>255)) return 0;
320 if (keyd_editor_mode && key_get_shift_status() )
326 if ( !keyd_pressed[scancode] ) {
327 time_down = key_data.TimeKeyHeldDown[scancode];
328 key_data.TimeKeyHeldDown[scancode] = 0;
330 time = timer_get_fixed_secondsX();
331 time_down = time - key_data.TimeKeyWentDown[scancode];
332 key_data.TimeKeyWentDown[scancode] = time;
339 // Returns number of times key has went from up to down since last call.
340 unsigned int key_down_count(int scancode) {
343 if ((scancode<0)|| (scancode>255)) return 0;
346 n = key_data.NumDowns[scancode];
347 key_data.NumDowns[scancode] = 0;
354 // Returns number of times key has went from down to up since last call.
355 unsigned int key_up_count(int scancode) {
358 if ((scancode<0)|| (scancode>255)) return 0;
361 n = key_data.NumUps[scancode];
362 key_data.NumUps[scancode] = 0;
368 // Use intrinsic forms so that we stay in the locked interrup code.
372 #pragma aux Int5 = "int 5";
375 #define Int5() asm volatile("int $5")
379 void __interrupt __far key_handler()
381 unsigned char scancode, breakbit, temp;
382 unsigned short keycode;
386 #ifdef __WATCOMC__ /* must have _chain_intr */
387 ubyte * MONO = (ubyte *)(0x0b0000+24*80*2);
388 if ( ((MONO[0]=='D') && (MONO[2]=='B') && (MONO[4]=='G') && (MONO[6]=='>')) ||
389 ((MONO[14]=='<') && (MONO[16]=='i') && (MONO[18]=='>') && (MONO[20]==' ') && (MONO[22]=='-')) ||
390 ((MONO[0]==200 ) && (MONO[2]==27) && (MONO[4]==17) )
392 _chain_intr( key_data.prev_int_9 );
398 scancode = inp( 0x60 );
402 key_data.E0Flag = 0x80;
405 // Parse scancode and break bit
406 if (key_data.E1Flag > 0 ) { // Special code for Pause, which is E1 1D 45 E1 9D C5
408 if ( scancode == 0x1D ) {
409 scancode = KEY_PAUSE;
411 } else if ( scancode == 0x9d ) {
412 scancode = KEY_PAUSE;
415 break; // skip this keycode
417 } else if ( scancode==0xE1 ) {
421 breakbit = scancode & 0x80; // Get make/break bit
422 scancode &= 0x7f; // Strip make/break bit off of scancode
423 scancode |= key_data.E0Flag; // Add in extended key code
425 key_data.E0Flag = 0; // Clear extended key code
429 keyd_last_released = scancode;
430 keyd_pressed[scancode] = 0;
431 key_data.NumUps[scancode]++;
433 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
434 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
435 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
437 temp |= keyd_pressed[KEY_DELETE];
438 if ( !(keyd_editor_mode && temp) )
439 #endif // NOTICE LINK TO ABOVE IF!!!!
440 key_data.TimeKeyHeldDown[scancode] += timer_get_fixed_secondsX() - key_data.TimeKeyWentDown[scancode];
443 keyd_last_pressed = scancode;
444 keyd_time_when_last_pressed = timer_get_fixed_secondsX();
445 if (!keyd_pressed[scancode]) {
447 key_data.TimeKeyWentDown[scancode] = timer_get_fixed_secondsX();
448 keyd_pressed[scancode] = 1;
449 key_data.NumDowns[scancode]++;
451 if ( (keyd_pressed[KEY_LSHIFT]) && (scancode == KEY_BACKSP) ) {
452 keyd_pressed[KEY_LSHIFT] = 0;
456 } else if (!keyd_repeat) {
457 // Don't buffer repeating key if repeat mode is off
461 if ( scancode!=0xAA ) {
464 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
465 keycode |= KEY_SHIFTED;
467 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
468 keycode |= KEY_ALTED;
470 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
471 keycode |= KEY_CTRLED;
474 if ( keyd_pressed[KEY_DELETE] )
475 keycode |= KEY_DEBUGGED;
478 temp = key_data.keytail+1;
479 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
481 if (temp!=key_data.keyhead) {
482 key_data.keybuffer[key_data.keytail] = keycode;
483 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
484 key_data.keytail = temp;
491 #ifdef PASS_KEYS_TO_BIOS
492 _chain_intr( key_data.prev_int_9 );
496 temp = inp(0x61); // Get current port 61h state
497 temp |= 0x80; // Turn on bit 7 to signal clear keybrd
498 outp( 0x61, temp ); // Send to port
499 temp &= 0x7f; // Turn off bit 7 to signal break
500 outp( 0x61, temp ); // Send to port
501 outp( 0x20, 0x20 ); // Reset interrupt controller
505 void key_handler_end() { // Dummy function to help calculate size of keyboard handler function
512 keyd_time_when_last_pressed = timer_get_fixed_seconds();
513 keyd_buffer_type = 1;
515 key_data.in_key_handler = 0;
519 // Clear the keyboard array
522 if (Installed) return;
525 //--------------- lock everything for the virtal memory ----------------------------------
526 if (!dpmi_lock_region ((void near *)key_handler, (char *)key_handler_end - (char near *)key_handler)) {
527 printf( "Error locking keyboard handler!\n" );
530 if (!dpmi_lock_region ((void *)&key_data, sizeof(keyboard))) {
531 printf( "Error locking keyboard handler's data1!\n" );
534 if (!dpmi_lock_region (&keyd_buffer_type, sizeof(char))) {
535 printf( "Error locking keyboard handler's data2!\n" );
538 if (!dpmi_lock_region (&keyd_repeat, sizeof(char))) {
539 printf( "Error locking keyboard handler's data3!\n" );
542 if (!dpmi_lock_region (&keyd_editor_mode, sizeof(char))) {
543 printf( "Error locking keyboard handler's data4!\n" );
546 if (!dpmi_lock_region ((void *)&keyd_last_pressed, sizeof(char))) {
547 printf( "Error locking keyboard handler's data5!\n" );
550 if (!dpmi_lock_region ((void *)&keyd_last_released, sizeof(char))) {
551 printf( "Error locking keyboard handler's data6!\n" );
554 if (!dpmi_lock_region ((void *)&keyd_pressed, sizeof(char)*256)) {
555 printf( "Error locking keyboard handler's data7!\n" );
558 if (!dpmi_lock_region ((void *)&keyd_time_when_last_pressed, sizeof(int))) {
559 printf( "Error locking keyboard handler's data8!\n" );
564 key_data.prev_int_9 = (void *)_dos_getvect( 9 );
565 _dos_setvect( 9, key_handler );
567 _go32_dpmi_get_protected_mode_interrupt_vector(9,
568 (_go32_dpmi_seginfo *)&key_data.prev_int_9);
569 kbd_hand_info.pm_offset = (int)key_handler;
570 kbd_hand_info.pm_selector = _my_cs();
571 _go32_dpmi_allocate_iret_wrapper(&kbd_hand_info);
572 _go32_dpmi_set_protected_mode_interrupt_vector(9, &kbd_hand_info);
580 if (!Installed) return;
584 _dos_setvect( 9, key_data.prev_int_9 );
586 _go32_dpmi_set_protected_mode_interrupt_vector(9,
587 (_go32_dpmi_seginfo *)&key_data.prev_int_9);
591 key_clear_bios_buffer_all();