src/video/win32/SDL_win32events.c
author Eli Gottlieb <eligottlieb@gmail.com>
Thu, 29 Jul 2010 22:57:39 -0400
changeset 4815 93402b9dd20c
parent 4788 0bfda420c936
child 4862 7b1d35d98294
permissions -rw-r--r--
Added Andreas's patch to fix my silly C++-style errors.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 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@2710
    22
slouken@2710
    23
#if (_WIN32_WINNT < 0x0501)
slouken@2710
    24
#undef _WIN32_WINNT
slouken@2710
    25
#define _WIN32_WINNT 0x0501
slouken@2710
    26
#endif
slouken@2710
    27
slouken@1895
    28
#include "SDL_config.h"
slouken@1895
    29
slouken@1895
    30
#include "SDL_win32video.h"
eligottlieb@4815
    31
#include "SDL_win32shape.h"
slouken@1895
    32
#include "SDL_syswm.h"
slouken@1895
    33
#include "SDL_vkeys.h"
slouken@1895
    34
#include "../../events/SDL_events_c.h"
slouken@1895
    35
slouken@1895
    36
/*#define WMMSG_DEBUG*/
slouken@1895
    37
#ifdef WMMSG_DEBUG
slouken@1913
    38
#include <stdio.h>
slouken@1895
    39
#include "wmmsg.h"
slouken@1895
    40
#endif
slouken@1895
    41
slouken@1895
    42
/* Masks for processing the windows KEYDOWN and KEYUP messages */
slouken@2317
    43
#define REPEATED_KEYMASK    (1<<30)
slouken@2317
    44
#define EXTENDED_KEYMASK    (1<<24)
slouken@1895
    45
bob@2324
    46
#define VK_ENTER    10          /* Keypad Enter ... no VKEY defined? */
slouken@2313
    47
icculus@2127
    48
/* Make sure XBUTTON stuff is defined that isn't in older Platform SDKs... */
icculus@2127
    49
#ifndef WM_XBUTTONDOWN
icculus@2127
    50
#define WM_XBUTTONDOWN 0x020B
icculus@2127
    51
#endif
icculus@2127
    52
#ifndef WM_XBUTTONUP
icculus@2127
    53
#define WM_XBUTTONUP 0x020C
icculus@2127
    54
#endif
icculus@2127
    55
#ifndef GET_XBUTTON_WPARAM
icculus@2127
    56
#define GET_XBUTTON_WPARAM(w) (HIWORD(w))
icculus@2127
    57
#endif
bobbens@2733
    58
#ifndef WM_INPUT
bobbens@2733
    59
#define WM_INPUT 0x00ff
bobbens@2733
    60
#endif
slouken@1895
    61
slouken@2710
    62
extern HCTX *g_hCtx;
slouken@2710
    63
extern HANDLE *mice;
slouken@2710
    64
extern int total_mice;
slouken@2710
    65
extern int tablet;
slouken@2710
    66
int pressure = 0;               /* the pressure reported by the tablet */
slouken@2710
    67
slouken@2310
    68
static WPARAM
slouken@2310
    69
RemapVKEY(WPARAM wParam, LPARAM lParam)
slouken@2310
    70
{
slouken@2317
    71
    int i;
bob@2324
    72
    BYTE scancode = (BYTE) ((lParam >> 16) & 0xFF);
slouken@2317
    73
slouken@2310
    74
    /* Windows remaps alphabetic keys based on current layout.
slouken@2310
    75
       We try to provide USB scancodes, so undo this mapping.
slouken@2310
    76
     */
slouken@2310
    77
    if (wParam >= 'A' && wParam <= 'Z') {
slouken@2311
    78
        if (scancode != alpha_scancodes[wParam - 'A']) {
slouken@2311
    79
            for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
slouken@2311
    80
                if (scancode == alpha_scancodes[i]) {
slouken@2310
    81
                    wParam = 'A' + i;
slouken@2310
    82
                    break;
slouken@2310
    83
                }
slouken@2310
    84
            }
slouken@2310
    85
        }
slouken@2310
    86
    }
slouken@2317
    87
slouken@3700
    88
    /* Keypad keys are a little trickier, we always scan for them.
slouken@3700
    89
       Keypad arrow keys have the same scancode as normal arrow keys,
slouken@3700
    90
       except they don't have the extended bit (0x1000000) set.
slouken@3700
    91
     */
slouken@3700
    92
    if (!(lParam & 0x1000000)) {
slouken@3700
    93
        for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
slouken@3700
    94
            if (scancode == keypad_scancodes[i]) {
slouken@3700
    95
                wParam = VK_NUMPAD0 + i;
slouken@3700
    96
                break;
slouken@3700
    97
            }
slouken@2317
    98
        }
slouken@2317
    99
    }
slouken@2317
   100
