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