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