src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5477 50cd1fb880e5
child 6044 35448a5ea044
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     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 #include "SDL_events.h"
    24 #include "SDL_cocoavideo.h"
    25 
    26 #include "../../events/SDL_mouse_c.h"
    27 
    28 
    29 static SDL_Cursor *
    30 Cocoa_CreateDefaultCursor()
    31 {
    32     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    33     NSCursor *nscursor;
    34     SDL_Cursor *cursor = NULL;
    35 
    36     nscursor = [NSCursor arrowCursor];
    37 
    38     if (nscursor) {
    39         cursor = SDL_calloc(1, sizeof(*cursor));
    40         if (cursor) {
    41             cursor->driverdata = nscursor;
    42             [nscursor retain];
    43         }
    44     }
    45 
    46     [pool release];
    47 
    48     return cursor;
    49 }
    50 
    51 static SDL_Cursor *
    52 Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
    53 {
    54     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    55     NSImage *nsimage;
    56     NSCursor *nscursor = NULL;
    57     SDL_Cursor *cursor = NULL;
    58 
    59     nsimage = Cocoa_CreateImage(surface);
    60     if (nsimage) {
    61         nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
    62     }
    63 
    64     if (nscursor) {
    65         cursor = SDL_calloc(1, sizeof(*cursor));
    66         if (cursor) {
    67             cursor->driverdata = nscursor;
    68         }
    69     }
    70 
    71     [pool release];
    72 
    73     return cursor;
    74 }
    75 
    76 static void
    77 Cocoa_FreeCursor(SDL_Cursor * cursor)
    78 {
    79     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    80     NSCursor *nscursor = (NSCursor *)cursor->driverdata;
    81 
    82     [nscursor release];
    83     SDL_free(cursor);
    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 &&
   171         ([event type] == NSMouseMoved ||
   172          [event type] == NSLeftMouseDragged ||
   173          [event type] == NSRightMouseDragged ||
   174          [event type] == NSOtherMouseDragged)) {
   175         float x = [event deltaX];
   176         float y = [event deltaY];
   177         SDL_SendMouseMotion(mouse->focus, 1, (int)x, (int)y);
   178     }
   179 }
   180 
   181 void
   182 Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
   183 {
   184     float x = [event deltaX];
   185     float y = [event deltaY];
   186 
   187     if (x > 0) {
   188         x += 0.9f;
   189     } else if (x < 0) {
   190         x -= 0.9f;
   191     }
   192     if (y > 0) {
   193         y += 0.9f;
   194     } else if (y < 0) {
   195         y -= 0.9f;
   196     }
   197     SDL_SendMouseWheel(window, (int)x, (int)y);
   198 }
   199 
   200 void
   201 Cocoa_QuitMouse(_THIS)
   202 {
   203 }
   204 
   205 /* vi: set ts=4 sw=4 expandtab: */