src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5477 50cd1fb880e5
child 6044 35448a5ea044
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
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@3517
    23
#include "SDL_events.h"
slouken@1931
    24
#include "SDL_cocoavideo.h"
slouken@1931
    25
slouken@1931
    26
#include "../../events/SDL_mouse_c.h"
slouken@1931
    27
slouken@5376
    28
slouken@5376
    29
static SDL_Cursor *
slouken@5376
    30
Cocoa_CreateDefaultCursor()
slouken@5376
    31
{
slouken@5376
    32
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5376
    33
    NSCursor *nscursor;
slouken@5376
    34
    SDL_Cursor *cursor = NULL;
slouken@5376
    35
slouken@5376
    36
    nscursor = [NSCursor arrowCursor];
slouken@5376
    37
slouken@5376
    38
    if (nscursor) {
slouken@5376
    39
        cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5376
    40
        if (cursor) {
slouken@5376
    41
            cursor->driverdata = nscursor;
slouken@5376
    42
            [nscursor retain];
slouken@5376
    43
        }
slouken@5376
    44
    }
slouken@5376
    45
slouken@5376
    46
    [pool release];
slouken@5376
    47
slouken@5376
    48
    return cursor;
slouken@5376
    49
}
slouken@5376
    50
slouken@5376
    51
static SDL_Cursor *
slouken@5376
    52
Cocoa_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@5376
    53
{
slouken@5376
    54
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5376
    55
    NSImage *nsimage;
slouken@5376
    56
    NSCursor *nscursor = NULL;
slouken@5376
    57
    SDL_Cursor *cursor = NULL;
slouken@5376
    58
slouken@5376
    59
    nsimage = Cocoa_CreateImage(surface);
slouken@5376
    60
    if (nsimage) {
slouken@5376
    61
        nscursor = [[NSCursor alloc] initWithImage: nsimage hotSpot: NSMakePoint(hot_x, hot_y)];
slouken@5376
    62
    }
slouken@5376
    63
slouken@5376
    64
    if (nscursor) {
slouken@5376
    65
        cursor = SDL_calloc(1, sizeof(*cursor));
slouken@5376
    66
        if (cursor) {
slouken@5376
    67
            cursor->driverdata = nscursor;
slouken@5376
    68
        }
slouken@5376
    69
    }
slouken@5376
    70
slouken@5376
    71
    [pool release];
slouken@5376
    72
slouken@5376
    73
    return cursor;
slouken@5376
    74
}
slouken@5376
    75
slouken@5376
    76
static void
slouken@5376
    77
Cocoa_FreeCursor(SDL_Cursor * cursor)
slouken@5376
    78
{
slouken@5376
    79
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
slouken@5376
    80
    NSCursor *nscursor = (NSCursor *)cursor->driverdata;
slouken@5376
    81
slouken@5376
    82
    [nscursor release];
slouken@5419
    83
    SDL_free(cursor);
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@5477
   170
    if (mouse->relative_mode &&
slouken@5477
   171
        ([event type] == NSMouseMoved ||
slouken@5477
   172
         [event type] == NSLeftMouseDragged ||
slouken@5477
   173
         [event type] == NSRightMouseDragged ||
slouken@5477
   174
         [event type] == NSOtherMouseDragged)) {
slouken@5406
   175
        float x = [event deltaX];
slouken@5406
   176
        float y = [event deltaY];
slouken@5406
   177
        SDL_SendMouseMotion(mouse->focus, 1, (int)x, (int)y);
slouken@5406
   178
    }
slouken@3517
   179
}
slouken@3517
   180
slouken@1931
   181
void
gzjjgod@5057
   182
Cocoa_HandleMouseWheel(SDL_Window *window, NSEvent *event)
gzjjgod@5057
   183
{
gzjjgod@5057
   184
    float x = [event deltaX];
gzjjgod@5057
   185
    float y = [event deltaY];
gzjjgod@5057
   186
gzjjgod@5057
   187
    if (x > 0) {
gzjjgod@5057
   188
        x += 0.9f;
gzjjgod@5057
   189
    } else if (x < 0) {
gzjjgod@5057
   190
        x -= 0.9f;
gzjjgod@5057
   191
    }
gzjjgod@5057
   192
    if (y > 0) {
gzjjgod@5057
   193
        y += 0.9f;
gzjjgod@5057
   194
    } else if (y < 0) {
gzjjgod@5057
   195
        y -= 0.9f;
gzjjgod@5057
   196
    }
gzjjgod@5057
   197
    SDL_SendMouseWheel(window, (int)x, (int)y);
gzjjgod@5057
   198
}
gzjjgod@5057
   199
slouken@5058
   200
void
slouken@5058
   201
Cocoa_QuitMouse(_THIS)
slouken@5058
   202
{
slouken@5058
   203
}
slouken@5058
   204
slouken@1931
   205
/* vi: set ts=4 sw=4 expandtab: */