src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 27 Feb 2011 22:06:46 -0800
changeset 5406 1517da4ab6b1
parent 5405 64fa8526e1ce
child 5419 210b0abe9e44
permissions -rw-r--r--
Implemented mouse relative mode on Mac OS X.
slouken@1931
     1
/*
slouken@1931
     2
    SDL - Simple DirectMedia Layer
slouken@5262
     3
    Copyright (C) 1997-2011 Sam Lantinga
slouken@1931
     4
slouken@1931
     5
    This library is free software; you can redistribute it and/or
slouken@1931
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1931
     7
    License as published by the Free Software Foundation; either
slouken@1931
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1931
     9
slouken@1931
    10
    This library is distributed in the hope that it will be useful,
slouken@1931
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1931
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1931
    13
    Lesser General Public License for more details.
slouken@1931
    14
slouken@1931
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1931
    16
    License along with this library; if not, write to the Free Software
slouken@1931
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1931
    18
slouken@1931
    19
    Sam Lantinga
slouken@1931
    20
    slouken@libsdl.org
slouken@1931
    21
*/
slouken@1931
    22
#include "SDL_config.h"
slouken@1931
    23
slouken@3517
    24
#include "SDL_events.h"
slouken@1931
    25
#include "SDL_cocoavideo.h"
slouken@1931
    26
slouken@1931
    27
#include "../../events/SDL_mouse_c.h"
slouken@1931
    28
slouken@5376
    29
slouken@5376
    30
static SDL_Cursor *
slouken@5376
    31
Cocoa_CreateDefaultCursor()
slouken@5376
    32
{
slouken@5376
    33
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5376
    34
    NSCursor *nscursor;
slouken@5376
    35
    SDL_Cursor *cursor = NULL;
slouken@5376
    36
slouken@5376
    37
    nscursor = [NSCursor arrowCursor];
slouken@5376
    38
slouken@5376
    39
    if (nscursor) {
slouken@5376
    40
        cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5376
    41
        if (cursor) {
slouken@5376
    42
            cursor->driverdata = nscursor;
slouken@5376
    43
            [nscursor retain];
slouken@5376
    44
        }
slouken@5376
    45
    }
slouken@5376
    46
slouken@5376
    47
    [pool release];
slouken@5376
    48
slouken@5376
    49
    return cursor;
slouken@5376
    50
}
slouken@5376
    51
slouken@5376
    52
static SDL_Cursor *
slouken@5376
    53
Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5376
    54
{
slouken@5376
    55
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5376
    56
    NSImage *nsimage;
slouken@5376
    57
    NSCursor *nscursor = NULL;
slouken@5376
    58
    SDL_Cursor *cursor = NULL;
slouken@5376
    59
slouken@5376
    60
    nsimage = Cocoa_CreateImage(surface);
slouken@5376
    61
    if (nsimage) {
slouken@5376
    62
        nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
slouken@5376
    63
    }
slouken@5376
    64
slouken@5376
    65
    if (nscursor) {
slouken@5376
    66
        cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5376
    67
        if (cursor) {
slouken@5376
    68
            cursor->driverdata = nscursor;
slouken@5376
    69
        }
slouken@5376
    70
    }
slouken@5376
    71
slouken@5376
    72
    [pool release];
slouken@5376
    73
slouken@5376
    74
    return cursor;
slouken@5376
    75
}
slouken@5376
    76
slouken@5376
    77
static void
slouken@5376
    78
Cocoa_FreeCursor(SDL_Cursor * cursor)
slouken@5376
    79
{
slouken@5376
    80
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5376
    81
    NSCursor *nscursor = (NSCursor *)cursor->driverdata;
slouken@5376
    82
slouken@5376
    83
    [nscursor release];
slouken@5376
    84
slouken@5376
    85
    [pool release];
slouken@5376
    86
}
slouken@5376
    87
slouken@5376
    88
static int
slouken@5376
    89
Cocoa_ShowCursor(SDL_Cursor * cursor)
slouken@5376
    90
{
slouken@5376
    91
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5376
    92
slouken@5376
    93
    if (SDL_GetMouseFocus()) {
slouken@5376
    94
        if (cursor) {
slouken@5387
    95
            NSCursor *nscursor = (NSCursor *)cursor->driverdata;
slouken@5387
    96
slouken@5387
    97
            [nscursor set];
slouken@5376
    98
            [NSCursor unhide];
slouken@5376
    99
        } else {
slouken@5376
   100
            [NSCursor hide];
slouken@5376
   101
        }
slouken@5376
   102
    }
slouken@5376
   103
slouken@5376
   104
    [pool release];
slouken@5376
   105
slouken@5376
   106
    return 0;
slouken@5376
   107
}
slouken@5376
   108