slouken@2310
   101
    return wParam;
slouken@2310
   102
}
slouken@2310
   103
slouken@1895
   104
LRESULT CALLBACK
slouken@1895
   105
WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@1895
   106
{
slouken@1895
   107
    SDL_WindowData *data;
slouken@2710
   108
    RAWINPUT *raw;
slouken@2710
   109
    PACKET packet;
slouken@3566
   110
    LRESULT returnCode = -1;
slouken@1895
   111
slouken@1951
   112
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
   113
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
   114
        SDL_SysWMmsg wmmsg;
slouken@1951
   115
slouken@1951
   116
        SDL_VERSION(&wmmsg.version);
slouken@1951
   117
        wmmsg.hwnd = hwnd;
slouken@1951
   118
        wmmsg.msg = msg;
slouken@1951
   119
        wmmsg.wParam = wParam;
slouken@1951
   120
        wmmsg.lParam = lParam;
slouken@1951
   121
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   122
    }
slouken@1951
   123
slouken@1895
   124
    /* Get the window data for the window */
slouken@1895
   125
    data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
slouken@1895
   126
    if (!data) {
slouken@1895
   127
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@1895
   128
    }
slouken@1895
   129
#ifdef WMMSG_DEBUG
slouken@1913
   130
    {
slouken@1913
   131
        FILE *log = fopen("wmmsg.txt", "a");
slouken@1913
   132
        fprintf(log, "Received windows message: %p ", hwnd);
slouken@1913
   133
        if (msg > MAX_WMMSG) {
slouken@1913
   134
            fprintf(log, "%d", msg);
slouken@1913
   135
        } else {
slouken@1913
   136
            fprintf(log, "%s", wmtab[msg]);
slouken@1913
   137
        }
slouken@1913
   138
        fprintf(log, " -- 0x%X, 0x%X\n", wParam, lParam);
slouken@1913
   139
        fclose(log);
slouken@1895
   140
    }
slouken@2710
   141
slouken@1895
   142
#endif
slouken@1895
   143
