src/video/ataricommon/SDL_ikbdevents.c
author Sam Lantinga
Wed, 06 Mar 2002 11:23:08 +0000
changeset 297 f6ffac90895c
parent 281 c5010ab8ba35
child 331 e9278438fb3b
permissions -rw-r--r--
Updated copyright information for 2002
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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 /* To save state of keyboard */
    58 #define ATARIBIOS_MAXKEYS 128
    59 
    60 static unsigned char ikbd_previouskeyboard[ATARIBIOS_MAXKEYS];
    61 static Uint16 atari_prevmouseb;	/* buttons */
    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 int caps_state;
    73 _KEYTAB *curtables;
    74 static unsigned char *tab_unshift, *tab_shift, *tab_caps;
    75 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    76 
    77 static SDL_keysym *TranslateKey(int scancode, int numkeytable, SDL_keysym *keysym);
    78 
    79 void AtariIkbd_InitOSKeymap(_THIS)
    80 {
    81 	int i;
    82 
    83 	memset(SDL_AtariIkbd_keyboard, 0, ATARIBIOS_MAXKEYS);
    84 	memset(ikbd_previouskeyboard, 0, 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] && !ikbd_previouskeyboard[SCANCODE_CAPSLOCK])
   156 		caps_state ^= 1;
   157 
   158 	/* Choose the translation table */
   159 	specialkeys=KT_UNSHIFT;
   160 	if (SDL_AtariIkbd_keyboard[SCANCODE_LEFTSHIFT] || SDL_AtariIkbd_keyboard[SCANCODE_RIGHTSHIFT])
   161 		specialkeys = KT_SHIFT;
   162 	if (caps_state)
   163 		specialkeys = KT_CAPS;
   164 
   165 	/* Now generate events */
   166 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   167 		/* Key pressed ? */
   168 		if (SDL_AtariIkbd_keyboard[i] && !ikbd_previouskeyboard[i])
   169 			SDL_PrivateKeyboard(SDL_PRESSED, TranslateKey(i, specialkeys, &keysym));
   170 			
   171 		/* Key unpressed ? */
   172 		if (ikbd_previouskeyboard[i] && !SDL_AtariIkbd_keyboard[i])
   173 			SDL_PrivateKeyboard(SDL_RELEASED, TranslateKey(i, specialkeys, &keysym));
   174 	}
   175 
   176 	/* Will be previous table */
   177 	memcpy(ikbd_previouskeyboard, SDL_AtariIkbd_keyboard, ATARIBIOS_MAXKEYS);
   178 
   179 	/*--- Send mouse events ---*/
   180 
   181 	/* Mouse motion ? */
   182 	if (SDL_AtariIkbd_mousex || SDL_AtariIkbd_mousey) {
   183 		SDL_PrivateMouseMotion(0, 1, SDL_AtariIkbd_mousex, SDL_AtariIkbd_mousey);
   184 		SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
   185 	}
   186 
   187 	/* Mouse button ? */
   188 	if (SDL_AtariIkbd_mouseb != atari_prevmouseb) {
   189 		for (i=0;i<2;i++) {
   190 			int curbutton, prevbutton;
   191 
   192 			curbutton = SDL_AtariIkbd_mouseb & (1<<i);
   193 			prevbutton = atari_prevmouseb & (1<<i);
   194 
   195 			if (curbutton & !prevbutton) {
   196 				SDL_PrivateMouseButton(SDL_PRESSED, atari_GetButton(i), 0, 0);
   197 			}
   198 			if (!curbutton & prevbutton) {
   199 				SDL_PrivateMouseButton(SDL_RELEASED, atari_GetButton(i), 0, 0);
   200 			}
   201 		}
   202 		atari_prevmouseb = SDL_AtariIkbd_mouseb;
   203 	}
   204 }
   205 
   206 static SDL_keysym *TranslateKey(int scancode, int numkeytable, SDL_keysym *keysym)
   207 {
   208 	unsigned char asciicode;
   209 
   210 	/* Set the keysym information */
   211 	keysym->scancode = scancode;
   212 
   213 	asciicode=0;
   214 	switch(numkeytable) {
   215 		case KT_UNSHIFT:
   216 			asciicode=tab_unshift[scancode];
   217 			break;
   218 		case KT_SHIFT:
   219 			asciicode=tab_shift[scancode];
   220 			break;
   221 		case KT_CAPS:
   222 			asciicode=tab_caps[scancode];
   223 			break;
   224 	}
   225 
   226 	if (asciicode)
   227 		keysym->sym = asciicode;		
   228 	else
   229 		keysym->sym = keymap[scancode];
   230 
   231 	keysym->mod = KMOD_NONE;
   232 	keysym->unicode = 0;
   233 
   234 	return(keysym);
   235 }
   236 
   237 void AtariIkbd_ShutdownEvents(void)
   238 {
   239 	Supexec(SDL_AtariIkbdUninstall);
   240 }
   241