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