src/video/win32/SDL_win32keyboard.c
author dewyatt
Tue, 03 Aug 2010 16:57:49 -0400
changeset 4759 863ba7d1f029
parent 4753 11b0a6a3eb4d
child 4760 c40027ee6d47
permissions -rw-r--r--
Large commit with initial reading string support, TSF UILess code, etc.
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 <imm.h>
dewyatt@4759
    30
#include <oleauto.h>
dewyatt@4759
    31
dewyatt@4759
    32
static void IME_Init(SDL_VideoData *videodata, HWND hwnd);
dewyatt@4759
    33
static void IME_Enable(SDL_VideoData *videodata, HWND hwnd);
dewyatt@4759
    34
static void IME_Disable(SDL_VideoData *videodata, HWND hwnd);
dewyatt@4759
    35
static void IME_Quit(SDL_VideoData *videodata);
dewyatt@4752
    36
slouken@2311
    37
#ifndef MAPVK_VK_TO_VSC
slouken@2311
    38
#define MAPVK_VK_TO_VSC     0
slouken@2311
    39
#endif
slouken@2311
    40
#ifndef MAPVK_VSC_TO_VK
slouken@2311
    41
#define MAPVK_VSC_TO_VK     1
slouken@2311
    42
#endif
slouken@2311
    43
#ifndef MAPVK_VK_TO_CHAR
slouken@2311
    44
#define MAPVK_VK_TO_CHAR    2
slouken@2311
    45
#endif
slouken@2311
    46
slouken@2311
    47
/* Alphabetic scancodes for PC keyboards */
slouken@2311
    48
BYTE alpha_scancodes[26] = {
slouken@2311
    49
    30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38, 50, 49, 24,
slouken@2311
    50
    25, 16, 19, 31, 20, 22, 47, 17, 45, 21, 44
slouken@2311
    51
};
slouken@2735
    52
slouken@2317
    53
BYTE keypad_scancodes[10] = {
slouken@2317
    54
    82, 79, 80, 81, 75, 76, 77, 71, 72, 73
slouken@2317
    55
};
slouken@2311
    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@4759
    93
    data->ime_threadmgr = 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@4759
   100
    data->ime_composition[0] = 0;
dewyatt@4759
   101
    data->ime_readingstring[0] = 0;
dewyatt@4759
   102
    data->ime_cursor = 0;
dewyatt@4759
   103
    data->ime_hkl = 0;
dewyatt@4759
   104
    data->ime_himm32 = 0;
dewyatt@4759
   105
    data->GetReadingString = 0;
dewyatt@4759
   106
    data->ShowReadingWindow = 0;
dewyatt@4759
   107
    data->ImmLockIMC = 0;
dewyatt@4759
   108
    data->ImmUnlockIMC = 0;
dewyatt@4759
   109
    data->ImmLockIMCC = 0;
dewyatt@4759
   110
    data->ImmUnlockIMCC = 0;
dewyatt@4759
   111
    data->ime_uiless = SDL_FALSE;
dewyatt@4759
   112
    data->ime_threadmgrex = 0;
dewyatt@4759
   113
    data->ime_uielemsinkcookie = TF_INVALID_COOKIE;
dewyatt@4759
   114
    data->ime_alpnsinkcookie = TF_INVALID_COOKIE;
dewyatt@4759
   115
    data->ime_openmodesinkcookie = TF_INVALID_COOKIE;
dewyatt@4759
   116
    data->ime_convmodesinkcookie = TF_INVALID_COOKIE;
dewyatt@4759
   117
    data->ime_uielemsink = 0;
dewyatt@4759
   118
    data->ime_ippasink = 0;
dewyatt@4752
   119
slouken@4465
   120
    WIN_UpdateKeymap();
slouken@2308
   121
slouken@2308
   122
    SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
slouken@2308
   123
    SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
slouken@2308
   124
    SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");
slouken@2308
   125
}
slouken@2308
   126
slouken@2308
   127
void
slouken@4465
   128
WIN_UpdateKeymap()
slouken@2308
   129
{
slouken@2308
   130
    int i;
slouken@2308
   131
    SDL_scancode scancode;
slouken@2308
   132
    SDLKey keymap[SDL_NUM_SCANCODES];
slouken@2308
   133
slouken@2308
   134
    SDL_GetDefaultKeymap(keymap);
slouken@2308
   135
slouken@2308
   136
    for (i = 0; i < SDL_arraysize(win32_scancode_table); i++) {
slouken@2308
   137
slouken@2308
   138
        /* Make sure this scancode is a valid character scancode */
slouken@2308
   139
        scancode = win32_scancode_table[i];
slouken@2308
   140
        if (scancode == SDL_SCANCODE_UNKNOWN ||
slouken@2308
   141
            (keymap[scancode] & SDLK_SCANCODE_MASK)) {
slouken@2308
   142
            continue;
slouken@2308
   143
        }
slouken@2311
   144
slouken@2311
   145
        /* Alphabetic keys are handled specially, since Windows remaps them */
slouken@2311
   146
        if (i >= 'A' && i <= 'Z') {
bob@2324
   147
            BYTE vsc = alpha_scancodes[i - 'A'];
slouken@2311
   148
            keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
slouken@2311
   149
        } else {
slouken@2311
   150
            keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
slouken@2311
   151
        }
slouken@2308
   152
    }
slouken@4465
   153
    SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
slouken@1895
   154
}
slouken@1895
   155
slouken@1895
   156
void
slouken@1895
   157
WIN_QuitKeyboard(_THIS)
slouken@1895
   158
{
dewyatt@4752
   159
    IME_Quit((SDL_VideoData *)_this->driverdata);
slouken@1895
   160
}
slouken@1895
   161
dewyatt@4747
   162
void
dewyatt@4753
   163
WIN_StartTextInput(_THIS)
dewyatt@4747
   164
{
dewyatt@4753
   165
    SDL_Window *window = SDL_GetKeyboardFocus();
dewyatt@4759
   166
    if (window) {
dewyatt@4753
   167
        HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
dewyatt@4753
   168
        SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
dewyatt@4753
   169
        IME_Init(videodata, hwnd);
dewyatt@4753
   170
        IME_Enable(videodata, hwnd);
dewyatt@4753
   171
    }
dewyatt@4747
   172
}
dewyatt@4747
   173
dewyatt@4747
   174
void
dewyatt@4753
   175
WIN_StopTextInput(_THIS)
dewyatt@4747
   176
{
dewyatt@4753
   177
    SDL_Window *window = SDL_GetKeyboardFocus();
dewyatt@4759
   178
    if (window) {
dewyatt@4753
   179
        HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
dewyatt@4753
   180
        SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
dewyatt@4753
   181
        IME_Init(videodata, hwnd);
dewyatt@4753
   182
        IME_Disable(videodata, hwnd);
dewyatt@4753
   183
    }
dewyatt@4747
   184
}
dewyatt@4747
   185
dewyatt@4747
   186
void
dewyatt@4747
   187
WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
dewyatt@4747
   188
{
dewyatt@4747
   189
dewyatt@4747
   190
}
dewyatt@4747
   191
dewyatt@4759
   192
#define LANG_CHT MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL)
dewyatt@4759
   193
#define LANG_CHS MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED)
dewyatt@4759
   194
dewyatt@4759
   195
#define MAKEIMEVERSION(major,minor) ((DWORD) (((BYTE)(major) << 24) | ((BYTE)(minor) << 16) ))
dewyatt@4759
   196
#define IMEID_VER(id) ((id) & 0xffff0000)
dewyatt@4759
   197
#define IMEID_LANG(id) ((id) & 0x0000ffff)
dewyatt@4759
   198
dewyatt@4759
   199
#define CHT_HKL_DAYI            ((HKL)0xE0060404)
dewyatt@4759
   200
#define CHT_HKL_NEW_PHONETIC    ((HKL)0xE0080404)
dewyatt@4759
   201
#define CHT_HKL_NEW_CHANG_JIE   ((HKL)0xE0090404)
dewyatt@4759
   202
#define CHT_HKL_NEW_QUICK       ((HKL)0xE00A0404)
dewyatt@4759
   203
