src/video/cocoa/SDL_cocoamousetap.m
author Sam Lantinga <slouken@libsdl.org>
Sat, 16 Nov 2013 21:19:16 -0800
changeset 8005 d8edfca6e3c2
parent 7917 686df9c2b98a
child 8054 2a38ef3eeabb
permissions -rw-r--r--
When the mouse is grabbed it's constrained to the client area, not the window frame.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7593
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
     1
/*
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
     2
  Simple DirectMedia Layer
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
     4
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
     5
  This software is provided 'as-is', without any express or implied
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
     7
  arising from the use of this software.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
     8
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    11
  freely, subject to the following restrictions:
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    12
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    14
     claim that you wrote the original software. If you use this software
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    16
     appreciated but is not required.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    18
     misrepresented as being the original software.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    20
*/
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    21
#include "SDL_config.h"
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    22
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    23
#if SDL_VIDEO_DRIVER_COCOA
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    24
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    25
#include "SDL_cocoamousetap.h"
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    26
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    27
/* Event taps are forbidden in the Mac App Store, so we can only enable this
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    28
 * code if your app doesn't need to ship through the app store.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    29
 * This code makes it so that a grabbed cursor cannot "leak" a mouse click
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    30
 * past the edge of the window if moving the cursor too fast.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    31
 */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    32
#if SDL_MAC_NO_SANDBOX
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    33
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    34
#include "SDL_keyboard.h"
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    35
#include "SDL_thread.h"
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    36
#include "SDL_cocoavideo.h"
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    37
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    38
#include "../../events/SDL_mouse_c.h"
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    39
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    40
typedef struct {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    41
    CFMachPortRef tap;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    42
    CFRunLoopRef runloop;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    43
    CFRunLoopSourceRef runloopSource;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    44
    SDL_Thread *thread;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    45
    SDL_sem *runloopStartedSemaphore;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    46
} SDL_MouseEventTapData;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    47
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    48
static const CGEventMask movementEventsMask =
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    49
      CGEventMaskBit(kCGEventLeftMouseDragged)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    50
    | CGEventMaskBit(kCGEventRightMouseDragged)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    51
    | CGEventMaskBit(kCGEventMouseMoved);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    52
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    53
static const CGEventMask allGrabbedEventsMask =
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    54
      CGEventMaskBit(kCGEventLeftMouseDown)    | CGEventMaskBit(kCGEventLeftMouseUp)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    55
    | CGEventMaskBit(kCGEventRightMouseDown)   | CGEventMaskBit(kCGEventRightMouseUp)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    56
    | CGEventMaskBit(kCGEventOtherMouseDown)   | CGEventMaskBit(kCGEventOtherMouseUp)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    57
    | CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    58
    | CGEventMaskBit(kCGEventMouseMoved);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    59
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    60
static CGEventRef
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    61
Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    62
{
7917
686df9c2b98a Mac: Fix a crash with SDL_MAC_NO_SANDBOX.
Sam Lantinga <slouken@libsdl.org>
parents: 7610
diff changeset
    63
    SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)refcon;
7593
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    64
    SDL_Mouse *mouse = SDL_GetMouse();
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    65
    SDL_Window *window = SDL_GetKeyboardFocus();
8005
d8edfca6e3c2 When the mouse is grabbed it's constrained to the client area, not the window frame.
Sam Lantinga <slouken@libsdl.org>
parents: 7917
diff changeset
    66
    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
7593
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    67
    NSRect windowRect;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    68
    CGPoint eventLocation;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    69
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    70
    switch (type)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    71
    {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    72
        case kCGEventTapDisabledByTimeout:
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    73
        case kCGEventTapDisabledByUserInput:
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    74
            {
7917
686df9c2b98a Mac: Fix a crash with SDL_MAC_NO_SANDBOX.
Sam Lantinga <slouken@libsdl.org>
parents: 7610
diff changeset
    75
                CGEventTapEnable(tapdata->tap, true);
7593
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    76
                return NULL;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    77
            }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    78
        default:
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    79
            break;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    80
    }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    81
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    82
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    83
    if (!window || !mouse) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    84
        return event;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    85
    }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    86
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    87
    if (mouse->relative_mode) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    88
        return event;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    89
    }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    90
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    91
    if (!(window->flags & SDL_WINDOW_INPUT_GRABBED)) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    92
        return event;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    93
    }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    94
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    95
    /* This is the same coordinate system as Cocoa uses. */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    96
    eventLocation = CGEventGetUnflippedLocation(event);
