src/video/x11/SDL_x11keyboard.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Dec 2009 08:11:06 +0000
changeset 3565 f43c8f688f77
parent 3162 dc1eb82ffdaa
child 3667 cc7eca3a4e13
permissions -rw-r--r--
Fixed bug #906

Added better error reporting for OpenGL context creation failing.
     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 "SDL_x11video.h"
    25 
    26 #include "../../events/SDL_keyboard_c.h"
    27 #include "../../events/scancodes_darwin.h"
    28 #include "../../events/scancodes_xfree86.h"
    29 
    30 #include <X11/keysym.h>
    31 
    32 #include "imKStoUCS.h"
    33 
    34 /* *INDENT-OFF* */
    35 static const struct {
    36     KeySym keysym;
    37     SDLKey sdlkey;
    38 } KeySymToSDLKey[] = {
    39     { XK_Return, SDLK_RETURN },
    40     { XK_Escape, SDLK_ESCAPE },
    41     { XK_BackSpace, SDLK_BACKSPACE },
    42     { XK_Tab, SDLK_TAB },
    43     { XK_Caps_Lock, SDLK_CAPSLOCK },
    44     { XK_F1, SDLK_F1 },
    45     { XK_F2, SDLK_F2 },
    46     { XK_F3, SDLK_F3 },
    47     { XK_F4, SDLK_F4 },
    48     { XK_F5, SDLK_F5 },
    49     { XK_F6, SDLK_F6 },
    50     { XK_F7, SDLK_F7 },
    51     { XK_F8, SDLK_F8 },
    52     { XK_F9, SDLK_F9 },
    53     { XK_F10, SDLK_F10 },
    54     { XK_F11, SDLK_F11 },
    55     { XK_F12, SDLK_F12 },
    56     { XK_Print, SDLK_PRINTSCREEN },
    57     { XK_Scroll_Lock, SDLK_SCROLLLOCK },
    58     { XK_Pause, SDLK_PAUSE },
    59     { XK_Insert, SDLK_INSERT },
    60     { XK_Home, SDLK_HOME },
    61     { XK_Prior, SDLK_PAGEUP },
    62     { XK_Delete, SDLK_DELETE },
    63     { XK_End, SDLK_END },
    64     { XK_Next, SDLK_PAGEDOWN },
    65     { XK_Right, SDLK_RIGHT },
    66     { XK_Left, SDLK_LEFT },
    67     { XK_Down, SDLK_DOWN },
    68     { XK_Up, SDLK_UP },
    69     { XK_Num_Lock, SDLK_NUMLOCKCLEAR },
    70     { XK_KP_Divide, SDLK_KP_DIVIDE },
    71     { XK_KP_Multiply, SDLK_KP_MULTIPLY },
    72     { XK_KP_Subtract, SDLK_KP_MINUS },
    73     { XK_KP_Add, SDLK_KP_PLUS },
    74     { XK_KP_Enter, SDLK_KP_ENTER },
    75     { XK_KP_Delete, SDLK_KP_PERIOD },
    76     { XK_KP_End, SDLK_KP_1 },
    77     { XK_KP_Down, SDLK_KP_2 },
    78     { XK_KP_Next, SDLK_KP_3 },
    79     { XK_KP_Left, SDLK_KP_4 },
    80     { XK_KP_Begin, SDLK_KP_5 },
    81     { XK_KP_Right, SDLK_KP_6 },
    82     { XK_KP_Home, SDLK_KP_7 },
    83     { XK_KP_Up, SDLK_KP_8 },
    84     { XK_KP_Prior, SDLK_KP_9 },
    85     { XK_KP_Insert, SDLK_KP_0 },
    86     { XK_KP_Decimal, SDLK_KP_PERIOD },
    87     { XK_KP_1, SDLK_KP_1 },
    88     { XK_KP_2, SDLK_KP_2 },
    89     { XK_KP_3, SDLK_KP_3 },
    90     { XK_KP_4, SDLK_KP_4 },
    91     { XK_KP_5, SDLK_KP_5 },
    92     { XK_KP_6, SDLK_KP_6 },
    93     { XK_KP_7, SDLK_KP_7 },
    94     { XK_KP_8, SDLK_KP_8 },
    95     { XK_KP_9, SDLK_KP_9 },
    96     { XK_KP_0, SDLK_KP_0 },
    97     { XK_KP_Decimal, SDLK_KP_PERIOD },
    98     { XK_Hyper_R, SDLK_APPLICATION },
    99     { XK_KP_Equal, SDLK_KP_EQUALS },
   100     { XK_F13, SDLK_F13 },
   101     { XK_F14, SDLK_F14 },
   102     { XK_F15, SDLK_F15 },
   103     { XK_F16, SDLK_F16 },
   104     { XK_F17, SDLK_F17 },
   105     { XK_F18, SDLK_F18 },
   106     { XK_F19, SDLK_F19 },
   107     { XK_F20, SDLK_F20 },
   108     { XK_F21, SDLK_F21 },
   109     { XK_F22, SDLK_F22 },
   110     { XK_F23, SDLK_F23 },
   111     { XK_F24, SDLK_F24 },
   112     { XK_Execute, SDLK_EXECUTE },
   113     { XK_Help, SDLK_HELP },
   114     { XK_Menu, SDLK_MENU },
   115     { XK_Select, SDLK_SELECT },
   116     { XK_Cancel, SDLK_STOP },
   117     { XK_Redo, SDLK_AGAIN },
   118     { XK_Undo, SDLK_UNDO },
   119     { XK_Find, SDLK_FIND },
   120     { XK_KP_Separator, SDLK_KP_COMMA },
   121     { XK_Sys_Req, SDLK_SYSREQ },
   122     { XK_Control_L, SDLK_LCTRL },
   123     { XK_Shift_L, SDLK_LSHIFT },
   124     { XK_Alt_L, SDLK_LALT },
   125     { XK_Meta_L, SDLK_LGUI },
   126     { XK_Super_L, SDLK_LGUI },
   127     { XK_Control_R, SDLK_RCTRL },
   128     { XK_Shift_R, SDLK_RSHIFT },
   129     { XK_Alt_R, SDLK_RALT },
   130     { XK_Meta_R, SDLK_RGUI },
   131     { XK_Super_R, SDLK_RGUI },
   132     { XK_Mode_switch, SDLK_MODE },
   133 };
   134 
   135 static const struct
   136 {
   137     const SDL_scancode const *table;
   138     int table_size;
   139 } scancode_set[] = {
   140     { darwin_scancode_table, SDL_arraysize(darwin_scancode_table) },
   141     { xfree86_scancode_table, SDL_arraysize(xfree86_scancode_table) },
   142     { xfree86_scancode_table2, SDL_arraysize(xfree86_scancode_table2) },
   143 };
   144 /* *INDENT-OFF* */
   145 
   146 static SDLKey
   147 X11_KeyCodeToSDLKey(Display *display, KeyCode keycode)
   148 {
   149     KeySym keysym;
   150     unsigned int ucs4;
   151     int i;
   152 
   153     keysym = XKeycodeToKeysym(display, keycode, 0);
   154     if (keysym == NoSymbol) {
   155         return SDLK_UNKNOWN;
   156     }
   157 
   158     ucs4 = X11_KeySymToUcs4(keysym);
   159     if (ucs4) {
   160         return (SDLKey) ucs4;
   161     }
   162 
   163     for (i = 0; i < SDL_arraysize(KeySymToSDLKey); ++i) {
   164         if (keysym == KeySymToSDLKey[i].keysym) {
   165             return KeySymToSDLKey[i].sdlkey;
   166         }
   167     }
   168     return SDLK_UNKNOWN;
   169 }
   170 
   171 int
   172 X11_InitKeyboard(_THIS)
   173 {
   174     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   175     SDL_Keyboard keyboard;
   176     int i, j;
   177     int min_keycode, max_keycode;
   178     struct {
   179         SDL_scancode scancode;
   180         KeySym keysym;
   181         int value;
   182     } fingerprint[] = {
   183         { SDL_SCANCODE_HOME, XK_Home, 0 },
   184         { SDL_SCANCODE_PAGEUP, XK_Prior, 0 },
   185         { SDL_SCANCODE_PAGEDOWN, XK_Next, 0 },
   186     };
   187     SDL_bool fingerprint_detected;
   188 
   189     XAutoRepeatOn(data->display);
   190 
   191     /* Try to determine which scancodes are being used based on fingerprint */
   192     fingerprint_detected = SDL_FALSE;
   193     XDisplayKeycodes(data->display, &min_keycode, &max_keycode);
   194     for (i = 0; i < SDL_arraysize(fingerprint); ++i) {
   195         fingerprint[i].value =
   196             XKeysymToKeycode(data->display, fingerprint[i].keysym) -
   197             min_keycode;
   198     }
   199     for (i = 0; i < SDL_arraysize(scancode_set); ++i) {
   200         /* Make sure the scancode set isn't too big */
   201         if ((max_keycode - min_keycode + 1) <= scancode_set[i].table_size) {
   202             continue;
   203         }
   204         for (j = 0; j < SDL_arraysize(fingerprint); ++j) {
   205             if (fingerprint[j].value < 0
   206                 || fingerprint[j].value >= scancode_set[i].table_size) {
   207                 break;
   208             }
   209             if (scancode_set[i].table[fingerprint[j].value] !=
   210                 fingerprint[j].scancode) {
   211                 break;
   212             }
   213         }
   214         if (j == SDL_arraysize(fingerprint)) {
   215             /* printf("Using scancode set %d\n", i); */
   216             SDL_memcpy(&data->key_layout[min_keycode], scancode_set[i].table,
   217                        sizeof(SDL_scancode) * scancode_set[i].table_size);
   218             fingerprint_detected = SDL_TRUE;
   219             break;
   220         }
   221     }
   222 
   223     if (!fingerprint_detected) {
   224         SDLKey keymap[SDL_NUM_SCANCODES];
   225 
   226         printf
   227             ("Keyboard layout unknown, please send the following to the SDL mailing list (sdl@libsdl.org):\n");
   228 
   229         /* Determine key_layout - only works on US QWERTY layout */
   230         SDL_GetDefaultKeymap(keymap);
   231         for (i = min_keycode; i <= max_keycode; ++i) {
   232             KeySym sym;
   233             sym = XKeycodeToKeysym(data->display, i, 0);
   234             if (sym != NoSymbol) {
   235                 SDLKey key;
   236                 printf("code = %d, sym = 0x%X (%s) ", i - min_keycode, sym,
   237                        XKeysymToString(sym));
   238                 key = X11_KeyCodeToSDLKey(data->display, i);
   239                 for (j = 0; j < SDL_arraysize(keymap); ++j) {
   240                     if (keymap[j] == key) {
   241                         data->key_layout[i] = (SDL_scancode) j;
   242                         break;
   243                     }
   244                 }
   245                 if (j == SDL_arraysize(keymap)) {
   246                     printf("scancode not found\n");
   247                 } else {
   248                     printf("scancode = %d (%s)\n", j, SDL_GetScancodeName(j));
   249                 }
   250             }
   251         }
   252     }
   253 
   254     SDL_zero(keyboard);
   255     data->keyboard = SDL_AddKeyboard(&keyboard, -1);
   256     X11_UpdateKeymap(_this);
   257 
   258     SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
   259 
   260     return 0;
   261 }
   262 
   263 void
   264 X11_UpdateKeymap(_THIS)
   265 {
   266     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   267     int i;
   268     SDL_scancode scancode;
   269     SDLKey keymap[SDL_NUM_SCANCODES];
   270 
   271     SDL_zero(keymap);
   272 
   273     for (i = 0; i < SDL_arraysize(data->key_layout); i++) {
   274 
   275         /* Make sure this is a valid scancode */
   276         scancode = data->key_layout[i];
   277         if (scancode == SDL_SCANCODE_UNKNOWN) {
   278             continue;
   279         }
   280 
   281         keymap[scancode] = X11_KeyCodeToSDLKey(data->display, (KeyCode)i);
   282     }
   283     SDL_SetKeymap(data->keyboard, 0, keymap, SDL_NUM_SCANCODES);
   284 }
   285 
   286 void
   287 X11_QuitKeyboard(_THIS)
   288 {
   289     SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
   290 
   291     SDL_DelKeyboard(data->keyboard);
   292 }
   293 
   294 /* vi: set ts=4 sw=4 expandtab: */