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