src/video/win32/SDL_win32keyboard.c
author dewyatt
Mon, 12 Jul 2010 11:33:27 -0400
changeset 4752 dc7bdcf06367
parent 4751 f3908cd80b10
child 4753 11b0a6a3eb4d
permissions -rw-r--r--
Initial IMM implementation.
IME input should now work fairly well.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#include "SDL_win32video.h"
slouken@1895
    25
slouken@1895
    26
#include "../../events/SDL_keyboard_c.h"
slouken@2308
    27
#include "../../events/scancodes_win32.h"
slouken@1895
    28
dewyatt@4752
    29
#include <msctf.h>
dewyatt@4752
    30
#include <imm.h>
dewyatt@4752
    31
slouken@2311
    32
#ifndef MAPVK_VK_TO_VSC
slouken@2311
    33
#define MAPVK_VK_TO_VSC     0
slouken@2311
    34
#endif
slouken@2311
    35
#ifndef MAPVK_VSC_TO_VK
slouken@2311
    36
#define MAPVK_VSC_TO_VK     1
slouken@2311
    37
#endif
slouken@2311
    38
#ifndef MAPVK_VK_TO_CHAR
slouken@2311
    39
#define MAPVK_VK_TO_CHAR    2
slouken@2311
    40
#endif
slouken@2311
    41
slouken@2311
    42
/* Alphabetic scancodes for PC keyboards */
slouken@2311
    43
BYTE alpha_scancodes[26] = {
slouken@2311
    44
    30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24,
slouken@2311
    45
    25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44
slouken@2311
    46
};
slouken@2735
    47
slouken@2317
    48
BYTE keypad_scancodes[10] = {
slouken@2317
    49
    82, 79, 80, 81, 75, 76, 77, 71, 72, 73
slouken@2317
    50
};
slouken@2311
    51
dewyatt@4752
    52
void IME_Disable(SDL_VideoData *videodata, HWND hwnd);
dewyatt@4752
    53
void IME_Enable(SDL_VideoData *videodata, HWND hwnd);
dewyatt@4752
    54
void IME_Init(SDL_VideoData *videodata, HWND hwnd);
dewyatt@4752
    55
void IME_Quit(SDL_VideoData *videodata);
dewyatt@4752
    56
slouken@1895
    57
void
slouken@1895
    58
WIN_InitKeyboard(_THIS)
slouken@1895
    59
{
slouken@1895
    60
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@2311
    61
    int i;
slouken@2311
    62
slouken@2311
    63
    /* Make sure the alpha scancodes are correct.  T isn't usually remapped */
bob@2324
    64
    if (MapVirtualKey('T', MAPVK_VK_TO_VSC) != alpha_scancodes['T' - 'A']) {
slouken@2724
    65
#if 0
slouken@2726
    66
        printf
slouken@2726
    67
            ("Fixing alpha scancode map, assuming US QWERTY layout!\nPlease send the following 26 lines of output to the SDL mailing list <sdl@libsdl.org>, including a description of your keyboard hardware.\n");
slouken@2724
    68
#endif
slouken@2311
    69
        for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
bob@2324
    70
            alpha_scancodes[i] = MapVirtualKey('A' + i, MAPVK_VK_TO_VSC);
slouken@2724
    71
#if 0
bob@2324
    72
            printf("%d = %d\n", i, alpha_scancodes[i]);
slouken@2724
    73
#endif
slouken@2311
    74
        }
slouken@2311
    75
    }
slouken@2317
    76
    if (MapVirtualKey(VK_NUMPAD0, MAPVK_VK_TO_VSC) != keypad_scancodes[0]) {
slouken@2724
    77
#if 0
slouken@2726
    78
        printf
slouken@2726
    79
            ("Fixing keypad scancode map!\nPlease send the following 10 lines of output to the SDL mailing list <sdl@libsdl.org>, including a description of your keyboard hardware.\n");
slouken@2724
    80
#endif
slouken@2317
    81
        for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
bob@2324
    82
            keypad_scancodes[i] =
bob@2324
    83
                MapVirtualKey(VK_NUMPAD0 + i, MAPVK_VK_TO_VSC);
slouken@2724
    84
#if 0
bob@2324
    85
            printf("%d = %d\n", i, keypad_scancodes[i]);
slouken@2724
    86
#endif
slouken@2317
    87
        }
slouken@2317
    88
    }
slouken@1895
    89
slouken@2308
    90
    data->key_layout = win32_scancode_table;
slouken@2308
    91
