Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions MiddleClick/Controller.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
CFMachPortRef currentEventTap;
CFRunLoopSourceRef currentRunLoopSource;

BOOL loggedOut;

#pragma mark Implementation

@implementation Controller {
Expand All @@ -66,6 +68,9 @@ - (void)start

threeDown = NO;
wasThreeDown = NO;
// Ignore restart command if user is logged out
// Primarily happens due to "Display reconfigured, restarting..."
loggedOut = false;

fingersQua = [[NSUserDefaults standardUserDefaults] integerForKey:kFingersNum];

Expand All @@ -83,6 +88,20 @@ - (void)start
selector:@selector(receiveWakeNote:)
name:NSWorkspaceDidWakeNotification
object:NULL];

// register a callback to know when user switched out from their account to stop listeners
[[[NSWorkspace sharedWorkspace] notificationCenter]
addObserver:self
selector:@selector(receiveResignActiveNote:)
name:NSWorkspaceSessionDidResignActiveNotification
object:NULL];

// register a callback to know when user logged back in to start working again
[[[NSWorkspace sharedWorkspace] notificationCenter]
addObserver:self
selector:@selector(receiveBecomeActiveNote:)
name:NSWorkspaceSessionDidBecomeActiveNotification
object:NULL];

// Register IOService notifications for added devices.
IONotificationPortRef port = IONotificationPortCreate(kIOMasterPortDefault);
Expand Down Expand Up @@ -221,6 +240,26 @@ - (void)receiveWakeNote:(NSNotification*)note
[self scheduleRestart:10];
}

/// Callback for user logout. This stops all listeners so the taps won't duplicate across 2 users
- (void)receiveResignActiveNote:(NSNotification*)note
{
NSLog(@"User logged out, stopping...");
loggedOut = true;
stopUnstableListeners();
}

/// Callback for user login. Initialize callbacks when the user is active on this account again
/// Start listeners istead of calling 'scheduleRestart' because it would fail to unregister them
- (void)receiveBecomeActiveNote:(NSNotification*)note
{
NSLog(@"User logged back in, restarting...");
loggedOut = false;
// If we try to restart instead of just plainly subscribe, the app crashes
// At the 'unregisterMTDeviceCallback MTDeviceRelease(device);'
// Seems like it can't find the device to unsub from
[self startUnstableListeners];
}

- (BOOL)getClickMode
{
return needToClick;
Expand Down Expand Up @@ -350,6 +389,10 @@ int touchCallback(int device, Finger* data, int nFingers, double timestamp,
/// Restart the listeners when devices are connected/invalidated.
- (void)restartListeners
{
if (loggedOut) {
NSLog(@"User is logged out, abort restart...");
return;
}
NSLog(@"Restarting app functionality...");
stopUnstableListeners();
[self startUnstableListeners];
Expand Down