Skip to content

Commit

Permalink
Less heavy checking of no built in display (laptop lid closed)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhuusko5 committed Mar 5, 2014
1 parent 87c53b2 commit 6766199
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 92 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Cocoa Mac Multitouch Support Wrapper.xcodeproj/project.xcworkspace/xcuserdata/mhuusko.xcuserdatad/UserInterfaceState.xcuserstate
51 changes: 2 additions & 49 deletions Cocoa Mac Multitouch Support Wrapper.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -226,55 +226,16 @@
D0F9EC3317E5119400224B34 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.7;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = macosx10.8;
};
name = Debug;
};
D0F9EC3417E5119400224B34 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
MACOSX_DEPLOYMENT_TARGET = 10.7;
SDKROOT = macosx10.8;
};
Expand All @@ -283,37 +244,29 @@
D0F9EC3617E5119400224B34 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"\"$(SYSTEM_LIBRARY_DIR)/PrivateFrameworks\"",
"$(SYSTEM_LIBRARY_DIR)/PrivateFrameworks",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Cocoa Mac Multitouch Support Wrapper/Cocoa Mac Multitouch Support Wrapper-Prefix.pch";
INFOPLIST_FILE = "Cocoa Mac Multitouch Support Wrapper/Cocoa Mac Multitouch Support Wrapper-Info.plist";
MACOSX_DEPLOYMENT_TARGET = 10.7;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx10.8;
WRAPPER_EXTENSION = app;
};
name = Debug;
};
D0F9EC3717E5119400224B34 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
COMBINE_HIDPI_IMAGES = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"\"$(SYSTEM_LIBRARY_DIR)/PrivateFrameworks\"",
"$(SYSTEM_LIBRARY_DIR)/PrivateFrameworks",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Cocoa Mac Multitouch Support Wrapper/Cocoa Mac Multitouch Support Wrapper-Prefix.pch";
INFOPLIST_FILE = "Cocoa Mac Multitouch Support Wrapper/Cocoa Mac Multitouch Support Wrapper-Info.plist";
MACOSX_DEPLOYMENT_TARGET = 10.7;
PRODUCT_NAME = "$(TARGET_NAME)";
SDKROOT = macosx10.8;
WRAPPER_EXTENSION = app;
};
name = Release;
Expand Down
Binary file not shown.
85 changes: 42 additions & 43 deletions Cocoa Mac Multitouch Support Wrapper/MultitouchManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,39 @@ @interface MultitouchManager ()
@property NSMutableArray *multitouchListeners;
@property NSMutableArray *multitouchDevices;
@property BOOL forwardingMultitouchEventsToListeners;
@property NSTimer *checkForMultitouchDevicesTimer;
@property NSTimer *multitouchHardwareCheckTimer;

@end

@implementation MultitouchManager

static BOOL laptopLidClosed = NO;
- (void)checkMultitouchHardware {
CGDirectDisplayID builtInDisplay = 0;
CGDirectDisplayID activeDisplays[10];
uint32_t numActiveDisplays;
CGGetActiveDisplayList(10, activeDisplays, &numActiveDisplays);

while (numActiveDisplays-- > 0) {
if (CGDisplayIsBuiltin(activeDisplays[numActiveDisplays])) {
builtInDisplay = activeDisplays[numActiveDisplays];
break;
}
}

laptopLidClosed = (builtInDisplay == 0);

NSArray *mtDevices = (NSArray *)CFBridgingRelease(MTDeviceCreateList());

int mtDeviceCount = (int)mtDevices.count;
if (mtDeviceCount != _multitouchDevices.count) {
[_multitouchHardwareCheckTimer invalidate];
_multitouchHardwareCheckTimer = nil;

[self restartMultitouchEventForwardingAfterWake:nil];
}
}

