src/video/ataricommon/SDL_ikbdevents.c
author Patrice Mandin
Mon, 02 Jan 2006 19:49:21 +0000
changeset 1221 8ef3e7e92a91
parent 1209 a55ac374271c
child 1312 c9b51268668f
permissions -rw-r--r--
Use a simple table for Unicode translation
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /*
    29  *	Atari keyboard events manager, using hardware IKBD
    30  *
    31  *	Patrice Mandin
    32  */
    33 
    34 #include <string.h>
    35 
    36 /* Mint includes */
    37 #include <mint/osbind.h>
    38 
    39 #include "SDL.h"
    40 #include "SDL_sysevents.h"
    41 #include "SDL_events_c.h"
    42 
    43 #include "SDL_atarikeys.h"
    44 #include "SDL_atarievents_c.h"
    45 #include "SDL_ikbdinterrupt_s.h"
    46 
    47 /* Special keys state */
    48 enum {
    49 	K_RSHIFT=0,
    50 	K_LSHIFT,
    51 	K_CTRL,
    52 	K_ALT,
    53 	K_CAPSLOCK,
    54 	K_CLRHOME,
    55 	K_INSERT
    56 };
    57 
    58 #define ATARIBIOS_MAXKEYS 128
    59 
    60 #define KEY_PRESSED		0xff
    61 #define KEY_UNDEFINED	0x80
    62 #define KEY_RELEASED	0x00
    63 
    64 /* The translation tables from a console scancode to a SDL keysym */
    65 #define KT_NOCHANGE -1
    66 
    67 enum {
    68 	KT_UNSHIFT=0,
    69 	KT_SHIFT=1,
    70 	KT_CAPS=2
    71 };
    72 
    73 static Uint16 atari_prevmouseb;	/* save state of mouse buttons */
    74 static int caps_state;			/* caps lock state */
    75 _KEYTAB *curtables;
    76 static unsigned char *tab_unshift, *tab_shift, *tab_caps;
    77 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    78 
    79 static SDL_keysym *TranslateKey(int scancode, int numkeytable, SDL_keysym *keysym,
    80 	SDL_bool pressed);
    81 
    82 void AtariIkbd_InitOSKeymap(_THIS)
    83 {
    84 	int i;
    85 
    86 	memset(SDL_AtariIkbd_keyboard, KEY_UNDEFINED, ATARIBIOS_MAXKEYS);
    87 
    88 	/* Initialize keymap */
    89 	for ( i=0; i<sizeof(keymap); i++ )
    90 		keymap[i] = SDLK_UNKNOWN;
    91 
    92 	/* Functions keys */
    93 	for ( i = 0; i<10; i++ )
    94 		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
    95 
    96 	/* Cursor keypad */
    97 	keymap[SCANCODE_HELP] = SDLK_HELP;
    98 	keymap[SCANCODE_UNDO] = SDLK_UNDO;
    99 	keymap[SCANCODE_INSERT] = SDLK_INSERT;
   100 	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
   101 	keymap[SCANCODE_UP] = SDLK_UP;
   102 	keymap[SCANCODE_DOWN] = SDLK_DOWN;
   103 	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
   104 	keymap[SCANCODE_LEFT] = SDLK_LEFT;
   105 
   106 	/* Special keys */
   107 	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
   108 	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
   109 	keymap[SCANCODE_TAB] = SDLK_TAB;
   110 	keymap[SCANCODE_ENTER] = SDLK_RETURN;
   111 	keymap[SCANCODE_DELETE] = SDLK_DELETE;
   112 	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
   113 	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
   114 	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
   115 	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
   116 	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
   117 
   118 	/* Read XBIOS tables for scancode -> ascii translation */
   119 	curtables=Keytbl(KT_NOCHANGE, KT_NOCHANGE, KT_NOCHANGE);
   120 	tab_unshift=curtables->unshift;
   121 	tab_shift=curtables->shift;
   122 	tab_caps=curtables->caps;
   123 
   124 	/* Set Caps lock initial state */
   125 	caps_state=(Kbshift(-1) & (1<<K_CAPSLOCK));
   126 
   127 	/* Now install our handler */
   128 	SDL_AtariIkbd_mouseb = SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
   129 	atari_prevmouseb = 0;
   130 
   131 	Supexec(SDL_AtariIkbdInstall);
   132 }
   133 
   134 static int atari_GetButton(int button)
   135 {
   136 	switch(button)
   137 	{
   138 		case 0:
   139 			return SDL_BUTTON_RIGHT;
   140 			break;
   141 		case 1:
   142 		default:
   143 			return SDL_BUTTON_LEFT;
   144 			break;
   145 	}
   146 }
   147 
   148 void AtariIkbd_PumpEvents(_THIS)
   149 {
   150 	int i;
   151 	SDL_keysym keysym;
   152 	int specialkeys;
   153 
   154 	/*--- Send keyboard events ---*/
   155 
   156 	/* Update caps lock state */
   157 	if (SDL_AtariIkbd_keyboard[SCANCODE_CAPSLOCK]==KEY_PRESSED) {
   158 		caps_state ^= 1;
   159 	}
   160 
   161 	/* Choose the translation table */
   162 	specialkeys=KT_UNSHIFT;
   163 	if ((SDL_AtariIkbd_keyboard[SCANCODE_LEFTSHIFT]==KEY_PRESSED)
   164 		|| (SDL_AtariIkbd_keyboard[SCANCODE_RIGHTSHIFT]==KEY_PRESSED))
   165 	{
   166 		specialkeys = KT_SHIFT;
   167 	}
   168 	if (caps_state) {
   169 		specialkeys = KT_CAPS;
   170 	}
   171 
   172 	/* Now generate events */
   173 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   174 		/* Key pressed ? */
   175 		if (SDL_AtariIkbd_keyboard[i]==KEY_PRESSED) {
   176 			SDL_PrivateKeyboard(SDL_PRESSED,
   177 				TranslateKey(i, specialkeys, &keysym, SDL_TRUE));
   178 			SDL_AtariIkbd_keyboard[i]=KEY_UNDEFINED;
   179 		}
   180 			
   181 		/* Key released ? */
   182 		if (SDL_AtariIkbd_keyboard[i]==KEY_RELEASED) {
   183 			SDL_PrivateKeyboard(SDL_RELEASED,
   184 				TranslateKey(i, specialkeys, &keysym, SDL_FALSE));
   185 			SDL_AtariIkbd_keyboard[i]=KEY_UNDEFINED;
   186 		}
   187 	}
   188 
   189 	/*--- Send mouse events ---*/
   190 
   191 	/* Mouse motion ? */
   192 	if (SDL_AtariIkbd_mousex || SDL_AtariIkbd_mousey) {
   193 		SDL_PrivateMouseMotion(0, 1, SDL_AtariIkbd_mousex, SDL_AtariIkbd_mousey);
   194 		SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
   195 	}
   196 
   197 	/* Mouse button ? */
   198 	if (SDL_AtariIkbd_mouseb != atari_prevmouseb) {
   199 		for (i=0;i<2;i++) {
   200 			int curbutton, prevbutton;
   201 
   202 			curbutton = SDL_AtariIkbd_mouseb & (1<<i);
   203 			prevbutton = atari_prevmouseb & (1<<i);
   204 
   205 			if (curbutton && !prevbutton) {
   206 				SDL_PrivateMouseButton(SDL_PRESSED, atari_GetButton(i), 0, 0);
   207 			}
   208 			if (!curbutton && prevbutton) {
   209 				SDL_PrivateMouseButton(SDL_RELEASED, atari_GetButton(i), 0, 0);
   210 			}
   211 		}
   212 		atari_prevmouseb = SDL_AtariIkbd_mouseb;
   213 	}
   214 }
   215 
   216 static SDL_keysym *TranslateKey(int scancode, int numkeytable, SDL_keysym *keysym,
   217 	SDL_bool pressed)
   218 {
   219 	unsigned char asciicode;
   220 
   221 	/* Set the keysym information */
   222 	keysym->scancode = scancode;
   223 
   224 	asciicode=0;
   225 	switch(numkeytable) {
   226 		case KT_UNSHIFT:
   227 			asciicode=tab_unshift[scancode];
   228 			break;
   229 		case KT_SHIFT:
   230 			asciicode=tab_shift[scancode];
   231 			break;
   232 		case KT_CAPS:
   233 			asciicode=tab_caps[scancode];
   234 			break;
   235 	}
   236 
   237 	if (asciicode)
   238 		keysym->sym = asciicode;		
   239 	else
   240 		keysym->sym = keymap[scancode];
   241 
   242 	keysym->mod = KMOD_NONE;
   243 	keysym->unicode = 0;
   244 	if (SDL_TranslateUNICODE && pressed) {
   245 		keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
   246 	}
   247 
   248 	return(keysym);
   249 }
   250 
   251 void AtariIkbd_ShutdownEvents(void)
   252 {
   253 	Supexec(SDL_AtariIkbdUninstall);
   254 }