src/video/win32/SDL_win32keyboard.c
author dewyatt
Wed, 04 Aug 2010 23:17:12 -0400
changeset 4760 c40027ee6d47
parent 4759 863ba7d1f029
child 4763 518d1679d2d0
permissions -rw-r--r--
Updated to fit SDL style a little more.
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@4760
   323
    if (videodata->ime_himm32) {
dewyatt@4759
   324
        FreeLibrary(videodata->ime_himm32);
dewyatt@4759
   325
        videodata->ime_himm32 = 0;
dewyatt@4759
   326
    }
dewyatt@4760
   327
    if (videodata->ime_threadmgr) {
dewyatt@4759
   328
        videodata->ime_threadmgr->lpVtbl->Release(videodata->ime_threadmgr);
dewyatt@4759
   329
        videodata->ime_threadmgr = 0;
dewyatt@4752
   330
    }
dewyatt@4760
   331
    if (videodata->ime_com_initialized) {
dewyatt@4752
   332
        CoUninitialize();
dewyatt@4752
   333
        videodata->ime_com_initialized = SDL_FALSE;
dewyatt@4752
   334
    }
dewyatt@4752
   335
    videodata->ime_initialized = SDL_FALSE;
dewyatt@4752
   336
}
dewyatt@4752
   337
dewyatt@4759
   338
static void
dewyatt@4759
   339
IME_GetReadingString(SDL_VideoData *videodata, HWND hwnd)
dewyatt@4759
   340
{
dewyatt@4759
   341
    DWORD id = 0;
dewyatt@4759
   342
    HIMC himc = 0;
dewyatt@4759
   343
    WCHAR buffer[16];
dewyatt@4759
   344
    WCHAR *s = buffer;
dewyatt@4759
   345
    DWORD len = 0;
dewyatt@4759
   346
    DWORD err = 0;
dewyatt@4759
   347
    BOOL vertical = FALSE;
dewyatt@4759
   348
    UINT maxuilen = 0;
dewyatt@4759
   349
    static OSVERSIONINFOA osversion = {0};
dewyatt@4759
   350
    if (videodata->ime_uiless)
dewyatt@4759
   351
        return;
dewyatt@4759
   352
dewyatt@4759
   353
    videodata->ime_readingstring[0] = 0;
dewyatt@4760
   354
    if (!osversion.dwOSVersionInfoSize) {
dewyatt@4759
   355
        osversion.dwOSVersionInfoSize = sizeof(osversion);
dewyatt@4759
   356
        GetVersionExA(&osversion);
dewyatt@4759
   357
    }
dewyatt@4759
   358
    id = IME_GetId(videodata, 0);
dewyatt@4759
   359
    if (!id)
dewyatt@4759
   360
        return;
dewyatt@4759
   361
dewyatt@4759
   362
    himc = ImmGetContext(hwnd);
dewyatt@4759
   363
    if (!himc)
dewyatt@4759
   364
        return;
dewyatt@4759
   365
dewyatt@4760
   366
    if (videodata->GetReadingString) {
dewyatt@4759
   367
        len = videodata->GetReadingString(himc, 0, 0, &err, &vertical, &maxuilen);
dewyatt@4760
   368
        if (len) {
dewyatt@4759
   369
            if (len > SDL_arraysize(buffer))
dewyatt@4759
   370
                len = SDL_arraysize(buffer);
dewyatt@4759
   371
dewyatt@4759
   372
            len = videodata->GetReadingString(himc, len, s, &err, &vertical, &maxuilen);
dewyatt@4759
   373
        }
dewyatt@4759
   374
        SDL_wcslcpy(videodata->ime_readingstring, s, len);
dewyatt@4759
   375
    }
dewyatt@4760
   376
    else {
dewyatt@4759
   377
        LPINPUTCONTEXT2 lpimc = videodata->ImmLockIMC(himc);
dewyatt@4759
   378
        LPBYTE p = 0;
dewyatt@4759
   379
        s = 0;
dewyatt@4759
   380
        switch (id)
dewyatt@4759
   381
        {
dewyatt@4759
   382
        case IMEID_CHT_VER42:
dewyatt@4759
   383
        case IMEID_CHT_VER43:
dewyatt@4759
   384
        case IMEID_CHT_VER44:
dewyatt@4759
   385
            p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 24);
dewyatt@4759
   386
            if (!p)
dewyatt@4759
   387
                break;
dewyatt@4759
   388
dewyatt@4759
   389
            len = *(DWORD *)(p + 7*4 + 32*4);
dewyatt@4759
   390
            s = (WCHAR *)(p + 56);
dewyatt@4759
   391
            break;
dewyatt@4759
   392
        case IMEID_CHT_VER51:
dewyatt@4759
   393
        case IMEID_CHT_VER52:
dewyatt@4759
   394
        case IMEID_CHS_VER53:
dewyatt@4759
   395
            p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 4);
dewyatt@4759
   396
            if (!p)
dewyatt@4759
   397
                break;
dewyatt@4759
   398
dewyatt@4759
   399
            p = *(LPBYTE *)((LPBYTE)p + 1*4 + 5*4);
dewyatt@4759
   400
            if (!p)
dewyatt@4759
   401
                break;
dewyatt@4759
   402
dewyatt@4759
   403
            len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2);
dewyatt@4759
   404
            s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4);
dewyatt@4759
   405
            break;
dewyatt@4759
   406
        case IMEID_CHS_VER41:
dewyatt@4759
   407
            {
dewyatt@4759
   408
                int offset = (IME_GetId(videodata, 1) >= 0x00000002) ? 8 : 7;
dewyatt@4759
   409
                p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + offset * 4);
