src/video/windows/SDL_windowsmouse.c
author Gabriel Jacobo <gabomdq@gmail.com>
Wed, 24 Apr 2013 12:22:08 -0300
changeset 7103 9d146930d4f2
parent 7037 3fedf1f25b94
child 7191 75360622e65f
permissions -rw-r--r--
Bug 1787 - memory leak in WIN_InitMouse() by Marcel Bakker
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@5472
    25
#include "SDL_assert.h"
slouken@5062
    26
#include "SDL_windowsvideo.h"
slouken@1895
    27
slouken@5421
    28
#include "../../events/SDL_mouse_c.h"
slouken@5421
    29
slouken@5421
    30
slouken@5421
    31
HCURSOR SDL_cursor = NULL;
slouken@5421
    32
slouken@5421
    33
slouken@5421
    34
static SDL_Cursor *
slouken@5421
    35
WIN_CreateDefaultCursor()
slouken@5421
    36
{
slouken@5421
    37
    SDL_Cursor *cursor;
slouken@5421
    38
slouken@5421
    39
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5421
    40
    if (cursor) {
slouken@5421
    41
        cursor->driverdata = LoadCursor(NULL, IDC_ARROW);
slouken@5421
    42
    } else {
slouken@5421
    43
        SDL_OutOfMemory();
slouken@5421
    44
    }
slouken@5421
    45
slouken@5421
    46
    return cursor;
slouken@5421
    47
}
slouken@5421
    48
slouken@5421
    49
static SDL_Cursor *
slouken@5421
    50
WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5421
    51
{
slouken@5421
    52
    SDL_Cursor *cursor;
slouken@5421
    53
    HICON hicon;
slouken@5421
    54
    HDC hdc;
slouken@5421
    55
    BITMAPV4HEADER bmh;
slouken@5421
    56
    LPVOID pixels;
slouken@5421
    57
    ICONINFO ii;
slouken@5421
    58
slouken@5421
    59
    SDL_zero(bmh);
slouken@5421
    60
    bmh.bV4Size = sizeof(bmh);
slouken@5421
    61
    bmh.bV4Width = surface->w;
slouken@5421
    62
    bmh.bV4Height = -surface->h; /* Invert the image */
slouken@5421
    63
    bmh.bV4Planes = 1;
slouken@5421
    64
    bmh.bV4BitCount = 32;
slouken@5421
    65
    bmh.bV4V4Compression = BI_BITFIELDS;
slouken@5421
    66
    bmh.bV4AlphaMask = 0xFF000000;
slouken@5421
    67
    bmh.bV4RedMask   = 0x00FF0000;
slouken@5421
    68
    bmh.bV4GreenMask = 0x0000FF00;
slouken@5421
    69
    bmh.bV4BlueMask  = 0x000000FF;
slouken@5421
    70
slouken@5421
    71
    hdc = GetDC(NULL);
slouken@5421
    72
    SDL_zero(ii);
slouken@5421
    73
    ii.fIcon = FALSE;
slouken@5421
    74
    ii.xHotspot = (DWORD)hot_x;
slouken@5421
    75
    ii.yHotspot = (DWORD)hot_y;
slouken@5421
    76
    ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
slouken@5421
    77
    ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, NULL);
slouken@5421
    78
    ReleaseDC(NULL, hdc);
slouken@5421
    79
slouken@5472
    80
    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@5472
    81
    SDL_assert(surface->pitch == surface->w * 4);
slouken@5472
    82
    SDL_memcpy(pixels, surface->pixels, surface->h * surface->pitch);
slouken@5421
    83
slouken@5421
    84
    hicon = CreateIconIndirect(&ii);
slouken@5421
    85
slouken@5421
    86
    DeleteObject(ii.hbmColor);
slouken@5421
    87
    DeleteObject(ii.hbmMask);
slouken@5421
    88
slouken@5421
    89
    if (!hicon) {
slouken@5421
    90
        WIN_SetError("CreateIconIndirect()");
slouken@5421
    91
        return NULL;
slouken@5421
    92
    }
slouken@5421
    93
slouken@5421
    94
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5421
    95
    if (cursor) {
slouken@5421
    96
        cursor->driverdata = hicon;
slouken@5421
    97
    } else {
slouken@5421
    98
        DestroyIcon(hicon);
slouken@5421
    99
        SDL_OutOfMemory();
slouken@5421
   100
    }
