src/video/win32/SDL_win32events.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Feb 2008 08:35:49 +0000
changeset 2308 514f7c1651fc
parent 2284 545fbf461c5b
child 2309 21591ae7355d
permissions -rw-r--r--
Untested Win32 keyboard scancode code.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@1895
     3
    Copyright (C) 1997-2006 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@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#include "SDL_win32video.h"
slouken@1895
    25
#include "SDL_syswm.h"
slouken@1895
    26
#include "SDL_vkeys.h"
slouken@1895
    27
#include "../../events/SDL_events_c.h"
slouken@1895
    28
slouken@1895
    29
/*#define WMMSG_DEBUG*/
slouken@1895
    30
#ifdef WMMSG_DEBUG
slouken@1913
    31
#include <stdio.h>
slouken@1895
    32
#include "wmmsg.h"
slouken@1895
    33
#endif
slouken@1895
    34
slouken@1895
    35
/* Masks for processing the windows KEYDOWN and KEYUP messages */
slouken@1895
    36
#define REPEATED_KEYMASK	(1<<30)
slouken@1895
    37
#define EXTENDED_KEYMASK	(1<<24)
slouken@1895
    38
icculus@2127
    39
/* Make sure XBUTTON stuff is defined that isn't in older Platform SDKs... */
icculus@2127
    40
#ifndef WM_XBUTTONDOWN
icculus@2127
    41
#define WM_XBUTTONDOWN 0x020B
icculus@2127
    42
#endif
icculus@2127
    43
#ifndef WM_XBUTTONUP
icculus@2127
    44
#define WM_XBUTTONUP 0x020C
icculus@2127
    45
#endif
icculus@2127
    46
#ifndef GET_XBUTTON_WPARAM
icculus@2127
    47
#define GET_XBUTTON_WPARAM(w) (HIWORD(w))
icculus@2127
    48
#endif
slouken@1895
    49
slouken@1895
    50
LRESULT CALLBACK
slouken@1895
    51
WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@1895
    52
{
slouken@1895
    53
    SDL_WindowData *data;
slouken@1895
    54
slouken@1951
    55
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@1951
    56
    if (SDL_ProcessEvents[SDL_SYSWMEVENT] == SDL_ENABLE) {
slouken@1951
    57
        SDL_SysWMmsg wmmsg;
slouken@1951
    58
slouken@1951
    59
        SDL_VERSION(&wmmsg.version);
slouken@1951
    60
        wmmsg.hwnd = hwnd;
slouken@1951
    61
        wmmsg.msg = msg;
slouken@1951
    62
        wmmsg.wParam = wParam;
slouken@1951
    63
        wmmsg.lParam = lParam;
slouken@1951
    64
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
    65
    }
slouken@1951
    66
slouken@1895
    67
    /* Get the window data for the window */
slouken@1895
    68
    data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
slouken@1895
    69
    if (!data) {
slouken@1895
    70
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@1895
    71
    }
slouken@1895
    72
#ifdef WMMSG_DEBUG
slouken@1913
    73
    {
slouken@1913
    74
        FILE *log = fopen("wmmsg.txt", "a");
slouken@1913
    75
        fprintf(log, "Received windows message: %p ", hwnd);
slouken@1913
    76
        if (msg > MAX_WMMSG) {
slouken@1913
    77
            fprintf(log, "%d", msg);
slouken@1913
    78
        } else {
slouken@1913
    79
            fprintf(log, "%s", wmtab[msg]);
slouken@1913
    80
        }
slouken@1913
    81
        fprintf(log, " -- 0x%X, 0x%X\n", wParam, lParam);
slouken@1913
    82
        fclose(log);
slouken@1895
    83
    }
slouken@1895
    84
#endif
slouken@1895
    85
slouken@1895
    86
    switch (msg) {
slouken@1895
    87
slouken@1895
    88
    case WM_SHOWWINDOW:
slouken@1895
    89
        {
slouken@1895
    90
            if (wParam) {
slouken@1895
    91
                SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_SHOWN, 0,
slouken@1895
    92
                                    0);
slouken@1895
    93
            } else {
slouken@1895
    94
                SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_HIDDEN, 0,
slouken@1895
    95
                                    0);
slouken@1895
    96
            }
slouken@1895
    97
        }
