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