src/video/cocoa/SDL_cocoamouse.m
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Thu, 25 Apr 2013 18:40:29 -0700
changeset 7114 02b2fe147478
parent 7113 7b4b596b3cfb
child 7158 ff52fba70795
permissions -rw-r--r--
Mac: Fix relative mode message after gaining focus. This fixes a bug where relative mode would give a large jump if the cursor was moved when the app doesn't have focus.
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
6885
700f1b25f77f Happy New Year!
Sam Lantinga <slouken@libsdl.org>
parents: 6848
diff changeset
     3
  Copyright (C) 1997-2013 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
6676
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
    25
#include "SDL_assert.h"
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
    26
#include "SDL_events.h"
7114
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
    27
#include "SDL_cocoamouse.h"
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
#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
    30
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    31
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    32
static SDL_Cursor *
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    33
Cocoa_CreateDefaultCursor()
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    34
{
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    35
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    36
    NSCursor *nscursor;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    37
    SDL_Cursor *cursor = NULL;
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    38
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    39
    nscursor = [NSCursor arrowCursor];
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    40
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    41
    if (nscursor) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    42
        cursor = SDL_calloc(1, sizeof(*cursor));
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    43
        if (cursor) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    44
            cursor->driverdata = nscursor;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    45
            [nscursor retain];
5376
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
    }
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    48
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    49
    [pool release];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    50
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    51
    return cursor;
5376
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
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    54
static SDL_Cursor *
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    55
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
    56
{
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    57
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    58
    NSImage *nsimage;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    59
    NSCursor *nscursor = NULL;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    60
    SDL_Cursor *cursor = NULL;
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    61
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    62
    nsimage = Cocoa_CreateImage(surface);
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    63
    if (nsimage) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    64
        nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    65
    }
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    66
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    67
    if (nscursor) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    68
        cursor = SDL_calloc(1, sizeof(*cursor));
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    69
        if (cursor) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    70
            cursor->driverdata = nscursor;
5376
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
    }
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    73
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    74
    [pool release];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    75
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    76
    return cursor;
5376
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
6676
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
    79
static SDL_Cursor *
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
    80
