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