dewyatt@4752
    92
    data->ime_com_initialized = SDL_FALSE;
dewyatt@4752
    93
    data->ime_thread_mgr = 0;
dewyatt@4752
    94
    data->ime_initialized = SDL_FALSE;
dewyatt@4752
    95
    data->ime_enabled = SDL_FALSE;
dewyatt@4752
    96
    data->ime_available = SDL_FALSE;
dewyatt@4752
    97
    data->ime_hwnd_main = 0;
dewyatt@4752
    98
    data->ime_hwnd_current = 0;
dewyatt@4752
    99
    data->ime_himc = 0;
dewyatt@4752
   100
slouken@4465
   101
    WIN_UpdateKeymap();
slouken@2308
   102
slouken@2308
   103
    SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
slouken@2308
   104
    SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
slouken@2308
   105
    SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");
slouken@2308
   106
}
slouken@2308
   107
slouken@2308
   108
void
slouken@4465
   109
WIN_UpdateKeymap()
slouken@2308
   110
{
slouken@2308
   111
    int i;
slouken@2308
   112
    SDL_scancode scancode;
slouken@2308
   113
    SDLKey keymap[SDL_NUM_SCANCODES];
slouken@2308
   114
slouken@2308
   115
    SDL_GetDefaultKeymap(keymap);
slouken@2308
   116
slouken@2308
   117
    for (i = 0; i < SDL_arraysize(win32_scancode_table); i++) {
slouken@2308
   118
slouken@2308
   119
        /* Make sure this scancode is a valid character scancode */
slouken@2308
   120
        scancode = win32_scancode_table[i];
slouken@2308
   121
        if (scancode == SDL_SCANCODE_UNKNOWN ||
slouken@2308
   122
            (keymap[scancode] & SDLK_SCANCODE_MASK)) {
slouken@2308
   123
            continue;
slouken@2308
   124
        }
slouken@2311
   125
slouken@2311
   126
        /* Alphabetic keys are handled specially, since Windows remaps them */
slouken@2311
   127
        if (i >= 'A' && i <= 'Z') {
bob@2324
   128
            BYTE vsc = alpha_scancodes[i - 'A'];
slouken@2311
   129
            keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
slouken@2311
   130
        } else {
slouken@2311
   131
            keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
slouken@2311
   132
        }
slouken@2308
   133
    }
slouken@4465
   134
    SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
slouken@1895
   135
}
slouken@1895
   136
slouken@1895
   137
void
slouken@1895
   138
WIN_QuitKeyboard(_THIS)
slouken@1895
   139
{
dewyatt@4752
   140
    IME_Quit((SDL_VideoData *)_this->driverdata);
slouken@1895
   141
}
slouken@1895
   142
dewyatt@4747
   143
void
dewyatt@4750
   144
WIN_StartTextInput(_THIS, SDL_Window *window)
dewyatt@4747
   145
{
dewyatt@4752
   146
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
dewyatt@4752
   147
    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
dewyatt@4752
   148
    IME_Init(videodata, hwnd);
dewyatt@4752
   149
    IME_Enable(videodata, hwnd);
dewyatt@4747
   150
}
dewyatt@4747
   151
dewyatt@4747
   152
void
dewyatt@4751
   153
WIN_StopTextInput(_THIS, SDL_Window *window)
dewyatt@4747
   154
{
dewyatt@4752
   155
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
dewyatt@4752
   156
    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
dewyatt@4752
   157
    IME_Init(videodata, hwnd);
dewyatt@4752
   158
    IME_Disable(videodata, hwnd);
dewyatt@4747
   159
}
dewyatt@4747
   160
dewyatt@4747
   161
void
dewyatt@4747
   162
WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
dewyatt@4747
   163
{
dewyatt@4747
   164
dewyatt@4747
   165
}
dewyatt@4747
   166
dewyatt@4752
   167
void
dewyatt@4752
   168
