src/video/gem/SDL_gemevents.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2004 16:49:27 +0000
changeset 769 b8d311d90021
parent 736 028e03e273c8
child 919 4b1dd7691f05
permissions -rw-r--r--
Updated copyright information for 2004 (Happy New Year!)
     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 
   197 	quit=0;
   198 	switch (message[0]) {
   199 		case WM_CLOSED:
   200 		case AP_TERM:    
   201 			posted = SDL_PrivateQuit();
   202 			quit=1;
   203 			break;
   204 		case WM_MOVED:
   205 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   206 			break;
   207 		case WM_TOPPED:
   208 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   209 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   210 			break;
   211 		case WM_REDRAW:
   212 			GEM_wind_redraw(this, message[3],&message[4]);
   213 			break;
   214 		case WM_ICONIFY:
   215 		case WM_ALLICONIFY:
   216 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   217 			/* If we're active, make ourselves inactive */
   218 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   219 				/* Send an internal deactivate event */
   220 				SDL_PrivateAppActive(0, SDL_APPACTIVE|SDL_APPINPUTFOCUS);
   221 			}
   222 			/* Update window title */
   223 			if (GEM_refresh_name && GEM_icon_name) {
   224 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   225 				GEM_refresh_name = SDL_FALSE;
   226 			}
   227 			break;
   228 		case WM_UNICONIFY:
   229 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   230 			/* If we're not active, make ourselves active */
   231 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   232 				/* Send an internal activate event */
   233 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   234 			}
   235 			if (GEM_refresh_name && GEM_title_name) {
   236 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   237 				GEM_refresh_name = SDL_FALSE;
   238 			}
   239 			break;
   240 		case WM_SIZED:
   241 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   242 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   243 			SDL_PrivateResize(message[6], message[7]);
   244 			break;
   245 		case WM_FULLED:
   246 			{
   247 				short x,y,w,h;
   248 
   249 				if (GEM_win_fulled) {
   250 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   251 					GEM_win_fulled = SDL_FALSE;
   252 				} else {
   253 					x = GEM_desk_x;
   254 					y = GEM_desk_y;
   255 					w = GEM_desk_w;
   256 					h = GEM_desk_h;
   257 					GEM_win_fulled = SDL_TRUE;
   258 				}
   259 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   260 				SDL_PrivateResize(w, h);
   261 			}
   262 			break;
   263 		case WM_BOTTOMED:
   264 		case WM_UNTOPPED:
   265 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   266 			break;
   267 	}
   268 	
   269 	return quit;
   270 }
   271 
   272 static void do_keyboard(short kc, short ks)
   273 {
   274 	int			scancode, asciicode;
   275 
   276 	if (kc) {
   277 		scancode=(kc>>8) & 127;
   278 		asciicode=kc & 255;
   279 
   280 		gem_currentkeyboard[scancode]=0xFF;
   281 		gem_currentascii[scancode]=asciicode;
   282 	}
   283 
   284 	/* Read special keys */
   285 	if (ks & K_RSHIFT)
   286 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   287 	if (ks & K_LSHIFT)
   288 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   289 	if (ks & K_CTRL)
   290 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   291 	if (ks & K_ALT)
   292 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   293 }
   294 
   295 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
   296 {
   297 	static short prevmousex=0, prevmousey=0, prevmouseb=0;
   298 
   299 	/* Mouse motion ? */
   300 	if ((prevmousex!=mx) || (prevmousey!=my)) {
   301 		if (GEM_mouse_relative) {
   302 			SDL_PrivateMouseMotion(0, 1, SDL_AtariXbios_mousex, SDL_AtariXbios_mousey);
   303 			SDL_AtariXbios_mousex = SDL_AtariXbios_mousey = 0;
   304 		} else {
   305 			SDL_PrivateMouseMotion(0, 1, mx, my);
   306 		}
   307 		prevmousex = mx;
   308 		prevmousey = my;
   309 	}
   310 
   311 	/* Mouse button ? */
   312 	if (prevmouseb!=mb) {
   313 		int i;
   314 
   315 		for (i=0;i<2;i++) {
   316 			int curbutton, prevbutton;
   317 		
   318 			curbutton = mb & (1<<i);
   319 			prevbutton = prevmouseb & (1<<i);
   320 		
   321 			if (curbutton && !prevbutton) {
   322 				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   323 			}
   324 			if (!curbutton && prevbutton) {
   325 				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   326 			}
   327 		}
   328 		prevmouseb = mb;
   329 	}
   330 
   331 	/* Read special keys */
   332 	if (ks & K_RSHIFT)
   333 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   334 	if (ks & K_LSHIFT)
   335 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   336 	if (ks & K_CTRL)
   337 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   338 	if (ks & K_ALT)
   339 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   340 }