src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 5889 c2059a52efa1
child 6138 4c64952a58fb
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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
*/
slouken@6044
    21
#include "SDL_config.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@1895
    28
#include "SDL_vkeys.h"
slouken@1895
    29
#include "../../events/SDL_events_c.h"
slouken@4919
    30
#include "../../events/SDL_touch_c.h"
slouken@1895
    31
jimtla@4650
    32
jimtla@4650
    33
slouken@4868
    34
/*#define WMMSG_DEBUG*/
slouken@1895
    35
#ifdef WMMSG_DEBUG
jimtla@4650
    36
#include <stdio.h>	
slouken@1895
    37
#include "wmmsg.h"
slouken@1895
    38
#endif
slouken@1895
    39
slouken@1895
    40
/* Masks for processing the windows KEYDOWN and KEYUP messages */
slouken@2317
    41
#define REPEATED_KEYMASK    (1<<30)
slouken@2317
    42
#define EXTENDED_KEYMASK    (1<<24)
slouken@1895
    43
bob@2324
    44
#define VK_ENTER    10          /* Keypad Enter ... no VKEY defined? */
slouken@2313
    45
icculus@2127
    46
/* Make sure XBUTTON stuff is defined that isn't in older Platform SDKs... */
icculus@2127
    47
#ifndef WM_XBUTTONDOWN
icculus@2127
    48
#define WM_XBUTTONDOWN 0x020B
icculus@2127
    49
#endif
icculus@2127
    50
#ifndef WM_XBUTTONUP
icculus@2127
    51
#define WM_XBUTTONUP 0x020C
icculus@2127
    52
#endif
icculus@2127
    53
#ifndef GET_XBUTTON_WPARAM
icculus@2127
    54
#define GET_XBUTTON_WPARAM(w) (HIWORD(w))
icculus@2127
    55
#endif
bobbens@2733
    56
#ifndef WM_INPUT
bobbens@2733
    57
#define WM_INPUT 0x00ff
bobbens@2733
    58
#endif
slouken@4932
    59
#ifndef WM_TOUCH
slouken@4868
    60
#define WM_TOUCH 0x0240
slouken@4932
    61
#endif
slouken@4919
    62
slouken@1895
    63
slouken@2310
    64
static WPARAM
slouken@2310
    65
RemapVKEY(WPARAM wParam, LPARAM lParam)
slouken@2310
    66
{
slouken@2317
    67
    int i;
bob@2324
    68
    BYTE scancode = (BYTE) ((lParam >> 16) & 0xFF);
slouken@2317
    69
slouken@2310
    70
    /* Windows remaps alphabetic keys based on current layout.
slouken@2310
    71
       We try to provide USB scancodes, so undo this mapping.
slouken@2310
    72
     */
slouken@2310
    73
    if (wParam >= 'A' && wParam <= 'Z') {
slouken@2311
    74
        if (scancode != alpha_scancodes[wParam - 'A']) {
slouken@2311
    75
            for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
slouken@2311
    76
                if (scancode == alpha_scancodes[i]) {
slouken@2310
    77
                    wParam = 'A' + i;
slouken@2310
    78
                    break;
slouken@2310
    79
                }
slouken@2310
    80
            }
slouken@2310
    81
        }
slouken@2310
    82
    }
slouken@2317
    83
slouken@3700
    84
    /* Keypad keys are a little trickier, we always scan for them.
slouken@3700
    85
       Keypad arrow keys have the same scancode as normal arrow keys,
slouken@3700
    86
       except they don't have the extended bit (0x1000000) set.
slouken@3700
    87
     */
slouken@3700
    88
    if (!(lParam & 0x1000000)) {
slouken@4561
    89
        if (wParam == VK_DELETE) {
slouken@4561
    90
            wParam = VK_DECIMAL;
slouken@4561
    91
        } else {
slouken@4561
    92
            for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
slouken@4561
    93
                if (scancode == keypad_scancodes[i]) {
slouken@4561
    94
                    wParam = VK_NUMPAD0 + i;
slouken@4561
    95
                    break;
slouken@4561
    96
                }
slouken@3700
    97
            }
slouken@2317
    98
        }
slouken@2317
    99
    }
slouken@2317
   100
slouken@2310
   101
    return wParam;
