src/video/wscons/SDL_wsconsevents.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Aug 2009 18:39:57 +0000
changeset 3227 458e53d8662c
parent 2985 4e4d814884aa
permissions -rw-r--r--
Clarified API documentation
icculus@1187
     1
/*
icculus@1187
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
icculus@1187
     4
icculus@1187
     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
icculus@1187
     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.
icculus@1187
     9
icculus@1187
    10
    This library is distributed in the hope that it will be useful,
icculus@1187
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1187
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
icculus@1187
    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
icculus@1187
    18
icculus@1187
    19
    Sam Lantinga
icculus@1187
    20
    slouken@libsdl.org
icculus@1187
    21
*/
slouken@1402
    22
#include "SDL_config.h"
icculus@1187
    23
icculus@1187
    24
#include <sys/types.h>
icculus@1187
    25
#include <dev/wscons/wsdisplay_usl_io.h>
icculus@1187
    26
#include <sys/ioctl.h>
slouken@1895
    27
#include <fcntl.h>
slouken@1895
    28
#include <unistd.h>
icculus@1187
    29
#include <termios.h>
slouken@1895
    30
#include <errno.h>
icculus@1187
    31
#include <string.h>
icculus@1187
    32
slouken@1361
    33
#include "../../events/SDL_sysevents.h"
slouken@1361
    34
#include "../../events/SDL_events_c.h"
icculus@1187
    35
#include "SDL_wsconsvideo.h"
icculus@1187
    36
#include "SDL_wsconsevents_c.h"
icculus@1187
    37
icculus@1187
    38
static int posted = 0;
icculus@1187
    39
slouken@1895
    40
int
slouken@1895
    41
WSCONS_InitKeyboard(_THIS)
icculus@1187
    42
{
slouken@1895
    43
    struct termios tty;
icculus@1187
    44
slouken@1895
    45
    if (ioctl(private->fd, WSKBDIO_GTYPE, &private->kbdType) == -1) {
slouken@1895
    46
        WSCONS_ReportError("cannot get keyboard type: %s", strerror(errno));
slouken@1895
    47
        return -1;
slouken@1895
    48
    }
icculus@1187
    49
slouken@1895
    50
    if (tcgetattr(private->fd, &private->saved_tty) == -1) {
slouken@1895
    51
        WSCONS_ReportError("cannot get terminal attributes: %s",
slouken@1895
    52
                           strerror(errno));
slouken@1895
    53
        return -1;
slouken@1895
    54
    }
slouken@1895
    55
    private->did_save_tty = 1;
slouken@1895
    56
    tty = private->saved_tty;
slouken@1895
    57
    tty.c_iflag = IGNPAR | IGNBRK;
slouken@1895
    58
    tty.c_oflag = 0;
slouken@1895
    59
    tty.c_cflag = CREAD | CS8;
slouken@1895
    60
    tty.c_lflag = 0;
slouken@1895
    61
    tty.c_cc[VTIME] = 0;
slouken@1895
    62
    tty.c_cc[VMIN] = 1;
slouken@1895
    63
    cfsetispeed(&tty, 9600);
slouken@1895
    64
    cfsetospeed(&tty, 9600);
slouken@1895
    65
    if (tcsetattr(private->fd, TCSANOW, &tty) < 0) {
slouken@1895
    66
        WSCONS_ReportError("cannot set terminal attributes: %s",
slouken@1895
    67
                           strerror(errno));
slouken@1895
    68
        return -1;
slouken@1895
    69
    }
slouken@1895
    70
    if (ioctl(private->fd, KDSKBMODE, K_RAW) == -1) {
slouken@1895
    71
        WSCONS_ReportError("cannot set raw keyboard mode: %s",
slouken@1895
    72
                           strerror(errno));
slouken@1895
    73
        return -1;
slouken@1895
    74
    }
icculus@1187
    75
slouken@1895
    76
    return 0;
icculus@1187
    77
}
icculus@1187
    78
slouken@1895
    79
void
slouken@1895
    80
