src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Fri, 03 Jun 2005 08:18:55 +0000
changeset 1067 f73b199bcd75
parent 1065 5d9947da7510
child 1073 6d3c1134cd19
permissions -rw-r--r--
Better mouse lose/gain focus
slouken@281
     1
/*
slouken@281
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@281
     4
slouken@281
     5
    This library is free software; you can redistribute it and/or
slouken@281
     6
    modify it under the terms of the GNU Library General Public
slouken@281
     7
    License as published by the Free Software Foundation; either
slouken@281
     8
    version 2 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@281
    13
    Library General Public License for more details.
slouken@281
    14
slouken@281
    15
    You should have received a copy of the GNU Library General Public
slouken@281
    16
    License along with this library; if not, write to the Free
slouken@281
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@281
    18
slouken@281
    19
    Sam Lantinga
slouken@281
    20
    slouken@libsdl.org
slouken@281
    21
*/
slouken@281
    22
slouken@281
    23
#ifdef SAVE_RCSID
slouken@281
    24
static char rcsid =
slouken@281
    25
 "@(#) $Id$";
slouken@281
    26
#endif
slouken@281
    27
slouken@281
    28
/*
slouken@281
    29
 * GEM SDL video driver implementation
slouken@281
    30
 * inspired from the Dummy SDL driver
slouken@281
    31
 * 
slouken@281
    32
 * Patrice Mandin
slouken@281
    33
 * and work from
slouken@281
    34
 * Olivier Landemarre, Johan Klockars, Xavier Joubert, Claude Attard
slouken@281
    35
 */
slouken@281
    36
slouken@281
    37
#include <string.h>
slouken@281
    38
slouken@281
    39
#include <gem.h>
slouken@281
    40
slouken@281
    41
#include "SDL.h"
slouken@281
    42
#include "SDL_sysevents.h"
slouken@281
    43
#include "SDL_events_c.h"
slouken@281
    44
#include "SDL_gemvideo.h"
slouken@281
    45
#include "SDL_gemevents_c.h"
slouken@319
    46
#include "SDL_atarikeys.h"	/* for keyboard scancodes */
slouken@319
    47
#include "SDL_xbiosinterrupt_s.h"
slouken@281
    48
slouken@281
    49
/* Defines */
slouken@281
    50
slouken@281
    51
#define ATARIBIOS_MAXKEYS 128
slouken@281
    52
slouken@281
    53
/* Variables */
slouken@281
    54
slouken@281
    55
static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    56
static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    57
static unsigned char gem_currentascii[ATARIBIOS_MAXKEYS];
slouken@281
    58
slouken@281
    59
/* The translation tables from a console scancode to a SDL keysym */
slouken@281
    60
static SDLKey keymap[ATARIBIOS_MAXKEYS];
slouken@281
    61
slouken@281
    62
/* Functions prototypes */
slouken@281
    63
slouken@281
    64
static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym);
slouken@281
    65
static int do_messages(_THIS, short *message);
slouken@281
    66
static void do_keyboard(short kc, short ks);
slouken@319
    67
static void do_mouse(_THIS, short mx, short my, short mb, short ks);
slouken@281
    68
slouken@281
    69
/* Functions */
slouken@281
    70
slouken@281
    71
static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym)
slouken@281
    72
{
slouken@281
    73
	/* Set the keysym information */
slouken@281
    74
	keysym->scancode = scancode;
slouken@281
    75
slouken@281
    76
	if (asciicode)
slouken@281
    77
		keysym->sym = asciicode;		
slouken@281
    78
	else
slouken@281
    79
		keysym->sym = keymap[scancode];
slouken@281
    80
slouken@281
    81
	keysym->mod = KMOD_NONE;
slouken@281
    82
	keysym->unicode = 0;
slouken@281
    83
slouken@281
    84
	return(keysym);
slouken@281
    85
}
slouken@281
    86
slouken@281
    87
