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: Unix / X11
27 * Description: X11 event queue implementation for the MGL.
28 * This can be used both for windowed and fullscreen (DGA) modes.
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 static Display *_EVT_dpy;
38 static Window _EVT_win;
45 xkeymap xkeymaps[] = {
59 /*---------------------------- Implementation -----------------------------*/
61 /* These are not used under non-DOS systems */
62 #define _EVT_disableInt() 1
63 #define _EVT_restoreInt(flags)
65 /****************************************************************************
67 scanCode - Scan code to test
70 This macro determines if a specified key is currently down at the
71 time that the call is made.
72 ****************************************************************************/
73 #define _EVT_isKeyDown(scanCode) (keyUpMsg[scanCode] != 0)
75 /****************************************************************************
77 This function is used to return the number of ticks since system
78 startup in milliseconds. This should be the same value that is placed into
79 the time stamp fields of events, and is used to implement auto mouse down
81 ****************************************************************************/
82 ulong _EVT_getTicks(void)
84 static unsigned starttime = 0;
87 gettimeofday(&t, NULL);
89 starttime = t.tv_sec * 1000 + (t.tv_usec/1000);
90 return ((t.tv_sec * 1000 + (t.tv_usec/1000)) - starttime);
93 static int getScancode(int keycode)
98 /****************************************************************************
100 Pumps all messages in the application message queue into our event queue.
101 ****************************************************************************/
103 static void _EVT_pumpX11Messages(void)
105 static void _EVT_pumpMessages(void)
108 // TODO: The purpose of this function is to read all keyboard and mouse
109 // events from the OS specific event queue, translate them and post
110 // them into the SciTech event queue.
113 static int old_mx = 0, old_my = 0, buts = 0, c;
116 while (XPending(_EVT_dpy) && XNextEvent(_EVT_dpy,&ev)) {
117 evt.when = _MGL_getTicks();
121 c = getScancode(ev.xkey.keycode);
122 evt.what = EVT_KEYDOWN;
123 evt.message = c << 8;
124 XLookupString(&ev.xkey, buf, 2, NULL, NULL);
125 evt.message |= buf[0];
128 c = getScancode(ev.xkey.keycode);
129 evt.what = EVT_KEYUP;
130 evt.message = keyUpMsg[c];
131 if(count < EVENTQSIZE)
137 evt.what = EVT_MOUSEDOWN;
138 if(ev.xbutton.button == 1){
140 evt.message = EVT_LEFTBMASK;
141 }else if(ev.xbutton.button == 2){
142 buts |= EVT_MIDDLEBUT;
143 evt.message = EVT_MIDDLEBMASK;
144 }else if(ev.xbutton.button == 3){
145 buts |= EVT_RIGHTBUT;
146 evt.message = EVT_RIGHTBMASK;
148 evt.modifiers = modifiers | buts;
152 evt.what = EVT_MOUSEUP;
153 if(ev.xbutton.button == 1){
154 buts &= ~EVT_LEFTBUT;
155 evt.message = EVT_LEFTBMASK;
156 }else if(ev.xbutton.button == 2){
157 buts &= ~EVT_MIDDLEBUT;
158 evt.message = EVT_MIDDLEBMASK;
159 }else if(ev.xbutton.button == 3){
160 buts &= ~EVT_RIGHTBUT;
161 evt.message = EVT_RIGHTBMASK;
163 evt.modifiers = modifiers | buts;
167 evt.what = EVT_MOUSEMOVE;
168 evt.where_x = ev.xmotion.x;
169 evt.where_y = ev.xmotion.y;
170 evt.relative_x = evt.where_x - old_mx;
171 evt.relative_y = evt.where_y - old_my;
172 old_mx = evt.where_x;
173 old_my = evt.where_y;
176 if (count < EVENTQSIZE)
182 /****************************************************************************
184 This macro/function is used to converts the scan codes reported by the
185 keyboard to our event libraries normalised format. We only have one scan
186 code for the 'A' key, and use shift modifiers to determine if it is a
187 Ctrl-F1, Alt-F1 etc. The raw scan codes from the keyboard work this way,
188 but the OS gives us 'cooked' scan codes, we have to translate them back
190 ****************************************************************************/
191 #define _EVT_maskKeyCode(evt)
193 /****************************************************************************
195 Safely abort the event module upon catching a fatal error.
196 ****************************************************************************/
200 PM_fatalError("Unhandled exception!");
203 /****************************************************************************
205 mouseMove - Callback function to call wheneve the mouse needs to be moved
208 Initiliase the event handling module. Here we install our mouse handling ISR
209 to be called whenever any button's are pressed or released. We also build
210 the free list of events in the event queue.
212 We use handler number 2 of the mouse libraries interrupt handlers for our
213 event handling routines.
214 ****************************************************************************/
216 void EVTAPI EVT_initX11(
218 void EVTAPI EVT_init(
220 _EVT_mouseMoveHandler mouseMove)
223 XDeviceInfoPtr list,slist;
225 /* Initialise the event queue */
226 _mouseMove = mouseMove;
228 memset(keyUpMsg,0,sizeof(keyUpMsg));
231 /* query server for input extensions */
232 result =XQueryExtension(_EVT_dpy,"XInputExtension",&i,&j,&k);
234 fprintf(stderr,"Your server doesn't support XInput Extensions\n");
235 fprintf(stderr,"X11 Joystick disabled\n");
237 list = XListInputDevices(_EVT_dpy,&result);
239 fprintf(stderr,"No extended input devices found !!\n");
240 fprintf(stderr,"X11 Joystick disabled\n");
244 /* Catch program termination signals so we can clean up properly */
245 signal(SIGABRT, _EVT_abort);
246 signal(SIGFPE, _EVT_abort);
247 signal(SIGINT, _EVT_abort);
250 /****************************************************************************
252 Changes the range of coordinates returned by the mouse functions to the
253 specified range of values. This is used when changing between graphics
254 modes set the range of mouse coordinates for the new display mode.
255 ****************************************************************************/
256 void EVTAPI EVT_setMouseRange(
264 /****************************************************************************
266 Initiailises the internal event handling modules. The EVT_suspend function
267 can be called to suspend event handling (such as when shelling out to DOS),
268 and this function can be used to resume it again later.
269 ****************************************************************************/
270 void EVT_resume(void)
272 // Do nothing for non DOS systems
275 /****************************************************************************
277 Suspends all of our event handling operations. This is also used to
278 de-install the event handling code.
279 ****************************************************************************/
280 void EVT_suspend(void)
282 // Do nothing for non DOS systems
285 /****************************************************************************
287 Exits the event module for program terminatation.
288 ****************************************************************************/
291 /* Restore signal handlers */
292 signal(SIGABRT, SIG_DFL);
293 signal(SIGFPE, SIG_DFL);
294 signal(SIGINT, SIG_DFL);
296 // TODO: Do any OS specific cleanup in here
299 /****************************************************************************
301 Sets the current X11 display
302 ****************************************************************************/
303 void EVT_setX11Display(Display *dpy, Window win)