slouken@5421
   101
slouken@5421
   102
    return cursor;
slouken@5421
   103
}
slouken@5421
   104
mikesart@6675
   105
static SDL_Cursor *
mikesart@6675
   106
WIN_CreateSystemCursor(SDL_SystemCursor id)
mikesart@6675
   107
{
mikesart@6675
   108
    SDL_Cursor *cursor;
mikesart@6675
   109
    LPCTSTR name;
mikesart@6675
   110
mikesart@6675
   111
    switch(id)
mikesart@6675
   112
    {
mikesart@6675
   113
    default:
mikesart@6675
   114
        SDL_assert(0);
mikesart@6675
   115
        return NULL;
mikesart@6675
   116
    case SDL_SYSTEM_CURSOR_ARROW:     name = IDC_ARROW; break;
mikesart@6675
   117
    case SDL_SYSTEM_CURSOR_IBEAM:     name = IDC_IBEAM; break;
mikesart@6675
   118
    case SDL_SYSTEM_CURSOR_WAIT:      name = IDC_WAIT; break;
mikesart@6675
   119
    case SDL_SYSTEM_CURSOR_CROSSHAIR: name = IDC_CROSS; break;
mikesart@6675
   120
    case SDL_SYSTEM_CURSOR_WAITARROW: name = IDC_WAIT; break;
mikesart@6675
   121
    case SDL_SYSTEM_CURSOR_SIZENWSE:  name = IDC_SIZENWSE; break;
mikesart@6675
   122
    case SDL_SYSTEM_CURSOR_SIZENESW:  name = IDC_SIZENESW; break;
mikesart@6675
   123
    case SDL_SYSTEM_CURSOR_SIZEWE:    name = IDC_SIZEWE; break;
mikesart@6675
   124
    case SDL_SYSTEM_CURSOR_SIZENS:    name = IDC_SIZENS; break;
mikesart@6675
   125
    case SDL_SYSTEM_CURSOR_SIZEALL:   name = IDC_SIZEALL; break;
mikesart@6675
   126
    case SDL_SYSTEM_CURSOR_NO:        name = IDC_NO; break;
mikesart@6675
   127
    case SDL_SYSTEM_CURSOR_HAND:      name = IDC_HAND; break;
mikesart@6675
   128
    }
mikesart@6675
   129
mikesart@6675
   130
    cursor = SDL_calloc(1, sizeof(*cursor));
mikesart@6675
   131
    if (cursor) {
mikesart@6675
   132
        HICON hicon;
mikesart@6675
   133
mikesart@6675
   134
        hicon = LoadCursor(NULL, name);
mikesart@6675
   135
mikesart@6675
   136
        cursor->driverdata = hicon;
mikesart@6675
   137
    } else {
mikesart@6675
   138
        SDL_OutOfMemory();
mikesart@6675
   139
    }
mikesart@6675
   140
mikesart@6675
   141
    return cursor;
mikesart@6675
   142
}
mikesart@6675
   143
slouken@5421
   144
static void
slouken@5421
   145
WIN_FreeCursor(SDL_Cursor * cursor)
slouken@5421
   146
{
slouken@5421
   147
    HICON hicon = (HICON)cursor->driverdata;
slouken@5421
   148
slouken@5421
   149
    DestroyIcon(hicon);
slouken@5421
   150
    SDL_free(cursor);
slouken@5421
   151
}
slouken@5421
   152
slouken@5421
   153
static int
slouken@5421
   154
WIN_ShowCursor(SDL_Cursor * cursor)
slouken@5421
   155
{
slouken@5421
   156
    if (cursor) {
slouken@5421
   157
        SDL_cursor = (HCURSOR)cursor->driverdata;
slouken@5421
   158
    } else {
slouken@5421
   159
        SDL_cursor = NULL;
slouken@5421
   160
    }
slouken@5421
   161
    if (SDL_GetMouseFocus() != NULL) {
slouken@5421
   162
        SetCursor(SDL_cursor);
slouken@5421
   163
    }
slouken@5421
   164
    return 0;
slouken@5421
   165
}
slouken@5421
   166
slouken@5421
   167
static void
slouken@5421
   168
