src/video/windows/SDL_windowsevents.c
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 10 Apr 2013 14:11:26 -0700
changeset 7060 9d96148c2e3e
parent 7037 3fedf1f25b94
child 7089 257fc4e541e1
permissions -rw-r--r--
Numlock & pause fix from Alfred.

Fix numlock and pause keys not being pressable on win32, they both report under
the same scancode, so use the VK to tell them apart
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@6973
    70
WindowsScanCodeToSDLScanCode( LPARAM lParam, WPARAM 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@7060
    76
	/* 0x45 here to work around both pause and numlock sharing the same scancode, so use the VK key to tell them apart */
jorgen@7060
    77
	if ( nScanCode == 0 || nScanCode == 0x45 )
jorgen@6922
    78
	{
jorgen@6922
    79
		switch( wParam )
jorgen@6922
    80
		{
jorgen@6922
    81
		case VK_CLEAR: return SDL_SCANCODE_CLEAR;
jorgen@6922
    82
		case VK_MODECHANGE: return SDL_SCANCODE_MODE;
jorgen@6922
    83
		case VK_SELECT: return SDL_SCANCODE_SELECT;
jorgen@6922
    84
		case VK_EXECUTE: return SDL_SCANCODE_EXECUTE;
jorgen@6922
    85
		case VK_HELP: return SDL_SCANCODE_HELP;
jorgen@7060
    86
		case VK_PAUSE: return SDL_SCANCODE_PAUSE;
jorgen@7060
    87
		case VK_NUMLOCK: return SDL_SCANCODE_NUMLOCKCLEAR;
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
slouken@6938
   138
	code = windows_scancode_table[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:
slouken@6938
   164
			return SDL_SCANCODE_KP_PERIOD;
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;
slouken@7029
   185
        default:
slouken@7029
   186
            break;
jorgen@6922
   187
		}
jorgen@6922
   188
	}
jorgen@6922
   189
jorgen@6922
   190
	return code;
jorgen@6922
   191
}
jorgen@6922
   192
jorgen@6922
   193
slouken@6943
   194
void 
slouken@6943
   195
WIN_CheckWParamMouseButton( SDL_bool bwParamMousePressed, SDL_bool bSDLMousePressed, SDL_WindowData *data, Uint8 button )
slouken@6943
   196
{
slouken@6943
   197
	if ( bwParamMousePressed && !bSDLMousePressed )
slouken@6943
   198
	{
slouken@6950
   199
		SDL_SendMouseButton(data->window, 0, SDL_PRESSED, button);
slouken@6943
   200
	}
slouken@6943
   201
	else if ( !bwParamMousePressed && bSDLMousePressed )
slouken@6943
   202
	{
slouken@6950
   203
		SDL_SendMouseButton(data->window, 0, SDL_RELEASED, button);
slouken@6943
   204
	}
slouken@6943
   205
}
slouken@6943
   206
slouken@6943
   207
/*
slouken@6943
   208
* Some windows systems fail to send a WM_LBUTTONDOWN sometimes, but each mouse move contains the current button state also
slouken@6943
   209
*  so this funciton reconciles our view of the world with the current buttons reported by windows
slouken@6943
   210
*/
slouken@6943
   211
void 
slouken@6943
   212
WIN_CheckWParamMouseButtons( WPARAM wParam, SDL_WindowData *data )
slouken@6943
   213
{
slouken@6943
   214
	if ( wParam != data->mouse_button_flags )
slouken@6943
   215
	{
slouken@6943
   216
		Uint32 mouseFlags = SDL_GetMouseState( NULL, NULL );
slouken@6943
   217
		WIN_CheckWParamMouseButton(  (wParam & MK_LBUTTON), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT );
slouken@6943
   218
		WIN_CheckWParamMouseButton(  (wParam & MK_MBUTTON), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE );
slouken@6943
   219
		WIN_CheckWParamMouseButton(  (wParam & MK_RBUTTON), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT );
slouken@6943
   220
		WIN_CheckWParamMouseButton(  (wParam & MK_XBUTTON1), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1 );
slouken@6943
   221
		WIN_CheckWParamMouseButton(  (wParam & MK_XBUTTON2), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2 );
slouken@6943
   222
		data->mouse_button_flags = wParam;
slouken@6943
   223
	}
slouken@6943
   224
}
slouken@6943
   225
slouken@6943
   226
slouken@6943
   227
void 
slouken@6943
   228
WIN_CheckRawMouseButtons( ULONG rawButtons, SDL_WindowData *data )
slouken@6943
   229
{
slouken@6943
   230
	if ( rawButtons != data->mouse_button_flags )
slouken@6943
   231
	{
slouken@6943
   232
		Uint32 mouseFlags = SDL_GetMouseState( NULL, NULL );
slouken@6945
   233
		if ( (rawButtons & RI_MOUSE_BUTTON_1_DOWN) )
slouken@6945
   234
			WIN_CheckWParamMouseButton(  (rawButtons & RI_MOUSE_BUTTON_1_DOWN), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT );
slouken@6945
   235
		if ( (rawButtons & RI_MOUSE_BUTTON_1_UP) )
slouken@6945
   236
			WIN_CheckWParamMouseButton(  !(rawButtons & RI_MOUSE_BUTTON_1_UP), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT );
slouken@6945
   237
		if ( (rawButtons & RI_MOUSE_BUTTON_2_DOWN) )
slouken@6945
   238
			WIN_CheckWParamMouseButton(  (rawButtons & RI_MOUSE_BUTTON_2_DOWN), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT );
slouken@6945
   239
		if ( (rawButtons & RI_MOUSE_BUTTON_2_UP) )
slouken@6945
   240
			WIN_CheckWParamMouseButton(  !(rawButtons & RI_MOUSE_BUTTON_2_UP), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT );
slouken@6945
   241
		if ( (rawButtons & RI_MOUSE_BUTTON_3_DOWN) )
slouken@6945
   242
			WIN_CheckWParamMouseButton(  (rawButtons & RI_MOUSE_BUTTON_3_DOWN), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE );
slouken@6945
   243
		if ( (rawButtons & RI_MOUSE_BUTTON_3_UP) )
slouken@6945
   244
			WIN_CheckWParamMouseButton(  !(rawButtons & RI_MOUSE_BUTTON_3_UP), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE );
slouken@6945
   245
		if ( (rawButtons & RI_MOUSE_BUTTON_4_DOWN) )
slouken@6945
   246
			WIN_CheckWParamMouseButton(  (rawButtons & RI_MOUSE_BUTTON_4_DOWN), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1 );
slouken@6945
   247
		if ( (rawButtons & RI_MOUSE_BUTTON_4_UP) )
slouken@6945
   248
			WIN_CheckWParamMouseButton(  !(rawButtons & RI_MOUSE_BUTTON_4_UP), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1 );
slouken@6945
   249
		if ( (rawButtons & RI_MOUSE_BUTTON_5_DOWN) )
slouken@6945
   250
			WIN_CheckWParamMouseButton(  (rawButtons & RI_MOUSE_BUTTON_5_DOWN), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2 );
slouken@6945
   251
		if ( (rawButtons & RI_MOUSE_BUTTON_5_UP) )
slouken@6945
   252
			WIN_CheckWParamMouseButton(  !(rawButtons & RI_MOUSE_BUTTON_5_UP), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2 );
slouken@6943
   253
		data->mouse_button_flags = rawButtons;
slouken@6943
   254
	}
slouken@6943
   255
}
slouken@6943
   256
slouken@1895
   257
LRESULT CALLBACK
slouken@1895
   258
WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@1895
   259
{
slouken@1895
   260
    SDL_WindowData *data;
slouken@3566
   261
    LRESULT returnCode = -1;
slouken@1895
   262
slouken@1951
   263
    /* Send a SDL_SYSWMEVENT if the application wants them */
slouken@4429
   264
    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
slouken@1951
   265
        SDL_SysWMmsg wmmsg;
slouken@1951
   266
slouken@1951
   267
        SDL_VERSION(&wmmsg.version);
slouken@4900
   268
        wmmsg.subsystem = SDL_SYSWM_WINDOWS;
slouken@5056
   269
        wmmsg.msg.win.hwnd = hwnd;
slouken@5056
   270
        wmmsg.msg.win.msg = msg;
slouken@5056
   271
        wmmsg.msg.win.wParam = wParam;
slouken@5056
   272
        wmmsg.msg.win.lParam = lParam;
slouken@1951
   273
        SDL_SendSysWMEvent(&wmmsg);
slouken@1951
   274
    }
slouken@1951
   275
slouken@1895
   276
    /* Get the window data for the window */
slouken@1895
   277
    data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
slouken@1895
   278
    if (!data) {
slouken@1895
   279
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@1895
   280
    }
jimtla@4650
   281
slouken@1895
   282
#ifdef WMMSG_DEBUG
jimtla@4650
   283
    {		
jimtla@4650
   284
        FILE *log = fopen("wmmsg.txt", "a");		
slouken@1913
   285
        fprintf(log, "Received windows message: %p ", hwnd);
slouken@1913
   286
        if (msg > MAX_WMMSG) {
slouken@1913
   287
            fprintf(log, "%d", msg);
slouken@1913
   288
        } else {
slouken@1913
   289
            fprintf(log, "%s", wmtab[msg]);
slouken@1913
   290
        }
slouken@1913
   291
        fprintf(log, " -- 0x%X, 0x%X\n", wParam, lParam);
slouken@1913
   292
        fclose(log);
slouken@1895
   293
    }
slouken@4868
   294
#endif
slouken@2710
   295
dewyatt@4752
   296
    if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata))
