src/video/win32/SDL_win32events.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 19 Jan 2011 22:39:02 -0800
changeset 5049 28003ba91f57
parent 4932 0d1bb1ce9d15
child 5056 8b7988f42fcb
permissions -rw-r--r--
John Wilson 2010-08-17 17:09:16 PDT

The are no event handlers for the middle mouse button, right mouse buttons, and
mouse wheel in the latest HG revision 4636. It has been like this for 3 months.
I made a patch for this, though I'm not sure if the Xbutton or mouse wheel code
is "correct" by your standards.
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@1895
    25
#include "SDL_win32video.h"
eligottlieb@4815
    26
#include "SDL_win32shape.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@4902
   116
        wmmsg.win.hwnd = hwnd;
slouken@4902
   117
        wmmsg.win.msg = msg;
slouken@4902
   118
        wmmsg.win.wParam = wParam;
slouken@4902
   119
        wmmsg.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@4569
   195
	/* transform coords for VGA, WVGA... */
slouken@4569
   196
	{
slouken@4569
   197
	    SDL_VideoData *videodata = data->videodata;
slouken@4569
   198
	    if(videodata->CoordTransform &&
slouken@4569
   199
		(videodata->render == RENDER_GAPI || videodata->render == RENDER_RAW))
slouken@4569
   200
	    {
slouken@4569
   201
		POINT pt;
slouken@4569
   202
		pt.x = LOWORD(lParam);
slouken@4569
   203
		pt.y = HIWORD(lParam);
slouken@4569
   204
		videodata->CoordTransform(data->window, &pt);
slouken@4569
   205
    		SDL_SendMouseMotion(data->window, 0, pt.x, pt.y);
slouken@4569
   206
		break;
slouken@4569
   207
	    }
slouken@4569
   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@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@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@2311
   385
    case WM_INPUTLANGCHANGE:
slouken@2311
   386
        {
slouken@4465
   387
            WIN_UpdateKeymap();
slouken@2311
   388
        }
slouken@3566
   389
        returnCode = 1;
slouken@3566
   390
        break;
slouken@2311
   391
slouken@1895
   392
    case WM_GETMINMAXINFO:
slouken@1895
   393
        {
slouken@1895
   394
            MINMAXINFO *info;
slouken@1895
   395
            RECT size;
slouken@1895
   396
            int x, y;
slouken@1895
   397
            int w, h;
slouken@1895
   398
            int style;
slouken@3168
   399
            BOOL menu;
slouken@1895
   400
slouken@1895
   401
            /* If we allow resizing, let the resize happen naturally */
eligottlieb@4788
   402
            if(SDL_IsShapedWindow(data->window))
eligottlieb@4815
   403
                Win32_ResizeWindowShape(data->window);
slouken@3685
   404
            if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
slouken@3566
   405
                returnCode = 0;
slouken@3566
   406
                break;
slouken@1895
   407
            }
slouken@1895
   408
slouken@1895
   409
            /* Get the current position of our window */
slouken@1895
   410
            GetWindowRect(hwnd, &size);
slouken@1895
   411
            x = size.left;
slouken@1895
   412
            y = size.top;
slouken@1895
   413
slouken@1895
   414
            /* Calculate current size of our window */
slouken@3685
   415
            SDL_GetWindowSize(data->window, &w, &h);
slouken@1895
   416
            size.top = 0;
slouken@1895
   417
            size.left = 0;
slouken@1895
   418
            size.bottom = h;
slouken@1895
   419
            size.right = w;
slouken@1895
   420
slouken@3168
   421
slouken@3168
   422
            style = GetWindowLong(hwnd, GWL_STYLE);
slouken@3168
   423
#ifdef _WIN32_WCE
slouken@3168
   424
            menu = FALSE;
slouken@3168
   425
#else
slouken@1895
   426
            /* DJM - according to the docs for GetMenu(), the
slouken@1895
   427
               return value is undefined if hwnd is a child window.
slouken@1895
   428
               Aparently it's too difficult for MS to check
slouken@1895
   429
               inside their function, so I have to do it here.
slouken@1895
   430
             */
slouken@3168
   431
            menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   432
#endif
slouken@3168
   433
            AdjustWindowRectEx(&size, style, menu, 0);
slouken@1895
   434
            w = size.right - size.left;
slouken@1895
   435
            h = size.bottom - size.top;
slouken@1895
   436
slouken@1895
   437
            /* Fix our size to the current size */
slouken@1895
   438
            info = (MINMAXINFO *) lParam;
slouken@1895
   439
            info->ptMaxSize.x = w;
slouken@1895
   440
            info->ptMaxSize.y = h;
slouken@1895
   441
            info->ptMaxPosition.x = x;
slouken@1895
   442
            info->ptMaxPosition.y = y;
slouken@1895
   443
            info->ptMinTrackSize.x = w;
slouken@1895
   444
            info->ptMinTrackSize.y = h;
slouken@1895
   445
            info->ptMaxTrackSize.x = w;
slouken@1895
   446
            info->ptMaxTrackSize.y = h;
slouken@1895
   447
        }