#define CHT_HKL_HK_CANTONESE    ((HKL)0xE00B0404)
dewyatt@4759
   204
#define CHT_IMEFILENAME1        "TINTLGNT.IME"
dewyatt@4759
   205
#define CHT_IMEFILENAME2        "CINTLGNT.IME"
dewyatt@4759
   206
#define CHT_IMEFILENAME3        "MSTCIPHA.IME"
dewyatt@4759
   207
#define IMEID_CHT_VER42         (LANG_CHT | MAKEIMEVERSION(4, 2))
dewyatt@4759
   208
#define IMEID_CHT_VER43         (LANG_CHT | MAKEIMEVERSION(4, 3))
dewyatt@4759
   209
#define IMEID_CHT_VER44         (LANG_CHT | MAKEIMEVERSION(4, 4))
dewyatt@4759
   210
#define IMEID_CHT_VER50         (LANG_CHT | MAKEIMEVERSION(5, 0))
dewyatt@4759
   211
#define IMEID_CHT_VER51         (LANG_CHT | MAKEIMEVERSION(5, 1))
dewyatt@4759
   212
#define IMEID_CHT_VER52         (LANG_CHT | MAKEIMEVERSION(5, 2))
dewyatt@4759
   213
#define IMEID_CHT_VER60         (LANG_CHT | MAKEIMEVERSION(6, 0))
dewyatt@4759
   214
#define IMEID_CHT_VER_VISTA     (LANG_CHT | MAKEIMEVERSION(7, 0))
dewyatt@4759
   215
dewyatt@4759
   216
#define CHS_HKL                 ((HKL)0xE00E0804)
dewyatt@4759
   217
#define CHS_IMEFILENAME1        "PINTLGNT.IME"
dewyatt@4759
   218
#define CHS_IMEFILENAME2        "MSSCIPYA.IME"
dewyatt@4759
   219
#define IMEID_CHS_VER41         (LANG_CHS | MAKEIMEVERSION(4, 1))
dewyatt@4759
   220
#define IMEID_CHS_VER42         (LANG_CHS | MAKEIMEVERSION(4, 2))
dewyatt@4759
   221
#define IMEID_CHS_VER53         (LANG_CHS | MAKEIMEVERSION(5, 3))
dewyatt@4759
   222
dewyatt@4759
   223
#define LANG() LOWORD((videodata->ime_hkl))
dewyatt@4759
   224
#define PRIMLANG() ((WORD)PRIMARYLANGID(LANG()))
dewyatt@4759
   225
#define SUBLANG() SUBLANGID(LANG())
dewyatt@4759
   226
dewyatt@4759
   227
static void IME_UpdateInputLocale(SDL_VideoData *videodata);
dewyatt@4759
   228
static void IME_ClearComposition(SDL_VideoData *videodata);
dewyatt@4759
   229
static void IME_SetWindow(SDL_VideoData* videodata, HWND hwnd);
dewyatt@4759
   230
static void IME_SetupAPI(SDL_VideoData *videodata);
dewyatt@4759
   231
static DWORD IME_GetId(SDL_VideoData *videodata, UINT uIndex);
dewyatt@4759
   232
static void IME_SendEditingEvent(SDL_VideoData *videodata);
dewyatt@4759
   233
#define SDL_IsEqualIID(riid1, riid2) SDL_IsEqualGUID(riid1, riid2)
dewyatt@4759
   234
#define SDL_IsEqualGUID(rguid1, rguid2) (!SDL_memcmp(rguid1, rguid2, sizeof(GUID)))
dewyatt@4759
   235
dewyatt@4759
   236
static SDL_bool UILess_SetupSinks(SDL_VideoData *videodata);
dewyatt@4759
   237
static void UILess_ReleaseSinks(SDL_VideoData *videodata);
dewyatt@4759
   238
static void UILess_EnableUIUpdates(SDL_VideoData *videodata);
dewyatt@4759
   239
static void UILess_DisableUIUpdates(SDL_VideoData *videodata);
dewyatt@4759
   240
dewyatt@4759
   241
static void
dewyatt@4759
   242
IME_Init(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4752
   243
{
dewyatt@4759
   244
    if (videodata->ime_initialized)
dewyatt@4752
   245
        return;
dewyatt@4752
   246
dewyatt@4759
   247
    videodata->ime_hwnd_main = hwnd;
dewyatt@4759
   248
    if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
dewyatt@4759
   249
        videodata->ime_com_initialized = SDL_TRUE;
dewyatt@4759
   250
        CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, &videodata->ime_threadmgr);
dewyatt@4759
   251
    }
dewyatt@4759
   252
    videodata->ime_initialized = SDL_TRUE;
dewyatt@4759
   253
    videodata->ime_himm32 = LoadLibraryA("imm32.dll");
dewyatt@4759
   254
    if (!videodata->ime_himm32) {
dewyatt@4759
   255
        videodata->ime_available = SDL_FALSE;
dewyatt@4759
   256
        return;
dewyatt@4759
   257
    }
dewyatt@4759
   258
    videodata->ImmLockIMC = (LPINPUTCONTEXT2 (WINAPI *)(HIMC))GetProcAddress(videodata->ime_himm32, "ImmLockIMC");
dewyatt@4759
   259
    videodata->ImmUnlockIMC = (BOOL (WINAPI *)(HIMC))GetProcAddress(videodata->ime_himm32, "ImmUnlockIMC");
dewyatt@4759
   260
    videodata->ImmLockIMCC = (LPVOID (WINAPI *)(HIMCC))GetProcAddress(videodata->ime_himm32, "ImmLockIMCC");
dewyatt@4759
   261
    videodata->ImmUnlockIMCC = (BOOL (WINAPI *)(HIMCC))GetProcAddress(videodata->ime_himm32, "ImmUnlockIMCC");
dewyatt@4752
   262
dewyatt@4759
   263
    IME_SetWindow(videodata, hwnd);
dewyatt@4759
   264
    videodata->ime_himc = ImmGetContext(hwnd);
dewyatt@4759
   265
    ImmReleaseContext(hwnd, videodata->ime_himc);
dewyatt@4759
   266
    if (!videodata->ime_himc) {
dewyatt@4759
   267
        videodata->ime_available = SDL_FALSE;
dewyatt@4759
   268
        IME_Disable(videodata, hwnd);
dewyatt@4759
   269
        return;
dewyatt@4759
   270
    }
dewyatt@4759
   271
    videodata->ime_available = SDL_TRUE;
dewyatt@4759
   272
    IME_UpdateInputLocale(videodata);
dewyatt@4759
   273
    IME_SetupAPI(videodata);
dewyatt@4759
   274
    videodata->ime_uiless = UILess_SetupSinks(videodata);
dewyatt@4759
   275
    IME_UpdateInputLocale(videodata);
dewyatt@4759
   276
    IME_Disable(videodata, hwnd);
dewyatt@4752
   277
}
dewyatt@4752
   278
dewyatt@4759
   279
static void
dewyatt@4752
   280