dewyatt@4759
   410
                if (!p)
dewyatt@4759
   411
                    break;
dewyatt@4759
   412
dewyatt@4759
   413
                len = *(DWORD *)(p + 7*4 + 16*2*4);
dewyatt@4759
   414
                s = (WCHAR *)(p + 6*4 + 16*2*1);
dewyatt@4759
   415
            }
dewyatt@4759
   416
            break;
dewyatt@4759
   417
        case IMEID_CHS_VER42:
dewyatt@4759
   418
            if (osversion.dwPlatformId != VER_PLATFORM_WIN32_NT)
dewyatt@4759
   419
                break;
dewyatt@4759
   420
dewyatt@4759
   421
            p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 1*4 + 1*4 + 6*4);
dewyatt@4759
   422
            if (!p)
dewyatt@4759
   423
                break;
dewyatt@4759
   424
dewyatt@4759
   425
            len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2);
dewyatt@4759
   426
            s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4);
dewyatt@4759
   427
            break;
dewyatt@4759
   428
        }
dewyatt@4759
   429
        if (s)
dewyatt@4759
   430
            SDL_wcslcpy(videodata->ime_readingstring, s, len + 1);
dewyatt@4760
   431
dewyatt@4759
   432
        videodata->ImmUnlockIMCC(lpimc->hPrivate);
dewyatt@4759
   433
        videodata->ImmUnlockIMC(himc);
dewyatt@4759
   434
    }
dewyatt@4759
   435
    ImmReleaseContext(hwnd, himc);
dewyatt@4759
   436
    IME_SendEditingEvent(videodata);
dewyatt@4759
   437
}
dewyatt@4759
   438
dewyatt@4759
   439
static void
dewyatt@4759
   440
IME_InputLangChanged(SDL_VideoData *videodata)
dewyatt@4759
   441
{
dewyatt@4760
   442
    UINT lang = PRIMLANG();
dewyatt@4759
   443
    HWND hwndime = 0;
dewyatt@4759
   444
    IME_UpdateInputLocale(videodata);
dewyatt@4759
   445
    IME_SetupAPI(videodata);
dewyatt@4760
   446
    if (lang != PRIMLANG()) {
dewyatt@4759
   447
        IME_ClearComposition(videodata);
dewyatt@4759
   448
    }
dewyatt@4759
   449
    hwndime = ImmGetDefaultIMEWnd(videodata->ime_hwnd_current);
dewyatt@4760
   450
    if (hwndime) {
dewyatt@4759
   451
        SendMessageA(hwndime, WM_IME_CONTROL, IMC_OPENSTATUSWINDOW, 0);
dewyatt@4759
   452
        SendMessageA(hwndime, WM_IME_CONTROL, IMC_CLOSESTATUSWINDOW, 0);
dewyatt@4759
   453
    }
dewyatt@4759
   454
}
dewyatt@4759
   455
dewyatt@4759
   456
static DWORD
dewyatt@4759
   457
IME_GetId(SDL_VideoData *videodata, UINT uIndex)
dewyatt@4759
   458
{
dewyatt@4759
   459
    static HKL hklprev = 0;
dewyatt@4759
   460
    static DWORD dwRet[2] = {0};
dewyatt@4759
   461
    DWORD dwVerSize = 0;
dewyatt@4759
   462
    DWORD dwVerHandle = 0;
dewyatt@4759
   463
    LPVOID lpVerBuffer = 0;
dewyatt@4759
   464
    LPVOID lpVerData = 0;
dewyatt@4759
   465
    UINT cbVerData = 0;
dewyatt@4759
   466
    char szTemp[256];
dewyatt@4759
   467
    HKL hkl = 0;
dewyatt@4759
   468
    DWORD dwLang = 0;
dewyatt@4759
   469
    if (uIndex >= sizeof(dwRet) / sizeof(dwRet[0]))
dewyatt@4759
   470
        return 0;
dewyatt@4759
   471
dewyatt@4759
   472
    hkl = videodata->ime_hkl;
dewyatt@4759
   473
    if (hklprev == hkl)
dewyatt@4759
   474
        return dwRet[uIndex];
dewyatt@4759
   475
dewyatt@4759
   476
    hklprev = hkl;
dewyatt@4759
   477
    dwLang = ((DWORD)hkl & 0xffff);
dewyatt@4760
   478
    if (videodata->ime_uiless && LANG() == LANG_CHT) {
dewyatt@4759
   479
        dwRet[0] = IMEID_CHT_VER_VISTA;
dewyatt@4759
   480
        dwRet[1] = 0;
dewyatt@4759
   481
        return dwRet[0];
dewyatt@4759
   482
    }
dewyatt@4759
   483
    if (hkl != CHT_HKL_NEW_PHONETIC
dewyatt@4759
   484
        && hkl != CHT_HKL_NEW_CHANG_JIE
dewyatt@4759
   485
        && hkl != CHT_HKL_NEW_QUICK
dewyatt@4759
   486
        && hkl != CHT_HKL_HK_CANTONESE
dewyatt@4760
   487
        && hkl != CHS_HKL) {
dewyatt@4759
   488
        dwRet[0] = dwRet[1] = 0;
dewyatt@4759
   489
        return dwRet[uIndex];
dewyatt@4759
   490
    }
dewyatt@4760
   491
    if (ImmGetIMEFileNameA(hkl, szTemp, sizeof(szTemp) - 1) <= 0) {
dewyatt@4759
   492
        dwRet[0] = dwRet[1] = 0;
dewyatt@4759
   493
        return dwRet[uIndex];
dewyatt@4759
   494
    }
dewyatt@4760
   495
    if (!videodata->GetReadingString) {
dewyatt@4759
   496
        #define LCID_INVARIANT MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT)
dewyatt@4759
   497
        if (CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME1, -1) != 2
dewyatt@4759
   498
            && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME2, -1) != 2
dewyatt@4759
   499
            && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME3, -1) != 2