slouken@1895
   144
    switch (msg) {
slouken@1895
   145
slouken@2710
   146
    case WT_PACKET:
slouken@2710
   147
        {
slouken@2710
   148
            /* if we receive such data we need to update the pressure */
slouken@2726
   149
            SDL_VideoData *videodata = data->videodata;
slouken@2726
   150
            if (videodata->wintabDLL
slouken@3253
   151
                && videodata->WTPacket((HCTX) lParam, (UINT) wParam, &packet)) {
slouken@2710
   152
                SDL_ChangeEnd(tablet, (int) packet.pkCursor);
slouken@2710
   153
                pressure = (int) packet.pkNormalPressure;
slouken@2710
   154
            }
slouken@2710
   155
        }
slouken@2710
   156
        break;
slouken@2710
   157
slouken@2710
   158
    case WT_PROXIMITY:
slouken@2710
   159
        {
slouken@2710
   160
            /* checking where the proximity message showed up */
slouken@2710
   161
            int h_context = LOWORD(lParam);
slouken@2724
   162
            POINT point;
slouken@2710
   163
            GetCursorPos(&point);
slouken@2710
   164
            ScreenToClient(hwnd, &point);
slouken@2710
   165
slouken@2710
   166
            /* are we in proximity or out of proximity */
slouken@2710
   167
            if (h_context == 0) {
slouken@2724
   168
                SDL_SendProximity(tablet, point.x, point.y, SDL_PROXIMITYOUT);
slouken@2710
   169
            } else {
slouken@2724
   170
                SDL_SendProximity(tablet, point.x, point.y, SDL_PROXIMITYIN);
slouken@2710
   171
            }
slouken@2710
   172
        }
slouken@2710
   173
        break;
slouken@2710
   174
slouken@1895
   175
    case WM_SHOWWINDOW:
slouken@1895
   176
        {
slouken@1895
   177
            if (wParam) {
slouken@3685
   178
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
   179
            } else {
slouken@3685
   180
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
   181
            }
slouken@1895
   182
        }
slouken@1895
   183
        break;
slouken@1895
   184
slouken@1895
   185
    case WM_ACTIVATE:
slouken@1895
   186
        {
slouken@1895
   187
            int index;
slouken@1895
   188
            SDL_Keyboard *keyboard;
slouken@1895
   189
            BOOL minimized;
slouken@1895
   190
slouken@1895
   191
            minimized = HIWORD(wParam);
slouken@1895
   192
            index = data->videodata->keyboard;
slouken@1895
   193
            keyboard = SDL_GetKeyboard(index);
slouken@1895
   194
            if (!minimized && (LOWORD(wParam) != WA_INACTIVE)) {
slouken@3685
   195
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   196
                SDL_SendWindowEvent(data->window,
slouken@1895
   197
                                    SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@3139
   198
#ifndef _WIN32_WCE              /* WinCE misses IsZoomed() */
slouken@1895
   199
                if (IsZoomed(hwnd)) {
slouken@3685
   200
                    SDL_SendWindowEvent(data->window,
slouken@1895
   201
                                        SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
   202
                }
slouken@3139
   203
#endif
slouken@3685
   204
                if (keyboard && keyboard->focus != data->window) {
slouken@3685
   205
                    SDL_SetKeyboardFocus(index, data->window);
slouken@1895
   206
                }
slouken@1895
   207
                /* FIXME: Update keyboard state */
slouken@1895
   208
            } else {
slouken@3685
   209
                if (keyboard && keyboard->focus == data->window) {
slouken@1895
   210
                    SDL_SetKeyboardFocus(index, 0);
slouken@1895
   211
                }
slouken@1895
   212
                if (minimized) {
slouken@3685
   213
                    SDL_SendWindowEvent(data->window,
slouken@1895
   214
                                        SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
   215
                }
slouken@1895
   216
            }
slouken@1895
   217
        }
slouken@3566
   218
        returnCode = 0;
slouken@3566
   219
        break;
slouken@1895
   220
slouken@3097
   221
/* WinCE has no RawInput, so we use the classic mouse events.
slouken@3097
   222
   In classic Win32 this is done by WM_INPUT
slouken@3097
   223
 */
slouken@3097
   224
#ifdef _WIN32_WCE
slouken@3097
   225
    case WM_MOUSEMOVE:
slouken@3139
   226
        SDL_SendMouseMotion(0, 0, LOWORD(lParam), HIWORD(lParam), 0);
slouken@3139
   227
        break;
slouken@3139
   228
slouken@3097
   229
    case WM_LBUTTONDOWN:
slouken@3139
   230
        SDL_SendMouseMotion(0, 0, LOWORD(lParam), HIWORD(lParam), 0);
slouken@3139
   231
        SDL_SendMouseButton(0, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@3139
   232
        break;
slouken@3139
   233
slouken@3097
   234
    case WM_LBUTTONUP:
slouken@3139
   235
        SDL_SendMouseMotion(0, 0, LOWORD(lParam), HIWORD(lParam), 0);
slouken@3139
   236
        SDL_SendMouseButton(0, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@3139
   237
        break;
slouken@3097
   238
#else /* _WIN32_WCE */
slouken@3097
   239
slouken@2710
   240
    case WM_INPUT:             /* mouse events */
slouken@1895
   241
        {
slouken@2710
   242
            LPBYTE lpb;
slouken@2710
   243
            const RAWINPUTHEADER *header;
slouken@3260
   244
            int index = -1;
slouken@2710
   245
            int i;
slouken@2710
   246
            int size = 0;
slouken@2710
   247
            const RAWMOUSE *raw_mouse = NULL;
slouken@2724
   248
            POINT point;
slouken@2710
   249
            USHORT flags;
slouken@2974
   250
            int w, h;
slouken@1895
   251
slouken@3097
   252
            /* we're collecting raw data to be able to identify the mouse (if there are several) */
eligottlieb@4815
   253
            GetRawInputData((HRAWINPUT) lParam, RID_INPUT, NULL, (PUINT)&size,
slouken@2710
   254
                            sizeof(RAWINPUTHEADER));
slouken@2766
   255
            lpb = SDL_stack_alloc(BYTE, size);
eligottlieb@4815
   256
            GetRawInputData((HRAWINPUT) lParam, RID_INPUT, lpb, (PUINT)&size,
slouken@2710
   257
                            sizeof(RAWINPUTHEADER));
slouken@2710
   258
            raw = (RAWINPUT *) lpb;
slouken@2710
   259
            header = &raw->header;
slouken@2710
   260
            flags = raw->data.mouse.usButtonFlags;
slouken@1895
   261
slouken@2710
   262
            /* we're checking which mouse generated the event */
slouken@2710
   263
            for (i = 0; i < total_mice; ++i) {
slouken@2710
   264
                if (mice[i] == header->hDevice) {
slouken@2710
   265
                    index = i;
slouken@2710
   266
                    break;
slouken@2710
   267
                }
slouken@2710
   268
            }
slouken@3260
   269
            if (index < 0) {
slouken@3260
   270
                /* New mouse?  Should we dynamically update mouse list? */
slouken@3566
   271
                returnCode = 0;
slouken@3566
   272
                break;
slouken@3260
   273
            }
slouken@3139
   274
slouken@2710
   275
            GetCursorPos(&point);
slouken@2974
   276
            ScreenToClient(hwnd, &point);
slouken@2974
   277
slouken@3685
   278
            SDL_GetWindowSize(data->window, &w, &h);
slouken@2974
   279
            if (point.x >= 0 && point.y >= 0 && point.x < w && point.y < h) {
slouken@3685
   280
                SDL_SetMouseFocus(index, data->window);
slouken@2974
   281
            } else {
slouken@2974
   282
                SDL_SetMouseFocus(index, 0);
slouken@2974
   283
                /* FIXME: Should we be doing anything else here? */
slouken@2974
   284
                break;
slouken@2974
   285
            }
slouken@1895
   286
slouken@2710
   287
            /* if the message was sent by a tablet we have to send also pressure */
slouken@2794
   288
            if (index == tablet) {
slouken@2724
   289
                SDL_SendMouseMotion(index, 0, point.x, point.y, pressure);
slouken@2710
   290
            } else {
slouken@2724
   291
                SDL_SendMouseMotion(index, 0, point.x, point.y, 0);
slouken@1895
   292
            }
slouken@2710
   293
            /* we're sending mouse buttons messages to check up if sth changed */
slouken@2766
   294
            if (flags & RI_MOUSE_LEFT_BUTTON_DOWN) {
slouken@2710
   295
                SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@2766
   296
            } else if (flags & RI_MOUSE_LEFT_BUTTON_UP) {
slouken@2710
   297
                SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@2710
   298
            }
slouken@2766
   299
            if (flags & RI_MOUSE_MIDDLE_BUTTON_DOWN) {
slouken@2710
   300
                SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_MIDDLE);
slouken@2766
   301
            } else if (flags & RI_MOUSE_MIDDLE_BUTTON_UP) {
slouken@2710
   302
                SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_MIDDLE);
slouken@2710
   303
            }
slouken@2766
   304
            if (flags & RI_MOUSE_RIGHT_BUTTON_DOWN) {
slouken@2710
   305
                SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_RIGHT);
slouken@2766
   306
            } else if (flags & RI_MOUSE_RIGHT_BUTTON_UP) {
slouken@2710
   307
                SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_RIGHT);
slouken@2710
   308
            }
slouken@2711
   309
            if (flags & RI_MOUSE_BUTTON_4_DOWN) {
slouken@2711
   310
                SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_X1);
slouken@2711
   311
            } else if (flags & RI_MOUSE_BUTTON_4_UP) {
slouken@2711
   312
                SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_X1);
slouken@2711
   313
            }
slouken@2711
   314
            if (flags & RI_MOUSE_BUTTON_5_DOWN) {
slouken@2711
   315
                SDL_SendMouseButton(index, SDL_PRESSED, SDL_BUTTON_X2);
slouken@2711
   316
            } else if (flags & RI_MOUSE_BUTTON_5_UP) {
slouken@2711
   317
                SDL_SendMouseButton(index, SDL_RELEASED, SDL_BUTTON_X2);
slouken@2711
   318
            }
slouken@2710
   319
            if (flags & RI_MOUSE_WHEEL) {
slouken@2975
   320
                SDL_SendMouseWheel(index, 0,
slouken@2990
   321
                                   (short) raw->data.mouse.usButtonData);
slouken@1895
   322
            }
slouken@2766
   323
            SDL_stack_free(lpb);
slouken@1895
   324
        }