slouken@2310
   102
}
slouken@2310
   103
slouken@1895
   104
LRESULT CALLBACK
slouken@1895
   105
WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@1895
   106
{
slouken@1895
   107
    SDL_WindowData *data;
slouken@3566
   108
    LRESULT returnCode = -1;
slouken@1895
   109
slouken@1951
   110
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
   111
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
   112
        SDL_SysWMmsg wmmsg;
slouken@1951
   113
slouken@1951
   114
        SDL_VERSION(&wmmsg.version);
slouken@4900
   115
        wmmsg.subsystem = SDL_SYSWM_WINDOWS;
slouken@5056
   116
        wmmsg.msg.win.hwnd = hwnd;
slouken@5056
   117
        wmmsg.msg.win.msg = msg;
slouken@5056
   118
        wmmsg.msg.win.wParam = wParam;
slouken@5056
   119
        wmmsg.msg.win.lParam = lParam;
slouken@1951
   120
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   121
    }
slouken@1951
   122
slouken@1895
   123
    /* Get the window data for the window */
slouken@1895
   124
    data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
slouken@1895
   125
    if (!data) {
slouken@1895
   126
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@1895
   127
    }
jimtla@4650
   128
slouken@1895
   129
#ifdef WMMSG_DEBUG
jimtla@4650
   130
    {		
jimtla@4650
   131
        FILE *log = fopen("wmmsg.txt", "a");		
slouken@1913
   132
        fprintf(log, "Received windows message: %p ", hwnd);
slouken@1913
   133
        if (msg > MAX_WMMSG) {
slouken@1913
   134
            fprintf(log, "%d", msg);
slouken@1913
   135
        } else {
slouken@1913
   136
            fprintf(log, "%s", wmtab[msg]);
slouken@1913
   137
        }
slouken@1913
   138
        fprintf(log, " -- 0x%X, 0x%X\n", wParam, lParam);
slouken@1913
   139
        fclose(log);
slouken@1895
   140
    }
slouken@4868
   141
#endif
slouken@2710
   142
dewyatt@4752
   143
    if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata))
dewyatt@4752
   144
        return 0;
slouken@1895
   145