dewyatt@4759
   500
            && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME1, -1) != 2
dewyatt@4760
   501
            && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME2, -1) != 2) {
dewyatt@4759
   502
            dwRet[0] = dwRet[1] = 0;
dewyatt@4759
   503
            return dwRet[uIndex];
dewyatt@4759
   504
        }
dewyatt@4759
   505
        #undef LCID_INVARIANT
dewyatt@4759
   506
        dwVerSize = GetFileVersionInfoSizeA(szTemp, &dwVerHandle);
dewyatt@4760
   507
        if (dwVerSize) {
dewyatt@4759
   508
            lpVerBuffer = SDL_malloc(dwVerSize);
dewyatt@4760
   509
            if (lpVerBuffer) {
dewyatt@4760
   510
                if (GetFileVersionInfoA(szTemp, dwVerHandle, dwVerSize, lpVerBuffer)) {
dewyatt@4760
   511
                    if (VerQueryValueA(lpVerBuffer, "\\", &lpVerData, &cbVerData)) {
dewyatt@4759
   512
                        #define pVerFixedInfo   ((VS_FIXEDFILEINFO FAR*)lpVerData)
dewyatt@4759
   513
                        DWORD dwVer = pVerFixedInfo->dwFileVersionMS;
dewyatt@4759
   514
                        dwVer = (dwVer & 0x00ff0000) << 8 | (dwVer & 0x000000ff) << 16;
dewyatt@4759
   515
                        if (videodata->GetReadingString ||
dewyatt@4759
   516
                            dwLang == LANG_CHT && (
dewyatt@4759
   517
                            dwVer == MAKEIMEVERSION(4, 2) ||
dewyatt@4759
   518
                            dwVer == MAKEIMEVERSION(4, 3) ||
dewyatt@4759
   519
                            dwVer == MAKEIMEVERSION(4, 4) ||
dewyatt@4759
   520
                            dwVer == MAKEIMEVERSION(5, 0) ||
dewyatt@4759
   521
                            dwVer == MAKEIMEVERSION(5, 1) ||
dewyatt@4759
   522
                            dwVer == MAKEIMEVERSION(5, 2) ||
dewyatt@4759
   523
                            dwVer == MAKEIMEVERSION(6, 0))
dewyatt@4759
   524
                            ||
dewyatt@4759
   525
                            dwLang == LANG_CHS && (
dewyatt@4759
   526
                            dwVer == MAKEIMEVERSION(4, 1) ||
dewyatt@4759
   527
                            dwVer == MAKEIMEVERSION(4, 2) ||
dewyatt@4760
   528
                            dwVer == MAKEIMEVERSION(5, 3))) {
dewyatt@4759
   529
                            dwRet[0] = dwVer | dwLang;
dewyatt@4759
   530
                            dwRet[1] = pVerFixedInfo->dwFileVersionLS;
dewyatt@4759
   531
                            SDL_free(lpVerBuffer);
dewyatt@4759
   532
                            return dwRet[0];
dewyatt@4759
   533
                        }
dewyatt@4759
   534
                        #undef pVerFixedInfo
dewyatt@4759
   535
                    }
dewyatt@4759
   536
                }
dewyatt@4759
   537
            }
dewyatt@4759
   538
            SDL_free(lpVerBuffer);
dewyatt@4759
   539
        }
dewyatt@4759
   540
    }
dewyatt@4759
   541
    dwRet[0] = dwRet[1] = 0;
dewyatt@4759
   542
    return dwRet[uIndex];
dewyatt@4759
   543
}
dewyatt@4759
   544
dewyatt@4759
   545
static void
dewyatt@4759
   546
IME_SetupAPI(SDL_VideoData *videodata)
dewyatt@4759
   547
{
dewyatt@4759
   548
    char ime_file[MAX_PATH + 1];
dewyatt@4759
   549
    HMODULE hime = 0;
dewyatt@4759
   550
    HKL hkl = 0;
dewyatt@4759
   551
    videodata->GetReadingString = 0;
dewyatt@4759
   552
    videodata->ShowReadingWindow = 0;
dewyatt@4759
   553
    if (videodata->ime_uiless)
dewyatt@4759
   554
        return;
dewyatt@4759
   555
dewyatt@4759
   556
    hkl = videodata->ime_hkl;
dewyatt@4759
   557
    if (ImmGetIMEFileNameA(hkl, ime_file, sizeof(ime_file) - 1) <= 0)
dewyatt@4759
   558
        return;
dewyatt@4759
   559
dewyatt@4759
   560
    hime = LoadLibraryA(ime_file);
dewyatt@4759
   561
    if (!hime)
dewyatt@4759
   562
        return;
dewyatt@4759
   563
dewyatt@4759
   564
    videodata->GetReadingString = (UINT (WINAPI *)(HIMC, UINT, LPWSTR, PINT, BOOL*, PUINT))
dewyatt@4759
   565
        GetProcAddress(hime, "GetReadingString");
dewyatt@4759
   566
    videodata->ShowReadingWindow = (BOOL (WINAPI *)(HIMC, BOOL))
dewyatt@4759
   567
        GetProcAddress(hime, "ShowReadingWindow");
dewyatt@4759
   568
dewyatt@4760
   569
    if (videodata->ShowReadingWindow) {
dewyatt@4759
   570
        HIMC himc = ImmGetContext(videodata->ime_hwnd_current);
dewyatt@4760
   571
        if (himc) {
dewyatt@4759
   572
            videodata->ShowReadingWindow(himc, FALSE);
dewyatt@4759
   573
            ImmReleaseContext(videodata->ime_hwnd_current, himc);
dewyatt@4759
   574
        }
dewyatt@4759
   575
    }
dewyatt@4759
   576
}
dewyatt@4759
   577
