src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 15:46:11 -0800
changeset 5086 c2539ff054c8
parent 5062 e8916fe9cfc8
child 5139 57851a238c8f
permissions -rw-r--r--
Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@2710
    22
slouken@1895
    23
#include "SDL_config.h"
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@5086
   198
            if(videodata->CoordTransform &&
slouken@5086
   199
                (videodata->render == RENDER_GAPI || videodata->render == RENDER_RAW))
slouken@5086
   200
            {
slouken@5086
   201
                POINT pt;
slouken@5086
   202
                pt.x = LOWORD(lParam);
slouken@5086
   203
                pt.y = HIWORD(lParam);
slouken@5086
   204
                videodata->CoordTransform(data->window, &pt);
slouken@5086
   205
                    SDL_SendMouseMotion(data->window, 0, pt.x, pt.y);
slouken@5086
   206
                break;
slouken@5086
   207
            }
slouken@5086
   208
        }
slouken@4569
   209
#endif
slouken@4484
   210
        SDL_SendMouseMotion(data->window, 0, LOWORD(lParam), HIWORD(lParam));
slouken@3139
   211
        break;
slouken@3139
   212
slouken@3097
   213
    case WM_LBUTTONDOWN:
slouken@4484
   214
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@3139
   215
        break;
slouken@3139
   216
slouken@3097
   217
    case WM_LBUTTONUP:
slouken@4484
   218
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@3139
   219
        break;
slouken@3139
   220
slouken@5049
   221
    case WM_RBUTTONDOWN:
slouken@5049
   222
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_RIGHT);
slouken@5049
   223
        break;
slouken@5049
   224
slouken@5049
   225
    case WM_RBUTTONUP:
slouken@5049
   226
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_RIGHT);
slouken@5049
   227
        break;
slouken@5049
   228
slouken@5049
   229
    case WM_MBUTTONDOWN:
slouken@5049
   230
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_MIDDLE);
slouken@5049
   231
        break;
slouken@5049
   232
slouken@5049
   233
    case WM_MBUTTONUP:
slouken@5049
   234
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_MIDDLE);
slouken@5049
   235
        break;
slouken@5049
   236
slouken@5049
   237
    case WM_XBUTTONDOWN:
slouken@5049
   238
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_X1 + GET_XBUTTON_WPARAM(wParam) - 1);
slouken@5049
   239
        returnCode = TRUE;
slouken@5049
   240
        break;
slouken@5049
   241
slouken@5049
   242
    case WM_XBUTTONUP:
slouken@5049
   243
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_X1 + GET_XBUTTON_WPARAM(wParam) - 1);
slouken@5049
   244
        returnCode = TRUE;
slouken@5049
   245
        break;
slouken@5049
   246
slouken@5049
   247
    case WM_MOUSEWHEEL:
slouken@5049
   248
        {
slouken@5049
   249
            int motion = (short) HIWORD(wParam);
slouken@5049
   250
slouken@5049
   251
            SDL_SendMouseWheel(data->window, 0, motion);
slouken@5049
   252
            break;
slouken@5049
   253
        }
slouken@5049
   254
slouken@5086
   255
#ifdef WM_MOUSELEAVE
slouken@5086
   256
    /* FIXME: Do we need the SDL 1.2 hack to generate WM_MOUSELEAVE now? */
slouken@1895
   257
    case WM_MOUSELEAVE:
slouken@4470
   258
        if (SDL_GetMouseFocus() == data->window) {
slouken@4470
   259
            SDL_SetMouseFocus(NULL);
slouken@1895
   260
        }
slouken@3566
   261
        returnCode = 0;
slouken@3566
   262
        break;
slouken@5086
   263
#endif /* WM_MOUSELEAVE */
slouken@1895
   264
slouken@1895
   265
    case WM_SYSKEYDOWN:
slouken@1895
   266
    case WM_KEYDOWN:
