src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Dec 2012 12:15:25 -0800
changeset 6788 036f53f2f5aa
parent 6782 582d35419e8a
child 6837 a69e10925a58
permissions -rw-r--r--
Added SDL_SetWindowMaximumSize() and SDL_GetWindowMaximumSize()
Also fixed Cocoa implementation so that it affects client area, not the whole window area.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1895
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1895
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1895
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1895
    20
*/
slouken@6044
    21
#include "SDL_config.h"
slouken@2710
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_WINDOWS
slouken@1895
    24
slouken@5062
    25
#include "SDL_windowsvideo.h"
slouken@5062
    26
#include "SDL_windowsshape.h"
slouken@1895
    27
#include "SDL_syswm.h"
slouken@1895
    28
#include "SDL_vkeys.h"
slouken@1895
    29
#include "../../events/SDL_events_c.h"
slouken@4919
    30
#include "../../events/SDL_touch_c.h"
slouken@1895
    31
slouken@6523
    32
/* Dropfile support */
slouken@6523
    33
#include <shellapi.h>
slouken@6523
    34
slouken@6523
    35
jimtla@4650
    36
jimtla@4650
    37
slouken@4868
    38
/*#define WMMSG_DEBUG*/
slouken@1895
    39
#ifdef WMMSG_DEBUG
jimtla@4650
    40
#include <stdio.h>	
slouken@1895
    41
#include "wmmsg.h"
slouken@1895
    42
#endif
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@4932
    63
#ifndef WM_TOUCH
slouken@4868
    64
#define WM_TOUCH 0x0240
slouken@4932
    65
#endif
slouken@4919
    66
slouken@1895
    67
slouken@2310
    68
static WPARAM
slouken@2310
    69
RemapVKEY(WPARAM wParam, LPARAM lParam)
slouken@2310
    70
{
slouken@2317
    71
    int i;
bob@2324
    72
    BYTE scancode = (BYTE) ((lParam >> 16) & 0xFF);
slouken@2317
    73
slouken@2310
    74
    /* Windows remaps alphabetic keys based on current layout.
slouken@2310
    75
       We try to provide USB scancodes, so undo this mapping.
slouken@2310
    76
     */
slouken@2310
    77
    if (wParam >= 'A' && wParam <= 'Z') {
slouken@2311
    78
        if (scancode != alpha_scancodes[wParam - 'A']) {
slouken@2311
    79
            for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
slouken@2311
    80
                if (scancode == alpha_scancodes[i]) {
slouken@2310
    81
                    wParam = 'A' + i;
slouken@2310
    82
                    break;
slouken@2310
    83
                }
slouken@2310
    84
            }
slouken@2310
    85
        }
slouken@2310
    86
    }
slouken@2317
    87
slouken@3700
    88
    /* Keypad keys are a little trickier, we always scan for them.
slouken@3700
    89
       Keypad arrow keys have the same scancode as normal arrow keys,
slouken@3700
    90
       except they don't have the extended bit (0x1000000) set.
slouken@3700
    91
     */
slouken@3700
    92
    if (!(lParam & 0x1000000)) {
slouken@4561
    93
        if (wParam == VK_DELETE) {
slouken@4561
    94
            wParam = VK_DECIMAL;
slouken@4561
    95
        } else {
slouken@4561
    96
            for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
slouken@4561
    97
                if (scancode == keypad_scancodes[i]) {
slouken@4561
    98
                    wParam = VK_NUMPAD0 + i;
slouken@4561
    99
                    break;
slouken@4561
   100
                }
slouken@3700
   101
            }
slouken@2317
   102
        }
slouken@2317
   103
    }
slouken@2317
   104
slouken@2310
   105
    return wParam;
slouken@2310
   106
}
slouken@2310
   107
slouken@1895
   108
LRESULT CALLBACK
slouken@1895
   109
WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@1895
   110
{
slouken@1895
   111
    SDL_WindowData *data;
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@4900
   119
        wmmsg.subsystem = SDL_SYSWM_WINDOWS;
slouken@5056
   120
        wmmsg.msg.win.hwnd = hwnd;
slouken@5056
   121
        wmmsg.msg.win.msg = msg;
slouken@5056
   122
        wmmsg.msg.win.wParam = wParam;
slouken@5056
   123
        wmmsg.msg.win.lParam = lParam;
slouken@1951
   124
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   125
    }
