src/video/vgl/SDL_vglevents.c
author Edgar Simo <bobbens@gmail.com>
Sun, 06 Jul 2008 17:06:37 +0000
branchgsoc2008_force_feedback
changeset 2498 ab567bd667bf
parent 1895 c121d94672cb
child 2669 e27bdcc80744
permissions -rw-r--r--
Fixed various mistakes in the doxygen.
slouken@75
     1
/*
slouken@75
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@75
     4
slouken@75
     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@75
     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@75
     9
slouken@75
    10
    This library is distributed in the hope that it will be useful,
slouken@75
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@75
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@75
    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@75
    18
slouken@75
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@75
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@75
    23
slouken@75
    24
/* Handle the event stream, converting X11 events into SDL events */
slouken@75
    25
slouken@75
    26
#include <stdio.h>
slouken@75
    27
slouken@75
    28
#include <sys/fbio.h>
slouken@75
    29
#include <sys/consio.h>
slouken@75
    30
#include <sys/kbio.h>
slouken@75
    31
#include <vgl.h>
slouken@75
    32
slouken@75
    33
#include "SDL.h"
slouken@75
    34
#include "SDL_thread.h"
slouken@1361
    35
#include "../../events/SDL_sysevents.h"
slouken@1361
    36
#include "../../events/SDL_events_c.h"
slouken@75
    37
#include "SDL_vglvideo.h"
slouken@75
    38
#include "SDL_vglevents_c.h"
slouken@75
    39
slouken@75
    40
/* The translation tables from a console scancode to a SDL keysym */
slouken@75
    41
/* FIXME: Free the keymap when we shut down the video mode */
slouken@75
    42
static keymap_t *vga_keymap = NULL;
slouken@75
    43
static SDLKey keymap[128];
slouken@1895
    44
static SDL_keysym *TranslateKey(int scancode, SDL_keysym * keysym);
slouken@75
    45
slouken@75
    46
static int posted = 0;
slouken@75
    47
static int oldx = -1;
slouken@75
    48
static int oldy = -1;
slouken@75
    49
static struct mouse_info mouseinfo;
slouken@75
    50
slouken@75
    51
/* Ugh, we have to duplicate the kernel's keysym mapping code...
slouken@75
    52
   Oh, it's not so bad. :-)
slouken@75
    53
slouken@75
    54
   FIXME: Add keyboard LED handling code
slouken@75
    55
 */
slouken@1895
    56
int
slouken@1895
    57
VGL_initkeymaps(int fd)
slouken@75
    58
{
slouken@1895
    59
    vga_keymap = SDL_malloc(sizeof(keymap_t));
slouken@1895
    60
    if (!vga_keymap) {
slouken@1895
    61
        SDL_OutOfMemory();
slouken@1895
    62
        return (-1);
slouken@1895
    63
    }
slouken@1895
    64
    if (ioctl(fd, GIO_KEYMAP, vga_keymap) == -1) {
slouken@1895
    65
        SDL_free(vga_keymap);
slouken@1895
    66
        vga_keymap = NULL;
slouken@1895
    67
        SDL_SetError("Unable to get keyboard map");
slouken@1895
    68
        return (-1);
slouken@1895
    69
    }
slouken@1895
    70
    return (0);
slouken@75
    71
}
slouken@75
    72
slouken@1895
    73
static void
slouken@1895
    74
handle_keyboard(_THIS)
slouken@75
    75
{
slouken@1895
    76
    SDL_keysym keysym;
slouken@1895
    77
    int c, pressed, scancode;
slouken@75
    78
slouken@1895
    79
    while ((c = VGLKeyboardGetCh()) != 0) {
slouken@1895
    80
        scancode = c & 0x7F;
slouken@1895
    81
        if (c & 0x80) {
slouken@1895
    82
            pressed = SDL_RELEASED;
slouken@1895
    83
        } else {
slouken@1895
    84
            pressed = SDL_PRESSED;
slouken@1895
    85
        }
slouken@75
    86
slouken@1895
    87
        posted += SDL_PrivateKeyboard(pressed,
slouken@1895
    88
                                      TranslateKey(scancode, &keysym));
slouken@1895
    89
    }
slouken@75
    90
}
slouken@75
    91