slouken@1895
   267
        {
slouken@2310
   268
            wParam = RemapVKEY(wParam, lParam);
slouken@1895
   269
            switch (wParam) {
slouken@1895
   270
            case VK_CONTROL:
slouken@1895
   271
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   272
                    wParam = VK_RCONTROL;
slouken@1895
   273
                else
slouken@1895
   274
                    wParam = VK_LCONTROL;
slouken@1895
   275
                break;
slouken@1895
   276
            case VK_SHIFT:
slouken@1895
   277
                /* EXTENDED trick doesn't work here */
slouken@1895
   278
                {
slouken@2308
   279
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   280
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_RELEASED
slouken@1895
   281
                        && (GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   282
                        wParam = VK_LSHIFT;
slouken@2308
   283
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_RELEASED
slouken@1895
   284
                               && (GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   285
                        wParam = VK_RSHIFT;
slouken@1895
   286
                    } else {
slouken@1895
   287
                        /* Probably a key repeat */
slouken@3566
   288
                        wParam = 256;
slouken@1895
   289
                    }
slouken@1895
   290
                }
slouken@1895
   291
                break;
slouken@1895
   292
            case VK_MENU:
slouken@1895
   293
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   294
                    wParam = VK_RMENU;
slouken@1895
   295
                else
slouken@1895
   296
                    wParam = VK_LMENU;
slouken@1895
   297
                break;
slouken@2313
   298
            case VK_RETURN:
slouken@2313
   299
                if (lParam & EXTENDED_KEYMASK)
slouken@2313
   300
                    wParam = VK_ENTER;
slouken@2313
   301
                break;
slouken@1895
   302
            }
slouken@2308
   303
            if (wParam < 256) {
slouken@4465
   304
                SDL_SendKeyboardKey(SDL_PRESSED,
slouken@2308
   305
                                    data->videodata->key_layout[wParam]);
slouken@2308
   306
            }
slouken@1895
   307
        }
slouken@3566
   308
        returnCode = 0;
slouken@3566
   309
        break;
slouken@1895
   310
slouken@1895
   311
    case WM_SYSKEYUP:
slouken@1895
   312
    case WM_KEYUP:
slouken@1895
   313
        {
slouken@2310
   314
            wParam = RemapVKEY(wParam, lParam);
slouken@1895
   315
            switch (wParam) {
slouken@1895
   316
            case VK_CONTROL:
slouken@1895
   317
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   318
                    wParam = VK_RCONTROL;
slouken@1895
   319
                else
slouken@1895
   320
                    wParam = VK_LCONTROL;
slouken@1895
   321
                break;
slouken@1895
   322
            case VK_SHIFT:
slouken@1895
   323
                /* EXTENDED trick doesn't work here */
slouken@1895
   324
                {
slouken@2308
   325
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   326
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_PRESSED
slouken@1895
   327
                        && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   328
                        wParam = VK_LSHIFT;
slouken@2308
   329
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_PRESSED
slouken@1895
   330
                               && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   331
                        wParam = VK_RSHIFT;
slouken@1895
   332
                    } else {
slouken@1895
   333
                        /* Probably a key repeat */
slouken@3566
   334
                        wParam = 256;
slouken@1895
   335
                    }
slouken@1895
   336
                }
slouken@1895
   337
                break;
slouken@1895
   338
            case VK_MENU:
slouken@1895
   339
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   340
                    wParam = VK_RMENU;
slouken@1895
   341
                else
slouken@1895
   342
                    wParam = VK_LMENU;
slouken@1895
   343
                break;
slouken@2313
   344
            case VK_RETURN:
slouken@2313
   345
                if (lParam & EXTENDED_KEYMASK)
slouken@2313
   346
                    wParam = VK_ENTER;
slouken@2313
   347
                break;
slouken@1895
   348
            }
slouken@2710
   349
slouken@1895
   350
            /* Windows only reports keyup for print screen */
slouken@1895
   351
            if (wParam == VK_SNAPSHOT
slouken@2308
   352
                && SDL_GetKeyboardState(NULL)[SDL_SCANCODE_PRINTSCREEN] ==
slouken@2308
   353
                SDL_RELEASED) {
slouken@4465
   354
                SDL_SendKeyboardKey(SDL_PRESSED,
slouken@2308
   355
                                    data->videodata->key_layout[wParam]);
slouken@1895
   356
            }
slouken@2308
   357
            if (wParam < 256) {
slouken@4465
   358
                SDL_SendKeyboardKey(SDL_RELEASED,
slouken@2308
   359
                                    data->videodata->key_layout[wParam]);
slouken@2308
   360
            }
slouken@1895
   361
        }
slouken@3566
   362
        returnCode = 0;
slouken@3566
   363
        break;
slouken@1895
   364
slouken@2309
   365
    case WM_CHAR:
slouken@2309
   366
        {
slouken@2309
   367
            char text[4];
slouken@2309
   368
slouken@2309
   369
            /* Convert to UTF-8 and send it on... */
slouken@2309
   370
            if (wParam <= 0x7F) {
slouken@2309
   371
                text[0] = (char) wParam;
slouken@2309
   372
                text[1] = '\0';
slouken@2309
   373
            } else if (wParam <= 0x7FF) {
slouken@2309
   374
                text[0] = 0xC0 | (char) ((wParam >> 6) & 0x1F);
slouken@2309
   375
                text[1] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   376
                text[2] = '\0';
slouken@2309
   377
            } else {
slouken@2309
   378
                text[0] = 0xE0 | (char) ((wParam >> 12) & 0x0F);
slouken@2309
   379
                text[1] = 0x80 | (char) ((wParam >> 6) & 0x3F);
slouken@2309
   380
                text[2] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   381
                text[3] = '\0';
slouken@2309
   382
            }
slouken@4465
   383
            SDL_SendKeyboardText(text);
slouken@2309
   384
        }
slouken@3566
   385
        returnCode = 0;
slouken@3566
   386
        break;
slouken@2309
   387
slouken@5086
   388
#ifdef WM_INPUTLANGCHANGE
slouken@2311
   389
    case WM_INPUTLANGCHANGE:
slouken@2311
   390
        {
slouken@4465
   391
            WIN_UpdateKeymap();
slouken@2311
   392
        }
slouken@3566
   393
        returnCode = 1;
slouken@3566
   394
        break;
slouken@5086
   395
#endif /* WM_INPUTLANGCHANGE */
slouken@2311
   396
slouken@5086
   397
#ifdef WM_GETMINMAXINFO
slouken@1895
   398
    case WM_GETMINMAXINFO:
slouken@1895
   399
        {
slouken@1895
   400
            MINMAXINFO *info;
slouken@1895
   401
            RECT size;
slouken@1895
   402
            int x, y;
slouken@1895
   403
            int w, h;
slouken@1895
   404
            int style;
slouken@3168
   405
            BOOL menu;
slouken@1895
   406
slouken@1895
   407
            /* If we allow resizing, let the resize happen naturally */
eligottlieb@4788
   408
            if(SDL_IsShapedWindow(data->window))
eligottlieb@4815
   409
                Win32_ResizeWindowShape(data->window);
slouken@3685
   410
            if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
slouken@3566
   411
                returnCode = 0;
slouken@3566
   412
                break;
slouken@1895
   413
            }
slouken@1895
   414
slouken@1895
   415
            /* Get the current position of our window */
slouken@1895
   416
            GetWindowRect(hwnd, &size);
slouken@1895
   417
            x = size.left;
slouken@1895
   418
            y = size.top;
slouken@1895
   419
slouken@1895
   420
            /* Calculate current size of our window */
slouken@3685
   421
            SDL_GetWindowSize(data->window, &w, &h);
slouken@1895
   422
            size.top = 0;
slouken@1895
   423
            size.left = 0;
slouken@1895
   424
            size.bottom = h;
slouken@1895
   425
            size.right = w;
slouken@1895
   426
slouken@3168
   427
slouken@3168
   428
            style = GetWindowLong(hwnd, GWL_STYLE);
slouken@3168
   429
#ifdef _WIN32_WCE
slouken@3168
   430
            menu = FALSE;
slouken@3168
   431
#else
slouken@1895
   432
            /* DJM - according to the docs for GetMenu(), the
slouken@1895
   433
               return value is undefined if hwnd is a child window.
slouken@1895
   434
               Aparently it's too difficult for MS to check
slouken@1895
   435
               inside their function, so I have to do it here.
slouken@1895
   436
             */
slouken@3168
   437
            menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   438
#endif
slouken@3168
   439
            AdjustWindowRectEx(&size, style, menu, 0);
slouken@1895
   440
            w = size.right - size.left;
slouken@1895
   441
            h = size.bottom - size.top;
slouken@1895
   442
slouken@1895
   443
            /* Fix our size to the current size */
slouken@1895
   444
            info = (MINMAXINFO *) lParam;
slouken@1895
   445
            info->ptMaxSize.x = w;
slouken@1895
   446
            info->ptMaxSize.y = h;
slouken@1895
   447
            info->ptMaxPosition.x = x;
slouken@1895
   448
            info->ptMaxPosition.y = y;
slouken@1895
   449
            info->ptMinTrackSize.x = w;
