src/video/cocoa/SDL_cocoamouse.m
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 07 Aug 2013 16:29:15 -0700
changeset 7593 20298a0d8631
parent 7465 1bcf7aac5623
child 7792 b8d4be87970a
permissions -rw-r--r--
Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.

This uses a better mouse grab if you define SDL_MAC_NO_SANDBOX. This
mouse grab uses CGEventTapCreate, which you cannot access if you have
sandboxing enabled.
slouken@1931
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 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"
jorgen@7114
    27
#include "SDL_cocoamouse.h"
jorgen@7593
    28
#include "SDL_cocoamousetap.h"
slouken@1931
    29
slouken@1931
    30
#include "../../events/SDL_mouse_c.h"
slouken@1931
    31
jorgen@7158
    32
@implementation NSCursor (InvisibleCursor)
jorgen@7158
    33
+ (NSCursor *)invisibleCursor
jorgen@7158
    34
{
jorgen@7158
    35
    static NSCursor *invisibleCursor = NULL;
jorgen@7158
    36
    if (!invisibleCursor) {
jorgen@7158
    37
        /* RAW 16x16 transparent GIF */
jorgen@7158
    38
        static unsigned char cursorBytes[] = {
jorgen@7158
    39
            0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80,
jorgen@7158
    40
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04,
jorgen@7158
    41
            0x01, 0x00, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x10,
jorgen@7158
    42
            0x00, 0x10, 0x00, 0x00, 0x02, 0x0E, 0x8C, 0x8F, 0xA9, 0xCB, 0xED,
jorgen@7158
    43
            0x0F, 0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0x3E, 0x05, 0x00, 0x3B
jorgen@7158
    44
        };
jorgen@7158
    45
jorgen@7158
    46
        NSData *cursorData = [NSData dataWithBytesNoCopy:&cursorBytes[0]
jorgen@7158
    47
                                                  length:sizeof(cursorBytes)
jorgen@7158
    48
                                            freeWhenDone:NO];
jorgen@7158
    49
        NSImage *cursorImage = [[[NSImage alloc] initWithData:cursorData] autorelease];
jorgen@7158
    50
        invisibleCursor = [[NSCursor alloc] initWithImage:cursorImage
jorgen@7158
    51
                                                  hotSpot:NSZeroPoint];
jorgen@7158
    52
    }
jorgen@7158
    53
jorgen@7158
    54
    return invisibleCursor;
jorgen@7158
    55
}
jorgen@7158
    56
@end
jorgen@7158
    57
slouken@5376
    58
slouken@5376
    59
static SDL_Cursor *
slouken@5376
    60
Cocoa_CreateDefaultCursor()
slouken@5376
    61
{
slouken@6848
    62
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
    63
    NSCursor *nscursor;
slouken@6848
    64
    SDL_Cursor *cursor = NULL;
slouken@5376
    65
slouken@6848
    66
    nscursor = [NSCursor arrowCursor];
slouken@5376
    67
slouken@6848
    68
    if (nscursor) {
slouken@6848
    69
        cursor = SDL_calloc(1, sizeof(*cursor));
slouken@6848
    70
        if (cursor) {
slouken@6848
    71
            cursor->driverdata = nscursor;
slouken@6848
    72
            [nscursor retain];
slouken@5376
    73
        }
slouken@5376
    74
    }
slouken@6848
    75
slouken@6848
    76
    [pool release];
slouken@6848
    77
slouken@6848
    78
    return cursor;
slouken@5376
    79
}
slouken@5376
    80
slouken@5376
    81
static SDL_Cursor *
slouken@5376
    82
Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5376
    83
{
slouken@6848
    84
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
    85
    NSImage *nsimage;
slouken@6848
    86
    NSCursor *nscursor = NULL;
slouken@6848
    87
    SDL_Cursor *cursor = NULL;
slouken@5376
    88
slouken@6848
    89
    nsimage = Cocoa_CreateImage(surface);
slouken@6848
    90
    if (nsimage) {
slouken@6848
    91
        nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
slouken@6848
    92
    }
slouken@6848
    93
slouken@6848
    94
    if (nscursor) {
slouken@6848
    95
        cursor = SDL_calloc(1, sizeof(*cursor));
slouken@6848
    96
        if (cursor) {
slouken@6848
    97
            cursor->driverdata = nscursor;
jorgen@7593
    98
        } else {
jorgen@7593
    99
            [nscursor release];
alexey@6832
   100
        }
slouken@6848
   101
    }
alexey@6832
   102
slouken@6848
   103
    [pool release];
slouken@6848
   104
slouken@6848
   105
    return cursor;
slouken@5376
   106
}
slouken@5376
   107
