src/video/windows/SDL_windowsmouse.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 04 Jun 2014 10:55:26 -0700
changeset 8815 c6d0a457f3b2
parent 8269 6e18328e25e0
child 8916 96d944f74851
permissions -rw-r--r--
Added an API function to warp the mouse cursor in global screen space: SDL_WarpMouseGlobal()
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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
*/
icculus@8093
    21
#include "../../SDL_internal.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
{
icculus@7489
    52
    /* msdn says cursor mask has to be padded out to word alignment. Not sure
icculus@7489
    53
        if that means machine word or WORD, but this handles either case. */
icculus@7489
    54
    const size_t pad = (sizeof (size_t) * 8);  /* 32 or 64, or whatever. */
slouken@5421
    55
    SDL_Cursor *cursor;
slouken@5421
    56
    HICON hicon;
slouken@5421
    57
    HDC hdc;
slouken@5421
    58
    BITMAPV4HEADER bmh;
slouken@5421
    59
    LPVOID pixels;
icculus@7489
    60
    LPVOID maskbits;
icculus@7489
    61
    size_t maskbitslen;
slouken@5421
    62
    ICONINFO ii;
slouken@5421
    63
slouken@5421
    64
    SDL_zero(bmh);
slouken@5421
    65
    bmh.bV4Size = sizeof(bmh);
slouken@5421
    66
    bmh.bV4Width = surface->w;
slouken@5421
    67
    bmh.bV4Height = -surface->h; /* Invert the image */
slouken@5421
    68
    bmh.bV4Planes = 1;
slouken@5421
    69
    bmh.bV4BitCount = 32;
slouken@5421
    70
    bmh.bV4V4Compression = BI_BITFIELDS;
slouken@5421
    71
    bmh.bV4AlphaMask = 0xFF000000;
slouken@5421
    72
    bmh.bV4RedMask   = 0x00FF0000;
slouken@5421
    73
    bmh.bV4GreenMask = 0x0000FF00;
slouken@5421
    74
    bmh.bV4BlueMask  = 0x000000FF;
slouken@5421
    75
icculus@7489
    76
    maskbitslen = ((surface->w + (pad - (surface->w % pad))) / 8) * surface->h;
icculus@7489
    77
    maskbits = SDL_stack_alloc(Uint8,maskbitslen);
icculus@7489
    78
    if (maskbits == NULL) {
icculus@7489
    79
        SDL_OutOfMemory();
icculus@7489
    80
        return NULL;
icculus@7489
    81
    }
icculus@7489
    82
icculus@7489
    83
    /* AND the cursor against full bits: no change. We already have alpha. */
icculus@7489
    84
    SDL_memset(maskbits, 0xFF, maskbitslen);
icculus@7489
    85
slouken@5421
    86
    hdc = GetDC(NULL);
slouken@5421
    87
    SDL_zero(ii);
slouken@5421
    88
    ii.fIcon = FALSE;
slouken@5421
    89
    ii.xHotspot = (DWORD)hot_x;
slouken@5421
    90
    ii.yHotspot = (DWORD)hot_y;
slouken@5421
    91
    ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
icculus@7489
    92
    ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, maskbits);
slouken@5421
    93
    ReleaseDC(NULL, hdc);
icculus@7489
    94
    SDL_stack_free(maskbits);
slouken@5421
    95
slouken@5472
    96
    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@5472
    97
    SDL_assert(surface->pitch == surface->w * 4);
slouken@5472
    98
    SDL_memcpy(pixels, surface->pixels, surface->h * surface->pitch);
slouken@5421
    99
slouken@5421
   100
    hicon = CreateIconIndirect(&ii);
slouken@5421
   101
slouken@5421
   102
    DeleteObject(ii.hbmColor);
slouken@5421
   103
    DeleteObject(ii.hbmMask);
slouken@5421
   104
slouken@5421
   105
    if (!hicon) {
slouken@5421
   106
        WIN_SetError("CreateIconIndirect()");
slouken@5421
   107
        return NULL;
slouken@5421
   108
    }
slouken@5421
   109
slouken@5421
   110
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5421
   111
    if (cursor) {
slouken@5421
   112
        cursor->driverdata = hicon;
slouken@5421
   113
    } else {
slouken@5421
   114
        DestroyIcon(hicon);
slouken@5421
   115
        SDL_OutOfMemory();
slouken@5421
   116
    }
