Initial IMM implementation.
authordewyatt
Mon, 12 Jul 2010 11:33:27 -0400
changeset 4752dc7bdcf06367
parent 4751 f3908cd80b10
child 4753 11b0a6a3eb4d
Initial IMM implementation.
IME input should now work fairly well.
src/video/win32/SDL_win32events.c
src/video/win32/SDL_win32keyboard.c
src/video/win32/SDL_win32keyboard.h
src/video/win32/SDL_win32video.h
     1.1 --- a/src/video/win32/SDL_win32events.c	Sat Jul 10 13:15:47 2010 -0400
     1.2 +++ b/src/video/win32/SDL_win32events.c	Mon Jul 12 11:33:27 2010 -0400
     1.3 @@ -131,6 +131,8 @@
     1.4      }
     1.5  
     1.6  #endif
     1.7 +    if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata))
     1.8 +        return 0;
     1.9  
    1.10      switch (msg) {
    1.11  
     2.1 --- a/src/video/win32/SDL_win32keyboard.c	Sat Jul 10 13:15:47 2010 -0400
     2.2 +++ b/src/video/win32/SDL_win32keyboard.c	Mon Jul 12 11:33:27 2010 -0400
     2.3 @@ -26,6 +26,9 @@
     2.4  #include "../../events/SDL_keyboard_c.h"
     2.5  #include "../../events/scancodes_win32.h"
     2.6  
     2.7 +#include <msctf.h>
     2.8 +#include <imm.h>
     2.9 +
    2.10  #ifndef MAPVK_VK_TO_VSC
    2.11  #define MAPVK_VK_TO_VSC     0
    2.12  #endif
    2.13 @@ -46,6 +49,11 @@
    2.14      82, 79, 80, 81, 75, 76, 77, 71, 72, 73
    2.15  };
    2.16  
    2.17 +void IME_Disable(SDL_VideoData *videodata, HWND hwnd);
    2.18 +void IME_Enable(SDL_VideoData *videodata, HWND hwnd);
    2.19 +void IME_Init(SDL_VideoData *videodata, HWND hwnd);
    2.20 +void IME_Quit(SDL_VideoData *videodata);
    2.21 +
    2.22  void
    2.23  WIN_InitKeyboard(_THIS)
    2.24  {
    2.25 @@ -81,6 +89,15 @@
    2.26  
    2.27      data->key_layout = win32_scancode_table;
    2.28  
    2.29 +    data->ime_com_initialized = SDL_FALSE;
    2.30 +    data->ime_thread_mgr = 0;
    2.31 +    data->ime_initialized = SDL_FALSE;
    2.32 +    data->ime_enabled = SDL_FALSE;
    2.33 +    data->ime_available = SDL_FALSE;
    2.34 +    data->ime_hwnd_main = 0;
    2.35 +    data->ime_hwnd_current = 0;
    2.36 +    data->ime_himc = 0;
    2.37 +
    2.38      WIN_UpdateKeymap();
    2.39  
    2.40      SDL_SetScancodeName(SDL_SCANCODE_APPLICATION, "Menu");
    2.41 @@ -120,19 +137,25 @@
    2.42  void
    2.43  WIN_QuitKeyboard(_THIS)
    2.44  {
    2.45 +    IME_Quit((SDL_VideoData *)_this->driverdata);
    2.46  }
    2.47  
    2.48  void
    2.49  WIN_StartTextInput(_THIS, SDL_Window *window)
    2.50  {
    2.51 -	HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
    2.52 -
    2.53 +    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
    2.54 +    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
    2.55 +    IME_Init(videodata, hwnd);
    2.56 +    IME_Enable(videodata, hwnd);
    2.57  }
    2.58  
    2.59  void
    2.60  WIN_StopTextInput(_THIS, SDL_Window *window)
    2.61  {
    2.62 -
    2.63 +    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
    2.64 +    SDL_VideoData *videodata = (SDL_VideoData *)_this->driverdata;
    2.65 +    IME_Init(videodata, hwnd);
    2.66 +    IME_Disable(videodata, hwnd);
    2.67  }
    2.68  
    2.69  void
    2.70 @@ -141,4 +164,161 @@
    2.71  
    2.72  }
    2.73  
    2.74 +void
    2.75 +IME_Disable(SDL_VideoData *videodata, HWND hwnd)
    2.76 +{
    2.77 +    if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
    2.78 +        return;
    2.79 +
    2.80 +    if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
    2.81 +        ImmAssociateContext(videodata->ime_hwnd_current, NULL);
    2.82 +
    2.83 +    videodata->ime_enabled = SDL_FALSE;
    2.84 +}
    2.85 +
    2.86 +void
    2.87 +IME_Enable(SDL_VideoData *videodata, HWND hwnd)
    2.88 +{
    2.89 +    if (!videodata->ime_initialized || !videodata->ime_hwnd_current)
    2.90 +        return;
    2.91 +
    2.92 +    if (!videodata->ime_available) {
    2.93 +        IME_Disable(videodata, hwnd);
    2.94 +        return;
    2.95 +    }
    2.96 +    if (videodata->ime_hwnd_current == videodata->ime_hwnd_main)
    2.97 +        ImmAssociateContext(videodata->ime_hwnd_current, videodata->ime_himc);
    2.98 +
    2.99 +    videodata->ime_enabled = SDL_TRUE;
   2.100 +}
   2.101 +
   2.102 +void
   2.103 +IME_Init(SDL_VideoData *videodata, HWND hwnd)
   2.104 +{
   2.105 +    if (videodata->ime_initialized)
   2.106 +        return;
   2.107 +
   2.108 +    videodata->ime_hwnd_main = hwnd;
   2.109 +    if (SUCCEEDED(CoInitializeEx(NULL, COINIT_APARTMENTTHREADED))) {
   2.110 +        videodata->ime_com_initialized = SDL_TRUE;
   2.111 +        CoCreateInstance(&CLSID_TF_ThreadMgr, NULL, CLSCTX_INPROC_SERVER, &IID_ITfThreadMgr, &videodata->ime_thread_mgr);
   2.112 +    }
   2.113 +    videodata->ime_initialized = SDL_TRUE;
   2.114 +    videodata->ime_hwnd_current = videodata->ime_hwnd_main;
   2.115 +    if (videodata->ime_thread_mgr) {
   2.116 +        struct ITfDocumentMgr *document_mgr = 0;
   2.117 +        if (SUCCEEDED(videodata->ime_thread_mgr->lpVtbl->AssociateFocus(videodata->ime_thread_mgr, hwnd, NULL, &document_mgr))) {
   2.118 +            if (document_mgr)
   2.119 +                document_mgr->lpVtbl->Release(document_mgr);
   2.120 +        }
   2.121 +    }
   2.122 +    videodata->ime_himc = ImmGetContext(hwnd);
   2.123 +    ImmReleaseContext(hwnd, videodata->ime_himc);
   2.124 +    if (!videodata->ime_himc) {
   2.125 +        videodata->ime_available = SDL_FALSE;
   2.126 +        IME_Disable(videodata, hwnd);
   2.127 +        return;
   2.128 +    }
   2.129 +    videodata->ime_available = SDL_TRUE;
   2.130 +    IME_Disable(videodata, hwnd);
   2.131 +}
   2.132 +
   2.133 +void
   2.134 +IME_Quit(SDL_VideoData *videodata)
   2.135 +{
   2.136 +    if (!videodata->ime_initialized)
   2.137 +        return;
   2.138 +
   2.139 +    if (videodata->ime_hwnd_main)
   2.140 +        ImmAssociateContext(videodata->ime_hwnd_main, videodata->ime_himc);
   2.141 +
   2.142 +    videodata->ime_hwnd_main = 0;
   2.143 +    videodata->ime_himc = 0;
   2.144 +    if (videodata->ime_thread_mgr)
   2.145 +    {
   2.146 +        videodata->ime_thread_mgr->lpVtbl->Release(videodata->ime_thread_mgr);
   2.147 +        videodata->ime_thread_mgr = 0;
   2.148 +    }
   2.149 +    if (videodata->ime_com_initialized)
   2.150 +    {
   2.151 +        CoUninitialize();
   2.152 +        videodata->ime_com_initialized = SDL_FALSE;
   2.153 +    }
   2.154 +    videodata->ime_initialized = SDL_FALSE;
   2.155 +}
   2.156 +
   2.157 +SDL_bool
   2.158 +IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, SDL_VideoData *videodata)
   2.159 +{
   2.160 +    SDL_bool trap = SDL_FALSE;
   2.161 +    HIMC himc = 0;
   2.162 +    WCHAR Buffer[SDL_TEXTINPUTEVENT_TEXT_SIZE / 2];
   2.163 +    if (!videodata->ime_initialized || !videodata->ime_available || !videodata->ime_enabled)
   2.164 +        return SDL_FALSE;
   2.165 +
   2.166 +    switch (msg)
   2.167 +    {
   2.168 +    case WM_INPUTLANGCHANGE:
   2.169 +        break;
   2.170 +    case WM_IME_SETCONTEXT:
   2.171 +        *lParam = 0;
   2.172 +        break;
   2.173 +    case WM_IME_STARTCOMPOSITION:
   2.174 +        trap = SDL_TRUE;
   2.175 +        break;
   2.176 +    case WM_IME_COMPOSITION:
   2.177 +        trap = SDL_TRUE;
   2.178 +        himc = ImmGetContext(hwnd);
   2.179 +        if (*lParam & GCS_RESULTSTR)
   2.180 +        {
   2.181 +            LONG Length = 0;
   2.182 +            char *s = 0;
   2.183 +            Length = ImmGetCompositionStringW(himc, GCS_RESULTSTR, Buffer, sizeof(Buffer) * sizeof(Buffer[0]));
   2.184 +            Buffer[Length / sizeof(Buffer[0])] = 0;
   2.185 +            s = WIN_StringToUTF8(Buffer);
   2.186 +            SDL_SendKeyboardText(s);
   2.187 +            SDL_free(s);
   2.188 +        }
   2.189 +        if (*lParam & GCS_COMPSTR)
   2.190 +        {
   2.191 +            LONG Length = 0;
   2.192 +            DWORD Cursor = 0;
   2.193 +            char *s = 0;
   2.194 +            Length = ImmGetCompositionStringW(himc, GCS_COMPSTR, Buffer, sizeof(Buffer) * sizeof(Buffer[0]));
   2.195 +            Buffer[Length / sizeof(Buffer[0])] = 0;
   2.196 +            s = WIN_StringToUTF8(Buffer);
   2.197 +            Cursor = LOWORD(ImmGetCompositionStringW(himc, GCS_CURSORPOS, 0, 0));
   2.198 +            SDL_SendEditingText(s, Cursor, 0);
   2.199 +            SDL_free(s);
   2.200 +        }
   2.201 +        ImmReleaseContext(hwnd, himc);
   2.202 +        break;
   2.203 +    case WM_IME_ENDCOMPOSITION:
   2.204 +        SDL_SendKeyboardText("");
   2.205 +        break;
   2.206 +    case WM_IME_NOTIFY:
   2.207 +        switch (wParam)
   2.208 +        {
   2.209 +        case IMN_SETCONVERSIONMODE:
   2.210 +            break;
   2.211 +        case IMN_SETOPENSTATUS:
   2.212 +            break;
   2.213 +        case IMN_OPENCANDIDATE:
   2.214 +        case IMN_CHANGECANDIDATE:
   2.215 +            trap = SDL_TRUE;
   2.216 +            break;
   2.217 +        case IMN_CLOSECANDIDATE:
   2.218 +            trap = SDL_TRUE;
   2.219 +            break;
   2.220 +        case IMN_PRIVATE:
   2.221 +            break;
   2.222 +        default:
   2.223 +            trap = SDL_TRUE;
   2.224 +            break;
   2.225 +        }
   2.226 +        break;
   2.227 +    }
   2.228 +    return trap;
   2.229 +}
   2.230 +
   2.231  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/video/win32/SDL_win32keyboard.h	Sat Jul 10 13:15:47 2010 -0400
     3.2 +++ b/src/video/win32/SDL_win32keyboard.h	Mon Jul 12 11:33:27 2010 -0400
     3.3 @@ -35,6 +35,8 @@
     3.4  extern void WIN_StopTextInput(_THIS, SDL_Window *window);
     3.5  extern void WIN_SetTextInputRect(_THIS, SDL_Rect *rect);
     3.6  
     3.7 +extern SDL_bool IME_HandleMessage(HWND hwnd, UINT msg, WPARAM wParam, LPARAM *lParam, struct SDL_VideoData *videodata);
     3.8 +
     3.9  #endif /* _SDL_win32keyboard_h */
    3.10  
    3.11  /* vi: set ts=4 sw=4 expandtab: */
     4.1 --- a/src/video/win32/SDL_win32video.h	Sat Jul 10 13:15:47 2010 -0400
     4.2 +++ b/src/video/win32/SDL_win32video.h	Mon Jul 12 11:33:27 2010 -0400
     4.3 @@ -75,6 +75,16 @@
     4.4  #endif
     4.5  
     4.6      const SDL_scancode *key_layout;
     4.7 +    
     4.8 +    SDL_bool ime_com_initialized;
     4.9 +    struct ITfThreadMgr *ime_thread_mgr;
    4.10 +    SDL_bool ime_initialized;
    4.11 +    SDL_bool ime_enabled;
    4.12 +    SDL_bool ime_available;
    4.13 +    HWND ime_hwnd_main;
    4.14 +    HWND ime_hwnd_current;
    4.15 +    HIMC ime_himc;
    4.16 +
    4.17  } SDL_VideoData;
    4.18  
    4.19  #endif /* _SDL_win32video_h */