src/video/ataricommon/SDL_ikbdevents.c
author Patrice Mandin
Wed, 29 Jun 2005 20:32:46 +0000
changeset 1082 48436ffdf677
parent 769 b8d311d90021
child 1209 a55ac374271c
permissions -rw-r--r--
Avoid generating multiple key press/release messages for the same key
     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_ikbdinterrupt_s.h"
    45 
    46 /* Special keys state */
    47 enum {
    48 	K_RSHIFT=0,
    49 	K_LSHIFT,
    50 	K_CTRL,
    51 	K_ALT,
    52 	K_CAPSLOCK,
    53 	K_CLRHOME,
    54 	K_INSERT
    55 };
    56 
    57 #define ATARIBIOS_MAXKEYS 128
    58 
    59 #define KEY_PRESSED		0xff
    60 #define KEY_UNDEFINED	0x80
    61 #define KEY_RELEASED	0x00
    62 
    63 /* The translation tables from a console scancode to a SDL keysym */
    64 #define KT_NOCHANGE -1
    65 
    66 enum {
    67 	KT_UNSHIFT=0,
    68 	KT_SHIFT=1,
    69 	KT_CAPS=2
    70 };
    71 
    72 static Uint16 atari_prevmouseb;	/* save state of mouse buttons */
    73 static int caps_state;			/* caps lock state */
    74 _KEYTAB *curtables;
    75 static unsigned char *tab_unshift, *tab_shift, *tab_caps;
    76 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    77 
    78 static SDL_keysym *TranslateKey(int scancode, int numkeytable, SDL_keysym *keysym);
    79 
    80 void AtariIkbd_InitOSKeymap(_THIS)
    81 {
    82 	int i;
    83 
    84 	memset(SDL_AtariIkbd_keyboard, KEY_UNDEFINED, ATARIBIOS_MAXKEYS);
    85 
    86 	/* Initialize keymap */
    87 	for ( i=0; i<sizeof(keymap); i++ )
    88 		keymap[i] = SDLK_UNKNOWN;
    89 
    90 	/* Functions keys */
    91 	for ( i = 0; i<10; i++ )
    92 		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
    93 
    94 	/* Cursor keypad */
    95 	keymap[SCANCODE_HELP] = SDLK_HELP;
    96 	keymap[SCANCODE_UNDO] = SDLK_UNDO;
    97 	keymap[SCANCODE_INSERT] = SDLK_INSERT;
    98 	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
    99 	keymap[SCANCODE_UP] = SDLK_UP;
   100 	keymap[SCANCODE_DOWN] = SDLK_DOWN;
   101 	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
   102 	keymap[SCANCODE_LEFT] = SDLK_LEFT;
   103 
   104 	/* Special keys */
   105 	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
   106 	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
   107 	keymap[SCANCODE_TAB] = SDLK_TAB;
   108 	keymap[SCANCODE_ENTER] = SDLK_RETURN;
   109 	keymap[SCANCODE_DELETE] = SDLK_DELETE;
   110 	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
   111 	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
   112 	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
   113 	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
   114 	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
   115 
   116 	/* Read XBIOS tables for scancode -> ascii translation */
   117 	curtables=Keytbl(KT_NOCHANGE, KT_NOCHANGE, KT_NOCHANGE);
   118 	tab_unshift=curtables->unshift;
   119 	tab_shift=curtables->shift;
   120 	tab_caps=curtables->caps;
   121 
   122 	/* Set Caps lock initial state */
   123 	caps_state=(Kbshift(-1) & (1<<K_CAPSLOCK));
   124 
   125 	/* Now install our handler */
   126 	SDL_AtariIkbd_mouseb = SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
   127 	atari_prevmouseb = 0;
   128 
   129 	Supexec(SDL_AtariIkbdInstall);
   130 }
   131 
   132 static int atari_GetButton(int button)
   133 {
   134 	switch(button)
   135 	{
   136 		case 0:
   137 			return SDL_BUTTON_RIGHT;
   138 			break;
   139 		case 1:
   140 		default:
   141 			return SDL_BUTTON_LEFT;
   142 			break;
   143 	}
   144 }
   145 
   146 void AtariIkbd_PumpEvents(_THIS)
   147 {
   148 	int i;
   149 	SDL_keysym keysym;
   150 	int specialkeys;
   151 
   152 	/*--- Send keyboard events ---*/
   153 
   154 	/* Update caps lock state */
   155 	if (SDL_AtariIkbd_keyboard[SCANCODE_CAPSLOCK]==KEY_PRESSED) {
   156 		caps_state ^= 1;
   157 	}
   158 
   159 	/* Choose the translation table */
   160 	specialkeys=KT_UNSHIFT;
   161 	if ((SDL_AtariIkbd_keyboard[SCANCODE_LEFTSHIFT]==KEY_PRESSED)
   162 		|| (SDL_AtariIkbd_keyboard[SCANCODE_RIGHTSHIFT]==KEY_PRESSED))
   163 	{
   164 		specialkeys = KT_SHIFT;
   165 	}
   166 	if (caps_state) {
   167 		specialkeys = KT_CAPS;
   168 	}
   169 
   170 	/* Now generate events */
   171 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   172 		/* Key pressed ? */
   173 		if (SDL_AtariIkbd_keyboard[i]==KEY_PRESSED) {
   174 			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, specialkeys, &keysym));
   175 			SDL_AtariIkbd_keyboard[i]=KEY_UNDEFINED;
   176 		}
   177 			
   178 		/* Key released ? */
   179 		if (SDL_AtariIkbd_keyboard[i]==KEY_RELEASED) {
   180 			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, specialkeys, &keysym));
   181 			SDL_AtariIkbd_keyboard[i]=KEY_UNDEFINED;
   182 		}
   183 	}
   184 
   185 	/*--- Send mouse events ---*/
   186 
   187 	/* Mouse motion ? */
   188 	if (SDL_AtariIkbd_mousex || SDL_AtariIkbd_mousey) {
   189 		SDL_PrivateMouseMotion(0, 1, SDL_AtariIkbd_mousex, SDL_AtariIkbd_mousey);
   190 		SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
   191 	}
   192 
   193 	/* Mouse button ? */
   194 	if (SDL_AtariIkbd_mouseb != atari_prevmouseb) {
   195 		for (i=0;i<2;i++) {
   196 			int curbutton, prevbutton;
   197 
   198 			curbutton = SDL_AtariIkbd_mouseb & (1<<i);
   199 			prevbutton = atari_prevmouseb & (1<<i);
   200 
   201 			if (curbutton && !prevbutton) {
   202 				SDL_PrivateMouseButton(SDL_PRESSED, atari_GetButton(i), 0, 0);
   203 			}
   204 			if (!curbutton && prevbutton) {
   205 				SDL_PrivateMouseButton(SDL_RELEASED, atari_GetButton(i), 0, 0);
   206 			}
   207 		}
   208 		atari_prevmouseb = SDL_AtariIkbd_mouseb;
   209 	}
   210 }
   211 
   212 static SDL_keysym *TranslateKey(int scancode, int numkeytable, SDL_keysym *keysym)
   213 {
   214 	unsigned char asciicode;
   215 
   216 	/* Set the keysym information */
   217 	keysym->scancode = scancode;
   218 
   219 	asciicode=0;
   220 	switch(numkeytable) {
   221 		case KT_UNSHIFT:
   222 			asciicode=tab_unshift[scancode];
   223 			break;
   224 		case KT_SHIFT:
   225 			asciicode=tab_shift[scancode];
   226 			break;
   227 		case KT_CAPS:
   228 			asciicode=tab_caps[scancode];
   229 			break;
   230 	}
   231 
   232 	if (asciicode)
   233 		keysym->sym = asciicode;		
   234 	else
   235 		keysym->sym = keymap[scancode];
   236 
   237 	keysym->mod = KMOD_NONE;
   238 	keysym->unicode = 0;
   239 
   240 	return(keysym);
   241 }
   242 
   243 void AtariIkbd_ShutdownEvents(void)
   244 {
   245 	Supexec(SDL_AtariIkbdUninstall);
   246 }