src/video/ataricommon/SDL_gemdosevents.c
author Patrice Mandin
Tue, 31 Jan 2006 21:33:35 +0000
changeset 1310 a201a8c62522
parent 1237 a8068adf156b
child 1312 c9b51268668f
permissions -rw-r--r--
Add flag to post mouse button events or not, and use defined function to post them
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /*
    29  *	Atari keyboard events manager, using Gemdos
    30  *
    31  *	Patrice Mandin
    32  */
    33 
    34 #include <string.h>
    35 
    36 /* Mint includes */
    37 #include <mint/osbind.h>
    38 #include <mint/cookie.h>
    39 
    40 #include "SDL.h"
    41 #include "SDL_sysevents.h"
    42 #include "SDL_events_c.h"
    43 
    44 #include "SDL_atarikeys.h"
    45 #include "SDL_atarievents_c.h"
    46 #include "SDL_xbiosevents_c.h"
    47 
    48 /* To save state of keyboard */
    49 #define ATARIBIOS_MAXKEYS 128
    50 
    51 static unsigned char gemdos_currentkeyboard[ATARIBIOS_MAXKEYS];
    52 static unsigned char gemdos_previouskeyboard[ATARIBIOS_MAXKEYS];
    53 static unsigned char gemdos_currentascii[ATARIBIOS_MAXKEYS];
    54 
    55 /* Special keys state */
    56 enum {
    57 	K_RSHIFT=0,
    58 	K_LSHIFT,
    59 	K_CTRL,
    60 	K_ALT,
    61 	K_CAPSLOCK,
    62 	K_CLRHOME,
    63 	K_INSERT
    64 };
    65 
    66 enum {
    67 	DEV_BUSY=0,
    68 	DEV_READY
    69 };
    70 
    71 /* The translation tables from a console scancode to a SDL keysym */
    72 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    73 
    74 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
    75 	SDL_bool pressed);
    76 static void UpdateSpecialKeys(int special_keys_state);
    77 
    78 void AtariGemdos_InitOSKeymap(_THIS)
    79 {
    80 	int i, vectors_mask;
    81 	unsigned long dummy;
    82 
    83 	memset(gemdos_currentkeyboard, 0, sizeof(gemdos_currentkeyboard));
    84 	memset(gemdos_previouskeyboard, 0, sizeof(gemdos_previouskeyboard));
    85 
    86 	/* Initialize keymap */
    87 	for ( i=0; i<sizeof(keymap); i++ )
    88 		keymap[i] = SDLK_UNKNOWN;
    89 
    90 	/* Functions keys */
    91 	for ( i = 0; i<10; i++ )
    92 		keymap[SCANCODE_F1 + i] = SDLK_F1+i;
    93 
    94 	/* Cursor keypad */
    95 	keymap[SCANCODE_HELP] = SDLK_HELP;
    96 	keymap[SCANCODE_UNDO] = SDLK_UNDO;
    97 	keymap[SCANCODE_INSERT] = SDLK_INSERT;
    98 	keymap[SCANCODE_CLRHOME] = SDLK_HOME;
    99 	keymap[SCANCODE_UP] = SDLK_UP;
   100 	keymap[SCANCODE_DOWN] = SDLK_DOWN;
   101 	keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
   102 	keymap[SCANCODE_LEFT] = SDLK_LEFT;
   103 
   104 	/* Special keys */
   105 	keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
   106 	keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
   107 	keymap[SCANCODE_TAB] = SDLK_TAB;
   108 	keymap[SCANCODE_ENTER] = SDLK_RETURN;
   109 	keymap[SCANCODE_DELETE] = SDLK_DELETE;
   110 	keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
   111 	keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
   112 	keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
   113 	keymap[SCANCODE_LEFTALT] = SDLK_LALT;
   114 	keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
   115 
   116 	vectors_mask = ATARI_XBIOS_MOUSEEVENTS|ATARI_XBIOS_JOYSTICKEVENTS;
   117 	if (Getcookie(C_MiNT, &dummy)==C_FOUND) {
   118 		vectors_mask = 0;
   119 	}
   120 
   121 	SDL_AtariXbios_InstallVectors(vectors_mask);
   122 }
   123 
   124 void AtariGemdos_PumpEvents(_THIS)
   125 {
   126 	int i;
   127 	SDL_keysym keysym;
   128 
   129 	/* Update pressed keys */
   130 	memset(gemdos_currentkeyboard, 0, ATARIBIOS_MAXKEYS);
   131 
   132 	while (Cconis()!=DEV_BUSY) {
   133 		unsigned long key_pressed;
   134 		unsigned char scancode, asciicode;
   135 
   136 		key_pressed=Cnecin();
   137 
   138 		asciicode = key_pressed;
   139 		scancode = key_pressed >> 16;
   140 
   141 		gemdos_currentkeyboard[scancode]=0xFF;
   142 		gemdos_currentascii[scancode]=asciicode;
   143 	}
   144 
   145 	/* Read special keys */
   146 	UpdateSpecialKeys(Kbshift(-1));
   147 
   148 	/* Now generate events */
   149 	for (i=0; i<ATARIBIOS_MAXKEYS; i++) {
   150 		/* Key pressed ? */
   151 		if (gemdos_currentkeyboard[i] && !gemdos_previouskeyboard[i])
   152 			SDL_PrivateKeyboard(SDL_PRESSED,
   153 				TranslateKey(i, gemdos_currentascii[i], &keysym, SDL_TRUE));
   154 			
   155 		/* Key unpressed ? */
   156 		if (gemdos_previouskeyboard[i] && !gemdos_currentkeyboard[i])
   157 			SDL_PrivateKeyboard(SDL_RELEASED,
   158 				TranslateKey(i, gemdos_currentascii[i], &keysym, SDL_FALSE));
   159 	}
   160 
   161 	SDL_AtariXbios_PostMouseEvents(this, SDL_TRUE);
   162 
   163 	/* Will be previous table */
   164 	memcpy(gemdos_previouskeyboard, gemdos_currentkeyboard, ATARIBIOS_MAXKEYS);
   165 }
   166 
   167 static void UpdateSpecialKeys(int special_keys_state)
   168 {
   169 #define UPDATE_SPECIAL_KEYS(numbit,scancode) \
   170 	{	\
   171 		if (special_keys_state & (1<<(numbit))) { \
   172 			gemdos_currentkeyboard[scancode]=0xFF; \
   173 			gemdos_currentascii[scancode]=0; \
   174 		}	\
   175 	}
   176 
   177 	UPDATE_SPECIAL_KEYS(K_RSHIFT, SCANCODE_RIGHTSHIFT);
   178 	UPDATE_SPECIAL_KEYS(K_LSHIFT, SCANCODE_LEFTSHIFT);
   179 	UPDATE_SPECIAL_KEYS(K_CTRL, SCANCODE_LEFTCONTROL);
   180 	UPDATE_SPECIAL_KEYS(K_ALT, SCANCODE_LEFTALT);
   181 	UPDATE_SPECIAL_KEYS(K_CAPSLOCK, SCANCODE_CAPSLOCK);
   182 }
   183 
   184 static SDL_keysym *TranslateKey(int scancode, int asciicode, SDL_keysym *keysym,
   185 	SDL_bool pressed)
   186 {
   187 	/* Set the keysym information */
   188 	keysym->scancode = scancode;
   189 
   190 	if (asciicode)
   191 		keysym->sym = asciicode;		
   192 	else
   193 		keysym->sym = keymap[scancode];
   194 
   195 	keysym->mod = KMOD_NONE;
   196 	keysym->unicode = 0;
   197 	if (SDL_TranslateUNICODE && pressed) {
   198 		keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
   199 	}
   200 
   201 	return(keysym);
   202 }
   203 
   204 void AtariGemdos_ShutdownEvents(void)
   205 {
   206 	SDL_AtariXbios_RestoreVectors();
   207 }