src/video/fbcon/SDL_fbevents.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
slouken@0
     1
/*
slouken@1312
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     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@0
     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@0
    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@0
    18
slouken@1312
    19
    Sam Lantinga
slouken@1312
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Handle the event stream, converting console events into SDL events */
slouken@0
    25
slouken@1338
    26
#include <stdio.h>
slouken@0
    27
#include <sys/types.h>
slouken@0
    28
#include <sys/time.h>
slouken@0
    29
#include <sys/ioctl.h>
slouken@0
    30
#include <unistd.h>
slouken@0
    31
#include <fcntl.h>
slouken@0
    32
#include <errno.h>
slouken@0
    33
#include <limits.h>
slouken@0
    34
slouken@0
    35
/* For parsing /proc */
slouken@0
    36
#include <dirent.h>
slouken@0
    37
#include <ctype.h>
slouken@0
    38
slouken@0
    39
#include <linux/vt.h>
slouken@0
    40
#include <linux/kd.h>
slouken@0
    41
#include <linux/keyboard.h>
slouken@0
    42
slouken@1359
    43
#include "SDL_timer.h"
slouken@0
    44
#include "SDL_mutex.h"
slouken@1361
    45
#include "../SDL_sysvideo.h"
slouken@1361
    46
#include "../../events/SDL_sysevents.h"
slouken@1361
    47
#include "../../events/SDL_events_c.h"
slouken@0
    48
#include "SDL_fbvideo.h"
slouken@0
    49
#include "SDL_fbevents_c.h"
slouken@0
    50
#include "SDL_fbkeys.h"
slouken@0
    51
slouken@0
    52
#include "SDL_fbelo.h"
slouken@0
    53
slouken@0
    54
#ifndef GPM_NODE_FIFO
slouken@0
    55
#define GPM_NODE_FIFO	"/dev/gpmdata"
slouken@0
    56
#endif
slouken@0
    57
slouken@1659
    58
/*#define DEBUG_KEYBOARD*/
slouken@1659
    59
/*#define DEBUG_MOUSE*/
slouken@0
    60
slouken@0
    61
/* The translation tables from a console scancode to a SDL keysym */
slouken@0
    62
#define NUM_VGAKEYMAPS	(1<<KG_CAPSSHIFT)
slouken@0
    63
static Uint16 vga_keymap[NUM_VGAKEYMAPS][NR_KEYS];
slouken@0
    64
static SDLKey keymap[128];
slouken@0
    65
static Uint16 keymap_temp[128]; /* only used at startup */
slouken@1668
    66
static SDL_keysym *TranslateKey(int scancode, SDL_keysym * keysym);
slouken@0
    67
slouken@0
    68
/* Ugh, we have to duplicate the kernel's keysym mapping code...
slouken@0
    69
   Oh, it's not so bad. :-)
slouken@0
    70
slouken@0
    71
   FIXME: Add keyboard LED handling code
slouken@0
    72
 */
slouken@1662
    73
static void
slouken@1668
    74
FB_vgainitkeymaps(int fd)
slouken@0
    75
{
slouken@1662
    76
    struct kbentry entry;
slouken@1662
    77
    int map, i;
slouken@0
    78
slouken@1662
    79
    /* Don't do anything if we are passed a closed keyboard */
slouken@1662
    80
    if (fd < 0) {
slouken@1662
    81
        return;
slouken@1662
    82
    }
slouken@0
    83
slouken@1662
    84
    /* Load all the keysym mappings */
slouken@1662
    85
    for (map = 0; map < NUM_VGAKEYMAPS; ++map) {
slouken@1668
    86
        SDL_memset(vga_keymap[map], 0, NR_KEYS * sizeof(Uint16));
slouken@1662
    87
        for (i = 0; i < NR_KEYS; ++i) {
slouken@1662
    88
            entry.kb_table = map;
slouken@1662
    89
            entry.kb_index = i;
slouken@1668
    90
            if (ioctl(fd, KDGKBENT, &entry) == 0) {
slouken@1662
    91
                /* fill keytemp. This replaces SDL_fbkeys.h */
slouken@1662
    92
                if ((map == 0) && (i < 128)) {
slouken@1662
    93
                    keymap_temp[i] = entry.kb_value;
slouken@1662
    94
                }
slouken@1662
    95
                /* The "Enter" key is a special case */
slouken@1662
    96
                if (entry.kb_value == K_ENTER) {
slouken@1668
    97
                    entry.kb_value = K(KT_ASCII, 13);
slouken@1662
    98
                }
slouken@1662
    99
                /* Handle numpad specially as well */
slouken@1668
   100
                if (KTYP(entry.kb_value) == KT_PAD) {
slouken@1662
   101
                    switch (entry.kb_value) {
slouken@1662
   102
                    case K_P0:
slouken@1662
   103
                    case K_P1:
slouken@1662
   104
                    case K_P2:
slouken@1662
   105
                    case K_P3:
slouken@1662
   106
                    case K_P4:
slouken@1662
   107
                    case K_P5:
slouken@1662
   108
                    case K_P6:
slouken@1662
   109
                    case K_P7:
slouken@1662
   110
                    case K_P8:
slouken@1662
   111
                    case K_P9:
slouken@1662
   112
                        vga_keymap[map][i] = entry.kb_value;
slouken@1662
   113
                        vga_keymap[map][i] += '0';
slouken@1662
   114
                        break;
slouken@1662
   115
                    case K_PPLUS:
slouken@1668
   116
                        vga_keymap[map][i] = K(KT_ASCII, '+');
slouken@1662
   117
                        break;
slouken@1662
   118
                    case K_PMINUS:
slouken@1668
   119
                        vga_keymap[map][i] = K(KT_ASCII, '-');
slouken@1662
   120
                        break;
slouken@1662
   121
                    case K_PSTAR:
slouken@1668
   122
                        vga_keymap[map][i] = K(KT_ASCII, '*');
slouken@1662
   123
                        break;
slouken@1662
   124
                    case K_PSLASH:
slouken@1668
   125
                        vga_keymap[map][i] = K(KT_ASCII, '/');
slouken@1662
   126
                        break;
slouken@1662
   127
                    case K_PENTER:
slouken@1668
   128
                        vga_keymap[map][i] = K(KT_ASCII, '\r');
slouken@1662
   129
                        break;
slouken@1662
   130
                    case K_PCOMMA:
slouken@1668
   131
                        vga_keymap[map][i] = K(KT_ASCII, ',');
slouken@1662
   132
                        break;
slouken@1662
   133
                    case K_PDOT:
slouken@1668
   134
                        vga_keymap[map][i] = K(KT_ASCII, '.');
slouken@1662
   135
                        break;
slouken@1662
   136
                    default:
slouken@1662
   137
                        break;
slouken@1662
   138
                    }
slouken@1662
   139
                }
slouken@1662
   140
                /* Do the normal key translation */
slouken@1668
   141
                if ((KTYP(entry.kb_value) == KT_LATIN) ||
slouken@1668
   142
                    (KTYP(entry.kb_value) == KT_ASCII) ||
slouken@1668
   143
                    (KTYP(entry.kb_value) == KT_LETTER)) {
slouken@1662
   144
                    vga_keymap[map][i] = entry.kb_value;
slouken@1662
   145
                }
slouken@1662
   146
            }
slouken@1662
   147
        }
slouken@1662
   148
    }
slouken@0
   149
}
slouken@0
   150
slouken@1662
   151
int
slouken@1668
   152
FB_InGraphicsMode(_THIS)
slouken@0
   153
{
slouken@1662
   154
    return ((keyboard_fd >= 0) && (saved_kbd_mode >= 0));
slouken@0
   155
}
slouken@0
   156
slouken@1662
   157
int
slouken@1668
   158
FB_EnterGraphicsMode(_THIS)
slouken@0
   159
{
slouken@1662
   160
    struct termios keyboard_termios;
slouken@0
   161
slouken@1662
   162
    /* Set medium-raw keyboard mode */
slouken@1668
   163
    if ((keyboard_fd >= 0) && !FB_InGraphicsMode(this)) {
slouken@0
   164
slouken@1662
   165
        /* Switch to the correct virtual terminal */
slouken@1662
   166
        if (current_vt > 0) {
slouken@1662
   167
            struct vt_stat vtstate;
slouken@0
   168
slouken@1668
   169
            if (ioctl(keyboard_fd, VT_GETSTATE, &vtstate) == 0) {
slouken@1662
   170
                saved_vt = vtstate.v_active;
slouken@1662
   171
            }
slouken@1668
   172
            if (ioctl(keyboard_fd, VT_ACTIVATE, current_vt) == 0) {
slouken@1668
   173
                ioctl(keyboard_fd, VT_WAITACTIVE, current_vt);
slouken@1662
   174
            }
slouken@1662
   175
        }
slouken@0
   176
slouken@1662
   177
        /* Set the terminal input mode */
slouken@1668
   178
        if (tcgetattr(keyboard_fd, &saved_kbd_termios) < 0) {
slouken@1668
   179
            SDL_SetError("Unable to get terminal attributes");
slouken@1662
   180
            if (keyboard_fd > 0) {
slouken@1668
   181
                close(keyboard_fd);
slouken@1662
   182
            }
slouken@1662
   183
            keyboard_fd = -1;
slouken@1662
   184
            return (-1);
slouken@1662
   185
        }
slouken@1668
   186
        if (ioctl(keyboard_fd, KDGKBMODE, &saved_kbd_mode) < 0) {
slouken@1668
   187
            SDL_SetError("Unable to get current keyboard mode");
slouken@1662
   188
            if (keyboard_fd > 0) {
slouken@1668
   189
                close(keyboard_fd);
slouken@1662
   190
            }
slouken@1662
   191
            keyboard_fd = -1;
slouken@1662
   192
            return (-1);
slouken@1662
   193
        }
slouken@1662
   194
        keyboard_termios = saved_kbd_termios;
slouken@1662
   195
        keyboard_termios.c_lflag &= ~(ICANON | ECHO | ISIG);
slouken@1662
   196
        keyboard_termios.c_iflag &=
slouken@1662
   197
            ~(ISTRIP | IGNCR | ICRNL | INLCR | IXOFF | IXON);
slouken@1662
   198
        keyboard_termios.c_cc[VMIN] = 0;
slouken@1662
   199
        keyboard_termios.c_cc[VTIME] = 0;
slouken@1668
   200
        if (tcsetattr(keyboard_fd, TCSAFLUSH, &keyboard_termios) < 0) {
slouken@1668
   201
            FB_CloseKeyboard(this);
slouken@1668
   202
            SDL_SetError("Unable to set terminal attributes");
slouken@1662
   203
            return (-1);
slouken@1662
   204
        }
slouken@1662
   205
        /* This will fail if we aren't root or this isn't our tty */
slouken@1668
   206
        if (ioctl(keyboard_fd, KDSKBMODE, K_MEDIUMRAW) < 0) {
slouken@1668
   207
            FB_CloseKeyboard(this);
slouken@1668
   208
            SDL_SetError("Unable to set keyboard in raw mode");
slouken@1662
   209
            return (-1);
slouken@1662
   210
        }
slouken@1668
   211
        if (ioctl(keyboard_fd, KDSETMODE, KD_GRAPHICS) < 0) {
slouken@1668
   212
            FB_CloseKeyboard(this);
slouken@1668
   213
            SDL_SetError("Unable to set keyboard in graphics mode");
slouken@1662
   214
            return (-1);
slouken@1662
   215
        }
slouken@1662
   216
        /* Prevent switching the virtual terminal */
slouken@1668
   217
        ioctl(keyboard_fd, VT_LOCKSWITCH, 1);
slouken@1662
   218
    }
slouken@1662
   219
    return (keyboard_fd);
slouken@0
   220
}
slouken@0
   221