slouken@1895
   450
            info->ptMinTrackSize.y = h;
slouken@1895
   451
            info->ptMaxTrackSize.x = w;
slouken@1895
   452
            info->ptMaxTrackSize.y = h;
slouken@1895
   453
        }
slouken@3566
   454
        returnCode = 0;
slouken@3566
   455
        break;
slouken@5086
   456
#endif /* WM_GETMINMAXINFO */
slouken@1895
   457
slouken@1895
   458
    case WM_WINDOWPOSCHANGED:
slouken@1895
   459
        {
slouken@1895
   460
            RECT rect;
slouken@1895
   461
            int x, y;
slouken@1895
   462
            int w, h;
slouken@1895
   463
            Uint32 window_flags;
slouken@1895
   464
slouken@3256
   465
            if (!GetClientRect(hwnd, &rect) ||
slouken@3256
   466
                (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@3256
   467
                break;
slouken@3256
   468
            }
slouken@1895
   469
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   470
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   471
slouken@3685
   472
            window_flags = SDL_GetWindowFlags(data->window);
slouken@1895
   473
            if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   474
                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   475
                ClipCursor(&rect);
slouken@1895
   476
            }
slouken@1895
   477
slouken@1895
   478
            x = rect.left;
slouken@1895
   479
            y = rect.top;
slouken@3685
   480
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
   481
slouken@1895
   482
            w = rect.right - rect.left;
slouken@1895
   483
            h = rect.bottom - rect.top;
slouken@3685
   484
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
slouken@1895
   485
                                h);
slouken@1895
   486
        }
slouken@1895
   487
        break;
slouken@1895
   488
slouken@1895
   489
    case WM_SETCURSOR:
slouken@1895
   490
        {
slouken@3076
   491
            Uint16 hittest;
slouken@1895
   492
slouken@3076
   493
            hittest = LOWORD(lParam);
slouken@3076
   494
            if (hittest == HTCLIENT) {
slouken@3076
   495
                /* FIXME: Implement the cursor API */
slouken@3076
   496
                static HCURSOR cursor;
slouken@3076
   497
                if (!cursor) {
slouken@3076
   498
                    cursor = LoadCursor(NULL, IDC_ARROW);
slouken@3076
   499
                }
slouken@3076
   500
                SetCursor(cursor);
slouken@3566
   501
                returnCode = TRUE;
slouken@3076
   502
            }
slouken@1895
   503
        }
slouken@1895
   504
        break;
slouken@1895
   505
slouken@1895
   506
        /* We are about to get palette focus! */
slouken@1895
   507
    case WM_QUERYNEWPALETTE:
slouken@1895
   508
        {
slouken@1895
   509
            /*
slouken@3566
   510
                WIN_RealizePalette(current_video);
slouken@3566
   511
                returnCode = TRUE;
slouken@1895
   512
             */
slouken@1895
   513
        }
slouken@1895
   514
        break;
slouken@1895
   515
slouken@1895
   516
        /* Another application changed the palette */
slouken@1895
   517
    case WM_PALETTECHANGED:
slouken@1895
   518
        {
slouken@1895
   519
            /*
slouken@1895
   520
               WIN_PaletteChanged(current_video, (HWND) wParam);
slouken@1895
   521
             */
slouken@1895
   522
        }
slouken@1895
   523
        break;
slouken@1895
   524
slouken@1895
   525
        /* We were occluded, refresh our display */
slouken@1895
   526
    case WM_PAINT:
slouken@1895
   527
        {
slouken@1895
   528
            RECT rect;
slouken@1895
   529
            if (GetUpdateRect(hwnd, &rect, FALSE)) {
slouken@1895
   530
                ValidateRect(hwnd, &rect);
slouken@3685
   531
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
slouken@1895
   532
                                    0, 0);
slouken@1895
   533
            }
slouken@1895
   534
        }
slouken@3566
   535
        returnCode = 0;
slouken@3566
   536
        break;
slouken@3095
   537
slouken@1895
   538
        /* We'll do our own drawing, prevent flicker */
slouken@1895
   539
    case WM_ERASEBKGND:
slouken@1895
   540
        {
slouken@1895
   541
        }
slouken@1895
   542
        return (1);
slouken@1895
   543
slouken@5086
   544
#if defined(SC_SCREENSAVE) || defined(SC_MONITORPOWER)
slouken@1895
   545
    case WM_SYSCOMMAND:
