src/video/ataricommon/SDL_ikbdevents.c
changeset 281 c5010ab8ba35
child 297 f6ffac90895c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/ataricommon/SDL_ikbdevents.c	Sun Feb 17 19:54:28 2002 +0000
     1.3 @@ -0,0 +1,241 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/*
    1.32 + *	Atari keyboard events manager, using hardware IKBD
    1.33 + *
    1.34 + *	Patrice Mandin
    1.35 + */
    1.36 +
    1.37 +#include <string.h>
    1.38 +
    1.39 +/* Mint includes */
    1.40 +#include <mint/osbind.h>
    1.41 +
    1.42 +#include "SDL.h"
    1.43 +#include "SDL_sysevents.h"
    1.44 +#include "SDL_events_c.h"
    1.45 +
    1.46 +#include "SDL_atarikeys.h"
    1.47 +#include "SDL_ikbdinterrupt_s.h"
    1.48 +
    1.49 +/* Special keys state */
    1.50 +enum {
    1.51 +	K_RSHIFT=0,
    1.52 +	K_LSHIFT,
    1.53 +	K_CTRL,
    1.54 +	K_ALT,
    1.55 +	K_CAPSLOCK,
    1.56 +	K_CLRHOME,
    1.57 +	K_INSERT
    1.58 +};
    1.59 +
    1.60 +/* To save state of keyboard */
    1.61 +#define ATARIBIOS_MAXKEYS 128
    1.62 +
    1.63 +static unsigned char ikbd_previouskeyboard[ATARIBIOS_MAXKEYS];
    1.64 +static Uint16 atari_prevmouseb;	/* buttons */
    1.65 +
    1.66 +/* The translation tables from a console scancode to a SDL keysym */
    1.67 +#define KT_NOCHANGE -1
    1.68 +
    1.69 +enum {
    1.70 +	KT_UNSHIFT=0,
    1.71 +	KT_SHIFT=1,
    1.72 +	KT_CAPS=2
    1.73 +};
    1.74 +
    1.75 +static int caps_state;
    1.76 +_KEYTAB *curtables;
    1.77 +static unsigned char *tab_unshift, *tab_shift, *tab_caps;
    1.78 +static SDLKey keymap[ATARIBIOS_MAXKEYS];
    1.79 +
    1.80 +static SDL_keysym *TranslateKey(int scancode, int numkeytable, SDL_keysym *keysym);
    1.81 +
    1.82 +void AtariIkbd_InitOSKeymap(_THIS)
    1.83 +{
    1.84 +	int i;
    1.85 +
    1.86 +	memset(SDL_AtariIkbd_keyboard, 0, ATARIBIOS_MAXKEYS);
    1.87 +	memset(ikbd_previouskeyboard, 0, ATARIBIOS_MAXKEYS);
    1.88 +
    1.89 +	/* Initialize keymap */
    1.90 +	for ( i=0; i<sizeof(keymap); i++ )
    1.91 +		keymap[i] = SDLK_UNKNOWN;
    1.92 +
    1.93 +	/* Functions keys */
    1.94 +	for ( i = 0; i<10; i++ )
    1.95 +		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
    1.96 +
    1.97 +	/* Cursor keypad */
    1.98 +	keymap[SCANCODE_HELP] = SDLK_HELP;
    1.99 +	keymap[SCANCODE_UNDO] = SDLK_UNDO;
   1.100 +	keymap[SCANCODE_INSERT] = SDLK_INSERT;
   1.101 +	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
   1.102 +	keymap[SCANCODE_UP] = SDLK_UP;
   1.103 +	keymap[SCANCODE_DOWN] = SDLK_DOWN;
   1.104 +	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
   1.105 +	keymap[SCANCODE_LEFT] = SDLK_LEFT;
   1.106 +
   1.107 +	/* Special keys */
   1.108 +	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
   1.109 +	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
   1.110 +	keymap[SCANCODE_TAB] = SDLK_TAB;
   1.111 +	keymap[SCANCODE_ENTER] = SDLK_RETURN;
   1.112 +	keymap[SCANCODE_DELETE] = SDLK_DELETE;
   1.113 +	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
   1.114 +	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
   1.115 +	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
   1.116 +	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
   1.117 +	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
   1.118 +
   1.119 +	/* Read XBIOS tables for scancode -> ascii translation */
   1.120 +	curtables=Keytbl(KT_NOCHANGE, KT_NOCHANGE, KT_NOCHANGE);
   1.121 +	tab_unshift=curtables->unshift;
   1.122 +	tab_shift=curtables->shift;
   1.123 +	tab_caps=curtables->caps;
   1.124 +
   1.125 +	/* Set Caps lock initial state */
   1.126 +	caps_state=(Kbshift(-1) & (1<<K_CAPSLOCK));
   1.127 +
   1.128 +	/* Now install our handler */
   1.129 +	SDL_AtariIkbd_mouseb = SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
   1.130 +	atari_prevmouseb = 0;
   1.131 +
   1.132 +	Supexec(SDL_AtariIkbdInstall);
   1.133 +}
   1.134 +
   1.135 +static int atari_GetButton(int button)
   1.136 +{
   1.137 +	switch(button)
   1.138 +	{
   1.139 +		case 0:
   1.140 +			return SDL_BUTTON_RIGHT;
   1.141 +			break;
   1.142 +		case 1:
   1.143 +		default:
   1.144 +			return SDL_BUTTON_LEFT;
   1.145 +			break;
   1.146 +	}
   1.147 +}
   1.148 +
   1.149 +void AtariIkbd_PumpEvents(_THIS)
   1.150 +{
   1.151 +	int i;
   1.152 +	SDL_keysym keysym;
   1.153 +	int specialkeys;
   1.154 +
   1.155 +	/*--- Send keyboard events ---*/
   1.156 +
   1.157 +	/* Update caps lock state */
   1.158 +	if (SDL_AtariIkbd_keyboard[SCANCODE_CAPSLOCK] && !ikbd_previouskeyboard[SCANCODE_CAPSLOCK])
   1.159 +		caps_state ^= 1;
   1.160 +
   1.161 +	/* Choose the translation table */
   1.162 +	specialkeys=KT_UNSHIFT;
   1.163 +	if (SDL_AtariIkbd_keyboard[SCANCODE_LEFTSHIFT] || SDL_AtariIkbd_keyboard[SCANCODE_RIGHTSHIFT])
   1.164 +		specialkeys = KT_SHIFT;
   1.165 +	if (caps_state)
   1.166 +		specialkeys = KT_CAPS;
   1.167 +
   1.168 +	/* Now generate events */
   1.169 +	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   1.170 +		/* Key pressed ? */
   1.171 +		if (SDL_AtariIkbd_keyboard[i] && !ikbd_previouskeyboard[i])
   1.172 +			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, specialkeys, &keysym));
   1.173 +			
   1.174 +		/* Key unpressed ? */
   1.175 +		if (ikbd_previouskeyboard[i] && !SDL_AtariIkbd_keyboard[i])
   1.176 +			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, specialkeys, &keysym));
   1.177 +	}
   1.178 +
   1.179 +	/* Will be previous table */
   1.180 +	memcpy(ikbd_previouskeyboard, SDL_AtariIkbd_keyboard, ATARIBIOS_MAXKEYS);
   1.181 +
   1.182 +	/*--- Send mouse events ---*/
   1.183 +
   1.184 +	/* Mouse motion ? */
   1.185 +	if (SDL_AtariIkbd_mousex || SDL_AtariIkbd_mousey) {
   1.186 +		SDL_PrivateMouseMotion(0, 1, SDL_AtariIkbd_mousex, SDL_AtariIkbd_mousey);
   1.187 +		SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
   1.188 +	}
   1.189 +
   1.190 +	/* Mouse button ? */
   1.191 +	if (SDL_AtariIkbd_mouseb != atari_prevmouseb) {
   1.192 +		for (i=0;i<2;i++) {
   1.193 +			int curbutton, prevbutton;
   1.194 +
   1.195 +			curbutton = SDL_AtariIkbd_mouseb & (1<<i);
   1.196 +			prevbutton = atari_prevmouseb & (1<<i);
   1.197 +
   1.198 +			if (curbutton & !prevbutton) {
   1.199 +				SDL_PrivateMouseButton(SDL_PRESSED, atari_GetButton(i), 0, 0);
   1.200 +			}
   1.201 +			if (!curbutton & prevbutton) {
   1.202 +				SDL_PrivateMouseButton(SDL_RELEASED, atari_GetButton(i), 0, 0);
   1.203 +			}
   1.204 +		}
   1.205 +		atari_prevmouseb = SDL_AtariIkbd_mouseb;
   1.206 +	}
   1.207 +}
   1.208 +
   1.209 +static SDL_keysym *TranslateKey(int scancode, int numkeytable, SDL_keysym *keysym)
   1.210 +{
   1.211 +	unsigned char asciicode;
   1.212 +
   1.213 +	/* Set the keysym information */
   1.214 +	keysym->scancode = scancode;
   1.215 +
   1.216 +	asciicode=0;
   1.217 +	switch(numkeytable) {
   1.218 +		case KT_UNSHIFT:
   1.219 +			asciicode=tab_unshift[scancode];
   1.220 +			break;
   1.221 +		case KT_SHIFT:
   1.222 +			asciicode=tab_shift[scancode];
   1.223 +			break;
   1.224 +		case KT_CAPS:
   1.225 +			asciicode=tab_caps[scancode];
   1.226 +			break;
   1.227 +	}
   1.228 +
   1.229 +	if (asciicode)
   1.230 +		keysym->sym = asciicode;		
   1.231 +	else
   1.232 +		keysym->sym = keymap[scancode];
   1.233 +
   1.234 +	keysym->mod = KMOD_NONE;
   1.235 +	keysym->unicode = 0;
   1.236 +
   1.237 +	return(keysym);
   1.238 +}
   1.239 +
   1.240 +void AtariIkbd_ShutdownEvents(void)
   1.241 +{
   1.242 +	Supexec(SDL_AtariIkbdUninstall);
   1.243 +}
   1.244 +