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.
14 //#define PASS_KEYS_TO_BIOS 1 //if set, bios gets keys
30 _go32_dpmi_seginfo kbd_hand_info;
37 #define KEY_BUFFER_SIZE 16
40 //-------- Variable accessed by outside functions ---------
41 unsigned char keyd_buffer_type; // 0=No buffer, 1=buffer ASCII, 2=buffer scans
42 unsigned char keyd_repeat;
43 unsigned char keyd_editor_mode;
44 volatile unsigned char keyd_last_pressed;
45 volatile unsigned char keyd_last_released;
46 volatile unsigned char keyd_pressed[256];
47 volatile int keyd_time_when_last_pressed;
49 typedef struct keyboard {
50 unsigned short keybuffer[KEY_BUFFER_SIZE];
51 fix time_pressed[KEY_BUFFER_SIZE];
52 fix TimeKeyWentDown[256];
53 fix TimeKeyHeldDown[256];
54 unsigned int NumDowns[256];
55 unsigned int NumUps[256];
56 unsigned int keyhead, keytail;
61 _go32_dpmi_seginfo prev_int_9;
63 void (__interrupt __far *prev_int_9)();
67 static volatile keyboard key_data;
69 static unsigned char Installed=0;
71 unsigned char ascii_table[128] =
72 { 255, 255, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=',255,255,
73 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', 255, 255,
74 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', 39, '`',
75 255, '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', 255,'*',
76 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
77 255, 255, 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
78 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
79 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
80 255,255,255,255,255,255,255,255 };
82 unsigned char shifted_ascii_table[128] =
83 { 255, 255, '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+',255,255,
84 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}', 255, 255,
85 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', '~',
86 255, '|', 'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?', 255,255,
87 255, ' ', 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,255,255,
88 255, 255, 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,
90 255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
91 255,255,255,255,255,255,255,255 };
93 char * key_text[256] = {
94 "","ESC","1","2","3","4","5","6","7","8","9","0","-",
95 "=","BSPC","TAB","Q","W","E","R","T","Y","U","I","O",
96 "P","[","]","
\83","LCTRL","A","S","D","F",
97 "G","H","J","K","L",";","'","`",
98 "LSHFT","\\","Z","X","C","V","B","N","M",",",
99 ".","/","RSHFT","PAD*","LALT","SPC",
100 "CPSLK","F1","F2","F3","F4","F5","F6","F7","F8","F9",
101 "F10","NMLCK","SCLK","PAD7","PAD8","PAD9","PAD-",
102 "PAD4","PAD5","PAD6","PAD+","PAD1","PAD2","PAD3","PAD0",
103 "PAD.","","","","F11","F12","","","","","","","","","",
104 "","","","","","","","","","","","","","","","","","","","",
105 "","","","","","","","","","","","","","","","","","","","",
106 "","","","","","","","","","","","","","","","","","",
107 "PAD
\83","RCTRL","","","","","","","","","","","","","",
108 "","","","","","","","","","","PAD/","","","RALT","",
109 "","","","","","","","","","","","","","HOME","
\82","PGUP",
110 "","
\81","","
\7f","","END","
\80","PGDN","INS",
111 "DEL","","","","","","","","","","","","","","","","","",
112 "","","","","","","","","","","","","","","","","","","","",
113 "","","","","","","" };
115 unsigned char key_to_ascii(int keycode )
119 shifted = keycode & KEY_SHIFTED;
126 return shifted_ascii_table[keycode];
128 return ascii_table[keycode];
131 void key_clear_bios_buffer_all()
134 // Clear keyboard buffer...
135 *(ushort *)0x41a=*(ushort *)0x41c;
136 // Clear the status bits...
140 _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
141 _farpokeb(_dos_ds,0x417, 0);
142 _farpokeb(_dos_ds,0x418, 0);
146 void key_clear_bios_buffer()
149 // Clear keyboard buffer...
150 *(ushort *)0x41a=*(ushort *)0x41c;
152 _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
163 // Clear the BIOS buffer
164 key_clear_bios_buffer();
166 key_data.keyhead = key_data.keytail = 0;
168 //Clear the keyboard buffer
169 for (i=0; i<KEY_BUFFER_SIZE; i++ ) {
170 key_data.keybuffer[i] = 0;
171 key_data.time_pressed[i] = 0;
174 //Clear the keyboard array
176 CurTime =timer_get_fixed_secondsX();
178 for (i=0; i<256; i++ ) {
180 key_data.TimeKeyWentDown[i] = CurTime;
181 key_data.TimeKeyHeldDown[i] = 0;
182 key_data.NumDowns[i]=0;
183 key_data.NumUps[i]=0;
191 if ( n >= KEY_BUFFER_SIZE ) n=0;
195 // Returns 1 if character waiting... 0 otherwise
198 int is_one_waiting = 0;
202 key_clear_bios_buffer();
204 if (key_data.keytail!=key_data.keyhead)
207 return is_one_waiting;
216 key_clear_bios_buffer();
218 if (key_data.keytail!=key_data.keyhead) {
219 key = key_data.keybuffer[key_data.keyhead];
220 key_data.keyhead = add_one(key_data.keyhead);
226 int key_inkey_time(fix * time)
232 key_clear_bios_buffer();
234 if (key_data.keytail!=key_data.keyhead) {
235 key = key_data.keybuffer[key_data.keyhead];
236 *time = key_data.time_pressed[key_data.keyhead];
237 key_data.keyhead = add_one(key_data.keyhead);
251 key_clear_bios_buffer();
253 if (key_data.keytail!=key_data.keyhead) {
254 key = key_data.keybuffer[key_data.keyhead];
260 // If not installed, uses BIOS and returns getch();
261 // Else returns pending key (or waits for one if none waiting).
269 while (!key_checkch())
274 unsigned int key_get_shift_status()
276 unsigned int shift_status = 0;
280 key_clear_bios_buffer();
282 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
283 shift_status |= KEY_SHIFTED;
285 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
286 shift_status |= KEY_ALTED;
288 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
289 shift_status |= KEY_CTRLED;
292 if (keyd_pressed[KEY_DELETE])
293 shift_status |=KEY_DEBUGGED;
301 // Returns the number of seconds this key has been down since last call.
302 fix key_down_time(int scancode) {
305 if ((scancode<0)|| (scancode>255)) return 0;
308 if (keyd_editor_mode && key_get_shift_status() )
314 if ( !keyd_pressed[scancode] ) {
315 time_down = key_data.TimeKeyHeldDown[scancode];
316 key_data.TimeKeyHeldDown[scancode] = 0;
318 time = timer_get_fixed_secondsX();
319 time_down = time - key_data.TimeKeyWentDown[scancode];
320 key_data.TimeKeyWentDown[scancode] = time;
327 // Returns number of times key has went from up to down since last call.
328 unsigned int key_down_count(int scancode) {
331 if ((scancode<0)|| (scancode>255)) return 0;
334 n = key_data.NumDowns[scancode];
335 key_data.NumDowns[scancode] = 0;
342 // Returns number of times key has went from down to up since last call.
343 unsigned int key_up_count(int scancode) {
346 if ((scancode<0)|| (scancode>255)) return 0;
349 n = key_data.NumUps[scancode];
350 key_data.NumUps[scancode] = 0;
356 // Use intrinsic forms so that we stay in the locked interrup code.
360 #pragma aux Int5 = "int 5";
363 #define Int5() asm volatile("int $5")
367 void __interrupt __far key_handler()
369 unsigned char scancode, breakbit, temp;
370 unsigned short keycode;
374 #ifdef __WATCOMC__ /* must have _chain_intr */
375 ubyte * MONO = (ubyte *)(0x0b0000+24*80*2);
376 if ( ((MONO[0]=='D') && (MONO[2]=='B') && (MONO[4]=='G') && (MONO[6]=='>')) ||
377 ((MONO[14]=='<') && (MONO[16]=='i') && (MONO[18]=='>') && (MONO[20]==' ') && (MONO[22]=='-')) ||
378 ((MONO[0]==200 ) && (MONO[2]==27) && (MONO[4]==17) )
380 _chain_intr( key_data.prev_int_9 );
386 scancode = inp( 0x60 );
390 key_data.E0Flag = 0x80;
393 // Parse scancode and break bit
394 if (key_data.E1Flag > 0 ) { // Special code for Pause, which is E1 1D 45 E1 9D C5
396 if ( scancode == 0x1D ) {
397 scancode = KEY_PAUSE;
399 } else if ( scancode == 0x9d ) {
400 scancode = KEY_PAUSE;
403 break; // skip this keycode
405 } else if ( scancode==0xE1 ) {
409 breakbit = scancode & 0x80; // Get make/break bit
410 scancode &= 0x7f; // Strip make/break bit off of scancode
411 scancode |= key_data.E0Flag; // Add in extended key code
413 key_data.E0Flag = 0; // Clear extended key code
417 keyd_last_released = scancode;
418 keyd_pressed[scancode] = 0;
419 key_data.NumUps[scancode]++;
421 temp |= keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT];
422 temp |= keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT];
423 temp |= keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL];
425 temp |= keyd_pressed[KEY_DELETE];
426 if ( !(keyd_editor_mode && temp) )
427 #endif // NOTICE LINK TO ABOVE IF!!!!
428 key_data.TimeKeyHeldDown[scancode] += timer_get_fixed_secondsX() - key_data.TimeKeyWentDown[scancode];
431 keyd_last_pressed = scancode;
432 keyd_time_when_last_pressed = timer_get_fixed_secondsX();
433 if (!keyd_pressed[scancode]) {
435 key_data.TimeKeyWentDown[scancode] = timer_get_fixed_secondsX();
436 keyd_pressed[scancode] = 1;
437 key_data.NumDowns[scancode]++;
439 if ( (keyd_pressed[KEY_LSHIFT]) && (scancode == KEY_BACKSP) ) {
440 keyd_pressed[KEY_LSHIFT] = 0;
444 } else if (!keyd_repeat) {
445 // Don't buffer repeating key if repeat mode is off
449 if ( scancode!=0xAA ) {
452 if ( keyd_pressed[KEY_LSHIFT] || keyd_pressed[KEY_RSHIFT] )
453 keycode |= KEY_SHIFTED;
455 if ( keyd_pressed[KEY_LALT] || keyd_pressed[KEY_RALT] )
456 keycode |= KEY_ALTED;
458 if ( keyd_pressed[KEY_LCTRL] || keyd_pressed[KEY_RCTRL] )
459 keycode |= KEY_CTRLED;
462 if ( keyd_pressed[KEY_DELETE] )
463 keycode |= KEY_DEBUGGED;
466 temp = key_data.keytail+1;
467 if ( temp >= KEY_BUFFER_SIZE ) temp=0;
469 if (temp!=key_data.keyhead) {
470 key_data.keybuffer[key_data.keytail] = keycode;
471 key_data.time_pressed[key_data.keytail] = keyd_time_when_last_pressed;
472 key_data.keytail = temp;
479 #ifdef PASS_KEYS_TO_BIOS
480 _chain_intr( key_data.prev_int_9 );
484 temp = inp(0x61); // Get current port 61h state
485 temp |= 0x80; // Turn on bit 7 to signal clear keybrd
486 outp( 0x61, temp ); // Send to port
487 temp &= 0x7f; // Turn off bit 7 to signal break
488 outp( 0x61, temp ); // Send to port
489 outp( 0x20, 0x20 ); // Reset interrupt controller
493 void key_handler_end() { // Dummy function to help calculate size of keyboard handler function
500 keyd_time_when_last_pressed = timer_get_fixed_seconds();
501 keyd_buffer_type = 1;
503 key_data.in_key_handler = 0;
507 // Clear the keyboard array
510 if (Installed) return;
513 //--------------- lock everything for the virtal memory ----------------------------------
514 if (!dpmi_lock_region ((void near *)key_handler, (char *)key_handler_end - (char near *)key_handler)) {
515 printf( "Error locking keyboard handler!\n" );
518 if (!dpmi_lock_region ((void *)&key_data, sizeof(keyboard))) {
519 printf( "Error locking keyboard handler's data1!\n" );
522 if (!dpmi_lock_region (&keyd_buffer_type, sizeof(char))) {
523 printf( "Error locking keyboard handler's data2!\n" );
526 if (!dpmi_lock_region (&keyd_repeat, sizeof(char))) {
527 printf( "Error locking keyboard handler's data3!\n" );
530 if (!dpmi_lock_region (&keyd_editor_mode, sizeof(char))) {
531 printf( "Error locking keyboard handler's data4!\n" );
534 if (!dpmi_lock_region ((void *)&keyd_last_pressed, sizeof(char))) {
535 printf( "Error locking keyboard handler's data5!\n" );
538 if (!dpmi_lock_region ((void *)&keyd_last_released, sizeof(char))) {
539 printf( "Error locking keyboard handler's data6!\n" );
542 if (!dpmi_lock_region ((void *)&keyd_pressed, sizeof(char)*256)) {
543 printf( "Error locking keyboard handler's data7!\n" );
546 if (!dpmi_lock_region ((void *)&keyd_time_when_last_pressed, sizeof(int))) {
547 printf( "Error locking keyboard handler's data8!\n" );
552 key_data.prev_int_9 = (void *)_dos_getvect( 9 );
553 _dos_setvect( 9, key_handler );
555 _go32_dpmi_get_protected_mode_interrupt_vector(9,
556 (_go32_dpmi_seginfo *)&key_data.prev_int_9);
557 kbd_hand_info.pm_offset = (int)key_handler;
558 kbd_hand_info.pm_selector = _my_cs();
559 _go32_dpmi_allocate_iret_wrapper(&kbd_hand_info);
560 _go32_dpmi_set_protected_mode_interrupt_vector(9, &kbd_hand_info);
568 if (!Installed) return;
572 _dos_setvect( 9, key_data.prev_int_9 );
574 _go32_dpmi_set_protected_mode_interrupt_vector(9,
575 (_go32_dpmi_seginfo *)&key_data.prev_int_9);
579 key_clear_bios_buffer_all();
584 #endif // __ENV_DJGPP__