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