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