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