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