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.
16 * Functions for keyboard handler.
20 //#define PASS_KEYS_TO_BIOS 1 //if set, bios gets keys
29 #include <conio.h> /* for getch */
38 _go32_dpmi_seginfo kbd_hand_info;
45 #define KEY_BUFFER_SIZE 16
48 //-------- Variable accessed by outside functions ---------
49 unsigned char keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
50 unsigned char keyd_repeat;
51 unsigned char keyd_editor_mode;
52 volatile unsigned char keyd_last_pressed;
53 volatile unsigned char keyd_last_released;
54 volatile unsigned char keyd_pressed[256];
55 volatile int keyd_time_when_last_pressed;
57 typedef struct keyboard {
58 unsigned short keybuffer[KEY_BUFFER_SIZE];
59 fix time_pressed[KEY_BUFFER_SIZE];
60 fix TimeKeyWentDown[256];
61 fix TimeKeyHeldDown[256];
62 unsigned int NumDowns[256];
63 unsigned int NumUps[256];
64 unsigned int keyhead, keytail;
69 _go32_dpmi_seginfo prev_int_9;
71 void (__interrupt __far *prev_int_9)();
75 static volatile keyboard key_data;
77 static unsigned char Installed=0;
79 unsigned char ascii_table[128] =
80 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
81 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
82 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
83 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
84 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
85 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
86 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
87 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
88 255,255,255,255,255,255,255,255 };
90 unsigned char shifted_ascii_table[128] =
91 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
92 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
93 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
94 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
95 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
96 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
97 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
98 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
99 255,255,255,255,255,255,255,255 };
101 char * key_text[256] = {
102 "","ESC","1","2","3","4","5","6","7","8","9","0","-",
103 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",
104 "P","[","]","
\83","LCTRL","A","S","D","F",
105 "G","H","J","K","L",";","'","`",
106 "LSHFT","\\","Z","X","C","V","B","N","M",",",
107 ".","/","RSHFT","PAD*","LALT","SPC",
108 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",
109 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",
110 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0",
111 "PAD.","","","","F11","F12","","","","","","","","","",
112 "","","","","","","","","","","","","","","","","","","","",
113 "","","","","","","","","","","","","","","","","","","","",
114 "","","","","","","","","","","","","","","","","","",
115 "PAD
\83","RCTRL","","","","","","","","","","","","","",
116 "","","","","","","","","","","PAD/","","","RALT","",
117 "","","","","","","","","","","","","","HOME","
\82","PGUP",
118 "","
\81","","
\7f","","END","
\80","PGDN","INS",
119 "DEL","","","","","","","","","","","","","","","","","",
120 "","","","","","","","","","","","","","","","","","","","",
121 "","","","","","","" };
123 unsigned char key_to_ascii(int keycode )
127 shifted = keycode & KEY_SHIFTED;
134 return shifted_ascii_table[keycode];
136 return ascii_table[keycode];
139 void key_clear_bios_buffer_all()
142 // Clear keyboard buffer...
143 *(ushort *)0x41a=*(ushort *)0x41c;
144 // Clear the status bits...
148 _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
149 _farpokeb(_dos_ds,0x417, 0);
150 _farpokeb(_dos_ds,0x418, 0);
154 void key_clear_bios_buffer()
157 // Clear keyboard buffer...
158 *(ushort *)0x41a=*(ushort *)0x41c;
160 _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
171 // Clear the BIOS buffer
172 key_clear_bios_buffer();
174 key_data.keyhead = key_data.keytail = 0;
176 //Clear the keyboard buffer
177 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
178 key_data.keybuffer[i] = 0;
179 key_data.time_pressed[i] = 0;
182 //Clear the keyboard array
184 CurTime =timer_get_fixed_secondsX();
186 for (i=0; i<256; i++ ) {
188 key_data.TimeKeyWentDown[i] = CurTime;
189 key_data.TimeKeyHeldDown[i] = 0;
190 key_data.NumDowns[i]=0;
191 key_data.NumUps[i]=0;
199 if ( n >= KEY_BUFFER_SIZE ) n=0;
203 // Returns 1 if character waiting... 0 otherwise
206 int is_one_waiting = 0;
210 key_clear_bios_buffer();
212 if (key_data.keytail!=key_data.keyhead)
215 return is_one_waiting;
224 key_clear_bios_buffer();
226 if (key_data.keytail!=key_data.keyhead) {
227 key = key_data.keybuffer[key_data.keyhead];
228 key_data.keyhead = add_one(key_data.keyhead);
234 int key_inkey_time(fix * time)
240 key_clear_bios_buffer();
242 if (key_data.keytail!=key_data.keyhead) {
243 key = key_data.keybuffer[key_data.keyhead];
244 *time = key_data.time_pressed[key_data.keyhead];
245 key_data.keyhead = add_one(key_data.keyhead);
259 key_clear_bios_buffer();
261 if (key_data.keytail!=key_data.keyhead) {
262 key = key_data.keybuffer[key_data.keyhead];
268 // If not installed, uses BIOS and returns getch();
269 // Else returns pending key (or waits for one if none waiting).
277 while (!key_checkch())
282 unsigned int key_get_shift_status()
284 unsigned int shift_status = 0;
288 key_clear_bios_buffer();
290 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
291 shift_status |= KEY_SHIFTED;
293 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
294 shift_status |= KEY_ALTED;
296 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
297 shift_status |= KEY_CTRLED;
300 if (keyd_pressed[KEY_DELETE])
301 shift_status |=KEY_DEBUGGED;
309 // Returns the number of seconds this key has been down since last call.
310 fix key_down_time(int scancode) {
313 if ((scancode<0)|| (scancode>255)) return 0;
316 if (keyd_editor_mode && key_get_shift_status() )
322 if ( !keyd_pressed[scancode] ) {
323 time_down = key_data.TimeKeyHeldDown[scancode];
324 key_data.TimeKeyHeldDown[scancode] = 0;
326 time = timer_get_fixed_secondsX();
327 time_down = time - key_data.TimeKeyWentDown[scancode];
328 key_data.TimeKeyWentDown[scancode] = time;
335 // Returns number of times key has went from up to down since last call.
336 unsigned int key_down_count(int scancode) {
339 if ((scancode<0)|| (scancode>255)) return 0;
342 n = key_data.NumDowns[scancode];
343 key_data.NumDowns[scancode] = 0;
350 // Returns number of times key has went from down to up since last call.
351 unsigned int key_up_count(int scancode) {
354 if ((scancode<0)|| (scancode>255)) return 0;
357 n = key_data.NumUps[scancode];
358 key_data.NumUps[scancode] = 0;
364 // Use intrinsic forms so that we stay in the locked interrup code.
368 #pragma aux Int5 = "int 5";
371 #define Int5() asm volatile("int $5")
375 void __interrupt __far key_handler()
377 unsigned char scancode, breakbit, temp;
378 unsigned short keycode;
382 #ifdef __WATCOMC__ /* must have _chain_intr */
383 ubyte * MONO = (ubyte *)(0x0b0000+24*80*2);
384 if ( ((MONO[0]=='D') && (MONO[2]=='B') && (MONO[4]=='G') && (MONO[6]=='>')) ||
385 ((MONO[14]=='<') && (MONO[16]=='i') && (MONO[18]=='>') && (MONO[20]==' ') && (MONO[22]=='-')) ||
386 ((MONO[0]==200 ) && (MONO[2]==27) && (MONO[4]==17) )
388 _chain_intr( key_data.prev_int_9 );
394 scancode = inp( 0x60 );
398 key_data.E0Flag = 0x80;
401 // Parse scancode and break bit
402 if (key_data.E1Flag > 0 ) { // Special code for Pause, which is E1 1D 45 E1 9D C5
404 if ( scancode == 0x1D ) {
405 scancode = KEY_PAUSE;
407 } else if ( scancode == 0x9d ) {
408 scancode = KEY_PAUSE;
411 break; // skip this keycode
413 } else if ( scancode==0xE1 ) {
417 breakbit = scancode & 0x80; // Get make/break bit
418 scancode &= 0x7f; // Strip make/break bit off of scancode
419 scancode |= key_data.E0Flag; // Add in extended key code
421 key_data.E0Flag = 0; // Clear extended key code
425 keyd_last_released = scancode;
426 keyd_pressed[scancode] = 0;
427 key_data.NumUps[scancode]++;
429 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
430 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
431 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
433 temp |= keyd_pressed[KEY_DELETE];
434 if ( !(keyd_editor_mode && temp) )
435 #endif // NOTICE LINK TO ABOVE IF!!!!
436 key_data.TimeKeyHeldDown[scancode] += timer_get_fixed_secondsX() - key_data.TimeKeyWentDown[scancode];
439 keyd_last_pressed = scancode;
440 keyd_time_when_last_pressed = timer_get_fixed_secondsX();
441 if (!keyd_pressed[scancode]) {
443 key_data.TimeKeyWentDown[scancode] = timer_get_fixed_secondsX();
444 keyd_pressed[scancode] = 1;
445 key_data.NumDowns[scancode]++;
447 if ( (keyd_pressed[KEY_LSHIFT]) && (scancode == KEY_BACKSP) ) {
448 keyd_pressed[KEY_LSHIFT] = 0;
452 } else if (!keyd_repeat) {
453 // Don't buffer repeating key if repeat mode is off
457 if ( scancode!=0xAA ) {
460 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
461 keycode |= KEY_SHIFTED;
463 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
464 keycode |= KEY_ALTED;
466 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
467 keycode |= KEY_CTRLED;
470 if ( keyd_pressed[KEY_DELETE] )
471 keycode |= KEY_DEBUGGED;
474 temp = key_data.keytail+1;
475 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
477 if (temp!=key_data.keyhead) {
478 key_data.keybuffer[key_data.keytail] = keycode;
479 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
480 key_data.keytail = temp;
487 #ifdef PASS_KEYS_TO_BIOS
488 _chain_intr( key_data.prev_int_9 );
492 temp = inp(0x61); // Get current port 61h state
493 temp |= 0x80; // Turn on bit 7 to signal clear keybrd
494 outp( 0x61, temp ); // Send to port
495 temp &= 0x7f; // Turn off bit 7 to signal break
496 outp( 0x61, temp ); // Send to port
497 outp( 0x20, 0x20 ); // Reset interrupt controller
501 void key_handler_end() { // Dummy function to help calculate size of keyboard handler function
508 keyd_time_when_last_pressed = timer_get_fixed_seconds();
509 keyd_buffer_type = 1;
511 key_data.in_key_handler = 0;
515 // Clear the keyboard array
518 if (Installed) return;
521 //--------------- lock everything for the virtal memory ----------------------------------
522 if (!dpmi_lock_region ((void near *)key_handler, (char *)key_handler_end - (char near *)key_handler)) {
523 printf( "Error locking keyboard handler!\n" );
526 if (!dpmi_lock_region ((void *)&key_data, sizeof(keyboard))) {
527 printf( "Error locking keyboard handler's data1!\n" );
530 if (!dpmi_lock_region (&keyd_buffer_type, sizeof(char))) {
531 printf( "Error locking keyboard handler's data2!\n" );
534 if (!dpmi_lock_region (&keyd_repeat, sizeof(char))) {
535 printf( "Error locking keyboard handler's data3!\n" );
538 if (!dpmi_lock_region (&keyd_editor_mode, sizeof(char))) {
539 printf( "Error locking keyboard handler's data4!\n" );
542 if (!dpmi_lock_region ((void *)&keyd_last_pressed, sizeof(char))) {
543 printf( "Error locking keyboard handler's data5!\n" );
546 if (!dpmi_lock_region ((void *)&keyd_last_released, sizeof(char))) {
547 printf( "Error locking keyboard handler's data6!\n" );
550 if (!dpmi_lock_region ((void *)&keyd_pressed, sizeof(char)*256)) {
551 printf( "Error locking keyboard handler's data7!\n" );
554 if (!dpmi_lock_region ((void *)&keyd_time_when_last_pressed, sizeof(int))) {
555 printf( "Error locking keyboard handler's data8!\n" );
560 key_data.prev_int_9 = (void *)_dos_getvect( 9 );
561 _dos_setvect( 9, key_handler );
563 _go32_dpmi_get_protected_mode_interrupt_vector(9,
564 (_go32_dpmi_seginfo *)&key_data.prev_int_9);
565 kbd_hand_info.pm_offset = (int)key_handler;
566 kbd_hand_info.pm_selector = _my_cs();
567 _go32_dpmi_allocate_iret_wrapper(&kbd_hand_info);
568 _go32_dpmi_set_protected_mode_interrupt_vector(9, &kbd_hand_info);
576 if (!Installed) return;
580 _dos_setvect( 9, key_data.prev_int_9 );
582 _go32_dpmi_set_protected_mode_interrupt_vector(9,
583 (_go32_dpmi_seginfo *)&key_data.prev_int_9);
587 key_clear_bios_buffer_all();