IME_Enable(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4752
   281
{
dewyatt@4752
   282
    if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
dewyatt@4752
   283
        return;
dewyatt@4752
   284
dewyatt@4752
   285
    if (!videodata->ime_available) {
dewyatt@4752
   286
        IME_Disable(videodata, hwnd);
dewyatt@4752
   287
        return;
dewyatt@4752
   288
    }
dewyatt@4752
   289
    if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
dewyatt@4752
   290
        ImmAssociateContext(videodata->ime_hwnd_current, videodata->ime_himc);
dewyatt@4752
   291
dewyatt@4752
   292
    videodata->ime_enabled = SDL_TRUE;
dewyatt@4759
   293
    IME_UpdateInputLocale(videodata);
dewyatt@4759
   294
    UILess_EnableUIUpdates(videodata);
dewyatt@4752
   295
}
dewyatt@4752
   296
dewyatt@4759
   297
static void
dewyatt@4759
   298
IME_Disable(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4752
   299
{
dewyatt@4759
   300
    if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
dewyatt@4752
   301
        return;
dewyatt@4752
   302
dewyatt@4759
   303
    IME_ClearComposition(videodata);
dewyatt@4759
   304
    if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
dewyatt@4759
   305
        ImmAssociateContext(videodata->ime_hwnd_current, NULL);
dewyatt@4759
   306
dewyatt@4759
   307
    videodata->ime_enabled = SDL_FALSE;
dewyatt@4759
   308
    UILess_DisableUIUpdates(videodata);
dewyatt@4752
   309
}
dewyatt@4752
   310
dewyatt@4759
   311
static void
dewyatt@4752
   312
IME_Quit(SDL_VideoData *videodata)
dewyatt@4752
   313
{
dewyatt@4752
   314
    if (!videodata->ime_initialized)
dewyatt@4752
   315
        return;
dewyatt@4752
   316
dewyatt@4759
   317
    UILess_ReleaseSinks(videodata);
dewyatt@4752
   318
    if (videodata->ime_hwnd_main)
dewyatt@4752
   319
        ImmAssociateContext(videodata->ime_hwnd_main, videodata->ime_himc);
dewyatt@4752
   320
dewyatt@4752
   321
    videodata->ime_hwnd_main = 0;
dewyatt@4752
   322
    videodata->ime_himc = 0;
dewyatt@4759
   323
    if (videodata->ime_himm32)
dewyatt@4752
   324
    {
dewyatt@4759
   325
        FreeLibrary(videodata->ime_himm32);
dewyatt@4759
   326
        videodata->ime_himm32 = 0;
dewyatt@4759
   327
    }
dewyatt@4759
   328
    if (videodata->ime_threadmgr)
dewyatt@4759
   329
    {
dewyatt@4759
   330
        videodata->ime_threadmgr->lpVtbl->Release(videodata->ime_threadmgr);
dewyatt@4759
   331
        videodata->ime_threadmgr = 0;
dewyatt@4752
   332
    }
dewyatt@4752
   333
    if (videodata->ime_com_initialized)
dewyatt@4752
   334
    {
dewyatt@4752
   335
        CoUninitialize();
dewyatt@4752
   336
        videodata->ime_com_initialized = SDL_FALSE;
dewyatt@4752
   337
    }
dewyatt@4752
   338
    videodata->ime_initialized = SDL_FALSE;
dewyatt@4752
   339
}
dewyatt@4752
   340
dewyatt@4759
   341
static void
dewyatt@4759
   342
IME_GetReadingString(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4759
   343
{
dewyatt@4759
   344
    DWORD id = 0;
dewyatt@4759
   345
    HIMC himc = 0;
dewyatt@4759
   346
    WCHAR buffer[16];
dewyatt@4759
   347
    WCHAR *s = buffer;
dewyatt@4759
   348
    DWORD len = 0;
dewyatt@4759
   349
    DWORD err = 0;
dewyatt@4759
   350
    BOOL vertical = FALSE;
dewyatt@4759
   351
    UINT maxuilen = 0;
dewyatt@4759
   352
    static OSVERSIONINFOA osversion = {0};
dewyatt@4759
   353
    if (videodata->ime_uiless)
dewyatt@4759
   354
        return;
dewyatt@4759
   355
dewyatt@4759
   356
    videodata->ime_readingstring[0] = 0;
dewyatt@4759
   357
    if (!osversion.dwOSVersionInfoSize)
dewyatt@4759
   358
    {
dewyatt@4759
   359
        osversion.dwOSVersionInfoSize = sizeof(osversion);
dewyatt@4759
   360
        GetVersionExA(&osversion);
dewyatt@4759
   361
    }
dewyatt@4759
   362
    id = IME_GetId(videodata, 0);
dewyatt@4759
   363
    if (!id)
dewyatt@4759
   364
        return;
dewyatt@4759
   365
dewyatt@4759
   366
    himc = ImmGetContext(hwnd);
dewyatt@4759
   367
    if (!himc)
dewyatt@4759
   368
        return;
dewyatt@4759
   369
dewyatt@4759
   370
    if (videodata->GetReadingString)
dewyatt@4759
   371
    {
dewyatt@4759
   372
        len = videodata->GetReadingString(himc, 0, 0, &err, &vertical, &maxuilen);
dewyatt@4759
   373
        if (len)
dewyatt@4759
   374
        {
dewyatt@4759
   375
            if (len > SDL_arraysize(buffer))
dewyatt@4759
   376
                len = SDL_arraysize(buffer);
dewyatt@4759
   377
dewyatt@4759
   378
            len = videodata->GetReadingString(himc, len, s, &err, &vertical, &maxuilen);
dewyatt@4759
   379
        }
dewyatt@4759
   380
        SDL_wcslcpy(videodata->ime_readingstring, s, len);
dewyatt@4759
   381
    }
dewyatt@4759
   382
    else
dewyatt@4759
   383
    {
dewyatt@4759
   384
        LPINPUTCONTEXT2 lpimc = videodata->ImmLockIMC(himc);
dewyatt@4759
   385
        LPBYTE p = 0;
dewyatt@4759
   386
        s = 0;
dewyatt@4759
   387
        switch (id)
dewyatt@4759
   388
        {
dewyatt@4759
   389
        case IMEID_CHT_VER42:
dewyatt@4759
   390
        case IMEID_CHT_VER43:
dewyatt@4759
   391
        case IMEID_CHT_VER44:
dewyatt@4759
   392
            p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 24);
dewyatt@4759
   393
            if (!p)
dewyatt@4759
   394
                break;
dewyatt@4759
   395
dewyatt@4759
   396
            len = *(DWORD *)(p + 7*4 + 32*4);
dewyatt@4759
   397
            s = (WCHAR *)(p + 56);
dewyatt@4759
   398
            break;
dewyatt@4759
   399
        case IMEID_CHT_VER51:
dewyatt@4759
   400
        case IMEID_CHT_VER52:
dewyatt@4759
   401
        case IMEID_CHS_VER53:
dewyatt@4759
   402
            p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 4);
dewyatt@4759
   403
            if (!p)
dewyatt@4759
   404
                break;
dewyatt@4759
   405
dewyatt@4759
   406
            p = *(LPBYTE *)((LPBYTE)p + 1*4 + 5*4);
dewyatt@4759
   407
            if (!p)
dewyatt@4759
   408
                break;
dewyatt@4759
   409
dewyatt@4759
   410
            len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2);
dewyatt@4759
   411
            s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4);
dewyatt@4759
   412
            break;
dewyatt@4759
   413
        case IMEID_CHS_VER41:
dewyatt@4759
   414
            {
dewyatt@4759
   415
                int offset = (IME_GetId(videodata, 1) >= 0x00000002) ? 8 : 7;
dewyatt@4759
   416
                p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + offset * 4);
dewyatt@4759
   417
                if (!p)
dewyatt@4759
   418
                    break;
dewyatt@4759
   419
dewyatt@4759
   420
                len = *(DWORD *)(p + 7*4 + 16*2*4);
dewyatt@4759
   421
                s = (WCHAR *)(p + 6*4 + 16*2*1);
dewyatt@4759
   422
            }
dewyatt@4759
   423
            break;
dewyatt@4759
   424
        case IMEID_CHS_VER42:
dewyatt@4759
   425
            if (osversion.dwPlatformId != VER_PLATFORM_WIN32_NT)
dewyatt@4759
   426
                break;
dewyatt@4759
   427
dewyatt@4759
   428
            p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 1*4 + 1*4 + 6*4);
dewyatt@4759
   429
            if (!p)
dewyatt@4759
   430
                break;
dewyatt@4759
   431
dewyatt@4759
   432
            len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2);
dewyatt@4759
   433
            s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4);
dewyatt@4759
   434
            break;
dewyatt@4759
   435
        }
dewyatt@4759
   436
        if (s)
dewyatt@4759
   437
        {
dewyatt@4759
   438
            SDL_wcslcpy(videodata->ime_readingstring, s, len + 1);
dewyatt@4759
   439
        }
dewyatt@4759
   440
        videodata->ImmUnlockIMCC(lpimc->hPrivate);
dewyatt@4759
   441
        videodata->ImmUnlockIMC(himc);