WIN_WarpMouse(SDL_Window * window, int x, int y)
slouken@5421
   169
{
slouken@5421
   170
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@5421
   171
    POINT pt;
slouken@5421
   172
slouken@5421
   173
    pt.x = x;
slouken@5421
   174
    pt.y = y;
slouken@5421
   175
    ClientToScreen(hwnd, &pt);
slouken@5421
   176
    SetCursorPos(pt.x, pt.y);
slouken@5421
   177
}
slouken@5421
   178
slouken@5421
   179
static int
slouken@5421
   180
WIN_SetRelativeMouseMode(SDL_bool enabled)
slouken@5421
   181
{
slouken@6350
   182
    RAWINPUTDEVICE rawMouse = { 0x01, 0x02, 0, NULL }; /* Mouse: UsagePage = 1, Usage = 2 */
slouken@6350
   183
	HWND hWnd;
slouken@6350
   184
	hWnd = GetActiveWindow();
slouken@6350
   185
slouken@6350
   186
	rawMouse.hwndTarget = hWnd;
slouken@6350
   187
	if(!enabled) {
slouken@6350
   188
		rawMouse.dwFlags |= RIDEV_REMOVE;
slouken@6350
   189
		rawMouse.hwndTarget = NULL;
slouken@6350
   190
	}
slouken@6350
   191
slouken@6350
   192
slouken@6350
   193
	/* (Un)register raw input for mice */
slouken@6350
   194
	if(RegisterRawInputDevices(&rawMouse, 1, sizeof(RAWINPUTDEVICE)) == FALSE) {
slouken@6350
   195
slouken@6350
   196
		/* Only return an error when registering. If we unregister and fail, then
slouken@6350
   197
		it's probably that we unregistered twice. That's OK. */
slouken@6350
   198
		if(enabled) {
icculus@7037
   199
			return SDL_Unsupported();
slouken@6350
   200
		}
slouken@6350
   201
	}
slouken@6350
   202
slouken@6350
   203
	if(enabled) {
slouken@6350
   204
		LONG cx, cy;
slouken@6350
   205
		RECT rect;
slouken@6350
   206
		GetWindowRect(hWnd, &rect);
slouken@6350
   207
slouken@6350
   208
		cx = (rect.left + rect.right) / 2;
slouken@6350
   209
		cy = (rect.top + rect.bottom) / 2;
slouken@6350
   210
slouken@6350
   211
		/* Make an absurdly small clip rect */
slouken@6350
   212
		rect.left = cx-1;
slouken@6350
   213
		rect.right = cx+1;
slouken@6350
   214
		rect.top = cy-1;
slouken@6350
   215
		rect.bottom = cy+1;
slouken@6350
   216
slouken@6350
   217
		ClipCursor(&rect);
slouken@6350
   218
	}
slouken@6350
   219
	else
slouken@6350
   220
		ClipCursor(NULL);
slouken@6350
   221
slouken@6350
   222
    return 0;
slouken@5421
   223
}
slouken@5421
   224
slouken@1895
   225
void
slouken@1895
   226
WIN_InitMouse(_THIS)
slouken@1895
   227
{
slouken@5421
   228
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5421
   229
slouken@5421
   230
    mouse->CreateCursor = WIN_CreateCursor;
mikesart@6675
   231
	mouse->CreateSystemCursor = WIN_CreateSystemCursor;
slouken@5421
   232
    mouse->ShowCursor = WIN_ShowCursor;
slouken@5421
   233
    mouse->FreeCursor = WIN_FreeCursor;
slouken@5421
   234
    mouse->WarpMouse = WIN_WarpMouse;
slouken@5421
   235
    mouse->SetRelativeMouseMode = WIN_SetRelativeMouseMode;
slouken@5421
   236
slouken@5421
   237
    SDL_SetDefaultCursor(WIN_CreateDefaultCursor());
slouken@1895
   238
}
slouken@1895
   239
slouken@1895
   240
void
slouken@1895
   241
WIN_QuitMouse(_THIS)
slouken@1895
   242
{
gabomdq@7103
   243
    SDL_Mouse *mouse = SDL_GetMouse();
gabomdq@7103
   244
    if ( mouse->def_cursor ) {
gabomdq@7103
   245
        SDL_free(mouse->def_cursor);
gabomdq@7103
   246
        mouse->def_cursor = NULL;
gabomdq@7103
   247
        mouse->cur_cursor = NULL;
gabomdq@7103
   248
    }
slouken@1895
   249
}
slouken@1895
   250
slouken@6044
   251
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   252
slouken@1895
   253
/* vi: set ts=4 sw=4 expandtab: */