]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/MAI/bios_emulator/scitech/src/pm/win32/event.c
* Patch by Thomas Frieden, 13 Nov 2002:
[karo-tx-uboot.git] / board / MAI / bios_emulator / scitech / src / pm / win32 / event.c
1 /****************************************************************************
2 *
3 *                   SciTech Multi-platform Graphics 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:  Win32
26 *
27 * Description:  Win32 implementation for the SciTech cross platform
28 *               event library.
29 *
30 ****************************************************************************/
31
32 /*---------------------------- Global Variables ---------------------------*/
33
34 static ushort   keyUpMsg[256] = {0};    /* Table of key up messages     */
35 static int      rangeX,rangeY;          /* Range of mouse coordinates   */
36
37 /*---------------------------- Implementation -----------------------------*/
38
39 /* These are not used under Win32 */
40 #define _EVT_disableInt()       1
41 #define _EVT_restoreInt(flags)  (void)(flags)
42
43 /****************************************************************************
44 PARAMETERS:
45 scanCode    - Scan code to test
46
47 REMARKS:
48 This macro determines if a specified key is currently down at the
49 time that the call is made.
50 ****************************************************************************/
51 #define _EVT_isKeyDown(scanCode)    (keyUpMsg[scanCode] != 0)
52
53 /****************************************************************************
54 REMARKS:
55 This function is used to return the number of ticks since system
56 startup in milliseconds. This should be the same value that is placed into
57 the time stamp fields of events, and is used to implement auto mouse down
58 events.
59 ****************************************************************************/
60 ulong _EVT_getTicks(void)
61 { return timeGetTime(); }
62
63 /****************************************************************************
64 REMARKS:
65 Pumps all messages in the message queue from Win32 into our event queue.
66 ****************************************************************************/
67 void _EVT_pumpMessages(void)
68 {
69     MSG     msg;
70     MSG     charMsg;
71     event_t evt;
72
73     // TODO: Add support for DirectInput! We can't support relative mouse
74     //       movement motion counters without DirectInput ;-(.
75     while (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
76         memset(&evt,0,sizeof(evt));
77         switch (msg.message) {
78             case WM_MOUSEMOVE:
79                 evt.what = EVT_MOUSEMOVE;
80                 break;
81             case WM_LBUTTONDBLCLK:
82                 evt.what = EVT_MOUSEDOWN;
83                 evt.message = EVT_LEFTBMASK | EVT_DBLCLICK;
84                 break;
85             case WM_LBUTTONDOWN:
86                 evt.what = EVT_MOUSEDOWN;
87                 evt.message = EVT_LEFTBMASK;
88                 break;
89             case WM_LBUTTONUP:
90                 evt.what = EVT_MOUSEUP;
91                 evt.message = EVT_LEFTBMASK;
92                 break;
93             case WM_RBUTTONDBLCLK:
94                 evt.what = EVT_MOUSEDOWN | EVT_DBLCLICK;
95                 evt.message = EVT_RIGHTBMASK;
96                 break;
97             case WM_RBUTTONDOWN:
98                 evt.what = EVT_MOUSEDOWN;
99                 evt.message = EVT_RIGHTBMASK;
100                 break;
101             case WM_RBUTTONUP:
102                 evt.what = EVT_MOUSEUP;
103                 evt.message = EVT_RIGHTBMASK;
104                 break;
105             case WM_MBUTTONDBLCLK:
106                 evt.what = EVT_MOUSEDOWN | EVT_DBLCLICK;
107                 evt.message = EVT_MIDDLEBMASK;
108                 break;
109             case WM_MBUTTONDOWN:
110                 evt.what = EVT_MOUSEDOWN;
111                 evt.message = EVT_MIDDLEBMASK;
112                 break;
113             case WM_MBUTTONUP:
114                 evt.what = EVT_MOUSEUP;
115                 evt.message = EVT_MIDDLEBMASK;
116                 break;
117             case WM_KEYDOWN:
118             case WM_SYSKEYDOWN:
119                 if (HIWORD(msg.lParam) & KF_REPEAT) {
120                     evt.what = EVT_KEYREPEAT;
121                     }
122                 else {
123                     evt.what = EVT_KEYDOWN;
124                     }
125                 break;
126             case WM_KEYUP:
127             case WM_SYSKEYUP:
128                 evt.what = EVT_KEYUP;
129                 break;
130             }
131
132         /* Convert mouse event modifier flags */
133         if (evt.what & EVT_MOUSEEVT) {
134             if (_PM_deskX) {
135                 evt.where_x = ((long)msg.pt.x * rangeX) / _PM_deskX;
136                 evt.where_y = ((long)msg.pt.y * rangeY) / _PM_deskY;
137                 }
138             else {
139                 ScreenToClient(_PM_hwndConsole, &msg.pt);
140                 evt.where_x = msg.pt.x;
141                 evt.where_y = msg.pt.y;
142                 }
143             if (evt.what == EVT_MOUSEMOVE) {
144                 /* Save the current mouse position */
145                 EVT.mx = evt.where_x;
146                 EVT.my = evt.where_y;
147                 if (EVT.oldMove != -1) {
148                     EVT.evtq[EVT.oldMove].where_x = evt.where_x;/* Modify existing one  */
149                     EVT.evtq[EVT.oldMove].where_y = evt.where_y;
150 //                  EVT.evtq[EVT.oldMove].relative_x += mickeyX;    // TODO!
151 //                  EVT.evtq[EVT.oldMove].relative_y += mickeyY;    // TODO!
152                     evt.what = 0;
153                     }
154                 else {
155                     EVT.oldMove = EVT.freeHead; /* Save id of this move event   */
156 //                  evt.relative_x = mickeyX;    // TODO!
157 //                  evt.relative_y = mickeyY;    // TODO!
158                     }
159                 }
160             else
161                 EVT.oldMove = -1;
162             if (msg.wParam & MK_LBUTTON)
163                 evt.modifiers |= EVT_LEFTBUT;
164             if (msg.wParam & MK_RBUTTON)
165                 evt.modifiers |= EVT_RIGHTBUT;
166             if (msg.wParam & MK_MBUTTON)
167                 evt.modifiers |= EVT_MIDDLEBUT;
168             if (msg.wParam & MK_SHIFT)
169                 evt.modifiers |= EVT_SHIFTKEY;
170             if (msg.wParam & MK_CONTROL)
171                 evt.modifiers |= EVT_CTRLSTATE;
172             }
173
174         /* Convert keyboard codes */
175         TranslateMessage(&msg);
176         if (evt.what & EVT_KEYEVT) {
177             int scanCode = (msg.lParam >> 16) & 0xFF;
178             if (evt.what == EVT_KEYUP) {
179                 /* Get message for keyup code from table of cached down values */
180                 evt.message = keyUpMsg[scanCode];
181                 keyUpMsg[scanCode] = 0;
182                 }
183             else {
184                 if (PeekMessage(&charMsg,NULL,WM_CHAR,WM_CHAR,PM_REMOVE))
185                     evt.message = charMsg.wParam;
186                 if (PeekMessage(&charMsg,NULL,WM_SYSCHAR,WM_SYSCHAR,PM_REMOVE))
187                     evt.message = charMsg.wParam;
188                 evt.message |= ((msg.lParam >> 8) & 0xFF00);
189                 keyUpMsg[scanCode] = (ushort)evt.message;
190                 }
191             if (evt.what == EVT_KEYREPEAT)
192                 evt.message |= (msg.lParam << 16);
193             if (HIWORD(msg.lParam) & KF_ALTDOWN)
194                 evt.modifiers |= EVT_ALTSTATE;
195             if (GetKeyState(VK_SHIFT) & 0x8000U)
196                 evt.modifiers |= EVT_SHIFTKEY;
197             if (GetKeyState(VK_CONTROL) & 0x8000U)
198                 evt.modifiers |= EVT_CTRLSTATE;
199             EVT.oldMove = -1;
200             }
201
202         if (evt.what != 0) {
203             /* Add time stamp and add the event to the queue */
204             evt.when = msg.time;
205             if (EVT.count < EVENTQSIZE)
206                 addEvent(&evt);
207             }
208         DispatchMessage(&msg);
209         }
210 }
211
212 /****************************************************************************
213 REMARKS:
214 This macro/function is used to converts the scan codes reported by the
215 keyboard to our event libraries normalised format. We only have one scan
216 code for the 'A' key, and use shift modifiers to determine if it is a
217 Ctrl-F1, Alt-F1 etc. The raw scan codes from the keyboard work this way,
218 but the OS gives us 'cooked' scan codes, we have to translate them back
219 to the raw format.
220 ****************************************************************************/
221 #define _EVT_maskKeyCode(evt)
222
223 /****************************************************************************
224 REMARKS:
225 Safely abort the event module upon catching a fatal error.
226 ****************************************************************************/
227 void _EVT_abort(
228     int signal)
229 {
230     (void)signal;
231     EVT_exit();
232     PM_fatalError("Unhandled exception!");
233 }
234
235 /****************************************************************************
236 PARAMETERS:
237 mouseMove   - Callback function to call wheneve the mouse needs to be moved
238
239 REMARKS:
240 Initiliase the event handling module. Here we install our mouse handling ISR
241 to be called whenever any button's are pressed or released. We also build
242 the free list of events in the event queue.
243
244 We use handler number 2 of the mouse libraries interrupt handlers for our
245 event handling routines.
246 ****************************************************************************/
247 void EVTAPI EVT_init(
248     _EVT_mouseMoveHandler mouseMove)
249 {
250     /* Initialise the event queue */
251     EVT.mouseMove = mouseMove;
252     initEventQueue();
253     memset(keyUpMsg,0,sizeof(keyUpMsg));
254
255     /* Catch program termination signals so we can clean up properly */
256     signal(SIGABRT, _EVT_abort);
257     signal(SIGFPE, _EVT_abort);
258     signal(SIGINT, _EVT_abort);
259 }
260
261 /****************************************************************************
262 REMARKS
263 Changes the range of coordinates returned by the mouse functions to the
264 specified range of values. This is used when changing between graphics
265 modes set the range of mouse coordinates for the new display mode.
266 ****************************************************************************/
267 void EVTAPI EVT_setMouseRange(
268     int xRes,
269     int yRes)
270 {
271     rangeX = xRes;
272     rangeY = yRes;
273 }
274
275 /****************************************************************************
276 REMARKS
277 Modifes the mouse coordinates as necessary if scaling to OS coordinates,
278 and sets the OS mouse cursor position.
279 ****************************************************************************/
280 void _EVT_setMousePos(
281     int *x,
282     int *y)
283 {
284     /* Scale coordinates up to desktop coordinates first */
285     int scaledX = (*x * _PM_deskX) / rangeX;
286     int scaledY = (*y * _PM_deskY) / rangeY;
287
288     /* Scale coordinates back to screen coordinates again */
289     *x = (scaledX * rangeX) / _PM_deskX;
290     *y = (scaledY * rangeY) / _PM_deskY;
291     SetCursorPos(scaledX,scaledY);
292 }
293
294 /****************************************************************************
295 REMARKS:
296 Initiailises the internal event handling modules. The EVT_suspend function
297 can be called to suspend event handling (such as when shelling out to DOS),
298 and this function can be used to resume it again later.
299 ****************************************************************************/
300 void EVT_resume(void)
301 {
302     // Do nothing for Win32
303 }
304
305 /****************************************************************************
306 REMARKS
307 Suspends all of our event handling operations. This is also used to
308 de-install the event handling code.
309 ****************************************************************************/
310 void EVT_suspend(void)
311 {
312     // Do nothing for Win32
313 }
314
315 /****************************************************************************
316 REMARKS
317 Exits the event module for program terminatation.
318 ****************************************************************************/
319 void EVT_exit(void)
320 {
321     /* Restore signal handlers */
322     signal(SIGABRT, SIG_DFL);
323     signal(SIGFPE, SIG_DFL);
324     signal(SIGINT, SIG_DFL);
325 }
326
327 /****************************************************************************
328 DESCRIPTION:
329 Returns the mask indicating what joystick axes are attached.
330
331 HEADER:
332 event.h
333
334 REMARKS:
335 This function is used to detect the attached joysticks, and determine
336 what axes are present and functioning. This function will re-detect any
337 attached joysticks when it is called, so if the user forgot to attach
338 the joystick when the application started, you can call this function to
339 re-detect any newly attached joysticks.
340
341 SEE ALSO:
342 EVT_joySetLowerRight, EVT_joySetCenter, EVT_joyIsPresent
343 ****************************************************************************/
344 int EVTAPI EVT_joyIsPresent(void)
345 {
346     // TODO: Implement joystick code based on DirectX!
347     return 0;
348 }
349
350 /****************************************************************************
351 DESCRIPTION:
352 Polls the joystick for position and button information.
353
354 HEADER:
355 event.h
356
357 REMARKS:
358 This routine is used to poll analogue joysticks for button and position
359 information. It should be called once for each main loop of the user
360 application, just before processing all pending events via EVT_getNext.
361 All information polled from the joystick will be posted to the event
362 queue for later retrieval.
363
364 Note:   Most analogue joysticks will provide readings that change even
365         though the joystick has not moved. Hence if you call this routine
366         you will likely get an EVT_JOYMOVE event every time through your
367         event loop.
368
369 SEE ALSO:
370 EVT_getNext, EVT_peekNext, EVT_joySetUpperLeft, EVT_joySetLowerRight,
371 EVT_joySetCenter, EVT_joyIsPresent
372 ****************************************************************************/
373 void EVTAPI EVT_pollJoystick(void)
374 {
375 }
376
377 /****************************************************************************
378 DESCRIPTION:
379 Calibrates the joystick upper left position
380
381 HEADER:
382 event.h
383
384 REMARKS:
385 This function can be used to zero in on better joystick calibration factors,
386 which may work better than the default simplistic calibration (which assumes
387 the joystick is centered when the event library is initialised).
388 To use this function, ask the user to hold the stick in the upper left
389 position and then have them press a key or button. and then call this
390 function. This function will then read the joystick and update the
391 calibration factors.
392
393 Usually, assuming that the stick was centered when the event library was
394 initialized, you really only need to call EVT_joySetLowerRight since the
395 upper left position is usually always 0,0 on most joysticks. However, the
396 safest procedure is to call all three calibration functions.
397
398 SEE ALSO:
399 EVT_joySetUpperLeft, EVT_joySetLowerRight, EVT_joyIsPresent
400 ****************************************************************************/
401 void EVTAPI EVT_joySetUpperLeft(void)
402 {
403 }
404
405 /****************************************************************************
406 DESCRIPTION:
407 Calibrates the joystick lower right position
408
409 HEADER:
410 event.h
411
412 REMARKS:
413 This function can be used to zero in on better joystick calibration factors,
414 which may work better than the default simplistic calibration (which assumes
415 the joystick is centered when the event library is initialised).
416 To use this function, ask the user to hold the stick in the lower right
417 position and then have them press a key or button. and then call this
418 function. This function will then read the joystick and update the
419 calibration factors.
420
421 Usually, assuming that the stick was centered when the event library was
422 initialized, you really only need to call EVT_joySetLowerRight since the
423 upper left position is usually always 0,0 on most joysticks. However, the
424 safest procedure is to call all three calibration functions.
425
426 SEE ALSO:
427 EVT_joySetUpperLeft, EVT_joySetCenter, EVT_joyIsPresent
428 ****************************************************************************/
429 void EVTAPI EVT_joySetLowerRight(void)
430 {
431 }
432
433 /****************************************************************************
434 DESCRIPTION:
435 Calibrates the joystick center position
436
437 HEADER:
438 event.h
439
440 REMARKS:
441 This function can be used to zero in on better joystick calibration factors,
442 which may work better than the default simplistic calibration (which assumes
443 the joystick is centered when the event library is initialised).
444 To use this function, ask the user to hold the stick in the center
445 position and then have them press a key or button. and then call this
446 function. This function will then read the joystick and update the
447 calibration factors.
448
449 Usually, assuming that the stick was centered when the event library was
450 initialized, you really only need to call EVT_joySetLowerRight since the
451 upper left position is usually always 0,0 on most joysticks. However, the
452 safest procedure is to call all three calibration functions.
453
454 SEE ALSO:
455 EVT_joySetUpperLeft, EVT_joySetLowerRight, EVT_joySetCenter
456 ****************************************************************************/
457 void EVTAPI EVT_joySetCenter(void)
458 {
459 }
460