src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Fri, 12 Oct 2012 18:45:10 +0200
branchSDL-1.2
changeset 6585 bc23c3b164fa
parent 6584 6b04ece9686b
child 6647 42abe340dc14
permissions -rw-r--r--
gem: Handle mouse motion by checking when cursor leave previous position. Add function to check mouse focus using visible window areas.
slouken@281
     1
/*
slouken@281
     2
    SDL - Simple DirectMedia Layer
slouken@6137
     3
    Copyright (C) 1997-2012 Sam Lantinga
slouken@281
     4
slouken@281
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@281
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@281
     9
slouken@281
    10
    This library is distributed in the hope that it will be useful,
slouken@281
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@281
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@281
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@281
    18
slouken@281
    19
    Sam Lantinga
slouken@281
    20
    slouken@libsdl.org
slouken@281
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@281
    23
slouken@281
    24
/*
slouken@281
    25
 * GEM SDL video driver implementation
slouken@281
    26
 * inspired from the Dummy SDL driver
slouken@281
    27
 * 
slouken@281
    28
 * Patrice Mandin
slouken@281
    29
 * and work from
slouken@281
    30
 * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
slouken@281
    31
 */
slouken@281
    32
slouken@281
    33
#include <gem.h>
slouken@281
    34
slouken@1361
    35
#include "../../events/SDL_sysevents.h"
slouken@1361
    36
#include "../../events/SDL_events_c.h"
slouken@281
    37
#include "SDL_gemvideo.h"
slouken@281
    38
#include "SDL_gemevents_c.h"
patmandin@5649
    39
#include "SDL_gemmouse_c.h"
patmandin@1412
    40
#include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
patmandin@1412
    41
#include "../ataricommon/SDL_atarievents_c.h"
patmandin@1412
    42
#include "../ataricommon/SDL_xbiosevents_c.h"
patmandin@1420
    43
#include "../ataricommon/SDL_ataridevmouse_c.h"
slouken@281
    44
slouken@281
    45
/* Variables */
slouken@281
    46
slouken@281
    47
static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    48
static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    49
slouken@281
    50
/* Functions prototypes */
slouken@281
    51
slouken@281
    52
static int do_messages(_THIS, short *message);
patmandin@6582
    53
static void do_keyboard(short kc);
patmandin@6582
    54
static void do_keyboard_special(short ks);
patmandin@6583
    55
static void do_mouse_motion(_THIS, short mx, short my);
patmandin@6583
    56
static void do_mouse_buttons(_THIS, short mb);
patmandin@6585
    57
static int mouse_in_work_area(int winhandle, short mx, short my);
slouken@281
    58
slouken@281
    59
/* Functions */
slouken@281
    60
slouken@281
    61
void GEM_InitOSKeymap(_THIS)
slouken@281
    62
{
slouken@1336
    63
	SDL_memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
slouken@1336
    64
	SDL_memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
slouken@281
    65
slouken@281
    66
	/* Mouse init */
slouken@281
    67
	GEM_mouse_relative = SDL_FALSE;
patmandin@3861
    68
patmandin@3861
    69
	SDL_Atari_InitInternalKeymap(this);
slouken@281
    70
}
slouken@281
    71
slouken@281
    72
