src/video/x11/SDL_x11keyboard.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 07 Oct 2016 18:57:40 -0700
changeset 10496 6660aa9120d6
parent 10494 c54de4000f9f
child 10548 aea47b61c640
permissions -rw-r--r--
Fixed bug 2824 - Add Fcitx Input Method Support

Weitian Leung

Just moved ibus direct call to SDL_IME_* related functions, and adds fcitx IME support (uses DBus, too),
enable with env: SDL_IM_MODULE=fcitx (ibus still the default one)
slouken@1950
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@1950
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1950
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1950
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1950
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@1950
    22
slouken@5481
    23
#if SDL_VIDEO_DRIVER_X11
slouken@5481
    24
slouken@1950
    25
#include "SDL_x11video.h"
slouken@1950
    26
slouken@1950
    27
#include "../../events/SDL_keyboard_c.h"
slouken@2305
    28
#include "../../events/scancodes_darwin.h"
slouken@2305
    29
#include "../../events/scancodes_xfree86.h"
slouken@1950
    30
bob@2295
    31
#include <X11/keysym.h>
jwyatt@9842
    32
#include <X11/XKBlib.h>
bob@2295
    33
bob@2295
    34
#include "imKStoUCS.h"
bob@2295
    35
slouken@3001
    36
/* *INDENT-OFF* */
slouken@3162
    37
