src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 27 Feb 2011 22:06:46 -0800
changeset 5406 1517da4ab6b1
parent 5405 64fa8526e1ce
child 5419 210b0abe9e44
permissions -rw-r--r--
Implemented mouse relative mode on Mac OS X.
     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 
    85     [pool release];
    86 }
    87 
    88 static int
    89 Cocoa_ShowCursor(SDL_Cursor * cursor)
    90 {
    91     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    92 
    93     if (SDL_GetMouseFocus()) {
    94         if (cursor) {
    95             NSCursor *nscursor = (NSCursor *)cursor->driverdata;
    96 
    97             [nscursor set];
    98             [NSCursor unhide];
    99         } else {
   100             [NSCursor hide];
   101         }
   102     }
   103 
   104     [pool release];
   105 
   106     return 0;
   107 }
   108 
   109 static void
   110 Cocoa_WarpMouse(SDL_Window * window, int x, int y)
   111 {
   112     CGPoint point;
   113 
   114     point.x = (float)window->x + x;
   115     point.y = (float)window->y + y;
   116     CGWarpMouseCursorPosition(point);
   117 }
   118 
   119 static int
   120 Cocoa_SetRelativeMouseMode(SDL_bool enabled)
   121 {
   122     CGError result;
   123 
   124     if (enabled) {
   125         result = CGAssociateMouseAndMouseCursorPosition(NO);
   126     } else {
   127         result = CGAssociateMouseAndMouseCursorPosition(YES);
   128     }
   129     if (result != kCGErrorSuccess) {
   130         SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
   131         return -1;
   132     }
   133     return 0;
   134 }
   135 
   136 void
   137 Cocoa_InitMouse(_THIS)
   138 {
   139     SDL_Mouse *mouse = SDL_GetMouse();
   140 
   141     mouse->CreateCursor = Cocoa_CreateCursor;
   142     mouse->ShowCursor = Cocoa_ShowCursor;
   143     mouse->FreeCursor = Cocoa_FreeCursor;
   144     mouse->WarpMouse = Cocoa_WarpMouse;
   145     mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
   146 
   147     SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
   148 }
   149 
   150 static int
   151 ConvertMouseButtonToSDL(int button)
   152 {
   153     switch (button)
   154     {
   155         case 0:
   156             return(SDL_BUTTON_LEFT);   /* 1 */
   157         case 1:
   158             return(SDL_BUTTON_RIGHT);  /* 3 */
   159         case 2:
   160             return(SDL_BUTTON_MIDDLE); /* 2 */
   161     }
   162     return button+1;
   163 }
   164 
   165 void
   166 Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
   167 {
   168     SDL_Mouse *mouse = SDL_GetMouse();
   169 
   170     if (mouse->relative_mode && [event type] == NSMouseMoved) {
   171         float x = [event deltaX];
   172         float y = [event deltaY];
   173         SDL_SendMouseMotion(mouse->focus, 1, (int)x, (int)y);
   174     }
   175 }
   176 
   177 void
   178 Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
   179 {
   180     float x = [event deltaX];
   181     float y = [event deltaY];
   182 
   183     if (x > 0) {
   184         x += 0.9f;
   185     } else if (x < 0) {
   186         x -= 0.9f;
   187     }
   188     if (y > 0) {
   189         y += 0.9f;
   190     } else if (y < 0) {
   191         y -= 0.9f;
   192     }
   193     SDL_SendMouseWheel(window, (int)x, (int)y);
   194 }
   195 
   196 void
   197 Cocoa_QuitMouse(_THIS)
   198 {
   199 }
   200 
   201 /* vi: set ts=4 sw=4 expandtab: */