slouken@1895
    92
int
slouken@1895
    93
VGL_initmouse(int fd)
slouken@75
    94
{
slouken@1895
    95
    mouseinfo.operation = MOUSE_GETINFO;
slouken@1895
    96
    if (ioctl(fd, CONS_MOUSECTL, &mouseinfo) != 0)
slouken@1895
    97
        return -1;
slouken@75
    98
slouken@1895
    99
    return 0;
slouken@75
   100
}
slouken@75
   101
slouken@1895
   102
static void
slouken@1895
   103
handle_mouse(_THIS)
slouken@75
   104
{
slouken@1895
   105
    char buttons;
slouken@1895
   106
    int x, y;
slouken@1895
   107
    int button_state, state_changed, state;
slouken@1895
   108
    int i;
slouken@75
   109
slouken@1895
   110
    ioctl(0, CONS_MOUSECTL, &mouseinfo);
slouken@1895
   111
    x = mouseinfo.u.data.x;
slouken@1895
   112
    y = mouseinfo.u.data.y;
slouken@1895
   113
    buttons = mouseinfo.u.data.buttons;
slouken@75
   114
slouken@1895
   115
    if ((x != oldx) || (y != oldy)) {
slouken@1895
   116
        posted += SDL_PrivateMouseMotion(0, 0, x, y);
slouken@1895
   117
        oldx = x;
slouken@1895
   118
        oldy = y;
slouken@1895
   119
    }
slouken@75
   120
slouken@1895
   121
    /* See what's changed */
slouken@1895
   122
    button_state = SDL_GetMouseState(NULL, NULL);
slouken@1895
   123
    state_changed = button_state ^ buttons;
slouken@1895
   124
    for (i = 0; i < 8; i++) {
slouken@1895
   125
        if (state_changed & (1 << i)) {
slouken@1895
   126
            if (buttons & (1 << i)) {
slouken@1895
   127
                state = SDL_PRESSED;
slouken@1895
   128
            } else {
slouken@1895
   129
                state = SDL_RELEASED;
slouken@1895
   130
            }
slouken@1895
   131
            posted += SDL_PrivateMouseButton(state, i + 1, 0, 0);
slouken@1895
   132
        }
slouken@1895
   133
    }
slouken@75
   134
}
slouken@1895
   135
slouken@75
   136
slouken@1895
   137
void
slouken@1895
   138
VGL_PumpEvents(_THIS)
slouken@75
   139
{
slouken@1895
   140
    do {
slouken@1895
   141
        posted = 0;
slouken@1895
   142
        handle_keyboard(this);
slouken@1895
   143
        handle_mouse(this);
slouken@1895
   144
    }
slouken@1895
   145
    while (posted != 0);
slouken@75
   146
}
slouken@75
   147
slouken@1895
   148
void
slouken@1895
   149
