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