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