void GEM_PumpEvents(_THIS)
slouken@281
    73
{
patmandin@6585
    74
	short prevkc=0;
patmandin@6585
    75
	static short prevmx=0, prevmy=0, prevmb=0;
slouken@281
    76
	int i;
slouken@281
    77
	SDL_keysym	keysym;
slouken@281
    78
slouken@1336
    79
	SDL_memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
patmandin@4564
    80
slouken@281
    81
	for (;;)
slouken@281
    82
	{
patmandin@6585
    83
		int quit, resultat;
slouken@319
    84
		short buffer[8], kc;
patmandin@4564
    85
		short mousex, mousey, mouseb, dummy;
patmandin@4564
    86
		short kstate;
slouken@281
    87
patmandin@6585
    88
		quit = 0;
patmandin@1067
    89
slouken@281
    90
		resultat = evnt_multi(
patmandin@6585
    91
			MU_MESAG|MU_TIMER|MU_KEYBD|MU_BUTTON|MU_M1,
patmandin@6583
    92
			0x101,7,prevmb,
patmandin@6585
    93
			MO_LEAVE,prevmx,prevmy,1,1,
patmandin@1089
    94
			0,0,0,0,0,
slouken@281
    95
			buffer,
slouken@281
    96
			10,
patmandin@4564
    97
			&mousex,&mousey,&mouseb,&kstate,&kc,&dummy
slouken@281
    98
		);
slouken@281
    99
slouken@281
   100
		/* Message event ? */
slouken@281
   101
		if (resultat & MU_MESAG)
slouken@281
   102
			quit = do_messages(this, buffer);
slouken@281
   103
patmandin@6582
   104
		/* Special keys ? */
patmandin@6582
   105
		if (resultat & (MU_KEYBD|MU_BUTTON))
patmandin@6582
   106
			do_keyboard_special(kstate);
patmandin@6582
   107
slouken@281
   108
		/* Keyboard event ? */
slouken@319
   109
		if (resultat & MU_KEYBD) {
patmandin@6582
   110
			if (prevkc != kc) {
patmandin@6582
   111
				do_keyboard(kc);
patmandin@6572
   112
				prevkc = kc;
slouken@319
   113
			} else {
slouken@319
   114
				/* Avoid looping, if repeating same key */
patmandin@6582
   115
				quit = 1;
slouken@319
   116
			}
slouken@319
   117
		}
slouken@281
   118
patmandin@6585
   119
		/* Mouse motion event ? */
patmandin@1067
   120
		if (resultat & MU_M1) {
patmandin@1067
   121
			if (this->input_grab == SDL_GRAB_OFF) {
patmandin@4060
   122
				/* Switch mouse focus state */
patmandin@6585
   123
				if (!GEM_fullscreen && (GEM_handle>=0)) {
patmandin@6585
   124
					SDL_PrivateAppActive(
patmandin@6585
   125
						mouse_in_work_area(GEM_handle, mousex,mousey),
patmandin@6585
   126
						SDL_APPMOUSEFOCUS);
patmandin@6585
   127
				}
patmandin@1067
   128
			}
patmandin@4060
   129
			GEM_CheckMouseMode(this);
patmandin@6585
   130
patmandin@6585
   131
			do_mouse_motion(this, mousex, mousey);
patmandin@6585
   132
			prevmx = mousex;
patmandin@6585
   133
			prevmy = mousey;
patmandin@1067
   134
		}
patmandin@1067
   135
patmandin@4564
   136
		/* Mouse button event ? */
patmandin@4564
   137
		if (resultat & MU_BUTTON) {
patmandin@6583
   138
			do_mouse_buttons(this, mouseb);
patmandin@6583
   139
			prevmb = mouseb & 7;
patmandin@4564
   140
		}
patmandin@4564
   141
slouken@281
   142
		/* Timer event ? */
slouken@281
   143
		if ((resultat & MU_TIMER) || quit)
slouken@281
   144
			break;
slouken@281
   145
	}
slouken@281
   146
slouken@319
   147
	/* Now generate keyboard events */
slouken@281
   148
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   149
		/* Key pressed ? */
slouken@281
   150
		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
patmandin@1209
   151
			SDL_PrivateKeyboard(SDL_PRESSED,
patmandin@3861
   152
				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
slouken@281
   153
			
slouken@281
   154
		/* Key unpressed ? */
slouken@281
   155
		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
patmandin@1209
   156
			SDL_PrivateKeyboard(SDL_RELEASED,
patmandin@3861
   157
				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
slouken@281
   158
	}
slouken@281
   159
slouken@1336
   160
	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
patmandin@996
   161
patmandin@996
   162
	/* Refresh window name ? */
patmandin@996
   163
	if (GEM_refresh_name) {
patmandin@4059
   164
		const char *window_name =
patmandin@4059
   165
			(SDL_GetAppState() & SDL_APPACTIVE)
patmandin@4059
   166
			? GEM_title_name : GEM_icon_name;
patmandin@4059
   167
		if (window_name) {
patmandin@4059
   168
			wind_set(GEM_handle,WF_NAME,
patmandin@4059
   169
				(short)(((unsigned long)window_name)>>16),
patmandin@4059
   170
				(short)(((unsigned long)window_name) & 0xffff),
patmandin@4059
   171
				0,0);
patmandin@996
   172
		}
patmandin@996
   173
		GEM_refresh_name = SDL_FALSE;
patmandin@996
   174
	}
slouken@281
   175
}
slouken@281
   176
slouken@281
   177
static int do_messages(_THIS, short *message)
slouken@281
   178
{
patmandin@5867
   179
	int quit, check_mouse_mode;
patmandin@922
   180
	short x2,y2,w2,h2;
slouken@281
   181
patmandin@4060
   182
	quit = check_mouse_mode = 0;
slouken@281
   183
	switch (message[0]) {
slouken@281
   184
		case WM_CLOSED:
slouken@281
   185
		case AP_TERM:    
patmandin@5867
   186
			SDL_PrivateQuit();
slouken@281
   187
			quit=1;
slouken@281
   188
			break;
slouken@281
   189
		case WM_MOVED:
slouken@281
   190
			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
slouken@281
   191
			break;
slouken@281
   192
		case WM_TOPPED:
slouken@281
   193
			wind_set(message[3],WF_TOP,message[4],0,0,0);
patmandin@1091
   194
			/* Continue with TOP event processing */
patmandin@1091
   195
		case WM_ONTOP:
slouken@281
   196
			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
patmandin@1074
   197
			if (VDI_setpalette) {
patmandin@1074
   198
				VDI_setpalette(this, VDI_curpalette);
patmandin@1074
   199
			}
patmandin@4060
   200
			check_mouse_mode = 1;
slouken@281
   201
			break;
slouken@281
   202
		case WM_REDRAW:
patmandin@964
   203
			if (!GEM_lock_redraw) {
patmandin@964
   204
				GEM_wind_redraw(this, message[3],&message[4]);
patmandin@964
   205
			}
slouken@281
   206
			break;
slouken@281
   207
		case WM_ICONIFY:
slouken@281
   208
		case WM_ALLICONIFY:
slouken@281
   209
			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   210
			/* If we're active, make ourselves inactive */
slouken@281
   211
			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
slouken@281
   212
				/* Send an internal deactivate event */
patmandin@1088
   213
				SDL_PrivateAppActive(0, SDL_APPACTIVE);
slouken@281
   214
			}
patmandin@736
   215
			/* Update window title */
patmandin@736
   216
			if (GEM_refresh_name && GEM_icon_name) {
patmandin@736
   217
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
patmandin@736
   218
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   219
			}
patmandin@4060
   220
			check_mouse_mode = 1;
slouken@281
   221
			break;
slouken@281
   222
		case WM_UNICONIFY:
slouken@281
   223
			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   224
			/* If we're not active, make ourselves active */
slouken@281
   225
			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
slouken@281
   226
				/* Send an internal activate event */
slouken@281
   227
				SDL_PrivateAppActive(1, SDL_APPACTIVE);
slouken@281
   228
			}
patmandin@736
   229
			if (GEM_refresh_name && GEM_title_name) {
patmandin@736
   230
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
patmandin@736
   231
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   232
			}
patmandin@4060
   233
			check_mouse_mode = 1;
slouken@281
   234
			break;
slouken@281
   235
		case WM_SIZED:
slouken@281
   236
			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
patmandin@964
   237
			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
slouken@281
   238
			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
patmandin@964
   239
			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
patmandin@922
   240
			SDL_PrivateResize(w2, h2);
slouken@281
   241
			break;
slouken@281
   242
		case WM_FULLED:
slouken@281
   243
			{
slouken@281
   244
				short x,y,w,h;
slouken@281
   245
slouken@281
   246
				if (GEM_win_fulled) {
slouken@281
   247
					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
slouken@281
   248
					GEM_win_fulled = SDL_FALSE;
slouken@281
   249
				} else {
slouken@281
   250
					x = GEM_desk_x;
slouken@281
   251
					y = GEM_desk_y;
slouken@281
   252
					w = GEM_desk_w;
slouken@281
   253
					h = GEM_desk_h;
slouken@281
   254
					GEM_win_fulled = SDL_TRUE;
slouken@281
   255
				}
slouken@281
   256
				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
patmandin@922
   257
				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
patmandin@964
   258
				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
patmandin@922
   259
				SDL_PrivateResize(w2, h2);
slouken@281
   260
			}
slouken@281
   261
			break;
slouken@281
   262
		case WM_BOTTOMED:
patmandin@1091
   263
			wind_set(message[3],WF_BOTTOM,0,0,0,0);
patmandin@1091
   264
			/* Continue with BOTTOM event processing */
slouken@281
   265
		case WM_UNTOPPED:
slouken@281
   266
			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
patmandin@1074
   267
			if (VDI_setpalette) {
patmandin@1074
   268
				VDI_setpalette(this, VDI_oldpalette);
patmandin@1074
   269
			}
patmandin@4060
   270
			check_mouse_mode = 1;
slouken@281
   271
			break;
slouken@281
   272
	}
