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