src/video/gem/SDL_gemevents.c
author Patrice Mandin <patmandin@gmail.com>
Wed, 13 Sep 2006 21:19:13 +0000
branchSDL-1.2
changeset 3861 c3625a895caf
parent 3854 f1816773a309
child 4059 57b017e6aebf
permissions -rw-r--r--
Use new keyboard mapping routines
slouken@281
     1
/*
slouken@281
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 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@1089
    92
			if ( (SDL_GetAppState() & SDL_APPMOUSEFOCUS) ) {
patmandin@1089
    93
				mouse_event = MO_LEAVE;				
patmandin@1089
    94
			} else {
patmandin@1089
    95
				mouse_event = MO_ENTER;				
patmandin@1089
    96
			}
patmandin@1067
    97
		}
patmandin@1067
    98
slouken@281
    99
		resultat = evnt_multi(
patmandin@1067
   100
			event_mask,
slouken@281
   101
			0,0,0,
patmandin@1089
   102
			mouse_event,x2,y2,w2,h2,
patmandin@1089
   103
			0,0,0,0,0,
slouken@281
   104
			buffer,
slouken@281
   105
			10,
slouken@319
   106
			&dummy,&dummy,&dummy,&kstate,&kc,&dummy
slouken@281
   107
		);
slouken@281
   108
slouken@281
   109
		/* Message event ? */
slouken@281
   110
		if (resultat & MU_MESAG)
slouken@281
   111
			quit = do_messages(this, buffer);
slouken@281
   112
slouken@281
   113
		/* Keyboard event ? */
slouken@319
   114
		if (resultat & MU_KEYBD) {
slouken@319
   115
			if ((prevkc != kc) || (prevks != kstate)) {
slouken@319
   116
				do_keyboard(kc,kstate);
slouken@319
   117
			} else {
slouken@319
   118
				/* Avoid looping, if repeating same key */
slouken@319
   119
				break;
slouken@319
   120
			}
slouken@319
   121
		}
slouken@281
   122
patmandin@1067
   123
		/* Mouse entering/leaving window */
patmandin@1067
   124
		if (resultat & MU_M1) {
patmandin@1067
   125
			if (this->input_grab == SDL_GRAB_OFF) {
patmandin@1089
   126
				if (SDL_GetAppState() & SDL_APPMOUSEFOCUS) {
patmandin@1089
   127
					SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
patmandin@1857
   128
					if (SDL_GetAppState() & SDL_APPINPUTFOCUS) {
patmandin@1857
   129
						graf_mouse(ARROW, NULL);
patmandin@1857
   130
					}
patmandin@1089
   131
				} else {
patmandin@1088
   132
					SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
patmandin@1857
   133
					if (SDL_GetAppState() & SDL_APPINPUTFOCUS) {
patmandin@1857
   134
						if (GEM_cursor == (void *) -1) {
patmandin@1857
   135
							graf_mouse(M_OFF, NULL);
patmandin@1857
   136
						} else if (GEM_cursor) {
patmandin@1857
   137
							graf_mouse(USER_DEF, GEM_cursor->mform_p);
patmandin@1857
   138
						}
patmandin@1857
   139
					}
patmandin@1088
   140
				}
patmandin@1067
   141
			}
patmandin@1067
   142
		}
patmandin@1067
   143
slouken@281
   144
		/* Timer event ? */
slouken@281
   145
		if ((resultat & MU_TIMER) || quit)
slouken@281
   146
			break;
slouken@281
   147
	}
slouken@281
   148
slouken@281
   149
	/* Update mouse */
slouken@319
   150
	graf_mkstate(&mousex, &mousey, &mouseb, &kstate);
slouken@319
   151
	do_mouse(this, mousex, mousey, mouseb, kstate);
slouken@281
   152
slouken@319
   153
	/* Now generate keyboard events */
slouken@281
   154
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   155
		/* Key pressed ? */
slouken@281
   156
		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
patmandin@1209
   157
			SDL_PrivateKeyboard(SDL_PRESSED,
patmandin@3861
   158
				SDL_Atari_TranslateKey(i, &keysym, SDL_TRUE));
slouken@281
   159
			
slouken@281
   160
		/* Key unpressed ? */
slouken@281
   161
		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
patmandin@1209
   162
			SDL_PrivateKeyboard(SDL_RELEASED,
patmandin@3861
   163
				SDL_Atari_TranslateKey(i, &keysym, SDL_FALSE));
slouken@281
   164
	}
slouken@281
   165
slouken@1336
   166
	SDL_memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
patmandin@996
   167
