src/video/windows/SDL_windowsevents.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 26 Feb 2013 20:32:11 -0800
changeset 6931 843f9c1b16b8
parent 6929 7fa90e780e2d
child 6938 b272d47f70a7
child 6943 ce87e12970f5
permissions -rw-r--r--
Fixed cygwin compile
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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@6931
    49
#ifndef VK_OEM_NEC_EQUAL
slouken@6931
    50
#define VK_OEM_NEC_EQUAL 0x92 
slouken@6931
    51
#endif
slouken@2313
    52
icculus@2127
    53
/* Make sure XBUTTON stuff is defined that isn't in older Platform SDKs... */
icculus@2127
    54
#ifndef WM_XBUTTONDOWN
icculus@2127
    55
#define WM_XBUTTONDOWN 0x020B
icculus@2127
    56
#endif
icculus@2127
    57
#ifndef WM_XBUTTONUP
icculus@2127
    58
#define WM_XBUTTONUP 0x020C
icculus@2127
    59
#endif
icculus@2127
    60
#ifndef GET_XBUTTON_WPARAM
icculus@2127
    61
#define GET_XBUTTON_WPARAM(w) (HIWORD(w))
icculus@2127
    62
#endif
bobbens@2733
    63
#ifndef WM_INPUT
bobbens@2733
    64
#define WM_INPUT 0x00ff
bobbens@2733
    65
#endif
slouken@4932
    66
#ifndef WM_TOUCH
slouken@4868
    67
#define WM_TOUCH 0x0240
slouken@4932
    68
#endif
slouken@4919
    69
jorgen@6922
    70
static SDL_Scancode 
jorgen@6922
    71