slouken@1662
   222
void
slouken@1668
   223
FB_LeaveGraphicsMode(_THIS)
slouken@0
   224
{
slouken@1668
   225
    if (FB_InGraphicsMode(this)) {
slouken@1668
   226
        ioctl(keyboard_fd, KDSETMODE, KD_TEXT);
slouken@1668
   227
        ioctl(keyboard_fd, KDSKBMODE, saved_kbd_mode);
slouken@1668
   228
        tcsetattr(keyboard_fd, TCSAFLUSH, &saved_kbd_termios);
slouken@1662
   229
        saved_kbd_mode = -1;
slouken@0
   230
slouken@1662
   231
        /* Head back over to the original virtual terminal */
slouken@1668
   232
        ioctl(keyboard_fd, VT_UNLOCKSWITCH, 1);
slouken@1662
   233
        if (saved_vt > 0) {
slouken@1668
   234
            ioctl(keyboard_fd, VT_ACTIVATE, saved_vt);
slouken@1662
   235
        }
slouken@1662
   236
    }
slouken@0
   237
}
slouken@0
   238
slouken@1662
   239
void
slouken@1668
   240
FB_CloseKeyboard(_THIS)
slouken@0
   241
{
slouken@1662
   242
    if (keyboard_fd >= 0) {
slouken@1668
   243
        FB_LeaveGraphicsMode(this);
slouken@1662
   244
        if (keyboard_fd > 0) {
slouken@1668
   245
            close(keyboard_fd);
slouken@1662
   246
        }
slouken@1662
   247
    }
slouken@1662
   248
    keyboard_fd = -1;
slouken@0
   249
}
slouken@0
   250
slouken@1662
   251
int
slouken@1668
   252
FB_OpenKeyboard(_THIS)
slouken@0
   253
{
slouken@1662
   254
    /* Open only if not already opened */
slouken@1662
   255
    if (keyboard_fd < 0) {
slouken@1662
   256
        static const char *const tty0[] = { "/dev/tty0", "/dev/vc/0", NULL };
slouken@1662
   257
        static const char *const vcs[] = { "/dev/vc/%d", "/dev/tty%d", NULL };
slouken@1662
   258
        int i, tty0_fd;
slouken@0
   259
slouken@1662
   260
        /* Try to query for a free virtual terminal */
slouken@1662
   261
        tty0_fd = -1;
slouken@1662
   262
        for (i = 0; tty0[i] && (tty0_fd < 0); ++i) {
slouken@1668
   263
            tty0_fd = open(tty0[i], O_WRONLY, 0);
slouken@1662
   264
        }
slouken@1662
   265
        if (tty0_fd < 0) {
slouken@1668
   266
            tty0_fd = dup(0);   /* Maybe stdin is a VT? */
slouken@1662
   267
        }
slouken@1668
   268
        ioctl(tty0_fd, VT_OPENQRY, &current_vt);
slouken@1668
   269
        close(tty0_fd);
slouken@1668
   270
        if ((geteuid() == 0) && (current_vt > 0)) {
slouken@1662
   271
            for (i = 0; vcs[i] && (keyboard_fd < 0); ++i) {
slouken@1662
   272
                char vtpath[12];
slouken@0
   273
slouken@1668
   274
                SDL_snprintf(vtpath, SDL_arraysize(vtpath), vcs[i],
slouken@1668
   275
                             current_vt);
slouken@1668
   276
                keyboard_fd = open(vtpath, O_RDWR, 0);
slouken@0
   277
#ifdef DEBUG_KEYBOARD
slouken@1668
   278
                fprintf(stderr, "vtpath = %s, fd = %d\n",
slouken@1668
   279
                        vtpath, keyboard_fd);
slouken@0
   280
#endif /* DEBUG_KEYBOARD */
slouken@0
   281
slouken@1662
   282
                /* This needs to be our controlling tty
slouken@1662
   283
                   so that the kernel ioctl() calls work
slouken@1662
   284
                 */
slouken@1662
   285
                if (keyboard_fd >= 0) {
slouken@1668
   286
                    tty0_fd = open("/dev/tty", O_RDWR, 0);
slouken@1662
   287
                    if (tty0_fd >= 0) {
slouken@1668
   288
                        ioctl(tty0_fd, TIOCNOTTY, 0);
slouken@1668
   289
                        close(tty0_fd);
slouken@1662
   290
                    }
slouken@1662
   291
                }
slouken@1662
   292
            }
slouken@1662
   293
        }
slouken@1662
   294
        if (keyboard_fd < 0) {
slouken@1662
   295
            /* Last resort, maybe our tty is a usable VT */
slouken@1662
   296
            struct vt_stat vtstate;
slouken@1659
   297
slouken@1668
   298
            keyboard_fd = open("/dev/tty", O_RDWR);
slouken@1659
   299
slouken@1668
   300
            if (ioctl(keyboard_fd, VT_GETSTATE, &vtstate) == 0) {
slouken@1662
   301
                current_vt = vtstate.v_active;
slouken@1662
   302
            } else {
slouken@1662
   303
                current_vt = 0;
slouken@1662
   304
            }
slouken@1662
   305
        }
slouken@0
   306
#ifdef DEBUG_KEYBOARD
slouken@1668
   307
        fprintf(stderr, "Current VT: %d\n", current_vt);
slouken@0
   308
#endif
slouken@1662
   309
        saved_kbd_mode = -1;
slouken@0
   310
slouken@1662
   311
        /* Make sure that our input is a console terminal */
slouken@1662
   312
        {
slouken@1662
   313
            int dummy;
slouken@1668
   314
            if (ioctl(keyboard_fd, KDGKBMODE, &dummy) < 0) {
slouken@1668
   315
                close(keyboard_fd);
slouken@1662
   316
                keyboard_fd = -1;
slouken@1668
   317
                SDL_SetError("Unable to open a console terminal");
slouken@1662
   318
            }
slouken@1662
   319
        }
slouken@0
   320
slouken@1662
   321
        /* Set up keymap */
slouken@1668
   322
        FB_vgainitkeymaps(keyboard_fd);
slouken@1662
   323
    }
slouken@1662
   324
    return (keyboard_fd);
slouken@0
   325
}
slouken@0
   326
slouken@1662
   327
static enum
slouken@1662
   328
{
slouken@1662
   329
    MOUSE_NONE = -1,
slouken@1662
   330
    MOUSE_MSC,                  /* Note: GPM uses the MSC protocol */
slouken@1662
   331
    MOUSE_PS2,
slouken@1662
   332
    MOUSE_IMPS2,
slouken@1662
   333
    MOUSE_MS,
slouken@1662
   334
    MOUSE_BM,
slouken@1662
   335
    MOUSE_ELO,
slouken@1662
   336
    MOUSE_TSLIB,
slouken@1662
   337
    NUM_MOUSE_DRVS
slouken@0
   338
} mouse_drv = MOUSE_NONE;
slouken@0
   339
slouken@1662
   340
void
slouken@1668
   341
FB_CloseMouse(_THIS)
slouken@0
   342
{
slouken@1361
   343
#if SDL_INPUT_TSLIB
slouken@1662
   344
    if (ts_dev != NULL) {
slouken@1668
   345
        ts_close(ts_dev);
slouken@1662
   346
        ts_dev = NULL;
slouken@1662
   347
        mouse_fd = -1;
slouken@1662
   348
    }
slouken@1361
   349
#endif /* SDL_INPUT_TSLIB */
slouken@1662
   350
    if (mouse_fd > 0) {
slouken@1668
   351
        close(mouse_fd);
slouken@1662
   352
    }
slouken@1662
   353
    mouse_fd = -1;
slouken@0
   354
}
slouken@0
   355
slouken@0
   356
/* Returns processes listed in /proc with the desired name */
slouken@1662
   357
static int
slouken@1668
   358