slouken@6676
   108
static SDL_Cursor *
slouken@6676
   109
Cocoa_CreateSystemCursor(SDL_SystemCursor id)
slouken@6676
   110
{
slouken@6848
   111
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   112
    NSCursor *nscursor = NULL;
slouken@6848
   113
    SDL_Cursor *cursor = NULL;
slouken@6676
   114
slouken@6848
   115
    switch(id)
slouken@6848
   116
    {
slouken@6848
   117
    case SDL_SYSTEM_CURSOR_ARROW:
slouken@6848
   118
        nscursor = [NSCursor arrowCursor];
slouken@6848
   119
        break;
slouken@6848
   120
    case SDL_SYSTEM_CURSOR_IBEAM:
slouken@6848
   121
        nscursor = [NSCursor IBeamCursor];
slouken@6848
   122
        break;
slouken@6848
   123
    case SDL_SYSTEM_CURSOR_WAIT:
slouken@6848
   124
        nscursor = [NSCursor arrowCursor];
slouken@6848
   125
        break;
slouken@6848
   126
    case SDL_SYSTEM_CURSOR_CROSSHAIR:
slouken@6848
   127
        nscursor = [NSCursor crosshairCursor];
slouken@6848
   128
        break;
slouken@6848
   129
    case SDL_SYSTEM_CURSOR_WAITARROW:
slouken@6848
   130
        nscursor = [NSCursor arrowCursor];
slouken@6848
   131
        break;
slouken@6848
   132
    case SDL_SYSTEM_CURSOR_SIZENWSE:
slouken@6848
   133
    case SDL_SYSTEM_CURSOR_SIZENESW:
slouken@6848
   134
        nscursor = [NSCursor closedHandCursor];
slouken@6848
   135
        break;
slouken@6848
   136
    case SDL_SYSTEM_CURSOR_SIZEWE:
slouken@6848
   137
        nscursor = [NSCursor resizeLeftRightCursor];
slouken@6848
   138
        break;
slouken@6848
   139
    case SDL_SYSTEM_CURSOR_SIZENS:
slouken@6848
   140
        nscursor = [NSCursor resizeUpDownCursor];
slouken@6848
   141
        break;
slouken@6848
   142
    case SDL_SYSTEM_CURSOR_SIZEALL:
slouken@6848
   143
        nscursor = [NSCursor closedHandCursor];
slouken@6848
   144
        break;
slouken@6848
   145
    case SDL_SYSTEM_CURSOR_NO:
slouken@6848
   146
        nscursor = [NSCursor operationNotAllowedCursor];
slouken@6848
   147
        break;
slouken@6848
   148
    case SDL_SYSTEM_CURSOR_HAND:
slouken@6848
   149
        nscursor = [NSCursor pointingHandCursor];
slouken@6848
   150
        break;
slouken@6848
   151
    default:
slouken@6848
   152
        SDL_assert(!"Unknown system cursor");
slouken@6848
   153
        return NULL;
slouken@6848
   154
    }
slouken@6848
   155
slouken@6848
   156
    if (nscursor) {
slouken@6848
   157
        cursor = SDL_calloc(1, sizeof(*cursor));
slouken@6848
   158
        if (cursor) {
slouken@7191
   159
            /* We'll free it later, so retain it here */
slouken@6848
   160
            [nscursor retain];
slouken@6848
   161
            cursor->driverdata = nscursor;
alexey@6832
   162
        }
slouken@6848
   163
    }
alexey@6832
   164
slouken@6848
   165
    [pool release];
slouken@6848
   166
slouken@6848
   167
    return cursor;
slouken@6676
   168
}
slouken@6676
   169
slouken@5376
   170
static void
slouken@5376
   171
