src/video/ataricommon/SDL_gemdosevents.c
author Patrice Mandin
Sun, 30 Apr 2006 21:01:25 +0000
changeset 1751 a11c13151840
parent 1420 2405517b5eab
child 1662 782fd950bd46
child 1895 c121d94672cb
child 3854 f1816773a309
permissions -rw-r--r--
Declare volatile variables modified by interrupt 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
 *	Atari keyboard events manager, using Gemdos
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>
patmandin@1237
    32
#include <mint/cookie.h>
slouken@281
    33
slouken@1361
    34
#include "../../events/SDL_sysevents.h"
slouken@1361
    35
#include "../../events/SDL_events_c.h"
slouken@281
    36
slouken@281
    37
#include "SDL_atarikeys.h"
patmandin@1209
    38
#include "SDL_atarievents_c.h"
slouken@305
    39
#include "SDL_xbiosevents_c.h"
patmandin@1420
    40
#include "SDL_ataridevmouse_c.h"
slouken@281
    41
slouken@281
    42
/* To save state of keyboard */
slouken@281
    43
#define ATARIBIOS_MAXKEYS 128
slouken@281
    44
slouken@281
    45
static unsigned char gemdos_currentkeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    46
static unsigned char gemdos_previouskeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    47
static unsigned char gemdos_currentascii[ATARIBIOS_MAXKEYS];
patmandin@1420
    48
static SDL_bool use_dev_mouse = SDL_FALSE;
slouken@281
    49
slouken@281
    50
/* Special keys state */
slouken@281
    51
enum {
slouken@281
    52
	K_RSHIFT=0,
slouken@281
    53
	K_LSHIFT,
slouken@281
    54
	K_CTRL,
slouken@281
    55
	K_ALT,
slouken@281
    56
	K_CAPSLOCK,
slouken@281
    57
	K_CLRHOME,
slouken@281
    58
	K_INSERT
slouken@281
    59
};
slouken@281
    60
slouken@281
    61
enum {
slouken@281
    62
	DEV_BUSY=0,
slouken@281
    63
	DEV_READY
slouken@281
    64
};
slouken@281
    65
slouken@281
    66
/* The translation tables from a console scancode to a SDL keysym */
slouken@281
    67
static SDLKey keymap[ATARIBIOS_MAXKEYS];
slouken@281
    68
patmandin@1209
    69
static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
patmandin@1209
    70
	SDL_bool pressed);
slouken@281
    71
static void UpdateSpecialKeys(int special_keys_state);
slouken@281
    72
slouken@281
    73
void AtariGemdos_InitOSKeymap(_THIS)
slouken@281
    74
{
patmandin@1237
    75
	int i, vectors_mask;
patmandin@1237
    76
	unsigned long dummy;
slouken@281
    77
slouken@1336
    78
	SDL_memset(gemdos_currentkeyboard, 0, sizeof(gemdos_currentkeyboard));
slouken@1336
    79
	SDL_memset(gemdos_previouskeyboard, 0, sizeof(gemdos_previouskeyboard));
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
patmandin@1420
   111
	use_dev_mouse = (SDL_AtariDevMouse_Open()!=0) ? SDL_TRUE : SDL_FALSE;
patmandin@1420
   112
patmandin@1420
   113
	vectors_mask = ATARI_XBIOS_JOYSTICKEVENTS;	/* XBIOS joystick events */
patmandin@1420
   114
	if (!use_dev_mouse) {
patmandin@1420
   115
		vectors_mask |= ATARI_XBIOS_MOUSEEVENTS;	/* XBIOS mouse events */
patmandin@1420
   116
	}
patmandin@1237
   117
	if (Getcookie(C_MiNT, &dummy)==C_FOUND) {
patmandin@1237
   118
		vectors_mask = 0;
patmandin@1237
   119
	}
patmandin@1237
   120
	SDL_AtariXbios_InstallVectors(vectors_mask);
slouken@281
   121
}
slouken@281
   122
slouken@281
   123