slouken@1895
   146
    switch (msg) {
slouken@1895
   147
slouken@1895
   148
    case WM_SHOWWINDOW:
slouken@1895
   149
        {
slouken@1895
   150
            if (wParam) {
slouken@3685
   151
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
   152
            } else {
slouken@3685
   153
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
   154
            }
slouken@1895
   155
        }
slouken@1895
   156
        break;
slouken@1895
   157
slouken@1895
   158
    case WM_ACTIVATE:
slouken@1895
   159
        {
slouken@1895
   160
            BOOL minimized;
slouken@1895
   161
slouken@1895
   162
            minimized = HIWORD(wParam);
slouken@1895
   163
            if (!minimized && (LOWORD(wParam) != WA_INACTIVE)) {
slouken@3685
   164
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   165
                SDL_SendWindowEvent(data->window,
slouken@1895
   166
                                    SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@3139
   167
#ifndef _WIN32_WCE              /* WinCE misses IsZoomed() */
slouken@1895
   168
                if (IsZoomed(hwnd)) {
slouken@3685
   169
                    SDL_SendWindowEvent(data->window,
slouken@1895
   170
                                        SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
   171
                }
slouken@3139
   172
#endif
slouken@4465
   173
                if (SDL_GetKeyboardFocus() != data->window) {
slouken@4465
   174
                    SDL_SetKeyboardFocus(data->window);
slouken@1895
   175
                }
slouken@4504
   176
                /*
slouken@4504
   177
                 * FIXME: Update keyboard state
slouken@4504
   178
                 */
slouken@4504
   179
                WIN_CheckClipboardUpdate(data->videodata);
slouken@1895
   180
            } else {
slouken@4465
   181
                if (SDL_GetKeyboardFocus() == data->window) {
slouken@4465
   182
                    SDL_SetKeyboardFocus(NULL);
slouken@1895
   183
                }
slouken@1895
   184
                if (minimized) {
slouken@3685
   185
                    SDL_SendWindowEvent(data->window,
slouken@1895
   186
                                        SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
   187
                }
slouken@1895
   188
            }
slouken@1895
   189
        }
slouken@3566
   190
        returnCode = 0;
slouken@3566
   191
        break;
slouken@1895
   192
slouken@4470
   193
	case WM_MOUSEMOVE:
slouken@3097
   194
#ifdef _WIN32_WCE
slouken@5086
   195
        /* transform coords for VGA, WVGA... */
slouken@5086
   196
        {
slouken@5086
   197
            SDL_VideoData *videodata = data->videodata;
slouken@5139
   198
            if(videodata->CoordTransform) {
slouken@5086
   199
                POINT pt;
slouken@5086
   200
                pt.x = LOWORD(lParam);
slouken@5086
   201
                pt.y = HIWORD(lParam);
slouken@5086
   202
                videodata->CoordTransform(data->window, &pt);
slouken@5139
   203
                SDL_SendMouseMotion(data->window, 0, pt.x, pt.y);
slouken@5086
   204
                break;
slouken@5086
   205
            }
slouken@5086
   206
        }
slouken@4569
   207
#endif
slouken@4484
   208
        SDL_SendMouseMotion(data->window, 0, LOWORD(lParam), HIWORD(lParam));
slouken@3139
   209
        break;
slouken@3139
   210
slouken@3097
   211
    case WM_LBUTTONDOWN:
slouken@4484
   212
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@3139
   213
        break;
slouken@3139
   214
slouken@3097
   215
    case WM_LBUTTONUP:
slouken@4484
   216
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@3139
   217
        break;
slouken@3139
   218
slouken@5049
   219
    case WM_RBUTTONDOWN:
slouken@5049
   220
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_RIGHT);
slouken@5049
   221
        break;
slouken@5049
   222
slouken@5049
   223
    case WM_RBUTTONUP:
slouken@5049
   224
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_RIGHT);
slouken@5049
   225
        break;
slouken@5049
   226
slouken@5049
   227
    case WM_MBUTTONDOWN:
slouken@5049
   228
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_MIDDLE);
slouken@5049
   229
        break;
slouken@5049
   230
slouken@5049
   231
    case WM_MBUTTONUP:
slouken@5049
   232
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_MIDDLE);
slouken@5049
   233
        break;
slouken@5049
   234
slouken@5049
   235
    case WM_XBUTTONDOWN:
slouken@5049
   236
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_X1 + GET_XBUTTON_WPARAM(wParam) - 1);
slouken@5049
   237
        returnCode = TRUE;
slouken@5049
   238
        break;
slouken@5049
   239
slouken@5049
   240
    case WM_XBUTTONUP:
slouken@5049
   241
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_X1 + GET_XBUTTON_WPARAM(wParam) - 1);
slouken@5049
   242
        returnCode = TRUE;
slouken@5049
   243
        break;
slouken@5049
   244
slouken@5049
   245
    case WM_MOUSEWHEEL:
slouken@5049
   246
        {
slouken@5049
   247
            int motion = (short) HIWORD(wParam);
slouken@5049
   248
slouken@5049
   249
            SDL_SendMouseWheel(data->window, 0, motion);
slouken@5049
   250
            break;
slouken@5049
   251
        }
slouken@5049
   252
slouken@5086
   253
#ifdef WM_MOUSELEAVE
slouken@5086
   254
    /* FIXME: Do we need the SDL 1.2 hack to generate WM_MOUSELEAVE now? */
slouken@1895
   255
    case WM_MOUSELEAVE:
slouken@4470
   256
        if (SDL_GetMouseFocus() == data->window) {
slouken@4470
   257
            SDL_SetMouseFocus(NULL);
slouken@1895
   258
        }
slouken@3566
   259
        returnCode = 0;
slouken@3566
   260
        break;
slouken@5086
   261
#endif /* WM_MOUSELEAVE */
slouken@1895
   262
slouken@1895
   263
    case WM_SYSKEYDOWN:
slouken@1895
   264
    case WM_KEYDOWN:
