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