src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 27 Feb 2011 21:36:23 -0800
changeset 5405 64fa8526e1ce
parent 5391 06b239f48b91
child 5406 1517da4ab6b1
permissions -rw-r--r--
Added a cleaner way to set the default cursor.
Added a way to cycle through the default cursor in testcursor.c
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@1931
   119
void
slouken@1931
   120
Cocoa_InitMouse(_THIS)
slouken@1931
   121
{
slouken@5376
   122
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5376
   123
slouken@5376
   124
    mouse->CreateCursor = Cocoa_CreateCursor;
slouken@5376
   125
    mouse->ShowCursor = Cocoa_ShowCursor;
slouken@5376
   126
    mouse->WarpMouse = Cocoa_WarpMouse;
slouken@5376
   127
    mouse->FreeCursor = Cocoa_FreeCursor;
slouken@5376
   128
slouken@5405
   129
    SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
slouken@1931
   130
}
slouken@1931
   131
slouken@3517
   132
static int
slouken@3517
   133
ConvertMouseButtonToSDL(int button)
slouken@3517
   134
{
slouken@3517
   135
    switch (button)
slouken@3517
   136
    {
slouken@3517
   137
        case 0:
slouken@3517
   138
            return(SDL_BUTTON_LEFT);   /* 1 */
slouken@3517
   139
        case 1:
slouken@3517
   140
            return(SDL_BUTTON_RIGHT);  /* 3 */
slouken@3517
   141
        case 2:
slouken@3517
   142
            return(SDL_BUTTON_MIDDLE); /* 2 */
slouken@3517
   143
    }
slouken@5060
   144
    return button+1;
slouken@3517
   145
}
slouken@3517
   146
slouken@3517
   147
void
slouken@3517
   148
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
slouken@3517
   149
{
slouken@5371
   150
    /* We're correctly using views even in fullscreen mode now */
slouken@3517
   151
}
slouken@3517
   152
slouken@1931
   153
void
gzjjgod@5057
   154
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
gzjjgod@5057
   155
{
gzjjgod@5057
   156
    float x = [event deltaX];
gzjjgod@5057
   157
    float y = [event deltaY];
gzjjgod@5057
   158
gzjjgod@5057
   159
    if (x > 0) {
gzjjgod@5057
   160
        x += 0.9f;
gzjjgod@5057
   161
    } else if (x < 0) {
gzjjgod@5057
   162
        x -= 0.9f;
gzjjgod@5057
   163
    }
gzjjgod@5057
   164
    if (y > 0) {
gzjjgod@5057
   165
        y += 0.9f;
gzjjgod@5057
   166
    } else if (y < 0) {
gzjjgod@5057
   167
        y -= 0.9f;
gzjjgod@5057
   168
    }
gzjjgod@5057
   169
    SDL_SendMouseWheel(window, (int)x, (int)y);
gzjjgod@5057
   170
}
gzjjgod@5057
   171
slouken@5058
   172
void
slouken@5058
   173
Cocoa_QuitMouse(_THIS)
slouken@5058
   174
{
slouken@5058
   175
}
slouken@5058
   176
slouken@1931
   177
/* vi: set ts=4 sw=4 expandtab: */