dewyatt@4759
   442
    }
dewyatt@4759
   443
    ImmReleaseContext(hwnd, himc);
dewyatt@4759
   444
    IME_SendEditingEvent(videodata);
dewyatt@4759
   445
}
dewyatt@4759
   446
dewyatt@4759
   447
static void
dewyatt@4759
   448
IME_InputLangChanged(SDL_VideoData *videodata)
dewyatt@4759
   449
{
dewyatt@4759
   450
    UINT uLang = PRIMLANG();
dewyatt@4759
   451
    HWND hwndime = 0;
dewyatt@4759
   452
    IME_UpdateInputLocale(videodata);
dewyatt@4759
   453
    IME_SetupAPI(videodata);
dewyatt@4759
   454
    if (uLang != PRIMLANG())
dewyatt@4759
   455
    {
dewyatt@4759
   456
        IME_ClearComposition(videodata);
dewyatt@4759
   457
    }
dewyatt@4759
   458
    hwndime = ImmGetDefaultIMEWnd(videodata->ime_hwnd_current);
dewyatt@4759
   459
    if (hwndime)
dewyatt@4759
   460
    {
dewyatt@4759
   461
        SendMessageA(hwndime, WM_IME_CONTROL, IMC_OPENSTATUSWINDOW, 0);
dewyatt@4759
   462
        SendMessageA(hwndime, WM_IME_CONTROL, IMC_CLOSESTATUSWINDOW, 0);
dewyatt@4759
   463
    }
dewyatt@4759
   464
}
dewyatt@4759
   465
dewyatt@4759
   466
static DWORD
dewyatt@4759
   467
IME_GetId(SDL_VideoData *videodata, UINT uIndex)
dewyatt@4759
   468
{
dewyatt@4759
   469
    static HKL hklprev = 0;
dewyatt@4759
   470
    static DWORD dwRet[2] = {0};
dewyatt@4759
   471
    DWORD dwVerSize = 0;
dewyatt@4759
   472
    DWORD dwVerHandle = 0;
dewyatt@4759
   473
    LPVOID lpVerBuffer = 0;
dewyatt@4759
   474
    LPVOID lpVerData = 0;
dewyatt@4759
   475
    UINT cbVerData = 0;
dewyatt@4759
   476
    char szTemp[256];
dewyatt@4759
   477
    HKL hkl = 0;
dewyatt@4759
   478
    DWORD dwLang = 0;
dewyatt@4759
   479
    if (uIndex >= sizeof(dwRet) / sizeof(dwRet[0]))
dewyatt@4759
   480
        return 0;
dewyatt@4759
   481
dewyatt@4759
   482
    hkl = videodata->ime_hkl;
dewyatt@4759
   483
    if (hklprev == hkl)
dewyatt@4759
   484
        return dwRet[uIndex];
dewyatt@4759
   485
dewyatt@4759
   486
    hklprev = hkl;
dewyatt@4759
   487
    dwLang = ((DWORD)hkl & 0xffff);
dewyatt@4759
   488
    if (videodata->ime_uiless && LANG() == LANG_CHT)
dewyatt@4759
   489
    {
dewyatt@4759
   490
        dwRet[0] = IMEID_CHT_VER_VISTA;
dewyatt@4759
   491
        dwRet[1] = 0;
dewyatt@4759
   492
        return dwRet[0];
dewyatt@4759
   493
    }
dewyatt@4759
   494
    if (hkl != CHT_HKL_NEW_PHONETIC
dewyatt@4759
   495
        && hkl != CHT_HKL_NEW_CHANG_JIE
dewyatt@4759
   496
        && hkl != CHT_HKL_NEW_QUICK
dewyatt@4759
   497
        && hkl != CHT_HKL_HK_CANTONESE
dewyatt@4759
   498
        && hkl != CHS_HKL)
dewyatt@4759
   499
    {
dewyatt@4759
   500
        dwRet[0] = dwRet[1] = 0;
dewyatt@4759
   501
        return dwRet[uIndex];
dewyatt@4759
   502
    }
dewyatt@4759
   503
    if (ImmGetIMEFileNameA(hkl, szTemp, sizeof(szTemp) - 1) <= 0)
dewyatt@4759
   504
    {
dewyatt@4759
   505
        dwRet[0] = dwRet[1] = 0;
dewyatt@4759
   506
        return dwRet[uIndex];
dewyatt@4759
   507
    }
dewyatt@4759
   508
    if (!videodata->GetReadingString)
dewyatt@4759
   509
    {
dewyatt@4759
   510
        #define LCID_INVARIANT MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT)
dewyatt@4759
   511
        if (CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME1, -1) != 2
dewyatt@4759
   512
            && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME2, -1) != 2
dewyatt@4759
   513
            && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME3, -1) != 2
dewyatt@4759
   514
            && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME1, -1) != 2
dewyatt@4759
   515
            && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME2, -1) != 2
dewyatt@4759
   516
            )
dewyatt@4759
   517
        {
dewyatt@4759
   518
            dwRet[0] = dwRet[1] = 0;
dewyatt@4759
   519
            return dwRet[uIndex];
dewyatt@4759
   520
        }
dewyatt@4759
   521
        #undef LCID_INVARIANT
dewyatt@4759
   522
        dwVerSize = GetFileVersionInfoSizeA(szTemp, &dwVerHandle);
dewyatt@4759
   523
        if (dwVerSize)
dewyatt@4759
   524
        {
dewyatt@4759
   525
            lpVerBuffer = SDL_malloc(dwVerSize);
dewyatt@4759
   526
            if (lpVerBuffer)
dewyatt@4759
   527
            {
dewyatt@4759
   528
                if (GetFileVersionInfoA(szTemp, dwVerHandle, dwVerSize, lpVerBuffer))
dewyatt@4759
   529
                {
dewyatt@4759
   530
                    if (VerQueryValueA(lpVerBuffer, "\\", &lpVerData, &cbVerData))
dewyatt@4759
   531
                    {
dewyatt@4759
   532
                        #define pVerFixedInfo   ((VS_FIXEDFILEINFO FAR*)lpVerData)
dewyatt@4759
   533
                        DWORD dwVer = pVerFixedInfo->dwFileVersionMS;
dewyatt@4759
   534
                        dwVer = (dwVer & 0x00ff0000) << 8 | (dwVer & 0x000000ff) << 16;
dewyatt@4759
   535
                        if (videodata->GetReadingString ||
dewyatt@4759
   536
                            dwLang == LANG_CHT && (
dewyatt@4759
   537
                            dwVer == MAKEIMEVERSION(4, 2) ||
dewyatt@4759
   538
                            dwVer == MAKEIMEVERSION(4, 3) ||
dewyatt@4759
   539
                            dwVer == MAKEIMEVERSION(4, 4) ||
dewyatt@4759
   540
                            dwVer == MAKEIMEVERSION(5, 0) ||
dewyatt@4759
   541
                            dwVer == MAKEIMEVERSION(5, 1) ||
dewyatt@4759
   542
                            dwVer == MAKEIMEVERSION(5, 2) ||
dewyatt@4759
   543
                            dwVer == MAKEIMEVERSION(6, 0))
dewyatt@4759
   544
                            ||
dewyatt@4759
   545
                            dwLang == LANG_CHS && (
dewyatt@4759
   546
                            dwVer == MAKEIMEVERSION(4, 1) ||
dewyatt@4759
   547
                            dwVer == MAKEIMEVERSION(4, 2) ||
dewyatt@4759
   548
                            dwVer == MAKEIMEVERSION(5, 3))
dewyatt@4759
   549
                            )
dewyatt@4759
   550
                        {
dewyatt@4759
   551
                            dwRet[0] = dwVer | dwLang;
dewyatt@4759
   552
                            dwRet[1] = pVerFixedInfo->dwFileVersionLS;
dewyatt@4759
   553
                            SDL_free(lpVerBuffer);
dewyatt@4759
   554
                            return dwRet[0];
dewyatt@4759
   555
                        }
dewyatt@4759
   556
                        #undef pVerFixedInfo
dewyatt@4759
   557
                    }
dewyatt@4759
   558
                }
dewyatt@4759
   559
            }
dewyatt@4759
   560
            SDL_free(lpVerBuffer);
dewyatt@4759
   561
        }
