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