src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Sun, 04 Nov 2012 19:00:20 +0100
branchSDL-1.2
changeset 6647 42abe340dc14
parent 6585 bc23c3b164fa
child 6648 5415ef59343a
permissions -rw-r--r--
gem: move some variables, and remove duplicates
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@1067
   120
			if (this->input_grab == SDL_GRAB_OFF) {
patmandin@4060
   121
				/* Switch mouse focus state */
patmandin@6585
   122
				if (!GEM_fullscreen && (GEM_handle>=0)) {
patmandin@6585
   123
					SDL_PrivateAppActive(
patmandin@6585
   124
						mouse_in_work_area(GEM_handle, mousex,mousey),
patmandin@6585
   125
						SDL_APPMOUSEFOCUS);
patmandin@6585
   126
				}
patmandin@1067
   127
			}
patmandin@4060
   128
			GEM_CheckMouseMode(this);
patmandin@6585
   129
patmandin@6585
   130
			do_mouse_motion(this, mousex, mousey);
patmandin@6585
   131
			prevmx = mousex;
patmandin@6585
   132
			prevmy = mousey;
patmandin@1067
   133
		}
patmandin@1067
   134
patmandin@4564
   135
		/* Mouse button event ? */
patmandin@4564
   136
		if (resultat & MU_BUTTON) {
patmandin@6583
   137
			do_mouse_buttons(this, mouseb);
patmandin@6583
   138
			prevmb = mouseb & 7;
patmandin@4564
   139
		}
patmandin@4564
   140
slouken@281
   141
		/* Timer event ? */
slouken@281
   142
		if ((resultat & MU_TIMER) || quit)
slouken@281
   143
			break;
slouken@281
   144
	}
slouken@281
   145
slouken@319
   146
	/* Now generate keyboard events */
slouken@281
   147
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   148
		/* Key pressed ? */
slouken@281
   149
		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
patmandin@1209
   150
			SDL_PrivateKeyboard(SDL_PRESSED,
patmandin@3861
   151
				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
slouken@281
   152
			
slouken@281
   153
		/* Key unpressed ? */
slouken@281
   154
		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
patmandin@1209
   155
			SDL_PrivateKeyboard(SDL_RELEASED,
patmandin@3861
   156
				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
slouken@281
   157
	}
slouken@281
   158
slouken@1336
   159
	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
patmandin@996
   160
patmandin@996
   161
	/* Refresh window name ? */
patmandin@996
   162
	if (GEM_refresh_name) {
patmandin@4059
   163
		const char *window_name =
patmandin@4059
   164
			(SDL_GetAppState() & SDL_APPACTIVE)
patmandin@4059
   165
			? GEM_title_name : GEM_icon_name;
patmandin@4059
   166
		if (window_name) {
patmandin@4059
   167
			wind_set(GEM_handle,WF_NAME,
patmandin@4059
   168
				(short)(((unsigned long)window_name)>>16),
patmandin@4059
   169
				(short)(((unsigned long)window_name) & 0xffff),
patmandin@4059
   170
				0,0);
patmandin@996
   171
		}
patmandin@996
   172
		GEM_refresh_name = SDL_FALSE;
patmandin@996
   173
	}
slouken@281
   174
}
slouken@281
   175
slouken@281
   176
static int do_messages(_THIS, short *message)
slouken@281
   177
{
patmandin@5867
   178
	int quit, check_mouse_mode;
patmandin@922
   179
	short x2,y2,w2,h2;
slouken@281
   180
patmandin@4060
   181
	quit = check_mouse_mode = 0;
slouken@281
   182
	switch (message[0]) {
slouken@281
   183
		case WM_CLOSED:
slouken@281
   184
		case AP_TERM:    
patmandin@5867
   185
			SDL_PrivateQuit();
slouken@281
   186
			quit=1;
slouken@281
   187
			break;
slouken@281
   188
		case WM_MOVED:
slouken@281
   189
			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
slouken@281
   190
			break;
slouken@281
   191
		case WM_TOPPED:
slouken@281
   192
			wind_set(message[3],WF_TOP,message[4],0,0,0);
patmandin@1091
   193
			/* Continue with TOP event processing */
patmandin@1091
   194
		case WM_ONTOP:
slouken@281
   195
			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
patmandin@1074
   196
			if (VDI_setpalette) {
patmandin@1074
   197
				VDI_setpalette(this, VDI_curpalette);
patmandin@1074
   198
			}
patmandin@4060
   199
			check_mouse_mode = 1;
slouken@281
   200
			break;
slouken@281
   201
		case WM_REDRAW:
patmandin@964
   202
			if (!GEM_lock_redraw) {
patmandin@964
   203
				GEM_wind_redraw(this, message[3],&message[4]);
patmandin@964
   204
			}
slouken@281
   205
			break;
slouken@281
   206
		case WM_ICONIFY:
slouken@281
   207
		case WM_ALLICONIFY:
slouken@281
   208
			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   209
			/* If we're active, make ourselves inactive */
slouken@281
   210
			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
slouken@281
   211
				/* Send an internal deactivate event */
patmandin@1088
   212
				SDL_PrivateAppActive(0, SDL_APPACTIVE);
slouken@281
   213
			}
patmandin@736
   214
			/* Update window title */
patmandin@736
   215
			if (GEM_refresh_name && GEM_icon_name) {
patmandin@736
   216
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
patmandin@736
   217
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   218
			}
patmandin@4060
   219
			check_mouse_mode = 1;
slouken@281
   220
			break;
slouken@281
   221
		case WM_UNICONIFY:
slouken@281
   222
			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   223
			/* If we're not active, make ourselves active */
slouken@281
   224
			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
slouken@281
   225
				/* Send an internal activate event */
slouken@281
   226
				SDL_PrivateAppActive(1, SDL_APPACTIVE);
slouken@281
   227
			}