WindowsScanCodeToSDLScanCode( int lParam, int wParam, const SDL_Scancode *key_map )
jorgen@6922
    72
{
jorgen@6922
    73
	SDL_Scancode code;
jorgen@6922
    74
	char bIsExtended;
jorgen@6922
    75
	int nScanCode = ( lParam >> 16 ) & 0xFF;
jorgen@6922
    76
jorgen@6922
    77
	if ( nScanCode == 0 )
jorgen@6922
    78
	{
jorgen@6922
    79
		switch( wParam )
jorgen@6922
    80
		{
jorgen@6922
    81
		case VK_CLEAR: return SDL_SCANCODE_CLEAR;
jorgen@6922
    82
		case VK_MODECHANGE: return SDL_SCANCODE_MODE;
jorgen@6922
    83
		case VK_SELECT: return SDL_SCANCODE_SELECT;
jorgen@6922
    84
		case VK_EXECUTE: return SDL_SCANCODE_EXECUTE;
jorgen@6922
    85
		case VK_HELP: return SDL_SCANCODE_HELP;
jorgen@6922
    86
jorgen@6922
    87
		case VK_F13: return SDL_SCANCODE_F13;
jorgen@6922
    88
		case VK_F14: return SDL_SCANCODE_F14;
jorgen@6922
    89
		case VK_F15: return SDL_SCANCODE_F15;
jorgen@6922
    90
		case VK_F16: return SDL_SCANCODE_F16;
jorgen@6922
    91
		case VK_F17: return SDL_SCANCODE_F17;
jorgen@6922
    92
		case VK_F18: return SDL_SCANCODE_F18;
jorgen@6922
    93
		case VK_F19: return SDL_SCANCODE_F19;
jorgen@6922
    94
		case VK_F20: return SDL_SCANCODE_F20;
jorgen@6922
    95
		case VK_F21: return SDL_SCANCODE_F21;
jorgen@6922
    96
		case VK_F22: return SDL_SCANCODE_F22;
jorgen@6922
    97
		case VK_F23: return SDL_SCANCODE_F23;
jorgen@6922
    98
		case VK_F24: return SDL_SCANCODE_F24;
jorgen@6922
    99
jorgen@6922
   100
		case VK_OEM_NEC_EQUAL: return SDL_SCANCODE_KP_EQUALS;
jorgen@6922
   101
		case VK_BROWSER_BACK: return SDL_SCANCODE_AC_BACK;
jorgen@6922
   102
		case VK_BROWSER_FORWARD: return SDL_SCANCODE_AC_FORWARD;
jorgen@6922
   103
		case VK_BROWSER_REFRESH: return SDL_SCANCODE_AC_REFRESH;
jorgen@6922
   104
		case VK_BROWSER_STOP: return SDL_SCANCODE_AC_STOP;
jorgen@6922
   105
		case VK_BROWSER_SEARCH: return SDL_SCANCODE_AC_SEARCH;
jorgen@6922
   106
		case VK_BROWSER_FAVORITES: return SDL_SCANCODE_AC_BOOKMARKS;
jorgen@6922
   107
		case VK_BROWSER_HOME: return SDL_SCANCODE_AC_HOME;
jorgen@6922
   108
		case VK_VOLUME_MUTE: return SDL_SCANCODE_AUDIOMUTE;
jorgen@6922
   109
		case VK_VOLUME_DOWN: return SDL_SCANCODE_VOLUMEDOWN;
jorgen@6922
   110
		case VK_VOLUME_UP: return SDL_SCANCODE_VOLUMEUP;
jorgen@6922
   111
	
jorgen@6922
   112
		case VK_MEDIA_NEXT_TRACK: return SDL_SCANCODE_AUDIONEXT;
jorgen@6922
   113
		case VK_MEDIA_PREV_TRACK: return SDL_SCANCODE_AUDIOPREV;
jorgen@6922
   114
		case VK_MEDIA_STOP: return SDL_SCANCODE_AUDIOSTOP;
jorgen@6922
   115
		case VK_MEDIA_PLAY_PAUSE: return SDL_SCANCODE_AUDIOPLAY;
jorgen@6922
   116
		case VK_LAUNCH_MAIL: return SDL_SCANCODE_MAIL;
jorgen@6922
   117
		case VK_LAUNCH_MEDIA_SELECT: return SDL_SCANCODE_MEDIASELECT;
jorgen@6922
   118
		
jorgen@6922
   119
		case VK_OEM_102: return SDL_SCANCODE_NONUSBACKSLASH;
jorgen@6922
   120
jorgen@6922
   121
		case VK_ATTN: return SDL_SCANCODE_SYSREQ;
jorgen@6922
   122
		case VK_CRSEL: return SDL_SCANCODE_CRSEL;
jorgen@6922
   123
		case VK_EXSEL: return SDL_SCANCODE_EXSEL;
jorgen@6922
   124
		case VK_OEM_CLEAR: return SDL_SCANCODE_CLEAR;
jorgen@6922
   125
jorgen@6922
   126
		case VK_LAUNCH_APP1: return SDL_SCANCODE_APP1;
jorgen@6922
   127
		case VK_LAUNCH_APP2: return SDL_SCANCODE_APP2;
jorgen@6922
   128
jorgen@6922
   129
		default: return SDL_SCANCODE_UNKNOWN;
jorgen@6922
   130
		}
jorgen@6922
   131
	}
jorgen@6922
   132
jorgen@6922
   133
	if ( nScanCode > 127 )
jorgen@6922
   134
		return SDL_SCANCODE_UNKNOWN;
jorgen@6922
   135
jorgen@6922
   136
	code = key_map[nScanCode];
jorgen@6922
   137
jorgen@6922
   138
	bIsExtended = ( lParam & ( 1 << 24 ) ) != 0;
jorgen@6922
   139
	if ( !bIsExtended )
jorgen@6922
   140
	{
jorgen@6922
   141
		switch ( code )
jorgen@6922
   142
		{
jorgen@6922
   143
		case SDL_SCANCODE_HOME:
jorgen@6922
   144
			return SDL_SCANCODE_KP_7;
jorgen@6922
   145
		case SDL_SCANCODE_UP:
jorgen@6922
   146
			return SDL_SCANCODE_KP_8;
jorgen@6922
   147
		case SDL_SCANCODE_PAGEUP:
jorgen@6922
   148
			return SDL_SCANCODE_KP_9;
jorgen@6922
   149
		case SDL_SCANCODE_LEFT:
jorgen@6922
   150
			return SDL_SCANCODE_KP_4;
jorgen@6922
   151
		case SDL_SCANCODE_RIGHT:
jorgen@6922
   152
			return SDL_SCANCODE_KP_6;
jorgen@6922
   153
		case SDL_SCANCODE_END:
jorgen@6922
   154
			return SDL_SCANCODE_KP_1;
jorgen@6922
   155
		case SDL_SCANCODE_DOWN:
jorgen@6922
   156
			return SDL_SCANCODE_KP_2;
jorgen@6922
   157
		case SDL_SCANCODE_PAGEDOWN:
jorgen@6922
   158
			return SDL_SCANCODE_KP_3;
jorgen@6922
   159
		case SDL_SCANCODE_INSERT:
jorgen@6922
   160
			return SDL_SCANCODE_KP_0;
jorgen@6922
   161
		case SDL_SCANCODE_DELETE:
jorgen@6922
   162
			return SDL_SCANCODE_KP_DECIMAL;
jorgen@6922
   163
		case SDL_SCANCODE_PRINTSCREEN:
jorgen@6922
   164
			return SDL_SCANCODE_KP_MULTIPLY;
jorgen@6922
   165
		default:
jorgen@6922
   166
			break;
jorgen@6922
   167
		}
jorgen@6922
   168
	}
jorgen@6922
   169
	else
jorgen@6922
   170
	{
jorgen@6922
   171
		switch ( code )
jorgen@6922
   172
		{
jorgen@6922
   173
		case SDL_SCANCODE_RETURN:
jorgen@6922
   174
			return SDL_SCANCODE_KP_ENTER;
jorgen@6922
   175
		case SDL_SCANCODE_LALT:
jorgen@6922
   176
			return SDL_SCANCODE_RALT;
jorgen@6922
   177
		case SDL_SCANCODE_LCTRL:
jorgen@6922
   178
			return SDL_SCANCODE_RCTRL;
jorgen@6922
   179
		case SDL_SCANCODE_SLASH:
jorgen@6922
   180
			return SDL_SCANCODE_KP_DIVIDE;
jorgen@6922
   181
		case SDL_SCANCODE_CAPSLOCK:
jorgen@6922
   182
			return SDL_SCANCODE_KP_PLUS;
jorgen@6922
   183
		}
jorgen@6922
   184
	}
jorgen@6922
   185
jorgen@6922
   186
	return code;
jorgen@6922
   187
}
jorgen@6922
   188
