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