src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 28 Feb 2011 22:21:32 -0800
changeset 5419 210b0abe9e44
parent 5406 1517da4ab6b1
child 5477 50cd1fb880e5
permissions -rw-r--r--
Fixed memory leak freeing cursors
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2011 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_events.h"
    25 #include "SDL_cocoavideo.h"
    26 
    27 #include "../../events/SDL_mouse_c.h"
    28 
    29 
    30 static SDL_Cursor *
    31 Cocoa_CreateDefaultCursor()
    32 {
    33     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    34     NSCursor *nscursor;
    35     SDL_Cursor *cursor = NULL;
    36 
    37     nscursor = [NSCursor arrowCursor];
    38 
    39     if (nscursor) {
    40         cursor = SDL_calloc(1, sizeof(*cursor));
    41         if (cursor) {
    42             cursor->driverdata = nscursor;
    43             [nscursor retain];
    44         }
    45     }
    46 
    47     [pool release];
    48 
    49     return cursor;
    50 }
    51 
    52 static SDL_Cursor *
    53 Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
    54 {
    55     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    56     NSImage *nsimage;
    57     NSCursor *nscursor = NULL;
    58     SDL_Cursor *cursor = NULL;
    59 
    60     nsimage = Cocoa_CreateImage(surface);
    61     if (nsimage) {
    62         nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
    63     }
    64 
    65     if (nscursor) {
    66         cursor = SDL_calloc(1, sizeof(*cursor));
    67         if (cursor) {
    68             cursor->driverdata = nscursor;
    69         }
    70     }
    71 
    72     [pool release];
    73 
    74     return cursor;
    75 }
    76 
    77 static void
    78 Cocoa_FreeCursor(SDL_Cursor * cursor)
    79 {
    80     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    81     NSCursor *nscursor = (NSCursor *)cursor->driverdata;
    82 
    83     [nscursor release];
    84     SDL_free(cursor);
    85 
    86     [pool release];
    87 }
    88 
    89 static int
    90 Cocoa_ShowCursor(SDL_Cursor * cursor)
    91 {
    92     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    93 
    94     if (SDL_GetMouseFocus()) {
    95         if (cursor) {
    96             NSCursor *nscursor = (NSCursor *)cursor->driverdata;
    97 
    98             [nscursor set];
    99             [NSCursor unhide];
   100         } else {
   101             [NSCursor hide];
   102         }
   103     }
   104 
   105     [pool release];
   106 
   107     return 0;
   108 }
   109 
   110 static void
   111 Cocoa_WarpMouse(SDL_Window * window, int x, int y)
   112 {
   113     CGPoint point;
   114 
   115     point.x = (float)window->x + x;
   116     point.y = (float)window->y + y;
   117     CGWarpMouseCursorPosition(point);
   118 }
   119 
   120 static int
   121 Cocoa_SetRelativeMouseMode(SDL_bool enabled)
   122 {
   123     CGError result;
   124 
   125     if (enabled) {
   126         result = CGAssociateMouseAndMouseCursorPosition(NO);
   127     } else {
   128         result = CGAssociateMouseAndMouseCursorPosition(YES);
   129     }
   130     if (result != kCGErrorSuccess) {
   131         SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
   132         return -1;
   133     }
   134     return 0;
   135 }
   136 
   137 void
   138 Cocoa_InitMouse(_THIS)
   139 {
   140     SDL_Mouse *mouse = SDL_GetMouse();
   141 
   142     mouse->CreateCursor = Cocoa_CreateCursor;
   143     mouse->ShowCursor = Cocoa_ShowCursor;
   144     mouse->FreeCursor = Cocoa_FreeCursor;
   145     mouse->WarpMouse = Cocoa_WarpMouse;
   146     mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
   147 
   148     SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
   149 }
   150 
   151 static int
   152 ConvertMouseButtonToSDL(int button)
   153 {
   154     switch (button)
   155     {
   156         case 0:
   157             return(SDL_BUTTON_LEFT);   /* 1 */
   158         case 1:
   159             return(SDL_BUTTON_RIGHT);  /* 3 */
   160         case 2:
   161             return(SDL_BUTTON_MIDDLE); /* 2 */
   162     }
   163     return button+1;
   164 }
   165 
   166 void
   167 Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
   168 {
   169     SDL_Mouse *mouse = SDL_GetMouse();
   170 
   171     if (mouse->relative_mode && [event type] == NSMouseMoved) {
   172         float x = [event deltaX];
   173         float y = [event deltaY];
   174         SDL_SendMouseMotion(mouse->focus, 1, (int)x, (int)y);
   175     }
   176 }
   177 
   178 void
   179 Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
   180 {
   181     float x = [event deltaX];
   182     float y = [event deltaY];
   183 
   184     if (x > 0) {
   185         x += 0.9f;
   186     } else if (x < 0) {
   187         x -= 0.9f;
   188     }
   189     if (y > 0) {
   190         y += 0.9f;
   191     } else if (y < 0) {
   192         y -= 0.9f;
   193     }
   194     SDL_SendMouseWheel(window, (int)x, (int)y);
   195 }
   196 
   197 void
   198 Cocoa_QuitMouse(_THIS)
   199 {
   200 }
   201 
   202 /* vi: set ts=4 sw=4 expandtab: */