WSCONS_ReleaseKeyboard(_THIS)
icculus@1187
    81
{
slouken@1895
    82
    if (private->fd != -1) {
slouken@1895
    83
        if (ioctl(private->fd, KDSKBMODE, K_XLATE) == -1) {
slouken@1895
    84
            WSCONS_ReportError
slouken@1895
    85
                ("cannot restore keyboard to translated mode: %s",
slouken@1895
    86
                 strerror(errno));
slouken@1895
    87
        }
slouken@1895
    88
        if (private->did_save_tty) {
slouken@1895
    89
            if (tcsetattr(private->fd, TCSANOW, &private->saved_tty) < 0) {
slouken@1895
    90
                WSCONS_ReportError
slouken@1895
    91
                    ("cannot restore keynoard attributes: %s",
slouken@1895
    92
                     strerror(errno));
slouken@1895
    93
            }
slouken@1895
    94
        }
icculus@1187
    95
    }
icculus@1187
    96
}
icculus@1187
    97
slouken@1895
    98
static void
slouken@1895
    99
updateMouse()
icculus@1187
   100
{
icculus@1187
   101
}
icculus@1187
   102
icculus@1187
   103
static SDLKey keymap[128];
icculus@1187
   104
slouken@1895
   105
static SDL_keysym *
slouken@1895
   106
TranslateKey(int scancode, SDL_keysym * keysym)
icculus@1187
   107
{
slouken@1895
   108
    keysym->scancode = scancode;
slouken@1895
   109
    keysym->sym = SDLK_UNKNOWN;
slouken@1895
   110
    keysym->mod = KMOD_NONE;
icculus@1187
   111
slouken@1895
   112
    if (scancode < SDL_arraysize(keymap))
slouken@1895
   113
        keysym->sym = keymap[scancode];
icculus@1187
   114
slouken@1895
   115
    if (keysym->sym == SDLK_UNKNOWN)
slouken@1895
   116
        printf("Unknown mapping for scancode %d\n", scancode);
icculus@1187
   117
slouken@1895
   118
    return keysym;
icculus@1187
   119
}
icculus@1187
   120
slouken@1895
   121
static void
slouken@1895
   122
updateKeyboard(_THIS)
icculus@1187
   123
{
slouken@1895
   124
    unsigned char buf[100];
slouken@1895
   125
    SDL_keysym keysym;
slouken@1895
   126
    int n, i;
icculus@1187
   127
slouken@1895
   128
    if ((n = read(private->fd, buf, sizeof(buf))) > 0) {
slouken@1895
   129
        for (i = 0; i < n; i++) {
slouken@1895
   130
            unsigned char c = buf[i] & 0x7f;
slouken@1895
   131
            if (c == 224)       // special key prefix -- what should we do with it?
slouken@1895
   132
                continue;
slouken@1895
   133
            posted +=
slouken@1895
   134
                SDL_PrivateKeyboard((buf[i] & 0x80) ? SDL_RELEASED :
slouken@1895
   135
                                    SDL_PRESSED, TranslateKey(c, &keysym));
slouken@1895
   136
        }
icculus@1187
   137
    }
icculus@1187
   138
}
icculus@1187
   139
slouken@1895
   140
void
slouken@1895
   141
WSCONS_PumpEvents(_THIS)
icculus@1187
   142
{
slouken@1895
   143
    do {
slouken@1895
   144
        posted = 0;
slouken@1895
   145
        updateMouse();
slouken@1895
   146
        updateKeyboard(this);
slouken@2735
   147
    } while (posted);
icculus@1187
   148
}
icculus@1187
   149
slouken@1895
   150
void
slouken@1895
   151
