src/video/ataricommon/SDL_biosevents.c
author Sam Lantinga
Thu, 16 Feb 2006 10:11:48 +0000
changeset 1361 19418e4422cb
parent 1358 c71e05b4dc2e
child 1402 d910939febfa
permissions -rw-r--r--
New configure-based build system. Still work in progress, but much improved
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@281
    22
slouken@281
    23
/*
slouken@281
    24
 *	Atari keyboard events manager, using BIOS
slouken@281
    25
 *
slouken@281
    26
 *	Patrice Mandin
slouken@281
    27
 */
slouken@281
    28
slouken@281
    29
/* Mint includes */
slouken@281
    30
#include <mint/osbind.h>
patmandin@1237
    31
#include <mint/cookie.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@305
    38
#include "SDL_xbiosevents_c.h"
slouken@281
    39
slouken@281
    40
/* To save state of keyboard */
slouken@281
    41
#define ATARIBIOS_MAXKEYS 128
slouken@281
    42
slouken@281
    43
static unsigned char bios_currentkeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    44
static unsigned char bios_previouskeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    45
static unsigned char bios_currentascii[ATARIBIOS_MAXKEYS];
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
/* The translation tables from a console scancode to a SDL keysym */
slouken@281
    59
static SDLKey keymap[ATARIBIOS_MAXKEYS];
slouken@281
    60
patmandin@1209
    61
static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
patmandin@1209
    62
	SDL_bool pressed);
slouken@281
    63
static void UpdateSpecialKeys(int special_keys_state);
slouken@281
    64
slouken@281
    65
void AtariBios_InitOSKeymap(_THIS)
slouken@281
    66
{
patmandin@1237
    67
	int i, vectors_mask;
patmandin@1237
    68
	unsigned long dummy;
slouken@281
    69
slouken@1336
    70
	SDL_memset(bios_currentkeyboard, 0, sizeof(bios_currentkeyboard));
slouken@1336
    71
	SDL_memset(bios_previouskeyboard, 0, sizeof(bios_previouskeyboard));
slouken@281
    72
slouken@281
    73
	/* Initialize keymap */
slouken@281
    74
	for ( i=0; i<sizeof(keymap); i++ )
slouken@281
    75
		keymap[i] = SDLK_UNKNOWN;
slouken@281
    76
slouken@281
    77
	/* Functions keys */
slouken@281
    78
	for ( i = 0; i<10; i++ )
slouken@281
    79
		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
slouken@281
    80
slouken@281
    81
	/* Cursor keypad */
slouken@281
    82
	keymap[SCANCODE_HELP] = SDLK_HELP;
slouken@281
    83
	keymap[SCANCODE_UNDO] = SDLK_UNDO;
slouken@281
    84
	keymap[SCANCODE_INSERT] = SDLK_INSERT;
slouken@281
    85
	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
slouken@281
    86
	keymap[SCANCODE_UP] = SDLK_UP;
slouken@281
    87
	keymap[SCANCODE_DOWN] = SDLK_DOWN;
slouken@281
    88
	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
slouken@281
    89
	keymap[SCANCODE_LEFT] = SDLK_LEFT;
slouken@281
    90
slouken@281
    91
	/* Special keys */
slouken@281
    92
	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
slouken@281
    93
	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
slouken@281
    94
	keymap[SCANCODE_TAB] = SDLK_TAB;
slouken@281
    95
	keymap[SCANCODE_ENTER] = SDLK_RETURN;
slouken@281
    96
	keymap[SCANCODE_DELETE] = SDLK_DELETE;
slouken@281
    97
	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
slouken@281
    98
	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
slouken@281
    99
	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
slouken@281
   100
	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
slouken@281
   101
	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
slouken@281
   102
patmandin@1237
   103
	vectors_mask = ATARI_XBIOS_MOUSEEVENTS|ATARI_XBIOS_JOYSTICKEVENTS;
patmandin@1237
   104
	if (Getcookie(C_MiNT, &dummy)==C_FOUND) {
patmandin@1237
   105
		vectors_mask = 0;
patmandin@1237
   106
	}
patmandin@1237
   107
patmandin@1237
   108
	SDL_AtariXbios_InstallVectors(vectors_mask);
slouken@281
   109
}
slouken@281
   110
slouken@281
   111
