src/video/cocoa/SDL_cocoamouse.m
author stopiccot <alexey.petruchik@gmail.com>
Sun, 06 Jan 2013 19:04:53 +0300
changeset 6832 156e608ec4ef
parent 6676 4ff261ffaa42
child 6848 478ecc8a58b3
permissions -rw-r--r--
Replaced manual NSAutoreleasePool handing with @autorelease
slouken@1931
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6138
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@1931
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@1931
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@1931
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@1931
    20
*/
slouken@1931
    21
#include "SDL_config.h"
slouken@1931
    22
slouken@6044
    23
#if SDL_VIDEO_DRIVER_COCOA
slouken@6044
    24
slouken@6676
    25
#include "SDL_assert.h"
slouken@3517
    26
#include "SDL_events.h"
slouken@1931
    27
#include "SDL_cocoavideo.h"
slouken@1931
    28
slouken@1931
    29
#include "../../events/SDL_mouse_c.h"
slouken@1931
    30
slouken@5376
    31
slouken@5376
    32
static SDL_Cursor *
slouken@5376
    33
Cocoa_CreateDefaultCursor()
slouken@5376
    34
{
alexey@6832
    35
    @autoreleasepool {
alexey@6832
    36
        NSCursor *nscursor;
alexey@6832
    37
        SDL_Cursor *cursor = NULL;
slouken@5376
    38
alexey@6832
    39
        nscursor = [NSCursor arrowCursor];
slouken@5376
    40
alexey@6832
    41
        if (nscursor) {
alexey@6832
    42
            cursor = SDL_calloc(1, sizeof(*cursor));
alexey@6832
    43
            if (cursor) {
alexey@6832
    44
                cursor->driverdata = nscursor;
alexey@6832
    45
                [nscursor retain];
alexey@6832
    46
            }
slouken@5376
    47
        }
alexey@6832
    48
        
alexey@6832
    49
        return cursor;
slouken@5376
    50
    }
slouken@5376
    51
}
slouken@5376
    52
slouken@5376
    53
static SDL_Cursor *
slouken@5376
    54
Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5376
    55
{
alexey@6832
    56
    @autoreleasepool {
alexey@6832
    57
        NSImage *nsimage;
alexey@6832
    58
        NSCursor *nscursor = NULL;
alexey@6832
    59
        SDL_Cursor *cursor = NULL;
slouken@5376
    60
alexey@6832
    61
        nsimage = Cocoa_CreateImage(surface);
alexey@6832
    62
        if (nsimage) {
alexey@6832
    63
            nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
alexey@6832
    64
        }
alexey@6832
    65
alexey@6832
    66
        if (nscursor) {
alexey@6832
    67
            cursor = SDL_calloc(1, sizeof(*cursor));
alexey@6832
    68
            if (cursor) {
alexey@6832
    69
                cursor->driverdata = nscursor;
alexey@6832
    70
            }
alexey@6832
    71
        }
alexey@6832
    72
        
alexey@6832
    73
        return cursor;
slouken@5376
    74
    }
slouken@5376
    75
}
slouken@5376
    76
slouken@6676
    77
static SDL_Cursor *
slouken@6676
    78
