src/video/ps2gs/SDL_gsevents.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Dec 2009 08:11:06 +0000
changeset 3565 f43c8f688f77
parent 2859 99210400e8b9
permissions -rw-r--r--
Fixed bug #906

Added better error reporting for OpenGL context creation failing.
slouken@70
     1
/*
slouken@1312
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@70
     4
slouken@1312
     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@1312
     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@70
     9
slouken@1312
    10
    This library is distributed in the hope that it will be useful,
slouken@1312
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1312
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@70
    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@70
    18
slouken@1312
    19
    Sam Lantinga
slouken@1312
    20
    slouken@libsdl.org
slouken@70
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@70
    23
slouken@70
    24
/* Handle the event stream, converting console events into SDL events */
slouken@70
    25
slouken@70
    26
#include <sys/types.h>
slouken@70
    27
#include <sys/time.h>
slouken@70
    28
#include <sys/ioctl.h>
slouken@70
    29
#include <unistd.h>
slouken@70
    30
#include <fcntl.h>
slouken@70
    31
#include <errno.h>
slouken@70
    32
#include <limits.h>
slouken@70
    33
slouken@70
    34
/* For parsing /proc */
slouken@70
    35
#include <dirent.h>
slouken@70
    36
#include <ctype.h>
slouken@70
    37
slouken@70
    38
#include <linux/vt.h>
slouken@70
    39
#include <linux/kd.h>
slouken@70
    40
#include <linux/keyboard.h>
slouken@70
    41
slouken@70
    42
#include "SDL_mutex.h"
slouken@1361
    43
#include "../SDL_sysvideo.h"
slouken@1361
    44
#include "../../events/SDL_sysevents.h"
slouken@1361
    45
#include "../../events/SDL_events_c.h"
slouken@70
    46
#include "SDL_gsvideo.h"
slouken@70
    47
#include "SDL_gsevents_c.h"
slouken@70
    48
#include "SDL_gskeys.h"
slouken@70
    49
slouken@70
    50
#ifndef GPM_NODE_FIFO
slouken@70
    51
#define GPM_NODE_FIFO	"/dev/gpmdata"
slouken@70
    52
#endif
slouken@70
    53
slouken@70
    54
/* The translation tables from a console scancode to a SDL keysym */
slouken@70
    55
#define NUM_VGAKEYMAPS	(1<<KG_CAPSSHIFT)
slouken@70
    56
static Uint16 vga_keymap[NUM_VGAKEYMAPS][NR_KEYS];
slouken@70
    57
static SDLKey keymap[128];
slouken@70
    58
static Uint16 keymap_temp[128]; /* only used at startup */
slouken@1895
    59
static SDL_keysym *TranslateKey(int scancode, SDL_keysym * keysym);
slouken@70
    60
slouken@70
    61
/* Ugh, we have to duplicate the kernel's keysym mapping code...
slouken@70
    62
   Oh, it's not so bad. :-)
slouken@70
    63
slouken@70
    64
   FIXME: Add keyboard LED handling code
slouken@70
    65
 */
slouken@1895
    66
static void
slouken@1895
    67
GS_vgainitkeymaps(int fd)
slouken@70
    68
{
slouken@1895
    69
    struct kbentry entry;
slouken@1895
    70
    int map, i;
slouken@70
    71
slouken@1895
    72
    /* Don't do anything if we are passed a closed keyboard */
slouken@1895
    73
    if (fd < 0) {
slouken@1895
    74
        return;
slouken@1895
    75
    }
slouken@70
    76
slouken@1895
    77
    /* Load all the keysym mappings */
slouken@1895
    78
    for (map = 0; map < NUM_VGAKEYMAPS; ++map) {
slouken@1895
    79
        SDL_memset(vga_keymap[map], 0, NR_KEYS * sizeof(Uint16));
slouken@1895
    80
        for (i = 0; i < NR_KEYS; ++i) {
slouken@1895
    81
            entry.kb_table = map;
slouken@1895
    82
            entry.kb_index = i;
slouken@1895
    83
            if (ioctl(fd, KDGKBENT, &entry) == 0) {
slouken@1895
    84
                /* fill keytemp. This replaces SDL_fbkeys.h */
slouken@1895
    85
                if ((map == 0) && (i < 128)) {
slouken@1895
    86
                    keymap_temp[i] = entry.kb_value;
slouken@1895
    87
                }
slouken@1895
    88
                /* The "Enter" key is a special case */
slouken@1895
    89
                if (entry.kb_value == K_ENTER) {
slouken@1895
    90
                    entry.kb_value = K(KT_ASCII, 13);
slouken@1895
    91
                }
slouken@1895
    92
                /* Handle numpad specially as well */
slouken@1895
    93
                if (KTYP(entry.kb_value) == KT_PAD) {
slouken@1895
    94
                    switch (entry.kb_value) {
slouken@1895
    95
                    case K_P0:
slouken@1895
    96
                    case K_P1:
slouken@1895
    97
                    case K_P2:
slouken@1895
    98
                    case K_P3:
slouken@1895
    99
                    case K_P4:
slouken@1895
   100
                    case K_P5:
slouken@1895
   101
                    case K_P6:
slouken@1895
   102
                    case K_P7:
slouken@1895
   103
                    case K_P8:
slouken@1895
   104
                    case K_P9:
slouken@1895
   105
                        vga_keymap[map][i] = entry.kb_value;
slouken@1895
   106
                        vga_keymap[map][i] += '0';
slouken@1895
   107
                        break;
slouken@1895
   108
                    case K_PPLUS:
slouken@1895
   109
                        vga_keymap[map][i] = K(KT_ASCII, '+');
slouken@1895
   110
                        break;
slouken@1895
   111
                    case K_PMINUS:
slouken@1895
   112
                        vga_keymap[map][i] = K(KT_ASCII, '-');
slouken@1895
   113
                        break;
slouken@1895
   114
                    case K_PSTAR:
slouken@1895
   115
                        vga_keymap[map][i] = K(KT_ASCII, '*');
slouken@1895
   116
                        break;
slouken@1895
   117
                    case K_PSLASH:
slouken@1895
   118
                        vga_keymap[map][i] = K(KT_ASCII, '/');
slouken@1895
   119
                        break;
slouken@1895
   120
                    case K_PENTER:
slouken@1895
   121
                        vga_keymap[map][i] = K(KT_ASCII, '\r');
slouken@1895
   122
                        break;
slouken@1895
   123
                    case K_PCOMMA:
slouken@1895
   124
                        vga_keymap[map][i] = K(KT_ASCII, ',');
slouken@1895
   125
                        break;
slouken@1895
   126
                    case K_PDOT:
slouken@1895
   127
                        vga_keymap[map][i] = K(KT_ASCII, '.');
slouken@1895
   128
                        break;
slouken@1895
   129
                    default:
slouken@1895
   130
                        break;
slouken@1895
   131
                    }
slouken@1895
   132
                }
slouken@1895
   133
                /* Do the normal key translation */
slouken@1895
   134
                if ((KTYP(entry.kb_value) == KT_LATIN) ||
slouken@1895
   135
                    (KTYP(entry.kb_value) == KT_ASCII) ||
slouken@1895
   136
                    (KTYP(entry.kb_value) == KT_LETTER)) {
slouken@1895
   137
                    vga_keymap[map][i] = entry.kb_value;
slouken@1895
   138
                }
slouken@1895
   139
            }
slouken@1895
   140
        }
slouken@1895
   141
    }
slouken@70
   142
}
slouken@70
   143
slouken@1895
   144
int
slouken@1895
   145