jorgen@6922
   189
slouken@1895
   190
LRESULT CALLBACK
slouken@1895
   191
WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@1895
   192
{
slouken@1895
   193
    SDL_WindowData *data;
slouken@3566
   194
    LRESULT returnCode = -1;
slouken@1895
   195
slouken@1951
   196
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
   197
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
   198
        SDL_SysWMmsg wmmsg;
slouken@1951
   199
slouken@1951
   200
        SDL_VERSION(&wmmsg.version);
slouken@4900
   201
        wmmsg.subsystem = SDL_SYSWM_WINDOWS;
slouken@5056
   202
        wmmsg.msg.win.hwnd = hwnd;
slouken@5056
   203
        wmmsg.msg.win.msg = msg;
slouken@5056
   204
        wmmsg.msg.win.wParam = wParam;
slouken@5056
   205
        wmmsg.msg.win.lParam = lParam;
slouken@1951
   206
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   207
    }
slouken@1951
   208
slouken@1895
   209
    /* Get the window data for the window */
slouken@1895
   210
    data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
slouken@1895
   211
    if (!data) {
slouken@1895
   212
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@1895
   213
    }
jimtla@4650
   214
slouken@1895
   215
#ifdef WMMSG_DEBUG
jimtla@4650
   216
    {		
jimtla@4650
   217
        FILE *log = fopen("wmmsg.txt", "a");		
slouken@1913
   218
        fprintf(log, "Received windows message: %p ", hwnd);
slouken@1913
   219
        if (msg > MAX_WMMSG) {
slouken@1913
   220
            fprintf(log, "%d", msg);
slouken@1913
   221
        } else {
slouken@1913
   222
            fprintf(log, "%s", wmtab[msg]);
slouken@1913
   223
        }
slouken@1913
   224
        fprintf(log, " -- 0x%X, 0x%X\n", wParam, lParam);
slouken@1913
   225
        fclose(log);
slouken@1895
   226
    }
slouken@4868
   227
#endif
slouken@2710
   228
dewyatt@4752
   229
    if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata))
dewyatt@4752
   230
        return 0;
slouken@1895
   231
