src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Tue, 10 Aug 2004 20:58:47 +0000
changeset 928 6c87754f324c
parent 927 c5689bd09eaa
child 964 d9209754ebee
permissions -rw-r--r--
Forgot to initialize maximum position for mouse
     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_xbiosinterrupt_s.h"
    48 
    49 /* Defines */
    50 
    51 #define ATARIBIOS_MAXKEYS 128
    52 
    53 /* Variables */
    54 
    55 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    56 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    57 static unsigned char gem_currentascii[ATARIBIOS_MAXKEYS];
    58 
    59 /* The translation tables from a console scancode to a SDL keysym */
    60 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    61 
    62 /* Functions prototypes */
    63 
    64 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym);
    65 static int do_messages(_THIS, short *message);
    66 static void do_keyboard(short kc, short ks);
    67 static void do_mouse(_THIS, short mx, short my, short mb, short ks);
    68 
    69 /* Functions */
    70 
    71 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym)
    72 {
    73 	/* Set the keysym information */
    74 	keysym->scancode = scancode;
    75 
    76 	if (asciicode)
    77 		keysym->sym = asciicode;		
    78 	else
    79 		keysym->sym = keymap[scancode];
    80 
    81 	keysym->mod = KMOD_NONE;
    82 	keysym->unicode = 0;
    83 
    84 	return(keysym);
    85 }
    86 
    87 void GEM_InitOSKeymap(_THIS)
    88 {
    89 	int i;
    90 
    91 	memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    92 	memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    93 	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 	memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
   137 	prevkc = prevks = 0;
   138 	
   139 	for (;;)
   140 	{
   141 		int quit, resultat;
   142 		short buffer[8], kc;
   143 
   144 		quit = 0;
   145 
   146 		resultat = evnt_multi(
   147 			MU_MESAG|MU_TIMER|MU_KEYBD,
   148 			0,0,0,
   149 			0,0,0,0,0,
   150 			0,0,0,0,0,
   151 			buffer,
   152 			10,
   153 			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
   154 		);
   155 
   156 		/* Message event ? */
   157 		if (resultat & MU_MESAG)
   158 			quit = do_messages(this, buffer);
   159 
   160 		/* Keyboard event ? */
   161 		if (resultat & MU_KEYBD) {
   162 			if ((prevkc != kc) || (prevks != kstate)) {
   163 				do_keyboard(kc,kstate);
   164 			} else {
   165 				/* Avoid looping, if repeating same key */
   166 				break;
   167 			}
   168 		}
   169 
   170 		/* Timer event ? */
   171 		if ((resultat & MU_TIMER) || quit)
   172 			break;
   173 	}
   174 
   175 	/* Update mouse */
   176 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   177 	do_mouse(this, mousex, mousey, mouseb, kstate);
   178 
   179 	/* Now generate keyboard events */
   180 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   181 		/* Key pressed ? */
   182 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   183 			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, gem_currentascii[i], &keysym));
   184 			
   185 		/* Key unpressed ? */
   186 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   187 			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, gem_currentascii[i], &keysym));
   188 	}
   189 
   190 	memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   191 }
   192 
   193 static int do_messages(_THIS, short *message)
   194 {
   195 	int quit, posted;
   196 	short x2,y2,w2,h2;
   197 
   198 	quit=0;
   199 	switch (message[0]) {
   200 		case WM_CLOSED:
   201 		case AP_TERM:    
   202 			posted = SDL_PrivateQuit();
   203 			quit=1;
   204 			break;
   205 		case WM_MOVED:
   206 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   207 			break;
   208 		case WM_TOPPED:
   209 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   210 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   211 			break;
   212 		case WM_REDRAW:
   213 			GEM_wind_redraw(this, message[3],&message[4]);
   214 			break;
   215 		case WM_ICONIFY:
   216 		case WM_ALLICONIFY:
   217 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   218 			/* If we're active, make ourselves inactive */
   219 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   220 				/* Send an internal deactivate event */
   221 				SDL_PrivateAppActive(0, SDL_APPACTIVE|SDL_APPINPUTFOCUS);
   222 			}
   223 			/* Update window title */
   224 			if (GEM_refresh_name && GEM_icon_name) {
   225 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   226 				GEM_refresh_name = SDL_FALSE;
   227 			}
   228 			break;
   229 		case WM_UNICONIFY:
   230 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   231 			/* If we're not active, make ourselves active */
   232 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   233 				/* Send an internal activate event */
   234 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   235 			}
   236 			if (GEM_refresh_name && GEM_title_name) {
   237 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   238 				GEM_refresh_name = SDL_FALSE;
   239 			}
   240 			break;
   241 		case WM_SIZED:
   242 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   243 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   244 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   245 			SDL_PrivateResize(w2, h2);
   246 			break;
   247 		case WM_FULLED:
   248 			{
   249 				short x,y,w,h;
   250 
   251 				if (GEM_win_fulled) {
   252 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   253 					GEM_win_fulled = SDL_FALSE;
   254 				} else {
   255 					x = GEM_desk_x;
   256 					y = GEM_desk_y;
   257 					w = GEM_desk_w;
   258 					h = GEM_desk_h;
   259 					GEM_win_fulled = SDL_TRUE;
   260 				}
   261 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   262 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   263 				SDL_PrivateResize(w2, h2);
   264 			}
   265 			break;
   266 		case WM_BOTTOMED:
   267 		case WM_UNTOPPED:
   268 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   269 			break;
   270 	}
   271 	
   272 	return quit;
   273 }
   274 
   275 static void do_keyboard(short kc, short ks)
   276 {
   277 	int			scancode, asciicode;
   278 
   279 	if (kc) {
   280 		scancode=(kc>>8) & 127;
   281 		asciicode=kc & 255;
   282 
   283 		gem_currentkeyboard[scancode]=0xFF;
   284 		gem_currentascii[scancode]=asciicode;
   285 	}
   286 
   287 	/* Read special keys */
   288 	if (ks & K_RSHIFT)
   289 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   290 	if (ks & K_LSHIFT)
   291 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   292 	if (ks & K_CTRL)
   293 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   294 	if (ks & K_ALT)
   295 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   296 }
   297 
   298 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
   299 {
   300 	static short prevmousex=0, prevmousey=0, prevmouseb=0;
   301 	short x2, y2, w2, h2;
   302 
   303 	/* Retrieve window coords, and generate mouse events accordingly */
   304 	x2 = y2 = 0;
   305 	w2 = VDI_w;
   306 	h2 = VDI_h;
   307 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   308 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   309 
   310 		/* Do not generate mouse button event if out of window working area */
   311 		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
   312 			mb=prevmouseb;
   313 		}
   314 	}
   315 
   316 	/* Mouse motion ? */
   317 	if ((prevmousex!=mx) || (prevmousey!=my)) {
   318 		if (GEM_mouse_relative) {
   319 			SDL_PrivateMouseMotion(0, 1, SDL_AtariXbios_mousex, SDL_AtariXbios_mousey);
   320 			SDL_AtariXbios_mousex = SDL_AtariXbios_mousey = 0;
   321 		} else {
   322 			int posx, posy;
   323 
   324 			/* Give mouse position relative to window position */
   325 			posx = mx - x2;
   326 			if (posx<0) posx = 0;
   327 			if (posx>w2) posx = w2-1;
   328 			posy = my - y2;
   329 			if (posy<0) posy = 0;
   330 			if (posy>h2) posy = h2-1;
   331 
   332 			SDL_PrivateMouseMotion(0, 0, posx, posy);
   333 		}
   334 		prevmousex = mx;
   335 		prevmousey = my;
   336 	}
   337 
   338 	/* Mouse button ? */
   339 	if (prevmouseb!=mb) {
   340 		int i;
   341 
   342 		for (i=0;i<2;i++) {
   343 			int curbutton, prevbutton;
   344 		
   345 			curbutton = mb & (1<<i);
   346 			prevbutton = prevmouseb & (1<<i);
   347 		
   348 			if (curbutton && !prevbutton) {
   349 				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   350 			}
   351 			if (!curbutton && prevbutton) {
   352 				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   353 			}
   354 		}
   355 		prevmouseb = mb;
   356 	}
   357 
   358 	/* Read special keys */
   359 	if (ks & K_RSHIFT)
   360 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   361 	if (ks & K_LSHIFT)
   362 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   363 	if (ks & K_CTRL)
   364 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   365 	if (ks & K_ALT)
   366 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   367 }