src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Fri, 03 Jun 2005 08:18:55 +0000
changeset 1067 f73b199bcd75
parent 1065 5d9947da7510
child 1073 6d3c1134cd19
permissions -rw-r--r--
Better mouse lose/gain focus
     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;
   142 		short buffer[8], kc;
   143 		short x2,y2,w2,h2;
   144 
   145 		quit = 0;
   146 
   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;
   151 		} else {
   152 			x2=y2=w2=h2=0;
   153 		}
   154 
   155 		resultat = evnt_multi(
   156 			event_mask,
   157 			0,0,0,
   158 			MO_ENTER,x2,y2,w2,h2,
   159 			MO_LEAVE,x2,y2,w2,h2,
   160 			buffer,
   161 			10,
   162 			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
   163 		);
   164 
   165 		/* Message event ? */
   166 		if (resultat & MU_MESAG)
   167 			quit = do_messages(this, buffer);
   168 
   169 		/* Keyboard event ? */
   170 		if (resultat & MU_KEYBD) {
   171 			if ((prevkc != kc) || (prevks != kstate)) {
   172 				do_keyboard(kc,kstate);
   173 			} else {
   174 				/* Avoid looping, if repeating same key */
   175 				break;
   176 			}
   177 		}
   178 
   179 		/* Mouse entering/leaving window */
   180 		if (resultat & MU_M1) {
   181 			if (this->input_grab == SDL_GRAB_OFF) {
   182 				SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   183 			}
   184 		}
   185 		if (resultat & MU_M2) {
   186 			if (this->input_grab == SDL_GRAB_OFF) {
   187 				SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
   188 			}
   189 		}
   190 
   191 		/* Timer event ? */
   192 		if ((resultat & MU_TIMER) || quit)
   193 			break;
   194 	}
   195 
   196 	/* Update mouse */
   197 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   198 	do_mouse(this, mousex, mousey, mouseb, kstate);
   199 
   200 	/* Now generate keyboard events */
   201 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   202 		/* Key pressed ? */
   203 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   204 			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, gem_currentascii[i], &keysym));
   205 			
   206 		/* Key unpressed ? */
   207 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   208 			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, gem_currentascii[i], &keysym));
   209 	}
   210 
   211 	memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   212 
   213 	/* Refresh window name ? */
   214 	if (GEM_refresh_name) {
   215 		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   216 			/* Fullscreen/windowed */
   217 			if (GEM_title_name) {
   218 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   219 			}
   220 		} else {
   221 			/* Iconified */
   222 			if (GEM_icon_name) {
   223 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   224 			}
   225 		}
   226 		GEM_refresh_name = SDL_FALSE;
   227 	}
   228 }
   229 
   230 static int do_messages(_THIS, short *message)
   231 {
   232 	int quit, posted;
   233 	short x2,y2,w2,h2;
   234 
   235 	quit=0;
   236 	switch (message[0]) {
   237 		case WM_CLOSED:
   238 		case AP_TERM:    
   239 			posted = SDL_PrivateQuit();
   240 			quit=1;
   241 			break;
   242 		case WM_MOVED:
   243 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   244 			break;
   245 		case WM_TOPPED:
   246 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   247 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   248 			break;
   249 		case WM_REDRAW:
   250 			if (!GEM_lock_redraw) {
   251 				GEM_wind_redraw(this, message[3],&message[4]);
   252 			}
   253 			break;
   254 		case WM_ICONIFY:
   255 		case WM_ALLICONIFY:
   256 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   257 			/* If we're active, make ourselves inactive */
   258 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   259 				/* Send an internal deactivate event */
   260 				SDL_PrivateAppActive(0, SDL_APPACTIVE|SDL_APPINPUTFOCUS);
   261 			}
   262 			/* Update window title */
   263 			if (GEM_refresh_name && GEM_icon_name) {
   264 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   265 				GEM_refresh_name = SDL_FALSE;
   266 			}
   267 			break;
   268 		case WM_UNICONIFY:
   269 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   270 			/* If we're not active, make ourselves active */
   271 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   272 				/* Send an internal activate event */
   273 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   274 			}
   275 			if (GEM_refresh_name && GEM_title_name) {
   276 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   277 				GEM_refresh_name = SDL_FALSE;
   278 			}
   279 			break;
   280 		case WM_SIZED:
   281 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   282 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   283 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   284 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   285 			SDL_PrivateResize(w2, h2);
   286 			break;
   287 		case WM_FULLED:
   288 			{
   289 				short x,y,w,h;
   290 
   291 				if (GEM_win_fulled) {
   292 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   293 					GEM_win_fulled = SDL_FALSE;
   294 				} else {
   295 					x = GEM_desk_x;
   296 					y = GEM_desk_y;
   297 					w = GEM_desk_w;
   298 					h = GEM_desk_h;
   299 					GEM_win_fulled = SDL_TRUE;
   300 				}
   301 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   302 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   303 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   304 				SDL_PrivateResize(w2, h2);
   305 			}
   306 			break;
   307 		case WM_BOTTOMED:
   308 		case WM_UNTOPPED:
   309 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   310 			break;
   311 	}
   312 	
   313 	return quit;
   314 }
   315 
   316 static void do_keyboard(short kc, short ks)
   317 {
   318 	int			scancode, asciicode;
   319 
   320 	if (kc) {
   321 		scancode=(kc>>8) & 127;
   322 		asciicode=kc & 255;
   323 
   324 		gem_currentkeyboard[scancode]=0xFF;
   325 		gem_currentascii[scancode]=asciicode;
   326 	}
   327 
   328 	/* Read special keys */
   329 	if (ks & K_RSHIFT)
   330 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   331 	if (ks & K_LSHIFT)
   332 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   333 	if (ks & K_CTRL)
   334 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   335 	if (ks & K_ALT)
   336 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   337 }
   338 
   339 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
   340 {
   341 	static short prevmousex=0, prevmousey=0, prevmouseb=0;
   342 	short x2, y2, w2, h2;
   343 
   344 	/* Don't return mouse events if out of window */
   345 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   346 		return;
   347 	}
   348 
   349 	/* Retrieve window coords, and generate mouse events accordingly */
   350 	x2 = y2 = 0;
   351 	w2 = VDI_w;
   352 	h2 = VDI_h;
   353 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   354 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   355 
   356 		/* Do not generate mouse button event if out of window working area */
   357 		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
   358 			mb=prevmouseb;
   359 		}
   360 	}
   361 
   362 	/* Mouse motion ? */
   363 	if ((prevmousex!=mx) || (prevmousey!=my)) {
   364 		if (GEM_mouse_relative) {
   365 			SDL_PrivateMouseMotion(0, 1, SDL_AtariXbios_mousex, SDL_AtariXbios_mousey);
   366 			SDL_AtariXbios_mousex = SDL_AtariXbios_mousey = 0;
   367 		} else {
   368 			int posx, posy;
   369 
   370 			/* Give mouse position relative to window position */
   371 			posx = mx - x2;
   372 			if (posx<0) posx = 0;
   373 			if (posx>w2) posx = w2-1;
   374 			posy = my - y2;
   375 			if (posy<0) posy = 0;
   376 			if (posy>h2) posy = h2-1;
   377 
   378 			SDL_PrivateMouseMotion(0, 0, posx, posy);
   379 		}
   380 		prevmousex = mx;
   381 		prevmousey = my;
   382 	}
   383 
   384 	/* Mouse button ? */
   385 	if (prevmouseb!=mb) {
   386 		int i;
   387 
   388 		for (i=0;i<2;i++) {
   389 			int curbutton, prevbutton;
   390 		
   391 			curbutton = mb & (1<<i);
   392 			prevbutton = prevmouseb & (1<<i);
   393 		
   394 			if (curbutton && !prevbutton) {
   395 				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   396 			}
   397 			if (!curbutton && prevbutton) {
   398 				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   399 			}
   400 		}
   401 		prevmouseb = mb;
   402 	}
   403 
   404 	/* Read special keys */
   405 	if (ks & K_RSHIFT)
   406 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   407 	if (ks & K_LSHIFT)
   408 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   409 	if (ks & K_CTRL)
   410 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   411 	if (ks & K_ALT)
   412 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   413 }