]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/MAI/bios_emulator/scitech/src/pm/dos/event.c
* Patch by Thomas Frieden, 13 Nov 2002:
[karo-tx-uboot.git] / board / MAI / bios_emulator / scitech / src / pm / dos / 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 DOS
26 *
27 * Description:  32-bit DOS implementation for the SciTech cross platform
28 *               event library.
29 *
30 ****************************************************************************/
31
32 /*--------------------------- Global variables ----------------------------*/
33
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      */
38
39 /*---------------------------- Implementation -----------------------------*/
40
41 /* External assembler functions */
42
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);
54
55 #ifdef NO_KEYBOARD_INTERRUPT
56 /****************************************************************************
57 REMARKS:
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)
63 {
64     RMREGS  regs;
65         uint    key,ps;
66
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.
71          */
72         while ((key = _EVT_getKeyCode()) != 0) {
73                 ps = _EVT_disableInt();
74                 addKeyEvent(EVT_KEYDOWN, key);
75                 _EVT_restoreInt(ps);
76                 }
77
78     regs.x.ax = 0x0B;           // Reset Move Mouse
79     PM_int86(0x33,&regs,&regs);
80 }
81 #endif
82
83 /****************************************************************************
84 REMARKS:
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
88 events.
89 ****************************************************************************/
90 ulong _EVT_getTicks(void)
91 {
92     return (ulong)PM_getLong(_EVT_biosPtr+0x6C) * 55UL;
93 }
94
95 /****************************************************************************
96 REMARKS:
97 Reboots the machine from DOS (warm boot)
98 ****************************************************************************/
99 static void Reboot(void)
100 {
101     PMREGS  regs;
102     PMSREGS sregs;
103
104     ushort *rebootType = PM_mapRealPointer(0x40,0x72);
105     *rebootType = 0x1234;
106     PM_callRealMode(0xFFFF,0x0000,&regs,&sregs);
107 }
108
109 /****************************************************************************
110 REMARKS:
111 Include generic raw scancode keyboard module.
112 ****************************************************************************/
113 #define SUPPORT_CTRL_ALT_DEL
114 #include "common/keyboard.c"
115
116 /****************************************************************************
117 REMARKS:
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)
123 {
124     int oldmode = PM_getByte(_EVT_biosPtr+0x49);
125     PM_setByte(_EVT_biosPtr+0x49,0x10);
126     oldmode |= (EVT_rdinx(0x3C4,0x2) << 8);
127     return oldmode;
128 }
129
130 /****************************************************************************
131 REMARKS:
132 This function unfools the DOS mouse driver after we have finished calling it.
133 ****************************************************************************/
134 void _EVT_unfoolMouse(
135     int oldmode)
136 {
137     PM_setByte(_EVT_biosPtr+0x49,oldmode);
138
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
142      * the problem.
143      */
144     EVT_wrinx(0x3C4,0x2,oldmode >> 8);
145 }
146
147 /****************************************************************************
148 REMARKS:
149 Determines if we have a mouse attached and functioning.
150 ****************************************************************************/
151 static ibool detectMouse(void)
152 {
153     RMREGS  regs;
154     RMSREGS sregs;
155     uchar   *p;
156     ibool   retval;
157
158     regs.x.ax = 0x3533;                 /* Get interrupt vector 0x33    */
159     PM_int86x(0x21,&regs,&regs,&sregs);
160
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
163      */
164     p = PM_mapRealPointer(sregs.es, regs.x.bx);
165     retval = ((sregs.es != 0) || (regs.x.bx != 0)) && (PM_getByte(p) != 207);
166     return retval;
167 }
168
169 /****************************************************************************
170 PARAMETERS:
171 what        - Event code
172 message     - Event message
173 x,y         - Mouse position at time of event
174 but_stat    - Mouse button status at time of event
175
176 REMARKS:
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.
179
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(
185     uint what,
186     uint message,
187     int x,
188     int y,
189     int mickeyX,
190     int mickeyY,
191     uint but_stat)
192 {
193     event_t evt;
194
195     if (EVT.count < EVENTQSIZE) {
196         /* Save information in event record. */
197         evt.when = _EVT_getTicks();
198         evt.what = what;
199         evt.message = message;
200         evt.modifiers = but_stat;
201         evt.where_x = x;                /* Save mouse event position    */
202         evt.where_y = y;
203         evt.relative_x = mickeyX;
204         evt.relative_y = mickeyY;
205         evt.modifiers |= EVT.keyModifiers;
206         addEvent(&evt);                 /* Add to tail of event queue   */
207         }
208 }
209
210 /****************************************************************************
211 PARAMETERS:
212 mask        - Event mask
213 butstate    - Button state
214 x           - Mouse x coordinate
215 y           - Mouse y coordinate
216
217 REMARKS:
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
220 to the event queue.
221
222 Note: Interrupts are ON when this routine is called by the mouse driver code.
223 ****************************************************************************/
224 static void EVTAPI mouseISR(
225     uint mask,
226     uint butstate,
227     int x,
228     int y,
229     int mickeyX,
230     int mickeyY)
231 {
232     uint    ps;
233     uint    buttonMask;
234
235     if (mask & 1) {
236         /* Save the current mouse coordinates */
237         EVT.mx = x; EVT.my = y;
238
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.
243          */
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;
250             }
251         else {
252             EVT.oldMove = EVT.freeHead;         /* Save id of this move event   */
253             addMouseEvent(EVT_MOUSEMOVE,0,x,y,mickeyX,mickeyY,butstate);
254             }
255         _EVT_restoreInt(ps);
256         }
257     if (mask & 0x2A) {
258         ps = _EVT_disableInt();
259         buttonMask = 0;
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);
264         EVT.oldMove = -1;
265         _EVT_restoreInt(ps);
266         }
267     if (mask & 0x54) {
268         ps = _EVT_disableInt();
269         buttonMask = 0;
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);
274         EVT.oldMove = -1;
275         _EVT_restoreInt(ps);
276         }
277     EVT.oldKey = -1;
278 }
279
280 /****************************************************************************
281 REMARKS:
282 Keyboard interrupt handler function.
283
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)
288 {
289     processRawScanCode(PM_inpb(0x60));
290     PM_outpb(0x20,0x20);
291 }
292
293 /****************************************************************************
294 REMARKS:
295 Safely abort the event module upon catching a fatal error.
296 ****************************************************************************/
297 void _EVT_abort()
298 {
299     EVT_exit();
300     PM_fatalError("Unhandled exception!");
301 }
302
303 /****************************************************************************
304 PARAMETERS:
305 mouseMove   - Callback function to call wheneve the mouse needs to be moved
306
307 REMARKS:
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.
311
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)
317 {
318     int     i;
319
320     PM_init();
321     EVT.mouseMove = mouseMove;
322     _EVT_biosPtr = PM_getBIOSPointer();
323     EVT_resume();
324
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.
328      */
329     while ((i = _EVT_getKeyCode()) != 0)
330         addKeyEvent(EVT_KEYDOWN,i);
331 }
332
333 /****************************************************************************
334 REMARKS:
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)
340 {
341     static int      locked = 0;
342     int             stat;
343     uchar           mods;
344     PM_lockHandle   lh; /* Unused in DOS */
345
346     if (_EVT_useEvents) {
347         /* Initialise the event queue and enable our interrupt handlers */
348         initEventQueue();
349 #ifndef NO_KEYBOARD_INTERRUPT
350         PM_setKeyHandler(keyboardISR);
351 #endif
352 #ifndef NO_MOUSE_INTERRUPT
353         if ((haveMouse = detectMouse()) != 0) {
354             int oldmode = _EVT_foolMouse();
355             PM_setMouseHandler(0xFFFF,mouseISR);
356             _EVT_unfoolMouse(oldmode);
357             }
358 #endif
359
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
363          * CAPSLOCK.
364          */
365         EVT.keyModifiers = 0;
366         mods = PM_getByte(_EVT_biosPtr+0x17);
367         if (mods & 0x10)
368             EVT.keyModifiers |= EVT_SCROLLLOCK;
369         if (mods & 0x20)
370             EVT.keyModifiers |= EVT_NUMLOCK;
371         if (mods & 0x40)
372             EVT.keyModifiers |= EVT_CAPSLOCK;
373
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
376          * under real mode.
377          */
378         if (!locked) {
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
384              * correct).
385              */
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);
390             if (stat) {
391                 PM_fatalError("Page locking services failed - interrupt handling not safe!");
392                 exit(1);
393                 }
394             locked = 1;
395             }
396
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;
402         }
403 }
404
405 /****************************************************************************
406 REMARKS
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(
412     int xRes,
413     int yRes)
414 {
415     RMREGS  regs;
416
417     if (haveMouse) {
418         int oldmode = _EVT_foolMouse();
419         PM_resetMouseDriver(1);
420         regs.x.ax = 7;  /* Mouse function 7 - Set horizontal min and max */
421         regs.x.cx = 0;
422         regs.x.dx = xRes;
423         PM_int86(0x33,&regs,&regs);
424         regs.x.ax = 8;  /* Mouse function 8 - Set vertical min and max   */
425         regs.x.cx = 0;
426         regs.x.dx = yRes;
427         PM_int86(0x33,&regs,&regs);
428         _EVT_unfoolMouse(oldmode);
429         }
430 }
431
432 /****************************************************************************
433 REMARKS
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(
438     int *x,
439     int *y)
440 {
441     RMREGS  regs;
442
443     if (haveMouse) {
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,&regs,&regs);
449         _EVT_unfoolMouse(oldmode);
450         }
451 }
452
453 /****************************************************************************
454 REMARKS
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)
459 {
460     uchar   mods;
461
462     if (_EVT_installed) {
463         /* Restore the interrupt handlers */
464         PM_restoreKeyHandler();
465         if (haveMouse)
466             PM_restoreMouseHandler();
467         signal(SIGABRT, SIG_DFL);
468         signal(SIGFPE, SIG_DFL);
469         signal(SIGINT, SIG_DFL);
470
471         /* Set the keyboard modifier flags in the BIOS to our values */
472         EVT_allowLEDS(true);
473         mods = PM_getByte(_EVT_biosPtr+0x17) & ~0x70;
474         if (EVT.keyModifiers & EVT_SCROLLLOCK)
475             mods |= 0x10;
476         if (EVT.keyModifiers & EVT_NUMLOCK)
477             mods |= 0x20;
478         if (EVT.keyModifiers & EVT_CAPSLOCK)
479             mods |= 0x40;
480         PM_setByte(_EVT_biosPtr+0x17,mods);
481
482         /* Flag that we are no longer installed */
483         _EVT_installed = false;
484         }
485 }
486
487 /****************************************************************************
488 REMARKS
489 Exits the event module for program terminatation.
490 ****************************************************************************/
491 void EVTAPI EVT_exit(void)
492 {
493     EVT_suspend();
494 }