slouken@3566
   325
        returnCode = 0;
slouken@3566
   326
        break;
slouken@3097
   327
#endif /* _WIN32_WCE */
slouken@3139
   328
slouken@1895
   329
    case WM_MOUSELEAVE:
slouken@1895
   330
        {
slouken@2974
   331
            int i;
slouken@1895
   332
slouken@2974
   333
            for (i = 0; i < SDL_GetNumMice(); ++i) {
slouken@2974
   334
                SDL_Mouse *mouse = SDL_GetMouse(i);
slouken@1895
   335
slouken@3685
   336
                if (mouse->focus == data->window) {
slouken@2974
   337
                    SDL_SetMouseFocus(i, 0);
slouken@2974
   338
                }
slouken@1895
   339
            }
slouken@1895
   340
        }
slouken@3566
   341
        returnCode = 0;
slouken@3566
   342
        break;
slouken@1895
   343
slouken@1895
   344
    case WM_SYSKEYDOWN:
slouken@1895
   345
    case WM_KEYDOWN:
slouken@1895
   346
        {
slouken@1895
   347
            int index;
slouken@1895
   348
slouken@1895
   349
            /* Ignore repeated keys */
slouken@1895
   350
            if (lParam & REPEATED_KEYMASK) {
slouken@3566
   351
                returnCode = 0;
slouken@3566
   352
                break;
slouken@1895
   353
            }
slouken@1895
   354
slouken@1895
   355
            index = data->videodata->keyboard;
slouken@2310
   356
            wParam = RemapVKEY(wParam, lParam);
slouken@1895
   357
            switch (wParam) {
slouken@1895
   358
            case VK_CONTROL:
slouken@1895
   359
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   360
                    wParam = VK_RCONTROL;
slouken@1895
   361
                else
slouken@1895
   362
                    wParam = VK_LCONTROL;
slouken@1895
   363
                break;
slouken@1895
   364
            case VK_SHIFT:
slouken@1895
   365
                /* EXTENDED trick doesn't work here */
slouken@1895
   366
                {
slouken@2308
   367
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   368
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_RELEASED
slouken@1895
   369
                        && (GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   370
                        wParam = VK_LSHIFT;
slouken@2308
   371
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_RELEASED
slouken@1895
   372
                               && (GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   373
                        wParam = VK_RSHIFT;
slouken@1895
   374
                    } else {
slouken@1895
   375
                        /* Probably a key repeat */
slouken@3566
   376
                        wParam = 256;
slouken@1895
   377
                    }
slouken@1895
   378
                }
slouken@1895
   379
                break;
slouken@1895
   380
            case VK_MENU:
slouken@1895
   381
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   382
                    wParam = VK_RMENU;
slouken@1895
   383
                else
slouken@1895
   384
                    wParam = VK_LMENU;
slouken@1895
   385
                break;
slouken@2313
   386
            case VK_RETURN:
slouken@2313
   387
                if (lParam & EXTENDED_KEYMASK)
slouken@2313
   388
                    wParam = VK_ENTER;
slouken@2313
   389
                break;
slouken@1895
   390
            }
slouken@2308
   391
            if (wParam < 256) {
slouken@2308
   392
                SDL_SendKeyboardKey(index, SDL_PRESSED,
slouken@2308
   393
                                    data->videodata->key_layout[wParam]);
slouken@2308
   394
            }
slouken@1895
   395
        }
slouken@3566
   396
        returnCode = 0;
slouken@3566
   397
        break;
slouken@1895
   398
slouken@1895
   399
    case WM_SYSKEYUP:
slouken@1895
   400
    case WM_KEYUP:
slouken@1895
   401
        {
slouken@1895
   402
            int index;
slouken@1895
   403
slouken@1895
   404
            index = data->videodata->keyboard;
slouken@2310
   405
            wParam = RemapVKEY(wParam, lParam);
slouken@1895
   406
            switch (wParam) {
slouken@1895
   407
            case VK_CONTROL:
slouken@1895
   408
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   409
                    wParam = VK_RCONTROL;
slouken@1895
   410
                else
slouken@1895
   411
                    wParam = VK_LCONTROL;
slouken@1895
   412
                break;
slouken@1895
   413
            case VK_SHIFT:
slouken@1895
   414
                /* EXTENDED trick doesn't work here */
slouken@1895
   415
                {
slouken@2308
   416
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   417
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_PRESSED
slouken@1895
   418
                        && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   419
                        wParam = VK_LSHIFT;
slouken@2308
   420
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_PRESSED
slouken@1895
   421
                               && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   422
                        wParam = VK_RSHIFT;
slouken@1895
   423
                    } else {
slouken@1895
   424
                        /* Probably a key repeat */
slouken@3566
   425
                        wParam = 256;
slouken@1895
   426
                    }
slouken@1895
   427
                }
slouken@1895
   428
                break;
slouken@1895
   429
            case VK_MENU:
slouken@1895
   430
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   431
                    wParam = VK_RMENU;
slouken@1895
   432
                else
slouken@1895
   433
                    wParam = VK_LMENU;
slouken@1895
   434
                break;
slouken@2313
   435
            case VK_RETURN:
slouken@2313
   436
                if (lParam & EXTENDED_KEYMASK)
slouken@2313
   437
                    wParam = VK_ENTER;
slouken@2313
   438
                break;
slouken@1895
   439
            }
slouken@2710
   440
slouken@1895
   441
            /* Windows only reports keyup for print screen */
slouken@1895
   442
            if (wParam == VK_SNAPSHOT
slouken@2308
   443
                && SDL_GetKeyboardState(NULL)[SDL_SCANCODE_PRINTSCREEN] ==
slouken@2308
   444
                SDL_RELEASED) {
slouken@1895
   445
                SDL_SendKeyboardKey(index, SDL_PRESSED,
slouken@2308
   446
                                    data->videodata->key_layout[wParam]);
slouken@1895
   447
            }
slouken@2308
   448
            if (wParam < 256) {
slouken@2308
   449
                SDL_SendKeyboardKey(index, SDL_RELEASED,
slouken@2308
   450
                                    data->videodata->key_layout[wParam]);
slouken@2308
   451
            }
slouken@1895
   452
        }
