src/video/win32/SDL_win32events.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 08 Jul 2010 23:07:34 -0700
changeset 4504 9faebccfefb3
parent 4500 eff4e88cc1e8
child 4560 95352c671a6e
child 4661 03dcb795c583
child 4755 436183eb30c8
permissions -rwxr-xr-x
Amazingly the Windows code is almost identical to the Mac OS X code. :)
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@4504
   165
                /*
slouken@4504
   166
                 * FIXME: Update keyboard state
slouken@4504
   167
                 */
slouken@4504
   168
                WIN_CheckClipboardUpdate(data->videodata);
slouken@1895
   169
            } else {
slouken@4465
   170
                if (SDL_GetKeyboardFocus() == data->window) {
slouken@4465
   171
                    SDL_SetKeyboardFocus(NULL);
slouken@1895
   172
                }
slouken@1895
   173
                if (minimized) {
slouken@3685
   174
                    SDL_SendWindowEvent(data->window,
slouken@1895
   175
                                        SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
   176
                }
slouken@1895
   177
            }
slouken@1895
   178
        }
slouken@3566
   179
        returnCode = 0;
slouken@3566
   180
        break;
slouken@1895
   181
slouken@4470
   182
	case WM_MOUSEMOVE:
slouken@4484
   183
        SDL_SendMouseMotion(data->window, 0, LOWORD(lParam), HIWORD(lParam));
slouken@3139
   184
        break;
slouken@3139
   185
slouken@3097
   186
    case WM_LBUTTONDOWN:
slouken@4484
   187
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@3139
   188
        break;
slouken@3139
   189
slouken@3097
   190
    case WM_LBUTTONUP:
slouken@4484
   191
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@3139
   192
        break;
slouken@3139
   193
slouken@1895
   194
    case WM_MOUSELEAVE:
slouken@4470
   195
        if (SDL_GetMouseFocus() == data->window) {
slouken@4470
   196
            SDL_SetMouseFocus(NULL);
slouken@1895
   197
        }
slouken@3566
   198
        returnCode = 0;
slouken@3566
   199
        break;
slouken@1895
   200
slouken@1895
   201
    case WM_SYSKEYDOWN:
slouken@1895
   202
    case WM_KEYDOWN:
slouken@1895
   203
        {
slouken@1895
   204
            /* Ignore repeated keys */
slouken@1895
   205
            if (lParam & REPEATED_KEYMASK) {
slouken@3566
   206
                returnCode = 0;
slouken@3566
   207
                break;
slouken@1895
   208
            }
slouken@1895
   209
slouken@2310
   210
            wParam = RemapVKEY(wParam, lParam);
slouken@1895
   211
            switch (wParam) {
slouken@1895
   212
            case VK_CONTROL:
slouken@1895
   213
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   214
                    wParam = VK_RCONTROL;
slouken@1895
   215
                else
slouken@1895
   216
                    wParam = VK_LCONTROL;
slouken@1895
   217
                break;
slouken@1895
   218
            case VK_SHIFT:
slouken@1895
   219
                /* EXTENDED trick doesn't work here */
slouken@1895
   220
                {
slouken@2308
   221
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   222
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_RELEASED
slouken@1895
   223
                        && (GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   224
                        wParam = VK_LSHIFT;
slouken@2308
   225
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_RELEASED
slouken@1895
   226
                               && (GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   227
                        wParam = VK_RSHIFT;
slouken@1895
   228
                    } else {
slouken@1895
   229
                        /* Probably a key repeat */
slouken@3566
   230
                        wParam = 256;
slouken@1895
   231
                    }
slouken@1895
   232
                }
slouken@1895
   233
                break;
slouken@1895
   234
            case VK_MENU:
slouken@1895
   235
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   236
                    wParam = VK_RMENU;
slouken@1895
   237
                else
slouken@1895
   238
                    wParam = VK_LMENU;
slouken@1895
   239
                break;
slouken@2313
   240
            case VK_RETURN:
slouken@2313
   241
                if (lParam & EXTENDED_KEYMASK)
slouken@2313
   242
                    wParam = VK_ENTER;
slouken@2313
   243
                break;
slouken@1895
   244
            }
slouken@2308
   245
            if (wParam < 256) {
slouken@4465
   246
                SDL_SendKeyboardKey(SDL_PRESSED,
slouken@2308
   247
                                    data->videodata->key_layout[wParam]);
slouken@2308
   248
            }
slouken@1895
   249
        }
slouken@3566
   250
        returnCode = 0;
slouken@3566
   251
        break;
slouken@1895
   252
slouken@1895
   253
    case WM_SYSKEYUP:
slouken@1895
   254
    case WM_KEYUP:
slouken@1895
   255
        {
slouken@2310
   256
            wParam = RemapVKEY(wParam, lParam);
slouken@1895
   257
            switch (wParam) {
slouken@1895
   258
            case VK_CONTROL:
slouken@1895
   259
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   260
                    wParam = VK_RCONTROL;
slouken@1895
   261
                else
slouken@1895
   262
                    wParam = VK_LCONTROL;
slouken@1895
   263
                break;
slouken@1895
   264
            case VK_SHIFT:
slouken@1895
   265
                /* EXTENDED trick doesn't work here */
slouken@1895
   266
                {
slouken@2308
   267
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   268
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_PRESSED
slouken@1895
   269
                        && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   270
                        wParam = VK_LSHIFT;
slouken@2308
   271
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_PRESSED
slouken@1895
   272
                               && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   273
                        wParam = VK_RSHIFT;
slouken@1895
   274
                    } else {
slouken@1895
   275
                        /* Probably a key repeat */
slouken@3566
   276
                        wParam = 256;
slouken@1895
   277
                    }
slouken@1895
   278
                }
slouken@1895
   279
                break;
slouken@1895
   280
            case VK_MENU:
slouken@1895
   281
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   282
                    wParam = VK_RMENU;
slouken@1895
   283
                else
slouken@1895
   284
                    wParam = VK_LMENU;
slouken@1895
   285
                break;
slouken@2313
   286
            case VK_RETURN:
slouken@2313
   287
                if (lParam & EXTENDED_KEYMASK)
slouken@2313
   288
                    wParam = VK_ENTER;
slouken@2313
   289
                break;
slouken@1895
   290
            }
slouken@2710
   291
slouken@1895
   292
            /* Windows only reports keyup for print screen */
slouken@1895
   293
            if (wParam == VK_SNAPSHOT
slouken@2308
   294
                && SDL_GetKeyboardState(NULL)[SDL_SCANCODE_PRINTSCREEN] ==
slouken@2308
   295
                SDL_RELEASED) {
slouken@4465
   296
                SDL_SendKeyboardKey(SDL_PRESSED,
slouken@2308
   297
                                    data->videodata->key_layout[wParam]);
slouken@1895
   298
            }
slouken@2308
   299
            if (wParam < 256) {
slouken@4465
   300
                SDL_SendKeyboardKey(SDL_RELEASED,
slouken@2308
   301
                                    data->videodata->key_layout[wParam]);
slouken@2308
   302
            }
slouken@1895
   303
        }
slouken@3566
   304
        returnCode = 0;
slouken@3566
   305
        break;
slouken@1895
   306
slouken@2309
   307
    case WM_CHAR:
slouken@2309
   308
        {
slouken@2309
   309
            char text[4];
slouken@2309
   310
slouken@2309
   311
            /* Convert to UTF-8 and send it on... */
slouken@2309
   312
            if (wParam <= 0x7F) {
slouken@2309
   313
                text[0] = (char) wParam;
slouken@2309
   314
                text[1] = '\0';
slouken@2309
   315
            } else if (wParam <= 0x7FF) {
slouken@2309
   316
                text[0] = 0xC0 | (char) ((wParam >> 6) & 0x1F);
slouken@2309
   317
                text[1] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   318
                text[2] = '\0';
slouken@2309
   319
            } else {
slouken@2309
   320
                text[0] = 0xE0 | (char) ((wParam >> 12) & 0x0F);
slouken@2309
   321
                text[1] = 0x80 | (char) ((wParam >> 6) & 0x3F);
slouken@2309
   322
                text[2] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   323
                text[3] = '\0';
slouken@2309
   324
            }
slouken@4465
   325
            SDL_SendKeyboardText(text);
slouken@2309
   326
        }