slouken@1951
   126
slouken@1895
   127
    /* Get the window data for the window */
slouken@1895
   128
    data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
slouken@1895
   129
    if (!data) {
slouken@1895
   130
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@1895
   131
    }
jimtla@4650
   132
slouken@1895
   133
#ifdef WMMSG_DEBUG
jimtla@4650
   134
    {		
jimtla@4650
   135
        FILE *log = fopen("wmmsg.txt", "a");		
slouken@1913
   136
        fprintf(log, "Received windows message: %p ", hwnd);
slouken@1913
   137
        if (msg > MAX_WMMSG) {
slouken@1913
   138
            fprintf(log, "%d", msg);
slouken@1913
   139
        } else {
slouken@1913
   140
            fprintf(log, "%s", wmtab[msg]);
slouken@1913
   141
        }
slouken@1913
   142
        fprintf(log, " -- 0x%X, 0x%X\n", wParam, lParam);
slouken@1913
   143
        fclose(log);
slouken@1895
   144
    }
slouken@4868
   145
#endif
slouken@2710
   146
dewyatt@4752
   147
    if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata))
dewyatt@4752
   148
        return 0;
slouken@1895
   149
slouken@1895
   150
    switch (msg) {
slouken@1895
   151
slouken@1895
   152
    case WM_SHOWWINDOW:
slouken@1895
   153
        {
slouken@1895
   154
            if (wParam) {
slouken@3685
   155
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
   156
            } else {
slouken@3685
   157
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
   158
            }
slouken@1895
   159
        }
slouken@1895
   160
        break;
slouken@1895
   161
slouken@1895
   162
    case WM_ACTIVATE:
slouken@1895
   163
        {
slouken@1895
   164
            BOOL minimized;
slouken@1895
   165
slouken@1895
   166
            minimized = HIWORD(wParam);
slouken@1895
   167
            if (!minimized && (LOWORD(wParam) != WA_INACTIVE)) {
slouken@3685
   168
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   169
                SDL_SendWindowEvent(data->window,
slouken@1895
   170
                                    SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1895
   171
                if (IsZoomed(hwnd)) {
slouken@3685
   172
                    SDL_SendWindowEvent(data->window,
slouken@1895
   173
                                        SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
   174
                }
slouken@4465
   175
                if (SDL_GetKeyboardFocus() != data->window) {
slouken@4465
   176
                    SDL_SetKeyboardFocus(data->window);
slouken@1895
   177
                }
slouken@6350
   178
slouken@6350
   179
				if(SDL_GetMouse()->relative_mode) {
slouken@6350
   180
					LONG cx, cy;
slouken@6350
   181
					RECT rect;
slouken@6350
   182
					GetWindowRect(hwnd, &rect);
slouken@6350
   183
slouken@6350
   184
					cx = (rect.left + rect.right) / 2;
slouken@6350
   185
					cy = (rect.top + rect.bottom) / 2;
slouken@6350
   186
slouken@6350
   187
					/* Make an absurdly small clip rect */
slouken@6350
   188
					rect.left = cx-1;
slouken@6350
   189
					rect.right = cx+1;
slouken@6350
   190
					rect.top = cy-1;
slouken@6350
   191
					rect.bottom = cy+1;
slouken@6350
   192
slouken@6350
   193
					ClipCursor(&rect);
slouken@6350
   194
				}
slouken@6350
   195
slouken@4504
   196
                /*
slouken@4504
   197
                 * FIXME: Update keyboard state
slouken@4504
   198
                 */
slouken@4504
   199
                WIN_CheckClipboardUpdate(data->videodata);
slouken@1895
   200
            } else {
slouken@4465
   201
                if (SDL_GetKeyboardFocus() == data->window) {
slouken@4465
   202
                    SDL_SetKeyboardFocus(NULL);
slouken@1895
   203
                }
slouken@1895
   204
                if (minimized) {
slouken@3685
   205
                    SDL_SendWindowEvent(data->window,
slouken@1895
   206
                                        SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
   207
                }
slouken@1895
   208
            }
slouken@1895
   209
        }
slouken@3566
   210
        returnCode = 0;
slouken@3566
   211
        break;
slouken@1895
   212
slouken@4470
   213
	case WM_MOUSEMOVE:
slouken@6350
   214
		if(SDL_GetMouse()->relative_mode)
slouken@6350
   215
			break;
slouken@4484
   216
        SDL_SendMouseMotion(data->window, 0, LOWORD(lParam), HIWORD(lParam));
slouken@3139
   217
        break;
slouken@3139
   218
slouken@6350
   219
	case WM_INPUT:
slouken@6350
   220
	{
slouken@6350
   221
		HRAWINPUT hRawInput = (HRAWINPUT)lParam;
slouken@6350
   222
		RAWINPUT inp;
slouken@6350
   223
		UINT size = sizeof(inp);
slouken@6782
   224
slouken@6782
   225
		if(!SDL_GetMouse()->relative_mode)
slouken@6782
   226
			break;
slouken@6782
   227
slouken@6350
   228
		GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
slouken@6350
   229
slouken@6350
   230
		/* Mouse data */
slouken@6350
   231
		if(inp.header.dwType == RIM_TYPEMOUSE)
slouken@6350
   232
		{
slouken@6350
   233
			RAWMOUSE* mouse = &inp.data.mouse;
slouken@6350
   234
slouken@6350
   235
			if((mouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE)
slouken@6782
   236
			{
slouken@6350
   237
				SDL_SendMouseMotion(data->window, 1, (int)mouse->lLastX, (int)mouse->lLastY);
slouken@6782
   238
			}
slouken@6782
   239
			else
slouken@6782
   240
			{
slouken@6782
   241
				// synthesize relative moves from the abs position
slouken@6782
   242
				static SDL_Point initialMousePoint;
slouken@6782
   243
				if ( initialMousePoint.x == 0 && initialMousePoint.y == 0 )
slouken@6782
   244
				{
slouken@6782
   245
					initialMousePoint.x = mouse->lLastX;
slouken@6782
   246
					initialMousePoint.y = mouse->lLastY;
slouken@6782
   247
				}
slouken@6350
   248
slouken@6782
   249
				SDL_SendMouseMotion(data->window, 1, (int)(mouse->lLastX-initialMousePoint.x), (int)(mouse->lLastY-initialMousePoint.y) );
slouken@6782
   250
slouken@6782
   251
				initialMousePoint.x = mouse->lLastX;
slouken@6782
   252
				initialMousePoint.y = mouse->lLastY;
slouken@6782
   253
			}
slouken@6350
   254
		}
slouken@6350
   255
		break;
slouken@6350
   256
	}
slouken@6350
   257
slouken@3097
   258
    case WM_LBUTTONDOWN:
slouken@4484
   259
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@3139
   260
        break;
slouken@3139
   261
slouken@3097
   262
    case WM_LBUTTONUP:
slouken@4484
   263
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@3139
   264
        break;
slouken@3139
   265
slouken@5049
   266
    case WM_RBUTTONDOWN:
slouken@5049
   267
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_RIGHT);
slouken@5049
   268
        break;
slouken@5049
   269
slouken@5049
   270
    case WM_RBUTTONUP:
slouken@5049
   271
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_RIGHT);
slouken@5049
   272
        break;
slouken@5049
   273
slouken@5049
   274
    case WM_MBUTTONDOWN:
slouken@5049
   275
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_MIDDLE);
slouken@5049
   276
        break;
slouken@5049
   277
slouken@5049
   278
    case WM_MBUTTONUP:
slouken@5049
   279
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_MIDDLE);
slouken@5049
   280
        break;
slouken@5049
   281
slouken@5049
   282
    case WM_XBUTTONDOWN:
slouken@5049
   283
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_X1 + GET_XBUTTON_WPARAM(wParam) - 1);
slouken@5049
   284
        returnCode = TRUE;