WSCONS_InitOSKeymap(_THIS)
icculus@1187
   152
{
slouken@1895
   153
    int i;
icculus@1187
   154
slouken@1895
   155
    /* Make sure unknown keys are mapped correctly */
slouken@1895
   156
    for (i = 0; i < SDL_arraysize(keymap); i++) {
slouken@1895
   157
        keymap[i] = SDLK_UNKNOWN;
slouken@1895
   158
    }
icculus@1187
   159
slouken@1895
   160
    switch (private->kbdType) {
slouken@1383
   161
#ifdef WSKBD_TYPE_ZAURUS
slouken@1895
   162
    case WSKBD_TYPE_ZAURUS:
slouken@1895
   163
        /* top row */
slouken@1895
   164
        keymap[2] = SDLK_1;
slouken@1895
   165
        keymap[3] = SDLK_2;
slouken@1895
   166
        keymap[4] = SDLK_3;
slouken@1895
   167
        keymap[5] = SDLK_4;
slouken@1895
   168
        keymap[6] = SDLK_5;
slouken@1895
   169
        keymap[7] = SDLK_6;
slouken@1895
   170
        keymap[8] = SDLK_7;
slouken@1895
   171
        keymap[9] = SDLK_8;
slouken@1895
   172
        keymap[10] = SDLK_9;
slouken@1895
   173
        keymap[11] = SDLK_0;
slouken@1895
   174
        keymap[14] = SDLK_BACKSPACE;
slouken@1895
   175
slouken@1895
   176
        /* second row */
slouken@1895
   177
        keymap[16] = SDLK_q;
slouken@1895
   178
        keymap[17] = SDLK_w;
slouken@1895
   179
        keymap[18] = SDLK_e;
slouken@1895
   180
        keymap[19] = SDLK_r;
slouken@1895
   181
        keymap[20] = SDLK_t;
slouken@1895
   182
        keymap[21] = SDLK_y;
slouken@1895
   183
        keymap[22] = SDLK_u;
slouken@1895
   184
        keymap[23] = SDLK_i;
slouken@1895
   185
        keymap[24] = SDLK_o;
slouken@1895
   186
        keymap[25] = SDLK_p;
icculus@1187
   187
slouken@1895
   188
        /* third row */
slouken@1895
   189
        keymap[15] = SDLK_TAB;
slouken@1895
   190
        keymap[30] = SDLK_a;
slouken@1895
   191
        keymap[31] = SDLK_s;
slouken@1895
   192
        keymap[32] = SDLK_d;
slouken@1895
   193
        keymap[33] = SDLK_f;
slouken@1895
   194
        keymap[34] = SDLK_g;
slouken@1895
   195
        keymap[35] = SDLK_h;
slouken@1895
   196
        keymap[36] = SDLK_j;
slouken@1895
   197
        keymap[37] = SDLK_k;
slouken@1895
   198
        keymap[38] = SDLK_l;
icculus@1187
   199
slouken@1895
   200
        /* fourth row */
slouken@1895
   201
        keymap[42] = SDLK_LSHIFT;
slouken@1895
   202
        keymap[44] = SDLK_z;
slouken@1895
   203
        keymap[45] = SDLK_x;
slouken@1895
   204
        keymap[46] = SDLK_c;
slouken@1895
   205
        keymap[47] = SDLK_v;
slouken@1895
   206
        keymap[48] = SDLK_b;
slouken@1895
   207
        keymap[49] = SDLK_n;
slouken@1895
   208
        keymap[50] = SDLK_m;
slouken@1895
   209
        keymap[54] = SDLK_RSHIFT;
slouken@1895
   210
        keymap[28] = SDLK_RETURN;
icculus@1187
   211
slouken@1895
   212
        /* fifth row */
slouken@1895
   213
        keymap[56] = SDLK_LALT;
slouken@1895
   214
        keymap[29] = SDLK_LCTRL;
slouken@1895
   215
        /* keymap[56] = ; */
slouken@1895
   216
        keymap[0] = SDLK_LSUPER;
slouken@1895
   217
        keymap[12] = SDLK_MINUS;
slouken@1895
   218
        keymap[57] = SDLK_SPACE;
slouken@1895
   219
        keymap[51] = SDLK_COMMA;
slouken@1895
   220
        keymap[52] = SDLK_PERIOD;
icculus@1187
   221
slouken@1895
   222
        /* misc */
slouken@1895
   223
        keymap[59] = SDLK_F1;
slouken@1895
   224
        keymap[60] = SDLK_F2;
slouken@1895
   225
        keymap[61] = SDLK_F3;
slouken@1895
   226
        keymap[62] = SDLK_F4;
slouken@1895
   227
        keymap[63] = SDLK_F5;
slouken@1895
   228
        keymap[1] = SDLK_ESCAPE;
slouken@1895
   229
        /* keymap[28] = SDLK_KP_ENTER; */
slouken@1895
   230
        keymap[72] = SDLK_UP;
slouken@1895
   231
        keymap[75] = SDLK_LEFT;
slouken@1895
   232
        keymap[77] = SDLK_RIGHT;
slouken@1895
   233
        keymap[80] = SDLK_DOWN;
slouken@1895
   234
        break;
slouken@1383
   235
#endif /* WSKBD_TYPE_ZAURUS */
icculus@1187
   236
slouken@1895
   237
    default:
slouken@1895
   238
        WSCONS_ReportError("Unable to map keys for keyboard type %u",
slouken@1895
   239
                           private->kbdType);
slouken@1895
   240
        break;
slouken@1895
   241
    }
icculus@1187
   242
}
icculus@1187
   243
icculus@1187
   244
/* end of SDL_wsconsevents.c ... */
slouken@1895
   245
/* vi: set ts=4 sw=4 expandtab: */