find_pid(DIR * proc, const char *wanted_name)
slouken@0
   359
{
slouken@1662
   360
    struct dirent *entry;
slouken@1662
   361
    int pid;
slouken@0
   362
slouken@1662
   363
    /* First scan proc for the gpm process */
slouken@1662
   364
    pid = 0;
slouken@1668
   365
    while ((pid == 0) && ((entry = readdir(proc)) != NULL)) {
slouken@1668
   366
        if (isdigit(entry->d_name[0])) {
slouken@1662
   367
            FILE *status;
slouken@1662
   368
            char path[PATH_MAX];
slouken@1662
   369
            char name[PATH_MAX];
slouken@0
   370
slouken@1668
   371
            SDL_snprintf(path, SDL_arraysize(path), "/proc/%s/status",
slouken@1668
   372
                         entry->d_name);
slouken@1668
   373
            status = fopen(path, "r");
slouken@1662
   374
            if (status) {
slouken@1662
   375
                name[0] = '\0';
slouken@1668
   376
                fscanf(status, "Name: %s", name);
slouken@1668
   377
                if (SDL_strcmp(name, wanted_name) == 0) {
slouken@1668
   378
                    pid = SDL_atoi(entry->d_name);
slouken@1662
   379
                }
slouken@1668
   380
                fclose(status);
slouken@1662
   381
            }
slouken@1662
   382
        }
slouken@1662
   383
    }
slouken@1662
   384
    return pid;
slouken@0
   385
}
slouken@0
   386
slouken@0
   387
/* Returns true if /dev/gpmdata is being written to by gpm */
slouken@1662
   388
static int
slouken@1668
   389
gpm_available(char *proto, size_t protolen)
slouken@0
   390
{
slouken@1662
   391
    int available;
slouken@1662
   392
    DIR *proc;
slouken@1662
   393
    int pid;
slouken@1662
   394
    int cmdline, len, arglen;
slouken@1662
   395
    char path[PATH_MAX];
slouken@1662
   396
    char args[PATH_MAX], *arg;
slouken@0
   397
slouken@1662
   398
    /* Don't bother looking if the fifo isn't there */
slouken@1662
   399
#ifdef DEBUG_MOUSE
slouken@1668
   400
    fprintf(stderr, "testing gpm\n");
slouken@1659
   401
#endif
slouken@1668
   402
    if (access(GPM_NODE_FIFO, F_OK) < 0) {
slouken@1662
   403
        return (0);
slouken@1662
   404
    }
slouken@0
   405
slouken@1662
   406
    available = 0;
slouken@1668
   407
    proc = opendir("/proc");
slouken@1662
   408
    if (proc) {
slouken@1662
   409
        char raw_proto[10] = { '\0' };
slouken@1662
   410
        char repeat_proto[10] = { '\0' };
slouken@1668
   411
        while (!available && (pid = find_pid(proc, "gpm")) > 0) {
slouken@1668
   412
            SDL_snprintf(path, SDL_arraysize(path), "/proc/%d/cmdline", pid);
slouken@1668
   413
            cmdline = open(path, O_RDONLY, 0);
slouken@1662
   414
            if (cmdline >= 0) {
slouken@1668
   415
                len = read(cmdline, args, sizeof(args));
slouken@1662
   416
                arg = args;
slouken@1662
   417
                while (len > 0) {
slouken@1668
   418
                    arglen = SDL_strlen(arg) + 1;
slouken@1662
   419
#ifdef DEBUG_MOUSE
slouken@1668
   420
                    fprintf(stderr, "gpm arg %s len %d\n", arg, arglen);
slouken@1659
   421
#endif
slouken@1668
   422
                    if (SDL_strcmp(arg, "-t") == 0) {
slouken@1662
   423
                        /* protocol string, keep it for later */
slouken@1662
   424
                        char *t, *s;
slouken@1662
   425
                        t = arg + arglen;
slouken@1668
   426
                        s = SDL_strchr(t, ' ');
slouken@1662
   427
                        if (s)
slouken@1662
   428
                            *s = 0;
slouken@1668
   429
                        SDL_strlcpy(raw_proto, t, SDL_arraysize(raw_proto));
slouken@1662
   430
                        if (s)
slouken@1662
   431
                            *s = ' ';
slouken@1662
   432
                    }
slouken@1668
   433
                    if (SDL_strncmp(arg, "-R", 2) == 0) {
slouken@1662
   434
                        char *t, *s;
slouken@1662
   435
                        available = 1;
slouken@1662
   436
                        t = arg + 2;
slouken@1668
   437
                        s = SDL_strchr(t, ' ');
slouken@1662
   438
                        if (s)
slouken@1662
   439
                            *s = 0;
slouken@1668
   440
                        SDL_strlcpy(repeat_proto, t,
slouken@1668
   441
                                    SDL_arraysize(repeat_proto));
slouken@1662
   442
                        if (s)
slouken@1662
   443
                            *s = ' ';
slouken@1662
   444
                    }
slouken@1662
   445
                    len -= arglen;
slouken@1662
   446
                    arg += arglen;
slouken@1662
   447
                }
slouken@1668
   448
                close(cmdline);
slouken@1662
   449
            }
slouken@1662
   450
        }
slouken@1668
   451
        closedir(proc);
slouken@1659
   452
slouken@1662
   453
        if (available) {
slouken@1668
   454
            if (SDL_strcmp(repeat_proto, "raw") == 0) {
slouken@1668
   455
                SDL_strlcpy(proto, raw_proto, protolen);
slouken@1662
   456
            } else if (*repeat_proto) {
slouken@1668
   457
                SDL_strlcpy(proto, repeat_proto, protolen);
slouken@1662
   458
            } else {
slouken@1668
   459
                SDL_strlcpy(proto, "msc", protolen);
slouken@1662
   460
            }
slouken@1662
   461
        }
slouken@1662
   462
    }
slouken@1662
   463
    return available;
slouken@0
   464
}
slouken@0
   465
slouken@59
   466
slouken@59
   467
/* rcg06112001 Set up IMPS/2 mode, if possible. This gives
slouken@59
   468
 *  us access to the mousewheel, etc. Returns zero if
slouken@59
   469
 *  writes to device failed, but you still need to query the
slouken@59
   470
 *  device to see which mode it's actually in.
slouken@59
   471
 */
slouken@1662
   472
static int
slouken@1668
   473