Cocoa_FreeCursor(SDL_Cursor * cursor)
slouken@5376
   172
{
slouken@6848
   173
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@6848
   174
    NSCursor *nscursor = (NSCursor *)cursor->driverdata;
slouken@5376
   175
slouken@6848
   176
    [nscursor release];
slouken@6848
   177
    SDL_free(cursor);
slouken@6848
   178
slouken@6848
   179
    [pool release];
slouken@5376
   180
}
slouken@5376
   181
slouken@5376
   182
static int
slouken@5376
   183
Cocoa_ShowCursor(SDL_Cursor * cursor)
slouken@5376
   184
{
slouken@6848
   185
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5376
   186
jorgen@7158
   187
    SDL_VideoDevice *device = SDL_GetVideoDevice();
jorgen@7158
   188
    SDL_Window *window = (device ? device->windows : NULL);
jorgen@7158
   189
    for (; window != NULL; window = window->next) {
jorgen@7158
   190
        SDL_WindowData *driverdata = (SDL_WindowData *)window->driverdata;
jorgen@7158
   191
        if (driverdata) {
jorgen@7158
   192
            [driverdata->nswindow performSelectorOnMainThread:@selector(invalidateCursorRectsForView:)
jorgen@7158
   193
                                                   withObject:[driverdata->nswindow contentView]
jorgen@7158
   194
                                                waitUntilDone:NO];
jorgen@7158
   195
        }
slouken@5376
   196
    }
slouken@5376
   197
slouken@6848
   198
    [pool release];
slouken@6848
   199
slouken@5376
   200
    return 0;
slouken@5376
   201
}
slouken@5376
   202
slouken@5376
   203
static void
slouken@5376
   204
Cocoa_WarpMouse(SDL_Window * window, int x, int y)
slouken@5376
   205
{
jorgen@7106
   206
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7465
   207
    CGPoint point = CGPointMake(x, y);
slouken@5376
   208
jorgen@7464
   209
    if (!(window->flags & SDL_WINDOW_FULLSCREEN))
jorgen@7464
   210
    {
jorgen@7464
   211
        point.x += window->x;
jorgen@7464
   212
        point.y += window->y;
jorgen@7464
   213
    }
jorgen@7113
   214
jorgen@7114
   215
    {
jorgen@7114
   216
        /* This makes Cocoa_HandleMouseEvent ignore this delta in the next
jorgen@7114
   217
         * movement event.
jorgen@7114
   218
         */
jorgen@7114
   219
        SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
jorgen@7114
   220
        NSPoint location =  [NSEvent mouseLocation];
jorgen@7114
   221
        driverdata->deltaXOffset = location.x - point.x;
jorgen@7114
   222
        driverdata->deltaYOffset = point.y - location.y;
jorgen@7114
   223
    }
jorgen@7114
   224
jorgen@7113
   225
    /* According to the docs, this was deprecated in 10.6, but it's still
jorgen@7113
   226
     * around. The substitute requires a CGEventSource, but I'm not entirely
jorgen@7113
   227
     * sure how we'd procure the right one for this event.
jorgen@7113
   228
     */
jorgen@7113
   229
    CGSetLocalEventsSuppressionInterval(0.0);
slouken@5376
   230
    CGWarpMouseCursorPosition(point);
jorgen@7113
   231
    CGSetLocalEventsSuppressionInterval(0.25);
jorgen@7106
   232
jorgen@7271
   233
    if (!mouse->relative_mode) {
jorgen@7271
   234
        /* CGWarpMouseCursorPosition doesn't generate a window event, unlike our
jorgen@7271
   235
         * other implementations' APIs.
jorgen@7271
   236
         */
jorgen@7271
   237
        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x, y);
jorgen@7271
   238
    }
slouken@5376
   239
}
slouken@5376
   240
slouken@5406
   241
static int
slouken@5406
   242
Cocoa_SetRelativeMouseMode(SDL_bool enabled)
slouken@5406
   243
{
slouken@5406
   244
    CGError result;
slouken@5406
   245
slouken@5406
   246
    if (enabled) {
slouken@5406
   247
        result = CGAssociateMouseAndMouseCursorPosition(NO);
slouken@5406
   248
    } else {
slouken@5406
   249
        result = CGAssociateMouseAndMouseCursorPosition(YES);
slouken@5406
   250
    }
slouken@5406
   251
    if (result != kCGErrorSuccess) {
icculus@7037
   252
        return SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
slouken@5406
   253
    }
slouken@5406
   254
    return 0;
slouken@5406
   255
}
slouken@5406
   256
