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