src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Sun, 04 Nov 2012 19:02:21 +0100
branchSDL-1.2
changeset 6648 5415ef59343a
parent 6647 42abe340dc14
child 6649 1aab0b09a0e3
permissions -rw-r--r--
gem: Check mouse focus inside mouse motion function
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2012 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 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     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /*
    25  * GEM SDL video driver implementation
    26  * inspired from the Dummy SDL driver
    27  * 
    28  * Patrice Mandin
    29  * and work from
    30  * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
    31  */
    32 
    33 #include <gem.h>
    34 
    35 #include "../../events/SDL_sysevents.h"
    36 #include "../../events/SDL_events_c.h"
    37 #include "SDL_gemvideo.h"
    38 #include "SDL_gemevents_c.h"
    39 #include "SDL_gemmouse_c.h"
    40 #include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
    41 #include "../ataricommon/SDL_atarievents_c.h"
    42 #include "../ataricommon/SDL_xbiosevents_c.h"
    43 #include "../ataricommon/SDL_ataridevmouse_c.h"
    44 
    45 /* Variables */
    46 
    47 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    48 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    49 
    50 static short prevmx=0,prevmy=0,prevmb=0;
    51 
    52 /* Functions prototypes */
    53 
    54 static int do_messages(_THIS, short *message);
    55 static void do_keyboard(short kc);
    56 static void do_keyboard_special(short ks);
    57 static void do_mouse_motion(_THIS, short mx, short my);
    58 static void do_mouse_buttons(_THIS, short mb);
    59 static int mouse_in_work_area(int winhandle, short mx, short my);
    60 
    61 /* Functions */
    62 
    63 void GEM_InitOSKeymap(_THIS)
    64 {
    65 	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    66 	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    67 
    68 	/* Mouse init */
    69 	GEM_mouse_relative = SDL_FALSE;
    70 
    71 	SDL_Atari_InitInternalKeymap(this);
    72 }
    73 
    74 void GEM_PumpEvents(_THIS)
    75 {
    76 	short prevkc=0, mousex, mousey, mouseb, kstate;
    77 	int i;
    78 	SDL_keysym keysym;
    79 
    80 	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
    81 
    82 	for (;;)
    83 	{
    84 		int quit, resultat;
    85 		short buffer[8], kc, dummy;
    86 
    87 		quit = 0;
    88 
    89 		resultat = evnt_multi(
    90 			MU_MESAG|MU_TIMER|MU_KEYBD|MU_BUTTON|MU_M1,
    91 			0x101,7,prevmb,
    92 			MO_LEAVE,prevmx,prevmy,1,1,
    93 			0,0,0,0,0,
    94 			buffer,
    95 			10,
    96 			&mousex,&mousey,&mouseb,&kstate,&kc,&dummy
    97 		);
    98 
    99 		/* Message event ? */
   100 		if (resultat & MU_MESAG)
   101 			quit = do_messages(this, buffer);
   102 
   103 		/* Special keys ? */
   104 		if (resultat & (MU_KEYBD|MU_BUTTON))
   105 			do_keyboard_special(kstate);
   106 
   107 		/* Keyboard event ? */
   108 		if (resultat & MU_KEYBD) {
   109 			if (prevkc != kc) {
   110 				do_keyboard(kc);
   111 				prevkc = kc;
   112 			} else {
   113 				/* Avoid looping, if repeating same key */
   114 				quit = 1;
   115 			}
   116 		}
   117 
   118 		/* Mouse motion event ? */
   119 		if (resultat & MU_M1) {
   120 			do_mouse_motion(this, mousex, mousey);
   121 			prevmx = mousex;
   122 			prevmy = mousey;
   123 		}
   124 
   125 		/* Mouse button event ? */
   126 		if (resultat & MU_BUTTON) {
   127 			do_mouse_buttons(this, mouseb);
   128 			prevmb = mouseb & 7;
   129 		}
   130 
   131 		/* Timer event ? */
   132 		if ((resultat & MU_TIMER) || quit)
   133 			break;
   134 	}
   135 
   136 	/* Now generate keyboard events */
   137 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   138 		/* Key pressed ? */
   139 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   140 			SDL_PrivateKeyboard(SDL_PRESSED,
   141 				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
   142 			
   143 		/* Key unpressed ? */
   144 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   145 			SDL_PrivateKeyboard(SDL_RELEASED,
   146 				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
   147 	}
   148 
   149 	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   150 
   151 	/* Refresh window name ? */
   152 	if (GEM_refresh_name) {
   153 		const char *window_name =
   154 			(SDL_GetAppState() & SDL_APPACTIVE)
   155 			? GEM_title_name : GEM_icon_name;
   156 		if (window_name) {
   157 			wind_set(GEM_handle,WF_NAME,
   158 				(short)(((unsigned long)window_name)>>16),
   159 				(short)(((unsigned long)window_name) & 0xffff),
   160 				0,0);
   161 		}
   162 		GEM_refresh_name = SDL_FALSE;
   163 	}
   164 }
   165 
   166 static int do_messages(_THIS, short *message)
   167 {
   168 	int quit, check_mouse_mode;
   169 	short x2,y2,w2,h2;
   170 
   171 	quit = check_mouse_mode = 0;
   172 	switch (message[0]) {
   173 		case WM_CLOSED:
   174 		case AP_TERM:    
   175 			SDL_PrivateQuit();
   176 			quit=1;
   177 			break;
   178 		case WM_MOVED:
   179 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   180 			break;
   181 		case WM_TOPPED:
   182 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   183 			/* Continue with TOP event processing */
   184 		case WM_ONTOP:
   185 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   186 			if (VDI_setpalette) {
   187 				VDI_setpalette(this, VDI_curpalette);
   188 			}
   189 			check_mouse_mode = 1;
   190 			break;
   191 		case WM_REDRAW:
   192 			if (!GEM_lock_redraw) {
   193 				GEM_wind_redraw(this, message[3],&message[4]);
   194 			}
   195 			break;
   196 		case WM_ICONIFY:
   197 		case WM_ALLICONIFY:
   198 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   199 			/* If we're active, make ourselves inactive */
   200 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   201 				/* Send an internal deactivate event */
   202 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   203 			}
   204 			/* Update window title */
   205 			if (GEM_refresh_name && GEM_icon_name) {
   206 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   207 				GEM_refresh_name = SDL_FALSE;
   208 			}
   209 			check_mouse_mode = 1;
   210 			break;
   211 		case WM_UNICONIFY:
   212 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   213 			/* If we're not active, make ourselves active */
   214 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   215 				/* Send an internal activate event */
   216 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   217 			}
   218 			if (GEM_refresh_name && GEM_title_name) {
   219 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   220 				GEM_refresh_name = SDL_FALSE;
   221 			}
   222 			check_mouse_mode = 1;
   223 			break;
   224 		case WM_SIZED:
   225 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   226 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   227 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   228 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   229 			SDL_PrivateResize(w2, h2);
   230 			break;
   231 		case WM_FULLED:
   232 			{
   233 				short x,y,w,h;
   234 
   235 				if (GEM_win_fulled) {
   236 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   237 					GEM_win_fulled = SDL_FALSE;
   238 				} else {
   239 					x = GEM_desk_x;
   240 					y = GEM_desk_y;
   241 					w = GEM_desk_w;
   242 					h = GEM_desk_h;
   243 					GEM_win_fulled = SDL_TRUE;
   244 				}
   245 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   246 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   247 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   248 				SDL_PrivateResize(w2, h2);
   249 			}
   250 			break;
   251 		case WM_BOTTOMED:
   252 			wind_set(message[3],WF_BOTTOM,0,0,0,0);
   253 			/* Continue with BOTTOM event processing */
   254 		case WM_UNTOPPED:
   255 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   256 			if (VDI_setpalette) {
   257 				VDI_setpalette(this, VDI_oldpalette);
   258 			}
   259 			check_mouse_mode = 1;
   260 			break;
   261 	}
   262 
   263 	if (check_mouse_mode) {
   264 		GEM_CheckMouseMode(this);
   265 	}
   266 	
   267 	return quit;
   268 }
   269 
   270 static void do_keyboard(short kc)
   271 {
   272 	int scancode;
   273 
   274 	if (kc) {
   275 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   276 		gem_currentkeyboard[scancode]=0xFF;
   277 	}
   278 }
   279 
   280 static void do_keyboard_special(short ks)
   281 {
   282 	/* Read special keys */
   283 	if (ks & K_RSHIFT)
   284 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   285 	if (ks & K_LSHIFT)
   286 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   287 	if (ks & K_CTRL)
   288 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   289 	if (ks & K_ALT)
   290 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   291 }
   292 
   293 static void do_mouse_motion(_THIS, short mx, short my)
   294 {
   295 	short x2, y2, w2, h2;
   296 
   297 	if (this->input_grab == SDL_GRAB_OFF) {
   298 		/* Switch mouse focus state */
   299 		if (!GEM_fullscreen && (GEM_handle>=0)) {
   300 			SDL_PrivateAppActive(
   301 				mouse_in_work_area(GEM_handle, mx,my),
   302 				SDL_APPMOUSEFOCUS);
   303 		}
   304 	}
   305 	GEM_CheckMouseMode(this);
   306 
   307 	/* Don't return mouse events if out of window */
   308 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   309 		return;
   310 	}
   311 
   312 	/* Relative mouse motion ? */
   313 	if (GEM_mouse_relative) {
   314 		if (GEM_usedevmouse) {
   315 			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   316 		} else {
   317 			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   318 		}
   319 		return;
   320 	}
   321 
   322 	/* Retrieve window coords, and generate mouse events accordingly */
   323 	x2 = y2 = 0;
   324 	w2 = VDI_w;
   325 	h2 = VDI_h;
   326 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   327 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   328 	}
   329 
   330 	if ((prevmx!=mx) || (prevmy!=my)) {
   331 		int posx, posy;
   332 
   333 		/* Give mouse position relative to window position */
   334 		posx = mx - x2;
   335 		if (posx<0) posx = 0;
   336 		if (posx>w2) posx = w2-1;
   337 		posy = my - y2;
   338 		if (posy<0) posy = 0;
   339 		if (posy>h2) posy = h2-1;
   340 
   341 		SDL_PrivateMouseMotion(0, 0, posx, posy);
   342 	}
   343 
   344 	prevmx = mx;
   345 	prevmy = my;
   346 }
   347 
   348 static void do_mouse_buttons(_THIS, short mb)
   349 {
   350 	int i;
   351 
   352 	/* Don't return mouse events if out of window */
   353 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0)
   354 		return;
   355 
   356 	if (prevmb==mb)
   357 		return;
   358 
   359 	for (i=0;i<3;i++) {
   360 		int curbutton, prevbutton;
   361 		
   362 		curbutton = mb & (1<<i);
   363 		prevbutton = prevmb & (1<<i);
   364 	
   365 		if (curbutton && !prevbutton) {
   366 			SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   367 		}
   368 		if (!curbutton && prevbutton) {
   369 			SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   370 		}
   371 	}
   372 
   373 	prevmb = mb;
   374 }
   375 
   376 /* Check if mouse in visible area of the window */
   377 static int mouse_in_work_area(int winhandle, short mx, short my)
   378 {
   379 	short todo[4];
   380 	short inside[4] = {mx,my,1,1};
   381 
   382 	/* Browse the rectangle list */
   383 	if (wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])!=0) {
   384 		while (todo[2] && todo[3]) {
   385 			if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
   386 				return 1;
   387 			}
   388 
   389 			if (wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])==0) {
   390 				break;
   391 			}
   392 		}
   393 
   394 	}
   395 
   396 	return 0;
   397 }