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