src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Mon, 28 Jul 2014 11:44:39 +0200
branchSDL-1.2
changeset 9018 4a9c67d11c9f
parent 6649 1aab0b09a0e3
child 9042 89d54976ff74
permissions -rw-r--r--
atari: Do not use system interrupt for handling timer. Do it from userspace, like RISCOS backend.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2012 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 "SDL_gemmouse_c.h"
    40 #include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
    41 #include "../ataricommon/SDL_atarievents_c.h"
    42 #include "../ataricommon/SDL_xbiosevents_c.h"
    43 #include "../ataricommon/SDL_ataridevmouse_c.h"
    44 
    45 /* Variables */
    46 
    47 static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
    48 static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
    49 
    50 static short prevmx=0,prevmy=0,prevmb=0;
    51 
    52 /* Functions prototypes */
    53 
    54 static int do_messages(_THIS, short *message);
    55 static void do_keyboard(short kc);
    56 static void do_keyboard_special(short ks);
    57 static void do_mouse_motion(_THIS, short mx, short my);
    58 static void do_mouse_buttons(_THIS, short mb);
    59 static int mouse_in_work_area(int winhandle, short mx, short my);
    60 
    61 /* Functions */
    62 
    63 void GEM_InitOSKeymap(_THIS)
    64 {
    65 	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
    66 	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
    67 
    68 	/* Mouse init */
    69 	GEM_mouse_relative = SDL_FALSE;
    70 
    71 	SDL_Atari_InitInternalKeymap(this);
    72 }
    73 
    74 void GEM_PumpEvents(_THIS)
    75 {
    76 	short prevkc=0, mousex, mousey, mouseb, kstate;
    77 	int i;
    78 	SDL_keysym keysym;
    79 
    80 	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
    81 
    82 	for (;;)
    83 	{
    84 		int quit, resultat;
    85 		short buffer[8], kc, dummy;
    86 
    87 		quit = 0;
    88 
    89 		SDL_AtariMint_BackgroundTasks();
    90 
    91 		resultat = evnt_multi(
    92 			MU_MESAG|MU_TIMER|MU_KEYBD,
    93 			0,0,0,
    94 			0,0,0,0,0,
    95 			0,0,0,0,0,
    96 			buffer,
    97 			10,
    98 			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
    99 		);
   100 
   101 		/* Message event ? */
   102 		if (resultat & MU_MESAG)
   103 			quit = do_messages(this, buffer);
   104 
   105 		/* Keyboard event ? */
   106 		if (resultat & MU_KEYBD) {
   107 			do_keyboard_special(kstate);
   108 			if (prevkc != kc) {
   109 				do_keyboard(kc);
   110 				prevkc = kc;
   111 			} else {
   112 				/* Avoid looping, if repeating same key */
   113 				quit = 1;
   114 			}
   115 		}
   116 
   117 		/* Timer event ? */
   118 		if ((resultat & MU_TIMER) || quit)
   119 			break;
   120 	}
   121 
   122 	/* Update mouse state */
   123 	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
   124 	do_keyboard_special(kstate);
   125 	do_mouse_motion(this, mousex, mousey);
   126 	do_mouse_buttons(this, mouseb);
   127 
   128 	/* Now generate keyboard events */
   129 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   130 		/* Key pressed ? */
   131 		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
   132 			SDL_PrivateKeyboard(SDL_PRESSED,
   133 				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
   134 			
   135 		/* Key unpressed ? */
   136 		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
   137 			SDL_PrivateKeyboard(SDL_RELEASED,
   138 				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
   139 	}
   140 
   141 	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
   142 
   143 	/* Refresh window name ? */
   144 	if (GEM_refresh_name) {
   145 		const char *window_name =
   146 			(SDL_GetAppState() & SDL_APPACTIVE)
   147 			? GEM_title_name : GEM_icon_name;
   148 		if (window_name) {
   149 			wind_set(GEM_handle,WF_NAME,
   150 				(short)(((unsigned long)window_name)>>16),
   151 				(short)(((unsigned long)window_name) & 0xffff),
   152 				0,0);
   153 		}
   154 		GEM_refresh_name = SDL_FALSE;
   155 	}
   156 }
   157 
   158 static int do_messages(_THIS, short *message)
   159 {
   160 	int quit, check_mouse_mode;
   161 	short x2,y2,w2,h2;
   162 
   163 	quit = check_mouse_mode = 0;
   164 	switch (message[0]) {
   165 		case WM_CLOSED:
   166 		case AP_TERM:    
   167 			SDL_PrivateQuit();
   168 			quit=1;
   169 			break;
   170 		case WM_MOVED:
   171 			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
   172 			break;
   173 		case WM_TOPPED:
   174 			wind_set(message[3],WF_TOP,message[4],0,0,0);
   175 			/* Continue with TOP event processing */
   176 		case WM_ONTOP:
   177 			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
   178 			if (VDI_setpalette) {
   179 				VDI_setpalette(this, VDI_curpalette);
   180 			}
   181 			check_mouse_mode = 1;
   182 			break;
   183 		case WM_REDRAW:
   184 			if (!GEM_lock_redraw) {
   185 				GEM_wind_redraw(this, message[3],&message[4]);
   186 			}
   187 			break;
   188 		case WM_ICONIFY:
   189 		case WM_ALLICONIFY:
   190 			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
   191 			/* If we're active, make ourselves inactive */
   192 			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
   193 				/* Send an internal deactivate event */
   194 				SDL_PrivateAppActive(0, SDL_APPACTIVE);
   195 			}
   196 			/* Update window title */
   197 			if (GEM_refresh_name && GEM_icon_name) {
   198 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
   199 				GEM_refresh_name = SDL_FALSE;
   200 			}
   201 			check_mouse_mode = 1;
   202 			break;
   203 		case WM_UNICONIFY:
   204 			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
   205 			/* If we're not active, make ourselves active */
   206 			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
   207 				/* Send an internal activate event */
   208 				SDL_PrivateAppActive(1, SDL_APPACTIVE);
   209 			}
   210 			if (GEM_refresh_name && GEM_title_name) {
   211 				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
   212 				GEM_refresh_name = SDL_FALSE;
   213 			}
   214 			check_mouse_mode = 1;
   215 			break;
   216 		case WM_SIZED:
   217 			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
   218 			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   219 			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
   220 			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   221 			SDL_PrivateResize(w2, h2);
   222 			break;
   223 		case WM_FULLED:
   224 			{
   225 				short x,y,w,h;
   226 
   227 				if (GEM_win_fulled) {
   228 					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
   229 					GEM_win_fulled = SDL_FALSE;
   230 				} else {
   231 					x = GEM_desk_x;
   232 					y = GEM_desk_y;
   233 					w = GEM_desk_w;
   234 					h = GEM_desk_h;
   235 					GEM_win_fulled = SDL_TRUE;
   236 				}
   237 				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
   238 				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
   239 				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
   240 				SDL_PrivateResize(w2, h2);
   241 			}
   242 			break;
   243 		case WM_BOTTOMED:
   244 			wind_set(message[3],WF_BOTTOM,0,0,0,0);
   245 			/* Continue with BOTTOM event processing */
   246 		case WM_UNTOPPED:
   247 			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
   248 			if (VDI_setpalette) {
   249 				VDI_setpalette(this, VDI_oldpalette);
   250 			}
   251 			check_mouse_mode = 1;
   252 			break;
   253 	}
   254 
   255 	if (check_mouse_mode) {
   256 		GEM_CheckMouseMode(this);
   257 	}
   258 	
   259 	return quit;
   260 }
   261 
   262 static void do_keyboard(short kc)
   263 {
   264 	int scancode;
   265 
   266 	if (kc) {
   267 		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
   268 		gem_currentkeyboard[scancode]=0xFF;
   269 	}
   270 }
   271 
   272 static void do_keyboard_special(short ks)
   273 {
   274 	/* Read special keys */
   275 	if (ks & K_RSHIFT)
   276 		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
   277 	if (ks & K_LSHIFT)
   278 		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
   279 	if (ks & K_CTRL)
   280 		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
   281 	if (ks & K_ALT)
   282 		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
   283 }
   284 
   285 static void do_mouse_motion(_THIS, short mx, short my)
   286 {
   287 	short x2, y2, w2, h2;
   288 
   289 	if (this->input_grab == SDL_GRAB_OFF) {
   290 		/* Switch mouse focus state */
   291 		if (!GEM_fullscreen && (GEM_handle>=0)) {
   292 			SDL_PrivateAppActive(
   293 				mouse_in_work_area(GEM_handle, mx,my),
   294 				SDL_APPMOUSEFOCUS);
   295 		}
   296 	}
   297 	GEM_CheckMouseMode(this);
   298 
   299 	/* Don't return mouse events if out of window */
   300 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
   301 		return;
   302 	}
   303 
   304 	/* Relative mouse motion ? */
   305 	if (GEM_mouse_relative) {
   306 		if (GEM_usedevmouse) {
   307 			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
   308 		} else {
   309 			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
   310 		}
   311 		return;
   312 	}
   313 
   314 	/* Retrieve window coords, and generate mouse events accordingly */
   315 	x2 = y2 = 0;
   316 	w2 = VDI_w;
   317 	h2 = VDI_h;
   318 	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
   319 		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
   320 	}
   321 
   322 	if ((prevmx!=mx) || (prevmy!=my)) {
   323 		int posx, posy;
   324 
   325 		/* Give mouse position relative to window position */
   326 		posx = mx - x2;
   327 		if (posx<0) posx = 0;
   328 		if (posx>w2) posx = w2-1;
   329 		posy = my - y2;
   330 		if (posy<0) posy = 0;
   331 		if (posy>h2) posy = h2-1;
   332 
   333 		SDL_PrivateMouseMotion(0, 0, posx, posy);
   334 	}
   335 
   336 	prevmx = mx;
   337 	prevmy = my;
   338 }
   339 
   340 static void do_mouse_buttons(_THIS, short mb)
   341 {
   342 	int i;
   343 
   344 	/* Don't return mouse events if out of window */
   345 	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0)
   346 		return;
   347 
   348 	if (prevmb==mb)
   349 		return;
   350 
   351 	for (i=0;i<3;i++) {
   352 		int curbutton, prevbutton;
   353 		
   354 		curbutton = mb & (1<<i);
   355 		prevbutton = prevmb & (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 
   365 	prevmb = mb;
   366 }
   367 
   368 /* Check if mouse in visible area of the window */
   369 static int mouse_in_work_area(int winhandle, short mx, short my)
   370 {
   371 	short todo[4];
   372 	short inside[4] = {mx,my,1,1};
   373 
   374 	/* Browse the rectangle list */
   375 	if (wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])!=0) {
   376 		while (todo[2] && todo[3]) {
   377 			if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
   378 				return 1;
   379 			}
   380 
   381 			if (wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])==0) {
   382 				break;
   383 			}
   384 		}
   385 
   386 	}
   387 
   388 	return 0;
   389 }