static const struct {
slouken@3001
    38
    KeySym keysym;
slouken@7580
    39
    SDL_Scancode scancode;
slouken@7580
    40
} KeySymToSDLScancode[] = {
slouken@7580
    41
    { XK_Return, SDL_SCANCODE_RETURN },
slouken@7580
    42
    { XK_Escape, SDL_SCANCODE_ESCAPE },
slouken@7580
    43
    { XK_BackSpace, SDL_SCANCODE_BACKSPACE },
slouken@7580
    44
    { XK_Tab, SDL_SCANCODE_TAB },
slouken@7580
    45
    { XK_Caps_Lock, SDL_SCANCODE_CAPSLOCK },
slouken@7580
    46
    { XK_F1, SDL_SCANCODE_F1 },
slouken@7580
    47
    { XK_F2, SDL_SCANCODE_F2 },
slouken@7580
    48
    { XK_F3, SDL_SCANCODE_F3 },
slouken@7580
    49
    { XK_F4, SDL_SCANCODE_F4 },
slouken@7580
    50
    { XK_F5, SDL_SCANCODE_F5 },
slouken@7580
    51
    { XK_F6, SDL_SCANCODE_F6 },
slouken@7580
    52
    { XK_F7, SDL_SCANCODE_F7 },
slouken@7580
    53
    { XK_F8, SDL_SCANCODE_F8 },
slouken@7580
    54
    { XK_F9, SDL_SCANCODE_F9 },
slouken@7580
    55
    { XK_F10, SDL_SCANCODE_F10 },
slouken@7580
    56
    { XK_F11, SDL_SCANCODE_F11 },
slouken@7580
    57
    { XK_F12, SDL_SCANCODE_F12 },
slouken@7580
    58
    { XK_Print, SDL_SCANCODE_PRINTSCREEN },
slouken@7580
    59
    { XK_Scroll_Lock, SDL_SCANCODE_SCROLLLOCK },
slouken@7580
    60
    { XK_Pause, SDL_SCANCODE_PAUSE },
slouken@7580
    61
    { XK_Insert, SDL_SCANCODE_INSERT },
slouken@7580
    62
    { XK_Home, SDL_SCANCODE_HOME },
slouken@7580
    63
    { XK_Prior, SDL_SCANCODE_PAGEUP },
slouken@7580
    64
    { XK_Delete, SDL_SCANCODE_DELETE },
slouken@7580
    65
    { XK_End, SDL_SCANCODE_END },
slouken@7580
    66
    { XK_Next, SDL_SCANCODE_PAGEDOWN },
slouken@7580
    67
    { XK_Right, SDL_SCANCODE_RIGHT },
slouken@7580
    68
    { XK_Left, SDL_SCANCODE_LEFT },
slouken@7580
    69
    { XK_Down, SDL_SCANCODE_DOWN },
slouken@7580
    70
    { XK_Up, SDL_SCANCODE_UP },
slouken@7580
    71
    { XK_Num_Lock, SDL_SCANCODE_NUMLOCKCLEAR },
slouken@7580
    72
    { XK_KP_Divide, SDL_SCANCODE_KP_DIVIDE },
slouken@7580
    73
    { XK_KP_Multiply, SDL_SCANCODE_KP_MULTIPLY },
slouken@7580
    74
    { XK_KP_Subtract, SDL_SCANCODE_KP_MINUS },
slouken@7580
    75
    { XK_KP_Add, SDL_SCANCODE_KP_PLUS },
slouken@7580
    76
    { XK_KP_Enter, SDL_SCANCODE_KP_ENTER },
slouken@7580
    77
    { XK_KP_Delete, SDL_SCANCODE_KP_PERIOD },
slouken@7580
    78
    { XK_KP_End, SDL_SCANCODE_KP_1 },
slouken@7580
    79
    { XK_KP_Down, SDL_SCANCODE_KP_2 },
slouken@7580
    80
    { XK_KP_Next, SDL_SCANCODE_KP_3 },
slouken@7580
    81
    { XK_KP_Left, SDL_SCANCODE_KP_4 },
slouken@7580
    82
    { XK_KP_Begin, SDL_SCANCODE_KP_5 },
slouken@7580
    83
    { XK_KP_Right, SDL_SCANCODE_KP_6 },
slouken@7580
    84
    { XK_KP_Home, SDL_SCANCODE_KP_7 },
slouken@7580
    85
    { XK_KP_Up, SDL_SCANCODE_KP_8 },
slouken@7580
    86
    { XK_KP_Prior, SDL_SCANCODE_KP_9 },
slouken@7580
    87
    { XK_KP_Insert, SDL_SCANCODE_KP_0 },
slouken@7580
    88
    { XK_KP_Decimal, SDL_SCANCODE_KP_PERIOD },
slouken@7580
    89
    { XK_KP_1, SDL_SCANCODE_KP_1 },
slouken@7580
    90
    { XK_KP_2, SDL_SCANCODE_KP_2 },
slouken@7580
    91
    { XK_KP_3, SDL_SCANCODE_KP_3 },
slouken@7580
    92
    { XK_KP_4, SDL_SCANCODE_KP_4 },
slouken@7580
    93
    { XK_KP_5, SDL_SCANCODE_KP_5 },
slouken@7580
    94
    { XK_KP_6, SDL_SCANCODE_KP_6 },
slouken@7580
    95
    { XK_KP_7, SDL_SCANCODE_KP_7 },
slouken@7580
    96
    { XK_KP_8, SDL_SCANCODE_KP_8 },
slouken@7580
    97
    { XK_KP_9, SDL_SCANCODE_KP_9 },
slouken@7580
    98
    { XK_KP_0, SDL_SCANCODE_KP_0 },
slouken@7580
    99
    { XK_KP_Decimal, SDL_SCANCODE_KP_PERIOD },
slouken@7580
   100
    { XK_Hyper_R, SDL_SCANCODE_APPLICATION },
slouken@7580
   101
    { XK_KP_Equal, SDL_SCANCODE_KP_EQUALS },
slouken@7580
   102
    { XK_F13, SDL_SCANCODE_F13 },
slouken@7580
   103
    { XK_F14, SDL_SCANCODE_F14 },
slouken@7580
   104
    { XK_F15, SDL_SCANCODE_F15 },
slouken@7580
   105
    { XK_F16, SDL_SCANCODE_F16 },
slouken@7580
   106
    { XK_F17, SDL_SCANCODE_F17 },
slouken@7580
   107
    { XK_F18, SDL_SCANCODE_F18 },
slouken@7580
   108
    { XK_F19, SDL_SCANCODE_F19 },
slouken@7580
   109
    { XK_F20, SDL_SCANCODE_F20 },
slouken@7580
   110
    { XK_F21, SDL_SCANCODE_F21 },
slouken@7580
   111
    { XK_F22, SDL_SCANCODE_F22 },
slouken@7580
   112
    { XK_F23, SDL_SCANCODE_F23 },
slouken@7580
   113
    { XK_F24, SDL_SCANCODE_F24 },
slouken@7580
   114
    { XK_Execute, SDL_SCANCODE_EXECUTE },
slouken@7580
   115
    { XK_Help, SDL_SCANCODE_HELP },
slouken@7580
   116
    { XK_Menu, SDL_SCANCODE_MENU },
slouken@7580
   117
    { XK_Select, SDL_SCANCODE_SELECT },
slouken@7580
   118
    { XK_Cancel, SDL_SCANCODE_STOP },
slouken@7580
   119
    { XK_Redo, SDL_SCANCODE_AGAIN },
slouken@7580
   120
    { XK_Undo, SDL_SCANCODE_UNDO },
slouken@7580
   121
    { XK_Find, SDL_SCANCODE_FIND },
slouken@7580
   122
    { XK_KP_Separator, SDL_SCANCODE_KP_COMMA },
slouken@7580
   123
    { XK_Sys_Req, SDL_SCANCODE_SYSREQ },
slouken@7580
   124
    { XK_Control_L, SDL_SCANCODE_LCTRL },
slouken@7580
   125
    { XK_Shift_L, SDL_SCANCODE_LSHIFT },
slouken@7580
   126
    { XK_Alt_L, SDL_SCANCODE_LALT },
slouken@7580
   127
    { XK_Meta_L, SDL_SCANCODE_LGUI },
slouken@7580
   128
    { XK_Super_L, SDL_SCANCODE_LGUI },
slouken@7580
   129
    { XK_Control_R, SDL_SCANCODE_RCTRL },
slouken@7580
   130
    { XK_Shift_R, SDL_SCANCODE_RSHIFT },
slouken@7580
   131
    { XK_Alt_R, SDL_SCANCODE_RALT },
slime73@10059
   132
    { XK_ISO_Level3_Shift, SDL_SCANCODE_RALT },
slouken@7580
   133
    { XK_Meta_R, SDL_SCANCODE_RGUI },
slouken@7580
   134
    { XK_Super_R, SDL_SCANCODE_RGUI },
slouken@7580
   135
    { XK_Mode_switch, SDL_SCANCODE_MODE },
william@10493
   136
    { XK_period, SDL_SCANCODE_PERIOD },
william@10493
   137
    { XK_comma, SDL_SCANCODE_COMMA },
william@10493
   138
    { XK_slash, SDL_SCANCODE_SLASH },
william@10493
   139
    { XK_backslash, SDL_SCANCODE_BACKSLASH },
william@10493
   140
    { XK_minus, SDL_SCANCODE_MINUS },
william@10493
   141
    { XK_equal, SDL_SCANCODE_EQUALS },
william@10493
   142
    { XK_space, SDL_SCANCODE_SPACE },
william@10493
   143
    { XK_grave, SDL_SCANCODE_GRAVE },
william@10493
   144
    { XK_apostrophe, SDL_SCANCODE_APOSTROPHE },
william@10493
   145
    { XK_bracketleft, SDL_SCANCODE_LEFTBRACKET },
william@10493
   146
    { XK_bracketright, SDL_SCANCODE_RIGHTBRACKET },
bob@2295
   147
};
bob@2295
   148