dewyatt@4759
   578
static void
dewyatt@4759
   579
IME_SetWindow(SDL_VideoData* videodata, HWND hwnd)
dewyatt@4759
   580
{
dewyatt@4759
   581
    videodata->ime_hwnd_current = hwnd;
dewyatt@4759
   582
    if (videodata->ime_threadmgr) {
dewyatt@4759
   583
        struct ITfDocumentMgr *document_mgr = 0;
dewyatt@4759
   584
        if (SUCCEEDED(videodata->ime_threadmgr->lpVtbl->AssociateFocus(videodata->ime_threadmgr, hwnd, NULL, &document_mgr))) {
dewyatt@4759
   585
            if (document_mgr)
dewyatt@4759
   586
                document_mgr->lpVtbl->Release(document_mgr);
dewyatt@4759
   587
        }
dewyatt@4759
   588
    }
dewyatt@4759
   589
}
dewyatt@4759
   590
dewyatt@4759
   591
static void
dewyatt@4759
   592
IME_UpdateInputLocale(SDL_VideoData *videodata)
dewyatt@4759
   593
{
dewyatt@4760
   594
    static HKL hklprev = 0;
dewyatt@4759
   595
    videodata->ime_hkl = GetKeyboardLayout(0);
dewyatt@4760
   596
    if (hklprev == videodata->ime_hkl)
dewyatt@4759
   597
        return;
dewyatt@4759
   598
dewyatt@4760
   599
    hklprev = videodata->ime_hkl;
dewyatt@4759
   600
}
dewyatt@4759
   601
dewyatt@4759
   602
static void
dewyatt@4759
   603