dewyatt@4752
   297
        return 0;
slouken@1895
   298
slouken@1895
   299
    switch (msg) {
slouken@1895
   300
slouken@1895
   301
    case WM_SHOWWINDOW:
slouken@1895
   302
        {
slouken@1895
   303
            if (wParam) {
slouken@3685
   304
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@1895
   305
            } else {
slouken@3685
   306
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
slouken@1895
   307
            }
slouken@1895
   308
        }
slouken@1895
   309
        break;
slouken@1895
   310
slouken@1895
   311
    case WM_ACTIVATE:
slouken@1895
   312
        {
slouken@1895
   313
            BOOL minimized;
slouken@1895
   314
slouken@1895
   315
            minimized = HIWORD(wParam);
slouken@1895
   316
            if (!minimized && (LOWORD(wParam) != WA_INACTIVE)) {
slouken@6945
   317
				Uint32 mouseFlags;
slouken@6945
   318
				SHORT keyState;
slouken@6945
   319
slouken@3685
   320
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
slouken@3685
   321
                SDL_SendWindowEvent(data->window,
slouken@1895
   322
                                    SDL_WINDOWEVENT_RESTORED, 0, 0);
slouken@1895
   323
                if (IsZoomed(hwnd)) {
slouken@3685
   324
                    SDL_SendWindowEvent(data->window,
slouken@1895
   325
                                        SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
slouken@1895
   326
                }
slouken@4465
   327
                if (SDL_GetKeyboardFocus() != data->window) {
slouken@4465
   328
                    SDL_SetKeyboardFocus(data->window);
slouken@1895
   329
                }
slouken@6945
   330
				/* mouse buttons may have changed state here, we need
slouken@6943
   331
				to resync them, but we will get a WM_MOUSEMOVE right away which will fix 
slouken@6945
   332
				things up if in non raw mode also
slouken@6943
   333
				*/
slouken@6945
   334
				mouseFlags = SDL_GetMouseState( NULL, NULL );
slouken@6945
   335
slouken@6945
   336
				keyState = GetAsyncKeyState( VK_LBUTTON );
slouken@6945
   337
				WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_LMASK), data, SDL_BUTTON_LEFT );
slouken@6945
   338
				keyState = GetAsyncKeyState( VK_RBUTTON );
slouken@6945
   339
				WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_RMASK), data, SDL_BUTTON_RIGHT );