slouken@3566
   448
        returnCode = 0;
slouken@3566
   449
        break;
slouken@1895
   450
slouken@1895
   451
    case WM_WINDOWPOSCHANGED:
slouken@1895
   452
        {
slouken@1895
   453
            RECT rect;
slouken@1895
   454
            int x, y;
slouken@1895
   455
            int w, h;
slouken@1895
   456
            Uint32 window_flags;
slouken@1895
   457
slouken@3256
   458
            if (!GetClientRect(hwnd, &rect) ||
slouken@3256
   459
                (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@3256
   460
                break;
slouken@3256
   461
            }
slouken@1895
   462
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   463
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   464
slouken@3685
   465
            window_flags = SDL_GetWindowFlags(data->window);
slouken@1895
   466
            if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   467
                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   468
                ClipCursor(&rect);
slouken@1895
   469
            }
slouken@1895
   470
slouken@1895
   471
            x = rect.left;
slouken@1895
   472
            y = rect.top;
slouken@3685
   473
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
   474
slouken@1895
   475
            w = rect.right - rect.left;
slouken@1895
   476
            h = rect.bottom - rect.top;
slouken@3685
   477
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
slouken@1895
   478
                                h);
slouken@1895
   479
        }
slouken@1895
   480
        break;
slouken@1895
   481
slouken@1895
   482
    case WM_SETCURSOR:
slouken@1895
   483
        {
slouken@3076
   484
            Uint16 hittest;
slouken@1895
   485
slouken@3076
   486
            hittest = LOWORD(lParam);
slouken@3076
   487
            if (hittest == HTCLIENT) {
slouken@3076
   488
                /* FIXME: Implement the cursor API */
slouken@3076
   489
                static HCURSOR cursor;
slouken@3076
   490
                if (!cursor) {
slouken@3076
   491
                    cursor = LoadCursor(NULL, IDC_ARROW);
slouken@3076
   492
                }
slouken@3076
   493
                SetCursor(cursor);
slouken@3566
   494
                returnCode = TRUE;
slouken@3076
   495
            }
slouken@1895
   496
        }
slouken@1895
   497
        break;
slouken@1895
   498
slouken@1895
   499
        /* We are about to get palette focus! */
slouken@1895
   500
    case WM_QUERYNEWPALETTE:
slouken@1895
   501
        {
slouken@1895
   502
            /*
slouken@3566
   503
                WIN_RealizePalette(current_video);
slouken@3566
   504
                returnCode = TRUE;
slouken@1895
   505
             */
slouken@1895
   506
        }
slouken@1895
   507
        break;
slouken@1895
   508
slouken@1895
   509
        /* Another application changed the palette */
slouken@1895
   510
    case WM_PALETTECHANGED:
slouken@1895
   511
        {
slouken@1895
   512
            /*
slouken@1895
   513
               WIN_PaletteChanged(current_video, (HWND) wParam);
slouken@1895
   514
             */
slouken@1895
   515
        }
slouken@1895
   516
        break;
slouken@1895
   517
slouken@1895
   518
        /* We were occluded, refresh our display */
slouken@1895
   519
    case WM_PAINT:
slouken@1895
   520
        {
slouken@1895
   521
            RECT rect;
slouken@1895
   522
            if (GetUpdateRect(hwnd, &rect, FALSE)) {
slouken@1895
   523
                ValidateRect(hwnd, &rect);
slouken@3685
   524
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
slouken@1895
   525
                                    0, 0);
slouken@1895
   526
            }
slouken@1895
   527
        }
slouken@3566
   528
        returnCode = 0;
slouken@3566
   529
        break;
slouken@3095
   530
slouken@1895
   531
        /* We'll do our own drawing, prevent flicker */
slouken@1895
   532
    case WM_ERASEBKGND:
slouken@1895
   533
        {
slouken@1895
   534
        }
slouken@1895
   535
        return (1);
slouken@1895
   536
slouken@1895
   537
    case WM_SYSCOMMAND:
slouken@1895
   538
        {
slouken@1895
   539
            /* Don't start the screensaver or blank the monitor in fullscreen apps */
slouken@1895
   540
            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
slouken@1895
   541
                (wParam & 0xFFF0) == SC_MONITORPOWER) {
slouken@3032
   542
                if (SDL_GetVideoDevice()->suspend_screensaver) {
slouken@1895
   543
                    return (0);
slouken@1895
   544
                }
slouken@1895
   545
            }
slouken@1895
   546
        }
slouken@1895
   547
        break;
slouken@1895
   548
slouken@1895
   549
    case WM_CLOSE:
slouken@1895
   550
        {
slouken@3685
   551
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1895
   552
        }
slouken@3566
   553
        returnCode = 0;
slouken@3566
   554
        break;
slouken@4919
   555
jimtla@4650
   556
	case WM_TOUCH:
slouken@4932
   557
		{
slouken@4932
   558
			UINT i, num_inputs = LOWORD(wParam);
slouken@4932
   559
			PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
slouken@4919
   560
			if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
slouken@4932
   561
				RECT rect;
slouken@4919
   562
				float x, y;
slouken@4919
   563
slouken@4919
   564
				if (!GetClientRect(hwnd, &rect) ||
slouken@4919
   565
				    (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@4919
   566
					break;
slouken@4919
   567
				}
slouken@4919
   568
				ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@4932
   569
				ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@4932
   570
				rect.top *= 100;
slouken@4932
   571
				rect.left *= 100;
slouken@4932
   572
				rect.bottom *= 100;
slouken@4932
   573
				rect.right *= 100;
slouken@4932
   574
slouken@4932
   575
				for (i = 0; i < num_inputs; ++i) {
slouken@4932
   576
					PTOUCHINPUT input = &inputs[i];
slouken@4919
   577
slouken@4919
   578
					SDL_TouchID touchId = (SDL_TouchID)input->hSource;
slouken@4919
   579
					if (!SDL_GetTouch(touchId)) {
slouken@4919
   580
						SDL_Touch touch;
slouken@4919
   581
slouken@4919
   582
						touch.id = touchId;
slouken@4919
   583
						touch.x_min = 0;
slouken@4919
   584
						touch.x_max = 1;
slouken@4919
   585
						touch.native_xres = touch.x_max - touch.x_min;
slouken@4919
   586
						touch.y_min = 0;
slouken@4919
   587
						touch.y_max = 1;
slouken@4919
   588
						touch.native_yres = touch.y_max - touch.y_min;
slouken@4919
   589
						touch.pressure_min = 0;
slouken@4919
   590
						touch.pressure_max = 1;
slouken@4919
   591
						touch.native_pressureres = touch.pressure_max - touch.pressure_min;
slouken@4919
   592
slouken@4919
   593
						if (SDL_AddTouch(&touch, "") < 0) {
slouken@4919
   594
							continue;
slouken@4919
   595
						}
slouken@4932
   596
					}
slouken@4932
   597
slouken@4932
   598
					// Get the normalized coordinates for the window
slouken@4932
   599
					x = (float)(input->x - rect.left)/(rect.right - rect.left);
slouken@4932
   600
					y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
slouken@4932
   601
slouken@4919
   602
					if (input->dwFlags & TOUCHEVENTF_DOWN) {
slouken@4932
   603
						SDL_SendFingerDown(touchId, input->dwID, SDL_TRUE, x, y, 1);
slouken@4932
   604
					}
slouken@4919
   605
					if (input->dwFlags & TOUCHEVENTF_MOVE) {
slouken@4932
   606
						SDL_SendTouchMotion(touchId, input->dwID, SDL_FALSE, x, y, 1);
slouken@4932
   607
					}
slouken@4919
   608
					if (input->dwFlags & TOUCHEVENTF_UP) {
slouken@4932
   609
						SDL_SendFingerDown(touchId, input->dwID, SDL_FALSE, x, y, 1);
slouken@4932
   610
					}
slouken@4932
   611
				}
slouken@4932
   612
			}
slouken@4932
   613
			SDL_stack_free(inputs);
slouken@4932
   614
slouken@4932
   615
			data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
slouken@4919
   616
			return 0;
jimtla@4650
   617
		}