IME_ClearComposition(SDL_VideoData *videodata)
dewyatt@4759
   604
{
dewyatt@4759
   605
    HIMC himc = 0;
dewyatt@4759
   606
    if (!videodata->ime_initialized)
dewyatt@4759
   607
        return;
dewyatt@4759
   608
dewyatt@4759
   609
    himc = ImmGetContext(videodata->ime_hwnd_current);
dewyatt@4759
   610
    if (!himc)
dewyatt@4759
   611
        return;
dewyatt@4759
   612
dewyatt@4759
   613
    ImmNotifyIME(himc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
dewyatt@4759
   614
    if (videodata->ime_uiless)
dewyatt@4759
   615
        ImmSetCompositionString(himc, SCS_SETSTR, TEXT(""), sizeof(TCHAR), TEXT(""), sizeof(TCHAR));
dewyatt@4759
   616
dewyatt@4759
   617
    ImmNotifyIME(himc, NI_CLOSECANDIDATE, 0, 0);
dewyatt@4759
   618
    ImmReleaseContext(videodata->ime_hwnd_current, himc);
dewyatt@4759
   619
    SDL_SendEditingText("", 0, 0);
dewyatt@4759
   620
}
dewyatt@4759
   621
dewyatt@4759
   622
static void
dewyatt@4759
   623
IME_ClearEditing(SDL_VideoData *videodata)
dewyatt@4759
   624
{
dewyatt@4759
   625
dewyatt@4759
   626
}
dewyatt@4759
   627
dewyatt@4759
   628
static void
dewyatt@4759
   629
IME_GetCompositionString(SDL_VideoData *videodata, HIMC himc, DWORD string)
dewyatt@4759
   630
{
dewyatt@4760
   631
    LONG length = ImmGetCompositionStringW(himc, string, videodata->ime_composition, sizeof(videodata->ime_composition));
dewyatt@4760
   632
    if (length < 0)
dewyatt@4760
   633
        length = 0;
dewyatt@4759
   634
dewyatt@4760
   635
    length /= sizeof(videodata->ime_composition[0]);
dewyatt@4759
   636
    videodata->ime_cursor = LOWORD(ImmGetCompositionStringW(himc, GCS_CURSORPOS, 0, 0));
dewyatt@4760
   637
    if (videodata->ime_composition[videodata->ime_cursor] == 0x3000) {
dewyatt@4759
   638
        int i;
dewyatt@4760
   639
        for (i = videodata->ime_cursor + 1; i < length; ++i)
dewyatt@4759
   640
            videodata->ime_composition[i - 1] = videodata->ime_composition[i];
dewyatt@4759
   641
dewyatt@4760
   642
        --length;
dewyatt@4759
   643
    }
dewyatt@4760
   644
    videodata->ime_composition[length] = 0;
dewyatt@4759
   645
}
dewyatt@4759
   646
dewyatt@4759
   647
static void
dewyatt@4759
   648
IME_SendInputEvent(SDL_VideoData *videodata)
dewyatt@4759
   649
{
dewyatt@4759
   650
    char *s = 0;
dewyatt@4759
   651
    s = WIN_StringToUTF8(videodata->ime_composition);
dewyatt@4759
   652
    SDL_SendKeyboardText(s);
dewyatt@4759
   653
    SDL_free(s);
dewyatt@4759
   654
dewyatt@4759
   655
    videodata->ime_composition[0] = 0;
dewyatt@4759
   656
    videodata->ime_readingstring[0] = 0;
dewyatt@4759
   657
    videodata->ime_cursor = 0;
dewyatt@4759
   658
}
dewyatt@4759
   659
dewyatt@4759
   660
static void
dewyatt@4759
   661
IME_SendEditingEvent(SDL_VideoData *videodata)
dewyatt@4759
   662
{
dewyatt@4759
   663
    char *s = 0;
dewyatt@4760
   664
    WCHAR buffer[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
dewyatt@4760
   665
    buffer[0] = 0;
dewyatt@4760
   666
    if (videodata->ime_readingstring[0]) {
dewyatt@4759
   667
        size_t len = SDL_min(SDL_wcslen(videodata->ime_composition), (size_t)videodata->ime_cursor);
dewyatt@4760
   668
        SDL_wcslcpy(buffer, videodata->ime_composition, len + 1);
dewyatt@4760
   669
        SDL_wcslcat(buffer, videodata->ime_readingstring, sizeof(buffer));
dewyatt@4760
   670
        SDL_wcslcat(buffer, &videodata->ime_composition[len], sizeof(buffer) - len);
dewyatt@4759
   671
    }
dewyatt@4760
   672
    else {
dewyatt@4760
   673
        SDL_wcslcpy(buffer, videodata->ime_composition, sizeof(videodata->ime_composition));
dewyatt@4759
   674
    }
dewyatt@4760
   675
    s = WIN_StringToUTF8(buffer);
dewyatt@4759
   676
    SDL_SendEditingText(s, videodata->ime_cursor + SDL_wcslen(videodata->ime_readingstring), 0);
dewyatt@4759
   677
    SDL_free(s);
dewyatt@4759
   678
}
dewyatt@4759
   679
dewyatt@4752
   680
SDL_bool
dewyatt@4752
   681
IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
dewyatt@4752
   682
{
dewyatt@4752
   683
    SDL_bool trap = SDL_FALSE;
dewyatt@4752
   684
    HIMC himc = 0;
dewyatt@4752
   685
    if (!videodata->ime_initialized || !videodata->ime_available || !videodata->ime_enabled)
dewyatt@4752
   686
        return SDL_FALSE;
dewyatt@4752
   687
dewyatt@4752
   688
    switch (msg)
dewyatt@4752
   689
    {
dewyatt@4752
   690
    case WM_INPUTLANGCHANGE:
dewyatt@4759
   691
        //IME_InputLangChanged(videodata);
dewyatt@4752
   692
        break;
dewyatt@4752
   693
    case WM_IME_SETCONTEXT:
dewyatt@4752
   694
        *lParam = 0;
dewyatt@4752
   695
        break;
dewyatt@4752
   696
    case WM_IME_STARTCOMPOSITION:
dewyatt@4752
   697
        trap = SDL_TRUE;
dewyatt@4752
   698
        break;
dewyatt@4752
   699
    case WM_IME_COMPOSITION:
dewyatt@4752
   700
        trap = SDL_TRUE;
dewyatt@4752
   701
        himc = ImmGetContext(hwnd);
dewyatt@4760
   702
        if (*lParam & GCS_RESULTSTR) {
dewyatt@4759
   703
            IME_GetCompositionString(videodata, himc, GCS_RESULTSTR);
dewyatt@4759
   704
            IME_SendInputEvent(videodata);
dewyatt@4752
   705
        }
dewyatt@4760
   706
        if (*lParam & GCS_COMPSTR) {
dewyatt@4759
   707
            if (!videodata->ime_uiless)
dewyatt@4759
   708
                videodata->ime_readingstring[0] = 0;
dewyatt@4759
   709
dewyatt@4759
   710
            IME_GetCompositionString(videodata, himc, GCS_COMPSTR);
dewyatt@4759
   711
            IME_SendEditingEvent(videodata);
dewyatt@4752
   712
        }
dewyatt@4752
   713
        ImmReleaseContext(hwnd, himc);
dewyatt@4752
   714
        break;
dewyatt@4752
   715
    case WM_IME_ENDCOMPOSITION:
dewyatt@4759
   716
        videodata->ime_composition[0] = 0;
dewyatt@4759
   717
        videodata->ime_readingstring[0] = 0;
dewyatt@4759
   718
        videodata->ime_cursor = 0;
dewyatt@4759
   719
        SDL_SendEditingText("", 0, 0);
dewyatt@4752
   720
        break;
dewyatt@4752
   721
    case WM_IME_NOTIFY:
dewyatt@4752
   722
        switch (wParam)
dewyatt@4752
   723
        {
dewyatt@4752
   724
        case IMN_SETCONVERSIONMODE:
dewyatt@4752
   725
        case IMN_SETOPENSTATUS:
dewyatt@4759
   726
            IME_UpdateInputLocale(videodata);
dewyatt@4752
   727
            break;
dewyatt@4752
   728
        case IMN_OPENCANDIDATE:
dewyatt@4752
   729
        case IMN_CHANGECANDIDATE:
dewyatt@4752
   730
            trap = SDL_TRUE;
dewyatt@4752
   731
            break;
dewyatt@4752
   732
        case IMN_CLOSECANDIDATE:
dewyatt@4752
   733
            trap = SDL_TRUE;
dewyatt@4752
   734
            break;
dewyatt@4752
   735
        case IMN_PRIVATE:
dewyatt@4759
   736
            {
dewyatt@4759
   737
                DWORD dwId = IME_GetId(videodata, 0);
dewyatt@4759
   738
                IME_GetReadingString(videodata, hwnd);
dewyatt@4759
   739
                switch (dwId)
dewyatt@4759
   740
                {
dewyatt@4759
   741
                case IMEID_CHT_VER42:
dewyatt@4759
   742
                case IMEID_CHT_VER43:
dewyatt@4759
   743
                case IMEID_CHT_VER44:
dewyatt@4759
   744
                case IMEID_CHS_VER41:
dewyatt@4759
   745
                case IMEID_CHS_VER42:
dewyatt@4759
   746
                    if (*lParam == 1 || *lParam == 2)
dewyatt@4759
   747
                        trap = SDL_TRUE;
dewyatt@4759
   748
dewyatt@4759
   749
                    break;
dewyatt@4759
   750
                case IMEID_CHT_VER50:
dewyatt@4759
   751
                case IMEID_CHT_VER51:
dewyatt@4759
   752
                case IMEID_CHT_VER52:
dewyatt@4759
   753
                case IMEID_CHT_VER60:
dewyatt@4759
   754
                case IMEID_CHS_VER53:
dewyatt@4759
   755
                    if (*lParam == 16
dewyatt@4759
   756
                        || *lParam == 17
dewyatt@4759
   757
                        || *lParam == 26
dewyatt@4759
   758
                        || *lParam == 27
dewyatt@4759
   759
                        || *lParam == 28)
dewyatt@4759
   760
                        trap = SDL_TRUE;
dewyatt@4759
   761
                    break;
dewyatt@4759
   762
                }
dewyatt@4759
   763
            }
dewyatt@4752
   764
            break;
dewyatt@4752
   765
        default:
dewyatt@4752
   766
            trap = SDL_TRUE;
dewyatt@4752
   767
            break;
dewyatt@4752
   768
        }
dewyatt@4752
   769
        break;
dewyatt@4752
   770
    }
dewyatt@4752
   771
    return trap;
dewyatt@4752
   772
}
dewyatt@4752
   773
dewyatt@4759
   774
STDMETHODIMP_(ULONG) TSFSink_AddRef(TSFSink *sink)
dewyatt@4759
   775
{
dewyatt@4759
   776
    return ++sink->refcount;
dewyatt@4759
   777
}
dewyatt@4759
   778
dewyatt@4759
   779
STDMETHODIMP_(ULONG)TSFSink_Release(TSFSink *sink)
dewyatt@4759
   780
{
dewyatt@4759
   781
    --sink->refcount;
dewyatt@4759
   782
    if (sink->refcount == 0)
dewyatt@4759
   783
    {
dewyatt@4759
   784
        SDL_free(sink);
dewyatt@4759
   785
        return 0;
dewyatt@4759
   786
    }
dewyatt@4759
   787
    return sink->refcount;
dewyatt@4759
   788
}
dewyatt@4759
   789
dewyatt@4759
   790
STDMETHODIMP UIElementSink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv)
dewyatt@4759
   791
{
dewyatt@4759
   792
    if (!ppv)
dewyatt@4759
   793
        return E_INVALIDARG;
dewyatt@4759
   794
dewyatt@4759
   795
    *ppv = 0;
dewyatt@4759
   796
    if (SDL_IsEqualIID(riid, &IID_IUnknown))
dewyatt@4759
   797
        *ppv = (IUnknown *)sink;
dewyatt@4759
   798
    else if (SDL_IsEqualIID(riid, &IID_ITfUIElementSink))
dewyatt@4759
   799
        *ppv = (ITfUIElementSink *)sink;
dewyatt@4759
   800
dewyatt@4760
   801
    if (*ppv) {
dewyatt@4759
   802
        TSFSink_AddRef(sink);
dewyatt@4759
   803
        return S_OK;
dewyatt@4759
   804
    }
dewyatt@4759
   805
    return E_NOINTERFACE;
dewyatt@4759
   806
}
dewyatt@4759
   807
dewyatt@4759
   808
ITfUIElement *UILess_GetUIElement(SDL_VideoData *videodata, DWORD dwUIElementId)
dewyatt@4759
   809
{
dewyatt@4759
   810
    ITfUIElementMgr *puiem = 0;
dewyatt@4759
   811
    ITfUIElement *pelem = 0;
dewyatt@4759
   812
    ITfThreadMgrEx *threadmgrex = videodata->ime_threadmgrex;
dewyatt@4759
   813
dewyatt@4760
   814
    if (SUCCEEDED(threadmgrex->lpVtbl->QueryInterface(threadmgrex, &IID_ITfUIElementMgr, (LPVOID *)&puiem))) {
dewyatt@4759
   815
        puiem->lpVtbl->GetUIElement(puiem, dwUIElementId, &pelem);
dewyatt@4759
   816
        puiem->lpVtbl->Release(puiem);
dewyatt@4759
   817
    }
dewyatt@4759
   818
    return pelem;
dewyatt@4759
   819
}
dewyatt@4759
   820
dewyatt@4759
   821
STDMETHODIMP UIElementSink_BeginUIElement(TSFSink *sink, DWORD dwUIElementId, BOOL *pbShow)
dewyatt@4759
   822
{
dewyatt@4760
   823
    ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
dewyatt@4759
   824
    ITfReadingInformationUIElement *preading = 0;
dewyatt@4759
   825
    SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
dewyatt@4760
   826
    if (!element)
dewyatt@4759
   827
        return E_INVALIDARG;
dewyatt@4759
   828
dewyatt@4759
   829
    *pbShow = FALSE;
dewyatt@4760
   830
    if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
dewyatt@4759
   831
        BSTR bstr;
dewyatt@4760
   832
        if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr) {
dewyatt@4759
   833
            WCHAR *s = (WCHAR *)bstr;
dewyatt@4759
   834
            SysFreeString(bstr);
dewyatt@4759
   835
        }
dewyatt@4759
   836
        preading->lpVtbl->Release(preading);
dewyatt@4759
   837
    }