void GEM_InitOSKeymap(_THIS)
slouken@281
    88
{
slouken@281
    89
	int i;
slouken@281
    90
slouken@281
    91
	memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
slouken@281
    92
	memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
slouken@281
    93
	memset(gem_currentascii, 0, sizeof(gem_currentascii));
slouken@281
    94
slouken@281
    95
	/* Initialize keymap */
slouken@281
    96
	for ( i=0; i<sizeof(keymap); i++ )
slouken@281
    97
		keymap[i] = SDLK_UNKNOWN;
slouken@281
    98
slouken@281
    99
	/* Functions keys */
slouken@281
   100
	for ( i = 0; i<10; i++ )
slouken@281
   101
		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
slouken@281
   102
slouken@281
   103
	/* Cursor keypad */
slouken@281
   104
	keymap[SCANCODE_HELP] = SDLK_HELP;
slouken@281
   105
	keymap[SCANCODE_UNDO] = SDLK_UNDO;
slouken@281
   106
	keymap[SCANCODE_INSERT] = SDLK_INSERT;
slouken@281
   107
	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
slouken@281
   108
	keymap[SCANCODE_UP] = SDLK_UP;
slouken@281
   109
	keymap[SCANCODE_DOWN] = SDLK_DOWN;
slouken@281
   110
	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
slouken@281
   111
	keymap[SCANCODE_LEFT] = SDLK_LEFT;
slouken@281
   112
slouken@281
   113
	/* Special keys */
slouken@281
   114
	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
slouken@281
   115
	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
slouken@281
   116
	keymap[SCANCODE_TAB] = SDLK_TAB;
slouken@281
   117
	keymap[SCANCODE_ENTER] = SDLK_RETURN;
slouken@281
   118
	keymap[SCANCODE_DELETE] = SDLK_DELETE;
slouken@281
   119
	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
slouken@281
   120
	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
slouken@281
   121
	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
slouken@281
   122
	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
slouken@281
   123
	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
slouken@281
   124
slouken@281
   125
	/* Mouse init */
slouken@281
   126
	GEM_mouse_relative = SDL_FALSE;
slouken@281
   127
}
slouken@281
   128
slouken@281
   129
