PageRenderTime 49ms CodeModel.GetById 15ms app.highlight 27ms RepoModel.GetById 1ms app.codeStats 0ms

/project/jni/sdl-1.3/src/video/windows/SDL_windowsmouse.c

https://github.com/aichunyu/FFPlayer
C | 168 lines | 117 code | 30 blank | 21 comment | 11 complexity | 0af8e01eb59c8b108e32a288e04895eb MD5 | raw file
  1/*
  2  Simple DirectMedia Layer
  3  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
  4
  5  This software is provided 'as-is', without any express or implied
  6  warranty.  In no event will the authors be held liable for any damages
  7  arising from the use of this software.
  8
  9  Permission is granted to anyone to use this software for any purpose,
 10  including commercial applications, and to alter it and redistribute it
 11  freely, subject to the following restrictions:
 12
 13  1. The origin of this software must not be misrepresented; you must not
 14     claim that you wrote the original software. If you use this software
 15     in a product, an acknowledgment in the product documentation would be
 16     appreciated but is not required.
 17  2. Altered source versions must be plainly marked as such, and must not be
 18     misrepresented as being the original software.
 19  3. This notice may not be removed or altered from any source distribution.
 20*/
 21#include "SDL_config.h"
 22
 23#if SDL_VIDEO_DRIVER_WINDOWS
 24
 25#include "SDL_assert.h"
 26#include "SDL_windowsvideo.h"
 27
 28#include "../../events/SDL_mouse_c.h"
 29
 30
 31HCURSOR SDL_cursor = NULL;
 32
 33
 34static SDL_Cursor *
 35WIN_CreateDefaultCursor()
 36{
 37    SDL_Cursor *cursor;
 38
 39    cursor = SDL_calloc(1, sizeof(*cursor));
 40    if (cursor) {
 41        cursor->driverdata = LoadCursor(NULL, IDC_ARROW);
 42    } else {
 43        SDL_OutOfMemory();
 44    }
 45
 46    return cursor;
 47}
 48
 49static SDL_Cursor *
 50WIN_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
 51{
 52    SDL_Cursor *cursor;
 53    HICON hicon;
 54    HDC hdc;
 55    BITMAPV4HEADER bmh;
 56    LPVOID pixels;
 57    ICONINFO ii;
 58
 59    SDL_zero(bmh);
 60    bmh.bV4Size = sizeof(bmh);
 61    bmh.bV4Width = surface->w;
 62    bmh.bV4Height = -surface->h; /* Invert the image */
 63    bmh.bV4Planes = 1;
 64    bmh.bV4BitCount = 32;
 65    bmh.bV4V4Compression = BI_BITFIELDS;
 66    bmh.bV4AlphaMask = 0xFF000000;
 67    bmh.bV4RedMask   = 0x00FF0000;
 68    bmh.bV4GreenMask = 0x0000FF00;
 69    bmh.bV4BlueMask  = 0x000000FF;
 70
 71    hdc = GetDC(NULL);
 72    SDL_zero(ii);
 73    ii.fIcon = FALSE;
 74    ii.xHotspot = (DWORD)hot_x;
 75    ii.yHotspot = (DWORD)hot_y;
 76    ii.hbmColor = CreateDIBSection(hdc, (BITMAPINFO*)&bmh, DIB_RGB_COLORS, &pixels, NULL, 0);
 77    ii.hbmMask = CreateBitmap(surface->w, surface->h, 1, 1, NULL);
 78    ReleaseDC(NULL, hdc);
 79
 80    SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
 81    SDL_assert(surface->pitch == surface->w * 4);
 82    SDL_memcpy(pixels, surface->pixels, surface->h * surface->pitch);
 83
 84    hicon = CreateIconIndirect(&ii);
 85
 86    DeleteObject(ii.hbmColor);
 87    DeleteObject(ii.hbmMask);
 88
 89    if (!hicon) {
 90        WIN_SetError("CreateIconIndirect()");
 91        return NULL;
 92    }
 93
 94    cursor = SDL_calloc(1, sizeof(*cursor));
 95    if (cursor) {
 96        cursor->driverdata = hicon;
 97    } else {
 98        DestroyIcon(hicon);
 99        SDL_OutOfMemory();
100    }
101
102    return cursor;
103}
104
105static void
106WIN_FreeCursor(SDL_Cursor * cursor)
107{
108    HICON hicon = (HICON)cursor->driverdata;
109
110    DestroyIcon(hicon);
111    SDL_free(cursor);
112}
113
114static int
115WIN_ShowCursor(SDL_Cursor * cursor)
116{
117    if (cursor) {
118        SDL_cursor = (HCURSOR)cursor->driverdata;
119    } else {
120        SDL_cursor = NULL;
121    }
122    if (SDL_GetMouseFocus() != NULL) {
123        SetCursor(SDL_cursor);
124    }
125    return 0;
126}
127
128static void
129WIN_WarpMouse(SDL_Window * window, int x, int y)
130{
131    HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
132    POINT pt;
133
134    pt.x = x;
135    pt.y = y;
136    ClientToScreen(hwnd, &pt);
137    SetCursorPos(pt.x, pt.y);
138}
139
140static int
141WIN_SetRelativeMouseMode(SDL_bool enabled)
142{
143    SDL_Unsupported();
144    return -1;
145}
146
147void
148WIN_InitMouse(_THIS)
149{
150    SDL_Mouse *mouse = SDL_GetMouse();
151
152    mouse->CreateCursor = WIN_CreateCursor;
153    mouse->ShowCursor = WIN_ShowCursor;
154    mouse->FreeCursor = WIN_FreeCursor;
155    mouse->WarpMouse = WIN_WarpMouse;
156    mouse->SetRelativeMouseMode = WIN_SetRelativeMouseMode;
157
158    SDL_SetDefaultCursor(WIN_CreateDefaultCursor());
159}
160
161void
162WIN_QuitMouse(_THIS)
163{
164}
165
166#endif /* SDL_VIDEO_DRIVER_WINDOWS */
167
168/* vi: set ts=4 sw=4 expandtab: */