* Code cleanup:
[oweals/u-boot.git] / board / MAI / bios_emulator / scitech / src / pm / smx / event.c
1 /****************************************************************************
2 *
3 *                   SciTech OS Portability Manager Library
4 *
5 *  ========================================================================
6 *
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
11 *
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.
16 *
17 *    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
18 *
19 *    The Initial Developer of the Original Code is SciTech Software, Inc.
20 *    All Rights Reserved.
21 *
22 *  ========================================================================
23 *
24 * Language:     ANSI C
25 * Environment:  32-bit SMX embedded systems development
26 *
27 * Description:  32-bit SMX implementation for the SciTech cross platform
28 *               event library.
29 *
30 ****************************************************************************/
31
32 #include "smx/ps2mouse.h"
33
34 /*--------------------------- Global variables ----------------------------*/
35
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      */
40
41 /*---------------------------- Implementation -----------------------------*/
42
43 /* External assembler functions */
44
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);
55
56 /****************************************************************************
57 REMARKS:
58 Do nothing for DOS, because we are fully interrupt driven.
59 ****************************************************************************/
60 #define _EVT_pumpMessages()
61
62 /****************************************************************************
63 REMARKS:
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
67 events.
68 ****************************************************************************/
69 ulong _EVT_getTicks(void)
70 {
71     return (ulong)PM_getLong(_EVT_biosPtr+0x6C) * 55UL;
72 }
73
74 /****************************************************************************
75 REMARKS:
76 Include generic raw scancode keyboard module.
77 ****************************************************************************/
78 #include "common/keyboard.c"
79
80 /****************************************************************************
81 REMARKS:
82 Determines if we have a mouse attached and functioning.
83 ****************************************************************************/
84 static ibool detectMouse(void)
85 {
86    return(ps2Query());
87 }
88
89 /****************************************************************************
90 PARAMETERS:
91 what        - Event code
92 message     - Event message
93 x,y         - Mouse position at time of event
94 but_stat    - Mouse button status at time of event
95
96 REMARKS:
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.
99
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(
105     uint what,
106     uint message,
107     int x,
108     int y,
109     int mickeyX,
110     int mickeyY,
111     uint but_stat)
112 {
113     event_t evt;
114
115     if (EVT.count < EVENTQSIZE) {
116         /* Save information in event record. */
117         evt.when = _EVT_getTicks();
118         evt.what = what;
119         evt.message = message;
120         evt.modifiers = but_stat;
121         evt.where_x = x;                /* Save mouse event position    */
122         evt.where_y = y;
123         evt.relative_x = mickeyX;
124         evt.relative_y = mickeyY;
125         evt.modifiers |= EVT.keyModifiers;
126         addEvent(&evt);                 /* Add to tail of event queue   */
127         }
128 }
129
130 /****************************************************************************
131 PARAMETERS:
132 mask        - Event mask
133 butstate    - Button state
134 x           - Mouse x coordinate
135 y           - Mouse y coordinate
136
137 REMARKS:
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
140 to the event queue.
141
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(
147     uint mask,
148     uint butstate,
149     int x,
150     int y,
151     int mickeyX,
152     int mickeyY)
153 {
154     RMREGS  regs;
155     uint    ps;
156
157     if (mask & 1) {
158         /* Save the current mouse coordinates */
159         EVT.mx = x; EVT.my = y;
160
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.
165          */
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;
172             }
173         else {
174             EVT.oldMove = EVT.freeHead;         /* Save id of this move event   */
175             addMouseEvent(EVT_MOUSEMOVE,0,x,y,mickeyX,mickeyY,butstate);
176             }
177         _EVT_restoreInt(ps);
178         }
179     if (mask & 0x2A) {
180         ps = _EVT_disableInt();
181         addMouseEvent(EVT_MOUSEDOWN,mask >> 1,x,y,0,0,butstate);
182         EVT.oldMove = -1;
183         _EVT_restoreInt(ps);
184         }
185     if (mask & 0x54) {
186         ps = _EVT_disableInt();
187         addMouseEvent(EVT_MOUSEUP,mask >> 2,x,y,0,0,butstate);
188         EVT.oldMove = -1;
189         _EVT_restoreInt(ps);
190         }
191     EVT.oldKey = -1;
192 }
193
194 /****************************************************************************
195 REMARKS:
196 Keyboard interrupt handler function.
197
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)
203 {
204    PM_chainPrevKey();
205     processRawScanCode(PM_inpb(0x60));
206     PM_outpb(0x20,0x20);
207 }
208
209 /****************************************************************************
210 REMARKS:
211 Safely abort the event module upon catching a fatal error.
212 ****************************************************************************/
213 void _EVT_abort()
214 {
215     EVT_exit();
216     PM_fatalError("Unhandled exception!");
217 }
218
219 /****************************************************************************
220 PARAMETERS:
221 mouseMove   - Callback function to call wheneve the mouse needs to be moved
222
223 REMARKS:
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.
227
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)
233 {
234     int     i;
235
236     EVT.mouseMove = mouseMove;
237     _EVT_biosPtr = PM_getBIOSPointer();
238     EVT_resume();
239 }
240
241 /****************************************************************************
242 REMARKS:
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)
248 {
249     static int      locked = 0;
250     int             stat;
251     uchar           mods;
252     PM_lockHandle   lh;
253
254     if (_EVT_useEvents) {
255         /* Initialise the event queue and enable our interrupt handlers */
256         initEventQueue();
257         PM_setKeyHandler(keyboardISR);
258         if ((haveMouse = detectMouse()) != 0)
259             PM_setMouseHandler(0xFFFF,mouseISR);
260
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
264          * CAPSLOCK.
265          */
266         EVT.keyModifiers = 0;
267         mods = PM_getByte(_EVT_biosPtr+0x17);
268         if (mods & 0x10)
269             EVT.keyModifiers |= EVT_SCROLLLOCK;
270         if (mods & 0x20)
271             EVT.keyModifiers |= EVT_NUMLOCK;
272         if (mods & 0x40)
273             EVT.keyModifiers |= EVT_CAPSLOCK;
274
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
277          * under real mode.
278          */
279         if (!locked) {
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
285              * correct).
286              */
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);
291             if (stat) {
292                 PM_fatalError("Page locking services failed - interrupt handling not safe!");
293                 exit(1);
294                 }
295             locked = 1;
296             }
297
298         _EVT_installed = true;
299         }
300 }
301
302 /****************************************************************************
303 REMARKS
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(
309     int xRes,
310     int yRes)
311 {
312     if (haveMouse) {
313         ps2MouseStop();
314         ps2MouseStart( 0, xRes, 0, yRes, -1, -1, -1);
315         }
316 }
317
318 /****************************************************************************
319 REMARKS
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(
324     int *x,
325     int *y)
326 {
327     if (haveMouse)
328         ps2MouseMove(*x, *y);
329 }
330
331 /****************************************************************************
332 REMARKS
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)
337 {
338     uchar   mods;
339
340     if (_EVT_installed) {
341         PM_restoreKeyHandler();
342     if (haveMouse)
343         PM_restoreMouseHandler();
344
345         /* Set the keyboard modifier flags in the BIOS to our values */
346         EVT_allowLEDS(true);
347         mods = PM_getByte(_EVT_biosPtr+0x17) & ~0x70;
348         if (EVT.keyModifiers & EVT_SCROLLLOCK)
349             mods |= 0x10;
350         if (EVT.keyModifiers & EVT_NUMLOCK)
351             mods |= 0x20;
352         if (EVT.keyModifiers & EVT_CAPSLOCK)
353             mods |= 0x40;
354         PM_setByte(_EVT_biosPtr+0x17,mods);
355
356         /* Flag that we are no longer installed */
357         _EVT_installed = false;
358         }
359 }
360
361 /****************************************************************************
362 REMARKS
363 Exits the event module for program terminatation.
364 ****************************************************************************/
365 void EVTAPI EVT_exit(void)
366 {
367     EVT_suspend();
368 }