src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Sun, 04 Nov 2012 19:00:20 +0100
branchSDL-1.2
changeset 6647 42abe340dc14
parent 6585 bc23c3b164fa
child 6648 5415ef59343a
permissions -rw-r--r--
gem: move some variables, and remove duplicates
     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 			if (this->input_grab == SDL_GRAB_OFF) {
   121 				/* Switch mouse focus state */
   122 				if (!GEM_fullscreen && (GEM_handle>=0)) {
   123 					SDL_PrivateAppActive(
   124 						mouse_in_work_area(GEM_handle, mousex,mousey),
   125 						SDL_APPMOUSEFOCUS);
   126 				}
   127 			}
   128 			GEM_CheckMouseMode(this);
   129 
   130 			do_mouse_motion(this, mousex, mousey);
   131 			prevmx = mousex;
   132 			prevmy = mousey;
   133 		}
   134 
   135 		/* Mouse button event ? */
   136 		if (resultat & MU_BUTTON) {
   137 			do_mouse_buttons(this, mouseb);
   138 			prevmb = mouseb & 7;
   139 		}
   140 
   141 		/* Timer event ? */
   142 		if ((resultat & MU_TIMER) || quit)
   143 			break;
   144 	}
   145 
   146 	/* Now generate keyboard events */
   147 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   148 		/* Key pressed ? */
   149 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   150 			SDL_PrivateKeyboard(SDL_PRESSED,
   151 				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
   152 			
   153 		/* Key unpressed ? */
   154 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   155 			SDL_PrivateKeyboard(SDL_RELEASED,
   156 				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
   157 	}
   158 
   159 	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   160 
   161 	/* Refresh window name ? */
   162 	if (GEM_refresh_name) {
   163 		const char *window_name =
   164 			(SDL_GetAppState() & SDL_APPACTIVE)
   165 			? GEM_title_name : GEM_icon_name;
   166 		if (window_name) {
   167 			wind_set(GEM_handle,WF_NAME,
   168 				(short)(((unsigned long)window_name)>>16),
   169 				(short)(((unsigned long)window_name) & 0xffff),
   170 				0,0);
   171 		}
   172 		GEM_refresh_name = SDL_FALSE;
   173 	}
   174 }
   175 
   176 static int do_messages(_THIS, short *message)
   177 {
   178 	int quit, check_mouse_mode;
   179 	short x2,y2,w2,h2;
   180 
   181 	quit = check_mouse_mode = 0;
   182 	switch (message[0]) {
   183 		case WM_CLOSED:
   184 		case AP_TERM:    
   185 			SDL_PrivateQuit();
   186 			quit=1;
   187 			break;
   188 		case WM_MOVED:
   189 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   190 			break;
   191 		case WM_TOPPED:
   192 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   193 			/* Continue with TOP event processing */
   194 		case WM_ONTOP:
   195 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   196 			if (VDI_setpalette) {
   197 				VDI_setpalette(this, VDI_curpalette);
   198 			}
   199 			check_mouse_mode = 1;
   200 			break;
   201 		case WM_REDRAW:
   202 			if (!GEM_lock_redraw) {
   203 				GEM_wind_redraw(this, message[3],&message[4]);
   204 			}
   205 			break;
   206 		case WM_ICONIFY:
   207 		case WM_ALLICONIFY:
   208 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   209 			/* If we're active, make ourselves inactive */
   210 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   211 				/* Send an internal deactivate event */
   212 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   213 			}
   214 			/* Update window title */
   215 			if (GEM_refresh_name && GEM_icon_name) {
   216 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   217 				GEM_refresh_name = SDL_FALSE;
   218 			}
   219 			check_mouse_mode = 1;
   220 			break;
   221 		case WM_UNICONIFY:
   222 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   223 			/* If we're not active, make ourselves active */
   224 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   225 				/* Send an internal activate event */
   226 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   227 			}
   228 			if (GEM_refresh_name && GEM_title_name) {
   229 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   230 				GEM_refresh_name = SDL_FALSE;
   231 			}
   232 			check_mouse_mode = 1;
   233 			break;
   234 		case WM_SIZED:
   235 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   236 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   237 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   238 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   239 			SDL_PrivateResize(w2, h2);
   240 			break;
   241 		case WM_FULLED:
   242 			{
   243 				short x,y,w,h;
   244 
   245 				if (GEM_win_fulled) {
   246 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   247 					GEM_win_fulled = SDL_FALSE;
   248 				} else {
   249 					x = GEM_desk_x;
   250 					y = GEM_desk_y;
   251 					w = GEM_desk_w;
   252 					h = GEM_desk_h;
   253 					GEM_win_fulled = SDL_TRUE;
   254 				}
   255 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   256 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   257 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   258 				SDL_PrivateResize(w2, h2);
   259 			}
   260 			break;
   261 		case WM_BOTTOMED:
   262 			wind_set(message[3],WF_BOTTOM,0,0,0,0);
   263 			/* Continue with BOTTOM event processing */
   264 		case WM_UNTOPPED:
   265 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   266 			if (VDI_setpalette) {
   267 				VDI_setpalette(this, VDI_oldpalette);
   268 			}
   269 			check_mouse_mode = 1;
   270 			break;
   271 	}
   272 
   273 	if (check_mouse_mode) {
   274 		GEM_CheckMouseMode(this);
   275 	}
   276 	
   277 	return quit;
   278 }
   279 
   280 static void do_keyboard(short kc)
   281 {
   282 	int scancode;
   283 
   284 	if (kc) {
   285 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   286 		gem_currentkeyboard[scancode]=0xFF;
   287 	}
   288 }
   289 
   290 static void do_keyboard_special(short ks)
   291 {
   292 	/* Read special keys */
   293 	if (ks & K_RSHIFT)
   294 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   295 	if (ks & K_LSHIFT)
   296 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   297 	if (ks & K_CTRL)
   298 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   299 	if (ks & K_ALT)
   300 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   301 }
   302 
   303 static void do_mouse_motion(_THIS, short mx, short my)
   304 {
   305 	short x2, y2, w2, h2;
   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 }