src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Wed, 13 Sep 2006 21:19:13 +0000
branchSDL-1.2
changeset 3861 c3625a895caf
parent 3854 f1816773a309
child 4059 57b017e6aebf
permissions -rw-r--r--
Use new keyboard mapping routines
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 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 mousex, mousey, mouseb, dummy;
    71 	short kstate, prevkc, prevks;
    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 
    84 		quit =
    85 			mouse_event =
    86 			x2=y2=w2=h2 = 0;
    87 
    88 		event_mask = MU_MESAG|MU_TIMER|MU_KEYBD;
    89 		if (!GEM_fullscreen && (GEM_handle>=0)) {
    90 			wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
    91 			event_mask |= MU_M1;
    92 			if ( (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
    93 				mouse_event = MO_LEAVE;				
    94 			} else {
    95 				mouse_event = MO_ENTER;				
    96 			}
    97 		}
    98 
    99 		resultat = evnt_multi(
   100 			event_mask,
   101 			0,0,0,
   102 			mouse_event,x2,y2,w2,h2,
   103 			0,0,0,0,0,
   104 			buffer,
   105 			10,
   106 			&dummy,&dummy,&dummy,&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 			} else {
   118 				/* Avoid looping, if repeating same key */
   119 				break;
   120 			}
   121 		}
   122 
   123 		/* Mouse entering/leaving window */
   124 		if (resultat & MU_M1) {
   125 			if (this->input_grab == SDL_GRAB_OFF) {
   126 				if (SDL_GetAppState() & SDL_APPMOUSEFOCUS) {
   127 					SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
   128 					if (SDL_GetAppState() & SDL_APPINPUTFOCUS) {
   129 						graf_mouse(ARROW, NULL);
   130 					}
   131 				} else {
   132 					SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   133 					if (SDL_GetAppState() & SDL_APPINPUTFOCUS) {
   134 						if (GEM_cursor == (void *) -1) {
   135 							graf_mouse(M_OFF, NULL);
   136 						} else if (GEM_cursor) {
   137 							graf_mouse(USER_DEF, GEM_cursor->mform_p);
   138 						}
   139 					}
   140 				}
   141 			}
   142 		}
   143 
   144 		/* Timer event ? */
   145 		if ((resultat & MU_TIMER) || quit)
   146 			break;
   147 	}
   148 
   149 	/* Update mouse */
   150 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   151 	do_mouse(this, mousex, mousey, mouseb, kstate);
   152 
   153 	/* Now generate keyboard events */
   154 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   155 		/* Key pressed ? */
   156 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   157 			SDL_PrivateKeyboard(SDL_PRESSED,
   158 				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
   159 			
   160 		/* Key unpressed ? */
   161 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   162 			SDL_PrivateKeyboard(SDL_RELEASED,
   163 				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
   164 	}
   165 
   166 	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   167 
   168 	/* Refresh window name ? */
   169 	if (GEM_refresh_name) {
   170 		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   171 			/* Fullscreen/windowed */
   172 			if (GEM_title_name) {
   173 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   174 			}
   175 		} else {
   176 			/* Iconified */
   177 			if (GEM_icon_name) {
   178 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   179 			}
   180 		}
   181 		GEM_refresh_name = SDL_FALSE;
   182 	}
   183 }
   184 
   185 static int do_messages(_THIS, short *message)
   186 {
   187 	int quit, posted;
   188 	short x2,y2,w2,h2;
   189 
   190 	quit=0;
   191 	switch (message[0]) {
   192 		case WM_CLOSED:
   193 		case AP_TERM:    
   194 			posted = SDL_PrivateQuit();
   195 			quit=1;
   196 			break;
   197 		case WM_MOVED:
   198 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   199 			break;
   200 		case WM_TOPPED:
   201 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   202 			/* Continue with TOP event processing */
   203 		case WM_ONTOP:
   204 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   205 			if (VDI_setpalette) {
   206 				VDI_setpalette(this, VDI_curpalette);
   207 			}
   208 			break;
   209 		case WM_REDRAW:
   210 			if (!GEM_lock_redraw) {
   211 				GEM_wind_redraw(this, message[3],&message[4]);
   212 			}
   213 			break;
   214 		case WM_ICONIFY:
   215 		case WM_ALLICONIFY:
   216 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   217 			/* If we're active, make ourselves inactive */
   218 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   219 				/* Send an internal deactivate event */
   220 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   221 			}
   222 			/* Update window title */
   223 			if (GEM_refresh_name && GEM_icon_name) {
   224 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   225 				GEM_refresh_name = SDL_FALSE;
   226 			}
   227 			break;
   228 		case WM_UNICONIFY:
   229 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   230 			/* If we're not active, make ourselves active */
   231 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   232 				/* Send an internal activate event */
   233 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   234 			}
   235 			if (GEM_refresh_name && GEM_title_name) {
   236 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   237 				GEM_refresh_name = SDL_FALSE;
   238 			}
   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 			break;
   276 	}
   277 	
   278 	return quit;
   279 }
   280 
   281 static void do_keyboard(short kc, short ks)
   282 {
   283 	int scancode;
   284 
   285 	if (kc) {
   286 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   287 		gem_currentkeyboard[scancode]=0xFF;
   288 	}
   289 
   290 	/* Read special keys */
   291 	if (ks & K_RSHIFT)
   292 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   293 	if (ks & K_LSHIFT)
   294 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   295 	if (ks & K_CTRL)
   296 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   297 	if (ks & K_ALT)
   298 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   299 }
   300 
   301 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
   302 {
   303 	static short prevmousex=0, prevmousey=0, prevmouseb=0;
   304 	short x2, y2, w2, h2;
   305 
   306 	/* Don't return mouse events if out of window */
   307 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   308 		return;
   309 	}
   310 
   311 	/* Retrieve window coords, and generate mouse events accordingly */
   312 	x2 = y2 = 0;
   313 	w2 = VDI_w;
   314 	h2 = VDI_h;
   315 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   316 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   317 
   318 		/* Do not generate mouse button event if out of window working area */
   319 		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
   320 			mb=prevmouseb;
   321 		}
   322 	}
   323 
   324 	/* Mouse motion ? */
   325 	if (GEM_mouse_relative) {
   326 		if (GEM_usedevmouse) {
   327 			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   328 		} else {
   329 			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   330 		}
   331 	} else {
   332 		if ((prevmousex!=mx) || (prevmousey!=my)) {
   333 			int posx, posy;
   334 
   335 			/* Give mouse position relative to window position */
   336 			posx = mx - x2;
   337 			if (posx<0) posx = 0;
   338 			if (posx>w2) posx = w2-1;
   339 			posy = my - y2;
   340 			if (posy<0) posy = 0;
   341 			if (posy>h2) posy = h2-1;
   342 
   343 			SDL_PrivateMouseMotion(0, 0, posx, posy);
   344 		}
   345 		prevmousex = mx;
   346 		prevmousey = my;
   347 	}
   348 
   349 	/* Mouse button ? */
   350 	if (prevmouseb!=mb) {
   351 		int i;
   352 
   353 		for (i=0;i<2;i++) {
   354 			int curbutton, prevbutton;
   355 		
   356 			curbutton = mb & (1<<i);
   357 			prevbutton = prevmouseb & (1<<i);
   358 		
   359 			if (curbutton && !prevbutton) {
   360 				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   361 			}
   362 			if (!curbutton && prevbutton) {
   363 				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   364 			}
   365 		}
   366 		prevmouseb = mb;
   367 	}
   368 
   369 	/* Read special keys */
   370 	if (ks & K_RSHIFT)
   371 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   372 	if (ks & K_LSHIFT)
   373 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   374 	if (ks & K_CTRL)
   375 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   376 	if (ks & K_ALT)
   377 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   378 }