src/video/ataricommon/SDL_ikbdevents.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 hardware IKBD
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>
slouken@281
    32
slouken@1361
    33
#include "../../events/SDL_sysevents.h"
slouken@1361
    34
#include "../../events/SDL_events_c.h"
slouken@281
    35
slouken@281
    36
#include "SDL_atarikeys.h"
patmandin@1209
    37
#include "SDL_atarievents_c.h"
slouken@281
    38
#include "SDL_ikbdinterrupt_s.h"
slouken@281
    39
slouken@281
    40
/* Special keys state */
slouken@1895
    41
enum
slouken@1895
    42
{
slouken@1895
    43
    K_RSHIFT = 0,
slouken@1895
    44
    K_LSHIFT,
slouken@1895
    45
    K_CTRL,
slouken@1895
    46
    K_ALT,
slouken@1895
    47
    K_CAPSLOCK,
slouken@1895
    48
    K_CLRHOME,
slouken@1895
    49
    K_INSERT
slouken@281
    50
};
slouken@281
    51
slouken@281
    52
#define ATARIBIOS_MAXKEYS 128
slouken@281
    53
patmandin@1082
    54
#define KEY_PRESSED		0xff
patmandin@1082
    55
#define KEY_UNDEFINED	0x80
patmandin@1082
    56
#define KEY_RELEASED	0x00
slouken@281
    57
slouken@281
    58
/* The translation tables from a console scancode to a SDL keysym */
slouken@281
    59
#define KT_NOCHANGE -1
slouken@281
    60
slouken@1895
    61
enum
slouken@1895
    62
{
slouken@1895
    63
    KT_UNSHIFT = 0,
slouken@1895
    64
    KT_SHIFT = 1,
slouken@1895
    65
    KT_CAPS = 2
slouken@281
    66
};
slouken@281
    67
slouken@1895
    68
static Uint16 atari_prevmouseb; /* save state of mouse buttons */
slouken@1895
    69
static int caps_state;          /* caps lock state */
slouken@281
    70
_KEYTAB *curtables;
slouken@281
    71
static unsigned char *tab_unshift, *tab_shift, *tab_caps;
slouken@281
    72
static SDLKey keymap[ATARIBIOS_MAXKEYS];
slouken@281
    73
slouken@1895
    74
static SDL_keysym *TranslateKey(int scancode, int numkeytable,
slouken@1895
    75
                                SDL_keysym * keysym, SDL_bool pressed);
slouken@281
    76
slouken@1895
    77
void
slouken@1895
    78
AtariIkbd_InitOSKeymap(_THIS)
slouken@281
    79
{
slouken@1895
    80
    int i;
slouken@281
    81
slouken@1895
    82
    SDL_memset(SDL_AtariIkbd_keyboard, KEY_UNDEFINED, ATARIBIOS_MAXKEYS);
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
    /* Read XBIOS tables for scancode -> ascii translation */
slouken@1895
   115
    curtables = Keytbl(KT_NOCHANGE, KT_NOCHANGE, KT_NOCHANGE);
slouken@1895
   116
    tab_unshift = curtables->unshift;
slouken@1895
   117
    tab_shift = curtables->shift;
slouken@1895
   118
    tab_caps = curtables->caps;
slouken@281
   119
slouken@1895
   120
    /* Set Caps lock initial state */
slouken@1895
   121
    caps_state = (Kbshift(-1) & (1 << K_CAPSLOCK));
slouken@281
   122
slouken@1895
   123
    /* Now install our handler */
slouken@1895
   124
    SDL_AtariIkbd_mouseb = SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
slouken@1895
   125
    atari_prevmouseb = 0;
slouken@281
   126
slouken@1895
   127
    Supexec(SDL_AtariIkbdInstall);
slouken@281
   128
}
slouken@281
   129
slouken@1895
   130
static int
slouken@1895
   131
atari_GetButton(int button)
slouken@281
   132
{
slouken@1895
   133
    switch (button) {
slouken@1895
   134
    case 0:
slouken@1895
   135
        return SDL_BUTTON_RIGHT;
slouken@1895
   136
        break;
slouken@1895
   137
    case 1:
slouken@1895
   138
    default:
slouken@1895
   139
        return SDL_BUTTON_LEFT;
slouken@1895
   140
        break;
slouken@1895
   141
    }
slouken@281
   142
}
slouken@281
   143
slouken@1895
   144
void
slouken@1895
   145