dewyatt@4759
   562
    }
dewyatt@4759
   563
    dwRet[0] = dwRet[1] = 0;
dewyatt@4759
   564
    return dwRet[uIndex];
dewyatt@4759
   565
}
dewyatt@4759
   566
dewyatt@4759
   567
static void
dewyatt@4759
   568
IME_SetupAPI(SDL_VideoData *videodata)
dewyatt@4759
   569
{
dewyatt@4759
   570
    char ime_file[MAX_PATH + 1];
dewyatt@4759
   571
    HMODULE hime = 0;
dewyatt@4759
   572
    HKL hkl = 0;
dewyatt@4759
   573
    videodata->GetReadingString = 0;
dewyatt@4759
   574
    videodata->ShowReadingWindow = 0;
dewyatt@4759
   575
    if (videodata->ime_uiless)
dewyatt@4759
   576
        return;
dewyatt@4759
   577
dewyatt@4759
   578
    hkl = videodata->ime_hkl;
dewyatt@4759
   579
    if (ImmGetIMEFileNameA(hkl, ime_file, sizeof(ime_file) - 1) <= 0)
dewyatt@4759
   580
        return;
dewyatt@4759
   581
dewyatt@4759
   582
    hime = LoadLibraryA(ime_file);
dewyatt@4759
   583
    if (!hime)
dewyatt@4759
   584
        return;
dewyatt@4759
   585
dewyatt@4759
   586
    videodata->GetReadingString = (UINT (WINAPI *)(HIMC, UINT, LPWSTR, PINT, BOOL*, PUINT))
dewyatt@4759
   587
        GetProcAddress(hime, "GetReadingString");
dewyatt@4759
   588
    videodata->ShowReadingWindow = (BOOL (WINAPI *)(HIMC, BOOL))
dewyatt@4759
   589
        GetProcAddress(hime, "ShowReadingWindow");
dewyatt@4759
   590
dewyatt@4759
   591
    if (videodata->ShowReadingWindow)
dewyatt@4759
   592
    {
dewyatt@4759
   593
        HIMC himc = ImmGetContext(videodata->ime_hwnd_current);
dewyatt@4759
   594
        if (himc)
dewyatt@4759
   595
        {
dewyatt@4759
   596
            videodata->ShowReadingWindow(himc, FALSE);
dewyatt@4759
   597
            ImmReleaseContext(videodata->ime_hwnd_current, himc);
dewyatt@4759
   598
        }
dewyatt@4759
   599
    }
dewyatt@4759
   600
}
dewyatt@4759
   601
dewyatt@4759
   602
static void
dewyatt@4759
   603
IME_SetWindow(SDL_VideoData* videodata, HWND hwnd)
dewyatt@4759
   604
{
dewyatt@4759
   605
    videodata->ime_hwnd_current = hwnd;
dewyatt@4759
   606
    if (videodata->ime_threadmgr) {
dewyatt@4759
   607
        struct ITfDocumentMgr *document_mgr = 0;
dewyatt@4759
   608
        if (SUCCEEDED(videodata->ime_threadmgr->lpVtbl->AssociateFocus(videodata->ime_threadmgr, hwnd, NULL, &document_mgr))) {
dewyatt@4759
   609
            if (document_mgr)
dewyatt@4759
   610
                document_mgr->lpVtbl->Release(document_mgr);
dewyatt@4759
   611
        }
dewyatt@4759
   612
    }
dewyatt@4759
   613
}
dewyatt@4759
   614
dewyatt@4759
   615
static void
dewyatt@4759
   616
IME_UpdateInputLocale(SDL_VideoData *videodata)
dewyatt@4759
   617
{
dewyatt@4759
   618
    static HKL hklPrevious = 0;
dewyatt@4759
   619
    videodata->ime_hkl = GetKeyboardLayout(0);
dewyatt@4759
   620
    if (hklPrevious == videodata->ime_hkl)
dewyatt@4759
   621
        return;
dewyatt@4759
   622
dewyatt@4759
   623
    hklPrevious = videodata->ime_hkl;
dewyatt@4759
   624
}
dewyatt@4759
   625
dewyatt@4759
   626
static void
dewyatt@4759
   627
