Navigation Menu

Skip to content

Commit

Permalink
SDL for Mac - only enable global event tap when actually necessary (a…
Browse files Browse the repository at this point in the history
…pp has focus and has requested relative mouse mode or has asked for a mouse grab). in other situations the event tap impacts system performance and battery life with no benefit.
  • Loading branch information
slouken committed Nov 26, 2016
1 parent ff56c7b commit 354a8f2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/video/cocoa/SDL_cocoamousetap.h
Expand Up @@ -26,6 +26,7 @@
#include "SDL_cocoamouse.h"

extern void Cocoa_InitMouseEventTap(SDL_MouseData *driverdata);
extern void Cocoa_EnableMouseEventTap(SDL_MouseData *driverdata, SDL_bool enabled);
extern void Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata);

#endif /* _SDL_cocoamousetap_h */
Expand Down
39 changes: 28 additions & 11 deletions src/video/cocoa/SDL_cocoamousetap.m
Expand Up @@ -142,15 +142,12 @@
{
SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)data;

/* Create a tap. */
CFMachPortRef eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap,
kCGEventTapOptionDefault, allGrabbedEventsMask,
&Cocoa_MouseTapCallback, tapdata);
/* Tap was created on main thread but we own it now. */
CFMachPortRef eventTap = tapdata->tap;
if (eventTap) {
/* Try to create a runloop source we can schedule. */
CFRunLoopSourceRef runloopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
if (runloopSource) {
tapdata->tap = eventTap;
tapdata->runloopSource = runloopSource;
} else {
CFRelease(eventTap);
Expand Down Expand Up @@ -202,15 +199,30 @@

tapdata->runloopStartedSemaphore = SDL_CreateSemaphore(0);
if (tapdata->runloopStartedSemaphore) {
tapdata->thread = SDL_CreateThreadInternal(&Cocoa_MouseTapThread, "Event Tap Loop", 512 * 1024, tapdata);
if (!tapdata->thread) {
SDL_DestroySemaphore(tapdata->runloopStartedSemaphore);
tapdata->tap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap,
kCGEventTapOptionDefault, allGrabbedEventsMask,
&Cocoa_MouseTapCallback, tapdata);
if (tapdata->tap) {
tapdata->thread = SDL_CreateThreadInternal(&Cocoa_MouseTapThread, "Event Tap Loop", 512 * 1024, tapdata);
if (tapdata->thread) {
/* Success - early out. Ownership transferred to thread. */
return;
}
CFRelease(tapdata->tap);
}
SDL_DestroySemaphore(tapdata->runloopStartedSemaphore);
}
SDL_free(driverdata->tapdata);
driverdata->tapdata = NULL;
}

if (!tapdata->thread) {
SDL_free(driverdata->tapdata);
driverdata->tapdata = NULL;
void
Cocoa_EnableMouseEventTap(SDL_MouseData *driverdata, SDL_bool enabled)
{
SDL_MouseEventTapData *tapdata = (SDL_MouseEventTapData*)driverdata->tapdata;
if (tapdata && tapdata->tap)
{
CGEventTapEnable(tapdata->tap, enabled);
}
}

Expand Down Expand Up @@ -245,6 +257,11 @@
{
}

void
Cocoa_EnableMouseEventTap(SDL_MouseData *driverdata, SDL_bool enabled)
{
}

void
Cocoa_QuitMouseEventTap(SDL_MouseData *driverdata)
{
Expand Down
8 changes: 7 additions & 1 deletion src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -38,6 +38,7 @@
#include "SDL_cocoavideo.h"
#include "SDL_cocoashape.h"
#include "SDL_cocoamouse.h"
#include "SDL_cocoamousetap.h"
#include "SDL_cocoaopengl.h"
#include "SDL_assert.h"

Expand Down Expand Up @@ -1634,8 +1635,13 @@ - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
void
Cocoa_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
{
/* Move the cursor to the nearest point in the window */
SDL_Mouse *mouse = SDL_GetMouse();
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;

/* Enable or disable the event tap as necessary */
Cocoa_EnableMouseEventTap(mouse->driverdata, grabbed);

/* Move the cursor to the nearest point in the window */
if (grabbed && data && ![data->listener isMoving]) {
int x, y;
CGPoint cgpoint;
Expand Down

0 comments on commit 354a8f2

Please sign in to comment.