Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.
authorJørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 07 Aug 2013 16:29:15 -0700
changeset 759320298a0d8631
parent 7592 063a932a264f
child 7594 6abcf951af68
Mac: Better mouse-grab if you define SDL_MAC_NO_SANDBOX.

This uses a better mouse grab if you define SDL_MAC_NO_SANDBOX. This
mouse grab uses CGEventTapCreate, which you cannot access if you have
sandboxing enabled.
Xcode/SDL/SDL.xcodeproj/project.pbxproj
src/video/cocoa/SDL_cocoamouse.h
src/video/cocoa/SDL_cocoamouse.m
src/video/cocoa/SDL_cocoamousetap.h
src/video/cocoa/SDL_cocoamousetap.m
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/Xcode/SDL/SDL.xcodeproj/project.pbxproj	Wed Aug 07 14:00:41 2013 -0700
     1.2 +++ b/Xcode/SDL/SDL.xcodeproj/project.pbxproj	Wed Aug 07 16:29:15 2013 -0700
     1.3 @@ -516,6 +516,12 @@
     1.4  		AADA5B8716CCAB3000107CF7 /* SDL_bits.h in Headers */ = {isa = PBXBuildFile; fileRef = AADA5B8616CCAB3000107CF7 /* SDL_bits.h */; };
     1.5  		AADA5B8816CCAB3000107CF7 /* SDL_bits.h in Headers */ = {isa = PBXBuildFile; fileRef = AADA5B8616CCAB3000107CF7 /* SDL_bits.h */; };
     1.6  		BBFC088D164C6647003E6A99 /* SDL_gamecontroller.c in Sources */ = {isa = PBXBuildFile; fileRef = BBFC088A164C6514003E6A99 /* SDL_gamecontroller.c */; };
     1.7 +		D55A1B81179F262300625D7C /* SDL_cocoamousetap.h in Headers */ = {isa = PBXBuildFile; fileRef = D55A1B7F179F262300625D7C /* SDL_cocoamousetap.h */; };
     1.8 +		D55A1B82179F262300625D7C /* SDL_cocoamousetap.m in Sources */ = {isa = PBXBuildFile; fileRef = D55A1B80179F262300625D7C /* SDL_cocoamousetap.m */; };
     1.9 +		D55A1B83179F263500625D7C /* SDL_cocoamousetap.m in Sources */ = {isa = PBXBuildFile; fileRef = D55A1B80179F262300625D7C /* SDL_cocoamousetap.m */; };
    1.10 +		D55A1B84179F263600625D7C /* SDL_cocoamousetap.m in Sources */ = {isa = PBXBuildFile; fileRef = D55A1B80179F262300625D7C /* SDL_cocoamousetap.m */; };
    1.11 +		D55A1B85179F278E00625D7C /* SDL_cocoamousetap.h in Headers */ = {isa = PBXBuildFile; fileRef = D55A1B7F179F262300625D7C /* SDL_cocoamousetap.h */; };
    1.12 +		D55A1B86179F278F00625D7C /* SDL_cocoamousetap.h in Headers */ = {isa = PBXBuildFile; fileRef = D55A1B7F179F262300625D7C /* SDL_cocoamousetap.h */; };
    1.13  		DB313F7417554B71006C0E22 /* SDL_diskaudio.h in Headers */ = {isa = PBXBuildFile; fileRef = 04BDFD8912E6671700899322 /* SDL_diskaudio.h */; };
    1.14  		DB313F7517554B71006C0E22 /* SDL_dummyaudio.h in Headers */ = {isa = PBXBuildFile; fileRef = 04BDFD9512E6671700899322 /* SDL_dummyaudio.h */; };
    1.15  		DB313F7617554B71006C0E22 /* SDL_coreaudio.h in Headers */ = {isa = PBXBuildFile; fileRef = 04BDFDA112E6671700899322 /* SDL_coreaudio.h */; };
    1.16 @@ -1044,6 +1050,8 @@
    1.17  		BECDF66C0761BA81005FE872 /* SDL2.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDL2.framework; sourceTree = BUILT_PRODUCTS_DIR; };
    1.18  		BECDF6B30761BA81005FE872 /* libSDL2.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libSDL2.a; sourceTree = BUILT_PRODUCTS_DIR; };
    1.19  		BECDF6BE0761BA81005FE872 /* Standard DMG */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = "Standard DMG"; sourceTree = BUILT_PRODUCTS_DIR; };
    1.20 +		D55A1B7F179F262300625D7C /* SDL_cocoamousetap.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SDL_cocoamousetap.h; sourceTree = "<group>"; };
    1.21 +		D55A1B80179F262300625D7C /* SDL_cocoamousetap.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SDL_cocoamousetap.m; sourceTree = "<group>"; };
    1.22  		DB31407717554B71006C0E22 /* libSDL2.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libSDL2.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
    1.23  		F59C70FF00D5CB5801000001 /* ReadMe.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = ReadMe.txt; sourceTree = "<group>"; };
    1.24  		F59C710000D5CB5801000001 /* Welcome.txt */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = text; path = Welcome.txt; sourceTree = "<group>"; };
    1.25 @@ -1534,6 +1542,8 @@
    1.26  				04BDFEC912E6671800899322 /* SDL_cocoamodes.m */,
    1.27  				04BDFECA12E6671800899322 /* SDL_cocoamouse.h */,
    1.28  				04BDFECB12E6671800899322 /* SDL_cocoamouse.m */,
    1.29 +				D55A1B7F179F262300625D7C /* SDL_cocoamousetap.h */,
    1.30 +				D55A1B80179F262300625D7C /* SDL_cocoamousetap.m */,
    1.31  				04BDFECC12E6671800899322 /* SDL_cocoaopengl.h */,
    1.32  				04BDFECD12E6671800899322 /* SDL_cocoaopengl.m */,
    1.33  				04BDFECE12E6671800899322 /* SDL_cocoashape.h */,
    1.34 @@ -1611,8 +1621,11 @@
    1.35  				BEC562FE0761C0E800A33029 /* Linked Frameworks */,
    1.36  			);
    1.37  			comments = "To build Universal Binaries, we have experimented with a variety of different options.\nThe complication is that we must retain compatibility with at least 10.2. \nThe Universal Binary defaults only work for > 10.3.9\n\nSo far, we have found:\ngcc 4.0.0 with Xcode 2.1 always links against libgcc_s. gcc 4.0.1 from Xcode 2.2 fixes this problem.\n\nBut gcc 4.0 will not work with < 10.3.9 because we continue to get an undefined symbol to _fprintf$LDBL128.\nSo we must use gcc 3.3 on PPC to accomplish 10.2 support. (But 4.0 is required for i386.)\n\nSetting the deployment target to 10.4 will disable prebinding, so for PPC, we set it less than 10.4 to preserve prebinding for legacy support.\n\nSetting the PPC SDKROOT to /Developers/SDKs/MacOSX10.2.8.sdk will link to 63.0.0 libSystem.B.dylib. Leaving it at current or 10.4u links to 88.1.2. However, as long as we are using gcc 3.3, it doesn't seem to matter as testing has demonstrated both will run. We have decided not to invoke the 10.2.8 SDK because it is not a default installed component with Xcode which will probably cause most people problems. However, rather than deleting the SDKROOT_ppc entry entirely, we have mapped it to 10.4u in case we decide we need to change this setting.\n\nTo use Altivec or SSE, we needed architecture specific flags:\nOTHER_CFLAGS_ppc\nOTHER_CFLAGS_i386\nOTHER_CFLAGS=$(OTHER_CFLAGS_($CURRENT_ARCH))\n\nThe general OTHER_CFLAGS needed to be manually mapped to architecture specific options because Xcode didn't do this automatically for us.\n\n\n";
    1.38 +			indentWidth = 4;
    1.39  			name = SDLFramework;
    1.40  			sourceTree = "<group>";
    1.41 +			tabWidth = 4;
    1.42 +			usesTabs = 0;
    1.43  		};
    1.44  		089C1665FE841158C02AAC07 /* Resources */ = {
    1.45  			isa = PBXGroup;
    1.46 @@ -1842,6 +1855,7 @@
    1.47  				AA628AD3159367F2005138DD /* SDL_x11xinput2.h in Headers */,
    1.48  				AABCC38D164063D200AB8930 /* SDL_cocoamessagebox.h in Headers */,
    1.49  				AADA5B8716CCAB3000107CF7 /* SDL_bits.h in Headers */,
    1.50 +				D55A1B81179F262300625D7C /* SDL_cocoamousetap.h in Headers */,
    1.51  			);
    1.52  			runOnlyForDeploymentPostprocessing = 0;
    1.53  		};
    1.54 @@ -1985,6 +1999,7 @@
    1.55  				AABCC38E164063D200AB8930 /* SDL_cocoamessagebox.h in Headers */,
    1.56  				A77E6EB5167AB0A90010E40B /* SDL_gamecontroller.h in Headers */,
    1.57  				AADA5B8816CCAB3000107CF7 /* SDL_bits.h in Headers */,
    1.58 +				D55A1B85179F278E00625D7C /* SDL_cocoamousetap.h in Headers */,
    1.59  			);
    1.60  			runOnlyForDeploymentPostprocessing = 0;
    1.61  		};
    1.62 @@ -2128,6 +2143,7 @@
    1.63  				DB313FFA17554B71006C0E22 /* SDL_cocoamessagebox.h in Headers */,
    1.64  				DB313FFB17554B71006C0E22 /* SDL_gamecontroller.h in Headers */,
    1.65  				DB313FFC17554B71006C0E22 /* SDL_bits.h in Headers */,
    1.66 +				D55A1B86179F278F00625D7C /* SDL_cocoamousetap.h in Headers */,
    1.67  			);
    1.68  			runOnlyForDeploymentPostprocessing = 0;
    1.69  		};
    1.70 @@ -2410,6 +2426,7 @@
    1.71  				AABCC38F164063D200AB8930 /* SDL_cocoamessagebox.m in Sources */,
    1.72  				AA0AD09D16648D1700CE5896 /* SDL_gamecontroller.c in Sources */,
    1.73  				AA0F8491178D5ECC00823F9D /* SDL_systls.c in Sources */,
    1.74 +				D55A1B82179F262300625D7C /* SDL_cocoamousetap.m in Sources */,
    1.75  			);
    1.76  			runOnlyForDeploymentPostprocessing = 0;
    1.77  		};
    1.78 @@ -2526,6 +2543,7 @@
    1.79  				AA9E4094163BE51E007A2AD0 /* SDL_x11messagebox.c in Sources */,
    1.80  				AABCC390164063D200AB8930 /* SDL_cocoamessagebox.m in Sources */,
    1.81  				AA0F8492178D5ECC00823F9D /* SDL_systls.c in Sources */,
    1.82 +				D55A1B84179F263600625D7C /* SDL_cocoamousetap.m in Sources */,
    1.83  			);
    1.84  			runOnlyForDeploymentPostprocessing = 0;
    1.85  		};
    1.86 @@ -2642,6 +2660,7 @@
    1.87  				DB31406917554B71006C0E22 /* SDL_x11messagebox.c in Sources */,
    1.88  				DB31406A17554B71006C0E22 /* SDL_cocoamessagebox.m in Sources */,
    1.89  				AA0F8493178D5ECC00823F9D /* SDL_systls.c in Sources */,
    1.90 +				D55A1B83179F263500625D7C /* SDL_cocoamousetap.m in Sources */,
    1.91  			);
    1.92  			runOnlyForDeploymentPostprocessing = 0;
    1.93  		};
     2.1 --- a/src/video/cocoa/SDL_cocoamouse.h	Wed Aug 07 14:00:41 2013 -0700
     2.2 +++ b/src/video/cocoa/SDL_cocoamouse.h	Wed Aug 07 16:29:15 2013 -0700
     2.3 @@ -33,6 +33,7 @@
     2.4  typedef struct {
     2.5      int deltaXOffset;
     2.6      int deltaYOffset;
     2.7 +    void *tapdata;
     2.8  } SDL_MouseData;
     2.9  
    2.10  @interface NSCursor (InvisibleCursor)
     3.1 --- a/src/video/cocoa/SDL_cocoamouse.m	Wed Aug 07 14:00:41 2013 -0700
     3.2 +++ b/src/video/cocoa/SDL_cocoamouse.m	Wed Aug 07 16:29:15 2013 -0700
     3.3 @@ -25,6 +25,7 @@
     3.4  #include "SDL_assert.h"
     3.5  #include "SDL_events.h"
     3.6  #include "SDL_cocoamouse.h"
     3.7 +#include "SDL_cocoamousetap.h"
     3.8  
     3.9  #include "../../events/SDL_mouse_c.h"
    3.10  
    3.11 @@ -94,6 +95,8 @@
    3.12          cursor = SDL_calloc(1, sizeof(*cursor));
    3.13          if (cursor) {
    3.14              cursor->driverdata = nscursor;
    3.15 +        } else {
    3.16 +            [nscursor release];
    3.17          }
    3.18      }
    3.19  
    3.20 @@ -266,6 +269,8 @@
    3.21      mouse->SetRelativeMouseMode = Cocoa_SetRelativeMouseMode;
    3.22  
    3.23      SDL_SetDefaultCursor(Cocoa_CreateDefaultCursor());
    3.24 +
    3.25 +    Cocoa_InitMouseEventTap(mouse->driverdata);
    3.26  }
    3.27  
    3.28  void
    3.29 @@ -313,6 +318,10 @@
    3.30  {
    3.31      SDL_Mouse *mouse = SDL_GetMouse();
    3.32      if (mouse) {
    3.33 +        if (mouse->driverdata) {
    3.34 +            Cocoa_QuitMouseEventTap(((SDL_MouseData*)mouse->driverdata));
    3.35 +        }
    3.36 +
    3.37          SDL_free(mouse->driverdata);
    3.38      }
    3.39  }
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/video/cocoa/SDL_cocoamousetap.h	Wed Aug 07 16:29:15 2013 -0700
     4.3 @@ -0,0 +1,33 @@
     4.4 +/*
     4.5 +  Simple DirectMedia Layer
     4.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4.7 +
     4.8 +  This software is provided 'as-is', without any express or implied
     4.9 +  warranty.  In no event will the authors be held liable for any damages
    4.10 +  arising from the use of this software.
    4.11 +
    4.12 +  Permission is granted to anyone to use this software for any purpose,
    4.13 +  including commercial applications, and to alter it and redistribute it
    4.14 +  freely, subject to the following restrictions:
    4.15 +
    4.16 +  1. The origin of this software must not be misrepresented; you must not
    4.17 +     claim that you wrote the original software. If you use this software
    4.18 +     in a product, an acknowledgment in the product documentation would be
    4.19 +     appreciated but is not required.
    4.20 +  2. Altered source versions must be plainly marked as such, and must not be
    4.21 +     misrepresented as being the original software.
    4.22 +  3. This notice may not be removed or altered from any source distribution.
    4.23 +*/
    4.24 +#include "SDL_config.h"
    4.25 +
    4.26 +#ifndef _SDL_cocoamousetap_h
    4.27 +#define _SDL_cocoamousetap_h
    4.28 +
    4.29 +#include "SDL_cocoamouse.h"
    4.30 +
    4.31 +extern void Cocoa_InitMouseEventTap(SDL_MouseData *driverdata);
    4.32 +extern void Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata);
    4.33 +
    4.34 +#endif /* _SDL_cocoamousetap_h */
    4.35 +
    4.36 +/* vi: set ts=4 sw=4 expandtab: */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/video/cocoa/SDL_cocoamousetap.m	Wed Aug 07 16:29:15 2013 -0700
     5.3 @@ -0,0 +1,252 @@
     5.4 +/*
     5.5 +  Simple DirectMedia Layer
     5.6 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     5.7 +
     5.8 +  This software is provided 'as-is', without any express or implied
     5.9 +  warranty.  In no event will the authors be held liable for any damages
    5.10 +  arising from the use of this software.
    5.11 +
    5.12 +  Permission is granted to anyone to use this software for any purpose,
    5.13 +  including commercial applications, and to alter it and redistribute it
    5.14 +  freely, subject to the following restrictions:
    5.15 +
    5.16 +  1. The origin of this software must not be misrepresented; you must not
    5.17 +     claim that you wrote the original software. If you use this software
    5.18 +     in a product, an acknowledgment in the product documentation would be
    5.19 +     appreciated but is not required.
    5.20 +  2. Altered source versions must be plainly marked as such, and must not be
    5.21 +     misrepresented as being the original software.
    5.22 +  3. This notice may not be removed or altered from any source distribution.
    5.23 +*/
    5.24 +#include "SDL_config.h"
    5.25 +
    5.26 +#if SDL_VIDEO_DRIVER_COCOA
    5.27 +
    5.28 +#define SDL_MAC_NO_SANDBOX 1
    5.29 +
    5.30 +#include "SDL_cocoamousetap.h"
    5.31 +
    5.32 +/* Event taps are forbidden in the Mac App Store, so we can only enable this
    5.33 + * code if your app doesn't need to ship through the app store.
    5.34 + * This code makes it so that a grabbed cursor cannot "leak" a mouse click
    5.35 + * past the edge of the window if moving the cursor too fast.
    5.36 + */
    5.37 +#if SDL_MAC_NO_SANDBOX
    5.38 +
    5.39 +#include "SDL_keyboard.h"
    5.40 +#include "SDL_thread.h"
    5.41 +#include "SDL_cocoavideo.h"
    5.42 +
    5.43 +#include "../../events/SDL_mouse_c.h"
    5.44 +
    5.45 +typedef struct {
    5.46 +    CFMachPortRef tap;
    5.47 +    CFRunLoopRef runloop;
    5.48 +    CFRunLoopSourceRef runloopSource;
    5.49 +    SDL_Thread *thread;
    5.50 +    SDL_sem *runloopStartedSemaphore;
    5.51 +} SDL_MouseEventTapData;
    5.52 +
    5.53 +static const CGEventMask movementEventsMask =
    5.54 +      CGEventMaskBit(kCGEventLeftMouseDragged)
    5.55 +    | CGEventMaskBit(kCGEventRightMouseDragged)
    5.56 +    | CGEventMaskBit(kCGEventMouseMoved);
    5.57 +
    5.58 +static const CGEventMask allGrabbedEventsMask =
    5.59 +      CGEventMaskBit(kCGEventLeftMouseDown)    | CGEventMaskBit(kCGEventLeftMouseUp)
    5.60 +    | CGEventMaskBit(kCGEventRightMouseDown)   | CGEventMaskBit(kCGEventRightMouseUp)
    5.61 +    | CGEventMaskBit(kCGEventOtherMouseDown)   | CGEventMaskBit(kCGEventOtherMouseUp)
    5.62 +    | CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged)
    5.63 +    | CGEventMaskBit(kCGEventMouseMoved);
    5.64 +
    5.65 +static CGEventRef
    5.66 +Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
    5.67 +{
    5.68 +    SDL_MouseData *driverdata = (SDL_MouseData*)refcon;
    5.69 +    SDL_Mouse *mouse = SDL_GetMouse();
    5.70 +    SDL_Window *window = SDL_GetKeyboardFocus();
    5.71 +    NSRect windowRect;
    5.72 +    CGPoint eventLocation;
    5.73 +
    5.74 +    switch (type)
    5.75 +    {
    5.76 +        case kCGEventTapDisabledByTimeout:
    5.77 +        case kCGEventTapDisabledByUserInput:
    5.78 +            {
    5.79 +                CGEventTapEnable(((SDL_MouseEventTapData*)(driverdata->tapdata))->tap, true);
    5.80 +                return NULL;
    5.81 +            }
    5.82 +        default:
    5.83 +            break;
    5.84 +    }
    5.85 +
    5.86 +
    5.87 +    if (!window || !mouse) {
    5.88 +        return event;
    5.89 +    }
    5.90 +
    5.91 +    if (mouse->relative_mode) {
    5.92 +        return event;
    5.93 +    }
    5.94 +
    5.95 +    if (!(window->flags & SDL_WINDOW_INPUT_GRABBED)) {
    5.96 +        return event;
    5.97 +    }
    5.98 +
    5.99 +    /* This is the same coordinate system as Cocoa uses. */
   5.100 +    eventLocation = CGEventGetUnflippedLocation(event);
   5.101 +    windowRect = [((SDL_WindowData *) window->driverdata)->nswindow frame];
   5.102 +
   5.103 +    if (!NSPointInRect(NSPointFromCGPoint(eventLocation), windowRect)) {
   5.104 +
   5.105 +        /* This is in CGs global screenspace coordinate system, which has a
   5.106 +         * flipped Y.
   5.107 +         */
   5.108 +        CGPoint newLocation = CGEventGetLocation(event);
   5.109 +
   5.110 +        if (eventLocation.x < NSMinX(windowRect)) {
   5.111 +            newLocation.x = NSMinX(windowRect);
   5.112 +        } else if (eventLocation.x >= NSMaxX(windowRect)) {
   5.113 +            newLocation.x = NSMaxX(windowRect) - 1.0;
   5.114 +        }
   5.115 +
   5.116 +        if (eventLocation.y < NSMinY(windowRect)) {
   5.117 +            newLocation.y -= (NSMinY(windowRect) - eventLocation.y + 1);
   5.118 +        } else if (eventLocation.y >= NSMaxY(windowRect)) {
   5.119 +            newLocation.y += (eventLocation.y - NSMaxY(windowRect) + 1);
   5.120 +        }
   5.121 +
   5.122 +        CGSetLocalEventsSuppressionInterval(0);
   5.123 +        CGWarpMouseCursorPosition(newLocation);
   5.124 +        CGSetLocalEventsSuppressionInterval(0.25);
   5.125 +
   5.126 +        if ((CGEventMaskBit(type) & movementEventsMask) == 0) {
   5.127 +            /* For click events, we just constrain the event to the window, so
   5.128 +             * no other app receives the click event. We can't due the same to
   5.129 +             * movement events, since they mean that our warp cursor above
   5.130 +             * behaves strangely.
   5.131 +             */
   5.132 +            CGEventSetLocation(event, newLocation);
   5.133 +        }
   5.134 +    }
   5.135 +
   5.136 +    return event;
   5.137 +}
   5.138 +
   5.139 +static int
   5.140 +Cocoa_MouseTapThread(void *data)
   5.141 +{
   5.142 +    SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)data;
   5.143 +
   5.144 +    /* Create a tap. */
   5.145 +    CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap,
   5.146 +                                              kCGEventTapOptionDefault, allGrabbedEventsMask,
   5.147 +                                              &Cocoa_MouseTapCallback, tapdata);
   5.148 +    if (eventTap) {
   5.149 +        /* Try to create a runloop source we can schedule. */
   5.150 +        CFRunLoopSourceRef runloopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
   5.151 +        if  (runloopSource) {
   5.152 +            tapdata->tap = eventTap;
   5.153 +            tapdata->runloopSource = runloopSource;
   5.154 +        } else {
   5.155 +            CFRelease(eventTap);
   5.156 +            SDL_SemPost(tapdata->runloopStartedSemaphore);
   5.157 +            /* TODO: Both here and in the return below, set some state in
   5.158 +             * tapdata to indicate that initialization failed, which we should
   5.159 +             * check in InitMouseEventTap, after we move the semaphore check
   5.160 +             * from Quit to Init.
   5.161 +             */
   5.162 +            return 1;
   5.163 +        }
   5.164 +    } else {
   5.165 +        SDL_SemPost(tapdata->runloopStartedSemaphore);
   5.166 +        return 1;
   5.167 +    }
   5.168 +
   5.169 +    tapdata->runloop = CFRunLoopGetCurrent();
   5.170 +    CFRunLoopAddSource(tapdata->runloop, tapdata->runloopSource, kCFRunLoopCommonModes);
   5.171 +    CFRunLoopPerformBlock(tapdata->runloop, kCFRunLoopCommonModes, ^{
   5.172 +        /* We signal this *after* the run loop has started, indicating it's safe to CFRunLoopStop it. */
   5.173 +        SDL_SemPost(tapdata->runloopStartedSemaphore);
   5.174 +    });
   5.175 +
   5.176 +    /* Run the event loop to handle events in the event tap. */
   5.177 +    CFRunLoopRun();
   5.178 +    /* Make sure this is signaled so that SDL_QuitMouseEventTap knows it can safely SDL_WaitThread for us. */
   5.179 +    if (SDL_SemValue(tapdata->runloopStartedSemaphore) < 1) {
   5.180 +        SDL_SemPost(tapdata->runloopStartedSemaphore);
   5.181 +    }
   5.182 +    CFRunLoopRemoveSource(tapdata->runloop, tapdata->runloopSource, kCFRunLoopCommonModes);
   5.183 +
   5.184 +    /* Clean up. */
   5.185 +    CGEventTapEnable(tapdata->tap, false);
   5.186 +    CFRelease(tapdata->runloopSource);
   5.187 +    CFRelease(tapdata->tap);
   5.188 +    tapdata->runloopSource = NULL;
   5.189 +    tapdata->tap = NULL;
   5.190 +
   5.191 +    return 0;
   5.192 +}
   5.193 +
   5.194 +void
   5.195 +Cocoa_InitMouseEventTap(SDL_MouseData* driverdata)
   5.196 +{
   5.197 +    SDL_MouseEventTapData *tapdata;
   5.198 +    driverdata->tapdata = SDL_calloc(1, sizeof(SDL_MouseEventTapData));
   5.199 +    tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
   5.200 +
   5.201 +    tapdata->runloopStartedSemaphore = SDL_CreateSemaphore(0);
   5.202 +    if (tapdata->runloopStartedSemaphore) {
   5.203 +        tapdata->thread = SDL_CreateThread(&Cocoa_MouseTapThread, "Event Tap Loop", tapdata);
   5.204 +        if (!tapdata->thread) {
   5.205 +            SDL_DestroySemaphore(tapdata->runloopStartedSemaphore);
   5.206 +        }
   5.207 +    }
   5.208 +
   5.209 +    if (!tapdata->thread) {
   5.210 +        SDL_free(driverdata->tapdata);
   5.211 +        driverdata->tapdata = NULL;
   5.212 +    }
   5.213 +}
   5.214 +
   5.215 +void
   5.216 +Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
   5.217 +{
   5.218 +    SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
   5.219 +    int status;
   5.220 +
   5.221 +    /* Ensure that the runloop has been started first.
   5.222 +     * TODO: Move this to InitMouseEventTap, check for error conditions that can
   5.223 +     * happen in Cocoa_MouseTapThread, and fall back to the non-EventTap way of
   5.224 +     * grabbing the mouse if it fails to Init.
   5.225 +     */
   5.226 +    status = SDL_SemWaitTimeout(tapdata->runloopStartedSemaphore, 5000);
   5.227 +    if (status > -1) {
   5.228 +        /* Then stop it, which will cause Cocoa_MouseTapThread to return. */
   5.229 +        CFRunLoopStop(tapdata->runloop);
   5.230 +        /* And then wait for Cocoa_MouseTapThread to finish cleaning up. It
   5.231 +         * releases some of the pointers in tapdata. */
   5.232 +        SDL_WaitThread(tapdata->thread, &status);
   5.233 +    }
   5.234 +
   5.235 +    SDL_free(driverdata->tapdata);
   5.236 +    driverdata->tapdata = NULL;
   5.237 +}
   5.238 +
   5.239 +#else /* SDL_MAC_NO_SANDBOX */
   5.240 +
   5.241 +void
   5.242 +Cocoa_InitMouseEventTap(SDL_MouseData *unused)
   5.243 +{
   5.244 +}
   5.245 +
   5.246 +void
   5.247 +Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
   5.248 +{
   5.249 +}
   5.250 +
   5.251 +#endif /* !SDL_MAC_NO_SANDBOX */
   5.252 +
   5.253 +#endif /* SDL_VIDEO_DRIVER_COCOA */
   5.254 +
   5.255 +/* vi: set ts=4 sw=4 expandtab: */
     6.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Wed Aug 07 14:00:41 2013 -0700
     6.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Wed Aug 07 16:29:15 2013 -0700
     6.3 @@ -414,6 +414,11 @@
     6.4                  y = window->h - 1;
     6.5              }
     6.6  
     6.7 +#if !SDL_MAC_NO_SANDBOX
     6.8 +            /* When SDL_MAC_NO_SANDBOX is set, this is handled by
     6.9 +             * SDL_cocoamousetap.m.
    6.10 +             */
    6.11 +
    6.12              cgpoint.x = window->x + x;
    6.13              cgpoint.y = window->y + y;
    6.14  
    6.15 @@ -424,6 +429,7 @@
    6.16              CGSetLocalEventsSuppressionInterval(0.0);
    6.17              CGDisplayMoveCursorToPoint(kCGDirectMainDisplay, cgpoint);
    6.18              CGSetLocalEventsSuppressionInterval(0.25);
    6.19 +#endif
    6.20          }
    6.21      }
    6.22      SDL_SendMouseMotion(window, 0, 0, x, y);