src/video/ataricommon/SDL_biosevents.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 07 Sep 2006 20:50:13 +0000
changeset 2019 d20f4510c718
parent 1895 c121d94672cb
child 2030 d774e9d7c2a8
permissions -rw-r--r--
/me dumb, sizeof(keymap != number of elements
     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 {
    53     K_RSHIFT = 0,
    54     K_LSHIFT,
    55     K_CTRL,
    56     K_ALT,
    57     K_CAPSLOCK,
    58     K_CLRHOME,
    59     K_INSERT
    60 };
    61 
    62 /* The translation tables from a console scancode to a SDL keysym */
    63 static SDLKey keymap[ATARIBIOS_MAXKEYS];
    64 
    65 static SDL_keysym *TranslateKey(int scancode, int asciicode,
    66                                 SDL_keysym * keysym, SDL_bool pressed);
    67 static void UpdateSpecialKeys(int special_keys_state);
    68 
    69 void
    70 AtariBios_InitOSKeymap(_THIS)
    71 {
    72     int i, vectors_mask;
    73     unsigned long dummy;
    74 
    75     SDL_memset(bios_currentkeyboard, 0, sizeof(bios_currentkeyboard));
    76     SDL_memset(bios_previouskeyboard, 0, sizeof(bios_previouskeyboard));
    77 
    78     /* Initialize keymap */
    79     for (i = 0; i < ATARIBIOS_MAXKEYS; 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     use_dev_mouse = (SDL_AtariDevMouse_Open() != 0) ? SDL_TRUE : SDL_FALSE;
   109 
   110     vectors_mask = ATARI_XBIOS_JOYSTICKEVENTS;  /* XBIOS joystick events */
   111     if (!use_dev_mouse) {
   112         vectors_mask |= ATARI_XBIOS_MOUSEEVENTS;        /* XBIOS mouse events */
   113     }
   114     if (Getcookie(C_MiNT, &dummy) == C_FOUND) {
   115         vectors_mask = 0;
   116     }
   117 
   118     SDL_AtariXbios_InstallVectors(vectors_mask);
   119 }
   120 
   121 void
   122 AtariBios_PumpEvents(_THIS)
   123 {
   124     int i;
   125     SDL_keysym keysym;
   126 
   127     /* Update pressed keys */
   128     SDL_memset(bios_currentkeyboard, 0, ATARIBIOS_MAXKEYS);
   129 
   130     while (Bconstat(_CON)) {
   131         unsigned long key_pressed;
   132         unsigned char asciicode, scancode;
   133 
   134         key_pressed = Bconin(_CON);
   135 
   136         asciicode = key_pressed;
   137         scancode = key_pressed >> 16;
   138 
   139         bios_currentkeyboard[scancode] = 0xFF;
   140         bios_currentascii[scancode] = asciicode;
   141     }
   142 
   143     /* Read special keys */
   144     UpdateSpecialKeys(Kbshift(-1));
   145 
   146     /* Now generate events */
   147     for (i = 0; i < ATARIBIOS_MAXKEYS; i++) {
   148         /* Key pressed ? */
   149         if (bios_currentkeyboard[i] && !bios_previouskeyboard[i])
   150             SDL_PrivateKeyboard(SDL_PRESSED,
   151                                 TranslateKey(i, bios_currentascii[i],
   152                                              &keysym, SDL_TRUE));
   153 
   154         /* Key unpressed ? */
   155         if (bios_previouskeyboard[i] && !bios_currentkeyboard[i])
   156             SDL_PrivateKeyboard(SDL_RELEASED,
   157                                 TranslateKey(i, bios_currentascii[i],
   158                                              &keysym, SDL_FALSE));
   159     }
   160 
   161     if (use_dev_mouse) {
   162         SDL_AtariDevMouse_PostMouseEvents(this, SDL_TRUE);
   163     } else {
   164         SDL_AtariXbios_PostMouseEvents(this, SDL_TRUE);
   165     }
   166 
   167     /* Will be previous table */
   168     SDL_memcpy(bios_previouskeyboard, bios_currentkeyboard,
   169                ATARIBIOS_MAXKEYS);
   170 }
   171 
   172 static void
   173 UpdateSpecialKeys(int special_keys_state)
   174 {
   175 #define UPDATE_SPECIAL_KEYS(numbit,scancode) \
   176 	{	\
   177 		if (special_keys_state & (1<<(numbit))) { \
   178 			bios_currentkeyboard[scancode]=0xFF; \
   179 			bios_currentascii[scancode]=0; \
   180 		}	\
   181 	}
   182 
   183     UPDATE_SPECIAL_KEYS(K_RSHIFT, SCANCODE_RIGHTSHIFT);
   184     UPDATE_SPECIAL_KEYS(K_LSHIFT, SCANCODE_LEFTSHIFT);
   185     UPDATE_SPECIAL_KEYS(K_CTRL, SCANCODE_LEFTCONTROL);
   186     UPDATE_SPECIAL_KEYS(K_ALT, SCANCODE_LEFTALT);
   187     UPDATE_SPECIAL_KEYS(K_CAPSLOCK, SCANCODE_CAPSLOCK);
   188 }
   189 
   190 static SDL_keysym *
   191 TranslateKey(int scancode, int asciicode, SDL_keysym * keysym,
   192              SDL_bool pressed)
   193 {
   194     /* Set the keysym information */
   195     keysym->scancode = scancode;
   196 
   197     if (asciicode)
   198         keysym->sym = asciicode;
   199     else
   200         keysym->sym = keymap[scancode];
   201 
   202     keysym->mod = KMOD_NONE;
   203     keysym->unicode = 0;
   204     if (SDL_TranslateUNICODE && pressed) {
   205         keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
   206     }
   207 
   208     return (keysym);
   209 }
   210 
   211 void
   212 AtariBios_ShutdownEvents(void)
   213 {
   214     SDL_AtariXbios_RestoreVectors();
   215     if (use_dev_mouse) {
   216         SDL_AtariDevMouse_Close();
   217     }
   218 }
   219 
   220 /* vi: set ts=4 sw=4 expandtab: */