patmandin@4060
   273
patmandin@4060
   274
	if (check_mouse_mode) {
patmandin@4060
   275
		GEM_CheckMouseMode(this);
patmandin@4060
   276
	}
slouken@281
   277
	
slouken@281
   278
	return quit;
slouken@281
   279
}
slouken@281
   280
patmandin@6582
   281
static void do_keyboard(short kc)
slouken@281
   282
{
patmandin@3861
   283
	int scancode;
slouken@281
   284
slouken@281
   285
	if (kc) {
patmandin@3861
   286
		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
slouken@281
   287
		gem_currentkeyboard[scancode]=0xFF;
slouken@281
   288
	}
patmandin@6582
   289
}
slouken@281
   290
patmandin@6582
   291
static void do_keyboard_special(short ks)
patmandin@6582
   292
{
slouken@281
   293
	/* Read special keys */
slouken@281
   294
	if (ks & K_RSHIFT)
slouken@281
   295
		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
slouken@281
   296
	if (ks & K_LSHIFT)
slouken@281
   297
		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
slouken@281
   298
	if (ks & K_CTRL)
slouken@281
   299
		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
slouken@281
   300
	if (ks & K_ALT)
slouken@281
   301
		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
slouken@281
   302
}
slouken@281
   303
patmandin@6583
   304
