src/video/gem/SDL_gemevents.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 285 e5a489f0288c
child 319 189a6a3416c7
permissions -rw-r--r--
Updated copyright information for 2002
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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 "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
    47 
    48 /* Defines */
    49 
    50 #define ATARIBIOS_MAXKEYS 128
    51 
    52 /* Variables */
    53 
    54 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    55 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    56 static unsigned char gem_currentascii[ATARIBIOS_MAXKEYS];
    57 
    58 static short prevmousex, prevmousey, prevmouseb;
    59 
    60 /* The translation tables from a console scancode to a SDL keysym */
    61 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    62 
    63 /* Functions prototypes */
    64 
    65 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym);
    66 static int do_messages(_THIS, short *message);
    67 static void do_keyboard(short kc, short ks);
    68 static void do_mouse(_THIS, short mx, short my, short mb);
    69 
    70 /* Functions */
    71 
    72 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym)
    73 {
    74 	/* Set the keysym information */
    75 	keysym->scancode = scancode;
    76 
    77 	if (asciicode)
    78 		keysym->sym = asciicode;		
    79 	else
    80 		keysym->sym = keymap[scancode];
    81 
    82 	keysym->mod = KMOD_NONE;
    83 	keysym->unicode = 0;
    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 	prevmousex = prevmousey = prevmouseb = 0;
   128 	GEM_mouse_relative = SDL_FALSE;
   129 }
   130 
   131 void GEM_PumpEvents(_THIS)
   132 {
   133 	short mx, my, mb, dummy;
   134 	int i;
   135 	SDL_keysym	keysym;
   136 
   137 	memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
   138 	
   139 	for (;;)
   140 	{
   141 		int quit, resultat;
   142 		short buffer[8], kc, ks;
   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,&ks,&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 			do_keyboard(kc,ks);
   163 		else
   164 			do_keyboard(0,0);
   165 
   166 		/* Timer event ? */
   167 		if ((resultat & MU_TIMER) || quit)
   168 			break;
   169 	}
   170 
   171 	/* Update mouse */
   172 	graf_mkstate(&mx, &my, &mb, &dummy);
   173 	do_mouse(this, mx, my, mb);
   174 
   175 	/* Now generates keyboard events */
   176 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   177 		/* Key pressed ? */
   178 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   179 			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, gem_currentascii[i], &keysym));
   180 			
   181 		/* Key unpressed ? */
   182 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   183 			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, gem_currentascii[i], &keysym));
   184 	}
   185 
   186 	memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   187 }
   188 
   189 static int do_messages(_THIS, short *message)
   190 {
   191 	int quit, posted;
   192 
   193 	quit=0;
   194 	switch (message[0]) {
   195 		case WM_CLOSED:
   196 		case AP_TERM:    
   197 			posted = SDL_PrivateQuit();
   198 			quit=1;
   199 			break;
   200 		case WM_MOVED:
   201 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   202 			break;
   203 		case WM_TOPPED:
   204 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   205 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   206 			break;
   207 		case WM_REDRAW:
   208 			GEM_wind_redraw(this, message[3],&message[4]);
   209 			break;
   210 		case WM_ICONIFY:
   211 		case WM_ALLICONIFY:
   212 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   213 			/* If we're active, make ourselves inactive */
   214 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   215 				/* Send an internal deactivate event */
   216 				SDL_PrivateAppActive(0, SDL_APPACTIVE|SDL_APPINPUTFOCUS);
   217 			}
   218 			break;
   219 		case WM_UNICONIFY:
   220 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   221 			/* If we're not active, make ourselves active */
   222 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   223 				/* Send an internal activate event */
   224 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   225 			}
   226 			break;
   227 		case WM_SIZED:
   228 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   229 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   230 			SDL_PrivateResize(message[6], message[7]);
   231 			break;
   232 		case WM_FULLED:
   233 			{
   234 				short x,y,w,h;
   235 
   236 				if (GEM_win_fulled) {
   237 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   238 					GEM_win_fulled = SDL_FALSE;
   239 				} else {
   240 					x = GEM_desk_x;
   241 					y = GEM_desk_y;
   242 					w = GEM_desk_w;
   243 					h = GEM_desk_h;
   244 					GEM_win_fulled = SDL_TRUE;
   245 				}
   246 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   247 				SDL_PrivateResize(w, h);
   248 			}
   249 			break;
   250 		case WM_BOTTOMED:
   251 		case WM_UNTOPPED:
   252 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   253 			break;
   254 	}
   255 	
   256 	return quit;
   257 }
   258 
   259 static void do_keyboard(short kc, short ks)
   260 {
   261 	int			scancode, asciicode;
   262 	short		dummy;
   263 
   264 	if (kc) {
   265 		scancode=(kc>>8) & 127;
   266 		asciicode=kc & 255;
   267 
   268 		gem_currentkeyboard[scancode]=0xFF;
   269 		gem_currentascii[scancode]=asciicode;
   270 	}
   271 
   272 	if (!ks)
   273 		graf_mkstate(&dummy, &dummy, &dummy, &ks);
   274 
   275 	/* Read special keys */
   276 	if (ks & K_RSHIFT)
   277 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   278 	if (ks & K_LSHIFT)
   279 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   280 	if (ks & K_CTRL)
   281 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   282 	if (ks & K_ALT)
   283 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   284 }
   285 
   286 static void do_mouse(_THIS, short mx, short my, short mb)
   287 {
   288 	/* Mouse motion ? */
   289 	if ((prevmousex!=mx) || (prevmousey!=my)) {
   290 		if (GEM_mouse_relative) {
   291 			short wind_pxy[8];
   292 
   293 			wind_get(GEM_handle, WF_WORKXYWH, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3]);
   294 
   295 			SDL_PrivateMouseMotion(0, 1, mx-wind_pxy[0], my-wind_pxy[1]);
   296 		} else {
   297 			SDL_PrivateMouseMotion(0, 1, mx, my);
   298 		}
   299 		prevmousex = mx;
   300 		prevmousey = my;
   301 	}
   302 
   303 	/* Mouse button ? */
   304 	if (prevmouseb!=mb) {
   305 		int i;
   306 
   307 		for (i=0;i<3;i++) {
   308 			int curbutton, prevbutton;
   309 		
   310 			curbutton = mb & (1<<i);
   311 			prevbutton = prevmouseb & (1<<i);
   312 		
   313 			if (curbutton & !prevbutton) {
   314 				SDL_PrivateMouseButton(SDL_PRESSED, i, 0, 0);
   315 			}
   316 			if (!curbutton & prevbutton) {
   317 				SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0);
   318 			}
   319 		}
   320 		prevmouseb = mb;
   321 	}
   322 }