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