slouken@1895
   265
        {
slouken@2310
   266
            wParam = RemapVKEY(wParam, lParam);
slouken@1895
   267
            switch (wParam) {
slouken@1895
   268
            case VK_CONTROL:
slouken@1895
   269
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   270
                    wParam = VK_RCONTROL;
slouken@1895
   271
                else
slouken@1895
   272
                    wParam = VK_LCONTROL;
slouken@1895
   273
                break;
slouken@1895
   274
            case VK_SHIFT:
slouken@1895
   275
                /* EXTENDED trick doesn't work here */
slouken@1895
   276
                {
slouken@2308
   277
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   278
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_RELEASED
slouken@1895
   279
                        && (GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   280
                        wParam = VK_LSHIFT;
slouken@2308
   281
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_RELEASED
slouken@1895
   282
                               && (GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   283
                        wParam = VK_RSHIFT;
slouken@1895
   284
                    } else {
slouken@1895
   285
                        /* Probably a key repeat */
slouken@3566
   286
                        wParam = 256;
slouken@1895
   287
                    }
slouken@1895
   288
                }
slouken@1895
   289
                break;
slouken@1895
   290
            case VK_MENU:
slouken@1895
   291
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   292
                    wParam = VK_RMENU;
slouken@1895
   293
                else
slouken@1895
   294
                    wParam = VK_LMENU;
slouken@1895
   295
                break;
slouken@2313
   296
            case VK_RETURN:
slouken@2313
   297
                if (lParam & EXTENDED_KEYMASK)
slouken@2313
   298
                    wParam = VK_ENTER;
slouken@2313
   299
                break;
slouken@1895
   300
            }
slouken@2308
   301
            if (wParam < 256) {
slouken@4465
   302
                SDL_SendKeyboardKey(SDL_PRESSED,
slouken@2308
   303
                                    data->videodata->key_layout[wParam]);
slouken@2308
   304
            }
slouken@1895
   305
        }
slouken@3566
   306
        returnCode = 0;
slouken@3566
   307
        break;
slouken@1895
   308
slouken@1895
   309
    case WM_SYSKEYUP:
slouken@1895
   310
    case WM_KEYUP:
slouken@1895
   311
        {
slouken@2310
   312
            wParam = RemapVKEY(wParam, lParam);
slouken@1895
   313
            switch (wParam) {
slouken@1895
   314
            case VK_CONTROL:
slouken@1895
   315
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   316
                    wParam = VK_RCONTROL;
slouken@1895
   317
                else
slouken@1895
   318
                    wParam = VK_LCONTROL;
slouken@1895
   319
                break;
slouken@1895
   320
            case VK_SHIFT:
slouken@1895
   321
                /* EXTENDED trick doesn't work here */
slouken@1895
   322
                {
slouken@2308
   323
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   324
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_PRESSED
slouken@1895
   325
                        && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   326
                        wParam = VK_LSHIFT;
slouken@2308
   327
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_PRESSED
slouken@1895
   328
                               && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   329
                        wParam = VK_RSHIFT;
slouken@1895
   330
                    } else {
slouken@1895
   331
                        /* Probably a key repeat */
slouken@3566
   332
                        wParam = 256;
slouken@1895
   333
                    }
slouken@1895
   334
                }
slouken@1895
   335
                break;
slouken@1895
   336
            case VK_MENU:
slouken@1895
   337
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   338
                    wParam = VK_RMENU;
slouken@1895
   339
                else
slouken@1895
   340
                    wParam = VK_LMENU;
slouken@1895
   341
                break;
slouken@2313
   342
            case VK_RETURN:
slouken@2313
   343
                if (lParam & EXTENDED_KEYMASK)
slouken@2313
   344
                    wParam = VK_ENTER;
slouken@2313
   345
                break;
slouken@1895
   346
            }
slouken@2710
   347
slouken@1895
   348
            /* Windows only reports keyup for print screen */
slouken@1895
   349
            if (wParam == VK_SNAPSHOT
slouken@2308
   350
                && SDL_GetKeyboardState(NULL)[SDL_SCANCODE_PRINTSCREEN] ==
slouken@2308
   351
                SDL_RELEASED) {
slouken@4465
   352
                SDL_SendKeyboardKey(SDL_PRESSED,
slouken@2308
   353
                                    data->videodata->key_layout[wParam]);
slouken@1895
   354
            }
slouken@2308
   355
            if (wParam < 256) {
slouken@4465
   356
                SDL_SendKeyboardKey(SDL_RELEASED,
slouken@2308
   357
                                    data->videodata->key_layout[wParam]);
slouken@2308
   358
            }
slouken@1895
   359
        }
