src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Wed, 08 Feb 2006 18:59:49 +0000
changeset 1350 adc8ff3c9597
parent 1336 3692456e7b0f
child 1358 c71e05b4dc2e
permissions -rw-r--r--
Fixes for new SDL embedded libc stuff
     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.h"
    35 #include "SDL_string.h"
    36 #include "SDL_sysevents.h"
    37 #include "SDL_events_c.h"
    38 #include "SDL_gemvideo.h"
    39 #include "SDL_gemevents_c.h"
    40 #include "SDL_atarikeys.h"	/* for keyboard scancodes */
    41 #include "SDL_atarievents_c.h"
    42 #include "SDL_xbiosevents_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<sizeof(keymap); 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 				} else {
   190 					SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   191 				}
   192 			}
   193 		}
   194 
   195 		/* Timer event ? */
   196 		if ((resultat & MU_TIMER) || quit)
   197 			break;
   198 	}
   199 
   200 	/* Update mouse */
   201 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   202 	do_mouse(this, mousex, mousey, mouseb, kstate);
   203 
   204 	/* Now generate keyboard events */
   205 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   206 		/* Key pressed ? */
   207 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   208 			SDL_PrivateKeyboard(SDL_PRESSED,
   209 				TranslateKey(i, gem_currentascii[i], &keysym, SDL_TRUE));
   210 			
   211 		/* Key unpressed ? */
   212 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   213 			SDL_PrivateKeyboard(SDL_RELEASED,
   214 				TranslateKey(i, gem_currentascii[i], &keysym, SDL_FALSE));
   215 	}
   216 
   217 	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   218 
   219 	/* Refresh window name ? */
   220 	if (GEM_refresh_name) {
   221 		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   222 			/* Fullscreen/windowed */
   223 			if (GEM_title_name) {
   224 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   225 			}
   226 		} else {
   227 			/* Iconified */
   228 			if (GEM_icon_name) {
   229 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   230 			}
   231 		}
   232 		GEM_refresh_name = SDL_FALSE;
   233 	}
   234 }
   235 
   236 static int do_messages(_THIS, short *message)
   237 {
   238 	int quit, posted;
   239 	short x2,y2,w2,h2;
   240 
   241 	quit=0;
   242 	switch (message[0]) {
   243 		case WM_CLOSED:
   244 		case AP_TERM:    
   245 			posted = SDL_PrivateQuit();
   246 			quit=1;
   247 			break;
   248 		case WM_MOVED:
   249 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   250 			break;
   251 		case WM_TOPPED:
   252 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   253 			/* Continue with TOP event processing */
   254 		case WM_ONTOP:
   255 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   256 			if (VDI_setpalette) {
   257 				VDI_setpalette(this, VDI_curpalette);
   258 			}
   259 			break;
   260 		case WM_REDRAW:
   261 			if (!GEM_lock_redraw) {
   262 				GEM_wind_redraw(this, message[3],&message[4]);
   263 			}
   264 			break;
   265 		case WM_ICONIFY:
   266 		case WM_ALLICONIFY:
   267 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   268 			/* If we're active, make ourselves inactive */
   269 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   270 				/* Send an internal deactivate event */
   271 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   272 			}
   273 			/* Update window title */
   274 			if (GEM_refresh_name && GEM_icon_name) {
   275 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   276 				GEM_refresh_name = SDL_FALSE;
   277 			}
   278 			break;
   279 		case WM_UNICONIFY:
   280 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   281 			/* If we're not active, make ourselves active */
   282 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   283 				/* Send an internal activate event */
   284 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   285 			}
   286 			if (GEM_refresh_name && GEM_title_name) {
   287 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   288 				GEM_refresh_name = SDL_FALSE;
   289 			}
   290 			break;
   291 		case WM_SIZED:
   292 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   293 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   294 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   295 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   296 			SDL_PrivateResize(w2, h2);
   297 			break;
   298 		case WM_FULLED:
   299 			{
   300 				short x,y,w,h;
   301 
   302 				if (GEM_win_fulled) {
   303 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   304 					GEM_win_fulled = SDL_FALSE;
   305 				} else {
   306 					x = GEM_desk_x;
   307 					y = GEM_desk_y;
   308 					w = GEM_desk_w;
   309 					h = GEM_desk_h;
   310 					GEM_win_fulled = SDL_TRUE;
   311 				}
   312 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   313 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   314 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   315 				SDL_PrivateResize(w2, h2);
   316 			}
   317 			break;
   318 		case WM_BOTTOMED:
   319 			wind_set(message[3],WF_BOTTOM,0,0,0,0);
   320 			/* Continue with BOTTOM event processing */
   321 		case WM_UNTOPPED:
   322 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   323 			if (VDI_setpalette) {
   324 				VDI_setpalette(this, VDI_oldpalette);
   325 			}
   326 			break;
   327 	}
   328 	
   329 	return quit;
   330 }
   331 
   332 static void do_keyboard(short kc, short ks)
   333 {
   334 	int			scancode, asciicode;
   335 
   336 	if (kc) {
   337 		scancode=(kc>>8) & 127;
   338 		asciicode=kc & 255;
   339 
   340 		gem_currentkeyboard[scancode]=0xFF;
   341 		gem_currentascii[scancode]=asciicode;
   342 	}
   343 
   344 	/* Read special keys */
   345 	if (ks & K_RSHIFT)
   346 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   347 	if (ks & K_LSHIFT)
   348 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   349 	if (ks & K_CTRL)
   350 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   351 	if (ks & K_ALT)
   352 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   353 }
   354 
   355 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
   356 {
   357 	static short prevmousex=0, prevmousey=0, prevmouseb=0;
   358 	short x2, y2, w2, h2;
   359 
   360 	/* Don't return mouse events if out of window */
   361 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   362 		return;
   363 	}
   364 
   365 	/* Retrieve window coords, and generate mouse events accordingly */
   366 	x2 = y2 = 0;
   367 	w2 = VDI_w;
   368 	h2 = VDI_h;
   369 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   370 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   371 
   372 		/* Do not generate mouse button event if out of window working area */
   373 		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
   374 			mb=prevmouseb;
   375 		}
   376 	}
   377 
   378 	/* Mouse motion ? */
   379 	if (GEM_mouse_relative) {
   380 		SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   381 	} else {
   382 		if ((prevmousex!=mx) || (prevmousey!=my)) {
   383 			int posx, posy;
   384 
   385 			/* Give mouse position relative to window position */
   386 			posx = mx - x2;
   387 			if (posx<0) posx = 0;
   388 			if (posx>w2) posx = w2-1;
   389 			posy = my - y2;
   390 			if (posy<0) posy = 0;
   391 			if (posy>h2) posy = h2-1;
   392 
   393 			SDL_PrivateMouseMotion(0, 0, posx, posy);
   394 		}
   395 		prevmousex = mx;
   396 		prevmousey = my;
   397 	}
   398 
   399 	/* Mouse button ? */
   400 	if (prevmouseb!=mb) {
   401 		int i;
   402 
   403 		for (i=0;i<2;i++) {
   404 			int curbutton, prevbutton;
   405 		
   406 			curbutton = mb & (1<<i);
   407 			prevbutton = prevmouseb & (1<<i);
   408 		
   409 			if (curbutton && !prevbutton) {
   410 				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   411 			}
   412 			if (!curbutton && prevbutton) {
   413 				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   414 			}
   415 		}
   416 		prevmouseb = mb;
   417 	}
   418 
   419 	/* Read special keys */
   420 	if (ks & K_RSHIFT)
   421 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   422 	if (ks & K_LSHIFT)
   423 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   424 	if (ks & K_CTRL)
   425 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   426 	if (ks & K_ALT)
   427 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   428 }