slouken@5049
   285
        break;
slouken@5049
   286
slouken@5049
   287
    case WM_XBUTTONUP:
slouken@5049
   288
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_X1 + GET_XBUTTON_WPARAM(wParam) - 1);
slouken@5049
   289
        returnCode = TRUE;
slouken@5049
   290
        break;
slouken@5049
   291
slouken@5049
   292
    case WM_MOUSEWHEEL:
slouken@5049
   293
        {
slouken@5049
   294
            int motion = (short) HIWORD(wParam);
slouken@5049
   295
slouken@5049
   296
            SDL_SendMouseWheel(data->window, 0, motion);
slouken@5049
   297
            break;
slouken@5049
   298
        }
slouken@5049
   299
slouken@5086
   300
#ifdef WM_MOUSELEAVE
slouken@5086
   301
    /* FIXME: Do we need the SDL 1.2 hack to generate WM_MOUSELEAVE now? */
slouken@1895
   302
    case WM_MOUSELEAVE:
slouken@4470
   303
        if (SDL_GetMouseFocus() == data->window) {
slouken@4470
   304
            SDL_SetMouseFocus(NULL);
slouken@1895
   305
        }
slouken@3566
   306
        returnCode = 0;
slouken@3566
   307
        break;
slouken@5086
   308