slouken@1895
   232
    switch (msg) {
slouken@1895
   233
slouken@1895
   234
    case WM_SHOWWINDOW:
slouken@1895
   235
        {
slouken@1895
   236
            if (wParam) {
slouken@3685
   237
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
   238
            } else {
slouken@3685
   239
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
   240
            }
slouken@1895
   241
        }
slouken@1895
   242
        break;
slouken@1895
   243
slouken@1895
   244
    case WM_ACTIVATE:
slouken@1895
   245
        {
slouken@1895
   246
            BOOL minimized;
slouken@1895
   247
slouken@1895
   248
            minimized = HIWORD(wParam);
slouken@1895
   249
            if (!minimized && (LOWORD(wParam) != WA_INACTIVE)) {
slouken@3685
   250
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   251
                SDL_SendWindowEvent(data->window,
slouken@1895
   252
                                    SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1895
   253
                if (IsZoomed(hwnd)) {
slouken@3685
   254
                    SDL_SendWindowEvent(data->window,
slouken@1895
   255
                                        SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
   256
                }
slouken@4465
   257
                if (SDL_GetKeyboardFocus() != data->window) {
slouken@4465
   258
                    SDL_SetKeyboardFocus(data->window);
slouken@1895
   259
                }
slouken@6350
   260
slouken@6350
   261
				if(SDL_GetMouse()->relative_mode) {
slouken@6350
   262
					LONG cx, cy;
slouken@6350
   263
					RECT rect;
slouken@6350
   264
					GetWindowRect(hwnd, &rect);
slouken@6350
   265
slouken@6350
   266
					cx = (rect.left + rect.right) / 2;
slouken@6350
   267
					cy = (rect.top + rect.bottom) / 2;
slouken@6350
   268
slouken@6350
   269
					/* Make an absurdly small clip rect */
slouken@6350
   270
					rect.left = cx-1;
slouken@6350
   271
					rect.right = cx+1;
slouken@6350
   272
					rect.top = cy-1;
slouken@6350
   273
					rect.bottom = cy+1;
slouken@6350
   274
slouken@6350
   275
					ClipCursor(&rect);
slouken@6350
   276
				}
slouken@6350
   277
slouken@4504
   278
                /*
slouken@4504
   279
                 * FIXME: Update keyboard state
slouken@4504
   280
                 */
slouken@4504
   281
                WIN_CheckClipboardUpdate(data->videodata);
slouken@1895
   282
            } else {
slouken@4465
   283
                if (SDL_GetKeyboardFocus() == data->window) {
slouken@4465
   284
                    SDL_SetKeyboardFocus(NULL);
slouken@1895
   285
                }
slouken@1895
   286
                if (minimized) {
slouken@3685
   287
                    SDL_SendWindowEvent(data->window,
slouken@1895
   288
                                        SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
   289
                }
slouken@1895
   290
            }
slouken@1895
   291
        }
slouken@3566
   292
        returnCode = 0;
slouken@3566
   293
        break;
slouken@1895
   294
slouken@4470
   295
	case WM_MOUSEMOVE:
slouken@6350
   296
		if(SDL_GetMouse()->relative_mode)
slouken@6350
   297
			break;
slouken@4484
   298
        SDL_SendMouseMotion(data->window, 0, LOWORD(lParam), HIWORD(lParam));
slouken@3139
   299
        break;
slouken@3139
   300
slouken@6350
   301
	case WM_INPUT:
slouken@6350
   302
	{
slouken@6350
   303
		HRAWINPUT hRawInput = (HRAWINPUT)lParam;
slouken@6350
   304
		RAWINPUT inp;
slouken@6350
   305
		UINT size = sizeof(inp);
slouken@6782
   306
slouken@6782
   307
		if(!SDL_GetMouse()->relative_mode)
slouken@6782
   308
			break;
slouken@6782
   309
slouken@6350
   310
		GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
slouken@6350
   311
slouken@6350
   312
		/* Mouse data */
slouken@6350
   313
		if(inp.header.dwType == RIM_TYPEMOUSE)
slouken@6350
   314
		{
slouken@6350
   315
			RAWMOUSE* mouse = &inp.data.mouse;
slouken@6350
   316
slouken@6350
   317
			if((mouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE)
slouken@6782
   318
			{
slouken@6350
   319
				SDL_SendMouseMotion(data->window, 1, (int)mouse->lLastX, (int)mouse->lLastY);
slouken@6782
   320
			}
slouken@6782
   321
			else
slouken@6782
   322
			{
slouken@6782
   323
				// synthesize relative moves from the abs position
slouken@6782
   324
				static SDL_Point initialMousePoint;
slouken@6782
   325
				if ( initialMousePoint.x == 0 && initialMousePoint.y == 0 )
slouken@6782
   326
				{
slouken@6782
   327
					initialMousePoint.x = mouse->lLastX;
slouken@6782
   328
					initialMousePoint.y = mouse->lLastY;
slouken@6782
   329
				}
slouken@6350
   330
slouken@6782
   331
				SDL_SendMouseMotion(data->window, 1, (int)(mouse->lLastX-initialMousePoint.x), (int)(mouse->lLastY-initialMousePoint.y) );
slouken@6782
   332
slouken@6782
   333
				initialMousePoint.x = mouse->lLastX;
slouken@6782
   334
				initialMousePoint.y = mouse->lLastY;
slouken@6782
   335
			}
slouken@6350
   336
		}
slouken@6350
   337
		break;
slouken@6350
   338
	}
slouken@6350
   339
slouken@3097
   340
    case WM_LBUTTONDOWN:
slouken@4484
   341
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_LEFT);
slouken@3139
   342
        break;
slouken@3139
   343
slouken@3097
   344
    case WM_LBUTTONUP:
slouken@4484
   345
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_LEFT);
slouken@3139
   346
        break;
slouken@3139
   347
slouken@5049
   348
    case WM_RBUTTONDOWN:
slouken@5049
   349
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_RIGHT);
slouken@5049
   350
        break;
slouken@5049
   351
slouken@5049
   352
    case WM_RBUTTONUP:
slouken@5049
   353
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_RIGHT);
slouken@5049
   354
        break;
slouken@5049
   355
slouken@5049
   356
    case WM_MBUTTONDOWN:
slouken@5049
   357
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_MIDDLE);
slouken@5049
   358
        break;
slouken@5049
   359
slouken@5049
   360
    case WM_MBUTTONUP:
slouken@5049
   361
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_MIDDLE);
slouken@5049
   362
        break;
slouken@5049
   363
slouken@5049
   364
    case WM_XBUTTONDOWN:
slouken@5049
   365
        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_X1 + GET_XBUTTON_WPARAM(wParam) - 1);
slouken@5049
   366
        returnCode = TRUE;
slouken@5049
   367
        break;
slouken@5049
   368
slouken@5049
   369
    case WM_XBUTTONUP:
slouken@5049
   370
        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_X1 + GET_XBUTTON_WPARAM(wParam) - 1);
slouken@5049
   371
        returnCode = TRUE;
slouken@5049
   372
        break;
slouken@5049
   373
slouken@5049
   374
    case WM_MOUSEWHEEL:
slouken@5049
   375
        {
slouken@6861
   376
            // FIXME: This may need to accumulate deltas up to WHEEL_DELTA
slouken@6861
   377
            short motion = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
slouken@5049
   378
slouken@5049
   379
            SDL_SendMouseWheel(data->window, 0, motion);
slouken@5049
   380
            break;
slouken@5049
   381
        }
slouken@5049
   382
slouken@5086
   383
#ifdef WM_MOUSELEAVE
slouken@5086
   384
    /* FIXME: Do we need the SDL 1.2 hack to generate WM_MOUSELEAVE now? */
slouken@1895
   385
    case WM_MOUSELEAVE:
slouken@4470
   386
        if (SDL_GetMouseFocus() == data->window) {
slouken@4470
   387
            SDL_SetMouseFocus(NULL);
slouken@1895
   388
        }
slouken@3566
   389
        returnCode = 0;
slouken@3566
   390
        break;
slouken@5086
   391
