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