src/video/ataricommon/SDL_biosevents.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 21 Feb 2002 21:51:29 +0000
changeset 284 d8b7eae78652
parent 281 c5010ab8ba35
child 297 f6ffac90895c
permissions -rw-r--r--
*** empty log message ***
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
 *	Atari keyboard events manager, using BIOS
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"
slouken@281
    44
#include "SDL_xbiosmouseevents_c.h"
slouken@281
    45
slouken@281
    46
/* To save state of keyboard */
slouken@281
    47
#define ATARIBIOS_MAXKEYS 128
slouken@281
    48
slouken@281
    49
static unsigned char bios_currentkeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    50
static unsigned char bios_previouskeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    51
static unsigned char bios_currentascii[ATARIBIOS_MAXKEYS];
slouken@281
    52
slouken@281
    53
/* Special keys state */
slouken@281
    54
enum {
slouken@281
    55
	K_RSHIFT=0,
slouken@281
    56
	K_LSHIFT,
slouken@281
    57
	K_CTRL,
slouken@281
    58
	K_ALT,
slouken@281
    59
	K_CAPSLOCK,
slouken@281
    60
	K_CLRHOME,
slouken@281
    61
	K_INSERT
slouken@281
    62
};
slouken@281
    63
slouken@281
    64
/* The translation tables from a console scancode to a SDL keysym */
slouken@281
    65
static SDLKey keymap[ATARIBIOS_MAXKEYS];
slouken@281
    66
slouken@281
    67
static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym);
slouken@281
    68
static void UpdateSpecialKeys(int special_keys_state);
slouken@281
    69
slouken@281
    70
void AtariBios_InitOSKeymap(_THIS)
slouken@281
    71
{
slouken@281
    72
	int i;
slouken@281
    73
slouken@281
    74
	memset(bios_currentkeyboard, 0, sizeof(bios_currentkeyboard));
slouken@281
    75
	memset(bios_previouskeyboard, 0, sizeof(bios_previouskeyboard));
slouken@281
    76
slouken@281
    77
	/* Initialize keymap */
slouken@281
    78
	for ( i=0; i<sizeof(keymap); i++ )
slouken@281
    79
		keymap[i] = SDLK_UNKNOWN;
slouken@281
    80
slouken@281
    81
	/* Functions keys */
slouken@281
    82
	for ( i = 0; i<10; i++ )
slouken@281
    83
		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
slouken@281
    84
slouken@281
    85
	/* Cursor keypad */
slouken@281
    86
	keymap[SCANCODE_HELP] = SDLK_HELP;
slouken@281
    87
	keymap[SCANCODE_UNDO] = SDLK_UNDO;
slouken@281
    88
	keymap[SCANCODE_INSERT] = SDLK_INSERT;
slouken@281
    89
	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
slouken@281
    90
	keymap[SCANCODE_UP] = SDLK_UP;
slouken@281
    91
	keymap[SCANCODE_DOWN] = SDLK_DOWN;
slouken@281
    92
	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
slouken@281
    93
	keymap[SCANCODE_LEFT] = SDLK_LEFT;
slouken@281
    94
slouken@281
    95
	/* Special keys */
slouken@281
    96
	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
slouken@281
    97
	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
slouken@281
    98
	keymap[SCANCODE_TAB] = SDLK_TAB;
slouken@281
    99
	keymap[SCANCODE_ENTER] = SDLK_RETURN;
slouken@281
   100
	keymap[SCANCODE_DELETE] = SDLK_DELETE;
slouken@281
   101
	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
slouken@281
   102
	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
slouken@281
   103
	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
slouken@281
   104
	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
slouken@281
   105
	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
slouken@281
   106
slouken@281
   107
	AtariXbios_InstallMouseVector();
slouken@281
   108
}
slouken@281
   109
slouken@281
   110
