src/video/win32/SDL_win32keyboard.c
author dewyatt
Mon, 12 Jul 2010 14:17:43 -0400
changeset 4753 11b0a6a3eb4d
parent 4752 dc7bdcf06367
child 4759 863ba7d1f029
permissions -rw-r--r--
Changed Start/StopTextInput back to not take any parameters.
We call SDL_GetKeyboardFocus internally now.
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@4753
   144
WIN_StartTextInput(_THIS)
dewyatt@4747
   145
{
dewyatt@4753
   146
    SDL_Window *window = SDL_GetKeyboardFocus();
dewyatt@4753
   147
    if (window)
dewyatt@4753
   148
    {
dewyatt@4753
   149
        HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
dewyatt@4753
   150
        SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
dewyatt@4753
   151
        IME_Init(videodata, hwnd);
dewyatt@4753
   152
        IME_Enable(videodata, hwnd);
dewyatt@4753
   153
    }
dewyatt@4747
   154
}
dewyatt@4747
   155
dewyatt@4747
   156
void
dewyatt@4753
   157
WIN_StopTextInput(_THIS)
dewyatt@4747
   158
{
dewyatt@4753
   159
    SDL_Window *window = SDL_GetKeyboardFocus();
dewyatt@4753
   160
    if (window)
dewyatt@4753
   161
    {
dewyatt@4753
   162
        HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
dewyatt@4753
   163
        SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
dewyatt@4753
   164
        IME_Init(videodata, hwnd);
dewyatt@4753
   165
        IME_Disable(videodata, hwnd);
dewyatt@4753
   166
    }
dewyatt@4747
   167
}
dewyatt@4747
   168
dewyatt@4747
   169
void
dewyatt@4747
   170
WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
dewyatt@4747
   171
{
dewyatt@4747
   172
dewyatt@4747
   173
}
dewyatt@4747
   174
dewyatt@4752
   175
void
dewyatt@4752
   176
