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