GS_InGraphicsMode(_THIS)
slouken@70
   146
{
slouken@1895
   147
    return ((keyboard_fd >= 0) && (saved_kbd_mode >= 0));
slouken@70
   148
}
slouken@70
   149
slouken@1895
   150
int
slouken@1895
   151
GS_EnterGraphicsMode(_THIS)
slouken@70
   152
{
slouken@1895
   153
    struct termios keyboard_termios;
slouken@70
   154
slouken@1895
   155
    /* Set medium-raw keyboard mode */
slouken@1895
   156
    if ((keyboard_fd >= 0) && !GS_InGraphicsMode(this)) {
slouken@70
   157
slouken@1895
   158
        /* Switch to the correct virtual terminal */
slouken@1895
   159
        if (current_vt > 0) {
slouken@1895
   160
            struct vt_stat vtstate;
slouken@70
   161
slouken@1895
   162
            if (ioctl(keyboard_fd, VT_GETSTATE, &vtstate) == 0) {
slouken@1895
   163
                saved_vt = vtstate.v_active;
slouken@1895
   164
            }
slouken@1895
   165
            if (ioctl(keyboard_fd, VT_ACTIVATE, current_vt) == 0) {
slouken@1895
   166
                ioctl(keyboard_fd, VT_WAITACTIVE, current_vt);
slouken@1895
   167
            }
slouken@1895
   168
        }
slouken@70
   169
slouken@1895
   170
        /* Set the terminal input mode */
slouken@1895
   171
        if (tcgetattr(keyboard_fd, &saved_kbd_termios) < 0) {
slouken@1895
   172
            SDL_SetError("Unable to get terminal attributes");
slouken@1895
   173
            if (keyboard_fd > 0) {
slouken@1895
   174
                close(keyboard_fd);
slouken@1895
   175
            }
slouken@1895
   176
            keyboard_fd = -1;
slouken@1895
   177
            return (-1);
slouken@1895
   178
        }
slouken@1895
   179
        if (ioctl(keyboard_fd, KDGKBMODE, &saved_kbd_mode) < 0) {
slouken@1895
   180
            SDL_SetError("Unable to get current keyboard mode");
slouken@1895
   181
            if (keyboard_fd > 0) {
slouken@1895
   182
                close(keyboard_fd);
slouken@1895
   183
            }
slouken@1895
   184
            keyboard_fd = -1;
slouken@1895
   185
            return (-1);
slouken@1895
   186
        }
slouken@1895
   187
        keyboard_termios = saved_kbd_termios;
slouken@1895
   188
        keyboard_termios.c_lflag &= ~(ICANON | ECHO | ISIG);
slouken@1895
   189
        keyboard_termios.c_iflag &=
slouken@1895
   190
            ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
slouken@1895
   191
        keyboard_termios.c_cc[VMIN] = 0;
slouken@1895
   192
        keyboard_termios.c_cc[VTIME] = 0;
slouken@1895
   193
        if (tcsetattr(keyboard_fd, TCSAFLUSH, &keyboard_termios) < 0) {
slouken@1895
   194
            GS_CloseKeyboard(this);
slouken@1895
   195
            SDL_SetError("Unable to set terminal attributes");
slouken@1895
   196
            return (-1);
slouken@1895
   197
        }
slouken@1895
   198
        /* This will fail if we aren't root or this isn't our tty */
slouken@1895
   199
        if (ioctl(keyboard_fd, KDSKBMODE, K_MEDIUMRAW) < 0) {
slouken@1895
   200
            GS_CloseKeyboard(this);
slouken@1895
   201
            SDL_SetError("Unable to set keyboard in raw mode");
slouken@1895
   202
            return (-1);
slouken@1895
   203
        }
slouken@1895
   204
        if (ioctl(keyboard_fd, KDSETMODE, KD_GRAPHICS) < 0) {
slouken@1895
   205
            GS_CloseKeyboard(this);
slouken@1895
   206
            SDL_SetError("Unable to set keyboard in graphics mode");
slouken@1895
   207
            return (-1);
slouken@1895
   208
        }
slouken@1895
   209
    }
slouken@1895
   210
    return (keyboard_fd);
slouken@70
   211
}
slouken@70
   212
slouken@1895
   213
void
slouken@1895
   214
GS_LeaveGraphicsMode(_THIS)
slouken@70
   215
{
slouken@1895
   216
    if (GS_InGraphicsMode(this)) {
slouken@1895
   217
        ioctl(keyboard_fd, KDSETMODE, KD_TEXT);
slouken@1895
   218
        ioctl(keyboard_fd, KDSKBMODE, saved_kbd_mode);
slouken@1895
   219
        tcsetattr(keyboard_fd, TCSAFLUSH, &saved_kbd_termios);
slouken@1895
   220
        saved_kbd_mode = -1;
slouken@70
   221
slouken@1895
   222
        /* Head back over to the original virtual terminal */
slouken@1895
   223
        if (saved_vt > 0) {
slouken@1895
   224
            ioctl(keyboard_fd, VT_ACTIVATE, saved_vt);
slouken@1895
   225
        }
slouken@1895
   226
    }
slouken@70
   227
}
slouken@70
   228
slouken@1895
   229
void
slouken@1895
   230
GS_CloseKeyboard(_THIS)
slouken@70
   231
{
slouken@1895
   232
    if (keyboard_fd >= 0) {
slouken@1895
   233
        GS_LeaveGraphicsMode(this);
slouken@1895
   234
        if (keyboard_fd > 0) {
slouken@1895
   235
            close(keyboard_fd);
slouken@1895
   236
        }
slouken@1895
   237
    }
slouken@1895
   238
    keyboard_fd = -1;
slouken@70
   239
}
slouken@70
   240
slouken@1895
   241
int
slouken@1895
   242
GS_OpenKeyboard(_THIS)
slouken@70
   243
{
slouken@1895
   244
    /* Open only if not already opened */
slouken@1895
   245
    if (keyboard_fd < 0) {
slouken@1895
   246
        char *tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
slouken@1895
   247
        char *vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };
slouken@1895
   248
        int i, tty0_fd;
slouken@70
   249
slouken@1895
   250
        /* Try to query for a free virtual terminal */
slouken@1895
   251
        tty0_fd = -1;
slouken@1895
   252
        for (i = 0; tty0[i] && (tty0_fd < 0); ++i) {
slouken@1895
   253
            tty0_fd = open(tty0[i], O_WRONLY, 0);
slouken@1895
   254
        }
slouken@1895
   255
        if (tty0_fd < 0) {
slouken@1895
   256
            tty0_fd = dup(0);   /* Maybe stdin is a VT? */
slouken@1895
   257
        }
slouken@1895
   258
        ioctl(tty0_fd, VT_OPENQRY, &current_vt);
slouken@1895
   259
        close(tty0_fd);
slouken@1895
   260
        if ((geteuid() == 0) && (current_vt > 0)) {
slouken@1895
   261
            for (i = 0; vcs[i] && (keyboard_fd < 0); ++i) {
slouken@1895
   262
                char vtpath[12];
slouken@70
   263
slouken@1895
   264
                SDL_snprintf(vtpath, SDL_arraysize(vtpath), vcs[i],
slouken@1895
   265
                             current_vt);
slouken@1895
   266
                keyboard_fd = open(vtpath, O_RDWR, 0);
slouken@70
   267
#ifdef DEBUG_KEYBOARD
slouken@1895
   268
                fprintf(stderr, "vtpath = %s, fd = %d\n",
slouken@1895
   269
                        vtpath, keyboard_fd);
slouken@70
   270
#endif /* DEBUG_KEYBOARD */
slouken@70
   271
slouken@1895
   272
                /* This needs to be our controlling tty
slouken@1895
   273
                   so that the kernel ioctl() calls work
slouken@1895
   274
                 */
slouken@1895
   275
                if (keyboard_fd >= 0) {
slouken@1895
   276
                    tty0_fd = open("/dev/tty", O_RDWR, 0);
slouken@1895
   277
                    if (tty0_fd >= 0) {
slouken@1895
   278
                        ioctl(tty0_fd, TIOCNOTTY, 0);
slouken@1895
   279
                        close(tty0_fd);
slouken@1895
   280
                    }
slouken@1895
   281
                }
slouken@1895
   282
            }
slouken@1895
   283
        }
slouken@1895
   284
        if (keyboard_fd < 0) {
slouken@1895
   285
            /* Last resort, maybe our tty is a usable VT */
slouken@1895
   286
            current_vt = 0;
slouken@1895
   287
            keyboard_fd = open("/dev/tty", O_RDWR);
slouken@1895
   288
        }
slouken@70
   289
#ifdef DEBUG_KEYBOARD
slouken@1895
   290
        fprintf(stderr, "Current VT: %d\n", current_vt);
slouken@70
   291
#endif
slouken@1895
   292
        saved_kbd_mode = -1;
slouken@70
   293
slouken@1895
   294
        /* Make sure that our input is a console terminal */
slouken@1895
   295
        {
slouken@1895
   296
            int dummy;
slouken@1895
   297
            if (ioctl(keyboard_fd, KDGKBMODE, &dummy) < 0) {
slouken@1895
   298
                close(keyboard_fd);
slouken@1895
   299
                keyboard_fd = -1;
slouken@1895
   300
                SDL_SetError("Unable to open a console terminal");
slouken@1895
   301
            }
slouken@1895
   302
        }
slouken@70
   303
slouken@1895
   304
        /* Set up keymap */
slouken@1895
   305
        GS_vgainitkeymaps(keyboard_fd);
slouken@1895
   306
    }