8005
d8edfca6e3c2 When the mouse is grabbed it's constrained to the client area, not the window frame.
Sam Lantinga <slouken@libsdl.org>
parents: 7917
diff changeset
    97
    windowRect = [nswindow contentRectForFrameRect:[nswindow frame]];
7593
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    98
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
    99
    if (!NSPointInRect(NSPointFromCGPoint(eventLocation), windowRect)) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   100
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   101
        /* This is in CGs global screenspace coordinate system, which has a
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   102
         * flipped Y.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   103
         */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   104
        CGPoint newLocation = CGEventGetLocation(event);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   105
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   106
        if (eventLocation.x < NSMinX(windowRect)) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   107
            newLocation.x = NSMinX(windowRect);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   108
        } else if (eventLocation.x >= NSMaxX(windowRect)) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   109
            newLocation.x = NSMaxX(windowRect) - 1.0;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   110
        }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   111
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   112
        if (eventLocation.y < NSMinY(windowRect)) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   113
            newLocation.y -= (NSMinY(windowRect) - eventLocation.y + 1);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   114
        } else if (eventLocation.y >= NSMaxY(windowRect)) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   115
            newLocation.y += (eventLocation.y - NSMaxY(windowRect) + 1);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   116
        }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   117
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   118
        CGSetLocalEventsSuppressionInterval(0);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   119
        CGWarpMouseCursorPosition(newLocation);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   120
        CGSetLocalEventsSuppressionInterval(0.25);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   121
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   122
        if ((CGEventMaskBit(type) & movementEventsMask) == 0) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   123
            /* For click events, we just constrain the event to the window, so
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   124
             * no other app receives the click event. We can't due the same to
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   125
             * movement events, since they mean that our warp cursor above
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   126
             * behaves strangely.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   127
             */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   128
            CGEventSetLocation(event, newLocation);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   129
        }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   130
    }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   131
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   132
    return event;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   133
}
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   134
7610
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   135
static void
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   136
SemaphorePostCallback(CFRunLoopTimerRef timer, void *info)
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   137
{
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   138
    SDL_SemPost((SDL_sem*)info);
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   139
}
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   140
7593
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   141
static int
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   142
Cocoa_MouseTapThread(void *data)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   143
{
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   144
    SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)data;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   145
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   146
    /* Create a tap. */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   147
    CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap,
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   148
                                              kCGEventTapOptionDefault, allGrabbedEventsMask,
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   149
                                              &Cocoa_MouseTapCallback, tapdata);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   150
    if (eventTap) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   151
        /* Try to create a runloop source we can schedule. */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   152
        CFRunLoopSourceRef runloopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   153
        if  (runloopSource) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   154
            tapdata->tap = eventTap;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   155
            tapdata->runloopSource = runloopSource;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   156
        } else {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   157
            CFRelease(eventTap);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   158
            SDL_SemPost(tapdata->runloopStartedSemaphore);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   159
            /* TODO: Both here and in the return below, set some state in
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   160
             * tapdata to indicate that initialization failed, which we should
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   161
             * check in InitMouseEventTap, after we move the semaphore check
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   162
             * from Quit to Init.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   163
             */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   164
            return 1;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   165
        }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   166
    } else {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   167
        SDL_SemPost(tapdata->runloopStartedSemaphore);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   168
        return 1;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   169
    }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   170
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   171
    tapdata->runloop = CFRunLoopGetCurrent();
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   172
    CFRunLoopAddSource(tapdata->runloop, tapdata->runloopSource, kCFRunLoopCommonModes);
