src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 12 Oct 2017 22:49:00 +0200
branchSDL-1.2
changeset 11608 850f1a16dc81
parent 11576 b3255371e439
permissions -rw-r--r--
atari:gem: Simplify palette changes by removing most temp variables
     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, update_work_area, sdl_resize;
   191 
   192 	quit = update_work_area = sdl_resize = 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 			update_work_area = 1;
   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 			VDI_setpalette(this, VDI_curpalette);
   212 			break;
   213 		case WM_REDRAW:
   214 			if (!GEM_lock_redraw) {
   215 				GEM_wind_redraw(this, message[3], &message[4], SDL_FALSE);
   216 			}
   217 			break;
   218 		case WM_ICONIFY:
   219 		case WM_ALLICONIFY:
   220 			wind_set (message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   221 			/* If we're active, make ourselves inactive */
   222 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   223 				/* Send an internal deactivate event */
   224 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   225 			}
   226 			/* Update window title */
   227 			if (GEM_refresh_name && GEM_icon_name) {
   228 				wind_set(GEM_handle,WF_NAME,
   229 					(short)(((unsigned long)GEM_icon_name)>>16),
   230 					(short)(((unsigned long)GEM_icon_name) & 0xffff),
   231 					0,0);
   232 				GEM_refresh_name = SDL_FALSE;
   233 			}
   234 			GEM_iconified = SDL_TRUE;
   235 			update_work_area = 1;
   236 			break;
   237 		case WM_UNICONIFY:
   238 			wind_set (message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   239 			/* If we're not active, make ourselves active */
   240 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   241 				/* Send an internal activate event */
   242 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   243 			}
   244 			if (GEM_refresh_name && GEM_title_name) {
   245 				wind_set(GEM_handle,WF_NAME,
   246 					(short)(((unsigned long)GEM_title_name)>>16),
   247 					(short)(((unsigned long)GEM_title_name) & 0xffff),
   248 					0,0);
   249 				GEM_refresh_name = SDL_FALSE;
   250 			}
   251 			GEM_iconified = SDL_FALSE;
   252 			update_work_area = 1;
   253 			break;
   254 		case WM_SIZED:
   255 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   256 			update_work_area = sdl_resize = 1;
   257 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   258 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   259 			break;
   260 		case WM_FULLED:
   261 			{
   262 				short x,y,w,h;
   263 
   264 				if (GEM_win_fulled) {
   265 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   266 					GEM_win_fulled = SDL_FALSE;
   267 				} else {
   268 					x = GEM_desk_x;
   269 					y = GEM_desk_y;
   270 					w = GEM_desk_w;
   271 					h = GEM_desk_h;
   272 					GEM_win_fulled = SDL_TRUE;
   273 				}
   274 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   275 				update_work_area = sdl_resize = 1;
   276 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   277 			}
   278 			break;
   279 		case WM_BOTTOMED:
   280 			wind_set(message[3],WF_BOTTOM,0,0,0,0);
   281 			/* Continue with BOTTOM event processing */
   282 		case WM_UNTOPPED:
   283 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   284 			VDI_setpalette(this, VDI_oldpalette);
   285 			break;
   286 	}
   287 
   288 	if (update_work_area) {
   289 		GEM_align_work_area(this, message[3], SDL_TRUE);
   290 
   291 		if (sdl_resize) {
   292 			SDL_PrivateResize(GEM_work_w, GEM_work_h);
   293 		}
   294 	}
   295 
   296 	return quit;
   297 }
   298 
   299 static void do_keyboard(short kc, Uint32 tick)
   300 {
   301 	int scancode;
   302 
   303 	if (kc) {
   304 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   305 		gem_currentkeyboard[scancode]=0xFF;
   306 		keyboard_ticks[scancode]=tick;
   307 	}
   308 }
   309 
   310 static void do_keyboard_special(short ks, Uint32 tick)
   311 {
   312 	int scancode=0;
   313 
   314 	/* Read special keys */
   315 	if (ks & K_RSHIFT)
   316 		scancode=SCANCODE_RIGHTSHIFT;
   317 	if (ks & K_LSHIFT)
   318 		scancode=SCANCODE_LEFTSHIFT;
   319 	if (ks & K_CTRL)
   320 		scancode=SCANCODE_LEFTCONTROL;
   321 	if (ks & K_ALT)
   322 		scancode=SCANCODE_LEFTALT;
   323 
   324 	if (scancode) {
   325 		gem_currentkeyboard[scancode]=0xFF;
   326 		keyboard_ticks[scancode]=tick;
   327 	}
   328 }
   329 
   330 static void do_mouse_motion(_THIS, short mx, short my)
   331 {
   332 	short x2, y2, w2, h2;
   333 
   334 	if (this->input_grab == SDL_GRAB_OFF) {
   335 		/* Switch mouse focus state */
   336 		if (!GEM_fullscreen && (GEM_handle>=0)) {
   337 			SDL_PrivateAppActive(
   338 				mouse_in_work_area(GEM_handle, mx,my),
   339 				SDL_APPMOUSEFOCUS);
   340 		}
   341 	}
   342 	GEM_CheckMouseMode(this);
   343 
   344 	/* Don't return mouse events if out of window */
   345 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   346 		return;
   347 	}
   348 
   349 	/* Relative mouse motion ? */
   350 	if (GEM_mouse_relative) {
   351 		if (GEM_usedevmouse) {
   352 			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   353 		} else {
   354 			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   355 		}
   356 		return;
   357 	}
   358 
   359 	/* Retrieve window coords, and generate mouse events accordingly */
   360 	x2 = y2 = 0;
   361 	w2 = VDI_w;
   362 	h2 = VDI_h;
   363 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   364 		x2 = GEM_work_x;
   365 		y2 = GEM_work_y;
   366 		w2 = GEM_work_w;
   367 		h2 = GEM_work_h;
   368 	}
   369 
   370 	if ((prevmx!=mx) || (prevmy!=my)) {
   371 		int posx, posy;
   372 
   373 		/* Give mouse position relative to window position */
   374 		posx = mx - x2;
   375 		if (posx<0) posx = 0;
   376 		if (posx>w2) posx = w2-1;
   377 		posy = my - y2;
   378 		if (posy<0) posy = 0;
   379 		if (posy>h2) posy = h2-1;
   380 
   381 		SDL_PrivateMouseMotion(0, 0, posx, posy);
   382 	}
   383 
   384 	prevmx = mx;
   385 	prevmy = my;
   386 }
   387 
   388 static void do_mouse_buttons(_THIS, short mb)
   389 {
   390 	int i;
   391 
   392 	/* Don't return mouse events if out of window */
   393 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0)
   394 		return;
   395 
   396 	if (prevmb==mb)
   397 		return;
   398 
   399 	for (i=0;i<3;i++) {
   400 		int curbutton, prevbutton;
   401 
   402 		curbutton = mb & (1<<i);
   403 		prevbutton = prevmb & (1<<i);
   404 
   405 		if (curbutton && !prevbutton) {
   406 			SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   407 		}
   408 		if (!curbutton && prevbutton) {
   409 			SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   410 		}
   411 	}
   412 
   413 	prevmb = mb;
   414 }
   415 
   416 /* Check if mouse in visible area of the window */
   417 static int mouse_in_work_area(int winhandle, short mx, short my)
   418 {
   419 	short todo[4];
   420 	short inside[4] = {mx,my,1,1};
   421 
   422 	/* Browse the rectangle list */
   423 	if (wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])!=0) {
   424 		while (todo[2] && todo[3]) {
   425 			if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
   426 				return 1;
   427 			}
   428 
   429 			if (wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])==0) {
   430 				break;
   431 			}
   432 		}
   433 
   434 	}
   435 
   436 	return 0;
   437 }
   438 
   439 /* Clear key state for which we did not receive events for a while */
   440 
   441 static void clearKeyboardState(Uint32 tick)
   442 {
   443 	int i;
   444 
   445 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   446 		if (keyboard_ticks[i]) {
   447 			if (tick-keyboard_ticks[i] > KEY_PRESS_DURATION) {
   448 				gem_currentkeyboard[i]=0;
   449 				keyboard_ticks[i]=0;
   450 			}
   451 		}
   452 	}
   453 }