slouken@3566
   327
        returnCode = 0;
slouken@3566
   328
        break;
slouken@2309
   329
slouken@2311
   330
    case WM_INPUTLANGCHANGE:
slouken@2311
   331
        {
slouken@4465
   332
            WIN_UpdateKeymap();
slouken@2311
   333
        }
slouken@3566
   334
        returnCode = 1;
slouken@3566
   335
        break;
slouken@2311
   336
slouken@1895
   337
    case WM_GETMINMAXINFO:
slouken@1895
   338
        {
slouken@1895
   339
            MINMAXINFO *info;
slouken@1895
   340
            RECT size;
slouken@1895
   341
            int x, y;
slouken@1895
   342
            int w, h;
slouken@1895
   343
            int style;
slouken@3168
   344
            BOOL menu;
slouken@1895
   345
slouken@1895
   346
            /* If we allow resizing, let the resize happen naturally */
slouken@3685
   347
            if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
slouken@3566
   348
                returnCode = 0;
slouken@3566
   349
                break;
slouken@1895
   350
            }
slouken@1895
   351
slouken@1895
   352
            /* Get the current position of our window */
slouken@1895
   353
            GetWindowRect(hwnd, &size);
slouken@1895
   354
            x = size.left;
slouken@1895
   355
            y = size.top;
slouken@1895
   356
slouken@1895
   357
            /* Calculate current size of our window */
slouken@3685
   358
            SDL_GetWindowSize(data->window, &w, &h);
slouken@1895
   359
            size.top = 0;
slouken@1895
   360
            size.left = 0;
slouken@1895
   361
            size.bottom = h;
slouken@1895
   362
            size.right = w;
slouken@1895
   363
slouken@3168
   364
slouken@3168
   365
            style = GetWindowLong(hwnd, GWL_STYLE);
slouken@3168
   366
#ifdef _WIN32_WCE
slouken@3168
   367
            menu = FALSE;
slouken@3168
   368
#else
slouken@1895
   369
            /* DJM - according to the docs for GetMenu(), the
slouken@1895
   370
               return value is undefined if hwnd is a child window.
slouken@1895
   371
               Aparently it's too difficult for MS to check
slouken@1895
   372
               inside their function, so I have to do it here.
slouken@1895
   373
             */
slouken@3168
   374
            menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   375
#endif
slouken@3168
   376
            AdjustWindowRectEx(&size, style, menu, 0);
slouken@1895
   377
            w = size.right - size.left;
slouken@1895
   378
            h = size.bottom - size.top;
slouken@1895
   379
slouken@1895
   380
            /* Fix our size to the current size */
slouken@1895
   381
            info = (MINMAXINFO *) lParam;
slouken@1895
   382
            info->ptMaxSize.x = w;
slouken@1895
   383
            info->ptMaxSize.y = h;
slouken@1895
   384
            info->ptMaxPosition.x = x;
slouken@1895
   385
            info->ptMaxPosition.y = y;
slouken@1895
   386
            info->ptMinTrackSize.x = w;
slouken@1895
   387
            info->ptMinTrackSize.y = h;
slouken@1895
   388
            info->ptMaxTrackSize.x = w;
slouken@1895
   389
            info->ptMaxTrackSize.y = h;
slouken@1895
   390
        }
slouken@3566
   391
        returnCode = 0;
slouken@3566
   392
        break;
slouken@1895
   393
slouken@1895
   394
    case WM_WINDOWPOSCHANGED:
slouken@1895
   395
        {
slouken@1895
   396
            RECT rect;
slouken@1895
   397
            int x, y;
slouken@1895
   398
            int w, h;
slouken@1895
   399
            Uint32 window_flags;
slouken@1895
   400
slouken@3256
   401
            if (!GetClientRect(hwnd, &rect) ||
slouken@3256
   402
                (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@3256
   403
                break;
slouken@3256
   404
            }
slouken@1895
   405
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   406
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   407
slouken@3685
   408
            window_flags = SDL_GetWindowFlags(data->window);
slouken@1895
   409
            if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   410
                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   411
                ClipCursor(&rect);
slouken@1895
   412
            }
slouken@1895
   413
slouken@1895
   414
            x = rect.left;
slouken@1895
   415
            y = rect.top;
slouken@3685
   416
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
   417
slouken@1895
   418
            w = rect.right - rect.left;
slouken@1895
   419
            h = rect.bottom - rect.top;
slouken@3685
   420
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
slouken@1895
   421
                                h);
