src/video/cocoa/SDL_cocoamouse.m
author Wim Looman <ghostunderscore@gmail.com>
Fri, 03 Feb 2012 23:53:51 +1300
changeset 6301 e8a69c5378e7
parent 6138 4c64952a58fb
child 6676 4ff261ffaa42
permissions -rwxr-xr-x
Change SDL_SetCursor to set the cursor back to the default cursor when the window is unfocused.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
     2
  Simple DirectMedia Layer
6138
4c64952a58fb Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6044
diff changeset
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
     7
  arising from the use of this software.
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
    11
  freely, subject to the following restrictions:
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5477
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
#include "SDL_config.h"
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    23
#if SDL_VIDEO_DRIVER_COCOA
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
    24
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    25
#include "SDL_events.h"
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
#include "SDL_cocoavideo.h"
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
#include "../../events/SDL_mouse_c.h"
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    30
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    31
static SDL_Cursor *
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    32
Cocoa_CreateDefaultCursor()
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    33
{
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    34
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    35
    NSCursor *nscursor;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    36
    SDL_Cursor *cursor = NULL;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    37
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    38
    nscursor = [NSCursor arrowCursor];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    39
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    40
    if (nscursor) {
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    41
        cursor = SDL_calloc(1, sizeof(*cursor));
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    42
        if (cursor) {
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    43
            cursor->driverdata = nscursor;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    44
            [nscursor retain];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    45
        }
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    46
    }
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    47
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    48
    [pool release];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    49
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    50
    return cursor;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    51
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    52
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    53
static SDL_Cursor *
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    54
Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    55
{
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    56
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    57
    NSImage *nsimage;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    58
    NSCursor *nscursor = NULL;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    59
    SDL_Cursor *cursor = NULL;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    60
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    61
    nsimage = Cocoa_CreateImage(surface);
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    62
    if (nsimage) {
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    63
        nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    64
    }
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    65
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    66
    if (nscursor) {
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    67
        cursor = SDL_calloc(1, sizeof(*cursor));
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    68
        if (cursor) {
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    69
            cursor->driverdata = nscursor;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    70
        }
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    71
    }
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    72
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    73
    [pool release];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    74
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    75
    return cursor;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    76
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    77
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    78
static void
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    79
Cocoa_FreeCursor(SDL_Cursor * cursor)
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    80
{
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    81
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    82
    NSCursor *nscursor = (NSCursor *)cursor->driverdata;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    83
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    84
    [nscursor release];
5419
210b0abe9e44 Fixed memory leak freeing cursors
Sam Lantinga <slouken@libsdl.org>
parents: 5406
diff changeset
    85
    SDL_free(cursor);
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    86
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    87
    [pool release];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    88
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    89
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    90
static int
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    91
Cocoa_ShowCursor(SDL_Cursor * cursor)
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    92
{
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    93
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    94
6301
e8a69c5378e7 Change SDL_SetCursor to set the cursor back to the default cursor when the
Wim Looman <ghostunderscore@gmail.com>
parents: 6138
diff changeset
    95
    if (cursor) {
e8a69c5378e7 Change SDL_SetCursor to set the cursor back to the default cursor when the
Wim Looman <ghostunderscore@gmail.com>
parents: 6138
diff changeset
    96
        NSCursor *nscursor = (NSCursor *)cursor->driverdata;
5387
03c79d46bf5c Actually set the cursor! :)
Sam Lantinga <slouken@libsdl.org>
parents: 5376
diff changeset
    97
6301
e8a69c5378e7 Change SDL_SetCursor to set the cursor back to the default cursor when the
Wim Looman <ghostunderscore@gmail.com>
parents: 6138
diff changeset
    98
        [nscursor set];
e8a69c5378e7 Change SDL_SetCursor to set the cursor back to the default cursor when the
Wim Looman <ghostunderscore@gmail.com>
parents: 6138
diff changeset
    99
        [NSCursor unhide];
e8a69c5378e7 Change SDL_SetCursor to set the cursor back to the default cursor when the
Wim Looman <ghostunderscore@gmail.com>
parents: 6138
diff changeset
   100
    } else {
e8a69c5378e7 Change SDL_SetCursor to set the cursor back to the default cursor when the
Wim Looman <ghostunderscore@gmail.com>
parents: 6138
diff changeset
   101
        [NSCursor hide];
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   102
    }
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   103
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   104
    [pool release];
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   105
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   106
    return 0;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   107
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   108
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   109
static void
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   110
Cocoa_WarpMouse(SDL_Window * window, int x, int y)
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   111
{
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   112
    CGPoint point;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   113
5391
06b239f48b91 Fixed compile error on some versions of Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5387
diff changeset
   114
    point.x = (float)window->x + x;
06b239f48b91 Fixed compile error on some versions of Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5387
diff changeset
   115
    point.y = (float)window->y + y;
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   116
    CGWarpMouseCursorPosition(point);
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   117
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   118
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   119
static int
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   120
Cocoa_SetRelativeMouseMode(SDL_bool enabled)
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   121
{
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   122
    CGError result;
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   123
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   124
    if (enabled) {
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   125
        result = CGAssociateMouseAndMouseCursorPosition(NO);
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   126
    } else {
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   127
        result = CGAssociateMouseAndMouseCursorPosition(YES);
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   128
    }
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   129
    if (result != kCGErrorSuccess) {
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   130
        SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   131
        return -1;
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   132
    }
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   133
    return 0;
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   134
}
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   135
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   136
void
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   137
Cocoa_InitMouse(_THIS)
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   138
{
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   139
    SDL_Mouse *mouse = SDL_GetMouse();
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   140
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   141
    mouse->CreateCursor = Cocoa_CreateCursor;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   142
    mouse->ShowCursor = Cocoa_ShowCursor;
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   143
    mouse->FreeCursor = Cocoa_FreeCursor;
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   144
    mouse->WarpMouse = Cocoa_WarpMouse;
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   145
    mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   146
5405
64fa8526e1ce Added a cleaner way to set the default cursor.
Sam Lantinga <slouken@libsdl.org>
parents: 5391
diff changeset
   147
    SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
}
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   150
void
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   151
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   152
{
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   153
    SDL_Mouse *mouse = SDL_GetMouse();
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   154
5477
50cd1fb880e5 Fixed bug 1166 (No mouse motion events while SDL_SetRelativeMouseMode() is active and holding the mouse button)
Sam Lantinga <slouken@libsdl.org>
parents: 5419
diff changeset
   155
    if (mouse->relative_mode &&
50cd1fb880e5 Fixed bug 1166 (No mouse motion events while SDL_SetRelativeMouseMode() is active and holding the mouse button)
Sam Lantinga <slouken@libsdl.org>
parents: 5419
diff changeset
   156
        ([event type] == NSMouseMoved ||
50cd1fb880e5 Fixed bug 1166 (No mouse motion events while SDL_SetRelativeMouseMode() is active and holding the mouse button)
Sam Lantinga <slouken@libsdl.org>
parents: 5419
diff changeset
   157
         [event type] == NSLeftMouseDragged ||
50cd1fb880e5 Fixed bug 1166 (No mouse motion events while SDL_SetRelativeMouseMode() is active and holding the mouse button)
Sam Lantinga <slouken@libsdl.org>
parents: 5419
diff changeset
   158
         [event type] == NSRightMouseDragged ||
50cd1fb880e5 Fixed bug 1166 (No mouse motion events while SDL_SetRelativeMouseMode() is active and holding the mouse button)
Sam Lantinga <slouken@libsdl.org>
parents: 5419
diff changeset
   159
         [event type] == NSOtherMouseDragged)) {
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   160
        float x = [event deltaX];
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   161
        float y = [event deltaY];
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   162
        SDL_SendMouseMotion(mouse->focus, 1, (int)x, (int)y);
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   163
    }
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   164
}
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   165
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
void
5057
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   167
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   168
{
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   169
    float x = [event deltaX];
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   170
    float y = [event deltaY];
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   171
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   172
    if (x > 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   173
        x += 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   174
    } else if (x < 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   175
        x -= 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   176
    }
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   177
    if (y > 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   178
        y += 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   179
    } else if (y < 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   180
        y -= 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   181
    }
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   182
    SDL_SendMouseWheel(window, (int)x, (int)y);
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   183
}
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   184
5058
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   185
void
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   186
Cocoa_QuitMouse(_THIS)
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   187
{
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   188
}
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   189
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   190
#endif /* SDL_VIDEO_DRIVER_COCOA */
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   191
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
/* vi: set ts=4 sw=4 expandtab: */