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