slouken@1895
   546
        {
slouken@1895
   547
            /* Don't start the screensaver or blank the monitor in fullscreen apps */
slouken@1895
   548
            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
slouken@1895
   549
                (wParam & 0xFFF0) == SC_MONITORPOWER) {
slouken@3032
   550
                if (SDL_GetVideoDevice()->suspend_screensaver) {
slouken@1895
   551
                    return (0);
slouken@1895
   552
                }
slouken@1895
   553
            }
slouken@1895
   554
        }
slouken@1895
   555
        break;
slouken@5086
   556
#endif /* System has screensaver support */
slouken@1895
   557
slouken@1895
   558
    case WM_CLOSE:
slouken@1895
   559
        {
slouken@3685
   560
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1895
   561
        }
slouken@3566
   562
        returnCode = 0;
slouken@3566
   563
        break;
slouken@4919
   564
jimtla@4650
   565
	case WM_TOUCH:
slouken@4932
   566
		{
slouken@4932
   567
			UINT i, num_inputs = LOWORD(wParam);
slouken@4932
   568
			PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
slouken@4919
   569
			if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
slouken@4932
   570
				RECT rect;
slouken@4919
   571
				float x, y;
slouken@4919
   572
slouken@4919
   573
				if (!GetClientRect(hwnd, &rect) ||
slouken@4919
   574
				    (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@4919
   575
					break;
slouken@4919
   576
				}
slouken@4919
   577
				ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@4932
   578
				ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@4932
   579
				rect.top *= 100;
slouken@4932
   580
				rect.left *= 100;
slouken@4932
   581
				rect.bottom *= 100;
slouken@4932
   582
				rect.right *= 100;
slouken@4932
   583
slouken@4932
   584
				for (i = 0; i < num_inputs; ++i) {
slouken@4932
   585
					PTOUCHINPUT input = &inputs[i];
slouken@4919
   586
slouken@4919
   587
					SDL_TouchID touchId = (SDL_TouchID)input->hSource;
slouken@4919
   588
					if (!SDL_GetTouch(touchId)) {
slouken@4919
   589
						SDL_Touch touch;
slouken@4919
   590
slouken@4919
   591
						touch.id = touchId;
slouken@4919
   592
						touch.x_min = 0;
slouken@4919
   593
						touch.x_max = 1;
slouken@4919
   594
						touch.native_xres = touch.x_max - touch.x_min;
slouken@4919
   595
						touch.y_min = 0;
slouken@4919
   596
						touch.y_max = 1;
slouken@4919
   597
						touch.native_yres = touch.y_max - touch.y_min;
slouken@4919
   598
						touch.pressure_min = 0;
slouken@4919
   599
						touch.pressure_max = 1;
slouken@4919
   600
						touch.native_pressureres = touch.pressure_max - touch.pressure_min;
slouken@4919
   601
slouken@4919
   602
						if (SDL_AddTouch(&touch, "") < 0) {
slouken@4919
   603
							continue;
slouken@4919
   604
						}
slouken@4932
   605
					}
slouken@4932
   606
slouken@4932
   607
					// Get the normalized coordinates for the window
slouken@4932
   608
					x = (float)(input->x - rect.left)/(rect.right - rect.left);
slouken@4932
   609
					y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
slouken@4932
   610
slouken@4919
   611
					if (input->dwFlags & TOUCHEVENTF_DOWN) {
slouken@4932
   612
						SDL_SendFingerDown(touchId, input->dwID, SDL_TRUE, x, y, 1);
slouken@4932
   613
					}
slouken@4919
   614
					if (input->dwFlags & TOUCHEVENTF_MOVE) {
slouken@4932
   615
						SDL_SendTouchMotion(touchId, input->dwID, SDL_FALSE, x, y, 1);
slouken@4932
   616
					}
slouken@4919
   617
					if (input->dwFlags & TOUCHEVENTF_UP) {
slouken@4932
   618
						SDL_SendFingerDown(touchId, input->dwID, SDL_FALSE, x, y, 1);
slouken@4932
   619
					}
slouken@4932
   620
				}
slouken@4932
   621
			}
slouken@4932
   622
			SDL_stack_free(inputs);
slouken@4932
   623
slouken@4932
   624
			data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
slouken@4919
   625
			return 0;
jimtla@4650
   626
		}
jimtla@4650
   627
		break;
jimtla@4650
   628
	}
