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