Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
authorSam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 17:15:50 -0800
changeset 5376183ec2d4485c
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
     1.1 --- a/src/events/SDL_mouse.c	Mon Feb 21 16:45:23 2011 -0800
     1.2 +++ b/src/events/SDL_mouse.c	Mon Feb 21 17:15:50 2011 -0800
     1.3 @@ -37,6 +37,10 @@
     1.4  int
     1.5  SDL_MouseInit(void)
     1.6  {
     1.7 +    SDL_Mouse *mouse = SDL_GetMouse();
     1.8 +
     1.9 +    mouse->cursor_shown = SDL_TRUE;
    1.10 +
    1.11      return (0);
    1.12  }
    1.13  
    1.14 @@ -46,12 +50,6 @@
    1.15      return &SDL_mouse;
    1.16  }
    1.17  
    1.18 -void
    1.19 -SDL_ResetMouse(void)
    1.20 -{
    1.21 -    /* FIXME */
    1.22 -}
    1.23 -
    1.24  SDL_Window *
    1.25  SDL_GetMouseFocus(void)
    1.26  {
     2.1 --- a/src/events/SDL_mouse_c.h	Mon Feb 21 16:45:23 2011 -0800
     2.2 +++ b/src/events/SDL_mouse_c.h	Mon Feb 21 17:15:50 2011 -0800
     2.3 @@ -72,9 +72,6 @@
     2.4  /* Get the mouse state structure */
     2.5  SDL_Mouse *SDL_GetMouse(void);
     2.6  
     2.7 -/* Clear the mouse state */
     2.8 -extern void SDL_ResetMouse(void);
     2.9 -
    2.10  /* Set the mouse focus window */
    2.11  extern void SDL_SetMouseFocus(SDL_Window * window);
    2.12  
     3.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Mon Feb 21 16:45:23 2011 -0800
     3.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Mon Feb 21 17:15:50 2011 -0800
     3.3 @@ -26,9 +26,106 @@
     3.4  
     3.5  #include "../../events/SDL_mouse_c.h"
     3.6  
     3.7 +
     3.8 +static SDL_Cursor *
     3.9 +Cocoa_CreateDefaultCursor()
    3.10 +{
    3.11 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    3.12 +    NSCursor *nscursor;
    3.13 +    SDL_Cursor *cursor = NULL;
    3.14 +
    3.15 +    nscursor = [NSCursor arrowCursor];
    3.16 +
    3.17 +    if (nscursor) {
    3.18 +        cursor = SDL_calloc(1, sizeof(*cursor));
    3.19 +        if (cursor) {
    3.20 +            cursor->driverdata = nscursor;
    3.21 +            [nscursor retain];
    3.22 +        }
    3.23 +    }
    3.24 +
    3.25 +    [pool release];
    3.26 +
    3.27 +    return cursor;
    3.28 +}
    3.29 +
    3.30 +static SDL_Cursor *
    3.31 +Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
    3.32 +{
    3.33 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    3.34 +    NSImage *nsimage;
    3.35 +    NSCursor *nscursor = NULL;
    3.36 +    SDL_Cursor *cursor = NULL;
    3.37 +
    3.38 +    nsimage = Cocoa_CreateImage(surface);
    3.39 +    if (nsimage) {
    3.40 +        nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
    3.41 +    }
    3.42 +
    3.43 +    if (nscursor) {
    3.44 +        cursor = SDL_calloc(1, sizeof(*cursor));
    3.45 +        if (cursor) {
    3.46 +            cursor->driverdata = nscursor;
    3.47 +        }
    3.48 +    }
    3.49 +
    3.50 +    [pool release];
    3.51 +
    3.52 +    return cursor;
    3.53 +}
    3.54 +
    3.55 +static void
    3.56 +Cocoa_FreeCursor(SDL_Cursor * cursor)
    3.57 +{
    3.58 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    3.59 +    NSCursor *nscursor = (NSCursor *)cursor->driverdata;
    3.60 +
    3.61 +    [nscursor release];
    3.62 +
    3.63 +    [pool release];
    3.64 +}
    3.65 +
    3.66 +static int
    3.67 +Cocoa_ShowCursor(SDL_Cursor * cursor)
    3.68 +{
    3.69 +    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    3.70 +
    3.71 +    if (SDL_GetMouseFocus()) {
    3.72 +        if (cursor) {
    3.73 +            [NSCursor unhide];
    3.74 +        } else {
    3.75 +            [NSCursor hide];
    3.76 +        }
    3.77 +    }
    3.78 +
    3.79 +    [pool release];
    3.80 +
    3.81 +    return 0;
    3.82 +}
    3.83 +
    3.84 +static void
    3.85 +Cocoa_WarpMouse(SDL_Window * window, int x, int y)
    3.86 +{
    3.87 +    CGPoint point;
    3.88 +
    3.89 +    point.x = (CGFloat)window->x + x;
    3.90 +    point.y = (CGFloat)window->y + y;
    3.91 +    CGWarpMouseCursorPosition(point);
    3.92 +}
    3.93 +
    3.94  void
    3.95  Cocoa_InitMouse(_THIS)
    3.96  {
    3.97 +    SDL_Mouse *mouse = SDL_GetMouse();
    3.98 +    SDL_Cursor *cursor;
    3.99 +
   3.100 +    mouse->CreateCursor = Cocoa_CreateCursor;
   3.101 +    mouse->ShowCursor = Cocoa_ShowCursor;
   3.102 +    mouse->WarpMouse = Cocoa_WarpMouse;
   3.103 +    mouse->FreeCursor = Cocoa_FreeCursor;
   3.104 +
   3.105 +    cursor = Cocoa_CreateDefaultCursor();
   3.106 +    mouse->cursors = mouse->cur_cursor = cursor;
   3.107  }
   3.108  
   3.109  static int
     4.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 21 16:45:23 2011 -0800
     4.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Feb 21 17:15:50 2011 -0800
     4.3 @@ -251,12 +251,19 @@
     4.4  
     4.5  - (void)mouseEntered:(NSEvent *)theEvent
     4.6  {
     4.7 +    SDL_Mouse *mouse = SDL_GetMouse();
     4.8 +
     4.9      SDL_SetMouseFocus(_data->window);
    4.10 +
    4.11 +    if (!mouse->cursor_shown) {
    4.12 +        [NSCursor hide];
    4.13 +    }
    4.14  }
    4.15  
    4.16  - (void)mouseExited:(NSEvent *)theEvent
    4.17  {
    4.18      SDL_Window *window = _data->window;
    4.19 +    SDL_Mouse *mouse = SDL_GetMouse();
    4.20  
    4.21      if (SDL_GetMouseFocus() == window) {
    4.22          if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
    4.23 @@ -276,6 +283,10 @@
    4.24              SDL_SetMouseFocus(NULL);
    4.25          }
    4.26      }
    4.27 +
    4.28 +    if (!mouse->cursor_shown) {
    4.29 +        [NSCursor unhide];
    4.30 +    }
    4.31  }
    4.32  
    4.33  - (void)mouseMoved:(NSEvent *)theEvent