src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Jun 2015 11:49:48 -0700
changeset 9781 02273530b7bf
parent 9679 7fc4a8be47a8
child 9821 c1d91866a968
permissions -rw-r--r--
Fixed bug 2834 - Patch to support dead keys on Windows

Elise Maurer

When inputting text, dead-keys are currently not handled correctly on Windows with the latest SDL2 tip as well as the 2.0.3 release.

Using a French AZERTY keyboard, when I type the `^` key followed by `e` key to compose the `` character, I erroneously get two SDL_TEXTINPUT events, one with the `^` character and one with the `e` character.

I've looked at the history for SDL_windowsevents.c and there's been some back-and-forth with several methods for handling text input:

* r8142 removed any handling of WM_CHAR because keyboard input was being handled through WM_KEYDOWN along with ToUnicode since r7645.

* But using ToUnicode actually breaks dead-keys (googling for "ToUnicode dead keys" reports many horror stories of people trying to work around that and failing).

* It seems like r7645 introduced a double-fix: it fixed WM_CHAR to properly handle Unicode, and also (unnecessarily?) added text input handling to WM_KEYDOWN. Later, r8142 removed the WM_CHAR stuff instead of the WM_KEYDOWN stuff.

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