slouken@3566
   453
        returnCode = 0;
slouken@3566
   454
        break;
slouken@1895
   455
slouken@2309
   456
    case WM_CHAR:
slouken@2309
   457
        {
slouken@2309
   458
            char text[4];
slouken@2309
   459
slouken@2309
   460
            /* Convert to UTF-8 and send it on... */
slouken@2309
   461
            if (wParam <= 0x7F) {
slouken@2309
   462
                text[0] = (char) wParam;
slouken@2309
   463
                text[1] = '\0';
slouken@2309
   464
            } else if (wParam <= 0x7FF) {
slouken@2309
   465
                text[0] = 0xC0 | (char) ((wParam >> 6) & 0x1F);
slouken@2309
   466
                text[1] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   467
                text[2] = '\0';
slouken@2309
   468
            } else {
slouken@2309
   469
                text[0] = 0xE0 | (char) ((wParam >> 12) & 0x0F);
slouken@2309
   470
                text[1] = 0x80 | (char) ((wParam >> 6) & 0x3F);
slouken@2309
   471
                text[2] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   472
                text[3] = '\0';
slouken@2309
   473
            }
slouken@2309
   474
            SDL_SendKeyboardText(data->videodata->keyboard, text);
slouken@2309
   475
        }
slouken@3566
   476
        returnCode = 0;