Cocoa_CreateSystemCursor(SDL_SystemCursor id)
slouken@6676
    79
{
alexey@6832
    80
    @autoreleasepool {
alexey@6832
    81
        NSCursor *nscursor = NULL;
alexey@6832
    82
        SDL_Cursor *cursor = NULL;
slouken@6676
    83
alexey@6832
    84
        switch(id)
alexey@6832
    85
        {
alexey@6832
    86
        case SDL_SYSTEM_CURSOR_ARROW:
alexey@6832
    87
            nscursor = [NSCursor arrowCursor];
alexey@6832
    88
            break;
alexey@6832
    89
        case SDL_SYSTEM_CURSOR_IBEAM:
alexey@6832
    90
            nscursor = [NSCursor IBeamCursor];
alexey@6832
    91
            break;
alexey@6832
    92
        case SDL_SYSTEM_CURSOR_WAIT:
alexey@6832
    93
            nscursor = [NSCursor arrowCursor];
alexey@6832
    94
            break;
alexey@6832
    95
        case SDL_SYSTEM_CURSOR_CROSSHAIR:
alexey@6832
    96
            nscursor = [NSCursor crosshairCursor];
alexey@6832
    97
            break;
alexey@6832
    98
        case SDL_SYSTEM_CURSOR_WAITARROW:
alexey@6832
    99
            nscursor = [NSCursor arrowCursor];
alexey@6832
   100
            break;
alexey@6832
   101
        case SDL_SYSTEM_CURSOR_SIZENWSE:
alexey@6832
   102
        case SDL_SYSTEM_CURSOR_SIZENESW:
alexey@6832
   103
            nscursor = [NSCursor closedHandCursor];
alexey@6832
   104
            break;
alexey@6832
   105
        case SDL_SYSTEM_CURSOR_SIZEWE:
alexey@6832
   106
            nscursor = [NSCursor resizeLeftRightCursor];
alexey@6832
   107
            break;
alexey@6832
   108
        case SDL_SYSTEM_CURSOR_SIZENS:
alexey@6832
   109
            nscursor = [NSCursor resizeUpDownCursor];
alexey@6832
   110
            break;
alexey@6832
   111
        case SDL_SYSTEM_CURSOR_SIZEALL:
alexey@6832
   112
            nscursor = [NSCursor closedHandCursor];
alexey@6832
   113
            break;
alexey@6832
   114
        case SDL_SYSTEM_CURSOR_NO:
alexey@6832
   115
            nscursor = [NSCursor operationNotAllowedCursor];
alexey@6832
   116
            break;
alexey@6832
   117
        case SDL_SYSTEM_CURSOR_HAND:
alexey@6832
   118
            nscursor = [NSCursor pointingHandCursor];
alexey@6832
   119
            break;
alexey@6832
   120
        default:
alexey@6832
   121
            SDL_assert(!"Unknown system cursor");
alexey@6832
   122
            return NULL;
alexey@6832
   123
        }
alexey@6832
   124
alexey@6832
   125
        if (nscursor) {
alexey@6832
   126
            cursor = SDL_calloc(1, sizeof(*cursor));
alexey@6832
   127
            if (cursor) {
alexey@6832
   128
                // We'll free it later, so retain it here
alexey@6832
   129
                [nscursor retain];
alexey@6832
   130
                cursor->driverdata = nscursor;
alexey@6832
   131
            }
alexey@6832
   132
        }
alexey@6832
   133
            
alexey@6832
   134
        return cursor;
slouken@6676
   135
    }
slouken@6676
   136
}
slouken@6676
   137
slouken@5376
   138
static void
slouken@5376
   139
Cocoa_FreeCursor(SDL_Cursor * cursor)
slouken@5376
   140
{
alexey@6832
   141
    @autoreleasepool {
alexey@6832
   142
        NSCursor *nscursor = (NSCursor *)cursor->driverdata;
slouken@5376
   143
alexey@6832
   144
        [nscursor release];
alexey@6832
   145
        SDL_free(cursor);
alexey@6832
   146
    }
slouken@5376
   147
}
slouken@5376
   148
slouken@5376
   149
static int
slouken@5376
   150
Cocoa_ShowCursor(SDL_Cursor * cursor)
slouken@5376
   151
{
alexey@6832
   152
    @autoreleasepool {
alexey@6832
   153
        if (cursor) {
alexey@6832
   154
            NSCursor *nscursor = (NSCursor *)cursor->driverdata;
slouken@5376
   155
alexey@6832
   156
            [nscursor set];
alexey@6832
   157
            [NSCursor unhide];
alexey@6832
   158
        } else {
alexey@6832
   159
            [NSCursor hide];
alexey@6832
   160
        }
slouken@5376
   161
    }
slouken@5376
   162
slouken@5376
   163
    return 0;
slouken@5376
   164
}
slouken@5376
   165
slouken@5376
   166
static void
slouken@5376
   167
