Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
authorSam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 17:15:50 -0800
changeset 5376 183ec2d4485c
parent 5375 16877f74123c
child 5377 6125f6da3dae
Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
src/events/SDL_mouse.c
src/events/SDL_mouse_c.h
src/video/cocoa/SDL_cocoamouse.m
src/video/cocoa/SDL_cocoawindow.m
--- a/src/events/SDL_mouse.c	Mon Feb 21 16:45:23 2011 -0800
+++ b/src/events/SDL_mouse.c	Mon Feb 21 17:15:50 2011 -0800
@@ -37,6 +37,10 @@
 int
 SDL_MouseInit(void)
 {
+    SDL_Mouse *mouse = SDL_GetMouse();
+
+    mouse->cursor_shown = SDL_TRUE;
+
     return (0);
 }
 
@@ -46,12 +50,6 @@
     return &SDL_mouse;
 }
 
-void
-SDL_ResetMouse(void)
-{
-    /* FIXME */
-}
-
 SDL_Window *
 SDL_GetMouseFocus(void)
 {
--- a/src/events/SDL_mouse_c.h	Mon Feb 21 16:45:23 2011 -0800
+++ b/src/events/SDL_mouse_c.h	Mon Feb 21 17:15:50 2011 -0800
@@ -72,9 +72,6 @@
 /* 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);
 
--- a/src/video/cocoa/SDL_cocoamouse.m	Mon Feb 21 16:45:23 2011 -0800
+++ b/src/video/cocoa/SDL_cocoamouse.m	Mon Feb 21 17:15:50 2011 -0800
@@ -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
--- a/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 21 16:45:23 2011 -0800
+++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 21 17:15:50 2011 -0800
@@ -251,12 +251,19 @@
 
 - (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 @@
             SDL_SetMouseFocus(NULL);
         }
     }
+
+    if (!mouse->cursor_shown) {
+        [NSCursor unhide];
+    }
 }
 
 - (void)mouseMoved:(NSEvent *)theEvent