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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
     2
  Simple DirectMedia Layer
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6675
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
     7
  arising from the use of this software.
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
    11
  freely, subject to the following restrictions:
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5472
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    21
#include "SDL_config.h"
2710
44e49d3fa6cf Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 1895
diff changeset
    22
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    23
#if SDL_VIDEO_DRIVER_WINDOWS
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
5472
674d45edfaeb We can assume ARGB surface format when creating cursors (enforced by higher level API)
Sam Lantinga <slouken@libsdl.org>
parents: 5421
diff changeset
    25
#include "SDL_assert.h"
5062
e8916fe9cfc8 Fixed bug #925
Sam Lantinga <slouken@libsdl.org>
parents: 4465
diff changeset
    26
#include "SDL_windowsvideo.h"
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
5421
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    28
#include "../../events/SDL_mouse_c.h"
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    29
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    30
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    31
HCURSOR SDL_cursor = NULL;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    32
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    33
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    34
static SDL_Cursor *
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    35
WIN_CreateDefaultCursor()
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    36
{
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    37
    SDL_Cursor *cursor;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    38
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    39
    cursor = SDL_calloc(1, sizeof(*cursor));
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    40
    if (cursor) {
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    41
        cursor->driverdata = LoadCursor(NULL, IDC_ARROW);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    42
    } else {
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    43
        SDL_OutOfMemory();
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    44
    }
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    45
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    46
    return cursor;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    47
}
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    48
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    49
static SDL_Cursor *
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    50
WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    51
{
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    52
    SDL_Cursor *cursor;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    53
    HICON hicon;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    54
    HDC hdc;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    55
    BITMAPV4HEADER bmh;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    56
    LPVOID pixels;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    57
    ICONINFO ii;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    58
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    59
    SDL_zero(bmh);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    60
    bmh.bV4Size = sizeof(bmh);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    61
    bmh.bV4Width = surface->w;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    62
    bmh.bV4Height = -surface->h; /* Invert the image */
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    63
    bmh.bV4Planes = 1;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    64
    bmh.bV4BitCount = 32;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    65
    bmh.bV4V4Compression = BI_BITFIELDS;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    66
    bmh.bV4AlphaMask = 0xFF000000;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    67
    bmh.bV4RedMask   = 0x00FF0000;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    68
    bmh.bV4GreenMask = 0x0000FF00;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    69
    bmh.bV4BlueMask  = 0x000000FF;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    70
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    71
    hdc = GetDC(NULL);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    72
    SDL_zero(ii);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    73
    ii.fIcon = FALSE;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    74
    ii.xHotspot = (DWORD)hot_x;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    75
    ii.yHotspot = (DWORD)hot_y;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    76
    ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    77
    ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, NULL);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    78
    ReleaseDC(NULL, hdc);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    79
5472
674d45edfaeb We can assume ARGB surface format when creating cursors (enforced by higher level API)
Sam Lantinga <slouken@libsdl.org>
parents: 5421
diff changeset
    80
    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
674d45edfaeb We can assume ARGB surface format when creating cursors (enforced by higher level API)
Sam Lantinga <slouken@libsdl.org>
parents: 5421
diff changeset
    81
    SDL_assert(surface->pitch == surface->w * 4);
674d45edfaeb We can assume ARGB surface format when creating cursors (enforced by higher level API)
Sam Lantinga <slouken@libsdl.org>
parents: 5421
diff changeset
    82
    SDL_memcpy(pixels, surface->pixels, surface->h * surface->pitch);
