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