src/video/cocoa/SDL_cocoamouse.m
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Tue, 04 Jun 2013 14:54:49 -0700
changeset 7271 963d2ba35cfe
parent 7191 75360622e65f
child 7464 7cdeb64faa72
permissions -rw-r--r--
Mac: Fix incorrect relative jump on focus / start. We should no longer send huge jumps on relative mode focus changes if the cursor was moved around, or on initial start. Fixes http://bugzilla.libsdl.org/show_bug.cgi?id=1836
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
7158
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    31
@implementation NSCursor (InvisibleCursor)
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    32
+ (NSCursor *)invisibleCursor
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    33
{
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    34
    static NSCursor *invisibleCursor = NULL;
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    35
    if (!invisibleCursor) {
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    36
        /* RAW 16x16 transparent GIF */
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    37
        static unsigned char cursorBytes[] = {
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    38
            0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x10, 0x00, 0x10, 0x00, 0x80,
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    39
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, 0xF9, 0x04,
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    40
            0x01, 0x00, 0x00, 0x01, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00, 0x10,
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    41
            0x00, 0x10, 0x00, 0x00, 0x02, 0x0E, 0x8C, 0x8F, 0xA9, 0xCB, 0xED,
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    42
            0x0F, 0xA3, 0x9C, 0xB4, 0xDA, 0x8B, 0xB3, 0x3E, 0x05, 0x00, 0x3B
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    43
        };
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    44
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    45
        NSData *cursorData = [NSData dataWithBytesNoCopy:&cursorBytes[0]
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    46
                                                  length:sizeof(cursorBytes)
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    47
                                            freeWhenDone:NO];
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    48
        NSImage *cursorImage = [[[NSImage alloc] initWithData:cursorData] autorelease];
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    49
        invisibleCursor = [[NSCursor alloc] initWithImage:cursorImage
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    50
                                                  hotSpot:NSZeroPoint];
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    51
    }
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    52
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    53
    return invisibleCursor;
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    54
}
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    55
@end
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
    56
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    57
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    58
static SDL_Cursor *
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    59
Cocoa_CreateDefaultCursor()
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    60
{
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
    61
    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
    62
    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
    63
    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
    64
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
    65
    nscursor = [NSCursor arrowCursor];
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;
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
    71
            [nscursor retain];
5376
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
    }
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
    74
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
    [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
    76
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
    77
    return cursor;
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    78
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    79
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    80
static SDL_Cursor *
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    81
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
    82
{
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
    83
    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
    84
    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
    85
    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
    86
    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
    87
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
    88
    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
    89
    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
    90
        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
    91
    }
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    92
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
    93
    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
    94
        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
    95
        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
    96
            cursor->driverdata = nscursor;
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    97
        }
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
    98
    }
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
    99
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
    [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
   101
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
    return cursor;
5376
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
6676
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
   105
static SDL_Cursor *
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
   106
Cocoa_CreateSystemCursor(SDL_SystemCursor id)
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
   107
{
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
   108
    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
   109
    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
   110
    SDL_Cursor *cursor = NULL;
6676
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
   111
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
   112
    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
   113
    {
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
    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
   115
        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
   116
        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
   117
    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
   118
        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
   119
        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
   120
    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
   121
        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
   122
        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
   123
    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
   124
        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
   125
        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
   126
    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
   127
        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
   128
        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
   129
    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
   130
    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
   131
        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
   132
        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
   133
    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
   134
        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
   135
        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
   136
    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
   137
        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
   138
        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
   139
    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
   140
        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
   141
        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
   142
    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
   143
        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
   144
        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
   145
    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
   146
        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
   147
        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
   148
    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
   149
        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
   150
        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
   151
    }
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
   152
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
   153
    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
   154
        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
   155
        if (cursor) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7158
diff changeset
   156
            /* We'll free it later, so retain it here */
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
   157
            [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
   158
            cursor->driverdata = nscursor;
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6676
diff changeset
   159
        }
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
    }
6832
156e608ec4ef Replaced manual NSAutoreleasePool handing with @autorelease
stopiccot <alexey.petruchik@gmail.com>
parents: 6676
diff changeset
   161
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
   162
    [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
   163
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
   164
    return cursor;
6676
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
   165
}
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
   166
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   167
static void
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   168
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
   169
{
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
   170
    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
   171
    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
   172
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
   173
    [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
   174
    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
   175
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
   176
    [pool release];
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   177
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   178
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   179
static int
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   180
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
   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
    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
   183
7158
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
   184
    SDL_VideoDevice *device = SDL_GetVideoDevice();
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
   185
    SDL_Window *window = (device ? device->windows : NULL);
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
   186
    for (; window != NULL; window = window->next) {
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
   187
        SDL_WindowData *driverdata = (SDL_WindowData *)window->driverdata;
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
   188
        if (driverdata) {
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
   189
            [driverdata->nswindow performSelectorOnMainThread:@selector(invalidateCursorRectsForView:)
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
   190
                                                   withObject:[driverdata->nswindow contentView]
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
   191
                                                waitUntilDone:NO];
ff52fba70795 Mac: Use cursor rects instead of NSCursor hide/unhide.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7114
diff changeset
   192
        }
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   193
    }
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   194
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
   195
    [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
   196
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   197
    return 0;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   198
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   199
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   200
static void
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   201
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
   202
{
7106
31f8acac196b Mac: Update mouse position on warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7100
diff changeset
   203
    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
   204
    CGPoint point;
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   205
5391
06b239f48b91 Fixed compile error on some versions of Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5387
diff changeset
   206
    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
   207
    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
   208
7114
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   209
    {
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   210
        /* 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
   211
         * movement event.
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   212
         */
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   213
        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
   214
        NSPoint location =  [NSEvent mouseLocation];
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   215
        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
   216
        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
   217
    }
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   218
7113
7b4b596b3cfb Mac: Don't supress mousemoves after warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7106
diff changeset
   219
    /* 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
   220
     * 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
   221
     * 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
   222
     */
7b4b596b3cfb Mac: Don't supress mousemoves after warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7106
diff changeset
   223
    CGSetLocalEventsSuppressionInterval(0.0);
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   224
    CGWarpMouseCursorPosition(point);
7113
7b4b596b3cfb Mac: Don't supress mousemoves after warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7106
diff changeset
   225
    CGSetLocalEventsSuppressionInterval(0.25);
7106
31f8acac196b Mac: Update mouse position on warp.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7100
diff changeset
   226
7271
963d2ba35cfe Mac: Fix incorrect relative jump on focus / start.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   227
    if (!mouse->relative_mode) {
963d2ba35cfe Mac: Fix incorrect relative jump on focus / start.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   228
        /* CGWarpMouseCursorPosition doesn't generate a window event, unlike our
963d2ba35cfe Mac: Fix incorrect relative jump on focus / start.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   229
         * other implementations' APIs.
963d2ba35cfe Mac: Fix incorrect relative jump on focus / start.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   230
         */
963d2ba35cfe Mac: Fix incorrect relative jump on focus / start.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   231
        SDL_SendMouseMotion(mouse->focus, mouse->mouseID, 0, x, y);
963d2ba35cfe Mac: Fix incorrect relative jump on focus / start.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7191
diff changeset
   232
    }
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   233
}
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   234
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   235
static int
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   236
Cocoa_SetRelativeMouseMode(SDL_bool enabled)
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   237
{
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   238
    CGError result;
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   239
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   240
    if (enabled) {
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   241
        result = CGAssociateMouseAndMouseCursorPosition(NO);
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   242
    } else {
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   243
        result = CGAssociateMouseAndMouseCursorPosition(YES);
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   244
    }
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   245
    if (result != kCGErrorSuccess) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 6950
diff changeset
   246
        return SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   247
    }
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   248
    return 0;
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   249
}
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   250
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   251
void
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   252
Cocoa_InitMouse(_THIS)
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   253
{
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   254
    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
   255
7114
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   256
    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
   257
5376
183ec2d4485c Implemented cursor support and SDL_WarpMouseInWindow() on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 5371
diff changeset
   258
    mouse->CreateCursor = Cocoa_CreateCursor;
6676
4ff261ffaa42 Added a Cocoa implementation of SDL_CreateSystemCursor()
Sam Lantinga <slouken@libsdl.org>
parents: 6301
diff changeset
   259
    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
   260
    mouse->ShowCursor = Cocoa_ShowCursor;
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   261
    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
   262
    mouse->WarpMouse = Cocoa_WarpMouse;
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   263
    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
   264
5405
64fa8526e1ce Added a cleaner way to set the default cursor.
Sam Lantinga <slouken@libsdl.org>
parents: 5391
diff changeset
   265
    SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   266
}
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   267
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   268
void
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   269
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
   270
{
5406
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   271
    SDL_Mouse *mouse = SDL_GetMouse();
1517da4ab6b1 Implemented mouse relative mode on Mac OS X.
Sam Lantinga <slouken@libsdl.org>
parents: 5405
diff changeset
   272
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
   273
    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
   274
        ([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
   275
         [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
   276
         [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
   277
         [event type] == NSOtherMouseDragged)) {
7114
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   278
        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
   279
        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
   280
        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
   281
        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
   282
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
   283
        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
   284
    }
3517
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   285
}
e7eec78e4b92 Fixed mouse events for fullscreen windows on Mac OS X
Sam Lantinga <slouken@libsdl.org>
parents: 2940
diff changeset
   286
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   287
void
5057
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   288
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
   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_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
   291
5057
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   292
    float x = [event deltaX];
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   293
    float y = [event deltaY];
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   294
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   295
    if (x > 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   296
        x += 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   297
    } else if (x < 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   298
        x -= 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   299
    }
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   300
    if (y > 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   301
        y += 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   302
    } else if (y < 0) {
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   303
        y -= 0.9f;
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   304
    }
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
   305
    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
   306
}
bdff53ed6c8b Fix mouse wheel events in fullscreen mode for OS X
Jjgod Jiang <gzjjgod@gmail.com>
parents: 4498
diff changeset
   307
5058
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   308
void
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   309
Cocoa_QuitMouse(_THIS)
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   310
{
7114
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   311
    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
   312
    if (mouse) {
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   313
        SDL_free(mouse->driverdata);
02b2fe147478 Mac: Fix relative mode message after gaining focus.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7113
diff changeset
   314
    }
5058
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   315
}
03db7d4b02b4 Moved function for consistency
Sam Lantinga <slouken@libsdl.org>
parents: 5057
diff changeset
   316
6044
35448a5ea044 Lots of fixes importing SDL source wholesale into a new iOS project
Sam Lantinga <slouken@libsdl.org>
parents: 5535
diff changeset
   317
#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
   318
1931
103c6fec2a60 The Mac OS X Cocoa video driver is under construction...
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   319
/* vi: set ts=4 sw=4 expandtab: */