src/video/ataricommon/SDL_biosevents.c
author Sam Lantinga
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1420 2405517b5eab
child 2019 d20f4510c718
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
slouken@281
     1
/*
slouken@281
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@281
     4
slouken@281
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@281
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@281
     9
slouken@281
    10
    This library is distributed in the hope that it will be useful,
slouken@281
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@281
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@281
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@281
    18
slouken@281
    19
    Sam Lantinga
slouken@281
    20
    slouken@libsdl.org
slouken@281
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@281
    23
slouken@281
    24
/*
slouken@281
    25
 *	Atari keyboard events manager, using BIOS
slouken@281
    26
 *
slouken@281
    27
 *	Patrice Mandin
slouken@281
    28
 */
slouken@281
    29
slouken@281
    30
/* Mint includes */
slouken@281
    31
#include <mint/osbind.h>
patmandin@1237
    32
#include <mint/cookie.h>
slouken@281
    33
slouken@1361
    34
#include "../../events/SDL_sysevents.h"
slouken@1361
    35
#include "../../events/SDL_events_c.h"
slouken@281
    36
slouken@281
    37
#include "SDL_atarikeys.h"
patmandin@1209
    38
#include "SDL_atarievents_c.h"
slouken@305
    39
#include "SDL_xbiosevents_c.h"
patmandin@1420
    40
#include "SDL_ataridevmouse_c.h"
slouken@281
    41
slouken@281
    42
/* To save state of keyboard */
slouken@281
    43
#define ATARIBIOS_MAXKEYS 128
slouken@281
    44
slouken@281
    45
static unsigned char bios_currentkeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    46
static unsigned char bios_previouskeyboard[ATARIBIOS_MAXKEYS];
slouken@281
    47
static unsigned char bios_currentascii[ATARIBIOS_MAXKEYS];
patmandin@1420
    48
static SDL_bool use_dev_mouse = SDL_FALSE;
slouken@281
    49
slouken@281
    50
/* Special keys state */
slouken@1895
    51
enum
slouken@1895
    52
{
slouken@1895
    53
    K_RSHIFT = 0,
slouken@1895
    54
    K_LSHIFT,
slouken@1895
    55
    K_CTRL,
slouken@1895
    56
    K_ALT,
slouken@1895
    57
    K_CAPSLOCK,
slouken@1895
    58
    K_CLRHOME,
slouken@1895
    59
    K_INSERT
slouken@281
    60
};
slouken@281
    61
slouken@281
    62
/* The translation tables from a console scancode to a SDL keysym */
slouken@281
    63
static SDLKey keymap[ATARIBIOS_MAXKEYS];
slouken@281
    64
slouken@1895
    65
static SDL_keysym *TranslateKey(int scancode, int asciicode,
slouken@1895
    66
                                SDL_keysym * keysym, SDL_bool pressed);
slouken@281
    67
static void UpdateSpecialKeys(int special_keys_state);
slouken@281
    68
slouken@1895
    69
void
slouken@1895
    70