slouken@3566
   477
        break;
slouken@2309
   478
slouken@2311
   479
    case WM_INPUTLANGCHANGE:
slouken@2311
   480
        {
slouken@2311
   481
            WIN_UpdateKeymap(data->videodata->keyboard);
slouken@2311
   482
        }
slouken@3566
   483
        returnCode = 1;
slouken@3566
   484
        break;
slouken@2311
   485
slouken@1895
   486
    case WM_GETMINMAXINFO:
slouken@1895
   487
        {
slouken@1895
   488
            MINMAXINFO *info;
slouken@1895
   489
            RECT size;
slouken@1895
   490
            int x, y;
slouken@1895
   491
            int w, h;
slouken@1895
   492
            int style;
slouken@3168
   493
            BOOL menu;
slouken@1895
   494
slouken@1895
   495
            /* If we allow resizing, let the resize happen naturally */
eligottlieb@4788
   496
            if(SDL_IsShapedWindow(data->window))
eligottlieb@4815
   497
                Win32_ResizeWindowShape(data->window);
slouken@3685
   498
            if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
slouken@3566
   499
                returnCode = 0;
slouken@3566
   500
                break;
slouken@1895
   501
            }
slouken@1895
   502
slouken@1895
   503
            /* Get the current position of our window */
slouken@1895
   504
            GetWindowRect(hwnd, &size);
slouken@1895
   505
            x = size.left;
slouken@1895
   506
            y = size.top;
slouken@1895
   507
slouken@1895
   508
            /* Calculate current size of our window */
slouken@3685
   509
            SDL_GetWindowSize(data->window, &w, &h);
slouken@1895
   510
            size.top = 0;
slouken@1895
   511
            size.left = 0;
slouken@1895
   512
            size.bottom = h;
slouken@1895
   513
            size.right = w;
slouken@1895
   514
slouken@3168
   515
slouken@3168
   516
            style = GetWindowLong(hwnd, GWL_STYLE);
slouken@3168
   517
#ifdef _WIN32_WCE
slouken@3168
   518
            menu = FALSE;
slouken@3168
   519
#else
slouken@1895
   520
            /* DJM - according to the docs for GetMenu(), the
slouken@1895
   521
               return value is undefined if hwnd is a child window.
slouken@1895
   522
               Aparently it's too difficult for MS to check
slouken@1895
   523
               inside their function, so I have to do it here.
slouken@1895
   524
             */
slouken@3168
   525
            menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   526
#endif
slouken@3168
   527
            AdjustWindowRectEx(&size, style, menu, 0);
slouken@1895
   528
            w = size.right - size.left;
slouken@1895
   529
            h = size.bottom - size.top;
slouken@1895
   530
slouken@1895
   531
            /* Fix our size to the current size */
slouken@1895
   532
            info = (MINMAXINFO *) lParam;
slouken@1895
   533
            info->ptMaxSize.x = w;
slouken@1895
   534
            info->ptMaxSize.y = h;
slouken@1895
   535
            info->ptMaxPosition.x = x;
slouken@1895
   536
            info->ptMaxPosition.y = y;
slouken@1895
   537
            info->ptMinTrackSize.x = w;
slouken@1895
   538
            info->ptMinTrackSize.y = h;
slouken@1895
   539
            info->ptMaxTrackSize.x = w;
slouken@1895
   540
            info->ptMaxTrackSize.y = h;
slouken@1895
   541
        }
slouken@3566
   542
        returnCode = 0;
slouken@3566
   543
        break;
slouken@1895
   544
slouken@1895
   545
    case WM_WINDOWPOSCHANGED:
slouken@1895
   546
        {
slouken@1895
   547
            RECT rect;
slouken@1895
   548
            int x, y;
slouken@1895
   549
            int w, h;
slouken@1895
   550
            Uint32 window_flags;
slouken@1895
   551
slouken@3256
   552
            if (!GetClientRect(hwnd, &rect) ||
slouken@3256
   553
                (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@3256
   554
                break;
slouken@3256
   555
            }
slouken@1895
   556
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   557
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   558
slouken@3685
   559
            window_flags = SDL_GetWindowFlags(data->window);
slouken@1895
   560
            if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   561
                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   562
                ClipCursor(&rect);
slouken@1895
   563
            }
slouken@1895
   564
slouken@1895
   565
            x = rect.left;
slouken@1895
   566
            y = rect.top;
slouken@3685
   567
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
   568
slouken@1895
   569
            w = rect.right - rect.left;
slouken@1895
   570
            h = rect.bottom - rect.top;
slouken@3685
   571
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
slouken@1895
   572
                                h);
slouken@1895
   573
        }
slouken@1895
   574
        break;
slouken@1895
   575
slouken@1895
   576
    case WM_SETCURSOR:
slouken@1895
   577
        {
slouken@3076
   578
            Uint16 hittest;
slouken@1895
   579
slouken@3076
   580
            hittest = LOWORD(lParam);
slouken@3076
   581
            if (hittest == HTCLIENT) {
slouken@3076
   582
                /* FIXME: Implement the cursor API */
slouken@3076
   583
                static HCURSOR cursor;
slouken@3076
   584
                if (!cursor) {
slouken@3076
   585
                    cursor = LoadCursor(NULL, IDC_ARROW);
slouken@3076
   586
                }
slouken@3076
   587
                SetCursor(cursor);
slouken@3566
   588
                returnCode = TRUE;
slouken@3076
   589
            }
slouken@1895
   590
        }
slouken@1895
   591
        break;
slouken@1895
   592
slouken@1895
   593
        /* We are about to get palette focus! */
slouken@1895
   594
    case WM_QUERYNEWPALETTE:
slouken@1895
   595
        {
slouken@1895
   596
            /*
slouken@3566
   597
                WIN_RealizePalette(current_video);
slouken@3566
   598
                returnCode = TRUE;
slouken@1895
   599
             */
slouken@1895
   600
        }
slouken@1895
   601
        break;
slouken@1895
   602
slouken@1895
   603
        /* Another application changed the palette */
slouken@1895
   604
    case WM_PALETTECHANGED:
slouken@1895
   605
        {
slouken@1895
   606
            /*
slouken@1895
   607
               WIN_PaletteChanged(current_video, (HWND) wParam);
slouken@1895
   608
             */
slouken@1895
   609
        }
slouken@1895
   610
        break;
slouken@1895
   611
slouken@1895
   612
        /* We were occluded, refresh our display */
slouken@1895
   613
    case WM_PAINT:
slouken@1895
   614
        {
slouken@1895
   615
            RECT rect;
slouken@1895
   616
            if (GetUpdateRect(hwnd, &rect, FALSE)) {
slouken@1895
   617
                ValidateRect(hwnd, &rect);
slouken@3685
   618
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
slouken@1895
   619
                                    0, 0);
slouken@1895
   620
            }
slouken@1895
   621
        }
slouken@3566
   622
        returnCode = 0;
slouken@3566
   623
        break;
slouken@3095
   624
slouken@1895
   625
        /* We'll do our own drawing, prevent flicker */
slouken@1895
   626
    case WM_ERASEBKGND:
slouken@1895
   627
        {
slouken@1895
   628
        }
slouken@1895
   629
        return (1);
slouken@1895
   630
slouken@1895
   631
    case WM_SYSCOMMAND:
slouken@1895
   632
        {
slouken@1895
   633
            /* Don't start the screensaver or blank the monitor in fullscreen apps */
slouken@1895
   634
            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
slouken@1895
   635
                (wParam & 0xFFF0) == SC_MONITORPOWER) {
slouken@3032
   636
                if (SDL_GetVideoDevice()->suspend_screensaver) {
slouken@1895
   637
                    return (0);
slouken@1895
   638
                }
slouken@1895
   639
            }
slouken@1895
   640
        }
slouken@1895
   641
        break;
slouken@1895
   642
slouken@1895
   643
    case WM_CLOSE:
slouken@1895
   644
        {
slouken@3685
   645
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1895
   646
        }
slouken@3566
   647
        returnCode = 0;
slouken@3566
   648
        break;
slouken@1895
   649
    }
slouken@3566
   650
slouken@3566
   651
    /* If there's a window proc, assume it's going to handle messages */
slouken@3566
   652
    if (data->wndproc) {
slouken@3566
   653
        return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
slouken@3566
   654
    } else if (returnCode >= 0) {
slouken@3566
   655
        return returnCode;
slouken@3566
   656
    } else {
slouken@3566
   657
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@3566
   658
    }
