src/video/ataricommon/SDL_ikbdevents.c
author Patrice Mandin
Mon, 02 Jan 2006 19:49:21 +0000
changeset 1221 8ef3e7e92a91
parent 1209 a55ac374271c
child 1312 c9b51268668f
permissions -rw-r--r--
Use a simple table for Unicode translation
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
 *	Atari keyboard events manager, using hardware IKBD
slouken@281
    30
 *
slouken@281
    31
 *	Patrice Mandin
slouken@281
    32
 */
slouken@281
    33
slouken@281
    34
#include <string.h>
slouken@281
    35
slouken@281
    36
/* Mint includes */
slouken@281
    37
#include <mint/osbind.h>
slouken@281
    38
slouken@281
    39
#include "SDL.h"
slouken@281
    40
#include "SDL_sysevents.h"
slouken@281
    41
#include "SDL_events_c.h"
slouken@281
    42
slouken@281
    43
#include "SDL_atarikeys.h"
patmandin@1209
    44
#include "SDL_atarievents_c.h"
slouken@281
    45
#include "SDL_ikbdinterrupt_s.h"
slouken@281
    46
slouken@281
    47
/* Special keys state */
slouken@281
    48
enum {
slouken@281
    49
	K_RSHIFT=0,
slouken@281
    50
	K_LSHIFT,
slouken@281
    51
	K_CTRL,
slouken@281
    52
	K_ALT,
slouken@281
    53
	K_CAPSLOCK,
slouken@281
    54
	K_CLRHOME,
slouken@281
    55
	K_INSERT
slouken@281
    56
};
slouken@281
    57
slouken@281
    58
#define ATARIBIOS_MAXKEYS 128
slouken@281
    59
patmandin@1082
    60
#define KEY_PRESSED		0xff
patmandin@1082
    61
#define KEY_UNDEFINED	0x80
patmandin@1082
    62
#define KEY_RELEASED	0x00
slouken@281
    63
slouken@281
    64
/* The translation tables from a console scancode to a SDL keysym */
slouken@281
    65
#define KT_NOCHANGE -1
slouken@281
    66
slouken@281
    67
enum {
slouken@281
    68
	KT_UNSHIFT=0,
slouken@281
    69
	KT_SHIFT=1,
slouken@281
    70
	KT_CAPS=2
slouken@281
    71
};
slouken@281
    72
patmandin@1082
    73
static Uint16 atari_prevmouseb;	/* save state of mouse buttons */
patmandin@1082
    74
static int caps_state;			/* caps lock state */
slouken@281
    75
_KEYTAB *curtables;
slouken@281
    76
static unsigned char *tab_unshift, *tab_shift, *tab_caps;
slouken@281
    77
static SDLKey keymap[ATARIBIOS_MAXKEYS];
slouken@281
    78
patmandin@1209
    79
static SDL_keysym *TranslateKey(int scancode, int numkeytable, SDL_keysym *keysym,
patmandin@1209
    80
	SDL_bool pressed);
slouken@281
    81
slouken@281
    82