patmandin@736
   228
			if (GEM_refresh_name && GEM_title_name) {
patmandin@736
   229
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
patmandin@736
   230
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   231
			}
patmandin@4060
   232
			check_mouse_mode = 1;
slouken@281
   233
			break;
slouken@281
   234
		case WM_SIZED:
slouken@281
   235
			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
patmandin@964
   236
			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
slouken@281
   237
			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
patmandin@964
   238
			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
patmandin@922
   239
			SDL_PrivateResize(w2, h2);
slouken@281
   240
			break;
slouken@281
   241
		case WM_FULLED:
slouken@281
   242
			{
slouken@281
   243
				short x,y,w,h;
slouken@281
   244
slouken@281
   245
				if (GEM_win_fulled) {
slouken@281
   246
					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
slouken@281
   247
					GEM_win_fulled = SDL_FALSE;
slouken@281
   248
				} else {
slouken@281
   249
					x = GEM_desk_x;
slouken@281
   250
					y = GEM_desk_y;
slouken@281
   251
					w = GEM_desk_w;
slouken@281
   252
					h = GEM_desk_h;
slouken@281
   253
					GEM_win_fulled = SDL_TRUE;
slouken@281
   254
				}
slouken@281
   255
				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
patmandin@922
   256
				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
patmandin@964
   257
				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
patmandin@922
   258
				SDL_PrivateResize(w2, h2);
slouken@281
   259
			}
slouken@281
   260
			break;
slouken@281
   261
		case WM_BOTTOMED:
patmandin@1091
   262
			wind_set(message[3],WF_BOTTOM,0,0,0,0);
patmandin@1091
   263
			/* Continue with BOTTOM event processing */
slouken@281
   264
		case WM_UNTOPPED:
slouken@281
   265
			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
patmandin@1074
   266
			if (VDI_setpalette) {
patmandin@1074
   267
				VDI_setpalette(this, VDI_oldpalette);
patmandin@1074
   268
			}
patmandin@4060
   269
			check_mouse_mode = 1;
slouken@281
   270
			break;
slouken@281
   271
	}
patmandin@4060
   272
patmandin@4060
   273
	if (check_mouse_mode) {
patmandin@4060
   274
		GEM_CheckMouseMode(this);
patmandin@4060
   275
	}
slouken@281
   276
	
slouken@281
   277
	return quit;
slouken@281
   278
}
slouken@281
   279
patmandin@6582
   280
static void do_keyboard(short kc)
slouken@281
   281
{
patmandin@3861
   282
	int scancode;
slouken@281
   283
slouken@281
   284
	if (kc) {
patmandin@3861
   285
		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
slouken@281
   286
		gem_currentkeyboard[scancode]=0xFF;
slouken@281
   287
	}
patmandin@6582
   288
}
slouken@281
   289
patmandin@6582
   290
static void do_keyboard_special(short ks)
patmandin@6582
   291
{
slouken@281
   292
	/* Read special keys */
slouken@281
   293
	if (ks & K_RSHIFT)
slouken@281
   294
		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
slouken@281
   295
	if (ks & K_LSHIFT)
slouken@281
   296
		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
slouken@281
   297
	if (ks & K_CTRL)
slouken@281
   298
		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
slouken@281
   299
	if (ks & K_ALT)
slouken@281
   300
		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
slouken@281
   301
}
slouken@281
   302
patmandin@6583
   303
static void do_mouse_motion(_THIS, short mx, short my)
slouken@281
   304
{
patmandin@926
   305
	short x2, y2, w2, h2;
patmandin@926
   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
}