slouken@1895
    98
        break;
slouken@1895
    99
slouken@1895
   100
    case WM_ACTIVATE:
slouken@1895
   101
        {
slouken@1895
   102
            int index;
slouken@1895
   103
            SDL_Keyboard *keyboard;
slouken@1895
   104
            BOOL minimized;
slouken@1895
   105
slouken@1895
   106
            minimized = HIWORD(wParam);
slouken@1895
   107
            index = data->videodata->keyboard;
slouken@1895
   108
            keyboard = SDL_GetKeyboard(index);
slouken@1895
   109
            if (!minimized && (LOWORD(wParam) != WA_INACTIVE)) {
slouken@1895
   110
                SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_SHOWN,
slouken@1895
   111
                                    0, 0);
slouken@1895
   112
                SDL_SendWindowEvent(data->windowID,
slouken@1895
   113
                                    SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1895
   114
                if (IsZoomed(hwnd)) {
slouken@1895
   115
                    SDL_SendWindowEvent(data->windowID,
slouken@1895
   116
                                        SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
   117
                }
slouken@1895
   118
                if (keyboard && keyboard->focus != data->windowID) {
slouken@1895
   119
                    SDL_SetKeyboardFocus(index, data->windowID);
slouken@1895
   120
                }
slouken@1895
   121
                /* FIXME: Update keyboard state */
slouken@1895
   122
            } else {
slouken@1895
   123
                if (keyboard && keyboard->focus == data->windowID) {
slouken@1895
   124
                    SDL_SetKeyboardFocus(index, 0);
slouken@1895
   125
                }
slouken@1895
   126
                if (minimized) {
slouken@1895
   127
                    SDL_SendWindowEvent(data->windowID,
slouken@1895
   128
                                        SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
   129
                }
slouken@1895
   130
            }
slouken@1895
   131
            return (0);
slouken@1895
   132
        }
slouken@1895
   133
        break;
slouken@1895
   134
slouken@1895
   135
    case WM_MOUSEMOVE:
slouken@1895
   136
        {
slouken@1895
   137
            int index;
slouken@1895
   138
            SDL_Mouse *mouse;
slouken@1895
   139
            int x, y;
slouken@1895
   140
slouken@1895
   141
            index = data->videodata->mouse;
slouken@1895
   142
            mouse = SDL_GetMouse(index);
slouken@1895
   143
slouken@1895
   144
            if (mouse->focus != data->windowID) {
slouken@1895
   145
                TRACKMOUSEEVENT tme;
slouken@1895
   146
slouken@1895
   147
                tme.cbSize = sizeof(tme);
slouken@1895
   148
                tme.dwFlags = TME_LEAVE;
slouken@1895
   149
                tme.hwndTrack = hwnd;
slouken@1895
   150
                TrackMouseEvent(&tme);
slouken@1895
   151
slouken@1895
   152
                SDL_SetMouseFocus(index, data->windowID);
slouken@1895
   153
            }
slouken@1895
   154
slouken@1895
   155
            /* mouse has moved within the window */
slouken@1895
   156
            x = LOWORD(lParam);
slouken@1895
   157
            y = HIWORD(lParam);
slouken@1895
   158
            if (mouse->relative_mode) {
slouken@1895
   159
                int w, h;
slouken@1895
   160
                POINT center;
slouken@1895
   161
                SDL_GetWindowSize(data->windowID, &w, &h);
slouken@1895
   162
                center.x = (w / 2);
slouken@1895
   163
                center.y = (h / 2);
slouken@1895
   164
                x -= center.x;
slouken@1895
   165
                y -= center.y;
slouken@1895
   166
                if (x || y) {
slouken@1895
   167
                    ClientToScreen(hwnd, &center);
slouken@1895
   168
                    SetCursorPos(center.x, center.y);
slouken@1895
   169
                    SDL_SendMouseMotion(index, 1, x, y);
slouken@1895
   170
                }
slouken@1895
   171
            } else {
slouken@1895
   172
                SDL_SendMouseMotion(index, 0, x, y);
slouken@1895
   173
            }
slouken@1895
   174
        }
slouken@1895
   175
        return (0);
slouken@1895
   176
slouken@1895
   177
    case WM_MOUSELEAVE:
slouken@1895
   178
        {
slouken@1895
   179
            int index;
slouken@1895
   180
            SDL_Mouse *mouse;
slouken@1895
   181
slouken@1895
   182
            index = data->videodata->mouse;
slouken@1895
   183
            mouse = SDL_GetMouse(index);
slouken@1895
   184
slouken@1895
   185
            if (mouse->focus == data->windowID) {
slouken@1895
   186
                SDL_SetMouseFocus(index, 0);
slouken@1895
   187
            }
slouken@1895
   188
        }
slouken@1895
   189
        return (0);
slouken@1895
   190
slouken@1895
   191
    case WM_LBUTTONDOWN:
slouken@1895
   192
    case WM_LBUTTONUP:
slouken@1895
   193
    case WM_MBUTTONDOWN:
slouken@1895
   194
    case WM_MBUTTONUP:
slouken@1895
   195
    case WM_RBUTTONDOWN:
slouken@1895
   196
    case WM_RBUTTONUP:
icculus@2127
   197
    case WM_XBUTTONDOWN:
icculus@2127
   198
    case WM_XBUTTONUP:
slouken@1895
   199
        {
icculus@2127
   200
            int xbuttonval = 0;
slouken@1895
   201
            int index;
slouken@1895
   202
            SDL_Mouse *mouse;
slouken@1895
   203
            Uint8 button, state;
slouken@1895
   204
slouken@1895
   205
            /* DJM:
slouken@1895
   206
               We want the SDL window to take focus so that
slouken@1895
   207
               it acts like a normal windows "component"
slouken@1895
   208
               (e.g. gains keyboard focus on a mouse click).
slouken@1895
   209
             */
slouken@1895
   210
            SetFocus(hwnd);
slouken@1895
   211
slouken@1895
   212
            index = data->videodata->mouse;
slouken@1895
   213
            mouse = SDL_GetMouse(index);
slouken@1895
   214
slouken@1895
   215
            /* Figure out which button to use */
slouken@1895
   216
            switch (msg) {
slouken@1895
   217
            case WM_LBUTTONDOWN:
slouken@1895
   218
                button = SDL_BUTTON_LEFT;
slouken@1895
   219
                state = SDL_PRESSED;
slouken@1895
   220
                break;
slouken@1895
   221
            case WM_LBUTTONUP:
slouken@1895
   222
                button = SDL_BUTTON_LEFT;
slouken@1895
   223
                state = SDL_RELEASED;
slouken@1895
   224
                break;
slouken@1895
   225
            case WM_MBUTTONDOWN:
slouken@1895
   226
                button = SDL_BUTTON_MIDDLE;
slouken@1895
   227
                state = SDL_PRESSED;
slouken@1895
   228
                break;
slouken@1895
   229
            case WM_MBUTTONUP:
slouken@1895
   230
                button = SDL_BUTTON_MIDDLE;
slouken@1895
   231
                state = SDL_RELEASED;
slouken@1895
   232
                break;
slouken@1895
   233
            case WM_RBUTTONDOWN:
slouken@1895
   234
                button = SDL_BUTTON_RIGHT;
slouken@1895
   235
                state = SDL_PRESSED;
slouken@1895
   236
                break;
slouken@1895
   237
            case WM_RBUTTONUP:
slouken@1895
   238
                button = SDL_BUTTON_RIGHT;
slouken@1895
   239
                state = SDL_RELEASED;
slouken@1895
   240
                break;
icculus@2127
   241
            case WM_XBUTTONDOWN:
icculus@2127
   242
                xbuttonval = GET_XBUTTON_WPARAM(wParam);
slouken@2284
   243
                button = SDL_BUTTON_X1 + xbuttonval - 1;
icculus@2127
   244
                state = SDL_PRESSED;
icculus@2127
   245
                break;
icculus@2127
   246
            case WM_XBUTTONUP:
icculus@2127
   247
                xbuttonval = GET_XBUTTON_WPARAM(wParam);
slouken@2284
   248
                button = SDL_BUTTON_X1 + xbuttonval - 1;
icculus@2127
   249
                state = SDL_RELEASED;
icculus@2127
   250
                break;
slouken@1895
   251
            default:
slouken@1895
   252
                /* Eh? Unknown button? */
slouken@1895
   253
                return (0);
slouken@1895
   254
            }
slouken@1895
   255
            if (state == SDL_PRESSED) {
slouken@1895
   256
                /* Grab mouse so we get up events */
slouken@1895
   257
                if (++data->mouse_pressed > 0) {
slouken@1895
   258
                    SetCapture(hwnd);
slouken@1895
   259
                }
slouken@1895
   260
            } else {
slouken@1895
   261
                /* Release mouse after all up events */
slouken@1895
   262
                if (--data->mouse_pressed <= 0) {
slouken@1895
   263
                    ReleaseCapture();
slouken@1895
   264
                    data->mouse_pressed = 0;
slouken@1895
   265
                }
slouken@1895
   266
            }
slouken@1895
   267
slouken@1895
   268
            if (!mouse->relative_mode) {
slouken@1895
   269
                int x, y;
slouken@1895
   270
                x = LOWORD(lParam);
slouken@1895
   271
                y = HIWORD(lParam);
slouken@1895
   272
                SDL_SendMouseMotion(index, 0, x, y);
slouken@1895
   273
            }
slouken@1895
   274
            SDL_SendMouseButton(index, state, button);
icculus@2127
   275
icculus@2127
   276
            /*
icculus@2127
   277
             * MSDN says:
icculus@2127
   278
             *  "Unlike the WM_LBUTTONUP, WM_MBUTTONUP, and WM_RBUTTONUP
icculus@2127
   279
             *   messages, an application should return TRUE from [an
icculus@2127
   280
             *   XBUTTON message] if it processes it. Doing so will allow
icculus@2127
   281
             *   software that simulates this message on Microsoft Windows
icculus@2127
   282
             *   systems earlier than Windows 2000 to determine whether
icculus@2127
   283
             *   the window procedure processed the message or called
icculus@2127
   284
             *   DefWindowProc to process it.
icculus@2127
   285
             */
icculus@2127
   286
            if (xbuttonval > 0) {
slouken@2141
   287
                return (TRUE);
icculus@2127
   288
            }
slouken@1895
   289
        }
slouken@1895
   290
        return (0);
slouken@1895
   291
slouken@1895
   292
    case WM_MOUSEWHEEL:
slouken@1895
   293
        {
slouken@1895
   294
            int index;
slouken@1895
   295
            int motion = (short) HIWORD(wParam);
slouken@1895
   296
slouken@1895
   297
            index = data->videodata->mouse;
slouken@2152
   298
            SDL_SendMouseWheel(index, 0, motion);
slouken@1895
   299
        }
slouken@1895
   300
        return (0);
slouken@1895
   301
slouken@1895
   302
    case WM_SYSKEYDOWN:
slouken@1895
   303
    case WM_KEYDOWN:
slouken@1895
   304
        {
slouken@1895
   305
            int index;
slouken@1895
   306
slouken@1895
   307
            /* Ignore repeated keys */
slouken@1895
   308
            if (lParam & REPEATED_KEYMASK) {
slouken@1895
   309
                return (0);
slouken@1895
   310
            }
slouken@1895
   311
slouken@1895
   312
            index = data->videodata->keyboard;
slouken@1895
   313
            switch (wParam) {
slouken@1895
   314
            case VK_CONTROL:
slouken@1895
   315
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   316
                    wParam = VK_RCONTROL;
slouken@1895
   317
                else
slouken@1895
   318
                    wParam = VK_LCONTROL;
slouken@1895
   319
                break;
slouken@1895
   320
            case VK_SHIFT:
slouken@1895
   321
                /* EXTENDED trick doesn't work here */
slouken@1895
   322
                {
slouken@2308
   323
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   324
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_RELEASED
slouken@1895
   325
                        && (GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   326
                        wParam = VK_LSHIFT;
slouken@2308
   327
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_RELEASED
slouken@1895
   328
                               && (GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   329
                        wParam = VK_RSHIFT;
slouken@1895
   330
                    } else {
slouken@1895
   331
                        /* Probably a key repeat */
slouken@1895
   332
                        return (0);
slouken@1895
   333
                    }
slouken@1895
   334
                }
slouken@1895
   335
                break;
slouken@1895
   336
            case VK_MENU:
slouken@1895
   337
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   338
                    wParam = VK_RMENU;
slouken@1895
   339
                else
slouken@1895
   340
                    wParam = VK_LMENU;
slouken@1895
   341
                break;
slouken@1895
   342
            }
slouken@2308
   343
            if (wParam < 256) {
slouken@2308
   344
                SDL_SendKeyboardKey(index, SDL_PRESSED,
slouken@2308
   345
                                    data->videodata->key_layout[wParam]);
slouken@2308
   346
            }
slouken@1895
   347
        }
slouken@1895
   348
        return (0);
slouken@1895
   349
slouken@1895
   350
    case WM_SYSKEYUP:
slouken@1895
   351
    case WM_KEYUP:
slouken@1895
   352
        {
slouken@1895
   353
            int index;
slouken@1895
   354
slouken@1895
   355
            index = data->videodata->keyboard;
slouken@1895
   356
            switch (wParam) {
slouken@1895
   357
            case VK_CONTROL:
slouken@1895
   358
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   359
                    wParam = VK_RCONTROL;
slouken@1895
   360
                else
slouken@1895
   361
                    wParam = VK_LCONTROL;
slouken@1895
   362
                break;
slouken@1895
   363
            case VK_SHIFT:
slouken@1895
   364
                /* EXTENDED trick doesn't work here */
slouken@1895
   365
                {
slouken@2308
   366
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   367
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_PRESSED
slouken@1895
   368
                        && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   369
                        wParam = VK_LSHIFT;
slouken@2308
   370
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_PRESSED
slouken@1895
   371
                               && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   372
                        wParam = VK_RSHIFT;
slouken@1895
   373
                    } else {
slouken@1895
   374
                        /* Probably a key repeat */
slouken@1895
   375
                        return (0);
slouken@1895
   376
                    }
slouken@1895
   377
                }
slouken@1895
   378
                break;
slouken@1895
   379
            case VK_MENU:
slouken@1895
   380
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   381
                    wParam = VK_RMENU;
slouken@1895
   382
                else
slouken@1895
   383
                    wParam = VK_LMENU;
slouken@1895
   384
                break;
slouken@1895
   385
            }
slouken@1895
   386
            /* Windows only reports keyup for print screen */
slouken@1895
   387
            if (wParam == VK_SNAPSHOT
slouken@2308
   388
                && SDL_GetKeyboardState(NULL)[SDL_SCANCODE_PRINTSCREEN] ==
slouken@2308
   389
                SDL_RELEASED) {
slouken@1895
   390
                SDL_SendKeyboardKey(index, SDL_PRESSED,
slouken@2308
   391
                                    data->videodata->key_layout[wParam]);
slouken@1895
   392
            }
slouken@2308
   393
            if (wParam < 256) {
slouken@2308
   394
                SDL_SendKeyboardKey(index, SDL_RELEASED,
slouken@2308
   395
                                    data->videodata->key_layout[wParam]);
slouken@2308
   396
            }
slouken@1895
   397
        }
slouken@1895
   398
        return (0);
slouken@1895
   399
slouken@1895
   400
    case WM_GETMINMAXINFO:
slouken@1895
   401
        {
slouken@1895
   402
            MINMAXINFO *info;
slouken@1895
   403
            RECT size;
slouken@1895
   404
            int x, y;
slouken@1895
   405
            int w, h;
slouken@1895
   406
            int style;
slouken@1895
   407
slouken@1895
   408
            /* If we allow resizing, let the resize happen naturally */
slouken@1895
   409
            if (SDL_GetWindowFlags(data->windowID) & SDL_WINDOW_RESIZABLE) {
slouken@1895
   410
                return (0);
slouken@1895
   411
            }
slouken@1895
   412
slouken@1895
   413
            /* Get the current position of our window */
slouken@1895
   414
            GetWindowRect(hwnd, &size);
slouken@1895
   415
            x = size.left;
slouken@1895
   416
            y = size.top;
slouken@1895
   417
slouken@1895
   418
            /* Calculate current size of our window */
slouken@1895
   419
            SDL_GetWindowSize(data->windowID, &w, &h);
slouken@1895
   420
            size.top = 0;
slouken@1895
   421
            size.left = 0;
slouken@1895
   422
            size.bottom = h;
slouken@1895
   423
            size.right = w;
slouken@1895
   424
slouken@1895
   425
            /* DJM - according to the docs for GetMenu(), the
slouken@1895
   426
               return value is undefined if hwnd is a child window.
slouken@1895
   427
               Aparently it's too difficult for MS to check
slouken@1895
   428
               inside their function, so I have to do it here.
slouken@1895
   429
             */
slouken@1895
   430
            style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   431
            AdjustWindowRect(&size,
slouken@1895
   432
                             style,
slouken@1895
   433
                             style & WS_CHILDWINDOW ? FALSE : GetMenu(hwnd) !=
slouken@1895
   434
                             NULL);
slouken@1895
   435
slouken@1895
   436
            w = size.right - size.left;
slouken@1895
   437
            h = size.bottom - size.top;
slouken@1895
   438
slouken@1895
   439
            /* Fix our size to the current size */
slouken@1895
   440
            info = (MINMAXINFO *) lParam;
slouken@1895
   441
            info->ptMaxSize.x = w;
slouken@1895
   442
            info->ptMaxSize.y = h;
slouken@1895
   443
            info->ptMaxPosition.x = x;
slouken@1895
   444
            info->ptMaxPosition.y = y;
slouken@1895
   445
            info->ptMinTrackSize.x = w;
slouken@1895
   446
            info->ptMinTrackSize.y = h;
slouken@1895
   447
            info->ptMaxTrackSize.x = w;
slouken@1895
   448
            info->ptMaxTrackSize.y = h;
slouken@1895
   449
        }
slouken@1895
   450
        return (0);
slouken@1895
   451
slouken@1895
   452
    case WM_WINDOWPOSCHANGED:
slouken@1895
   453
        {
slouken@1895
   454
            RECT rect;
slouken@1895
   455
            int x, y;
slouken@1895
   456
            int w, h;
slouken@1895
   457
            Uint32 window_flags;
slouken@1895
   458
slouken@1895
   459
            GetClientRect(hwnd, &rect);
slouken@1895
   460
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   461
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   462
slouken@1895
   463
            window_flags = SDL_GetWindowFlags(data->windowID);
slouken@1895
   464
            if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   465
                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   466
                ClipCursor(&rect);
slouken@1895
   467
            }
slouken@1895
   468
slouken@1895
   469
            x = rect.left;
slouken@1895
   470
            y = rect.top;
slouken@1895
   471
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
   472
slouken@1895
   473
            w = rect.right - rect.left;
slouken@1895
   474
            h = rect.bottom - rect.top;
slouken@1895
   475
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_RESIZED, w,
slouken@1895
   476
                                h);
slouken@1895
   477
        }