#endif /* WM_MOUSELEAVE */
slouken@1895
   309
slouken@1895
   310
    case WM_SYSKEYDOWN:
slouken@1895
   311
    case WM_KEYDOWN:
slouken@1895
   312
        {
slouken@2310
   313
            wParam = RemapVKEY(wParam, lParam);
slouken@1895
   314
            switch (wParam) {
slouken@1895
   315
            case VK_CONTROL:
slouken@1895
   316
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   317
                    wParam = VK_RCONTROL;
slouken@1895
   318
                else
slouken@1895
   319
                    wParam = VK_LCONTROL;
slouken@1895
   320
                break;
slouken@1895
   321
            case VK_SHIFT:
slouken@1895
   322
                /* EXTENDED trick doesn't work here */
slouken@1895
   323
                {
slouken@2308
   324
                    Uint8 *state = SDL_GetKeyboardState(NULL);
slouken@2308
   325
                    if (state[SDL_SCANCODE_LSHIFT] == SDL_RELEASED
slouken@1895
   326
                        && (GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   327
                        wParam = VK_LSHIFT;
slouken@2308
   328
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_RELEASED
slouken@1895
   329
                               && (GetKeyState(VK_RSHIFT) & 0x8000)) {
slouken@1895
   330
                        wParam = VK_RSHIFT;
slouken@1895
   331
                    } else {
slouken@1895
   332
                        /* Probably a key repeat */
slouken@3566
   333
                        wParam = 256;
slouken@1895
   334
                    }
slouken@1895
   335
                }
slouken@1895
   336
                break;
slouken@1895
   337
            case VK_MENU:
slouken@1895
   338
                if (lParam & EXTENDED_KEYMASK)
slouken@1895
   339
                    wParam = VK_RMENU;
slouken@1895
   340
                else
slouken@1895
   341
                    wParam = VK_LMENU;
slouken@1895
   342
                break;
slouken@2313
   343
            case VK_RETURN:
slouken@2313
   344
                if (lParam & EXTENDED_KEYMASK)
slouken@2313
   345
                    wParam = VK_ENTER;
slouken@2313
   346
                break;
slouken@1895
   347
            }
slouken@2308
   348
            if (wParam < 256) {
slouken@4465
   349
                SDL_SendKeyboardKey(SDL_PRESSED,
slouken@2308
   350
                                    data->videodata->key_layout[wParam]);
slouken@2308
   351
            }
slouken@1895
   352
        }
slouken@3566
   353
        returnCode = 0;
slouken@3566
   354
        break;
slouken@1895
   355
slouken@1895
   356
    case WM_SYSKEYUP:
slouken@1895
   357
    case WM_KEYUP:
slouken@1895
   358
        {
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_PRESSED
slouken@1895
   372
                        && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
slouken@1895
   373
                        wParam = VK_LSHIFT;
slouken@2308
   374
                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_PRESSED
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@2710
   394
slouken@1895
   395
            /* Windows only reports keyup for print screen */
slouken@1895
   396
            if (wParam == VK_SNAPSHOT
slouken@2308
   397
                && SDL_GetKeyboardState(NULL)[SDL_SCANCODE_PRINTSCREEN] ==
slouken@2308
   398
                SDL_RELEASED) {
slouken@4465
   399
                SDL_SendKeyboardKey(SDL_PRESSED,
slouken@2308
   400
                                    data->videodata->key_layout[wParam]);
slouken@1895
   401
            }
slouken@2308
   402
            if (wParam < 256) {
slouken@4465
   403
                SDL_SendKeyboardKey(SDL_RELEASED,
slouken@2308
   404
                                    data->videodata->key_layout[wParam]);
slouken@2308
   405
            }
slouken@1895
   406
        }
slouken@3566
   407
        returnCode = 0;
slouken@3566
   408
        break;
slouken@1895
   409
slouken@2309
   410
    case WM_CHAR:
slouken@2309
   411
        {
slouken@2309
   412
            char text[4];
slouken@2309
   413
slouken@2309
   414
            /* Convert to UTF-8 and send it on... */
slouken@2309
   415
            if (wParam <= 0x7F) {
slouken@2309
   416
                text[0] = (char) wParam;
slouken@2309
   417
                text[1] = '\0';
slouken@2309
   418
            } else if (wParam <= 0x7FF) {
slouken@2309
   419
                text[0] = 0xC0 | (char) ((wParam >> 6) & 0x1F);
slouken@2309
   420
                text[1] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   421
                text[2] = '\0';
slouken@2309
   422
            } else {
slouken@2309
   423
                text[0] = 0xE0 | (char) ((wParam >> 12) & 0x0F);
slouken@2309
   424
                text[1] = 0x80 | (char) ((wParam >> 6) & 0x3F);
slouken@2309
   425
                text[2] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   426
                text[3] = '\0';
slouken@2309
   427
            }
slouken@4465
   428
            SDL_SendKeyboardText(text);
slouken@2309
   429
        }
slouken@3566
   430
        returnCode = 0;
slouken@3566
   431
        break;
slouken@2309
   432
slouken@5086
   433
#ifdef WM_INPUTLANGCHANGE
slouken@2311
   434
    case WM_INPUTLANGCHANGE:
slouken@2311
   435
        {
slouken@4465
   436
            WIN_UpdateKeymap();
slouken@2311
   437
        }
slouken@3566
   438
        returnCode = 1;
slouken@3566
   439
        break;
slouken@5086
   440
#endif /* WM_INPUTLANGCHANGE */
slouken@2311
   441
slouken@5086
   442
#ifdef WM_GETMINMAXINFO
slouken@1895
   443
    case WM_GETMINMAXINFO:
slouken@1895
   444
        {
slouken@1895
   445
            MINMAXINFO *info;
slouken@1895
   446
            RECT size;
slouken@1895
   447
            int x, y;
slouken@1895
   448
            int w, h;
stopiccot@6682
   449
            int min_w, min_h;
slouken@6788
   450
            int max_w, max_h;
slouken@1895
   451
            int style;
slouken@3168
   452
            BOOL menu;
slouken@1895
   453
slouken@1895
   454
            /* If we allow resizing, let the resize happen naturally */
stopiccot@6682
   455
            if (SDL_IsShapedWindow(data->window))
eligottlieb@4815
   456
                Win32_ResizeWindowShape(data->window);
slouken@1895
   457
slouken@1895
   458
            /* Get the current position of our window */
slouken@1895
   459
            GetWindowRect(hwnd, &size);
slouken@1895
   460
            x = size.left;
slouken@1895
   461
            y = size.top;
slouken@1895
   462
slouken@1895
   463
            /* Calculate current size of our window */
slouken@3685
   464
            SDL_GetWindowSize(data->window, &w, &h);
stopiccot@6682
   465
            SDL_GetWindowMinimumSize(data->window, &min_w, &min_h);
slouken@6788
   466
            SDL_GetWindowMaximumSize(data->window, &max_w, &max_h);
stopiccot@6682
   467
stopiccot@6682
   468
            /* Store in min_w and min_h difference between current size and minimal 
stopiccot@6682
   469
               size so we don't need to call AdjustWindowRectEx twice */
stopiccot@6682
   470
            min_w -= w;
stopiccot@6682
   471
            min_h -= h;
slouken@6788
   472
            max_w -= w;
slouken@6788
   473
            max_h -= h;
stopiccot@6682
   474
slouken@1895
   475
            size.top = 0;
slouken@1895
   476
            size.left = 0;
slouken@1895
   477
            size.bottom = h;
slouken@1895
   478
            size.right = w;
slouken@1895
   479
slouken@3168
   480
            style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   481
            /* DJM - according to the docs for GetMenu(), the
slouken@1895
   482
               return value is undefined if hwnd is a child window.
slouken@1895
   483
               Aparently it's too difficult for MS to check
slouken@1895
   484
               inside their function, so I have to do it here.
slouken@1895
   485
             */
slouken@3168
   486
            menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   487
            AdjustWindowRectEx(&size, style, menu, 0);
slouken@1895
   488
            w = size.right - size.left;
slouken@1895
   489
            h = size.bottom - size.top;
slouken@1895
   490
slouken@1895
   491
            /* Fix our size to the current size */
slouken@1895
   492
            info = (MINMAXINFO *) lParam;
stopiccot@6682
   493
            if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
stopiccot@6682
   494
                info->ptMinTrackSize.x = w + min_w;
stopiccot@6682
   495
                info->ptMinTrackSize.y = h + min_h;
slouken@6788
   496
                info->ptMaxTrackSize.x = w + max_w;
slouken@6788
   497
                info->ptMaxTrackSize.y = h + max_h;
stopiccot@6682
   498
            } else {
stopiccot@6682
   499
                info->ptMaxSize.x = w;
stopiccot@6682
   500
                info->ptMaxSize.y = h;
stopiccot@6682
   501
                info->ptMaxPosition.x = x;
stopiccot@6682
   502
                info->ptMaxPosition.y = y;
stopiccot@6682
   503
                info->ptMinTrackSize.x = w;
stopiccot@6682
   504
                info->ptMinTrackSize.y = h;
stopiccot@6682
   505
                info->ptMaxTrackSize.x = w;
stopiccot@6682
   506
                info->ptMaxTrackSize.y = h;
stopiccot@6682
   507
            }
slouken@1895
   508
        }