slouken@3162
   149
static const struct
bob@2295
   150
{
slouken@7882
   151
    SDL_Scancode const *table;
slouken@2305
   152
    int table_size;
slouken@2305
   153
} scancode_set[] = {
slouken@2308
   154
    { darwin_scancode_table, SDL_arraysize(darwin_scancode_table) },
slouken@2308
   155
    { xfree86_scancode_table, SDL_arraysize(xfree86_scancode_table) },
slouken@2825
   156
    { xfree86_scancode_table2, SDL_arraysize(xfree86_scancode_table2) },
alex@10494
   157
    { xvnc_scancode_table, SDL_arraysize(xvnc_scancode_table) },
slouken@2308
   158
};
slouken@2308
   159
/* *INDENT-OFF* */
bob@2295
   160
slouken@7580
   161
/* This function only works for keyboards in US QWERTY layout */
slouken@7580
   162
static SDL_Scancode
alex@10494
   163
X11_KeyCodeToSDLScancode(_THIS, KeyCode keycode)
slouken@3001
   164
{
slouken@3001
   165
    KeySym keysym;
slouken@3001
   166
    int i;
slouken@3001
   167
alex@10494
   168
    keysym = X11_KeyCodeToSym(_this, keycode, 0);
slouken@3001
   169
    if (keysym == NoSymbol) {
slouken@7580
   170
        return SDL_SCANCODE_UNKNOWN;
slouken@3001
   171
    }
slouken@3001
   172
william@10493
   173
    if (keysym >= XK_a && keysym <= XK_z) {
william@10493
   174
        return SDL_SCANCODE_A + (keysym - XK_a);
william@10493
   175
    }
slouken@7580
   176
    if (keysym >= XK_A && keysym <= XK_Z) {
slouken@7580
   177
        return SDL_SCANCODE_A + (keysym - XK_A);
slouken@3001
   178
    }
slouken@3001
   179
william@10493
   180
    if (keysym == XK_0) {
william@10493
   181
        return SDL_SCANCODE_0;
william@10493
   182
    }
william@10493
   183
    if (keysym >= XK_1 && keysym <= XK_9) {
william@10493
   184
        return SDL_SCANCODE_1 + (keysym - XK_1);
slouken@7580
   185
    }
slouken@7580
   186
slouken@7580
   187
    for (i = 0; i < SDL_arraysize(KeySymToSDLScancode); ++i) {
slouken@7580
   188
        if (keysym == KeySymToSDLScancode[i].keysym) {
slouken@7580
   189
            return KeySymToSDLScancode[i].scancode;
slouken@3001
   190
        }
slouken@3001
   191
    }
slouken@7580
   192
    return SDL_SCANCODE_UNKNOWN;
slouken@7580
   193
}
slouken@7580
   194
