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