src/video/windows/SDL_windowsevents.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Mon, 25 Feb 2013 16:52:51 -0800
changeset 6925 59fedfb8faaf
parent 6922 91d157d9f283
child 6929 7fa90e780e2d
permissions -rw-r--r--
sdl2
- more scancode goodness, removing now dead wparam translation code
- add scancode for SDL_SCANCODE_NONUSBACKSLASH
- don't translate sdl key values for numeric and grave key

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