AtariBios_InitOSKeymap(_THIS)
slouken@281
    71
{
slouken@1895
    72
    int i, vectors_mask;
slouken@1895
    73
    unsigned long dummy;
slouken@281
    74
slouken@1895
    75
    SDL_memset(bios_currentkeyboard, 0, sizeof(bios_currentkeyboard));
slouken@1895
    76
    SDL_memset(bios_previouskeyboard, 0, sizeof(bios_previouskeyboard));
slouken@281
    77
slouken@1895
    78
    /* Initialize keymap */
slouken@1895
    79
    for (i = 0; i < sizeof(keymap); i++)
slouken@1895
    80
        keymap[i] = SDLK_UNKNOWN;
slouken@1895
    81
slouken@1895
    82
    /* Functions keys */
slouken@1895
    83
    for (i = 0; i < 10; i++)
slouken@1895
    84
        keymap[SCANCODE_F1 + i] = SDLK_F1 + i;
slouken@281
    85
slouken@1895
    86
    /* Cursor keypad */
slouken@1895
    87
    keymap[SCANCODE_HELP] = SDLK_HELP;
slouken@1895
    88
    keymap[SCANCODE_UNDO] = SDLK_UNDO;
slouken@1895
    89
    keymap[SCANCODE_INSERT] = SDLK_INSERT;
slouken@1895
    90
    keymap[SCANCODE_CLRHOME] = SDLK_HOME;
slouken@1895
    91
    keymap[SCANCODE_UP] = SDLK_UP;
slouken@1895
    92
    keymap[SCANCODE_DOWN] = SDLK_DOWN;
slouken@1895
    93
    keymap[SCANCODE_RIGHT] = SDLK_RIGHT;
slouken@1895
    94
    keymap[SCANCODE_LEFT] = SDLK_LEFT;
slouken@281
    95
slouken@1895
    96
    /* Special keys */
slouken@1895
    97
    keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
slouken@1895
    98
    keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
slouken@1895
    99
    keymap[SCANCODE_TAB] = SDLK_TAB;
slouken@1895
   100
    keymap[SCANCODE_ENTER] = SDLK_RETURN;
slouken@1895
   101
    keymap[SCANCODE_DELETE] = SDLK_DELETE;
slouken@1895
   102
    keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
slouken@1895
   103
    keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
slouken@1895
   104
    keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
slouken@1895
   105
    keymap[SCANCODE_LEFTALT] = SDLK_LALT;
slouken@1895
   106
    keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
slouken@281
   107
slouken@1895
   108
    use_dev_mouse = (SDL_AtariDevMouse_Open() != 0) ? SDL_TRUE : SDL_FALSE;
patmandin@1420
   109
slouken@1895
   110
    vectors_mask = ATARI_XBIOS_JOYSTICKEVENTS;  /* XBIOS joystick events */
slouken@1895
   111
    if (!use_dev_mouse) {
slouken@1895
   112
        vectors_mask |= ATARI_XBIOS_MOUSEEVENTS;        /* XBIOS mouse events */
slouken@1895
   113
    }
slouken@1895
   114
    if (Getcookie(C_MiNT, &dummy) == C_FOUND) {
slouken@1895
   115
        vectors_mask = 0;
slouken@1895
   116
    }
patmandin@1237
   117
slouken@1895
   118
    SDL_AtariXbios_InstallVectors(vectors_mask);
slouken@281
   119
}
slouken@281
   120
slouken@1895
   121
void
slouken@1895
   122
AtariBios_PumpEvents(_THIS)
slouken@281
   123
{
slouken@1895
   124
    int i;
slouken@1895
   125
    SDL_keysym keysym;
slouken@281
   126
slouken@1895
   127
    /* Update pressed keys */
slouken@1895
   128
    SDL_memset(bios_currentkeyboard, 0, ATARIBIOS_MAXKEYS);
slouken@281
   129
slouken@1895
   130
    while (Bconstat(_CON)) {
slouken@1895
   131
        unsigned long key_pressed;
slouken@1895
   132
        unsigned char asciicode, scancode;
slouken@281
   133
slouken@1895
   134
        key_pressed = Bconin(_CON);
slouken@281
   135
slouken@1895
   136
        asciicode = key_pressed;
slouken@1895
   137
        scancode = key_pressed >> 16;
slouken@281
   138
slouken@1895
   139
        bios_currentkeyboard[scancode] = 0xFF;
slouken@1895
   140
        bios_currentascii[scancode] = asciicode;
slouken@1895
   141
    }
slouken@1895
   142
slouken@1895
   143
    /* Read special keys */
slouken@1895
   144
    UpdateSpecialKeys(Kbshift(-1));
slouken@281
   145
slouken@1895
   146
    /* Now generate events */
slouken@1895
   147
    for (i = 0; i < ATARIBIOS_MAXKEYS; i++) {
slouken@1895
   148
        /* Key pressed ? */
slouken@1895
   149
        if (bios_currentkeyboard[i] && !bios_previouskeyboard[i])
slouken@1895
   150
            SDL_PrivateKeyboard(SDL_PRESSED,
slouken@1895
   151
                                TranslateKey(i, bios_currentascii[i],
slouken@1895
   152
                                             &keysym, SDL_TRUE));
slouken@281
   153
slouken@1895
   154
        /* Key unpressed ? */
slouken@1895
   155
        if (bios_previouskeyboard[i] && !bios_currentkeyboard[i])
slouken@1895
   156
            SDL_PrivateKeyboard(SDL_RELEASED,
slouken@1895
   157
                                TranslateKey(i, bios_currentascii[i],
slouken@1895
   158
                                             &keysym, SDL_FALSE));
slouken@1895
   159
    }
slouken@281
   160
slouken@1895
   161
    if (use_dev_mouse) {
slouken@1895
   162
        SDL_AtariDevMouse_PostMouseEvents(this, SDL_TRUE);
slouken@1895
   163
    } else {
slouken@1895
   164
        SDL_AtariXbios_PostMouseEvents(this, SDL_TRUE);
slouken@1895
   165
    }
slouken@281
   166
slouken@1895
   167
    /* Will be previous table */
slouken@1895
   168
    SDL_memcpy(bios_previouskeyboard, bios_currentkeyboard,
slouken@1895
   169
               ATARIBIOS_MAXKEYS);
slouken@281
   170
}
slouken@281
   171
