src/video/cocoa/SDL_cocoamouse.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 03 Dec 2009 08:33:39 +0000
changeset 3517 e7eec78e4b92
parent 2940 b93965a16fe0
child 3525 455a6c47d2c6
permissions -rw-r--r--
Fixed mouse events for fullscreen windows on Mac OS X
slouken@1931
     1
/*
slouken@1931
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 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@1931
    29
void
slouken@1931
    30
Cocoa_InitMouse(_THIS)
slouken@1931
    31
{
slouken@1931
    32
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1931
    33
    SDL_Mouse mouse;
slouken@1931
    34
slouken@1931
    35
    SDL_zero(mouse);
slouken@2940
    36
    data->mouse = SDL_AddMouse(&mouse, "Mouse", 0, 0, 1);
slouken@1931
    37
}
slouken@1931
    38
slouken@3517
    39
static int
slouken@3517
    40
ConvertMouseButtonToSDL(int button)
slouken@3517
    41
{
slouken@3517
    42
    switch (button)
slouken@3517
    43
    {
slouken@3517
    44
        case 0:
slouken@3517
    45
            return(SDL_BUTTON_LEFT);   /* 1 */
slouken@3517
    46
        case 1:
slouken@3517
    47
            return(SDL_BUTTON_RIGHT);  /* 3 */
slouken@3517
    48
        case 2:
slouken@3517
    49
            return(SDL_BUTTON_MIDDLE); /* 2 */
slouken@3517
    50
    }
slouken@3517
    51
    return button;
slouken@3517
    52
}
slouken@3517
    53
slouken@3517
    54
void
slouken@3517
    55
Cocoa_HandleMouseEvent(_THIS, NSEvent *event)
slouken@3517
    56
{
slouken@3517
    57
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@3517
    58
    SDL_Mouse *mouse = SDL_GetMouse(data->mouse);
slouken@3517
    59
    int i;
slouken@3517
    60
    NSPoint point;
slouken@3517
    61
    SDL_Window *window;
slouken@3517
    62
slouken@3517
    63
    /* See if there are any fullscreen windows that might handle this event */
slouken@3517
    64
    window = NULL;
slouken@3517
    65
    for (i = 0; i < _this->num_displays; ++i) {
slouken@3517
    66
        SDL_VideoDisplay *display = &_this->displays[i];
slouken@3517
    67
        SDL_Window *candidate = display->fullscreen_window;
slouken@3517
    68
slouken@3517
    69
        if (candidate) {
slouken@3517
    70
            SDL_DisplayData *displaydata = (SDL_DisplayData *)display->driverdata;
slouken@3517
    71
            NSRect rect = CGDisplayBounds(displaydata->display);
slouken@3517
    72
slouken@3517
    73
            point = [NSEvent mouseLocation];
slouken@3517
    74
            point.x = point.x - rect.origin.x;
slouken@3517
    75
            point.y = CGDisplayPixelsHigh(kCGDirectMainDisplay) - point.y - rect.origin.y;
slouken@3517
    76
            if (point.x < 0 || point.x >= candidate->w ||
slouken@3517
    77
                point.y < 0 || point.y >= candidate->h) {
slouken@3517
    78
                /* The mouse is out of this fullscreen display */
slouken@3517
    79
                if (mouse->focus == candidate->id) {
slouken@3517
    80
                    SDL_SetMouseFocus(data->mouse, 0);
slouken@3517
    81
                }
slouken@3517
    82
            } else {
slouken@3517
    83
                /* This is it! */
slouken@3517
    84
                window = candidate;
slouken@3517
    85
                break;
slouken@3517
    86
            }
slouken@3517
    87
        }
slouken@3517
    88
    }
slouken@3517
    89
    if (!window) {
slouken@3517
    90
        return;
slouken@3517
    91
    }
slouken@3517
    92
slouken@3517
    93
    /* Set the focus appropriately */
slouken@3517
    94
    if (mouse->focus != window->id) {
slouken@3517
    95
        SDL_SetMouseFocus(data->mouse, window->id);
slouken@3517
    96
    }
slouken@3517
    97
slouken@3517
    98
    switch ([event type]) {
slouken@3517
    99
    case NSLeftMouseDown:
slouken@3517
   100
    case NSOtherMouseDown:
slouken@3517
   101
    case NSRightMouseDown:
slouken@3517
   102
        SDL_SendMouseButton(data->mouse, SDL_PRESSED, ConvertMouseButtonToSDL([event buttonNumber]));
slouken@3517
   103
        break;
slouken@3517
   104
    case NSLeftMouseUp:
slouken@3517
   105
    case NSOtherMouseUp:
slouken@3517
   106
    case NSRightMouseUp:
slouken@3517
   107
        SDL_SendMouseButton(data->mouse, SDL_RELEASED, ConvertMouseButtonToSDL([event buttonNumber]));
slouken@3517
   108
        break;
slouken@3517
   109
    case NSLeftMouseDragged:
slouken@3517
   110
    case NSRightMouseDragged:
slouken@3517
   111
    case NSOtherMouseDragged: /* usually middle mouse dragged */
slouken@3517
   112
    case NSMouseMoved:
slouken@3517
   113
        SDL_SendMouseMotion(data->mouse, 0, (int)point.x, (int)point.y, 0);
slouken@3517
   114
        break;
slouken@3517
   115
    default: /* just to avoid compiler warnings */
slouken@3517
   116
        break;
slouken@3517
   117
    }
slouken@3517
   118
}
slouken@3517
   119
slouken@1931
   120
void
slouken@1931
   121
Cocoa_QuitMouse(_THIS)
slouken@1931
   122
{
slouken@1931
   123
    SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
slouken@1931
   124
slouken@1931
   125
    SDL_DelMouse(data->mouse);
slouken@1931
   126
}
slouken@1931
   127
slouken@1931
   128
/* vi: set ts=4 sw=4 expandtab: */