slouken@6945
   340
				keyState = GetAsyncKeyState( VK_MBUTTON );
slouken@6945
   341
				WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_MMASK), data, SDL_BUTTON_MIDDLE );
slouken@6945
   342
				keyState = GetAsyncKeyState( VK_XBUTTON1 );
slouken@6945
   343
				WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_X1MASK), data, SDL_BUTTON_X1 );
slouken@6945
   344
				keyState = GetAsyncKeyState( VK_XBUTTON2 );
slouken@6945
   345
				WIN_CheckWParamMouseButton( ( keyState & 0x8000 ), (mouseFlags & SDL_BUTTON_X2MASK), data, SDL_BUTTON_X2 );
slouken@6945
   346
				data->mouse_button_flags = 0;
slouken@6350
   347
slouken@6350
   348
				if(SDL_GetMouse()->relative_mode) {
slouken@6350
   349
					LONG cx, cy;
slouken@6350
   350
					RECT rect;
slouken@6350
   351
					GetWindowRect(hwnd, &rect);
slouken@6350
   352
slouken@6350
   353
					cx = (rect.left + rect.right) / 2;
slouken@6350
   354
					cy = (rect.top + rect.bottom) / 2;
slouken@6350
   355
slouken@6350
   356
					/* Make an absurdly small clip rect */
slouken@6350
   357
					rect.left = cx-1;
slouken@6350
   358
					rect.right = cx+1;
slouken@6350
   359
					rect.top = cy-1;
slouken@6350
   360
					rect.bottom = cy+1;
slouken@6350
   361
slouken@6350
   362
					ClipCursor(&rect);
slouken@6350
   363
				}
slouken@6350
   364
slouken@4504
   365
                /*
slouken@4504
   366
                 * FIXME: Update keyboard state
slouken@4504
   367
                 */
slouken@4504
   368
                WIN_CheckClipboardUpdate(data->videodata);
slouken@1895
   369
            } else {
slouken@4465
   370
                if (SDL_GetKeyboardFocus() == data->window) {
slouken@4465
   371
                    SDL_SetKeyboardFocus(NULL);
slouken@1895
   372
                }
slouken@1895
   373
                if (minimized) {
slouken@3685
   374
                    SDL_SendWindowEvent(data->window,
slouken@1895
   375
                                        SDL_WINDOWEVENT_MINIMIZED, 0, 0);
slouken@1895
   376
                }
slouken@1895
   377
            }
slouken@1895
   378
        }
slouken@3566
   379
        returnCode = 0;
slouken@3566
   380
        break;
slouken@1895
   381
slouken@4470
   382
	case WM_MOUSEMOVE:
slouken@6943
   383
		if( !SDL_GetMouse()->relative_mode )
slouken@6950
   384
	        SDL_SendMouseMotion(data->window, 0, 0, LOWORD(lParam), HIWORD(lParam));