void AtariBios_PumpEvents(_THIS)
slouken@281
   112
{
slouken@281
   113
	int i;
slouken@281
   114
	SDL_keysym keysym;
slouken@281
   115
slouken@281
   116
	/* Update pressed keys */
slouken@1336
   117
	SDL_memset(bios_currentkeyboard, 0, ATARIBIOS_MAXKEYS);
slouken@281
   118
slouken@281
   119
	while (Bconstat(_CON)) {
slouken@281
   120
		unsigned long key_pressed;
slouken@281
   121
		unsigned char asciicode, scancode;
slouken@281
   122
slouken@281
   123
		key_pressed=Bconin(_CON);
slouken@281
   124
slouken@281
   125
		asciicode = key_pressed;
slouken@281
   126
		scancode = key_pressed >> 16;
slouken@281
   127
slouken@281
   128
		bios_currentkeyboard[scancode]=0xFF;
slouken@281
   129
		bios_currentascii[scancode]=asciicode;
slouken@281
   130
	}
slouken@281
   131
slouken@281
   132
	/* Read special keys */
slouken@281
   133
	UpdateSpecialKeys(Kbshift(-1));
slouken@281
   134
slouken@281
   135
	/* Now generate events */
slouken@281
   136
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   137
		/* Key pressed ? */
slouken@281
   138
		if (bios_currentkeyboard[i] && !bios_previouskeyboard[i])
patmandin@1209
   139
			SDL_PrivateKeyboard(SDL_PRESSED,
patmandin@1209
   140
				TranslateKey(i, bios_currentascii[i], &keysym, SDL_TRUE));
slouken@281
   141
			
slouken@281
   142
		/* Key unpressed ? */
slouken@281
   143
		if (bios_previouskeyboard[i] && !bios_currentkeyboard[i])
patmandin@1209
   144
			SDL_PrivateKeyboard(SDL_RELEASED,
patmandin@1209
   145
				TranslateKey(i, bios_currentascii[i], &keysym, SDL_FALSE));
slouken@281
   146
	}
slouken@281
   147
patmandin@1310
   148
	SDL_AtariXbios_PostMouseEvents(this, SDL_TRUE);
slouken@281
   149
slouken@281
   150
	/* Will be previous table */
slouken@1336
   151
	SDL_memcpy(bios_previouskeyboard, bios_currentkeyboard, ATARIBIOS_MAXKEYS);
slouken@281
   152
}
slouken@281
   153
slouken@281
   154
static void UpdateSpecialKeys(int special_keys_state)
slouken@281
   155
{
slouken@281
   156
#define UPDATE_SPECIAL_KEYS(numbit,scancode) \
slouken@281
   157
	{	\
slouken@281
   158
		if (special_keys_state & (1<<(numbit))) { \
slouken@281
   159
			bios_currentkeyboard[scancode]=0xFF; \
slouken@281
   160
			bios_currentascii[scancode]=0; \
slouken@281
   161
		}	\
slouken@281
   162
	}
slouken@281
   163
slouken@281
   164
	UPDATE_SPECIAL_KEYS(K_RSHIFT, SCANCODE_RIGHTSHIFT);
slouken@281
   165
	UPDATE_SPECIAL_KEYS(K_LSHIFT, SCANCODE_LEFTSHIFT);
slouken@281
   166
	UPDATE_SPECIAL_KEYS(K_CTRL, SCANCODE_LEFTCONTROL);
slouken@281
   167
	UPDATE_SPECIAL_KEYS(K_ALT, SCANCODE_LEFTALT);
slouken@281
   168
	UPDATE_SPECIAL_KEYS(K_CAPSLOCK, SCANCODE_CAPSLOCK);
slouken@281
   169
}
slouken@281
   170
patmandin@1209
   171
static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
patmandin@1209
   172
	SDL_bool pressed)
slouken@281
   173
{
slouken@281
   174
	/* Set the keysym information */
slouken@281
   175
	keysym->scancode = scancode;
slouken@281
   176
slouken@281
   177
	if (asciicode)
slouken@281
   178
		keysym->sym = asciicode;		
slouken@281
   179
	else
slouken@281
   180
		keysym->sym = keymap[scancode];
slouken@281
   181
slouken@281
   182
	keysym->mod = KMOD_NONE;
slouken@281
   183
	keysym->unicode = 0;
patmandin@1221
   184
	if (SDL_TranslateUNICODE && pressed) {
patmandin@1221
   185
		keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
patmandin@1209
   186
	}
slouken@281
   187
slouken@281
   188
	return(keysym);
slouken@281
   189
}
slouken@281
   190
slouken@281
   191
void AtariBios_ShutdownEvents(void)
slouken@281
   192
{
slouken@305
   193
	SDL_AtariXbios_RestoreVectors();
slouken@281
   194
}