]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/MAI/bios_emulator/scitech/src/pm/x11/event.c
* Patch by Thomas Frieden, 13 Nov 2002:
[karo-tx-uboot.git] / board / MAI / bios_emulator / scitech / src / pm / x11 / 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:  Unix / X11
26 *
27 * Description:  X11 event queue implementation for the MGL.
28 *               This can be used both for windowed and fullscreen (DGA) modes.
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 static Display  *_EVT_dpy;
38 static Window    _EVT_win;
39
40 typedef struct {
41   int keycode;
42   int scancode;
43 } xkeymap;
44
45 xkeymap xkeymaps[] = {
46   { 9, KB_esc},
47   {24, KB_Q},
48   {25, KB_W},
49   {26, KB_E},
50   {27, KB_R},
51   {28, KB_T},
52   {29, KB_Y},
53   {30, KB_U},
54   {31, KB_I},
55   {32, KB_O},
56   {33, KB_P},
57 };
58
59 /*---------------------------- Implementation -----------------------------*/
60
61 /* These are not used under non-DOS systems */
62 #define _EVT_disableInt()       1
63 #define _EVT_restoreInt(flags)
64
65 /****************************************************************************
66 PARAMETERS:
67 scanCode    - Scan code to test
68
69 REMARKS:
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)
74
75 /****************************************************************************
76 REMARKS:
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
80 events.
81 ****************************************************************************/
82 ulong _EVT_getTicks(void)
83 {
84   static unsigned starttime = 0;
85   struct timeval t;
86   
87   gettimeofday(&t, NULL);
88   if (starttime == 0)
89     starttime = t.tv_sec * 1000 + (t.tv_usec/1000);
90   return ((t.tv_sec * 1000 + (t.tv_usec/1000)) - starttime);
91 }
92
93 static int getScancode(int keycode)
94 {
95   return keycode-8;
96 }
97
98 /****************************************************************************
99 REMARKS:
100 Pumps all messages in the application message queue into our event queue.
101 ****************************************************************************/
102 #ifdef X11_CORE
103 static void _EVT_pumpX11Messages(void)
104 #else
105 static void _EVT_pumpMessages(void)
106 #endif
107 {
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.
111   event_t evt;
112   XEvent  ev;
113   static int old_mx = 0, old_my = 0, buts = 0, c;
114   char buf[2];
115   
116   while (XPending(_EVT_dpy) && XNextEvent(_EVT_dpy,&ev)) {
117     evt.when = _MGL_getTicks();
118
119     switch(ev.type){
120     case KeyPress:
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];
126       break;
127     case KeyRelease:
128       c = getScancode(ev.xkey.keycode);
129       evt.what = EVT_KEYUP;
130       evt.message = keyUpMsg[c];
131       if(count < EVENTQSIZE)
132         addEvent(&evt);
133       keyUpMsg[c] = 0;
134       repeatKey[c] = 0;
135       break;
136     case ButtonPress:
137       evt.what = EVT_MOUSEDOWN;
138       if(ev.xbutton.button == 1){
139         buts |= EVT_LEFTBUT;
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;
147       }
148       evt.modifiers = modifiers | buts;
149       
150       break;
151     case ButtonRelease:
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;
162       }
163       evt.modifiers = modifiers | buts;
164
165       break;
166     case MotionNotify:
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;
174       break;
175     }
176     if (count < EVENTQSIZE)
177       addEvent(&evt);
178   }
179
180 }
181
182 /****************************************************************************
183 REMARKS:
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
189 to the raw format.
190 ****************************************************************************/
191 #define _EVT_maskKeyCode(evt)
192
193 /****************************************************************************
194 REMARKS:
195 Safely abort the event module upon catching a fatal error.
196 ****************************************************************************/
197 void _EVT_abort()
198 {
199     EVT_exit();
200     PM_fatalError("Unhandled exception!");
201 }
202
203 /****************************************************************************
204 PARAMETERS:
205 mouseMove   - Callback function to call wheneve the mouse needs to be moved
206
207 REMARKS:
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.
211
212 We use handler number 2 of the mouse libraries interrupt handlers for our
213 event handling routines.
214 ****************************************************************************/
215 #ifdef X11_CORE
216 void EVTAPI EVT_initX11(
217 #else
218 void EVTAPI EVT_init(
219 #endif
220     _EVT_mouseMoveHandler mouseMove)
221 {
222   int result, i,j,k;
223   XDeviceInfoPtr    list,slist;
224
225   /* Initialise the event queue */
226   _mouseMove = mouseMove;
227   initEventQueue();
228   memset(keyUpMsg,0,sizeof(keyUpMsg));
229   
230
231   /* query server for input extensions */
232   result =XQueryExtension(_EVT_dpy,"XInputExtension",&i,&j,&k);
233   if(!result) {
234     fprintf(stderr,"Your server doesn't support XInput Extensions\n");
235     fprintf(stderr,"X11 Joystick disabled\n");
236   }
237   list = XListInputDevices(_EVT_dpy,&result);
238   if (!list) {
239     fprintf(stderr,"No extended input devices found !!\n");
240     fprintf(stderr,"X11 Joystick disabled\n");
241   }
242
243
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);
248 }
249
250 /****************************************************************************
251 REMARKS
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(
257     int xRes,
258     int yRes)
259 {
260     rangeX = xRes;
261     rangeY = yRes;
262 }
263
264 /****************************************************************************
265 REMARKS:
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)
271 {
272     // Do nothing for non DOS systems
273 }
274
275 /****************************************************************************
276 REMARKS
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)
281 {
282     // Do nothing for non DOS systems
283 }
284
285 /****************************************************************************
286 REMARKS
287 Exits the event module for program terminatation.
288 ****************************************************************************/
289 void EVT_exit(void)
290 {
291     /* Restore signal handlers */
292     signal(SIGABRT, SIG_DFL);
293     signal(SIGFPE, SIG_DFL);
294     signal(SIGINT, SIG_DFL);
295
296     // TODO: Do any OS specific cleanup in here
297 }
298
299 /****************************************************************************
300 REMARKS
301 Sets the current X11 display
302 ****************************************************************************/
303 void EVT_setX11Display(Display *dpy, Window win)
304 {
305   _EVT_dpy = dpy;
306   _EVT_win = win;
307 }