src/video/windows/SDL_windowsevents.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 30 May 2014 01:51:13 -0400
changeset 8942 3d84839c97b2
parent 8941 9229a54e4952
child 8948 3ed9a59efda1
permissions -rw-r--r--
Fixed up SDL_CaptureMouse() on Windows to work like I expected.

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