src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 21 Feb 2011 17:15:50 -0800
changeset 5376 183ec2d4485c
parent 5371 fc3d3d580777
child 5387 03c79d46bf5c
permissions -rw-r--r--
Implemented cursor support and SDL_WarpMouseInWindow() 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@5376
    95
            [NSCursor unhide];
slouken@5376
    96
        } else {
slouken@5376
    97
            [NSCursor hide];
slouken@5376
    98
        }
slouken@5376
    99
    }
slouken@5376
   100
slouken@5376
   101
    [pool release];
slouken@5376
   102
slouken@5376
   103
    return 0;
slouken@5376
   104
}
slouken@5376
   105
slouken@5376
   106
static void
slouken@5376
   107
Cocoa_WarpMouse(SDL_Window * window, int x, int y)
slouken@5376
   108
{
slouken@5376
   109
    CGPoint point;
slouken@5376
   110
slouken@5376
   111
    point.x = (CGFloat)window->x + x;
slouken@5376
   112
    point.y = (CGFloat)window->y + y;
slouken@5376
   113
    CGWarpMouseCursorPosition(point);
slouken@5376
   114
}
slouken@5376
   115
slouken@1931
   116
void
slouken@1931
   117
Cocoa_InitMouse(_THIS)
slouken@1931
   118
{
slouken@5376
   119
    SDL_Mouse *mouse = SDL_GetMouse();
slouken@5376
   120
    SDL_Cursor *cursor;
slouken@5376
   121
slouken@5376
   122
    mouse->CreateCursor = Cocoa_CreateCursor;
slouken@5376
   123
    mouse->ShowCursor = Cocoa_ShowCursor;
slouken@5376
   124
    mouse->WarpMouse = Cocoa_WarpMouse;
slouken@5376
   125
    mouse->FreeCursor = Cocoa_FreeCursor;
slouken@5376
   126
slouken@5376
   127
    cursor = Cocoa_CreateDefaultCursor();
slouken@5376
   128
    mouse->cursors = mouse->cur_cursor = cursor;
slouken@1931
   129
}
slouken@1931
   130
slouken@3517
   131
static int
slouken@3517
   132
ConvertMouseButtonToSDL(int button)
slouken@3517
   133
{
slouken@3517
   134
    switch (button)
slouken@3517
   135
    {
slouken@3517
   136
        case 0:
slouken@3517
   137
            return(SDL_BUTTON_LEFT);   /* 1 */
slouken@3517
   138
        case 1:
slouken@3517
   139
            return(SDL_BUTTON_RIGHT);  /* 3 */
slouken@3517
   140
        case 2:
slouken@3517
   141
            return(SDL_BUTTON_MIDDLE); /* 2 */
slouken@3517
   142
    }
slouken@5060
   143
    return button+1;
slouken@3517
   144
}
slouken@3517
   145
slouken@3517
   146
void
slouken@3517
   147
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
slouken@3517
   148
{
slouken@5371
   149
    /* We're correctly using views even in fullscreen mode now */
slouken@3517
   150
}
slouken@3517
   151
slouken@1931
   152
void
gzjjgod@5057
   153
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
gzjjgod@5057
   154
{
gzjjgod@5057
   155
    float x = [event deltaX];
gzjjgod@5057
   156
    float y = [event deltaY];
gzjjgod@5057
   157
gzjjgod@5057
   158
    if (x > 0) {
gzjjgod@5057
   159
        x += 0.9f;
gzjjgod@5057
   160
    } else if (x < 0) {
gzjjgod@5057
   161
        x -= 0.9f;
gzjjgod@5057
   162
    }
gzjjgod@5057
   163
    if (y > 0) {
gzjjgod@5057
   164
        y += 0.9f;
gzjjgod@5057
   165
    } else if (y < 0) {
gzjjgod@5057
   166
        y -= 0.9f;
gzjjgod@5057
   167
    }
gzjjgod@5057
   168
    SDL_SendMouseWheel(window, (int)x, (int)y);
gzjjgod@5057
   169
}
gzjjgod@5057
   170
slouken@5058
   171
void
slouken@5058
   172
Cocoa_QuitMouse(_THIS)
slouken@5058
   173
{
slouken@5058
   174
}
slouken@5058
   175
slouken@1931
   176
/* vi: set ts=4 sw=4 expandtab: */