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