src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 17 Apr 2014 21:00:25 -0700
changeset 8713 e3a80970cf0b
parent 8624 c87db33e4cae
child 8796 9159d5c47819
child 8938 c73bac338b90
permissions -rw-r--r--
Fixed bug 2496 - mouse left button double click event issue

cplu

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