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