- (void)handleMultitouchEvent:(MultitouchEvent *)event {
if (_forwardingMultitouchEventsToListeners) {
int multitouchListenerCount = (int)_multitouchListeners.count;
Expand All @@ -21,18 +48,6 @@ - (void)handleMultitouchEvent:(MultitouchEvent *)event {
}
}

- (void)checkForMultitouchDevices {
NSArray *mtDevices = (NSArray *)CFBridgingRelease(MTDeviceCreateList());

int mtDeviceCount = (int)mtDevices.count;
if (mtDeviceCount != _multitouchDevices.count) {
[_checkForMultitouchDevicesTimer invalidate];
_checkForMultitouchDevicesTimer = nil;

[self restartMultitouchEventForwardingAfterWake:nil];
}
}

- (void)startForwardingMultitouchEventsToListeners {
if ([[NSThread currentThread] isMainThread]) {
if (!_forwardingMultitouchEventsToListeners && [MultitouchManager systemIsMultitouchCapable]) {
Expand All @@ -54,7 +69,7 @@ - (void)startForwardingMultitouchEventsToListeners {
[_multitouchDevices addObject:device];
}

_checkForMultitouchDevicesTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkForMultitouchDevices) userInfo:nil repeats:YES];
_multitouchHardwareCheckTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(checkMultitouchHardware) userInfo:nil repeats:YES];

_forwardingMultitouchEventsToListeners = YES;
}
Expand All @@ -67,8 +82,8 @@ - (void)startForwardingMultitouchEventsToListeners {
- (void)stopForwardingMultitouchEventsToListeners {
if ([[NSThread currentThread] isMainThread]) {
if (_forwardingMultitouchEventsToListeners) {
[_checkForMultitouchDevicesTimer invalidate];
_checkForMultitouchDevicesTimer = nil;
[_multitouchHardwareCheckTimer invalidate];
_multitouchHardwareCheckTimer = nil;

int multitouchDeviceCount = (int)_multitouchDevices.count;
while (multitouchDeviceCount-- > 0) {
Expand All @@ -95,6 +110,16 @@ - (void)stopForwardingMultitouchEventsToListeners {
}
}

- (void)restartMultitouchEventForwardingAfterWake:(NSNotification *)wakeNotification {
if ([[NSThread currentThread] isMainThread]) {
[self stopForwardingMultitouchEventsToListeners];
[self startForwardingMultitouchEventsToListeners];
}
else {
[self performSelectorOnMainThread:@selector(restartMultitouchEventForwardingAfterWake:) withObject:wakeNotification waitUntilDone:NO];
}
}

- (void)removeMultitouchListenersWithTarget:(id)target andCallback:(SEL)callback {
int multitouchListenerCount = (int)_multitouchListeners.count;
while (multitouchListenerCount-- > 0) {
Expand All @@ -113,24 +138,8 @@ - (void)addMultitouchListenerWithTarget:(id)target callback:(SEL)callback andThr
[self startForwardingMultitouchEventsToListeners];
}

static bool laptopLidClosed() {
CGDirectDisplayID builtInDisplay = 0;
CGDirectDisplayID activeDisplays[10];
uint32_t numActiveDisplays;
CGGetActiveDisplayList(10, activeDisplays, &numActiveDisplays);

while (numActiveDisplays-- > 0) {
if (CGDisplayIsBuiltin(activeDisplays[numActiveDisplays])) {
builtInDisplay = activeDisplays[numActiveDisplays];
break;
}
}

return (builtInDisplay == 0);
}

static void mtEventHandler(MTDeviceRef mtEventDevice, MTTouch mtEventTouches[], int mtEventTouchesNum, double mtEventTimestamp, int mtEventFrameId) {
if (MTDeviceIsBuiltIn && MTDeviceIsBuiltIn(mtEventDevice) && laptopLidClosed()) {
if (MTDeviceIsBuiltIn && MTDeviceIsBuiltIn(mtEventDevice) && laptopLidClosed) {
/*When a Mac laptop lid is closed, it can cause the trackpad to send random
multitouch input (insane, I know!). Obviously we want to ignore that input.*/
return;
Expand All @@ -147,16 +156,6 @@ multitouch input (insane, I know!). Obviously we want to ignore that input.*/
[[MultitouchManager sharedMultitouchManager] handleMultitouchEvent:multitouchEvent];
}

- (void)restartMultitouchEventForwardingAfterWake:(NSNotification *)wakeNotification {
if ([[NSThread currentThread] isMainThread]) {
[self stopForwardingMultitouchEventsToListeners];
[self startForwardingMultitouchEventsToListeners];
}
else {
[self performSelectorOnMainThread:@selector(restartMultitouchEventForwardingAfterWake:) withObject:wakeNotification waitUntilDone:NO];
}
}

- (id)init {
self = [super init];

Expand Down

0 comments on commit 6766199

Please sign in to comment.