void AtariIkbd_InitOSKeymap(_THIS)
slouken@281
    83
{
slouken@281
    84
	int i;
slouken@281
    85
patmandin@1082
    86
	memset(SDL_AtariIkbd_keyboard, KEY_UNDEFINED, ATARIBIOS_MAXKEYS);
slouken@281
    87
slouken@281
    88
	/* Initialize keymap */
slouken@281
    89
	for ( i=0; i<sizeof(keymap); i++ )
slouken@281
    90
		keymap[i] = SDLK_UNKNOWN;
slouken@281
    91
slouken@281
    92
	/* Functions keys */
slouken@281
    93
	for ( i = 0; i<10; i++ )
slouken@281
    94
		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
slouken@281
    95
slouken@281
    96
	/* Cursor keypad */
slouken@281
    97
	keymap[SCANCODE_HELP] = SDLK_HELP;
slouken@281
    98
	keymap[SCANCODE_UNDO] = SDLK_UNDO;
slouken@281
    99
	keymap[SCANCODE_INSERT] = SDLK_INSERT;
slouken@281
   100
	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
slouken@281
   101
	keymap[SCANCODE_UP] = SDLK_UP;
slouken@281
   102
	keymap[SCANCODE_DOWN] = SDLK_DOWN;
slouken@281
   103
	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
slouken@281
   104
	keymap[SCANCODE_LEFT] = SDLK_LEFT;
slouken@281
   105
slouken@281
   106
	/* Special keys */
slouken@281
   107
	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
slouken@281
   108
	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
slouken@281
   109
	keymap[SCANCODE_TAB] = SDLK_TAB;
slouken@281
   110
	keymap[SCANCODE_ENTER] = SDLK_RETURN;
slouken@281
   111
	keymap[SCANCODE_DELETE] = SDLK_DELETE;
slouken@281
   112
	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
slouken@281
   113
	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
slouken@281
   114
	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
slouken@281
   115
	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
slouken@281
   116
	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
slouken@281
   117
slouken@281
   118
	/* Read XBIOS tables for scancode -> ascii translation */
slouken@281
   119
	curtables=Keytbl(KT_NOCHANGE, KT_NOCHANGE, KT_NOCHANGE);
slouken@281
   120
	tab_unshift=curtables->unshift;
slouken@281
   121
	tab_shift=curtables->shift;
slouken@281
   122
	tab_caps=curtables->caps;
slouken@281
   123
slouken@281
   124
	/* Set Caps lock initial state */
slouken@281
   125
	caps_state=(Kbshift(-1) & (1<<K_CAPSLOCK));
slouken@281
   126
slouken@281
   127
	/* Now install our handler */
slouken@281
   128
	SDL_AtariIkbd_mouseb = SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
slouken@281
   129
	atari_prevmouseb = 0;
slouken@281
   130
slouken@281
   131
	Supexec(SDL_AtariIkbdInstall);
slouken@281
   132
}
slouken@281
   133
slouken@281
   134
static int atari_GetButton(int button)
slouken@281
   135
{
slouken@281
   136
	switch(button)
slouken@281
   137
	{
slouken@281
   138
		case 0:
slouken@281
   139
			return SDL_BUTTON_RIGHT;
slouken@281
   140
			break;
slouken@281
   141
		case 1:
slouken@281
   142
		default:
slouken@281
   143
			return SDL_BUTTON_LEFT;
slouken@281
   144
			break;
slouken@281
   145
	}
slouken@281
   146
}
slouken@281
   147
slouken@281
   148
void AtariIkbd_PumpEvents(_THIS)
slouken@281
   149
{
slouken@281
   150
	int i;
slouken@281
   151
	SDL_keysym keysym;
slouken@281
   152
	int specialkeys;
slouken@281
   153
slouken@281
   154
	/*--- Send keyboard events ---*/
slouken@281
   155
slouken@281
   156
	/* Update caps lock state */
patmandin@1082
   157
	if (SDL_AtariIkbd_keyboard[SCANCODE_CAPSLOCK]==KEY_PRESSED) {
slouken@281
   158
		caps_state ^= 1;
patmandin@1082
   159
	}
slouken@281
   160
slouken@281
   161
	/* Choose the translation table */
slouken@281
   162
	specialkeys=KT_UNSHIFT;
patmandin@1082
   163
	if ((SDL_AtariIkbd_keyboard[SCANCODE_LEFTSHIFT]==KEY_PRESSED)
patmandin@1082
   164
		|| (SDL_AtariIkbd_keyboard[SCANCODE_RIGHTSHIFT]==KEY_PRESSED))
patmandin@1082
   165
	{
slouken@281
   166
		specialkeys = KT_SHIFT;
patmandin@1082
   167
	}
patmandin@1082
   168
	if (caps_state) {
slouken@281
   169
		specialkeys = KT_CAPS;
patmandin@1082
   170
	}
slouken@281
   171
slouken@281
   172
	/* Now generate events */
slouken@281
   173
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   174
		/* Key pressed ? */
patmandin@1082
   175
		if (SDL_AtariIkbd_keyboard[i]==KEY_PRESSED) {
patmandin@1209
   176
			SDL_PrivateKeyboard(SDL_PRESSED,
patmandin@1209
   177
				TranslateKey(i, specialkeys, &keysym, SDL_TRUE));
patmandin@1082
   178
			SDL_AtariIkbd_keyboard[i]=KEY_UNDEFINED;
patmandin@1082
   179
		}
slouken@281
   180
			
patmandin@1082
   181
		/* Key released ? */
patmandin@1082
   182
		if (SDL_AtariIkbd_keyboard[i]==KEY_RELEASED) {
patmandin@1209
   183
			SDL_PrivateKeyboard(SDL_RELEASED,
patmandin@1209
   184
				TranslateKey(i, specialkeys, &keysym, SDL_FALSE));
patmandin@1082
   185
			SDL_AtariIkbd_keyboard[i]=KEY_UNDEFINED;
patmandin@1082
   186
		}
slouken@281
   187
	}