slouken@3566
   509
        returnCode = 0;
slouken@3566
   510
        break;
slouken@5086
   511
#endif /* WM_GETMINMAXINFO */
slouken@1895
   512
slouken@1895
   513
    case WM_WINDOWPOSCHANGED:
slouken@1895
   514
        {
slouken@1895
   515
            RECT rect;
slouken@1895
   516
            int x, y;
slouken@1895
   517
            int w, h;
slouken@1895
   518
            Uint32 window_flags;
slouken@1895
   519
slouken@3256
   520
            if (!GetClientRect(hwnd, &rect) ||
slouken@3256
   521
                (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@3256
   522
                break;
slouken@3256
   523
            }
slouken@1895
   524
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   525
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   526
slouken@3685
   527
            window_flags = SDL_GetWindowFlags(data->window);
slouken@1895
   528
            if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   529
                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   530
                ClipCursor(&rect);
slouken@1895
   531
            }
slouken@1895
   532
slouken@1895
   533
            x = rect.left;
slouken@1895
   534
            y = rect.top;
slouken@3685
   535
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
   536
slouken@1895
   537
            w = rect.right - rect.left;
slouken@1895
   538
            h = rect.bottom - rect.top;
slouken@3685
   539
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
slouken@1895
   540
                                h);
slouken@1895
   541
        }
