src/video/windows/SDL_windowsevents.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 01 Aug 2013 01:50:02 -0400
changeset 7561 65f2b183e2aa
parent 7543 a96f309059a1
child 7631 9fcda7425ccc
permissions -rw-r--r--
Windows: Unstick shift keys in a timely manner when the OS loses a KEYUP event.

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