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