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