Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Feb 22, 2011
1 parent f5c24e8 commit db00b94
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/events/SDL_mouse.c
Expand Up @@ -37,6 +37,10 @@ static SDL_Mouse SDL_mouse;
int
SDL_MouseInit(void)
{
SDL_Mouse *mouse = SDL_GetMouse();

mouse->cursor_shown = SDL_TRUE;

return (0);
}

Expand All @@ -46,12 +50,6 @@ SDL_GetMouse(void)
return &SDL_mouse;
}

void
SDL_ResetMouse(void)
{
/* FIXME */
}

SDL_Window *
SDL_GetMouseFocus(void)
{
Expand Down
3 changes: 0 additions & 3 deletions src/events/SDL_mouse_c.h
Expand Up @@ -72,9 +72,6 @@ extern int SDL_MouseInit(void);
/* Get the mouse state structure */
SDL_Mouse *SDL_GetMouse(void);

/* Clear the mouse state */
extern void SDL_ResetMouse(void);

/* Set the mouse focus window */
extern void SDL_SetMouseFocus(SDL_Window * window);

Expand Down
97 changes: 97 additions & 0 deletions src/video/cocoa/SDL_cocoamouse.m
Expand Up @@ -26,9 +26,106 @@

#include "../../events/SDL_mouse_c.h"


static SDL_Cursor *
Cocoa_CreateDefaultCursor()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSCursor *nscursor;
SDL_Cursor *cursor = NULL;

nscursor = [NSCursor arrowCursor];

if (nscursor) {
cursor = SDL_calloc(1, sizeof(*cursor));
if (cursor) {
cursor->driverdata = nscursor;
[nscursor retain];
}
}

[pool release];

return cursor;
}

static SDL_Cursor *
Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSImage *nsimage;
NSCursor *nscursor = NULL;
SDL_Cursor *cursor = NULL;

nsimage = Cocoa_CreateImage(surface);
if (nsimage) {
nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
}

if (nscursor) {
cursor = SDL_calloc(1, sizeof(*cursor));
if (cursor) {
cursor->driverdata = nscursor;
}
}

[pool release];

return cursor;
}

static void
Cocoa_FreeCursor(SDL_Cursor * cursor)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSCursor *nscursor = (NSCursor *)cursor->driverdata;

[nscursor release];

[pool release];
}

static int
Cocoa_ShowCursor(SDL_Cursor * cursor)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

if (SDL_GetMouseFocus()) {
if (cursor) {
[NSCursor unhide];
} else {
[NSCursor hide];
}
}

[pool release];

return 0;
}

static void
Cocoa_WarpMouse(SDL_Window * window, int x, int y)
{
CGPoint point;

point.x = (CGFloat)window->x + x;
point.y = (CGFloat)window->y + y;
CGWarpMouseCursorPosition(point);
}

void
Cocoa_InitMouse(_THIS)
{
SDL_Mouse *mouse = SDL_GetMouse();
SDL_Cursor *cursor;

mouse->CreateCursor = Cocoa_CreateCursor;
mouse->ShowCursor = Cocoa_ShowCursor;
mouse->WarpMouse = Cocoa_WarpMouse;
mouse->FreeCursor = Cocoa_FreeCursor;

cursor = Cocoa_CreateDefaultCursor();
mouse->cursors = mouse->cur_cursor = cursor;
}

static int
Expand Down
11 changes: 11 additions & 0 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -251,12 +251,19 @@ - (void)otherMouseUp:(NSEvent *)theEvent

- (void)mouseEntered:(NSEvent *)theEvent
{
SDL_Mouse *mouse = SDL_GetMouse();

SDL_SetMouseFocus(_data->window);

if (!mouse->cursor_shown) {
[NSCursor hide];
}
}

- (void)mouseExited:(NSEvent *)theEvent
{
SDL_Window *window = _data->window;
SDL_Mouse *mouse = SDL_GetMouse();

if (SDL_GetMouseFocus() == window) {
if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
Expand All @@ -276,6 +283,10 @@ - (void)mouseExited:(NSEvent *)theEvent
SDL_SetMouseFocus(NULL);
}
}

if (!mouse->cursor_shown) {
[NSCursor unhide];
}
}

- (void)mouseMoved:(NSEvent *)theEvent
Expand Down

0 comments on commit db00b94

Please sign in to comment.