src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 23 Jun 2014 10:09:15 -0700
changeset 8916 96d944f74851
parent 8817 13003597b6c4
child 8953 dc80dc0bd22e
permissions -rw-r--r--
Fixed grab interaction with Windows Classic theme

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