src/video/windows/SDL_windowsevents.c
author Gabriel Jacobo <gabomdq@gmail.com>
Wed, 13 Nov 2013 11:18:37 -0300
changeset 7975 1b49b680f319
parent 7941 7c249ca44069
child 8035 2cc220c7e899
permissions -rwxr-xr-x
[Windows] Fixes bug #1555, handle ALT+F4 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 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             POINT cursorPos;
   500             GetCursorPos(&cursorPos);
   501             ScreenToClient(hwnd, &cursorPos);
   502             SDL_SendMouseMotion(data->window, 0, 0, cursorPos.x, cursorPos.y);
   503             SDL_SetMouseFocus(NULL);
   504         }
   505         returnCode = 0;
   506         break;
   507 #endif /* WM_MOUSELEAVE */
   508 
   509     case WM_KEYDOWN:
   510     case WM_SYSKEYDOWN:
   511         {
   512             SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam );
   513             if ( code != SDL_SCANCODE_UNKNOWN ) {
   514                 SDL_SendKeyboardKey(SDL_PRESSED, code );
   515             }
   516         }
   517         if (msg == WM_KEYDOWN) {
   518             BYTE keyboardState[256];
   519             char text[5];
   520             UINT32 utf32 = 0;
   521 
   522             GetKeyboardState(keyboardState);
   523             if (ToUnicode(wParam, (lParam >> 16) & 0xff, keyboardState, (LPWSTR)&utf32, 1, 0) > 0) {
   524                 WORD repitition;
   525                 for (repitition = lParam & 0xffff; repitition > 0; repitition--) {
   526                     WIN_ConvertUTF32toUTF8(utf32, text);
   527                     SDL_SendKeyboardText(text);
   528                 }
   529             }
   530         }
   531         returnCode = 0;
   532         break;
   533 
   534     case WM_SYSKEYUP:
   535     case WM_KEYUP:
   536         {
   537             SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam );
   538             const Uint8 *keyboardState = SDL_GetKeyboardState(NULL);
   539 
   540             /* Detect relevant keyboard shortcuts */
   541             if (keyboardState[SDL_SCANCODE_LALT] == SDL_PRESSED || keyboardState[SDL_SCANCODE_RALT] == SDL_PRESSED ) {
   542 	            /* ALT+F4: Close window */
   543 	            if (code == SDL_SCANCODE_F4) {
   544 		            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
   545 	            }
   546             }
   547 
   548             if ( code != SDL_SCANCODE_UNKNOWN ) {
   549                 if (code == SDL_SCANCODE_PRINTSCREEN &&
   550                     keyboardState[code] == SDL_RELEASED) {
   551                     SDL_SendKeyboardKey(SDL_PRESSED, code);
   552                 }
   553                 SDL_SendKeyboardKey(SDL_RELEASED, code);
   554             }
   555         }
   556         returnCode = 0;
   557         break;
   558 
   559     case WM_UNICHAR:
   560         {
   561             if (wParam == UNICODE_NOCHAR) {
   562                 returnCode = 1;
   563                 break;
   564             }
   565         }
   566         /* no break */
   567     case WM_CHAR:
   568         {
   569             char text[5];
   570 
   571             WIN_ConvertUTF32toUTF8(wParam, text);
   572             SDL_SendKeyboardText(text);
   573         }
   574         returnCode = 0;
   575         break;
   576 
   577 #ifdef WM_INPUTLANGCHANGE
   578     case WM_INPUTLANGCHANGE:
   579         {
   580             WIN_UpdateKeymap();
   581         }
   582         returnCode = 1;
   583         break;
   584 #endif /* WM_INPUTLANGCHANGE */
   585 
   586 #ifdef WM_GETMINMAXINFO
   587     case WM_GETMINMAXINFO:
   588         {
   589             MINMAXINFO *info;
   590             RECT size;
   591             int x, y;
   592             int w, h;
   593             int min_w, min_h;
   594             int max_w, max_h;
   595             int style;
   596             BOOL menu;
   597             BOOL constrain_max_size;
   598 
   599             if (SDL_IsShapedWindow(data->window))
   600                 Win32_ResizeWindowShape(data->window);
   601 
   602             /* If this is an expected size change, allow it */
   603             if (data->expected_resize) {
   604                 break;
   605             }
   606 
   607             /* Get the current position of our window */
   608             GetWindowRect(hwnd, &size);
   609             x = size.left;
   610             y = size.top;
   611 
   612             /* Calculate current size of our window */
   613             SDL_GetWindowSize(data->window, &w, &h);
   614             SDL_GetWindowMinimumSize(data->window, &min_w, &min_h);
   615             SDL_GetWindowMaximumSize(data->window, &max_w, &max_h);
   616 
   617             /* Store in min_w and min_h difference between current size and minimal
   618                size so we don't need to call AdjustWindowRectEx twice */
   619             min_w -= w;
   620             min_h -= h;
   621             if (max_w && max_h) {
   622                 max_w -= w;
   623                 max_h -= h;
   624                 constrain_max_size = TRUE;
   625             } else {
   626                 constrain_max_size = FALSE;
   627             }
   628 
   629             size.top = 0;
   630             size.left = 0;
   631             size.bottom = h;
   632             size.right = w;
   633 
   634             style = GetWindowLong(hwnd, GWL_STYLE);
   635             /* DJM - according to the docs for GetMenu(), the
   636                return value is undefined if hwnd is a child window.
   637                Aparently it's too difficult for MS to check
   638                inside their function, so I have to do it here.
   639              */
   640             menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
   641             AdjustWindowRectEx(&size, style, menu, 0);
   642             w = size.right - size.left;
   643             h = size.bottom - size.top;
   644 
   645             /* Fix our size to the current size */
   646             info = (MINMAXINFO *) lParam;
   647             if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
   648                 info->ptMinTrackSize.x = w + min_w;
   649                 info->ptMinTrackSize.y = h + min_h;
   650                 if (constrain_max_size) {
   651                     info->ptMaxTrackSize.x = w + max_w;
   652                     info->ptMaxTrackSize.y = h + max_h;
   653                 }
   654             } else {
   655                 info->ptMaxSize.x = w;
   656                 info->ptMaxSize.y = h;
   657                 info->ptMaxPosition.x = x;
   658                 info->ptMaxPosition.y = y;
   659                 info->ptMinTrackSize.x = w;
   660                 info->ptMinTrackSize.y = h;
   661                 info->ptMaxTrackSize.x = w;
   662                 info->ptMaxTrackSize.y = h;
   663             }
   664         }
   665         returnCode = 0;
   666         break;
   667 #endif /* WM_GETMINMAXINFO */
   668 
   669     case WM_WINDOWPOSCHANGED:
   670         {
   671             RECT rect;
   672             int x, y;
   673             int w, h;
   674             Uint32 window_flags;
   675 
   676             if (!GetClientRect(hwnd, &rect) ||
   677                 (rect.right == rect.left && rect.bottom == rect.top)) {
   678                 break;
   679             }
   680             ClientToScreen(hwnd, (LPPOINT) & rect);
   681             ClientToScreen(hwnd, (LPPOINT) & rect + 1);
   682 
   683             window_flags = SDL_GetWindowFlags(data->window);
   684             if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
   685                 (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
   686                 ClipCursor(&rect);
   687             }
   688 
   689             x = rect.left;
   690             y = rect.top;
   691             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
   692 
   693             w = rect.right - rect.left;
   694             h = rect.bottom - rect.top;
   695             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
   696                                 h);
   697         }
   698         break;
   699 
   700     case WM_SIZE:
   701         {
   702             switch (wParam)
   703             {
   704             case SIZE_MAXIMIZED:
   705                 SDL_SendWindowEvent(data->window,
   706                     SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
   707                 break;
   708             case SIZE_MINIMIZED:
   709                 SDL_SendWindowEvent(data->window,
   710                     SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   711                 break;
   712             default:
   713                 SDL_SendWindowEvent(data->window,
   714                     SDL_WINDOWEVENT_RESTORED, 0, 0);
   715                 break;
   716             }
   717         }
   718         break;
   719 
   720     case WM_SETCURSOR:
   721         {
   722             Uint16 hittest;
   723 
   724             hittest = LOWORD(lParam);
   725             if (hittest == HTCLIENT) {
   726                 SetCursor(SDL_cursor);
   727                 returnCode = TRUE;
   728             }
   729         }
   730         break;
   731 
   732         /* We were occluded, refresh our display */
   733     case WM_PAINT:
   734         {
   735             RECT rect;
   736             if (GetUpdateRect(hwnd, &rect, FALSE)) {
   737                 ValidateRect(hwnd, NULL);
   738                 SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
   739                                     0, 0);
   740             }
   741         }
   742         returnCode = 0;
   743         break;
   744 
   745         /* We'll do our own drawing, prevent flicker */
   746     case WM_ERASEBKGND:
   747         {
   748         }
   749         return (1);
   750 
   751 #if defined(SC_SCREENSAVE) || defined(SC_MONITORPOWER)
   752     case WM_SYSCOMMAND:
   753         {
   754             /* Don't start the screensaver or blank the monitor in fullscreen apps */
   755             if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
   756                 (wParam & 0xFFF0) == SC_MONITORPOWER) {
   757                 if (SDL_GetVideoDevice()->suspend_screensaver) {
   758                     return (0);
   759                 }
   760             }
   761         }
   762         break;
   763 #endif /* System has screensaver support */
   764 
   765     case WM_CLOSE:
   766         {
   767             SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
   768         }
   769         returnCode = 0;
   770         break;
   771 
   772     case WM_TOUCH:
   773         {
   774             UINT i, num_inputs = LOWORD(wParam);
   775             PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
   776             if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
   777                 RECT rect;
   778                 float x, y;
   779 
   780                 if (!GetClientRect(hwnd, &rect) ||
   781                     (rect.right == rect.left && rect.bottom == rect.top)) {
   782                     if (inputs) {
   783                         SDL_stack_free(inputs);
   784                     }
   785                     break;
   786                 }
   787                 ClientToScreen(hwnd, (LPPOINT) & rect);
   788                 ClientToScreen(hwnd, (LPPOINT) & rect + 1);
   789                 rect.top *= 100;
   790                 rect.left *= 100;
   791                 rect.bottom *= 100;
   792                 rect.right *= 100;
   793 
   794                 for (i = 0; i < num_inputs; ++i) {
   795                     PTOUCHINPUT input = &inputs[i];
   796 
   797                     const SDL_TouchID touchId = (SDL_TouchID)((size_t)input->hSource);
   798                     if (!SDL_GetTouch(touchId)) {
   799                         if (SDL_AddTouch(touchId, "") < 0) {
   800                             continue;
   801                         }
   802                     }
   803 
   804                     /* Get the normalized coordinates for the window */
   805                     x = (float)(input->x - rect.left)/(rect.right - rect.left);
   806                     y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
   807 
   808                     if (input->dwFlags & TOUCHEVENTF_DOWN) {
   809                         SDL_SendTouch(touchId, input->dwID, SDL_TRUE, x, y, 1.0f);
   810                     }
   811                     if (input->dwFlags & TOUCHEVENTF_MOVE) {
   812                         SDL_SendTouchMotion(touchId, input->dwID, x, y, 1.0f);
   813                     }
   814                     if (input->dwFlags & TOUCHEVENTF_UP) {
   815                         SDL_SendTouch(touchId, input->dwID, SDL_FALSE, x, y, 1.0f);
   816                     }
   817                 }
   818             }
   819             SDL_stack_free(inputs);
   820 
   821             data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
   822             return 0;
   823         }
   824         break;
   825 
   826     case WM_DROPFILES:
   827         {
   828             UINT i;
   829             HDROP drop = (HDROP) wParam;
   830             UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
   831             for (i = 0; i < count; ++i) {
   832                 UINT size = DragQueryFile(drop, i, NULL, 0) + 1;
   833                 LPTSTR buffer = SDL_stack_alloc(TCHAR, size);
   834                 if (buffer) {
   835                     if (DragQueryFile(drop, i, buffer, size)) {
   836                         char *file = WIN_StringToUTF8(buffer);
   837                         SDL_SendDropFile(file);
   838                         SDL_free(file);
   839                     }
   840                     SDL_stack_free(buffer);
   841                 }
   842             }
   843             DragFinish(drop);
   844             return 0;
   845         }
   846         break;
   847     }
   848 
   849     /* If there's a window proc, assume it's going to handle messages */
   850     if (data->wndproc) {
   851         return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
   852     } else if (returnCode >= 0) {
   853         return returnCode;
   854     } else {
   855         return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
   856     }
   857 }
   858 
   859 void
   860 WIN_PumpEvents(_THIS)
   861 {
   862     const Uint8 *keystate;
   863     MSG msg;
   864     while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
   865         DispatchMessage(&msg);
   866     }
   867 
   868     /* Windows loses a shift KEYUP event when you have both pressed at once and let go of one.
   869        You won't get a KEYUP until both are released, and that keyup will only be for the second
   870        key you released. Take heroic measures and check the keystate as of the last handled event,
   871        and if we think a key is pressed when Windows doesn't, unstick it in SDL's state. */
   872     keystate = SDL_GetKeyboardState(NULL);
   873     if ((keystate[SDL_SCANCODE_LSHIFT] == SDL_PRESSED) && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
   874         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_LSHIFT);
   875     }
   876     if ((keystate[SDL_SCANCODE_RSHIFT] == SDL_PRESSED) && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
   877         SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_RSHIFT);
   878     }
   879 }
   880 
   881 static int app_registered = 0;
   882 LPTSTR SDL_Appname = NULL;
   883 Uint32 SDL_Appstyle = 0;
   884 HINSTANCE SDL_Instance = NULL;
   885 
   886 /* Register the class for this application */
   887 int
   888 SDL_RegisterApp(char *name, Uint32 style, void *hInst)
   889 {
   890     WNDCLASS class;
   891 
   892     /* Only do this once... */
   893     if (app_registered) {
   894         ++app_registered;
   895         return (0);
   896     }
   897     if (!name && !SDL_Appname) {
   898         name = "SDL_app";
   899 #if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
   900         SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
   901 #endif
   902         SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
   903     }
   904 
   905     if (name) {
   906         SDL_Appname = WIN_UTF8ToString(name);
   907         SDL_Appstyle = style;
   908         SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
   909     }
   910 
   911     /* Register the application class */
   912     class.hCursor = NULL;
   913     class.hIcon =
   914         LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
   915                   LR_DEFAULTCOLOR);
   916     class.lpszMenuName = NULL;
   917     class.lpszClassName = SDL_Appname;
   918     class.hbrBackground = NULL;
   919     class.hInstance = SDL_Instance;
   920     class.style = SDL_Appstyle;
   921     class.lpfnWndProc = WIN_WindowProc;
   922     class.cbWndExtra = 0;
   923     class.cbClsExtra = 0;
   924     if (!RegisterClass(&class)) {
   925         return SDL_SetError("Couldn't register application class");
   926     }
   927 
   928     app_registered = 1;
   929     return 0;
   930 }
   931 
   932 /* Unregisters the windowclass registered in SDL_RegisterApp above. */
   933 void
   934 SDL_UnregisterApp()
   935 {
   936     WNDCLASS class;
   937 
   938     /* SDL_RegisterApp might not have been called before */
   939     if (!app_registered) {
   940         return;
   941     }
   942     --app_registered;
   943     if (app_registered == 0) {
   944         /* Check for any registered window classes. */
   945         if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
   946             UnregisterClass(SDL_Appname, SDL_Instance);
   947         }
   948         SDL_free(SDL_Appname);
   949         SDL_Appname = NULL;
   950     }
   951 }
   952 
   953 #endif /* SDL_VIDEO_DRIVER_WINDOWS */
   954 
   955 /* vi: set ts=4 sw=4 expandtab: */