void GEM_PumpEvents(_THIS)
slouken@281
   130
{
slouken@319
   131
	short mousex, mousey, mouseb, dummy;
slouken@319
   132
	short kstate, prevkc, prevks;
slouken@281
   133
	int i;
slouken@281
   134
	SDL_keysym	keysym;
slouken@281
   135
slouken@281
   136
	memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
slouken@319
   137
	prevkc = prevks = 0;
slouken@281
   138
	
slouken@281
   139
	for (;;)
slouken@281
   140
	{
slouken@281
   141
		int quit, resultat;
slouken@319
   142
		short buffer[8], kc;
patmandin@1067
   143
		short x2,y2,w2,h2;
slouken@281
   144
slouken@281
   145
		quit = 0;
slouken@281
   146
patmandin@1067
   147
		event_mask = MU_MESAG|MU_TIMER|MU_KEYBD;
patmandin@1067
   148
		if (!GEM_fullscreen && (GEM_handle>=0)) {
patmandin@1067
   149
			wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
patmandin@1067
   150
			event_mask |= MU_M1|MU_M2;
patmandin@1067
   151
		} else {
patmandin@1067
   152
			x2=y2=w2=h2=0;
patmandin@1067
   153
		}
patmandin@1067
   154
slouken@281
   155
		resultat = evnt_multi(
patmandin@1067
   156
			event_mask,
slouken@281
   157
			0,0,0,
patmandin@1067
   158
			MO_ENTER,x2,y2,w2,h2,
patmandin@1067
   159
			MO_LEAVE,x2,y2,w2,h2,
slouken@281
   160
			buffer,
slouken@281
   161
			10,
slouken@319
   162
			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
slouken@281
   163
		);
slouken@281
   164
slouken@281
   165
		/* Message event ? */
slouken@281
   166
		if (resultat & MU_MESAG)
slouken@281
   167
			quit = do_messages(this, buffer);
slouken@281
   168
slouken@281
   169
		/* Keyboard event ? */
slouken@319
   170
		if (resultat & MU_KEYBD) {
slouken@319
   171
			if ((prevkc != kc) || (prevks != kstate)) {
slouken@319
   172
				do_keyboard(kc,kstate);
slouken@319
   173
			} else {
slouken@319
   174
				/* Avoid looping, if repeating same key */
slouken@319
   175
				break;
slouken@319
   176
			}
slouken@319
   177
		}
slouken@281
   178
patmandin@1067
   179
		/* Mouse entering/leaving window */
patmandin@1067
   180
		if (resultat & MU_M1) {
patmandin@1067
   181
			if (this->input_grab == SDL_GRAB_OFF) {
patmandin@1067
   182
				SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
patmandin@1067
   183
			}
patmandin@1067
   184
		}
patmandin@1067
   185
		if (resultat & MU_M2) {
patmandin@1067
   186
			if (this->input_grab == SDL_GRAB_OFF) {
patmandin@1067
   187
				SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
patmandin@1067
   188
			}
patmandin@1067
   189
		}
patmandin@1067
   190
slouken@281
   191
		/* Timer event ? */
slouken@281
   192
		if ((resultat & MU_TIMER) || quit)
slouken@281
   193
			break;
slouken@281
   194
	}
slouken@281
   195
slouken@281
   196
	/* Update mouse */
slouken@319
   197
	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
slouken@319
   198
	do_mouse(this, mousex, mousey, mouseb, kstate);
slouken@281
   199
slouken@319
   200
	/* Now generate keyboard events */
slouken@281
   201
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   202
		/* Key pressed ? */
slouken@281
   203
		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
slouken@281
   204
			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, gem_currentascii[i], &keysym));
slouken@281
   205
			
slouken@281
   206
		/* Key unpressed ? */
slouken@281
   207
		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
slouken@285
   208
			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, gem_currentascii[i], &keysym));
slouken@281
   209
	}
slouken@281
   210
slouken@281
   211
	memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
patmandin@996
   212
patmandin@996
   213
	/* Refresh window name ? */
patmandin@996
   214
	if (GEM_refresh_name) {
patmandin@996
   215
		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
patmandin@996
   216
			/* Fullscreen/windowed */
patmandin@996
   217
			if (GEM_title_name) {
patmandin@996
   218
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
patmandin@996
   219
			}
patmandin@996
   220
		} else {
patmandin@996
   221
			/* Iconified */
patmandin@996
   222
			if (GEM_icon_name) {
patmandin@996
   223
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
patmandin@996
   224
			}
patmandin@996
   225
		}
patmandin@996
   226
		GEM_refresh_name = SDL_FALSE;
patmandin@996
   227
	}
slouken@281
   228
}
slouken@281
   229
slouken@281
   230
