comparison src/video/windows/SDL_windowskeyboard.c @ 5062:e8916fe9cfc8

Fixed bug #925 Changed "win32" to "windows"
author Sam Lantinga <slouken@libsdl.org>
date Thu, 20 Jan 2011 18:04:05 -0800
parents src/video/win32/SDL_win32keyboard.c@3617cec7f0ee
children 25d4feb7c127
comparison
equal deleted inserted replaced
5061:9e9940eae455 5062:e8916fe9cfc8
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_windowsvideo.h"
25
26 #include "../../events/SDL_keyboard_c.h"
27 #include "../../events/scancodes_windows.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 = windows_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
104 data->ime_candlist = SDL_FALSE;
105 SDL_memset(data->ime_candidates, 0, sizeof(data->ime_candidates));
106 data->ime_candcount = 0;
107 data->ime_candref = 0;
108 data->ime_candsel = 0;
109 data->ime_candpgsize = 0;
110 data->ime_candlistindexbase = 0;
111 data->ime_candvertical = SDL_TRUE;
112
113 data->ime_candtex = NULL;
114 data->ime_dirty = SDL_FALSE;
115 SDL_memset(&data->ime_rect, 0, sizeof(data->ime_rect));
116 SDL_memset(&data->ime_candlistrect, 0, sizeof(data->ime_candlistrect));
117 data->ime_winwidth = 0;
118 data->ime_winheight = 0;
119
120 data->ime_hkl = 0;
121 data->ime_himm32 = 0;
122 data->GetReadingString = 0;
123 data->ShowReadingWindow = 0;
124 data->ImmLockIMC = 0;
125 data->ImmUnlockIMC = 0;
126 data->ImmLockIMCC = 0;
127 data->ImmUnlockIMCC = 0;
128 data->ime_uiless = SDL_FALSE;
129 data->ime_threadmgrex = 0;
130 data->ime_uielemsinkcookie = TF_INVALID_COOKIE;
131 data->ime_alpnsinkcookie = TF_INVALID_COOKIE;
132 data->ime_openmodesinkcookie = TF_INVALID_COOKIE;
133 data->ime_convmodesinkcookie = TF_INVALID_COOKIE;
134 data->ime_uielemsink = 0;
135 data->ime_ippasink = 0;
136
137 WIN_UpdateKeymap();
138
139 SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
140 SDL_SetScancodeName(SDL_SCANCODE_LGUI, "Left Windows");
141 SDL_SetScancodeName(SDL_SCANCODE_RGUI, "Right Windows");
142 }
143
144 void
145 WIN_UpdateKeymap()
146 {
147 int i;
148 SDL_scancode scancode;
149 SDLKey keymap[SDL_NUM_SCANCODES];
150
151 SDL_GetDefaultKeymap(keymap);
152
153 for (i = 0; i < SDL_arraysize(windows_scancode_table); i++) {
154
155 /* Make sure this scancode is a valid character scancode */
156 scancode = windows_scancode_table[i];
157 if (scancode == SDL_SCANCODE_UNKNOWN || keymap[scancode] >= 127) {
158 continue;
159 }
160
161 /* Alphabetic keys are handled specially, since Windows remaps them */
162 if (i >= 'A' && i <= 'Z') {
163 BYTE vsc = alpha_scancodes[i - 'A'];
164 keymap[scancode] = MapVirtualKey(vsc, MAPVK_VSC_TO_VK) + 0x20;
165 } else {
166 keymap[scancode] = (MapVirtualKey(i, MAPVK_VK_TO_CHAR) & 0x7FFF);
167 }
168 }
169 SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
170 }
171
172 void
173 WIN_QuitKeyboard(_THIS)
174 {
175 IME_Quit((SDL_VideoData *)_this->driverdata);
176 }
177
178 void
179 WIN_StartTextInput(_THIS)
180 {
181 SDL_Window *window = SDL_GetKeyboardFocus();
182 if (window) {
183 HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
184 SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
185 SDL_GetWindowSize(window, &videodata->ime_winwidth, &videodata->ime_winheight);
186 IME_Init(videodata, hwnd);
187 IME_Enable(videodata, hwnd);
188 }
189 }
190
191 void
192 WIN_StopTextInput(_THIS)
193 {
194 SDL_Window *window = SDL_GetKeyboardFocus();
195 if (window) {
196 HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
197 SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
198 IME_Init(videodata, hwnd);
199 IME_Disable(videodata, hwnd);
200 }
201 }
202
203 void
204 WIN_SetTextInputRect(_THIS, SDL_Rect *rect)
205 {
206 SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
207 videodata->ime_rect = *rect;
208 }
209
210 #ifdef __GNUC__
211 #undef DEFINE_GUID
212 #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}}
213 DEFINE_GUID(IID_ITfInputProcessorProfileActivationSink, 0x71C6E74E,0x0F28,0x11D8,0xA8,0x2A,0x00,0x06,0x5B,0x84,0x43,0x5C);
214 DEFINE_GUID(IID_ITfUIElementSink, 0xEA1EA136,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
215 DEFINE_GUID(GUID_TFCAT_TIP_KEYBOARD, 0x34745C63,0xB2F0,0x4784,0x8B,0x67,0x5E,0x12,0xC8,0x70,0x1A,0x31);
216 DEFINE_GUID(IID_ITfSource, 0x4EA48A35,0x60AE,0x446F,0x8F,0xD6,0xE6,0xA8,0xD8,0x24,0x59,0xF7);
217 DEFINE_GUID(IID_ITfUIElementMgr, 0xEA1EA135,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
218 DEFINE_GUID(IID_ITfCandidateListUIElement, 0xEA1EA138,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
219 DEFINE_GUID(IID_ITfReadingInformationUIElement, 0xEA1EA139,0x19DF,0x11D7,0xA6,0xD2,0x00,0x06,0x5B,0x84,0x43,0x5C);
220 DEFINE_GUID(IID_ITfThreadMgr, 0xAA80E801,0x2021,0x11D2,0x93,0xE0,0x00,0x60,0xB0,0x67,0xB8,0x6E);
221 DEFINE_GUID(CLSID_TF_ThreadMgr, 0x529A9E6B,0x6587,0x4F23,0xAB,0x9E,0x9C,0x7D,0x68,0x3E,0x3C,0x50);
222 DEFINE_GUID(IID_ITfThreadMgrEx, 0x3E90ADE3,0x7594,0x4CB0,0xBB,0x58,0x69,0x62,0x8F,0x5F,0x45,0x8C);
223 #endif
224
225 #define LANG_CHT MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL)
226 #define LANG_CHS MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED)
227
228 #define MAKEIMEVERSION(major,minor) ((DWORD) (((BYTE)(major) << 24) | ((BYTE)(minor) << 16) ))
229 #define IMEID_VER(id) ((id) & 0xffff0000)
230 #define IMEID_LANG(id) ((id) & 0x0000ffff)
231
232 #define CHT_HKL_DAYI ((HKL)0xE0060404)
233 #define CHT_HKL_NEW_PHONETIC ((HKL)0xE0080404)
234 #define CHT_HKL_NEW_CHANG_JIE ((HKL)0xE0090404)
235 #define CHT_HKL_NEW_QUICK ((HKL)0xE00A0404)
236 #define CHT_HKL_HK_CANTONESE ((HKL)0xE00B0404)
237 #define CHT_IMEFILENAME1 "TINTLGNT.IME"
238 #define CHT_IMEFILENAME2 "CINTLGNT.IME"
239 #define CHT_IMEFILENAME3 "MSTCIPHA.IME"
240 #define IMEID_CHT_VER42 (LANG_CHT | MAKEIMEVERSION(4, 2))
241 #define IMEID_CHT_VER43 (LANG_CHT | MAKEIMEVERSION(4, 3))
242 #define IMEID_CHT_VER44 (LANG_CHT | MAKEIMEVERSION(4, 4))
243 #define IMEID_CHT_VER50 (LANG_CHT | MAKEIMEVERSION(5, 0))
244 #define IMEID_CHT_VER51 (LANG_CHT | MAKEIMEVERSION(5, 1))
245 #define IMEID_CHT_VER52 (LANG_CHT | MAKEIMEVERSION(5, 2))
246 #define IMEID_CHT_VER60 (LANG_CHT | MAKEIMEVERSION(6, 0))
247 #define IMEID_CHT_VER_VISTA (LANG_CHT | MAKEIMEVERSION(7, 0))
248
249 #define CHS_HKL ((HKL)0xE00E0804)
250 #define CHS_IMEFILENAME1 "PINTLGNT.IME"
251 #define CHS_IMEFILENAME2 "MSSCIPYA.IME"
252 #define IMEID_CHS_VER41 (LANG_CHS | MAKEIMEVERSION(4, 1))
253 #define IMEID_CHS_VER42 (LANG_CHS | MAKEIMEVERSION(4, 2))
254 #define IMEID_CHS_VER53 (LANG_CHS | MAKEIMEVERSION(5, 3))
255
256 #define LANG() LOWORD((videodata->ime_hkl))
257 #define PRIMLANG() ((WORD)PRIMARYLANGID(LANG()))
258 #define SUBLANG() SUBLANGID(LANG())
259
260 static void IME_UpdateInputLocale(SDL_VideoData *videodata);
261 static void IME_ClearComposition(SDL_VideoData *videodata);
262 static void IME_SetWindow(SDL_VideoData* videodata, HWND hwnd);
263 static void IME_SetupAPI(SDL_VideoData *videodata);
264 static DWORD IME_GetId(SDL_VideoData *videodata, UINT uIndex);
265 static void IME_SendEditingEvent(SDL_VideoData *videodata);
266 static void IME_DestroyTextures(SDL_VideoData *videodata);
267
268 #define SDL_IsEqualIID(riid1, riid2) SDL_IsEqualGUID(riid1, riid2)
269 #define SDL_IsEqualGUID(rguid1, rguid2) (!SDL_memcmp(rguid1, rguid2, sizeof(GUID)))
270
271 static SDL_bool UILess_SetupSinks(SDL_VideoData *videodata);
272 static void UILess_ReleaseSinks(SDL_VideoData *videodata);
273 static void UILess_EnableUIUpdates(SDL_VideoData *videodata);
274 static void UILess_DisableUIUpdates(SDL_VideoData *videodata);
275
276 static void
277 IME_Init(SDL_VideoData *videodata, HWND hwnd)
278 {
279 if (videodata->ime_initialized)
280 return;
281
282 videodata->ime_hwnd_main = hwnd;
283 if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
284 videodata->ime_com_initialized = SDL_TRUE;
285 CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, (LPVOID *)&videodata->ime_threadmgr);
286 }
287 videodata->ime_initialized = SDL_TRUE;
288 videodata->ime_himm32 = LoadLibraryA("imm32.dll");
289 if (!videodata->ime_himm32) {
290 videodata->ime_available = SDL_FALSE;
291 return;
292 }
293 videodata->ImmLockIMC = (LPINPUTCONTEXT2 (WINAPI *)(HIMC))GetProcAddress(videodata->ime_himm32, "ImmLockIMC");
294 videodata->ImmUnlockIMC = (BOOL (WINAPI *)(HIMC))GetProcAddress(videodata->ime_himm32, "ImmUnlockIMC");
295 videodata->ImmLockIMCC = (LPVOID (WINAPI *)(HIMCC))GetProcAddress(videodata->ime_himm32, "ImmLockIMCC");
296 videodata->ImmUnlockIMCC = (BOOL (WINAPI *)(HIMCC))GetProcAddress(videodata->ime_himm32, "ImmUnlockIMCC");
297
298 IME_SetWindow(videodata, hwnd);
299 videodata->ime_himc = ImmGetContext(hwnd);
300 ImmReleaseContext(hwnd, videodata->ime_himc);
301 if (!videodata->ime_himc) {
302 videodata->ime_available = SDL_FALSE;
303 IME_Disable(videodata, hwnd);
304 return;
305 }
306 videodata->ime_available = SDL_TRUE;
307 IME_UpdateInputLocale(videodata);
308 IME_SetupAPI(videodata);
309 videodata->ime_uiless = UILess_SetupSinks(videodata);
310 IME_UpdateInputLocale(videodata);
311 IME_Disable(videodata, hwnd);
312 }
313
314 static void
315 IME_Enable(SDL_VideoData *videodata, HWND hwnd)
316 {
317 if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
318 return;
319
320 if (!videodata->ime_available) {
321 IME_Disable(videodata, hwnd);
322 return;
323 }
324 if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
325 ImmAssociateContext(videodata->ime_hwnd_current, videodata->ime_himc);
326
327 videodata->ime_enabled = SDL_TRUE;
328 IME_UpdateInputLocale(videodata);
329 UILess_EnableUIUpdates(videodata);
330 }
331
332 static void
333 IME_Disable(SDL_VideoData *videodata, HWND hwnd)
334 {
335 if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
336 return;
337
338 IME_ClearComposition(videodata);
339 if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
340 ImmAssociateContext(videodata->ime_hwnd_current, (HIMC)0);
341
342 videodata->ime_enabled = SDL_FALSE;
343 UILess_DisableUIUpdates(videodata);
344 }
345
346 static void
347 IME_Quit(SDL_VideoData *videodata)
348 {
349 if (!videodata->ime_initialized)
350 return;
351
352 UILess_ReleaseSinks(videodata);
353 if (videodata->ime_hwnd_main)
354 ImmAssociateContext(videodata->ime_hwnd_main, videodata->ime_himc);
355
356 videodata->ime_hwnd_main = 0;
357 videodata->ime_himc = 0;
358 if (videodata->ime_himm32) {
359 FreeLibrary(videodata->ime_himm32);
360 videodata->ime_himm32 = 0;
361 }
362 if (videodata->ime_threadmgr) {
363 videodata->ime_threadmgr->lpVtbl->Release(videodata->ime_threadmgr);
364 videodata->ime_threadmgr = 0;
365 }
366 if (videodata->ime_com_initialized) {
367 CoUninitialize();
368 videodata->ime_com_initialized = SDL_FALSE;
369 }
370 IME_DestroyTextures(videodata);
371 videodata->ime_initialized = SDL_FALSE;
372 }
373
374 static void
375 IME_GetReadingString(SDL_VideoData *videodata, HWND hwnd)
376 {
377 DWORD id = 0;
378 HIMC himc = 0;
379 WCHAR buffer[16];
380 WCHAR *s = buffer;
381 DWORD len = 0;
382 INT err = 0;
383 BOOL vertical = FALSE;
384 UINT maxuilen = 0;
385 static OSVERSIONINFOA osversion = {0};
386 if (videodata->ime_uiless)
387 return;
388
389 videodata->ime_readingstring[0] = 0;
390 if (!osversion.dwOSVersionInfoSize) {
391 osversion.dwOSVersionInfoSize = sizeof(osversion);
392 GetVersionExA(&osversion);
393 }
394 id = IME_GetId(videodata, 0);
395 if (!id)
396 return;
397
398 himc = ImmGetContext(hwnd);
399 if (!himc)
400 return;
401
402 if (videodata->GetReadingString) {
403 len = videodata->GetReadingString(himc, 0, 0, &err, &vertical, &maxuilen);
404 if (len) {
405 if (len > SDL_arraysize(buffer))
406 len = SDL_arraysize(buffer);
407
408 len = videodata->GetReadingString(himc, len, s, &err, &vertical, &maxuilen);
409 }
410 SDL_wcslcpy(videodata->ime_readingstring, s, len);
411 }
412 else {
413 LPINPUTCONTEXT2 lpimc = videodata->ImmLockIMC(himc);
414 LPBYTE p = 0;
415 s = 0;
416 switch (id)
417 {
418 case IMEID_CHT_VER42:
419 case IMEID_CHT_VER43:
420 case IMEID_CHT_VER44:
421 p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 24);
422 if (!p)
423 break;
424
425 len = *(DWORD *)(p + 7*4 + 32*4);
426 s = (WCHAR *)(p + 56);
427 break;
428 case IMEID_CHT_VER51:
429 case IMEID_CHT_VER52:
430 case IMEID_CHS_VER53:
431 p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 4);
432 if (!p)
433 break;
434
435 p = *(LPBYTE *)((LPBYTE)p + 1*4 + 5*4);
436 if (!p)
437 break;
438
439 len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2);
440 s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4);
441 break;
442 case IMEID_CHS_VER41:
443 {
444 int offset = (IME_GetId(videodata, 1) >= 0x00000002) ? 8 : 7;
445 p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + offset * 4);
446 if (!p)
447 break;
448
449 len = *(DWORD *)(p + 7*4 + 16*2*4);
450 s = (WCHAR *)(p + 6*4 + 16*2*1);
451 }
452 break;
453 case IMEID_CHS_VER42:
454 if (osversion.dwPlatformId != VER_PLATFORM_WIN32_NT)
455 break;
456
457 p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 1*4 + 1*4 + 6*4);
458 if (!p)
459 break;
460
461 len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2);
462 s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4);
463 break;
464 }
465 if (s)
466 SDL_wcslcpy(videodata->ime_readingstring, s, len + 1);
467
468 videodata->ImmUnlockIMCC(lpimc->hPrivate);
469 videodata->ImmUnlockIMC(himc);
470 }
471 ImmReleaseContext(hwnd, himc);
472 IME_SendEditingEvent(videodata);
473 }
474
475 static void
476 IME_InputLangChanged(SDL_VideoData *videodata)
477 {
478 UINT lang = PRIMLANG();
479 IME_UpdateInputLocale(videodata);
480 if (!videodata->ime_uiless)
481 videodata->ime_candlistindexbase = (videodata->ime_hkl == CHT_HKL_DAYI) ? 0 : 1;
482
483 IME_SetupAPI(videodata);
484 if (lang != PRIMLANG()) {
485 IME_ClearComposition(videodata);
486 }
487 }
488
489 static DWORD
490 IME_GetId(SDL_VideoData *videodata, UINT uIndex)
491 {
492 static HKL hklprev = 0;
493 static DWORD dwRet[2] = {0};
494 DWORD dwVerSize = 0;
495 DWORD dwVerHandle = 0;
496 LPVOID lpVerBuffer = 0;
497 LPVOID lpVerData = 0;
498 UINT cbVerData = 0;
499 char szTemp[256];
500 HKL hkl = 0;
501 DWORD dwLang = 0;
502 if (uIndex >= sizeof(dwRet) / sizeof(dwRet[0]))
503 return 0;
504
505 hkl = videodata->ime_hkl;
506 if (hklprev == hkl)
507 return dwRet[uIndex];
508
509 hklprev = hkl;
510 dwLang = ((DWORD)hkl & 0xffff);
511 if (videodata->ime_uiless && LANG() == LANG_CHT) {
512 dwRet[0] = IMEID_CHT_VER_VISTA;
513 dwRet[1] = 0;
514 return dwRet[0];
515 }
516 if (hkl != CHT_HKL_NEW_PHONETIC
517 && hkl != CHT_HKL_NEW_CHANG_JIE
518 && hkl != CHT_HKL_NEW_QUICK
519 && hkl != CHT_HKL_HK_CANTONESE
520 && hkl != CHS_HKL) {
521 dwRet[0] = dwRet[1] = 0;
522 return dwRet[uIndex];
523 }
524 if (ImmGetIMEFileNameA(hkl, szTemp, sizeof(szTemp) - 1) <= 0) {
525 dwRet[0] = dwRet[1] = 0;
526 return dwRet[uIndex];
527 }
528 if (!videodata->GetReadingString) {
529 #define LCID_INVARIANT MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT)
530 if (CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME1, -1) != 2
531 && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME2, -1) != 2
532 && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME3, -1) != 2
533 && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME1, -1) != 2
534 && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME2, -1) != 2) {
535 dwRet[0] = dwRet[1] = 0;
536 return dwRet[uIndex];
537 }
538 #undef LCID_INVARIANT
539 dwVerSize = GetFileVersionInfoSizeA(szTemp, &dwVerHandle);
540 if (dwVerSize) {
541 lpVerBuffer = SDL_malloc(dwVerSize);
542 if (lpVerBuffer) {
543 if (GetFileVersionInfoA(szTemp, dwVerHandle, dwVerSize, lpVerBuffer)) {
544 if (VerQueryValueA(lpVerBuffer, "\\", &lpVerData, &cbVerData)) {
545 #define pVerFixedInfo ((VS_FIXEDFILEINFO FAR*)lpVerData)
546 DWORD dwVer = pVerFixedInfo->dwFileVersionMS;
547 dwVer = (dwVer & 0x00ff0000) << 8 | (dwVer & 0x000000ff) << 16;
548 if (videodata->GetReadingString ||
549 dwLang == LANG_CHT && (
550 dwVer == MAKEIMEVERSION(4, 2) ||
551 dwVer == MAKEIMEVERSION(4, 3) ||
552 dwVer == MAKEIMEVERSION(4, 4) ||
553 dwVer == MAKEIMEVERSION(5, 0) ||
554 dwVer == MAKEIMEVERSION(5, 1) ||
555 dwVer == MAKEIMEVERSION(5, 2) ||
556 dwVer == MAKEIMEVERSION(6, 0))
557 ||
558 dwLang == LANG_CHS && (
559 dwVer == MAKEIMEVERSION(4, 1) ||
560 dwVer == MAKEIMEVERSION(4, 2) ||
561 dwVer == MAKEIMEVERSION(5, 3))) {
562 dwRet[0] = dwVer | dwLang;
563 dwRet[1] = pVerFixedInfo->dwFileVersionLS;
564 SDL_free(lpVerBuffer);
565 return dwRet[0];
566 }
567 #undef pVerFixedInfo
568 }
569 }
570 }
571 SDL_free(lpVerBuffer);
572 }
573 }
574 dwRet[0] = dwRet[1] = 0;
575 return dwRet[uIndex];
576 }
577
578 static void
579 IME_SetupAPI(SDL_VideoData *videodata)
580 {
581 char ime_file[MAX_PATH + 1];
582 HMODULE hime = 0;
583 HKL hkl = 0;
584 videodata->GetReadingString = 0;
585 videodata->ShowReadingWindow = 0;
586 if (videodata->ime_uiless)
587 return;
588
589 hkl = videodata->ime_hkl;
590 if (ImmGetIMEFileNameA(hkl, ime_file, sizeof(ime_file) - 1) <= 0)
591 return;
592
593 hime = LoadLibraryA(ime_file);
594 if (!hime)
595 return;
596
597 videodata->GetReadingString = (UINT (WINAPI *)(HIMC, UINT, LPWSTR, PINT, BOOL*, PUINT))
598 GetProcAddress(hime, "GetReadingString");
599 videodata->ShowReadingWindow = (BOOL (WINAPI *)(HIMC, BOOL))
600 GetProcAddress(hime, "ShowReadingWindow");
601
602 if (videodata->ShowReadingWindow) {
603 HIMC himc = ImmGetContext(videodata->ime_hwnd_current);
604 if (himc) {
605 videodata->ShowReadingWindow(himc, FALSE);
606 ImmReleaseContext(videodata->ime_hwnd_current, himc);
607 }
608 }
609 }
610
611 static void
612 IME_SetWindow(SDL_VideoData* videodata, HWND hwnd)
613 {
614 videodata->ime_hwnd_current = hwnd;
615 if (videodata->ime_threadmgr) {
616 struct ITfDocumentMgr *document_mgr = 0;
617 if (SUCCEEDED(videodata->ime_threadmgr->lpVtbl->AssociateFocus(videodata->ime_threadmgr, hwnd, NULL, &document_mgr))) {
618 if (document_mgr)
619 document_mgr->lpVtbl->Release(document_mgr);
620 }
621 }
622 }
623
624 static void
625 IME_UpdateInputLocale(SDL_VideoData *videodata)
626 {
627 static HKL hklprev = 0;
628 videodata->ime_hkl = GetKeyboardLayout(0);
629 if (hklprev == videodata->ime_hkl)
630 return;
631
632 hklprev = videodata->ime_hkl;
633 switch (PRIMLANG()) {
634 case LANG_CHINESE:
635 videodata->ime_candvertical = SDL_TRUE;
636 if (SUBLANG() == SUBLANG_CHINESE_SIMPLIFIED)
637 videodata->ime_candvertical = SDL_FALSE;
638
639 break;
640 case LANG_JAPANESE:
641 videodata->ime_candvertical = SDL_TRUE;
642 break;
643 case LANG_KOREAN:
644 videodata->ime_candvertical = SDL_FALSE;
645 break;
646 }
647 }
648
649 static void
650 IME_ClearComposition(SDL_VideoData *videodata)
651 {
652 HIMC himc = 0;
653 if (!videodata->ime_initialized)
654 return;
655
656 himc = ImmGetContext(videodata->ime_hwnd_current);
657 if (!himc)
658 return;
659
660 ImmNotifyIME(himc, NI_COMPOSITIONSTR, CPS_CANCEL, 0);
661 if (videodata->ime_uiless)
662 ImmSetCompositionString(himc, SCS_SETSTR, TEXT(""), sizeof(TCHAR), TEXT(""), sizeof(TCHAR));
663
664 ImmNotifyIME(himc, NI_CLOSECANDIDATE, 0, 0);
665 ImmReleaseContext(videodata->ime_hwnd_current, himc);
666 SDL_SendEditingText("", 0, 0);
667 }
668
669 static void
670 IME_GetCompositionString(SDL_VideoData *videodata, HIMC himc, DWORD string)
671 {
672 LONG length = ImmGetCompositionStringW(himc, string, videodata->ime_composition, sizeof(videodata->ime_composition));
673 if (length < 0)
674 length = 0;
675
676 length /= sizeof(videodata->ime_composition[0]);
677 videodata->ime_cursor = LOWORD(ImmGetCompositionStringW(himc, GCS_CURSORPOS, 0, 0));
678 if (videodata->ime_composition[videodata->ime_cursor] == 0x3000) {
679 int i;
680 for (i = videodata->ime_cursor + 1; i < length; ++i)
681 videodata->ime_composition[i - 1] = videodata->ime_composition[i];
682
683 --length;
684 }
685 videodata->ime_composition[length] = 0;
686 }
687
688 static void
689 IME_SendInputEvent(SDL_VideoData *videodata)
690 {
691 char *s = 0;
692 s = WIN_StringToUTF8(videodata->ime_composition);
693 SDL_SendKeyboardText(s);
694 SDL_free(s);
695
696 videodata->ime_composition[0] = 0;
697 videodata->ime_readingstring[0] = 0;
698 videodata->ime_cursor = 0;
699 }
700
701 static void
702 IME_SendEditingEvent(SDL_VideoData *videodata)
703 {
704 char *s = 0;
705 WCHAR buffer[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
706 buffer[0] = 0;
707 if (videodata->ime_readingstring[0]) {
708 size_t len = SDL_min(SDL_wcslen(videodata->ime_composition), (size_t)videodata->ime_cursor);
709 SDL_wcslcpy(buffer, videodata->ime_composition, len + 1);
710 SDL_wcslcat(buffer, videodata->ime_readingstring, sizeof(buffer));
711 SDL_wcslcat(buffer, &videodata->ime_composition[len], sizeof(buffer) - len);
712 }
713 else {
714 SDL_wcslcpy(buffer, videodata->ime_composition, sizeof(videodata->ime_composition));
715 }
716 s = WIN_StringToUTF8(buffer);
717 SDL_SendEditingText(s, videodata->ime_cursor + SDL_wcslen(videodata->ime_readingstring), 0);
718 SDL_free(s);
719 }
720
721 static void
722 IME_AddCandidate(SDL_VideoData *videodata, UINT i, LPCWSTR candidate)
723 {
724 LPWSTR dst = videodata->ime_candidates[i];
725 *dst++ = (WCHAR)(TEXT('0') + ((i + videodata->ime_candlistindexbase) % 10));
726 if (videodata->ime_candvertical)
727 *dst++ = TEXT(' ');
728
729 while (*candidate && (SDL_arraysize(videodata->ime_candidates[i]) > (dst - videodata->ime_candidates[i])))
730 *dst++ = *candidate++;
731
732 *dst = (WCHAR)'\0';
733 }
734
735 static void
736 IME_GetCandidateList(HIMC himc, SDL_VideoData *videodata)
737 {
738 LPCANDIDATELIST cand_list = 0;
739 DWORD size = ImmGetCandidateListW(himc, 0, 0, 0);
740 if (size) {
741 cand_list = (LPCANDIDATELIST)SDL_malloc(size);
742 if (cand_list) {
743 size = ImmGetCandidateListW(himc, 0, cand_list, size);
744 if (size) {
745 int i = 0;
746 int j = 0;
747 int page_start = 0;
748 videodata->ime_candsel = cand_list->dwSelection;
749 videodata->ime_candcount = cand_list->dwCount;
750
751 if (LANG() == LANG_CHS && IME_GetId(videodata, 0)) {
752 const UINT maxcandchar = 18;
753 UINT i = 0;
754 UINT cchars = 0;
755
756 for (; i < videodata->ime_candcount; ++i) {
757 UINT len = SDL_wcslen((LPWSTR)((DWORD)cand_list + cand_list->dwOffset[i])) + 1;
758 if (len + cchars > maxcandchar) {
759 if (i > cand_list->dwSelection)
760 break;
761
762 page_start = i;
763 cchars = len;
764 }
765 else {
766 cchars += len;
767 }
768 }
769 videodata->ime_candpgsize = i - page_start;
770 }
771 else {
772 videodata->ime_candpgsize = SDL_min(cand_list->dwPageSize, MAX_CANDLIST);
773 page_start = (cand_list->dwSelection / videodata->ime_candpgsize) * videodata->ime_candpgsize;
774 }
775 SDL_memset(&videodata->ime_candidates, 0, sizeof(videodata->ime_candidates));
776 for (i = page_start, j = 0; (DWORD)i < cand_list->dwCount && j < (int)videodata->ime_candpgsize; i++, j++) {
777 LPCWSTR candidate = (LPCWSTR)((DWORD)cand_list + cand_list->dwOffset[i]);
778 IME_AddCandidate(videodata, j, candidate);
779 }
780 if (PRIMLANG() == LANG_KOREAN || (PRIMLANG() == LANG_CHT && !IME_GetId(videodata, 0)))
781 videodata->ime_candsel = -1;
782
783 }
784 SDL_free(cand_list);
785 }
786 }
787 }
788
789 static void
790 IME_ShowCandidateList(SDL_VideoData *videodata)
791 {
792 videodata->ime_dirty = SDL_TRUE;
793 videodata->ime_candlist = SDL_TRUE;
794 IME_DestroyTextures(videodata);
795 IME_SendEditingEvent(videodata);
796 }
797
798 static void
799 IME_HideCandidateList(SDL_VideoData *videodata)
800 {
801 videodata->ime_dirty = SDL_FALSE;
802 videodata->ime_candlist = SDL_FALSE;
803 IME_DestroyTextures(videodata);
804 IME_SendEditingEvent(videodata);
805 }
806
807 SDL_bool
808 IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
809 {
810 SDL_bool trap = SDL_FALSE;
811 HIMC himc = 0;
812 if (!videodata->ime_initialized || !videodata->ime_available || !videodata->ime_enabled)
813 return SDL_FALSE;
814
815 switch (msg) {
816 case WM_INPUTLANGCHANGE:
817 IME_InputLangChanged(videodata);
818 break;
819 case WM_IME_SETCONTEXT:
820 *lParam = 0;
821 break;
822 case WM_IME_STARTCOMPOSITION:
823 trap = SDL_TRUE;
824 break;
825 case WM_IME_COMPOSITION:
826 trap = SDL_TRUE;
827 himc = ImmGetContext(hwnd);
828 if (*lParam & GCS_RESULTSTR) {
829 IME_GetCompositionString(videodata, himc, GCS_RESULTSTR);
830 IME_SendInputEvent(videodata);
831 }
832 if (*lParam & GCS_COMPSTR) {
833 if (!videodata->ime_uiless)
834 videodata->ime_readingstring[0] = 0;
835
836 IME_GetCompositionString(videodata, himc, GCS_COMPSTR);
837 IME_SendEditingEvent(videodata);
838 }
839 ImmReleaseContext(hwnd, himc);
840 break;
841 case WM_IME_ENDCOMPOSITION:
842 videodata->ime_composition[0] = 0;
843 videodata->ime_readingstring[0] = 0;
844 videodata->ime_cursor = 0;
845 SDL_SendEditingText("", 0, 0);
846 break;
847 case WM_IME_NOTIFY:
848 switch (wParam) {
849 case IMN_SETCONVERSIONMODE:
850 case IMN_SETOPENSTATUS:
851 IME_UpdateInputLocale(videodata);
852 break;
853 case IMN_OPENCANDIDATE:
854 case IMN_CHANGECANDIDATE:
855 if (videodata->ime_uiless)
856 break;
857
858 trap = SDL_TRUE;
859 IME_ShowCandidateList(videodata);
860 himc = ImmGetContext(hwnd);
861 if (!himc)
862 break;
863
864 IME_GetCandidateList(himc, videodata);
865 ImmReleaseContext(hwnd, himc);
866 break;
867 case IMN_CLOSECANDIDATE:
868 trap = SDL_TRUE;
869 IME_HideCandidateList(videodata);
870 break;
871 case IMN_PRIVATE:
872 {
873 DWORD dwId = IME_GetId(videodata, 0);
874 IME_GetReadingString(videodata, hwnd);
875 switch (dwId)
876 {
877 case IMEID_CHT_VER42:
878 case IMEID_CHT_VER43:
879 case IMEID_CHT_VER44:
880 case IMEID_CHS_VER41:
881 case IMEID_CHS_VER42:
882 if (*lParam == 1 || *lParam == 2)
883 trap = SDL_TRUE;
884
885 break;
886 case IMEID_CHT_VER50:
887 case IMEID_CHT_VER51:
888 case IMEID_CHT_VER52:
889 case IMEID_CHT_VER60:
890 case IMEID_CHS_VER53:
891 if (*lParam == 16
892 || *lParam == 17
893 || *lParam == 26
894 || *lParam == 27
895 || *lParam == 28)
896 trap = SDL_TRUE;
897 break;
898 }
899 }
900 break;
901 default:
902 trap = SDL_TRUE;
903 break;
904 }
905 break;
906 }
907 return trap;
908 }
909
910 static void
911 IME_CloseCandidateList(SDL_VideoData *videodata)
912 {
913 IME_HideCandidateList(videodata);
914 videodata->ime_candcount = 0;
915 SDL_memset(videodata->ime_candidates, 0, sizeof(videodata->ime_candidates));
916 }
917
918 static void
919 UILess_GetCandidateList(SDL_VideoData *videodata, ITfCandidateListUIElement *pcandlist)
920 {
921 UINT selection = 0;
922 UINT count = 0;
923 UINT page = 0;
924 UINT pgcount = 0;
925 DWORD pgstart = 0;
926 DWORD pgsize = 0;
927 UINT i, j;
928 pcandlist->lpVtbl->GetSelection(pcandlist, &selection);
929 pcandlist->lpVtbl->GetCount(pcandlist, &count);
930 pcandlist->lpVtbl->GetCurrentPage(pcandlist, &page);
931
932 videodata->ime_candsel = selection;
933 videodata->ime_candcount = count;
934 IME_ShowCandidateList(videodata);
935
936 pcandlist->lpVtbl->GetPageIndex(pcandlist, 0, 0, &pgcount);
937 if (pgcount > 0) {
938 UINT *idxlist = SDL_malloc(sizeof(UINT) * pgcount);
939 if (idxlist) {
940 pcandlist->lpVtbl->GetPageIndex(pcandlist, idxlist, pgcount, &pgcount);
941 pgstart = idxlist[page];
942 if (page < pgcount - 1)
943 pgsize = SDL_min(count, idxlist[page + 1]) - pgstart;
944 else
945 pgsize = count - pgstart;
946
947 SDL_free(idxlist);
948 }
949 }
950 videodata->ime_candpgsize = SDL_min(pgsize, MAX_CANDLIST);
951 videodata->ime_candsel = videodata->ime_candsel - pgstart;
952
953 SDL_memset(videodata->ime_candidates, 0, sizeof(videodata->ime_candidates));
954 for (i = pgstart, j = 0; (DWORD)i < count && j < videodata->ime_candpgsize; i++, j++) {
955 BSTR bstr;
956 if (SUCCEEDED(pcandlist->lpVtbl->GetString(pcandlist, i, &bstr))) {
957 if (bstr) {
958 IME_AddCandidate(videodata, j, bstr);
959 SysFreeString(bstr);
960 }
961 }
962 }
963 if (PRIMLANG() == LANG_KOREAN)
964 videodata->ime_candsel = -1;
965 }
966
967 STDMETHODIMP_(ULONG) TSFSink_AddRef(TSFSink *sink)
968 {
969 return ++sink->refcount;
970 }
971
972 STDMETHODIMP_(ULONG)TSFSink_Release(TSFSink *sink)
973 {
974 --sink->refcount;
975 if (sink->refcount == 0) {
976 SDL_free(sink);
977 return 0;
978 }
979 return sink->refcount;
980 }
981
982 STDMETHODIMP UIElementSink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv)
983 {
984 if (!ppv)
985 return E_INVALIDARG;
986
987 *ppv = 0;
988 if (SDL_IsEqualIID(riid, &IID_IUnknown))
989 *ppv = (IUnknown *)sink;
990 else if (SDL_IsEqualIID(riid, &IID_ITfUIElementSink))
991 *ppv = (ITfUIElementSink *)sink;
992
993 if (*ppv) {
994 TSFSink_AddRef(sink);
995 return S_OK;
996 }
997 return E_NOINTERFACE;
998 }
999
1000 ITfUIElement *UILess_GetUIElement(SDL_VideoData *videodata, DWORD dwUIElementId)
1001 {
1002 ITfUIElementMgr *puiem = 0;
1003 ITfUIElement *pelem = 0;
1004 ITfThreadMgrEx *threadmgrex = videodata->ime_threadmgrex;
1005
1006 if (SUCCEEDED(threadmgrex->lpVtbl->QueryInterface(threadmgrex, &IID_ITfUIElementMgr, (LPVOID *)&puiem))) {
1007 puiem->lpVtbl->GetUIElement(puiem, dwUIElementId, &pelem);
1008 puiem->lpVtbl->Release(puiem);
1009 }
1010 return pelem;
1011 }
1012
1013 STDMETHODIMP UIElementSink_BeginUIElement(TSFSink *sink, DWORD dwUIElementId, BOOL *pbShow)
1014 {
1015 ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
1016 ITfReadingInformationUIElement *preading = 0;
1017 ITfCandidateListUIElement *pcandlist = 0;
1018 SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
1019 if (!element)
1020 return E_INVALIDARG;
1021
1022 *pbShow = FALSE;
1023 if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
1024 BSTR bstr;
1025 if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr) {
1026 WCHAR *s = (WCHAR *)bstr;
1027 SysFreeString(bstr);
1028 }
1029 preading->lpVtbl->Release(preading);
1030 }
1031 else if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfCandidateListUIElement, (LPVOID *)&pcandlist))) {
1032 videodata->ime_candref++;
1033 UILess_GetCandidateList(videodata, pcandlist);
1034 pcandlist->lpVtbl->Release(pcandlist);
1035 }
1036 return S_OK;
1037 }
1038
1039 STDMETHODIMP UIElementSink_UpdateUIElement(TSFSink *sink, DWORD dwUIElementId)
1040 {
1041 ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
1042 ITfReadingInformationUIElement *preading = 0;
1043 ITfCandidateListUIElement *pcandlist = 0;
1044 SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
1045 if (!element)
1046 return E_INVALIDARG;
1047
1048 if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
1049 BSTR bstr;
1050 if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr) {
1051 WCHAR *s = (WCHAR *)bstr;
1052 SDL_wcslcpy(videodata->ime_readingstring, s, sizeof(videodata->ime_readingstring));
1053 IME_SendEditingEvent(videodata);
1054 SysFreeString(bstr);
1055 }
1056 preading->lpVtbl->Release(preading);
1057 }
1058 else if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfCandidateListUIElement, (LPVOID *)&pcandlist))) {
1059 UILess_GetCandidateList(videodata, pcandlist);
1060 pcandlist->lpVtbl->Release(pcandlist);
1061 }
1062 return S_OK;
1063 }
1064
1065 STDMETHODIMP UIElementSink_EndUIElement(TSFSink *sink, DWORD dwUIElementId)
1066 {
1067 ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId);
1068 ITfReadingInformationUIElement *preading = 0;
1069 ITfCandidateListUIElement *pcandlist = 0;
1070 SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
1071 if (!element)
1072 return E_INVALIDARG;
1073
1074 if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) {
1075 videodata->ime_readingstring[0] = 0;
1076 IME_SendEditingEvent(videodata);
1077 preading->lpVtbl->Release(preading);
1078 }
1079 if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfCandidateListUIElement, (LPVOID *)&pcandlist))) {
1080 videodata->ime_candref--;
1081 if (videodata->ime_candref == 0)
1082 IME_CloseCandidateList(videodata);
1083
1084 pcandlist->lpVtbl->Release(pcandlist);
1085 }
1086 return S_OK;
1087 }
1088
1089 STDMETHODIMP IPPASink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv)
1090 {
1091 if (!ppv)
1092 return E_INVALIDARG;
1093
1094 *ppv = 0;
1095 if (SDL_IsEqualIID(riid, &IID_IUnknown))
1096 *ppv = (IUnknown *)sink;
1097 else if (SDL_IsEqualIID(riid, &IID_ITfInputProcessorProfileActivationSink))
1098 *ppv = (ITfInputProcessorProfileActivationSink *)sink;
1099
1100 if (*ppv) {
1101 TSFSink_AddRef(sink);
1102 return S_OK;
1103 }
1104 return E_NOINTERFACE;
1105 }
1106
1107 STDMETHODIMP IPPASink_OnActivated(TSFSink *sink, DWORD dwProfileType, LANGID langid, REFCLSID clsid, REFGUID catid, REFGUID guidProfile, HKL hkl, DWORD dwFlags)
1108 {
1109 static GUID TF_PROFILE_DAYI = {0x037B2C25, 0x480C, 0x4D7F, 0xB0, 0x27, 0xD6, 0xCA, 0x6B, 0x69, 0x78, 0x8A};
1110 SDL_VideoData *videodata = (SDL_VideoData *)sink->data;
1111 videodata->ime_candlistindexbase = SDL_IsEqualGUID(&TF_PROFILE_DAYI, guidProfile) ? 0 : 1;
1112 if (SDL_IsEqualIID(catid, &GUID_TFCAT_TIP_KEYBOARD) && (dwFlags & TF_IPSINK_FLAG_ACTIVE))
1113 IME_InputLangChanged((SDL_VideoData *)sink->data);
1114
1115 IME_HideCandidateList(videodata);
1116 return S_OK;
1117 }
1118
1119 static void *vtUIElementSink[] = {
1120 (void *)(UIElementSink_QueryInterface),
1121 (void *)(TSFSink_AddRef),
1122 (void *)(TSFSink_Release),
1123 (void *)(UIElementSink_BeginUIElement),
1124 (void *)(UIElementSink_UpdateUIElement),
1125 (void *)(UIElementSink_EndUIElement)
1126 };
1127
1128 static void *vtIPPASink[] = {
1129 (void *)(IPPASink_QueryInterface),
1130 (void *)(TSFSink_AddRef),
1131 (void *)(TSFSink_Release),
1132 (void *)(IPPASink_OnActivated)
1133 };
1134
1135 static void
1136 UILess_EnableUIUpdates(SDL_VideoData *videodata)
1137 {
1138 ITfSource *source = 0;
1139 if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie != TF_INVALID_COOKIE)
1140 return;
1141
1142 if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
1143 source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie);
1144 source->lpVtbl->Release(source);
1145 }
1146 }
1147
1148 static void
1149 UILess_DisableUIUpdates(SDL_VideoData *videodata)
1150 {
1151 ITfSource *source = 0;
1152 if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie == TF_INVALID_COOKIE)
1153 return;
1154
1155 if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
1156 source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie);
1157 videodata->ime_uielemsinkcookie = TF_INVALID_COOKIE;
1158 source->lpVtbl->Release(source);
1159 }
1160 }
1161
1162 static SDL_bool
1163 UILess_SetupSinks(SDL_VideoData *videodata)
1164 {
1165 TfClientId clientid = 0;
1166 SDL_bool result = SDL_FALSE;
1167 ITfSource *source = 0;
1168 if (FAILED(CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgrEx, (LPVOID *)&videodata->ime_threadmgrex)))
1169 return SDL_FALSE;
1170
1171 if (FAILED(videodata->ime_threadmgrex->lpVtbl->ActivateEx(videodata->ime_threadmgrex, &clientid, TF_TMAE_UIELEMENTENABLEDONLY)))
1172 return SDL_FALSE;
1173
1174 videodata->ime_uielemsink = SDL_malloc(sizeof(TSFSink));
1175 videodata->ime_ippasink = SDL_malloc(sizeof(TSFSink));
1176
1177 videodata->ime_uielemsink->lpVtbl = vtUIElementSink;
1178 videodata->ime_uielemsink->refcount = 1;
1179 videodata->ime_uielemsink->data = videodata;
1180
1181 videodata->ime_ippasink->lpVtbl = vtIPPASink;
1182 videodata->ime_ippasink->refcount = 1;
1183 videodata->ime_ippasink->data = videodata;
1184
1185 if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
1186 if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie))) {
1187 if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfInputProcessorProfileActivationSink, (IUnknown *)videodata->ime_ippasink, &videodata->ime_alpnsinkcookie))) {
1188 result = SDL_TRUE;
1189 }
1190 }
1191 source->lpVtbl->Release(source);
1192 }
1193 return result;
1194 }
1195
1196 #define SAFE_RELEASE(p) \
1197 { \
1198 if (p) { \
1199 (p)->lpVtbl->Release((p)); \
1200 (p) = 0; \
1201 } \
1202 }
1203
1204 static void
1205 UILess_ReleaseSinks(SDL_VideoData *videodata)
1206 {
1207 ITfSource *source = 0;
1208 if (videodata->ime_threadmgrex && SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) {
1209 source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie);
1210 source->lpVtbl->UnadviseSink(source, videodata->ime_alpnsinkcookie);
1211 SAFE_RELEASE(source);
1212 videodata->ime_threadmgrex->lpVtbl->Deactivate(videodata->ime_threadmgrex);
1213 SAFE_RELEASE(videodata->ime_threadmgrex);
1214 TSFSink_Release(videodata->ime_uielemsink);
1215 videodata->ime_uielemsink = 0;
1216 TSFSink_Release(videodata->ime_ippasink);
1217 videodata->ime_ippasink = 0;
1218 }
1219 }
1220
1221 static void *
1222 StartDrawToBitmap(HDC hdc, HBITMAP *hhbm, int width, int height)
1223 {
1224 BITMAPINFO info = {0};
1225 BITMAPINFOHEADER *infoHeader = &info.bmiHeader;
1226 BYTE *bits = NULL;
1227 if (hhbm) {
1228 infoHeader->biSize = sizeof(BITMAPINFOHEADER);
1229 infoHeader->biWidth = width;
1230 infoHeader->biHeight = -1 * SDL_abs(height);
1231 infoHeader->biPlanes = 1;
1232 infoHeader->biBitCount = 32;
1233 infoHeader->biCompression = BI_RGB;
1234 *hhbm = CreateDIBSection(hdc, &info, DIB_RGB_COLORS, (void **)&bits, 0, 0);
1235 if (*hhbm)
1236 SelectObject(hdc, *hhbm);
1237 }
1238 return bits;
1239 }
1240
1241 static void
1242 StopDrawToBitmap(HDC hdc, HBITMAP *hhbm)
1243 {
1244 if (hhbm && *hhbm) {
1245 DeleteObject(*hhbm);
1246 *hhbm = NULL;
1247 }
1248 }
1249
1250 static void
1251 BitmapToTexture(HBITMAP hbm, BYTE *bits, int width, int height, SDL_Texture **texture)
1252 {
1253 SDL_Surface *surface = NULL;
1254 BITMAP bm = {0};
1255
1256 if (GetObject(hbm, sizeof(bm), &bm) == 0)
1257 return;
1258
1259 if (bits && texture) {
1260 /*
1261 For transparency:
1262
1263 const Uint8 alpha = 130;
1264 unsigned long *p = (unsigned long *)bits;
1265 unsigned long *end = (unsigned long *)(bits + (bm.bmWidthBytes * bm.bmHeight));
1266 while (p < end) {
1267 *p = RGB(GetRValue(*p), GetGValue(*p), GetBValue(*p)) | (alpha << 24);
1268 ++p;
1269 }
1270 surface = SDL_CreateRGBSurfaceFrom(bits, width, height, bm.bmBitsPixel, bm.bmWidthBytes, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
1271 */
1272 surface = SDL_CreateRGBSurfaceFrom(bits, width, height, bm.bmBitsPixel, bm.bmWidthBytes, 0x00ff0000, 0x0000ff00, 0x000000ff, 0);
1273 if (surface) {
1274 *texture = SDL_CreateTextureFromSurface(0, surface);
1275 SDL_FreeSurface(surface);
1276 }
1277 }
1278 }
1279
1280 /* This draws only within the specified area and fills the entire region. */
1281 static void
1282 DrawRect(HDC hdc, int left, int top, int right, int bottom, int pensize)
1283 {
1284 /* The case of no pen (PenSize = 0) is automatically taken care of. */
1285 const int penadjust = (int)SDL_floor(pensize / 2.0f - 0.5f);
1286 left += pensize / 2;
1287 top += pensize / 2;
1288 right -= penadjust;
1289 bottom -= penadjust;
1290 Rectangle(hdc, left, top, right, bottom);
1291 }
1292
1293 static void
1294 DestroyTexture(SDL_Texture **texture)
1295 {
1296 if (texture && *texture) {
1297 SDL_DestroyTexture(*texture);
1298 *texture = NULL;
1299 }
1300 }
1301
1302 static void
1303 IME_DestroyTextures(SDL_VideoData *videodata)
1304 {
1305 DestroyTexture(&videodata->ime_candtex);
1306 }
1307
1308 #define SDL_swap(a,b) { \
1309 int c = (a); \
1310 (a) = (b); \
1311 (b) = c; \
1312 }
1313
1314 static void
1315 IME_PositionCandidateList(SDL_VideoData *videodata, SIZE size)
1316 {
1317 int left, top, right, bottom;
1318 SDL_bool ok = SDL_FALSE;
1319 int winw = videodata->ime_winwidth;
1320 int winh = videodata->ime_winheight;
1321
1322 /* Bottom */
1323 left = videodata->ime_rect.x;
1324 top = videodata->ime_rect.y + videodata->ime_rect.h;
1325 right = left + size.cx;
1326 bottom = top + size.cy;
1327 if (right >= winw) {
1328 left -= right - winw;
1329 right = winw;
1330 }
1331 if (bottom < winh)
1332 ok = SDL_TRUE;
1333
1334 /* Top */
1335 if (!ok) {
1336 left = videodata->ime_rect.x;
1337 top = videodata->ime_rect.y - size.cy;
1338 right = left + size.cx;
1339 bottom = videodata->ime_rect.y;
1340 if (right >= winw) {
1341 left -= right - winw;
1342 right = winw;
1343 }
1344 if (top >= 0)
1345 ok = SDL_TRUE;
1346 }
1347
1348 /* Right */
1349 if (!ok) {
1350 left = videodata->ime_rect.x + size.cx;
1351 top = 0;
1352 right = left + size.cx;
1353 bottom = size.cy;
1354 if (right < winw)
1355 ok = SDL_TRUE;
1356 }
1357
1358 /* Left */
1359 if (!ok) {
1360 left = videodata->ime_rect.x - size.cx;
1361 top = 0;
1362 right = videodata->ime_rect.x;
1363 bottom = size.cy;
1364 if (right >= 0)
1365 ok = SDL_TRUE;
1366 }
1367
1368 /* Window too small, show at (0,0) */
1369 if (!ok) {
1370 left = 0;
1371 top = 0;
1372 right = size.cx;
1373 bottom = size.cy;
1374 }
1375
1376 videodata->ime_candlistrect.x = left;
1377 videodata->ime_candlistrect.y = top;
1378 videodata->ime_candlistrect.w = right - left;
1379 videodata->ime_candlistrect.h = bottom - top;
1380 }
1381
1382 static void
1383 IME_RenderCandidateList(SDL_VideoData *videodata, HDC hdc)
1384 {
1385 int i, j;
1386 SIZE size = {0};
1387 SIZE candsizes[MAX_CANDLIST];
1388 SIZE maxcandsize = {0};
1389 HBITMAP hbm = NULL;
1390 BYTE *bits = NULL;
1391 const int candcount = SDL_min(SDL_min(MAX_CANDLIST, videodata->ime_candcount), videodata->ime_candpgsize);
1392 SDL_bool vertical = videodata->ime_candvertical;
1393
1394 const int listborder = 1;
1395 const int listpadding = 0;
1396 const int listbordercolor = RGB(0xB4, 0xC7, 0xAA);
1397 const int listfillcolor = RGB(255, 255, 255);
1398
1399 const int candborder = 1;
1400 const int candpadding = 0;
1401 const int candmargin = 1;
1402 const COLORREF candbordercolor = RGB(255, 255, 255);
1403 const COLORREF candfillcolor = RGB(255, 255, 255);
1404 const COLORREF candtextcolor = RGB(0, 0, 0);
1405 const COLORREF selbordercolor = RGB(0x84, 0xAC, 0xDD);
1406 const COLORREF selfillcolor = RGB(0xD2, 0xE6, 0xFF);
1407 const COLORREF seltextcolor = RGB(0, 0, 0);
1408 const int horzcandspacing = 5;
1409
1410 HPEN listpen = listborder != 0 ? CreatePen(PS_SOLID, listborder, listbordercolor) : (HPEN)GetStockObject(NULL_PEN);
1411 HBRUSH listbrush = CreateSolidBrush(listfillcolor);
1412 HPEN candpen = candborder != 0 ? CreatePen(PS_SOLID, candborder, candbordercolor) : (HPEN)GetStockObject(NULL_PEN);
1413 HBRUSH candbrush = CreateSolidBrush(candfillcolor);
1414 HPEN selpen = candborder != 0 ? CreatePen(PS_DOT, candborder, selbordercolor) : (HPEN)GetStockObject(NULL_PEN);
1415 HBRUSH selbrush = CreateSolidBrush(selfillcolor);
1416 HFONT font = CreateFont((int)(1 + videodata->ime_rect.h * 0.75f), 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH | FF_SWISS, TEXT("Microsoft Sans Serif"));
1417
1418 SetBkMode(hdc, TRANSPARENT);
1419 SelectObject(hdc, font);
1420
1421 for (i = 0; i < candcount; ++i) {
1422 const WCHAR *s = videodata->ime_candidates[i];
1423 if (!*s)
1424 break;
1425
1426 GetTextExtentPoint32W(hdc, s, SDL_wcslen(s), &candsizes[i]);
1427 maxcandsize.cx = SDL_max(maxcandsize.cx, candsizes[i].cx);
1428 maxcandsize.cy = SDL_max(maxcandsize.cy, candsizes[i].cy);
1429
1430 }
1431 if (vertical) {
1432 size.cx =
1433 (listborder * 2) +
1434 (listpadding * 2) +
1435 (candmargin * 2) +
1436 (candborder * 2) +
1437 (candpadding * 2) +
1438 (maxcandsize.cx)
1439 ;
1440 size.cy =
1441 (listborder * 2) +
1442 (listpadding * 2) +
1443 ((candcount + 1) * candmargin) +
1444 (candcount * candborder * 2) +
1445 (candcount * candpadding * 2) +
1446 (candcount * maxcandsize.cy)
1447 ;
1448 }
1449 else {
1450 size.cx =
1451 (listborder * 2) +
1452 (listpadding * 2) +
1453 ((candcount + 1) * candmargin) +
1454 (candcount * candborder * 2) +
1455 (candcount * candpadding * 2) +
1456 ((candcount - 1) * horzcandspacing);
1457 ;
1458
1459 for (i = 0; i < candcount; ++i)
1460 size.cx += candsizes[i].cx;
1461
1462 size.cy =
1463 (listborder * 2) +
1464 (listpadding * 2) +
1465 (candmargin * 2) +
1466 (candborder * 2) +
1467 (candpadding * 2) +
1468 (maxcandsize.cy)
1469 ;
1470 }
1471
1472 bits = StartDrawToBitmap(hdc, &hbm, size.cx, size.cy);
1473
1474 SelectObject(hdc, listpen);
1475 SelectObject(hdc, listbrush);
1476 DrawRect(hdc, 0, 0, size.cx, size.cy, listborder);
1477
1478 SelectObject(hdc, candpen);
1479 SelectObject(hdc, candbrush);
1480 SetTextColor(hdc, candtextcolor);
1481 SetBkMode(hdc, TRANSPARENT);
1482
1483 for (i = 0; i < candcount; ++i) {
1484 const WCHAR *s = videodata->ime_candidates[i];
1485 int left, top, right, bottom;
1486 if (!*s)
1487 break;
1488
1489 if (vertical) {
1490 left = listborder + listpadding + candmargin;
1491 top = listborder + listpadding + (i * candborder * 2) + (i * candpadding * 2) + ((i + 1) * candmargin) + (i * maxcandsize.cy);
1492 right = size.cx - listborder - listpadding - candmargin;
1493 bottom = top + maxcandsize.cy + (candpadding * 2) + (candborder * 2);
1494 }
1495 else {
1496 left = listborder + listpadding + (i * candborder * 2) + (i * candpadding * 2) + ((i + 1) * candmargin) + (i * horzcandspacing);
1497
1498 for (j = 0; j < i; ++j)
1499 left += candsizes[j].cx;
1500
1501 top = listborder + listpadding + candmargin;
1502 right = left + candsizes[i].cx + (candpadding * 2) + (candborder * 2);
1503 bottom = size.cy - listborder - listpadding - candmargin;
1504 }
1505
1506 if (i == videodata->ime_candsel) {
1507 SelectObject(hdc, selpen);
1508 SelectObject(hdc, selbrush);
1509 SetTextColor(hdc, seltextcolor);
1510 }
1511 else {
1512 SelectObject(hdc, candpen);
1513 SelectObject(hdc, candbrush);
1514 SetTextColor(hdc, candtextcolor);
1515 }
1516
1517 DrawRect(hdc, left, top, right, bottom, candborder);
1518 ExtTextOutW(hdc, left + candborder + candpadding, top + candborder + candpadding, 0, NULL, s, SDL_wcslen(s), NULL);
1519 }
1520 BitmapToTexture(hbm, bits, size.cx, size.cy, &videodata->ime_candtex);
1521 StopDrawToBitmap(hdc, &hbm);
1522
1523 DeleteObject(listpen);
1524 DeleteObject(listbrush);
1525 DeleteObject(candpen);
1526 DeleteObject(candbrush);
1527 DeleteObject(selpen);
1528 DeleteObject(selbrush);
1529 DeleteObject(font);
1530
1531 IME_PositionCandidateList(videodata, size);
1532 }
1533
1534 static void
1535 IME_Render(SDL_VideoData *videodata)
1536 {
1537 HDC hdc = CreateCompatibleDC(NULL);
1538
1539 if (videodata->ime_candlist)
1540 IME_RenderCandidateList(videodata, hdc);
1541
1542 DeleteDC(hdc);
1543
1544 videodata->ime_dirty = SDL_FALSE;
1545 }
1546
1547 void IME_Present(SDL_VideoData *videodata)
1548 {
1549 if (videodata->ime_dirty)
1550 IME_Render(videodata);
1551
1552 SDL_RenderCopy(videodata->ime_candtex, NULL, &videodata->ime_candlistrect);
1553 }
1554
1555 /* vi: set ts=4 sw=4 expandtab: */