slouken@7580
   195
static Uint32
alex@10494
   196
X11_KeyCodeToUcs4(_THIS, KeyCode keycode, unsigned char group)
slouken@7580
   197
{
alex@10494
   198
    KeySym keysym = X11_KeyCodeToSym(_this, keycode, group);
alex@10494
   199
alex@10494
   200
    if (keysym == NoSymbol) {
alex@10494
   201
        return 0;
alex@10494
   202
    }
alex@10494
   203
alex@10494
   204
    return X11_KeySymToUcs4(keysym);
alex@10494
   205
}
alex@10494
   206
alex@10494
   207
KeySym
alex@10494
   208
X11_KeyCodeToSym(_THIS, KeyCode keycode, unsigned char group)
alex@10494
   209
{
alex@10494
   210
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@7580
   211
    KeySym keysym;
slouken@7580
   212
slouken@7580
   213
#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
slouken@10439
   214
    if (data->xkb) {
slouken@10439
   215
        int num_groups     = XkbKeyNumGroups(data->xkb, keycode);
slouken@10439
   216
        unsigned char info = XkbKeyGroupInfo(data->xkb, keycode);
slouken@10439
   217
        
slouken@10439
   218
        if (num_groups && group >= num_groups) {
slouken@10439
   219
        
slouken@10439
   220
            int action = XkbOutOfRangeGroupAction(info);
slouken@10439
   221
            
slouken@10439
   222
            if (action == XkbRedirectIntoRange) {
slouken@10439
   223
                if ((group = XkbOutOfRangeGroupNumber(info)) >= num_groups) {
slouken@10439
   224
                    group = 0;
slouken@10439
   225
                }
slouken@10439
   226
            } else if (action == XkbClampIntoRange) {
slouken@10439
   227
                group = num_groups - 1;
slouken@10439
   228
            } else {
slouken@10439
   229
                group %= num_groups;
slouken@10439
   230
            }
slouken@10439
   231
        }
alex@10494
   232
        keysym = X11_XkbKeycodeToKeysym(data->display, keycode, group, 0);
slouken@10439
   233
    } else {
alex@10494
   234
        keysym = X11_XKeycodeToKeysym(data->display, keycode, 0);
slouken@10439
   235
    }
slouken@7580
   236
#else
slouken@10439
   237
    keysym = X11_XKeycodeToKeysym(data->display, keycode, 0);
slouken@7580
   238
#endif
slouken@7580
   239
alex@10494
   240
    return keysym;
slouken@3001
   241
}
slouken@3001
   242
