src/video/gem/SDL_gemevents.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 17 Feb 2002 19:54:28 +0000
changeset 281 c5010ab8ba35
child 285 e5a489f0288c
permissions -rw-r--r--
Added initial support for Atari (thanks Patrice!)
slouken@281
     1
/*
slouken@281
     2
    SDL - Simple DirectMedia Layer
slouken@281
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001  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@281
    46
#include "../ataricommon/SDL_atarikeys.h"	/* for keyboard scancodes */
slouken@281
    47
slouken@281
    48
/* Defines */
slouken@281
    49
slouken@281
    50
#define ATARIBIOS_MAXKEYS 128
slouken@281
    51
slouken@281
    52
/* Variables */
slouken@281
    53
slouken@281
    54
static unsigned char gem_currentkeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    55
static unsigned char gem_previouskeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    56
static unsigned char gem_currentascii[ATARIBIOS_MAXKEYS];
slouken@281
    57
slouken@281
    58
static short prevmousex, prevmousey, prevmouseb;
slouken@281
    59
slouken@281
    60
/* The translation tables from a console scancode to a SDL keysym */
slouken@281
    61
static SDLKey keymap[ATARIBIOS_MAXKEYS];
slouken@281
    62
slouken@281
    63
/* Functions prototypes */
slouken@281
    64
slouken@281
    65
static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym);
slouken@281
    66
static int do_messages(_THIS, short *message);
slouken@281
    67
static void do_keyboard(short kc, short ks);
slouken@281
    68
static void do_mouse(_THIS, short mx, short my, short mb);
slouken@281
    69
slouken@281
    70
/* Functions */
slouken@281
    71
slouken@281
    72
static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym)
slouken@281
    73
{
slouken@281
    74
	/* Set the keysym information */
slouken@281
    75
	keysym->scancode = scancode;
slouken@281
    76
slouken@281
    77
	if (asciicode)
slouken@281
    78
		keysym->sym = asciicode;		
slouken@281
    79
	else
slouken@281
    80
		keysym->sym = keymap[scancode];
slouken@281
    81
slouken@281
    82
	keysym->mod = KMOD_NONE;
slouken@281
    83
	keysym->unicode = 0;
slouken@281
    84
slouken@281
    85
	return(keysym);
slouken@281
    86
}
slouken@281
    87
slouken@281
    88
void GEM_InitOSKeymap(_THIS)
slouken@281
    89
{
slouken@281
    90
	int i;
slouken@281
    91
slouken@281
    92
	memset(gem_currentkeyboard, 0, sizeof(gem_currentkeyboard));
slouken@281
    93
	memset(gem_previouskeyboard, 0, sizeof(gem_previouskeyboard));
slouken@281
    94
	memset(gem_currentascii, 0, sizeof(gem_currentascii));
slouken@281
    95
slouken@281
    96
	/* Initialize keymap */
slouken@281
    97
	for ( i=0; i<sizeof(keymap); i++ )
slouken@281
    98
		keymap[i] = SDLK_UNKNOWN;
slouken@281
    99
slouken@281
   100
	/* Functions keys */
slouken@281
   101
	for ( i = 0; i<10; i++ )
slouken@281
   102
		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
slouken@281
   103
slouken@281
   104
	/* Cursor keypad */
slouken@281
   105
	keymap[SCANCODE_HELP] = SDLK_HELP;
slouken@281
   106
	keymap[SCANCODE_UNDO] = SDLK_UNDO;
slouken@281
   107
	keymap[SCANCODE_INSERT] = SDLK_INSERT;
slouken@281
   108
	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
slouken@281
   109
	keymap[SCANCODE_UP] = SDLK_UP;
slouken@281
   110
	keymap[SCANCODE_DOWN] = SDLK_DOWN;
slouken@281
   111
	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
slouken@281
   112
	keymap[SCANCODE_LEFT] = SDLK_LEFT;
slouken@281
   113
slouken@281
   114
	/* Special keys */
slouken@281
   115
	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
slouken@281
   116
	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
slouken@281
   117
	keymap[SCANCODE_TAB] = SDLK_TAB;
slouken@281
   118
	keymap[SCANCODE_ENTER] = SDLK_RETURN;
slouken@281
   119
	keymap[SCANCODE_DELETE] = SDLK_DELETE;
slouken@281
   120
	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
slouken@281
   121
	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
slouken@281
   122
	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
slouken@281
   123
	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
slouken@281
   124
	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
slouken@281
   125
slouken@281
   126
	/* Mouse init */
slouken@281
   127
	prevmousex = prevmousey = prevmouseb = 0;
slouken@281
   128
	GEM_mouse_relative = SDL_FALSE;
slouken@281
   129
}
slouken@281
   130
slouken@281
   131