slouken@281
   188
slouken@281
   189
	/*--- Send mouse events ---*/
slouken@281
   190
slouken@281
   191
	/* Mouse motion ? */
slouken@281
   192
	if (SDL_AtariIkbd_mousex || SDL_AtariIkbd_mousey) {
slouken@281
   193
		SDL_PrivateMouseMotion(0, 1, SDL_AtariIkbd_mousex, SDL_AtariIkbd_mousey);
slouken@281
   194
		SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
slouken@281
   195
	}
slouken@281
   196
slouken@281
   197
	/* Mouse button ? */
slouken@281
   198
	if (SDL_AtariIkbd_mouseb != atari_prevmouseb) {
slouken@281
   199
		for (i=0;i<2;i++) {
slouken@281
   200
			int curbutton, prevbutton;
slouken@281
   201
slouken@281
   202
			curbutton = SDL_AtariIkbd_mouseb & (1<<i);
slouken@281
   203
			prevbutton = atari_prevmouseb & (1<<i);
slouken@281
   204
slouken@331
   205
			if (curbutton && !prevbutton) {
slouken@281
   206
				SDL_PrivateMouseButton(SDL_PRESSED, atari_GetButton(i), 0, 0);
slouken@281
   207
			}
slouken@331
   208
			if (!curbutton && prevbutton) {
slouken@281
   209
				SDL_PrivateMouseButton(SDL_RELEASED, atari_GetButton(i), 0, 0);
slouken@281
   210
			}
slouken@281
   211
		}
slouken@281
   212
		atari_prevmouseb = SDL_AtariIkbd_mouseb;
slouken@281
   213
	}
slouken@281
   214
}
slouken@281
   215
patmandin@1209
   216
static SDL_keysym *TranslateKey(int scancode, int numkeytable, SDL_keysym *keysym,
patmandin@1209
   217
	SDL_bool pressed)
slouken@281
   218
{
slouken@281
   219
	unsigned char asciicode;
slouken@281
   220
slouken@281
   221
	/* Set the keysym information */
slouken@281
   222
	keysym->scancode = scancode;
slouken@281
   223
slouken@281
   224
	asciicode=0;
slouken@281
   225
	switch(numkeytable) {
slouken@281
   226
		case KT_UNSHIFT:
slouken@281
   227
			asciicode=tab_unshift[scancode];
slouken@281
   228
			break;
slouken@281
   229
		case KT_SHIFT:
slouken@281
   230
			asciicode=tab_shift[scancode];
slouken@281
   231
			break;
slouken@281
   232
		case KT_CAPS:
slouken@281
   233
			asciicode=tab_caps[scancode];
slouken@281
   234
			break;
slouken@281
   235
	}
slouken@281
   236
slouken@281
   237
	if (asciicode)
slouken@281
   238
		keysym->sym = asciicode;		
slouken@281
   239
	else
slouken@281
   240
		keysym->sym = keymap[scancode];
slouken@281
   241
slouken@281
   242
	keysym->mod = KMOD_NONE;
slouken@281
   243
	keysym->unicode = 0;
patmandin@1221
   244
	if (SDL_TranslateUNICODE && pressed) {
patmandin@1221
   245
		keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
patmandin@1209
   246
	}
slouken@281
   247
slouken@281
   248
	return(keysym);
slouken@281
   249
}
slouken@281
   250
slouken@281
   251
void AtariIkbd_ShutdownEvents(void)
slouken@281
   252
{
slouken@281
   253
	Supexec(SDL_AtariIkbdUninstall);
slouken@281
   254
}