src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 16 Aug 2013 15:35:10 -0700
changeset 7645 cc775832d501
parent 7631 9fcda7425ccc
child 7646 42c3b0c24c6c
permissions -rw-r--r--
Fixed bug 1876 - SDL_TEXTINPUT only returns '?' (0x3F) in event.text.text with Khmer language input

Andreas

The issue comes down to this line on MSDN:
"TranslateMessage produces WM_CHAR messages only for keys that are mapped to ASCII characters by the keyboard driver."

"WM_KEYDOWN and WM_KEYUP combinations produce a WM_CHAR or WM_DEADCHAR message. WM_SYSKEYDOWN and WM_SYSKEYUP combinations produce a WM_SYSCHAR or WM_SYSDEADCHAR message."
Except for WM_CHAR, none of these messages are used in SDL. Hence TranslateMessage should be dropped entirely and proper handling be included in the WM_KEYDOWN event.
Currently TranslateMessage is called for every message even if it must not be called in certain cases (like "An application should not call TranslateMessage if the TranslateAccelerator function returns a nonzero value.").

WM_CHAR message handling should remain for external processes posting these messages - additionally, WM_UNICHAR should be added.

I made a patch for src/video/windows/SDL_windowsevents.c that seems to work fine. It doesn't solve the "missing" composition for Khmer, but at least input for languages that cannot be mapped to ASCII characters (and for which IME is not used) will now work on Windows.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_WINDOWS
    24 
    25 #include "SDL_windowsvideo.h"
    26 #include "SDL_windowsshape.h"
    27 #include "SDL_syswm.h"
    28 #include "SDL_vkeys.h"
    29 #include "../../events/SDL_events_c.h"
    30 #include "../../events/SDL_touch_c.h"
    31 #include "../../events/scancodes_windows.h"
    32 
    33 /* Dropfile support */
    34 #include <shellapi.h>
    35 
    36 /* For GET_X_LPARAM, GET_Y_LPARAM. */
    37 #include <windowsx.h>
    38 
    39 /*#define WMMSG_DEBUG*/
    40 #ifdef WMMSG_DEBUG
    41 #include <stdio.h>
    42 #include "wmmsg.h"
    43 #endif
    44 
    45 /* Masks for processing the windows KEYDOWN and KEYUP messages */
    46 #define REPEATED_KEYMASK    (1<<30)
    47 #define EXTENDED_KEYMASK    (1<<24)
    48 
    49 #define VK_ENTER    10          /* Keypad Enter ... no VKEY defined? */
    50 #ifndef VK_OEM_NEC_EQUAL
    51 #define VK_OEM_NEC_EQUAL 0x92
    52 #endif
    53 
    54 /* Make sure XBUTTON stuff is defined that isn't in older Platform SDKs... */
    55 #ifndef WM_XBUTTONDOWN
    56 #define WM_XBUTTONDOWN 0x020B
    57 #endif
    58 #ifndef WM_XBUTTONUP
    59 #define WM_XBUTTONUP 0x020C
    60 #endif
    61 #ifndef GET_XBUTTON_WPARAM
    62 #define GET_XBUTTON_WPARAM(w) (HIWORD(w))
    63 #endif
    64 #ifndef WM_INPUT
    65 #define WM_INPUT 0x00ff
    66 #endif
    67 #ifndef WM_TOUCH
    68 #define WM_TOUCH 0x0240
    69 #endif
    70 
    71 static SDL_Scancode
    72 WindowsScanCodeToSDLScanCode( LPARAM lParam, WPARAM wParam )
    73 {
    74     SDL_Scancode code;
    75     char bIsExtended;
    76     int nScanCode = ( lParam >> 16 ) & 0xFF;
    77 
    78     /* 0x45 here to work around both pause and numlock sharing the same scancode, so use the VK key to tell them apart */
    79     if ( nScanCode == 0 || nScanCode == 0x45 )
    80     {
    81         switch( wParam )
    82         {
    83         case VK_CLEAR: return SDL_SCANCODE_CLEAR;
    84         case VK_MODECHANGE: return SDL_SCANCODE_MODE;
    85         case VK_SELECT: return SDL_SCANCODE_SELECT;
    86         case VK_EXECUTE: return SDL_SCANCODE_EXECUTE;
    87         case VK_HELP: return SDL_SCANCODE_HELP;
    88         case VK_PAUSE: return SDL_SCANCODE_PAUSE;
    89         case VK_NUMLOCK: return SDL_SCANCODE_NUMLOCKCLEAR;
    90 
    91         case VK_F13: return SDL_SCANCODE_F13;
    92         case VK_F14: return SDL_SCANCODE_F14;
    93         case VK_F15: return SDL_SCANCODE_F15;
    94         case VK_F16: return SDL_SCANCODE_F16;
    95         case VK_F17: return SDL_SCANCODE_F17;
    96         case VK_F18: return SDL_SCANCODE_F18;
    97         case VK_F19: return SDL_SCANCODE_F19;
    98         case VK_F20: return SDL_SCANCODE_F20;
    99         case VK_F21: return SDL_SCANCODE_F21;
   100         case VK_F22: return SDL_SCANCODE_F22;
   101         case VK_F23: return SDL_SCANCODE_F23;
   102         case VK_F24: return SDL_SCANCODE_F24;
   103 
   104         case VK_OEM_NEC_EQUAL: return SDL_SCANCODE_KP_EQUALS;
   105         case VK_BROWSER_BACK: return SDL_SCANCODE_AC_BACK;
   106         case VK_BROWSER_FORWARD: return SDL_SCANCODE_AC_FORWARD;
   107         case VK_BROWSER_REFRESH: return SDL_SCANCODE_AC_REFRESH;
   108         case VK_BROWSER_STOP: return SDL_SCANCODE_AC_STOP;
   109         case VK_BROWSER_SEARCH: return SDL_SCANCODE_AC_SEARCH;
   110         case VK_BROWSER_FAVORITES: return SDL_SCANCODE_AC_BOOKMARKS;
   111         case VK_BROWSER_HOME: return SDL_SCANCODE_AC_HOME;
   112         case VK_VOLUME_MUTE: return SDL_SCANCODE_AUDIOMUTE;
   113         case VK_VOLUME_DOWN: return SDL_SCANCODE_VOLUMEDOWN;
   114         case VK_VOLUME_UP: return SDL_SCANCODE_VOLUMEUP;
   115 
   116         case VK_MEDIA_NEXT_TRACK: return SDL_SCANCODE_AUDIONEXT;
   117         case VK_MEDIA_PREV_TRACK: return SDL_SCANCODE_AUDIOPREV;
   118         case VK_MEDIA_STOP: return SDL_SCANCODE_AUDIOSTOP;
   119         case VK_MEDIA_PLAY_PAUSE: return SDL_SCANCODE_AUDIOPLAY;
   120         case VK_LAUNCH_MAIL: return SDL_SCANCODE_MAIL;
   121         case VK_LAUNCH_MEDIA_SELECT: return SDL_SCANCODE_MEDIASELECT;
   122 
   123         case VK_OEM_102: return SDL_SCANCODE_NONUSBACKSLASH;
   124 
   125         case VK_ATTN: return SDL_SCANCODE_SYSREQ;
   126         case VK_CRSEL: return SDL_SCANCODE_CRSEL;
   127         case VK_EXSEL: return SDL_SCANCODE_EXSEL;
   128         case VK_OEM_CLEAR: return SDL_SCANCODE_CLEAR;
   129 
   130         case VK_LAUNCH_APP1: return SDL_SCANCODE_APP1;
   131         case VK_LAUNCH_APP2: return SDL_SCANCODE_APP2;
   132 
   133         default: return SDL_SCANCODE_UNKNOWN;
   134         }
   135     }
   136 
   137     if ( nScanCode > 127 )
   138         return SDL_SCANCODE_UNKNOWN;
   139 
   140     code = windows_scancode_table[nScanCode];
   141 
   142     bIsExtended = ( lParam & ( 1 << 24 ) ) != 0;
   143     if ( !bIsExtended )
   144     {
   145         switch ( code )
   146         {
   147         case SDL_SCANCODE_HOME:
   148             return SDL_SCANCODE_KP_7;
   149         case SDL_SCANCODE_UP:
   150             return SDL_SCANCODE_KP_8;
   151         case SDL_SCANCODE_PAGEUP:
   152             return SDL_SCANCODE_KP_9;
   153         case SDL_SCANCODE_LEFT:
   154             return SDL_SCANCODE_KP_4;
   155         case SDL_SCANCODE_RIGHT:
   156             return SDL_SCANCODE_KP_6;
   157         case SDL_SCANCODE_END:
   158             return SDL_SCANCODE_KP_1;
   159         case SDL_SCANCODE_DOWN:
   160             return SDL_SCANCODE_KP_2;
   161         case SDL_SCANCODE_PAGEDOWN:
   162             return SDL_SCANCODE_KP_3;
   163         case SDL_SCANCODE_INSERT:
   164             return SDL_SCANCODE_KP_0;
   165         case SDL_SCANCODE_DELETE:
   166             return SDL_SCANCODE_KP_PERIOD;
   167         case SDL_SCANCODE_PRINTSCREEN:
   168             return SDL_SCANCODE_KP_MULTIPLY;
   169         default:
   170             break;
   171         }
   172     }
   173     else
   174     {
   175         switch ( code )
   176         {
   177         case SDL_SCANCODE_RETURN:
   178             return SDL_SCANCODE_KP_ENTER;
   179         case SDL_SCANCODE_LALT:
   180             return SDL_SCANCODE_RALT;
   181         case SDL_SCANCODE_LCTRL:
   182             return SDL_SCANCODE_RCTRL;
   183         case SDL_SCANCODE_SLASH:
   184             return SDL_SCANCODE_KP_DIVIDE;
   185         case SDL_SCANCODE_CAPSLOCK:
   186             return SDL_SCANCODE_KP_PLUS;
   187         default:
   188             break;
   189         }
   190     }
   191 
   192     return code;
   193 }
   194 
   195 
   196 void
   197 WIN_CheckWParamMouseButton( SDL_bool bwParamMousePressed, SDL_bool bSDLMousePressed, SDL_WindowData *data, Uint8 button )
   198 {
   199     if ( bwParamMousePressed && !bSDLMousePressed )
   200     {
   201         SDL_SendMouseButton(data->window, 0, SDL_PRESSED, button);
   202     }
   203     else if ( !bwParamMousePressed && bSDLMousePressed )
   204     {
   205         SDL_SendMouseButton(data->window, 0, SDL_RELEASED, button);
   206     }
   207 }
   208 
   209 /*
   210 * Some windows systems fail to send a WM_LBUTTONDOWN sometimes, but each mouse move contains the current button state also
   211 *  so this funciton reconciles our view of the world with the current buttons reported by windows
   212 */
   213 void
   214 WIN_CheckWParamMouseButtons( WPARAM wParam, SDL_WindowData *data )
   215 {
   216     if ( wParam != data->mouse_button_flags )
   217     {
   218         Uint32 mouseFlags = SDL_GetMouseState( NULL, NULL );
   219         WIN_CheckWParamMouseButton(  (wParam & MK_LBUTTON), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT );
   220         WIN_CheckWParamMouseButton(  (wParam & MK_MBUTTON), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE );
   221         WIN_CheckWParamMouseButton(  (wParam & MK_RBUTTON), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT );
   222         WIN_CheckWParamMouseButton(  (wParam & MK_XBUTTON1), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1 );
   223         WIN_CheckWParamMouseButton(  (wParam & MK_XBUTTON2), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2 );
   224         data->mouse_button_flags = wParam;
   225     }
   226 }
   227 
   228 
   229 void
   230 WIN_CheckRawMouseButtons( ULONG rawButtons, SDL_WindowData *data )
   231 {
   232     if ( rawButtons != data->mouse_button_flags )
   233     {
   234         Uint32 mouseFlags = SDL_GetMouseState( NULL, NULL );
   235         if ( (rawButtons & RI_MOUSE_BUTTON_1_DOWN) )
   236             WIN_CheckWParamMouseButton(  (rawButtons & RI_MOUSE_BUTTON_1_DOWN), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT );
   237         if ( (rawButtons & RI_MOUSE_BUTTON_1_UP) )
   238             WIN_CheckWParamMouseButton(  !(rawButtons & RI_MOUSE_BUTTON_1_UP), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT );
   239         if ( (rawButtons & RI_MOUSE_BUTTON_2_DOWN) )
   240             WIN_CheckWParamMouseButton(  (rawButtons & RI_MOUSE_BUTTON_2_DOWN), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT );
   241         if ( (rawButtons & RI_MOUSE_BUTTON_2_UP) )
   242             WIN_CheckWParamMouseButton(  !(rawButtons & RI_MOUSE_BUTTON_2_UP), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT );
   243         if ( (rawButtons & RI_MOUSE_BUTTON_3_DOWN) )
   244             WIN_CheckWParamMouseButton(  (rawButtons & RI_MOUSE_BUTTON_3_DOWN), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE );
   245         if ( (rawButtons & RI_MOUSE_BUTTON_3_UP) )
   246             WIN_CheckWParamMouseButton(  !(rawButtons & RI_MOUSE_BUTTON_3_UP), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE );
   247         if ( (rawButtons & RI_MOUSE_BUTTON_4_DOWN) )
   248             WIN_CheckWParamMouseButton(  (rawButtons & RI_MOUSE_BUTTON_4_DOWN), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1 );
   249         if ( (rawButtons & RI_MOUSE_BUTTON_4_UP) )
   250             WIN_CheckWParamMouseButton(  !(rawButtons & RI_MOUSE_BUTTON_4_UP), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1 );
   251         if ( (rawButtons & RI_MOUSE_BUTTON_5_DOWN) )
   252             WIN_CheckWParamMouseButton(  (rawButtons & RI_MOUSE_BUTTON_5_DOWN), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2 );
   253         if ( (rawButtons & RI_MOUSE_BUTTON_5_UP) )
   254             WIN_CheckWParamMouseButton(  !(rawButtons & RI_MOUSE_BUTTON_5_UP), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2 );
   255         data->mouse_button_flags = rawButtons;
   256     }
   257 }
   258 
   259 static SDL_FORCE_INLINE BOOL
   260 WIN_ConvertUTF32toUTF8(UINT32 codepoint, char * text)
   261 {
   262     if (codepoint <= 0x7F) {
   263         text[0] = (char) codepoint;
   264         text[1] = '\0';
   265     } else if (codepoint <= 0x7FF) {
   266         text[0] = 0xC0 | (char) ((codepoint >> 6) & 0x1F);
   267         text[1] = 0x80 | (char) (codepoint & 0x3F);
   268         text[2] = '\0';
   269     } else if (codepoint <= 0xFFFF) {
   270         text[0] = 0xE0 | (char) ((codepoint >> 12) & 0x0F);
   271         text[1] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
   272         text[2] = 0x80 | (char) (codepoint & 0x3F);
   273         text[3] = '\0';
   274     } else if (codepoint <= 0x10FFFF) {
   275         text[0] = 0xF0 | (char) ((codepoint >> 18) & 0x0F);
   276         text[1] = 0x80 | (char) ((codepoint >> 12) & 0x3F);
   277         text[2] = 0x80 | (char) ((codepoint >> 6) & 0x3F);
   278         text[3] = 0x80 | (char) (codepoint & 0x3F);
   279         text[4] = '\0';
   280     } else {
   281         return SDL_FALSE;
   282     }
   283     return SDL_TRUE;
   284 }
   285 
   286 LRESULT CALLBACK
   287 WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
   288 {
   289     SDL_WindowData *data;
   290     LRESULT returnCode = -1;
   291 
   292     /* Send a SDL_SYSWMEVENT if the application wants them */
   293     if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
   294         SDL_SysWMmsg wmmsg;
   295 
   296         SDL_VERSION(&wmmsg.version);
   297         wmmsg.subsystem = SDL_SYSWM_WINDOWS;
   298         wmmsg.msg.win.hwnd = hwnd;
   299         wmmsg.msg.win.msg = msg;
   300         wmmsg.msg.win.wParam = wParam;
   301         wmmsg.msg.win.lParam = lParam;
   302         SDL_SendSysWMEvent(&wmmsg);
   303     }
   304 
   305     /* Get the window data for the window */
   306     data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
   307     if (!data) {
   308         return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
   309     }
   310 
   311 #ifdef WMMSG_DEBUG
   312     {
   313         FILE *log = fopen("wmmsg.txt", "a");
   314         fprintf(log, "Received windows message: %p ", hwnd);
   315         if (msg > MAX_WMMSG) {
   316             fprintf(log, "%d", msg);
   317         } else {
   318             fprintf(log, "%s", wmtab[msg]);
   319         }
   320         fprintf(log, " -- 0x%X, 0x%X\n", wParam, lParam);
   321         fclose(log);
   322     }
   323 #endif
   324 
   325     if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata))
   326         return 0;
   327 
   328     switch (msg) {
   329 
   330     case WM_SHOWWINDOW:
   331         {
   332             if (wParam) {
   333                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
   334             } else {
   335                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   336             }
   337         }
   338         break;
   339 
   340     case WM_ACTIVATE:
   341         {
   342             BOOL minimized;
   343 
   344             minimized = HIWORD(wParam);
   345             if (!minimized && (LOWORD(wParam) != WA_INACTIVE)) {
   346                 Uint32 mouseFlags;
   347                 SHORT keyState;
   348 
   349                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
   350                 SDL_SendWindowEvent(data->window,
   351                                     SDL_WINDOWEVENT_RESTORED, 0, 0);
   352                 if (IsZoomed(hwnd)) {
   353                     SDL_SendWindowEvent(data->window,
   354                                         SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
   355                 }
   356                 if (SDL_GetKeyboardFocus() != data->window) {
   357                     SDL_SetKeyboardFocus(data->window);
   358                 }
   359                 /* mouse buttons may have changed state here, we need
   360                 to resync them, but we will get a WM_MOUSEMOVE right away which will fix
   361                 things up if in non raw mode also
   362                 */
   363                 mouseFlags = SDL_GetMouseState( NULL, NULL );
   364 
   365                 keyState = GetAsyncKeyState( VK_LBUTTON );
   366                 WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT );
   367                 keyState = GetAsyncKeyState( VK_RBUTTON );
   368                 WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT );
   369                 keyState = GetAsyncKeyState( VK_MBUTTON );
   370                 WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE );
   371                 keyState = GetAsyncKeyState( VK_XBUTTON1 );
   372                 WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1 );
   373                 keyState = GetAsyncKeyState( VK_XBUTTON2 );
   374                 WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2 );
   375                 data->mouse_button_flags = 0;
   376 
   377                 if(SDL_GetMouse()->relative_mode) {
   378                     LONG cx, cy;
   379                     RECT rect;
   380                     GetWindowRect(hwnd, &rect);
   381 
   382                     cx = (rect.left + rect.right) / 2;
   383                     cy = (rect.top + rect.bottom) / 2;
   384 
   385                     /* Make an absurdly small clip rect */
   386                     rect.left = cx-1;
   387                     rect.right = cx+1;
   388                     rect.top = cy-1;
   389                     rect.bottom = cy+1;
   390 
   391                     ClipCursor(&rect);
   392                 }
   393 
   394                 /*
   395                  * FIXME: Update keyboard state
   396                  */
   397                 WIN_CheckClipboardUpdate(data->videodata);
   398             } else {
   399                 if (SDL_GetKeyboardFocus() == data->window) {
   400                     SDL_SetKeyboardFocus(NULL);
   401                 }
   402                 if (minimized) {
   403                     SDL_SendWindowEvent(data->window,
   404                                         SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   405                 }
   406             }
   407         }
   408         returnCode = 0;
   409         break;
   410 
   411     case WM_MOUSEMOVE:
   412         if( !SDL_GetMouse()->relative_mode )
   413             SDL_SendMouseMotion(data->window, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
   414         /* don't break here, fall through to check the wParam like the button presses */
   415     case WM_LBUTTONUP:
   416     case WM_RBUTTONUP:
   417     case WM_MBUTTONUP:
   418     case WM_XBUTTONUP:
   419     case WM_LBUTTONDOWN:
   420     case WM_RBUTTONDOWN:
   421     case WM_MBUTTONDOWN:
   422     case WM_XBUTTONDOWN:
   423         if( !SDL_GetMouse()->relative_mode )
   424             WIN_CheckWParamMouseButtons( wParam, data );
   425         break;
   426 
   427     case WM_INPUT:
   428     {
   429         HRAWINPUT hRawInput = (HRAWINPUT)lParam;
   430         RAWINPUT inp;
   431         UINT size = sizeof(inp);
   432 
   433         if(!SDL_GetMouse()->relative_mode)
   434             break;
   435 
   436         GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
   437 
   438         /* Mouse data */
   439         if(inp.header.dwType == RIM_TYPEMOUSE)
   440         {
   441             RAWMOUSE* mouse = &inp.data.mouse;
   442 
   443             if((mouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE)
   444             {
   445                 SDL_SendMouseMotion(data->window, 0, 1, (int)mouse->lLastX, (int)mouse->lLastY);
   446             }
   447             else
   448             {
   449                 /* synthesize relative moves from the abs position */
   450                 static SDL_Point initialMousePoint;
   451                 if ( initialMousePoint.x == 0 && initialMousePoint.y == 0 )
   452                 {
   453                     initialMousePoint.x = mouse->lLastX;
   454                     initialMousePoint.y = mouse->lLastY;
   455                 }
   456 
   457                 SDL_SendMouseMotion(data->window, 0, 1, (int)(mouse->lLastX-initialMousePoint.x), (int)(mouse->lLastY-initialMousePoint.y) );
   458 
   459                 initialMousePoint.x = mouse->lLastX;
   460                 initialMousePoint.y = mouse->lLastY;
   461             }
   462             WIN_CheckRawMouseButtons( mouse->usButtonFlags, data );
   463         }
   464         break;
   465     }
   466 
   467     case WM_MOUSEWHEEL:
   468         {
   469             /* FIXME: This may need to accumulate deltas up to WHEEL_DELTA */
   470             short motion = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
   471 
   472             SDL_SendMouseWheel(data->window, 0, 0, motion);
   473             break;
   474         }
   475 
   476 #ifdef WM_MOUSELEAVE
   477     case WM_MOUSELEAVE:
   478         if (SDL_GetMouseFocus() == data->window && !SDL_GetMouse()->relative_mode) {
   479             POINT cursorPos;
   480             GetCursorPos(&cursorPos);
   481             ScreenToClient(hwnd, &cursorPos);
   482             SDL_SendMouseMotion(data->window, 0, 0, cursorPos.x, cursorPos.y);
   483             SDL_SetMouseFocus(NULL);
   484         }
   485         returnCode = 0;
   486         break;
   487 #endif /* WM_MOUSELEAVE */
   488 
   489     case WM_KEYDOWN:
   490         {
   491             BYTE keyboardState[256];
   492             char text[5];
   493             UINT32 utf32 = 0;
   494 
   495             GetKeyboardState(keyboardState);
   496             if (ToUnicode(wParam, (lParam >> 16) & 0xff, keyboardState, (LPWSTR)&utf32, 1, 0) > 0) {
   497                 WORD repitition;
   498                 for (repitition = lParam & 0xffff; repitition > 0; repitition--) {
   499                     WIN_ConvertUTF32toUTF8(utf32, text);
   500                     SDL_SendKeyboardText(text);
   501                 }
   502             }
   503         }
   504         // no break
   505     case WM_SYSKEYDOWN:
   506         {
   507             SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam );
   508             if ( code != SDL_SCANCODE_UNKNOWN ) {
   509                 SDL_SendKeyboardKey(SDL_PRESSED, code );
   510             }
   511         }
   512         returnCode = 0;
   513         break;
   514 
   515     case WM_SYSKEYUP:
   516     case WM_KEYUP:
   517         {
   518             SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam );
   519             if ( code != SDL_SCANCODE_UNKNOWN ) {
   520                 if (code == SDL_SCANCODE_PRINTSCREEN &&
   521                     SDL_GetKeyboardState(NULL)[code] == SDL_RELEASED) {
   522                     SDL_SendKeyboardKey(SDL_PRESSED, code);
   523                 }
   524                 SDL_SendKeyboardKey(SDL_RELEASED, code);
   525             }
   526         }
   527         returnCode = 0;
   528         break;
   529 
   530     case WM_UNICHAR:
   531         {
   532             if (wParam == UNICODE_NOCHAR) {
   533                 returnCode = 1;
   534                 break;
   535             }
   536         }
   537         // no break
   538     case WM_CHAR:
   539         {
   540             char text[5];
   541 
   542             WIN_ConvertUTF32toUTF8(wParam, text);
   543             SDL_SendKeyboardText(text);
   544         }
   545         returnCode = 0;
   546         break;
   547 
   548 #ifdef WM_INPUTLANGCHANGE
   549     case WM_INPUTLANGCHANGE:
   550         {
   551             WIN_UpdateKeymap();
   552         }
   553         returnCode = 1;
   554         break;
   555 #endif /* WM_INPUTLANGCHANGE */
   556 
   557 #ifdef WM_GETMINMAXINFO
   558     case WM_GETMINMAXINFO:
   559         {
   560             MINMAXINFO *info;
   561             RECT size;
   562             int x, y;
   563             int w, h;
   564             int min_w, min_h;
   565             int max_w, max_h;
   566             int style;
   567             BOOL menu;
   568             BOOL constrain_max_size;
   569 
   570             /* If we allow resizing, let the resize happen naturally */
   571             if (SDL_IsShapedWindow(data->window))
   572                 Win32_ResizeWindowShape(data->window);
   573 
   574             /* Get the current position of our window */
   575             GetWindowRect(hwnd, &size);
   576             x = size.left;
   577             y = size.top;
   578 
   579             /* Calculate current size of our window */
   580             SDL_GetWindowSize(data->window, &w, &h);
   581             SDL_GetWindowMinimumSize(data->window, &min_w, &min_h);
   582             SDL_GetWindowMaximumSize(data->window, &max_w, &max_h);
   583 
   584             /* Store in min_w and min_h difference between current size and minimal
   585                size so we don't need to call AdjustWindowRectEx twice */
   586             min_w -= w;
   587             min_h -= h;
   588             if (max_w && max_h) {
   589                 max_w -= w;
   590                 max_h -= h;
   591                 constrain_max_size = TRUE;
   592             } else {
   593                 constrain_max_size = FALSE;
   594             }
   595 
   596             size.top = 0;
   597             size.left = 0;
   598             size.bottom = h;
   599             size.right = w;
   600 
   601             style = GetWindowLong(hwnd, GWL_STYLE);
   602             /* DJM - according to the docs for GetMenu(), the
   603                return value is undefined if hwnd is a child window.
   604                Aparently it's too difficult for MS to check
   605                inside their function, so I have to do it here.
   606              */
   607             menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
   608             AdjustWindowRectEx(&size, style, menu, 0);
   609             w = size.right - size.left;
   610             h = size.bottom - size.top;
   611 
   612             /* Fix our size to the current size */
   613             info = (MINMAXINFO *) lParam;
   614             if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
   615                 info->ptMinTrackSize.x = w + min_w;
   616                 info->ptMinTrackSize.y = h + min_h;
   617                 if (constrain_max_size) {
   618                     info->ptMaxTrackSize.x = w + max_w;
   619                     info->ptMaxTrackSize.y = h + max_h;
   620                 }
   621             } else {
   622                 info->ptMaxSize.x = w;
   623                 info->ptMaxSize.y = h;
   624                 info->ptMaxPosition.x = x;
   625                 info->ptMaxPosition.y = y;
   626                 info->ptMinTrackSize.x = w;
   627                 info->ptMinTrackSize.y = h;
   628                 info->ptMaxTrackSize.x = w;
   629                 info->ptMaxTrackSize.y = h;
   630             }
   631         }
   632         returnCode = 0;
   633         break;
   634 #endif /* WM_GETMINMAXINFO */
   635 
   636     case WM_WINDOWPOSCHANGED:
   637         {
   638             RECT rect;
   639             int x, y;
   640             int w, h;
   641             Uint32 window_flags;
   642 
   643             if (!GetClientRect(hwnd, &rect) ||
   644                 (rect.right == rect.left && rect.bottom == rect.top)) {
   645                 break;
   646             }
   647             ClientToScreen(hwnd, (LPPOINT) & rect);
   648             ClientToScreen(hwnd, (LPPOINT) & rect + 1);
   649 
   650             window_flags = SDL_GetWindowFlags(data->window);
   651             if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
   652                 (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
   653                 ClipCursor(&rect);
   654             }
   655 
   656             x = rect.left;
   657             y = rect.top;
   658             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
   659 
   660             w = rect.right - rect.left;
   661             h = rect.bottom - rect.top;
   662             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
   663                                 h);
   664         }
   665         break;
   666 
   667     case WM_SETCURSOR:
   668         {
   669             Uint16 hittest;
   670 
   671             hittest = LOWORD(lParam);
   672             if (hittest == HTCLIENT) {
   673                 SetCursor(SDL_cursor);
   674                 returnCode = TRUE;
   675             }
   676         }
   677         break;
   678 
   679         /* We were occluded, refresh our display */
   680     case WM_PAINT:
   681         {
   682             RECT rect;
   683             if (GetUpdateRect(hwnd, &rect, FALSE)) {
   684                 ValidateRect(hwnd, NULL);
   685                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
   686                                     0, 0);
   687             }
   688         }
   689         returnCode = 0;
   690         break;
   691 
   692         /* We'll do our own drawing, prevent flicker */
   693     case WM_ERASEBKGND:
   694         {
   695         }
   696         return (1);
   697 
   698 #if defined(SC_SCREENSAVE) || defined(SC_MONITORPOWER)
   699     case WM_SYSCOMMAND:
   700         {
   701             /* Don't start the screensaver or blank the monitor in fullscreen apps */
   702             if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
   703                 (wParam & 0xFFF0) == SC_MONITORPOWER) {
   704                 if (SDL_GetVideoDevice()->suspend_screensaver) {
   705                     return (0);
   706                 }
   707             }
   708         }
   709         break;
   710 #endif /* System has screensaver support */
   711 
   712     case WM_CLOSE:
   713         {
   714             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
   715         }
   716         returnCode = 0;
   717         break;
   718 
   719     case WM_TOUCH:
   720         {
   721             UINT i, num_inputs = LOWORD(wParam);
   722             PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
   723             if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
   724                 RECT rect;
   725                 float x, y;
   726 
   727                 if (!GetClientRect(hwnd, &rect) ||
   728                     (rect.right == rect.left && rect.bottom == rect.top)) {
   729                     if (inputs) {
   730                         SDL_stack_free(inputs);
   731                     }
   732                     break;
   733                 }
   734                 ClientToScreen(hwnd, (LPPOINT) & rect);
   735                 ClientToScreen(hwnd, (LPPOINT) & rect + 1);
   736                 rect.top *= 100;
   737                 rect.left *= 100;
   738                 rect.bottom *= 100;
   739                 rect.right *= 100;
   740 
   741                 for (i = 0; i < num_inputs; ++i) {
   742                     PTOUCHINPUT input = &inputs[i];
   743 
   744                     const SDL_TouchID touchId = (SDL_TouchID)((size_t)input->hSource);
   745                     if (!SDL_GetTouch(touchId)) {
   746                         if (SDL_AddTouch(touchId, "") < 0) {
   747                             continue;
   748                         }
   749                     }
   750 
   751                     /* Get the normalized coordinates for the window */
   752                     x = (float)(input->x - rect.left)/(rect.right - rect.left);
   753                     y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
   754 
   755                     if (input->dwFlags & TOUCHEVENTF_DOWN) {
   756                         SDL_SendTouch(touchId, input->dwID, SDL_TRUE, x, y, 1.0f);
   757                     }
   758                     if (input->dwFlags & TOUCHEVENTF_MOVE) {
   759                         SDL_SendTouchMotion(touchId, input->dwID, x, y, 1.0f);
   760                     }
   761                     if (input->dwFlags & TOUCHEVENTF_UP) {
   762                         SDL_SendTouch(touchId, input->dwID, SDL_FALSE, x, y, 1.0f);
   763                     }
   764                 }
   765             }
   766             SDL_stack_free(inputs);
   767 
   768             data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
   769             return 0;
   770         }
   771         break;
   772 
   773     case WM_DROPFILES:
   774         {
   775             UINT i;
   776             HDROP drop = (HDROP) wParam;
   777             UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
   778             for (i = 0; i < count; ++i) {
   779                 UINT size = DragQueryFile(drop, i, NULL, 0) + 1;
   780                 LPTSTR buffer = SDL_stack_alloc(TCHAR, size);
   781                 if (buffer) {
   782                     if (DragQueryFile(drop, i, buffer, size)) {
   783                         char *file = WIN_StringToUTF8(buffer);
   784                         SDL_SendDropFile(file);
   785                         SDL_free(file);
   786                     }
   787                     SDL_stack_free(buffer);
   788                 }
   789             }
   790             DragFinish(drop);
   791             return 0;
   792         }
   793         break;
   794     }
   795 
   796     /* If there's a window proc, assume it's going to handle messages */
   797     if (data->wndproc) {
   798         return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
   799     } else if (returnCode >= 0) {
   800         return returnCode;
   801     } else {
   802         return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
   803     }
   804 }
   805 
   806 void
   807 WIN_PumpEvents(_THIS)
   808 {
   809     const Uint8 *keystate;
   810     MSG msg;
   811     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
   812         DispatchMessage(&msg);
   813     }
   814 
   815     /* Windows loses a shift KEYUP event when you have both pressed at once and let go of one.
   816        You won't get a KEYUP until both are released, and that keyup will only be for the second
   817        key you released. Take heroic measures and check the keystate as of the last handled event,
   818        and if we think a key is pressed when Windows doesn't, unstick it in SDL's state. */
   819     keystate = SDL_GetKeyboardState(NULL);
   820     if ((keystate[SDL_SCANCODE_LSHIFT] == SDL_PRESSED) && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
   821         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
   822     }
   823     if ((keystate[SDL_SCANCODE_RSHIFT] == SDL_PRESSED) && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
   824         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RSHIFT);
   825     }
   826 }
   827 
   828 static int app_registered = 0;
   829 LPTSTR SDL_Appname = NULL;
   830 Uint32 SDL_Appstyle = 0;
   831 HINSTANCE SDL_Instance = NULL;
   832 
   833 /* Register the class for this application */
   834 int
   835 SDL_RegisterApp(char *name, Uint32 style, void *hInst)
   836 {
   837     WNDCLASS class;
   838 
   839     /* Only do this once... */
   840     if (app_registered) {
   841         ++app_registered;
   842         return (0);
   843     }
   844     if (!name && !SDL_Appname) {
   845         name = "SDL_app";
   846 #if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
   847         SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
   848 #endif
   849         SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
   850     }
   851 
   852     if (name) {
   853         SDL_Appname = WIN_UTF8ToString(name);
   854         SDL_Appstyle = style;
   855         SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
   856     }
   857 
   858     /* Register the application class */
   859     class.hCursor = NULL;
   860     class.hIcon =
   861         LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
   862                   LR_DEFAULTCOLOR);
   863     class.lpszMenuName = NULL;
   864     class.lpszClassName = SDL_Appname;
   865     class.hbrBackground = NULL;
   866     class.hInstance = SDL_Instance;
   867     class.style = SDL_Appstyle;
   868     class.lpfnWndProc = WIN_WindowProc;
   869     class.cbWndExtra = 0;
   870     class.cbClsExtra = 0;
   871     if (!RegisterClass(&class)) {
   872         return SDL_SetError("Couldn't register application class");
   873     }
   874 
   875     app_registered = 1;
   876     return 0;
   877 }
   878 
   879 /* Unregisters the windowclass registered in SDL_RegisterApp above. */
   880 void
   881 SDL_UnregisterApp()
   882 {
   883     WNDCLASS class;
   884 
   885     /* SDL_RegisterApp might not have been called before */
   886     if (!app_registered) {
   887         return;
   888     }
   889     --app_registered;
   890     if (app_registered == 0) {
   891         /* Check for any registered window classes. */
   892         if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
   893             UnregisterClass(SDL_Appname, SDL_Instance);
   894         }
   895         SDL_free(SDL_Appname);
   896         SDL_Appname = NULL;
   897     }
   898 }
   899 
   900 #endif /* SDL_VIDEO_DRIVER_WINDOWS */
   901 
   902 /* vi: set ts=4 sw=4 expandtab: */