void AtariGemdos_PumpEvents(_THIS)
slouken@281
   124
{
slouken@281
   125
	int i;
slouken@281
   126
	SDL_keysym keysym;
slouken@281
   127
slouken@281
   128
	/* Update pressed keys */
slouken@1336
   129
	SDL_memset(gemdos_currentkeyboard, 0, ATARIBIOS_MAXKEYS);
slouken@281
   130
slouken@281
   131
	while (Cconis()!=DEV_BUSY) {
slouken@281
   132
		unsigned long key_pressed;
slouken@281
   133
		unsigned char scancode, asciicode;
slouken@281
   134
slouken@281
   135
		key_pressed=Cnecin();
slouken@281
   136
slouken@281
   137
		asciicode = key_pressed;
slouken@281
   138
		scancode = key_pressed >> 16;
slouken@281
   139
slouken@281
   140
		gemdos_currentkeyboard[scancode]=0xFF;
slouken@281
   141
		gemdos_currentascii[scancode]=asciicode;
slouken@281
   142
	}
slouken@281
   143
slouken@281
   144
	/* Read special keys */
slouken@281
   145
	UpdateSpecialKeys(Kbshift(-1));
slouken@281
   146
slouken@281
   147
	/* Now generate events */
slouken@281
   148
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   149
		/* Key pressed ? */
slouken@281
   150
		if (gemdos_currentkeyboard[i] && !gemdos_previouskeyboard[i])
patmandin@1209
   151
			SDL_PrivateKeyboard(SDL_PRESSED,
patmandin@1209
   152
				TranslateKey(i, gemdos_currentascii[i], &keysym, SDL_TRUE));
slouken@281
   153
			
slouken@281
   154
		/* Key unpressed ? */
slouken@281
   155
		if (gemdos_previouskeyboard[i] && !gemdos_currentkeyboard[i])
patmandin@1209
   156
			SDL_PrivateKeyboard(SDL_RELEASED,
patmandin@1209
   157
				TranslateKey(i, gemdos_currentascii[i], &keysym, SDL_FALSE));
slouken@281
   158
	}
slouken@281
   159
patmandin@1420
   160
	if (use_dev_mouse) {
patmandin@1420
   161
		SDL_AtariDevMouse_PostMouseEvents(this, SDL_TRUE);
patmandin@1420
   162
	} else {
patmandin@1420
   163
		SDL_AtariXbios_PostMouseEvents(this, SDL_TRUE);
patmandin@1420
   164
	}
slouken@281
   165
slouken@281
   166
	/* Will be previous table */
slouken@1336
   167
	SDL_memcpy(gemdos_previouskeyboard, gemdos_currentkeyboard, ATARIBIOS_MAXKEYS);
slouken@281
   168
}
slouken@281
   169
slouken@281
   170
static void UpdateSpecialKeys(int special_keys_state)
slouken@281
   171
{
slouken@281
   172
#define UPDATE_SPECIAL_KEYS(numbit,scancode) \
slouken@281
   173
	{	\
slouken@281
   174
		if (special_keys_state & (1<<(numbit))) { \
slouken@281
   175
			gemdos_currentkeyboard[scancode]=0xFF; \
slouken@281
   176
			gemdos_currentascii[scancode]=0; \
slouken@281
   177
		}	\
slouken@281
   178
	}
slouken@281
   179
slouken@281
   180
	UPDATE_SPECIAL_KEYS(K_RSHIFT, SCANCODE_RIGHTSHIFT);
slouken@281
   181
	UPDATE_SPECIAL_KEYS(K_LSHIFT, SCANCODE_LEFTSHIFT);
slouken@281
   182
	UPDATE_SPECIAL_KEYS(K_CTRL, SCANCODE_LEFTCONTROL);
slouken@281
   183
	UPDATE_SPECIAL_KEYS(K_ALT, SCANCODE_LEFTALT);
slouken@281
   184
	UPDATE_SPECIAL_KEYS(K_CAPSLOCK, SCANCODE_CAPSLOCK);
slouken@281
   185
}
slouken@281
   186
patmandin@1209
   187
static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
patmandin@1209
   188
	SDL_bool pressed)
slouken@281
   189
{
slouken@281
   190
	/* Set the keysym information */
slouken@281
   191
	keysym->scancode = scancode;
slouken@281
   192
slouken@281
   193
	if (asciicode)
slouken@281
   194
		keysym->sym = asciicode;		
slouken@281
   195
	else
slouken@281
   196
		keysym->sym = keymap[scancode];
slouken@281
   197
slouken@281
   198
	keysym->mod = KMOD_NONE;
slouken@281
   199
	keysym->unicode = 0;
patmandin@1221
   200
	if (SDL_TranslateUNICODE && pressed) {
patmandin@1221
   201
		keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
patmandin@1209
   202
	}
slouken@281
   203
slouken@281
   204
	return(keysym);
slouken@281
   205
}
slouken@281
   206
slouken@281
   207
void AtariGemdos_ShutdownEvents(void)
slouken@281
   208
{
slouken@305
   209
	SDL_AtariXbios_RestoreVectors();
patmandin@1420
   210
	if (use_dev_mouse) {
patmandin@1420
   211
		SDL_AtariDevMouse_Close();
patmandin@1420
   212
	}
slouken@281
   213
}