static int do_messages(_THIS, short *message)
slouken@281
   231
{
slouken@281
   232
	int quit, posted;
patmandin@922
   233
	short x2,y2,w2,h2;
slouken@281
   234
slouken@281
   235
	quit=0;
slouken@281
   236
	switch (message[0]) {
slouken@281
   237
		case WM_CLOSED:
slouken@281
   238
		case AP_TERM:    
slouken@281
   239
			posted = SDL_PrivateQuit();
slouken@281
   240
			quit=1;
slouken@281
   241
			break;
slouken@281
   242
		case WM_MOVED:
slouken@281
   243
			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
slouken@281
   244
			break;
slouken@281
   245
		case WM_TOPPED:
slouken@281
   246
			wind_set(message[3],WF_TOP,message[4],0,0,0);
slouken@281
   247
			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
slouken@281
   248
			break;
slouken@281
   249
		case WM_REDRAW:
patmandin@964
   250
			if (!GEM_lock_redraw) {
patmandin@964
   251
				GEM_wind_redraw(this, message[3],&message[4]);
patmandin@964
   252
			}
slouken@281
   253
			break;
slouken@281
   254
		case WM_ICONIFY:
slouken@281
   255
		case WM_ALLICONIFY:
slouken@281
   256
			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   257
			/* If we're active, make ourselves inactive */
slouken@281
   258
			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
slouken@281
   259
				/* Send an internal deactivate event */
slouken@281
   260
				SDL_PrivateAppActive(0, SDL_APPACTIVE|SDL_APPINPUTFOCUS);
slouken@281
   261
			}
patmandin@736
   262
			/* Update window title */
patmandin@736
   263
			if (GEM_refresh_name && GEM_icon_name) {
patmandin@736
   264
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
patmandin@736
   265
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   266
			}
slouken@281
   267
			break;
slouken@281
   268
		case WM_UNICONIFY:
slouken@281
   269
			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   270
			/* If we're not active, make ourselves active */
slouken@281
   271
			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
slouken@281
   272
				/* Send an internal activate event */
slouken@281
   273
				SDL_PrivateAppActive(1, SDL_APPACTIVE);
slouken@281
   274
			}
patmandin@736
   275
			if (GEM_refresh_name && GEM_title_name) {
patmandin@736
   276
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
patmandin@736
   277
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   278
			}
slouken@281
   279
			break;
slouken@281
   280
		case WM_SIZED:
slouken@281
   281
			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
patmandin@964
   282
			wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
slouken@281
   283
			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
patmandin@964
   284
			GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
patmandin@922
   285
			SDL_PrivateResize(w2, h2);
slouken@281
   286
			break;
slouken@281
   287
		case WM_FULLED:
slouken@281
   288
			{
slouken@281
   289
				short x,y,w,h;
slouken@281
   290
slouken@281
   291
				if (GEM_win_fulled) {
slouken@281
   292
					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
slouken@281
   293
					GEM_win_fulled = SDL_FALSE;
slouken@281
   294
				} else {
slouken@281
   295
					x = GEM_desk_x;
slouken@281
   296
					y = GEM_desk_y;
slouken@281
   297
					w = GEM_desk_w;
slouken@281
   298
					h = GEM_desk_h;
slouken@281
   299
					GEM_win_fulled = SDL_TRUE;
slouken@281
   300
				}
slouken@281
   301
				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
patmandin@922
   302
				wind_get (message[3], WF_WORKXYWH, &x2, &y2, &w2, &h2);
patmandin@964
   303
				GEM_lock_redraw = SDL_TRUE;		/* Prevent redraw till buffers resized */
patmandin@922
   304
				SDL_PrivateResize(w2, h2);
slouken@281
   305
			}
slouken@281
   306
			break;
slouken@281
   307
		case WM_BOTTOMED:
slouken@281
   308
		case WM_UNTOPPED:
slouken@281
   309
			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
slouken@281
   310
			break;
slouken@281
   311
	}
slouken@281
   312
	
slouken@281
   313
	return quit;
slouken@281
   314
}
slouken@281
   315
slouken@281
   316
static void do_keyboard(short kc, short ks)
slouken@281
   317
{
slouken@281
   318
	int			scancode, asciicode;
slouken@281
   319
slouken@281
   320
	if (kc) {
slouken@281
   321
		scancode=(kc>>8) & 127;
slouken@281
   322
		asciicode=kc & 255;
slouken@281
   323
slouken@281
   324
		gem_currentkeyboard[scancode]=0xFF;
slouken@281
   325
		gem_currentascii[scancode]=asciicode;
slouken@281
   326
	}
slouken@281
   327
slouken@281
   328
	/* Read special keys */
slouken@281
   329
	if (ks & K_RSHIFT)
slouken@281
   330
		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
slouken@281
   331
	if (ks & K_LSHIFT)
slouken@281
   332
		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
slouken@281
   333
	if (ks & K_CTRL)
slouken@281
   334
		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
slouken@281
   335
	if (ks & K_ALT)
slouken@281
   336
		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
slouken@281
   337
}
slouken@281
   338
