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