diff options
author | Sam Lantinga <slouken@libsdl.org> | 2010-08-22 12:39:27 -0700 |
---|---|---|
committer | Sam Lantinga <slouken@libsdl.org> | 2010-08-22 12:39:27 -0700 |
commit | 1f8dacabd7b5e26b173ce323677a4f0071d2e4e6 (patch) | |
tree | 97fc90b74ba948256182925a49b55d767430b124 /src | |
parent | 66d40ad33399cbcf2387d95453e78cc2f3f5f2d3 (diff) | |
parent | 7d245d0532a411552d3eb4f3f75c4102819c158e (diff) |
Merged Daniel's Google Summer of Code work from SDL-gsoc2010_IME
Diffstat (limited to 'src')
-rw-r--r-- | src/events/SDL_keyboard.c | 4 | ||||
-rw-r--r-- | src/haptic/nds/SDL_syshaptic.c | 82 | ||||
-rw-r--r-- | src/joystick/nds/SDL_sysjoystick.c | 46 | ||||
-rw-r--r-- | src/stdlib/SDL_string.c | 74 | ||||
-rw-r--r--[-rwxr-xr-x] | src/video/win32/SDL_win32events.c | 4 | ||||
-rw-r--r-- | src/video/win32/SDL_win32keyboard.c | 881 | ||||
-rw-r--r-- | src/video/win32/SDL_win32keyboard.h | 6 | ||||
-rw-r--r-- | src/video/win32/SDL_win32video.c | 3 | ||||
-rw-r--r-- | src/video/win32/SDL_win32video.h | 66 | ||||
-rw-r--r-- | src/video/win32/SDL_win32window.c | 6 |
10 files changed, 1102 insertions, 70 deletions
diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c index 23c85d70..cdee58f8 100644 --- a/src/events/SDL_keyboard.c +++ b/src/events/SDL_keyboard.c @@ -778,7 +778,7 @@ SDL_SendKeyboardText(const char *text) SDL_Event event; event.text.type = SDL_TEXTINPUT; event.text.windowID = keyboard->focus ? keyboard->focus->id : 0; - SDL_strlcpy(event.text.text, text, SDL_arraysize(event.text.text)); + SDL_utf8strlcpy(event.text.text, text, SDL_arraysize(event.text.text)); event.text.windowID = keyboard->focus ? keyboard->focus->id : 0; posted = (SDL_PushEvent(&event) > 0); } @@ -799,7 +799,7 @@ SDL_SendEditingText(const char *text, int start, int length) event.edit.windowID = keyboard->focus ? keyboard->focus->id : 0; event.edit.start = start; event.edit.length = length; - SDL_strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text)); + SDL_utf8strlcpy(event.edit.text, text, SDL_arraysize(event.edit.text)); posted = (SDL_PushEvent(&event) > 0); } return (posted); diff --git a/src/haptic/nds/SDL_syshaptic.c b/src/haptic/nds/SDL_syshaptic.c index 6cf9518b..7ba3c90e 100644 --- a/src/haptic/nds/SDL_syshaptic.c +++ b/src/haptic/nds/SDL_syshaptic.c @@ -43,35 +43,35 @@ typedef struct } NDS_HapticData; -
void -NDS_EZF_OpenNorWrite()
+void +NDS_EZF_OpenNorWrite() { -
GBA_BUS[0x0FF0000] = 0xD200; -
GBA_BUS[0x0000000] = 0x1500; -
GBA_BUS[0x0010000] = 0xD200; -
GBA_BUS[0x0020000] = 0x1500; -
GBA_BUS[0x0E20000] = 0x1500; -
GBA_BUS[0x0FE0000] = 0x1500; -
}
void - -NDS_EZF_CloseNorWrite()
+ GBA_BUS[0x0FF0000] = 0xD200; + GBA_BUS[0x0000000] = 0x1500; + GBA_BUS[0x0010000] = 0xD200; + GBA_BUS[0x0020000] = 0x1500; + GBA_BUS[0x0E20000] = 0x1500; + GBA_BUS[0x0FE0000] = 0x1500; +}
void + +NDS_EZF_CloseNorWrite() { -
GBA_BUS[0x0FF0000] = 0xD200; -
GBA_BUS[0x0000000] = 0x1500; -
GBA_BUS[0x0010000] = 0xD200; -
GBA_BUS[0x0020000] = 0x1500; -
GBA_BUS[0x0E20000] = 0xD200; -
GBA_BUS[0x0FE0000] = 0x1500; -
} + GBA_BUS[0x0FF0000] = 0xD200; + GBA_BUS[0x0000000] = 0x1500; + GBA_BUS[0x0010000] = 0xD200; + GBA_BUS[0x0020000] = 0x1500; + GBA_BUS[0x0E20000] = 0xD200; + GBA_BUS[0x0FE0000] = 0x1500; +} void NDS_EZF_ChipReset() { -
GBA_BUS[0x0000] = 0x00F0; -
GBA_BUS[0x1000] = 0x00F0; -}
uint32 NDS_EZF_IsPresent()
+ GBA_BUS[0x0000] = 0x00F0; + GBA_BUS[0x1000] = 0x00F0; +} uint32 NDS_EZF_IsPresent() { -
vuint16 id1, id2; + vuint16 id1, id2; NDS_EZF_OpenNorWrite(); @@ -81,35 +81,35 @@ NDS_EZF_ChipReset() GBA_BUS[0x1555] = 0x00AA; GBA_BUS[0x12AA] = 0x0055; GBA_BUS[0x1555] = 0x0090; -
id1 = GBA_BUS[0x0001]; -
id2 = GBA_BUS[0x1001]; -
if ((id1 != 0x227E) || (id2 != 0x227E)) { + id1 = GBA_BUS[0x0001]; + id2 = GBA_BUS[0x1001]; + if ((id1 != 0x227E) || (id2 != 0x227E)) { NDS_EZF_CloseNorWrite(); -
return 0; + return 0; } -
id1 = GBA_BUS[0x000E]; -
id2 = GBA_BUS[0x100E]; + id1 = GBA_BUS[0x000E]; + id2 = GBA_BUS[0x100E]; NDS_EZF_CloseNorWrite(); -
if (id1 == 0x2218 && id2 == 0x2218) { -
return 1; + if (id1 == 0x2218 && id2 == 0x2218) { + return 1; } -
return 0; -
} -
void -NDS_EZF_SetShake(u8 pos)
+ return 0; +} +void +NDS_EZF_SetShake(u8 pos) { u16 data = ((pos % 3) | 0x00F0); -
GBA_BUS[0x0FF0000] = 0xD200; -
GBA_BUS[0x0000000] = 0x1500; -
GBA_BUS[0x0010000] = 0xD200; -
GBA_BUS[0x0020000] = 0x1500; -
GBA_BUS[0x0F10000] = data; -
GBA_BUS[0x0FE0000] = 0x1500; + GBA_BUS[0x0FF0000] = 0xD200; + GBA_BUS[0x0000000] = 0x1500; + GBA_BUS[0x0010000] = 0xD200; + GBA_BUS[0x0020000] = 0x1500; + GBA_BUS[0x0F10000] = data; + GBA_BUS[0x0FE0000] = 0x1500; GBA_BUS[0] = 0x0000; /* write any value for vibration. */ GBA_BUS[0] = 0x0002; -
} +} static int SDL_SYS_LogicError(void) diff --git a/src/joystick/nds/SDL_sysjoystick.c b/src/joystick/nds/SDL_sysjoystick.c index cdfbdaca..e885a5da 100644 --- a/src/joystick/nds/SDL_sysjoystick.c +++ b/src/joystick/nds/SDL_sysjoystick.c @@ -45,7 +45,7 @@ int SDL_SYS_JoystickInit(void) { SDL_numjoysticks = 1; -
return (1); + return (1); } /* Function to get the device-dependent name of a joystick */ @@ -73,7 +73,7 @@ SDL_SYS_JoystickOpen(SDL_Joystick * joystick) return 0; } -
+ /* Function to update the state of a joystick - called as a device poll. * This function shouldn't update the joystick structure directly, * but instead should call SDL_PrivateJoystick*() to deliver events @@ -84,8 +84,8 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) { u32 keysd, keysu; int magnitude = 16384; -
- /*scanKeys(); - this is done in PumpEvents, because touch uses it too */
+ + /*scanKeys(); - this is done in PumpEvents, because touch uses it too */ keysd = keysDown(); keysu = keysUp(); @@ -101,61 +101,61 @@ SDL_SYS_JoystickUpdate(SDL_Joystick * joystick) if ((keysd & KEY_RIGHT)) { SDL_PrivateJoystickAxis(joystick, 0, magnitude); } -
if ((keysu & (KEY_UP | KEY_DOWN))) { + if ((keysu & (KEY_UP | KEY_DOWN))) { SDL_PrivateJoystickAxis(joystick, 1, 0); } -
if ((keysu & (KEY_LEFT | KEY_RIGHT))) { + if ((keysu & (KEY_LEFT | KEY_RIGHT))) { SDL_PrivateJoystickAxis(joystick, 0, 0); } -
if ((keysd & KEY_A)) { + if ((keysd & KEY_A)) { SDL_PrivateJoystickButton(joystick, 0, SDL_PRESSED); } -
if ((keysd & KEY_B)) { + if ((keysd & KEY_B)) { SDL_PrivateJoystickButton(joystick, 1, SDL_PRESSED); } -
if ((keysd & KEY_X)) { + if ((keysd & KEY_X)) { SDL_PrivateJoystickButton(joystick, 2, SDL_PRESSED); } -
if ((keysd & KEY_Y)) { + if ((keysd & KEY_Y)) { SDL_PrivateJoystickButton(joystick, 3, SDL_PRESSED); } -
if ((keysd & KEY_L)) { + if ((keysd & KEY_L)) { SDL_PrivateJoystickButton(joystick, 4, SDL_PRESSED); } -
if ((keysd & KEY_R)) { + if ((keysd & KEY_R)) { SDL_PrivateJoystickButton(joystick, 5, SDL_PRESSED); } -
if ((keysd & KEY_SELECT)) { + if ((keysd & KEY_SELECT)) { SDL_PrivateJoystickButton(joystick, 6, SDL_PRESSED); } -
if ((keysd & KEY_START)) { + if ((keysd & KEY_START)) { SDL_PrivateJoystickButton(joystick, 7, SDL_PRESSED); } -
if ((keysu & KEY_A)) { + if ((keysu & KEY_A)) { SDL_PrivateJoystickButton(joystick, 0, SDL_RELEASED); } -
if ((keysu & KEY_B)) { + if ((keysu & KEY_B)) { SDL_PrivateJoystickButton(joystick, 1, SDL_RELEASED); } -
if ((keysu & KEY_X)) { + if ((keysu & KEY_X)) { SDL_PrivateJoystickButton(joystick, 2, SDL_RELEASED); } -
if ((keysu & KEY_Y)) { + if ((keysu & KEY_Y)) { SDL_PrivateJoystickButton(joystick, 3, SDL_RELEASED); } -
if ((keysu & KEY_L)) { + if ((keysu & KEY_L)) { SDL_PrivateJoystickButton(joystick, 4, SDL_RELEASED); } -
if ((keysu & KEY_R)) { + if ((keysu & KEY_R)) { SDL_PrivateJoystickButton(joystick, 5, SDL_RELEASED); } -
if ((keysu & KEY_SELECT)) { + if ((keysu & KEY_SELECT)) { SDL_PrivateJoystickButton(joystick, 6, SDL_RELEASED); } -
if ((keysu & KEY_START)) { + if ((keysu & KEY_START)) { SDL_PrivateJoystickButton(joystick, 7, SDL_RELEASED); } -
} +} /* Function to close a joystick after use */ void diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c index d70f8666..02f08677 100644 --- a/src/stdlib/SDL_string.c +++ b/src/stdlib/SDL_string.c @@ -29,6 +29,21 @@ #define SDL_isupperhex(X) (((X) >= 'A') && ((X) <= 'F')) #define SDL_islowerhex(X) (((X) >= 'a') && ((X) <= 'f')) +#define UTF8_IsLeadByte(c) ((c) >= 0xC0 && (c) <= 0xF4) +#define UTF8_IsTrailingByte(c) ((c) >= 0x80 && (c) <= 0xBF) + +int UTF8_TrailingBytes(unsigned char c) +{ + if (c >= 0xC0 && c<= 0xDF) + return 1; + else if (c >= 0xE0 && c <= 0xEF) + return 2; + else if (c >= 0xF0 && c <= 0xF4) + return 3; + else + return 0; +} + #if !defined(HAVE_SSCANF) || !defined(HAVE_STRTOL) static size_t SDL_ScanLong(const char *text, int radix, long *valuep) @@ -348,6 +363,33 @@ SDL_wcslen(const wchar_t * string) } #endif +#ifndef HAVE_WCSLCPY +size_t +SDL_wcslcpy(wchar_t *dst, const wchar_t *src, size_t maxlen) +{ + size_t srclen = SDL_wcslen(src); + if (maxlen > 0) { + size_t len = SDL_min(srclen, maxlen - 1); + SDL_memcpy(dst, src, len * sizeof(wchar_t)); + dst[len] = '\0'; + } + return srclen; +} +#endif + +#ifndef HAVE_WCSLCAT +size_t +SDL_wcslcat(wchar_t *dst, const wchar_t *src, size_t maxlen) +{ + size_t dstlen = SDL_wcslen(dst); + size_t srclen = SDL_wcslen(src); + if (dstlen < maxlen) { + SDL_wcslcpy(dst + dstlen, src, maxlen - dstlen); + } + return dstlen + srclen; +} +#endif + #ifndef HAVE_STRLCPY size_t SDL_strlcpy(char *dst, const char *src, size_t maxlen) @@ -362,6 +404,38 @@ SDL_strlcpy(char *dst, const char *src, size_t maxlen) } #endif +size_t SDL_utf8strlcpy(char *dst, const char *src, size_t dst_bytes) +{ + size_t src_bytes = SDL_strlen(src); + size_t bytes = SDL_min(src_bytes, dst_bytes - 1); + int i = 0; + char trailing_bytes = 0; + if (bytes) + { + unsigned char c = (unsigned char)src[bytes - 1]; + if (UTF8_IsLeadByte(c)) + --bytes; + else if (UTF8_IsTrailingByte(c)) + { + for (i = bytes - 1; i != 0; --i) + { + c = (unsigned char)src[i]; + trailing_bytes = UTF8_TrailingBytes(c); + if (trailing_bytes) + { + if (bytes - i != trailing_bytes + 1) + bytes = i; + + break; + } + } + } + SDL_memcpy(dst, src, bytes); + } + dst[bytes] = '\0'; + return bytes; +} + #ifndef HAVE_STRLCAT size_t SDL_strlcat(char *dst, const char *src, size_t maxlen) diff --git a/src/video/win32/SDL_win32events.c b/src/video/win32/SDL_win32events.c index 3eae9bbe..272cedca 100755..100644 --- a/src/video/win32/SDL_win32events.c +++ b/src/video/win32/SDL_win32events.c @@ -139,6 +139,8 @@ WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) } #endif + if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata)) + return 0; switch (msg) { @@ -605,7 +607,7 @@ SDL_RegisterApp(char *name, Uint32 style, void *hInst) class.hbrBackground = NULL; class.hInstance = SDL_Instance; class.style = SDL_Appstyle; - class.lpfnWndProc = DefWindowProc; + class.lpfnWndProc = WIN_WindowProc; class.cbWndExtra = 0; class.cbClsExtra = 0; if (!RegisterClass(&class)) { diff --git a/src/video/win32/SDL_win32keyboard.c b/src/video/win32/SDL_win32keyboard.c index 5a7523b0..597cc534 100644 --- a/src/video/win32/SDL_win32keyboard.c +++ b/src/video/win32/SDL_win32keyboard.c @@ -26,6 +26,14 @@ #include "../../events/SDL_keyboard_c.h" #include "../../events/scancodes_win32.h" +#include <imm.h> +#include <oleauto.h> + +static void IME_Init(SDL_VideoData *videodata, HWND hwnd); +static void IME_Enable(SDL_VideoData *videodata, HWND hwnd); +static void IME_Disable(SDL_VideoData *videodata, HWND hwnd); +static void IME_Quit(SDL_VideoData *videodata); + #ifndef MAPVK_VK_TO_VSC #define MAPVK_VK_TO_VSC 0 #endif @@ -81,6 +89,34 @@ WIN_InitKeyboard(_THIS) data->key_layout = win32_scancode_table; + data->ime_com_initialized = SDL_FALSE; + data->ime_threadmgr = 0; + data->ime_initialized = SDL_FALSE; + data->ime_enabled = SDL_FALSE; + data->ime_available = SDL_FALSE; + data->ime_hwnd_main = 0; + data->ime_hwnd_current = 0; + data->ime_himc = 0; + data->ime_composition[0] = 0; + data->ime_readingstring[0] = 0; + data->ime_cursor = 0; + data->ime_hkl = 0; + data->ime_himm32 = 0; + data->GetReadingString = 0; + data->ShowReadingWindow = 0; + data->ImmLockIMC = 0; + data->ImmUnlockIMC = 0; + data->ImmLockIMCC = 0; + data->ImmUnlockIMCC = 0; + data->ime_uiless = SDL_FALSE; + data->ime_threadmgrex = 0; + data->ime_uielemsinkcookie = TF_INVALID_COOKIE; + data->ime_alpnsinkcookie = TF_INVALID_COOKIE; + data->ime_openmodesinkcookie = TF_INVALID_COOKIE; + data->ime_convmodesinkcookie = TF_INVALID_COOKIE; + data->ime_uielemsink = 0; + data->ime_ippasink = 0; + WIN_UpdateKeymap(); SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu"); @@ -119,6 +155,851 @@ WIN_UpdateKeymap() void WIN_QuitKeyboard(_THIS) { + IME_Quit((SDL_VideoData *)_this->driverdata); +} + +void +WIN_StartTextInput(_THIS) +{ + SDL_Window *window = SDL_GetKeyboardFocus(); + if (window) { + HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd; + SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata; + IME_Init(videodata, hwnd); + IME_Enable(videodata, hwnd); + } +} + +void +WIN_StopTextInput(_THIS) +{ + SDL_Window *window = SDL_GetKeyboardFocus(); + if (window) { + HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd; + SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata; + IME_Init(videodata, hwnd); + IME_Disable(videodata, hwnd); + } +} + +void +WIN_SetTextInputRect(_THIS, SDL_Rect *rect) +{ + +} + +#define LANG_CHT MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_TRADITIONAL) +#define LANG_CHS MAKELANGID(LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED) + +#define MAKEIMEVERSION(major,minor) ((DWORD) (((BYTE)(major) << 24) | ((BYTE)(minor) << 16) )) +#define IMEID_VER(id) ((id) & 0xffff0000) +#define IMEID_LANG(id) ((id) & 0x0000ffff) + +#define CHT_HKL_DAYI ((HKL)0xE0060404) +#define CHT_HKL_NEW_PHONETIC ((HKL)0xE0080404) +#define CHT_HKL_NEW_CHANG_JIE ((HKL)0xE0090404) +#define CHT_HKL_NEW_QUICK ((HKL)0xE00A0404) +#define CHT_HKL_HK_CANTONESE ((HKL)0xE00B0404) +#define CHT_IMEFILENAME1 "TINTLGNT.IME" +#define CHT_IMEFILENAME2 "CINTLGNT.IME" +#define CHT_IMEFILENAME3 "MSTCIPHA.IME" +#define IMEID_CHT_VER42 (LANG_CHT | MAKEIMEVERSION(4, 2)) +#define IMEID_CHT_VER43 (LANG_CHT | MAKEIMEVERSION(4, 3)) +#define IMEID_CHT_VER44 (LANG_CHT | MAKEIMEVERSION(4, 4)) +#define IMEID_CHT_VER50 (LANG_CHT | MAKEIMEVERSION(5, 0)) +#define IMEID_CHT_VER51 (LANG_CHT | MAKEIMEVERSION(5, 1)) +#define IMEID_CHT_VER52 (LANG_CHT | MAKEIMEVERSION(5, 2)) +#define IMEID_CHT_VER60 (LANG_CHT | MAKEIMEVERSION(6, 0)) +#define IMEID_CHT_VER_VISTA (LANG_CHT | MAKEIMEVERSION(7, 0)) + +#define CHS_HKL ((HKL)0xE00E0804) +#define CHS_IMEFILENAME1 "PINTLGNT.IME" +#define CHS_IMEFILENAME2 "MSSCIPYA.IME" +#define IMEID_CHS_VER41 (LANG_CHS | MAKEIMEVERSION(4, 1)) +#define IMEID_CHS_VER42 (LANG_CHS | MAKEIMEVERSION(4, 2)) +#define IMEID_CHS_VER53 (LANG_CHS | MAKEIMEVERSION(5, 3)) + +#define LANG() LOWORD((videodata->ime_hkl)) +#define PRIMLANG() ((WORD)PRIMARYLANGID(LANG())) +#define SUBLANG() SUBLANGID(LANG()) + +static void IME_UpdateInputLocale(SDL_VideoData *videodata); +static void IME_ClearComposition(SDL_VideoData *videodata); +static void IME_SetWindow(SDL_VideoData* videodata, HWND hwnd); +static void IME_SetupAPI(SDL_VideoData *videodata); +static DWORD IME_GetId(SDL_VideoData *videodata, UINT uIndex); +static void IME_SendEditingEvent(SDL_VideoData *videodata); +#define SDL_IsEqualIID(riid1, riid2) SDL_IsEqualGUID(riid1, riid2) +#define SDL_IsEqualGUID(rguid1, rguid2) (!SDL_memcmp(rguid1, rguid2, sizeof(GUID))) + +static SDL_bool UILess_SetupSinks(SDL_VideoData *videodata); +static void UILess_ReleaseSinks(SDL_VideoData *videodata); +static void UILess_EnableUIUpdates(SDL_VideoData *videodata); +static void UILess_DisableUIUpdates(SDL_VideoData *videodata); + +static void +IME_Init(SDL_VideoData *videodata, HWND hwnd) +{ + if (videodata->ime_initialized) + return; + + videodata->ime_hwnd_main = hwnd; + if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) { + videodata->ime_com_initialized = SDL_TRUE; + CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, &videodata->ime_threadmgr); + } + videodata->ime_initialized = SDL_TRUE; + videodata->ime_himm32 = LoadLibraryA("imm32.dll"); + if (!videodata->ime_himm32) { + videodata->ime_available = SDL_FALSE; + return; + } + videodata->ImmLockIMC = (LPINPUTCONTEXT2 (WINAPI *)(HIMC))GetProcAddress(videodata->ime_himm32, "ImmLockIMC"); + videodata->ImmUnlockIMC = (BOOL (WINAPI *)(HIMC))GetProcAddress(videodata->ime_himm32, "ImmUnlockIMC"); + videodata->ImmLockIMCC = (LPVOID (WINAPI *)(HIMCC))GetProcAddress(videodata->ime_himm32, "ImmLockIMCC"); + videodata->ImmUnlockIMCC = (BOOL (WINAPI *)(HIMCC))GetProcAddress(videodata->ime_himm32, "ImmUnlockIMCC"); + + IME_SetWindow(videodata, hwnd); + videodata->ime_himc = ImmGetContext(hwnd); + ImmReleaseContext(hwnd, videodata->ime_himc); + if (!videodata->ime_himc) { + videodata->ime_available = SDL_FALSE; + IME_Disable(videodata, hwnd); + return; + } + videodata->ime_available = SDL_TRUE; + IME_UpdateInputLocale(videodata); + IME_SetupAPI(videodata); + videodata->ime_uiless = UILess_SetupSinks(videodata); + IME_UpdateInputLocale(videodata); + IME_Disable(videodata, hwnd); +} + +static void +IME_Enable(SDL_VideoData *videodata, HWND hwnd) +{ + if (!videodata->ime_initialized || !videodata->ime_hwnd_current) + return; + + if (!videodata->ime_available) { + IME_Disable(videodata, hwnd); + return; + } + if (videodata->ime_hwnd_current == videodata->ime_hwnd_main) + ImmAssociateContext(videodata->ime_hwnd_current, videodata->ime_himc); + + videodata->ime_enabled = SDL_TRUE; + IME_UpdateInputLocale(videodata); + UILess_EnableUIUpdates(videodata); +} + +static void +IME_Disable(SDL_VideoData *videodata, HWND hwnd) +{ + if (!videodata->ime_initialized || !videodata->ime_hwnd_current) + return; + + IME_ClearComposition(videodata); + if (videodata->ime_hwnd_current == videodata->ime_hwnd_main) + ImmAssociateContext(videodata->ime_hwnd_current, NULL); + + videodata->ime_enabled = SDL_FALSE; + UILess_DisableUIUpdates(videodata); +} + +static void +IME_Quit(SDL_VideoData *videodata) +{ + if (!videodata->ime_initialized) + return; + + UILess_ReleaseSinks(videodata); + if (videodata->ime_hwnd_main) + ImmAssociateContext(videodata->ime_hwnd_main, videodata->ime_himc); + + videodata->ime_hwnd_main = 0; + videodata->ime_himc = 0; + if (videodata->ime_himm32) { + FreeLibrary(videodata->ime_himm32); + videodata->ime_himm32 = 0; + } + if (videodata->ime_threadmgr) { + videodata->ime_threadmgr->lpVtbl->Release(videodata->ime_threadmgr); + videodata->ime_threadmgr = 0; + } + if (videodata->ime_com_initialized) { + CoUninitialize(); + videodata->ime_com_initialized = SDL_FALSE; + } + videodata->ime_initialized = SDL_FALSE; +} + +static void +IME_GetReadingString(SDL_VideoData *videodata, HWND hwnd) +{ + DWORD id = 0; + HIMC himc = 0; + WCHAR buffer[16]; + WCHAR *s = buffer; + DWORD len = 0; + DWORD err = 0; + BOOL vertical = FALSE; + UINT maxuilen = 0; + static OSVERSIONINFOA osversion = {0}; + if (videodata->ime_uiless) + return; + + videodata->ime_readingstring[0] = 0; + if (!osversion.dwOSVersionInfoSize) { + osversion.dwOSVersionInfoSize = sizeof(osversion); + GetVersionExA(&osversion); + } + id = IME_GetId(videodata, 0); + if (!id) + return; + + himc = ImmGetContext(hwnd); + if (!himc) + return; + + if (videodata->GetReadingString) { + len = videodata->GetReadingString(himc, 0, 0, &err, &vertical, &maxuilen); + if (len) { + if (len > SDL_arraysize(buffer)) + len = SDL_arraysize(buffer); + + len = videodata->GetReadingString(himc, len, s, &err, &vertical, &maxuilen); + } + SDL_wcslcpy(videodata->ime_readingstring, s, len); + } + else { + LPINPUTCONTEXT2 lpimc = videodata->ImmLockIMC(himc); + LPBYTE p = 0; + s = 0; + switch (id) + { + case IMEID_CHT_VER42: + case IMEID_CHT_VER43: + case IMEID_CHT_VER44: + p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 24); + if (!p) + break; + + len = *(DWORD *)(p + 7*4 + 32*4); + s = (WCHAR *)(p + 56); + break; + case IMEID_CHT_VER51: + case IMEID_CHT_VER52: + case IMEID_CHS_VER53: + p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 4); + if (!p) + break; + + p = *(LPBYTE *)((LPBYTE)p + 1*4 + 5*4); + if (!p) + break; + + len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2); + s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4); + break; + case IMEID_CHS_VER41: + { + int offset = (IME_GetId(videodata, 1) >= 0x00000002) ? 8 : 7; + p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + offset * 4); + if (!p) + break; + + len = *(DWORD *)(p + 7*4 + 16*2*4); + s = (WCHAR *)(p + 6*4 + 16*2*1); + } + break; + case IMEID_CHS_VER42: + if (osversion.dwPlatformId != VER_PLATFORM_WIN32_NT) + break; + + p = *(LPBYTE *)((LPBYTE)videodata->ImmLockIMCC(lpimc->hPrivate) + 1*4 + 1*4 + 6*4); + if (!p) + break; + + len = *(DWORD *)(p + 1*4 + (16*2+2*4) + 5*4 + 16*2); + s = (WCHAR *)(p + 1*4 + (16*2+2*4) + 5*4); + break; + } + if (s) + SDL_wcslcpy(videodata->ime_readingstring, s, len + 1); + + videodata->ImmUnlockIMCC(lpimc->hPrivate); + videodata->ImmUnlockIMC(himc); + } + ImmReleaseContext(hwnd, himc); + IME_SendEditingEvent(videodata); +} + +static void +IME_InputLangChanged(SDL_VideoData *videodata) +{ + UINT lang = PRIMLANG(); + HWND hwndime = 0; + IME_UpdateInputLocale(videodata); + IME_SetupAPI(videodata); + if (lang != PRIMLANG()) { + IME_ClearComposition(videodata); + } + hwndime = ImmGetDefaultIMEWnd(videodata->ime_hwnd_current); + if (hwndime) { + SendMessageA(hwndime, WM_IME_CONTROL, IMC_OPENSTATUSWINDOW, 0); + SendMessageA(hwndime, WM_IME_CONTROL, IMC_CLOSESTATUSWINDOW, 0); + } +} + +static DWORD +IME_GetId(SDL_VideoData *videodata, UINT uIndex) +{ + static HKL hklprev = 0; + static DWORD dwRet[2] = {0}; + DWORD dwVerSize = 0; + DWORD dwVerHandle = 0; + LPVOID lpVerBuffer = 0; + LPVOID lpVerData = 0; + UINT cbVerData = 0; + char szTemp[256]; + HKL hkl = 0; + DWORD dwLang = 0; + if (uIndex >= sizeof(dwRet) / sizeof(dwRet[0])) + return 0; + + hkl = videodata->ime_hkl; + if (hklprev == hkl) + return dwRet[uIndex]; + + hklprev = hkl; + dwLang = ((DWORD)hkl & 0xffff); + if (videodata->ime_uiless && LANG() == LANG_CHT) { + dwRet[0] = IMEID_CHT_VER_VISTA; + dwRet[1] = 0; + return dwRet[0]; + } + if (hkl != CHT_HKL_NEW_PHONETIC + && hkl != CHT_HKL_NEW_CHANG_JIE + && hkl != CHT_HKL_NEW_QUICK + && hkl != CHT_HKL_HK_CANTONESE + && hkl != CHS_HKL) { + dwRet[0] = dwRet[1] = 0; + return dwRet[uIndex]; + } + if (ImmGetIMEFileNameA(hkl, szTemp, sizeof(szTemp) - 1) <= 0) { + dwRet[0] = dwRet[1] = 0; + return dwRet[uIndex]; + } + if (!videodata->GetReadingString) { + #define LCID_INVARIANT MAKELCID(MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), SORT_DEFAULT) + if (CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME1, -1) != 2 + && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME2, -1) != 2 + && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHT_IMEFILENAME3, -1) != 2 + && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME1, -1) != 2 + && CompareStringA(LCID_INVARIANT, NORM_IGNORECASE, szTemp, -1, CHS_IMEFILENAME2, -1) != 2) { + dwRet[0] = dwRet[1] = 0; + return dwRet[uIndex]; + } + #undef LCID_INVARIANT + dwVerSize = GetFileVersionInfoSizeA(szTemp, &dwVerHandle); + if (dwVerSize) { + lpVerBuffer = SDL_malloc(dwVerSize); + if (lpVerBuffer) { + if (GetFileVersionInfoA(szTemp, dwVerHandle, dwVerSize, lpVerBuffer)) { + if (VerQueryValueA(lpVerBuffer, "\\", &lpVerData, &cbVerData)) { + #define pVerFixedInfo ((VS_FIXEDFILEINFO FAR*)lpVerData) + DWORD dwVer = pVerFixedInfo->dwFileVersionMS; + dwVer = (dwVer & 0x00ff0000) << 8 | (dwVer & 0x000000ff) << 16; + if (videodata->GetReadingString || + dwLang == LANG_CHT && ( + dwVer == MAKEIMEVERSION(4, 2) || + dwVer == MAKEIMEVERSION(4, 3) || + dwVer == MAKEIMEVERSION(4, 4) || + dwVer == MAKEIMEVERSION(5, 0) || + dwVer == MAKEIMEVERSION(5, 1) || + dwVer == MAKEIMEVERSION(5, 2) || + dwVer == MAKEIMEVERSION(6, 0)) + || + dwLang == LANG_CHS && ( + dwVer == MAKEIMEVERSION(4, 1) || + dwVer == MAKEIMEVERSION(4, 2) || + dwVer == MAKEIMEVERSION(5, 3))) { + dwRet[0] = dwVer | dwLang; + dwRet[1] = pVerFixedInfo->dwFileVersionLS; + SDL_free(lpVerBuffer); + return dwRet[0]; + } + #undef pVerFixedInfo + } + } + } + SDL_free(lpVerBuffer); + } + } + dwRet[0] = dwRet[1] = 0; + return dwRet[uIndex]; +} + +static void +IME_SetupAPI(SDL_VideoData *videodata) +{ + char ime_file[MAX_PATH + 1]; + HMODULE hime = 0; + HKL hkl = 0; + videodata->GetReadingString = 0; + videodata->ShowReadingWindow = 0; + if (videodata->ime_uiless) + return; + + hkl = videodata->ime_hkl; + if (ImmGetIMEFileNameA(hkl, ime_file, sizeof(ime_file) - 1) <= 0) + return; + + hime = LoadLibraryA(ime_file); + if (!hime) + return; + + videodata->GetReadingString = (UINT (WINAPI *)(HIMC, UINT, LPWSTR, PINT, BOOL*, PUINT)) + GetProcAddress(hime, "GetReadingString"); + videodata->ShowReadingWindow = (BOOL (WINAPI *)(HIMC, BOOL)) + GetProcAddress(hime, "ShowReadingWindow"); + + if (videodata->ShowReadingWindow) { + HIMC himc = ImmGetContext(videodata->ime_hwnd_current); + if (himc) { + videodata->ShowReadingWindow(himc, FALSE); + ImmReleaseContext(videodata->ime_hwnd_current, himc); + } + } +} + +static void +IME_SetWindow(SDL_VideoData* videodata, HWND hwnd) +{ + videodata->ime_hwnd_current = hwnd; + if (videodata->ime_threadmgr) { + struct ITfDocumentMgr *document_mgr = 0; + if (SUCCEEDED(videodata->ime_threadmgr->lpVtbl->AssociateFocus(videodata->ime_threadmgr, hwnd, NULL, &document_mgr))) { + if (document_mgr) + document_mgr->lpVtbl->Release(document_mgr); + } + } +} + +static void +IME_UpdateInputLocale(SDL_VideoData *videodata) +{ + static HKL hklprev = 0; + videodata->ime_hkl = GetKeyboardLayout(0); + if (hklprev == videodata->ime_hkl) + return; + + hklprev = videodata->ime_hkl; +} + +static void +IME_ClearComposition(SDL_VideoData *videodata) +{ + HIMC himc = 0; + if (!videodata->ime_initialized) + return; + + himc = ImmGetContext(videodata->ime_hwnd_current); + if (!himc) + return; + + ImmNotifyIME(himc, NI_COMPOSITIONSTR, CPS_CANCEL, 0); + if (videodata->ime_uiless) + ImmSetCompositionString(himc, SCS_SETSTR, TEXT(""), sizeof(TCHAR), TEXT(""), sizeof(TCHAR)); + + ImmNotifyIME(himc, NI_CLOSECANDIDATE, 0, 0); + ImmReleaseContext(videodata->ime_hwnd_current, himc); + SDL_SendEditingText("", 0, 0); +} + +static void +IME_ClearEditing(SDL_VideoData *videodata) +{ + +} + +static void +IME_GetCompositionString(SDL_VideoData *videodata, HIMC himc, DWORD string) +{ + LONG length = ImmGetCompositionStringW(himc, string, videodata->ime_composition, sizeof(videodata->ime_composition)); + if (length < 0) + length = 0; + + length /= sizeof(videodata->ime_composition[0]); + videodata->ime_cursor = LOWORD(ImmGetCompositionStringW(himc, GCS_CURSORPOS, 0, 0)); + if (videodata->ime_composition[videodata->ime_cursor] == 0x3000) { + int i; + for (i = videodata->ime_cursor + 1; i < length; ++i) + videodata->ime_composition[i - 1] = videodata->ime_composition[i]; + + --length; + } + videodata->ime_composition[length] = 0; +} + +static void +IME_SendInputEvent(SDL_VideoData *videodata) +{ + char *s = 0; + s = WIN_StringToUTF8(videodata->ime_composition); + SDL_SendKeyboardText(s); + SDL_free(s); + + videodata->ime_composition[0] = 0; + videodata->ime_readingstring[0] = 0; + videodata->ime_cursor = 0; +} + +static void +IME_SendEditingEvent(SDL_VideoData *videodata) +{ + char *s = 0; + WCHAR buffer[SDL_TEXTEDITINGEVENT_TEXT_SIZE]; + buffer[0] = 0; + if (videodata->ime_readingstring[0]) { + size_t len = SDL_min(SDL_wcslen(videodata->ime_composition), (size_t)videodata->ime_cursor); + SDL_wcslcpy(buffer, videodata->ime_composition, len + 1); + SDL_wcslcat(buffer, videodata->ime_readingstring, sizeof(buffer)); + SDL_wcslcat(buffer, &videodata->ime_composition[len], sizeof(buffer) - len); + } + else { + SDL_wcslcpy(buffer, videodata->ime_composition, sizeof(videodata->ime_composition)); + } + s = WIN_StringToUTF8(buffer); + SDL_SendEditingText(s, videodata->ime_cursor + SDL_wcslen(videodata->ime_readingstring), 0); + SDL_free(s); +} + +SDL_bool +IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata) +{ + SDL_bool trap = SDL_FALSE; + HIMC himc = 0; + if (!videodata->ime_initialized || !videodata->ime_available || !videodata->ime_enabled) + return SDL_FALSE; + + switch (msg) + { + case WM_INPUTLANGCHANGE: + //IME_InputLangChanged(videodata); + break; + case WM_IME_SETCONTEXT: + *lParam = 0; + break; + case WM_IME_STARTCOMPOSITION: + trap = SDL_TRUE; + break; + case WM_IME_COMPOSITION: + trap = SDL_TRUE; + himc = ImmGetContext(hwnd); + if (*lParam & GCS_RESULTSTR) { + IME_GetCompositionString(videodata, himc, GCS_RESULTSTR); + IME_SendInputEvent(videodata); + } + if (*lParam & GCS_COMPSTR) { + if (!videodata->ime_uiless) + videodata->ime_readingstring[0] = 0; + + IME_GetCompositionString(videodata, himc, GCS_COMPSTR); + IME_SendEditingEvent(videodata); + } + ImmReleaseContext(hwnd, himc); + break; + case WM_IME_ENDCOMPOSITION: + videodata->ime_composition[0] = 0; + videodata->ime_readingstring[0] = 0; + videodata->ime_cursor = 0; + SDL_SendEditingText("", 0, 0); + break; + case WM_IME_NOTIFY: + switch (wParam) + { + case IMN_SETCONVERSIONMODE: + case IMN_SETOPENSTATUS: + IME_UpdateInputLocale(videodata); + break; + case IMN_OPENCANDIDATE: + case IMN_CHANGECANDIDATE: + trap = SDL_TRUE; + break; + case IMN_CLOSECANDIDATE: + trap = SDL_TRUE; + break; + case IMN_PRIVATE: + { + DWORD dwId = IME_GetId(videodata, 0); + IME_GetReadingString(videodata, hwnd); + switch (dwId) + { + case IMEID_CHT_VER42: + case IMEID_CHT_VER43: + case IMEID_CHT_VER44: + case IMEID_CHS_VER41: + case IMEID_CHS_VER42: + if (*lParam == 1 || *lParam == 2) + trap = SDL_TRUE; + + break; + case IMEID_CHT_VER50: + case IMEID_CHT_VER51: + case IMEID_CHT_VER52: + case IMEID_CHT_VER60: + case IMEID_CHS_VER53: + if (*lParam == 16 + || *lParam == 17 + || *lParam == 26 + || *lParam == 27 + || *lParam == 28) + trap = SDL_TRUE; + break; + } + } + break; + default: + trap = SDL_TRUE; + break; + } + break; + } + return trap; +} + +STDMETHODIMP_(ULONG) TSFSink_AddRef(TSFSink *sink) +{ + return ++sink->refcount; +} + +STDMETHODIMP_(ULONG)TSFSink_Release(TSFSink *sink) +{ + --sink->refcount; + if (sink->refcount == 0) + { + SDL_free(sink); + return 0; + } + return sink->refcount; +} + +STDMETHODIMP UIElementSink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv) +{ + if (!ppv) + return E_INVALIDARG; + + *ppv = 0; + if (SDL_IsEqualIID(riid, &IID_IUnknown)) + *ppv = (IUnknown *)sink; + else if (SDL_IsEqualIID(riid, &IID_ITfUIElementSink)) + *ppv = (ITfUIElementSink *)sink; + + if (*ppv) { + TSFSink_AddRef(sink); + return S_OK; + } + return E_NOINTERFACE; +} + +ITfUIElement *UILess_GetUIElement(SDL_VideoData *videodata, DWORD dwUIElementId) +{ + ITfUIElementMgr *puiem = 0; + ITfUIElement *pelem = 0; + ITfThreadMgrEx *threadmgrex = videodata->ime_threadmgrex; + + if (SUCCEEDED(threadmgrex->lpVtbl->QueryInterface(threadmgrex, &IID_ITfUIElementMgr, (LPVOID *)&puiem))) { + puiem->lpVtbl->GetUIElement(puiem, dwUIElementId, &pelem); + puiem->lpVtbl->Release(puiem); + } + return pelem; +} + +STDMETHODIMP UIElementSink_BeginUIElement(TSFSink *sink, DWORD dwUIElementId, BOOL *pbShow) +{ + ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId); + ITfReadingInformationUIElement *preading = 0; + SDL_VideoData *videodata = (SDL_VideoData *)sink->data; + if (!element) + return E_INVALIDARG; + + *pbShow = FALSE; + if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) { + BSTR bstr; + if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr) { + WCHAR *s = (WCHAR *)bstr; + SysFreeString(bstr); + } + preading->lpVtbl->Release(preading); + } + return S_OK; +} + +STDMETHODIMP UIElementSink_UpdateUIElement(TSFSink *sink, DWORD dwUIElementId) +{ + ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId); + ITfReadingInformationUIElement *preading = 0; + SDL_VideoData *videodata = (SDL_VideoData *)sink->data; + if (!element) + return E_INVALIDARG; + + if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) { + BSTR bstr; + if (SUCCEEDED(preading->lpVtbl->GetString(preading, &bstr)) && bstr) { + WCHAR *s = (WCHAR *)bstr; + SDL_wcslcpy(videodata->ime_readingstring, s, sizeof(videodata->ime_readingstring)); + IME_SendEditingEvent(videodata); + SysFreeString(bstr); + } + preading->lpVtbl->Release(preading); + } + return S_OK; +} + +STDMETHODIMP UIElementSink_EndUIElement(TSFSink *sink, DWORD dwUIElementId) +{ + ITfUIElement *element = UILess_GetUIElement((SDL_VideoData *)sink->data, dwUIElementId); + ITfReadingInformationUIElement *preading = 0; + SDL_VideoData *videodata = (SDL_VideoData *)sink->data; + if (!element) + return E_INVALIDARG; + + if (SUCCEEDED(element->lpVtbl->QueryInterface(element, &IID_ITfReadingInformationUIElement, (LPVOID *)&preading))) { + videodata->ime_readingstring[0] = 0; + IME_SendEditingEvent(videodata); + preading->lpVtbl->Release(preading); + } + return S_OK; +} + +STDMETHODIMP IPPASink_QueryInterface(TSFSink *sink, REFIID riid, PVOID *ppv) +{ + if (!ppv) + return E_INVALIDARG; + + *ppv = 0; + if (SDL_IsEqualIID(riid, &IID_IUnknown)) + *ppv = (IUnknown *)sink; + else if (SDL_IsEqualIID(riid, &IID_ITfInputProcessorProfileActivationSink)) + *ppv = (ITfInputProcessorProfileActivationSink *)sink; + + if (*ppv) { + TSFSink_AddRef(sink); + return S_OK; + } + return E_NOINTERFACE; +} + +STDMETHODIMP IPPASink_OnActivated(TSFSink *sink, DWORD dwProfileType, LANGID langid, REFCLSID clsid, REFGUID catid, REFGUID guidProfile, HKL hkl, DWORD dwFlags) +{ + if (SDL_IsEqualIID(catid, &GUID_TFCAT_TIP_KEYBOARD) && (dwFlags & TF_IPSINK_FLAG_ACTIVE)) + IME_InputLangChanged((SDL_VideoData *)sink->data); + + return S_OK; +} + +static void *vtUIElementSink[] = { + (void *)(UIElementSink_QueryInterface), + (void *)(TSFSink_AddRef), + (void *)(TSFSink_Release), + (void *)(UIElementSink_BeginUIElement), + (void *)(UIElementSink_UpdateUIElement), + (void *)(UIElementSink_EndUIElement) +}; + +static void *vtIPPASink[] = { + (void *)(IPPASink_QueryInterface), + (void *)(TSFSink_AddRef), + (void *)(TSFSink_Release), + (void *)(IPPASink_OnActivated) +}; + +static void +UILess_EnableUIUpdates(SDL_VideoData *videodata) +{ + ITfSource *source = 0; + if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie != TF_INVALID_COOKIE) + return; + + if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) { + source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie); + source->lpVtbl->Release(source); + } +} + +static void +UILess_DisableUIUpdates(SDL_VideoData *videodata) +{ + ITfSource *source = 0; + if (!videodata->ime_threadmgrex || videodata->ime_uielemsinkcookie == TF_INVALID_COOKIE) + return; + + if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) { + source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie); + videodata->ime_uielemsinkcookie = TF_INVALID_COOKIE; + source->lpVtbl->Release(source); + } +} + +static SDL_bool +UILess_SetupSinks(SDL_VideoData *videodata) +{ + TfClientId clientid = 0; + SDL_bool result = SDL_FALSE; + ITfSource *source = 0; + if (FAILED(CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgrEx, &videodata->ime_threadmgrex))) + return SDL_FALSE; + + if (FAILED(videodata->ime_threadmgrex->lpVtbl->ActivateEx(videodata->ime_threadmgrex, &clientid, TF_TMAE_UIELEMENTENABLEDONLY))) + return SDL_FALSE; + + videodata->ime_uielemsink = SDL_malloc(sizeof(TSFSink)); + videodata->ime_ippasink = SDL_malloc(sizeof(TSFSink)); + + videodata->ime_uielemsink->lpVtbl = vtUIElementSink; + videodata->ime_uielemsink->refcount = 1; + videodata->ime_uielemsink->data = videodata; + + videodata->ime_ippasink->lpVtbl = vtIPPASink; + videodata->ime_ippasink->refcount = 1; + videodata->ime_ippasink->data = videodata; + + if (SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, (LPVOID *)&source))) { + if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfUIElementSink, (IUnknown *)videodata->ime_uielemsink, &videodata->ime_uielemsinkcookie))) { + if (SUCCEEDED(source->lpVtbl->AdviseSink(source, &IID_ITfInputProcessorProfileActivationSink, (IUnknown *)videodata->ime_ippasink, &videodata->ime_alpnsinkcookie))) { + result = SDL_TRUE; + } + } + source->lpVtbl->Release(source); + } + return result; +} + +#define SAFE_RELEASE(p) \ +{ \ + if (p) { \ + (p)->lpVtbl->Release((p)); \ + (p) = 0; \ + } \ +} + +static void +UILess_ReleaseSinks(SDL_VideoData *videodata) +{ + ITfSource *source = 0; + if (videodata->ime_threadmgrex && SUCCEEDED(videodata->ime_threadmgrex->lpVtbl->QueryInterface(videodata->ime_threadmgrex, &IID_ITfSource, &source))) { + source->lpVtbl->UnadviseSink(source, videodata->ime_uielemsinkcookie); + source->lpVtbl->UnadviseSink(source, videodata->ime_alpnsinkcookie); + SAFE_RELEASE(source); + videodata->ime_threadmgrex->lpVtbl->Deactivate(videodata->ime_threadmgrex); + SAFE_RELEASE(videodata->ime_threadmgrex); + TSFSink_Release(videodata->ime_uielemsink); + videodata->ime_uielemsink = 0; + TSFSink_Release(videodata->ime_ippasink); + videodata->ime_ippasink = 0; + } } /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/win32/SDL_win32keyboard.h b/src/video/win32/SDL_win32keyboard.h index c67410a6..670730bb 100644 --- a/src/video/win32/SDL_win32keyboard.h +++ b/src/video/win32/SDL_win32keyboard.h @@ -31,6 +31,12 @@ extern void WIN_InitKeyboard(_THIS); extern void WIN_UpdateKeymap(void); extern void WIN_QuitKeyboard(_THIS); +extern void WIN_StartTextInput(_THIS); +extern void WIN_StopTextInput(_THIS); +extern void WIN_SetTextInputRect(_THIS, SDL_Rect *rect); + +extern SDL_bool IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, struct SDL_VideoData *videodata); + #endif /* _SDL_win32keyboard_h */ /* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/win32/SDL_win32video.c b/src/video/win32/SDL_win32video.c index af21a2ff..b82a1cd7 100644 --- a/src/video/win32/SDL_win32video.c +++ b/src/video/win32/SDL_win32video.c @@ -191,6 +191,9 @@ WIN_CreateDevice(int devindex) device->GL_SwapWindow = WIN_GL_SwapWindow; device->GL_DeleteContext = WIN_GL_DeleteContext; #endif + device->StartTextInput = WIN_StartTextInput; + device->StopTextInput = WIN_StopTextInput; + device->SetTextInputRect = WIN_SetTextInputRect; device->SetClipboardText = WIN_SetClipboardText; device->GetClipboardText = WIN_GetClipboardText; diff --git a/src/video/win32/SDL_win32video.h b/src/video/win32/SDL_win32video.h index 2f26320a..2f1f7f75 100644 --- a/src/video/win32/SDL_win32video.h +++ b/src/video/win32/SDL_win32video.h @@ -42,6 +42,8 @@ #include <windows.h> +#include <msctf.h> + #if SDL_VIDEO_RENDER_D3D //#include <d3d9.h> #define D3D_DEBUG_INFO @@ -62,6 +64,7 @@ #include "SDL_win32mouse.h" #include "SDL_win32opengl.h" #include "SDL_win32window.h" +#include "SDL_events.h" #ifdef UNICODE #define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "UCS-2", (char *)S, (SDL_wcslen(S)+1)*sizeof(WCHAR)) @@ -77,6 +80,37 @@ enum { RENDER_NONE, RENDER_D3D, RENDER_DDRAW, RENDER_GDI, RENDER_GAPI, RENDER_RA typedef BOOL (*PFNSHFullScreen)(HWND, DWORD); typedef void (*PFCoordTransform)(SDL_Window*, POINT*); +typedef struct +{ + void **lpVtbl; + int refcount; + void *data; +} TSFSink; + +// Definition from Win98DDK version of IMM.H +typedef struct tagINPUTCONTEXT2 { + HWND hWnd; + BOOL fOpen; + POINT ptStatusWndPos; + POINT ptSoftKbdPos; + DWORD fdwConversion; + DWORD fdwSentence; + union { + LOGFONTA A; + LOGFONTW W; + } lfFont; + COMPOSITIONFORM cfCompForm; + CANDIDATEFORM cfCandForm[4]; + HIMCC hCompStr; + HIMCC hCandInfo; + HIMCC hGuideLine; + HIMCC hPrivate; + DWORD dwNumMsgBuf; + HIMCC hMsgBuf; + DWORD fdwInit; + DWORD dwReserve[3]; +} INPUTCONTEXT2, *PINPUTCONTEXT2, NEAR *NPINPUTCONTEXT2, FAR *LPINPUTCONTEXT2; + /* Private display data */ typedef struct SDL_VideoData @@ -97,9 +131,39 @@ typedef struct SDL_VideoData PFCoordTransform CoordTransform; #endif + const SDL_scancode *key_layout; DWORD clipboard_count; - const SDL_scancode *key_layout; + SDL_bool ime_com_initialized; + struct ITfThreadMgr *ime_threadmgr; + SDL_bool ime_initialized; + SDL_bool ime_enabled; + SDL_bool ime_available; + HWND ime_hwnd_main; + HWND ime_hwnd_current; + HIMC ime_himc; + + WCHAR ime_composition[SDL_TEXTEDITINGEVENT_TEXT_SIZE]; + WCHAR ime_readingstring[16]; + int ime_cursor; + + HKL ime_hkl; + HMODULE ime_himm32; + UINT (WINAPI *GetReadingString)(HIMC himc, UINT uReadingBufLen, LPWSTR lpwReadingBuf, PINT pnErrorIndex, BOOL *pfIsVertical, PUINT puMaxReadingLen); + BOOL (WINAPI *ShowReadingWindow)(HIMC himc, BOOL bShow); + LPINPUTCONTEXT2 (WINAPI *ImmLockIMC)(HIMC himc); + BOOL (WINAPI *ImmUnlockIMC)(HIMC himc); + LPVOID (WINAPI *ImmLockIMCC)(HIMCC himcc); + BOOL (WINAPI *ImmUnlockIMCC)(HIMCC himcc); + + SDL_bool ime_uiless; + struct ITfThreadMgrEx *ime_threadmgrex; + DWORD ime_uielemsinkcookie; + DWORD ime_alpnsinkcookie; + DWORD ime_openmodesinkcookie; + DWORD ime_convmodesinkcookie; + TSFSink *ime_uielemsink; + TSFSink *ime_ippasink; } SDL_VideoData; #endif /* _SDL_win32video_h */ diff --git a/src/video/win32/SDL_win32window.c b/src/video/win32/SDL_win32window.c index eb579152..9a9a52df 100644 --- a/src/video/win32/SDL_win32window.c +++ b/src/video/win32/SDL_win32window.c @@ -77,10 +77,12 @@ SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created) /* Set up the window proc function */ data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC); - if (data->wndproc == DefWindowProc) { + if (data->wndproc == WIN_WindowProc) { data->wndproc = NULL; } - SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc); + else { + SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc); + } /* Fill in the SDL window with the window data */ { |