Cocoa_WarpMouse(SDL_Window * window, int x, int y)
slouken@5376
   168
{
slouken@5376
   169
    CGPoint point;
slouken@5376
   170
slouken@5391
   171
    point.x = (float)window->x + x;
slouken@5391
   172
    point.y = (float)window->y + y;
slouken@5376
   173
    CGWarpMouseCursorPosition(point);
slouken@5376
   174
}
slouken@5376
   175
slouken@5406
   176
static int
slouken@5406
   177
Cocoa_SetRelativeMouseMode(SDL_bool enabled)
slouken@5406
   178
{
slouken@5406
   179
    CGError result;
slouken@5406
   180
slouken@5406
   181
    if (enabled) {
slouken@5406
   182
        result = CGAssociateMouseAndMouseCursorPosition(NO);
slouken@5406
   183
    } else {
slouken@5406
   184
        result = CGAssociateMouseAndMouseCursorPosition(YES);
slouken@5406
   185
    }
slouken@5406
   186
    if (result != kCGErrorSuccess) {
slouken@5406
   187
        SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
slouken@5406
   188
        return -1;
slouken@5406
   189
    }
slouken@5406
   190
    return 0;
slouken@5406
   191
}
slouken@5406
   192
slouken@1931
   193
void
slouken@1931
   194
Cocoa_InitMouse(_THIS)
slouken@1931
   195
{
slouken@5376
   196
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5376
   197
slouken@5376
   198
    mouse->CreateCursor = Cocoa_CreateCursor;
slouken@6676
   199
    mouse->CreateSystemCursor = Cocoa_CreateSystemCursor;
slouken@5376
   200
    mouse->ShowCursor = Cocoa_ShowCursor;
slouken@5406
   201
    mouse->FreeCursor = Cocoa_FreeCursor;
slouken@5376
   202
    mouse->WarpMouse = Cocoa_WarpMouse;
slouken@5406
   203
    mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
slouken@5376
   204
slouken@5405
   205
    SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
slouken@1931
   206
}
slouken@1931
   207
slouken@3517
   208
void
slouken@3517
   209
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
slouken@3517
   210
{
slouken@5406
   211
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5406
   212
slouken@5477
   213
    if (mouse->relative_mode &&
slouken@5477
   214
        ([event type] == NSMouseMoved ||
slouken@5477
   215
         [event type] == NSLeftMouseDragged ||
slouken@5477
   216
         [event type] == NSRightMouseDragged ||
slouken@5477
   217
         [event type] == NSOtherMouseDragged)) {
slouken@5406
   218
        float x = [event deltaX];
slouken@5406
   219
        float y = [event deltaY];
slouken@5406
   220
        SDL_SendMouseMotion(mouse->focus, 1, (int)x, (int)y);
slouken@5406
   221
    }
slouken@3517
   222
}
slouken@3517
   223
slouken@1931
   224
void
gzjjgod@5057
   225
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
gzjjgod@5057
   226
{
gzjjgod@5057
   227
    float x = [event deltaX];
gzjjgod@5057
   228
    float y = [event deltaY];
gzjjgod@5057
   229
gzjjgod@5057
   230
    if (x > 0) {
gzjjgod@5057
   231
        x += 0.9f;
gzjjgod@5057
   232
    } else if (x < 0) {
gzjjgod@5057
   233
        x -= 0.9f;
gzjjgod@5057
   234
    }
gzjjgod@5057
   235
    if (y > 0) {
gzjjgod@5057
   236
        y += 0.9f;
gzjjgod@5057
   237
    } else if (y < 0) {
gzjjgod@5057
   238
        y -= 0.9f;
gzjjgod@5057
   239
    }
gzjjgod@5057
   240
    SDL_SendMouseWheel(window, (int)x, (int)y);
gzjjgod@5057
   241
}
gzjjgod@5057
   242
slouken@5058
   243
void
slouken@5058
   244
Cocoa_QuitMouse(_THIS)
slouken@5058
   245
{
slouken@5058
   246
}
slouken@5058
   247
slouken@6044
   248
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   249
slouken@1931
   250
/* vi: set ts=4 sw=4 expandtab: */