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