dewyatt@4759
   838
    return S_OK;
dewyatt@4759
   839
}
dewyatt@4759
   840
dewyatt@4759
   841
STDMETHODIMP UIElementSink_UpdateUIElement(TSFSink *sink, DWORD dwUIElementId)
dewyatt@4759
   842
{
dewyatt@4760
   843
    ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
dewyatt@4759
   844
    ITfReadingInformationUIElement *preading = 0;
dewyatt@4759
   845
    SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
dewyatt@4760
   846
    if (!element)
dewyatt@4759
   847
        return E_INVALIDARG;
dewyatt@4759
   848
dewyatt@4760
   849
    if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
dewyatt@4759
   850
        BSTR bstr;
dewyatt@4760
   851
        if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr) {
dewyatt@4759
   852
            WCHAR *s = (WCHAR *)bstr;
dewyatt@4759
   853
            SDL_wcslcpy(videodata->ime_readingstring, s, sizeof(videodata->ime_readingstring));
dewyatt@4759
   854
            IME_SendEditingEvent(videodata);
dewyatt@4759
   855
            SysFreeString(bstr);
dewyatt@4759
   856
        }
dewyatt@4759
   857
        preading->lpVtbl->Release(preading);
dewyatt@4759
   858
    }
dewyatt@4759
   859
    return S_OK;
