src/video/ataricommon/SDL_gemdosevents.c
author Sam Lantinga
Tue, 07 Feb 2006 06:59:48 +0000
changeset 1336 3692456e7b0f
parent 1312 c9b51268668f
child 1350 adc8ff3c9597
permissions -rw-r--r--
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
     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 #include <string.h>
    30 
    31 /* Mint includes */
    32 #include <mint/osbind.h>
    33 #include <mint/cookie.h>
    34 
    35 #include "SDL.h"
    36 #include "SDL_sysevents.h"
    37 #include "SDL_events_c.h"
    38 
    39 #include "SDL_atarikeys.h"
    40 #include "SDL_atarievents_c.h"
    41 #include "SDL_xbiosevents_c.h"
    42 
    43 /* To save state of keyboard */
    44 #define ATARIBIOS_MAXKEYS 128
    45 
    46 static unsigned char gemdos_currentkeyboard[ATARIBIOS_MAXKEYS];
    47 static unsigned char gemdos_previouskeyboard[ATARIBIOS_MAXKEYS];
    48 static unsigned char gemdos_currentascii[ATARIBIOS_MAXKEYS];
    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 enum {
    62 	DEV_BUSY=0,
    63 	DEV_READY
    64 };
    65 
    66 /* The translation tables from a console scancode to a SDL keysym */
    67 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    68 
    69 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
    70 	SDL_bool pressed);
    71 static void UpdateSpecialKeys(int special_keys_state);
    72 
    73 void AtariGemdos_InitOSKeymap(_THIS)
    74 {
    75 	int i, vectors_mask;
    76 	unsigned long dummy;
    77 
    78 	SDL_memset(gemdos_currentkeyboard, 0, sizeof(gemdos_currentkeyboard));
    79 	SDL_memset(gemdos_previouskeyboard, 0, sizeof(gemdos_previouskeyboard));
    80 
    81 	/* Initialize keymap */
    82 	for ( i=0; i<sizeof(keymap); i++ )
    83 		keymap[i] = SDLK_UNKNOWN;
    84 
    85 	/* Functions keys */
    86 	for ( i = 0; i<10; i++ )
    87 		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
    88 
    89 	/* Cursor keypad */
    90 	keymap[SCANCODE_HELP] = SDLK_HELP;
    91 	keymap[SCANCODE_UNDO] = SDLK_UNDO;
    92 	keymap[SCANCODE_INSERT] = SDLK_INSERT;
    93 	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
    94 	keymap[SCANCODE_UP] = SDLK_UP;
    95 	keymap[SCANCODE_DOWN] = SDLK_DOWN;
    96 	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
    97 	keymap[SCANCODE_LEFT] = SDLK_LEFT;
    98 
    99 	/* Special keys */
   100 	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
   101 	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
   102 	keymap[SCANCODE_TAB] = SDLK_TAB;
   103 	keymap[SCANCODE_ENTER] = SDLK_RETURN;
   104 	keymap[SCANCODE_DELETE] = SDLK_DELETE;
   105 	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
   106 	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
   107 	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
   108 	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
   109 	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
   110 
   111 	vectors_mask = ATARI_XBIOS_MOUSEEVENTS|ATARI_XBIOS_JOYSTICKEVENTS;
   112 	if (Getcookie(C_MiNT, &dummy)==C_FOUND) {
   113 		vectors_mask = 0;
   114 	}
   115 
   116 	SDL_AtariXbios_InstallVectors(vectors_mask);
   117 }
   118 
   119 void AtariGemdos_PumpEvents(_THIS)
   120 {
   121 	int i;
   122 	SDL_keysym keysym;
   123 
   124 	/* Update pressed keys */
   125 	SDL_memset(gemdos_currentkeyboard, 0, ATARIBIOS_MAXKEYS);
   126 
   127 	while (Cconis()!=DEV_BUSY) {
   128 		unsigned long key_pressed;
   129 		unsigned char scancode, asciicode;
   130 
   131 		key_pressed=Cnecin();
   132 
   133 		asciicode = key_pressed;
   134 		scancode = key_pressed >> 16;
   135 
   136 		gemdos_currentkeyboard[scancode]=0xFF;
   137 		gemdos_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 (gemdos_currentkeyboard[i] && !gemdos_previouskeyboard[i])
   147 			SDL_PrivateKeyboard(SDL_PRESSED,
   148 				TranslateKey(i, gemdos_currentascii[i], &keysym, SDL_TRUE));
   149 			
   150 		/* Key unpressed ? */
   151 		if (gemdos_previouskeyboard[i] && !gemdos_currentkeyboard[i])
   152 			SDL_PrivateKeyboard(SDL_RELEASED,
   153 				TranslateKey(i, gemdos_currentascii[i], &keysym, SDL_FALSE));
   154 	}
   155 
   156 	SDL_AtariXbios_PostMouseEvents(this, SDL_TRUE);
   157 
   158 	/* Will be previous table */
   159 	SDL_memcpy(gemdos_previouskeyboard, gemdos_currentkeyboard, ATARIBIOS_MAXKEYS);
   160 }
   161 
   162 static void UpdateSpecialKeys(int special_keys_state)
   163 {
   164 #define UPDATE_SPECIAL_KEYS(numbit,scancode) \
   165 	{	\
   166 		if (special_keys_state & (1<<(numbit))) { \
   167 			gemdos_currentkeyboard[scancode]=0xFF; \
   168 			gemdos_currentascii[scancode]=0; \
   169 		}	\
   170 	}
   171 
   172 	UPDATE_SPECIAL_KEYS(K_RSHIFT, SCANCODE_RIGHTSHIFT);
   173 	UPDATE_SPECIAL_KEYS(K_LSHIFT, SCANCODE_LEFTSHIFT);
   174 	UPDATE_SPECIAL_KEYS(K_CTRL, SCANCODE_LEFTCONTROL);
   175 	UPDATE_SPECIAL_KEYS(K_ALT, SCANCODE_LEFTALT);
   176 	UPDATE_SPECIAL_KEYS(K_CAPSLOCK, SCANCODE_CAPSLOCK);
   177 }
   178 
   179 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
   180 	SDL_bool pressed)
   181 {
   182 	/* Set the keysym information */
   183 	keysym->scancode = scancode;
   184 
   185 	if (asciicode)
   186 		keysym->sym = asciicode;		
   187 	else
   188 		keysym->sym = keymap[scancode];
   189 
   190 	keysym->mod = KMOD_NONE;
   191 	keysym->unicode = 0;
   192 	if (SDL_TranslateUNICODE && pressed) {
   193 		keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
   194 	}
   195 
   196 	return(keysym);
   197 }
   198 
   199 void AtariGemdos_ShutdownEvents(void)
   200 {
   201 	SDL_AtariXbios_RestoreVectors();
   202 }