src/video/ataricommon/SDL_biosevents.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 BIOS
    25  *
    26  *	Patrice Mandin
    27  */
    28 
    29 /* Mint includes */
    30 #include <mint/osbind.h>
    31 #include <mint/cookie.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_xbiosevents_c.h"
    39 
    40 /* To save state of keyboard */
    41 #define ATARIBIOS_MAXKEYS 128
    42 
    43 static unsigned char bios_currentkeyboard[ATARIBIOS_MAXKEYS];
    44 static unsigned char bios_previouskeyboard[ATARIBIOS_MAXKEYS];
    45 static unsigned char bios_currentascii[ATARIBIOS_MAXKEYS];
    46 
    47 /* Special keys state */
    48 enum {
    49 	K_RSHIFT=0,
    50 	K_LSHIFT,
    51 	K_CTRL,
    52 	K_ALT,
    53 	K_CAPSLOCK,
    54 	K_CLRHOME,
    55 	K_INSERT
    56 };
    57 
    58 /* The translation tables from a console scancode to a SDL keysym */
    59 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    60 
    61 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
    62 	SDL_bool pressed);
    63 static void UpdateSpecialKeys(int special_keys_state);
    64 
    65 void AtariBios_InitOSKeymap(_THIS)
    66 {
    67 	int i, vectors_mask;
    68 	unsigned long dummy;
    69 
    70 	SDL_memset(bios_currentkeyboard, 0, sizeof(bios_currentkeyboard));
    71 	SDL_memset(bios_previouskeyboard, 0, sizeof(bios_previouskeyboard));
    72 
    73 	/* Initialize keymap */
    74 	for ( i=0; i<sizeof(keymap); i++ )
    75 		keymap[i] = SDLK_UNKNOWN;
    76 
    77 	/* Functions keys */
    78 	for ( i = 0; i<10; i++ )
    79 		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
    80 
    81 	/* Cursor keypad */
    82 	keymap[SCANCODE_HELP] = SDLK_HELP;
    83 	keymap[SCANCODE_UNDO] = SDLK_UNDO;
    84 	keymap[SCANCODE_INSERT] = SDLK_INSERT;
    85 	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
    86 	keymap[SCANCODE_UP] = SDLK_UP;
    87 	keymap[SCANCODE_DOWN] = SDLK_DOWN;
    88 	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
    89 	keymap[SCANCODE_LEFT] = SDLK_LEFT;
    90 
    91 	/* Special keys */
    92 	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
    93 	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
    94 	keymap[SCANCODE_TAB] = SDLK_TAB;
    95 	keymap[SCANCODE_ENTER] = SDLK_RETURN;
    96 	keymap[SCANCODE_DELETE] = SDLK_DELETE;
    97 	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
    98 	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
    99 	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
   100 	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
   101 	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
   102 
   103 	vectors_mask = ATARI_XBIOS_MOUSEEVENTS|ATARI_XBIOS_JOYSTICKEVENTS;
   104 	if (Getcookie(C_MiNT, &dummy)==C_FOUND) {
   105 		vectors_mask = 0;
   106 	}
   107 
   108 	SDL_AtariXbios_InstallVectors(vectors_mask);
   109 }
   110 
   111 void AtariBios_PumpEvents(_THIS)
   112 {
   113 	int i;
   114 	SDL_keysym keysym;
   115 
   116 	/* Update pressed keys */
   117 	SDL_memset(bios_currentkeyboard, 0, ATARIBIOS_MAXKEYS);
   118 
   119 	while (Bconstat(_CON)) {
   120 		unsigned long key_pressed;
   121 		unsigned char asciicode, scancode;
   122 
   123 		key_pressed=Bconin(_CON);
   124 
   125 		asciicode = key_pressed;
   126 		scancode = key_pressed >> 16;
   127 
   128 		bios_currentkeyboard[scancode]=0xFF;
   129 		bios_currentascii[scancode]=asciicode;
   130 	}
   131 
   132 	/* Read special keys */
   133 	UpdateSpecialKeys(Kbshift(-1));
   134 
   135 	/* Now generate events */
   136 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   137 		/* Key pressed ? */
   138 		if (bios_currentkeyboard[i] && !bios_previouskeyboard[i])
   139 			SDL_PrivateKeyboard(SDL_PRESSED,
   140 				TranslateKey(i, bios_currentascii[i], &keysym, SDL_TRUE));
   141 			
   142 		/* Key unpressed ? */
   143 		if (bios_previouskeyboard[i] && !bios_currentkeyboard[i])
   144 			SDL_PrivateKeyboard(SDL_RELEASED,
   145 				TranslateKey(i, bios_currentascii[i], &keysym, SDL_FALSE));
   146 	}
   147 
   148 	SDL_AtariXbios_PostMouseEvents(this, SDL_TRUE);
   149 
   150 	/* Will be previous table */
   151 	SDL_memcpy(bios_previouskeyboard, bios_currentkeyboard, ATARIBIOS_MAXKEYS);
   152 }
   153 
   154 static void UpdateSpecialKeys(int special_keys_state)
   155 {
   156 #define UPDATE_SPECIAL_KEYS(numbit,scancode) \
   157 	{	\
   158 		if (special_keys_state & (1<<(numbit))) { \
   159 			bios_currentkeyboard[scancode]=0xFF; \
   160 			bios_currentascii[scancode]=0; \
   161 		}	\
   162 	}
   163 
   164 	UPDATE_SPECIAL_KEYS(K_RSHIFT, SCANCODE_RIGHTSHIFT);
   165 	UPDATE_SPECIAL_KEYS(K_LSHIFT, SCANCODE_LEFTSHIFT);
   166 	UPDATE_SPECIAL_KEYS(K_CTRL, SCANCODE_LEFTCONTROL);
   167 	UPDATE_SPECIAL_KEYS(K_ALT, SCANCODE_LEFTALT);
   168 	UPDATE_SPECIAL_KEYS(K_CAPSLOCK, SCANCODE_CAPSLOCK);
   169 }
   170 
   171 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
   172 	SDL_bool pressed)
   173 {
   174 	/* Set the keysym information */
   175 	keysym->scancode = scancode;
   176 
   177 	if (asciicode)
   178 		keysym->sym = asciicode;		
   179 	else
   180 		keysym->sym = keymap[scancode];
   181 
   182 	keysym->mod = KMOD_NONE;
   183 	keysym->unicode = 0;
   184 	if (SDL_TranslateUNICODE && pressed) {
   185 		keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
   186 	}
   187 
   188 	return(keysym);
   189 }
   190 
   191 void AtariBios_ShutdownEvents(void)
   192 {
   193 	SDL_AtariXbios_RestoreVectors();
   194 }