static void do_mouse_motion(_THIS, short mx, short my)
slouken@281
   305
{
patmandin@6583
   306
	static short prevmousex=0, prevmousey=0;
patmandin@926
   307
	short x2, y2, w2, h2;
patmandin@926
   308
patmandin@1067
   309
	/* Don't return mouse events if out of window */
patmandin@1067
   310
	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
patmandin@1067
   311
		return;
patmandin@1067
   312
	}
patmandin@1067
   313
patmandin@6584
   314
	/* Relative mouse motion ? */
patmandin@6584
   315
	if (GEM_mouse_relative) {
patmandin@6584
   316
		if (GEM_usedevmouse) {
patmandin@6584
   317
			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
patmandin@6584
   318
		} else {
patmandin@6584
   319
			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
patmandin@6584
   320
		}
patmandin@6584
   321
		return;
patmandin@6584
   322
	}
patmandin@6584
   323
patmandin@926
   324
	/* Retrieve window coords, and generate mouse events accordingly */
patmandin@926
   325
	x2 = y2 = 0;
patmandin@928
   326
	w2 = VDI_w;
patmandin@928
   327
	h2 = VDI_h;
patmandin@926
   328
	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
patmandin@926
   329
		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
patmandin@926
   330
	}
slouken@319
   331
patmandin@6584
   332
	if ((prevmousex!=mx) || (prevmousey!=my)) {
patmandin@6584
   333
		int posx, posy;
patmandin@926
   334
patmandin@6584
   335
		/* Give mouse position relative to window position */
patmandin@6584
   336
		posx = mx - x2;
patmandin@6584
   337
		if (posx<0) posx = 0;
patmandin@6584
   338
		if (posx>w2) posx = w2-1;
patmandin@6584
   339
		posy = my - y2;
patmandin@6584
   340
		if (posy<0) posy = 0;
patmandin@6584
   341
		if (posy>h2) posy = h2-1;
patmandin@926
   342
patmandin@6584
   343
		SDL_PrivateMouseMotion(0, 0, posx, posy);
slouken@281
   344
	}
