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