slouken@3566
   360
        returnCode = 0;
slouken@3566
   361
        break;
slouken@1895
   362
slouken@2309
   363
    case WM_CHAR:
slouken@2309
   364
        {
slouken@2309
   365
            char text[4];
slouken@2309
   366
slouken@2309
   367
            /* Convert to UTF-8 and send it on... */
slouken@2309
   368
            if (wParam <= 0x7F) {
slouken@2309
   369
                text[0] = (char) wParam;
slouken@2309
   370
                text[1] = '\0';
slouken@2309
   371
            } else if (wParam <= 0x7FF) {
slouken@2309
   372
                text[0] = 0xC0 | (char) ((wParam >> 6) & 0x1F);
slouken@2309
   373
                text[1] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   374
                text[2] = '\0';
slouken@2309
   375
            } else {
slouken@2309
   376
                text[0] = 0xE0 | (char) ((wParam >> 12) & 0x0F);
slouken@2309
   377
                text[1] = 0x80 | (char) ((wParam >> 6) & 0x3F);
slouken@2309
   378
                text[2] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   379
                text[3] = '\0';
slouken@2309
   380
            }
slouken@4465
   381
            SDL_SendKeyboardText(text);
slouken@2309
   382
        }
slouken@3566
   383
        returnCode = 0;
slouken@3566
   384
        break;
slouken@2309
   385
slouken@5086
   386
#ifdef WM_INPUTLANGCHANGE
slouken@2311
   387
    case WM_INPUTLANGCHANGE:
slouken@2311
   388
        {
slouken@4465
   389
            WIN_UpdateKeymap();
slouken@2311
   390
        }
slouken@3566
   391
        returnCode = 1;
slouken@3566
   392
        break;
slouken@5086
   393
#endif /* WM_INPUTLANGCHANGE */
slouken@2311
   394
slouken@5086
   395
#ifdef WM_GETMINMAXINFO
slouken@1895
   396
    case WM_GETMINMAXINFO:
slouken@1895
   397
        {
slouken@1895
   398
            MINMAXINFO *info;
slouken@1895
   399
            RECT size;
slouken@1895
   400
            int x, y;
slouken@1895
   401
            int w, h;
slouken@1895
   402
            int style;
slouken@3168
   403
            BOOL menu;
slouken@1895
   404
slouken@1895
   405
            /* If we allow resizing, let the resize happen naturally */
eligottlieb@4788
   406
            if(SDL_IsShapedWindow(data->window))
eligottlieb@4815
   407
                Win32_ResizeWindowShape(data->window);
slouken@3685
   408
            if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
slouken@3566
   409
                returnCode = 0;
slouken@3566
   410
                break;
slouken@1895
   411
            }
slouken@1895
   412
slouken@1895
   413
            /* Get the current position of our window */
slouken@1895
   414
            GetWindowRect(hwnd, &size);
slouken@1895
   415
            x = size.left;
slouken@1895
   416
            y = size.top;
slouken@1895
   417
slouken@1895
   418
            /* Calculate current size of our window */
slouken@3685
   419
            SDL_GetWindowSize(data->window, &w, &h);
slouken@1895
   420
            size.top = 0;
slouken@1895
   421
            size.left = 0;
slouken@1895
   422
            size.bottom = h;
slouken@1895
   423
            size.right = w;
slouken@1895
   424
slouken@3168
   425
slouken@3168
   426
            style = GetWindowLong(hwnd, GWL_STYLE);
slouken@3168
   427
#ifdef _WIN32_WCE
slouken@3168
   428
            menu = FALSE;
slouken@3168
   429
#else
slouken@1895
   430
            /* DJM - according to the docs for GetMenu(), the
slouken@1895
   431
               return value is undefined if hwnd is a child window.
slouken@1895
   432
               Aparently it's too difficult for MS to check
slouken@1895
   433
               inside their function, so I have to do it here.
slouken@1895
   434
             */
slouken@3168
   435
            menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   436
#endif
slouken@3168
   437
            AdjustWindowRectEx(&size, style, menu, 0);
slouken@1895
   438
            w = size.right - size.left;
