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