slouken@5421
   117
slouken@5421
   118
    return cursor;
slouken@5421
   119
}
slouken@5421
   120
mikesart@6675
   121
static SDL_Cursor *
mikesart@6675
   122
WIN_CreateSystemCursor(SDL_SystemCursor id)
mikesart@6675
   123
{
mikesart@6675
   124
    SDL_Cursor *cursor;
mikesart@6675
   125
    LPCTSTR name;
mikesart@6675
   126
mikesart@6675
   127
    switch(id)
mikesart@6675
   128
    {
mikesart@6675
   129
    default:
mikesart@6675
   130
        SDL_assert(0);
mikesart@6675
   131
        return NULL;
mikesart@6675
   132
    case SDL_SYSTEM_CURSOR_ARROW:     name = IDC_ARROW; break;
mikesart@6675
   133
    case SDL_SYSTEM_CURSOR_IBEAM:     name = IDC_IBEAM; break;
mikesart@6675
   134
    case SDL_SYSTEM_CURSOR_WAIT:      name = IDC_WAIT; break;
mikesart@6675
   135
    case SDL_SYSTEM_CURSOR_CROSSHAIR: name = IDC_CROSS; break;
mikesart@6675
   136
    case SDL_SYSTEM_CURSOR_WAITARROW: name = IDC_WAIT; break;
mikesart@6675
   137
    case SDL_SYSTEM_CURSOR_SIZENWSE:  name = IDC_SIZENWSE; break;
mikesart@6675
   138
    case SDL_SYSTEM_CURSOR_SIZENESW:  name = IDC_SIZENESW; break;
mikesart@6675
   139
    case SDL_SYSTEM_CURSOR_SIZEWE:    name = IDC_SIZEWE; break;
mikesart@6675
   140
    case SDL_SYSTEM_CURSOR_SIZENS:    name = IDC_SIZENS; break;
mikesart@6675
   141
    case SDL_SYSTEM_CURSOR_SIZEALL:   name = IDC_SIZEALL; break;
mikesart@6675
   142
    case SDL_SYSTEM_CURSOR_NO:        name = IDC_NO; break;
mikesart@6675
   143
    case SDL_SYSTEM_CURSOR_HAND:      name = IDC_HAND; break;
mikesart@6675
   144
    }
mikesart@6675
   145
mikesart@6675
   146
    cursor = SDL_calloc(1, sizeof(*cursor));
mikesart@6675
   147
    if (cursor) {
mikesart@6675
   148
        HICON hicon;
mikesart@6675
   149
mikesart@6675
   150
        hicon = LoadCursor(NULL, name);
mikesart@6675
   151
mikesart@6675
   152
        cursor->driverdata = hicon;
mikesart@6675
   153
    } else {
mikesart@6675
   154
        SDL_OutOfMemory();
mikesart@6675
   155
    }
mikesart@6675
   156
mikesart@6675
   157
    return cursor;
mikesart@6675
   158
}
mikesart@6675
   159
slouken@5421
   160
static void
slouken@5421
   161
WIN_FreeCursor(SDL_Cursor * cursor)
slouken@5421
   162
{
slouken@5421
   163
    HICON hicon = (HICON)cursor->driverdata;
slouken@5421
   164
slouken@5421
   165
    DestroyIcon(hicon);
slouken@5421
   166
    SDL_free(cursor);
slouken@5421
   167
}
slouken@5421
   168
slouken@5421
   169
static int
slouken@5421
   170
WIN_ShowCursor(SDL_Cursor * cursor)
slouken@5421
   171
{
slouken@5421
   172
    if (cursor) {
slouken@5421
   173
        SDL_cursor = (HCURSOR)cursor->driverdata;
slouken@5421
   174
    } else {
slouken@5421
   175
        SDL_cursor = NULL;
slouken@5421
   176
    }
slouken@5421
   177
    if (SDL_GetMouseFocus() != NULL) {
slouken@5421
   178
        SetCursor(SDL_cursor);
slouken@5421
   179
    }
slouken@5421
   180
    return 0;
slouken@5421
   181
}
slouken@5421
   182
slouken@5421
   183
static void
slouken@5421
   184