dewyatt@4759
   860
}
dewyatt@4759
   861
dewyatt@4759
   862
STDMETHODIMP UIElementSink_EndUIElement(TSFSink *sink, DWORD dwUIElementId)
dewyatt@4759
   863
{
dewyatt@4760
   864
    ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
dewyatt@4759
   865
    ITfReadingInformationUIElement *preading = 0;
dewyatt@4759
   866
    SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
dewyatt@4760
   867
    if (!element)
dewyatt@4759
   868
        return E_INVALIDARG;
dewyatt@4759
   869
dewyatt@4760
   870
    if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
dewyatt@4759
   871
        videodata->ime_readingstring[0] = 0;
dewyatt@4759
   872
        IME_SendEditingEvent(videodata);
dewyatt@4759
   873
        preading->lpVtbl->Release(preading);
dewyatt@4759
   874
    }
dewyatt@4759
   875
    return S_OK;
dewyatt@4759
   876
}
dewyatt@4759
   877
dewyatt@4759
   878
STDMETHODIMP IPPASink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv)
dewyatt@4759
   879
{
dewyatt@4759
   880
    if (!ppv)
dewyatt@4759
   881
        return E_INVALIDARG;
dewyatt@4759
   882
dewyatt@4759
   883
    *ppv = 0;
dewyatt@4759
   884
    if (SDL_IsEqualIID(riid, &IID_IUnknown))
dewyatt@4759
   885
        *ppv = (IUnknown *)sink;
dewyatt@4759
   886
    else if (SDL_IsEqualIID(riid, &IID_ITfInputProcessorProfileActivationSink))
dewyatt@4759
   887
        *ppv = (ITfInputProcessorProfileActivationSink *)sink;
dewyatt@4759
   888
dewyatt@4760
   889
    if (*ppv) {
dewyatt@4759
   890
        TSFSink_AddRef(sink);
dewyatt@4759
   891
        return S_OK;
dewyatt@4759
   892
    }
dewyatt@4759
   893
    return E_NOINTERFACE;
dewyatt@4759
   894
}
dewyatt@4759
   895
dewyatt@4759
   896
STDMETHODIMP IPPASink_OnActivated(TSFSink *sink, DWORD dwProfileType, LANGID langid, REFCLSID clsid, REFGUID catid, REFGUID guidProfile, HKL hkl, DWORD dwFlags)
dewyatt@4759
   897
{
dewyatt@4759
   898
    if (SDL_IsEqualIID(catid, &GUID_TFCAT_TIP_KEYBOARD) && (dwFlags & TF_IPSINK_FLAG_ACTIVE))
dewyatt@4759
   899
        IME_InputLangChanged((SDL_VideoData *)sink->data);
dewyatt@4759
   900
dewyatt@4759
   901
    return S_OK;
dewyatt@4759
   902
}
dewyatt@4759
   903
dewyatt@4759
   904
static void *vtUIElementSink[] = {
dewyatt@4759
   905
    (void *)(UIElementSink_QueryInterface),
dewyatt@4759
   906
    (void *)(TSFSink_AddRef),
dewyatt@4759
   907
    (void *)(TSFSink_Release),
dewyatt@4759
   908
    (void *)(UIElementSink_BeginUIElement),
dewyatt@4759
   909
    (void *)(UIElementSink_UpdateUIElement),
dewyatt@4759
   910
    (void *)(UIElementSink_EndUIElement)
dewyatt@4759
   911
};
dewyatt@4759
   912
dewyatt@4759
   913
static void *vtIPPASink[] = {
dewyatt@4759
   914
    (void *)(IPPASink_QueryInterface),
dewyatt@4759
   915
    (void *)(TSFSink_AddRef),
dewyatt@4759
   916
    (void *)(TSFSink_Release),
dewyatt@4759
   917
    (void *)(IPPASink_OnActivated)
dewyatt@4759
   918
};
dewyatt@4759
   919