patmandin@996
   168
	/* Refresh window name ? */
patmandin@996
   169
	if (GEM_refresh_name) {
patmandin@996
   170
		if ( SDL_GetAppState() & SDL_APPACTIVE ) {
patmandin@996
   171
			/* Fullscreen/windowed */
patmandin@996
   172
			if (GEM_title_name) {
patmandin@996
   173
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
patmandin@996
   174
			}
patmandin@996
   175
		} else {
patmandin@996
   176
			/* Iconified */
patmandin@996
   177
			if (GEM_icon_name) {
patmandin@996
   178
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
patmandin@996
   179
			}
patmandin@996
   180
		}
patmandin@996
   181
		GEM_refresh_name = SDL_FALSE;
patmandin@996
   182
	}
slouken@281
   183
}
slouken@281
   184
slouken@281
   185
static int do_messages(_THIS, short *message)
slouken@281
   186
{
slouken@281
   187
	int quit, posted;
patmandin@922
   188
	short x2,y2,w2,h2;
slouken@281
   189
slouken@281
   190
	quit=0;
slouken@281
   191
	switch (message[0]) {
slouken@281
   192
		case WM_CLOSED:
slouken@281
   193
		case AP_TERM:    
slouken@281
   194
			posted = SDL_PrivateQuit();
slouken@281
   195
			quit=1;
slouken@281
   196
			break;
slouken@281
   197
		case WM_MOVED:
slouken@281
   198
			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
slouken@281
   199
			break;
slouken@281
   200
		case WM_TOPPED:
slouken@281
   201
			wind_set(message[3],WF_TOP,message[4],0,0,0);
patmandin@1091
   202
			/* Continue with TOP event processing */
patmandin@1091
   203
		case WM_ONTOP:
slouken@281
   204
			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
patmandin@1074
   205
			if (VDI_setpalette) {
patmandin@1074
   206
				VDI_setpalette(this, VDI_curpalette);
patmandin@1074
   207
			}
slouken@281
   208
			break;
slouken@281
   209
		case WM_REDRAW:
patmandin@964
   210
			if (!GEM_lock_redraw) {
patmandin@964
   211
				GEM_wind_redraw(this, message[3],&message[4]);
patmandin@964
   212
			}
slouken@281
   213
			break;
slouken@281
   214
		case WM_ICONIFY:
slouken@281
   215
		case WM_ALLICONIFY:
slouken@281
   216
			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   217
			/* If we're active, make ourselves inactive */
slouken@281
   218
			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
slouken@281
   219
				/* Send an internal deactivate event */
patmandin@1088
   220
				SDL_PrivateAppActive(0, SDL_APPACTIVE);
slouken@281
   221
			}
patmandin@736
   222
			/* Update window title */
patmandin@736
   223
			if (GEM_refresh_name && GEM_icon_name) {
patmandin@736
   224
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_icon_name)>>16),(short)(((unsigned long)GEM_icon_name) & 0xffff),0,0);
patmandin@736
   225
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   226
			}
slouken@281
   227
			break;
slouken@281
   228
		case WM_UNICONIFY:
slouken@281
   229
			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   230
			/* If we're not active, make ourselves active */
slouken@281
   231
			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
slouken@281
   232
				/* Send an internal activate event */
slouken@281
   233
				SDL_PrivateAppActive(1, SDL_APPACTIVE);
slouken@281
   234
			}
patmandin@736
   235
			if (GEM_refresh_name && GEM_title_name) {
patmandin@736
   236
				wind_set(GEM_handle,WF_NAME,(short)(((unsigned long)GEM_title_name)>>16),(short)(((unsigned long)GEM_title_name) & 0xffff),0,0);
patmandin@736
   237
				GEM_refresh_name = SDL_FALSE;
patmandin@736
   238
			}
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
			}
slouken@281
   275
			break;
slouken@281
   276
	}
slouken@281
   277
	
slouken@281
   278
	return quit;
slouken@281
   279
}
slouken@281
   280
slouken@281
   281
static void do_keyboard(short kc, short ks)
slouken@281
   282
{
patmandin@3861
   283
	int scancode;
slouken@281
   284
slouken@281
   285
	if (kc) {
patmandin@3861
   286
		scancode=(kc>>8) & (ATARIBIOS_MAXKEYS-1);
slouken@281
   287
		gem_currentkeyboard[scancode]=0xFF;
slouken@281
   288
	}
slouken@281
   289
slouken@281
   290
	/* Read special keys */
slouken@281
   291
	if (ks & K_RSHIFT)
slouken@281
   292
		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
slouken@281
   293
	if (ks & K_LSHIFT)
slouken@281
   294
		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
slouken@281
   295
	if (ks & K_CTRL)
slouken@281
   296
		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
slouken@281
   297
	if (ks & K_ALT)
slouken@281
   298
		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
slouken@281
   299
}
slouken@281
   300