slouken@1895
   659
}
slouken@1895
   660
slouken@1895
   661
void
slouken@1895
   662
WIN_PumpEvents(_THIS)
slouken@1895
   663
{
slouken@1895
   664
    MSG msg;
slouken@1895
   665
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
slouken@1895
   666
        TranslateMessage(&msg);
slouken@1895
   667
        DispatchMessage(&msg);
slouken@1895
   668
    }
slouken@1895
   669
}
slouken@1895
   670
slouken@1895
   671
static int app_registered = 0;
slouken@1895
   672
LPTSTR SDL_Appname = NULL;
slouken@1895
   673
Uint32 SDL_Appstyle = 0;
slouken@1895
   674
HINSTANCE SDL_Instance = NULL;
slouken@1895
   675
slouken@1895
   676
/* Register the class for this application */
slouken@1895
   677
int
slouken@1895
   678
SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@1895
   679
{
slouken@1895
   680
    WNDCLASS class;
slouken@1895
   681
slouken@1895
   682
    /* Only do this once... */
slouken@1895
   683
    if (app_registered) {
slouken@1895
   684
        ++app_registered;
slouken@1895
   685
        return (0);
slouken@1895
   686
    }
slouken@1895
   687
    if (!name && !SDL_Appname) {
slouken@1895
   688
        name = "SDL_app";
slouken@1895
   689
        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
slouken@1895
   690
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   691
    }
slouken@1895
   692
slouken@1895
   693
    if (name) {
slouken@1895
   694
        SDL_Appname = WIN_UTF8ToString(name);
slouken@1895
   695
        SDL_Appstyle = style;
slouken@1895
   696
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   697
    }
slouken@1895
   698
slouken@1895
   699
    /* Register the application class */
slouken@1895
   700
    class.hCursor = NULL;
slouken@2710
   701
    class.hIcon =
slouken@2710
   702
        LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
slouken@2710
   703
                  LR_DEFAULTCOLOR);
slouken@1895
   704
    class.lpszMenuName = NULL;
slouken@1895
   705
    class.lpszClassName = SDL_Appname;
slouken@1895
   706
    class.hbrBackground = NULL;
slouken@1895
   707
    class.hInstance = SDL_Instance;
slouken@1895
   708
    class.style = SDL_Appstyle;
slouken@1895
   709
    class.lpfnWndProc = DefWindowProc;
slouken@1895
   710
    class.cbWndExtra = 0;
slouken@1895
   711
    class.cbClsExtra = 0;
slouken@1895
   712
    if (!RegisterClass(&class)) {
slouken@1895
   713
        SDL_SetError("Couldn't register application class");
slouken@1895
   714
        return (-1);
slouken@1895
   715
    }
slouken@1895
   716
slouken@1895
   717
    app_registered = 1;
slouken@1895
   718
    return (0);
slouken@1895
   719
}
slouken@1895
   720
slouken@1895
   721
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
slouken@1895
   722
void
slouken@1895
   723
SDL_UnregisterApp()
slouken@1895
   724
{
slouken@1895
   725
    WNDCLASS class;
slouken@1895
   726
slouken@1895
   727
    /* SDL_RegisterApp might not have been called before */
slouken@1895
   728
    if (!app_registered) {
slouken@1895
   729
        return;
slouken@1895
   730
    }
slouken@1895
   731
    --app_registered;
slouken@1895
   732
    if (app_registered == 0) {
slouken@1895
   733
        /* Check for any registered window classes. */
slouken@1895
   734
        if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
slouken@1895
   735
            UnregisterClass(SDL_Appname, SDL_Instance);
slouken@1895
   736
        }
slouken@1895
   737
        SDL_free(SDL_Appname);
slouken@1895
   738
        SDL_Appname = NULL;
slouken@1895
   739
    }
slouken@1895
   740
}
slouken@1895
   741
slouken@1895
   742
/* Sets an error message based on GetLastError() */
slouken@1895
   743
void
slouken@1895
   744
WIN_SetError(const char *prefix)
slouken@1895
   745
{
slouken@1895
   746
    TCHAR buffer[1024];
slouken@1895
   747
    char *message;
slouken@2710
   748
    FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0,
slouken@2710
   749
                  buffer, SDL_arraysize(buffer), NULL);
slouken@1895
   750
    message = WIN_StringToUTF8(buffer);
slouken@3565
   751
    SDL_SetError("%s%s%s", prefix ? prefix : "", prefix ? ": " : "", message);
slouken@1895
   752
    SDL_free(message);
slouken@1895
   753
}
slouken@1895
   754
slouken@1895
   755
/* vi: set ts=4 sw=4 expandtab: */