dewyatt@4759
   920
static void
dewyatt@4759
   921
UILess_EnableUIUpdates(SDL_VideoData *videodata)
dewyatt@4759
   922
{
dewyatt@4759
   923
    ITfSource *source = 0;
dewyatt@4759
   924
    if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie != TF_INVALID_COOKIE)
dewyatt@4759
   925
        return;
dewyatt@4759
   926
dewyatt@4760
   927
    if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
dewyatt@4759
   928
        source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie);
dewyatt@4759
   929
        source->lpVtbl->Release(source);
dewyatt@4759
   930
    }
dewyatt@4759
   931
}
dewyatt@4759
   932
dewyatt@4759
   933
static void
dewyatt@4759
   934
UILess_DisableUIUpdates(SDL_VideoData *videodata)
dewyatt@4759
   935
{
dewyatt@4759
   936
    ITfSource *source = 0;
dewyatt@4759
   937
    if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie == TF_INVALID_COOKIE)
dewyatt@4759
   938
        return;
dewyatt@4759
   939
dewyatt@4760
   940
    if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
dewyatt@4759
   941
        source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie);
dewyatt@4759
   942
        videodata->ime_uielemsinkcookie = TF_INVALID_COOKIE;
dewyatt@4759
   943
        source->lpVtbl->Release(source);
dewyatt@4759
   944
    }
dewyatt@4759
   945
}
dewyatt@4759
   946
dewyatt@4759
   947
static SDL_bool
dewyatt@4759
   948
UILess_SetupSinks(SDL_VideoData *videodata)
dewyatt@4759
   949
{
dewyatt@4759
   950
    TfClientId clientid = 0;
dewyatt@4759
   951
    SDL_bool result = SDL_FALSE;
dewyatt@4759
   952
    ITfSource *source = 0;
dewyatt@4759
   953
    if (FAILED(CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgrEx, &videodata->ime_threadmgrex)))
dewyatt@4759
   954
        return SDL_FALSE;
dewyatt@4759
   955
dewyatt@4759
   956
    if (FAILED(videodata->ime_threadmgrex->lpVtbl->ActivateEx(videodata->ime_threadmgrex, &clientid, TF_TMAE_UIELEMENTENABLEDONLY)))
dewyatt@4759
   957
        return SDL_FALSE;
dewyatt@4759
   958
dewyatt@4759
   959
    videodata->ime_uielemsink = SDL_malloc(sizeof(TSFSink));
dewyatt@4759
   960
    videodata->ime_ippasink = SDL_malloc(sizeof(TSFSink));
dewyatt@4759
   961
dewyatt@4759
   962
    videodata->ime_uielemsink->lpVtbl = vtUIElementSink;
dewyatt@4759
   963
    videodata->ime_uielemsink->refcount = 1;
dewyatt@4759
   964
    videodata->ime_uielemsink->data = videodata;
dewyatt@4759
   965
dewyatt@4759
   966
    videodata->ime_ippasink->lpVtbl = vtIPPASink;
dewyatt@4759
   967
    videodata->ime_ippasink->refcount = 1;
dewyatt@4759
   968
    videodata->ime_ippasink->data = videodata;
dewyatt@4759
   969
dewyatt@4760
   970
    if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
dewyatt@4760
   971
        if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie))) {
dewyatt@4760
   972
            if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfInputProcessorProfileActivationSink, (IUnknown *)videodata->ime_ippasink, &videodata->ime_alpnsinkcookie))) {
dewyatt@4759
   973
                result = SDL_TRUE;
dewyatt@4759
   974
            }
dewyatt@4759
   975
        }
dewyatt@4759
   976
        source->lpVtbl->Release(source);
dewyatt@4759
   977
    }
dewyatt@4759
   978
    return result;
dewyatt@4759
   979
}
dewyatt@4759
   980
dewyatt@4759
   981
#define SAFE_RELEASE(p)                             \
dewyatt@4759
   982
{                                                   \
dewyatt@4759
   983
    if (p) {                                        \
dewyatt@4759
   984
        (p)->lpVtbl->Release((p));                  \
dewyatt@4759
   985
        (p) = 0;                                    \
dewyatt@4759
   986
    }                                               \
dewyatt@4759
   987
}
dewyatt@4759
   988
dewyatt@4759
   989
static void
dewyatt@4759
   990
UILess_ReleaseSinks(SDL_VideoData *videodata)
dewyatt@4759
   991
{
dewyatt@4760
   992
    ITfSource *source = 0;
dewyatt@4760
   993
    if (videodata->ime_threadmgrex && SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, &source))) {
dewyatt@4760
   994
        source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie);
dewyatt@4760
   995
        source->lpVtbl->UnadviseSink(source, videodata->ime_alpnsinkcookie);
dewyatt@4760
   996
        SAFE_RELEASE(source);
dewyatt@4759
   997
        videodata->ime_threadmgrex->lpVtbl->Deactivate(videodata->ime_threadmgrex);
dewyatt@4759
   998
        SAFE_RELEASE(videodata->ime_threadmgrex);
dewyatt@4759
   999
        TSFSink_Release(videodata->ime_uielemsink);
dewyatt@4759
  1000
        videodata->ime_uielemsink = 0;
dewyatt@4759
  1001
        TSFSink_Release(videodata->ime_ippasink);
dewyatt@4759
  1002
        videodata->ime_ippasink = 0;
dewyatt@4759
  1003
    }
dewyatt@4759
  1004
}
dewyatt@4759
  1005
slouken@1895
  1006
/* vi: set ts=4 sw=4 expandtab: */