IME_Disable(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4752
   169
{
dewyatt@4752
   170
    if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
dewyatt@4752
   171
        return;
dewyatt@4752
   172
dewyatt@4752
   173
    if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
dewyatt@4752
   174
        ImmAssociateContext(videodata->ime_hwnd_current, NULL);
dewyatt@4752
   175
dewyatt@4752
   176
    videodata->ime_enabled = SDL_FALSE;
dewyatt@4752
   177
}
dewyatt@4752
   178
dewyatt@4752
   179
void
dewyatt@4752
   180
IME_Enable(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4752
   181
{
dewyatt@4752
   182
    if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
dewyatt@4752
   183
        return;
dewyatt@4752
   184
dewyatt@4752
   185
    if (!videodata->ime_available) {
dewyatt@4752
   186
        IME_Disable(videodata, hwnd);
dewyatt@4752
   187
        return;
dewyatt@4752
   188
    }
dewyatt@4752
   189
    if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
dewyatt@4752
   190
        ImmAssociateContext(videodata->ime_hwnd_current, videodata->ime_himc);
dewyatt@4752
   191
dewyatt@4752
   192
    videodata->ime_enabled = SDL_TRUE;
dewyatt@4752
   193
}
dewyatt@4752
   194
dewyatt@4752
   195
void
dewyatt@4752
   196
IME_Init(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4752
   197
{
dewyatt@4752
   198
    if (videodata->ime_initialized)
dewyatt@4752
   199
        return;
dewyatt@4752
   200
dewyatt@4752
   201
    videodata->ime_hwnd_main = hwnd;
dewyatt@4752
   202
    if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
dewyatt@4752
   203
        videodata->ime_com_initialized = SDL_TRUE;
dewyatt@4752
   204
        CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, &videodata->ime_thread_mgr);
dewyatt@4752
   205
    }
dewyatt@4752
   206
    videodata->ime_initialized = SDL_TRUE;
dewyatt@4752
   207
    videodata->ime_hwnd_current = videodata->ime_hwnd_main;
dewyatt@4752
   208
    if (videodata->ime_thread_mgr) {
dewyatt@4752
   209
        struct ITfDocumentMgr *document_mgr = 0;
dewyatt@4752
   210
        if (SUCCEEDED(videodata->ime_thread_mgr->lpVtbl->AssociateFocus(videodata->ime_thread_mgr, hwnd, NULL, &document_mgr))) {
dewyatt@4752
   211
            if (document_mgr)
dewyatt@4752
   212
                document_mgr->lpVtbl->Release(document_mgr);
dewyatt@4752
   213
        }
dewyatt@4752
   214
    }
dewyatt@4752
   215
    videodata->ime_himc = ImmGetContext(hwnd);
dewyatt@4752
   216
    ImmReleaseContext(hwnd, videodata->ime_himc);
dewyatt@4752
   217
    if (!videodata->ime_himc) {
dewyatt@4752
   218
        videodata->ime_available = SDL_FALSE;
dewyatt@4752
   219
        IME_Disable(videodata, hwnd);
dewyatt@4752
   220
        return;
dewyatt@4752
   221
    }
dewyatt@4752
   222
    videodata->ime_available = SDL_TRUE;
dewyatt@4752
   223
    IME_Disable(videodata, hwnd);
dewyatt@4752
   224
}
dewyatt@4752
   225
dewyatt@4752
   226
void
dewyatt@4752
   227
IME_Quit(SDL_VideoData *videodata)
dewyatt@4752
   228
{
dewyatt@4752
   229
    if (!videodata->ime_initialized)
dewyatt@4752
   230
        return;
dewyatt@4752
   231
dewyatt@4752
   232
    if (videodata->ime_hwnd_main)
dewyatt@4752
   233
        ImmAssociateContext(videodata->ime_hwnd_main, videodata->ime_himc);
dewyatt@4752
   234
dewyatt@4752
   235
    videodata->ime_hwnd_main = 0;
dewyatt@4752
   236
    videodata->ime_himc = 0;
dewyatt@4752
   237
    if (videodata->ime_thread_mgr)
dewyatt@4752
   238
    {
dewyatt@4752
   239
        videodata->ime_thread_mgr->lpVtbl->Release(videodata->ime_thread_mgr);
dewyatt@4752
   240
        videodata->ime_thread_mgr = 0;
dewyatt@4752
   241
    }
dewyatt@4752
   242
    if (videodata->ime_com_initialized)
dewyatt@4752
   243
    {
dewyatt@4752
   244
        CoUninitialize();
dewyatt@4752
   245
        videodata->ime_com_initialized = SDL_FALSE;
dewyatt@4752
   246
    }
dewyatt@4752
   247
    videodata->ime_initialized = SDL_FALSE;
dewyatt@4752
   248
}
dewyatt@4752
   249
dewyatt@4752
   250
SDL_bool
dewyatt@4752
   251
IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
dewyatt@4752
   252
{
dewyatt@4752
   253
    SDL_bool trap = SDL_FALSE;
dewyatt@4752
   254
    HIMC himc = 0;
dewyatt@4752
   255
    WCHAR Buffer[SDL_TEXTINPUTEVENT_TEXT_SIZE / 2];
dewyatt@4752
   256
    if (!videodata->ime_initialized || !videodata->ime_available || !videodata->ime_enabled)
dewyatt@4752
   257
        return SDL_FALSE;
dewyatt@4752
   258
dewyatt@4752
   259
    switch (msg)
dewyatt@4752
   260
    {
dewyatt@4752
   261
    case WM_INPUTLANGCHANGE:
dewyatt@4752
   262
        break;
dewyatt@4752
   263
    case WM_IME_SETCONTEXT:
dewyatt@4752
   264
        *lParam = 0;
dewyatt@4752
   265
        break;
dewyatt@4752
   266
    case WM_IME_STARTCOMPOSITION:
dewyatt@4752
   267
        trap = SDL_TRUE;
dewyatt@4752
   268
        break;
dewyatt@4752
   269
    case WM_IME_COMPOSITION:
dewyatt@4752
   270
        trap = SDL_TRUE;
dewyatt@4752
   271
        himc = ImmGetContext(hwnd);
dewyatt@4752
   272
        if (*lParam & GCS_RESULTSTR)
dewyatt@4752
   273
        {
dewyatt@4752
   274
            LONG Length = 0;
dewyatt@4752
   275
            char *s = 0;
dewyatt@4752
   276
            Length = ImmGetCompositionStringW(himc, GCS_RESULTSTR, Buffer, sizeof(Buffer) * sizeof(Buffer[0]));
dewyatt@4752
   277
            Buffer[Length / sizeof(Buffer[0])] = 0;
dewyatt@4752
   278
            s = WIN_StringToUTF8(Buffer);
dewyatt@4752
   279
            SDL_SendKeyboardText(s);
dewyatt@4752
   280
            SDL_free(s);
dewyatt@4752
   281
        }
dewyatt@4752
   282
        if (*lParam & GCS_COMPSTR)
dewyatt@4752
   283
        {
dewyatt@4752
   284
            LONG Length = 0;
dewyatt@4752
   285
            DWORD Cursor = 0;
dewyatt@4752
   286
            char *s = 0;
dewyatt@4752
   287
            Length = ImmGetCompositionStringW(himc, GCS_COMPSTR, Buffer, sizeof(Buffer) * sizeof(Buffer[0]));
dewyatt@4752
   288
            Buffer[Length / sizeof(Buffer[0])] = 0;
dewyatt@4752
   289
            s = WIN_StringToUTF8(Buffer);
dewyatt@4752
   290
            Cursor = LOWORD(ImmGetCompositionStringW(himc, GCS_CURSORPOS, 0, 0));
dewyatt@4752
   291
            SDL_SendEditingText(s, Cursor, 0);
dewyatt@4752
   292
            SDL_free(s);
dewyatt@4752
   293
        }
dewyatt@4752
   294
        ImmReleaseContext(hwnd, himc);
dewyatt@4752
   295
        break;
dewyatt@4752
   296
    case WM_IME_ENDCOMPOSITION:
dewyatt@4752
   297
        SDL_SendKeyboardText("");
dewyatt@4752
   298
        break;
dewyatt@4752
   299
    case WM_IME_NOTIFY:
dewyatt@4752
   300
        switch (wParam)
dewyatt@4752
   301
        {
dewyatt@4752
   302
        case IMN_SETCONVERSIONMODE:
dewyatt@4752
   303
            break;
dewyatt@4752
   304
        case IMN_SETOPENSTATUS:
dewyatt@4752
   305
            break;
dewyatt@4752
   306
        case IMN_OPENCANDIDATE:
dewyatt@4752
   307
        case IMN_CHANGECANDIDATE:
dewyatt@4752
   308
            trap = SDL_TRUE;
dewyatt@4752
   309
            break;
dewyatt@4752
   310
        case IMN_CLOSECANDIDATE:
dewyatt@4752
   311
            trap = SDL_TRUE;
dewyatt@4752
   312
            break;
dewyatt@4752
   313
        case IMN_PRIVATE:
dewyatt@4752
   314
            break;
dewyatt@4752
   315
        default:
dewyatt@4752
   316
            trap = SDL_TRUE;
dewyatt@4752
   317
            break;
dewyatt@4752
   318
        }
dewyatt@4752
   319
        break;
dewyatt@4752
   320
    }
dewyatt@4752
   321
    return trap;
dewyatt@4752
   322
}
dewyatt@4752
   323
slouken@1895
   324
/* vi: set ts=4 sw=4 expandtab: */