slouken@1895
   422
        }
slouken@1895
   423
        break;
slouken@1895
   424
slouken@1895
   425
    case WM_SETCURSOR:
slouken@1895
   426
        {
slouken@3076
   427
            Uint16 hittest;
slouken@1895
   428
slouken@3076
   429
            hittest = LOWORD(lParam);
slouken@3076
   430
            if (hittest == HTCLIENT) {
slouken@3076
   431
                /* FIXME: Implement the cursor API */
slouken@3076
   432
                static HCURSOR cursor;
slouken@3076
   433
                if (!cursor) {
slouken@3076
   434
                    cursor = LoadCursor(NULL, IDC_ARROW);
slouken@3076
   435
                }
slouken@3076
   436
                SetCursor(cursor);
slouken@3566
   437
                returnCode = TRUE;
slouken@3076
   438
            }
slouken@1895
   439
        }
slouken@1895
   440
        break;
slouken@1895
   441
slouken@1895
   442
        /* We are about to get palette focus! */
slouken@1895
   443
    case WM_QUERYNEWPALETTE:
slouken@1895
   444
        {
slouken@1895
   445
            /*
slouken@3566
   446
                WIN_RealizePalette(current_video);
slouken@3566
   447
                returnCode = TRUE;
slouken@1895
   448
             */
slouken@1895
   449
        }
slouken@1895
   450
        break;
slouken@1895
   451
slouken@1895
   452
        /* Another application changed the palette */
slouken@1895
   453
    case WM_PALETTECHANGED:
slouken@1895
   454
        {
slouken@1895
   455
            /*
slouken@1895
   456
               WIN_PaletteChanged(current_video, (HWND) wParam);
slouken@1895
   457
             */
slouken@1895
   458
        }
slouken@1895
   459
        break;
slouken@1895
   460
slouken@1895
   461
        /* We were occluded, refresh our display */
slouken@1895
   462
    case WM_PAINT:
slouken@1895
   463
        {
slouken@1895
   464
            RECT rect;
slouken@1895
   465
            if (GetUpdateRect(hwnd, &rect, FALSE)) {
slouken@1895
   466
                ValidateRect(hwnd, &rect);
slouken@3685
   467
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
slouken@1895
   468
                                    0, 0);
slouken@1895
   469
            }
slouken@1895
   470
        }
slouken@3566
   471
        returnCode = 0;
slouken@3566
   472
        break;
slouken@3095
   473
slouken@1895
   474
        /* We'll do our own drawing, prevent flicker */
slouken@1895
   475
    case WM_ERASEBKGND:
slouken@1895
   476
        {
slouken@1895
   477
        }
slouken@1895
   478
        return (1);
slouken@1895
   479
slouken@1895
   480
    case WM_SYSCOMMAND:
slouken@1895
   481
        {
slouken@1895
   482
            /* Don't start the screensaver or blank the monitor in fullscreen apps */
slouken@1895
   483
            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
slouken@1895
   484
                (wParam & 0xFFF0) == SC_MONITORPOWER) {
slouken@3032
   485
                if (SDL_GetVideoDevice()->suspend_screensaver) {
slouken@1895
   486
                    return (0);
slouken@1895
   487
                }
slouken@1895
   488
            }
slouken@1895
   489
        }
slouken@1895
   490
        break;
slouken@1895
   491
slouken@1895
   492
    case WM_CLOSE:
slouken@1895
   493
        {
slouken@3685
   494
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1895
   495
        }
slouken@3566
   496
        returnCode = 0;
slouken@3566
   497
        break;
slouken@1895
   498
    }
slouken@3566
   499
slouken@3566
   500
    /* If there's a window proc, assume it's going to handle messages */
