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