slouken@1895
   542
        break;
slouken@1895
   543
slouken@1895
   544
    case WM_SETCURSOR:
slouken@1895
   545
        {
slouken@3076
   546
            Uint16 hittest;
slouken@1895
   547
slouken@3076
   548
            hittest = LOWORD(lParam);
slouken@3076
   549
            if (hittest == HTCLIENT) {
slouken@5421
   550
                SetCursor(SDL_cursor);
slouken@3566
   551
                returnCode = TRUE;
slouken@3076
   552
            }
slouken@1895
   553
        }
slouken@1895
   554
        break;
slouken@1895
   555
slouken@1895
   556
        /* We were occluded, refresh our display */
slouken@1895
   557
    case WM_PAINT:
slouken@1895
   558
        {
slouken@1895
   559
            RECT rect;
slouken@1895
   560
            if (GetUpdateRect(hwnd, &rect, FALSE)) {
slouken@1895
   561
                ValidateRect(hwnd, &rect);
slouken@3685
   562
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
slouken@1895
   563
                                    0, 0);
slouken@1895
   564
            }
slouken@1895
   565
        }
slouken@3566
   566
        returnCode = 0;
slouken@3566
   567
        break;
slouken@3095
   568
slouken@1895
   569
        /* We'll do our own drawing, prevent flicker */
slouken@1895
   570
    case WM_ERASEBKGND:
slouken@1895
   571
        {
slouken@1895
   572
        }
slouken@1895
   573
        return (1);
slouken@1895
   574
slouken@5086
   575
#if defined(SC_SCREENSAVE) || defined(SC_MONITORPOWER)
slouken@1895
   576
    case WM_SYSCOMMAND:
slouken@1895
   577
        {
slouken@1895
   578
            /* Don't start the screensaver or blank the monitor in fullscreen apps */
slouken@1895
   579
            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
slouken@1895
   580
                (wParam & 0xFFF0) == SC_MONITORPOWER) {
slouken@3032
   581
                if (SDL_GetVideoDevice()->suspend_screensaver) {
slouken@1895
   582
                    return (0);
slouken@1895
   583
                }
slouken@1895
   584
            }
slouken@1895
   585
        }
slouken@1895
   586
        break;
slouken@5086
   587
#endif /* System has screensaver support */
slouken@1895
   588
slouken@1895
   589
    case WM_CLOSE:
slouken@1895
   590
        {
slouken@3685
   591
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1895
   592
        }
slouken@3566
   593
        returnCode = 0;
slouken@3566
   594
        break;
slouken@4919
   595
jimtla@4650
   596
	case WM_TOUCH:
slouken@4932
   597
		{
slouken@4932
   598
			UINT i, num_inputs = LOWORD(wParam);
slouken@4932
   599
			PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
slouken@4919
   600
			if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
slouken@4932
   601
				RECT rect;
slouken@4919
   602
				float x, y;
slouken@4919
   603
slouken@4919
   604
				if (!GetClientRect(hwnd, &rect) ||
slouken@4919
   605
				    (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@4919
   606
					break;
slouken@4919
   607
				}
slouken@4919
   608
				ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@4932
   609
				ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@4932
   610
				rect.top *= 100;
slouken@4932
   611
				rect.left *= 100;
slouken@4932
   612
				rect.bottom *= 100;
slouken@4932
   613
				rect.right *= 100;
slouken@4932
   614
slouken@4932
   615
				for (i = 0; i < num_inputs; ++i) {
slouken@4932
   616
					PTOUCHINPUT input = &inputs[i];
slouken@4919
   617
icculus@5889
   618
					const SDL_TouchID touchId = (SDL_TouchID)
icculus@5889
   619
												((size_t)input->hSource);
slouken@4919
   620
					if (!SDL_GetTouch(touchId)) {
slouken@4919
   621
						SDL_Touch touch;
slouken@4919
   622
slouken@4919
   623
						touch.id = touchId;
slouken@4919
   624
						touch.x_min = 0;
slouken@4919
   625
						touch.x_max = 1;
slouken@4919
   626
						touch.native_xres = touch.x_max - touch.x_min;
slouken@4919
   627
						touch.y_min = 0;
slouken@4919
   628
						touch.y_max = 1;
slouken@4919
   629
						touch.native_yres = touch.y_max - touch.y_min;
slouken@4919
   630
						touch.pressure_min = 0;
slouken@4919
   631
						touch.pressure_max = 1;
slouken@4919
   632
						touch.native_pressureres = touch.pressure_max - touch.pressure_min;
slouken@4919
   633
slouken@4919
   634
						if (SDL_AddTouch(&touch, "") < 0) {
slouken@4919
   635
							continue;
slouken@4919
   636
						}
slouken@4932
   637
					}
slouken@4932
   638
slouken@4932
   639
					// Get the normalized coordinates for the window
slouken@4932
   640
					x = (float)(input->x - rect.left)/(rect.right - rect.left);
slouken@4932
   641
					y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
slouken@4932
   642
slouken@4919
   643
					if (input->dwFlags & TOUCHEVENTF_DOWN) {
slouken@4932
   644
						SDL_SendFingerDown(touchId, input->dwID, SDL_TRUE, x, y, 1);
slouken@4932
   645
					}
slouken@4919
   646
					if (input->dwFlags & TOUCHEVENTF_MOVE) {
slouken@4932
   647
						SDL_SendTouchMotion(touchId, input->dwID, SDL_FALSE, x, y, 1);
slouken@4932
   648
					}
slouken@4919
   649
					if (input->dwFlags & TOUCHEVENTF_UP) {
slouken@4932
   650
						SDL_SendFingerDown(touchId, input->dwID, SDL_FALSE, x, y, 1);
slouken@4932
   651
					}
slouken@4932
   652
				}
slouken@4932
   653
			}
slouken@4932
   654
			SDL_stack_free(inputs);
slouken@4932
   655
slouken@4932
   656
			data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
slouken@4919
   657
			return 0;
jimtla@4650
   658
		}
