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