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