IME_ClearComposition(SDL_VideoData *videodata)
dewyatt@4759
   628
{
dewyatt@4759
   629
    HIMC himc = 0;
dewyatt@4759
   630
    if (!videodata->ime_initialized)
dewyatt@4759
   631
        return;
dewyatt@4759
   632
dewyatt@4759
   633
    himc = ImmGetContext(videodata->ime_hwnd_current);
dewyatt@4759
   634
    if (!himc)
dewyatt@4759
   635
        return;
dewyatt@4759
   636
dewyatt@4759
   637
    ImmNotifyIME(himc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
dewyatt@4759
   638
    if (videodata->ime_uiless)
dewyatt@4759
   639
        ImmSetCompositionString(himc, SCS_SETSTR, TEXT(""), sizeof(TCHAR), TEXT(""), sizeof(TCHAR));
dewyatt@4759
   640
dewyatt@4759
   641
    ImmNotifyIME(himc, NI_CLOSECANDIDATE, 0, 0);
dewyatt@4759
   642
    ImmReleaseContext(videodata->ime_hwnd_current, himc);
dewyatt@4759
   643
    SDL_SendEditingText("", 0, 0);
dewyatt@4759
   644
}
dewyatt@4759
   645
dewyatt@4759
   646
static void
dewyatt@4759
   647
IME_ClearEditing(SDL_VideoData *videodata)
dewyatt@4759
   648
{
dewyatt@4759
   649
dewyatt@4759
   650
}
dewyatt@4759
   651
dewyatt@4759
   652
static void
dewyatt@4759
   653
IME_GetCompositionString(SDL_VideoData *videodata, HIMC himc, DWORD string)
dewyatt@4759
   654
{
dewyatt@4759
   655
    LONG Length = ImmGetCompositionStringW(himc, string, videodata->ime_composition, sizeof(videodata->ime_composition));
dewyatt@4759
   656
    if (Length < 0)
dewyatt@4759
   657
        Length = 0;
dewyatt@4759
   658
dewyatt@4759
   659
    Length /= sizeof(videodata->ime_composition[0]);
dewyatt@4759
   660
    videodata->ime_cursor = LOWORD(ImmGetCompositionStringW(himc, GCS_CURSORPOS, 0, 0));
dewyatt@4759
   661
    if (videodata->ime_composition[videodata->ime_cursor] == 0x3000)
dewyatt@4759
   662
    {
dewyatt@4759
   663
        int i;
dewyatt@4759
   664
        for (i = videodata->ime_cursor + 1; i < Length; ++i)
dewyatt@4759
   665
            videodata->ime_composition[i - 1] = videodata->ime_composition[i];
dewyatt@4759
   666
dewyatt@4759
   667
        --Length;
dewyatt@4759
   668
    }
dewyatt@4759
   669
    videodata->ime_composition[Length] = 0;
dewyatt@4759
   670
}
dewyatt@4759
   671
dewyatt@4759
   672
static void
dewyatt@4759
   673
IME_SendInputEvent(SDL_VideoData *videodata)
dewyatt@4759
   674
{
dewyatt@4759
   675
    char *s = 0;
dewyatt@4759
   676
    s = WIN_StringToUTF8(videodata->ime_composition);
dewyatt@4759
   677
    SDL_SendKeyboardText(s);
dewyatt@4759
   678
    SDL_free(s);
dewyatt@4759
   679
dewyatt@4759
   680
    videodata->ime_composition[0] = 0;
dewyatt@4759
   681
    videodata->ime_readingstring[0] = 0;
dewyatt@4759
   682
    videodata->ime_cursor = 0;
dewyatt@4759
   683
}
dewyatt@4759
   684
dewyatt@4759
   685
static void
dewyatt@4759
   686
IME_SendEditingEvent(SDL_VideoData *videodata)
dewyatt@4759
   687
{
dewyatt@4759
   688
    char *s = 0;
dewyatt@4759
   689
    WCHAR wBuffer[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
dewyatt@4759
   690
    wBuffer[0] = 0;
dewyatt@4759
   691
    if (videodata->ime_readingstring[0])
dewyatt@4759
   692
    {
dewyatt@4759
   693
        size_t len = SDL_min(SDL_wcslen(videodata->ime_composition), (size_t)videodata->ime_cursor);
dewyatt@4759
   694
        SDL_wcslcpy(wBuffer, videodata->ime_composition, len + 1);
dewyatt@4759
   695
        SDL_wcslcat(wBuffer, videodata->ime_readingstring, sizeof(wBuffer));
dewyatt@4759
   696
        SDL_wcslcat(wBuffer, &videodata->ime_composition[len], sizeof(wBuffer) - len);
dewyatt@4759
   697
    }
dewyatt@4759
   698
    else
dewyatt@4759
   699
    {
dewyatt@4759
   700
        SDL_wcslcpy(wBuffer, videodata->ime_composition, sizeof(videodata->ime_composition));
dewyatt@4759
   701
    }
dewyatt@4759
   702
    s = WIN_StringToUTF8(wBuffer);
dewyatt@4759
   703
    SDL_SendEditingText(s, videodata->ime_cursor + SDL_wcslen(videodata->ime_readingstring), 0);
dewyatt@4759
   704
    SDL_free(s);
dewyatt@4759
   705
}
dewyatt@4759
   706
dewyatt@4752
   707
SDL_bool
dewyatt@4752
   708
IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
dewyatt@4752
   709
{
dewyatt@4752
   710
    SDL_bool trap = SDL_FALSE;
dewyatt@4752
   711
    HIMC himc = 0;
dewyatt@4752
   712
    if (!videodata->ime_initialized || !videodata->ime_available || !videodata->ime_enabled)
dewyatt@4752
   713
        return SDL_FALSE;
dewyatt@4752
   714
dewyatt@4752
   715
    switch (msg)
dewyatt@4752
   716
    {
dewyatt@4752
   717
    case WM_INPUTLANGCHANGE:
dewyatt@4759
   718
        //IME_InputLangChanged(videodata);
dewyatt@4752
   719
        break;
dewyatt@4752
   720
    case WM_IME_SETCONTEXT:
dewyatt@4752
   721
        *lParam = 0;
dewyatt@4752
   722
        break;
dewyatt@4752
   723
    case WM_IME_STARTCOMPOSITION:
dewyatt@4752
   724
        trap = SDL_TRUE;
dewyatt@4752
   725
        break;
dewyatt@4752
   726
    case WM_IME_COMPOSITION:
dewyatt@4752
   727
        trap = SDL_TRUE;
dewyatt@4752
   728
        himc = ImmGetContext(hwnd);
dewyatt@4752
   729
        if (*lParam & GCS_RESULTSTR)
dewyatt@4752
   730
        {
dewyatt@4759
   731
            IME_GetCompositionString(videodata, himc, GCS_RESULTSTR);
dewyatt@4759
   732
            IME_SendInputEvent(videodata);
dewyatt@4752
   733
        }
dewyatt@4752
   734
        if (*lParam & GCS_COMPSTR)
dewyatt@4752
   735
        {
dewyatt@4759
   736
            if (!videodata->ime_uiless)
dewyatt@4759
   737
                videodata->ime_readingstring[0] = 0;
dewyatt@4759
   738
dewyatt@4759
   739
            IME_GetCompositionString(videodata, himc, GCS_COMPSTR);
dewyatt@4759
   740
            IME_SendEditingEvent(videodata);
dewyatt@4752
   741
        }
dewyatt@4752
   742
        ImmReleaseContext(hwnd, himc);
dewyatt@4752
   743
        break;
dewyatt@4752
   744
    case WM_IME_ENDCOMPOSITION:
dewyatt@4759
   745
        videodata->ime_composition[0] = 0;
dewyatt@4759
   746
        videodata->ime_readingstring[0] = 0;
dewyatt@4759
   747
        videodata->ime_cursor = 0;
dewyatt@4759
   748
        SDL_SendEditingText("", 0, 0);
dewyatt@4752
   749
        break;
dewyatt@4752
   750
    case WM_IME_NOTIFY:
dewyatt@4752
   751
        switch (wParam)
dewyatt@4752
   752
        {
dewyatt@4752
   753
        case IMN_SETCONVERSIONMODE:
dewyatt@4752
   754
        case IMN_SETOPENSTATUS:
dewyatt@4759
   755
            IME_UpdateInputLocale(videodata);
dewyatt@4752
   756
            break;
dewyatt@4752
   757
        case IMN_OPENCANDIDATE:
dewyatt@4752
   758
        case IMN_CHANGECANDIDATE:
dewyatt@4752
   759
            trap = SDL_TRUE;
dewyatt@4752
   760
            break;
dewyatt@4752
   761
        case IMN_CLOSECANDIDATE:
dewyatt@4752
   762
            trap = SDL_TRUE;
dewyatt@4752
   763
            break;
dewyatt@4752
   764
        case IMN_PRIVATE:
dewyatt@4759
   765
            {
dewyatt@4759
   766
                DWORD dwId = IME_GetId(videodata, 0);
dewyatt@4759
   767
                IME_GetReadingString(videodata, hwnd);
dewyatt@4759
   768
                switch (dwId)
dewyatt@4759
   769
                {
dewyatt@4759
   770
                case IMEID_CHT_VER42:
dewyatt@4759
   771
                case IMEID_CHT_VER43:
dewyatt@4759
   772
                case IMEID_CHT_VER44:
dewyatt@4759
   773
                case IMEID_CHS_VER41:
dewyatt@4759
   774
                case IMEID_CHS_VER42:
dewyatt@4759
   775
                    if (*lParam == 1 || *lParam == 2)
dewyatt@4759
   776
                        trap = SDL_TRUE;
dewyatt@4759
   777
dewyatt@4759
   778
                    break;
dewyatt@4759
   779
                case IMEID_CHT_VER50:
dewyatt@4759
   780
                case IMEID_CHT_VER51:
dewyatt@4759
   781
                case IMEID_CHT_VER52:
dewyatt@4759
   782
                case IMEID_CHT_VER60:
dewyatt@4759
   783
                case IMEID_CHS_VER53:
dewyatt@4759
   784
                    if (*lParam == 16
dewyatt@4759
   785
                        || *lParam == 17
dewyatt@4759
   786
                        || *lParam == 26
dewyatt@4759
   787
                        || *lParam == 27
dewyatt@4759
   788
                        || *lParam == 28)
dewyatt@4759
   789
                        trap = SDL_TRUE;
dewyatt@4759
   790
                    break;
dewyatt@4759
   791
                }
dewyatt@4759
   792
            }
dewyatt@4752
   793
            break;
dewyatt@4752
   794
        default:
dewyatt@4752
   795
            trap = SDL_TRUE;
dewyatt@4752
   796
            break;
dewyatt@4752
   797
        }
dewyatt@4752
   798
        break;
dewyatt@4752
   799
    }
dewyatt@4752
   800
    return trap;
dewyatt@4752
   801
}
dewyatt@4752
   802
dewyatt@4759
   803
STDMETHODIMP_(ULONG) TSFSink_AddRef(TSFSink *sink)
dewyatt@4759
   804
{
dewyatt@4759
   805
    return ++sink->refcount;
dewyatt@4759
   806
}
dewyatt@4759
   807
dewyatt@4759
   808
STDMETHODIMP_(ULONG)TSFSink_Release(TSFSink *sink)
dewyatt@4759
   809
{
dewyatt@4759
   810
    --sink->refcount;
dewyatt@4759
   811
    if (sink->refcount == 0)
dewyatt@4759
   812
    {
dewyatt@4759
   813
        SDL_free(sink);
dewyatt@4759
   814
        return 0;
dewyatt@4759
   815
    }
dewyatt@4759
   816
    return sink->refcount;
dewyatt@4759
   817
}
dewyatt@4759
   818
dewyatt@4759
   819
STDMETHODIMP UIElementSink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv)
dewyatt@4759
   820
{
dewyatt@4759
   821
    if (!ppv)
dewyatt@4759
   822
        return E_INVALIDARG;
dewyatt@4759
   823
dewyatt@4759
   824
    *ppv = 0;
dewyatt@4759
   825
    if (SDL_IsEqualIID(riid, &IID_IUnknown))
dewyatt@4759
   826
        *ppv = (IUnknown *)sink;
dewyatt@4759
   827
    else if (SDL_IsEqualIID(riid, &IID_ITfUIElementSink))
dewyatt@4759
   828
        *ppv = (ITfUIElementSink *)sink;
dewyatt@4759
   829
dewyatt@4759
   830
    if (*ppv)
dewyatt@4759
   831
    {
dewyatt@4759
   832
        TSFSink_AddRef(sink);
dewyatt@4759
   833
        return S_OK;
dewyatt@4759
   834
    }
dewyatt@4759
   835
    return E_NOINTERFACE;
dewyatt@4759
   836
}
dewyatt@4759
   837
dewyatt@4759
   838
ITfUIElement *UILess_GetUIElement(SDL_VideoData *videodata, DWORD dwUIElementId)
dewyatt@4759
   839
{
dewyatt@4759
   840
    ITfUIElementMgr *puiem = 0;
dewyatt@4759
   841
    ITfUIElement *pelem = 0;
dewyatt@4759
   842
    ITfThreadMgrEx *threadmgrex = videodata->ime_threadmgrex;
dewyatt@4759
   843
dewyatt@4759
   844
    if (SUCCEEDED(threadmgrex->lpVtbl->QueryInterface(threadmgrex, &IID_ITfUIElementMgr, (LPVOID *)&puiem)))
dewyatt@4759
   845
    {
dewyatt@4759
   846
        puiem->lpVtbl->GetUIElement(puiem, dwUIElementId, &pelem);
dewyatt@4759
   847
        puiem->lpVtbl->Release(puiem);
dewyatt@4759
   848
    }
dewyatt@4759
   849
    return pelem;
dewyatt@4759
   850
}
dewyatt@4759
   851
dewyatt@4759
   852
STDMETHODIMP UIElementSink_BeginUIElement(TSFSink *sink, DWORD dwUIElementId, BOOL *pbShow)
dewyatt@4759
   853
{
dewyatt@4759
   854
    ITfUIElement *pElement = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
dewyatt@4759
   855
    ITfReadingInformationUIElement *preading = 0;
dewyatt@4759
   856
    ITfCandidateListUIElement *pcandidates = 0;
dewyatt@4759
   857
    SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
dewyatt@4759
   858
    if (!pElement)
dewyatt@4759
   859
        return E_INVALIDARG;
dewyatt@4759
   860
dewyatt@4759
   861
    *pbShow = FALSE;
dewyatt@4759
   862
    if (SUCCEEDED(pElement->lpVtbl->QueryInterface(pElement, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading)))
dewyatt@4759
   863
    {
dewyatt@4759
   864
        BSTR bstr;
dewyatt@4759
   865
        if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr)
dewyatt@4759
   866
        {
dewyatt@4759
   867
            WCHAR *s = (WCHAR *)bstr;
dewyatt@4759
   868
            SysFreeString(bstr);
dewyatt@4759
   869
        }
dewyatt@4759
   870
        preading->lpVtbl->Release(preading);
dewyatt@4759
   871
    }
dewyatt@4759
   872
    else if (SUCCEEDED(pElement->lpVtbl->QueryInterface(pElement, &IID_ITfCandidateListUIElement, (PVOID *)&pcandidates)))
dewyatt@4759
   873
    {
dewyatt@4759
   874
        pcandidates->lpVtbl->Release(pcandidates);
dewyatt@4759
   875
    }
dewyatt@4759
   876
    return S_OK;
dewyatt@4759
   877
}
dewyatt@4759
   878