slouken@1895
   307
    return (keyboard_fd);
slouken@70
   308
}
slouken@70
   309
slouken@1895
   310
static enum
slouken@1895
   311
{
slouken@1895
   312
    MOUSE_NONE = -1,
slouken@1895
   313
    MOUSE_GPM,                  /* Note: GPM uses the MSC protocol */
slouken@1895
   314
    MOUSE_PS2,
slouken@1895
   315
    MOUSE_IMPS2,
slouken@1895
   316
    MOUSE_MS,
slouken@1895
   317
    MOUSE_BM,
slouken@1895
   318
    NUM_MOUSE_DRVS
slouken@70
   319
} mouse_drv = MOUSE_NONE;
slouken@70
   320
slouken@1895
   321
void
slouken@1895
   322
GS_CloseMouse(_THIS)
slouken@70
   323
{
slouken@1895
   324
    if (mouse_fd > 0) {
slouken@1895
   325
        close(mouse_fd);
slouken@1895
   326
    }
slouken@1895
   327
    mouse_fd = -1;
slouken@70
   328
}
slouken@70
   329
slouken@70
   330
/* Returns processes listed in /proc with the desired name */
slouken@1895
   331
static int
slouken@1895
   332
find_pid(DIR * proc, const char *wanted_name)
slouken@70
   333
{
slouken@1895
   334
    struct dirent *entry;
slouken@1895
   335
    int pid;
slouken@70
   336
slouken@1895
   337
    /* First scan proc for the gpm process */
slouken@1895
   338
    pid = 0;
slouken@1895
   339
    while ((pid == 0) && ((entry = readdir(proc)) != NULL)) {
slouken@1895
   340
        if (isdigit(entry->d_name[0])) {
slouken@1895
   341
            FILE *status;
slouken@1895
   342
            char path[PATH_MAX];
slouken@1895
   343
            char name[PATH_MAX];
slouken@70
   344
slouken@1895
   345
            SDL_snprintf(path, SDL_arraysize(path), "/proc/%s/status",
slouken@1895
   346
                         entry->d_name);
slouken@1895
   347
            status = fopen(path, "r");
slouken@1895
   348
            if (status) {
slouken@1895
   349
                name[0] = '\0';
slouken@1895
   350
                fscanf(status, "Name: %s", name);
slouken@1895
   351
                if (SDL_strcmp(name, wanted_name) == 0) {
slouken@1895
   352
                    pid = atoi(entry->d_name);
slouken@1895
   353
                }
slouken@1895
   354
                fclose(status);
slouken@1895
   355
            }
slouken@1895
   356
        }
slouken@1895
   357
    }
slouken@1895
   358
    return pid;
slouken@70
   359
}
slouken@70
   360
slouken@70
   361
/* Returns true if /dev/gpmdata is being written to by gpm */
slouken@1895
   362
static int
slouken@1895
   363
gpm_available(void)
slouken@70
   364
{
slouken@1895
   365
    int available;
slouken@1895
   366
    DIR *proc;
slouken@1895
   367
    int pid;
slouken@1895
   368
    int cmdline, len, arglen;
slouken@1895
   369
    char path[PATH_MAX];
slouken@1895
   370
    char args[PATH_MAX], *arg;
slouken@70
   371
slouken@1895
   372
    /* Don't bother looking if the fifo isn't there */
slouken@1895
   373
    if (access(GPM_NODE_FIFO, F_OK) < 0) {
slouken@1895
   374
        return (0);
slouken@1895
   375
    }
slouken@70
   376
slouken@1895
   377
    available = 0;
slouken@1895
   378
    proc = opendir("/proc");
slouken@1895
   379
    if (proc) {
slouken@1895
   380
        while ((pid = find_pid(proc, "gpm")) > 0) {
slouken@1895
   381
            SDL_snprintf(path, SDL_arraysize(path), "/proc/%d/cmdline", pid);
slouken@1895
   382
            cmdline = open(path, O_RDONLY, 0);
slouken@1895
   383
            if (cmdline >= 0) {
slouken@1895
   384
                len = read(cmdline, args, sizeof(args));
slouken@1895
   385
                arg = args;
slouken@1895
   386
                while (len > 0) {
slouken@1895
   387
                    if (SDL_strcmp(arg, "-R") == 0) {
slouken@1895
   388
                        available = 1;
slouken@1895
   389
                    }
slouken@1895
   390
                    arglen = SDL_strlen(arg) + 1;
slouken@1895
   391
                    len -= arglen;
slouken@1895
   392
                    arg += arglen;
slouken@1895
   393
                }
slouken@1895
   394
                close(cmdline);
slouken@1895
   395
            }
slouken@1895
   396
        }
slouken@1895
   397
        closedir(proc);
slouken@1895
   398
    }
slouken@1895
   399
    return available;
slouken@70
   400
}
slouken@70
   401
slouken@70
   402
slouken@70
   403
/* rcg06112001 Set up IMPS/2 mode, if possible. This gives
slouken@70
   404
 *  us access to the mousewheel, etc. Returns zero if
slouken@70
   405
 *  writes to device failed, but you still need to query the
slouken@70
   406
 *  device to see which mode it's actually in.
slouken@70
   407
 */
slouken@1895
   408
static int
slouken@1895
   409