slouken@1895
   478
        break;
slouken@1895
   479
slouken@1895
   480
    case WM_SETCURSOR:
slouken@1895
   481
        {
slouken@1895
   482
            /*
slouken@1895
   483
               Uint16 hittest;
slouken@1895
   484
slouken@1895
   485
               hittest = LOWORD(lParam);
slouken@1895
   486
               if (hittest == HTCLIENT) {
slouken@1895
   487
               SetCursor(SDL_hcursor);
slouken@1895
   488
               return (TRUE);
slouken@1895
   489
               }
slouken@1895
   490
             */
slouken@1895
   491
        }
slouken@1895
   492
        break;
slouken@1895
   493
slouken@1895
   494
        /* We are about to get palette focus! */
slouken@1895
   495
    case WM_QUERYNEWPALETTE:
slouken@1895
   496
        {
slouken@1895
   497
            /*
slouken@1895
   498
               WIN_RealizePalette(current_video);
slouken@1895
   499
               return (TRUE);
slouken@1895
   500
             */
slouken@1895
   501
        }
slouken@1895
   502
        break;
slouken@1895
   503
slouken@1895
   504
        /* Another application changed the palette */
slouken@1895
   505
    case WM_PALETTECHANGED:
slouken@1895
   506
        {
slouken@1895
   507
            /*
slouken@1895
   508
               WIN_PaletteChanged(current_video, (HWND) wParam);
slouken@1895
   509
             */
slouken@1895
   510
        }
