src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Fri, 13 Jul 2007 16:09:16 +0000
branchSDL-1.2
changeset 4059 57b017e6aebf
parent 3861 c3625a895caf
child 4060 0b4ebec67cad
permissions -rw-r--r--
Simplify setting window title
     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 #include "SDL_config.h"
    23 
    24 /*
    25  * GEM SDL video driver implementation
    26  * inspired from the Dummy SDL driver
    27  * 
    28  * Patrice Mandin
    29  * and work from
    30  * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
    31  */
    32 
    33 #include <gem.h>
    34 
    35 #include "../../events/SDL_sysevents.h"
    36 #include "../../events/SDL_events_c.h"
    37 #include "SDL_gemvideo.h"
    38 #include "SDL_gemevents_c.h"
    39 #include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
    40 #include "../ataricommon/SDL_atarievents_c.h"
    41 #include "../ataricommon/SDL_xbiosevents_c.h"
    42 #include "../ataricommon/SDL_ataridevmouse_c.h"
    43 
    44 /* Variables */
    45 
    46 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    47 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    48 
    49 /* Functions prototypes */
    50 
    51 static int do_messages(_THIS, short *message);
    52 static void do_keyboard(short kc, short ks);
    53 static void do_mouse(_THIS, short mx, short my, short mb, short ks);
    54 
    55 /* Functions */
    56 
    57 void GEM_InitOSKeymap(_THIS)
    58 {
    59 	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    60 	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    61 
    62 	/* Mouse init */
    63 	GEM_mouse_relative = SDL_FALSE;
    64 
    65 	SDL_Atari_InitInternalKeymap(this);
    66 }
    67 
    68 void GEM_PumpEvents(_THIS)
    69 {
    70 	short mousex, mousey, mouseb, dummy;
    71 	short kstate, prevkc, prevks;
    72 	int i;
    73 	SDL_keysym	keysym;
    74 
    75 	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
    76 	prevkc = prevks = 0;
    77 	
    78 	for (;;)
    79 	{
    80 		int quit, resultat, event_mask, mouse_event;
    81 		short buffer[8], kc;
    82 		short x2,y2,w2,h2;
    83 
    84 		quit =
    85 			mouse_event =
    86 			x2=y2=w2=h2 = 0;
    87 
    88 		event_mask = MU_MESAG|MU_TIMER|MU_KEYBD;
    89 		if (!GEM_fullscreen && (GEM_handle>=0)) {
    90 			wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
    91 			event_mask |= MU_M1;
    92 			if ( (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
    93 				mouse_event = MO_LEAVE;				
    94 			} else {
    95 				mouse_event = MO_ENTER;				
    96 			}
    97 		}
    98 
    99 		resultat = evnt_multi(
   100 			event_mask,
   101 			0,0,0,
   102 			mouse_event,x2,y2,w2,h2,
   103 			0,0,0,0,0,
   104 			buffer,
   105 			10,
   106 			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
   107 		);
   108 
   109 		/* Message event ? */
   110 		if (resultat & MU_MESAG)
   111 			quit = do_messages(this, buffer);
   112 
   113 		/* Keyboard event ? */
   114 		if (resultat & MU_KEYBD) {
   115 			if ((prevkc != kc) || (prevks != kstate)) {
   116 				do_keyboard(kc,kstate);
   117 			} else {
   118 				/* Avoid looping, if repeating same key */
   119 				break;
   120 			}
   121 		}
   122 
   123 		/* Mouse entering/leaving window */
   124 		if (resultat & MU_M1) {
   125 			if (this->input_grab == SDL_GRAB_OFF) {
   126 				if (SDL_GetAppState() & SDL_APPMOUSEFOCUS) {
   127 					SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
   128 					if (SDL_GetAppState() & SDL_APPINPUTFOCUS) {
   129 						graf_mouse(ARROW, NULL);
   130 					}
   131 				} else {
   132 					SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
   133 					if (SDL_GetAppState() & SDL_APPINPUTFOCUS) {
   134 						if (GEM_cursor == (void *) -1) {
   135 							graf_mouse(M_OFF, NULL);
   136 						} else if (GEM_cursor) {
   137 							graf_mouse(USER_DEF, GEM_cursor->mform_p);
   138 						}
   139 					}
   140 				}
   141 			}
   142 		}
   143 
   144 		/* Timer event ? */
   145 		if ((resultat & MU_TIMER) || quit)
   146 			break;
   147 	}
   148 
   149 	/* Update mouse */
   150 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   151 	do_mouse(this, mousex, mousey, mouseb, kstate);
   152 
   153 	/* Now generate keyboard events */
   154 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   155 		/* Key pressed ? */
   156 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   157 			SDL_PrivateKeyboard(SDL_PRESSED,
   158 				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
   159 			
   160 		/* Key unpressed ? */
   161 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   162 			SDL_PrivateKeyboard(SDL_RELEASED,
   163 				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
   164 	}
   165 
   166 	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   167 
   168 	/* Refresh window name ? */
   169 	if (GEM_refresh_name) {
   170 		const char *window_name =
   171 			(SDL_GetAppState() & SDL_APPACTIVE)
   172 			? GEM_title_name : GEM_icon_name;
   173 		if (window_name) {
   174 			wind_set(GEM_handle,WF_NAME,
   175 				(short)(((unsigned long)window_name)>>16),
   176 				(short)(((unsigned long)window_name) & 0xffff),
   177 				0,0);
   178 		}
   179 		GEM_refresh_name = SDL_FALSE;
   180 	}
   181 }
   182 
   183 static int do_messages(_THIS, short *message)
   184 {
   185 	int quit, posted;
   186 	short x2,y2,w2,h2;
   187 
   188 	quit=0;
   189 	switch (message[0]) {
   190 		case WM_CLOSED:
   191 		case AP_TERM:    
   192 			posted = SDL_PrivateQuit();
   193 			quit=1;
   194 			break;
   195 		case WM_MOVED:
   196 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   197 			break;
   198 		case WM_TOPPED:
   199 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   200 			/* Continue with TOP event processing */
   201 		case WM_ONTOP:
   202 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   203 			if (VDI_setpalette) {
   204 				VDI_setpalette(this, VDI_curpalette);
   205 			}
   206 			break;
   207 		case WM_REDRAW:
   208 			if (!GEM_lock_redraw) {
   209 				GEM_wind_redraw(this, message[3],&message[4]);
   210 			}
   211 			break;
   212 		case WM_ICONIFY:
   213 		case WM_ALLICONIFY:
   214 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   215 			/* If we're active, make ourselves inactive */
   216 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   217 				/* Send an internal deactivate event */
   218 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   219 			}
   220 			/* Update window title */
   221 			if (GEM_refresh_name && GEM_icon_name) {
   222 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   223 				GEM_refresh_name = SDL_FALSE;
   224 			}
   225 			break;
   226 		case WM_UNICONIFY:
   227 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   228 			/* If we're not active, make ourselves active */
   229 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   230 				/* Send an internal activate event */
   231 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   232 			}
   233 			if (GEM_refresh_name && GEM_title_name) {
   234 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   235 				GEM_refresh_name = SDL_FALSE;
   236 			}
   237 			break;
   238 		case WM_SIZED:
   239 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   240 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   241 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   242 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   243 			SDL_PrivateResize(w2, h2);
   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 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   261 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   262 				SDL_PrivateResize(w2, h2);
   263 			}
   264 			break;
   265 		case WM_BOTTOMED:
   266 			wind_set(message[3],WF_BOTTOM,0,0,0,0);
   267 			/* Continue with BOTTOM event processing */
   268 		case WM_UNTOPPED:
   269 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   270 			if (VDI_setpalette) {
   271 				VDI_setpalette(this, VDI_oldpalette);
   272 			}
   273 			break;
   274 	}
   275 	
   276 	return quit;
   277 }
   278 
   279 static void do_keyboard(short kc, short ks)
   280 {
   281 	int scancode;
   282 
   283 	if (kc) {
   284 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   285 		gem_currentkeyboard[scancode]=0xFF;
   286 	}
   287 
   288 	/* Read special keys */
   289 	if (ks & K_RSHIFT)
   290 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   291 	if (ks & K_LSHIFT)
   292 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   293 	if (ks & K_CTRL)
   294 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   295 	if (ks & K_ALT)
   296 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   297 }
   298 
   299 static void do_mouse(_THIS, short mx, short my, short mb, short ks)
   300 {
   301 	static short prevmousex=0, prevmousey=0, prevmouseb=0;
   302 	short x2, y2, w2, h2;
   303 
   304 	/* Don't return mouse events if out of window */
   305 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   306 		return;
   307 	}
   308 
   309 	/* Retrieve window coords, and generate mouse events accordingly */
   310 	x2 = y2 = 0;
   311 	w2 = VDI_w;
   312 	h2 = VDI_h;
   313 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   314 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   315 
   316 		/* Do not generate mouse button event if out of window working area */
   317 		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
   318 			mb=prevmouseb;
   319 		}
   320 	}
   321 
   322 	/* Mouse motion ? */
   323 	if (GEM_mouse_relative) {
   324 		if (GEM_usedevmouse) {
   325 			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   326 		} else {
   327 			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   328 		}
   329 	} else {
   330 		if ((prevmousex!=mx) || (prevmousey!=my)) {
   331 			int posx, posy;
   332 
   333 			/* Give mouse position relative to window position */
   334 			posx = mx - x2;
   335 			if (posx<0) posx = 0;
   336 			if (posx>w2) posx = w2-1;
   337 			posy = my - y2;
   338 			if (posy<0) posy = 0;
   339 			if (posy>h2) posy = h2-1;
   340 
   341 			SDL_PrivateMouseMotion(0, 0, posx, posy);
   342 		}
   343 		prevmousex = mx;
   344 		prevmousey = my;
   345 	}
   346 
   347 	/* Mouse button ? */
   348 	if (prevmouseb!=mb) {
   349 		int i;
   350 
   351 		for (i=0;i<2;i++) {
   352 			int curbutton, prevbutton;
   353 		
   354 			curbutton = mb & (1<<i);
   355 			prevbutton = prevmouseb & (1<<i);
   356 		
   357 			if (curbutton && !prevbutton) {
   358 				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
   359 			}
   360 			if (!curbutton && prevbutton) {
   361 				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
   362 			}
   363 		}
   364 		prevmouseb = mb;
   365 	}
   366 
   367 	/* Read special keys */
   368 	if (ks & K_RSHIFT)
   369 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   370 	if (ks & K_LSHIFT)
   371 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   372 	if (ks & K_CTRL)
   373 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   374 	if (ks & K_ALT)
   375 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   376 }