set_imps2_mode(int fd)
slouken@70
   410
{
slouken@1895
   411
    /* If you wanted to control the mouse mode (and we do :)  ) ...
slouken@1895
   412
       Set IMPS/2 protocol:
slouken@1895
   413
       {0xf3,200,0xf3,100,0xf3,80}
slouken@1895
   414
       Reset mouse device:
slouken@1895
   415
       {0xFF}
slouken@1895
   416
     */
slouken@1895
   417
    Uint8 set_imps2[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
slouken@1895
   418
    Uint8 reset = 0xff;
slouken@1895
   419
    fd_set fdset;
slouken@1895
   420
    struct timeval tv;
slouken@1895
   421
    int retval = 0;
slouken@70
   422
slouken@1895
   423
    if (write(fd, &set_imps2, sizeof(set_imps2)) == sizeof(set_imps2)) {
slouken@1895
   424
        if (write(fd, &reset, sizeof(reset)) == sizeof(reset)) {
slouken@1895
   425
            retval = 1;
slouken@1895
   426
        }
slouken@1895
   427
    }
slouken@70
   428
slouken@1895
   429
    /* Get rid of any chatter from the above */
slouken@1895
   430
    FD_ZERO(&fdset);
slouken@1895
   431
    FD_SET(fd, &fdset);
slouken@1895
   432
    tv.tv_sec = 0;
slouken@1895
   433
    tv.tv_usec = 0;
slouken@1895
   434
    while (select(fd + 1, &fdset, 0, 0, &tv) > 0) {
slouken@1895
   435
        char temp[32];
slouken@1895
   436
        read(fd, temp, sizeof(temp));
slouken@1895
   437
    }
slouken@70
   438
slouken@1895
   439
    return retval;
slouken@70
   440
}
slouken@70
   441
slouken@70
   442
slouken@70
   443
/* Returns true if the mouse uses the IMPS/2 protocol */
slouken@1895
   444
static int
slouken@1895
   445
detect_imps2(int fd)
slouken@70
   446
{
slouken@1895
   447
    int imps2;
slouken@70
   448
slouken@1895
   449
    imps2 = 0;
slouken@70
   450
slouken@1895
   451
    if (SDL_getenv("SDL_MOUSEDEV_IMPS2")) {
slouken@1895
   452
        imps2 = 1;
slouken@1895
   453
    }
slouken@1895
   454
    if (!imps2) {
slouken@1895
   455
        Uint8 query_ps2 = 0xF2;
slouken@1895
   456
        fd_set fdset;
slouken@1895
   457
        struct timeval tv;
slouken@70
   458
slouken@1895
   459
        /* Get rid of any mouse motion noise */
slouken@1895
   460
        FD_ZERO(&fdset);
slouken@1895
   461
        FD_SET(fd, &fdset);
slouken@1895
   462
        tv.tv_sec = 0;
slouken@1895
   463
        tv.tv_usec = 0;
slouken@1895
   464
        while (select(fd + 1, &fdset, 0, 0, &tv) > 0) {
slouken@1895
   465
            char temp[32];
slouken@1895
   466
            read(fd, temp, sizeof(temp));
slouken@1895
   467
        }
slouken@70
   468
slouken@1895
   469
        /* Query for the type of mouse protocol */
slouken@1895
   470
        if (write(fd, &query_ps2, sizeof(query_ps2)) == sizeof(query_ps2)) {
slouken@1895
   471
            Uint8 ch = 0;
slouken@70
   472
slouken@1895
   473
            /* Get the mouse protocol response */
slouken@1895
   474
            do {
slouken@1895
   475
                FD_ZERO(&fdset);
slouken@1895
   476
                FD_SET(fd, &fdset);
slouken@1895
   477
                tv.tv_sec = 1;
slouken@1895
   478
                tv.tv_usec = 0;
slouken@1895
   479
                if (select(fd + 1, &fdset, 0, 0, &tv) < 1) {
slouken@1895
   480
                    break;
slouken@1895
   481
                }
slouken@2735
   482
            } while ((read(fd, &ch, sizeof(ch)) == sizeof(ch)) &&
slouken@2735
   483
                     ((ch == 0xFA) || (ch == 0xAA)));
slouken@70
   484
slouken@1895
   485
            /* Experimental values (Logitech wheelmouse) */
slouken@70
   486
#ifdef DEBUG_MOUSE
slouken@1895
   487
            fprintf(stderr, "Last mouse mode: 0x%x\n", ch);
slouken@70
   488
#endif
slouken@1895
   489
            if (ch == 3) {
slouken@1895
   490
                imps2 = 1;
slouken@1895
   491
            }
slouken@1895
   492
        }
slouken@1895
   493
    }
slouken@1895
   494
    return imps2;
slouken@70
   495
}
slouken@70
   496
slouken@1895
   497
int
slouken@1895
   498
GS_OpenMouse(_THIS)
slouken@70
   499
{
slouken@1895
   500
    int i;
slouken@1895
   501
    const char *mousedev;
slouken@1895
   502
    const char *mousedrv;
slouken@70
   503
slouken@1895
   504
    mousedrv = SDL_getenv("SDL_MOUSEDRV");
slouken@1895
   505
    mousedev = SDL_getenv("SDL_MOUSEDEV");
slouken@1895
   506
    mouse_fd = -1;
slouken@70
   507
slouken@1895
   508
    /* STD MICE */
slouken@70
   509
slouken@1895
   510
    if (mousedev == NULL) {
slouken@1895
   511
        /* FIXME someday... allow multiple mice in this driver */
slouken@1895
   512
        char *ps2mice[] = {
slouken@1895
   513
            "/dev/input/mice", "/dev/usbmouse", "/dev/psaux", NULL
slouken@1895
   514
        };
slouken@1895
   515
        /* First try to use GPM in repeater mode */
slouken@1895
   516
        if (mouse_fd < 0) {
slouken@1895
   517
            if (gpm_available()) {
slouken@1895
   518
                mouse_fd = open(GPM_NODE_FIFO, O_RDONLY, 0);
slouken@1895
   519
                if (mouse_fd >= 0) {
slouken@70
   520
#ifdef DEBUG_MOUSE
slouken@1895
   521
                    fprintf(stderr, "Using GPM mouse\n");
slouken@70
   522
#endif
slouken@1895
   523
                    mouse_drv = MOUSE_GPM;
slouken@1895
   524
                }
slouken@1895
   525
            }
slouken@1895
   526
        }
slouken@1895
   527
        /* Now try to use a modern PS/2 mouse */
slouken@1895
   528
        for (i = 0; (mouse_fd < 0) && ps2mice[i]; ++i) {
slouken@1895
   529
            mouse_fd = open(ps2mice[i], O_RDWR, 0);
slouken@1895
   530
            if (mouse_fd < 0) {
slouken@1895
   531
                mouse_fd = open(ps2mice[i], O_RDONLY, 0);
slouken@1895
   532
            }
slouken@1895
   533
            if (mouse_fd >= 0) {
slouken@1895
   534
                /* rcg06112001 Attempt to set IMPS/2 mode */
slouken@1895
   535
                if (i == 0) {
slouken@1895
   536
                    set_imps2_mode(mouse_fd);
slouken@1895
   537
                }
slouken@1895
   538
                if (detect_imps2(mouse_fd)) {
slouken@70
   539
#ifdef DEBUG_MOUSE
slouken@1895
   540
                    fprintf(stderr, "Using IMPS2 mouse\n");
slouken@70
   541
#endif
slouken@1895
   542
                    mouse_drv = MOUSE_IMPS2;
slouken@1895
   543
                } else {
slouken@1895
   544
                    mouse_drv = MOUSE_PS2;
slouken@70
   545
#ifdef DEBUG_MOUSE
slouken@1895
   546
                    fprintf(stderr, "Using PS2 mouse\n");
slouken@70
   547
#endif
slouken@1895
   548
                }
slouken@1895
   549
            }
slouken@1895
   550
        }
slouken@1895
   551
        /* Next try to use a PPC ADB port mouse */
slouken@1895
   552
        if (mouse_fd < 0) {
slouken@1895
   553
            mouse_fd = open("/dev/adbmouse", O_RDONLY, 0);
slouken@1895
   554
            if (mouse_fd >= 0) {
slouken@70
   555
#ifdef DEBUG_MOUSE
slouken@1895
   556
                fprintf(stderr, "Using ADB mouse\n");
slouken@70
   557
#endif
slouken@1895
   558
                mouse_drv = MOUSE_BM;
slouken@1895
   559
            }
slouken@1895
   560
        }
slouken@1895
   561
    }
slouken@1895
   562
    /* Default to a serial Microsoft mouse */
slouken@1895
   563
    if (mouse_fd < 0) {
slouken@1895
   564
        if (mousedev == NULL) {
slouken@1895
   565
            mousedev = "/dev/mouse";
slouken@1895
   566
        }
slouken@1895
   567
        mouse_fd = open(mousedev, O_RDONLY, 0);
slouken@1895
   568
        if (mouse_fd >= 0) {
slouken@1895
   569
            struct termios mouse_termios;
slouken@70
   570
slouken@1895
   571
            /* Set the sampling speed to 1200 baud */
slouken@1895
   572
            tcgetattr(mouse_fd, &mouse_termios);
slouken@1895
   573
            mouse_termios.c_iflag = IGNBRK | IGNPAR;
slouken@1895
   574
            mouse_termios.c_oflag = 0;
slouken@1895
   575
            mouse_termios.c_lflag = 0;
slouken@1895
   576
            mouse_termios.c_line = 0;
slouken@1895
   577
            mouse_termios.c_cc[VTIME] = 0;
slouken@1895
   578
            mouse_termios.c_cc[VMIN] = 1;
slouken@1895
   579
            mouse_termios.c_cflag = CREAD | CLOCAL | HUPCL;
slouken@1895
   580
            mouse_termios.c_cflag |= CS8;
slouken@1895
   581
            mouse_termios.c_cflag |= B1200;
slouken@1895
   582
            tcsetattr(mouse_fd, TCSAFLUSH, &mouse_termios);
slouken@70
   583
#ifdef DEBUG_MOUSE
slouken@1895
   584
            fprintf(stderr, "Using Microsoft mouse on %s\n", mousedev);
slouken@70
   585
#endif
slouken@1895
   586
            mouse_drv = MOUSE_MS;
slouken@1895
   587
        }
slouken@1895
   588
    }
slouken@1895
   589
    if (mouse_fd < 0) {
slouken@1895
   590
        mouse_drv = MOUSE_NONE;
slouken@1895
   591
    }
slouken@1895
   592
    return (mouse_fd);
slouken@70
   593
}
slouken@70
   594
slouken@70
   595
static int posted = 0;
slouken@70
   596
slouken@1895
   597
void
slouken@1895
   598
GS_vgamousecallback(int button, int dx, int dy)
slouken@70
   599
{
slouken@1895
   600
    int button_1, button_3;
slouken@1895
   601
    int button_state;
slouken@1895
   602
    int state_changed;
slouken@1895
   603
    int i;
slouken@1895
   604
    Uint8 state;
slouken@70
   605
slouken@1895
   606
    if (dx || dy) {
slouken@1895
   607
        posted += SDL_PrivateMouseMotion(0, 1, dx, dy);
slouken@1895
   608
    }
slouken@70
   609
slouken@1895
   610
    /* Swap button 1 and 3 */
slouken@1895
   611
    button_1 = (button & 0x04) >> 2;
slouken@1895
   612
    button_3 = (button & 0x01) << 2;
slouken@1895
   613
    button &= ~0x05;
slouken@1895
   614
    button |= (button_1 | button_3);
slouken@70
   615
slouken@1895
   616
    /* See what changed */
slouken@1895
   617
    button_state = SDL_GetMouseState(NULL, NULL);
slouken@1895
   618
    state_changed = button_state ^ button;
slouken@1895
   619
    for (i = 0; i < 8; ++i) {
slouken@1895
   620
        if (state_changed & (1 << i)) {
slouken@1895
   621
            if (button & (1 << i)) {
slouken@1895
   622
                state = SDL_PRESSED;
slouken@1895
   623
            } else {
slouken@1895
   624
                state = SDL_RELEASED;
slouken@1895
   625
            }
slouken@1895
   626
            posted += SDL_PrivateMouseButton(state, i + 1, 0, 0);
slouken@1895
   627
        }
slouken@1895
   628
    }
slouken@70
   629
}
slouken@70
   630
slouken@70
   631
/* For now, use GPM, PS/2, and MS protocols
slouken@70
   632
   Driver adapted from the SVGAlib mouse driver code (taken from gpm, etc.)
slouken@70
   633
 */
slouken@1895
   634
static void
slouken@1895
   635
handle_mouse(_THIS)
slouken@70
   636
{
slouken@1895
   637
    static int start = 0;
slouken@1895
   638
    static unsigned char mousebuf[BUFSIZ];
slouken@1895
   639
    int i, nread;
slouken@1895
   640
    int button = 0;
slouken@1895
   641
    int dx = 0, dy = 0;
slouken@1895
   642
    int packetsize = 0;
slouken@70
   643
slouken@1895
   644
    /* Figure out the mouse packet size */
slouken@1895
   645
    switch (mouse_drv) {
slouken@1895
   646
    case MOUSE_NONE:
slouken@1895
   647
        /* Ack! */
slouken@1895
   648
        read(mouse_fd, mousebuf, BUFSIZ);
slouken@1895
   649
        return;
slouken@1895
   650
    case MOUSE_GPM:
slouken@1895
   651
        packetsize = 5;
slouken@1895
   652
        break;
slouken@1895
   653
    case MOUSE_IMPS2:
slouken@1895
   654
        packetsize = 4;
slouken@1895
   655
        break;
slouken@1895
   656
    case MOUSE_PS2:
slouken@1895
   657
    case MOUSE_MS:
slouken@1895
   658
    case MOUSE_BM:
slouken@1895
   659
        packetsize = 3;
slouken@1895
   660
        break;
slouken@1895
   661
    case NUM_MOUSE_DRVS:
slouken@1895
   662
        /* Uh oh.. */
slouken@1895
   663
        packetsize = 0;
slouken@1895
   664
        break;
slouken@1895
   665
    }
slouken@70
   666
slouken@1895
   667
    /* Read as many packets as possible */
slouken@1895
   668
    nread = read(mouse_fd, &mousebuf[start], BUFSIZ - start);
slouken@1895
   669
    if (nread < 0) {
slouken@1895
   670
        return;
slouken@1895
   671
    }
slouken@1895
   672
    nread += start;
slouken@70
   673
#ifdef DEBUG_MOUSE
slouken@1895
   674
    fprintf(stderr, "Read %d bytes from mouse, start = %d\n", nread, start);
slouken@70
   675
#endif
slouken@1895
   676
    for (i = 0; i < (nread - (packetsize - 1)); i += packetsize) {
slouken@1895
   677
        switch (mouse_drv) {
slouken@1895
   678
        case MOUSE_NONE:
slouken@1895
   679
            break;
slouken@1895
   680
        case MOUSE_GPM:
slouken@1895
   681
            /* GPM protocol has 0x80 in high byte */
slouken@1895
   682
            if ((mousebuf[i] & 0xF8) != 0x80) {
slouken@1895
   683
                /* Go to next byte */
slouken@1895
   684
                i -= (packetsize - 1);
slouken@1895
   685
                continue;
slouken@1895
   686
            }
slouken@1895
   687
            /* Get current mouse state */
slouken@1895
   688
            button = (~mousebuf[i]) & 0x07;
slouken@1895
   689
            dx = (signed char) (mousebuf[i + 1]) +
slouken@1895
   690
                (signed char) (mousebuf[i + 3]);
slouken@1895
   691
            dy = -((signed char) (mousebuf[i + 2]) +
slouken@1895
   692
                   (signed char) (mousebuf[i + 4]));
slouken@1895
   693
            break;
slouken@1895
   694
        case MOUSE_PS2:
slouken@1895
   695
            /* PS/2 protocol has nothing in high byte */
slouken@1895
   696
            if ((mousebuf[i] & 0xC0) != 0) {
slouken@1895
   697
                /* Go to next byte */
slouken@1895
   698
                i -= (packetsize - 1);
slouken@1895
   699
                continue;
slouken@1895
   700
            }
slouken@1895
   701
            /* Get current mouse state */
slouken@1895
   702
            button = (mousebuf[i] & 0x04) >> 1 |        /*Middle */
slouken@1895
   703
                (mousebuf[i] & 0x02) >> 1 |     /*Right */
slouken@1895
   704
                (mousebuf[i] & 0x01) << 2;      /*Left */
slouken@1895
   705
            dx = (mousebuf[i] & 0x10) ?
slouken@1895
   706
                mousebuf[i + 1] - 256 : mousebuf[i + 1];
slouken@1895
   707
            dy = (mousebuf[i] & 0x20) ?
slouken@1895
   708
                -(mousebuf[i + 2] - 256) : -mousebuf[i + 2];
slouken@1895
   709
            break;
slouken@1895
   710
        case MOUSE_IMPS2:
slouken@1895
   711
            /* Get current mouse state */
slouken@1895
   712
            button = (mousebuf[i] & 0x04) >> 1 |        /*Middle */
slouken@1895
   713
                (mousebuf[i] & 0x02) >> 1 |     /*Right */
slouken@1895
   714
                (mousebuf[i] & 0x01) << 2 |     /*Left */
slouken@1895
   715
                (mousebuf[i] & 0x40) >> 3 |     /* 4 */
slouken@1895
   716
                (mousebuf[i] & 0x80) >> 3;      /* 5 */
slouken@1895
   717
            dx = (mousebuf[i] & 0x10) ?
slouken@1895
   718
                mousebuf[i + 1] - 256 : mousebuf[i + 1];
slouken@1895
   719
            dy = (mousebuf[i] & 0x20) ?
slouken@1895
   720
                -(mousebuf[i + 2] - 256) : -mousebuf[i + 2];
slouken@1895
   721
            switch (mousebuf[i + 3] & 0x0F) {
slouken@1895
   722
            case 0x0E:         /* DX = +1 */
slouken@1895
   723
            case 0x02:         /* DX = -1 */
slouken@1895
   724
                break;
slouken@1895
   725
            case 0x0F:         /* DY = +1 (map button 4) */
slouken@1895
   726
                FB_vgamousecallback(button | (1 << 3), 1, 0, 0);
slouken@1895
   727
                break;
slouken@1895
   728
            case 0x01:         /* DY = -1 (map button 5) */
slouken@1895
   729
                FB_vgamousecallback(button | (1 << 4), 1, 0, 0);
slouken@1895
   730
                break;
slouken@1895
   731
            }
slouken@1895
   732
            break;
slouken@1895
   733
        case MOUSE_MS:
slouken@1895
   734
            /* Microsoft protocol has 0x40 in high byte */
slouken@1895
   735
            if ((mousebuf[i] & 0x40) != 0x40) {
slouken@1895
   736
                /* Go to next byte */
slouken@1895
   737
                i -= (packetsize - 1);
slouken@1895
   738
                continue;
slouken@1895
   739
            }
slouken@1895
   740
            /* Get current mouse state */
slouken@1895
   741
            button = ((mousebuf[i] & 0x20) >> 3) |
slouken@1895
   742
                ((mousebuf[i] & 0x10) >> 4);
slouken@1895
   743
            dx = (signed char) (((mousebuf[i] & 0x03) << 6) |
slouken@1895
   744
                                (mousebuf[i + 1] & 0x3F));
slouken@1895
   745
            dy = (signed char) (((mousebuf[i] & 0x0C) << 4) |
slouken@1895
   746
                                (mousebuf[i + 2] & 0x3F));
slouken@1895
   747
            break;
slouken@1895
   748
        case MOUSE_BM:
slouken@1895
   749
            /* BusMouse protocol has 0xF8 in high byte */
slouken@1895
   750
            if ((mousebuf[i] & 0xF8) != 0x80) {
slouken@1895
   751
                /* Go to next byte */
slouken@1895
   752
                i -= (packetsize - 1);
slouken@1895
   753
                continue;
slouken@1895
   754
            }
slouken@1895
   755
            /* Get current mouse state */
slouken@1895
   756
            button = (~mousebuf[i]) & 0x07;
slouken@1895
   757
            dx = (signed char) mousebuf[i + 1];
slouken@1895
   758
            dy = -(signed char) mousebuf[i + 2];
slouken@1895
   759
            break;
slouken@1895
   760
        case NUM_MOUSE_DRVS:
slouken@1895
   761
            /* Uh oh.. */
slouken@1895
   762
            dx = 0;
slouken@1895
   763
            dy = 0;
slouken@1895
   764
            break;
slouken@1895
   765
        }
slouken@1895
   766
        GS_vgamousecallback(button, dx, dy);
slouken@1895
   767
    }
slouken@1895
   768
    if (i < nread) {
slouken@1895
   769
        SDL_memcpy(mousebuf, &mousebuf[i], (nread - i));
slouken@1895
   770
        start = (nread - i);
slouken@1895
   771
    } else {
slouken@1895
   772
        start = 0;
slouken@1895
   773
    }
slouken@1895
   774
    return;
slouken@70
   775
}
slouken@70
   776
slouken@1895
   777
static void
slouken@1895
   778
handle_keyboard(_THIS)
slouken@70
   779
{
slouken@1895
   780
    unsigned char keybuf[BUFSIZ];
slouken@1895
   781
    int i, nread;
slouken@1895
   782
    int pressed;
slouken@1895
   783
    int scancode;
slouken@1895
   784
    SDL_keysym keysym;
slouken@70
   785
slouken@1895
   786
    nread = read(keyboard_fd, keybuf, BUFSIZ);
slouken@1895
   787
    for (i = 0; i < nread; ++i) {
slouken@1895
   788
        scancode = keybuf[i] & 0x7F;
slouken@1895
   789
        if (keybuf[i] & 0x80) {
slouken@1895
   790
            pressed = SDL_RELEASED;
slouken@1895
   791
        } else {
slouken@1895
   792
            pressed = SDL_PRESSED;
slouken@1895
   793
        }
slouken@1895
   794
        TranslateKey(scancode, &keysym);
slouken@1895
   795
        posted += SDL_PrivateKeyboard(pressed, &keysym);
slouken@1895
   796
    }
slouken@70
   797
}
slouken@70
   798
slouken@1895
   799
void
slouken@1895
   800
GS_PumpEvents(_THIS)
slouken@70
   801
{
slouken@1895
   802
    fd_set fdset;
slouken@1895
   803
    int max_fd;
slouken@1895
   804
    static struct timeval zero;
slouken@70
   805
slouken@1895
   806
    do {
slouken@1895
   807
        posted = 0;
slouken@70
   808
slouken@1895
   809
        FD_ZERO(&fdset);
slouken@1895
   810
        max_fd = 0;
slouken@1895
   811
        if (keyboard_fd >= 0) {
slouken@1895
   812
            FD_SET(keyboard_fd, &fdset);
slouken@1895
   813
            if (max_fd < keyboard_fd) {
slouken@1895
   814
                max_fd = keyboard_fd;
slouken@1895
   815
            }
slouken@1895
   816
        }
slouken@1895
   817
        if (mouse_fd >= 0) {
slouken@1895
   818
            FD_SET(mouse_fd, &fdset);
slouken@1895
   819
            if (max_fd < mouse_fd) {
slouken@1895
   820
                max_fd = mouse_fd;
slouken@1895
   821
            }
slouken@1895
   822
        }
slouken@1895
   823
        if (select(max_fd + 1, &fdset, NULL, NULL, &zero) > 0) {
slouken@1895
   824
            if (keyboard_fd >= 0) {
slouken@1895
   825
                if (FD_ISSET(keyboard_fd, &fdset)) {
slouken@1895
   826
                    handle_keyboard(this);
slouken@1895
   827
                }
slouken@1895
   828
            }
slouken@1895
   829
            if (mouse_fd >= 0) {
slouken@1895
   830
                if (FD_ISSET(mouse_fd, &fdset)) {
slouken@1895
   831
                    handle_mouse(this);
slouken@1895
   832
                }
slouken@1895
   833
            }
slouken@1895
   834
        }
slouken@2735
   835
    } while (posted);
slouken@70
   836
}
slouken@70
   837
slouken@1895
   838
void
slouken@1895
   839
GS_InitOSKeymap(_THIS)
slouken@70
   840
{
slouken@1895
   841
    int i;
slouken@70
   842
slouken@1895
   843
    /* Initialize the Linux key translation table */
slouken@70
   844
slouken@1895
   845
    /* First get the ascii keys and others not well handled */
slouken@1895
   846
    for (i = 0; i < SDL_arraysize(keymap); ++i) {
slouken@1895
   847
        switch (i) {
slouken@1895
   848
            /* These aren't handled by the x86 kernel keymapping (?) */
slouken@1895
   849
        case SCANCODE_PRINTSCREEN:
slouken@1895
   850
            keymap[i] = SDLK_PRINT;
slouken@1895
   851
            break;
slouken@1895
   852
        case SCANCODE_BREAK:
slouken@1895
   853
            keymap[i] = SDLK_BREAK;
slouken@1895
   854
            break;
slouken@1895
   855
        case SCANCODE_BREAK_ALTERNATIVE:
slouken@1895
   856
            keymap[i] = SDLK_PAUSE;
slouken@1895
   857
            break;
slouken@1895
   858
        case SCANCODE_LEFTSHIFT:
slouken@1895
   859
            keymap[i] = SDLK_LSHIFT;
slouken@1895
   860
            break;
slouken@1895
   861
        case SCANCODE_RIGHTSHIFT:
slouken@1895
   862
            keymap[i] = SDLK_RSHIFT;
slouken@1895
   863
            break;
slouken@1895
   864
        case SCANCODE_LEFTCONTROL:
slouken@1895
   865
            keymap[i] = SDLK_LCTRL;
slouken@1895
   866
            break;
slouken@1895
   867
        case SCANCODE_RIGHTCONTROL:
slouken@1895
   868
            keymap[i] = SDLK_RCTRL;
slouken@1895
   869
            break;
slouken@1895
   870
        case SCANCODE_RIGHTWIN:
slouken@1895
   871
            keymap[i] = SDLK_RSUPER;
slouken@1895
   872
            break;
slouken@1895
   873
        case SCANCODE_LEFTWIN:
slouken@1895
   874
            keymap[i] = SDLK_LSUPER;
slouken@1895
   875
            break;
slouken@1895
   876
        case 127:
slouken@1895
   877
            keymap[i] = SDLK_MENU;
slouken@1895
   878
            break;
slouken@1895
   879
            /* this should take care of all standard ascii keys */
slouken@1895
   880
        default:
slouken@1895
   881
            keymap[i] = KVAL(vga_keymap[0][i]);
slouken@1895
   882
            break;
slouken@1895
   883
        }
slouken@1895
   884
    }
slouken@1895
   885
    for (i = 0; i < SDL_arraysize(keymap); ++i) {
slouken@1895
   886
        switch (keymap_temp[i]) {
slouken@1895
   887
        case K_F1:
slouken@1895
   888
            keymap[i] = SDLK_F1;
slouken@1895
   889
            break;
slouken@1895
   890
        case K_F2:
slouken@1895
   891
            keymap[i] = SDLK_F2;
slouken@1895
   892
            break;
slouken@1895
   893
        case K_F3:
slouken@1895
   894
            keymap[i] = SDLK_F3;
slouken@1895
   895
            break;
slouken@1895
   896
        case K_F4:
slouken@1895
   897
            keymap[i] = SDLK_F4;
slouken@1895
   898
            break;
slouken@1895
   899
        case K_F5:
slouken@1895
   900
            keymap[i] = SDLK_F5;
slouken@1895
   901
            break;
slouken@1895
   902
        case K_F6:
slouken@1895
   903
            keymap[i] = SDLK_F6;
slouken@1895
   904
            break;
slouken@1895
   905
        case K_F7:
slouken@1895
   906
            keymap[i] = SDLK_F7;
slouken@1895
   907
            break;
slouken@1895
   908
        case K_F8:
slouken@1895
   909
            keymap[i] = SDLK_F8;
slouken@1895
   910
            break;
slouken@1895
   911
        case K_F9:
slouken@1895
   912
            keymap[i] = SDLK_F9;
slouken@1895
   913
            break;
slouken@1895
   914
        case K_F10:
slouken@1895
   915
            keymap[i] = SDLK_F10;
slouken@1895
   916
            break;
slouken@1895
   917
        case K_F11:
slouken@1895
   918
            keymap[i] = SDLK_F11;
slouken@1895
   919
            break;
slouken@1895
   920
        case K_F12:
slouken@1895
   921
            keymap[i] = SDLK_F12;
slouken@1895
   922
            break;
slouken@70
   923
slouken@1895
   924
        case K_DOWN:
slouken@1895
   925
            keymap[i] = SDLK_DOWN;
slouken@1895
   926
            break;
slouken@1895
   927
        case K_LEFT:
slouken@1895
   928
            keymap[i] = SDLK_LEFT;
slouken@1895
   929
            break;
slouken@1895
   930
        case K_RIGHT:
slouken@1895
   931
            keymap[i] = SDLK_RIGHT;
slouken@1895
   932
            break;
slouken@1895
   933
        case K_UP:
slouken@1895
   934
            keymap[i] = SDLK_UP;
slouken@1895
   935
            break;
slouken@70
   936
slouken@1895
   937
        case K_P0:
slouken@1895
   938
            keymap[i] = SDLK_KP0;
slouken@1895
   939
            break;
slouken@1895
   940
        case K_P1:
slouken@1895
   941
            keymap[i] = SDLK_KP1;
slouken@1895
   942
            break;
slouken@1895
   943
        case K_P2:
slouken@1895
   944
            keymap[i] = SDLK_KP2;
slouken@1895
   945
            break;
slouken@1895
   946
        case K_P3:
slouken@1895
   947
            keymap[i] = SDLK_KP3;
slouken@1895
   948
            break;
slouken@1895
   949
        case K_P4:
slouken@1895
   950
            keymap[i] = SDLK_KP4;
slouken@1895
   951
            break;
slouken@1895
   952
        case K_P5:
slouken@1895
   953
            keymap[i] = SDLK_KP5;
slouken@1895
   954
            break;
slouken@1895
   955
        case K_P6:
slouken@1895
   956
            keymap[i] = SDLK_KP6;
slouken@1895
   957
            break;
slouken@1895
   958
        case K_P7:
slouken@1895
   959
            keymap[i] = SDLK_KP7;
slouken@1895
   960
            break;
slouken@1895
   961
        case K_P8:
slouken@1895
   962
            keymap[i] = SDLK_KP8;
slouken@1895
   963
            break;
slouken@1895
   964
        case K_P9:
slouken@1895
   965
            keymap[i] = SDLK_KP9;
slouken@1895
   966
            break;
slouken@1895
   967
        case K_PPLUS:
slouken@1895
   968
            keymap[i] = SDLK_KP_PLUS;
slouken@1895
   969
            break;
slouken@1895
   970
        case K_PMINUS:
slouken@1895
   971
            keymap[i] = SDLK_KP_MINUS;
slouken@1895
   972
            break;
slouken@1895
   973
        case K_PSTAR:
slouken@1895
   974
            keymap[i] = SDLK_KP_MULTIPLY;
slouken@1895
   975
            break;
slouken@1895
   976
        case K_PSLASH:
slouken@1895
   977
            keymap[i] = SDLK_KP_DIVIDE;
slouken@1895
   978
            break;
slouken@1895
   979
        case K_PENTER:
slouken@1895
   980
            keymap[i] = SDLK_KP_ENTER;
slouken@1895
   981
            break;
slouken@1895
   982
        case K_PDOT:
slouken@1895
   983
            keymap[i] = SDLK_KP_PERIOD;
slouken@1895
   984
            break;
slouken@70
   985
slouken@1895
   986
        case K_SHIFT:
slouken@1895
   987
            if (keymap[i] != SDLK_RSHIFT)
slouken@1895
   988
                keymap[i] = SDLK_LSHIFT;
slouken@1895
   989
            break;
slouken@1895
   990
        case K_SHIFTL:
slouken@1895
   991
            keymap[i] = SDLK_LSHIFT;
slouken@1895
   992
            break;
slouken@1895
   993
        case K_SHIFTR:
slouken@1895
   994
            keymap[i] = SDLK_RSHIFT;
slouken@1895
   995
            break;
slouken@1895
   996
        case K_CTRL:
slouken@1895
   997
            if (keymap[i] != SDLK_RCTRL)
slouken@1895
   998
                keymap[i] = SDLK_LCTRL;
slouken@1895
   999
            break;
slouken@1895
  1000
        case K_CTRLL:
slouken@1895
  1001
            keymap[i] = SDLK_LCTRL;
slouken@1895
  1002
            break;
slouken@1895
  1003
        case K_CTRLR:
slouken@1895
  1004
            keymap[i] = SDLK_RCTRL;
slouken@1895
  1005
            break;
slouken@1895
  1006
        case K_ALT:
slouken@1895
  1007
            keymap[i] = SDLK_LALT;
slouken@1895
  1008
            break;
slouken@1895
  1009
        case K_ALTGR:
slouken@1895
  1010
            keymap[i] = SDLK_RALT;
slouken@1895
  1011
            break;
slouken@70
  1012
slouken@1895
  1013
        case K_INSERT:
slouken@1895
  1014
            keymap[i] = SDLK_INSERT;
slouken@1895
  1015
            break;
slouken@1895
  1016
        case K_REMOVE:
slouken@1895
  1017
            keymap[i] = SDLK_DELETE;
slouken@1895
  1018
            break;
slouken@1895
  1019
        case K_PGUP:
slouken@1895
  1020
            keymap[i] = SDLK_PAGEUP;
slouken@1895
  1021
            break;
slouken@1895
  1022
        case K_PGDN:
slouken@1895
  1023
            keymap[i] = SDLK_PAGEDOWN;
slouken@1895
  1024
            break;
slouken@1895
  1025
        case K_FIND:
slouken@1895
  1026
            keymap[i] = SDLK_HOME;
slouken@1895
  1027
            break;
slouken@1895
  1028
        case K_SELECT:
slouken@1895
  1029
            keymap[i] = SDLK_END;
slouken@1895
  1030
            break;
slouken@70
  1031
slouken@1895
  1032
        case K_NUM:
slouken@1895
  1033
            keymap[i] = SDLK_NUMLOCK;
slouken@1895
  1034
            break;
slouken@1895
  1035
        case K_CAPS:
slouken@1895
  1036
            keymap[i] = SDLK_CAPSLOCK;
slouken@1895
  1037
            break;
slouken@70
  1038
slouken@1895
  1039
        case K_F13:
slouken@1895
  1040
            keymap[i] = SDLK_PRINT;
slouken@1895
  1041
            break;
slouken@1895
  1042
        case K_HOLD:
slouken@1895
  1043
            keymap[i] = SDLK_SCROLLOCK;
slouken@1895
  1044
            break;
slouken@1895
  1045
        case K_PAUSE:
slouken@1895
  1046
            keymap[i] = SDLK_PAUSE;
slouken@1895
  1047
            break;
slouken@70
  1048
slouken@1895
  1049
        case 127:
slouken@1895
  1050
            keymap[i] = SDLK_BACKSPACE;
slouken@1895
  1051
            break;
slouken@1895
  1052
slouken@1895
  1053
        default:
slouken@1895
  1054
            break;
slouken@1895
  1055
        }
slouken@1895
  1056
    }
slouken@70
  1057
}
slouken@70
  1058
slouken@1895
  1059
static SDL_keysym *
slouken@1895
  1060
TranslateKey(int scancode, SDL_keysym * keysym)
slouken@70
  1061
{
slouken@1895
  1062
    /* Set the keysym information */
slouken@1895
  1063
    keysym->scancode = scancode;
slouken@1895
  1064
    keysym->sym = keymap[scancode];
slouken@1895
  1065
    keysym->mod = KMOD_NONE;
slouken@70
  1066
slouken@1895
  1067
    /* If UNICODE is on, get the UNICODE value for the key */
slouken@1895
  1068
    keysym->unicode = 0;
slouken@1895
  1069
    if (SDL_TranslateUNICODE) {
slouken@1895
  1070
        int map;
slouken@1895
  1071
        SDLMod modstate;
slouken@70
  1072
slouken@1895
  1073
        modstate = SDL_GetModState();
slouken@1895
  1074
        map = 0;
slouken@1895
  1075
        if (modstate & KMOD_SHIFT) {
slouken@1895
  1076
            map |= (1 << KG_SHIFT);
slouken@1895
  1077
        }
slouken@1895
  1078
        if (modstate & KMOD_CTRL) {
slouken@1895
  1079
            map |= (1 << KG_CTRL);
slouken@1895
  1080
        }
slouken@1895
  1081
        if (modstate & KMOD_ALT) {
slouken@1895
  1082
            map |= (1 << KG_ALT);
slouken@1895
  1083
        }
slouken@1895
  1084
        if (modstate & KMOD_MODE) {
slouken@1895
  1085
            map |= (1 << KG_ALTGR);
slouken@1895
  1086
        }
slouken@1895
  1087
        if (KTYP(vga_keymap[map][scancode]) == KT_LETTER) {
slouken@1895
  1088
            if (modstate & KMOD_CAPS) {
slouken@1895
  1089
                map ^= (1 << KG_SHIFT);
slouken@1895
  1090
            }
slouken@1895
  1091
        }
slouken@1895
  1092
        if (KTYP(vga_keymap[map][scancode]) == KT_PAD) {
slouken@1895
  1093
            if (modstate & KMOD_NUM) {
slouken@1895
  1094
                keysym->unicode = KVAL(vga_keymap[map][scancode]);
slouken@1895
  1095
            }
slouken@1895
  1096
        } else {
slouken@1895
  1097
            keysym->unicode = KVAL(vga_keymap[map][scancode]);
slouken@1895
  1098
        }
slouken@1895
  1099
    }
slouken@1895
  1100
    return (keysym);
slouken@70
  1101
}
slouken@1895
  1102
slouken@1895
  1103
/* vi: set ts=4 sw=4 expandtab: */