1 /****************************************************************************
3 * SciTech OS Portability Manager Library
5 * ========================================================================
7 * The contents of this file are subject to the SciTech MGL Public
8 * License Version 1.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of
10 * the License at http://www.scitechsoft.com/mgl-license.txt
12 * Software distributed under the License is distributed on an
13 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * rights and limitations under the License.
17 * The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
19 * The Initial Developer of the Original Code is SciTech Software, Inc.
20 * All Rights Reserved.
22 * ========================================================================
25 * Environment: 32-bit DOS
27 * Description: 32-bit DOS implementation for the SciTech cross platform
30 ****************************************************************************/
32 /*--------------------------- Global variables ----------------------------*/
34 ibool _VARAPI _EVT_useEvents = true; /* True to use event handling */
35 ibool _VARAPI _EVT_installed = 0; /* Event handers installed? */
36 uchar _VARAPI *_EVT_biosPtr = NULL; /* Pointer to the BIOS data area */
37 static ibool haveMouse = false; /* True if we have a mouse */
39 /*---------------------------- Implementation -----------------------------*/
41 /* External assembler functions */
43 void EVTAPI _EVT_pollJoystick(void);
44 uint EVTAPI _EVT_disableInt(void);
45 uint EVTAPI _EVT_restoreInt(uint flags);
46 void EVTAPI _EVT_codeStart(void);
47 void EVTAPI _EVT_codeEnd(void);
48 void EVTAPI _EVT_cCodeStart(void);
49 void EVTAPI _EVT_cCodeEnd(void);
50 int EVTAPI _EVT_getKeyCode(void);
51 void EVTAPI _EVT_pumpMessages(void);
52 int EVTAPI EVT_rdinx(int port,int index);
53 void EVTAPI EVT_wrinx(int port,int index,int value);
55 #ifdef NO_KEYBOARD_INTERRUPT
56 /****************************************************************************
58 This function is used to pump all keyboard messages from the BIOS keyboard
59 handler into our event queue. This can be used to avoid using the
60 installable keyboard handler if this is causing problems.
61 ****************************************************************************/
62 static void EVTAPI _EVT_pumpMessages(void)
67 /* Since the keyboard ISR has not been installed if NO_IDE_BUG has
68 * been defined, we first check for any pending keyboard events
69 * here, and if there are some insert them into the event queue to
70 * be picked up later - what a kludge.
72 while ((key = _EVT_getKeyCode()) != 0) {
73 ps = _EVT_disableInt();
74 addKeyEvent(EVT_KEYDOWN, key);
78 regs.x.ax = 0x0B; /* Reset Move Mouse */
79 PM_int86(0x33,®s,®s);
83 /****************************************************************************
85 This function is used to return the number of ticks since system
86 startup in milliseconds. This should be the same value that is placed into
87 the time stamp fields of events, and is used to implement auto mouse down
89 ****************************************************************************/
90 ulong _EVT_getTicks(void)
92 return (ulong)PM_getLong(_EVT_biosPtr+0x6C) * 55UL;
95 /****************************************************************************
97 Reboots the machine from DOS (warm boot)
98 ****************************************************************************/
99 static void Reboot(void)
104 ushort *rebootType = PM_mapRealPointer(0x40,0x72);
105 *rebootType = 0x1234;
106 PM_callRealMode(0xFFFF,0x0000,®s,&sregs);
109 /****************************************************************************
111 Include generic raw scancode keyboard module.
112 ****************************************************************************/
113 #define SUPPORT_CTRL_ALT_DEL
114 #include "common/keyboard.c"
116 /****************************************************************************
118 This function fools the DOS mouse driver into thinking that it is running
119 in graphics mode, rather than text mode so we always get virtual coordinates
120 correctly rather than character coordinates.
121 ****************************************************************************/
122 int _EVT_foolMouse(void)
124 int oldmode = PM_getByte(_EVT_biosPtr+0x49);
125 PM_setByte(_EVT_biosPtr+0x49,0x10);
126 oldmode |= (EVT_rdinx(0x3C4,0x2) << 8);
130 /****************************************************************************
132 This function unfools the DOS mouse driver after we have finished calling it.
133 ****************************************************************************/
134 void _EVT_unfoolMouse(
137 PM_setByte(_EVT_biosPtr+0x49,oldmode);
139 /* Some mouse drivers reset the plane mask register for VGA plane 4
140 * modes, which screws up the display on some VGA compatible controllers
141 * in SuperVGA modes. We reset the value back again in here to solve
144 EVT_wrinx(0x3C4,0x2,oldmode >> 8);
147 /****************************************************************************
149 Determines if we have a mouse attached and functioning.
150 ****************************************************************************/
151 static ibool detectMouse(void)
158 regs.x.ax = 0x3533; /* Get interrupt vector 0x33 */
159 PM_int86x(0x21,®s,®s,&sregs);
161 /* Check that interrupt vector 0x33 is not a zero, and that the first
162 * instruction in the interrupt vector is not an IRET instruction
164 p = PM_mapRealPointer(sregs.es, regs.x.bx);
165 retval = ((sregs.es != 0) || (regs.x.bx != 0)) && (PM_getByte(p) != 207);
169 /****************************************************************************
172 message - Event message
173 x,y - Mouse position at time of event
174 but_stat - Mouse button status at time of event
177 Adds a new mouse event to the event queue. This routine is called from within
178 the mouse interrupt subroutine, so it must be efficient.
180 NOTE: Interrupts MUST be OFF while this routine is called to ensure we have
181 mutually exclusive access to our internal data structures for
182 interrupt driven systems (like under DOS).
183 ****************************************************************************/
184 static void addMouseEvent(
195 if (EVT.count < EVENTQSIZE) {
196 /* Save information in event record. */
197 evt.when = _EVT_getTicks();
199 evt.message = message;
200 evt.modifiers = but_stat;
201 evt.where_x = x; /* Save mouse event position */
203 evt.relative_x = mickeyX;
204 evt.relative_y = mickeyY;
205 evt.modifiers |= EVT.keyModifiers;
206 addEvent(&evt); /* Add to tail of event queue */
210 /****************************************************************************
213 butstate - Button state
214 x - Mouse x coordinate
215 y - Mouse y coordinate
218 Mouse event handling routine. This gets called when a mouse event occurs,
219 and we call the addMouseEvent() routine to add the appropriate mouse event
222 Note: Interrupts are ON when this routine is called by the mouse driver code.
223 ****************************************************************************/
224 static void EVTAPI mouseISR(
236 /* Save the current mouse coordinates */
237 EVT.mx = x; EVT.my = y;
239 /* If the last event was a movement event, then modify the last
240 * event rather than post a new one, so that the queue will not
241 * become saturated. Before we modify the data structures, we
242 * MUST ensure that interrupts are off.
244 ps = _EVT_disableInt();
245 if (EVT.oldMove != -1) {
246 EVT.evtq[EVT.oldMove].where_x = x; /* Modify existing one */
247 EVT.evtq[EVT.oldMove].where_y = y;
248 EVT.evtq[EVT.oldMove].relative_x += mickeyX;
249 EVT.evtq[EVT.oldMove].relative_y += mickeyY;
252 EVT.oldMove = EVT.freeHead; /* Save id of this move event */
253 addMouseEvent(EVT_MOUSEMOVE,0,x,y,mickeyX,mickeyY,butstate);
258 ps = _EVT_disableInt();
260 if (mask & 2) buttonMask |= EVT_LEFTBMASK;
261 if (mask & 8) buttonMask |= EVT_RIGHTBMASK;
262 if (mask & 32) buttonMask |= EVT_MIDDLEBMASK;
263 addMouseEvent(EVT_MOUSEDOWN,buttonMask,x,y,0,0,butstate);
268 ps = _EVT_disableInt();
270 if (mask & 2) buttonMask |= EVT_LEFTBMASK;
271 if (mask & 8) buttonMask |= EVT_RIGHTBMASK;
272 if (mask & 32) buttonMask |= EVT_MIDDLEBMASK;
273 addMouseEvent(EVT_MOUSEUP,buttonMask,x,y,0,0,butstate);
280 /****************************************************************************
282 Keyboard interrupt handler function.
284 NOTE: Interrupts are OFF when this routine is called by the keyboard ISR,
285 and we leave them OFF the entire time.
286 ****************************************************************************/
287 static void EVTAPI keyboardISR(void)
289 processRawScanCode(PM_inpb(0x60));
293 /****************************************************************************
295 Safely abort the event module upon catching a fatal error.
296 ****************************************************************************/
300 PM_fatalError("Unhandled exception!");
303 /****************************************************************************
305 mouseMove - Callback function to call wheneve the mouse needs to be moved
308 Initiliase the event handling module. Here we install our mouse handling ISR
309 to be called whenever any button's are pressed or released. We also build
310 the free list of events in the event queue.
312 We use handler number 2 of the mouse libraries interrupt handlers for our
313 event handling routines.
314 ****************************************************************************/
315 void EVTAPI EVT_init(
316 _EVT_mouseMoveHandler mouseMove)
321 EVT.mouseMove = mouseMove;
322 _EVT_biosPtr = PM_getBIOSPointer();
325 /* Grab all characters pending in the keyboard buffer and stuff
326 * them into our event buffer. This allows us to pick up any keypresses
327 * while the program is initialising.
329 while ((i = _EVT_getKeyCode()) != 0)
330 addKeyEvent(EVT_KEYDOWN,i);
333 /****************************************************************************
335 Initiailises the internal event handling modules. The EVT_suspend function
336 can be called to suspend event handling (such as when shelling out to DOS),
337 and this function can be used to resume it again later.
338 ****************************************************************************/
339 void EVTAPI EVT_resume(void)
341 static int locked = 0;
344 PM_lockHandle lh; /* Unused in DOS */
346 if (_EVT_useEvents) {
347 /* Initialise the event queue and enable our interrupt handlers */
349 #ifndef NO_KEYBOARD_INTERRUPT
350 PM_setKeyHandler(keyboardISR);
352 #ifndef NO_MOUSE_INTERRUPT
353 if ((haveMouse = detectMouse()) != 0) {
354 int oldmode = _EVT_foolMouse();
355 PM_setMouseHandler(0xFFFF,mouseISR);
356 _EVT_unfoolMouse(oldmode);
360 /* Read the keyboard modifier flags from the BIOS to get the
361 * correct initialisation state. The only state we care about is
362 * the correct toggle state flags such as SCROLLLOCK, NUMLOCK and
365 EVT.keyModifiers = 0;
366 mods = PM_getByte(_EVT_biosPtr+0x17);
368 EVT.keyModifiers |= EVT_SCROLLLOCK;
370 EVT.keyModifiers |= EVT_NUMLOCK;
372 EVT.keyModifiers |= EVT_CAPSLOCK;
374 /* Lock all of the code and data used by our protected mode interrupt
375 * handling routines, so that it will continue to work correctly
379 /* It is difficult to ensure that we lock our global data, so we
380 * do this by taking the address of a variable locking all data
381 * 2Kb on either side. This should properly cover the global data
382 * used by the module (the other alternative is to declare the
383 * variables in assembler, in which case we know it will be
386 stat = !PM_lockDataPages(&EVT,sizeof(EVT),&lh);
387 stat |= !PM_lockDataPages(&_EVT_biosPtr,sizeof(_EVT_biosPtr),&lh);
388 stat |= !PM_lockCodePages((__codePtr)_EVT_cCodeStart,(int)_EVT_cCodeEnd-(int)_EVT_cCodeStart,&lh);
389 stat |= !PM_lockCodePages((__codePtr)_EVT_codeStart,(int)_EVT_codeEnd-(int)_EVT_codeStart,&lh);
391 PM_fatalError("Page locking services failed - interrupt handling not safe!");
397 /* Catch program termination signals so we can clean up properly */
398 signal(SIGABRT, _EVT_abort);
399 signal(SIGFPE, _EVT_abort);
400 signal(SIGINT, _EVT_abort);
401 _EVT_installed = true;
405 /****************************************************************************
407 Changes the range of coordinates returned by the mouse functions to the
408 specified range of values. This is used when changing between graphics
409 modes set the range of mouse coordinates for the new display mode.
410 ****************************************************************************/
411 void EVTAPI EVT_setMouseRange(
418 int oldmode = _EVT_foolMouse();
419 PM_resetMouseDriver(1);
420 regs.x.ax = 7; /* Mouse function 7 - Set horizontal min and max */
423 PM_int86(0x33,®s,®s);
424 regs.x.ax = 8; /* Mouse function 8 - Set vertical min and max */
427 PM_int86(0x33,®s,®s);
428 _EVT_unfoolMouse(oldmode);
432 /****************************************************************************
434 Modifes the mouse coordinates as necessary if scaling to OS coordinates,
435 and sets the OS mouse cursor position.
436 ****************************************************************************/
437 void _EVT_setMousePos(
444 int oldmode = _EVT_foolMouse();
445 regs.x.ax = 4; /* Mouse function 4 - Set mouse position */
446 regs.x.cx = *x; /* New horizontal coordinate */
447 regs.x.dx = *y; /* New vertical coordinate */
448 PM_int86(0x33,®s,®s);
449 _EVT_unfoolMouse(oldmode);
453 /****************************************************************************
455 Suspends all of our event handling operations. This is also used to
456 de-install the event handling code.
457 ****************************************************************************/
458 void EVTAPI EVT_suspend(void)
462 if (_EVT_installed) {
463 /* Restore the interrupt handlers */
464 PM_restoreKeyHandler();
466 PM_restoreMouseHandler();
467 signal(SIGABRT, SIG_DFL);
468 signal(SIGFPE, SIG_DFL);
469 signal(SIGINT, SIG_DFL);
471 /* Set the keyboard modifier flags in the BIOS to our values */
473 mods = PM_getByte(_EVT_biosPtr+0x17) & ~0x70;
474 if (EVT.keyModifiers & EVT_SCROLLLOCK)
476 if (EVT.keyModifiers & EVT_NUMLOCK)
478 if (EVT.keyModifiers & EVT_CAPSLOCK)
480 PM_setByte(_EVT_biosPtr+0x17,mods);
482 /* Flag that we are no longer installed */
483 _EVT_installed = false;
487 /****************************************************************************
489 Exits the event module for program terminatation.
490 ****************************************************************************/
491 void EVTAPI EVT_exit(void)