2 Simple DirectMedia Layer
3 Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "../../SDL_internal.h"
23 #if SDL_VIDEO_DRIVER_COCOA
25 #include "SDL_cocoamousetap.h"
27 /* Event taps are forbidden in the Mac App Store, so we can only enable this
28 * code if your app doesn't need to ship through the app store.
29 * This code makes it so that a grabbed cursor cannot "leak" a mouse click
30 * past the edge of the window if moving the cursor too fast.
32 #if SDL_MAC_NO_SANDBOX
34 #include "SDL_keyboard.h"
35 #include "SDL_cocoavideo.h"
36 #include "../../thread/SDL_systhread.h"
38 #include "../../events/SDL_mouse_c.h"
43 CFRunLoopSourceRef runloopSource;
45 SDL_sem *runloopStartedSemaphore;
46 } SDL_MouseEventTapData;
48 static const CGEventMask movementEventsMask =
49 CGEventMaskBit(kCGEventLeftMouseDragged)
50 | CGEventMaskBit(kCGEventRightMouseDragged)
51 | CGEventMaskBit(kCGEventMouseMoved);
53 static const CGEventMask allGrabbedEventsMask =
54 CGEventMaskBit(kCGEventLeftMouseDown) | CGEventMaskBit(kCGEventLeftMouseUp)
55 | CGEventMaskBit(kCGEventRightMouseDown) | CGEventMaskBit(kCGEventRightMouseUp)
56 | CGEventMaskBit(kCGEventOtherMouseDown) | CGEventMaskBit(kCGEventOtherMouseUp)
57 | CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged)
58 | CGEventMaskBit(kCGEventMouseMoved);
61 Cocoa_MouseTapCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon)
63 SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)refcon;
64 SDL_Mouse *mouse = SDL_GetMouse();
65 SDL_Window *window = SDL_GetKeyboardFocus();
68 CGPoint eventLocation;
71 case kCGEventTapDisabledByTimeout:
73 CGEventTapEnable(tapdata->tap, true);
76 case kCGEventTapDisabledByUserInput:
85 if (!window || !mouse) {
89 if (mouse->relative_mode) {
93 if (!(window->flags & SDL_WINDOW_INPUT_GRABBED)) {
97 /* This is the same coordinate system as Cocoa uses. */
98 nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
99 eventLocation = CGEventGetUnflippedLocation(event);
100 windowRect = [nswindow contentRectForFrameRect:[nswindow frame]];
102 if (!NSMouseInRect(NSPointFromCGPoint(eventLocation), windowRect, NO)) {
104 /* This is in CGs global screenspace coordinate system, which has a
107 CGPoint newLocation = CGEventGetLocation(event);
109 if (eventLocation.x < NSMinX(windowRect)) {
110 newLocation.x = NSMinX(windowRect);
111 } else if (eventLocation.x >= NSMaxX(windowRect)) {
112 newLocation.x = NSMaxX(windowRect) - 1.0;
115 if (eventLocation.y <= NSMinY(windowRect)) {
116 newLocation.y -= (NSMinY(windowRect) - eventLocation.y + 1);
117 } else if (eventLocation.y > NSMaxY(windowRect)) {
118 newLocation.y += (eventLocation.y - NSMaxY(windowRect));
121 CGWarpMouseCursorPosition(newLocation);
122 CGAssociateMouseAndMouseCursorPosition(YES);
124 if ((CGEventMaskBit(type) & movementEventsMask) == 0) {
125 /* For click events, we just constrain the event to the window, so
126 * no other app receives the click event. We can't due the same to
127 * movement events, since they mean that our warp cursor above
130 CGEventSetLocation(event, newLocation);
138 SemaphorePostCallback(CFRunLoopTimerRef timer, void *info)
140 SDL_SemPost((SDL_sem*)info);
144 Cocoa_MouseTapThread(void *data)
146 SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)data;
148 /* Tap was created on main thread but we own it now. */
149 CFMachPortRef eventTap = tapdata->tap;
151 /* Try to create a runloop source we can schedule. */
152 CFRunLoopSourceRef runloopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
154 tapdata->runloopSource = runloopSource;
157 SDL_SemPost(tapdata->runloopStartedSemaphore);
158 /* TODO: Both here and in the return below, set some state in
159 * tapdata to indicate that initialization failed, which we should
160 * check in InitMouseEventTap, after we move the semaphore check
166 SDL_SemPost(tapdata->runloopStartedSemaphore);
170 tapdata->runloop = CFRunLoopGetCurrent();
171 CFRunLoopAddSource(tapdata->runloop, tapdata->runloopSource, kCFRunLoopCommonModes);
172 CFRunLoopTimerContext context = {.info = tapdata->runloopStartedSemaphore};
173 /* We signal the runloop started semaphore *after* the run loop has started, indicating it's safe to CFRunLoopStop it. */
174 CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, CFAbsoluteTimeGetCurrent(), 0, 0, 0, &SemaphorePostCallback, &context);
175 CFRunLoopAddTimer(tapdata->runloop, timer, kCFRunLoopCommonModes);
178 /* Run the event loop to handle events in the event tap. */
180 /* Make sure this is signaled so that SDL_QuitMouseEventTap knows it can safely SDL_WaitThread for us. */
181 if (SDL_SemValue(tapdata->runloopStartedSemaphore) < 1) {
182 SDL_SemPost(tapdata->runloopStartedSemaphore);
184 CFRunLoopRemoveSource(tapdata->runloop, tapdata->runloopSource, kCFRunLoopCommonModes);
187 CGEventTapEnable(tapdata->tap, false);
188 CFRelease(tapdata->runloopSource);
189 CFRelease(tapdata->tap);
190 tapdata->runloopSource = NULL;
197 Cocoa_InitMouseEventTap(SDL_MouseData* driverdata)
199 SDL_MouseEventTapData *tapdata;
200 driverdata->tapdata = SDL_calloc(1, sizeof(SDL_MouseEventTapData));
201 tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
203 tapdata->runloopStartedSemaphore = SDL_CreateSemaphore(0);
204 if (tapdata->runloopStartedSemaphore) {
205 tapdata->tap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap,
206 kCGEventTapOptionDefault, allGrabbedEventsMask,
207 &Cocoa_MouseTapCallback, tapdata);
209 /* Tap starts disabled, until app requests mouse grab */
210 CGEventTapEnable(tapdata->tap, false);
211 tapdata->thread = SDL_CreateThreadInternal(&Cocoa_MouseTapThread, "Event Tap Loop", 512 * 1024, tapdata);
212 if (tapdata->thread) {
213 /* Success - early out. Ownership transferred to thread. */
216 CFRelease(tapdata->tap);
218 SDL_DestroySemaphore(tapdata->runloopStartedSemaphore);
220 SDL_free(driverdata->tapdata);
221 driverdata->tapdata = NULL;
225 Cocoa_EnableMouseEventTap(SDL_MouseData *driverdata, SDL_bool enabled)
227 SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
228 if (tapdata && tapdata->tap)
230 CGEventTapEnable(tapdata->tap, !!enabled);
235 Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
237 SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
240 /* Ensure that the runloop has been started first.
241 * TODO: Move this to InitMouseEventTap, check for error conditions that can
242 * happen in Cocoa_MouseTapThread, and fall back to the non-EventTap way of
243 * grabbing the mouse if it fails to Init.
245 status = SDL_SemWaitTimeout(tapdata->runloopStartedSemaphore, 5000);
247 /* Then stop it, which will cause Cocoa_MouseTapThread to return. */
248 CFRunLoopStop(tapdata->runloop);
249 /* And then wait for Cocoa_MouseTapThread to finish cleaning up. It
250 * releases some of the pointers in tapdata. */
251 SDL_WaitThread(tapdata->thread, &status);
254 SDL_free(driverdata->tapdata);
255 driverdata->tapdata = NULL;
258 #else /* SDL_MAC_NO_SANDBOX */
261 Cocoa_InitMouseEventTap(SDL_MouseData *unused)
266 Cocoa_EnableMouseEventTap(SDL_MouseData *driverdata, SDL_bool enabled)
271 Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
275 #endif /* !SDL_MAC_NO_SANDBOX */
277 #endif /* SDL_VIDEO_DRIVER_COCOA */
279 /* vi: set ts=4 sw=4 expandtab: */