patmandin@6584
   345
	prevmousex = mx;
patmandin@6584
   346
	prevmousey = my;
patmandin@6583
   347
}
slouken@281
   348
patmandin@6583
   349
static void do_mouse_buttons(_THIS, short mb)
patmandin@6583
   350
{
patmandin@6583
   351
	static short prevmouseb=0;
patmandin@6583
   352
	int i;
slouken@281
   353
patmandin@6583
   354
	/* Don't return mouse events if out of window */
patmandin@6583
   355
	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0)
patmandin@6583
   356
		return;
patmandin@6583
   357
patmandin@6583
   358
	if (prevmouseb==mb)
patmandin@6583
   359
		return;
patmandin@6583
   360
patmandin@6583
   361
	for (i=0;i<3;i++) {
patmandin@6583
   362
		int curbutton, prevbutton;
slouken@281
   363
		
patmandin@6583
   364
		curbutton = mb & (1<<i);
patmandin@6583
   365
		prevbutton = prevmouseb & (1<<i);
patmandin@6583
   366
	
patmandin@6583
   367
		if (curbutton && !prevbutton) {
patmandin@6583
   368
			SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
slouken@281
   369
		}
patmandin@6583
   370
		if (!curbutton && prevbutton) {
patmandin@6583
   371
			SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
patmandin@6583
   372
		}
slouken@281
   373
	}
patmandin@6583
   374
patmandin@6583
   375
	prevmouseb = mb;
slouken@281
   376
}
patmandin@6585
   377
patmandin@6585
   378
/* Check if mouse in visible area of the window */
patmandin@6585
   379
static int mouse_in_work_area(int winhandle, short mx, short my)
patmandin@6585
   380
{
patmandin@6585
   381
	short todo[4];
patmandin@6585
   382
	short inside[4] = {mx,my,1,1};
patmandin@6585
   383
patmandin@6585
   384
	/* Browse the rectangle list */
patmandin@6585
   385
	if (wind_get(winhandle, WF_FIRSTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])!=0) {
patmandin@6585
   386
		while (todo[2] && todo[3]) {
patmandin@6585
   387
			if (rc_intersect((GRECT *)inside,(GRECT *)todo)) {
patmandin@6585
   388
				return 1;
patmandin@6585
   389
			}
patmandin@6585
   390
patmandin@6585
   391
			if (wind_get(winhandle, WF_NEXTXYWH, &todo[0], &todo[1], &todo[2], &todo[3])==0) {
patmandin@6585
   392
				break;
patmandin@6585
   393
			}
patmandin@6585
   394
		}
patmandin@6585
   395
patmandin@6585
   396
	}
patmandin@6585
   397
patmandin@6585
   398
	return 0;
patmandin@6585
   399
}