src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 11 Oct 2012 19:08:03 +0200
branchSDL-1.2
changeset 6572 0d391c407a4f
parent 6137 4720145f848b
child 6582 8b7e21749630
permissions -rw-r--r--
Use variables for what they were created for
     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 /* Functions prototypes */
    51 
    52 static int do_messages(_THIS, short *message);
    53 static void do_keyboard(short kc, short ks);
    54 static void do_mouse(_THIS, short mx, short my, short mb, short ks);
    55 
    56 /* Functions */
    57 
    58 void GEM_InitOSKeymap(_THIS)
    59 {
    60 	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    61 	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    62 
    63 	/* Mouse init */
    64 	GEM_mouse_relative = SDL_FALSE;
    65 
    66 	SDL_Atari_InitInternalKeymap(this);
    67 }
    68 
    69 void GEM_PumpEvents(_THIS)
    70 {
    71 	short prevkc, prevks;
    72 	static short maskmouseb=0;
    73 	int i;
    74 	SDL_keysym	keysym;
    75 
    76 	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
    77 	prevkc = prevks = 0;
    78 
    79 	for (;;)
    80 	{
    81 		int quit, resultat, event_mask, mouse_event;
    82 		short buffer[8], kc;
    83 		short x2,y2,w2,h2;
    84 		short mousex, mousey, mouseb, dummy;
    85 		short kstate;
    86 
    87 		quit =
    88 			mouse_event =
    89 			x2=y2=w2=h2 = 0;
    90 
    91 		event_mask = MU_MESAG|MU_TIMER|MU_KEYBD|MU_BUTTON;
    92 		if (!GEM_fullscreen && (GEM_handle>=0)) {
    93 			wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
    94 			event_mask |= MU_M1;
    95 			mouse_event = ( (SDL_GetAppState() & SDL_APPMOUSEFOCUS)
    96 				== SDL_APPMOUSEFOCUS) ? MO_LEAVE : MO_ENTER;
    97 		}
    98 
    99 		resultat = evnt_multi(
   100 			event_mask,
   101 			0x101,7,maskmouseb,
   102 			mouse_event,x2,y2,w2,h2,
   103 			0,0,0,0,0,
   104 			buffer,
   105 			10,
   106 			&mousex,&mousey,&mouseb,&kstate,&kc,&dummy
   107 		);
   108 
   109 		/* Message event ? */
   110 		if (resultat & MU_MESAG)
   111 			quit = do_messages(this, buffer);
   112 
   113 		/* Keyboard event ? */
   114 		if (resultat & MU_KEYBD) {
   115 			if ((prevkc != kc) || (prevks != kstate)) {
   116 				do_keyboard(kc,kstate);
   117 				prevkc = kc;
   118 				prevks = ks;
   119 			} else {
   120 				/* Avoid looping, if repeating same key */
   121 				break;
   122 			}
   123 		}
   124 
   125 		/* Mouse entering/leaving window */
   126 		if (resultat & MU_M1) {
   127 			if (this->input_grab == SDL_GRAB_OFF) {
   128 				/* Switch mouse focus state */
   129 				SDL_PrivateAppActive((mouse_event == MO_ENTER),
   130 					SDL_APPMOUSEFOCUS);
   131 			}
   132 			GEM_CheckMouseMode(this);
   133 		}
   134 
   135 		/* Mouse button event ? */
   136 		if (resultat & MU_BUTTON) {
   137 			do_mouse(this, mousex, mousey, mouseb, kstate);
   138 			maskmouseb = 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, short ks)
   281 {
   282 	int scancode;
   283 
   284 	if (kc) {
   285 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   286 		gem_currentkeyboard[scancode]=0xFF;
   287 	}
   288 
   289 	/* Read special keys */
   290 	if (ks & K_RSHIFT)
   291 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   292 	if (ks & K_LSHIFT)
   293 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   294 	if (ks & K_CTRL)
   295 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   296 	if (ks & K_ALT)
   297 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   298 }
   299 
   300 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
   301 {
   302 	static short prevmousex=0, prevmousey=0, prevmouseb=0;
   303 	short x2, y2, w2, h2;
   304 
   305 	/* Don't return mouse events if out of window */
   306 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   307 		return;
   308 	}
   309 
   310 	/* Retrieve window coords, and generate mouse events accordingly */
   311 	x2 = y2 = 0;
   312 	w2 = VDI_w;
   313 	h2 = VDI_h;
   314 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   315 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   316 
   317 		/* Do not generate mouse button event if out of window working area */
   318 		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
   319 			mb=prevmouseb;
   320 		}
   321 	}
   322 
   323 	/* Mouse motion ? */
   324 	if (GEM_mouse_relative) {
   325 		if (GEM_usedevmouse) {
   326 			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   327 		} else {
   328 			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   329 		}
   330 	} else {
   331 		if ((prevmousex!=mx) || (prevmousey!=my)) {
   332 			int posx, posy;
   333 
   334 			/* Give mouse position relative to window position */
   335 			posx = mx - x2;
   336 			if (posx<0) posx = 0;
   337 			if (posx>w2) posx = w2-1;
   338 			posy = my - y2;
   339 			if (posy<0) posy = 0;
   340 			if (posy>h2) posy = h2-1;
   341 
   342 			SDL_PrivateMouseMotion(0, 0, posx, posy);
   343 		}
   344 		prevmousex = mx;
   345 		prevmousey = my;
   346 	}
   347 
   348 	/* Mouse button ? */
   349 	if (prevmouseb!=mb) {
   350 		int i;
   351 
   352 		for (i=0;i<3;i++) {
   353 			int curbutton, prevbutton;
   354 		
   355 			curbutton = mb & (1<<i);
   356 			prevbutton = prevmouseb & (1<<i);
   357 		
   358 			if (curbutton && !prevbutton) {
   359 				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   360 			}
   361 			if (!curbutton && prevbutton) {
   362 				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   363 			}
   364 		}
   365 		prevmouseb = mb;
   366 	}
   367 
   368 	/* Read special keys */
   369 	if (ks & K_RSHIFT)
   370 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   371 	if (ks & K_LSHIFT)
   372 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   373 	if (ks & K_CTRL)
   374 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   375 	if (ks & K_ALT)
   376 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   377 }