src/video/windows/SDL_windowsevents.c
changeset 5062 e8916fe9cfc8
parent 5056 8b7988f42fcb
child 5086 c2539ff054c8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/windows/SDL_windowsevents.c	Thu Jan 20 18:04:05 2011 -0800
     1.3 @@ -0,0 +1,712 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2010 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +
    1.26 +#include "SDL_config.h"
    1.27 +
    1.28 +#include "SDL_windowsvideo.h"
    1.29 +#include "SDL_windowsshape.h"
    1.30 +#include "SDL_syswm.h"
    1.31 +#include "SDL_vkeys.h"
    1.32 +#include "../../events/SDL_events_c.h"
    1.33 +#include "../../events/SDL_touch_c.h"
    1.34 +
    1.35 +
    1.36 +
    1.37 +/*#define WMMSG_DEBUG*/
    1.38 +#ifdef WMMSG_DEBUG
    1.39 +#include <stdio.h>	
    1.40 +#include "wmmsg.h"
    1.41 +#endif
    1.42 +
    1.43 +/* Masks for processing the windows KEYDOWN and KEYUP messages */
    1.44 +#define REPEATED_KEYMASK    (1<<30)
    1.45 +#define EXTENDED_KEYMASK    (1<<24)
    1.46 +
    1.47 +#define VK_ENTER    10          /* Keypad Enter ... no VKEY defined? */
    1.48 +
    1.49 +/* Make sure XBUTTON stuff is defined that isn't in older Platform SDKs... */
    1.50 +#ifndef WM_XBUTTONDOWN
    1.51 +#define WM_XBUTTONDOWN 0x020B
    1.52 +#endif
    1.53 +#ifndef WM_XBUTTONUP
    1.54 +#define WM_XBUTTONUP 0x020C
    1.55 +#endif
    1.56 +#ifndef GET_XBUTTON_WPARAM
    1.57 +#define GET_XBUTTON_WPARAM(w) (HIWORD(w))
    1.58 +#endif
    1.59 +#ifndef WM_INPUT
    1.60 +#define WM_INPUT 0x00ff
    1.61 +#endif
    1.62 +#ifndef WM_TOUCH
    1.63 +#define WM_TOUCH 0x0240
    1.64 +#endif
    1.65 +
    1.66 +
    1.67 +static WPARAM
    1.68 +RemapVKEY(WPARAM wParam, LPARAM lParam)
    1.69 +{
    1.70 +    int i;
    1.71 +    BYTE scancode = (BYTE) ((lParam >> 16) & 0xFF);
    1.72 +
    1.73 +    /* Windows remaps alphabetic keys based on current layout.
    1.74 +       We try to provide USB scancodes, so undo this mapping.
    1.75 +     */
    1.76 +    if (wParam >= 'A' && wParam <= 'Z') {
    1.77 +        if (scancode != alpha_scancodes[wParam - 'A']) {
    1.78 +            for (i = 0; i < SDL_arraysize(alpha_scancodes); ++i) {
    1.79 +                if (scancode == alpha_scancodes[i]) {
    1.80 +                    wParam = 'A' + i;
    1.81 +                    break;
    1.82 +                }
    1.83 +            }
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    /* Keypad keys are a little trickier, we always scan for them.
    1.88 +       Keypad arrow keys have the same scancode as normal arrow keys,
    1.89 +       except they don't have the extended bit (0x1000000) set.
    1.90 +     */
    1.91 +    if (!(lParam & 0x1000000)) {
    1.92 +        if (wParam == VK_DELETE) {
    1.93 +            wParam = VK_DECIMAL;
    1.94 +        } else {
    1.95 +            for (i = 0; i < SDL_arraysize(keypad_scancodes); ++i) {
    1.96 +                if (scancode == keypad_scancodes[i]) {
    1.97 +                    wParam = VK_NUMPAD0 + i;
    1.98 +                    break;
    1.99 +                }
   1.100 +            }
   1.101 +        }
   1.102 +    }
   1.103 +
   1.104 +    return wParam;
   1.105 +}
   1.106 +
   1.107 +LRESULT CALLBACK
   1.108 +WIN_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
   1.109 +{
   1.110 +    SDL_WindowData *data;
   1.111 +    LRESULT returnCode = -1;
   1.112 +
   1.113 +    /* Send a SDL_SYSWMEVENT if the application wants them */
   1.114 +    if (SDL_GetEventState(SDL_SYSWMEVENT) == SDL_ENABLE) {
   1.115 +        SDL_SysWMmsg wmmsg;
   1.116 +
   1.117 +        SDL_VERSION(&wmmsg.version);
   1.118 +        wmmsg.subsystem = SDL_SYSWM_WINDOWS;
   1.119 +        wmmsg.msg.win.hwnd = hwnd;
   1.120 +        wmmsg.msg.win.msg = msg;
   1.121 +        wmmsg.msg.win.wParam = wParam;
   1.122 +        wmmsg.msg.win.lParam = lParam;
   1.123 +        SDL_SendSysWMEvent(&wmmsg);
   1.124 +    }
   1.125 +
   1.126 +    /* Get the window data for the window */
   1.127 +    data = (SDL_WindowData *) GetProp(hwnd, TEXT("SDL_WindowData"));
   1.128 +    if (!data) {
   1.129 +        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
   1.130 +    }
   1.131 +
   1.132 +#ifdef WMMSG_DEBUG
   1.133 +    {		
   1.134 +        FILE *log = fopen("wmmsg.txt", "a");		
   1.135 +        fprintf(log, "Received windows message: %p ", hwnd);
   1.136 +        if (msg > MAX_WMMSG) {
   1.137 +            fprintf(log, "%d", msg);
   1.138 +        } else {
   1.139 +            fprintf(log, "%s", wmtab[msg]);
   1.140 +        }
   1.141 +        fprintf(log, " -- 0x%X, 0x%X\n", wParam, lParam);
   1.142 +        fclose(log);
   1.143 +    }
   1.144 +#endif
   1.145 +
   1.146 +    if (IME_HandleMessage(hwnd, msg, wParam, &lParam, data->videodata))
   1.147 +        return 0;
   1.148 +
   1.149 +    switch (msg) {
   1.150 +
   1.151 +    case WM_SHOWWINDOW:
   1.152 +        {
   1.153 +            if (wParam) {
   1.154 +                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
   1.155 +            } else {
   1.156 +                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
   1.157 +            }
   1.158 +        }
   1.159 +        break;
   1.160 +
   1.161 +    case WM_ACTIVATE:
   1.162 +        {
   1.163 +            BOOL minimized;
   1.164 +
   1.165 +            minimized = HIWORD(wParam);
   1.166 +            if (!minimized && (LOWORD(wParam) != WA_INACTIVE)) {
   1.167 +                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
   1.168 +                SDL_SendWindowEvent(data->window,
   1.169 +                                    SDL_WINDOWEVENT_RESTORED, 0, 0);
   1.170 +#ifndef _WIN32_WCE              /* WinCE misses IsZoomed() */
   1.171 +                if (IsZoomed(hwnd)) {
   1.172 +                    SDL_SendWindowEvent(data->window,
   1.173 +                                        SDL_WINDOWEVENT_MAXIMIZED, 0, 0);
   1.174 +                }
   1.175 +#endif
   1.176 +                if (SDL_GetKeyboardFocus() != data->window) {
   1.177 +                    SDL_SetKeyboardFocus(data->window);
   1.178 +                }
   1.179 +                /*
   1.180 +                 * FIXME: Update keyboard state
   1.181 +                 */
   1.182 +                WIN_CheckClipboardUpdate(data->videodata);
   1.183 +            } else {
   1.184 +                if (SDL_GetKeyboardFocus() == data->window) {
   1.185 +                    SDL_SetKeyboardFocus(NULL);
   1.186 +                }
   1.187 +                if (minimized) {
   1.188 +                    SDL_SendWindowEvent(data->window,
   1.189 +                                        SDL_WINDOWEVENT_MINIMIZED, 0, 0);
   1.190 +                }
   1.191 +            }
   1.192 +        }
   1.193 +        returnCode = 0;
   1.194 +        break;
   1.195 +
   1.196 +	case WM_MOUSEMOVE:
   1.197 +#ifdef _WIN32_WCE
   1.198 +	/* transform coords for VGA, WVGA... */
   1.199 +	{
   1.200 +	    SDL_VideoData *videodata = data->videodata;
   1.201 +	    if(videodata->CoordTransform &&
   1.202 +		(videodata->render == RENDER_GAPI || videodata->render == RENDER_RAW))
   1.203 +	    {
   1.204 +		POINT pt;
   1.205 +		pt.x = LOWORD(lParam);
   1.206 +		pt.y = HIWORD(lParam);
   1.207 +		videodata->CoordTransform(data->window, &pt);
   1.208 +    		SDL_SendMouseMotion(data->window, 0, pt.x, pt.y);
   1.209 +		break;
   1.210 +	    }
   1.211 +	}
   1.212 +#endif
   1.213 +        SDL_SendMouseMotion(data->window, 0, LOWORD(lParam), HIWORD(lParam));
   1.214 +        break;
   1.215 +
   1.216 +    case WM_LBUTTONDOWN:
   1.217 +        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_LEFT);
   1.218 +        break;
   1.219 +
   1.220 +    case WM_LBUTTONUP:
   1.221 +        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_LEFT);
   1.222 +        break;
   1.223 +
   1.224 +    case WM_RBUTTONDOWN:
   1.225 +        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_RIGHT);
   1.226 +        break;
   1.227 +
   1.228 +    case WM_RBUTTONUP:
   1.229 +        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_RIGHT);
   1.230 +        break;
   1.231 +
   1.232 +    case WM_MBUTTONDOWN:
   1.233 +        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_MIDDLE);
   1.234 +        break;
   1.235 +
   1.236 +    case WM_MBUTTONUP:
   1.237 +        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_MIDDLE);
   1.238 +        break;
   1.239 +
   1.240 +    case WM_XBUTTONDOWN:
   1.241 +        SDL_SendMouseButton(data->window, SDL_PRESSED, SDL_BUTTON_X1 + GET_XBUTTON_WPARAM(wParam) - 1);
   1.242 +        returnCode = TRUE;
   1.243 +        break;
   1.244 +
   1.245 +    case WM_XBUTTONUP:
   1.246 +        SDL_SendMouseButton(data->window, SDL_RELEASED, SDL_BUTTON_X1 + GET_XBUTTON_WPARAM(wParam) - 1);
   1.247 +        returnCode = TRUE;
   1.248 +        break;
   1.249 +
   1.250 +    case WM_MOUSEWHEEL:
   1.251 +        {
   1.252 +            int motion = (short) HIWORD(wParam);
   1.253 +
   1.254 +            SDL_SendMouseWheel(data->window, 0, motion);
   1.255 +            break;
   1.256 +        }
   1.257 +
   1.258 +    case WM_MOUSELEAVE:
   1.259 +        if (SDL_GetMouseFocus() == data->window) {
   1.260 +            SDL_SetMouseFocus(NULL);
   1.261 +        }
   1.262 +        returnCode = 0;
   1.263 +        break;
   1.264 +
   1.265 +    case WM_SYSKEYDOWN:
   1.266 +    case WM_KEYDOWN:
   1.267 +        {
   1.268 +            wParam = RemapVKEY(wParam, lParam);
   1.269 +            switch (wParam) {
   1.270 +            case VK_CONTROL:
   1.271 +                if (lParam & EXTENDED_KEYMASK)
   1.272 +                    wParam = VK_RCONTROL;
   1.273 +                else
   1.274 +                    wParam = VK_LCONTROL;
   1.275 +                break;
   1.276 +            case VK_SHIFT:
   1.277 +                /* EXTENDED trick doesn't work here */
   1.278 +                {
   1.279 +                    Uint8 *state = SDL_GetKeyboardState(NULL);
   1.280 +                    if (state[SDL_SCANCODE_LSHIFT] == SDL_RELEASED
   1.281 +                        && (GetKeyState(VK_LSHIFT) & 0x8000)) {
   1.282 +                        wParam = VK_LSHIFT;
   1.283 +                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_RELEASED
   1.284 +                               && (GetKeyState(VK_RSHIFT) & 0x8000)) {
   1.285 +                        wParam = VK_RSHIFT;
   1.286 +                    } else {
   1.287 +                        /* Probably a key repeat */
   1.288 +                        wParam = 256;
   1.289 +                    }
   1.290 +                }
   1.291 +                break;
   1.292 +            case VK_MENU:
   1.293 +                if (lParam & EXTENDED_KEYMASK)
   1.294 +                    wParam = VK_RMENU;
   1.295 +                else
   1.296 +                    wParam = VK_LMENU;
   1.297 +                break;
   1.298 +            case VK_RETURN:
   1.299 +                if (lParam & EXTENDED_KEYMASK)
   1.300 +                    wParam = VK_ENTER;
   1.301 +                break;
   1.302 +            }
   1.303 +            if (wParam < 256) {
   1.304 +                SDL_SendKeyboardKey(SDL_PRESSED,
   1.305 +                                    data->videodata->key_layout[wParam]);
   1.306 +            }
   1.307 +        }
   1.308 +        returnCode = 0;
   1.309 +        break;
   1.310 +
   1.311 +    case WM_SYSKEYUP:
   1.312 +    case WM_KEYUP:
   1.313 +        {
   1.314 +            wParam = RemapVKEY(wParam, lParam);
   1.315 +            switch (wParam) {
   1.316 +            case VK_CONTROL:
   1.317 +                if (lParam & EXTENDED_KEYMASK)
   1.318 +                    wParam = VK_RCONTROL;
   1.319 +                else
   1.320 +                    wParam = VK_LCONTROL;
   1.321 +                break;
   1.322 +            case VK_SHIFT:
   1.323 +                /* EXTENDED trick doesn't work here */
   1.324 +                {
   1.325 +                    Uint8 *state = SDL_GetKeyboardState(NULL);
   1.326 +                    if (state[SDL_SCANCODE_LSHIFT] == SDL_PRESSED
   1.327 +                        && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
   1.328 +                        wParam = VK_LSHIFT;
   1.329 +                    } else if (state[SDL_SCANCODE_RSHIFT] == SDL_PRESSED
   1.330 +                               && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
   1.331 +                        wParam = VK_RSHIFT;
   1.332 +                    } else {
   1.333 +                        /* Probably a key repeat */
   1.334 +                        wParam = 256;
   1.335 +                    }
   1.336 +                }
   1.337 +                break;
   1.338 +            case VK_MENU:
   1.339 +                if (lParam & EXTENDED_KEYMASK)
   1.340 +                    wParam = VK_RMENU;
   1.341 +                else
   1.342 +                    wParam = VK_LMENU;
   1.343 +                break;
   1.344 +            case VK_RETURN:
   1.345 +                if (lParam & EXTENDED_KEYMASK)
   1.346 +                    wParam = VK_ENTER;
   1.347 +                break;
   1.348 +            }
   1.349 +
   1.350 +            /* Windows only reports keyup for print screen */
   1.351 +            if (wParam == VK_SNAPSHOT
   1.352 +                && SDL_GetKeyboardState(NULL)[SDL_SCANCODE_PRINTSCREEN] ==
   1.353 +                SDL_RELEASED) {
   1.354 +                SDL_SendKeyboardKey(SDL_PRESSED,
   1.355 +                                    data->videodata->key_layout[wParam]);
   1.356 +            }
   1.357 +            if (wParam < 256) {
   1.358 +                SDL_SendKeyboardKey(SDL_RELEASED,
   1.359 +                                    data->videodata->key_layout[wParam]);
   1.360 +            }
   1.361 +        }
   1.362 +        returnCode = 0;
   1.363 +        break;
   1.364 +
   1.365 +    case WM_CHAR:
   1.366 +        {
   1.367 +            char text[4];
   1.368 +
   1.369 +            /* Convert to UTF-8 and send it on... */
   1.370 +            if (wParam <= 0x7F) {
   1.371 +                text[0] = (char) wParam;
   1.372 +                text[1] = '\0';
   1.373 +            } else if (wParam <= 0x7FF) {
   1.374 +                text[0] = 0xC0 | (char) ((wParam >> 6) & 0x1F);
   1.375 +                text[1] = 0x80 | (char) (wParam & 0x3F);
   1.376 +                text[2] = '\0';
   1.377 +            } else {
   1.378 +                text[0] = 0xE0 | (char) ((wParam >> 12) & 0x0F);
   1.379 +                text[1] = 0x80 | (char) ((wParam >> 6) & 0x3F);
   1.380 +                text[2] = 0x80 | (char) (wParam & 0x3F);
   1.381 +                text[3] = '\0';
   1.382 +            }
   1.383 +            SDL_SendKeyboardText(text);
   1.384 +        }
   1.385 +        returnCode = 0;
   1.386 +        break;
   1.387 +
   1.388 +    case WM_INPUTLANGCHANGE:
   1.389 +        {
   1.390 +            WIN_UpdateKeymap();
   1.391 +        }
   1.392 +        returnCode = 1;
   1.393 +        break;
   1.394 +
   1.395 +    case WM_GETMINMAXINFO:
   1.396 +        {
   1.397 +            MINMAXINFO *info;
   1.398 +            RECT size;
   1.399 +            int x, y;
   1.400 +            int w, h;
   1.401 +            int style;
   1.402 +            BOOL menu;
   1.403 +
   1.404 +            /* If we allow resizing, let the resize happen naturally */
   1.405 +            if(SDL_IsShapedWindow(data->window))
   1.406 +                Win32_ResizeWindowShape(data->window);
   1.407 +            if (SDL_GetWindowFlags(data->window) & SDL_WINDOW_RESIZABLE) {
   1.408 +                returnCode = 0;
   1.409 +                break;
   1.410 +            }
   1.411 +
   1.412 +            /* Get the current position of our window */
   1.413 +            GetWindowRect(hwnd, &size);
   1.414 +            x = size.left;
   1.415 +            y = size.top;
   1.416 +
   1.417 +            /* Calculate current size of our window */
   1.418 +            SDL_GetWindowSize(data->window, &w, &h);
   1.419 +            size.top = 0;
   1.420 +            size.left = 0;
   1.421 +            size.bottom = h;
   1.422 +            size.right = w;
   1.423 +
   1.424 +
   1.425 +            style = GetWindowLong(hwnd, GWL_STYLE);
   1.426 +#ifdef _WIN32_WCE
   1.427 +            menu = FALSE;
   1.428 +#else
   1.429 +            /* DJM - according to the docs for GetMenu(), the
   1.430 +               return value is undefined if hwnd is a child window.
   1.431 +               Aparently it's too difficult for MS to check
   1.432 +               inside their function, so I have to do it here.
   1.433 +             */
   1.434 +            menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
   1.435 +#endif
   1.436 +            AdjustWindowRectEx(&size, style, menu, 0);
   1.437 +            w = size.right - size.left;
   1.438 +            h = size.bottom - size.top;
   1.439 +
   1.440 +            /* Fix our size to the current size */
   1.441 +            info = (MINMAXINFO *) lParam;
   1.442 +            info->ptMaxSize.x = w;
   1.443 +            info->ptMaxSize.y = h;
   1.444 +            info->ptMaxPosition.x = x;
   1.445 +            info->ptMaxPosition.y = y;
   1.446 +            info->ptMinTrackSize.x = w;
   1.447 +            info->ptMinTrackSize.y = h;
   1.448 +            info->ptMaxTrackSize.x = w;
   1.449 +            info->ptMaxTrackSize.y = h;
   1.450 +        }
   1.451 +        returnCode = 0;
   1.452 +        break;
   1.453 +
   1.454 +    case WM_WINDOWPOSCHANGED:
   1.455 +        {
   1.456 +            RECT rect;
   1.457 +            int x, y;
   1.458 +            int w, h;
   1.459 +            Uint32 window_flags;
   1.460 +
   1.461 +            if (!GetClientRect(hwnd, &rect) ||
   1.462 +                (rect.right == rect.left && rect.bottom == rect.top)) {
   1.463 +                break;
   1.464 +            }
   1.465 +            ClientToScreen(hwnd, (LPPOINT) & rect);
   1.466 +            ClientToScreen(hwnd, (LPPOINT) & rect + 1);
   1.467 +
   1.468 +            window_flags = SDL_GetWindowFlags(data->window);
   1.469 +            if ((window_flags & SDL_WINDOW_INPUT_GRABBED) &&
   1.470 +                (window_flags & SDL_WINDOW_INPUT_FOCUS)) {
   1.471 +                ClipCursor(&rect);
   1.472 +            }
   1.473 +
   1.474 +            x = rect.left;
   1.475 +            y = rect.top;
   1.476 +            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_MOVED, x, y);
   1.477 +
   1.478 +            w = rect.right - rect.left;
   1.479 +            h = rect.bottom - rect.top;
   1.480 +            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_RESIZED, w,
   1.481 +                                h);
   1.482 +        }
   1.483 +        break;
   1.484 +
   1.485 +    case WM_SETCURSOR:
   1.486 +        {
   1.487 +            Uint16 hittest;
   1.488 +
   1.489 +            hittest = LOWORD(lParam);
   1.490 +            if (hittest == HTCLIENT) {
   1.491 +                /* FIXME: Implement the cursor API */
   1.492 +                static HCURSOR cursor;
   1.493 +                if (!cursor) {
   1.494 +                    cursor = LoadCursor(NULL, IDC_ARROW);
   1.495 +                }
   1.496 +                SetCursor(cursor);
   1.497 +                returnCode = TRUE;
   1.498 +            }
   1.499 +        }
   1.500 +        break;
   1.501 +
   1.502 +        /* We are about to get palette focus! */
   1.503 +    case WM_QUERYNEWPALETTE:
   1.504 +        {
   1.505 +            /*
   1.506 +                WIN_RealizePalette(current_video);
   1.507 +                returnCode = TRUE;
   1.508 +             */
   1.509 +        }
   1.510 +        break;
   1.511 +
   1.512 +        /* Another application changed the palette */
   1.513 +    case WM_PALETTECHANGED:
   1.514 +        {
   1.515 +            /*
   1.516 +               WIN_PaletteChanged(current_video, (HWND) wParam);
   1.517 +             */
   1.518 +        }
   1.519 +        break;
   1.520 +
   1.521 +        /* We were occluded, refresh our display */
   1.522 +    case WM_PAINT:
   1.523 +        {
   1.524 +            RECT rect;
   1.525 +            if (GetUpdateRect(hwnd, &rect, FALSE)) {
   1.526 +                ValidateRect(hwnd, &rect);
   1.527 +                SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_EXPOSED,
   1.528 +                                    0, 0);
   1.529 +            }
   1.530 +        }
   1.531 +        returnCode = 0;
   1.532 +        break;
   1.533 +
   1.534 +        /* We'll do our own drawing, prevent flicker */
   1.535 +    case WM_ERASEBKGND:
   1.536 +        {
   1.537 +        }
   1.538 +        return (1);
   1.539 +
   1.540 +    case WM_SYSCOMMAND:
   1.541 +        {
   1.542 +            /* Don't start the screensaver or blank the monitor in fullscreen apps */
   1.543 +            if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
   1.544 +                (wParam & 0xFFF0) == SC_MONITORPOWER) {
   1.545 +                if (SDL_GetVideoDevice()->suspend_screensaver) {
   1.546 +                    return (0);
   1.547 +                }
   1.548 +            }
   1.549 +        }
   1.550 +        break;
   1.551 +
   1.552 +    case WM_CLOSE:
   1.553 +        {
   1.554 +            SDL_SendWindowEvent(data->window, SDL_WINDOWEVENT_CLOSE, 0, 0);
   1.555 +        }
   1.556 +        returnCode = 0;
   1.557 +        break;
   1.558 +
   1.559 +	case WM_TOUCH:
   1.560 +		{
   1.561 +			UINT i, num_inputs = LOWORD(wParam);
   1.562 +			PTOUCHINPUT inputs = SDL_stack_alloc(TOUCHINPUT, num_inputs);
   1.563 +			if (data->videodata->GetTouchInputInfo((HTOUCHINPUT)lParam, num_inputs, inputs, sizeof(TOUCHINPUT))) {
   1.564 +				RECT rect;
   1.565 +				float x, y;
   1.566 +
   1.567 +				if (!GetClientRect(hwnd, &rect) ||
   1.568 +				    (rect.right == rect.left && rect.bottom == rect.top)) {
   1.569 +					break;
   1.570 +				}
   1.571 +				ClientToScreen(hwnd, (LPPOINT) & rect);
   1.572 +				ClientToScreen(hwnd, (LPPOINT) & rect + 1);
   1.573 +				rect.top *= 100;
   1.574 +				rect.left *= 100;
   1.575 +				rect.bottom *= 100;
   1.576 +				rect.right *= 100;
   1.577 +
   1.578 +				for (i = 0; i < num_inputs; ++i) {
   1.579 +					PTOUCHINPUT input = &inputs[i];
   1.580 +
   1.581 +					SDL_TouchID touchId = (SDL_TouchID)input->hSource;
   1.582 +					if (!SDL_GetTouch(touchId)) {
   1.583 +						SDL_Touch touch;
   1.584 +
   1.585 +						touch.id = touchId;
   1.586 +						touch.x_min = 0;
   1.587 +						touch.x_max = 1;
   1.588 +						touch.native_xres = touch.x_max - touch.x_min;
   1.589 +						touch.y_min = 0;
   1.590 +						touch.y_max = 1;
   1.591 +						touch.native_yres = touch.y_max - touch.y_min;
   1.592 +						touch.pressure_min = 0;
   1.593 +						touch.pressure_max = 1;
   1.594 +						touch.native_pressureres = touch.pressure_max - touch.pressure_min;
   1.595 +
   1.596 +						if (SDL_AddTouch(&touch, "") < 0) {
   1.597 +							continue;
   1.598 +						}
   1.599 +					}
   1.600 +
   1.601 +					// Get the normalized coordinates for the window
   1.602 +					x = (float)(input->x - rect.left)/(rect.right - rect.left);
   1.603 +					y = (float)(input->y - rect.top)/(rect.bottom - rect.top);
   1.604 +
   1.605 +					if (input->dwFlags & TOUCHEVENTF_DOWN) {
   1.606 +						SDL_SendFingerDown(touchId, input->dwID, SDL_TRUE, x, y, 1);
   1.607 +					}
   1.608 +					if (input->dwFlags & TOUCHEVENTF_MOVE) {
   1.609 +						SDL_SendTouchMotion(touchId, input->dwID, SDL_FALSE, x, y, 1);
   1.610 +					}
   1.611 +					if (input->dwFlags & TOUCHEVENTF_UP) {
   1.612 +						SDL_SendFingerDown(touchId, input->dwID, SDL_FALSE, x, y, 1);
   1.613 +					}
   1.614 +				}
   1.615 +			}
   1.616 +			SDL_stack_free(inputs);
   1.617 +
   1.618 +			data->videodata->CloseTouchInputHandle((HTOUCHINPUT)lParam);
   1.619 +			return 0;
   1.620 +		}
   1.621 +		break;
   1.622 +	}
   1.623 +
   1.624 +    /* If there's a window proc, assume it's going to handle messages */
   1.625 +    if (data->wndproc) {
   1.626 +        return CallWindowProc(data->wndproc, hwnd, msg, wParam, lParam);
   1.627 +    } else if (returnCode >= 0) {
   1.628 +        return returnCode;
   1.629 +    } else {
   1.630 +        return CallWindowProc(DefWindowProc, hwnd, msg, wParam, lParam);
   1.631 +    }
   1.632 +}
   1.633 +
   1.634 +void
   1.635 +WIN_PumpEvents(_THIS)
   1.636 +{
   1.637 +    MSG msg;
   1.638 +    while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
   1.639 +        TranslateMessage(&msg);
   1.640 +        DispatchMessage(&msg);
   1.641 +    }
   1.642 +}
   1.643 +
   1.644 +static int app_registered = 0;
   1.645 +LPTSTR SDL_Appname = NULL;
   1.646 +Uint32 SDL_Appstyle = 0;
   1.647 +HINSTANCE SDL_Instance = NULL;
   1.648 +
   1.649 +/* Register the class for this application */
   1.650 +int
   1.651 +SDL_RegisterApp(char *name, Uint32 style, void *hInst)
   1.652 +{
   1.653 +    WNDCLASS class;
   1.654 +
   1.655 +    /* Only do this once... */
   1.656 +    if (app_registered) {
   1.657 +        ++app_registered;
   1.658 +        return (0);
   1.659 +    }
   1.660 +    if (!name && !SDL_Appname) {
   1.661 +        name = "SDL_app";
   1.662 +        SDL_Appstyle = (CS_BYTEALIGNCLIENT | CS_OWNDC);
   1.663 +        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
   1.664 +    }
   1.665 +
   1.666 +    if (name) {
   1.667 +        SDL_Appname = WIN_UTF8ToString(name);
   1.668 +        SDL_Appstyle = style;
   1.669 +        SDL_Instance = hInst ? hInst : GetModuleHandle(NULL);
   1.670 +    }
   1.671 +
   1.672 +    /* Register the application class */
   1.673 +    class.hCursor = NULL;
   1.674 +    class.hIcon =
   1.675 +        LoadImage(SDL_Instance, SDL_Appname, IMAGE_ICON, 0, 0,
   1.676 +                  LR_DEFAULTCOLOR);
   1.677 +    class.lpszMenuName = NULL;
   1.678 +    class.lpszClassName = SDL_Appname;
   1.679 +    class.hbrBackground = NULL;
   1.680 +    class.hInstance = SDL_Instance;
   1.681 +    class.style = SDL_Appstyle;
   1.682 +    class.lpfnWndProc = WIN_WindowProc;
   1.683 +    class.cbWndExtra = 0;
   1.684 +    class.cbClsExtra = 0;
   1.685 +    if (!RegisterClass(&class)) {
   1.686 +        SDL_SetError("Couldn't register application class");
   1.687 +        return (-1);
   1.688 +    }
   1.689 +
   1.690 +    app_registered = 1;
   1.691 +    return (0);
   1.692 +}
   1.693 +
   1.694 +/* Unregisters the windowclass registered in SDL_RegisterApp above. */
   1.695 +void
   1.696 +SDL_UnregisterApp()
   1.697 +{
   1.698 +    WNDCLASS class;
   1.699 +
   1.700 +    /* SDL_RegisterApp might not have been called before */
   1.701 +    if (!app_registered) {
   1.702 +        return;
   1.703 +    }
   1.704 +    --app_registered;
   1.705 +    if (app_registered == 0) {
   1.706 +        /* Check for any registered window classes. */
   1.707 +        if (GetClassInfo(SDL_Instance, SDL_Appname, &class)) {
   1.708 +            UnregisterClass(SDL_Appname, SDL_Instance);
   1.709 +        }
   1.710 +        SDL_free(SDL_Appname);
   1.711 +        SDL_Appname = NULL;
   1.712 +    }
   1.713 +}
   1.714 +
   1.715 +/* vi: set ts=4 sw=4 expandtab: */