bob@2295
   243
int
slouken@1950
   244
X11_InitKeyboard(_THIS)
slouken@1950
   245
{
slouken@1950
   246
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
icculus@7672
   247
    int i = 0;
icculus@7672
   248
    int j = 0;
slouken@2305
   249
    int min_keycode, max_keycode;
slouken@3001
   250
    struct {
slouken@5218
   251
        SDL_Scancode scancode;
slouken@3001
   252
        KeySym keysym;
slouken@3001
   253
        int value;
slouken@3001
   254
    } fingerprint[] = {
slouken@3001
   255
        { SDL_SCANCODE_HOME, XK_Home, 0 },
slouken@3001
   256
        { SDL_SCANCODE_PAGEUP, XK_Prior, 0 },
slouken@8667
   257
        { SDL_SCANCODE_UP, XK_Up, 0 },
slouken@8667
   258
        { SDL_SCANCODE_LEFT, XK_Left, 0 },
slouken@8667
   259
        { SDL_SCANCODE_DELETE, XK_Delete, 0 },
slouken@8667
   260
        { SDL_SCANCODE_KP_ENTER, XK_KP_Enter, 0 },
slouken@2305
   261
    };
slouken@8667
   262
    int best_distance;
slouken@8667
   263
    int best_index;
slouken@8667
   264
    int distance;
bob@2295
   265
icculus@7827
   266
    X11_XAutoRepeatOn(data->display);
bob@2299
   267
alex@10494
   268
#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
alex@10494
   269
    {
alex@10494
   270
	    int xkb_major = XkbMajorVersion;
alex@10494
   271
	    int xkb_minor = XkbMinorVersion;
alex@10494
   272
	    if (X11_XkbQueryExtension(data->display, NULL, NULL, NULL, &xkb_major, &xkb_minor)) {
alex@10494
   273
	        data->xkb = X11_XkbGetMap(data->display, XkbAllClientInfoMask, XkbUseCoreKbd);
alex@10494
   274
	    }
alex@10494
   275
	}
alex@10494
   276
#endif
alex@10494
   277
slouken@2305
   278
    /* Try to determine which scancodes are being used based on fingerprint */
slouken@8667
   279
    best_distance = SDL_arraysize(fingerprint) + 1;
slouken@8667
   280
    best_index = -1;
icculus@7827
   281
    X11_XDisplayKeycodes(data->display, &min_keycode, &max_keycode);
slouken@3001
   282
    for (i = 0; i < SDL_arraysize(fingerprint); ++i) {
slouken@3001
   283
        fingerprint[i].value =
icculus@7827
   284
            X11_XKeysymToKeycode(data->display, fingerprint[i].keysym) -
slouken@2305
   285
            min_keycode;
slouken@2305
   286
    }
slouken@2305
   287
    for (i = 0; i < SDL_arraysize(scancode_set); ++i) {
slouken@2305
   288
        /* Make sure the scancode set isn't too big */
slouken@2305
   289
        if ((max_keycode - min_keycode + 1) <= scancode_set[i].table_size) {
slouken@2305
   290
            continue;
slouken@2305
   291
        }
slouken@8667
   292
        distance = 0;
slouken@2305
   293
        for (j = 0; j < SDL_arraysize(fingerprint); ++j) {
slouken@3001
   294
            if (fingerprint[j].value < 0
slouken@3001
   295
                || fingerprint[j].value >= scancode_set[i].table_size) {
slouken@8667
   296
                distance += 1;
slouken@8667
   297
            } else if (scancode_set[i].table[fingerprint[j].value] != fingerprint[j].scancode) {
slouken@8667
   298
                distance += 1;
slouken@2305
   299
            }
slouken@2305
   300
        }
slouken@8667
   301
        if (distance < best_distance) {
slouken@8667
   302
            best_distance = distance;
slouken@8667
   303
            best_index = i;
slouken@2305
   304
        }
slouken@2305
   305
    }
slouken@8667
   306
    if (best_index >= 0 && best_distance <= 2) {
slouken@8667
   307
#ifdef DEBUG_KEYBOARD
slouken@8667
   308
        printf("Using scancode set %d, min_keycode = %d, max_keycode = %d, table_size = %d\n", best_index, min_keycode, max_keycode, scancode_set[best_index].table_size);
slouken@8667
   309
#endif
slouken@8667
   310
        SDL_memcpy(&data->key_layout[min_keycode], scancode_set[best_index].table,
slouken@8667
   311
                   sizeof(SDL_Scancode) * scancode_set[best_index].table_size);
slouken@9678
   312
    } else {
slouken@5220
   313
        SDL_Keycode keymap[SDL_NUM_SCANCODES];
slouken@3001
   314
slouken@2305
   315
        printf
slouken@2305
   316
            ("Keyboard layout unknown, please send the following to the SDL mailing list (sdl@libsdl.org):\n");
slouken@2305
   317
slouken@2305
   318
        /* Determine key_layout - only works on US QWERTY layout */
slouken@3001
   319
        SDL_GetDefaultKeymap(keymap);
slouken@2305
   320
        for (i = min_keycode; i <= max_keycode; ++i) {
slouken@2305
   321
            KeySym sym;
alex@10494
   322
            sym = X11_KeyCodeToSym(_this, (KeyCode) i, 0);
slouken@3001
   323
            if (sym != NoSymbol) {
slouken@7580
   324
                SDL_Scancode scancode;
icculus@3667
   325
                printf("code = %d, sym = 0x%X (%s) ", i - min_keycode,
icculus@7827
   326
                       (unsigned int) sym, X11_XKeysymToString(sym));
alex@10494
   327
                scancode = X11_KeyCodeToSDLScancode(_this, i);
slouken@7580
   328
                data->key_layout[i] = scancode;
slouken@7580
   329
                if (scancode == SDL_SCANCODE_UNKNOWN) {
slouken@2305
   330
                    printf("scancode not found\n");
slouken@2305
   331
                } else {
slouken@7824
   332
                    printf("scancode = %d (%s)\n", scancode, SDL_GetScancodeName(scancode));
slouken@2305
   333
                }
slouken@2305
   334
            }
slouken@2305
   335
        }
slouken@2305
   336
    }
slouken@1950
   337
slouken@2306
   338
    X11_UpdateKeymap(_this);
bob@2295
   339
slouken@2305
   340
    SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
bob@2295
   341
slouken@10496
   342
#ifdef SDL_USE_IME
slouken@10496
   343
    SDL_IME_Init();
alex@8889
   344
#endif
alex@8889
   345
bob@2295
   346
    return 0;
bob@2295
   347
}
bob@2295
   348