IME_Disable(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4752
   177
{
dewyatt@4752
   178
    if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
dewyatt@4752
   179
        return;
dewyatt@4752
   180
dewyatt@4752
   181
    if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
dewyatt@4752
   182
        ImmAssociateContext(videodata->ime_hwnd_current, NULL);
dewyatt@4752
   183
dewyatt@4752
   184
    videodata->ime_enabled = SDL_FALSE;
dewyatt@4752
   185
}
dewyatt@4752
   186
dewyatt@4752
   187
void
dewyatt@4752
   188
IME_Enable(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4752
   189
{
dewyatt@4752
   190
    if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
dewyatt@4752
   191
        return;
dewyatt@4752
   192
dewyatt@4752
   193
    if (!videodata->ime_available) {
dewyatt@4752
   194
        IME_Disable(videodata, hwnd);
dewyatt@4752
   195
        return;
dewyatt@4752
   196
    }
dewyatt@4752
   197
    if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
dewyatt@4752
   198
        ImmAssociateContext(videodata->ime_hwnd_current, videodata->ime_himc);
dewyatt@4752
   199
dewyatt@4752
   200
    videodata->ime_enabled = SDL_TRUE;
dewyatt@4752
   201
}
dewyatt@4752
   202
dewyatt@4752
   203
void
dewyatt@4752
   204
IME_Init(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4752
   205
{
dewyatt@4752
   206
    if (videodata->ime_initialized)
dewyatt@4752
   207
        return;
dewyatt@4752
   208
dewyatt@4752
   209
    videodata->ime_hwnd_main = hwnd;
dewyatt@4752
   210
    if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
dewyatt@4752
   211
        videodata->ime_com_initialized = SDL_TRUE;
dewyatt@4752
   212
        CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, &videodata->ime_thread_mgr);
dewyatt@4752
   213
    }
dewyatt@4752
   214
    videodata->ime_initialized = SDL_TRUE;
dewyatt@4752
   215
    videodata->ime_hwnd_current = videodata->ime_hwnd_main;
dewyatt@4752
   216
    if (videodata->ime_thread_mgr) {
dewyatt@4752
   217
        struct ITfDocumentMgr *document_mgr = 0;
dewyatt@4752
   218
        if (SUCCEEDED(videodata->ime_thread_mgr->lpVtbl->AssociateFocus(videodata->ime_thread_mgr, hwnd, NULL, &document_mgr))) {
dewyatt@4752
   219
            if (document_mgr)
dewyatt@4752
   220
                document_mgr->lpVtbl->Release(document_mgr);
dewyatt@4752
   221
        }
dewyatt@4752
   222
    }
dewyatt@4752
   223
    videodata->ime_himc = ImmGetContext(hwnd);
dewyatt@4752
   224
    ImmReleaseContext(hwnd, videodata->ime_himc);
dewyatt@4752
   225
    if (!videodata->ime_himc) {
dewyatt@4752
   226
        videodata->ime_available = SDL_FALSE;
dewyatt@4752
   227
        IME_Disable(videodata, hwnd);
dewyatt@4752
   228
        return;
dewyatt@4752
   229
    }
dewyatt@4752
   230
    videodata->ime_available = SDL_TRUE;
dewyatt@4752
   231
    IME_Disable(videodata, hwnd);
dewyatt@4752
   232
}
dewyatt@4752
   233
dewyatt@4752
   234
void
dewyatt@4752
   235
IME_Quit(SDL_VideoData *videodata)
dewyatt@4752
   236
{
dewyatt@4752
   237
    if (!videodata->ime_initialized)
dewyatt@4752
   238
        return;
dewyatt@4752
   239
dewyatt@4752
   240
    if (videodata->ime_hwnd_main)
dewyatt@4752
   241
        ImmAssociateContext(videodata->ime_hwnd_main, videodata->ime_himc);
dewyatt@4752
   242
dewyatt@4752
   243
    videodata->ime_hwnd_main = 0;
dewyatt@4752
   244
    videodata->ime_himc = 0;
dewyatt@4752
   245
    if (videodata->ime_thread_mgr)
dewyatt@4752
   246
    {
dewyatt@4752
   247
        videodata->ime_thread_mgr->lpVtbl->Release(videodata->ime_thread_mgr);
dewyatt@4752
   248
        videodata->ime_thread_mgr = 0;
dewyatt@4752
   249
    }
dewyatt@4752
   250
    if (videodata->ime_com_initialized)
dewyatt@4752
   251
    {
dewyatt@4752
   252
        CoUninitialize();
dewyatt@4752
   253
        videodata->ime_com_initialized = SDL_FALSE;
dewyatt@4752
   254
    }
dewyatt@4752
   255
    videodata->ime_initialized = SDL_FALSE;
dewyatt@4752
   256
}
dewyatt@4752
   257
dewyatt@4752
   258
SDL_bool
dewyatt@4752
   259
IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
dewyatt@4752
   260
{
dewyatt@4752
   261
    SDL_bool trap = SDL_FALSE;
dewyatt@4752
   262
    HIMC himc = 0;
dewyatt@4752
   263
    WCHAR Buffer[SDL_TEXTINPUTEVENT_TEXT_SIZE / 2];
dewyatt@4752
   264
    if (!videodata->ime_initialized || !videodata->ime_available || !videodata->ime_enabled)
dewyatt@4752
   265
        return SDL_FALSE;
dewyatt@4752
   266
dewyatt@4752
   267
    switch (msg)
dewyatt@4752
   268
    {
dewyatt@4752
   269
    case WM_INPUTLANGCHANGE:
dewyatt@4752
   270
        break;
dewyatt@4752
   271
    case WM_IME_SETCONTEXT:
dewyatt@4752
   272
        *lParam = 0;
dewyatt@4752
   273
        break;
dewyatt@4752
   274
    case WM_IME_STARTCOMPOSITION:
dewyatt@4752
   275
        trap = SDL_TRUE;
dewyatt@4752
   276
        break;
dewyatt@4752
   277
    case WM_IME_COMPOSITION:
dewyatt@4752
   278
        trap = SDL_TRUE;
dewyatt@4752
   279
        himc = ImmGetContext(hwnd);
dewyatt@4752
   280
        if (*lParam & GCS_RESULTSTR)
dewyatt@4752
   281
        {
dewyatt@4752
   282
            LONG Length = 0;
dewyatt@4752
   283
            char *s = 0;
dewyatt@4752
   284
            Length = ImmGetCompositionStringW(himc, GCS_RESULTSTR, Buffer, sizeof(Buffer) * sizeof(Buffer[0]));
dewyatt@4752
   285
            Buffer[Length / sizeof(Buffer[0])] = 0;
dewyatt@4752
   286
            s = WIN_StringToUTF8(Buffer);
dewyatt@4752
   287
            SDL_SendKeyboardText(s);
dewyatt@4752
   288
            SDL_free(s);
dewyatt@4752
   289
        }
dewyatt@4752
   290
        if (*lParam & GCS_COMPSTR)
dewyatt@4752
   291
        {
dewyatt@4752
   292
            LONG Length = 0;
dewyatt@4752
   293
            DWORD Cursor = 0;
dewyatt@4752
   294
            char *s = 0;
dewyatt@4752
   295
            Length = ImmGetCompositionStringW(himc, GCS_COMPSTR, Buffer, sizeof(Buffer) * sizeof(Buffer[0]));
dewyatt@4752
   296
            Buffer[Length / sizeof(Buffer[0])] = 0;
dewyatt@4752
   297
            s = WIN_StringToUTF8(Buffer);
dewyatt@4752
   298
            Cursor = LOWORD(ImmGetCompositionStringW(himc, GCS_CURSORPOS, 0, 0));
dewyatt@4752
   299
            SDL_SendEditingText(s, Cursor, 0);
dewyatt@4752
   300
            SDL_free(s);
dewyatt@4752
   301
        }
dewyatt@4752
   302
        ImmReleaseContext(hwnd, himc);
dewyatt@4752
   303
        break;
dewyatt@4752
   304
    case WM_IME_ENDCOMPOSITION:
dewyatt@4752
   305
        SDL_SendKeyboardText("");
dewyatt@4752
   306
        break;
dewyatt@4752
   307
    case WM_IME_NOTIFY:
dewyatt@4752
   308
        switch (wParam)
dewyatt@4752
   309
        {
dewyatt@4752
   310
        case IMN_SETCONVERSIONMODE:
dewyatt@4752
   311
            break;
dewyatt@4752
   312
        case IMN_SETOPENSTATUS:
dewyatt@4752
   313
            break;
dewyatt@4752
   314
        case IMN_OPENCANDIDATE:
dewyatt@4752
   315
        case IMN_CHANGECANDIDATE:
dewyatt@4752
   316
            trap = SDL_TRUE;
dewyatt@4752
   317
            break;
dewyatt@4752
   318
        case IMN_CLOSECANDIDATE:
dewyatt@4752
   319
            trap = SDL_TRUE;
dewyatt@4752
   320
            break;
dewyatt@4752
   321
        case IMN_PRIVATE:
dewyatt@4752
   322
            break;
dewyatt@4752
   323
        default:
dewyatt@4752
   324
            trap = SDL_TRUE;
dewyatt@4752
   325
            break;
dewyatt@4752
   326
        }
dewyatt@4752
   327
        break;
dewyatt@4752
   328
    }
dewyatt@4752
   329
    return trap;
dewyatt@4752
   330
}
dewyatt@4752
   331
slouken@1895
   332
/* vi: set ts=4 sw=4 expandtab: */