1 /****************************************************************************
3 * SciTech Multi-platform Graphics 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 * ========================================================================
27 * Description: Win32 implementation for the SciTech cross platform
30 ****************************************************************************/
32 /*---------------------------- Global Variables ---------------------------*/
34 static ushort keyUpMsg[256] = {0}; /* Table of key up messages */
35 static int rangeX,rangeY; /* Range of mouse coordinates */
37 /*---------------------------- Implementation -----------------------------*/
39 /* These are not used under Win32 */
40 #define _EVT_disableInt() 1
41 #define _EVT_restoreInt(flags) (void)(flags)
43 /****************************************************************************
45 scanCode - Scan code to test
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)
53 /****************************************************************************
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
59 ****************************************************************************/
60 ulong _EVT_getTicks(void)
61 { return timeGetTime(); }
63 /****************************************************************************
65 Pumps all messages in the message queue from Win32 into our event queue.
66 ****************************************************************************/
67 void _EVT_pumpMessages(void)
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) {
79 evt.what = EVT_MOUSEMOVE;
81 case WM_LBUTTONDBLCLK:
82 evt.what = EVT_MOUSEDOWN;
83 evt.message = EVT_LEFTBMASK | EVT_DBLCLICK;
86 evt.what = EVT_MOUSEDOWN;
87 evt.message = EVT_LEFTBMASK;
90 evt.what = EVT_MOUSEUP;
91 evt.message = EVT_LEFTBMASK;
93 case WM_RBUTTONDBLCLK:
94 evt.what = EVT_MOUSEDOWN | EVT_DBLCLICK;
95 evt.message = EVT_RIGHTBMASK;
98 evt.what = EVT_MOUSEDOWN;
99 evt.message = EVT_RIGHTBMASK;
102 evt.what = EVT_MOUSEUP;
103 evt.message = EVT_RIGHTBMASK;
105 case WM_MBUTTONDBLCLK:
106 evt.what = EVT_MOUSEDOWN | EVT_DBLCLICK;
107 evt.message = EVT_MIDDLEBMASK;
110 evt.what = EVT_MOUSEDOWN;
111 evt.message = EVT_MIDDLEBMASK;
114 evt.what = EVT_MOUSEUP;
115 evt.message = EVT_MIDDLEBMASK;
119 if (HIWORD(msg.lParam) & KF_REPEAT) {
120 evt.what = EVT_KEYREPEAT;
123 evt.what = EVT_KEYDOWN;
128 evt.what = EVT_KEYUP;
132 /* Convert mouse event modifier flags */
133 if (evt.what & EVT_MOUSEEVT) {
135 evt.where_x = ((long)msg.pt.x * rangeX) / _PM_deskX;
136 evt.where_y = ((long)msg.pt.y * rangeY) / _PM_deskY;
139 ScreenToClient(_PM_hwndConsole, &msg.pt);
140 evt.where_x = msg.pt.x;
141 evt.where_y = msg.pt.y;
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! */
155 EVT.oldMove = EVT.freeHead; /* Save id of this move event */
156 /* evt.relative_x = mickeyX; // TODO! */
157 /* evt.relative_y = mickeyY; // TODO! */
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;
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;
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;
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;
203 /* Add time stamp and add the event to the queue */
205 if (EVT.count < EVENTQSIZE)
208 DispatchMessage(&msg);
212 /****************************************************************************
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
220 ****************************************************************************/
221 #define _EVT_maskKeyCode(evt)
223 /****************************************************************************
225 Safely abort the event module upon catching a fatal error.
226 ****************************************************************************/
232 PM_fatalError("Unhandled exception!");
235 /****************************************************************************
237 mouseMove - Callback function to call wheneve the mouse needs to be moved
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.
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)
250 /* Initialise the event queue */
251 EVT.mouseMove = mouseMove;
253 memset(keyUpMsg,0,sizeof(keyUpMsg));
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);
261 /****************************************************************************
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(
275 /****************************************************************************
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(
284 /* Scale coordinates up to desktop coordinates first */
285 int scaledX = (*x * _PM_deskX) / rangeX;
286 int scaledY = (*y * _PM_deskY) / rangeY;
288 /* Scale coordinates back to screen coordinates again */
289 *x = (scaledX * rangeX) / _PM_deskX;
290 *y = (scaledY * rangeY) / _PM_deskY;
291 SetCursorPos(scaledX,scaledY);
294 /****************************************************************************
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)
302 /* Do nothing for Win32 */
305 /****************************************************************************
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)
312 /* Do nothing for Win32 */
315 /****************************************************************************
317 Exits the event module for program terminatation.
318 ****************************************************************************/
321 /* Restore signal handlers */
322 signal(SIGABRT, SIG_DFL);
323 signal(SIGFPE, SIG_DFL);
324 signal(SIGINT, SIG_DFL);
327 /****************************************************************************
329 Returns the mask indicating what joystick axes are attached.
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.
342 EVT_joySetLowerRight, EVT_joySetCenter, EVT_joyIsPresent
343 ****************************************************************************/
344 int EVTAPI EVT_joyIsPresent(void)
346 /* TODO: Implement joystick code based on DirectX! */
350 /****************************************************************************
352 Polls the joystick for position and button information.
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.
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
370 EVT_getNext, EVT_peekNext, EVT_joySetUpperLeft, EVT_joySetLowerRight,
371 EVT_joySetCenter, EVT_joyIsPresent
372 ****************************************************************************/
373 void EVTAPI EVT_pollJoystick(void)
377 /****************************************************************************
379 Calibrates the joystick upper left position
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
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.
399 EVT_joySetUpperLeft, EVT_joySetLowerRight, EVT_joyIsPresent
400 ****************************************************************************/
401 void EVTAPI EVT_joySetUpperLeft(void)
405 /****************************************************************************
407 Calibrates the joystick lower right position
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
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.
427 EVT_joySetUpperLeft, EVT_joySetCenter, EVT_joyIsPresent
428 ****************************************************************************/
429 void EVTAPI EVT_joySetLowerRight(void)
433 /****************************************************************************
435 Calibrates the joystick center position
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
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.
455 EVT_joySetUpperLeft, EVT_joySetLowerRight, EVT_joySetCenter
456 ****************************************************************************/
457 void EVTAPI EVT_joySetCenter(void)