VGL_InitOSKeymap(_THIS)
slouken@75
   150
{
slouken@1895
   151
    int i;
slouken@75
   152
slouken@1895
   153
    /* Initialize the BeOS key translation table */
slouken@1895
   154
    for (i = 0; i < SDL_arraysize(keymap); ++i)
slouken@1895
   155
        keymap[i] = SDLK_UNKNOWN;
slouken@75
   156
slouken@1895
   157
    keymap[SCANCODE_ESCAPE] = SDLK_ESCAPE;
slouken@1895
   158
    keymap[SCANCODE_1] = SDLK_1;
slouken@1895
   159
    keymap[SCANCODE_2] = SDLK_2;
slouken@1895
   160
    keymap[SCANCODE_3] = SDLK_3;
slouken@1895
   161
    keymap[SCANCODE_4] = SDLK_4;
slouken@1895
   162
    keymap[SCANCODE_5] = SDLK_5;
slouken@1895
   163
    keymap[SCANCODE_6] = SDLK_6;
slouken@1895
   164
    keymap[SCANCODE_7] = SDLK_7;
slouken@1895
   165
    keymap[SCANCODE_8] = SDLK_8;
slouken@1895
   166
    keymap[SCANCODE_9] = SDLK_9;
slouken@1895
   167
    keymap[SCANCODE_0] = SDLK_0;
slouken@1895
   168
    keymap[SCANCODE_MINUS] = SDLK_MINUS;
slouken@1895
   169
    keymap[SCANCODE_EQUAL] = SDLK_EQUALS;
slouken@1895
   170
    keymap[SCANCODE_BACKSPACE] = SDLK_BACKSPACE;
slouken@1895
   171
    keymap[SCANCODE_TAB] = SDLK_TAB;
slouken@1895
   172
    keymap[SCANCODE_Q] = SDLK_q;
slouken@1895
   173
    keymap[SCANCODE_W] = SDLK_w;
slouken@1895
   174
    keymap[SCANCODE_E] = SDLK_e;
slouken@1895
   175
    keymap[SCANCODE_R] = SDLK_r;
slouken@1895
   176
    keymap[SCANCODE_T] = SDLK_t;
slouken@1895
   177
    keymap[SCANCODE_Y] = SDLK_y;
slouken@1895
   178
    keymap[SCANCODE_U] = SDLK_u;
slouken@1895
   179
    keymap[SCANCODE_I] = SDLK_i;
slouken@1895
   180
    keymap[SCANCODE_O] = SDLK_o;
slouken@1895
   181
    keymap[SCANCODE_P] = SDLK_p;
slouken@1895
   182
    keymap[SCANCODE_BRACKET_LEFT] = SDLK_LEFTBRACKET;
slouken@1895
   183
    keymap[SCANCODE_BRACKET_RIGHT] = SDLK_RIGHTBRACKET;
slouken@1895
   184
    keymap[SCANCODE_ENTER] = SDLK_RETURN;
slouken@1895
   185
    keymap[SCANCODE_LEFTCONTROL] = SDLK_LCTRL;
slouken@1895
   186
    keymap[SCANCODE_A] = SDLK_a;
slouken@1895
   187
    keymap[SCANCODE_S] = SDLK_s;
slouken@1895
   188
    keymap[SCANCODE_D] = SDLK_d;
slouken@1895
   189
    keymap[SCANCODE_F] = SDLK_f;
slouken@1895
   190
    keymap[SCANCODE_G] = SDLK_g;
slouken@1895
   191
    keymap[SCANCODE_H] = SDLK_h;
slouken@1895
   192
    keymap[SCANCODE_J] = SDLK_j;
slouken@1895
   193
    keymap[SCANCODE_K] = SDLK_k;
slouken@1895
   194
    keymap[SCANCODE_L] = SDLK_l;
slouken@1895
   195
    keymap[SCANCODE_SEMICOLON] = SDLK_SEMICOLON;
slouken@1895
   196
    keymap[SCANCODE_APOSTROPHE] = SDLK_QUOTE;
slouken@1895
   197
    keymap[SCANCODE_GRAVE] = SDLK_BACKQUOTE;
slouken@1895
   198
    keymap[SCANCODE_LEFTSHIFT] = SDLK_LSHIFT;
slouken@1895
   199
    keymap[SCANCODE_BACKSLASH] = SDLK_BACKSLASH;
slouken@1895
   200
    keymap[SCANCODE_Z] = SDLK_z;
slouken@1895
   201
    keymap[SCANCODE_X] = SDLK_x;
slouken@1895
   202
    keymap[SCANCODE_C] = SDLK_c;
slouken@1895
   203
    keymap[SCANCODE_V] = SDLK_v;
slouken@1895
   204
    keymap[SCANCODE_B] = SDLK_b;
slouken@1895
   205
    keymap[SCANCODE_N] = SDLK_n;
slouken@1895
   206
    keymap[SCANCODE_M] = SDLK_m;
slouken@1895
   207
    keymap[SCANCODE_COMMA] = SDLK_COMMA;
slouken@1895
   208
    keymap[SCANCODE_PERIOD] = SDLK_PERIOD;
slouken@1895
   209
    keymap[SCANCODE_SLASH] = SDLK_SLASH;
slouken@1895
   210
    keymap[SCANCODE_RIGHTSHIFT] = SDLK_RSHIFT;
slouken@1895
   211
    keymap[SCANCODE_KEYPADMULTIPLY] = SDLK_KP_MULTIPLY;
slouken@1895
   212
    keymap[SCANCODE_LEFTALT] = SDLK_LALT;
slouken@1895
   213
    keymap[SCANCODE_SPACE] = SDLK_SPACE;
slouken@1895
   214
    keymap[SCANCODE_CAPSLOCK] = SDLK_CAPSLOCK;
slouken@1895
   215
    keymap[SCANCODE_F1] = SDLK_F1;
slouken@1895
   216
    keymap[SCANCODE_F2] = SDLK_F2;
slouken@1895
   217
    keymap[SCANCODE_F3] = SDLK_F3;
slouken@1895
   218
    keymap[SCANCODE_F4] = SDLK_F4;
slouken@1895
   219
    keymap[SCANCODE_F5] = SDLK_F5;
slouken@1895
   220
    keymap[SCANCODE_F6] = SDLK_F6;
slouken@1895
   221
    keymap[SCANCODE_F7] = SDLK_F7;
slouken@1895
   222
    keymap[SCANCODE_F8] = SDLK_F8;
slouken@1895
   223
    keymap[SCANCODE_F9] = SDLK_F9;
slouken@1895
   224
    keymap[SCANCODE_F10] = SDLK_F10;
slouken@1895
   225
    keymap[SCANCODE_NUMLOCK] = SDLK_NUMLOCK;
slouken@1895
   226
    keymap[SCANCODE_SCROLLLOCK] = SDLK_SCROLLOCK;
slouken@1895
   227
    keymap[SCANCODE_KEYPAD7] = SDLK_KP7;
slouken@1895
   228
    keymap[SCANCODE_CURSORUPLEFT] = SDLK_KP7;
slouken@1895
   229
    keymap[SCANCODE_KEYPAD8] = SDLK_KP8;
slouken@1895
   230
    keymap[SCANCODE_CURSORUP] = SDLK_KP8;
slouken@1895
   231
    keymap[SCANCODE_KEYPAD9] = SDLK_KP9;
slouken@1895
   232
    keymap[SCANCODE_CURSORUPRIGHT] = SDLK_KP9;
slouken@1895
   233
    keymap[SCANCODE_KEYPADMINUS] = SDLK_KP_MINUS;
slouken@1895
   234
    keymap[SCANCODE_KEYPAD4] = SDLK_KP4;
slouken@1895
   235
    keymap[SCANCODE_CURSORLEFT] = SDLK_KP4;
slouken@1895
   236
    keymap[SCANCODE_KEYPAD5] = SDLK_KP5;
slouken@1895
   237
    keymap[SCANCODE_KEYPAD6] = SDLK_KP6;
slouken@1895
   238
    keymap[SCANCODE_CURSORRIGHT] = SDLK_KP6;
slouken@1895
   239
    keymap[SCANCODE_KEYPADPLUS] = SDLK_KP_PLUS;
slouken@1895
   240
    keymap[SCANCODE_KEYPAD1] = SDLK_KP1;
slouken@1895
   241
    keymap[SCANCODE_CURSORDOWNLEFT] = SDLK_KP1;
slouken@1895
   242
    keymap[SCANCODE_KEYPAD2] = SDLK_KP2;
slouken@1895
   243
    keymap[SCANCODE_CURSORDOWN] = SDLK_KP2;
slouken@1895
   244
    keymap[SCANCODE_KEYPAD3] = SDLK_KP3;
slouken@1895
   245
    keymap[SCANCODE_CURSORDOWNRIGHT] = SDLK_KP3;
slouken@1895
   246
    keymap[SCANCODE_KEYPAD0] = SDLK_KP0;
slouken@1895
   247
    keymap[SCANCODE_KEYPADPERIOD] = SDLK_KP_PERIOD;
slouken@1895
   248
    keymap[SCANCODE_LESS] = SDLK_LESS;
slouken@1895
   249
    keymap[SCANCODE_F11] = SDLK_F11;
slouken@1895
   250
    keymap[SCANCODE_F12] = SDLK_F12;
slouken@1895
   251
    keymap[SCANCODE_KEYPADENTER] = SDLK_KP_ENTER;
slouken@1895
   252
    keymap[SCANCODE_RIGHTCONTROL] = SDLK_RCTRL;
slouken@1895
   253
    keymap[SCANCODE_CONTROL] = SDLK_RCTRL;
slouken@1895
   254
    keymap[SCANCODE_KEYPADDIVIDE] = SDLK_KP_DIVIDE;
slouken@1895
   255
    keymap[SCANCODE_PRINTSCREEN] = SDLK_PRINT;
slouken@1895
   256
    keymap[SCANCODE_RIGHTALT] = SDLK_RALT;
slouken@1895
   257
    keymap[SCANCODE_BREAK] = SDLK_BREAK;
slouken@1895
   258
    keymap[SCANCODE_BREAK_ALTERNATIVE] = SDLK_UNKNOWN;
slouken@1895
   259
    keymap[SCANCODE_HOME] = SDLK_HOME;
slouken@1895
   260
    keymap[SCANCODE_CURSORBLOCKUP] = SDLK_UP;
slouken@1895
   261
    keymap[SCANCODE_PAGEUP] = SDLK_PAGEUP;
slouken@1895
   262
    keymap[SCANCODE_CURSORBLOCKLEFT] = SDLK_LEFT;
slouken@1895
   263
    keymap[SCANCODE_CURSORBLOCKRIGHT] = SDLK_RIGHT;
slouken@1895
   264
    keymap[SCANCODE_END] = SDLK_END;
slouken@1895
   265
    keymap[SCANCODE_CURSORBLOCKDOWN] = SDLK_DOWN;
slouken@1895
   266
    keymap[SCANCODE_PAGEDOWN] = SDLK_PAGEDOWN;
slouken@1895
   267
    keymap[SCANCODE_INSERT] = SDLK_INSERT;
slouken@1895
   268
    keymap[SCANCODE_REMOVE] = SDLK_DELETE;
slouken@1895
   269
    keymap[119] = SDLK_PAUSE;
slouken@1895
   270
    keymap[SCANCODE_RIGHTWIN] = SDLK_RSUPER;
slouken@1895
   271
    keymap[SCANCODE_LEFTWIN] = SDLK_LSUPER;
slouken@1895
   272
    keymap[127] = SDLK_MENU;
slouken@75
   273
}
slouken@75
   274