slouken@6943
   385
		/* don't break here, fall through to check the wParam like the button presses */
slouken@6945
   386
	case WM_LBUTTONUP:
slouken@6945
   387
	case WM_RBUTTONUP:
slouken@6945
   388
	case WM_MBUTTONUP:
slouken@6945
   389
	case WM_XBUTTONUP:
slouken@6943
   390
	case WM_LBUTTONDOWN:
slouken@6943
   391
	case WM_RBUTTONDOWN:
slouken@6943
   392
	case WM_MBUTTONDOWN:
slouken@6943
   393
	case WM_XBUTTONDOWN:
slouken@6945
   394
		if( !SDL_GetMouse()->relative_mode )
slouken@6945
   395
			WIN_CheckWParamMouseButtons( wParam, data );
slouken@6943
   396
		break;
slouken@3139
   397
slouken@6350
   398
	case WM_INPUT:
slouken@6350
   399
	{
slouken@6350
   400
		HRAWINPUT hRawInput = (HRAWINPUT)lParam;
slouken@6350
   401
		RAWINPUT inp;
slouken@6350
   402
		UINT size = sizeof(inp);
slouken@6782
   403
slouken@6782
   404
		if(!SDL_GetMouse()->relative_mode)
slouken@6782
   405
			break;
slouken@6782
   406
slouken@6350
   407
		GetRawInputData(hRawInput, RID_INPUT, &inp, &size, sizeof(RAWINPUTHEADER));
slouken@6350
   408
slouken@6350
   409
		/* Mouse data */
slouken@6350
   410
		if(inp.header.dwType == RIM_TYPEMOUSE)
slouken@6350
   411
		{
slouken@6350
   412
			RAWMOUSE* mouse = &inp.data.mouse;
slouken@6350
   413
slouken@6350
   414
			if((mouse->usFlags & 0x01) == MOUSE_MOVE_RELATIVE)
slouken@6782
   415
			{
slouken@6950
   416
				SDL_SendMouseMotion(data->window, 0, 1, (int)mouse->lLastX, (int)mouse->lLastY);
slouken@6782
   417
			}
slouken@6782
   418
			else
slouken@6782
   419
			{
slouken@6782
   420
				// synthesize relative moves from the abs position
slouken@6782
   421
				static SDL_Point initialMousePoint;
slouken@6782
   422
				if ( initialMousePoint.x == 0 && initialMousePoint.y == 0 )
slouken@6782
   423
				{
slouken@6782
   424
					initialMousePoint.x = mouse->lLastX;
slouken@6782
   425
					initialMousePoint.y = mouse->lLastY;
slouken@6782
   426
				}
slouken@6350
   427
slouken@6950
   428
				SDL_SendMouseMotion(data->window, 0, 1, (int)(mouse->lLastX-initialMousePoint.x), (int)(mouse->lLastY-initialMousePoint.y) );
slouken@6782
   429
slouken@6782
   430
				initialMousePoint.x = mouse->lLastX;
slouken@6782
   431
				initialMousePoint.y = mouse->lLastY;
slouken@6782
   432
			}
slouken@6945
   433
			WIN_CheckRawMouseButtons( mouse->usButtonFlags, data ); 
slouken@6350
   434
		}
slouken@6350
   435
		break;
slouken@6350
   436
	}
slouken@6350
   437
slouken@5049
   438
    case WM_MOUSEWHEEL:
slouken@5049
   439
        {
slouken@6861
   440
            // FIXME: This may need to accumulate deltas up to WHEEL_DELTA
slouken@6861
   441
            short motion = GET_WHEEL_DELTA_WPARAM(wParam) / WHEEL_DELTA;
slouken@5049
   442
slouken@6950
   443
            SDL_SendMouseWheel(data->window, 0, 0, motion);
slouken@5049
   444
            break;
slouken@5049
   445
        }
slouken@5049
   446
slouken@5086
   447
#ifdef WM_MOUSELEAVE
slouken@5086
   448
    /* FIXME: Do we need the SDL 1.2 hack to generate WM_MOUSELEAVE now? */
slouken@1895
   449
    case WM_MOUSELEAVE:
slouken@4470
   450
        if (SDL_GetMouseFocus() == data->window) {
slouken@4470
   451
            SDL_SetMouseFocus(NULL);
slouken@1895
   452
        }
slouken@3566
   453
        returnCode = 0;
slouken@3566
   454
        break;
slouken@5086
   455
#endif /* WM_MOUSELEAVE */
slouken@1895
   456
slouken@1895
   457
    case WM_SYSKEYDOWN:
slouken@1895
   458
    case WM_KEYDOWN:
slouken@1895
   459
        {
slouken@6938
   460
			SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam );
jorgen@6922
   461
			if ( code != SDL_SCANCODE_UNKNOWN ) {
jorgen@6922
   462
                SDL_SendKeyboardKey(SDL_PRESSED, code );
slouken@2308
   463
            }
slouken@1895
   464
        }
slouken@3566
   465
        returnCode = 0;
