src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 17 Apr 2014 21:00:25 -0700
changeset 8713 e3a80970cf0b
parent 8624 c87db33e4cae
child 8796 9159d5c47819
child 8938 c73bac338b90
permissions -rw-r--r--
Fixed bug 2496 - mouse left button double click event issue

cplu

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