slouken@1895
   439
            h = size.bottom - size.top;
slouken@1895
   440
slouken@1895
   441
            /* Fix our size to the current size */
slouken@1895
   442
            info = (MINMAXINFO *) lParam;
slouken@1895
   443
            info->ptMaxSize.x = w;
slouken@1895
   444
            info->ptMaxSize.y = h;
slouken@1895
   445
            info->ptMaxPosition.x = x;
slouken@1895
   446
            info->ptMaxPosition.y = y;
slouken@1895
   447
            info->ptMinTrackSize.x = w;
slouken@1895
   448
            info->ptMinTrackSize.y = h;
slouken@1895
   449
            info->ptMaxTrackSize.x = w;
slouken@1895
   450
            info->ptMaxTrackSize.y = h;
slouken@1895
   451
        }
slouken@3566
   452
        returnCode = 0;
slouken@3566
   453
        break;
slouken@5086
   454
#endif /* WM_GETMINMAXINFO */
slouken@1895
   455
slouken@1895
   456
    case WM_WINDOWPOSCHANGED:
slouken@1895
   457
        {
slouken@1895
   458
            RECT rect;
slouken@1895
   459
            int x, y;
slouken@1895
   460
            int w, h;
slouken@1895
   461
            Uint32 window_flags;
slouken@1895
   462
slouken@3256
   463
            if (!GetClientRect(hwnd, &rect) ||
slouken@3256
   464
                (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@3256
   465
                break;
slouken@3256
   466
            }
slouken@1895
   467
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   468
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   469
slouken@3685
   470
            window_flags = SDL_GetWindowFlags(data->window);
slouken@1895
   471
            if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   472
                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   473
                ClipCursor(&rect);
slouken@1895
   474
            }
slouken@1895
   475
slouken@1895
   476
            x = rect.left;
slouken@1895
   477
            y = rect.top;
slouken@3685
   478
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
   479
slouken@1895
   480
            w = rect.right - rect.left;
slouken@1895
   481
            h = rect.bottom - rect.top;
slouken@3685
   482
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
slouken@1895
   483
                                h);
slouken@1895
   484
        }
slouken@1895
   485
        break;
slouken@1895
   486
slouken@1895
   487
    case WM_SETCURSOR:
slouken@1895
   488
        {
slouken@3076
   489
            Uint16 hittest;
slouken@1895
   490
slouken@3076
   491
            hittest = LOWORD(lParam);
slouken@3076
   492
            if (hittest == HTCLIENT) {
slouken@5421
   493
                SetCursor(SDL_cursor);
slouken@3566
   494
                returnCode = TRUE;
slouken@3076
   495
            }
slouken@1895
   496
        }
slouken@1895
   497
        break;
slouken@1895
   498
slouken@1895
   499
        /* We were occluded, refresh our display */
slouken@1895
   500
    case WM_PAINT:
slouken@1895
   501
        {
slouken@1895
   502
            RECT rect;
slouken@1895
   503
            if (GetUpdateRect(hwnd, &rect, FALSE)) {
slouken@1895
   504
                ValidateRect(hwnd, &rect);
slouken@3685
   505
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
slouken@1895
   506
                                    0, 0);
slouken@1895
   507
            }
slouken@1895
   508
        }
slouken@3566
   509
        returnCode = 0;
slouken@3566
   510
        break;
slouken@3095
   511
slouken@1895
   512
        /* We'll do our own drawing, prevent flicker */
slouken@1895
   513
    case WM_ERASEBKGND:
slouken@1895
   514
        {
slouken@1895
   515
        }
slouken@1895
   516
        return (1);
slouken@1895
   517
slouken@5086
   518
#if defined(SC_SCREENSAVE) || defined(SC_MONITORPOWER)
slouken@1895
   519
    case WM_SYSCOMMAND:
slouken@1895
   520
        {
slouken@1895
   521
            /* Don't start the screensaver or blank the monitor in fullscreen apps */
slouken@1895
   522
            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
slouken@1895
   523
                (wParam & 0xFFF0) == SC_MONITORPOWER) {
slouken@3032
   524
                if (SDL_GetVideoDevice()->suspend_screensaver) {
slouken@1895
   525
                    return (0);
slouken@1895
   526
                }
slouken@1895
   527
            }