slouken@1895
   511
        break;
slouken@1895
   512
slouken@1895
   513
        /* We were occluded, refresh our display */
slouken@1895
   514
    case WM_PAINT:
slouken@1895
   515
        {
slouken@1895
   516
            RECT rect;
slouken@1895
   517
            if (GetUpdateRect(hwnd, &rect, FALSE)) {
slouken@1895
   518
                ValidateRect(hwnd, &rect);
slouken@1895
   519
                SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_EXPOSED,
slouken@1895
   520
                                    0, 0);
slouken@1895
   521
            }
slouken@1895
   522
        }
slouken@1895
   523
        return (0);
slouken@1895
   524
slouken@1895
   525
        /* We'll do our own drawing, prevent flicker */
slouken@1895
   526
    case WM_ERASEBKGND:
slouken@1895
   527
        {
slouken@1895
   528
        }
slouken@1895
   529
        return (1);
slouken@1895
   530
slouken@1895
   531
    case WM_SYSCOMMAND:
slouken@1895
   532
        {
slouken@1895
   533
            /* Don't start the screensaver or blank the monitor in fullscreen apps */
slouken@1895
   534
            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
slouken@1895
   535
                (wParam & 0xFFF0) == SC_MONITORPOWER) {
slouken@1895
   536
                if (SDL_GetWindowFlags(data->windowID) &
slouken@1895
   537
                    SDL_WINDOW_FULLSCREEN) {
slouken@1895
   538
                    return (0);
slouken@1895
   539
                }
slouken@1895
   540
            }