slouken@1895
   172
static void
slouken@1895
   173
UpdateSpecialKeys(int special_keys_state)
slouken@281
   174
{
slouken@281
   175
#define UPDATE_SPECIAL_KEYS(numbit,scancode) \
slouken@281
   176
	{	\
slouken@281
   177
		if (special_keys_state & (1<<(numbit))) { \
slouken@281
   178
			bios_currentkeyboard[scancode]=0xFF; \
slouken@281
   179
			bios_currentascii[scancode]=0; \
slouken@281
   180
		}	\
slouken@281
   181
	}
slouken@281
   182
slouken@1895
   183
    UPDATE_SPECIAL_KEYS(K_RSHIFT, SCANCODE_RIGHTSHIFT);
slouken@1895
   184
    UPDATE_SPECIAL_KEYS(K_LSHIFT, SCANCODE_LEFTSHIFT);
slouken@1895
   185
    UPDATE_SPECIAL_KEYS(K_CTRL, SCANCODE_LEFTCONTROL);
slouken@1895
   186
    UPDATE_SPECIAL_KEYS(K_ALT, SCANCODE_LEFTALT);
slouken@1895
   187
    UPDATE_SPECIAL_KEYS(K_CAPSLOCK, SCANCODE_CAPSLOCK);
slouken@281
   188
}
slouken@281
   189
slouken@1895
   190
static SDL_keysym *
slouken@1895
   191
TranslateKey(int scancode, int asciicode, SDL_keysym * keysym,
slouken@1895
   192
             SDL_bool pressed)
slouken@281
   193
{
slouken@1895
   194
    /* Set the keysym information */
slouken@1895
   195
    keysym->scancode = scancode;
slouken@281
   196
slouken@1895
   197
    if (asciicode)
slouken@1895
   198
        keysym->sym = asciicode;
slouken@1895
   199
    else
slouken@1895
   200
        keysym->sym = keymap[scancode];
slouken@281
   201
slouken@1895
   202
    keysym->mod = KMOD_NONE;
slouken@1895
   203
    keysym->unicode = 0;
slouken@1895
   204
    if (SDL_TranslateUNICODE && pressed) {
slouken@1895
   205
        keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
slouken@1895
   206
    }
slouken@281
   207
slouken@1895
   208
    return (keysym);
slouken@281
   209
}
slouken@281
   210
slouken@1895
   211
void
slouken@1895
   212
AtariBios_ShutdownEvents(void)
slouken@281
   213
{
slouken@1895
   214
    SDL_AtariXbios_RestoreVectors();
slouken@1895
   215
    if (use_dev_mouse) {
slouken@1895
   216
        SDL_AtariDevMouse_Close();
slouken@1895
   217
    }
slouken@281
   218
}
slouken@1895
   219
slouken@1895
   220
/* vi: set ts=4 sw=4 expandtab: */