src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Oct 2011 05:56:58 -0400
changeset 6044 35448a5ea044
parent 5535 96594ac5fd1a
child 6138 4c64952a58fb
permissions -rw-r--r--
Lots of fixes importing SDL source wholesale into a new iOS project
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_VIDEO_DRIVER_COCOA
    24 
    25 #include "SDL_events.h"
    26 #include "SDL_cocoavideo.h"
    27 
    28 #include "../../events/SDL_mouse_c.h"
    29 
    30 
    31 static SDL_Cursor *
    32 Cocoa_CreateDefaultCursor()
    33 {
    34     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    35     NSCursor *nscursor;
    36     SDL_Cursor *cursor = NULL;
    37 
    38     nscursor = [NSCursor arrowCursor];
    39 
    40     if (nscursor) {
    41         cursor = SDL_calloc(1, sizeof(*cursor));
    42         if (cursor) {
    43             cursor->driverdata = nscursor;
    44             [nscursor retain];
    45         }
    46     }
    47 
    48     [pool release];
    49 
    50     return cursor;
    51 }
    52 
    53 static SDL_Cursor *
    54 Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
    55 {
    56     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    57     NSImage *nsimage;
    58     NSCursor *nscursor = NULL;
    59     SDL_Cursor *cursor = NULL;
    60 
    61     nsimage = Cocoa_CreateImage(surface);
    62     if (nsimage) {
    63         nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
    64     }
    65 
    66     if (nscursor) {
    67         cursor = SDL_calloc(1, sizeof(*cursor));
    68         if (cursor) {
    69             cursor->driverdata = nscursor;
    70         }
    71     }
    72 
    73     [pool release];
    74 
    75     return cursor;
    76 }
    77 
    78 static void
    79 Cocoa_FreeCursor(SDL_Cursor * cursor)
    80 {
    81     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    82     NSCursor *nscursor = (NSCursor *)cursor->driverdata;
    83 
    84     [nscursor release];
    85     SDL_free(cursor);
    86 
    87     [pool release];
    88 }
    89 
    90 static int
    91 Cocoa_ShowCursor(SDL_Cursor * cursor)
    92 {
    93     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    94 
    95     if (SDL_GetMouseFocus()) {
    96         if (cursor) {
    97             NSCursor *nscursor = (NSCursor *)cursor->driverdata;
    98 
    99             [nscursor set];
   100             [NSCursor unhide];
   101         } else {
   102             [NSCursor hide];
   103         }
   104     }
   105 
   106     [pool release];
   107 
   108     return 0;
   109 }
   110 
   111 static void
   112 Cocoa_WarpMouse(SDL_Window * window, int x, int y)
   113 {
   114     CGPoint point;
   115 
   116     point.x = (float)window->x + x;
   117     point.y = (float)window->y + y;
   118     CGWarpMouseCursorPosition(point);
   119 }
   120 
   121 static int
   122 Cocoa_SetRelativeMouseMode(SDL_bool enabled)
   123 {
   124     CGError result;
   125 
   126     if (enabled) {
   127         result = CGAssociateMouseAndMouseCursorPosition(NO);
   128     } else {
   129         result = CGAssociateMouseAndMouseCursorPosition(YES);
   130     }
   131     if (result != kCGErrorSuccess) {
   132         SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
   133         return -1;
   134     }
   135     return 0;
   136 }
   137 
   138 void
   139 Cocoa_InitMouse(_THIS)
   140 {
   141     SDL_Mouse *mouse = SDL_GetMouse();
   142 
   143     mouse->CreateCursor = Cocoa_CreateCursor;
   144     mouse->ShowCursor = Cocoa_ShowCursor;
   145     mouse->FreeCursor = Cocoa_FreeCursor;
   146     mouse->WarpMouse = Cocoa_WarpMouse;
   147     mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
   148 
   149     SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
   150 }
   151 
   152 void
   153 Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
   154 {
   155     SDL_Mouse *mouse = SDL_GetMouse();
   156 
   157     if (mouse->relative_mode &&
   158         ([event type] == NSMouseMoved ||
   159          [event type] == NSLeftMouseDragged ||
   160          [event type] == NSRightMouseDragged ||
   161          [event type] == NSOtherMouseDragged)) {
   162         float x = [event deltaX];
   163         float y = [event deltaY];
   164         SDL_SendMouseMotion(mouse->focus, 1, (int)x, (int)y);
   165     }
   166 }
   167 
   168 void
   169 Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
   170 {
   171     float x = [event deltaX];
   172     float y = [event deltaY];
   173 
   174     if (x > 0) {
   175         x += 0.9f;
   176     } else if (x < 0) {
   177         x -= 0.9f;
   178     }
   179     if (y > 0) {
   180         y += 0.9f;
   181     } else if (y < 0) {
   182         y -= 0.9f;
   183     }
   184     SDL_SendMouseWheel(window, (int)x, (int)y);
   185 }
   186 
   187 void
   188 Cocoa_QuitMouse(_THIS)
   189 {
   190 }
   191 
   192 #endif /* SDL_VIDEO_DRIVER_COCOA */
   193 
   194 /* vi: set ts=4 sw=4 expandtab: */