slouken@3566
   466
        break;
slouken@1895
   467
slouken@1895
   468
    case WM_SYSKEYUP:
slouken@1895
   469
    case WM_KEYUP:
slouken@1895
   470
        {
slouken@6938
   471
            SDL_Scancode code = WindowsScanCodeToSDLScanCode( lParam, wParam );
slouken@6938
   472
            if ( code != SDL_SCANCODE_UNKNOWN ) {
slouken@6938
   473
                if (code == SDL_SCANCODE_PRINTSCREEN &&
slouken@6938
   474
                    SDL_GetKeyboardState(NULL)[code] == SDL_RELEASED) {
slouken@6938
   475
                    SDL_SendKeyboardKey(SDL_PRESSED, code);
slouken@6938
   476
                }
slouken@6938
   477
                SDL_SendKeyboardKey(SDL_RELEASED, code);
slouken@2308
   478
            }
slouken@1895
   479
        }
slouken@3566
   480
        returnCode = 0;
slouken@3566
   481
        break;
slouken@1895
   482
slouken@2309
   483
    case WM_CHAR:
slouken@2309
   484
        {
slouken@2309
   485
            char text[4];
slouken@2309
   486
slouken@2309
   487
            /* Convert to UTF-8 and send it on... */
slouken@2309
   488
            if (wParam <= 0x7F) {
slouken@2309
   489
                text[0] = (char) wParam;
slouken@2309
   490
                text[1] = '\0';
slouken@2309
   491
            } else if (wParam <= 0x7FF) {
slouken@2309
   492
                text[0] = 0xC0 | (char) ((wParam >> 6) & 0x1F);
slouken@2309
   493
                text[1] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   494
                text[2] = '\0';
slouken@2309
   495
            } else {
slouken@2309
   496
                text[0] = 0xE0 | (char) ((wParam >> 12) & 0x0F);
slouken@2309
   497
                text[1] = 0x80 | (char) ((wParam >> 6) & 0x3F);
slouken@2309
   498
                text[2] = 0x80 | (char) (wParam & 0x3F);
slouken@2309
   499
                text[3] = '\0';
slouken@2309
   500
            }
slouken@4465
   501
            SDL_SendKeyboardText(text);
slouken@2309
   502
        }
slouken@3566
   503
        returnCode = 0;
slouken@3566
   504
        break;
slouken@2309
   505
slouken@5086
   506
#ifdef WM_INPUTLANGCHANGE
slouken@2311
   507
    case WM_INPUTLANGCHANGE:
slouken@2311
   508
        {
slouken@4465
   509
            WIN_UpdateKeymap();
slouken@2311
   510
        }
slouken@3566
   511
        returnCode = 1;
slouken@3566
   512
        break;
slouken@5086
   513
#endif /* WM_INPUTLANGCHANGE */
slouken@2311
   514
slouken@5086
   515
#ifdef WM_GETMINMAXINFO
slouken@1895
   516
    case WM_GETMINMAXINFO:
slouken@1895
   517
        {
slouken@1895
   518
            MINMAXINFO *info;
slouken@1895
   519
            RECT size;
slouken@1895
   520
            int x, y;
slouken@1895
   521
            int w, h;
stopiccot@6682
   522
            int min_w, min_h;
slouken@6788
   523
            int max_w, max_h;
slouken@1895
   524
            int style;
slouken@3168
   525
            BOOL menu;
slouken@6862
   526
			BOOL constrain_max_size;
slouken@1895
   527
slouken@1895
   528
            /* If we allow resizing, let the resize happen naturally */
stopiccot@6682
   529
            if (SDL_IsShapedWindow(data->window))
eligottlieb@4815
   530
                Win32_ResizeWindowShape(data->window);
slouken@1895
   531
slouken@1895
   532
            /* Get the current position of our window */
slouken@1895
   533
            GetWindowRect(hwnd, &size);
slouken@1895
   534
            x = size.left;
slouken@1895
   535
            y = size.top;
slouken@1895
   536
slouken@1895
   537
            /* Calculate current size of our window */
slouken@3685
   538
            SDL_GetWindowSize(data->window, &w, &h);
stopiccot@6682
   539
            SDL_GetWindowMinimumSize(data->window, &min_w, &min_h);
slouken@6788
   540
            SDL_GetWindowMaximumSize(data->window, &max_w, &max_h);
stopiccot@6682
   541
stopiccot@6682
   542
            /* Store in min_w and min_h difference between current size and minimal 
stopiccot@6682
   543
               size so we don't need to call AdjustWindowRectEx twice */
stopiccot@6682
   544
            min_w -= w;
stopiccot@6682
   545
            min_h -= h;
slouken@6837
   546
            if (max_w && max_h) {
slouken@6837
   547
                max_w -= w;
slouken@6837
   548
                max_h -= h;
slouken@6862
   549
                constrain_max_size = TRUE;
slouken@6862
   550
            } else {
slouken@6862
   551
                constrain_max_size = FALSE;
slouken@6837
   552
            }
stopiccot@6682
   553
slouken@1895
   554
            size.top = 0;
slouken@1895
   555
            size.left = 0;
slouken@1895
   556
            size.bottom = h;
slouken@1895
   557
            size.right = w;
slouken@1895
   558
slouken@3168
   559
            style = GetWindowLong(hwnd, GWL_STYLE);
slouken@1895
   560
            /* DJM - according to the docs for GetMenu(), the
slouken@1895
   561
               return value is undefined if hwnd is a child window.
slouken@1895
   562
               Aparently it's too difficult for MS to check
slouken@1895
   563
               inside their function, so I have to do it here.
slouken@1895
   564
             */
slouken@3168
   565
            menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
slouken@3168
   566
            AdjustWindowRectEx(&size, style, menu, 0);
slouken@1895
   567
            w = size.right - size.left;
slouken@1895
   568
            h = size.bottom - size.top;
slouken@1895
   569
slouken@1895
   570
            /* Fix our size to the current size */
slouken@1895
   571
            info = (MINMAXINFO *) lParam;
stopiccot@6682
   572
            if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
stopiccot@6682
   573
                info->ptMinTrackSize.x = w + min_w;
stopiccot@6682
   574
                info->ptMinTrackSize.y = h + min_h;
slouken@6862
   575
                if (constrain_max_size) {
slouken@6862
   576
                    info->ptMaxTrackSize.x = w + max_w;
slouken@6862
   577
                    info->ptMaxTrackSize.y = h + max_h;
slouken@6862
   578
                }
stopiccot@6682
   579
            } else {
stopiccot@6682
   580
                info->ptMaxSize.x = w;
stopiccot@6682
   581
                info->ptMaxSize.y = h;
stopiccot@6682
   582
                info->ptMaxPosition.x = x;
stopiccot@6682
   583
                info->ptMaxPosition.y = y;
stopiccot@6682
   584
                info->ptMinTrackSize.x = w;
stopiccot@6682
   585
                info->ptMinTrackSize.y = h;
stopiccot@6682
   586
                info->ptMaxTrackSize.x = w;
stopiccot@6682
   587
                info->ptMaxTrackSize.y = h;
stopiccot@6682
   588
            }
slouken@1895
   589
        }
slouken@3566
   590
        returnCode = 0;
slouken@3566
   591
        break;
slouken@5086
   592
#endif /* WM_GETMINMAXINFO */
slouken@1895
   593
slouken@1895
   594
    case WM_WINDOWPOSCHANGED:
slouken@1895
   595
        {
slouken@1895
   596
            RECT rect;
slouken@1895
   597
            int x, y;
slouken@1895
   598
            int w, h;
slouken@1895
   599
            Uint32 window_flags;
slouken@1895
   600
slouken@3256
   601
            if (!GetClientRect(hwnd, &rect) ||
slouken@3256
   602
                (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@3256
   603
                break;
slouken@3256
   604
            }
slouken@1895
   605
            ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@1895
   606
            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@1895
   607
slouken@3685
   608
            window_flags = SDL_GetWindowFlags(data->window);
slouken@1895
   609
            if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
slouken@1895
   610
                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
slouken@1895
   611
                ClipCursor(&rect);
slouken@1895
   612
            }
slouken@1895
   613
slouken@1895
   614
            x = rect.left;
slouken@1895
   615
            y = rect.top;
slouken@3685
   616
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
slouken@1895
   617
slouken@1895
   618
            w = rect.right - rect.left;
slouken@1895
   619
            h = rect.bottom - rect.top;
slouken@3685
   620
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
slouken@1895
   621
                                h);
slouken@1895
   622
        }
slouken@1895
   623
        break;
slouken@1895
   624
slouken@1895
   625
    case WM_SETCURSOR:
slouken@1895
   626
        {
slouken@3076
   627
            Uint16 hittest;
slouken@1895
   628
slouken@3076
   629
            hittest = LOWORD(lParam);
slouken@3076
   630
            if (hittest == HTCLIENT) {
slouken@5421
   631
                SetCursor(SDL_cursor);
slouken@3566
   632
                returnCode = TRUE;
slouken@3076
   633
            }
slouken@1895
   634
        }
slouken@1895
   635
        break;
slouken@1895
   636
slouken@1895
   637
        /* We were occluded, refresh our display */
slouken@1895
   638
    case WM_PAINT:
slouken@1895
   639
        {
slouken@1895
   640
            RECT rect;
slouken@1895
   641
            if (GetUpdateRect(hwnd, &rect, FALSE)) {
slouken@1895
   642
                ValidateRect(hwnd, &rect);
slouken@3685
   643
                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
slouken@1895
   644
                                    0, 0);
slouken@1895
   645
            }
slouken@1895
   646
        }
slouken@3566
   647
        returnCode = 0;
slouken@3566
   648
        break;
slouken@3095
   649
slouken@1895
   650
        /* We'll do our own drawing, prevent flicker */
slouken@1895
   651
    case WM_ERASEBKGND:
slouken@1895
   652
        {
slouken@1895
   653
        }
slouken@1895
   654
        return (1);
slouken@1895
   655
slouken@5086
   656
#if defined(SC_SCREENSAVE) || defined(SC_MONITORPOWER)
slouken@1895
   657
    case WM_SYSCOMMAND:
slouken@1895
   658
        {
slouken@1895
   659
            /* Don't start the screensaver or blank the monitor in fullscreen apps */
slouken@1895
   660
            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
slouken@1895
   661
                (wParam & 0xFFF0) == SC_MONITORPOWER) {
slouken@3032
   662
                if (SDL_GetVideoDevice()->suspend_screensaver) {
slouken@1895
   663
                    return (0);
slouken@1895
   664
                }
slouken@1895
   665
            }
slouken@1895
   666
        }
slouken@1895
   667
        break;
slouken@5086
   668
#endif /* System has screensaver support */
slouken@1895
   669
slouken@1895
   670
    case WM_CLOSE:
slouken@1895
   671
        {
slouken@3685
   672
            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
slouken@1895
   673
        }
slouken@3566
   674
        returnCode = 0;
slouken@3566
   675
        break;
slouken@4919
   676
jimtla@4650
   677
	case WM_TOUCH:
slouken@4932
   678
		{
slouken@4932
   679
			UINT i, num_inputs = LOWORD(wParam);
slouken@4932
   680
			PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
slouken@4919
   681
			if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
slouken@4932
   682
				RECT rect;
slouken@4919
   683
				float x, y;
slouken@4919
   684
slouken@4919
   685
				if (!GetClientRect(hwnd, &rect) ||
slouken@4919
   686
				    (rect.right == rect.left && rect.bottom == rect.top)) {
slouken@4919
   687
					break;
slouken@4919
   688
				}
slouken@4919
   689
				ClientToScreen(hwnd, (LPPOINT) & rect);
slouken@4932
   690
				ClientToScreen(hwnd, (LPPOINT) & rect + 1);
slouken@4932
   691
				rect.top *= 100;
slouken@4932
   692
				rect.left *= 100;
slouken@4932
   693
				rect.bottom *= 100;
slouken@4932
   694
				rect.right *= 100;
slouken@4932
   695
slouken@4932
   696
				for (i = 0; i < num_inputs; ++i) {
slouken@4932
   697
					PTOUCHINPUT input = &inputs[i];
slouken@4919
   698
slouken@6951
   699
					const SDL_TouchID touchId = (SDL_TouchID)input->hSource;
slouken@4919
   700
					if (!SDL_GetTouch(touchId)) {
slouken@6951
   701
						if (SDL_AddTouch(touchId, "") < 0) {
slouken@4919
   702
							continue;
slouken@4919
   703
						}
slouken@4932
   704
					}
slouken@4932
   705
slouken@4932
   706
					// Get the normalized coordinates for the window
slouken@4932
   707
					x = (float)(input->x - rect.left)/(rect.right - rect.left);
slouken@4932
   708
					y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
slouken@4932
   709
slouken@4919
   710
					if (input->dwFlags & TOUCHEVENTF_DOWN) {
slouken@6951
   711
						SDL_SendTouch(touchId, input->dwID, SDL_TRUE, x, y, 1.0f);
slouken@4932
   712
					}
slouken@4919
   713
					if (input->dwFlags & TOUCHEVENTF_MOVE) {
slouken@6951
   714
						SDL_SendTouchMotion(touchId, input->dwID, x, y, 1.0f);
slouken@4932
   715
					}
slouken@4919
   716
					if (input->dwFlags & TOUCHEVENTF_UP) {
slouken@6951
   717
						SDL_SendTouch(touchId, input->dwID, SDL_FALSE, x, y, 1.0f);
slouken@4932
   718
					}
slouken@4932
   719
				}
slouken@4932
   720
			}
slouken@4932
   721
			SDL_stack_free(inputs);
slouken@4932
   722
slouken@4932
   723
			data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
slouken@4919
   724
			return 0;
jimtla@4650
   725
		}
jimtla@4650
   726
		break;
slouken@6523
   727
slouken@6523
   728
    case WM_DROPFILES:
slouken@6523
   729
        {
slouken@6523
   730
            UINT i;
slouken@6523
   731
            HDROP drop = (HDROP) wParam;
slouken@6523
   732
            UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
slouken@6523
   733
            for (i = 0; i < count; ++i) {
slouken@6523
   734
                UINT size = DragQueryFile(drop, i, NULL, 0) + 1;
slouken@6523
   735
                LPTSTR buffer = SDL_stack_alloc(TCHAR, size);
slouken@6523
   736
                if (buffer) {
slouken@6523
   737
                    if (DragQueryFile(drop, i, buffer, size)) {
slouken@6523
   738
                        char *file = WIN_StringToUTF8(buffer);
slouken@6523
   739
                        SDL_SendDropFile(file);
slouken@6523
   740
                        SDL_free(file);
slouken@6523
   741
                    }
slouken@6523
   742
                    SDL_stack_free(buffer);
slouken@6523
   743
                }
slouken@6523
   744
            }
slouken@6523
   745
            DragFinish(drop);
slouken@6523
   746
            return 0;
slouken@6523
   747
        }
