src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 07 Sep 2006 20:52:31 +0000
branchSDL-1.2
changeset 3854 f1816773a309
parent 1857 417f2af2bd52
child 3861 c3625a895caf
permissions -rw-r--r--
/me dumb, sizeof(keymap) != number of elements
     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 /* Defines */
    45 
    46 #define ATARIBIOS_MAXKEYS 128
    47 
    48 /* Variables */
    49 
    50 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    51 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    52 static unsigned char gem_currentascii[ATARIBIOS_MAXKEYS];
    53 
    54 /* The translation tables from a console scancode to a SDL keysym */
    55 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    56 
    57 /* Functions prototypes */
    58 
    59 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
    60 	SDL_bool pressed);
    61 static int do_messages(_THIS, short *message);
    62 static void do_keyboard(short kc, short ks);
    63 static void do_mouse(_THIS, short mx, short my, short mb, short ks);
    64 
    65 /* Functions */
    66 
    67 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
    68 	SDL_bool pressed)
    69 {
    70 	/* Set the keysym information */
    71 	keysym->scancode = scancode;
    72 
    73 	if (asciicode)
    74 		keysym->sym = asciicode;		
    75 	else
    76 		keysym->sym = keymap[scancode];
    77 
    78 	keysym->mod = KMOD_NONE;
    79 	keysym->unicode = 0;
    80 	if (SDL_TranslateUNICODE && pressed) {
    81 		keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
    82 	}
    83 
    84 	return(keysym);
    85 }
    86 
    87 void GEM_InitOSKeymap(_THIS)
    88 {
    89 	int i;
    90 
    91 	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    92 	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    93 	SDL_memset(gem_currentascii, 0, sizeof(gem_currentascii));
    94 
    95 	/* Initialize keymap */
    96 	for ( i=0; i<ATARIBIOS_MAXKEYS; i++ )
    97 		keymap[i] = SDLK_UNKNOWN;
    98 
    99 	/* Functions keys */
   100 	for ( i = 0; i<10; i++ )
   101 		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
   102 
   103 	/* Cursor keypad */
   104 	keymap[SCANCODE_HELP] = SDLK_HELP;
   105 	keymap[SCANCODE_UNDO] = SDLK_UNDO;
   106 	keymap[SCANCODE_INSERT] = SDLK_INSERT;
   107 	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
   108 	keymap[SCANCODE_UP] = SDLK_UP;
   109 	keymap[SCANCODE_DOWN] = SDLK_DOWN;
   110 	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
   111 	keymap[SCANCODE_LEFT] = SDLK_LEFT;
   112 
   113 	/* Special keys */
   114 	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
   115 	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
   116 	keymap[SCANCODE_TAB] = SDLK_TAB;
   117 	keymap[SCANCODE_ENTER] = SDLK_RETURN;
   118 	keymap[SCANCODE_DELETE] = SDLK_DELETE;
   119 	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
   120 	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
   121 	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
   122 	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
   123 	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
   124 
   125 	/* Mouse init */
   126 	GEM_mouse_relative = SDL_FALSE;
   127 }
   128 
   129 void GEM_PumpEvents(_THIS)
   130 {
   131 	short mousex, mousey, mouseb, dummy;
   132 	short kstate, prevkc, prevks;
   133 	int i;
   134 	SDL_keysym	keysym;
   135 
   136 	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
   137 	prevkc = prevks = 0;
   138 	
   139 	for (;;)
   140 	{
   141 		int quit, resultat, event_mask, mouse_event;
   142 		short buffer[8], kc;
   143 		short x2,y2,w2,h2;
   144 
   145 		quit =
   146 			mouse_event =
   147 			x2=y2=w2=h2 = 0;
   148 
   149 		event_mask = MU_MESAG|MU_TIMER|MU_KEYBD;
   150 		if (!GEM_fullscreen && (GEM_handle>=0)) {
   151 			wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   152 			event_mask |= MU_M1;
   153 			if ( (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
   154 				mouse_event = MO_LEAVE;				
   155 			} else {
   156 				mouse_event = MO_ENTER;				
   157 			}
   158 		}
   159 
   160 		resultat = evnt_multi(
   161 			event_mask,
   162 			0,0,0,
   163 			mouse_event,x2,y2,w2,h2,
   164 			0,0,0,0,0,
   165 			buffer,
   166 			10,
   167 			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
   168 		);
   169 
   170 		/* Message event ? */
   171 		if (resultat & MU_MESAG)
   172 			quit = do_messages(this, buffer);
   173 
   174 		/* Keyboard event ? */
   175 		if (resultat & MU_KEYBD) {
   176 			if ((prevkc != kc) || (prevks != kstate)) {
   177 				do_keyboard(kc,kstate);
   178 			} else {
   179 				/* Avoid looping, if repeating same key */
   180 				break;
   181 			}
   182 		}
   183 
   184 		/* Mouse entering/leaving window */
   185 		if (resultat & MU_M1) {
   186 			if (this->input_grab == SDL_GRAB_OFF) {
   187 				if (SDL_GetAppState() & SDL_APPMOUSEFOCUS) {
   188 					SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
   189 					if (SDL_GetAppState() & SDL_APPINPUTFOCUS) {
   190 						graf_mouse(ARROW, NULL);
   191 					}
   192 				} else {
   193 					SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   194 					if (SDL_GetAppState() & SDL_APPINPUTFOCUS) {
   195 						if (GEM_cursor == (void *) -1) {
   196 							graf_mouse(M_OFF, NULL);
   197 						} else if (GEM_cursor) {
   198 							graf_mouse(USER_DEF, GEM_cursor->mform_p);
   199 						}
   200 					}
   201 				}
   202 			}
   203 		}
   204 
   205 		/* Timer event ? */
   206 		if ((resultat & MU_TIMER) || quit)
   207 			break;
   208 	}
   209 
   210 	/* Update mouse */
   211 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   212 	do_mouse(this, mousex, mousey, mouseb, kstate);
   213 
   214 	/* Now generate keyboard events */
   215 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   216 		/* Key pressed ? */
   217 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   218 			SDL_PrivateKeyboard(SDL_PRESSED,
   219 				TranslateKey(i, gem_currentascii[i], &keysym, SDL_TRUE));
   220 			
   221 		/* Key unpressed ? */
   222 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   223 			SDL_PrivateKeyboard(SDL_RELEASED,
   224 				TranslateKey(i, gem_currentascii[i], &keysym, SDL_FALSE));
   225 	}
   226 
   227 	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   228 
   229 	/* Refresh window name ? */
   230 	if (GEM_refresh_name) {
   231 		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   232 			/* Fullscreen/windowed */
   233 			if (GEM_title_name) {
   234 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   235 			}
   236 		} else {
   237 			/* Iconified */
   238 			if (GEM_icon_name) {
   239 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   240 			}
   241 		}
   242 		GEM_refresh_name = SDL_FALSE;
   243 	}
   244 }
   245 
   246 static int do_messages(_THIS, short *message)
   247 {
   248 	int quit, posted;
   249 	short x2,y2,w2,h2;
   250 
   251 	quit=0;
   252 	switch (message[0]) {
   253 		case WM_CLOSED:
   254 		case AP_TERM:    
   255 			posted = SDL_PrivateQuit();
   256 			quit=1;
   257 			break;
   258 		case WM_MOVED:
   259 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   260 			break;
   261 		case WM_TOPPED:
   262 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   263 			/* Continue with TOP event processing */
   264 		case WM_ONTOP:
   265 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   266 			if (VDI_setpalette) {
   267 				VDI_setpalette(this, VDI_curpalette);
   268 			}
   269 			break;
   270 		case WM_REDRAW:
   271 			if (!GEM_lock_redraw) {
   272 				GEM_wind_redraw(this, message[3],&message[4]);
   273 			}
   274 			break;
   275 		case WM_ICONIFY:
   276 		case WM_ALLICONIFY:
   277 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   278 			/* If we're active, make ourselves inactive */
   279 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   280 				/* Send an internal deactivate event */
   281 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   282 			}
   283 			/* Update window title */
   284 			if (GEM_refresh_name && GEM_icon_name) {
   285 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   286 				GEM_refresh_name = SDL_FALSE;
   287 			}
   288 			break;
   289 		case WM_UNICONIFY:
   290 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   291 			/* If we're not active, make ourselves active */
   292 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   293 				/* Send an internal activate event */
   294 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   295 			}
   296 			if (GEM_refresh_name && GEM_title_name) {
   297 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   298 				GEM_refresh_name = SDL_FALSE;
   299 			}
   300 			break;
   301 		case WM_SIZED:
   302 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   303 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   304 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   305 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   306 			SDL_PrivateResize(w2, h2);
   307 			break;
   308 		case WM_FULLED:
   309 			{
   310 				short x,y,w,h;
   311 
   312 				if (GEM_win_fulled) {
   313 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   314 					GEM_win_fulled = SDL_FALSE;
   315 				} else {
   316 					x = GEM_desk_x;
   317 					y = GEM_desk_y;
   318 					w = GEM_desk_w;
   319 					h = GEM_desk_h;
   320 					GEM_win_fulled = SDL_TRUE;
   321 				}
   322 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   323 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   324 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   325 				SDL_PrivateResize(w2, h2);
   326 			}
   327 			break;
   328 		case WM_BOTTOMED:
   329 			wind_set(message[3],WF_BOTTOM,0,0,0,0);
   330 			/* Continue with BOTTOM event processing */
   331 		case WM_UNTOPPED:
   332 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   333 			if (VDI_setpalette) {
   334 				VDI_setpalette(this, VDI_oldpalette);
   335 			}
   336 			break;
   337 	}
   338 	
   339 	return quit;
   340 }
   341 
   342 static void do_keyboard(short kc, short ks)
   343 {
   344 	int			scancode, asciicode;
   345 
   346 	if (kc) {
   347 		scancode=(kc>>8) & 127;
   348 		asciicode=kc & 255;
   349 
   350 		gem_currentkeyboard[scancode]=0xFF;
   351 		gem_currentascii[scancode]=asciicode;
   352 	}
   353 
   354 	/* Read special keys */
   355 	if (ks & K_RSHIFT)
   356 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   357 	if (ks & K_LSHIFT)
   358 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   359 	if (ks & K_CTRL)
   360 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   361 	if (ks & K_ALT)
   362 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   363 }
   364 
   365 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
   366 {
   367 	static short prevmousex=0, prevmousey=0, prevmouseb=0;
   368 	short x2, y2, w2, h2;
   369 
   370 	/* Don't return mouse events if out of window */
   371 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   372 		return;
   373 	}
   374 
   375 	/* Retrieve window coords, and generate mouse events accordingly */
   376 	x2 = y2 = 0;
   377 	w2 = VDI_w;
   378 	h2 = VDI_h;
   379 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   380 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   381 
   382 		/* Do not generate mouse button event if out of window working area */
   383 		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
   384 			mb=prevmouseb;
   385 		}
   386 	}
   387 
   388 	/* Mouse motion ? */
   389 	if (GEM_mouse_relative) {
   390 		if (GEM_usedevmouse) {
   391 			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   392 		} else {
   393 			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   394 		}
   395 	} else {
   396 		if ((prevmousex!=mx) || (prevmousey!=my)) {
   397 			int posx, posy;
   398 
   399 			/* Give mouse position relative to window position */
   400 			posx = mx - x2;
   401 			if (posx<0) posx = 0;
   402 			if (posx>w2) posx = w2-1;
   403 			posy = my - y2;
   404 			if (posy<0) posy = 0;
   405 			if (posy>h2) posy = h2-1;
   406 
   407 			SDL_PrivateMouseMotion(0, 0, posx, posy);
   408 		}
   409 		prevmousex = mx;
   410 		prevmousey = my;
   411 	}
   412 
   413 	/* Mouse button ? */
   414 	if (prevmouseb!=mb) {
   415 		int i;
   416 
   417 		for (i=0;i<2;i++) {
   418 			int curbutton, prevbutton;
   419 		
   420 			curbutton = mb & (1<<i);
   421 			prevbutton = prevmouseb & (1<<i);
   422 		
   423 			if (curbutton && !prevbutton) {
   424 				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   425 			}
   426 			if (!curbutton && prevbutton) {
   427 				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   428 			}
   429 		}
   430 		prevmouseb = mb;
   431 	}
   432 
   433 	/* Read special keys */
   434 	if (ks & K_RSHIFT)
   435 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   436 	if (ks & K_LSHIFT)
   437 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   438 	if (ks & K_CTRL)
   439 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   440 	if (ks & K_ALT)
   441 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   442 }