dewyatt@4759
   879
STDMETHODIMP UIElementSink_UpdateUIElement(TSFSink *sink, DWORD dwUIElementId)
dewyatt@4759
   880
{
dewyatt@4759
   881
    ITfUIElement *pElement = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
dewyatt@4759
   882
    ITfReadingInformationUIElement *preading = 0;
dewyatt@4759
   883
    ITfCandidateListUIElement *pcandidates = 0;
dewyatt@4759
   884
    SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
dewyatt@4759
   885
    if (!pElement)
dewyatt@4759
   886
        return E_INVALIDARG;
dewyatt@4759
   887
dewyatt@4759
   888
    if (SUCCEEDED(pElement->lpVtbl->QueryInterface(pElement, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading)))
dewyatt@4759
   889
    {
dewyatt@4759
   890
        BSTR bstr;
dewyatt@4759
   891
        if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr)
dewyatt@4759
   892
        {
dewyatt@4759
   893
            WCHAR *s = (WCHAR *)bstr;
dewyatt@4759
   894
            SDL_wcslcpy(videodata->ime_readingstring, s, sizeof(videodata->ime_readingstring));
dewyatt@4759
   895
            IME_SendEditingEvent(videodata);
dewyatt@4759
   896
            SysFreeString(bstr);
dewyatt@4759
   897
        }
dewyatt@4759
   898
        preading->lpVtbl->Release(preading);
dewyatt@4759
   899
    }
dewyatt@4759
   900
    else if (SUCCEEDED(pElement->lpVtbl->QueryInterface(pElement, &IID_ITfCandidateListUIElement, (PVOID *)&pcandidates)))
dewyatt@4759
   901
    {
dewyatt@4759
   902
        pcandidates->lpVtbl->Release(pcandidates);
dewyatt@4759
   903
    }
dewyatt@4759
   904
    return S_OK;
dewyatt@4759
   905
}
dewyatt@4759
   906
dewyatt@4759
   907
STDMETHODIMP UIElementSink_EndUIElement(TSFSink *sink, DWORD dwUIElementId)
dewyatt@4759
   908
{
dewyatt@4759
   909
    ITfUIElement *pElement = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
dewyatt@4759
   910
    ITfReadingInformationUIElement *preading = 0;
dewyatt@4759
   911
    ITfCandidateListUIElement *pcandidates = 0;
dewyatt@4759
   912
    SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
dewyatt@4759
   913
    if (!pElement)
dewyatt@4759
   914
        return E_INVALIDARG;
dewyatt@4759
   915
dewyatt@4759
   916
    if (SUCCEEDED(pElement->lpVtbl->QueryInterface(pElement, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading)))
dewyatt@4759
   917
    {
dewyatt@4759
   918
        videodata->ime_readingstring[0] = 0;
dewyatt@4759
   919
        IME_SendEditingEvent(videodata);
dewyatt@4759
   920
        preading->lpVtbl->Release(preading);
dewyatt@4759
   921
    }
dewyatt@4759
   922
    else if (SUCCEEDED(pElement->lpVtbl->QueryInterface(pElement, &IID_ITfCandidateListUIElement, (PVOID *)&pcandidates)))
dewyatt@4759
   923
    {
dewyatt@4759
   924
        pcandidates->lpVtbl->Release(pcandidates);
dewyatt@4759
   925
    }
dewyatt@4759
   926
    return S_OK;
dewyatt@4759
   927
}
dewyatt@4759
   928
dewyatt@4759
   929
STDMETHODIMP IPPASink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv)
dewyatt@4759
   930
{
dewyatt@4759
   931
    if (!ppv)
dewyatt@4759
   932
        return E_INVALIDARG;
dewyatt@4759
   933
dewyatt@4759
   934
    *ppv = 0;
dewyatt@4759
   935
    if (SDL_IsEqualIID(riid, &IID_IUnknown))
dewyatt@4759
   936
        *ppv = (IUnknown *)sink;
dewyatt@4759
   937
    else if (SDL_IsEqualIID(riid, &IID_ITfInputProcessorProfileActivationSink))
dewyatt@4759
   938
        *ppv = (ITfInputProcessorProfileActivationSink *)sink;
dewyatt@4759
   939
dewyatt@4759
   940
    if (*ppv)
dewyatt@4759
   941
    {
dewyatt@4759
   942
        TSFSink_AddRef(sink);
dewyatt@4759
   943
        return S_OK;
dewyatt@4759
   944
    }
dewyatt@4759
   945
    return E_NOINTERFACE;
dewyatt@4759
   946
}
dewyatt@4759
   947