slouken@2305
   349
void
slouken@2305
   350
X11_UpdateKeymap(_THIS)
bob@2295
   351
{
bob@2295
   352
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@2305
   353
    int i;
slouken@5218
   354
    SDL_Scancode scancode;
slouken@5220
   355
    SDL_Keycode keymap[SDL_NUM_SCANCODES];
jwyatt@9842
   356
    unsigned char group = 0;
bob@2295
   357
slouken@7580
   358
    SDL_GetDefaultKeymap(keymap);
alex@10494
   359
jwyatt@9842
   360
#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
alex@10494
   361
    if (data->xkb) {
slouken@10444
   362
        XkbStateRec state;
alex@10494
   363
        X11_XkbGetUpdatedMap(data->display, XkbAllClientInfoMask, data->xkb);
slouken@10439
   364
icculus@9844
   365
        if (X11_XkbGetState(data->display, XkbUseCoreKbd, &state) == Success) {
icculus@9844
   366
            group = state.group;
icculus@9844
   367
        }
icculus@9843
   368
    }
jwyatt@9842
   369
#endif
jwyatt@9842
   370
jwyatt@9842
   371
slouken@2305
   372
    for (i = 0; i < SDL_arraysize(data->key_layout); i++) {
slouken@7580
   373
        Uint32 key;
slouken@2305
   374
slouken@3001
   375
        /* Make sure this is a valid scancode */
slouken@2305
   376
        scancode = data->key_layout[i];
slouken@3001
   377
        if (scancode == SDL_SCANCODE_UNKNOWN) {
slouken@2305
   378
            continue;
slouken@2305
   379
        }
slouken@2305
   380
slouken@7580
   381
        /* See if there is a UCS keycode for this scancode */
alex@10494
   382
        key = X11_KeyCodeToUcs4(_this, (KeyCode)i, group);
slouken@7580
   383
        if (key) {
slouken@7580
   384
            keymap[scancode] = key;
slouken@9678
   385
        } else {
alex@10494
   386
            SDL_Scancode keyScancode = X11_KeyCodeToSDLScancode(_this, (KeyCode)i);
slouken@9682
   387
slouken@9682
   388
            switch (keyScancode) {
slouken@9682
   389
                case SDL_SCANCODE_RETURN:
slouken@9682
   390
                    keymap[scancode] = SDLK_RETURN;
slouken@9682
   391
                    break;
slouken@9682
   392
                case SDL_SCANCODE_ESCAPE:
slouken@9682
   393
                    keymap[scancode] = SDLK_ESCAPE;
slouken@9682
   394
                    break;
slouken@9682
   395
                case SDL_SCANCODE_BACKSPACE:
slouken@9682
   396
                    keymap[scancode] = SDLK_BACKSPACE;
slouken@9682
   397
                    break;
slouken@9682
   398
                case SDL_SCANCODE_TAB:
slouken@9682
   399
                    keymap[scancode] = SDLK_TAB;
slouken@9682
   400
                    break;
slouken@9682
   401
                case SDL_SCANCODE_DELETE:
slouken@9682
   402
                    keymap[scancode] = SDLK_DELETE;
slouken@9682
   403
                    break;
slouken@9682
   404
                default:
slouken@9682
   405
                    keymap[scancode] = SDL_SCANCODE_TO_KEYCODE(keyScancode);
slouken@9682
   406
                    break;
slouken@9682
   407
            }
slouken@7580
   408
        }
bob@2295
   409
    }
slouken@4465
   410
    SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
slouken@1950
   411
}
slouken@1950
   412
