src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Sat, 02 Aug 2014 11:16:16 +0200
branchSDL-1.2
changeset 9042 89d54976ff74
parent 9018 4a9c67d11c9f
child 9130 347df449e563
permissions -rw-r--r--
atari:gem: Simulate key released after X ms elapsed since last key pressed.
     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 	/* Update mouse state */
   133 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   134 	do_keyboard_special(kstate, cur_tick);
   135 	do_mouse_motion(this, mousex, mousey);
   136 	do_mouse_buttons(this, mouseb);
   137 
   138 	/* Now generate keyboard events */
   139 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   140 		/* Key pressed ? */
   141 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   142 			SDL_PrivateKeyboard(SDL_PRESSED,
   143 				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
   144 			
   145 		/* Key unpressed ? */
   146 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   147 			SDL_PrivateKeyboard(SDL_RELEASED,
   148 				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
   149 	}
   150 
   151 	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   152 
   153 	/* Refresh window name ? */
   154 	if (GEM_refresh_name) {
   155 		const char *window_name =
   156 			(SDL_GetAppState() & SDL_APPACTIVE)
   157 			? GEM_title_name : GEM_icon_name;
   158 		if (window_name) {
   159 			wind_set(GEM_handle,WF_NAME,
   160 				(short)(((unsigned long)window_name)>>16),
   161 				(short)(((unsigned long)window_name) & 0xffff),
   162 				0,0);
   163 		}
   164 		GEM_refresh_name = SDL_FALSE;
   165 	}
   166 }
   167 
   168 static int do_messages(_THIS, short *message)
   169 {
   170 	int quit, check_mouse_mode;
   171 	short x2,y2,w2,h2;
   172 
   173 	quit = check_mouse_mode = 0;
   174 	switch (message[0]) {
   175 		case WM_CLOSED:
   176 		case AP_TERM:    
   177 			SDL_PrivateQuit();
   178 			quit=1;
   179 			break;
   180 		case WM_MOVED:
   181 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   182 			break;
   183 		case WM_TOPPED:
   184 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   185 			/* Continue with TOP event processing */
   186 		case WM_ONTOP:
   187 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   188 			if (VDI_setpalette) {
   189 				VDI_setpalette(this, VDI_curpalette);
   190 			}
   191 			check_mouse_mode = 1;
   192 			break;
   193 		case WM_REDRAW:
   194 			if (!GEM_lock_redraw) {
   195 				GEM_wind_redraw(this, message[3],&message[4]);
   196 			}
   197 			break;
   198 		case WM_ICONIFY:
   199 		case WM_ALLICONIFY:
   200 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   201 			/* If we're active, make ourselves inactive */
   202 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   203 				/* Send an internal deactivate event */
   204 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   205 			}
   206 			/* Update window title */
   207 			if (GEM_refresh_name && GEM_icon_name) {
   208 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   209 				GEM_refresh_name = SDL_FALSE;
   210 			}
   211 			check_mouse_mode = 1;
   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 			check_mouse_mode = 1;
   225 			break;
   226 		case WM_SIZED:
   227 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   228 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   229 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   230 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   231 			SDL_PrivateResize(w2, h2);
   232 			break;
   233 		case WM_FULLED:
   234 			{
   235 				short x,y,w,h;
   236 
   237 				if (GEM_win_fulled) {
   238 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   239 					GEM_win_fulled = SDL_FALSE;
   240 				} else {
   241 					x = GEM_desk_x;
   242 					y = GEM_desk_y;
   243 					w = GEM_desk_w;
   244 					h = GEM_desk_h;
   245 					GEM_win_fulled = SDL_TRUE;
   246 				}
   247 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   248 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   249 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   250 				SDL_PrivateResize(w2, h2);
   251 			}
   252 			break;
   253 		case WM_BOTTOMED:
   254 			wind_set(message[3],WF_BOTTOM,0,0,0,0);
   255 			/* Continue with BOTTOM event processing */
   256 		case WM_UNTOPPED:
   257 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   258 			if (VDI_setpalette) {
   259 				VDI_setpalette(this, VDI_oldpalette);
   260 			}
   261 			check_mouse_mode = 1;
   262 			break;
   263 	}
   264 
   265 	if (check_mouse_mode) {
   266 		GEM_CheckMouseMode(this);
   267 	}
   268 	
   269 	return quit;
   270 }
   271 
   272 static void do_keyboard(short kc, Uint32 tick)
   273 {
   274 	int scancode;
   275 
   276 	if (kc) {
   277 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   278 		gem_currentkeyboard[scancode]=0xFF;
   279 		keyboard_ticks[scancode]=tick;
   280 	}
   281 }
   282 
   283 static void do_keyboard_special(short ks, Uint32 tick)
   284 {
   285 	int scancode=0;
   286 
   287 	/* Read special keys */
   288 	if (ks & K_RSHIFT)
   289 		scancode=SCANCODE_RIGHTSHIFT;
   290 	if (ks & K_LSHIFT)
   291 		scancode=SCANCODE_LEFTSHIFT;
   292 	if (ks & K_CTRL)
   293 		scancode=SCANCODE_LEFTCONTROL;
   294 	if (ks & K_ALT)
   295 		scancode=SCANCODE_LEFTALT;
   296 
   297 	if (scancode) {
   298 		gem_currentkeyboard[scancode]=0xFF;
   299 		keyboard_ticks[scancode]=tick;
   300 	}
   301 }
   302 
   303 static void do_mouse_motion(_THIS, short mx, short my)
   304 {
   305 	short x2, y2, w2, h2;
   306 
   307 	if (this->input_grab == SDL_GRAB_OFF) {
   308 		/* Switch mouse focus state */
   309 		if (!GEM_fullscreen && (GEM_handle>=0)) {
   310 			SDL_PrivateAppActive(
   311 				mouse_in_work_area(GEM_handle, mx,my),
   312 				SDL_APPMOUSEFOCUS);
   313 		}
   314 	}
   315 	GEM_CheckMouseMode(this);
   316 
   317 	/* Don't return mouse events if out of window */
   318 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   319 		return;
   320 	}
   321 
   322 	/* Relative mouse motion ? */
   323 	if (GEM_mouse_relative) {
   324 		if (GEM_usedevmouse) {
   325 			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   326 		} else {
   327 			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   328 		}
   329 		return;
   330 	}
   331 
   332 	/* Retrieve window coords, and generate mouse events accordingly */
   333 	x2 = y2 = 0;
   334 	w2 = VDI_w;
   335 	h2 = VDI_h;
   336 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   337 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   338 	}
   339 
   340 	if ((prevmx!=mx) || (prevmy!=my)) {
   341 		int posx, posy;
   342 
   343 		/* Give mouse position relative to window position */
   344 		posx = mx - x2;
   345 		if (posx<0) posx = 0;
   346 		if (posx>w2) posx = w2-1;
   347 		posy = my - y2;
   348 		if (posy<0) posy = 0;
   349 		if (posy>h2) posy = h2-1;
   350 
   351 		SDL_PrivateMouseMotion(0, 0, posx, posy);
   352 	}
   353 
   354 	prevmx = mx;
   355 	prevmy = my;
   356 }
   357 
   358 static void do_mouse_buttons(_THIS, short mb)
   359 {
   360 	int i;
   361 
   362 	/* Don't return mouse events if out of window */
   363 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0)
   364 		return;
   365 
   366 	if (prevmb==mb)
   367 		return;
   368 
   369 	for (i=0;i<3;i++) {
   370 		int curbutton, prevbutton;
   371 		
   372 		curbutton = mb & (1<<i);
   373 		prevbutton = prevmb & (1<<i);
   374 	
   375 		if (curbutton && !prevbutton) {
   376 			SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   377 		}
   378 		if (!curbutton && prevbutton) {
   379 			SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   380 		}
   381 	}
   382 
   383 	prevmb = mb;
   384 }
   385 
   386 /* Check if mouse in visible area of the window */
   387 static int mouse_in_work_area(int winhandle, short mx, short my)
   388 {
   389 	short todo[4];
   390 	short inside[4] = {mx,my,1,1};
   391 
   392 	/* Browse the rectangle list */
   393 	if (wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])!=0) {
   394 		while (todo[2] && todo[3]) {
   395 			if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
   396 				return 1;
   397 			}
   398 
   399 			if (wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])==0) {
   400 				break;
   401 			}
   402 		}
   403 
   404 	}
   405 
   406 	return 0;
   407 }
   408 
   409 /* Clear key state for which we did not receive events for a while */
   410 
   411 static void clearKeyboardState(Uint32 tick)
   412 {
   413 	int i;
   414 
   415 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   416 		if (keyboard_ticks[i]) {
   417 			if (tick-keyboard_ticks[i] > KEY_PRESS_DURATION) {
   418 				gem_currentkeyboard[i]=0;
   419 				keyboard_ticks[i]=0;
   420 			}
   421 		}
   422 	}
   423 }