2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2004 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * GEM SDL video driver implementation
30 * inspired from the Dummy SDL driver
34 * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
42 #include "SDL_sysevents.h"
43 #include "SDL_events_c.h"
44 #include "SDL_gemvideo.h"
45 #include "SDL_gemevents_c.h"
46 #include "SDL_atarikeys.h" /* for keyboard scancodes */
47 #include "SDL_xbiosinterrupt_s.h"
51 #define ATARIBIOS_MAXKEYS 128
55 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
56 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
57 static unsigned char gem_currentascii[ATARIBIOS_MAXKEYS];
59 /* The translation tables from a console scancode to a SDL keysym */
60 static SDLKey keymap[ATARIBIOS_MAXKEYS];
62 /* Functions prototypes */
64 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym);
65 static int do_messages(_THIS, short *message);
66 static void do_keyboard(short kc, short ks);
67 static void do_mouse(_THIS, short mx, short my, short mb, short ks);
71 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym)
73 /* Set the keysym information */
74 keysym->scancode = scancode;
77 keysym->sym = asciicode;
79 keysym->sym = keymap[scancode];
81 keysym->mod = KMOD_NONE;
87 void GEM_InitOSKeymap(_THIS)
91 memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
92 memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
93 memset(gem_currentascii, 0, sizeof(gem_currentascii));
95 /* Initialize keymap */
96 for ( i=0; i<sizeof(keymap); i++ )
97 keymap[i] = SDLK_UNKNOWN;
100 for ( i = 0; i<10; i++ )
101 keymap[SCANCODE_F1 + i] = SDLK_F1+i;
104 keymap[SCANCODE_HELP] = SDLK_HELP;
105 keymap[SCANCODE_UNDO] = SDLK_UNDO;
106 keymap[SCANCODE_INSERT] = SDLK_INSERT;
107 keymap[SCANCODE_CLRHOME] = SDLK_HOME;
108 keymap[SCANCODE_UP] = SDLK_UP;
109 keymap[SCANCODE_DOWN] = SDLK_DOWN;
110 keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
111 keymap[SCANCODE_LEFT] = SDLK_LEFT;
114 keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
115 keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
116 keymap[SCANCODE_TAB] = SDLK_TAB;
117 keymap[SCANCODE_ENTER] = SDLK_RETURN;
118 keymap[SCANCODE_DELETE] = SDLK_DELETE;
119 keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
120 keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
121 keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
122 keymap[SCANCODE_LEFTALT] = SDLK_LALT;
123 keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
126 GEM_mouse_relative = SDL_FALSE;
129 void GEM_PumpEvents(_THIS)
131 short mousex, mousey, mouseb, dummy;
132 short kstate, prevkc, prevks;
136 memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
141 int quit, resultat, event_mask;
147 event_mask = MU_MESAG|MU_TIMER|MU_KEYBD;
148 if (!GEM_fullscreen && (GEM_handle>=0)) {
149 wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
150 event_mask |= MU_M1|MU_M2;
155 resultat = evnt_multi(
158 MO_ENTER,x2,y2,w2,h2,
159 MO_LEAVE,x2,y2,w2,h2,
162 &dummy,&dummy,&dummy,&kstate,&kc,&dummy
165 /* Message event ? */
166 if (resultat & MU_MESAG)
167 quit = do_messages(this, buffer);
169 /* Keyboard event ? */
170 if (resultat & MU_KEYBD) {
171 if ((prevkc != kc) || (prevks != kstate)) {
172 do_keyboard(kc,kstate);
174 /* Avoid looping, if repeating same key */
179 /* Mouse entering/leaving window */
180 if (resultat & MU_M1) {
181 if (this->input_grab == SDL_GRAB_OFF) {
182 if ( !(SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
183 SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
187 if (resultat & MU_M2) {
188 if (this->input_grab == SDL_GRAB_OFF) {
189 if ( (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
190 SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
196 if ((resultat & MU_TIMER) || quit)
201 graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
202 do_mouse(this, mousex, mousey, mouseb, kstate);
204 /* Now generate keyboard events */
205 for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
207 if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
208 SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, gem_currentascii[i], &keysym));
210 /* Key unpressed ? */
211 if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
212 SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, gem_currentascii[i], &keysym));
215 memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
217 /* Refresh window name ? */
218 if (GEM_refresh_name) {
219 if ( SDL_GetAppState() & SDL_APPACTIVE ) {
220 /* Fullscreen/windowed */
221 if (GEM_title_name) {
222 wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
227 wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
230 GEM_refresh_name = SDL_FALSE;
234 static int do_messages(_THIS, short *message)
240 switch (message[0]) {
243 posted = SDL_PrivateQuit();
247 wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
250 wind_set(message[3],WF_TOP,message[4],0,0,0);
251 SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
252 if (VDI_setpalette) {
253 VDI_setpalette(this, VDI_curpalette);
257 if (!GEM_lock_redraw) {
258 GEM_wind_redraw(this, message[3],&message[4]);
263 wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
264 /* If we're active, make ourselves inactive */
265 if ( SDL_GetAppState() & SDL_APPACTIVE ) {
266 /* Send an internal deactivate event */
267 SDL_PrivateAppActive(0, SDL_APPACTIVE);
269 /* Update window title */
270 if (GEM_refresh_name && GEM_icon_name) {
271 wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
272 GEM_refresh_name = SDL_FALSE;
276 wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
277 /* If we're not active, make ourselves active */
278 if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
279 /* Send an internal activate event */
280 SDL_PrivateAppActive(1, SDL_APPACTIVE);
282 if (GEM_refresh_name && GEM_title_name) {
283 wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
284 GEM_refresh_name = SDL_FALSE;
288 wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
289 wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
290 GEM_win_fulled = SDL_FALSE; /* Cancel maximized flag */
291 GEM_lock_redraw = SDL_TRUE; /* Prevent redraw till buffers resized */
292 SDL_PrivateResize(w2, h2);
298 if (GEM_win_fulled) {
299 wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
300 GEM_win_fulled = SDL_FALSE;
306 GEM_win_fulled = SDL_TRUE;
308 wind_set (message[3], WF_CURRXYWH, x, y, w, h);
309 wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
310 GEM_lock_redraw = SDL_TRUE; /* Prevent redraw till buffers resized */
311 SDL_PrivateResize(w2, h2);
316 SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
317 if (VDI_setpalette) {
318 VDI_setpalette(this, VDI_oldpalette);
326 static void do_keyboard(short kc, short ks)
328 int scancode, asciicode;
331 scancode=(kc>>8) & 127;
334 gem_currentkeyboard[scancode]=0xFF;
335 gem_currentascii[scancode]=asciicode;
338 /* Read special keys */
340 gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
342 gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
344 gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
346 gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
349 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
351 static short prevmousex=0, prevmousey=0, prevmouseb=0;
352 short x2, y2, w2, h2;
354 /* Don't return mouse events if out of window */
355 if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
359 /* Retrieve window coords, and generate mouse events accordingly */
363 if ((!GEM_fullscreen) && (GEM_handle>=0)) {
364 wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
366 /* Do not generate mouse button event if out of window working area */
367 if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
373 if ((prevmousex!=mx) || (prevmousey!=my)) {
374 if (GEM_mouse_relative) {
375 SDL_PrivateMouseMotion(0, 1, SDL_AtariXbios_mousex, SDL_AtariXbios_mousey);
376 SDL_AtariXbios_mousex = SDL_AtariXbios_mousey = 0;
380 /* Give mouse position relative to window position */
382 if (posx<0) posx = 0;
383 if (posx>w2) posx = w2-1;
385 if (posy<0) posy = 0;
386 if (posy>h2) posy = h2-1;
388 SDL_PrivateMouseMotion(0, 0, posx, posy);
395 if (prevmouseb!=mb) {
399 int curbutton, prevbutton;
401 curbutton = mb & (1<<i);
402 prevbutton = prevmouseb & (1<<i);
404 if (curbutton && !prevbutton) {
405 SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
407 if (!curbutton && prevbutton) {
408 SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
414 /* Read special keys */
416 gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
418 gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
420 gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
422 gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;