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 * $Source: /cvs/cvsroot/d2x/arch/dos/key_arch.c,v $
17 * $Date: 2002-02-15 07:18:00 $
19 * Functions for keyboard handler.
21 * $Log: not supported by cvs2svn $
22 * Revision 1.2 2000/02/07 10:09:52 donut
25 * Revision 1.1 2000/01/17 05:58:38 donut
26 * switched from multiply reimplemented/reduntant/buggy key.c for each arch to a single main/key.c that calls the much smaller arch-specific parts. Also adds working emulated key repeat support.
28 * Revision 1.1.1.1 1999/06/14 21:58:32 donut
29 * Import of d1x 1.37 source.
31 * Revision 1.35 1995/01/25 20:13:30 john
32 * Took out not passing keys to debugger if w10.
34 * Revision 1.34 1995/01/14 19:19:31 john
35 * Made so when you press Shift+Baskspace, it release keys autmatically.
37 * Revision 1.33 1994/12/13 09:21:48 john
38 * Took out keyd_editor_mode, and KEY_DEBUGGED stuff for NDEBUG versions.
40 * Revision 1.32 1994/11/12 13:52:01 john
41 * Fixed bug with code that cleared bios buffer.
43 * Revision 1.31 1994/10/24 15:16:16 john
44 * Added code to detect KEY_PAUSE.
46 * Revision 1.30 1994/10/24 13:57:53 john
47 * Hacked in support for pause key onto code 0x61.
49 * Revision 1.29 1994/10/21 15:18:13 john
50 * *** empty log message ***
52 * Revision 1.28 1994/10/21 15:17:24 john
53 * Made LSHIFT+BACKSPACE do what PrtScr used to.
55 * Revision 1.27 1994/09/22 16:09:18 john
56 * Fixed some virtual memory lockdown problems with timer and
59 * Revision 1.26 1994/09/15 21:32:47 john
60 * Added bounds checking for down_count scancode
63 * Revision 1.25 1994/08/31 12:22:20 john
66 * Revision 1.24 1994/08/24 18:53:48 john
67 * Made Cyberman read like normal mouse; added dpmi module; moved
68 * mouse from assembly to c. Made mouse buttons return time_down.
70 * Revision 1.23 1994/08/18 15:17:51 john
71 * *** empty log message ***
73 * Revision 1.22 1994/08/18 15:16:38 john
74 * fixed some bugs with clear_key_times and then
75 * removed it because i fixed key_flush to do the
78 * Revision 1.21 1994/08/17 19:01:25 john
79 * Attempted to fix a bug with a key being held down
80 * key_flush called, then the key released having too
83 * Revision 1.20 1994/08/08 10:43:48 john
84 * Recorded when a key was pressed for key_inkey_time.
86 * Revision 1.19 1994/06/22 15:00:03 john
87 * Made keyboard close automatically on exit.
89 * Revision 1.18 1994/06/21 09:16:29 john
90 * *** empty log message ***
92 * Revision 1.17 1994/06/21 09:08:23 john
93 * *** empty log message ***
95 * Revision 1.16 1994/06/21 09:05:01 john
96 * *** empty log message ***
98 * Revision 1.15 1994/06/21 09:04:24 john
99 * Made PrtScreen do an int5
101 * Revision 1.14 1994/06/17 17:17:06 john
102 * Added keyd_time_last_key_was_pressed or something like that.
104 * Revision 1.13 1994/05/14 13:55:16 matt
105 * Added #define to control key passing to bios
107 * Revision 1.12 1994/05/05 18:09:39 john
108 * Took out BIOS to prevent stuck keys.
110 * Revision 1.11 1994/05/03 17:39:12 john
111 * *** empty log message ***
113 * Revision 1.10 1994/04/29 12:14:20 john
114 * Locked all memory used during interrupts so that program
115 * won't hang when using virtual memory.
117 * Revision 1.9 1994/04/28 23:49:41 john
118 * Made key_flush flush more keys and also did something else but i forget what.
120 * Revision 1.8 1994/04/22 12:52:12 john
121 * *** empty log message ***
123 * Revision 1.7 1994/04/01 10:44:59 mike
124 * Change key_getch() to call getch() if our interrupt hasn't been installed.
126 * Revision 1.6 1994/03/09 10:45:48 john
127 * Neatend code a bit.
129 * Revision 1.5 1994/02/17 17:24:16 john
132 * Revision 1.4 1994/02/17 16:30:29 john
133 * Put in code to pass keys when in debugger.
135 * Revision 1.3 1994/02/17 15:57:59 john
136 * Made handler not chain to BIOS handler.
138 * Revision 1.2 1994/02/17 15:56:06 john
141 * Revision 1.1 1994/02/17 15:54:07 john
147 //#define PASS_KEYS_TO_BIOS 1 //if set, bios gets keys
150 static char rcsid[] = "$Id: key_arch.c,v 1.1 2002-02-15 07:18:00 bradleyb Exp $";
166 _go32_dpmi_seginfo kbd_hand_info;
173 #define LOCAL_KEY_BUFFER_SIZE 32
176 typedef struct keyboard {
177 unsigned short keybuffer[LOCAL_KEY_BUFFER_SIZE];
178 unsigned short statebuffer[LOCAL_KEY_BUFFER_SIZE];
179 unsigned int keyhead, keytail;
180 unsigned char E0Flag;
181 unsigned char E1Flag;
184 _go32_dpmi_seginfo prev_int_9;
186 void (__interrupt __far *prev_int_9)();
190 static volatile keyboard key_data;
192 static unsigned char Installed=0;
194 void arch_key_poll(void){
196 while (key_data.keytail!=key_data.keyhead){
197 generic_key_handler(key_data.keybuffer[key_data.keyhead],key_data.statebuffer[key_data.keyhead]);
199 if ( key_data.keyhead >= LOCAL_KEY_BUFFER_SIZE ) key_data.keyhead=0;
204 void key_clear_bios_buffer_all()
207 // Clear keyboard buffer...
208 *(ushort *)0x41a=*(ushort *)0x41c;
209 // Clear the status bits...
213 _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
214 _farpokeb(_dos_ds,0x417, 0);
215 _farpokeb(_dos_ds,0x418, 0);
219 void key_clear_bios_buffer()
222 // Clear keyboard buffer...
223 *(ushort *)0x41a=*(ushort *)0x41c;
225 _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
229 void arch_key_flush()
235 // Clear the BIOS buffer
236 key_clear_bios_buffer();
238 key_data.keyhead = key_data.keytail = 0;
240 //Clear the keyboard buffer
241 for (i=0; i<LOCAL_KEY_BUFFER_SIZE; i++ ) {
242 key_data.keybuffer[i] = 0;
243 key_data.statebuffer[i] = 0;
246 //Clear the keyboard array
252 // Use intrinsic forms so that we stay in the locked interrup code.
256 #pragma aux Int5 = "int 5";
259 #define Int5() asm volatile("int $5")
264 #pragma off (check_stack)
266 void __interrupt __far key_handler()
268 unsigned char scancode, breakbit, temp;
269 unsigned short keycode;
273 #ifdef __WATCOMC__ /* must have _chain_intr */
274 ubyte * MONO = (ubyte *)(0x0b0000+24*80*2);
275 if ( ((MONO[0]=='D') && (MONO[2]=='B') && (MONO[4]=='G') && (MONO[6]=='>')) ||
276 ((MONO[14]=='<') && (MONO[16]=='i') && (MONO[18]=='>') && (MONO[20]==' ') && (MONO[22]=='-')) ||
277 ((MONO[0]==200 ) && (MONO[2]==27) && (MONO[4]==17) )
279 _chain_intr( key_data.prev_int_9 );
285 scancode = inp( 0x60 );
289 key_data.E0Flag = 0x80;
292 // Parse scancode and break bit
293 if (key_data.E1Flag > 0 ) { // Special code for Pause, which is E1 1D 45 E1 9D C5
295 if ( scancode == 0x1D ) {
296 scancode = KEY_PAUSE;
298 } else if ( scancode == 0x9d ) {
299 scancode = KEY_PAUSE;
302 break; // skip this keycode
304 } else if ( scancode==0xE1 ) {
308 breakbit = scancode & 0x80; // Get make/break bit
309 scancode &= 0x7f; // Strip make/break bit off of scancode
310 scancode |= key_data.E0Flag; // Add in extended key code
312 key_data.E0Flag = 0; // Clear extended key code
313 temp = key_data.keytail+1;
314 if ( temp >= LOCAL_KEY_BUFFER_SIZE ) temp=0;
316 if (temp!=key_data.keyhead) {
319 key_data.keybuffer[key_data.keytail] = keycode;
320 key_data.statebuffer[key_data.keytail] = !breakbit;
321 key_data.keytail = temp;
326 #ifdef PASS_KEYS_TO_BIOS
327 _chain_intr( key_data.prev_int_9 );
331 temp = inp(0x61); // Get current port 61h state
332 temp |= 0x80; // Turn on bit 7 to signal clear keybrd
333 outp( 0x61, temp ); // Send to port
334 temp &= 0x7f; // Turn off bit 7 to signal break
335 outp( 0x61, temp ); // Send to port
336 outp( 0x20, 0x20 ); // Reset interrupt controller
340 #pragma on (check_stack)
343 void key_handler_end() { // Dummy function to help calculate size of keyboard handler function
350 key_data.in_key_handler = 0;
354 if (Installed) return;
357 //--------------- lock everything for the virtal memory ----------------------------------
358 if (!dpmi_lock_region ((void near *)key_handler, (char *)key_handler_end - (char near *)key_handler)) {
359 printf( "Error locking keyboard handler!\n" );
362 if (!dpmi_lock_region ((void *)&key_data, sizeof(keyboard))) {
363 printf( "Error locking keyboard handler's data1!\n" );
368 key_data.prev_int_9 = (void *)_dos_getvect( 9 );
369 _dos_setvect( 9, key_handler );
371 _go32_dpmi_get_protected_mode_interrupt_vector(9,
372 (_go32_dpmi_seginfo *)&key_data.prev_int_9);
373 kbd_hand_info.pm_offset = (int)key_handler;
374 kbd_hand_info.pm_selector = _my_cs();
375 _go32_dpmi_allocate_iret_wrapper(&kbd_hand_info);
376 _go32_dpmi_set_protected_mode_interrupt_vector(9, &kbd_hand_info);
380 void arch_key_close()
382 if (!Installed) return;
386 _dos_setvect( 9, key_data.prev_int_9 );
388 _go32_dpmi_set_protected_mode_interrupt_vector(9,
389 (_go32_dpmi_seginfo *)&key_data.prev_int_9);
393 key_clear_bios_buffer_all();