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