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