set_imps2_mode(int fd)
slouken@59
   474
{
slouken@1662
   475
    /* If you wanted to control the mouse mode (and we do :)  ) ...
slouken@1662
   476
       Set IMPS/2 protocol:
slouken@1662
   477
       {0xf3,200,0xf3,100,0xf3,80}
slouken@1662
   478
       Reset mouse device:
slouken@1662
   479
       {0xFF}
slouken@1662
   480
     */
slouken@1662
   481
    Uint8 set_imps2[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
slouken@1662
   482
    /*Uint8 reset = 0xff; */
slouken@1662
   483
    fd_set fdset;
slouken@1662
   484
    struct timeval tv;
slouken@1662
   485
    int retval = 0;
slouken@59
   486
slouken@1668
   487
    if (write(fd, &set_imps2, sizeof(set_imps2)) == sizeof(set_imps2)) {
slouken@1662
   488
        /* Don't reset it, that'll clear IMPS/2 mode on some mice
slouken@1662
   489
           if (write(fd, &reset, sizeof (reset)) == sizeof (reset) ) {
slouken@1662
   490
           retval = 1;
slouken@1662
   491
           }
slouken@1662
   492
         */
slouken@1662
   493
    }
slouken@59
   494
slouken@1662
   495
    /* Get rid of any chatter from the above */
slouken@1668
   496
    FD_ZERO(&fdset);
slouken@1668
   497
    FD_SET(fd, &fdset);
slouken@1662
   498
    tv.tv_sec = 0;
slouken@1662
   499
    tv.tv_usec = 0;
slouken@1668
   500
    while (select(fd + 1, &fdset, 0, 0, &tv) > 0) {
slouken@1662
   501
        char temp[32];
slouken@1668
   502
        read(fd, temp, sizeof(temp));
slouken@1662
   503
    }
slouken@59
   504
slouken@1662
   505
    return retval;
slouken@59
   506
}
slouken@59
   507
slouken@59
   508
slouken@0
   509
/* Returns true if the mouse uses the IMPS/2 protocol */
slouken@1662
   510
static int
slouken@1668
   511
detect_imps2(int fd)
slouken@0
   512
{
slouken@1662
   513
    int imps2;
slouken@0
   514
slouken@1662
   515
    imps2 = 0;
slouken@59
   516
slouken@1668
   517
    if (SDL_getenv("SDL_MOUSEDEV_IMPS2")) {
slouken@1662
   518
        imps2 = 1;
slouken@1662
   519
    }
slouken@1662
   520
    if (!imps2) {
slouken@1662
   521
        Uint8 query_ps2 = 0xF2;
slouken@1662
   522
        fd_set fdset;
slouken@1662
   523
        struct timeval tv;
slouken@0
   524
slouken@1662
   525
        /* Get rid of any mouse motion noise */
slouken@1668
   526
        FD_ZERO(&fdset);
slouken@1668
   527
        FD_SET(fd, &fdset);
slouken@1662
   528
        tv.tv_sec = 0;
slouken@1662
   529
        tv.tv_usec = 0;
slouken@1668
   530
        while (select(fd + 1, &fdset, 0, 0, &tv) > 0) {
slouken@1662
   531
            char temp[32];
slouken@1668
   532
            read(fd, temp, sizeof(temp));
slouken@1662
   533
        }
slouken@0
   534
slouken@1662
   535
        /* Query for the type of mouse protocol */
slouken@1668
   536
        if (write(fd, &query_ps2, sizeof(query_ps2)) == sizeof(query_ps2)) {
slouken@1662
   537
            Uint8 ch = 0;
slouken@0
   538
slouken@1662
   539
            /* Get the mouse protocol response */
slouken@1662
   540
            do {
slouken@1668
   541
                FD_ZERO(&fdset);
slouken@1668
   542
                FD_SET(fd, &fdset);
slouken@1662
   543
                tv.tv_sec = 1;
slouken@1662
   544
                tv.tv_usec = 0;
slouken@1668
   545
                if (select(fd + 1, &fdset, 0, 0, &tv) < 1) {
slouken@1662
   546
                    break;
slouken@1662
   547
                }
slouken@1662
   548
            }
slouken@1668
   549
            while ((read(fd, &ch, sizeof(ch)) == sizeof(ch)) &&
slouken@1662
   550
                   ((ch == 0xFA) || (ch == 0xAA)));
slouken@0
   551
slouken@1662
   552
            /* Experimental values (Logitech wheelmouse) */
slouken@0
   553
#ifdef DEBUG_MOUSE
slouken@1668
   554
            fprintf(stderr, "Last mouse mode: 0x%x\n", ch);
slouken@0
   555
#endif
slouken@1662
   556
            if ((ch == 3) || (ch == 4)) {
slouken@1662
   557
                imps2 = 1;
slouken@1662
   558
            }
slouken@1662
   559
        }
slouken@1662
   560
    }
slouken@1662
   561
    return imps2;
slouken@0
   562
}
slouken@0
   563
slouken@1662
   564
int
slouken@1668
   565
FB_OpenMouse(_THIS)
slouken@0
   566
{
slouken@1662
   567
    int i;
slouken@1662
   568
    const char *mousedev;
slouken@1662
   569
    const char *mousedrv;
slouken@0
   570
slouken@1668
   571
    mousedrv = SDL_getenv("SDL_MOUSEDRV");
slouken@1668
   572
    mousedev = SDL_getenv("SDL_MOUSEDEV");
slouken@1662
   573
    mouse_fd = -1;
slouken@0
   574
slouken@1361
   575
#if SDL_INPUT_TSLIB
slouken@1668
   576
    if (mousedrv && (SDL_strcmp(mousedrv, "TSLIB") == 0)) {
slouken@1662
   577
        if (mousedev == NULL)
slouken@1668
   578
            mousedev = SDL_getenv("TSLIB_TSDEVICE");
slouken@1662
   579
        if (mousedev != NULL) {
slouken@1668
   580
            ts_dev = ts_open(mousedev, 1);
slouken@1668
   581
            if ((ts_dev != NULL) && (ts_config(ts_dev) >= 0)) {
icculus@1201
   582
#ifdef DEBUG_MOUSE
slouken@1668
   583
                fprintf(stderr, "Using tslib touchscreen\n");
icculus@1201
   584
#endif
slouken@1662
   585
                mouse_drv = MOUSE_TSLIB;
slouken@1668
   586
                mouse_fd = ts_fd(ts_dev);
slouken@1662
   587
                return mouse_fd;
slouken@1662
   588
            }
slouken@1662
   589
        }
slouken@1662
   590
        mouse_drv = MOUSE_NONE;
slouken@1662
   591
        return mouse_fd;
slouken@1662
   592
    }
slouken@1361
   593
#endif /* SDL_INPUT_TSLIB */
icculus@1201
   594
slouken@1662
   595
    /* ELO TOUCHSCREEN SUPPORT */
slouken@0
   596
slouken@1668
   597
    if (mousedrv && (SDL_strcmp(mousedrv, "ELO") == 0)) {
slouken@1668
   598
        mouse_fd = open(mousedev, O_RDWR);
slouken@1662
   599
        if (mouse_fd >= 0) {
slouken@1668
   600
            if (eloInitController(mouse_fd)) {
slouken@0
   601
#ifdef DEBUG_MOUSE
slouken@1668
   602
                fprintf(stderr, "Using ELO touchscreen\n");
slouken@0
   603
#endif
slouken@1662
   604
                mouse_drv = MOUSE_ELO;
slouken@1662
   605
            }
slouken@0
   606
slouken@1662
   607
        } else if (mouse_fd < 0) {
slouken@1662
   608
            mouse_drv = MOUSE_NONE;
slouken@1662
   609
        }
slouken@0
   610
slouken@1662
   611
        return (mouse_fd);
slouken@1662
   612
    }
slouken@0
   613
slouken@1662
   614
    /* STD MICE */
slouken@0
   615
slouken@1662
   616
    if (mousedev == NULL) {
slouken@1662
   617
        /* FIXME someday... allow multiple mice in this driver */
slouken@1662
   618
        static const char *ps2mice[] = {
slouken@1662
   619
            "/dev/input/mice", "/dev/usbmouse", "/dev/psaux", NULL
slouken@1662
   620
        };
slouken@1662
   621
        /* First try to use GPM in repeater mode */
slouken@1662
   622
        if (mouse_fd < 0) {
slouken@1662
   623
            char proto[10];
slouken@1668
   624
            if (gpm_available(proto, SDL_arraysize(proto))) {
slouken@1668
   625
                mouse_fd = open(GPM_NODE_FIFO, O_RDONLY, 0);
slouken@1662
   626
                if (mouse_fd >= 0) {
slouken@1668
   627
                    if (SDL_strcmp(proto, "msc") == 0) {
slouken@1662
   628
                        mouse_drv = MOUSE_MSC;
slouken@1668
   629
                    } else if (SDL_strcmp(proto, "ps2") == 0) {
slouken@1662
   630
                        mouse_drv = MOUSE_PS2;
slouken@1668
   631
                    } else if (SDL_strcmp(proto, "imps2") == 0) {
slouken@1662
   632
                        mouse_drv = MOUSE_IMPS2;
slouken@1668
   633
                    } else if (SDL_strcmp(proto, "ms") == 0 ||
slouken@1668
   634
                               SDL_strcmp(proto, "bare") == 0) {
slouken@1662
   635
                        mouse_drv = MOUSE_MS;
slouken@1668
   636
                    } else if (SDL_strcmp(proto, "bm") == 0) {
slouken@1662
   637
                        mouse_drv = MOUSE_BM;
slouken@1662
   638
                    } else {
slouken@1662
   639
                        /* Unknown protocol... */
slouken@0
   640
#ifdef DEBUG_MOUSE
slouken@1668
   641
                        fprintf(stderr,
slouken@1668
   642
                                "GPM mouse using unknown protocol = %s\n",
slouken@1668
   643
                                proto);
slouken@0
   644
#endif
slouken@1668
   645
                        close(mouse_fd);
slouken@1662
   646
                        mouse_fd = -1;
slouken@1662
   647
                    }
slouken@1662
   648
                }
slouken@1659
   649
#ifdef DEBUG_MOUSE
slouken@1662
   650
                if (mouse_fd >= 0) {
slouken@1668
   651
                    fprintf(stderr,
slouken@1668
   652
                            "Using GPM mouse, protocol = %s\n", proto);
slouken@1662
   653
                }
slouken@1659
   654
#endif /* DEBUG_MOUSE */
slouken@1662
   655
            }
slouken@1662
   656
        }
slouken@1662
   657
        /* Now try to use a modern PS/2 mouse */
slouken@1662
   658
        for (i = 0; (mouse_fd < 0) && ps2mice[i]; ++i) {
slouken@1668
   659
            mouse_fd = open(ps2mice[i], O_RDWR, 0);
slouken@1662
   660
            if (mouse_fd < 0) {
slouken@1668
   661
                mouse_fd = open(ps2mice[i], O_RDONLY, 0);
slouken@1662
   662
            }
slouken@1662
   663
            if (mouse_fd >= 0) {
slouken@1662
   664
                /* rcg06112001 Attempt to set IMPS/2 mode */
slouken@1668
   665
                set_imps2_mode(mouse_fd);
slouken@1668
   666
                if (detect_imps2(mouse_fd)) {
slouken@0
   667
#ifdef DEBUG_MOUSE
slouken@1668
   668
                    fprintf(stderr, "Using IMPS2 mouse\n");
slouken@0
   669
#endif
slouken@1662
   670
                    mouse_drv = MOUSE_IMPS2;
slouken@1662
   671
                } else {
slouken@0
   672
#ifdef DEBUG_MOUSE
slouken@1668
   673
                    fprintf(stderr, "Using PS2 mouse\n");
slouken@0
   674
#endif
slouken@1662
   675
                    mouse_drv = MOUSE_PS2;
slouken@1662
   676
                }
slouken@1662
   677
            }
slouken@1662
   678
        }
slouken@1662
   679
        /* Next try to use a PPC ADB port mouse */
slouken@1662
   680
        if (mouse_fd < 0) {
slouken@1668
   681
            mouse_fd = open("/dev/adbmouse", O_RDONLY, 0);
slouken@1662
   682
            if (mouse_fd >= 0) {
slouken@0
   683
#ifdef DEBUG_MOUSE
slouken@1668
   684
                fprintf(stderr, "Using ADB mouse\n");
slouken@0
   685
#endif
slouken@1662
   686
                mouse_drv = MOUSE_BM;
slouken@1662
   687
            }
slouken@1662
   688
        }
slouken@1662
   689
    }
slouken@1662
   690
    /* Default to a serial Microsoft mouse */
slouken@1662
   691
    if (mouse_fd < 0) {
slouken@1662
   692
        if (mousedev == NULL) {
slouken@1662
   693
            mousedev = "/dev/mouse";
slouken@1662
   694
        }
slouken@1668
   695
        mouse_fd = open(mousedev, O_RDONLY, 0);
slouken@1662
   696
        if (mouse_fd >= 0) {
slouken@1662
   697
            struct termios mouse_termios;
slouken@0
   698
slouken@1662
   699
            /* Set the sampling speed to 1200 baud */
slouken@1668
   700
            tcgetattr(mouse_fd, &mouse_termios);
slouken@1662
   701
            mouse_termios.c_iflag = IGNBRK | IGNPAR;
slouken@1662
   702
            mouse_termios.c_oflag = 0;
slouken@1662
   703
            mouse_termios.c_lflag = 0;
slouken@1662
   704
            mouse_termios.c_line = 0;
slouken@1662
   705
            mouse_termios.c_cc[VTIME] = 0;
slouken@1662
   706
            mouse_termios.c_cc[VMIN] = 1;
slouken@1662
   707
            mouse_termios.c_cflag = CREAD | CLOCAL | HUPCL;
slouken@1662
   708
            mouse_termios.c_cflag |= CS8;
slouken@1662
   709
            mouse_termios.c_cflag |= B1200;
slouken@1668
   710
            tcsetattr(mouse_fd, TCSAFLUSH, &mouse_termios);
slouken@1668
   711
            if (mousedrv && (SDL_strcmp(mousedrv, "PS2") == 0)) {
slouken@0
   712
#ifdef DEBUG_MOUSE
slouken@1668
   713
                fprintf(stderr,
slouken@1668
   714
                        "Using (user specified) PS2 mouse on %s\n", mousedev);
slouken@0
   715
#endif
slouken@1662
   716
                mouse_drv = MOUSE_PS2;
slouken@1662
   717
            } else {
slouken@1585
   718
#ifdef DEBUG_MOUSE
slouken@1668
   719
                fprintf(stderr, "Using (default) MS mouse on %s\n", mousedev);
slouken@1585
   720
#endif
slouken@1662
   721
                mouse_drv = MOUSE_MS;
slouken@1662
   722
            }
slouken@1662
   723
        }
slouken@1662
   724
    }
slouken@1662
   725
    if (mouse_fd < 0) {
slouken@1662
   726
        mouse_drv = MOUSE_NONE;
slouken@1662
   727
    }
slouken@1662
   728
    return (mouse_fd);
slouken@0
   729
}
slouken@0
   730
slouken@0
   731
static int posted = 0;
slouken@0
   732
slouken@1662
   733
void
slouken@1668
   734
FB_vgamousecallback(int button, int relative, int dx, int dy)
slouken@0
   735
{
slouken@1662
   736
    int button_1, button_3;
slouken@1662
   737
    int button_state;
slouken@1662
   738
    int state_changed;
slouken@1662
   739
    int i;
slouken@1662
   740
    Uint8 state;
slouken@0
   741
slouken@1662
   742
    if (dx || dy) {
slouken@1668
   743
        posted += SDL_PrivateMouseMotion(0, relative, dx, dy);
slouken@1662
   744
    }
slouken@0
   745
slouken@1662
   746
    /* Swap button 1 and 3 */
slouken@1662
   747
    button_1 = (button & 0x04) >> 2;
slouken@1662
   748
    button_3 = (button & 0x01) << 2;
slouken@1662
   749
    button &= ~0x05;
slouken@1662
   750
    button |= (button_1 | button_3);
slouken@0
   751
slouken@1662
   752
    /* See what changed */
slouken@1668
   753
    button_state = SDL_GetMouseState(NULL, NULL);
slouken@1662
   754
    state_changed = button_state ^ button;
slouken@1662
   755
    for (i = 0; i < 8; ++i) {
slouken@1662
   756
        if (state_changed & (1 << i)) {
slouken@1662
   757
            if (button & (1 << i)) {
slouken@1662
   758
                state = SDL_PRESSED;
slouken@1662
   759
            } else {
slouken@1662
   760
                state = SDL_RELEASED;
slouken@1662
   761
            }
slouken@1668
   762
            posted += SDL_PrivateMouseButton(state, i + 1, 0, 0);
slouken@1662
   763
        }
slouken@1662
   764
    }
slouken@0
   765
}
slouken@0
   766
icculus@1201
   767
/* Handle input from tslib */
slouken@1361
   768
#if SDL_INPUT_TSLIB
slouken@1662
   769
static void
slouken@1668
   770
handle_tslib(_THIS)
icculus@1201
   771
{
slouken@1662
   772
    struct ts_sample sample;
slouken@1662
   773
    int button;
icculus@1201
   774
slouken@1668
   775
    while (ts_read(ts_dev, &sample, 1) > 0) {
slouken@1662
   776
        button = (sample.pressure > 0) ? 1 : 0;
slouken@1662
   777
        button <<= 2;           /* must report it as button 3 */
slouken@1668
   778
        FB_vgamousecallback(button, 0, sample.x, sample.y);
slouken@1662
   779
    }
slouken@1662
   780
    return;
icculus@1201
   781
}
slouken@1361
   782
#endif /* SDL_INPUT_TSLIB */
icculus@1201
   783
slouken@109
   784
/* For now, use MSC, PS/2, and MS protocols
slouken@0
   785
   Driver adapted from the SVGAlib mouse driver code (taken from gpm, etc.)
slouken@0
   786
 */
slouken@1662
   787
static void
slouken@1668
   788
handle_mouse(_THIS)
slouken@0
   789
{
slouken@1662
   790
    static int start = 0;
slouken@1662
   791
    static unsigned char mousebuf[BUFSIZ];
slouken@1662
   792
    static int relative = 1;
slouken@0
   793
slouken@1662
   794
    int i, nread;
slouken@1662
   795
    int button = 0;
slouken@1662
   796
    int dx = 0, dy = 0;
slouken@1662
   797
    int packetsize = 0;
slouken@1662
   798
    int realx, realy;
slouken@1662
   799
slouken@1662
   800
    /* Figure out the mouse packet size */
slouken@1662
   801
    switch (mouse_drv) {
slouken@1662
   802
    case MOUSE_NONE:
slouken@1662
   803
        /* Ack! */
slouken@1668
   804
        read(mouse_fd, mousebuf, BUFSIZ);
slouken@1662
   805
        return;
slouken@1662
   806
    case MOUSE_MSC:
slouken@1662
   807
        packetsize = 5;
slouken@1662
   808
        break;
slouken@1662
   809
    case MOUSE_IMPS2:
slouken@1662
   810
        packetsize = 4;
slouken@1662
   811
        break;
slouken@1662
   812
    case MOUSE_PS2:
slouken@1662
   813
    case MOUSE_MS:
slouken@1662
   814
    case MOUSE_BM:
slouken@1662
   815
        packetsize = 3;
slouken@1662
   816
        break;
slouken@1662
   817
    case MOUSE_ELO:
slouken@1662
   818
        /* try to read the next packet */
slouken@1662
   819
        if (eloReadPosition
slouken@1662
   820
            (this, mouse_fd, &dx, &dy, &button, &realx, &realy)) {
slouken@1662
   821
            button = (button & 0x01) << 2;
slouken@1668
   822
            FB_vgamousecallback(button, 0, dx, dy);
slouken@1662
   823
        }
slouken@1662
   824
        return;                 /* nothing left to do */
slouken@1662
   825
    case MOUSE_TSLIB:
slouken@1361
   826
#if SDL_INPUT_TSLIB
slouken@1668
   827
        handle_tslib(this);
icculus@1201
   828
#endif
slouken@1662
   829
        return;                 /* nothing left to do */
slouken@1662
   830
    default:
slouken@1662
   831
        /* Uh oh.. */
slouken@1662
   832
        packetsize = 0;
slouken@1662
   833
        break;
slouken@1662
   834
    }
slouken@0
   835
slouken@1662
   836
    /* Special handling for the quite sensitive ELO controller */
slouken@1662
   837
    if (mouse_drv == MOUSE_ELO) {
slouken@1662
   838
slouken@1662
   839
    }
slouken@1662
   840
slouken@1662
   841
    /* Read as many packets as possible */
slouken@1668
   842
    nread = read(mouse_fd, &mousebuf[start], BUFSIZ - start);
slouken@1662
   843
    if (nread < 0) {
slouken@1662
   844
        return;
slouken@1662
   845
    }
slouken@1662
   846
    nread += start;
slouken@0
   847
#ifdef DEBUG_MOUSE
slouken@1668
   848
    fprintf(stderr, "Read %d bytes from mouse, start = %d\n", nread, start);
slouken@0
   849
#endif
slouken@1662
   850
    for (i = 0; i < (nread - (packetsize - 1)); i += packetsize) {
slouken@1662
   851
        switch (mouse_drv) {
slouken@1662
   852
        case MOUSE_NONE:
slouken@1662
   853
            break;
slouken@1662
   854
        case MOUSE_MSC:
slouken@1662
   855
            /* MSC protocol has 0x80 in high byte */
slouken@1662
   856
            if ((mousebuf[i] & 0xF8) != 0x80) {
slouken@1662
   857
                /* Go to next byte */
slouken@1662
   858
                i -= (packetsize - 1);
slouken@1662
   859
                continue;
slouken@1662
   860
            }
slouken@1662
   861
            /* Get current mouse state */
slouken@1662
   862
            button = (~mousebuf[i]) & 0x07;
slouken@1662
   863
            dx = (signed char) (mousebuf[i + 1]) +
slouken@1662
   864
                (signed char) (mousebuf[i + 3]);
slouken@1662
   865
            dy = -((signed char) (mousebuf[i + 2]) +
slouken@1662
   866
                   (signed char) (mousebuf[i + 4]));
slouken@1662
   867
            break;
slouken@1662
   868
        case MOUSE_PS2:
slouken@1662
   869
            /* PS/2 protocol has nothing in high byte */
slouken@1662
   870
            if ((mousebuf[i] & 0xC0) != 0) {
slouken@1662
   871
                /* Go to next byte */
slouken@1662
   872
                i -= (packetsize - 1);
slouken@1662
   873
                continue;
slouken@1662
   874
            }
slouken@1662
   875
            /* Get current mouse state */
slouken@1662
   876
            button = (mousebuf[i] & 0x04) >> 1 |        /*Middle */
slouken@1662
   877
                (mousebuf[i] & 0x02) >> 1 |     /*Right */
slouken@1662
   878
                (mousebuf[i] & 0x01) << 2;      /*Left */
slouken@1662
   879
            dx = (mousebuf[i] & 0x10) ?
slouken@1662
   880
                mousebuf[i + 1] - 256 : mousebuf[i + 1];
slouken@1662
   881
            dy = (mousebuf[i] & 0x20) ?
slouken@1662
   882
                -(mousebuf[i + 2] - 256) : -mousebuf[i + 2];
slouken@1662
   883
            break;
slouken@1662
   884
        case MOUSE_IMPS2:
slouken@1662
   885
            /* Get current mouse state */
slouken@1662
   886
            button = (mousebuf[i] & 0x04) >> 1 |        /*Middle */
slouken@1662
   887
                (mousebuf[i] & 0x02) >> 1 |     /*Right */
slouken@1662
   888
                (mousebuf[i] & 0x01) << 2 |     /*Left */
slouken@1662
   889
                (mousebuf[i] & 0x40) >> 3 |     /* 4 */
slouken@1662
   890
                (mousebuf[i] & 0x80) >> 3;      /* 5 */
slouken@1662
   891
            dx = (mousebuf[i] & 0x10) ?
slouken@1662
   892
                mousebuf[i + 1] - 256 : mousebuf[i + 1];
slouken@1662
   893
            dy = (mousebuf[i] & 0x20) ?
slouken@1662
   894
                -(mousebuf[i + 2] - 256) : -mousebuf[i + 2];
slouken@1662
   895
            switch (mousebuf[i + 3] & 0x0F) {
slouken@1662
   896
            case 0x0E:         /* DX = +1 */
slouken@1662
   897
            case 0x02:         /* DX = -1 */
slouken@1662
   898
                break;
slouken@1662
   899
            case 0x0F:         /* DY = +1 (map button 4) */
slouken@1668
   900
                FB_vgamousecallback(button | (1 << 3), 1, 0, 0);
slouken@1662
   901
                break;
slouken@1662
   902
            case 0x01:         /* DY = -1 (map button 5) */
slouken@1668
   903
                FB_vgamousecallback(button | (1 << 4), 1, 0, 0);
slouken@1662
   904
                break;
slouken@1662
   905
            }
slouken@1662
   906
            break;
slouken@1662
   907
        case MOUSE_MS:
slouken@1662
   908
            /* Microsoft protocol has 0x40 in high byte */
slouken@1662
   909
            if ((mousebuf[i] & 0x40) != 0x40) {
slouken@1662
   910
                /* Go to next byte */
slouken@1662
   911
                i -= (packetsize - 1);
slouken@1662
   912
                continue;
slouken@1662
   913
            }
slouken@1662
   914
            /* Get current mouse state */
slouken@1662
   915
            button = ((mousebuf[i] & 0x20) >> 3) |
slouken@1662
   916
                ((mousebuf[i] & 0x10) >> 4);
slouken@1662
   917
            dx = (signed char) (((mousebuf[i] & 0x03) << 6) |
slouken@1662
   918
                                (mousebuf[i + 1] & 0x3F));
slouken@1662
   919
            dy = (signed char) (((mousebuf[i] & 0x0C) << 4) |
slouken@1662
   920
                                (mousebuf[i + 2] & 0x3F));
slouken@1662
   921
            break;
slouken@1662
   922
        case MOUSE_BM:
slouken@1662
   923
            /* BusMouse protocol has 0xF8 in high byte */
slouken@1662
   924
            if ((mousebuf[i] & 0xF8) != 0x80) {
slouken@1662
   925
                /* Go to next byte */
slouken@1662
   926
                i -= (packetsize - 1);
slouken@1662
   927
                continue;
slouken@1662
   928
            }
slouken@1662
   929
            /* Get current mouse state */
slouken@1662
   930
            button = (~mousebuf[i]) & 0x07;
slouken@1662
   931
            dx = (signed char) mousebuf[i + 1];
slouken@1662
   932
            dy = -(signed char) mousebuf[i + 2];
slouken@1662
   933
            break;
slouken@1662
   934
        default:
slouken@1662
   935
            /* Uh oh.. */
slouken@1662
   936
            dx = 0;
slouken@1662
   937
            dy = 0;
slouken@1662
   938
            break;
slouken@1662
   939
        }
slouken@1668
   940
        FB_vgamousecallback(button, relative, dx, dy);
slouken@1662
   941
    }
slouken@1662
   942
    if (i < nread) {
slouken@1668
   943
        SDL_memcpy(mousebuf, &mousebuf[i], (nread - i));
slouken@1662
   944
        start = (nread - i);
slouken@1662
   945
    } else {
slouken@1662
   946
        start = 0;
slouken@1662
   947
    }
slouken@1662
   948
    return;
slouken@0
   949
}
slouken@0
   950
slouken@1659
   951
/* Handle switching to another VC, returns when our VC is back */
slouken@1662
   952
static void
slouken@1668
   953
switch_vt_prep(_THIS)
slouken@1659
   954
{
slouken@1662
   955
    SDL_Surface *screen = SDL_VideoSurface;
slouken@1659
   956
slouken@1668
   957
    SDL_PrivateAppActive(0,
slouken@1668
   958
                         (SDL_APPACTIVE | SDL_APPINPUTFOCUS |
slouken@1668
   959
                          SDL_APPMOUSEFOCUS));
slouken@1659
   960
slouken@1662
   961
    /* Save the contents of the screen, and go to text mode */
slouken@1668
   962
    wait_idle(this);
slouken@1662
   963
    screen_arealen = ((screen->h + (2 * this->offset_y)) * screen->pitch);
slouken@1668
   964
    screen_contents = (Uint8 *) SDL_malloc(screen_arealen);
slouken@1662
   965
    if (screen_contents) {
slouken@1668
   966
        SDL_memcpy(screen_contents, screen->pixels, screen_arealen);
slouken@1662
   967
    }
slouken@1668
   968
    FB_SavePaletteTo(this, 256, screen_palette);
slouken@1668
   969
    ioctl(console_fd, FBIOGET_VSCREENINFO, &screen_vinfo);
slouken@1668
   970
    ioctl(keyboard_fd, KDSETMODE, KD_TEXT);
slouken@1668
   971
    ioctl(keyboard_fd, VT_UNLOCKSWITCH, 1);
slouken@1659
   972
}
slouken@1662
   973
static void
slouken@1668
   974
switch_vt_done(_THIS)
slouken@1659
   975
{
slouken@1662
   976
    SDL_Surface *screen = SDL_VideoSurface;
slouken@1659
   977
slouken@1662
   978
    /* Restore graphics mode and the contents of the screen */
slouken@1668
   979
    ioctl(keyboard_fd, VT_LOCKSWITCH, 1);
slouken@1668
   980
    ioctl(keyboard_fd, KDSETMODE, KD_GRAPHICS);
slouken@1668
   981
    ioctl(console_fd, FBIOPUT_VSCREENINFO, &screen_vinfo);
slouken@1668
   982
    FB_RestorePaletteFrom(this, 256, screen_palette);
slouken@1662
   983
    if (screen_contents) {
slouken@1668
   984
        SDL_memcpy(screen->pixels, screen_contents, screen_arealen);
slouken@1668
   985
        SDL_free(screen_contents);
slouken@1662
   986
        screen_contents = NULL;
slouken@1662
   987
    }
slouken@1659
   988
slouken@1662
   989
    /* Get updates to the shadow surface while switched away */
slouken@1662
   990
    if (SDL_ShadowSurface) {
slouken@1668
   991
        SDL_UpdateRect(SDL_ShadowSurface, 0, 0, 0, 0);
slouken@1662
   992
    }
slouken@1659
   993
slouken@1668
   994
    SDL_PrivateAppActive(1,
slouken@1668
   995
                         (SDL_APPACTIVE | SDL_APPINPUTFOCUS |
slouken@1668
   996
                          SDL_APPMOUSEFOCUS));
slouken@1659
   997
}
slouken@1662
   998
static void
slouken@1668
   999
switch_vt(_THIS, unsigned short which)
slouken@0
  1000
{
slouken@1662
  1001
    struct vt_stat vtstate;
slouken@0
  1002
slouken@1662
  1003
    /* Figure out whether or not we're switching to a new console */
slouken@1668
  1004
    if ((ioctl(keyboard_fd, VT_GETSTATE, &vtstate) < 0) ||
slouken@1662
  1005
        (which == vtstate.v_active)) {
slouken@1662
  1006
        return;
slouken@1662
  1007
    }
slouken@0
  1008
slouken@1662
  1009
    /* New console, switch to it */
slouken@1668
  1010
    SDL_mutexP(hw_lock);
slouken@1668
  1011
    switch_vt_prep(this);
slouken@1668
  1012
    if (ioctl(keyboard_fd, VT_ACTIVATE, which) == 0) {
slouken@1668
  1013
        ioctl(keyboard_fd, VT_WAITACTIVE, which);
slouken@1662
  1014
        switched_away = 1;
slouken@1662
  1015
    } else {
slouken@1668
  1016
        switch_vt_done(this);
slouken@1662
  1017
    }
slouken@1668
  1018
    SDL_mutexV(hw_lock);
slouken@0
  1019
}
slouken@0
  1020
slouken@1662
  1021
static void
slouken@1668
  1022
handle_keyboard(_THIS)
slouken@0
  1023
{
slouken@1662
  1024
    unsigned char keybuf[BUFSIZ];
slouken@1662
  1025
    int i, nread;
slouken@1662
  1026
    int pressed;
slouken@1662
  1027
    int scancode;
slouken@1662
  1028
    SDL_keysym keysym;
slouken@0
  1029
slouken@1668
  1030
    nread = read(keyboard_fd, keybuf, BUFSIZ);
slouken@1662
  1031
    for (i = 0; i < nread; ++i) {
slouken@1662
  1032
        scancode = keybuf[i] & 0x7F;
slouken@1662
  1033
        if (keybuf[i] & 0x80) {
slouken@1662
  1034
            pressed = SDL_RELEASED;
slouken@1662
  1035
        } else {
slouken@1662
  1036
            pressed = SDL_PRESSED;
slouken@1662
  1037
        }
slouken@1668
  1038
        TranslateKey(scancode, &keysym);
slouken@1662
  1039
        /* Handle Ctrl-Alt-FN for vt switch */
slouken@1662
  1040
        switch (keysym.sym) {
slouken@1662
  1041
        case SDLK_F1:
slouken@1662
  1042
        case SDLK_F2:
slouken@1662
  1043
        case SDLK_F3:
slouken@1662
  1044
        case SDLK_F4:
slouken@1662
  1045
        case SDLK_F5:
slouken@1662
  1046
        case SDLK_F6:
slouken@1662
  1047
        case SDLK_F7:
slouken@1662
  1048
        case SDLK_F8:
slouken@1662
  1049
        case SDLK_F9:
slouken@1662
  1050
        case SDLK_F10:
slouken@1662
  1051
        case SDLK_F11:
slouken@1662
  1052
        case SDLK_F12:
slouken@1668
  1053
            if ((SDL_GetModState() & KMOD_CTRL) &&
slouken@1668
  1054
                (SDL_GetModState() & KMOD_ALT)) {
slouken@1662
  1055
                if (pressed) {
slouken@1668
  1056
                    switch_vt(this, (keysym.sym - SDLK_F1) + 1);
slouken@1662
  1057
                }
slouken@1662
  1058
                break;
slouken@1662
  1059
            }
slouken@1662
  1060
            /* Fall through to normal processing */
slouken@1662
  1061
        default:
slouken@1668
  1062
            posted += SDL_PrivateKeyboard(pressed, &keysym);
slouken@1662
  1063
            break;
slouken@1662
  1064
        }
slouken@1662
  1065
    }
slouken@0
  1066
}
slouken@0
  1067
slouken@1662
  1068
void
slouken@1668
  1069
FB_PumpEvents(_THIS)
slouken@0
  1070
{
slouken@1662
  1071
    fd_set fdset;
slouken@1662
  1072
    int max_fd;
slouken@1662
  1073
    static struct timeval zero;
slouken@0
  1074
slouken@1662
  1075
    do {
slouken@1662
  1076
        if (switched_away) {
slouken@1662
  1077
            struct vt_stat vtstate;
slouken@1659
  1078
slouken@1668
  1079
            SDL_mutexP(hw_lock);
slouken@1668
  1080
            if ((ioctl(keyboard_fd, VT_GETSTATE, &vtstate) == 0) &&
slouken@1662
  1081
                vtstate.v_active == current_vt) {
slouken@1662
  1082
                switched_away = 0;
slouken@1668
  1083
                switch_vt_done(this);
slouken@1662
  1084
            }
slouken@1668
  1085
            SDL_mutexV(hw_lock);
slouken@1662
  1086
        }
slouken@1659
  1087
slouken@1662
  1088
        posted = 0;
slouken@0
  1089
slouken@1668
  1090
        FD_ZERO(&fdset);
slouken@1662
  1091
        max_fd = 0;
slouken@1662
  1092
        if (keyboard_fd >= 0) {
slouken@1668
  1093
            FD_SET(keyboard_fd, &fdset);
slouken@1662
  1094
            if (max_fd < keyboard_fd) {
slouken@1662
  1095
                max_fd = keyboard_fd;
slouken@1662
  1096
            }
slouken@1662
  1097
        }
slouken@1662
  1098
        if (mouse_fd >= 0) {
slouken@1668
  1099
            FD_SET(mouse_fd, &fdset);
slouken@1662
  1100
            if (max_fd < mouse_fd) {
slouken@1662
  1101
                max_fd = mouse_fd;
slouken@1662
  1102
            }
slouken@1662
  1103
        }
slouken@1668
  1104
        if (select(max_fd + 1, &fdset, NULL, NULL, &zero) > 0) {
slouken@1662
  1105
            if (keyboard_fd >= 0) {
slouken@1668
  1106
                if (FD_ISSET(keyboard_fd, &fdset)) {
slouken@1668
  1107
                    handle_keyboard(this);
slouken@1662
  1108
                }
slouken@1662
  1109
            }
slouken@1662
  1110
            if (mouse_fd >= 0) {
slouken@1668
  1111
                if (FD_ISSET(mouse_fd, &fdset)) {
slouken@1668
  1112
                    handle_mouse(this);
slouken@1662
  1113
                }
slouken@1662
  1114
            }
slouken@1662
  1115
        }
slouken@1662
  1116
    }
slouken@1662
  1117
    while (posted);
slouken@0
  1118
}
slouken@0
  1119
slouken@1662
  1120
void
slouken@1668
  1121
FB_InitOSKeymap(_THIS)
slouken@0
  1122
{
slouken@1662
  1123
    int i;
slouken@0
  1124
slouken@1662
  1125
    /* Initialize the Linux key translation table */
slouken@0
  1126
slouken@1662
  1127
    /* First get the ascii keys and others not well handled */
slouken@1668
  1128
    for (i = 0; i < SDL_arraysize(keymap); ++i) {
slouken@1662
  1129
        switch (i) {
slouken@1662
  1130
            /* These aren't handled by the x86 kernel keymapping (?) */
slouken@1662
  1131
        case SCANCODE_PRINTSCREEN:
slouken@1662
  1132
            keymap[i] = SDLK_PRINT;
slouken@1662
  1133
            break;
slouken@1662
  1134
        case SCANCODE_BREAK:
slouken@1662
  1135
            keymap[i] = SDLK_BREAK;
slouken@1662
  1136
            break;
slouken@1662
  1137
        case SCANCODE_BREAK_ALTERNATIVE:
slouken@1662
  1138
            keymap[i] = SDLK_PAUSE;
slouken@1662
  1139
            break;
slouken@1662
  1140
        case SCANCODE_LEFTSHIFT:
slouken@1662
  1141
            keymap[i] = SDLK_LSHIFT;
slouken@1662
  1142
            break;
slouken@1662
  1143
        case SCANCODE_RIGHTSHIFT:
slouken@1662
  1144
            keymap[i] = SDLK_RSHIFT;
slouken@1662
  1145
            break;
slouken@1662
  1146
        case SCANCODE_LEFTCONTROL:
slouken@1662
  1147
            keymap[i] = SDLK_LCTRL;
slouken@1662
  1148
            break;
slouken@1662
  1149
        case SCANCODE_RIGHTCONTROL:
slouken@1662
  1150
            keymap[i] = SDLK_RCTRL;
slouken@1662
  1151
            break;
slouken@1662
  1152
        case SCANCODE_RIGHTWIN:
slouken@1662
  1153
            keymap[i] = SDLK_RSUPER;
slouken@1662
  1154
            break;
slouken@1662
  1155
        case SCANCODE_LEFTWIN:
slouken@1662
  1156
            keymap[i] = SDLK_LSUPER;
slouken@1662
  1157
            break;
slouken@1662
  1158
        case SCANCODE_LEFTALT:
slouken@1662
  1159
            keymap[i] = SDLK_LALT;
slouken@1662
  1160
            break;
slouken@1662
  1161
        case SCANCODE_RIGHTALT:
slouken@1662
  1162
            keymap[i] = SDLK_RALT;
slouken@1662
  1163
            break;
slouken@1662
  1164
        case 127:
slouken@1662
  1165
            keymap[i] = SDLK_MENU;
slouken@1662
  1166
            break;
slouken@1662
  1167
            /* this should take care of all standard ascii keys */
slouken@1662
  1168
        default:
slouken@1668
  1169
            keymap[i] = KVAL(vga_keymap[0][i]);
slouken@1662
  1170
            break;
slouken@1662
  1171
        }
slouken@1662
  1172
    }
slouken@1668
  1173
    for (i = 0; i < SDL_arraysize(keymap); ++i) {
slouken@1662
  1174
        switch (keymap_temp[i]) {
slouken@1662
  1175
        case K_F1:
slouken@1662
  1176
            keymap[i] = SDLK_F1;
slouken@1662
  1177
            break;
slouken@1662
  1178
        case K_F2:
slouken@1662
  1179
            keymap[i] = SDLK_F2;
slouken@1662
  1180
            break;
slouken@1662
  1181
        case K_F3:
slouken@1662
  1182
            keymap[i] = SDLK_F3;
slouken@1662
  1183
            break;
slouken@1662
  1184
        case K_F4:
slouken@1662
  1185
            keymap[i] = SDLK_F4;
slouken@1662
  1186
            break;
slouken@1662
  1187
        case K_F5:
slouken@1662
  1188
            keymap[i] = SDLK_F5;
slouken@1662
  1189
            break;
slouken@1662
  1190
        case K_F6:
slouken@1662
  1191
            keymap[i] = SDLK_F6;
slouken@1662
  1192
            break;
slouken@1662
  1193
        case K_F7:
slouken@1662
  1194
            keymap[i] = SDLK_F7;
slouken@1662
  1195
            break;
slouken@1662
  1196
        case K_F8:
slouken@1662
  1197
            keymap[i] = SDLK_F8;
slouken@1662
  1198
            break;
slouken@1662
  1199
        case K_F9:
slouken@1662
  1200
            keymap[i] = SDLK_F9;
slouken@1662
  1201
            break;
slouken@1662
  1202
        case K_F10:
slouken@1662
  1203
            keymap[i] = SDLK_F10;
slouken@1662
  1204
            break;
slouken@1662
  1205
        case K_F11:
slouken@1662
  1206
            keymap[i] = SDLK_F11;
slouken@1662
  1207
            break;
slouken@1662
  1208
        case K_F12:
slouken@1662
  1209
            keymap[i] = SDLK_F12;
slouken@1662
  1210
            break;
slouken@0
  1211
slouken@1662
  1212
        case K_DOWN:
slouken@1662
  1213
            keymap[i] = SDLK_DOWN;
slouken@1662
  1214
            break;
slouken@1662
  1215
        case K_LEFT:
slouken@1662
  1216
            keymap[i] = SDLK_LEFT;
slouken@1662
  1217
            break;
slouken@1662
  1218
        case K_RIGHT:
slouken@1662
  1219
            keymap[i] = SDLK_RIGHT;
slouken@1662
  1220
            break;
slouken@1662
  1221
        case K_UP:
slouken@1662
  1222
            keymap[i] = SDLK_UP;
slouken@1662
  1223
            break;
slouken@0
  1224
slouken@1662
  1225
        case K_P0:
slouken@1662
  1226
            keymap[i] = SDLK_KP0;
slouken@1662
  1227
            break;
slouken@1662
  1228
        case K_P1:
slouken@1662
  1229
            keymap[i] = SDLK_KP1;
slouken@1662
  1230
            break;
slouken@1662
  1231
        case K_P2:
slouken@1662
  1232
            keymap[i] = SDLK_KP2;
slouken@1662
  1233
            break;
slouken@1662
  1234
        case K_P3:
slouken@1662
  1235
            keymap[i] = SDLK_KP3;
slouken@1662
  1236
            break;
slouken@1662
  1237
        case K_P4:
slouken@1662
  1238
            keymap[i] = SDLK_KP4;
slouken@1662
  1239
            break;
slouken@1662
  1240
        case K_P5:
slouken@1662
  1241
            keymap[i] = SDLK_KP5;
slouken@1662
  1242
            break;
slouken@1662
  1243
        case K_P6:
slouken@1662
  1244
            keymap[i] = SDLK_KP6;
slouken@1662
  1245
            break;
slouken@1662
  1246
        case K_P7:
slouken@1662
  1247
            keymap[i] = SDLK_KP7;
slouken@1662
  1248
            break;
slouken@1662
  1249
        case K_P8:
slouken@1662
  1250
            keymap[i] = SDLK_KP8;
slouken@1662
  1251
            break;
slouken@1662
  1252
        case K_P9:
slouken@1662
  1253
            keymap[i] = SDLK_KP9;
slouken@1662
  1254
            break;
slouken@1662
  1255
        case K_PPLUS:
slouken@1662
  1256
            keymap[i] = SDLK_KP_PLUS;
slouken@1662
  1257
            break;
slouken@1662
  1258
        case K_PMINUS:
slouken@1662
  1259
            keymap[i] = SDLK_KP_MINUS;
slouken@1662
  1260
            break;
slouken@1662
  1261
        case K_PSTAR:
slouken@1662
  1262
            keymap[i] = SDLK_KP_MULTIPLY;
slouken@1662
  1263
            break;
slouken@1662
  1264
        case K_PSLASH:
slouken@1662
  1265
            keymap[i] = SDLK_KP_DIVIDE;
slouken@1662
  1266
            break;
slouken@1662
  1267
        case K_PENTER:
slouken@1662
  1268
            keymap[i] = SDLK_KP_ENTER;
slouken@1662
  1269
            break;
slouken@1662
  1270
        case K_PDOT:
slouken@1662
  1271
            keymap[i] = SDLK_KP_PERIOD;
slouken@1662
  1272
            break;
slouken@0
  1273
slouken@1662
  1274
        case K_SHIFT:
slouken@1662
  1275
            if (keymap[i] != SDLK_RSHIFT)
slouken@1662
  1276
                keymap[i] = SDLK_LSHIFT;
slouken@1662
  1277
            break;
slouken@1662
  1278
        case K_SHIFTL:
slouken@1662
  1279
            keymap[i] = SDLK_LSHIFT;
slouken@1662
  1280
            break;
slouken@1662
  1281
        case K_SHIFTR:
slouken@1662
  1282
            keymap[i] = SDLK_RSHIFT;
slouken@1662
  1283
            break;
slouken@1662
  1284
        case K_CTRL:
slouken@1662
  1285
            if (keymap[i] != SDLK_RCTRL)
slouken@1662
  1286
                keymap[i] = SDLK_LCTRL;
slouken@1662
  1287
            break;
slouken@1662
  1288
        case K_CTRLL:
slouken@1662
  1289
            keymap[i] = SDLK_LCTRL;
slouken@1662
  1290
            break;
slouken@1662
  1291
        case K_CTRLR:
slouken@1662
  1292
            keymap[i] = SDLK_RCTRL;
slouken@1662
  1293
            break;
slouken@1662
  1294
        case K_ALT:
slouken@1662
  1295
            keymap[i] = SDLK_LALT;
slouken@1662
  1296
            break;
slouken@1662
  1297
        case K_ALTGR:
slouken@1662
  1298
            keymap[i] = SDLK_RALT;
slouken@1662
  1299
            break;
slouken@0
  1300
slouken@1662
  1301
        case K_INSERT:
slouken@1662
  1302
            keymap[i] = SDLK_INSERT;
slouken@1662
  1303
            break;
slouken@1662
  1304
        case K_REMOVE:
slouken@1662
  1305
            keymap[i] = SDLK_DELETE;
slouken@1662
  1306
            break;
slouken@1662
  1307
        case K_PGUP:
slouken@1662
  1308
            keymap[i] = SDLK_PAGEUP;
slouken@1662
  1309
            break;
slouken@1662
  1310
        case K_PGDN:
slouken@1662
  1311
            keymap[i] = SDLK_PAGEDOWN;
slouken@1662
  1312
            break;
slouken@1662
  1313
        case K_FIND:
slouken@1662
  1314
            keymap[i] = SDLK_HOME;
slouken@1662
  1315
            break;
slouken@1662
  1316
        case K_SELECT:
slouken@1662
  1317
            keymap[i] = SDLK_END;
slouken@1662
  1318
            break;
slouken@0
  1319
slouken@1662
  1320
        case K_NUM:
slouken@1662
  1321
            keymap[i] = SDLK_NUMLOCK;
slouken@1662
  1322
            break;
slouken@1662
  1323
        case K_CAPS:
slouken@1662
  1324
            keymap[i] = SDLK_CAPSLOCK;
slouken@1662
  1325
            break;
slouken@0
  1326
slouken@1662
  1327
        case K_F13:
slouken@1662
  1328
            keymap[i] = SDLK_PRINT;
slouken@1662
  1329
            break;
slouken@1662
  1330
        case K_HOLD:
slouken@1662
  1331
            keymap[i] = SDLK_SCROLLOCK;
slouken@1662
  1332
            break;
slouken@1662
  1333
        case K_PAUSE:
slouken@1662
  1334
            keymap[i] = SDLK_PAUSE;
slouken@1662
  1335
            break;
slouken@0
  1336
slouken@1662
  1337
        case 127:
slouken@1662
  1338
            keymap[i] = SDLK_BACKSPACE;
slouken@1662
  1339
            break;
slouken@1662
  1340
slouken@1662
  1341
        default:
slouken@1662
  1342
            break;
slouken@1662
  1343
        }
slouken@1662
  1344
    }
slouken@0
  1345
}
slouken@0
  1346
slouken@1662
  1347
static SDL_keysym *
slouken@1668
  1348
TranslateKey(int scancode, SDL_keysym * keysym)
slouken@0
  1349
{
slouken@1662
  1350
    /* Set the keysym information */
slouken@1662
  1351
    keysym->scancode = scancode;
slouken@1662
  1352
    keysym->sym = keymap[scancode];
slouken@1662
  1353
    keysym->mod = KMOD_NONE;
slouken@0
  1354
slouken@1662
  1355
    /* If UNICODE is on, get the UNICODE value for the key */
slouken@1662
  1356
    keysym->unicode = 0;
slouken@1662
  1357
    if (SDL_TranslateUNICODE) {
slouken@1662
  1358
        int map;
slouken@1662
  1359
        SDLMod modstate;
slouken@0
  1360
slouken@1668
  1361
        modstate = SDL_GetModState();
slouken@1662
  1362
        map = 0;
slouken@1662
  1363
        if (modstate & KMOD_SHIFT) {
slouken@1662
  1364
            map |= (1 << KG_SHIFT);
slouken@1662
  1365
        }
slouken@1662
  1366
        if (modstate & KMOD_CTRL) {
slouken@1662
  1367
            map |= (1 << KG_CTRL);
slouken@1662
  1368
        }
slouken@1662
  1369
        if (modstate & KMOD_LALT) {
slouken@1662
  1370
            map |= (1 << KG_ALT);
slouken@1662
  1371
        }
slouken@1662
  1372
        if (modstate & KMOD_RALT) {
slouken@1662
  1373
            map |= (1 << KG_ALTGR);
slouken@1662
  1374
        }
slouken@1668
  1375
        if (KTYP(vga_keymap[map][scancode]) == KT_LETTER) {
slouken@1662
  1376
            if (modstate & KMOD_CAPS) {
slouken@1662
  1377
                map ^= (1 << KG_SHIFT);
slouken@1662
  1378
            }
slouken@1662
  1379
        }
slouken@1668
  1380
        if (KTYP(vga_keymap[map][scancode]) == KT_PAD) {
slouken@1662
  1381
            if (modstate & KMOD_NUM) {
slouken@1668
  1382
                keysym->unicode = KVAL(vga_keymap[map][scancode]);
slouken@1662
  1383
            }
slouken@1662
  1384
        } else {
slouken@1668
  1385
            keysym->unicode = KVAL(vga_keymap[map][scancode]);
slouken@1662
  1386
        }
slouken@1662
  1387
    }
slouken@1662
  1388
    return (keysym);
slouken@0
  1389
}
slouken@1662
  1390
slouken@1662
  1391
/* vi: set ts=4 sw=4 expandtab: */