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