5421
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    83
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    84
    hicon = CreateIconIndirect(&ii);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    85
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    86
    DeleteObject(ii.hbmColor);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    87
    DeleteObject(ii.hbmMask);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    88
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    89
    if (!hicon) {
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    90
        WIN_SetError("CreateIconIndirect()");
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    91
        return NULL;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    92
    }
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    93
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    94
    cursor = SDL_calloc(1, sizeof(*cursor));
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    95
    if (cursor) {
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    96
        cursor->driverdata = hicon;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    97
    } else {
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    98
        DestroyIcon(hicon);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
    99
        SDL_OutOfMemory();
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   100
    }
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   101
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   102
    return cursor;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   103
}
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   104
6675
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   105
static SDL_Cursor *
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   106
WIN_CreateSystemCursor(SDL_SystemCursor id)
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   107
{
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   108
    SDL_Cursor *cursor;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   109
    LPCTSTR name;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   110
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   111
    switch(id)
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   112
    {
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   113
    default:
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   114
        SDL_assert(0);
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   115
        return NULL;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   116
    case SDL_SYSTEM_CURSOR_ARROW:     name = IDC_ARROW; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   117
    case SDL_SYSTEM_CURSOR_IBEAM:     name = IDC_IBEAM; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   118
    case SDL_SYSTEM_CURSOR_WAIT:      name = IDC_WAIT; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   119
    case SDL_SYSTEM_CURSOR_CROSSHAIR: name = IDC_CROSS; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   120
    case SDL_SYSTEM_CURSOR_WAITARROW: name = IDC_WAIT; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   121
    case SDL_SYSTEM_CURSOR_SIZENWSE:  name = IDC_SIZENWSE; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   122
    case SDL_SYSTEM_CURSOR_SIZENESW:  name = IDC_SIZENESW; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   123
    case SDL_SYSTEM_CURSOR_SIZEWE:    name = IDC_SIZEWE; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   124
    case SDL_SYSTEM_CURSOR_SIZENS:    name = IDC_SIZENS; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   125
    case SDL_SYSTEM_CURSOR_SIZEALL:   name = IDC_SIZEALL; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   126
    case SDL_SYSTEM_CURSOR_NO:        name = IDC_NO; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   127
    case SDL_SYSTEM_CURSOR_HAND:      name = IDC_HAND; break;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   128
    }
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   129
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   130
    cursor = SDL_calloc(1, sizeof(*cursor));
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   131
    if (cursor) {
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   132
        HICON hicon;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   133
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   134
        hicon = LoadCursor(NULL, name);
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   135
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   136
        cursor->driverdata = hicon;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   137
    } else {
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   138
        SDL_OutOfMemory();
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   139
    }
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   140
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   141
    return cursor;
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   142
}
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   143
5421
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   144
static void
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   145
WIN_FreeCursor(SDL_Cursor * cursor)
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   146
{
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   147
    HICON hicon = (HICON)cursor->driverdata;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   148
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   149
    DestroyIcon(hicon);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   150
    SDL_free(cursor);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   151
}
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   152
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   153
static int
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   154
WIN_ShowCursor(SDL_Cursor * cursor)
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   155
{
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   156
    if (cursor) {
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   157
        SDL_cursor = (HCURSOR)cursor->driverdata;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   158
    } else {
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   159
        SDL_cursor = NULL;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   160
    }
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   161
    if (SDL_GetMouseFocus() != NULL) {
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   162
        SetCursor(SDL_cursor);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   163
    }
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   164
    return 0;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   165
}
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   166
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   167
static void
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   168
WIN_WarpMouse(SDL_Window * window, int x, int y)
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   169
{
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   170
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   171
    POINT pt;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   172
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   173
    pt.x = x;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   174
    pt.y = y;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   175
    ClientToScreen(hwnd, &pt);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   176
    SetCursorPos(pt.x, pt.y);
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   177
}
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   178
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   179
static int
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   180
WIN_SetRelativeMouseMode(SDL_bool enabled)
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   181
{
6350
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   182
    RAWINPUTDEVICE rawMouse = { 0x01, 0x02, 0, NULL }; /* Mouse: UsagePage = 1, Usage = 2 */
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   183
	HWND hWnd;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   184
	hWnd = GetActiveWindow();
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   185
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   186
	rawMouse.hwndTarget = hWnd;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   187
	if(!enabled) {
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   188
		rawMouse.dwFlags |= RIDEV_REMOVE;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   189
		rawMouse.hwndTarget = NULL;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   190
	}
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   191
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   192
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   193
	/* (Un)register raw input for mice */
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   194
	if(RegisterRawInputDevices(&rawMouse, 1, sizeof(RAWINPUTDEVICE)) == FALSE) {
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   195
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   196
		/* Only return an error when registering. If we unregister and fail, then
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   197
		it's probably that we unregistered twice. That's OK. */
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   198
		if(enabled) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6885
diff changeset
   199
			return SDL_Unsupported();
6350
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   200
		}
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   201
	}
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   202
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   203
	if(enabled) {
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   204
		LONG cx, cy;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   205
		RECT rect;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   206
		GetWindowRect(hWnd, &rect);
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   207
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   208
		cx = (rect.left + rect.right) / 2;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   209
		cy = (rect.top + rect.bottom) / 2;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   210
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   211
		/* Make an absurdly small clip rect */
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   212
		rect.left = cx-1;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   213
		rect.right = cx+1;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   214
		rect.top = cy-1;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   215
		rect.bottom = cy+1;
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   216
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   217
		ClipCursor(&rect);
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   218
	}
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   219
	else
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   220
		ClipCursor(NULL);
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   221
19545983ac76 Patrick Baggett implemented relative mouse mode on Win32
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   222
    return 0;
5421
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   223
}
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   224
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   225
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   226
WIN_InitMouse(_THIS)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   227
{
5421
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   228
    SDL_Mouse *mouse = SDL_GetMouse();
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   229
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   230
    mouse->CreateCursor = WIN_CreateCursor;
6675
20f3cdea0fd2 Add SDL_CreateSystemCursor for Windows and Linux.
Michael Sartain <mikesart@valvesoftware.com>
parents: 6350
diff changeset
   231
	mouse->CreateSystemCursor = WIN_CreateSystemCursor;
5421
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   232
    mouse->ShowCursor = WIN_ShowCursor;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   233
    mouse->FreeCursor = WIN_FreeCursor;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   234
    mouse->WarpMouse = WIN_WarpMouse;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   235
    mouse->SetRelativeMouseMode = WIN_SetRelativeMouseMode;
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   236
f8588afb6486 Added support for Windows cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5336
diff changeset
   237
    SDL_SetDefaultCursor(WIN_CreateDefaultCursor());
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   240
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   241
WIN_QuitMouse(_THIS)
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   242
{
7103
9d146930d4f2 Bug 1787 - memory leak in WIN_InitMouse() by Marcel Bakker
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7037
diff changeset
   243
    SDL_Mouse *mouse = SDL_GetMouse();
9d146930d4f2 Bug 1787 - memory leak in WIN_InitMouse() by Marcel Bakker
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7037
diff changeset
   244
    if ( mouse->def_cursor ) {
9d146930d4f2 Bug 1787 - memory leak in WIN_InitMouse() by Marcel Bakker
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7037
diff changeset
   245
        SDL_free(mouse->def_cursor);
9d146930d4f2 Bug 1787 - memory leak in WIN_InitMouse() by Marcel Bakker
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7037
diff changeset
   246
        mouse->def_cursor = NULL;
9d146930d4f2 Bug 1787 - memory leak in WIN_InitMouse() by Marcel Bakker
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7037
diff changeset
   247
        mouse->cur_cursor = NULL;
9d146930d4f2 Bug 1787 - memory leak in WIN_InitMouse() by Marcel Bakker
Gabriel Jacobo <gabomdq@gmail.com>
parents: 7037
diff changeset
   248
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   249
}
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   250
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   251
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   252
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   253
/* vi: set ts=4 sw=4 expandtab: */