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