7610
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   173
    CFRunLoopTimerContext context = {.info = tapdata->runloopStartedSemaphore};
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   174
    /* We signal the runloop started semaphore *after* the run loop has started, indicating it's safe to CFRunLoopStop it. */
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   175
    CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent(), 0, 0, 0, &SemaphorePostCallback, &context);
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   176
    CFRunLoopAddTimer(tapdata->runloop, timer, kCFRunLoopCommonModes);
b043c5c726c1 Mac: Fix mouse tap on 10.5.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents: 7607
diff changeset
   177
    CFRelease(timer);
7593
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   178
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   179
    /* Run the event loop to handle events in the event tap. */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   180
    CFRunLoopRun();
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   181
    /* Make sure this is signaled so that SDL_QuitMouseEventTap knows it can safely SDL_WaitThread for us. */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   182
    if (SDL_SemValue(tapdata->runloopStartedSemaphore) < 1) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   183
        SDL_SemPost(tapdata->runloopStartedSemaphore);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   184
    }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   185
    CFRunLoopRemoveSource(tapdata->runloop, tapdata->runloopSource, kCFRunLoopCommonModes);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   186
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   187
    /* Clean up. */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   188
    CGEventTapEnable(tapdata->tap, false);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   189
    CFRelease(tapdata->runloopSource);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   190
    CFRelease(tapdata->tap);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   191
    tapdata->runloopSource = NULL;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   192
    tapdata->tap = NULL;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   193
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   194
    return 0;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   195
}
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   196
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   197
void
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   198
Cocoa_InitMouseEventTap(SDL_MouseData* driverdata)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   199
{
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   200
    SDL_MouseEventTapData *tapdata;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   201
    driverdata->tapdata = SDL_calloc(1, sizeof(SDL_MouseEventTapData));
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   202
    tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   203
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   204
    tapdata->runloopStartedSemaphore = SDL_CreateSemaphore(0);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   205
    if (tapdata->runloopStartedSemaphore) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   206
        tapdata->thread = SDL_CreateThread(&Cocoa_MouseTapThread, "Event Tap Loop", tapdata);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   207
        if (!tapdata->thread) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   208
            SDL_DestroySemaphore(tapdata->runloopStartedSemaphore);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   209
        }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   210
    }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   211
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   212
    if (!tapdata->thread) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   213
        SDL_free(driverdata->tapdata);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   214
        driverdata->tapdata = NULL;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   215
    }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   216
}
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   217
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   218
void
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   219
Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   220
{
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   221
    SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   222
    int status;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   223
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   224
    /* Ensure that the runloop has been started first.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   225
     * TODO: Move this to InitMouseEventTap, check for error conditions that can
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   226
     * happen in Cocoa_MouseTapThread, and fall back to the non-EventTap way of
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   227
     * grabbing the mouse if it fails to Init.
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   228
     */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   229
    status = SDL_SemWaitTimeout(tapdata->runloopStartedSemaphore, 5000);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   230
    if (status > -1) {
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   231
        /* Then stop it, which will cause Cocoa_MouseTapThread to return. */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   232
        CFRunLoopStop(tapdata->runloop);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   233
        /* And then wait for Cocoa_MouseTapThread to finish cleaning up. It
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   234
         * releases some of the pointers in tapdata. */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   235
        SDL_WaitThread(tapdata->thread, &status);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   236
    }
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   237
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   238
    SDL_free(driverdata->tapdata);
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   239
    driverdata->tapdata = NULL;
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   240
}
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   241
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   242
#else /* SDL_MAC_NO_SANDBOX */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   243
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   244
void
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   245
Cocoa_InitMouseEventTap(SDL_MouseData *unused)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   246
{
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   247
}
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   248
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   249
void
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   250
Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   251
{
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   252
}
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   253
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   254
#endif /* !SDL_MAC_NO_SANDBOX */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   255
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   256
#endif /* SDL_VIDEO_DRIVER_COCOA */
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   257
20298a0d8631 Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
Jørgen P. Tjernø <jorgen@valvesoftware.com>
parents:
diff changeset
   258
/* vi: set ts=4 sw=4 expandtab: */