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