]> icculus.org git repositories - btb/d2x.git/blob - arch/dos/key_arch.c
copied files from d1x
[btb/d2x.git] / arch / dos / key_arch.c
1 /*
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.
12 */
13 /*
14  * $Source: /cvs/cvsroot/d2x/arch/dos/key_arch.c,v $
15  * $Revision: 1.1 $
16  * $Author: bradleyb $
17  * $Date: 2002-02-15 07:18:00 $
18  * 
19  * Functions for keyboard handler.
20  * 
21  * $Log: not supported by cvs2svn $
22  * Revision 1.2  2000/02/07 10:09:52  donut
23  * dos pragma ifdefs
24  *
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.
27  *
28  * Revision 1.1.1.1  1999/06/14 21:58:32  donut
29  * Import of d1x 1.37 source.
30  *
31  * Revision 1.35  1995/01/25  20:13:30  john
32  * Took out not passing keys to debugger if w10.
33  * 
34  * Revision 1.34  1995/01/14  19:19:31  john
35  * Made so when you press Shift+Baskspace, it release keys autmatically.
36  * 
37  * Revision 1.33  1994/12/13  09:21:48  john
38  * Took out keyd_editor_mode, and KEY_DEBUGGED stuff for NDEBUG versions.
39  * 
40  * Revision 1.32  1994/11/12  13:52:01  john
41  * Fixed bug with code that cleared bios buffer.
42  * 
43  * Revision 1.31  1994/10/24  15:16:16  john
44  * Added code to detect KEY_PAUSE.
45  * 
46  * Revision 1.30  1994/10/24  13:57:53  john
47  * Hacked in support for pause key onto code 0x61.
48  * 
49  * Revision 1.29  1994/10/21  15:18:13  john
50  * *** empty log message ***
51  * 
52  * Revision 1.28  1994/10/21  15:17:24  john
53  * Made LSHIFT+BACKSPACE do what PrtScr used to.
54  * 
55  * Revision 1.27  1994/09/22  16:09:18  john
56  * Fixed some virtual memory lockdown problems with timer and
57  * joystick.
58  * 
59  * Revision 1.26  1994/09/15  21:32:47  john
60  * Added bounds checking for down_count scancode
61  * parameter.
62  * 
63  * Revision 1.25  1994/08/31  12:22:20  john
64  * Added KEY_DEBUGGED
65  * 
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.
69  * 
70  * Revision 1.23  1994/08/18  15:17:51  john
71  * *** empty log message ***
72  * 
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
76  * same.
77  * 
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 
81  * long of a time.
82  * 
83  * Revision 1.20  1994/08/08  10:43:48  john
84  * Recorded when a key was pressed for key_inkey_time.
85  * 
86  * Revision 1.19  1994/06/22  15:00:03  john
87  * Made keyboard close automatically on exit.
88  * 
89  * Revision 1.18  1994/06/21  09:16:29  john
90  * *** empty log message ***
91  * 
92  * Revision 1.17  1994/06/21  09:08:23  john
93  * *** empty log message ***
94  * 
95  * Revision 1.16  1994/06/21  09:05:01  john
96  * *** empty log message ***
97  * 
98  * Revision 1.15  1994/06/21  09:04:24  john
99  * Made PrtScreen do an int5
100  * 
101  * Revision 1.14  1994/06/17  17:17:06  john
102  * Added keyd_time_last_key_was_pressed or something like that.
103  * 
104  * Revision 1.13  1994/05/14  13:55:16  matt
105  * Added #define to control key passing to bios
106  * 
107  * Revision 1.12  1994/05/05  18:09:39  john
108  * Took out BIOS to prevent stuck keys.
109  * 
110  * Revision 1.11  1994/05/03  17:39:12  john
111  * *** empty log message ***
112  * 
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.
116  * 
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.
119  * 
120  * Revision 1.8  1994/04/22  12:52:12  john
121  * *** empty log message ***
122  * 
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.
125  * 
126  * Revision 1.6  1994/03/09  10:45:48  john
127  * Neatend code a bit.
128  * 
129  * Revision 1.5  1994/02/17  17:24:16  john
130  * Neatened up a bit.
131  * 
132  * Revision 1.4  1994/02/17  16:30:29  john
133  * Put in code to pass keys when in debugger.
134  * 
135  * Revision 1.3  1994/02/17  15:57:59  john
136  * Made handler not chain to BIOS handler.
137  * 
138  * Revision 1.2  1994/02/17  15:56:06  john
139  * Initial version.
140  * 
141  * Revision 1.1  1994/02/17  15:54:07  john
142  * Initial revision
143  * 
144  * 
145  */
146
147 //#define PASS_KEYS_TO_BIOS     1                       //if set, bios gets keys
148
149 #ifdef RCS
150 static char rcsid[] = "$Id: key_arch.c,v 1.1 2002-02-15 07:18:00 bradleyb Exp $";
151 #endif
152
153 #include <stdlib.h>
154 #include <stdio.h>
155 #include <conio.h>
156 #include <dos.h>
157 #include <i86.h>
158
159 //#define WATCOM_10
160 #ifdef __DJGPP__
161 #include <dpmi.h>
162 #define _far
163 #define __far
164 #define __interrupt
165 #define near
166 _go32_dpmi_seginfo kbd_hand_info;
167 #endif
168 #include "error.h"
169 #include "key.h"
170 #include "timer.h"
171 #include "u_dpmi.h"
172
173 #define LOCAL_KEY_BUFFER_SIZE 32
174
175
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;
182         int                                     in_key_handler;
183 #ifdef __DJGPP__
184         _go32_dpmi_seginfo prev_int_9;
185 #else
186         void (__interrupt __far *prev_int_9)();
187 #endif
188 } keyboard;
189
190 static volatile keyboard key_data;
191
192 static unsigned char Installed=0;
193
194 void arch_key_poll(void){
195         _disable();
196         while (key_data.keytail!=key_data.keyhead){
197                 generic_key_handler(key_data.keybuffer[key_data.keyhead],key_data.statebuffer[key_data.keyhead]);
198                 key_data.keyhead++;
199                 if ( key_data.keyhead >= LOCAL_KEY_BUFFER_SIZE ) key_data.keyhead=0;
200         }
201         _enable();
202 }
203
204 void key_clear_bios_buffer_all()
205 {
206 #ifdef __WATCOMC__
207         // Clear keyboard buffer...
208         *(ushort *)0x41a=*(ushort *)0x41c;
209         // Clear the status bits...
210         *(ubyte *)0x417 = 0;
211         *(ubyte *)0x418 = 0;
212 #else
213         _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
214         _farpokeb(_dos_ds,0x417, 0);
215         _farpokeb(_dos_ds,0x418, 0);
216 #endif
217 }
218
219 void key_clear_bios_buffer()
220 {
221 #ifdef __WATCOMC__
222         // Clear keyboard buffer...
223         *(ushort *)0x41a=*(ushort *)0x41c;
224 #else
225         _farpokew(_dos_ds,0x41a, _farpeekw(_dos_ds, 0x41c));
226 #endif
227 }
228
229 void arch_key_flush()
230 {
231         int i;
232
233         _disable();
234
235         // Clear the BIOS buffer
236         key_clear_bios_buffer();
237
238         key_data.keyhead = key_data.keytail = 0;
239
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;
244         }
245
246         //Clear the keyboard array
247
248         _enable();
249 }
250
251
252 // Use intrinsic forms so that we stay in the locked interrup code.
253
254 #ifdef __WATCOMC__
255 void Int5();
256 #pragma aux Int5 = "int 5";
257 #else
258 #ifdef __GNUC__
259 #define Int5() asm volatile("int $5")
260 #endif
261 #endif
262
263 #ifndef __GNUC__
264 #pragma off (check_stack)
265 #endif
266 void __interrupt __far key_handler()
267 {
268         unsigned char scancode, breakbit, temp;
269         unsigned short keycode;
270
271 #ifndef WATCOM_10
272 #ifndef NDEBUG
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) )
278                 )
279                 _chain_intr( key_data.prev_int_9 );
280 #endif
281 #endif
282 #endif
283
284         // Read in scancode
285         scancode = inp( 0x60 );
286
287         switch( scancode )      {
288         case 0xE0:
289                 key_data.E0Flag = 0x80;
290                 break;
291         default:
292                 // Parse scancode and break bit
293                 if (key_data.E1Flag > 0 )       {               // Special code for Pause, which is E1 1D 45 E1 9D C5
294                         key_data.E1Flag--;
295                         if ( scancode == 0x1D ) {
296                                 scancode        = KEY_PAUSE;
297                                 breakbit        = 0;
298                         } else if ( scancode == 0x9d ) {
299                                 scancode        = KEY_PAUSE;
300                                 breakbit        = 1;
301                         } else {
302                                 break;          // skip this keycode
303                         }
304                 } else if ( scancode==0xE1 )    {
305                         key_data.E1Flag = 2;
306                         break;
307                 } else {
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
311                 }
312                 key_data.E0Flag = 0;                                                            // Clear extended key code
313                 temp = key_data.keytail+1;
314                 if ( temp >= LOCAL_KEY_BUFFER_SIZE ) temp=0;
315
316                 if (temp!=key_data.keyhead)     {
317                         keycode=scancode;
318
319                         key_data.keybuffer[key_data.keytail] = keycode;
320                         key_data.statebuffer[key_data.keytail] = !breakbit;
321                         key_data.keytail = temp;
322                 }
323         }
324
325 #ifndef NDEBUG
326 #ifdef PASS_KEYS_TO_BIOS
327         _chain_intr( key_data.prev_int_9 );
328 #endif
329 #endif
330
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
337 }
338
339 #ifndef __GNUC__
340 #pragma on (check_stack)
341 #endif
342
343 void key_handler_end()  {               // Dummy function to help calculate size of keyboard handler function
344 }
345
346 void arch_key_init()
347 {
348         // Initialize queue
349
350         key_data.in_key_handler = 0;
351         key_data.E0Flag = 0;
352         key_data.E1Flag = 0;
353
354         if (Installed) return;
355         Installed = 1;
356
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" );
360                 exit(1);
361         }
362         if (!dpmi_lock_region ((void *)&key_data, sizeof(keyboard)))    {
363                 printf( "Error locking keyboard handler's data1!\n" );
364                 exit(1);
365         }
366
367 #ifndef __DJGPP__
368         key_data.prev_int_9 = (void *)_dos_getvect( 9 );
369     _dos_setvect( 9, key_handler );
370 #else
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);
377 #endif
378 }
379
380 void arch_key_close()
381 {
382         if (!Installed) return;
383         Installed = 0;
384         
385 #ifndef __DJGPP__
386         _dos_setvect( 9, key_data.prev_int_9 );
387 #else
388         _go32_dpmi_set_protected_mode_interrupt_vector(9,
389          (_go32_dpmi_seginfo *)&key_data.prev_int_9);
390 #endif
391
392         _disable();
393         key_clear_bios_buffer_all();
394         _enable();
395
396 }