slouken@1895
   528
        }
slouken@1895
   529
        break;
slouken@5086
   530
#endif /* System has screensaver support */
slouken@1895
   531
slouken@1895
   532
    case WM_CLOSE:
slouken@1895
   533
        {
slouken@3685
   534
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1895
   535
        }
slouken@3566
   536
        returnCode = 0;
slouken@3566
   537
        break;
slouken@4919
   538
jimtla@4650
   539
	case WM_TOUCH:
slouken@4932
   540
		{
slouken@4932
   541
			UINT i, num_inputs = LOWORD(wParam);
slouken@4932
   542
			PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
slouken@4919
   543
			if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
slouken@4932
   544
				RECT rect;
slouken@4919
   545
				float x, y;
slouken@4919
   546
slouken@4919
   547
				if (!GetClientRect(hwnd, &rect) ||
slouken@4919
   548
				    (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@4919
   549
					break;
slouken@4919
   550
				}
slouken@4919
   551
				ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@4932
   552
				ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@4932
   553
				rect.top *= 100;
slouken@4932
   554
				rect.left *= 100;
slouken@4932
   555
				rect.bottom *= 100;
slouken@4932
   556
				rect.right *= 100;
slouken@4932
   557
slouken@4932
   558
				for (i = 0; i < num_inputs; ++i) {
slouken@4932
   559
					PTOUCHINPUT input = &inputs[i];
slouken@4919
   560
icculus@5889
   561
					const SDL_TouchID touchId = (SDL_TouchID)
icculus@5889
   562
												((size_t)input->hSource);
slouken@4919
   563
					if (!SDL_GetTouch(touchId)) {
slouken@4919
   564
						SDL_Touch touch;
slouken@4919
   565
slouken@4919
   566
						touch.id = touchId;
slouken@4919
   567
						touch.x_min = 0;
slouken@4919
   568
						touch.x_max = 1;
slouken@4919
   569
						touch.native_xres = touch.x_max - touch.x_min;
slouken@4919
   570
						touch.y_min = 0;
slouken@4919
   571
						touch.y_max = 1;
slouken@4919
   572
						touch.native_yres = touch.y_max - touch.y_min;
slouken@4919
   573
						touch.pressure_min = 0;
slouken@4919
   574
						touch.pressure_max = 1;
slouken@4919
   575
						touch.native_pressureres = touch.pressure_max - touch.pressure_min;
slouken@4919
   576
slouken@4919
   577
						if (SDL_AddTouch(&touch, "") < 0) {
slouken@4919
   578
							continue;
slouken@4919
   579
						}
slouken@4932
   580
					}
slouken@4932
   581
slouken@4932
   582
					// Get the normalized coordinates for the window
slouken@4932
   583
					x = (float)(input->x - rect.left)/(rect.right - rect.left);
slouken@4932
   584
					y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
slouken@4932
   585
slouken@4919
   586
					if (input->dwFlags & TOUCHEVENTF_DOWN) {
slouken@4932
   587
						SDL_SendFingerDown(touchId, input->dwID, SDL_TRUE, x, y, 1);
slouken@4932
   588
					}
slouken@4919
   589
					if (input->dwFlags & TOUCHEVENTF_MOVE) {
slouken@4932
   590
						SDL_SendTouchMotion(touchId, input->dwID, SDL_FALSE, x, y, 1);
slouken@4932
   591
					}
slouken@4919
   592
					if (input->dwFlags & TOUCHEVENTF_UP) {
slouken@4932
   593
						SDL_SendFingerDown(touchId, input->dwID, SDL_FALSE, x, y, 1);
slouken@4932
   594
					}
slouken@4932
   595
				}
slouken@4932
   596
			}
slouken@4932
   597
			SDL_stack_free(inputs);
slouken@4932
   598
slouken@4932
   599
			data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
slouken@4919
   600
			return 0;
jimtla@4650
   601
		}
jimtla@4650
   602
		break;
jimtla@4650
   603
	}
slouken@3566
   604
slouken@3566
   605
    /* If there's a window proc, assume it's going to handle messages */