slouken@1895
   275
static SDL_keysym *
slouken@1895
   276
TranslateKey(int scancode, SDL_keysym * keysym)
slouken@75
   277
{
slouken@1895
   278
    /* Set the keysym information */
slouken@1895
   279
    keysym->scancode = scancode;
slouken@1895
   280
    keysym->sym = keymap[scancode];
slouken@1895
   281
    keysym->mod = KMOD_NONE;
slouken@75
   282
slouken@1895
   283
    /* If UNICODE is on, get the UNICODE value for the key */
slouken@1895
   284
    keysym->unicode = 0;
slouken@1895
   285
    if (SDL_TranslateUNICODE && vga_keymap) {
slouken@1895
   286
        int map;
slouken@1895
   287
        SDLMod modstate;
slouken@75
   288
slouken@1895
   289
        modstate = SDL_GetModState();
slouken@1895
   290
        map = 0;
slouken@1895
   291
        if (modstate & KMOD_SHIFT) {
slouken@1895
   292
            map += 1;
slouken@1895
   293
        }
slouken@1895
   294
        if (modstate & KMOD_CTRL) {
slouken@1895
   295
            map += 2;
slouken@1895
   296
        }
slouken@1895
   297
        if (modstate & KMOD_ALT) {
slouken@1895
   298
            map += 4;
slouken@1895
   299
        }
slouken@1895
   300
        if (!(vga_keymap->key[scancode].spcl & (0x80 >> map))) {
slouken@1895
   301
            keysym->unicode = vga_keymap->key[scancode].map[map];
slouken@1895
   302
        }
slouken@75
   303
slouken@1895
   304
    }
slouken@1895
   305
    return (keysym);
slouken@75
   306
}
slouken@75
   307
slouken@1895
   308
/* vi: set ts=4 sw=4 expandtab: */