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