slouken@1950
   413
void
slouken@1950
   414
X11_QuitKeyboard(_THIS)
slouken@1950
   415
{
slouken@10439
   416
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@10439
   417
slouken@10439
   418
#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
slouken@10439
   419
    if (data->xkb) {
slouken@10439
   420
        X11_XkbFreeClientMap(data->xkb, 0, True);
alex@10494
   421
        data->xkb = NULL;
slouken@10439
   422
    }
slouken@10439
   423
#endif
slouken@10439
   424
slouken@10496
   425
#ifdef SDL_USE_IME
slouken@10496
   426
    SDL_IME_Quit();
alex@8889
   427
#endif
alex@8889
   428
}
alex@8889
   429
alex@8889
   430
void
alex@8889
   431
X11_StartTextInput(_THIS)
alex@8889
   432
{
alex@9096
   433
alex@8889
   434
}
alex@8889
   435
alex@8889
   436
void
alex@8889
   437
X11_StopTextInput(_THIS)
alex@8889
   438
{
slouken@10496
   439
#ifdef SDL_USE_IME
slouken@10496
   440
    SDL_IME_Reset();
alex@8889
   441
#endif
alex@8889
   442
}
alex@8889
   443
alex@8889
   444
void
alex@8889
   445
X11_SetTextInputRect(_THIS, SDL_Rect *rect)
alex@8889
   446
{
alex@8889
   447
    if (!rect) {
alex@8889
   448
        SDL_InvalidParamError("rect");
alex@8889
   449
        return;
alex@8889
   450
    }
alex@8889
   451
       
slouken@10496
   452
#ifdef SDL_USE_IME
slouken@10496
   453
    SDL_IME_UpdateTextRect(rect);
alex@8889
   454
#endif
slouken@1950
   455
}
slouken@1950
   456
slouken@5481
   457
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@5481
   458
slouken@1950
   459
/* vi: set ts=4 sw=4 expandtab: */