AtariIkbd_PumpEvents(_THIS)
slouken@281
   146
{
slouken@1895
   147
    int i;
slouken@1895
   148
    SDL_keysym keysym;
slouken@1895
   149
    int specialkeys;
slouken@281
   150
slouken@1895
   151
        /*--- Send keyboard events ---*/
slouken@281
   152
slouken@1895
   153
    /* Update caps lock state */
slouken@1895
   154
    if (SDL_AtariIkbd_keyboard[SCANCODE_CAPSLOCK] == KEY_PRESSED) {
slouken@1895
   155
        caps_state ^= 1;
slouken@1895
   156
    }
slouken@281
   157
slouken@1895
   158
    /* Choose the translation table */
slouken@1895
   159
    specialkeys = KT_UNSHIFT;
slouken@1895
   160
    if ((SDL_AtariIkbd_keyboard[SCANCODE_LEFTSHIFT] == KEY_PRESSED)
slouken@1895
   161
        || (SDL_AtariIkbd_keyboard[SCANCODE_RIGHTSHIFT] == KEY_PRESSED)) {
slouken@1895
   162
        specialkeys = KT_SHIFT;
slouken@1895
   163
    }
slouken@1895
   164
    if (caps_state) {
slouken@1895
   165
        specialkeys = KT_CAPS;
slouken@1895
   166
    }
slouken@281
   167
slouken@1895
   168
    /* Now generate events */
slouken@1895
   169
    for (i = 0; i < ATARIBIOS_MAXKEYS; i++) {
slouken@1895
   170
        /* Key pressed ? */
slouken@1895
   171
        if (SDL_AtariIkbd_keyboard[i] == KEY_PRESSED) {
slouken@1895
   172
            SDL_PrivateKeyboard(SDL_PRESSED,
slouken@1895
   173
                                TranslateKey(i, specialkeys, &keysym,
slouken@1895
   174
                                             SDL_TRUE));
slouken@1895
   175
            SDL_AtariIkbd_keyboard[i] = KEY_UNDEFINED;
slouken@1895
   176
        }
slouken@281
   177
slouken@1895
   178
        /* Key released ? */
slouken@1895
   179
        if (SDL_AtariIkbd_keyboard[i] == KEY_RELEASED) {
slouken@1895
   180
            SDL_PrivateKeyboard(SDL_RELEASED,
slouken@1895
   181
                                TranslateKey(i, specialkeys, &keysym,
slouken@1895
   182
                                             SDL_FALSE));
slouken@1895
   183
            SDL_AtariIkbd_keyboard[i] = KEY_UNDEFINED;
slouken@1895
   184
        }
slouken@1895
   185
    }
slouken@281
   186
slouken@1895
   187
        /*--- Send mouse events ---*/
slouken@281
   188
slouken@1895
   189
    /* Mouse motion ? */
slouken@1895
   190
    if (SDL_AtariIkbd_mousex || SDL_AtariIkbd_mousey) {
slouken@1895
   191
        SDL_PrivateMouseMotion(0, 1, SDL_AtariIkbd_mousex,
slouken@1895
   192
                               SDL_AtariIkbd_mousey);
slouken@1895
   193
        SDL_AtariIkbd_mousex = SDL_AtariIkbd_mousey = 0;
slouken@1895
   194
    }
slouken@281
   195
slouken@1895
   196
    /* Mouse button ? */
slouken@1895
   197
    if (SDL_AtariIkbd_mouseb != atari_prevmouseb) {
slouken@1895
   198
        for (i = 0; i < 2; i++) {
slouken@1895
   199
            int curbutton, prevbutton;
slouken@281
   200
slouken@1895
   201
            curbutton = SDL_AtariIkbd_mouseb & (1 << i);
slouken@1895
   202
            prevbutton = atari_prevmouseb & (1 << i);
slouken@1895
   203
slouken@1895
   204
            if (curbutton && !prevbutton) {
slouken@1895
   205
                SDL_PrivateMouseButton(SDL_PRESSED, atari_GetButton(i), 0, 0);
slouken@1895
   206
            }
slouken@1895
   207
            if (!curbutton && prevbutton) {
slouken@1895
   208
                SDL_PrivateMouseButton(SDL_RELEASED,
slouken@1895
   209
                                       atari_GetButton(i), 0, 0);
slouken@1895
   210
            }
slouken@1895
   211
        }
slouken@1895
   212
        atari_prevmouseb = SDL_AtariIkbd_mouseb;
slouken@1895
   213
    }
slouken@281
   214
}
slouken@281
   215
slouken@1895
   216
static SDL_keysym *
slouken@1895
   217
TranslateKey(int scancode, int numkeytable, SDL_keysym * keysym,
slouken@1895
   218
             SDL_bool pressed)
slouken@281
   219
{
slouken@1895
   220
    unsigned char asciicode;
slouken@281
   221
slouken@1895
   222
    /* Set the keysym information */
slouken@1895
   223
    keysym->scancode = scancode;
slouken@281
   224
slouken@1895
   225
    asciicode = 0;
slouken@1895
   226
    switch (numkeytable) {
slouken@1895
   227
    case KT_UNSHIFT:
slouken@1895
   228
        asciicode = tab_unshift[scancode];
slouken@1895
   229
        break;
slouken@1895
   230
    case KT_SHIFT:
slouken@1895
   231
        asciicode = tab_shift[scancode];
slouken@1895
   232
        break;
slouken@1895
   233
    case KT_CAPS:
slouken@1895
   234
        asciicode = tab_caps[scancode];
slouken@1895
   235
        break;
slouken@1895
   236
    }
slouken@281
   237
slouken@1895
   238
    if (asciicode)
slouken@1895
   239
        keysym->sym = asciicode;
slouken@1895
   240
    else
slouken@1895
   241
        keysym->sym = keymap[scancode];
slouken@281
   242
slouken@1895
   243
    keysym->mod = KMOD_NONE;
slouken@1895
   244
    keysym->unicode = 0;
slouken@1895
   245
    if (SDL_TranslateUNICODE && pressed) {
slouken@1895
   246
        keysym->unicode = SDL_AtariToUnicodeTable[asciicode];
slouken@1895
   247
    }
slouken@281
   248
slouken@1895
   249
    return (keysym);
slouken@281
   250
}
slouken@281
   251
slouken@1895
   252
void
slouken@1895
   253
AtariIkbd_ShutdownEvents(void)
slouken@281
   254
{
slouken@1895
   255
    Supexec(SDL_AtariIkbdUninstall);
slouken@281
   256
}
slouken@1895
   257
slouken@1895
   258
/* vi: set ts=4 sw=4 expandtab: */