slouken@5376
   109
static void
slouken@5376
   110
Cocoa_WarpMouse(SDL_Window * window, int x, int y)
slouken@5376
   111
{
slouken@5376
   112
    CGPoint point;
slouken@5376
   113
slouken@5391
   114
    point.x = (float)window->x + x;
slouken@5391
   115
    point.y = (float)window->y + y;
slouken@5376
   116
    CGWarpMouseCursorPosition(point);
slouken@5376
   117
}
slouken@5376
   118
slouken@5406
   119
static int
slouken@5406
   120
Cocoa_SetRelativeMouseMode(SDL_bool enabled)
slouken@5406
   121
{
slouken@5406
   122
    CGError result;
slouken@5406
   123
slouken@5406
   124
    if (enabled) {
slouken@5406
   125
        result = CGAssociateMouseAndMouseCursorPosition(NO);
slouken@5406
   126
    } else {
slouken@5406
   127
        result = CGAssociateMouseAndMouseCursorPosition(YES);
slouken@5406
   128
    }
slouken@5406
   129
    if (result != kCGErrorSuccess) {
slouken@5406
   130
        SDL_SetError("CGAssociateMouseAndMouseCursorPosition() failed");
slouken@5406
   131
        return -1;
slouken@5406
   132
    }
slouken@5406
   133
    return 0;
slouken@5406
   134
}
slouken@5406
   135
slouken@1931
   136
void
slouken@1931
   137
Cocoa_InitMouse(_THIS)
slouken@1931
   138
{
slouken@5376
   139
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5376
   140
slouken@5376
   141
    mouse->CreateCursor = Cocoa_CreateCursor;
slouken@5376
   142
    mouse->ShowCursor = Cocoa_ShowCursor;
slouken@5406
   143
    mouse->FreeCursor = Cocoa_FreeCursor;
slouken@5376
   144
    mouse->WarpMouse = Cocoa_WarpMouse;
slouken@5406
   145
    mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
slouken@5376
   146
slouken@5405
   147
    SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
slouken@1931
   148
}
slouken@1931
   149
slouken@3517
   150
static int
slouken@3517
   151
ConvertMouseButtonToSDL(int button)
slouken@3517
   152
{
slouken@3517
   153
    switch (button)
slouken@3517
   154
    {
slouken@3517
   155
        case 0:
slouken@3517
   156
            return(SDL_BUTTON_LEFT);   /* 1 */
slouken@3517
   157
        case 1:
slouken@3517
   158
            return(SDL_BUTTON_RIGHT);  /* 3 */
slouken@3517
   159
        case 2:
slouken@3517
   160
            return(SDL_BUTTON_MIDDLE); /* 2 */
slouken@3517
   161
    }
slouken@5060
   162
    return button+1;
slouken@3517
   163
}
slouken@3517
   164
slouken@3517
   165
void
slouken@3517
   166
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
slouken@3517
   167
{
slouken@5406
   168
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5406
   169
slouken@5406
   170
    if (mouse->relative_mode && [event type] == NSMouseMoved) {
slouken@5406
   171
        float x = [event deltaX];
slouken@5406
   172
        float y = [event deltaY];
slouken@5406
   173
        SDL_SendMouseMotion(mouse->focus, 1, (int)x, (int)y);
slouken@5406
   174
    }
slouken@3517
   175
}
slouken@3517
   176
slouken@1931
   177
void
gzjjgod@5057
   178
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
gzjjgod@5057
   179
{
gzjjgod@5057
   180
    float x = [event deltaX];
gzjjgod@5057
   181
    float y = [event deltaY];
gzjjgod@5057
   182
gzjjgod@5057
   183
    if (x > 0) {
gzjjgod@5057
   184
        x += 0.9f;
gzjjgod@5057
   185
    } else if (x < 0) {
gzjjgod@5057
   186
        x -= 0.9f;
gzjjgod@5057
   187
    }
gzjjgod@5057
   188
    if (y > 0) {
gzjjgod@5057
   189
        y += 0.9f;
gzjjgod@5057
   190
    } else if (y < 0) {
gzjjgod@5057
   191
        y -= 0.9f;
gzjjgod@5057
   192
    }
gzjjgod@5057
   193
    SDL_SendMouseWheel(window, (int)x, (int)y);
gzjjgod@5057
   194
}
gzjjgod@5057
   195
slouken@5058
   196
void
slouken@5058
   197
Cocoa_QuitMouse(_THIS)
slouken@5058
   198
{
slouken@5058
   199
}
slouken@5058
   200
slouken@1931
   201
/* vi: set ts=4 sw=4 expandtab: */