WIN_WarpMouse(SDL_Window * window, int x, int y)
slouken@5421
   185
{
slouken@8254
   186
    SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
slouken@8254
   187
    HWND hwnd = data->hwnd;
slouken@5421
   188
    POINT pt;
slouken@5421
   189
slouken@8254
   190
    /* Don't warp the mouse while we're doing a modal interaction */
slouken@8254
   191
    if (data->in_title_click || data->in_modal_loop) {
slouken@8254
   192
        return;
slouken@8254
   193
    }
slouken@8254
   194
slouken@5421
   195
    pt.x = x;
slouken@5421
   196
    pt.y = y;
slouken@5421
   197
    ClientToScreen(hwnd, &pt);
slouken@5421
   198
    SetCursorPos(pt.x, pt.y);
slouken@5421
   199
}
slouken@5421
   200
slouken@8815
   201
static void
slouken@8815
   202
WIN_WarpMouseGlobal(int x, int y)
slouken@8815
   203
{
slouken@8815
   204
    POINT pt;
slouken@8815
   205
slouken@8815
   206
    pt.x = x;
slouken@8815
   207
    pt.y = y;
slouken@8815
   208
    SetCursorPos(pt.x, pt.y);
slouken@8815
   209
}
slouken@8815
   210
slouken@5421
   211
static int
slouken@5421
   212
WIN_SetRelativeMouseMode(SDL_bool enabled)
slouken@5421
   213
{
slouken@6350
   214
    RAWINPUTDEVICE rawMouse = { 0x01, 0x02, 0, NULL }; /* Mouse: UsagePage = 1, Usage = 2 */
slouken@6350
   215
slouken@8252
   216
    if (!enabled) {
slouken@7191
   217
        rawMouse.dwFlags |= RIDEV_REMOVE;
slouken@7191
   218
    }
slouken@6350
   219
slouken@7191
   220
    /* (Un)register raw input for mice */
slouken@8036
   221
    if (RegisterRawInputDevices(&rawMouse, 1, sizeof(RAWINPUTDEVICE)) == FALSE) {
slouken@6350
   222
slouken@8269
   223
        /* Only return an error when registering. If we unregister and fail,
slouken@8269
   224
           then it's probably that we unregistered twice. That's OK. */
slouken@8252
   225
        if (enabled) {
slouken@7191
   226
            return SDL_Unsupported();
slouken@7191
   227
        }
slouken@7191
   228
    }
slouken@6350
   229
    return 0;
slouken@5421
   230
}
slouken@5421
   231
slouken@1895
   232
void
slouken@1895
   233
WIN_InitMouse(_THIS)
slouken@1895
   234
{
slouken@5421
   235
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5421
   236
slouken@5421
   237
    mouse->CreateCursor = WIN_CreateCursor;
slouken@7191
   238
    mouse->CreateSystemCursor = WIN_CreateSystemCursor;
slouken@5421
   239
    mouse->ShowCursor = WIN_ShowCursor;
slouken@5421
   240
    mouse->FreeCursor = WIN_FreeCursor;
slouken@5421
   241
    mouse->WarpMouse = WIN_WarpMouse;
slouken@8815
   242
    mouse->WarpMouseGlobal = WIN_WarpMouseGlobal;
slouken@5421
   243
    mouse->SetRelativeMouseMode = WIN_SetRelativeMouseMode;
slouken@5421
   244
slouken@5421
   245
    SDL_SetDefaultCursor(WIN_CreateDefaultCursor());
slouken@8066
   246
slouken@8066
   247
    SDL_SetDoubleClickTime(GetDoubleClickTime());
slouken@1895
   248
}
slouken@1895
   249
slouken@1895
   250
void
slouken@1895
   251
WIN_QuitMouse(_THIS)
slouken@1895
   252
{
gabomdq@7103
   253
    SDL_Mouse *mouse = SDL_GetMouse();
gabomdq@7103
   254
    if ( mouse->def_cursor ) {
gabomdq@7103
   255
        SDL_free(mouse->def_cursor);
gabomdq@7103
   256
        mouse->def_cursor = NULL;
gabomdq@7103
   257
        mouse->cur_cursor = NULL;
gabomdq@7103
   258
    }
slouken@1895
   259
}
slouken@1895
   260
slouken@6044
   261
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   262
slouken@1895
   263
/* vi: set ts=4 sw=4 expandtab: */