slouken@3566
   501
    if (data->wndproc) {
slouken@3566
   502
        return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
slouken@3566
   503
    } else if (returnCode >= 0) {
slouken@3566
   504
        return returnCode;
slouken@3566
   505
    } else {
slouken@3566
   506
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@3566
   507
    }
slouken@1895
   508
}
slouken@1895
   509
slouken@1895
   510
void
slouken@1895
   511
WIN_PumpEvents(_THIS)
slouken@1895
   512
{
slouken@1895
   513
    MSG msg;
slouken@1895
   514
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
slouken@1895
   515
        TranslateMessage(&msg);
slouken@1895
   516
        DispatchMessage(&msg);
slouken@1895
   517
    }
slouken@1895
   518
}
slouken@1895
   519
slouken@1895
   520
static int app_registered = 0;
slouken@1895
   521
LPTSTR SDL_Appname = NULL;
slouken@1895
   522
Uint32 SDL_Appstyle = 0;
slouken@1895
   523
HINSTANCE SDL_Instance = NULL;
slouken@1895
   524
slouken@1895
   525
/* Register the class for this application */
slouken@1895
   526
int
slouken@1895
   527
SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@1895
   528
{
slouken@1895
   529
    WNDCLASS class;
slouken@1895
   530
slouken@1895
   531
    /* Only do this once... */
slouken@1895
   532
    if (app_registered) {
slouken@1895
   533
        ++app_registered;
slouken@1895
   534
        return (0);
slouken@1895
   535
    }
slouken@1895
   536
    if (!name && !SDL_Appname) {
slouken@1895
   537
        name = "SDL_app";
slouken@1895
   538
        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
slouken@1895
   539
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   540
    }
slouken@1895
   541
slouken@1895
   542
    if (name) {
slouken@1895
   543
        SDL_Appname = WIN_UTF8ToString(name);
slouken@1895
   544
        SDL_Appstyle = style;
slouken@1895
   545
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   546
    }
slouken@1895
   547
slouken@1895
   548
    /* Register the application class */
slouken@1895
   549
    class.hCursor = NULL;
slouken@2710
   550
    class.hIcon =
slouken@2710
   551
        LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
slouken@2710
   552
                  LR_DEFAULTCOLOR);
slouken@1895
   553
    class.lpszMenuName = NULL;
slouken@1895
   554
    class.lpszClassName = SDL_Appname;
slouken@1895
   555
    class.hbrBackground = NULL;
slouken@1895
   556
    class.hInstance = SDL_Instance;
slouken@1895
   557
    class.style = SDL_Appstyle;
slouken@1895
   558
    class.lpfnWndProc = DefWindowProc;
slouken@1895
   559
    class.cbWndExtra = 0;
slouken@1895
   560
    class.cbClsExtra = 0;
slouken@1895
   561
    if (!RegisterClass(&class)) {
slouken@1895
   562
        SDL_SetError("Couldn't register application class");
slouken@1895
   563
        return (-1);
slouken@1895
   564
    }
slouken@1895
   565
slouken@1895
   566
    app_registered = 1;
slouken@1895
   567
    return (0);
slouken@1895
   568
}
slouken@1895
   569
slouken@1895
   570
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
slouken@1895
   571
void
slouken@1895
   572
SDL_UnregisterApp()
slouken@1895
   573
{
slouken@1895
   574
    WNDCLASS class;
slouken@1895
   575
slouken@1895
   576
    /* SDL_RegisterApp might not have been called before */
slouken@1895
   577
    if (!app_registered) {
slouken@1895
   578
        return;
slouken@1895
   579
    }
slouken@1895
   580
    --app_registered;
slouken@1895
   581
    if (app_registered == 0) {
slouken@1895
   582
        /* Check for any registered window classes. */
slouken@1895
   583
        if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
slouken@1895
   584
            UnregisterClass(SDL_Appname, SDL_Instance);
slouken@1895
   585
        }
slouken@1895
   586
        SDL_free(SDL_Appname);
slouken@1895
   587
        SDL_Appname = NULL;
slouken@1895
   588
    }
slouken@1895
   589
}
slouken@1895
   590
slouken@1895
   591
/* vi: set ts=4 sw=4 expandtab: */