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