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