#endif /* WM_MOUSELEAVE */
slouken@1895
   392
slouken@1895
   393
    case WM_SYSKEYDOWN:
slouken@1895
   394
    case WM_KEYDOWN:
slouken@1895
   395
        {
jorgen@6925
   396
			SDL_Scancode code =  WindowsScanCodeToSDLScanCode( lParam, wParam, data->videodata->key_layout );
jorgen@6922
   397
			if ( code != SDL_SCANCODE_UNKNOWN ) {
jorgen@6922
   398
                SDL_SendKeyboardKey(SDL_PRESSED, code );
slouken@2308
   399
            }
slouken@1895
   400
        }
slouken@3566
   401
        returnCode = 0;
slouken@3566
   402
        break;
slouken@1895
   403
slouken@1895
   404
    case WM_SYSKEYUP:
slouken@1895
   405
    case WM_KEYUP:
slouken@1895
   406
        {
jorgen@6925
   407
			SDL_Scancode code =  WindowsScanCodeToSDLScanCode( lParam, wParam, data->videodata->key_layout );
jorgen@6922
   408
			if ( code != SDL_SCANCODE_UNKNOWN ) {
jorgen@6922
   409
				if (wParam == VK_SNAPSHOT
jorgen@6922
   410
				    && SDL_GetKeyboardState(NULL)[SDL_SCANCODE_PRINTSCREEN] ==
jorgen@6922
   411
				    SDL_RELEASED) {
jorgen@6922
   412
				    SDL_SendKeyboardKey(SDL_PRESSED,
jorgen@6922
   413
				                         code);
jorgen@6922
   414
				}
jorgen@6922
   415
                SDL_SendKeyboardKey(SDL_RELEASED, code );
slouken@2308
   416
            }
slouken@1895
   417
        }
slouken@3566
   418
        returnCode = 0;
slouken@3566
   419
        break;
slouken@1895
   420
slouken@2309
   421
    case WM_CHAR:
slouken@2309
   422
        {
slouken@2309
   423
            char text[4];
slouken@2309
   424
slouken@2309
   425
            /* Convert to UTF-8 and send it on... */
slouken@2309
   426
            if (wParam <= 0x7F) {
slouken@2309
   427
                text[0] = (char) wParam;
slouken@2309
   428
                text[1] = '\0';
slouken@2309
   429
            } else if (wParam <= 0x7FF) {
slouken@2309
   430
                text[0] = 0xC0 | (char) ((wParam >> 6) & 0x1F);
slouken@2309
   431
                text[1] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   432
                text[2] = '\0';
slouken@2309
   433
            } else {
slouken@2309
   434
                text[0] = 0xE0 | (char) ((wParam >> 12) & 0x0F);
slouken@2309
   435
                text[1] = 0x80 | (char) ((wParam >> 6) & 0x3F);
slouken@2309
   436
                text[2] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   437
                text[3] = '\0';
slouken@2309
   438
            }
slouken@4465
   439
            SDL_SendKeyboardText(text);
slouken@2309
   440
        }
slouken@3566
   441
        returnCode = 0;
slouken@3566
   442
        break;
slouken@2309
   443
slouken@5086
   444
#ifdef WM_INPUTLANGCHANGE
slouken@2311
   445
    case WM_INPUTLANGCHANGE:
slouken@2311
   446
        {
slouken@4465
   447
            WIN_UpdateKeymap();
slouken@2311
   448
        }
slouken@3566
   449
        returnCode = 1;
slouken@3566
   450
        break;
slouken@5086
   451
#endif /* WM_INPUTLANGCHANGE */
slouken@2311
   452
slouken@5086
   453
#ifdef WM_GETMINMAXINFO
slouken@1895
   454
    case WM_GETMINMAXINFO:
slouken@1895
   455
        {
slouken@1895
   456
            MINMAXINFO *info;
slouken@1895
   457
            RECT size;
slouken@1895
   458
            int x, y;
slouken@1895
   459
            int w, h;
stopiccot@6682
   460
            int min_w, min_h;
slouken@6788
   461
            int max_w, max_h;
slouken@1895
   462
            int style;
slouken@3168
   463
            BOOL menu;
slouken@6862
   464
			BOOL constrain_max_size;
slouken@1895
   465
slouken@1895
   466
            /* If we allow resizing, let the resize happen naturally */
stopiccot@6682
   467
            if (SDL_IsShapedWindow(data->window))
eligottlieb@4815
   468
                Win32_ResizeWindowShape(data->window);
slouken@1895
   469
slouken@1895
   470
            /* Get the current position of our window */
slouken@1895
   471
            GetWindowRect(hwnd, &size);
slouken@1895
   472
            x = size.left;
slouken@1895
   473
            y = size.top;
slouken@1895
   474
slouken@1895
   475
            /* Calculate current size of our window */
slouken@3685
   476
            SDL_GetWindowSize(data->window, &w, &h);
stopiccot@6682
   477
            SDL_GetWindowMinimumSize(data->window, &min_w, &min_h);
slouken@6788
   478
            SDL_GetWindowMaximumSize(data->window, &max_w, &max_h);
stopiccot@6682
   479
stopiccot@6682
   480
            /* Store in min_w and min_h difference between current size and minimal 
stopiccot@6682
   481
               size so we don't need to call AdjustWindowRectEx twice */
stopiccot@6682
   482
            min_w -= w;
stopiccot@6682
   483
            min_h -= h;
slouken@6837
   484
            if (max_w && max_h) {
slouken@6837
   485
                max_w -= w;
slouken@6837
   486
                max_h -= h;
slouken@6862
   487
                constrain_max_size = TRUE;
slouken@6862
   488
            } else {
slouken@6862
   489
                constrain_max_size = FALSE;
slouken@6837
   490
            }
stopiccot@6682
   491
slouken@1895
   492
            size.top = 0;
slouken@1895
   493
            size.left = 0;
slouken@1895
   494
            size.bottom = h;
slouken@1895
   495
            size.right = w;
slouken@1895
   496
slouken@3168
   497
            style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   498
            /* DJM - according to the docs for GetMenu(), the
slouken@1895
   499
               return value is undefined if hwnd is a child window.
slouken@1895
   500
               Aparently it's too difficult for MS to check
slouken@1895
   501
               inside their function, so I have to do it here.
slouken@1895
   502
             */
slouken@3168
   503
            menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   504
            AdjustWindowRectEx(&size, style, menu, 0);
slouken@1895
   505
            w = size.right - size.left;
slouken@1895
   506
            h = size.bottom - size.top;
slouken@1895
   507
slouken@1895
   508
            /* Fix our size to the current size */
slouken@1895
   509
            info = (MINMAXINFO *) lParam;
stopiccot@6682
   510
            if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
stopiccot@6682
   511
                info->ptMinTrackSize.x = w + min_w;
stopiccot@6682
   512
                info->ptMinTrackSize.y = h + min_h;
slouken@6862
   513
                if (constrain_max_size) {
slouken@6862
   514
                    info->ptMaxTrackSize.x = w + max_w;
slouken@6862
   515
                    info->ptMaxTrackSize.y = h + max_h;
slouken@6862
   516
                }
stopiccot@6682
   517
            } else {
stopiccot@6682
   518
                info->ptMaxSize.x = w;
stopiccot@6682
   519
                info->ptMaxSize.y = h;
stopiccot@6682
   520
                info->ptMaxPosition.x = x;
stopiccot@6682
   521
                info->ptMaxPosition.y = y;
stopiccot@6682
   522
                info->ptMinTrackSize.x = w;
stopiccot@6682
   523
                info->ptMinTrackSize.y = h;
stopiccot@6682
   524
                info->ptMaxTrackSize.x = w;
stopiccot@6682
   525
                info->ptMaxTrackSize.y = h;
stopiccot@6682
   526
            }
slouken@1895
   527
        }
slouken@3566
   528
        returnCode = 0;
slouken@3566
   529
        break;
slouken@5086
   530
#endif /* WM_GETMINMAXINFO */
slouken@1895
   531
slouken@1895
   532
    case WM_WINDOWPOSCHANGED:
slouken@1895
   533
        {
slouken@1895
   534
            RECT rect;
slouken@1895
   535
            int x, y;
slouken@1895
   536
            int w, h;
slouken@1895
   537
            Uint32 window_flags;
slouken@1895
   538
slouken@3256
   539
            if (!GetClientRect(hwnd, &rect) ||
slouken@3256
   540
                (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@3256
   541
                break;
slouken@3256
   542
            }
slouken@1895
   543
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   544
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   545
slouken@3685
   546
            window_flags = SDL_GetWindowFlags(data->window);
slouken@1895
   547
            if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   548
                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   549
                ClipCursor(&rect);
slouken@1895
   550
            }
slouken@1895
   551
slouken@1895
   552
            x = rect.left;
slouken@1895
   553
            y = rect.top;
slouken@3685
   554
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
   555
slouken@1895
   556
            w = rect.right - rect.left;
slouken@1895
   557
            h = rect.bottom - rect.top;
slouken@3685
   558
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
slouken@1895
   559
                                h);
slouken@1895
   560
        }
slouken@1895
   561
        break;
slouken@1895
   562
slouken@1895
   563
    case WM_SETCURSOR:
slouken@1895
   564
        {
slouken@3076
   565
            Uint16 hittest;
slouken@1895
   566
slouken@3076
   567
            hittest = LOWORD(lParam);
slouken@3076
   568
            if (hittest == HTCLIENT) {
slouken@5421
   569
                SetCursor(SDL_cursor);
slouken@3566
   570
                returnCode = TRUE;
slouken@3076
   571
            }
slouken@1895
   572
        }
slouken@1895
   573
        break;
slouken@1895
   574
slouken@1895
   575
        /* We were occluded, refresh our display */
slouken@1895
   576
    case WM_PAINT:
slouken@1895
   577
        {
slouken@1895
   578
            RECT rect;
slouken@1895
   579
            if (GetUpdateRect(hwnd, &rect, FALSE)) {
slouken@1895
   580
                ValidateRect(hwnd, &rect);
slouken@3685
   581
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
slouken@1895
   582
                                    0, 0);
slouken@1895
   583
            }
slouken@1895
   584
        }
slouken@3566
   585
        returnCode = 0;
slouken@3566
   586
        break;
slouken@3095
   587
