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