slouken@1895
   541
        }
slouken@1895
   542
        break;
slouken@1895
   543
slouken@1895
   544
    case WM_CLOSE:
slouken@1895
   545
        {
slouken@1895
   546
            SDL_SendWindowEvent(data->windowID, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1895
   547
        }
slouken@1895
   548
        return (0);
slouken@1895
   549
    }
slouken@1895
   550
    return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
slouken@1895
   551
}
slouken@1895
   552
slouken@1895
   553
void
slouken@1895
   554
WIN_PumpEvents(_THIS)
slouken@1895
   555
{
slouken@1895
   556
    MSG msg;
slouken@1895
   557
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
slouken@1895
   558
        TranslateMessage(&msg);
slouken@1895
   559
        DispatchMessage(&msg);
slouken@1895
   560
    }
slouken@1895
   561
}
slouken@1895
   562
slouken@1895
   563
static int app_registered = 0;
slouken@1895
   564
LPTSTR SDL_Appname = NULL;
slouken@1895
   565
Uint32 SDL_Appstyle = 0;
slouken@1895
   566
HINSTANCE SDL_Instance = NULL;
slouken@1895
   567
slouken@1895
   568
/* Register the class for this application */
slouken@1895
   569
int
slouken@1895
   570
SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@1895
   571
{
slouken@1895
   572
    WNDCLASS class;
slouken@1895
   573
slouken@1895
   574
    /* Only do this once... */
slouken@1895
   575
    if (app_registered) {
slouken@1895
   576
        ++app_registered;
slouken@1895
   577
        return (0);
slouken@1895
   578
    }
slouken@1895
   579
    if (!name && !SDL_Appname) {
slouken@1895
   580
        name = "SDL_app";
slouken@1895
   581
        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
slouken@1895
   582
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   583
    }
slouken@1895
   584
slouken@1895
   585
    if (name) {
slouken@1895
   586
        SDL_Appname = WIN_UTF8ToString(name);
slouken@1895
   587
        SDL_Appstyle = style;
slouken@1895
   588
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   589
    }
slouken@1895
   590
slouken@1895
   591
    /* Register the application class */
slouken@1895
   592
    class.hCursor = NULL;
slouken@1895
   593
    class.hIcon = LoadImage(SDL_Instance, SDL_Appname,
slouken@1895
   594
                            IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
slouken@1895
   595
    class.lpszMenuName = NULL;
slouken@1895
   596
    class.lpszClassName = SDL_Appname;
slouken@1895
   597
    class.hbrBackground = NULL;
slouken@1895
   598
    class.hInstance = SDL_Instance;
slouken@1895
   599
    class.style = SDL_Appstyle;
slouken@1895
   600
    class.lpfnWndProc = DefWindowProc;
slouken@1895
   601
    class.cbWndExtra = 0;
slouken@1895
   602
    class.cbClsExtra = 0;
slouken@1895
   603
    if (!RegisterClass(&class)) {
slouken@1895
   604
        SDL_SetError("Couldn't register application class");
slouken@1895
   605
        return (-1);
slouken@1895
   606
    }
slouken@1895
   607
slouken@1895
   608
    app_registered = 1;
slouken@1895
   609
    return (0);
slouken@1895
   610
}
slouken@1895
   611
slouken@1895
   612
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
slouken@1895
   613
void
slouken@1895
   614
SDL_UnregisterApp()
slouken@1895
   615
{
slouken@1895
   616
    WNDCLASS class;
slouken@1895
   617
slouken@1895
   618
    /* SDL_RegisterApp might not have been called before */
slouken@1895
   619
    if (!app_registered) {
slouken@1895
   620
        return;
slouken@1895
   621
    }
slouken@1895
   622
    --app_registered;
slouken@1895
   623
    if (app_registered == 0) {
slouken@1895
   624
        /* Check for any registered window classes. */
slouken@1895
   625
        if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
slouken@1895
   626
            UnregisterClass(SDL_Appname, SDL_Instance);
slouken@1895
   627
        }
slouken@1895
   628
        SDL_free(SDL_Appname);
slouken@1895
   629
        SDL_Appname = NULL;
slouken@1895
   630
    }
slouken@1895
   631
}
slouken@1895
   632
slouken@1895
   633
/* Sets an error message based on GetLastError() */
slouken@1895
   634
void
slouken@1895
   635
WIN_SetError(const char *prefix)
slouken@1895
   636
{
slouken@1895
   637
    TCHAR buffer[1024];
slouken@1895
   638
    char *message;
slouken@1895
   639
slouken@1895
   640
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,
slouken@1895
   641
                  NULL,
slouken@1895
   642
                  GetLastError(), 0, buffer, SDL_arraysize(buffer), NULL);
slouken@1895
   643
slouken@1895
   644
    message = WIN_StringToUTF8(buffer);
slouken@1895
   645
    SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ":" : "", message);
slouken@1895
   646
    SDL_free(message);
slouken@1895
   647
}
slouken@1895
   648
slouken@1895
   649
/* vi: set ts=4 sw=4 expandtab: */