src/video/fbcon/SDL_fbevents.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1659 14717b52abc0
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    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@1662
    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@1662
    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@1662
    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@1662
    97
                    entry.kb_value = K (KT_ASCII, 13);
slouken@1662
    98
                }
slouken@1662
    99
                /* Handle numpad specially as well */
slouken@1662
   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@1662
   116
                        vga_keymap[map][i] = K (KT_ASCII, '+');
slouken@1662
   117
                        break;
slouken@1662
   118
                    case K_PMINUS:
slouken@1662
   119
                        vga_keymap[map][i] = K (KT_ASCII, '-');
slouken@1662
   120
                        break;
slouken@1662
   121
                    case K_PSTAR:
slouken@1662
   122
                        vga_keymap[map][i] = K (KT_ASCII, '*');
slouken@1662
   123
                        break;
slouken@1662
   124
                    case K_PSLASH:
slouken@1662
   125
                        vga_keymap[map][i] = K (KT_ASCII, '/');
slouken@1662
   126
                        break;
slouken@1662
   127
                    case K_PENTER:
slouken@1662
   128
                        vga_keymap[map][i] = K (KT_ASCII, '\r');
slouken@1662
   129
                        break;
slouken@1662
   130
                    case K_PCOMMA:
slouken@1662
   131
                        vga_keymap[map][i] = K (KT_ASCII, ',');
slouken@1662
   132
                        break;
slouken@1662
   133
                    case K_PDOT:
slouken@1662
   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@1662
   141
                if ((KTYP (entry.kb_value) == KT_LATIN) ||
slouken@1662
   142
                    (KTYP (entry.kb_value) == KT_ASCII) ||
slouken@1662
   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@1662
   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@1662
   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@1662
   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@1662
   169
            if (ioctl (keyboard_fd, VT_GETSTATE, &vtstate) == 0) {
slouken@1662
   170
                saved_vt = vtstate.v_active;
slouken@1662
   171
            }
slouken@1662
   172
            if (ioctl (keyboard_fd, VT_ACTIVATE, current_vt) == 0) {
slouken@1662
   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@1662
   178
        if (tcgetattr (keyboard_fd, &saved_kbd_termios) < 0) {
slouken@1662
   179
            SDL_SetError ("Unable to get terminal attributes");
slouken@1662
   180
            if (keyboard_fd > 0) {
slouken@1662
   181
                close (keyboard_fd);
slouken@1662
   182
            }
slouken@1662
   183
            keyboard_fd = -1;
slouken@1662
   184
            return (-1);
slouken@1662
   185
        }
slouken@1662
   186
        if (ioctl (keyboard_fd, KDGKBMODE, &saved_kbd_mode) < 0) {
slouken@1662
   187
            SDL_SetError ("Unable to get current keyboard mode");
slouken@1662
   188
            if (keyboard_fd > 0) {
slouken@1662
   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@1662
   200
        if (tcsetattr (keyboard_fd, TCSAFLUSH, &keyboard_termios) < 0) {
slouken@1662
   201
            FB_CloseKeyboard (this);
slouken@1662
   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@1662
   206
        if (ioctl (keyboard_fd, KDSKBMODE, K_MEDIUMRAW) < 0) {
slouken@1662
   207
            FB_CloseKeyboard (this);
slouken@1662
   208
            SDL_SetError ("Unable to set keyboard in raw mode");
slouken@1662
   209
            return (-1);
slouken@1662
   210
        }
slouken@1662
   211
        if (ioctl (keyboard_fd, KDSETMODE, KD_GRAPHICS) < 0) {
slouken@1662
   212
            FB_CloseKeyboard (this);
slouken@1662
   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@1662
   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@1662
   223
FB_LeaveGraphicsMode (_THIS)
slouken@0
   224
{
slouken@1662
   225
    if (FB_InGraphicsMode (this)) {
slouken@1662
   226
        ioctl (keyboard_fd, KDSETMODE, KD_TEXT);
slouken@1662
   227
        ioctl (keyboard_fd, KDSKBMODE, saved_kbd_mode);
slouken@1662
   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@1662
   232
        ioctl (keyboard_fd, VT_UNLOCKSWITCH, 1);
slouken@1662
   233
        if (saved_vt > 0) {
slouken@1662
   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@1662
   240
FB_CloseKeyboard (_THIS)
slouken@0
   241
{
slouken@1662
   242
    if (keyboard_fd >= 0) {
slouken@1662
   243
        FB_LeaveGraphicsMode (this);
slouken@1662
   244
        if (keyboard_fd > 0) {
slouken@1662
   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@1662
   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@1662
   263
            tty0_fd = open (tty0[i], O_WRONLY, 0);
slouken@1662
   264
        }
slouken@1662
   265
        if (tty0_fd < 0) {
slouken@1662
   266
            tty0_fd = dup (0);  /* Maybe stdin is a VT? */
slouken@1662
   267
        }
slouken@1662
   268
        ioctl (tty0_fd, VT_OPENQRY, &current_vt);
slouken@1662
   269
        close (tty0_fd);
slouken@1662
   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@1662
   274
                SDL_snprintf (vtpath, SDL_arraysize (vtpath), vcs[i],
slouken@1662
   275
                              current_vt);
slouken@1662
   276
                keyboard_fd = open (vtpath, O_RDWR, 0);
slouken@0
   277
#ifdef DEBUG_KEYBOARD
slouken@1662
   278
                fprintf (stderr, "vtpath = %s, fd = %d\n",
slouken@1662
   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@1662
   286
                    tty0_fd = open ("/dev/tty", O_RDWR, 0);
slouken@1662
   287
                    if (tty0_fd >= 0) {
slouken@1662
   288
                        ioctl (tty0_fd, TIOCNOTTY, 0);
slouken@1662
   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@1662
   298
            keyboard_fd = open ("/dev/tty", O_RDWR);
slouken@1659
   299
slouken@1662
   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@1662
   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@1662
   314
            if (ioctl (keyboard_fd, KDGKBMODE, &dummy) < 0) {
slouken@1662
   315
                close (keyboard_fd);
slouken@1662
   316
                keyboard_fd = -1;
slouken@1662
   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@1662
   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@1662
   341
FB_CloseMouse (_THIS)
slouken@0
   342
{
slouken@1361
   343
#if SDL_INPUT_TSLIB
slouken@1662
   344
    if (ts_dev != NULL) {
slouken@1662
   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@1662
   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@1662
   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@1662
   365
    while ((pid == 0) && ((entry = readdir (proc)) != NULL)) {
slouken@1662
   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@1662
   371
            SDL_snprintf (path, SDL_arraysize (path), "/proc/%s/status",
slouken@1662
   372
                          entry->d_name);
slouken@1662
   373
            status = fopen (path, "r");
slouken@1662
   374
            if (status) {
slouken@1662
   375
                name[0] = '\0';
slouken@1662
   376
                fscanf (status, "Name: %s", name);
slouken@1662
   377
                if (SDL_strcmp (name, wanted_name) == 0) {
slouken@1662
   378
                    pid = SDL_atoi (entry->d_name);
slouken@1662
   379
                }
slouken@1662
   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@1662
   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@1662
   400
    fprintf (stderr, "testing gpm\n");
slouken@1659
   401
#endif
slouken@1662
   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@1662
   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@1662
   411
        while (!available && (pid = find_pid (proc, "gpm")) > 0) {
slouken@1662
   412
            SDL_snprintf (path, SDL_arraysize (path), "/proc/%d/cmdline",
slouken@1662
   413
                          pid);
slouken@1662
   414
            cmdline = open (path, O_RDONLY, 0);
slouken@1662
   415
            if (cmdline >= 0) {
slouken@1662
   416
                len = read (cmdline, args, sizeof (args));
slouken@1662
   417
                arg = args;
slouken@1662
   418
                while (len > 0) {
slouken@1662
   419
                    arglen = SDL_strlen (arg) + 1;
slouken@1662
   420
#ifdef DEBUG_MOUSE
slouken@1662
   421
                    fprintf (stderr, "gpm arg %s len %d\n", arg, arglen);
slouken@1659
   422
#endif
slouken@1662
   423
                    if (SDL_strcmp (arg, "-t") == 0) {
slouken@1662
   424
                        /* protocol string, keep it for later */
slouken@1662
   425
                        char *t, *s;
slouken@1662
   426
                        t = arg + arglen;
slouken@1662
   427
                        s = SDL_strchr (t, ' ');
slouken@1662
   428
                        if (s)
slouken@1662
   429
                            *s = 0;
slouken@1662
   430
                        SDL_strlcpy (raw_proto, t, SDL_arraysize (raw_proto));
slouken@1662
   431
                        if (s)
slouken@1662
   432
                            *s = ' ';
slouken@1662
   433
                    }
slouken@1662
   434
                    if (SDL_strncmp (arg, "-R", 2) == 0) {
slouken@1662
   435
                        char *t, *s;
slouken@1662
   436
                        available = 1;
slouken@1662
   437
                        t = arg + 2;
slouken@1662
   438
                        s = SDL_strchr (t, ' ');
slouken@1662
   439
                        if (s)
slouken@1662
   440
                            *s = 0;
slouken@1662
   441
                        SDL_strlcpy (repeat_proto, t,
slouken@1662
   442
                                     SDL_arraysize (repeat_proto));
slouken@1662
   443
                        if (s)
slouken@1662
   444
                            *s = ' ';
slouken@1662
   445
                    }
slouken@1662
   446
                    len -= arglen;
slouken@1662
   447
                    arg += arglen;
slouken@1662
   448
                }
slouken@1662
   449
                close (cmdline);
slouken@1662
   450
            }
slouken@1662
   451
        }
slouken@1662
   452
        closedir (proc);
slouken@1659
   453
slouken@1662
   454
        if (available) {
slouken@1662
   455
            if (SDL_strcmp (repeat_proto, "raw") == 0) {
slouken@1662
   456
                SDL_strlcpy (proto, raw_proto, protolen);
slouken@1662
   457
            } else if (*repeat_proto) {
slouken@1662
   458
                SDL_strlcpy (proto, repeat_proto, protolen);
slouken@1662
   459
            } else {
slouken@1662
   460
                SDL_strlcpy (proto, "msc", protolen);
slouken@1662
   461
            }
slouken@1662
   462
        }
slouken@1662
   463
    }
slouken@1662
   464
    return available;
slouken@0
   465
}
slouken@0
   466
slouken@59
   467
slouken@59
   468
/* rcg06112001 Set up IMPS/2 mode, if possible. This gives
slouken@59
   469
 *  us access to the mousewheel, etc. Returns zero if
slouken@59
   470
 *  writes to device failed, but you still need to query the
slouken@59
   471
 *  device to see which mode it's actually in.
slouken@59
   472
 */
slouken@1662
   473
static int
slouken@1662
   474
set_imps2_mode (int fd)
slouken@59
   475
{
slouken@1662
   476
    /* If you wanted to control the mouse mode (and we do :)  ) ...
slouken@1662
   477
       Set IMPS/2 protocol:
slouken@1662
   478
       {0xf3,200,0xf3,100,0xf3,80}
slouken@1662
   479
       Reset mouse device:
slouken@1662
   480
       {0xFF}
slouken@1662
   481
     */
slouken@1662
   482
    Uint8 set_imps2[] = { 0xf3, 200, 0xf3, 100, 0xf3, 80 };
slouken@1662
   483
    /*Uint8 reset = 0xff; */
slouken@1662
   484
    fd_set fdset;
slouken@1662
   485
    struct timeval tv;
slouken@1662
   486
    int retval = 0;
slouken@59
   487
slouken@1662
   488
    if (write (fd, &set_imps2, sizeof (set_imps2)) == sizeof (set_imps2)) {
slouken@1662
   489
        /* Don't reset it, that'll clear IMPS/2 mode on some mice
slouken@1662
   490
           if (write(fd, &reset, sizeof (reset)) == sizeof (reset) ) {
slouken@1662
   491
           retval = 1;
slouken@1662
   492
           }
slouken@1662
   493
         */
slouken@1662
   494
    }
slouken@59
   495
slouken@1662
   496
    /* Get rid of any chatter from the above */
slouken@1662
   497
    FD_ZERO (&fdset);
slouken@1662
   498
    FD_SET (fd, &fdset);
slouken@1662
   499
    tv.tv_sec = 0;
slouken@1662
   500
    tv.tv_usec = 0;
slouken@1662
   501
    while (select (fd + 1, &fdset, 0, 0, &tv) > 0) {
slouken@1662
   502
        char temp[32];
slouken@1662
   503
        read (fd, temp, sizeof (temp));
slouken@1662
   504
    }
slouken@59
   505
slouken@1662
   506
    return retval;
slouken@59
   507
}
slouken@59
   508
slouken@59
   509
slouken@0
   510
/* Returns true if the mouse uses the IMPS/2 protocol */
slouken@1662
   511
static int
slouken@1662
   512
detect_imps2 (int fd)
slouken@0
   513
{
slouken@1662
   514
    int imps2;
slouken@0
   515
slouken@1662
   516
    imps2 = 0;
slouken@59
   517
slouken@1662
   518
    if (SDL_getenv ("SDL_MOUSEDEV_IMPS2")) {
slouken@1662
   519
        imps2 = 1;
slouken@1662
   520
    }
slouken@1662
   521
    if (!imps2) {
slouken@1662
   522
        Uint8 query_ps2 = 0xF2;
slouken@1662
   523
        fd_set fdset;
slouken@1662
   524
        struct timeval tv;
slouken@0
   525
slouken@1662
   526
        /* Get rid of any mouse motion noise */
slouken@1662
   527
        FD_ZERO (&fdset);
slouken@1662
   528
        FD_SET (fd, &fdset);
slouken@1662
   529
        tv.tv_sec = 0;
slouken@1662
   530
        tv.tv_usec = 0;
slouken@1662
   531
        while (select (fd + 1, &fdset, 0, 0, &tv) > 0) {
slouken@1662
   532
            char temp[32];
slouken@1662
   533
            read (fd, temp, sizeof (temp));
slouken@1662
   534
        }
slouken@0
   535
slouken@1662
   536
        /* Query for the type of mouse protocol */
slouken@1662
   537
        if (write (fd, &query_ps2, sizeof (query_ps2)) == sizeof (query_ps2)) {
slouken@1662
   538
            Uint8 ch = 0;
slouken@0
   539
slouken@1662
   540
            /* Get the mouse protocol response */
slouken@1662
   541
            do {
slouken@1662
   542
                FD_ZERO (&fdset);
slouken@1662
   543
                FD_SET (fd, &fdset);
slouken@1662
   544
                tv.tv_sec = 1;
slouken@1662
   545
                tv.tv_usec = 0;
slouken@1662
   546
                if (select (fd + 1, &fdset, 0, 0, &tv) < 1) {
slouken@1662
   547
                    break;
slouken@1662
   548
                }
slouken@1662
   549
            }
slouken@1662
   550
            while ((read (fd, &ch, sizeof (ch)) == sizeof (ch)) &&
slouken@1662
   551
                   ((ch == 0xFA) || (ch == 0xAA)));
slouken@0
   552
slouken@1662
   553
            /* Experimental values (Logitech wheelmouse) */
slouken@0
   554
#ifdef DEBUG_MOUSE
slouken@1662
   555
            fprintf (stderr, "Last mouse mode: 0x%x\n", ch);
slouken@0
   556
#endif
slouken@1662
   557
            if ((ch == 3) || (ch == 4)) {
slouken@1662
   558
                imps2 = 1;
slouken@1662
   559
            }
slouken@1662
   560
        }
slouken@1662
   561
    }
slouken@1662
   562
    return imps2;
slouken@0
   563
}
slouken@0
   564
slouken@1662
   565
int
slouken@1662
   566
FB_OpenMouse (_THIS)
slouken@0
   567
{
slouken@1662
   568
    int i;
slouken@1662
   569
    const char *mousedev;
slouken@1662
   570
    const char *mousedrv;
slouken@0
   571
slouken@1662
   572
    mousedrv = SDL_getenv ("SDL_MOUSEDRV");
slouken@1662
   573
    mousedev = SDL_getenv ("SDL_MOUSEDEV");
slouken@1662
   574
    mouse_fd = -1;
slouken@0
   575
slouken@1361
   576
#if SDL_INPUT_TSLIB
slouken@1662
   577
    if (mousedrv && (SDL_strcmp (mousedrv, "TSLIB") == 0)) {
slouken@1662
   578
        if (mousedev == NULL)
slouken@1662
   579
            mousedev = SDL_getenv ("TSLIB_TSDEVICE");
slouken@1662
   580
        if (mousedev != NULL) {
slouken@1662
   581
            ts_dev = ts_open (mousedev, 1);
slouken@1662
   582
            if ((ts_dev != NULL) && (ts_config (ts_dev) >= 0)) {
icculus@1201
   583
#ifdef DEBUG_MOUSE
slouken@1662
   584
                fprintf (stderr, "Using tslib touchscreen\n");
icculus@1201
   585
#endif
slouken@1662
   586
                mouse_drv = MOUSE_TSLIB;
slouken@1662
   587
                mouse_fd = ts_fd (ts_dev);
slouken@1662
   588
                return mouse_fd;
slouken@1662
   589
            }
slouken@1662
   590
        }
slouken@1662
   591
        mouse_drv = MOUSE_NONE;
slouken@1662
   592
        return mouse_fd;
slouken@1662
   593
    }
slouken@1361
   594
#endif /* SDL_INPUT_TSLIB */
icculus@1201
   595
slouken@1662
   596
    /* ELO TOUCHSCREEN SUPPORT */
slouken@0
   597
slouken@1662
   598
    if (mousedrv && (SDL_strcmp (mousedrv, "ELO") == 0)) {
slouken@1662
   599
        mouse_fd = open (mousedev, O_RDWR);
slouken@1662
   600
        if (mouse_fd >= 0) {
slouken@1662
   601
            if (eloInitController (mouse_fd)) {
slouken@0
   602
#ifdef DEBUG_MOUSE
slouken@1662
   603
                fprintf (stderr, "Using ELO touchscreen\n");
slouken@0
   604
#endif
slouken@1662
   605
                mouse_drv = MOUSE_ELO;
slouken@1662
   606
            }
slouken@0
   607
slouken@1662
   608
        } else if (mouse_fd < 0) {
slouken@1662
   609
            mouse_drv = MOUSE_NONE;
slouken@1662
   610
        }
slouken@0
   611
slouken@1662
   612
        return (mouse_fd);
slouken@1662
   613
    }
slouken@0
   614
slouken@1662
   615
    /* STD MICE */
slouken@0
   616
slouken@1662
   617
    if (mousedev == NULL) {
slouken@1662
   618
        /* FIXME someday... allow multiple mice in this driver */
slouken@1662
   619
        static const char *ps2mice[] = {
slouken@1662
   620
            "/dev/input/mice", "/dev/usbmouse", "/dev/psaux", NULL
slouken@1662
   621
        };
slouken@1662
   622
        /* First try to use GPM in repeater mode */
slouken@1662
   623
        if (mouse_fd < 0) {
slouken@1662
   624
            char proto[10];
slouken@1662
   625
            if (gpm_available (proto, SDL_arraysize (proto))) {
slouken@1662
   626
                mouse_fd = open (GPM_NODE_FIFO, O_RDONLY, 0);
slouken@1662
   627
                if (mouse_fd >= 0) {
slouken@1662
   628
                    if (SDL_strcmp (proto, "msc") == 0) {
slouken@1662
   629
                        mouse_drv = MOUSE_MSC;
slouken@1662
   630
                    } else if (SDL_strcmp (proto, "ps2") == 0) {
slouken@1662
   631
                        mouse_drv = MOUSE_PS2;
slouken@1662
   632
                    } else if (SDL_strcmp (proto, "imps2") == 0) {
slouken@1662
   633
                        mouse_drv = MOUSE_IMPS2;
slouken@1662
   634
                    } else if (SDL_strcmp (proto, "ms") == 0 ||
slouken@1662
   635
                               SDL_strcmp (proto, "bare") == 0) {
slouken@1662
   636
                        mouse_drv = MOUSE_MS;
slouken@1662
   637
                    } else if (SDL_strcmp (proto, "bm") == 0) {
slouken@1662
   638
                        mouse_drv = MOUSE_BM;
slouken@1662
   639
                    } else {
slouken@1662
   640
                        /* Unknown protocol... */
slouken@0
   641
#ifdef DEBUG_MOUSE
slouken@1662
   642
                        fprintf (stderr,
slouken@1662
   643
                                 "GPM mouse using unknown protocol = %s\n",
slouken@1662
   644
                                 proto);
slouken@0
   645
#endif
slouken@1662
   646
                        close (mouse_fd);
slouken@1662
   647
                        mouse_fd = -1;
slouken@1662
   648
                    }
slouken@1662
   649
                }
slouken@1659
   650
#ifdef DEBUG_MOUSE
slouken@1662
   651
                if (mouse_fd >= 0) {
slouken@1662
   652
                    fprintf (stderr,
slouken@1662
   653
                             "Using GPM mouse, protocol = %s\n", proto);
slouken@1662
   654
                }
slouken@1659
   655
#endif /* DEBUG_MOUSE */
slouken@1662
   656
            }
slouken@1662
   657
        }
slouken@1662
   658
        /* Now try to use a modern PS/2 mouse */
slouken@1662
   659
        for (i = 0; (mouse_fd < 0) && ps2mice[i]; ++i) {
slouken@1662
   660
            mouse_fd = open (ps2mice[i], O_RDWR, 0);
slouken@1662
   661
            if (mouse_fd < 0) {
slouken@1662
   662
                mouse_fd = open (ps2mice[i], O_RDONLY, 0);
slouken@1662
   663
            }
slouken@1662
   664
            if (mouse_fd >= 0) {
slouken@1662
   665
                /* rcg06112001 Attempt to set IMPS/2 mode */
slouken@1662
   666
                set_imps2_mode (mouse_fd);
slouken@1662
   667
                if (detect_imps2 (mouse_fd)) {
slouken@0
   668
#ifdef DEBUG_MOUSE
slouken@1662
   669
                    fprintf (stderr, "Using IMPS2 mouse\n");
slouken@0
   670
#endif
slouken@1662
   671
                    mouse_drv = MOUSE_IMPS2;
slouken@1662
   672
                } else {
slouken@0
   673
#ifdef DEBUG_MOUSE
slouken@1662
   674
                    fprintf (stderr, "Using PS2 mouse\n");
slouken@0
   675
#endif
slouken@1662
   676
                    mouse_drv = MOUSE_PS2;
slouken@1662
   677
                }
slouken@1662
   678
            }
slouken@1662
   679
        }
slouken@1662
   680
        /* Next try to use a PPC ADB port mouse */
slouken@1662
   681
        if (mouse_fd < 0) {
slouken@1662
   682
            mouse_fd = open ("/dev/adbmouse", O_RDONLY, 0);
slouken@1662
   683
            if (mouse_fd >= 0) {
slouken@0
   684
#ifdef DEBUG_MOUSE
slouken@1662
   685
                fprintf (stderr, "Using ADB mouse\n");
slouken@0
   686
#endif
slouken@1662
   687
                mouse_drv = MOUSE_BM;
slouken@1662
   688
            }
slouken@1662
   689
        }
slouken@1662
   690
    }
slouken@1662
   691
    /* Default to a serial Microsoft mouse */
slouken@1662
   692
    if (mouse_fd < 0) {
slouken@1662
   693
        if (mousedev == NULL) {
slouken@1662
   694
            mousedev = "/dev/mouse";
slouken@1662
   695
        }
slouken@1662
   696
        mouse_fd = open (mousedev, O_RDONLY, 0);
slouken@1662
   697
        if (mouse_fd >= 0) {
slouken@1662
   698
            struct termios mouse_termios;
slouken@0
   699
slouken@1662
   700
            /* Set the sampling speed to 1200 baud */
slouken@1662
   701
            tcgetattr (mouse_fd, &mouse_termios);
slouken@1662
   702
            mouse_termios.c_iflag = IGNBRK | IGNPAR;
slouken@1662
   703
            mouse_termios.c_oflag = 0;
slouken@1662
   704
            mouse_termios.c_lflag = 0;
slouken@1662
   705
            mouse_termios.c_line = 0;
slouken@1662
   706
            mouse_termios.c_cc[VTIME] = 0;
slouken@1662
   707
            mouse_termios.c_cc[VMIN] = 1;
slouken@1662
   708
            mouse_termios.c_cflag = CREAD | CLOCAL | HUPCL;
slouken@1662
   709
            mouse_termios.c_cflag |= CS8;
slouken@1662
   710
            mouse_termios.c_cflag |= B1200;
slouken@1662
   711
            tcsetattr (mouse_fd, TCSAFLUSH, &mouse_termios);
slouken@1662
   712
            if (mousedrv && (SDL_strcmp (mousedrv, "PS2") == 0)) {
slouken@0
   713
#ifdef DEBUG_MOUSE
slouken@1662
   714
                fprintf (stderr,
slouken@1662
   715
                         "Using (user specified) PS2 mouse on %s\n",
slouken@1662
   716
                         mousedev);
slouken@0
   717
#endif
slouken@1662
   718
                mouse_drv = MOUSE_PS2;
slouken@1662
   719
            } else {
slouken@1585
   720
#ifdef DEBUG_MOUSE
slouken@1662
   721
                fprintf (stderr, "Using (default) MS mouse on %s\n",
slouken@1662
   722
                         mousedev);
slouken@1585
   723
#endif
slouken@1662
   724
                mouse_drv = MOUSE_MS;
slouken@1662
   725
            }
slouken@1662
   726
        }
slouken@1662
   727
    }
slouken@1662
   728
    if (mouse_fd < 0) {
slouken@1662
   729
        mouse_drv = MOUSE_NONE;
slouken@1662
   730
    }
slouken@1662
   731
    return (mouse_fd);
slouken@0
   732
}
slouken@0
   733
slouken@0
   734
static int posted = 0;
slouken@0
   735
slouken@1662
   736
void
slouken@1662
   737
FB_vgamousecallback (int button, int relative, int dx, int dy)
slouken@0
   738
{
slouken@1662
   739
    int button_1, button_3;
slouken@1662
   740
    int button_state;
slouken@1662
   741
    int state_changed;
slouken@1662
   742
    int i;
slouken@1662
   743
    Uint8 state;
slouken@0
   744
slouken@1662
   745
    if (dx || dy) {
slouken@1662
   746
        posted += SDL_PrivateMouseMotion (0, relative, dx, dy);
slouken@1662
   747
    }
slouken@0
   748
slouken@1662
   749
    /* Swap button 1 and 3 */
slouken@1662
   750
    button_1 = (button & 0x04) >> 2;
slouken@1662
   751
    button_3 = (button & 0x01) << 2;
slouken@1662
   752
    button &= ~0x05;
slouken@1662
   753
    button |= (button_1 | button_3);
slouken@0
   754
slouken@1662
   755
    /* See what changed */
slouken@1662
   756
    button_state = SDL_GetMouseState (NULL, NULL);
slouken@1662
   757
    state_changed = button_state ^ button;
slouken@1662
   758
    for (i = 0; i < 8; ++i) {
slouken@1662
   759
        if (state_changed & (1 << i)) {
slouken@1662
   760
            if (button & (1 << i)) {
slouken@1662
   761
                state = SDL_PRESSED;
slouken@1662
   762
            } else {
slouken@1662
   763
                state = SDL_RELEASED;
slouken@1662
   764
            }
slouken@1662
   765
            posted += SDL_PrivateMouseButton (state, i + 1, 0, 0);
slouken@1662
   766
        }
slouken@1662
   767
    }
slouken@0
   768
}
slouken@0
   769
icculus@1201
   770
/* Handle input from tslib */
slouken@1361
   771
#if SDL_INPUT_TSLIB
slouken@1662
   772
static void
slouken@1662
   773
handle_tslib (_THIS)
icculus@1201
   774
{
slouken@1662
   775
    struct ts_sample sample;
slouken@1662
   776
    int button;
icculus@1201
   777
slouken@1662
   778
    while (ts_read (ts_dev, &sample, 1) > 0) {
slouken@1662
   779
        button = (sample.pressure > 0) ? 1 : 0;
slouken@1662
   780
        button <<= 2;           /* must report it as button 3 */
slouken@1662
   781
        FB_vgamousecallback (button, 0, sample.x, sample.y);
slouken@1662
   782
    }
slouken@1662
   783
    return;
icculus@1201
   784
}
slouken@1361
   785
#endif /* SDL_INPUT_TSLIB */
icculus@1201
   786
slouken@109
   787
/* For now, use MSC, PS/2, and MS protocols
slouken@0
   788
   Driver adapted from the SVGAlib mouse driver code (taken from gpm, etc.)
slouken@0
   789
 */
slouken@1662
   790
static void
slouken@1662
   791
handle_mouse (_THIS)
slouken@0
   792
{
slouken@1662
   793
    static int start = 0;
slouken@1662
   794
    static unsigned char mousebuf[BUFSIZ];
slouken@1662
   795
    static int relative = 1;
slouken@0
   796
slouken@1662
   797
    int i, nread;
slouken@1662
   798
    int button = 0;
slouken@1662
   799
    int dx = 0, dy = 0;
slouken@1662
   800
    int packetsize = 0;
slouken@1662
   801
    int realx, realy;
slouken@1662
   802
slouken@1662
   803
    /* Figure out the mouse packet size */
slouken@1662
   804
    switch (mouse_drv) {
slouken@1662
   805
    case MOUSE_NONE:
slouken@1662
   806
        /* Ack! */
slouken@1662
   807
        read (mouse_fd, mousebuf, BUFSIZ);
slouken@1662
   808
        return;
slouken@1662
   809
    case MOUSE_MSC:
slouken@1662
   810
        packetsize = 5;
slouken@1662
   811
        break;
slouken@1662
   812
    case MOUSE_IMPS2:
slouken@1662
   813
        packetsize = 4;
slouken@1662
   814
        break;
slouken@1662
   815
    case MOUSE_PS2:
slouken@1662
   816
    case MOUSE_MS:
slouken@1662
   817
    case MOUSE_BM:
slouken@1662
   818
        packetsize = 3;
slouken@1662
   819
        break;
slouken@1662
   820
    case MOUSE_ELO:
slouken@1662
   821
        /* try to read the next packet */
slouken@1662
   822
        if (eloReadPosition
slouken@1662
   823
            (this, mouse_fd, &dx, &dy, &button, &realx, &realy)) {
slouken@1662
   824
            button = (button & 0x01) << 2;
slouken@1662
   825
            FB_vgamousecallback (button, 0, dx, dy);
slouken@1662
   826
        }
slouken@1662
   827
        return;                 /* nothing left to do */
slouken@1662
   828
    case MOUSE_TSLIB:
slouken@1361
   829
#if SDL_INPUT_TSLIB
slouken@1662
   830
        handle_tslib (this);
icculus@1201
   831
#endif
slouken@1662
   832
        return;                 /* nothing left to do */
slouken@1662
   833
    default:
slouken@1662
   834
        /* Uh oh.. */
slouken@1662
   835
        packetsize = 0;
slouken@1662
   836
        break;
slouken@1662
   837
    }
slouken@0
   838
slouken@1662
   839
    /* Special handling for the quite sensitive ELO controller */
slouken@1662
   840
    if (mouse_drv == MOUSE_ELO) {
slouken@1662
   841
slouken@1662
   842
    }
slouken@1662
   843
slouken@1662
   844
    /* Read as many packets as possible */
slouken@1662
   845
    nread = read (mouse_fd, &mousebuf[start], BUFSIZ - start);
slouken@1662
   846
    if (nread < 0) {
slouken@1662
   847
        return;
slouken@1662
   848
    }
slouken@1662
   849
    nread += start;
slouken@0
   850
#ifdef DEBUG_MOUSE
slouken@1662
   851
    fprintf (stderr, "Read %d bytes from mouse, start = %d\n", nread, start);
slouken@0
   852
#endif
slouken@1662
   853
    for (i = 0; i < (nread - (packetsize - 1)); i += packetsize) {
slouken@1662
   854
        switch (mouse_drv) {
slouken@1662
   855
        case MOUSE_NONE:
slouken@1662
   856
            break;
slouken@1662
   857
        case MOUSE_MSC:
slouken@1662
   858
            /* MSC protocol has 0x80 in high byte */
slouken@1662
   859
            if ((mousebuf[i] & 0xF8) != 0x80) {
slouken@1662
   860
                /* Go to next byte */
slouken@1662
   861
                i -= (packetsize - 1);
slouken@1662
   862
                continue;
slouken@1662
   863
            }
slouken@1662
   864
            /* Get current mouse state */
slouken@1662
   865
            button = (~mousebuf[i]) & 0x07;
slouken@1662
   866
            dx = (signed char) (mousebuf[i + 1]) +
slouken@1662
   867
                (signed char) (mousebuf[i + 3]);
slouken@1662
   868
            dy = -((signed char) (mousebuf[i + 2]) +
slouken@1662
   869
                   (signed char) (mousebuf[i + 4]));
slouken@1662
   870
            break;
slouken@1662
   871
        case MOUSE_PS2:
slouken@1662
   872
            /* PS/2 protocol has nothing in high byte */
slouken@1662
   873
            if ((mousebuf[i] & 0xC0) != 0) {
slouken@1662
   874
                /* Go to next byte */
slouken@1662
   875
                i -= (packetsize - 1);
slouken@1662
   876
                continue;
slouken@1662
   877
            }
slouken@1662
   878
            /* Get current mouse state */
slouken@1662
   879
            button = (mousebuf[i] & 0x04) >> 1 |        /*Middle */
slouken@1662
   880
                (mousebuf[i] & 0x02) >> 1 |     /*Right */
slouken@1662
   881
                (mousebuf[i] & 0x01) << 2;      /*Left */
slouken@1662
   882
            dx = (mousebuf[i] & 0x10) ?
slouken@1662
   883
                mousebuf[i + 1] - 256 : mousebuf[i + 1];
slouken@1662
   884
            dy = (mousebuf[i] & 0x20) ?
slouken@1662
   885
                -(mousebuf[i + 2] - 256) : -mousebuf[i + 2];
slouken@1662
   886
            break;
slouken@1662
   887
        case MOUSE_IMPS2:
slouken@1662
   888
            /* Get current mouse state */
slouken@1662
   889
            button = (mousebuf[i] & 0x04) >> 1 |        /*Middle */
slouken@1662
   890
                (mousebuf[i] & 0x02) >> 1 |     /*Right */
slouken@1662
   891
                (mousebuf[i] & 0x01) << 2 |     /*Left */
slouken@1662
   892
                (mousebuf[i] & 0x40) >> 3 |     /* 4 */
slouken@1662
   893
                (mousebuf[i] & 0x80) >> 3;      /* 5 */
slouken@1662
   894
            dx = (mousebuf[i] & 0x10) ?
slouken@1662
   895
                mousebuf[i + 1] - 256 : mousebuf[i + 1];
slouken@1662
   896
            dy = (mousebuf[i] & 0x20) ?
slouken@1662
   897
                -(mousebuf[i + 2] - 256) : -mousebuf[i + 2];
slouken@1662
   898
            switch (mousebuf[i + 3] & 0x0F) {
slouken@1662
   899
            case 0x0E:         /* DX = +1 */
slouken@1662
   900
            case 0x02:         /* DX = -1 */
slouken@1662
   901
                break;
slouken@1662
   902
            case 0x0F:         /* DY = +1 (map button 4) */
slouken@1662
   903
                FB_vgamousecallback (button | (1 << 3), 1, 0, 0);
slouken@1662
   904
                break;
slouken@1662
   905
            case 0x01:         /* DY = -1 (map button 5) */
slouken@1662
   906
                FB_vgamousecallback (button | (1 << 4), 1, 0, 0);
slouken@1662
   907
                break;
slouken@1662
   908
            }
slouken@1662
   909
            break;
slouken@1662
   910
        case MOUSE_MS:
slouken@1662
   911
            /* Microsoft protocol has 0x40 in high byte */
slouken@1662
   912
            if ((mousebuf[i] & 0x40) != 0x40) {
slouken@1662
   913
                /* Go to next byte */
slouken@1662
   914
                i -= (packetsize - 1);
slouken@1662
   915
                continue;
slouken@1662
   916
            }
slouken@1662
   917
            /* Get current mouse state */
slouken@1662
   918
            button = ((mousebuf[i] & 0x20) >> 3) |
slouken@1662
   919
                ((mousebuf[i] & 0x10) >> 4);
slouken@1662
   920
            dx = (signed char) (((mousebuf[i] & 0x03) << 6) |
slouken@1662
   921
                                (mousebuf[i + 1] & 0x3F));
slouken@1662
   922
            dy = (signed char) (((mousebuf[i] & 0x0C) << 4) |
slouken@1662
   923
                                (mousebuf[i + 2] & 0x3F));
slouken@1662
   924
            break;
slouken@1662
   925
        case MOUSE_BM:
slouken@1662
   926
            /* BusMouse protocol has 0xF8 in high byte */
slouken@1662
   927
            if ((mousebuf[i] & 0xF8) != 0x80) {
slouken@1662
   928
                /* Go to next byte */
slouken@1662
   929
                i -= (packetsize - 1);
slouken@1662
   930
                continue;
slouken@1662
   931
            }
slouken@1662
   932
            /* Get current mouse state */
slouken@1662
   933
            button = (~mousebuf[i]) & 0x07;
slouken@1662
   934
            dx = (signed char) mousebuf[i + 1];
slouken@1662
   935
            dy = -(signed char) mousebuf[i + 2];
slouken@1662
   936
            break;
slouken@1662
   937
        default:
slouken@1662
   938
            /* Uh oh.. */
slouken@1662
   939
            dx = 0;
slouken@1662
   940
            dy = 0;
slouken@1662
   941
            break;
slouken@1662
   942
        }
slouken@1662
   943
        FB_vgamousecallback (button, relative, dx, dy);
slouken@1662
   944
    }
slouken@1662
   945
    if (i < nread) {
slouken@1662
   946
        SDL_memcpy (mousebuf, &mousebuf[i], (nread - i));
slouken@1662
   947
        start = (nread - i);
slouken@1662
   948
    } else {
slouken@1662
   949
        start = 0;
slouken@1662
   950
    }
slouken@1662
   951
    return;
slouken@0
   952
}
slouken@0
   953
slouken@1659
   954
/* Handle switching to another VC, returns when our VC is back */
slouken@1662
   955
static void
slouken@1662
   956
switch_vt_prep (_THIS)
slouken@1659
   957
{
slouken@1662
   958
    SDL_Surface *screen = SDL_VideoSurface;
slouken@1659
   959
slouken@1662
   960
    SDL_PrivateAppActive (0,
slouken@1662
   961
                          (SDL_APPACTIVE | SDL_APPINPUTFOCUS |
slouken@1662
   962
                           SDL_APPMOUSEFOCUS));
slouken@1659
   963
slouken@1662
   964
    /* Save the contents of the screen, and go to text mode */
slouken@1662
   965
    wait_idle (this);
slouken@1662
   966
    screen_arealen = ((screen->h + (2 * this->offset_y)) * screen->pitch);
slouken@1662
   967
    screen_contents = (Uint8 *) SDL_malloc (screen_arealen);
slouken@1662
   968
    if (screen_contents) {
slouken@1662
   969
        SDL_memcpy (screen_contents, screen->pixels, screen_arealen);
slouken@1662
   970
    }
slouken@1662
   971
    FB_SavePaletteTo (this, 256, screen_palette);
slouken@1662
   972
    ioctl (console_fd, FBIOGET_VSCREENINFO, &screen_vinfo);
slouken@1662
   973
    ioctl (keyboard_fd, KDSETMODE, KD_TEXT);
slouken@1662
   974
    ioctl (keyboard_fd, VT_UNLOCKSWITCH, 1);
slouken@1659
   975
}
slouken@1662
   976
static void
slouken@1662
   977
switch_vt_done (_THIS)
slouken@1659
   978
{
slouken@1662
   979
    SDL_Surface *screen = SDL_VideoSurface;
slouken@1659
   980
slouken@1662
   981
    /* Restore graphics mode and the contents of the screen */
slouken@1662
   982
    ioctl (keyboard_fd, VT_LOCKSWITCH, 1);
slouken@1662
   983
    ioctl (keyboard_fd, KDSETMODE, KD_GRAPHICS);
slouken@1662
   984
    ioctl (console_fd, FBIOPUT_VSCREENINFO, &screen_vinfo);
slouken@1662
   985
    FB_RestorePaletteFrom (this, 256, screen_palette);
slouken@1662
   986
    if (screen_contents) {
slouken@1662
   987
        SDL_memcpy (screen->pixels, screen_contents, screen_arealen);
slouken@1662
   988
        SDL_free (screen_contents);
slouken@1662
   989
        screen_contents = NULL;
slouken@1662
   990
    }
slouken@1659
   991
slouken@1662
   992
    /* Get updates to the shadow surface while switched away */
slouken@1662
   993
    if (SDL_ShadowSurface) {
slouken@1662
   994
        SDL_UpdateRect (SDL_ShadowSurface, 0, 0, 0, 0);
slouken@1662
   995
    }
slouken@1659
   996
slouken@1662
   997
    SDL_PrivateAppActive (1,
slouken@1662
   998
                          (SDL_APPACTIVE | SDL_APPINPUTFOCUS |
slouken@1662
   999
                           SDL_APPMOUSEFOCUS));
slouken@1659
  1000
}
slouken@1662
  1001
static void
slouken@1662
  1002
switch_vt (_THIS, unsigned short which)
slouken@0
  1003
{
slouken@1662
  1004
    struct vt_stat vtstate;
slouken@0
  1005
slouken@1662
  1006
    /* Figure out whether or not we're switching to a new console */
slouken@1662
  1007
    if ((ioctl (keyboard_fd, VT_GETSTATE, &vtstate) < 0) ||
slouken@1662
  1008
        (which == vtstate.v_active)) {
slouken@1662
  1009
        return;
slouken@1662
  1010
    }
slouken@0
  1011
slouken@1662
  1012
    /* New console, switch to it */
slouken@1662
  1013
    SDL_mutexP (hw_lock);
slouken@1662
  1014
    switch_vt_prep (this);
slouken@1662
  1015
    if (ioctl (keyboard_fd, VT_ACTIVATE, which) == 0) {
slouken@1662
  1016
        ioctl (keyboard_fd, VT_WAITACTIVE, which);
slouken@1662
  1017
        switched_away = 1;
slouken@1662
  1018
    } else {
slouken@1662
  1019
        switch_vt_done (this);
slouken@1662
  1020
    }
slouken@1662
  1021
    SDL_mutexV (hw_lock);
slouken@0
  1022
}
slouken@0
  1023
slouken@1662
  1024
static void
slouken@1662
  1025
handle_keyboard (_THIS)
slouken@0
  1026
{
slouken@1662
  1027
    unsigned char keybuf[BUFSIZ];
slouken@1662
  1028
    int i, nread;
slouken@1662
  1029
    int pressed;
slouken@1662
  1030
    int scancode;
slouken@1662
  1031
    SDL_keysym keysym;
slouken@0
  1032
slouken@1662
  1033
    nread = read (keyboard_fd, keybuf, BUFSIZ);
slouken@1662
  1034
    for (i = 0; i < nread; ++i) {
slouken@1662
  1035
        scancode = keybuf[i] & 0x7F;
slouken@1662
  1036
        if (keybuf[i] & 0x80) {
slouken@1662
  1037
            pressed = SDL_RELEASED;
slouken@1662
  1038
        } else {
slouken@1662
  1039
            pressed = SDL_PRESSED;
slouken@1662
  1040
        }
slouken@1662
  1041
        TranslateKey (scancode, &keysym);
slouken@1662
  1042
        /* Handle Ctrl-Alt-FN for vt switch */
slouken@1662
  1043
        switch (keysym.sym) {
slouken@1662
  1044
        case SDLK_F1:
slouken@1662
  1045
        case SDLK_F2:
slouken@1662
  1046
        case SDLK_F3:
slouken@1662
  1047
        case SDLK_F4:
slouken@1662
  1048
        case SDLK_F5:
slouken@1662
  1049
        case SDLK_F6:
slouken@1662
  1050
        case SDLK_F7:
slouken@1662
  1051
        case SDLK_F8:
slouken@1662
  1052
        case SDLK_F9:
slouken@1662
  1053
        case SDLK_F10:
slouken@1662
  1054
        case SDLK_F11:
slouken@1662
  1055
        case SDLK_F12:
slouken@1662
  1056
            if ((SDL_GetModState () & KMOD_CTRL) &&
slouken@1662
  1057
                (SDL_GetModState () & KMOD_ALT)) {
slouken@1662
  1058
                if (pressed) {
slouken@1662
  1059
                    switch_vt (this, (keysym.sym - SDLK_F1) + 1);
slouken@1662
  1060
                }
slouken@1662
  1061
                break;
slouken@1662
  1062
            }
slouken@1662
  1063
            /* Fall through to normal processing */
slouken@1662
  1064
        default:
slouken@1662
  1065
            posted += SDL_PrivateKeyboard (pressed, &keysym);
slouken@1662
  1066
            break;
slouken@1662
  1067
        }
slouken@1662
  1068
    }
slouken@0
  1069
}
slouken@0
  1070
slouken@1662
  1071
void
slouken@1662
  1072
FB_PumpEvents (_THIS)
slouken@0
  1073
{
slouken@1662
  1074
    fd_set fdset;
slouken@1662
  1075
    int max_fd;
slouken@1662
  1076
    static struct timeval zero;
slouken@0
  1077
slouken@1662
  1078
    do {
slouken@1662
  1079
        if (switched_away) {
slouken@1662
  1080
            struct vt_stat vtstate;
slouken@1659
  1081
slouken@1662
  1082
            SDL_mutexP (hw_lock);
slouken@1662
  1083
            if ((ioctl (keyboard_fd, VT_GETSTATE, &vtstate) == 0) &&
slouken@1662
  1084
                vtstate.v_active == current_vt) {
slouken@1662
  1085
                switched_away = 0;
slouken@1662
  1086
                switch_vt_done (this);
slouken@1662
  1087
            }
slouken@1662
  1088
            SDL_mutexV (hw_lock);
slouken@1662
  1089
        }
slouken@1659
  1090
slouken@1662
  1091
        posted = 0;
slouken@0
  1092
slouken@1662
  1093
        FD_ZERO (&fdset);
slouken@1662
  1094
        max_fd = 0;
slouken@1662
  1095
        if (keyboard_fd >= 0) {
slouken@1662
  1096
            FD_SET (keyboard_fd, &fdset);
slouken@1662
  1097
            if (max_fd < keyboard_fd) {
slouken@1662
  1098
                max_fd = keyboard_fd;
slouken@1662
  1099
            }
slouken@1662
  1100
        }
slouken@1662
  1101
        if (mouse_fd >= 0) {
slouken@1662
  1102
            FD_SET (mouse_fd, &fdset);
slouken@1662
  1103
            if (max_fd < mouse_fd) {
slouken@1662
  1104
                max_fd = mouse_fd;
slouken@1662
  1105
            }
slouken@1662
  1106
        }
slouken@1662
  1107
        if (select (max_fd + 1, &fdset, NULL, NULL, &zero) > 0) {
slouken@1662
  1108
            if (keyboard_fd >= 0) {
slouken@1662
  1109
                if (FD_ISSET (keyboard_fd, &fdset)) {
slouken@1662
  1110
                    handle_keyboard (this);
slouken@1662
  1111
                }
slouken@1662
  1112
            }
slouken@1662
  1113
            if (mouse_fd >= 0) {
slouken@1662
  1114
                if (FD_ISSET (mouse_fd, &fdset)) {
slouken@1662
  1115
                    handle_mouse (this);
slouken@1662
  1116
                }
slouken@1662
  1117
            }
slouken@1662
  1118
        }
slouken@1662
  1119
    }
slouken@1662
  1120
    while (posted);
slouken@0
  1121
}
slouken@0
  1122
slouken@1662
  1123
void
slouken@1662
  1124
FB_InitOSKeymap (_THIS)
slouken@0
  1125
{
slouken@1662
  1126
    int i;
slouken@0
  1127
slouken@1662
  1128
    /* Initialize the Linux key translation table */
slouken@0
  1129
slouken@1662
  1130
    /* First get the ascii keys and others not well handled */
slouken@1662
  1131
    for (i = 0; i < SDL_arraysize (keymap); ++i) {
slouken@1662
  1132
        switch (i) {
slouken@1662
  1133
            /* These aren't handled by the x86 kernel keymapping (?) */
slouken@1662
  1134
        case SCANCODE_PRINTSCREEN:
slouken@1662
  1135
            keymap[i] = SDLK_PRINT;
slouken@1662
  1136
            break;
slouken@1662
  1137
        case SCANCODE_BREAK:
slouken@1662
  1138
            keymap[i] = SDLK_BREAK;
slouken@1662
  1139
            break;
slouken@1662
  1140
        case SCANCODE_BREAK_ALTERNATIVE:
slouken@1662
  1141
            keymap[i] = SDLK_PAUSE;
slouken@1662
  1142
            break;
slouken@1662
  1143
        case SCANCODE_LEFTSHIFT:
slouken@1662
  1144
            keymap[i] = SDLK_LSHIFT;
slouken@1662
  1145
            break;
slouken@1662
  1146
        case SCANCODE_RIGHTSHIFT:
slouken@1662
  1147
            keymap[i] = SDLK_RSHIFT;
slouken@1662
  1148
            break;
slouken@1662
  1149
        case SCANCODE_LEFTCONTROL:
slouken@1662
  1150
            keymap[i] = SDLK_LCTRL;
slouken@1662
  1151
            break;
slouken@1662
  1152
        case SCANCODE_RIGHTCONTROL:
slouken@1662
  1153
            keymap[i] = SDLK_RCTRL;
slouken@1662
  1154
            break;
slouken@1662
  1155
        case SCANCODE_RIGHTWIN:
slouken@1662
  1156
            keymap[i] = SDLK_RSUPER;
slouken@1662
  1157
            break;
slouken@1662
  1158
        case SCANCODE_LEFTWIN:
slouken@1662
  1159
            keymap[i] = SDLK_LSUPER;
slouken@1662
  1160
            break;
slouken@1662
  1161
        case SCANCODE_LEFTALT:
slouken@1662
  1162
            keymap[i] = SDLK_LALT;
slouken@1662
  1163
            break;
slouken@1662
  1164
        case SCANCODE_RIGHTALT:
slouken@1662
  1165
            keymap[i] = SDLK_RALT;
slouken@1662
  1166
            break;
slouken@1662
  1167
        case 127:
slouken@1662
  1168
            keymap[i] = SDLK_MENU;
slouken@1662
  1169
            break;
slouken@1662
  1170
            /* this should take care of all standard ascii keys */
slouken@1662
  1171
        default:
slouken@1662
  1172
            keymap[i] = KVAL (vga_keymap[0][i]);
slouken@1662
  1173
            break;
slouken@1662
  1174
        }
slouken@1662
  1175
    }
slouken@1662
  1176
    for (i = 0; i < SDL_arraysize (keymap); ++i) {
slouken@1662
  1177
        switch (keymap_temp[i]) {
slouken@1662
  1178
        case K_F1:
slouken@1662
  1179
            keymap[i] = SDLK_F1;
slouken@1662
  1180
            break;
slouken@1662
  1181
        case K_F2:
slouken@1662
  1182
            keymap[i] = SDLK_F2;
slouken@1662
  1183
            break;
slouken@1662
  1184
        case K_F3:
slouken@1662
  1185
            keymap[i] = SDLK_F3;
slouken@1662
  1186
            break;
slouken@1662
  1187
        case K_F4:
slouken@1662
  1188
            keymap[i] = SDLK_F4;
slouken@1662
  1189
            break;
slouken@1662
  1190
        case K_F5:
slouken@1662
  1191
            keymap[i] = SDLK_F5;
slouken@1662
  1192
            break;
slouken@1662
  1193
        case K_F6:
slouken@1662
  1194
            keymap[i] = SDLK_F6;
slouken@1662
  1195
            break;
slouken@1662
  1196
        case K_F7:
slouken@1662
  1197
            keymap[i] = SDLK_F7;
slouken@1662
  1198
            break;
slouken@1662
  1199
        case K_F8:
slouken@1662
  1200
            keymap[i] = SDLK_F8;
slouken@1662
  1201
            break;
slouken@1662
  1202
        case K_F9:
slouken@1662
  1203
            keymap[i] = SDLK_F9;
slouken@1662
  1204
            break;
slouken@1662
  1205
        case K_F10:
slouken@1662
  1206
            keymap[i] = SDLK_F10;
slouken@1662
  1207
            break;
slouken@1662
  1208
        case K_F11:
slouken@1662
  1209
            keymap[i] = SDLK_F11;
slouken@1662
  1210
            break;
slouken@1662
  1211
        case K_F12:
slouken@1662
  1212
            keymap[i] = SDLK_F12;
slouken@1662
  1213
            break;
slouken@0
  1214
slouken@1662
  1215
        case K_DOWN:
slouken@1662
  1216
            keymap[i] = SDLK_DOWN;
slouken@1662
  1217
            break;
slouken@1662
  1218
        case K_LEFT:
slouken@1662
  1219
            keymap[i] = SDLK_LEFT;
slouken@1662
  1220
            break;
slouken@1662
  1221
        case K_RIGHT:
slouken@1662
  1222
            keymap[i] = SDLK_RIGHT;
slouken@1662
  1223
            break;
slouken@1662
  1224
        case K_UP:
slouken@1662
  1225
            keymap[i] = SDLK_UP;
slouken@1662
  1226
            break;
slouken@0
  1227
slouken@1662
  1228
        case K_P0:
slouken@1662
  1229
            keymap[i] = SDLK_KP0;
slouken@1662
  1230
            break;
slouken@1662
  1231
        case K_P1:
slouken@1662
  1232
            keymap[i] = SDLK_KP1;
slouken@1662
  1233
            break;
slouken@1662
  1234
        case K_P2:
slouken@1662
  1235
            keymap[i] = SDLK_KP2;
slouken@1662
  1236
            break;
slouken@1662
  1237
        case K_P3:
slouken@1662
  1238
            keymap[i] = SDLK_KP3;
slouken@1662
  1239
            break;
slouken@1662
  1240
        case K_P4:
slouken@1662
  1241
            keymap[i] = SDLK_KP4;
slouken@1662
  1242
            break;
slouken@1662
  1243
        case K_P5:
slouken@1662
  1244
            keymap[i] = SDLK_KP5;
slouken@1662
  1245
            break;
slouken@1662
  1246
        case K_P6:
slouken@1662
  1247
            keymap[i] = SDLK_KP6;
slouken@1662
  1248
            break;
slouken@1662
  1249
        case K_P7:
slouken@1662
  1250
            keymap[i] = SDLK_KP7;
slouken@1662
  1251
            break;
slouken@1662
  1252
        case K_P8:
slouken@1662
  1253
            keymap[i] = SDLK_KP8;
slouken@1662
  1254
            break;
slouken@1662
  1255
        case K_P9:
slouken@1662
  1256
            keymap[i] = SDLK_KP9;
slouken@1662
  1257
            break;
slouken@1662
  1258
        case K_PPLUS:
slouken@1662
  1259
            keymap[i] = SDLK_KP_PLUS;
slouken@1662
  1260
            break;
slouken@1662
  1261
        case K_PMINUS:
slouken@1662
  1262
            keymap[i] = SDLK_KP_MINUS;
slouken@1662
  1263
            break;
slouken@1662
  1264
        case K_PSTAR:
slouken@1662
  1265
            keymap[i] = SDLK_KP_MULTIPLY;
slouken@1662
  1266
            break;
slouken@1662
  1267
        case K_PSLASH:
slouken@1662
  1268
            keymap[i] = SDLK_KP_DIVIDE;
slouken@1662
  1269
            break;
slouken@1662
  1270
        case K_PENTER:
slouken@1662
  1271
            keymap[i] = SDLK_KP_ENTER;
slouken@1662
  1272
            break;
slouken@1662
  1273
        case K_PDOT:
slouken@1662
  1274
            keymap[i] = SDLK_KP_PERIOD;
slouken@1662
  1275
            break;
slouken@0
  1276
slouken@1662
  1277
        case K_SHIFT:
slouken@1662
  1278
            if (keymap[i] != SDLK_RSHIFT)
slouken@1662
  1279
                keymap[i] = SDLK_LSHIFT;
slouken@1662
  1280
            break;
slouken@1662
  1281
        case K_SHIFTL:
slouken@1662
  1282
            keymap[i] = SDLK_LSHIFT;
slouken@1662
  1283
            break;
slouken@1662
  1284
        case K_SHIFTR:
slouken@1662
  1285
            keymap[i] = SDLK_RSHIFT;
slouken@1662
  1286
            break;
slouken@1662
  1287
        case K_CTRL:
slouken@1662
  1288
            if (keymap[i] != SDLK_RCTRL)
slouken@1662
  1289
                keymap[i] = SDLK_LCTRL;
slouken@1662
  1290
            break;
slouken@1662
  1291
        case K_CTRLL:
slouken@1662
  1292
            keymap[i] = SDLK_LCTRL;
slouken@1662
  1293
            break;
slouken@1662
  1294
        case K_CTRLR:
slouken@1662
  1295
            keymap[i] = SDLK_RCTRL;
slouken@1662
  1296
            break;
slouken@1662
  1297
        case K_ALT:
slouken@1662
  1298
            keymap[i] = SDLK_LALT;
slouken@1662
  1299
            break;
slouken@1662
  1300
        case K_ALTGR:
slouken@1662
  1301
            keymap[i] = SDLK_RALT;
slouken@1662
  1302
            break;
slouken@0
  1303
slouken@1662
  1304
        case K_INSERT:
slouken@1662
  1305
            keymap[i] = SDLK_INSERT;
slouken@1662
  1306
            break;
slouken@1662
  1307
        case K_REMOVE:
slouken@1662
  1308
            keymap[i] = SDLK_DELETE;
slouken@1662
  1309
            break;
slouken@1662
  1310
        case K_PGUP:
slouken@1662
  1311
            keymap[i] = SDLK_PAGEUP;
slouken@1662
  1312
            break;
slouken@1662
  1313
        case K_PGDN:
slouken@1662
  1314
            keymap[i] = SDLK_PAGEDOWN;
slouken@1662
  1315
            break;
slouken@1662
  1316
        case K_FIND:
slouken@1662
  1317
            keymap[i] = SDLK_HOME;
slouken@1662
  1318
            break;
slouken@1662
  1319
        case K_SELECT:
slouken@1662
  1320
            keymap[i] = SDLK_END;
slouken@1662
  1321
            break;
slouken@0
  1322
slouken@1662
  1323
        case K_NUM:
slouken@1662
  1324
            keymap[i] = SDLK_NUMLOCK;
slouken@1662
  1325
            break;
slouken@1662
  1326
        case K_CAPS:
slouken@1662
  1327
            keymap[i] = SDLK_CAPSLOCK;
slouken@1662
  1328
            break;
slouken@0
  1329
slouken@1662
  1330
        case K_F13:
slouken@1662
  1331
            keymap[i] = SDLK_PRINT;
slouken@1662
  1332
            break;
slouken@1662
  1333
        case K_HOLD:
slouken@1662
  1334
            keymap[i] = SDLK_SCROLLOCK;
slouken@1662
  1335
            break;
slouken@1662
  1336
        case K_PAUSE:
slouken@1662
  1337
            keymap[i] = SDLK_PAUSE;
slouken@1662
  1338
            break;
slouken@0
  1339
slouken@1662
  1340
        case 127:
slouken@1662
  1341
            keymap[i] = SDLK_BACKSPACE;
slouken@1662
  1342
            break;
slouken@1662
  1343
slouken@1662
  1344
        default:
slouken@1662
  1345
            break;
slouken@1662
  1346
        }
slouken@1662
  1347
    }
slouken@0
  1348
}
slouken@0
  1349
slouken@1662
  1350
static SDL_keysym *
slouken@1662
  1351
TranslateKey (int scancode, SDL_keysym * keysym)
slouken@0
  1352
{
slouken@1662
  1353
    /* Set the keysym information */
slouken@1662
  1354
    keysym->scancode = scancode;
slouken@1662
  1355
    keysym->sym = keymap[scancode];
slouken@1662
  1356
    keysym->mod = KMOD_NONE;
slouken@0
  1357
slouken@1662
  1358
    /* If UNICODE is on, get the UNICODE value for the key */
slouken@1662
  1359
    keysym->unicode = 0;
slouken@1662
  1360
    if (SDL_TranslateUNICODE) {
slouken@1662
  1361
        int map;
slouken@1662
  1362
        SDLMod modstate;
slouken@0
  1363
slouken@1662
  1364
        modstate = SDL_GetModState ();
slouken@1662
  1365
        map = 0;
slouken@1662
  1366
        if (modstate & KMOD_SHIFT) {
slouken@1662
  1367
            map |= (1 << KG_SHIFT);
slouken@1662
  1368
        }
slouken@1662
  1369
        if (modstate & KMOD_CTRL) {
slouken@1662
  1370
            map |= (1 << KG_CTRL);
slouken@1662
  1371
        }
slouken@1662
  1372
        if (modstate & KMOD_LALT) {
slouken@1662
  1373
            map |= (1 << KG_ALT);
slouken@1662
  1374
        }
slouken@1662
  1375
        if (modstate & KMOD_RALT) {
slouken@1662
  1376
            map |= (1 << KG_ALTGR);
slouken@1662
  1377
        }
slouken@1662
  1378
        if (KTYP (vga_keymap[map][scancode]) == KT_LETTER) {
slouken@1662
  1379
            if (modstate & KMOD_CAPS) {
slouken@1662
  1380
                map ^= (1 << KG_SHIFT);
slouken@1662
  1381
            }
slouken@1662
  1382
        }
slouken@1662
  1383
        if (KTYP (vga_keymap[map][scancode]) == KT_PAD) {
slouken@1662
  1384
            if (modstate & KMOD_NUM) {
slouken@1662
  1385
                keysym->unicode = KVAL (vga_keymap[map][scancode]);
slouken@1662
  1386
            }
slouken@1662
  1387
        } else {
slouken@1662
  1388
            keysym->unicode = KVAL (vga_keymap[map][scancode]);
slouken@1662
  1389
        }
slouken@1662
  1390
    }
slouken@1662
  1391
    return (keysym);
slouken@0
  1392
}
slouken@1662
  1393
slouken@1662
  1394
/* vi: set ts=4 sw=4 expandtab: */