slouken@6523
   748
        break;
slouken@6523
   749
    }
slouken@3566
   750
slouken@3566
   751
    /* If there's a window proc, assume it's going to handle messages */
slouken@3566
   752
    if (data->wndproc) {
slouken@3566
   753
        return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
slouken@3566
   754
    } else if (returnCode >= 0) {
slouken@3566
   755
        return returnCode;
slouken@3566
   756
    } else {
slouken@3566
   757
        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
slouken@3566
   758
    }
slouken@1895
   759
}
slouken@1895
   760
slouken@1895
   761
void
slouken@1895
   762
WIN_PumpEvents(_THIS)
slouken@1895
   763
{
slouken@1895
   764
    MSG msg;
slouken@1895
   765
    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
slouken@1895
   766
        TranslateMessage(&msg);
slouken@1895
   767
        DispatchMessage(&msg);
slouken@1895
   768
    }
slouken@1895
   769
}
slouken@1895
   770
slouken@1895
   771
static int app_registered = 0;
slouken@1895
   772
LPTSTR SDL_Appname = NULL;
slouken@1895
   773
Uint32 SDL_Appstyle = 0;
slouken@1895
   774
HINSTANCE SDL_Instance = NULL;
slouken@1895
   775
slouken@1895
   776
/* Register the class for this application */
slouken@1895
   777
int
slouken@1895
   778
SDL_RegisterApp(char *name, Uint32 style, void *hInst)
slouken@1895
   779
{
slouken@1895
   780
    WNDCLASS class;
slouken@1895
   781
slouken@1895
   782
    /* Only do this once... */
slouken@1895
   783
    if (app_registered) {
slouken@1895
   784
        ++app_registered;
slouken@1895
   785
        return (0);
slouken@1895
   786
    }
slouken@1895
   787
    if (!name && !SDL_Appname) {
slouken@1895
   788
        name = "SDL_app";
slouken@5086
   789
#if defined(CS_BYTEALIGNCLIENT) || defined(CS_OWNDC)
slouken@1895
   790
        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
slouken@5086
   791
#endif
slouken@1895
   792
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   793
    }
slouken@1895
   794
slouken@1895
   795
    if (name) {
slouken@1895
   796
        SDL_Appname = WIN_UTF8ToString(name);
slouken@1895
   797
        SDL_Appstyle = style;
slouken@1895
   798
        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
slouken@1895
   799
    }
slouken@1895
   800
slouken@1895
   801
    /* Register the application class */
slouken@1895
   802
    class.hCursor = NULL;
slouken@2710
   803
    class.hIcon =
slouken@2710
   804
        LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
slouken@2710
   805
                  LR_DEFAULTCOLOR);
slouken@1895
   806
    class.lpszMenuName = NULL;
slouken@1895
   807
    class.lpszClassName = SDL_Appname;
slouken@1895
   808
    class.hbrBackground = NULL;
slouken@1895
   809
    class.hInstance = SDL_Instance;
slouken@1895
   810
    class.style = SDL_Appstyle;
dewyatt@4733
   811
    class.lpfnWndProc = WIN_WindowProc;
slouken@1895
   812
    class.cbWndExtra = 0;
slouken@1895
   813
    class.cbClsExtra = 0;
slouken@1895
   814
    if (!RegisterClass(&class)) {
icculus@7037
   815
        return SDL_SetError("Couldn't register application class");
slouken@1895
   816
    }
slouken@1895
   817
slouken@1895
   818
    app_registered = 1;
icculus@7037
   819
    return 0;
slouken@1895
   820
}
slouken@1895
   821
slouken@1895
   822
/* Unregisters the windowclass registered in SDL_RegisterApp above. */
slouken@1895
   823
void
slouken@1895
   824
SDL_UnregisterApp()
slouken@1895
   825
{
slouken@1895
   826
    WNDCLASS class;
slouken@1895
   827
slouken@1895
   828
    /* SDL_RegisterApp might not have been called before */
slouken@1895
   829
    if (!app_registered) {
slouken@1895
   830
        return;
slouken@1895
   831
    }
slouken@1895
   832
    --app_registered;
slouken@1895
   833
    if (app_registered == 0) {
slouken@1895
   834
        /* Check for any registered window classes. */
slouken@1895
   835
        if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
slouken@1895
   836
            UnregisterClass(SDL_Appname, SDL_Instance);
slouken@1895
   837
        }
slouken@1895
   838
        SDL_free(SDL_Appname);
slouken@1895
   839
        SDL_Appname = NULL;
slouken@1895
   840
    }
slouken@1895
   841
}
slouken@1895
   842
slouken@6044
   843
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   844
slouken@1895
   845
/* vi: set ts=4 sw=4 expandtab: */