slouken@3566
   629
slouken@3566
   630
    /* If there's a window proc, assume it's going to handle messages */
slouken@3566
   631
    if (data->wndproc) {
slouken@3566
   632
        return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
slouken@3566
   633
    } else if (returnCode >= 0) {
slouken@3566
   634
        return returnCode;
slouken@3566
   635
    } else {
slouken@3566
   636
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@3566
   637
    }
slouken@1895
   638
}
slouken@1895
   639
slouken@1895
   640
void
slouken@1895
   641
WIN_PumpEvents(_THIS)
slouken@1895
   642
{
slouken@1895
   643
    MSG msg;
slouken@1895
   644
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
slouken@1895
   645
        TranslateMessage(&msg);
slouken@1895
   646
        DispatchMessage(&msg);
slouken@1895
   647
    }
slouken@1895
   648
}
slouken@1895
   649
slouken@1895
   650
static int app_registered = 0;
slouken@1895
   651
LPTSTR SDL_Appname = NULL;
slouken@1895
   652
Uint32 SDL_Appstyle = 0;
slouken@1895
   653
HINSTANCE SDL_Instance = NULL;
slouken@1895
   654
slouken@1895
   655
/* Register the class for this application */
slouken@1895
   656
int
slouken@1895
   657
SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@1895
   658
{
slouken@1895
   659
    WNDCLASS class;
slouken@1895
   660
slouken@1895
   661
    /* Only do this once... */
slouken@1895
   662
    if (app_registered) {
slouken@1895
   663
        ++app_registered;
slouken@1895
   664
        return (0);
slouken@1895
   665
    }
slouken@1895
   666
    if (!name && !SDL_Appname) {
slouken@1895
   667
        name = "SDL_app";
slouken@5086
   668
#if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
slouken@1895
   669
        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
slouken@5086
   670
#endif
slouken@1895
   671
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   672
    }
slouken@1895
   673
slouken@1895
   674
    if (name) {
slouken@1895
   675
        SDL_Appname = WIN_UTF8ToString(name);
slouken@1895
   676
        SDL_Appstyle = style;
slouken@1895
   677
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   678
    }
slouken@1895
   679
slouken@1895
   680
    /* Register the application class */
slouken@1895
   681
    class.hCursor = NULL;
slouken@2710
   682
    class.hIcon =
slouken@2710
   683
        LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
slouken@2710
   684
                  LR_DEFAULTCOLOR);
slouken@1895
   685
    class.lpszMenuName = NULL;
slouken@1895
   686
    class.lpszClassName = SDL_Appname;
slouken@1895
   687
    class.hbrBackground = NULL;
slouken@1895
   688
    class.hInstance = SDL_Instance;
slouken@1895
   689
    class.style = SDL_Appstyle;
dewyatt@4733
   690
    class.lpfnWndProc = WIN_WindowProc;
slouken@1895
   691
    class.cbWndExtra = 0;
slouken@1895
   692
    class.cbClsExtra = 0;
slouken@1895
   693
    if (!RegisterClass(&class)) {
slouken@1895
   694
        SDL_SetError("Couldn't register application class");
slouken@1895
   695
        return (-1);
slouken@1895
   696
    }
slouken@1895
   697
slouken@1895
   698
    app_registered = 1;
slouken@1895
   699
    return (0);
slouken@1895
   700
}
slouken@1895
   701
slouken@1895
   702
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
slouken@1895
   703
void
slouken@1895
   704
SDL_UnregisterApp()
slouken@1895
   705
{
slouken@1895
   706
    WNDCLASS class;
slouken@1895
   707
slouken@1895
   708
    /* SDL_RegisterApp might not have been called before */
slouken@1895
   709
    if (!app_registered) {
slouken@1895
   710
        return;
slouken@1895
   711
    }
slouken@1895
   712
    --app_registered;
slouken@1895
   713
    if (app_registered == 0) {
slouken@1895
   714
        /* Check for any registered window classes. */
slouken@1895
   715
        if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
slouken@1895
   716
            UnregisterClass(SDL_Appname, SDL_Instance);
slouken@1895
   717
        }
slouken@1895
   718
        SDL_free(SDL_Appname);
slouken@1895
   719
        SDL_Appname = NULL;
slouken@1895
   720
    }
slouken@1895
   721
}
slouken@1895
   722
slouken@1895
   723
/* vi: set ts=4 sw=4 expandtab: */