slouken@1895
   588
        /* We'll do our own drawing, prevent flicker */
slouken@1895
   589
    case WM_ERASEBKGND:
slouken@1895
   590
        {
slouken@1895
   591
        }
slouken@1895
   592
        return (1);
slouken@1895
   593
slouken@5086
   594
#if defined(SC_SCREENSAVE) || defined(SC_MONITORPOWER)
slouken@1895
   595
    case WM_SYSCOMMAND:
slouken@1895
   596
        {
slouken@1895
   597
            /* Don't start the screensaver or blank the monitor in fullscreen apps */
slouken@1895
   598
            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
slouken@1895
   599
                (wParam & 0xFFF0) == SC_MONITORPOWER) {
slouken@3032
   600
                if (SDL_GetVideoDevice()->suspend_screensaver) {
slouken@1895
   601
                    return (0);
slouken@1895
   602
                }
slouken@1895
   603
            }
slouken@1895
   604
        }
slouken@1895
   605
        break;
slouken@5086
   606
#endif /* System has screensaver support */
slouken@1895
   607
slouken@1895
   608
    case WM_CLOSE:
slouken@1895
   609
        {
slouken@3685
   610
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1895
   611
        }
slouken@3566
   612
        returnCode = 0;
slouken@3566
   613
        break;
slouken@4919
   614
jimtla@4650
   615
	case WM_TOUCH:
slouken@4932
   616
		{
slouken@4932
   617
			UINT i, num_inputs = LOWORD(wParam);
slouken@4932
   618
			PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
slouken@4919
   619
			if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
slouken@4932
   620
				RECT rect;
slouken@4919
   621
				float x, y;
slouken@4919
   622
slouken@4919
   623
				if (!GetClientRect(hwnd, &rect) ||
slouken@4919
   624
				    (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@4919
   625
					break;
slouken@4919
   626
				}
slouken@4919
   627
				ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@4932
   628
				ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@4932
   629
				rect.top *= 100;
slouken@4932
   630
				rect.left *= 100;
slouken@4932
   631
				rect.bottom *= 100;
slouken@4932
   632
				rect.right *= 100;
slouken@4932
   633
slouken@4932
   634
				for (i = 0; i < num_inputs; ++i) {
slouken@4932
   635
					PTOUCHINPUT input = &inputs[i];
slouken@4919
   636
icculus@5889
   637
					const SDL_TouchID touchId = (SDL_TouchID)
icculus@5889
   638
												((size_t)input->hSource);
slouken@4919
   639
					if (!SDL_GetTouch(touchId)) {
slouken@4919
   640
						SDL_Touch touch;
slouken@4919
   641
slouken@4919
   642
						touch.id = touchId;
slouken@4919
   643
						touch.x_min = 0;
slouken@4919
   644
						touch.x_max = 1;
slouken@4919
   645
						touch.native_xres = touch.x_max - touch.x_min;
slouken@4919
   646
						touch.y_min = 0;
slouken@4919
   647
						touch.y_max = 1;
slouken@4919
   648
						touch.native_yres = touch.y_max - touch.y_min;
slouken@4919
   649
						touch.pressure_min = 0;
slouken@4919
   650
						touch.pressure_max = 1;
slouken@4919
   651
						touch.native_pressureres = touch.pressure_max - touch.pressure_min;
slouken@4919
   652
slouken@4919
   653
						if (SDL_AddTouch(&touch, "") < 0) {
slouken@4919
   654
							continue;
slouken@4919
   655
						}
slouken@4932
   656
					}
slouken@4932
   657
slouken@4932
   658
					// Get the normalized coordinates for the window
slouken@4932
   659
					x = (float)(input->x - rect.left)/(rect.right - rect.left);
slouken@4932
   660
					y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
slouken@4932
   661
slouken@4919
   662
					if (input->dwFlags & TOUCHEVENTF_DOWN) {
slouken@4932
   663
						SDL_SendFingerDown(touchId, input->dwID, SDL_TRUE, x, y, 1);
slouken@4932
   664
					}
slouken@4919
   665
					if (input->dwFlags & TOUCHEVENTF_MOVE) {
slouken@4932
   666
						SDL_SendTouchMotion(touchId, input->dwID, SDL_FALSE, x, y, 1);
slouken@4932
   667
					}
slouken@4919
   668
					if (input->dwFlags & TOUCHEVENTF_UP) {
slouken@4932
   669
						SDL_SendFingerDown(touchId, input->dwID, SDL_FALSE, x, y, 1);
slouken@4932
   670
					}
slouken@4932
   671
				}
slouken@4932
   672
			}
slouken@4932
   673
			SDL_stack_free(inputs);
slouken@4932
   674
slouken@4932
   675
			data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
slouken@4919
   676
			return 0;
jimtla@4650
   677
		}
jimtla@4650
   678
		break;
slouken@6523
   679
slouken@6523
   680
    case WM_DROPFILES:
slouken@6523
   681
        {
slouken@6523
   682
            UINT i;
slouken@6523
   683
            HDROP drop = (HDROP) wParam;
slouken@6523
   684
            UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
slouken@6523
   685
            for (i = 0; i < count; ++i) {
slouken@6523
   686
                UINT size = DragQueryFile(drop, i, NULL, 0) + 1;
slouken@6523
   687
                LPTSTR buffer = SDL_stack_alloc(TCHAR, size);
slouken@6523
   688
                if (buffer) {
slouken@6523
   689
                    if (DragQueryFile(drop, i, buffer, size)) {
slouken@6523
   690
                        char *file = WIN_StringToUTF8(buffer);
slouken@6523
   691
                        SDL_SendDropFile(file);
slouken@6523
   692
                        SDL_free(file);
slouken@6523
   693
                    }
slouken@6523
   694
                    SDL_stack_free(buffer);
slouken@6523
   695
                }
slouken@6523
   696
            }
slouken@6523
   697
            DragFinish(drop);
slouken@6523
   698
            return 0;
slouken@6523
   699
        }
slouken@6523
   700
        break;
slouken@6523
   701
    }
slouken@3566
   702
slouken@3566
   703
    /* If there's a window proc, assume it's going to handle messages */
slouken@3566
   704
    if (data->wndproc) {
slouken@3566
   705
        return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
slouken@3566
   706
    } else if (returnCode >= 0) {
slouken@3566
   707
        return returnCode;
slouken@3566
   708
    } else {
slouken@3566
   709
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@3566
   710
    }
slouken@1895
   711
}
slouken@1895
   712
slouken@1895
   713
void
slouken@1895
   714
WIN_PumpEvents(_THIS)
slouken@1895
   715
{
slouken@1895
   716
    MSG msg;
slouken@1895
   717
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
slouken@1895
   718
        TranslateMessage(&msg);
slouken@1895
   719
        DispatchMessage(&msg);
slouken@1895
   720
    }
slouken@1895
   721
}
slouken@1895
   722
slouken@1895
   723
static int app_registered = 0;
slouken@1895
   724
LPTSTR SDL_Appname = NULL;
slouken@1895
   725
Uint32 SDL_Appstyle = 0;
slouken@1895
   726
HINSTANCE SDL_Instance = NULL;
slouken@1895
   727
slouken@1895
   728
/* Register the class for this application */
slouken@1895
   729
int
slouken@1895
   730
SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@1895
   731
{
slouken@1895
   732
    WNDCLASS class;
slouken@1895
   733
slouken@1895
   734
    /* Only do this once... */
slouken@1895
   735
    if (app_registered) {
slouken@1895
   736
        ++app_registered;
slouken@1895
   737
        return (0);
slouken@1895
   738
    }
slouken@1895
   739
    if (!name && !SDL_Appname) {
slouken@1895
   740
        name = "SDL_app";
slouken@5086
   741
#if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
slouken@1895
   742
        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
slouken@5086
   743
#endif
slouken@1895
   744
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   745
    }
slouken@1895
   746
slouken@1895
   747
    if (name) {
slouken@1895
   748
        SDL_Appname = WIN_UTF8ToString(name);
slouken@1895
   749
        SDL_Appstyle = style;
slouken@1895
   750
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   751
    }
slouken@1895
   752
slouken@1895
   753
    /* Register the application class */
slouken@1895
   754
    class.hCursor = NULL;
slouken@2710
   755
    class.hIcon =
slouken@2710
   756
        LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
slouken@2710
   757
                  LR_DEFAULTCOLOR);
slouken@1895
   758
    class.lpszMenuName = NULL;
slouken@1895
   759
    class.lpszClassName = SDL_Appname;
slouken@1895
   760
    class.hbrBackground = NULL;
slouken@1895
   761
    class.hInstance = SDL_Instance;
slouken@1895
   762
    class.style = SDL_Appstyle;
dewyatt@4733
   763
    class.lpfnWndProc = WIN_WindowProc;
slouken@1895
   764
    class.cbWndExtra = 0;
slouken@1895
   765
    class.cbClsExtra = 0;
slouken@1895
   766
    if (!RegisterClass(&class)) {
slouken@1895
   767
        SDL_SetError("Couldn't register application class");
slouken@1895
   768
        return (-1);
slouken@1895
   769
    }
slouken@1895
   770
slouken@1895
   771
    app_registered = 1;
slouken@1895
   772
    return (0);
slouken@1895
   773
}
slouken@1895
   774
slouken@1895
   775
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
slouken@1895
   776
void
slouken@1895
   777
SDL_UnregisterApp()
slouken@1895
   778
{
slouken@1895
   779
    WNDCLASS class;
slouken@1895
   780
slouken@1895
   781
    /* SDL_RegisterApp might not have been called before */
slouken@1895
   782
    if (!app_registered) {
slouken@1895
   783
        return;
slouken@1895
   784
    }
slouken@1895
   785
    --app_registered;
slouken@1895
   786
    if (app_registered == 0) {
slouken@1895
   787
        /* Check for any registered window classes. */
slouken@1895
   788
        if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
slouken@1895
   789
            UnregisterClass(SDL_Appname, SDL_Instance);
slouken@1895
   790
        }
slouken@1895
   791
        SDL_free(SDL_Appname);
slouken@1895
   792
        SDL_Appname = NULL;
slouken@1895
   793
    }
slouken@1895
   794
}
slouken@1895
   795
slouken@6044
   796
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   797
slouken@1895
   798
/* vi: set ts=4 sw=4 expandtab: */