src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 27 Nov 2013 10:29:38 -0800
changeset 8036 d485906bd74a
parent 8035 2cc220c7e899
child 8050 be19dbc5ff4e
permissions -rw-r--r--
Fixed bug 2260 - SDL_SetCursorGrab() is buggy on Windows

BurnSpamAddress

Steps to reproduce:
1. Grab the cursor with SDL_SetCursorGrab()
2. Alt-tab away from the window
3. Click on the titlebar of the window

This will cause the window to disappear underneath the taskbar!

This appears to be a general issue with ClipCursor() on windows, i.e. I am getting the same behavior if I call ClipCursor() directly.

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