src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 27 Nov 2013 10:29:32 -0800
changeset 8035 2cc220c7e899
parent 7975 1b49b680f319
child 8036 d485906bd74a
permissions -rw-r--r--
Fixed large relative mouse motion when iconifying the SDL window.
Windows will move the window to -32000,-32000 when it is iconified, so we don't want to send mouse motion for iconic 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 LRESULT CALLBACK
   290 WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
   291 {
   292     SDL_WindowData *data;
   293     LRESULT returnCode = -1;
   294 
   295     /* Send a SDL_SYSWMEVENT if the application wants them */
   296     if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
   297         SDL_SysWMmsg wmmsg;
   298 
   299         SDL_VERSION(&wmmsg.version);
   300         wmmsg.subsystem = SDL_SYSWM_WINDOWS;
   301         wmmsg.msg.win.hwnd = hwnd;
   302         wmmsg.msg.win.msg = msg;
   303         wmmsg.msg.win.wParam = wParam;
   304         wmmsg.msg.win.lParam = lParam;
   305         SDL_SendSysWMEvent(&wmmsg);
   306     }
   307 
   308     /* Get the window data for the window */
   309     data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
   310     if (!data) {
   311         return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
   312     }
   313 
   314 #ifdef WMMSG_DEBUG
   315     {
   316         char message[1024];
   317         if (msg > MAX_WMMSG) {
   318             SDL_snprintf(message, sizeof(message), "Received windows message: %p UNKNOWN (%d) -- 0x%X, 0x%X\n", hwnd, msg, wParam, lParam);
   319         } else {
   320             SDL_snprintf(message, sizeof(message), "Received windows message: %p %s -- 0x%X, 0x%X\n", hwnd, wmtab[msg], wParam, lParam);
   321         }
   322         OutputDebugStringA(message);
   323     }
   324 #endif /* WMMSG_DEBUG */
   325 
   326     if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata))
   327         return 0;
   328 
   329     switch (msg) {
   330 
   331     case WM_SHOWWINDOW:
   332         {
   333             if (wParam) {
   334                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
   335             } else {
   336                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   337             }
   338         }
   339         break;
   340 
   341     case WM_ACTIVATE:
   342         {
   343             BOOL minimized;
   344 
   345             minimized = HIWORD(wParam);
   346             if (!minimized && (LOWORD(wParam) != WA_INACTIVE)) {
   347                 Uint32 mouseFlags;
   348                 SHORT keyState;
   349 
   350                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
   351                 if (SDL_GetKeyboardFocus() != data->window) {
   352                     SDL_SetKeyboardFocus(data->window);
   353                 }
   354                 /* mouse buttons may have changed state here, we need
   355                 to resync them, but we will get a WM_MOUSEMOVE right away which will fix
   356                 things up if in non raw mode also
   357                 */
   358                 mouseFlags = SDL_GetMouseState( NULL, NULL );
   359 
   360                 keyState = GetAsyncKeyState( VK_LBUTTON );
   361                 WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT );
   362                 keyState = GetAsyncKeyState( VK_RBUTTON );
   363                 WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT );
   364                 keyState = GetAsyncKeyState( VK_MBUTTON );
   365                 WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE );
   366                 keyState = GetAsyncKeyState( VK_XBUTTON1 );
   367                 WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1 );
   368                 keyState = GetAsyncKeyState( VK_XBUTTON2 );
   369                 WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2 );
   370                 data->mouse_button_flags = 0;
   371 
   372                 if(SDL_GetMouse()->relative_mode) {
   373                     LONG cx, cy;
   374                     RECT rect;
   375                     GetWindowRect(hwnd, &rect);
   376 
   377                     cx = (rect.left + rect.right) / 2;
   378                     cy = (rect.top + rect.bottom) / 2;
   379 
   380                     /* Make an absurdly small clip rect */
   381                     rect.left = cx-1;
   382                     rect.right = cx+1;
   383                     rect.top = cy-1;
   384                     rect.bottom = cy+1;
   385 
   386                     ClipCursor(&rect);
   387                 }
   388 
   389                 /*
   390                  * FIXME: Update keyboard state
   391                  */
   392                 WIN_CheckClipboardUpdate(data->videodata);
   393             } else {
   394                 if (SDL_GetKeyboardFocus() == data->window) {
   395                     SDL_SetKeyboardFocus(NULL);
   396                 }
   397             }
   398         }
   399         returnCode = 0;
   400         break;
   401 
   402     case WM_MOUSEMOVE:
   403         if( !SDL_GetMouse()->relative_mode )
   404             SDL_SendMouseMotion(data->window, 0, 0, GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
   405         /* don't break here, fall through to check the wParam like the button presses */
   406     case WM_LBUTTONUP:
   407     case WM_RBUTTONUP:
   408     case WM_MBUTTONUP:
   409     case WM_XBUTTONUP:
   410     case WM_LBUTTONDOWN:
   411     case WM_RBUTTONDOWN:
   412     case WM_MBUTTONDOWN:
   413     case WM_XBUTTONDOWN:
   414         if( !SDL_GetMouse()->relative_mode )
   415             WIN_CheckWParamMouseButtons( wParam, data );
   416         break;
   417 
   418     case WM_INPUT:
   419     {
   420         HRAWINPUT hRawInput = (HRAWINPUT)lParam;
   421         RAWINPUT inp;
   422         UINT size = sizeof(inp);
   423 
   424         if(!SDL_GetMouse()->relative_mode)
   425             break;
   426 
   427         GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
   428 
   429         /* Mouse data */
   430         if(inp.header.dwType == RIM_TYPEMOUSE)
   431         {
   432             RAWMOUSE* mouse = &inp.data.mouse;
   433 
   434             if((mouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE)
   435             {
   436                 SDL_SendMouseMotion(data->window, 0, 1, (int)mouse->lLastX, (int)mouse->lLastY);
   437             }
   438             else
   439             {
   440                 /* synthesize relative moves from the abs position */
   441                 static SDL_Point initialMousePoint;
   442                 if ( initialMousePoint.x == 0 && initialMousePoint.y == 0 )
   443                 {
   444                     initialMousePoint.x = mouse->lLastX;
   445                     initialMousePoint.y = mouse->lLastY;
   446                 }
   447 
   448                 SDL_SendMouseMotion(data->window, 0, 1, (int)(mouse->lLastX-initialMousePoint.x), (int)(mouse->lLastY-initialMousePoint.y) );
   449 
   450                 initialMousePoint.x = mouse->lLastX;
   451                 initialMousePoint.y = mouse->lLastY;
   452             }
   453             WIN_CheckRawMouseButtons( mouse->usButtonFlags, data );
   454         }
   455         break;
   456     }
   457 
   458     case WM_MOUSEWHEEL:
   459         {
   460             static short s_AccumulatedMotion;
   461 
   462             s_AccumulatedMotion += GET_WHEEL_DELTA_WPARAM(wParam);
   463             if (s_AccumulatedMotion > 0) {
   464                 while (s_AccumulatedMotion >= WHEEL_DELTA) {
   465                     SDL_SendMouseWheel(data->window, 0, 0, 1);
   466                     s_AccumulatedMotion -= WHEEL_DELTA;
   467                 }
   468             } else {
   469                 while (s_AccumulatedMotion <= -WHEEL_DELTA) {
   470                     SDL_SendMouseWheel(data->window, 0, 0, -1);
   471                     s_AccumulatedMotion += WHEEL_DELTA;
   472                 }
   473             }
   474             break;
   475         }
   476 
   477     case WM_MOUSEHWHEEL:
   478         {
   479             static short s_AccumulatedMotion;
   480 
   481             s_AccumulatedMotion += GET_WHEEL_DELTA_WPARAM(wParam);
   482             if (s_AccumulatedMotion > 0) {
   483                 while (s_AccumulatedMotion >= WHEEL_DELTA) {
   484                     SDL_SendMouseWheel(data->window, 0, 1, 0);
   485                     s_AccumulatedMotion -= WHEEL_DELTA;
   486                 }
   487             } else {
   488                 while (s_AccumulatedMotion <= -WHEEL_DELTA) {
   489                     SDL_SendMouseWheel(data->window, 0, -1, 0);
   490                     s_AccumulatedMotion += WHEEL_DELTA;
   491                 }
   492             }
   493             break;
   494         }
   495 
   496 #ifdef WM_MOUSELEAVE
   497     case WM_MOUSELEAVE:
   498         if (SDL_GetMouseFocus() == data->window && !SDL_GetMouse()->relative_mode) {
   499             if (!IsIconic(hwnd)) {
   500                 POINT cursorPos;
   501                 GetCursorPos(&cursorPos);
   502                 ScreenToClient(hwnd, &cursorPos);
   503                 SDL_SendMouseMotion(data->window, 0, 0, cursorPos.x, cursorPos.y);
   504             }
   505             SDL_SetMouseFocus(NULL);
   506         }
   507         returnCode = 0;
   508         break;
   509 #endif /* WM_MOUSELEAVE */
   510 
   511     case WM_KEYDOWN:
   512     case WM_SYSKEYDOWN:
   513         {
   514             SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam );
   515             if ( code != SDL_SCANCODE_UNKNOWN ) {
   516                 SDL_SendKeyboardKey(SDL_PRESSED, code );
   517             }
   518         }
   519         if (msg == WM_KEYDOWN) {
   520             BYTE keyboardState[256];
   521             char text[5];
   522             UINT32 utf32 = 0;
   523 
   524             GetKeyboardState(keyboardState);
   525             if (ToUnicode(wParam, (lParam >> 16) & 0xff, keyboardState, (LPWSTR)&utf32, 1, 0) > 0) {
   526                 WORD repitition;
   527                 for (repitition = lParam & 0xffff; repitition > 0; repitition--) {
   528                     WIN_ConvertUTF32toUTF8(utf32, text);
   529                     SDL_SendKeyboardText(text);
   530                 }
   531             }
   532         }
   533         returnCode = 0;
   534         break;
   535 
   536     case WM_SYSKEYUP:
   537     case WM_KEYUP:
   538         {
   539             SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam );
   540             const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
   541 
   542             /* Detect relevant keyboard shortcuts */
   543             if (keyboardState[SDL_SCANCODE_LALT] == SDL_PRESSED || keyboardState[SDL_SCANCODE_RALT] == SDL_PRESSED ) {
   544 	            /* ALT+F4: Close window */
   545 	            if (code == SDL_SCANCODE_F4) {
   546 		            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
   547 	            }
   548             }
   549 
   550             if ( code != SDL_SCANCODE_UNKNOWN ) {
   551                 if (code == SDL_SCANCODE_PRINTSCREEN &&
   552                     keyboardState[code] == SDL_RELEASED) {
   553                     SDL_SendKeyboardKey(SDL_PRESSED, code);
   554                 }
   555                 SDL_SendKeyboardKey(SDL_RELEASED, code);
   556             }
   557         }
   558         returnCode = 0;
   559         break;
   560 
   561     case WM_UNICHAR:
   562         {
   563             if (wParam == UNICODE_NOCHAR) {
   564                 returnCode = 1;
   565                 break;
   566             }
   567         }
   568         /* no break */
   569     case WM_CHAR:
   570         {
   571             char text[5];
   572 
   573             WIN_ConvertUTF32toUTF8(wParam, text);
   574             SDL_SendKeyboardText(text);
   575         }
   576         returnCode = 0;
   577         break;
   578 
   579 #ifdef WM_INPUTLANGCHANGE
   580     case WM_INPUTLANGCHANGE:
   581         {
   582             WIN_UpdateKeymap();
   583         }
   584         returnCode = 1;
   585         break;
   586 #endif /* WM_INPUTLANGCHANGE */
   587 
   588 #ifdef WM_GETMINMAXINFO
   589     case WM_GETMINMAXINFO:
   590         {
   591             MINMAXINFO *info;
   592             RECT size;
   593             int x, y;
   594             int w, h;
   595             int min_w, min_h;
   596             int max_w, max_h;
   597             int style;
   598             BOOL menu;
   599             BOOL constrain_max_size;
   600 
   601             if (SDL_IsShapedWindow(data->window))
   602                 Win32_ResizeWindowShape(data->window);
   603 
   604             /* If this is an expected size change, allow it */
   605             if (data->expected_resize) {
   606                 break;
   607             }
   608 
   609             /* Get the current position of our window */
   610             GetWindowRect(hwnd, &size);
   611             x = size.left;
   612             y = size.top;
   613 
   614             /* Calculate current size of our window */
   615             SDL_GetWindowSize(data->window, &w, &h);
   616             SDL_GetWindowMinimumSize(data->window, &min_w, &min_h);
   617             SDL_GetWindowMaximumSize(data->window, &max_w, &max_h);
   618 
   619             /* Store in min_w and min_h difference between current size and minimal
   620                size so we don't need to call AdjustWindowRectEx twice */
   621             min_w -= w;
   622             min_h -= h;
   623             if (max_w && max_h) {
   624                 max_w -= w;
   625                 max_h -= h;
   626                 constrain_max_size = TRUE;
   627             } else {
   628                 constrain_max_size = FALSE;
   629             }
   630 
   631             size.top = 0;
   632             size.left = 0;
   633             size.bottom = h;
   634             size.right = w;
   635 
   636             style = GetWindowLong(hwnd, GWL_STYLE);
   637             /* DJM - according to the docs for GetMenu(), the
   638                return value is undefined if hwnd is a child window.
   639                Aparently it's too difficult for MS to check
   640                inside their function, so I have to do it here.
   641              */
   642             menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
   643             AdjustWindowRectEx(&size, style, menu, 0);
   644             w = size.right - size.left;
   645             h = size.bottom - size.top;
   646 
   647             /* Fix our size to the current size */
   648             info = (MINMAXINFO *) lParam;
   649             if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
   650                 info->ptMinTrackSize.x = w + min_w;
   651                 info->ptMinTrackSize.y = h + min_h;
   652                 if (constrain_max_size) {
   653                     info->ptMaxTrackSize.x = w + max_w;
   654                     info->ptMaxTrackSize.y = h + max_h;
   655                 }
   656             } else {
   657                 info->ptMaxSize.x = w;
   658                 info->ptMaxSize.y = h;
   659                 info->ptMaxPosition.x = x;
   660                 info->ptMaxPosition.y = y;
   661                 info->ptMinTrackSize.x = w;
   662                 info->ptMinTrackSize.y = h;
   663                 info->ptMaxTrackSize.x = w;
   664                 info->ptMaxTrackSize.y = h;
   665             }
   666         }
   667         returnCode = 0;
   668         break;
   669 #endif /* WM_GETMINMAXINFO */
   670 
   671     case WM_WINDOWPOSCHANGED:
   672         {
   673             RECT rect;
   674             int x, y;
   675             int w, h;
   676             Uint32 window_flags;
   677 
   678             if (!GetClientRect(hwnd, &rect) ||
   679                 (rect.right == rect.left && rect.bottom == rect.top)) {
   680                 break;
   681             }
   682             ClientToScreen(hwnd, (LPPOINT) & rect);
   683             ClientToScreen(hwnd, (LPPOINT) & rect + 1);
   684 
   685             window_flags = SDL_GetWindowFlags(data->window);
   686             if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
   687                 (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
   688                 ClipCursor(&rect);
   689             }
   690 
   691             x = rect.left;
   692             y = rect.top;
   693             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
   694 
   695             w = rect.right - rect.left;
   696             h = rect.bottom - rect.top;
   697             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
   698                                 h);
   699         }
   700         break;
   701 
   702     case WM_SIZE:
   703         {
   704             switch (wParam)
   705             {
   706             case SIZE_MAXIMIZED:
   707                 SDL_SendWindowEvent(data->window,
   708                     SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
   709                 break;
   710             case SIZE_MINIMIZED:
   711                 SDL_SendWindowEvent(data->window,
   712                     SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   713                 break;
   714             default:
   715                 SDL_SendWindowEvent(data->window,
   716                     SDL_WINDOWEVENT_RESTORED, 0, 0);
   717                 break;
   718             }
   719         }
   720         break;
   721 
   722     case WM_SETCURSOR:
   723         {
   724             Uint16 hittest;
   725 
   726             hittest = LOWORD(lParam);
   727             if (hittest == HTCLIENT) {
   728                 SetCursor(SDL_cursor);
   729                 returnCode = TRUE;
   730             }
   731         }
   732         break;
   733 
   734         /* We were occluded, refresh our display */
   735     case WM_PAINT:
   736         {
   737             RECT rect;
   738             if (GetUpdateRect(hwnd, &rect, FALSE)) {
   739                 ValidateRect(hwnd, NULL);
   740                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
   741                                     0, 0);
   742             }
   743         }
   744         returnCode = 0;
   745         break;
   746 
   747         /* We'll do our own drawing, prevent flicker */
   748     case WM_ERASEBKGND:
   749         {
   750         }
   751         return (1);
   752 
   753 #if defined(SC_SCREENSAVE) || defined(SC_MONITORPOWER)
   754     case WM_SYSCOMMAND:
   755         {
   756             /* Don't start the screensaver or blank the monitor in fullscreen apps */
   757             if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
   758                 (wParam & 0xFFF0) == SC_MONITORPOWER) {
   759                 if (SDL_GetVideoDevice()->suspend_screensaver) {
   760                     return (0);
   761                 }
   762             }
   763         }
   764         break;
   765 #endif /* System has screensaver support */
   766 
   767     case WM_CLOSE:
   768         {
   769             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
   770         }
   771         returnCode = 0;
   772         break;
   773 
   774     case WM_TOUCH:
   775         {
   776             UINT i, num_inputs = LOWORD(wParam);
   777             PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
   778             if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
   779                 RECT rect;
   780                 float x, y;
   781 
   782                 if (!GetClientRect(hwnd, &rect) ||
   783                     (rect.right == rect.left && rect.bottom == rect.top)) {
   784                     if (inputs) {
   785                         SDL_stack_free(inputs);
   786                     }
   787                     break;
   788                 }
   789                 ClientToScreen(hwnd, (LPPOINT) & rect);
   790                 ClientToScreen(hwnd, (LPPOINT) & rect + 1);
   791                 rect.top *= 100;
   792                 rect.left *= 100;
   793                 rect.bottom *= 100;
   794                 rect.right *= 100;
   795 
   796                 for (i = 0; i < num_inputs; ++i) {
   797                     PTOUCHINPUT input = &inputs[i];
   798 
   799                     const SDL_TouchID touchId = (SDL_TouchID)((size_t)input->hSource);
   800                     if (!SDL_GetTouch(touchId)) {
   801                         if (SDL_AddTouch(touchId, "") < 0) {
   802                             continue;
   803                         }
   804                     }
   805 
   806                     /* Get the normalized coordinates for the window */
   807                     x = (float)(input->x - rect.left)/(rect.right - rect.left);
   808                     y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
   809 
   810                     if (input->dwFlags & TOUCHEVENTF_DOWN) {
   811                         SDL_SendTouch(touchId, input->dwID, SDL_TRUE, x, y, 1.0f);
   812                     }
   813                     if (input->dwFlags & TOUCHEVENTF_MOVE) {
   814                         SDL_SendTouchMotion(touchId, input->dwID, x, y, 1.0f);
   815                     }
   816                     if (input->dwFlags & TOUCHEVENTF_UP) {
   817                         SDL_SendTouch(touchId, input->dwID, SDL_FALSE, x, y, 1.0f);
   818                     }
   819                 }
   820             }
   821             SDL_stack_free(inputs);
   822 
   823             data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
   824             return 0;
   825         }
   826         break;
   827 
   828     case WM_DROPFILES:
   829         {
   830             UINT i;
   831             HDROP drop = (HDROP) wParam;
   832             UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
   833             for (i = 0; i < count; ++i) {
   834                 UINT size = DragQueryFile(drop, i, NULL, 0) + 1;
   835                 LPTSTR buffer = SDL_stack_alloc(TCHAR, size);
   836                 if (buffer) {
   837                     if (DragQueryFile(drop, i, buffer, size)) {
   838                         char *file = WIN_StringToUTF8(buffer);
   839                         SDL_SendDropFile(file);
   840                         SDL_free(file);
   841                     }
   842                     SDL_stack_free(buffer);
   843                 }
   844             }
   845             DragFinish(drop);
   846             return 0;
   847         }
   848         break;
   849     }
   850 
   851     /* If there's a window proc, assume it's going to handle messages */
   852     if (data->wndproc) {
   853         return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
   854     } else if (returnCode >= 0) {
   855         return returnCode;
   856     } else {
   857         return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
   858     }
   859 }
   860 
   861 void
   862 WIN_PumpEvents(_THIS)
   863 {
   864     const Uint8 *keystate;
   865     MSG msg;
   866     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
   867         DispatchMessage(&msg);
   868     }
   869 
   870     /* Windows loses a shift KEYUP event when you have both pressed at once and let go of one.
   871        You won't get a KEYUP until both are released, and that keyup will only be for the second
   872        key you released. Take heroic measures and check the keystate as of the last handled event,
   873        and if we think a key is pressed when Windows doesn't, unstick it in SDL's state. */
   874     keystate = SDL_GetKeyboardState(NULL);
   875     if ((keystate[SDL_SCANCODE_LSHIFT] == SDL_PRESSED) && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
   876         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
   877     }
   878     if ((keystate[SDL_SCANCODE_RSHIFT] == SDL_PRESSED) && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
   879         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RSHIFT);
   880     }
   881 }
   882 
   883 static int app_registered = 0;
   884 LPTSTR SDL_Appname = NULL;
   885 Uint32 SDL_Appstyle = 0;
   886 HINSTANCE SDL_Instance = NULL;
   887 
   888 /* Register the class for this application */
   889 int
   890 SDL_RegisterApp(char *name, Uint32 style, void *hInst)
   891 {
   892     WNDCLASS class;
   893 
   894     /* Only do this once... */
   895     if (app_registered) {
   896         ++app_registered;
   897         return (0);
   898     }
   899     if (!name && !SDL_Appname) {
   900         name = "SDL_app";
   901 #if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
   902         SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
   903 #endif
   904         SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
   905     }
   906 
   907     if (name) {
   908         SDL_Appname = WIN_UTF8ToString(name);
   909         SDL_Appstyle = style;
   910         SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
   911     }
   912 
   913     /* Register the application class */
   914     class.hCursor = NULL;
   915     class.hIcon =
   916         LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
   917                   LR_DEFAULTCOLOR);
   918     class.lpszMenuName = NULL;
   919     class.lpszClassName = SDL_Appname;
   920     class.hbrBackground = NULL;
   921     class.hInstance = SDL_Instance;
   922     class.style = SDL_Appstyle;
   923     class.lpfnWndProc = WIN_WindowProc;
   924     class.cbWndExtra = 0;
   925     class.cbClsExtra = 0;
   926     if (!RegisterClass(&class)) {
   927         return SDL_SetError("Couldn't register application class");
   928     }
   929 
   930     app_registered = 1;
   931     return 0;
   932 }
   933 
   934 /* Unregisters the windowclass registered in SDL_RegisterApp above. */
   935 void
   936 SDL_UnregisterApp()
   937 {
   938     WNDCLASS class;
   939 
   940     /* SDL_RegisterApp might not have been called before */
   941     if (!app_registered) {
   942         return;
   943     }
   944     --app_registered;
   945     if (app_registered == 0) {
   946         /* Check for any registered window classes. */
   947         if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
   948             UnregisterClass(SDL_Appname, SDL_Instance);
   949         }
   950         SDL_free(SDL_Appname);
   951         SDL_Appname = NULL;
   952     }
   953 }
   954 
   955 #endif /* SDL_VIDEO_DRIVER_WINDOWS */
   956 
   957 /* vi: set ts=4 sw=4 expandtab: */