dewyatt@4759
   948
STDMETHODIMP IPPASink_OnActivated(TSFSink *sink, DWORD dwProfileType, LANGID langid, REFCLSID clsid, REFGUID catid, REFGUID guidProfile, HKL hkl, DWORD dwFlags)
dewyatt@4759
   949
{
dewyatt@4759
   950
    if (SDL_IsEqualIID(catid, &GUID_TFCAT_TIP_KEYBOARD) && (dwFlags & TF_IPSINK_FLAG_ACTIVE))
dewyatt@4759
   951
        IME_InputLangChanged((SDL_VideoData *)sink->data);
dewyatt@4759
   952
dewyatt@4759
   953
    return S_OK;
dewyatt@4759
   954
}
dewyatt@4759
   955
dewyatt@4759
   956
static void *vtUIElementSink[] = {
dewyatt@4759
   957
    (void *)(UIElementSink_QueryInterface),
dewyatt@4759
   958
    (void *)(TSFSink_AddRef),
dewyatt@4759
   959
    (void *)(TSFSink_Release),
dewyatt@4759
   960
    (void *)(UIElementSink_BeginUIElement),
dewyatt@4759
   961
    (void *)(UIElementSink_UpdateUIElement),
dewyatt@4759
   962
    (void *)(UIElementSink_EndUIElement)
dewyatt@4759
   963
};
dewyatt@4759
   964
dewyatt@4759
   965
static void *vtIPPASink[] = {
dewyatt@4759
   966
    (void *)(IPPASink_QueryInterface),
dewyatt@4759
   967
    (void *)(TSFSink_AddRef),
dewyatt@4759
   968
    (void *)(TSFSink_Release),
dewyatt@4759
   969
    (void *)(IPPASink_OnActivated)
dewyatt@4759
   970
};
dewyatt@4759
   971
dewyatt@4759
   972
static void
dewyatt@4759
   973
UILess_EnableUIUpdates(SDL_VideoData *videodata)
dewyatt@4759
   974
{
dewyatt@4759
   975
    ITfSource *source = 0;
dewyatt@4759
   976
    if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie != TF_INVALID_COOKIE)
dewyatt@4759
   977
        return;
dewyatt@4759
   978
dewyatt@4759
   979
    if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source)))
dewyatt@4759
   980
    {
dewyatt@4759
   981
        source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie);
dewyatt@4759
   982
        source->lpVtbl->Release(source);
dewyatt@4759
   983
    }
dewyatt@4759
   984
}
dewyatt@4759
   985
dewyatt@4759
   986
static void
dewyatt@4759
   987
UILess_DisableUIUpdates(SDL_VideoData *videodata)
dewyatt@4759
   988
{
dewyatt@4759
   989
    ITfSource *source = 0;
dewyatt@4759
   990
    if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie == TF_INVALID_COOKIE)
dewyatt@4759
   991
        return;
dewyatt@4759
   992
dewyatt@4759
   993
    if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source)))
dewyatt@4759
   994
    {
dewyatt@4759
   995
        source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie);
dewyatt@4759
   996
        videodata->ime_uielemsinkcookie = TF_INVALID_COOKIE;
dewyatt@4759
   997
        source->lpVtbl->Release(source);
dewyatt@4759
   998
    }
dewyatt@4759
   999
}
dewyatt@4759
  1000
dewyatt@4759
  1001
static SDL_bool
dewyatt@4759
  1002
UILess_SetupSinks(SDL_VideoData *videodata)
dewyatt@4759
  1003
{
dewyatt@4759
  1004
    TfClientId clientid = 0;
dewyatt@4759
  1005
    SDL_bool result = SDL_FALSE;
dewyatt@4759
  1006
    ITfSource *source = 0;
dewyatt@4759
  1007
    if (FAILED(CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgrEx, &videodata->ime_threadmgrex)))
dewyatt@4759
  1008
        return SDL_FALSE;
dewyatt@4759
  1009
dewyatt@4759
  1010
    if (FAILED(videodata->ime_threadmgrex->lpVtbl->ActivateEx(videodata->ime_threadmgrex, &clientid, TF_TMAE_UIELEMENTENABLEDONLY)))
dewyatt@4759
  1011
        return SDL_FALSE;
dewyatt@4759
  1012
dewyatt@4759
  1013
    videodata->ime_uielemsink = SDL_malloc(sizeof(TSFSink));
dewyatt@4759
  1014
    videodata->ime_ippasink = SDL_malloc(sizeof(TSFSink));
dewyatt@4759
  1015
dewyatt@4759
  1016
    videodata->ime_uielemsink->lpVtbl = vtUIElementSink;
dewyatt@4759
  1017
    videodata->ime_uielemsink->refcount = 1;
dewyatt@4759
  1018
    videodata->ime_uielemsink->data = videodata;
dewyatt@4759
  1019
dewyatt@4759
  1020
    videodata->ime_ippasink->lpVtbl = vtIPPASink;
dewyatt@4759
  1021
    videodata->ime_ippasink->refcount = 1;
dewyatt@4759
  1022
    videodata->ime_ippasink->data = videodata;
dewyatt@4759
  1023
dewyatt@4759
  1024
    if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source)))
dewyatt@4759
  1025
    {
dewyatt@4759
  1026
        if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie)))
dewyatt@4759
  1027
        {
dewyatt@4759
  1028
            if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfInputProcessorProfileActivationSink, (IUnknown *)videodata->ime_ippasink, &videodata->ime_alpnsinkcookie)))
dewyatt@4759
  1029
            {
dewyatt@4759
  1030
                result = SDL_TRUE;
dewyatt@4759
  1031
            }
dewyatt@4759
  1032
        }
dewyatt@4759
  1033
        source->lpVtbl->Release(source);
dewyatt@4759
  1034
    }
dewyatt@4759
  1035
    return result;
dewyatt@4759
  1036
}
dewyatt@4759
  1037
dewyatt@4759
  1038
#define SAFE_RELEASE(p)                             \
dewyatt@4759
  1039
{                                                   \
dewyatt@4759
  1040
    if (p) {                                        \
dewyatt@4759
  1041
        (p)->lpVtbl->Release((p));                  \
dewyatt@4759
  1042
        (p) = 0;                                    \
dewyatt@4759
  1043
    }                                               \
dewyatt@4759
  1044
}
dewyatt@4759
  1045
dewyatt@4759
  1046
static void
dewyatt@4759
  1047
UILess_ReleaseSinks(SDL_VideoData *videodata)
dewyatt@4759
  1048
{
dewyatt@4759
  1049
    ITfSource *Source = 0;
dewyatt@4759
  1050
    if (videodata->ime_threadmgrex && SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, &Source)))
dewyatt@4759
  1051
    {
dewyatt@4759
  1052
        Source->lpVtbl->UnadviseSink(Source, videodata->ime_uielemsinkcookie);
dewyatt@4759
  1053
        Source->lpVtbl->UnadviseSink(Source, videodata->ime_alpnsinkcookie);
dewyatt@4759
  1054
        SAFE_RELEASE(Source);
dewyatt@4759
  1055
        videodata->ime_threadmgrex->lpVtbl->Deactivate(videodata->ime_threadmgrex);
dewyatt@4759
  1056
        SAFE_RELEASE(videodata->ime_threadmgrex);
dewyatt@4759
  1057
        TSFSink_Release(videodata->ime_uielemsink);
dewyatt@4759
  1058
        videodata->ime_uielemsink = 0;
dewyatt@4759
  1059
        TSFSink_Release(videodata->ime_ippasink);
dewyatt@4759
  1060
        videodata->ime_ippasink = 0;
dewyatt@4759
  1061
    }
dewyatt@4759
  1062
}
dewyatt@4759
  1063
slouken@1895
  1064
/* vi: set ts=4 sw=4 expandtab: */