Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X

  • Loading branch information
slouken committed Feb 22, 2011
1 parent f5c24e8 commit db00b94e9df952b0920ab247aa93587d064487a8
Showing with 112 additions and 9 deletions.
  1. +4 −6 src/events/SDL_mouse.c
  2. +0 −3 src/events/SDL_mouse_c.h
  3. +97 −0 src/video/cocoa/SDL_cocoamouse.m
  4. +11 −0 src/video/cocoa/SDL_cocoawindow.m
@@ -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);
}

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

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

SDL_Window *
SDL_GetMouseFocus(void)
{
@@ -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);

@@ -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
@@ -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) {
@@ -276,6 +283,10 @@ - (void)mouseExited:(NSEvent *)theEvent
SDL_SetMouseFocus(NULL);
}
}

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

- (void)mouseMoved:(NSEvent *)theEvent

0 comments on commit db00b94

Please sign in to comment.