void GEM_PumpEvents(_THIS)
slouken@281
   132
{
slouken@281
   133
	short mx, my, mb, dummy;
slouken@281
   134
	int i;
slouken@281
   135
	SDL_keysym	keysym;
slouken@281
   136
slouken@281
   137
	memset(gem_currentkeyboard,0,sizeof(gem_currentkeyboard));
slouken@281
   138
	
slouken@281
   139
	for (;;)
slouken@281
   140
	{
slouken@281
   141
		int quit, resultat;
slouken@281
   142
		short buffer[8], kc, ks;
slouken@281
   143
slouken@281
   144
		quit = 0;
slouken@281
   145
slouken@281
   146
		resultat = evnt_multi(
slouken@281
   147
			MU_MESAG|MU_TIMER|MU_KEYBD,
slouken@281
   148
			0,0,0,
slouken@281
   149
			0,0,0,0,0,
slouken@281
   150
			0,0,0,0,0,
slouken@281
   151
			buffer,
slouken@281
   152
			10,
slouken@281
   153
			&dummy,&dummy,&dummy,&ks,&kc,&dummy
slouken@281
   154
		);
slouken@281
   155
slouken@281
   156
		/* Message event ? */
slouken@281
   157
		if (resultat & MU_MESAG)
slouken@281
   158
			quit = do_messages(this, buffer);
slouken@281
   159
slouken@281
   160
		/* Keyboard event ? */
slouken@281
   161
		if (resultat & MU_KEYBD)
slouken@281
   162
			do_keyboard(kc,ks);
slouken@281
   163
		else
slouken@281
   164
			do_keyboard(0,0);
slouken@281
   165
slouken@281
   166
		/* Timer event ? */
slouken@281
   167
		if ((resultat & MU_TIMER) || quit)
slouken@281
   168
			break;
slouken@281
   169
	}
slouken@281
   170
slouken@281
   171
	/* Update mouse */
slouken@281
   172
	graf_mkstate(&mx, &my, &mb, &dummy);
slouken@281
   173
	do_mouse(this, mx, my, mb);
slouken@281
   174
slouken@281
   175
	/* Now generates keyboard events */
slouken@281
   176
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   177
		/* Key pressed ? */
slouken@281
   178
		if (gem_currentkeyboard[i] && !gem_previouskeyboard[i])
slouken@281
   179
			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, gem_currentascii[i], &keysym));
slouken@281
   180
			
slouken@281
   181
		/* Key unpressed ? */
slouken@281
   182
		if (gem_previouskeyboard[i] && !gem_currentkeyboard[i])
slouken@281
   183
			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, 0, &keysym));
slouken@281
   184
	}
slouken@281
   185
slouken@281
   186
	memcpy(gem_previouskeyboard,gem_currentkeyboard,sizeof(gem_previouskeyboard));
slouken@281
   187
}
slouken@281
   188
slouken@281
   189
