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