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