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