slouken@319
   339
static void do_mouse(_THIS, short mx, short my, short mb, short ks)
slouken@281
   340
{
slouken@319
   341
	static short prevmousex=0, prevmousey=0, prevmouseb=0;
patmandin@926
   342
	short x2, y2, w2, h2;
patmandin@926
   343
patmandin@1067
   344
	/* Don't return mouse events if out of window */
patmandin@1067
   345
	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
patmandin@1067
   346
		return;
patmandin@1067
   347
	}
patmandin@1067
   348
patmandin@926
   349
	/* Retrieve window coords, and generate mouse events accordingly */
patmandin@926
   350
	x2 = y2 = 0;
patmandin@928
   351
	w2 = VDI_w;
patmandin@928
   352
	h2 = VDI_h;
patmandin@926
   353
	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
patmandin@926
   354
		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
patmandin@926
   355
patmandin@926
   356
		/* Do not generate mouse button event if out of window working area */
patmandin@926
   357
		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
patmandin@926
   358
			mb=prevmouseb;
patmandin@926
   359
		}
patmandin@926
   360
	}
slouken@319
   361
slouken@281
   362
	/* Mouse motion ? */
slouken@281
   363
	if ((prevmousex!=mx) || (prevmousey!=my)) {
slouken@281
   364
		if (GEM_mouse_relative) {
slouken@319
   365
			SDL_PrivateMouseMotion(0, 1, SDL_AtariXbios_mousex, SDL_AtariXbios_mousey);
slouken@319
   366
			SDL_AtariXbios_mousex = SDL_AtariXbios_mousey = 0;
slouken@281
   367
		} else {
patmandin@926
   368
			int posx, posy;
patmandin@926
   369
patmandin@926
   370
			/* Give mouse position relative to window position */
patmandin@926
   371
			posx = mx - x2;
patmandin@927
   372
			if (posx<0) posx = 0;
patmandin@926
   373
			if (posx>w2) posx = w2-1;
patmandin@926
   374
			posy = my - y2;
patmandin@927
   375
			if (posy<0) posy = 0;
patmandin@926
   376
			if (posy>h2) posy = h2-1;
patmandin@926
   377
patmandin@926
   378
			SDL_PrivateMouseMotion(0, 0, posx, posy);
slouken@281
   379
		}
slouken@281
   380
		prevmousex = mx;
slouken@281
   381
		prevmousey = my;
slouken@281
   382
	}
slouken@281
   383
slouken@281
   384
	/* Mouse button ? */
slouken@281
   385
	if (prevmouseb!=mb) {
slouken@281
   386
		int i;
slouken@281
   387
slouken@319
   388
		for (i=0;i<2;i++) {
slouken@281
   389
			int curbutton, prevbutton;
slouken@281
   390
		
slouken@281
   391
			curbutton = mb & (1<<i);
slouken@281
   392
			prevbutton = prevmouseb & (1<<i);
slouken@281
   393
		
slouken@319
   394
			if (curbutton && !prevbutton) {
slouken@319
   395
				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
slouken@281
   396
			}
slouken@319
   397
			if (!curbutton && prevbutton) {
slouken@319
   398
				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
slouken@281
   399
			}
slouken@281
   400
		}
slouken@281
   401
		prevmouseb = mb;
slouken@281
   402
	}
slouken@319
   403
slouken@319
   404
	/* Read special keys */
slouken@319
   405
	if (ks & K_RSHIFT)
slouken@319
   406
		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
slouken@319
   407
	if (ks & K_LSHIFT)
slouken@319
   408
		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
slouken@319
   409
	if (ks & K_CTRL)
slouken@319
   410
		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
slouken@319
   411
	if (ks & K_ALT)
slouken@319
   412
		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
slouken@281
   413
}