void AtariBios_PumpEvents(_THIS)
slouken@281
   111
{
slouken@281
   112
	int i;
slouken@281
   113
	SDL_keysym keysym;
slouken@281
   114
slouken@281
   115
	/* Update pressed keys */
slouken@281
   116
	memset(bios_currentkeyboard, 0, ATARIBIOS_MAXKEYS);
slouken@281
   117
slouken@281
   118
	while (Bconstat(_CON)) {
slouken@281
   119
		unsigned long key_pressed;
slouken@281
   120
		unsigned char asciicode, scancode;
slouken@281
   121
slouken@281
   122
		key_pressed=Bconin(_CON);
slouken@281
   123
slouken@281
   124
		asciicode = key_pressed;
slouken@281
   125
		scancode = key_pressed >> 16;
slouken@281
   126
slouken@281
   127
		bios_currentkeyboard[scancode]=0xFF;
slouken@281
   128
		bios_currentascii[scancode]=asciicode;
slouken@281
   129
	}
slouken@281
   130
slouken@281
   131
	/* Read special keys */
slouken@281
   132
	UpdateSpecialKeys(Kbshift(-1));
slouken@281
   133
slouken@281
   134
	/* Now generate events */
slouken@281
   135
	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
slouken@281
   136
		/* Key pressed ? */
slouken@281
   137
		if (bios_currentkeyboard[i] && !bios_previouskeyboard[i])
slouken@281
   138
			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, bios_currentascii[i], &keysym));
slouken@281
   139
			
slouken@281
   140
		/* Key unpressed ? */
slouken@281
   141
		if (bios_previouskeyboard[i] && !bios_currentkeyboard[i])
slouken@284
   142
			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, bios_currentascii[i], &keysym));
slouken@281
   143
	}
slouken@281
   144
slouken@281
   145
	AtariXbios_PostMouseEvents(this);
slouken@281
   146
slouken@281
   147
	/* Will be previous table */
slouken@281
   148
	memcpy(bios_previouskeyboard, bios_currentkeyboard, ATARIBIOS_MAXKEYS);
slouken@281
   149
}
slouken@281
   150
slouken@281
   151
static void UpdateSpecialKeys(int special_keys_state)
slouken@281
   152
{
slouken@281
   153
#define UPDATE_SPECIAL_KEYS(numbit,scancode) \
slouken@281
   154
	{	\
slouken@281
   155
		if (special_keys_state & (1<<(numbit))) { \
slouken@281
   156
			bios_currentkeyboard[scancode]=0xFF; \
slouken@281
   157
			bios_currentascii[scancode]=0; \
slouken@281
   158
		}	\
slouken@281
   159
	}
slouken@281
   160
slouken@281
   161
	UPDATE_SPECIAL_KEYS(K_RSHIFT, SCANCODE_RIGHTSHIFT);
slouken@281
   162
	UPDATE_SPECIAL_KEYS(K_LSHIFT, SCANCODE_LEFTSHIFT);
slouken@281
   163
	UPDATE_SPECIAL_KEYS(K_CTRL, SCANCODE_LEFTCONTROL);
slouken@281
   164
	UPDATE_SPECIAL_KEYS(K_ALT, SCANCODE_LEFTALT);
slouken@281
   165
	UPDATE_SPECIAL_KEYS(K_CAPSLOCK, SCANCODE_CAPSLOCK);
slouken@281
   166
}
slouken@281
   167
slouken@281
   168
static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym)
slouken@281
   169
{
slouken@281
   170
	/* Set the keysym information */
slouken@281
   171
	keysym->scancode = scancode;
slouken@281
   172
slouken@281
   173
	if (asciicode)
slouken@281
   174
		keysym->sym = asciicode;		
slouken@281
   175
	else
slouken@281
   176
		keysym->sym = keymap[scancode];
slouken@281
   177
slouken@281
   178
	keysym->mod = KMOD_NONE;
slouken@281
   179
	keysym->unicode = 0;
slouken@281
   180
slouken@281
   181
	return(keysym);
slouken@281
   182
}
slouken@281
   183
slouken@281
   184
void AtariBios_ShutdownEvents(void)
slouken@281
   185
{
slouken@281
   186
	AtariXbios_RestoreMouseVector();
slouken@281
   187
}