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 SMX embedded systems development
27 * Description: 32-bit SMX implementation for the SciTech cross platform
30 ****************************************************************************/
32 #include "smx/ps2mouse.h"
34 /*--------------------------- Global variables ----------------------------*/
36 ibool _VARAPI _EVT_useEvents = true; /* True to use event handling */
37 ibool _VARAPI _EVT_installed = 0; /* Event handers installed? */
38 uchar _VARAPI *_EVT_biosPtr = NULL; /* Pointer to the BIOS data area */
39 static ibool haveMouse = false; /* True if we have a mouse */
41 /*---------------------------- Implementation -----------------------------*/
43 /* External assembler functions */
45 void EVTAPI _EVT_pollJoystick(void);
46 uint EVTAPI _EVT_disableInt(void);
47 uint EVTAPI _EVT_restoreInt(uint flags);
48 void EVTAPI _EVT_codeStart(void);
49 void EVTAPI _EVT_codeEnd(void);
50 void EVTAPI _EVT_cCodeStart(void);
51 void EVTAPI _EVT_cCodeEnd(void);
52 int EVTAPI _EVT_getKeyCode(void);
53 int EVTAPI EVT_rdinx(int port,int index);
54 void EVTAPI EVT_wrinx(int port,int index,int value);
56 /****************************************************************************
58 Do nothing for DOS, because we are fully interrupt driven.
59 ****************************************************************************/
60 #define _EVT_pumpMessages()
62 /****************************************************************************
64 This function is used to return the number of ticks since system
65 startup in milliseconds. This should be the same value that is placed into
66 the time stamp fields of events, and is used to implement auto mouse down
68 ****************************************************************************/
69 ulong _EVT_getTicks(void)
71 return (ulong)PM_getLong(_EVT_biosPtr+0x6C) * 55UL;
74 /****************************************************************************
76 Include generic raw scancode keyboard module.
77 ****************************************************************************/
78 #include "common/keyboard.c"
80 /****************************************************************************
82 Determines if we have a mouse attached and functioning.
83 ****************************************************************************/
84 static ibool detectMouse(void)
89 /****************************************************************************
92 message - Event message
93 x,y - Mouse position at time of event
94 but_stat - Mouse button status at time of event
97 Adds a new mouse event to the event queue. This routine is called from within
98 the mouse interrupt subroutine, so it must be efficient.
100 NOTE: Interrupts MUST be OFF while this routine is called to ensure we have
101 mutually exclusive access to our internal data structures for
102 interrupt driven systems (like under DOS).
103 ****************************************************************************/
104 static void addMouseEvent(
115 if (EVT.count < EVENTQSIZE) {
116 /* Save information in event record. */
117 evt.when = _EVT_getTicks();
119 evt.message = message;
120 evt.modifiers = but_stat;
121 evt.where_x = x; /* Save mouse event position */
123 evt.relative_x = mickeyX;
124 evt.relative_y = mickeyY;
125 evt.modifiers |= EVT.keyModifiers;
126 addEvent(&evt); /* Add to tail of event queue */
130 /****************************************************************************
133 butstate - Button state
134 x - Mouse x coordinate
135 y - Mouse y coordinate
138 Mouse event handling routine. This gets called when a mouse event occurs,
139 and we call the addMouseEvent() routine to add the appropriate mouse event
142 Note: Interrupts are ON when this routine is called by the mouse driver code.
143 /*AM: NOTE: This function has not actually been ported from DOS yet and should not */
144 /*AM: be installed until it is. */
145 ****************************************************************************/
146 static void EVTAPI mouseISR(
158 /* Save the current mouse coordinates */
159 EVT.mx = x; EVT.my = y;
161 /* If the last event was a movement event, then modify the last
162 * event rather than post a new one, so that the queue will not
163 * become saturated. Before we modify the data structures, we
164 * MUST ensure that interrupts are off.
166 ps = _EVT_disableInt();
167 if (EVT.oldMove != -1) {
168 EVT.evtq[EVT.oldMove].where_x = x; /* Modify existing one */
169 EVT.evtq[EVT.oldMove].where_y = y;
170 EVT.evtq[EVT.oldMove].relative_x += mickeyX;
171 EVT.evtq[EVT.oldMove].relative_y += mickeyY;
174 EVT.oldMove = EVT.freeHead; /* Save id of this move event */
175 addMouseEvent(EVT_MOUSEMOVE,0,x,y,mickeyX,mickeyY,butstate);
180 ps = _EVT_disableInt();
181 addMouseEvent(EVT_MOUSEDOWN,mask >> 1,x,y,0,0,butstate);
186 ps = _EVT_disableInt();
187 addMouseEvent(EVT_MOUSEUP,mask >> 2,x,y,0,0,butstate);
194 /****************************************************************************
196 Keyboard interrupt handler function.
198 NOTE: Interrupts are OFF when this routine is called by the keyboard ISR,
199 and we leave them OFF the entire time. This has been modified to work
200 in conjunction with smx keyboard handler.
201 ****************************************************************************/
202 static void EVTAPI keyboardISR(void)
205 processRawScanCode(PM_inpb(0x60));
209 /****************************************************************************
211 Safely abort the event module upon catching a fatal error.
212 ****************************************************************************/
216 PM_fatalError("Unhandled exception!");
219 /****************************************************************************
221 mouseMove - Callback function to call wheneve the mouse needs to be moved
224 Initiliase the event handling module. Here we install our mouse handling ISR
225 to be called whenever any button's are pressed or released. We also build
226 the free list of events in the event queue.
228 We use handler number 2 of the mouse libraries interrupt handlers for our
229 event handling routines.
230 ****************************************************************************/
231 void EVTAPI EVT_init(
232 _EVT_mouseMoveHandler mouseMove)
236 EVT.mouseMove = mouseMove;
237 _EVT_biosPtr = PM_getBIOSPointer();
241 /****************************************************************************
243 Initiailises the internal event handling modules. The EVT_suspend function
244 can be called to suspend event handling (such as when shelling out to DOS),
245 and this function can be used to resume it again later.
246 ****************************************************************************/
247 void EVTAPI EVT_resume(void)
249 static int locked = 0;
254 if (_EVT_useEvents) {
255 /* Initialise the event queue and enable our interrupt handlers */
257 PM_setKeyHandler(keyboardISR);
258 if ((haveMouse = detectMouse()) != 0)
259 PM_setMouseHandler(0xFFFF,mouseISR);
261 /* Read the keyboard modifier flags from the BIOS to get the
262 * correct initialisation state. The only state we care about is
263 * the correct toggle state flags such as SCROLLLOCK, NUMLOCK and
266 EVT.keyModifiers = 0;
267 mods = PM_getByte(_EVT_biosPtr+0x17);
269 EVT.keyModifiers |= EVT_SCROLLLOCK;
271 EVT.keyModifiers |= EVT_NUMLOCK;
273 EVT.keyModifiers |= EVT_CAPSLOCK;
275 /* Lock all of the code and data used by our protected mode interrupt
276 * handling routines, so that it will continue to work correctly
280 /* It is difficult to ensure that we lock our global data, so we
281 * do this by taking the address of a variable locking all data
282 * 2Kb on either side. This should properly cover the global data
283 * used by the module (the other alternative is to declare the
284 * variables in assembler, in which case we know it will be
287 stat = !PM_lockDataPages(&EVT,sizeof(EVT),&lh);
288 stat |= !PM_lockDataPages(&_EVT_biosPtr,sizeof(_EVT_biosPtr),&lh);
289 stat |= !PM_lockCodePages((__codePtr)_EVT_cCodeStart,(int)_EVT_cCodeEnd-(int)_EVT_cCodeStart,&lh);
290 stat |= !PM_lockCodePages((__codePtr)_EVT_codeStart,(int)_EVT_codeEnd-(int)_EVT_codeStart,&lh);
292 PM_fatalError("Page locking services failed - interrupt handling not safe!");
298 _EVT_installed = true;
302 /****************************************************************************
304 Changes the range of coordinates returned by the mouse functions to the
305 specified range of values. This is used when changing between graphics
306 modes set the range of mouse coordinates for the new display mode.
307 ****************************************************************************/
308 void EVTAPI EVT_setMouseRange(
314 ps2MouseStart( 0, xRes, 0, yRes, -1, -1, -1);
318 /****************************************************************************
320 Modifes the mouse coordinates as necessary if scaling to OS coordinates,
321 and sets the OS mouse cursor position.
322 ****************************************************************************/
323 void _EVT_setMousePos(
328 ps2MouseMove(*x, *y);
331 /****************************************************************************
333 Suspends all of our event handling operations. This is also used to
334 de-install the event handling code.
335 ****************************************************************************/
336 void EVTAPI EVT_suspend(void)
340 if (_EVT_installed) {
341 PM_restoreKeyHandler();
343 PM_restoreMouseHandler();
345 /* Set the keyboard modifier flags in the BIOS to our values */
347 mods = PM_getByte(_EVT_biosPtr+0x17) & ~0x70;
348 if (EVT.keyModifiers & EVT_SCROLLLOCK)
350 if (EVT.keyModifiers & EVT_NUMLOCK)
352 if (EVT.keyModifiers & EVT_CAPSLOCK)
354 PM_setByte(_EVT_biosPtr+0x17,mods);
356 /* Flag that we are no longer installed */
357 _EVT_installed = false;
361 /****************************************************************************
363 Exits the event module for program terminatation.
364 ****************************************************************************/
365 void EVTAPI EVT_exit(void)