Skip to content

Commit

Permalink
tap is stopped and started on waking from sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
pilotmoon committed May 25, 2015
1 parent a6d0787 commit a1399cb
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 50 deletions.
19 changes: 19 additions & 0 deletions AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ - (id)init
return self;
}

- (void)dealloc
{
[[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self];
}

- (void)awakeFromNib
{
if(quitting) return;
Expand All @@ -135,6 +140,9 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
[welcomeWindowController showWindow:self];
}

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(appDidWake:) name:NSWorkspaceDidWakeNotification object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(appWillSleep:) name:NSWorkspaceWillSleepNotification object:nil];

// TODO remove
[self showDebug:self];
[self showTestWindow:self];
Expand All @@ -145,6 +153,17 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
}
}

- (void)appDidWake:(NSNotification *)note
{
[self logAppEvent:@"OS woke from sleep"];
[tap resetTap];
}

- (void)appWillSleep:(NSNotification *)note
{
[self logAppEvent:@"OS is going to sleep"];
}

- (NSString *)settingsSummary
{
NSString *(^yn)(NSString *, BOOL) = ^(NSString *label, BOOL state) {
Expand Down
2 changes: 1 addition & 1 deletion DebugWindow.xib
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
<binding destination="-2" name="title" keyPath="self.uiStringDebugConsole" id="6Vx-2S-Mc4"/>
<outlet property="delegate" destination="-2" id="0bl-1N-AYu"/>
</connections>
<point key="canvasLocation" x="-77" y="-517"/>
<point key="canvasLocation" x="-118" y="-519"/>
</window>
</objects>
</document>
5 changes: 3 additions & 2 deletions MouseTap.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ typedef enum {
}
- (void)start;
- (void)stop;
- (void)enableTaps;
- (void)resetState;
- (void)enableTap;
- (void)resetTap;

@end


93 changes: 46 additions & 47 deletions MouseTap.m
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ static CGEventRef callback(CGEventTapProxy proxy,
[tap->logger logSource:source forKey:@"source"];

if(source!=lastSource) {
[tap->logger logMessage:@"Source changed!" special:YES];
[tap->logger logMessage:@"Source changed" special:YES];
}

/* Do the actual reversing. It's worth noting we have to set them in this order (lines then pixels)
Expand All @@ -210,9 +210,9 @@ static CGEventRef callback(CGEventTapProxy proxy,
if (tap->invertX) CGEventSetIntegerValueField(event, kCGScrollWheelEventPointDeltaAxis2, -pixel_axis2);
}
}
else if(type==kCGEventTapDisabledByTimeout)
else
{
[tap enableTaps];
[tap enableTap];
}

[tap->logger logEventType:type forKey:@"type"];
Expand All @@ -229,18 +229,6 @@ - (BOOL)isActive
return activeTapSource&&passiveTapSource&&activeTapPort&&passiveTapPort;
}

- (void)resetState
{
// touching=0;
// lastTouchTime=0;
// lastScrollTime=0;
// lastType=0;
// lastPhase=0;
// lastSource=0;
// TODO ^^
[(AppDelegate *)[NSApp delegate] logAppEvent:@"Tap state reset"];
}

- (void)start
{
if([self isActive])
Expand All @@ -249,31 +237,35 @@ - (void)start
// initialise
_preventReverseOtherApp=[[NSUserDefaults standardUserDefaults] boolForKey:@"ReverseOnlyRawInput"];
_detectWacomMouse=![[NSUserDefaults standardUserDefaults] boolForKey:@"DisableWacomMouseDetection"];
[self resetState];

CGEventMask activeEventMask=NSEventMaskGesture|NSScrollWheelMask;
// CGEventMask passiveEventMask=0;

// passiveTapPort=(CFMachPortRef)CGEventTapCreate(kCGSessionEventTap,
// kCGTailAppendEventTap,
// kCGEventTapOptionListenOnly,
// passiveEventMask,
// callback,
// (__bridge void *)(self));
// clear state
touching=0;
lastTouchTime=0;
lastSource=0;

activeTapPort=(CFMachPortRef)CGEventTapCreate(kCGSessionEventTap,
// passive tap
CGEventMask passiveEventMask=0;
passiveTapPort=(CFMachPortRef)CGEventTapCreate(kCGSessionEventTap,
kCGTailAppendEventTap,
kCGEventTapOptionListenOnly,
passiveEventMask,
callback,
(__bridge void *)(self));
passiveTapSource = (CFRunLoopSourceRef)CFMachPortCreateRunLoopSource(kCFAllocatorDefault, passiveTapPort, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), passiveTapSource, kCFRunLoopCommonModes);

// active tap
CGEventMask activeEventMask=NSEventMaskGesture|NSScrollWheelMask;
activeTapPort=(CFMachPortRef)CGEventTapCreate(kCGSessionEventTap,
kCGTailAppendEventTap,
kCGEventTapOptionDefault,
activeEventMask,
callback,
(__bridge void *)(self));

// create sources and add to run loop
//passiveTapSource = (CFRunLoopSourceRef)CFMachPortCreateRunLoopSource(kCFAllocatorDefault, passiveTapPort, 0);
activeTapSource = (CFRunLoopSourceRef)CFMachPortCreateRunLoopSource(kCFAllocatorDefault, activeTapPort, 0);

//CFRunLoopAddSource(CFRunLoopGetMain(), passiveTapSource, kCFRunLoopCommonModes);
activeTapSource = (CFRunLoopSourceRef)CFMachPortCreateRunLoopSource(kCFAllocatorDefault, activeTapPort, 0);
CFRunLoopAddSource(CFRunLoopGetMain(), activeTapSource, kCFRunLoopCommonModes);

[(AppDelegate *)[NSApp delegate] logAppEvent:@"Tap started"];
}

- (void)stop
Expand All @@ -282,30 +274,37 @@ - (void)stop
return;

CFRunLoopRemoveSource(CFRunLoopGetMain(), activeTapSource, kCFRunLoopCommonModes);
//CFRunLoopRemoveSource(CFRunLoopGetMain(), passiveTapSource, kCFRunLoopCommonModes);

CFMachPortInvalidate(activeTapPort);
//CFMachPortInvalidate(passiveTapPort);

CFRelease(activeTapSource);
//CFRelease(passiveTapSource);
activeTapSource=passiveTapSource=nil;

CFRelease(activeTapSource);
activeTapSource=nil;
CFRelease(activeTapPort);
//CFRelease(passiveTapPort);
activeTapPort=passiveTapPort=nil;
activeTapPort=nil;

CFRunLoopRemoveSource(CFRunLoopGetMain(), passiveTapSource, kCFRunLoopCommonModes);
CFMachPortInvalidate(passiveTapPort);
CFRelease(passiveTapSource);
passiveTapSource=nil;
CFRelease(passiveTapPort);
passiveTapPort=nil;

// TODO touches=nil;
[(AppDelegate *)[NSApp delegate] logAppEvent:@"Tap stopped"];
}

- (void)enableTaps
- (void)enableTap
{
if (!CGEventTapIsEnabled(activeTapPort)) {
CGEventTapEnable(activeTapPort, YES);
}
// if (!CGEventTapIsEnabled(passiveTapPort)) {
// CGEventTapEnable(passiveTapPort, YES);
// }
if (!CGEventTapIsEnabled(passiveTapPort)) {
CGEventTapEnable(passiveTapPort, YES);
}
}

- (void)resetTap
{
[self stop];
[self start];
[(AppDelegate *)[NSApp delegate] logAppEvent:@"Tap reset"];
}

@end

0 comments on commit a1399cb

Please sign in to comment.