src/video/cocoa/SDL_cocoamousetap.m
changeset 7593 20298a0d8631
child 7607 7753a6f8cda8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/cocoa/SDL_cocoamousetap.m	Wed Aug 07 16:29:15 2013 -0700
     1.3 @@ -0,0 +1,252 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +#include "SDL_config.h"
    1.25 +
    1.26 +#if SDL_VIDEO_DRIVER_COCOA
    1.27 +
    1.28 +#define SDL_MAC_NO_SANDBOX 1
    1.29 +
    1.30 +#include "SDL_cocoamousetap.h"
    1.31 +
    1.32 +/* Event taps are forbidden in the Mac App Store, so we can only enable this
    1.33 + * code if your app doesn't need to ship through the app store.
    1.34 + * This code makes it so that a grabbed cursor cannot "leak" a mouse click
    1.35 + * past the edge of the window if moving the cursor too fast.
    1.36 + */
    1.37 +#if SDL_MAC_NO_SANDBOX
    1.38 +
    1.39 +#include "SDL_keyboard.h"
    1.40 +#include "SDL_thread.h"
    1.41 +#include "SDL_cocoavideo.h"
    1.42 +
    1.43 +#include "../../events/SDL_mouse_c.h"
    1.44 +
    1.45 +typedef struct {
    1.46 +    CFMachPortRef tap;
    1.47 +    CFRunLoopRef runloop;
    1.48 +    CFRunLoopSourceRef runloopSource;
    1.49 +    SDL_Thread *thread;
    1.50 +    SDL_sem *runloopStartedSemaphore;
    1.51 +} SDL_MouseEventTapData;
    1.52 +
    1.53 +static const CGEventMask movementEventsMask =
    1.54 +      CGEventMaskBit(kCGEventLeftMouseDragged)
    1.55 +    | CGEventMaskBit(kCGEventRightMouseDragged)
    1.56 +    | CGEventMaskBit(kCGEventMouseMoved);
    1.57 +
    1.58 +static const CGEventMask allGrabbedEventsMask =
    1.59 +      CGEventMaskBit(kCGEventLeftMouseDown)    | CGEventMaskBit(kCGEventLeftMouseUp)
    1.60 +    | CGEventMaskBit(kCGEventRightMouseDown)   | CGEventMaskBit(kCGEventRightMouseUp)
    1.61 +    | CGEventMaskBit(kCGEventOtherMouseDown)   | CGEventMaskBit(kCGEventOtherMouseUp)
    1.62 +    | CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged)
    1.63 +    | CGEventMaskBit(kCGEventMouseMoved);
    1.64 +
    1.65 +static CGEventRef
    1.66 +Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
    1.67 +{
    1.68 +    SDL_MouseData *driverdata = (SDL_MouseData*)refcon;
    1.69 +    SDL_Mouse *mouse = SDL_GetMouse();
    1.70 +    SDL_Window *window = SDL_GetKeyboardFocus();
    1.71 +    NSRect windowRect;
    1.72 +    CGPoint eventLocation;
    1.73 +
    1.74 +    switch (type)
    1.75 +    {
    1.76 +        case kCGEventTapDisabledByTimeout:
    1.77 +        case kCGEventTapDisabledByUserInput:
    1.78 +            {
    1.79 +                CGEventTapEnable(((SDL_MouseEventTapData*)(driverdata->tapdata))->tap, true);
    1.80 +                return NULL;
    1.81 +            }
    1.82 +        default:
    1.83 +            break;
    1.84 +    }
    1.85 +
    1.86 +
    1.87 +    if (!window || !mouse) {
    1.88 +        return event;
    1.89 +    }
    1.90 +
    1.91 +    if (mouse->relative_mode) {
    1.92 +        return event;
    1.93 +    }
    1.94 +
    1.95 +    if (!(window->flags & SDL_WINDOW_INPUT_GRABBED)) {
    1.96 +        return event;
    1.97 +    }
    1.98 +
    1.99 +    /* This is the same coordinate system as Cocoa uses. */
   1.100 +    eventLocation = CGEventGetUnflippedLocation(event);
   1.101 +    windowRect = [((SDL_WindowData *) window->driverdata)->nswindow frame];
   1.102 +
   1.103 +    if (!NSPointInRect(NSPointFromCGPoint(eventLocation), windowRect)) {
   1.104 +
   1.105 +        /* This is in CGs global screenspace coordinate system, which has a
   1.106 +         * flipped Y.
   1.107 +         */
   1.108 +        CGPoint newLocation = CGEventGetLocation(event);
   1.109 +
   1.110 +        if (eventLocation.x < NSMinX(windowRect)) {
   1.111 +            newLocation.x = NSMinX(windowRect);
   1.112 +        } else if (eventLocation.x >= NSMaxX(windowRect)) {
   1.113 +            newLocation.x = NSMaxX(windowRect) - 1.0;
   1.114 +        }
   1.115 +
   1.116 +        if (eventLocation.y < NSMinY(windowRect)) {
   1.117 +            newLocation.y -= (NSMinY(windowRect) - eventLocation.y + 1);
   1.118 +        } else if (eventLocation.y >= NSMaxY(windowRect)) {
   1.119 +            newLocation.y += (eventLocation.y - NSMaxY(windowRect) + 1);
   1.120 +        }
   1.121 +
   1.122 +        CGSetLocalEventsSuppressionInterval(0);
   1.123 +        CGWarpMouseCursorPosition(newLocation);
   1.124 +        CGSetLocalEventsSuppressionInterval(0.25);
   1.125 +
   1.126 +        if ((CGEventMaskBit(type) & movementEventsMask) == 0) {
   1.127 +            /* For click events, we just constrain the event to the window, so
   1.128 +             * no other app receives the click event. We can't due the same to
   1.129 +             * movement events, since they mean that our warp cursor above
   1.130 +             * behaves strangely.
   1.131 +             */
   1.132 +            CGEventSetLocation(event, newLocation);
   1.133 +        }
   1.134 +    }
   1.135 +
   1.136 +    return event;
   1.137 +}
   1.138 +
   1.139 +static int
   1.140 +Cocoa_MouseTapThread(void *data)
   1.141 +{
   1.142 +    SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)data;
   1.143 +
   1.144 +    /* Create a tap. */
   1.145 +    CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap,
   1.146 +                                              kCGEventTapOptionDefault, allGrabbedEventsMask,
   1.147 +                                              &Cocoa_MouseTapCallback, tapdata);
   1.148 +    if (eventTap) {
   1.149 +        /* Try to create a runloop source we can schedule. */
   1.150 +        CFRunLoopSourceRef runloopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
   1.151 +        if  (runloopSource) {
   1.152 +            tapdata->tap = eventTap;
   1.153 +            tapdata->runloopSource = runloopSource;
   1.154 +        } else {
   1.155 +            CFRelease(eventTap);
   1.156 +            SDL_SemPost(tapdata->runloopStartedSemaphore);
   1.157 +            /* TODO: Both here and in the return below, set some state in
   1.158 +             * tapdata to indicate that initialization failed, which we should
   1.159 +             * check in InitMouseEventTap, after we move the semaphore check
   1.160 +             * from Quit to Init.
   1.161 +             */
   1.162 +            return 1;
   1.163 +        }
   1.164 +    } else {
   1.165 +        SDL_SemPost(tapdata->runloopStartedSemaphore);
   1.166 +        return 1;
   1.167 +    }
   1.168 +
   1.169 +    tapdata->runloop = CFRunLoopGetCurrent();
   1.170 +    CFRunLoopAddSource(tapdata->runloop, tapdata->runloopSource, kCFRunLoopCommonModes);
   1.171 +    CFRunLoopPerformBlock(tapdata->runloop, kCFRunLoopCommonModes, ^{
   1.172 +        /* We signal this *after* the run loop has started, indicating it's safe to CFRunLoopStop it. */
   1.173 +        SDL_SemPost(tapdata->runloopStartedSemaphore);
   1.174 +    });
   1.175 +
   1.176 +    /* Run the event loop to handle events in the event tap. */
   1.177 +    CFRunLoopRun();
   1.178 +    /* Make sure this is signaled so that SDL_QuitMouseEventTap knows it can safely SDL_WaitThread for us. */
   1.179 +    if (SDL_SemValue(tapdata->runloopStartedSemaphore) < 1) {
   1.180 +        SDL_SemPost(tapdata->runloopStartedSemaphore);
   1.181 +    }
   1.182 +    CFRunLoopRemoveSource(tapdata->runloop, tapdata->runloopSource, kCFRunLoopCommonModes);
   1.183 +
   1.184 +    /* Clean up. */
   1.185 +    CGEventTapEnable(tapdata->tap, false);
   1.186 +    CFRelease(tapdata->runloopSource);
   1.187 +    CFRelease(tapdata->tap);
   1.188 +    tapdata->runloopSource = NULL;
   1.189 +    tapdata->tap = NULL;
   1.190 +
   1.191 +    return 0;
   1.192 +}
   1.193 +
   1.194 +void
   1.195 +Cocoa_InitMouseEventTap(SDL_MouseData* driverdata)
   1.196 +{
   1.197 +    SDL_MouseEventTapData *tapdata;
   1.198 +    driverdata->tapdata = SDL_calloc(1, sizeof(SDL_MouseEventTapData));
   1.199 +    tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
   1.200 +
   1.201 +    tapdata->runloopStartedSemaphore = SDL_CreateSemaphore(0);
   1.202 +    if (tapdata->runloopStartedSemaphore) {
   1.203 +        tapdata->thread = SDL_CreateThread(&Cocoa_MouseTapThread, "Event Tap Loop", tapdata);
   1.204 +        if (!tapdata->thread) {
   1.205 +            SDL_DestroySemaphore(tapdata->runloopStartedSemaphore);
   1.206 +        }
   1.207 +    }
   1.208 +
   1.209 +    if (!tapdata->thread) {
   1.210 +        SDL_free(driverdata->tapdata);
   1.211 +        driverdata->tapdata = NULL;
   1.212 +    }
   1.213 +}
   1.214 +
   1.215 +void
   1.216 +Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
   1.217 +{
   1.218 +    SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
   1.219 +    int status;
   1.220 +
   1.221 +    /* Ensure that the runloop has been started first.
   1.222 +     * TODO: Move this to InitMouseEventTap, check for error conditions that can
   1.223 +     * happen in Cocoa_MouseTapThread, and fall back to the non-EventTap way of
   1.224 +     * grabbing the mouse if it fails to Init.
   1.225 +     */
   1.226 +    status = SDL_SemWaitTimeout(tapdata->runloopStartedSemaphore, 5000);
   1.227 +    if (status > -1) {
   1.228 +        /* Then stop it, which will cause Cocoa_MouseTapThread to return. */
   1.229 +        CFRunLoopStop(tapdata->runloop);
   1.230 +        /* And then wait for Cocoa_MouseTapThread to finish cleaning up. It
   1.231 +         * releases some of the pointers in tapdata. */
   1.232 +        SDL_WaitThread(tapdata->thread, &status);
   1.233 +    }
   1.234 +
   1.235 +    SDL_free(driverdata->tapdata);
   1.236 +    driverdata->tapdata = NULL;
   1.237 +}
   1.238 +
   1.239 +#else /* SDL_MAC_NO_SANDBOX */
   1.240 +
   1.241 +void
   1.242 +Cocoa_InitMouseEventTap(SDL_MouseData *unused)
   1.243 +{
   1.244 +}
   1.245 +
   1.246 +void
   1.247 +Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
   1.248 +{
   1.249 +}
   1.250 +
   1.251 +#endif /* !SDL_MAC_NO_SANDBOX */
   1.252 +
   1.253 +#endif /* SDL_VIDEO_DRIVER_COCOA */
   1.254 +
   1.255 +/* vi: set ts=4 sw=4 expandtab: */