slouken@3566
   606
    if (data->wndproc) {
slouken@3566
   607
        return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
slouken@3566
   608
    } else if (returnCode >= 0) {
slouken@3566
   609
        return returnCode;
slouken@3566
   610
    } else {
slouken@3566
   611
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@3566
   612
    }
slouken@1895
   613
}
slouken@1895
   614
slouken@1895
   615
void
slouken@1895
   616
WIN_PumpEvents(_THIS)
slouken@1895
   617
{
slouken@1895
   618
    MSG msg;
slouken@1895
   619
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
slouken@1895
   620
        TranslateMessage(&msg);
slouken@1895
   621
        DispatchMessage(&msg);
slouken@1895
   622
    }
slouken@1895
   623
}
slouken@1895
   624
slouken@1895
   625
static int app_registered = 0;
slouken@1895
   626
LPTSTR SDL_Appname = NULL;
slouken@1895
   627
Uint32 SDL_Appstyle = 0;
slouken@1895
   628
HINSTANCE SDL_Instance = NULL;
slouken@1895
   629
slouken@1895
   630
/* Register the class for this application */
slouken@1895
   631
int
slouken@1895
   632
SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@1895
   633
{
slouken@1895
   634
    WNDCLASS class;
slouken@1895
   635
slouken@1895
   636
    /* Only do this once... */
slouken@1895
   637
    if (app_registered) {
slouken@1895
   638
        ++app_registered;
slouken@1895
   639
        return (0);
slouken@1895
   640
    }
slouken@1895
   641
    if (!name && !SDL_Appname) {
slouken@1895
   642
        name = "SDL_app";
slouken@5086
   643
#if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
slouken@1895
   644
        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
slouken@5086
   645
#endif
slouken@1895
   646
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   647
    }
slouken@1895
   648
slouken@1895
   649
    if (name) {
slouken@1895
   650
        SDL_Appname = WIN_UTF8ToString(name);
slouken@1895
   651
        SDL_Appstyle = style;
slouken@1895
   652
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   653
    }
slouken@1895
   654
slouken@1895
   655
    /* Register the application class */
slouken@1895
   656
    class.hCursor = NULL;
slouken@2710
   657
    class.hIcon =
slouken@2710
   658
        LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
slouken@2710
   659
                  LR_DEFAULTCOLOR);
slouken@1895
   660
    class.lpszMenuName = NULL;
slouken@1895
   661
    class.lpszClassName = SDL_Appname;
slouken@1895
   662
    class.hbrBackground = NULL;
slouken@1895
   663
    class.hInstance = SDL_Instance;
slouken@1895
   664
    class.style = SDL_Appstyle;
dewyatt@4733
   665
    class.lpfnWndProc = WIN_WindowProc;
slouken@1895
   666
    class.cbWndExtra = 0;
slouken@1895
   667
    class.cbClsExtra = 0;
slouken@1895
   668
    if (!RegisterClass(&class)) {
slouken@1895
   669
        SDL_SetError("Couldn't register application class");
slouken@1895
   670
        return (-1);
slouken@1895
   671
    }
slouken@1895
   672
slouken@1895
   673
    app_registered = 1;
slouken@1895
   674
    return (0);
slouken@1895
   675
}
slouken@1895
   676
slouken@1895
   677
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
slouken@1895
   678
void
slouken@1895
   679
SDL_UnregisterApp()
slouken@1895
   680
{
slouken@1895
   681
    WNDCLASS class;
slouken@1895
   682
slouken@1895
   683
    /* SDL_RegisterApp might not have been called before */
slouken@1895
   684
    if (!app_registered) {
slouken@1895
   685
        return;
slouken@1895
   686
    }
slouken@1895
   687
    --app_registered;
slouken@1895
   688
    if (app_registered == 0) {
slouken@1895
   689
        /* Check for any registered window classes. */
slouken@1895
   690
        if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
slouken@1895
   691
            UnregisterClass(SDL_Appname, SDL_Instance);
slouken@1895
   692
        }
slouken@1895
   693
        SDL_free(SDL_Appname);
slouken@1895
   694
        SDL_Appname = NULL;
slouken@1895
   695
    }
slouken@1895
   696
}
slouken@1895
   697
slouken@6044
   698
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   699
slouken@1895
   700
/* vi: set ts=4 sw=4 expandtab: */