src/video/windows/SDL_windowsmouse.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5472 674d45edfaeb
child 6044 35448a5ea044
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@1895
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@5535
     3
  Copyright (C) 1997-2011 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@2710
    21
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@5472
    24
#include "SDL_assert.h"
slouken@5062
    25
#include "SDL_windowsvideo.h"
slouken@1895
    26
slouken@5421
    27
#include "../../events/SDL_mouse_c.h"
slouken@5421
    28
slouken@5421
    29
slouken@5421
    30
HCURSOR SDL_cursor = NULL;
slouken@5421
    31
slouken@5421
    32
slouken@5421
    33
static SDL_Cursor *
slouken@5421
    34
WIN_CreateDefaultCursor()
slouken@5421
    35
{
slouken@5421
    36
    SDL_Cursor *cursor;
slouken@5421
    37
slouken@5421
    38
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5421
    39
    if (cursor) {
slouken@5421
    40
        cursor->driverdata = LoadCursor(NULL, IDC_ARROW);
slouken@5421
    41
    } else {
slouken@5421
    42
        SDL_OutOfMemory();
slouken@5421
    43
    }
slouken@5421
    44
slouken@5421
    45
    return cursor;
slouken@5421
    46
}
slouken@5421
    47
slouken@5421
    48
static SDL_Cursor *
slouken@5421
    49
WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5421
    50
{
slouken@5421
    51
    SDL_Cursor *cursor;
slouken@5421
    52
    HICON hicon;
slouken@5421
    53
    HDC hdc;
slouken@5421
    54
    BITMAPV4HEADER bmh;
slouken@5421
    55
    LPVOID pixels;
slouken@5421
    56
    ICONINFO ii;
slouken@5421
    57
slouken@5421
    58
    SDL_zero(bmh);
slouken@5421
    59
    bmh.bV4Size = sizeof(bmh);
slouken@5421
    60
    bmh.bV4Width = surface->w;
slouken@5421
    61
    bmh.bV4Height = -surface->h; /* Invert the image */
slouken@5421
    62
    bmh.bV4Planes = 1;
slouken@5421
    63
    bmh.bV4BitCount = 32;
slouken@5421
    64
    bmh.bV4V4Compression = BI_BITFIELDS;
slouken@5421
    65
    bmh.bV4AlphaMask = 0xFF000000;
slouken@5421
    66
    bmh.bV4RedMask   = 0x00FF0000;
slouken@5421
    67
    bmh.bV4GreenMask = 0x0000FF00;
slouken@5421
    68
    bmh.bV4BlueMask  = 0x000000FF;
slouken@5421
    69
slouken@5421
    70
    hdc = GetDC(NULL);
slouken@5421
    71
    SDL_zero(ii);
slouken@5421
    72
    ii.fIcon = FALSE;
slouken@5421
    73
    ii.xHotspot = (DWORD)hot_x;
slouken@5421
    74
    ii.yHotspot = (DWORD)hot_y;
slouken@5421
    75
    ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
slouken@5421
    76
    ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, NULL);
slouken@5421
    77
    ReleaseDC(NULL, hdc);
slouken@5421
    78
slouken@5472
    79
    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
slouken@5472
    80
    SDL_assert(surface->pitch == surface->w * 4);
slouken@5472
    81
    SDL_memcpy(pixels, surface->pixels, surface->h * surface->pitch);
slouken@5421
    82
slouken@5421
    83
    hicon = CreateIconIndirect(&ii);
slouken@5421
    84
slouken@5421
    85
    DeleteObject(ii.hbmColor);
slouken@5421
    86
    DeleteObject(ii.hbmMask);
slouken@5421
    87
slouken@5421
    88
    if (!hicon) {
slouken@5421
    89
        WIN_SetError("CreateIconIndirect()");
slouken@5421
    90
        return NULL;
slouken@5421
    91
    }
slouken@5421
    92
slouken@5421
    93
    cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5421
    94
    if (cursor) {
slouken@5421
    95
        cursor->driverdata = hicon;
slouken@5421
    96
    } else {
slouken@5421
    97
        DestroyIcon(hicon);
slouken@5421
    98
        SDL_OutOfMemory();
slouken@5421
    99
    }
slouken@5421
   100
slouken@5421
   101
    return cursor;
slouken@5421
   102
}
slouken@5421
   103
slouken@5421
   104
static void
slouken@5421
   105
WIN_FreeCursor(SDL_Cursor * cursor)
slouken@5421
   106
{
slouken@5421
   107
    HICON hicon = (HICON)cursor->driverdata;
slouken@5421
   108
slouken@5421
   109
    DestroyIcon(hicon);
slouken@5421
   110
    SDL_free(cursor);
slouken@5421
   111
}
slouken@5421
   112
slouken@5421
   113
static int
slouken@5421
   114
WIN_ShowCursor(SDL_Cursor * cursor)
slouken@5421
   115
{
slouken@5421
   116
    if (cursor) {
slouken@5421
   117
        SDL_cursor = (HCURSOR)cursor->driverdata;
slouken@5421
   118
    } else {
slouken@5421
   119
        SDL_cursor = NULL;
slouken@5421
   120
    }
slouken@5421
   121
    if (SDL_GetMouseFocus() != NULL) {
slouken@5421
   122
        SetCursor(SDL_cursor);
slouken@5421
   123
    }
slouken@5421
   124
    return 0;
slouken@5421
   125
}
slouken@5421
   126
slouken@5421
   127
static void
slouken@5421
   128
WIN_WarpMouse(SDL_Window * window, int x, int y)
slouken@5421
   129
{
slouken@5421
   130
    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
slouken@5421
   131
    POINT pt;
slouken@5421
   132
slouken@5421
   133
    pt.x = x;
slouken@5421
   134
    pt.y = y;
slouken@5421
   135
    ClientToScreen(hwnd, &pt);
slouken@5421
   136
    SetCursorPos(pt.x, pt.y);
slouken@5421
   137
}
slouken@5421
   138
slouken@5421
   139
static int
slouken@5421
   140
WIN_SetRelativeMouseMode(SDL_bool enabled)
slouken@5421
   141
{
slouken@5421
   142
    SDL_Unsupported();
slouken@5421
   143
    return -1;
slouken@5421
   144
}
slouken@5421
   145
slouken@1895
   146
void
slouken@1895
   147
WIN_InitMouse(_THIS)
slouken@1895
   148
{
slouken@5421
   149
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5421
   150
slouken@5421
   151
    mouse->CreateCursor = WIN_CreateCursor;
slouken@5421
   152
    mouse->ShowCursor = WIN_ShowCursor;
slouken@5421
   153
    mouse->FreeCursor = WIN_FreeCursor;
slouken@5421
   154
    mouse->WarpMouse = WIN_WarpMouse;
slouken@5421
   155
    mouse->SetRelativeMouseMode = WIN_SetRelativeMouseMode;
slouken@5421
   156
slouken@5421
   157
    SDL_SetDefaultCursor(WIN_CreateDefaultCursor());
slouken@1895
   158
}
slouken@1895
   159
slouken@1895
   160
void
slouken@1895
   161
WIN_QuitMouse(_THIS)
slouken@1895
   162
{
slouken@1895
   163
}
slouken@1895
   164
slouken@1895
   165
/* vi: set ts=4 sw=4 expandtab: */