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