jimtla@4650
   618
		break;
jimtla@4650
   619
	}
slouken@3566
   620
slouken@3566
   621
    /* If there's a window proc, assume it's going to handle messages */
slouken@3566
   622
    if (data->wndproc) {
slouken@3566
   623
        return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
slouken@3566
   624
    } else if (returnCode >= 0) {
slouken@3566
   625
        return returnCode;
slouken@3566
   626
    } else {
slouken@3566
   627
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@3566
   628
    }
slouken@1895
   629
}
slouken@1895
   630
slouken@1895
   631
void
slouken@1895
   632
WIN_PumpEvents(_THIS)
slouken@1895
   633
{
slouken@1895
   634
    MSG msg;
slouken@1895
   635
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
slouken@1895
   636
        TranslateMessage(&msg);
slouken@1895
   637
        DispatchMessage(&msg);
slouken@1895
   638
    }
slouken@1895
   639
}
slouken@1895
   640
slouken@1895
   641
static int app_registered = 0;
slouken@1895
   642
LPTSTR SDL_Appname = NULL;
slouken@1895
   643
Uint32 SDL_Appstyle = 0;
slouken@1895
   644
HINSTANCE SDL_Instance = NULL;
slouken@1895
   645
slouken@1895
   646
/* Register the class for this application */
slouken@1895
   647
int
slouken@1895
   648
SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@1895
   649
{
slouken@1895
   650
    WNDCLASS class;
slouken@1895
   651
slouken@1895
   652
    /* Only do this once... */
slouken@1895
   653
    if (app_registered) {
slouken@1895
   654
        ++app_registered;
slouken@1895
   655
        return (0);
slouken@1895
   656
    }
slouken@1895
   657
    if (!name && !SDL_Appname) {
slouken@1895
   658
        name = "SDL_app";
slouken@1895
   659
        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
slouken@1895
   660
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   661
    }
slouken@1895
   662
slouken@1895
   663
    if (name) {
slouken@1895
   664
        SDL_Appname = WIN_UTF8ToString(name);
slouken@1895
   665
        SDL_Appstyle = style;
slouken@1895
   666
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   667
    }
slouken@1895
   668
slouken@1895
   669
    /* Register the application class */
slouken@1895
   670
    class.hCursor = NULL;
slouken@2710
   671
    class.hIcon =
slouken@2710
   672
        LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
slouken@2710
   673
                  LR_DEFAULTCOLOR);
slouken@1895
   674
    class.lpszMenuName = NULL;
slouken@1895
   675
    class.lpszClassName = SDL_Appname;
slouken@1895
   676
    class.hbrBackground = NULL;
slouken@1895
   677
    class.hInstance = SDL_Instance;
slouken@1895
   678
    class.style = SDL_Appstyle;
dewyatt@4733
   679
    class.lpfnWndProc = WIN_WindowProc;
slouken@1895
   680
    class.cbWndExtra = 0;
slouken@1895
   681
    class.cbClsExtra = 0;
slouken@1895
   682
    if (!RegisterClass(&class)) {
slouken@1895
   683
        SDL_SetError("Couldn't register application class");
slouken@1895
   684
        return (-1);
slouken@1895
   685
    }
slouken@1895
   686
slouken@1895
   687
    app_registered = 1;
slouken@1895
   688
    return (0);
slouken@1895
   689
}
slouken@1895
   690
slouken@1895
   691
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
slouken@1895
   692
void
slouken@1895
   693
SDL_UnregisterApp()
slouken@1895
   694
{
slouken@1895
   695
    WNDCLASS class;
slouken@1895
   696
slouken@1895
   697
    /* SDL_RegisterApp might not have been called before */
slouken@1895
   698
    if (!app_registered) {
slouken@1895
   699
        return;
slouken@1895
   700
    }
slouken@1895
   701
    --app_registered;
slouken@1895
   702
    if (app_registered == 0) {
slouken@1895
   703
        /* Check for any registered window classes. */
slouken@1895
   704
        if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
slouken@1895
   705
            UnregisterClass(SDL_Appname, SDL_Instance);
slouken@1895
   706
        }
slouken@1895
   707
        SDL_free(SDL_Appname);
slouken@1895
   708
        SDL_Appname = NULL;
slouken@1895
   709
    }
slouken@1895
   710
}
slouken@1895
   711
slouken@1895
   712
/* vi: set ts=4 sw=4 expandtab: */