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