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