slouken@1931
   257
void
slouken@1931
   258
Cocoa_InitMouse(_THIS)
slouken@1931
   259
{
slouken@5376
   260
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5376
   261
jorgen@7114
   262
    mouse->driverdata = SDL_calloc(1, sizeof(SDL_MouseData));
jorgen@7114
   263
slouken@5376
   264
    mouse->CreateCursor = Cocoa_CreateCursor;
slouken@6676
   265
    mouse->CreateSystemCursor = Cocoa_CreateSystemCursor;
slouken@5376
   266
    mouse->ShowCursor = Cocoa_ShowCursor;
slouken@5406
   267
    mouse->FreeCursor = Cocoa_FreeCursor;
slouken@5376
   268
    mouse->WarpMouse = Cocoa_WarpMouse;
slouken@5406
   269
    mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
slouken@5376
   270
slouken@5405
   271
    SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
jorgen@7593
   272
jorgen@7593
   273
    Cocoa_InitMouseEventTap(mouse->driverdata);
slouken@1931
   274
}
slouken@1931
   275
slouken@3517
   276
void
slouken@3517
   277
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
slouken@3517
   278
{
slouken@5406
   279
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5406
   280
slouken@5477
   281
    if (mouse->relative_mode &&
slouken@5477
   282
        ([event type] == NSMouseMoved ||
slouken@5477
   283
         [event type] == NSLeftMouseDragged ||
slouken@5477
   284
         [event type] == NSRightMouseDragged ||
slouken@5477
   285
         [event type] == NSOtherMouseDragged)) {
jorgen@7114
   286
        SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
jorgen@7114
   287
        float x = [event deltaX] + driverdata->deltaXOffset;
jorgen@7114
   288
        float y = [event deltaY] + driverdata->deltaYOffset;
jorgen@7114
   289
        driverdata->deltaXOffset = driverdata->deltaYOffset = 0;
jorgen@7114
   290
slouken@6950
   291
        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)x, (int)y);
slouken@5406
   292
    }
slouken@3517
   293
}
slouken@3517
   294
slouken@1931
   295
void
gzjjgod@5057
   296
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
gzjjgod@5057
   297
{
slouken@6950
   298
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@6950
   299
gzjjgod@5057
   300
    float x = [event deltaX];
gzjjgod@5057
   301
    float y = [event deltaY];
gzjjgod@5057
   302
gzjjgod@5057
   303
    if (x > 0) {
gzjjgod@5057
   304
        x += 0.9f;
gzjjgod@5057
   305
    } else if (x < 0) {
gzjjgod@5057
   306
        x -= 0.9f;
gzjjgod@5057
   307
    }
gzjjgod@5057
   308
    if (y > 0) {
gzjjgod@5057
   309
        y += 0.9f;
gzjjgod@5057
   310
    } else if (y < 0) {
gzjjgod@5057
   311
        y -= 0.9f;
gzjjgod@5057
   312
    }
slouken@6950
   313
    SDL_SendMouseWheel(window, mouse->mouseID, (int)x, (int)y);
gzjjgod@5057
   314
}
gzjjgod@5057
   315
slouken@5058
   316
void
slouken@5058
   317
Cocoa_QuitMouse(_THIS)
slouken@5058
   318
{
jorgen@7114
   319
    SDL_Mouse *mouse = SDL_GetMouse();
jorgen@7114
   320
    if (mouse) {
jorgen@7593
   321
        if (mouse->driverdata) {
jorgen@7593
   322
            Cocoa_QuitMouseEventTap(((SDL_MouseData*)mouse->driverdata));
jorgen@7593
   323
        }
jorgen@7593
   324
jorgen@7114
   325
        SDL_free(mouse->driverdata);
jorgen@7114
   326
    }
slouken@5058
   327
}
slouken@5058
   328
slouken@6044
   329
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   330
slouken@1931
   331
/* vi: set ts=4 sw=4 expandtab: */