src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Fri, 09 Jul 2010 17:19:18 +0200
branchSDL-1.2
changeset 4564 5002d6aeb85c
parent 4159 a1b03ba2fcd0
child 5649 02ab15cfa992
permissions -rw-r--r--
Use proper GEM API to handle mouse button events
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2009 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 "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
    40 #include "../ataricommon/SDL_atarievents_c.h"
    41 #include "../ataricommon/SDL_xbiosevents_c.h"
    42 #include "../ataricommon/SDL_ataridevmouse_c.h"
    43 
    44 /* Variables */
    45 
    46 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    47 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    48 
    49 /* Functions prototypes */
    50 
    51 static int do_messages(_THIS, short *message);
    52 static void do_keyboard(short kc, short ks);
    53 static void do_mouse(_THIS, short mx, short my, short mb, short ks);
    54 
    55 /* Functions */
    56 
    57 void GEM_InitOSKeymap(_THIS)
    58 {
    59 	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    60 	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    61 
    62 	/* Mouse init */
    63 	GEM_mouse_relative = SDL_FALSE;
    64 
    65 	SDL_Atari_InitInternalKeymap(this);
    66 }
    67 
    68 void GEM_PumpEvents(_THIS)
    69 {
    70 	short prevkc, prevks;
    71 	static short maskmouseb=0;
    72 	int i;
    73 	SDL_keysym	keysym;
    74 
    75 	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
    76 	prevkc = prevks = 0;
    77 
    78 	for (;;)
    79 	{
    80 		int quit, resultat, event_mask, mouse_event;
    81 		short buffer[8], kc;
    82 		short x2,y2,w2,h2;
    83 		short mousex, mousey, mouseb, dummy;
    84 		short kstate;
    85 
    86 		quit =
    87 			mouse_event =
    88 			x2=y2=w2=h2 = 0;
    89 
    90 		event_mask = MU_MESAG|MU_TIMER|MU_KEYBD|MU_BUTTON;
    91 		if (!GEM_fullscreen && (GEM_handle>=0)) {
    92 			wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
    93 			event_mask |= MU_M1;
    94 			mouse_event = ( (SDL_GetAppState() & SDL_APPMOUSEFOCUS)
    95 				== SDL_APPMOUSEFOCUS) ? MO_LEAVE : MO_ENTER;
    96 		}
    97 
    98 		resultat = evnt_multi(
    99 			event_mask,
   100 			0x101,7,maskmouseb,
   101 			mouse_event,x2,y2,w2,h2,
   102 			0,0,0,0,0,
   103 			buffer,
   104 			10,
   105 			&mousex,&mousey,&mouseb,&kstate,&kc,&dummy
   106 		);
   107 
   108 		/* Message event ? */
   109 		if (resultat & MU_MESAG)
   110 			quit = do_messages(this, buffer);
   111 
   112 		/* Keyboard event ? */
   113 		if (resultat & MU_KEYBD) {
   114 			if ((prevkc != kc) || (prevks != kstate)) {
   115 				do_keyboard(kc,kstate);
   116 			} else {
   117 				/* Avoid looping, if repeating same key */
   118 				break;
   119 			}
   120 		}
   121 
   122 		/* Mouse entering/leaving window */
   123 		if (resultat & MU_M1) {
   124 			if (this->input_grab == SDL_GRAB_OFF) {
   125 				/* Switch mouse focus state */
   126 				SDL_PrivateAppActive((mouse_event == MO_ENTER),
   127 					SDL_APPMOUSEFOCUS);
   128 			}
   129 			GEM_CheckMouseMode(this);
   130 		}
   131 
   132 		/* Mouse button event ? */
   133 		if (resultat & MU_BUTTON) {
   134 			do_mouse(this, mousex, mousey, mouseb, kstate);
   135 			maskmouseb = mouseb & 7;
   136 		}
   137 
   138 		/* Timer event ? */
   139 		if ((resultat & MU_TIMER) || quit)
   140 			break;
   141 	}
   142 
   143 	/* Now generate keyboard events */
   144 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   145 		/* Key pressed ? */
   146 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   147 			SDL_PrivateKeyboard(SDL_PRESSED,
   148 				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
   149 			
   150 		/* Key unpressed ? */
   151 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   152 			SDL_PrivateKeyboard(SDL_RELEASED,
   153 				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
   154 	}
   155 
   156 	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   157 
   158 	/* Refresh window name ? */
   159 	if (GEM_refresh_name) {
   160 		const char *window_name =
   161 			(SDL_GetAppState() & SDL_APPACTIVE)
   162 			? GEM_title_name : GEM_icon_name;
   163 		if (window_name) {
   164 			wind_set(GEM_handle,WF_NAME,
   165 				(short)(((unsigned long)window_name)>>16),
   166 				(short)(((unsigned long)window_name) & 0xffff),
   167 				0,0);
   168 		}
   169 		GEM_refresh_name = SDL_FALSE;
   170 	}
   171 }
   172 
   173 static int do_messages(_THIS, short *message)
   174 {
   175 	int quit, posted, check_mouse_mode;
   176 	short x2,y2,w2,h2;
   177 
   178 	quit = check_mouse_mode = 0;
   179 	switch (message[0]) {
   180 		case WM_CLOSED:
   181 		case AP_TERM:    
   182 			posted = SDL_PrivateQuit();
   183 			quit=1;
   184 			break;
   185 		case WM_MOVED:
   186 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   187 			break;
   188 		case WM_TOPPED:
   189 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   190 			/* Continue with TOP event processing */
   191 		case WM_ONTOP:
   192 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   193 			if (VDI_setpalette) {
   194 				VDI_setpalette(this, VDI_curpalette);
   195 			}
   196 			check_mouse_mode = 1;
   197 			break;
   198 		case WM_REDRAW:
   199 			if (!GEM_lock_redraw) {
   200 				GEM_wind_redraw(this, message[3],&message[4]);
   201 			}
   202 			break;
   203 		case WM_ICONIFY:
   204 		case WM_ALLICONIFY:
   205 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   206 			/* If we're active, make ourselves inactive */
   207 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   208 				/* Send an internal deactivate event */
   209 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   210 			}
   211 			/* Update window title */
   212 			if (GEM_refresh_name && GEM_icon_name) {
   213 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   214 				GEM_refresh_name = SDL_FALSE;
   215 			}
   216 			check_mouse_mode = 1;
   217 			break;
   218 		case WM_UNICONIFY:
   219 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   220 			/* If we're not active, make ourselves active */
   221 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   222 				/* Send an internal activate event */
   223 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   224 			}
   225 			if (GEM_refresh_name && GEM_title_name) {
   226 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   227 				GEM_refresh_name = SDL_FALSE;
   228 			}
   229 			check_mouse_mode = 1;
   230 			break;
   231 		case WM_SIZED:
   232 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   233 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   234 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   235 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   236 			SDL_PrivateResize(w2, h2);
   237 			break;
   238 		case WM_FULLED:
   239 			{
   240 				short x,y,w,h;
   241 
   242 				if (GEM_win_fulled) {
   243 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   244 					GEM_win_fulled = SDL_FALSE;
   245 				} else {
   246 					x = GEM_desk_x;
   247 					y = GEM_desk_y;
   248 					w = GEM_desk_w;
   249 					h = GEM_desk_h;
   250 					GEM_win_fulled = SDL_TRUE;
   251 				}
   252 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   253 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   254 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   255 				SDL_PrivateResize(w2, h2);
   256 			}
   257 			break;
   258 		case WM_BOTTOMED:
   259 			wind_set(message[3],WF_BOTTOM,0,0,0,0);
   260 			/* Continue with BOTTOM event processing */
   261 		case WM_UNTOPPED:
   262 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   263 			if (VDI_setpalette) {
   264 				VDI_setpalette(this, VDI_oldpalette);
   265 			}
   266 			check_mouse_mode = 1;
   267 			break;
   268 	}
   269 
   270 	if (check_mouse_mode) {
   271 		GEM_CheckMouseMode(this);
   272 	}
   273 	
   274 	return quit;
   275 }
   276 
   277 static void do_keyboard(short kc, short ks)
   278 {
   279 	int scancode;
   280 
   281 	if (kc) {
   282 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   283 		gem_currentkeyboard[scancode]=0xFF;
   284 	}
   285 
   286 	/* Read special keys */
   287 	if (ks & K_RSHIFT)
   288 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   289 	if (ks & K_LSHIFT)
   290 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   291 	if (ks & K_CTRL)
   292 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   293 	if (ks & K_ALT)
   294 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   295 }
   296 
   297 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
   298 {
   299 	static short prevmousex=0, prevmousey=0, prevmouseb=0;
   300 	short x2, y2, w2, h2;
   301 
   302 	/* Don't return mouse events if out of window */
   303 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   304 		return;
   305 	}
   306 
   307 	/* Retrieve window coords, and generate mouse events accordingly */
   308 	x2 = y2 = 0;
   309 	w2 = VDI_w;
   310 	h2 = VDI_h;
   311 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   312 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   313 
   314 		/* Do not generate mouse button event if out of window working area */
   315 		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
   316 			mb=prevmouseb;
   317 		}
   318 	}
   319 
   320 	/* Mouse motion ? */
   321 	if (GEM_mouse_relative) {
   322 		if (GEM_usedevmouse) {
   323 			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   324 		} else {
   325 			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   326 		}
   327 	} else {
   328 		if ((prevmousex!=mx) || (prevmousey!=my)) {
   329 			int posx, posy;
   330 
   331 			/* Give mouse position relative to window position */
   332 			posx = mx - x2;
   333 			if (posx<0) posx = 0;
   334 			if (posx>w2) posx = w2-1;
   335 			posy = my - y2;
   336 			if (posy<0) posy = 0;
   337 			if (posy>h2) posy = h2-1;
   338 
   339 			SDL_PrivateMouseMotion(0, 0, posx, posy);
   340 		}
   341 		prevmousex = mx;
   342 		prevmousey = my;
   343 	}
   344 
   345 	/* Mouse button ? */
   346 	if (prevmouseb!=mb) {
   347 		int i;
   348 
   349 		for (i=0;i<3;i++) {
   350 			int curbutton, prevbutton;
   351 		
   352 			curbutton = mb & (1<<i);
   353 			prevbutton = prevmouseb & (1<<i);
   354 		
   355 			if (curbutton && !prevbutton) {
   356 				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   357 			}
   358 			if (!curbutton && prevbutton) {
   359 				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   360 			}
   361 		}
   362 		prevmouseb = mb;
   363 	}
   364 
   365 	/* Read special keys */
   366 	if (ks & K_RSHIFT)
   367 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   368 	if (ks & K_LSHIFT)
   369 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   370 	if (ks & K_CTRL)
   371 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   372 	if (ks & K_ALT)
   373 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   374 }