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