jimtla@4650
   659
		break;
slouken@6523
   660
slouken@6523
   661
    case WM_DROPFILES:
slouken@6523
   662
        {
slouken@6523
   663
            UINT i;
slouken@6523
   664
            HDROP drop = (HDROP) wParam;
slouken@6523
   665
            UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
slouken@6523
   666
            for (i = 0; i < count; ++i) {
slouken@6523
   667
                UINT size = DragQueryFile(drop, i, NULL, 0) + 1;
slouken@6523
   668
                LPTSTR buffer = SDL_stack_alloc(TCHAR, size);
slouken@6523
   669
                if (buffer) {
slouken@6523
   670
                    if (DragQueryFile(drop, i, buffer, size)) {
slouken@6523
   671
                        char *file = WIN_StringToUTF8(buffer);
slouken@6523
   672
                        SDL_SendDropFile(file);
slouken@6523
   673
                        SDL_free(file);
slouken@6523
   674
                    }
slouken@6523
   675
                    SDL_stack_free(buffer);
slouken@6523
   676
                }
slouken@6523
   677
            }
slouken@6523
   678
            DragFinish(drop);
slouken@6523
   679
            return 0;
slouken@6523
   680
        }
slouken@6523
   681
        break;
slouken@6523
   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@5086
   722
#if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
slouken@1895
   723
        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
slouken@5086
   724
#endif
slouken@1895
   725
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   726
    }
slouken@1895
   727
slouken@1895
   728
    if (name) {
slouken@1895
   729
        SDL_Appname = WIN_UTF8ToString(name);
slouken@1895
   730
        SDL_Appstyle = style;
slouken@1895
   731
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   732
    }
slouken@1895
   733
slouken@1895
   734
    /* Register the application class */
slouken@1895
   735
    class.hCursor = NULL;
slouken@2710
   736
    class.hIcon =
slouken@2710
   737
        LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
slouken@2710
   738
                  LR_DEFAULTCOLOR);
slouken@1895
   739
    class.lpszMenuName = NULL;
slouken@1895
   740
    class.lpszClassName = SDL_Appname;
slouken@1895
   741
    class.hbrBackground = NULL;
slouken@1895
   742
    class.hInstance = SDL_Instance;
slouken@1895
   743
    class.style = SDL_Appstyle;
dewyatt@4733
   744
    class.lpfnWndProc = WIN_WindowProc;
slouken@1895
   745
    class.cbWndExtra = 0;
slouken@1895
   746
    class.cbClsExtra = 0;
slouken@1895
   747
    if (!RegisterClass(&class)) {
slouken@1895
   748
        SDL_SetError("Couldn't register application class");
slouken@1895
   749
        return (-1);
slouken@1895
   750
    }
slouken@1895
   751
slouken@1895
   752
    app_registered = 1;
slouken@1895
   753
    return (0);
slouken@1895
   754
}
slouken@1895
   755
slouken@1895
   756
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
slouken@1895
   757
void
slouken@1895
   758
SDL_UnregisterApp()
slouken@1895
   759
{
slouken@1895
   760
    WNDCLASS class;
slouken@1895
   761
slouken@1895
   762
    /* SDL_RegisterApp might not have been called before */
slouken@1895
   763
    if (!app_registered) {
slouken@1895
   764
        return;
slouken@1895
   765
    }
slouken@1895
   766
    --app_registered;
slouken@1895
   767
    if (app_registered == 0) {
slouken@1895
   768
        /* Check for any registered window classes. */
slouken@1895
   769
        if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
slouken@1895
   770
            UnregisterClass(SDL_Appname, SDL_Instance);
slouken@1895
   771
        }
slouken@1895
   772
        SDL_free(SDL_Appname);
slouken@1895
   773
        SDL_Appname = NULL;
slouken@1895
   774
    }
slouken@1895
   775
}
slouken@1895
   776
slouken@6044
   777
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   778
slouken@1895
   779
/* vi: set ts=4 sw=4 expandtab: */