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