src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 07 Jul 2005 15:29:38 +0000
changeset 1091 67b7f0f410a2
parent 1090 37fc855580af
child 1092 1f37386ef508
permissions -rw-r--r--
Correctly process top/bottom event messages
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     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.
     9 
    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.
    14 
    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
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /*
    29  * GEM SDL video driver implementation
    30  * inspired from the Dummy SDL driver
    31  * 
    32  * Patrice Mandin
    33  * and work from
    34  * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
    35  */
    36 
    37 #include <string.h>
    38 
    39 #include <gem.h>
    40 
    41 #include "SDL.h"
    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"
    48 
    49 /* Defines */
    50 
    51 #define ATARIBIOS_MAXKEYS 128
    52 
    53 /* Variables */
    54 
    55 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    56 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    57 static unsigned char gem_currentascii[ATARIBIOS_MAXKEYS];
    58 
    59 /* The translation tables from a console scancode to a SDL keysym */
    60 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    61 
    62 /* Functions prototypes */
    63 
    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);
    68 
    69 /* Functions */
    70 
    71 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym)
    72 {
    73 	/* Set the keysym information */
    74 	keysym->scancode = scancode;
    75 
    76 	if (asciicode)
    77 		keysym->sym = asciicode;		
    78 	else
    79 		keysym->sym = keymap[scancode];
    80 
    81 	keysym->mod = KMOD_NONE;
    82 	keysym->unicode = 0;
    83 
    84 	return(keysym);
    85 }
    86 
    87 void GEM_InitOSKeymap(_THIS)
    88 {
    89 	int i;
    90 
    91 	memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    92 	memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    93 	memset(gem_currentascii, 0, sizeof(gem_currentascii));
    94 
    95 	/* Initialize keymap */
    96 	for ( i=0; i<sizeof(keymap); i++ )
    97 		keymap[i] = SDLK_UNKNOWN;
    98 
    99 	/* Functions keys */
   100 	for ( i = 0; i<10; i++ )
   101 		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
   102 
   103 	/* Cursor keypad */
   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;
   112 
   113 	/* Special keys */
   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;
   124 
   125 	/* Mouse init */
   126 	GEM_mouse_relative = SDL_FALSE;
   127 }
   128 
   129 void GEM_PumpEvents(_THIS)
   130 {
   131 	short mousex, mousey, mouseb, dummy;
   132 	short kstate, prevkc, prevks;
   133 	int i;
   134 	SDL_keysym	keysym;
   135 
   136 	memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
   137 	prevkc = prevks = 0;
   138 	
   139 	for (;;)
   140 	{
   141 		int quit, resultat, event_mask;
   142 		short buffer[8], kc;
   143 		short x2,y2,w2,h2;
   144 
   145 		quit =
   146 			mouse_event =
   147 			x2=y2=w2=h2 = 0;
   148 
   149 		event_mask = MU_MESAG|MU_TIMER|MU_KEYBD;
   150 		if (!GEM_fullscreen && (GEM_handle>=0)) {
   151 			wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   152 			event_mask |= MU_M1;
   153 			if ( (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
   154 				mouse_event = MO_LEAVE;				
   155 			} else {
   156 				mouse_event = MO_ENTER;				
   157 			}
   158 		}
   159 
   160 		resultat = evnt_multi(
   161 			event_mask,
   162 			0,0,0,
   163 			mouse_event,x2,y2,w2,h2,
   164 			0,0,0,0,0,
   165 			buffer,
   166 			10,
   167 			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
   168 		);
   169 
   170 		/* Message event ? */
   171 		if (resultat & MU_MESAG)
   172 			quit = do_messages(this, buffer);
   173 
   174 		/* Keyboard event ? */
   175 		if (resultat & MU_KEYBD) {
   176 			if ((prevkc != kc) || (prevks != kstate)) {
   177 				do_keyboard(kc,kstate);
   178 			} else {
   179 				/* Avoid looping, if repeating same key */
   180 				break;
   181 			}
   182 		}
   183 
   184 		/* Mouse entering/leaving window */
   185 		if (resultat & MU_M1) {
   186 			if (this->input_grab == SDL_GRAB_OFF) {
   187 				if (SDL_GetAppState() & SDL_APPMOUSEFOCUS) {
   188 					SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
   189 				} else {
   190 					SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   191 				}
   192 			}
   193 		}
   194 
   195 		/* Timer event ? */
   196 		if ((resultat & MU_TIMER) || quit)
   197 			break;
   198 	}
   199 
   200 	/* Update mouse */
   201 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   202 	do_mouse(this, mousex, mousey, mouseb, kstate);
   203 
   204 	/* Now generate keyboard events */
   205 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   206 		/* Key pressed ? */
   207 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   208 			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, gem_currentascii[i], &keysym));
   209 			
   210 		/* Key unpressed ? */
   211 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   212 			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, gem_currentascii[i], &keysym));
   213 	}
   214 
   215 	memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   216 
   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);
   223 			}
   224 		} else {
   225 			/* Iconified */
   226 			if (GEM_icon_name) {
   227 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   228 			}
   229 		}
   230 		GEM_refresh_name = SDL_FALSE;
   231 	}
   232 }
   233 
   234 static int do_messages(_THIS, short *message)
   235 {
   236 	int quit, posted;
   237 	short x2,y2,w2,h2;
   238 
   239 	quit=0;
   240 	switch (message[0]) {
   241 		case WM_CLOSED:
   242 		case AP_TERM:    
   243 			posted = SDL_PrivateQuit();
   244 			quit=1;
   245 			break;
   246 		case WM_MOVED:
   247 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   248 			break;
   249 		case WM_TOPPED:
   250 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   251 			/* Continue with TOP event processing */
   252 		case WM_ONTOP:
   253 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   254 			if (VDI_setpalette) {
   255 				VDI_setpalette(this, VDI_curpalette);
   256 			}
   257 			break;
   258 		case WM_REDRAW:
   259 			if (!GEM_lock_redraw) {
   260 				GEM_wind_redraw(this, message[3],&message[4]);
   261 			}
   262 			break;
   263 		case WM_ICONIFY:
   264 		case WM_ALLICONIFY:
   265 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   266 			/* If we're active, make ourselves inactive */
   267 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   268 				/* Send an internal deactivate event */
   269 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   270 			}
   271 			/* Update window title */
   272 			if (GEM_refresh_name && GEM_icon_name) {
   273 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   274 				GEM_refresh_name = SDL_FALSE;
   275 			}
   276 			break;
   277 		case WM_UNICONIFY:
   278 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   279 			/* If we're not active, make ourselves active */
   280 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   281 				/* Send an internal activate event */
   282 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   283 			}
   284 			if (GEM_refresh_name && GEM_title_name) {
   285 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   286 				GEM_refresh_name = SDL_FALSE;
   287 			}
   288 			break;
   289 		case WM_SIZED:
   290 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   291 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   292 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   293 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   294 			SDL_PrivateResize(w2, h2);
   295 			break;
   296 		case WM_FULLED:
   297 			{
   298 				short x,y,w,h;
   299 
   300 				if (GEM_win_fulled) {
   301 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   302 					GEM_win_fulled = SDL_FALSE;
   303 				} else {
   304 					x = GEM_desk_x;
   305 					y = GEM_desk_y;
   306 					w = GEM_desk_w;
   307 					h = GEM_desk_h;
   308 					GEM_win_fulled = SDL_TRUE;
   309 				}
   310 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   311 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   312 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   313 				SDL_PrivateResize(w2, h2);
   314 			}
   315 			break;
   316 		case WM_BOTTOMED:
   317 			wind_set(message[3],WF_BOTTOM,0,0,0,0);
   318 			/* Continue with BOTTOM event processing */
   319 		case WM_UNTOPPED:
   320 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   321 			if (VDI_setpalette) {
   322 				VDI_setpalette(this, VDI_oldpalette);
   323 			}
   324 			break;
   325 	}
   326 	
   327 	return quit;
   328 }
   329 
   330 static void do_keyboard(short kc, short ks)
   331 {
   332 	int			scancode, asciicode;
   333 
   334 	if (kc) {
   335 		scancode=(kc>>8) & 127;
   336 		asciicode=kc & 255;
   337 
   338 		gem_currentkeyboard[scancode]=0xFF;
   339 		gem_currentascii[scancode]=asciicode;
   340 	}
   341 
   342 	/* Read special keys */
   343 	if (ks & K_RSHIFT)
   344 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   345 	if (ks & K_LSHIFT)
   346 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   347 	if (ks & K_CTRL)
   348 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   349 	if (ks & K_ALT)
   350 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   351 }
   352 
   353 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
   354 {
   355 	static short prevmousex=0, prevmousey=0, prevmouseb=0;
   356 	short x2, y2, w2, h2;
   357 
   358 	/* Don't return mouse events if out of window */
   359 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   360 		return;
   361 	}
   362 
   363 	/* Retrieve window coords, and generate mouse events accordingly */
   364 	x2 = y2 = 0;
   365 	w2 = VDI_w;
   366 	h2 = VDI_h;
   367 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   368 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   369 
   370 		/* Do not generate mouse button event if out of window working area */
   371 		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
   372 			mb=prevmouseb;
   373 		}
   374 	}
   375 
   376 	/* Mouse motion ? */
   377 	if ((prevmousex!=mx) || (prevmousey!=my)) {
   378 		if (GEM_mouse_relative) {
   379 			SDL_PrivateMouseMotion(0, 1, SDL_AtariXbios_mousex, SDL_AtariXbios_mousey);
   380 			SDL_AtariXbios_mousex = SDL_AtariXbios_mousey = 0;
   381 		} else {
   382 			int posx, posy;
   383 
   384 			/* Give mouse position relative to window position */
   385 			posx = mx - x2;
   386 			if (posx<0) posx = 0;
   387 			if (posx>w2) posx = w2-1;
   388 			posy = my - y2;
   389 			if (posy<0) posy = 0;
   390 			if (posy>h2) posy = h2-1;
   391 
   392 			SDL_PrivateMouseMotion(0, 0, posx, posy);
   393 		}
   394 		prevmousex = mx;
   395 		prevmousey = my;
   396 	}
   397 
   398 	/* Mouse button ? */
   399 	if (prevmouseb!=mb) {
   400 		int i;
   401 
   402 		for (i=0;i<2;i++) {
   403 			int curbutton, prevbutton;
   404 		
   405 			curbutton = mb & (1<<i);
   406 			prevbutton = prevmouseb & (1<<i);
   407 		
   408 			if (curbutton && !prevbutton) {
   409 				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   410 			}
   411 			if (!curbutton && prevbutton) {
   412 				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   413 			}
   414 		}
   415 		prevmouseb = mb;
   416 	}
   417 
   418 	/* Read special keys */
   419 	if (ks & K_RSHIFT)
   420 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   421 	if (ks & K_LSHIFT)
   422 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   423 	if (ks & K_CTRL)
   424 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   425 	if (ks & K_ALT)
   426 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   427 }