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