slouken@319
   301
static void do_mouse(_THIS, short mx, short my, short mb, short ks)
slouken@281
   302
{
slouken@319
   303
	static short prevmousex=0, prevmousey=0, prevmouseb=0;
patmandin@926
   304
	short x2, y2, w2, h2;
patmandin@926
   305
patmandin@1067
   306
	/* Don't return mouse events if out of window */
patmandin@1067
   307
	if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS)==0) {
patmandin@1067
   308
		return;
patmandin@1067
   309
	}
patmandin@1067
   310
patmandin@926
   311
	/* Retrieve window coords, and generate mouse events accordingly */
patmandin@926
   312
	x2 = y2 = 0;
patmandin@928
   313
	w2 = VDI_w;
patmandin@928
   314
	h2 = VDI_h;
patmandin@926
   315
	if ((!GEM_fullscreen) && (GEM_handle>=0)) {
patmandin@926
   316
		wind_get (GEM_handle, WF_WORKXYWH, &x2, &y2, &w2, &h2);
patmandin@926
   317
patmandin@926
   318
		/* Do not generate mouse button event if out of window working area */
patmandin@926
   319
		if ((mx<x2) || (mx>=x2+w2) || (my<y2) || (my>=y2+h2)) {
patmandin@926
   320
			mb=prevmouseb;
patmandin@926
   321
		}
patmandin@926
   322
	}
slouken@319
   323
slouken@281
   324
	/* Mouse motion ? */
patmandin@1267
   325
	if (GEM_mouse_relative) {
patmandin@1420
   326
		if (GEM_usedevmouse) {
patmandin@1420
   327
			SDL_AtariDevMouse_PostMouseEvents(this, SDL_FALSE);
patmandin@1420
   328
		} else {
patmandin@1420
   329
			SDL_AtariXbios_PostMouseEvents(this, SDL_FALSE);
patmandin@1420
   330
		}
patmandin@1267
   331
	} else {
patmandin@1267
   332
		if ((prevmousex!=mx) || (prevmousey!=my)) {
patmandin@926
   333
			int posx, posy;
patmandin@926
   334
patmandin@926
   335
			/* Give mouse position relative to window position */
patmandin@926
   336
			posx = mx - x2;
patmandin@927
   337
			if (posx<0) posx = 0;
patmandin@926
   338
			if (posx>w2) posx = w2-1;
patmandin@926
   339
			posy = my - y2;
patmandin@927
   340
			if (posy<0) posy = 0;
patmandin@926
   341
			if (posy>h2) posy = h2-1;
patmandin@926
   342
patmandin@926
   343
			SDL_PrivateMouseMotion(0, 0, posx, posy);
slouken@281
   344
		}
slouken@281
   345
		prevmousex = mx;
slouken@281
   346
		prevmousey = my;
slouken@281
   347
	}
slouken@281
   348
slouken@281
   349
	/* Mouse button ? */
slouken@281
   350
	if (prevmouseb!=mb) {
slouken@281
   351
		int i;
slouken@281
   352
slouken@319
   353
		for (i=0;i<2;i++) {
slouken@281
   354
			int curbutton, prevbutton;
slouken@281
   355
		
slouken@281
   356
			curbutton = mb & (1<<i);
slouken@281
   357
			prevbutton = prevmouseb & (1<<i);
slouken@281
   358
		
slouken@319
   359
			if (curbutton && !prevbutton) {
slouken@319
   360
				SDL_PrivateMouseButton(SDL_PRESSED, i+1, 0, 0);
slouken@281
   361
			}
slouken@319
   362
			if (!curbutton && prevbutton) {
slouken@319
   363
				SDL_PrivateMouseButton(SDL_RELEASED, i+1, 0, 0);
slouken@281
   364
			}
slouken@281
   365
		}
slouken@281
   366
		prevmouseb = mb;
slouken@281
   367
	}
slouken@319
   368
slouken@319
   369
	/* Read special keys */
slouken@319
   370
	if (ks & K_RSHIFT)
slouken@319
   371
		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
slouken@319
   372
	if (ks & K_LSHIFT)
slouken@319
   373
		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
slouken@319
   374
	if (ks & K_CTRL)
slouken@319
   375
		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
slouken@319
   376
	if (ks & K_ALT)
slouken@319
   377
		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
slouken@281
   378
}