static int do_messages(_THIS, short *message)
slouken@281
   190
{
slouken@281
   191
	int quit, posted;
slouken@281
   192
slouken@281
   193
	quit=0;
slouken@281
   194
	switch (message[0]) {
slouken@281
   195
		case WM_CLOSED:
slouken@281
   196
		case AP_TERM:    
slouken@281
   197
			posted = SDL_PrivateQuit();
slouken@281
   198
			quit=1;
slouken@281
   199
			break;
slouken@281
   200
		case WM_MOVED:
slouken@281
   201
			wind_set(message[3],WF_CURRXYWH,message[4],message[5],message[6],message[7]);
slouken@281
   202
			break;
slouken@281
   203
		case WM_TOPPED:
slouken@281
   204
			wind_set(message[3],WF_TOP,message[4],0,0,0);
slouken@281
   205
			SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
slouken@281
   206
			break;
slouken@281
   207
		case WM_REDRAW:
slouken@281
   208
			GEM_wind_redraw(this, message[3],&message[4]);
slouken@281
   209
			break;
slouken@281
   210
		case WM_ICONIFY:
slouken@281
   211
		case WM_ALLICONIFY:
slouken@281
   212
			wind_set(message[3],WF_ICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   213
			/* If we're active, make ourselves inactive */
slouken@281
   214
			if ( SDL_GetAppState() & SDL_APPACTIVE ) {
slouken@281
   215
				/* Send an internal deactivate event */
slouken@281
   216
				SDL_PrivateAppActive(0, SDL_APPACTIVE|SDL_APPINPUTFOCUS);
slouken@281
   217
			}
slouken@281
   218
			break;
slouken@281
   219
		case WM_UNICONIFY:
slouken@281
   220
			wind_set(message[3],WF_UNICONIFY,message[4],message[5],message[6],message[7]);
slouken@281
   221
			/* If we're not active, make ourselves active */
slouken@281
   222
			if ( !(SDL_GetAppState() & SDL_APPACTIVE) ) {
slouken@281
   223
				/* Send an internal activate event */
slouken@281
   224
				SDL_PrivateAppActive(1, SDL_APPACTIVE);
slouken@281
   225
			}
slouken@281
   226
			break;
slouken@281
   227
		case WM_SIZED:
slouken@281
   228
			wind_set (message[3], WF_CURRXYWH, message[4], message[5], message[6], message[7]);
slouken@281
   229
			GEM_win_fulled = SDL_FALSE;		/* Cancel maximized flag */
slouken@281
   230
			SDL_PrivateResize(message[6], message[7]);
slouken@281
   231
			break;
slouken@281
   232
		case WM_FULLED:
slouken@281
   233
			{
slouken@281
   234
				short x,y,w,h;
slouken@281
   235
slouken@281
   236
				if (GEM_win_fulled) {
slouken@281
   237
					wind_get (message[3], WF_PREVXYWH, &x, &y, &w, &h);
slouken@281
   238
					GEM_win_fulled = SDL_FALSE;
slouken@281
   239
				} else {
slouken@281
   240
					x = GEM_desk_x;
slouken@281
   241
					y = GEM_desk_y;
slouken@281
   242
					w = GEM_desk_w;
slouken@281
   243
					h = GEM_desk_h;
slouken@281
   244
					GEM_win_fulled = SDL_TRUE;
slouken@281
   245
				}
slouken@281
   246
				wind_set (message[3], WF_CURRXYWH, x, y, w, h);
slouken@281
   247
				SDL_PrivateResize(w, h);
slouken@281
   248
			}
slouken@281
   249
			break;
slouken@281
   250
		case WM_BOTTOMED:
slouken@281
   251
		case WM_UNTOPPED:
slouken@281
   252
			SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
slouken@281
   253
			break;
slouken@281
   254
	}
slouken@281
   255
	
slouken@281
   256
	return quit;
slouken@281
   257
}
slouken@281
   258
slouken@281
   259
static void do_keyboard(short kc, short ks)
slouken@281
   260
{
slouken@281
   261
	int			scancode, asciicode;
slouken@281
   262
	short		dummy;
slouken@281
   263
slouken@281
   264
	if (kc) {
slouken@281
   265
		scancode=(kc>>8) & 127;
slouken@281
   266
		asciicode=kc & 255;
slouken@281
   267
slouken@281
   268
		gem_currentkeyboard[scancode]=0xFF;
slouken@281
   269
		gem_currentascii[scancode]=asciicode;
slouken@281
   270
	}
slouken@281
   271
slouken@281
   272
	if (!ks)
slouken@281
   273
		graf_mkstate(&dummy, &dummy, &dummy, &ks);
slouken@281
   274
slouken@281
   275
	/* Read special keys */
slouken@281
   276
	if (ks & K_RSHIFT)
slouken@281
   277
		gem_currentkeyboard[SCANCODE_RIGHTSHIFT]=0xFF;
slouken@281
   278
	if (ks & K_LSHIFT)
slouken@281
   279
		gem_currentkeyboard[SCANCODE_LEFTSHIFT]=0xFF;
slouken@281
   280
	if (ks & K_CTRL)
slouken@281
   281
		gem_currentkeyboard[SCANCODE_LEFTCONTROL]=0xFF;
slouken@281
   282
	if (ks & K_ALT)
slouken@281
   283
		gem_currentkeyboard[SCANCODE_LEFTALT]=0xFF;
slouken@281
   284
}
slouken@281
   285
slouken@281
   286
static void do_mouse(_THIS, short mx, short my, short mb)
slouken@281
   287
{
slouken@281
   288
	/* Mouse motion ? */
slouken@281
   289
	if ((prevmousex!=mx) || (prevmousey!=my)) {
slouken@281
   290
		if (GEM_mouse_relative) {
slouken@281
   291
			short wind_pxy[8];
slouken@281
   292
slouken@281
   293
			wind_get(GEM_handle, WF_WORKXYWH, &wind_pxy[0], &wind_pxy[1], &wind_pxy[2], &wind_pxy[3]);
slouken@281
   294
slouken@281
   295
			SDL_PrivateMouseMotion(0, 1, mx-wind_pxy[0], my-wind_pxy[1]);
slouken@281
   296
		} else {
slouken@281
   297
			SDL_PrivateMouseMotion(0, 1, mx, my);
slouken@281
   298
		}
slouken@281
   299
		prevmousex = mx;
slouken@281
   300
		prevmousey = my;
slouken@281
   301
	}
slouken@281
   302
slouken@281
   303
	/* Mouse button ? */
slouken@281
   304
	if (prevmouseb!=mb) {
slouken@281
   305
		int i;
slouken@281
   306
slouken@281
   307
		for (i=0;i<3;i++) {
slouken@281
   308
			int curbutton, prevbutton;
slouken@281
   309
		
slouken@281
   310
			curbutton = mb & (1<<i);
slouken@281
   311
			prevbutton = prevmouseb & (1<<i);
slouken@281
   312
		
slouken@281
   313
			if (curbutton & !prevbutton) {
slouken@281
   314
				SDL_PrivateMouseButton(SDL_PRESSED, i, 0, 0);
slouken@281
   315
			}
slouken@281
   316
			if (!curbutton & prevbutton) {
slouken@281
   317
				SDL_PrivateMouseButton(SDL_RELEASED, i, 0, 0);
slouken@281
   318
			}
slouken@281
   319
		}
slouken@281
   320
		prevmouseb = mb;
slouken@281
   321
	}
slouken@281
   322
}