Cocoa_CreateSystemCursor(SDL_SystemCursor id)
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
    81
{
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    82
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    83
    NSCursor *nscursor = NULL;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    84
    SDL_Cursor *cursor = NULL;
6676
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
    85
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    86
    switch(id)
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    87
    {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    88
    case SDL_SYSTEM_CURSOR_ARROW:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    89
        nscursor = [NSCursor arrowCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    90
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    91
    case SDL_SYSTEM_CURSOR_IBEAM:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    92
        nscursor = [NSCursor IBeamCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    93
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    94
    case SDL_SYSTEM_CURSOR_WAIT:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    95
        nscursor = [NSCursor arrowCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    96
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    97
    case SDL_SYSTEM_CURSOR_CROSSHAIR:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    98
        nscursor = [NSCursor crosshairCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
    99
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   100
    case SDL_SYSTEM_CURSOR_WAITARROW:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   101
        nscursor = [NSCursor arrowCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   102
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   103
    case SDL_SYSTEM_CURSOR_SIZENWSE:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   104
    case SDL_SYSTEM_CURSOR_SIZENESW:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   105
        nscursor = [NSCursor closedHandCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   106
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   107
    case SDL_SYSTEM_CURSOR_SIZEWE:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   108
        nscursor = [NSCursor resizeLeftRightCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   109
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   110
    case SDL_SYSTEM_CURSOR_SIZENS:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   111
        nscursor = [NSCursor resizeUpDownCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   112
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   113
    case SDL_SYSTEM_CURSOR_SIZEALL:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   114
        nscursor = [NSCursor closedHandCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   115
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   116
    case SDL_SYSTEM_CURSOR_NO:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   117
        nscursor = [NSCursor operationNotAllowedCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   118
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   119
    case SDL_SYSTEM_CURSOR_HAND:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   120
        nscursor = [NSCursor pointingHandCursor];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   121
        break;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   122
    default:
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   123
        SDL_assert(!"Unknown system cursor");
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   124
        return NULL;
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   125
    }
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   126
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   127
    if (nscursor) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   128
        cursor = SDL_calloc(1, sizeof(*cursor));
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   129
        if (cursor) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   130
            // We'll free it later, so retain it here
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   131
            [nscursor retain];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   132
            cursor->driverdata = nscursor;
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6676
diff changeset
   133
        }
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   134
    }
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6676
diff changeset
   135
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   136
    [pool release];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   137
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   138
    return cursor;
6676
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
   139
}
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
   140
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   141
static void
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   142
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
   143
{
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   144
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   145
    NSCursor *nscursor = (NSCursor *)cursor->driverdata;
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   146
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   147
    [nscursor release];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   148
    SDL_free(cursor);
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   149
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   150
    [pool release];
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   151
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   152
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   153
static int
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   154
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
   155
{
7099
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   156
	/* We need to track the previous state because hide and unhide calls need to
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   157
	 * be matched, but ShowCursor calls don't.
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   158
	 */
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   159
	static SDL_bool isShown = SDL_TRUE;
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   160
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   161
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   162
    if (cursor) {
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   163
        NSCursor *nscursor = (NSCursor *)cursor->driverdata;
5387
03c79d46bf5c Actually set the cursor! :)
Sam Lantinga <slouken@libsdl.org>
parents: 5376
diff changeset
   164
7100
ffd910de700b Mac: Fix cursor not updating when re-focusing the window.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7099
diff changeset
   165
        /* We're possibly executing from an event handler where this operation
ffd910de700b Mac: Fix cursor not updating when re-focusing the window.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7099
diff changeset
   166
         * is unsupported. This will execute it in the main Cocoa event loop
ffd910de700b Mac: Fix cursor not updating when re-focusing the window.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7099
diff changeset
   167
         * after this returns.
ffd910de700b Mac: Fix cursor not updating when re-focusing the window.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7099
diff changeset
   168
         */
ffd910de700b Mac: Fix cursor not updating when re-focusing the window.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7099
diff changeset
   169
        [nscursor performSelectorOnMainThread:@selector(set)
ffd910de700b Mac: Fix cursor not updating when re-focusing the window.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7099
diff changeset
   170
                                   withObject:nil
ffd910de700b Mac: Fix cursor not updating when re-focusing the window.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7099
diff changeset
   171
                                waitUntilDone:NO];
7099
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   172
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   173
		if (!isShown) {
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   174
			[NSCursor unhide];
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   175
			isShown = SDL_TRUE;
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   176
		}
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   177
	} else if (isShown) {
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   178
		[NSCursor hide];
54f187c036e2 Mac: Fix unmatched hide/show cursor calls.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7037
diff changeset
   179
		isShown = SDL_FALSE;
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   180
    }
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   181
6848
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   182
    [pool release];
478ecc8a58b3 Backed out use of @autorelease keyword for now, since it's not supported by older Xcode versions.
Sam Lantinga <slouken@libsdl.org>
parents: 6832
diff changeset
   183
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   184
    return 0;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   185
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   186
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   187
static void
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   188
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
   189
{
7106
31f8acac196b Mac: Update mouse position on warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7100
diff changeset
   190
    SDL_Mouse *mouse = SDL_GetMouse();
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   191
    CGPoint point;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   192
5391
06b239f48b91 Fixed compile error on some versions of Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5387
diff changeset
   193
    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
   194
    point.y = (float)window->y + y;
7113
7b4b596b3cfb Mac: Don't supress mousemoves after warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7106
diff changeset
   195
7114
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   196
    {
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   197
        /* This makes Cocoa_HandleMouseEvent ignore this delta in the next
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   198
         * movement event.
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   199
         */
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   200
        SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   201
        NSPoint location =  [NSEvent mouseLocation];
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   202
        driverdata->deltaXOffset = location.x - point.x;
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   203
        driverdata->deltaYOffset = point.y - location.y;
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   204
    }
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   205
7113
7b4b596b3cfb Mac: Don't supress mousemoves after warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7106
diff changeset
   206
    /* According to the docs, this was deprecated in 10.6, but it's still
7b4b596b3cfb Mac: Don't supress mousemoves after warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7106
diff changeset
   207
     * around. The substitute requires a CGEventSource, but I'm not entirely
7b4b596b3cfb Mac: Don't supress mousemoves after warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7106
diff changeset
   208
     * sure how we'd procure the right one for this event.
7b4b596b3cfb Mac: Don't supress mousemoves after warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7106
diff changeset
   209
     */
7b4b596b3cfb Mac: Don't supress mousemoves after warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7106
diff changeset
   210
    CGSetLocalEventsSuppressionInterval(0.0);
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   211
    CGWarpMouseCursorPosition(point);
7113
7b4b596b3cfb Mac: Don't supress mousemoves after warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7106
diff changeset
   212
    CGSetLocalEventsSuppressionInterval(0.25);
7106
31f8acac196b Mac: Update mouse position on warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7100
diff changeset
   213
31f8acac196b Mac: Update mouse position on warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7100
diff changeset
   214
    /* CGWarpMouseCursorPosition doesn't generate a window event, unlike our
31f8acac196b Mac: Update mouse position on warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7100
diff changeset
   215
     * other implementations' APIs.
31f8acac196b Mac: Update mouse position on warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7100
diff changeset
   216
     */
31f8acac196b Mac: Update mouse position on warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7100
diff changeset
   217
    SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x, y);
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   218
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   219
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   220
static int
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   221
Cocoa_SetRelativeMouseMode(SDL_bool enabled)
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   222
{
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   223
    CGError result;
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   224
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   225
    if (enabled) {
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   226
        result = CGAssociateMouseAndMouseCursorPosition(NO);
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   227
    } else {
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   228
        result = CGAssociateMouseAndMouseCursorPosition(YES);
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   229
    }
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   230
    if (result != kCGErrorSuccess) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6950
diff changeset
   231
        return SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   232
    }
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   233
    return 0;
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   234
}
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   235
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   236
void
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   237
Cocoa_InitMouse(_THIS)
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   238
{
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   239
    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
   240
7114
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   241
    mouse->driverdata = SDL_calloc(1, sizeof(SDL_MouseData));
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   242
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   243
    mouse->CreateCursor = Cocoa_CreateCursor;
6676
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
   244
    mouse->CreateSystemCursor = Cocoa_CreateSystemCursor;
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   245
    mouse->ShowCursor = Cocoa_ShowCursor;
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   246
    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
   247
    mouse->WarpMouse = Cocoa_WarpMouse;
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   248
    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
   249
5405
64fa8526e1ce Added a cleaner way to set the default cursor.
Sam Lantinga <slouken@libsdl.org>
parents: 5391
diff changeset
   250
    SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
}
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   253
void
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   254
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
   255
{
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   256
    SDL_Mouse *mouse = SDL_GetMouse();
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   257
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
   258
    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
   259
        ([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
   260
         [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
   261
         [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
   262
         [event type] == NSOtherMouseDragged)) {
7114
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   263
        SDL_MouseData *driverdata = (SDL_MouseData*)mouse->driverdata;
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   264
        float x = [event deltaX] + driverdata->deltaXOffset;
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   265
        float y = [event deltaY] + driverdata->deltaYOffset;
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   266
        driverdata->deltaXOffset = driverdata->deltaYOffset = 0;
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   267
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   268
        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 1, (int)x, (int)y);
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   269
    }
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   270
}
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   271
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   272
void
5057
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   273
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
   274
{
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   275
    SDL_Mouse *mouse = SDL_GetMouse();
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   276
5057
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   277
    float x = [event deltaX];
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   278
    float y = [event deltaY];
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   279
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   280
    if (x > 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   281
        x += 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   282
    } else if (x < 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   283
        x -= 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   284
    }
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   285
    if (y > 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   286
        y += 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   287
    } else if (y < 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   288
        y -= 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   289
    }
6950
1ddb72193079 Added a mouse ID to the mouse events, which set to the special value SDL_TOUCH_MOUSEID for mouse events simulated by touch input.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   290
    SDL_SendMouseWheel(window, mouse->mouseID, (int)x, (int)y);
5057
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   291
}
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   292
5058
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   293
void
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   294
Cocoa_QuitMouse(_THIS)
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   295
{
7114
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   296
    SDL_Mouse *mouse = SDL_GetMouse();
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   297
    if (mouse) {
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   298
        SDL_free(mouse->driverdata);
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   299
    }
5058
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   300
}
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   301
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   302
#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
   303
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   304
/* vi: set ts=4 sw=4 expandtab: */