Skip to content

Commit

Permalink
Merge pull request #4 from aaronkollasch/cleanup-wakeup-refresh
Browse files Browse the repository at this point in the history
Cleanup MTDevice reloading on wakeup
  • Loading branch information
aaronkollasch authored Nov 27, 2021
2 parents ad3833f + c6f34b0 commit 415bea4
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 21 deletions.
2 changes: 2 additions & 0 deletions jitouch/Jitouch/Gesture.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

- (id)init;

- (void)reload;

@end

void turnOffGestures(void);
67 changes: 65 additions & 2 deletions jitouch/Jitouch/Gesture.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ @implementation Gesture
void MTUnregisterContactFrameCallback(MTDeviceRef, MTContactCallbackFunction);
void MTDeviceStop(MTDeviceRef);
void MTDeviceRelease(MTDeviceRef);
//BOOL MTDeviceIsRunning(MTDeviceRef);
bool MTDeviceIsRunning(MTDeviceRef);
void MTDeviceGetFamilyID(MTDeviceRef, int*);
OSStatus MTDeviceGetDeviceID(MTDeviceRef, uint64_t*) __attribute__ ((weak_import)); // no 10.5

void CoreDockSendNotification(NSString *notificationName);

Expand Down Expand Up @@ -2894,8 +2895,10 @@ static CGEventRef CGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEve
}

#pragma mark - Init
CFMutableArrayRef deviceList;

- (id)init {
NSLog(@"Initializing.");
if (self = [super init]) {
me = self;

Expand All @@ -2906,11 +2909,14 @@ - (id)init {
initChars();

{
CFArrayRef deviceList = MTDeviceCreateList();
deviceList = MTDeviceCreateList();
for (CFIndex i = 0; i < CFArrayGetCount(deviceList); i++) {
MTDeviceRef device = (MTDeviceRef)CFArrayGetValueAtIndex(deviceList, i);
int familyID;
MTDeviceGetFamilyID(device, &familyID);
uint64_t deviceID = 0;
MTDeviceGetDeviceID(device, &deviceID);
NSLog(@"start device %li %"PRIu64" family %d %s", (long)i, deviceID, familyID, (MTDeviceIsRunning(device)) ? "running" : "not running");
if (familyID == 98 || familyID == 99 || familyID == 100 // built-in trackpad
|| familyID == 101 // retina mbp
|| familyID == 102 // retina macbook with the Force Touch trackpad (2015)
Expand Down Expand Up @@ -3013,6 +3019,56 @@ - (id)init {
return self;
}

- (void)reload {
NSLog(@"Reloading gestures.");
for (CFIndex i = 0; i < CFArrayGetCount(deviceList); i++) {
MTDeviceRef device = (MTDeviceRef)CFArrayGetValueAtIndex(deviceList, i);
int familyID;
MTDeviceGetFamilyID(device, &familyID);
uint64_t deviceID = 0;
MTDeviceGetDeviceID(device, &deviceID);
NSLog(@"stop device %li %"PRIu64" family %d %s", (long)i, deviceID, familyID, (MTDeviceIsRunning(device)) ? "running" : "not running");
if (familyID >= 98) {
MTUnregisterContactFrameCallback(device, trackpadCallback);
MTUnregisterContactFrameCallback(device, magicMouseCallback);
MTDeviceStop(device);
}
}
CFRelease(deviceList);
sleep(1);
deviceList = MTDeviceCreateList();
for (CFIndex i = 0; i < CFArrayGetCount(deviceList); i++) {
MTDeviceRef device = (MTDeviceRef)CFArrayGetValueAtIndex(deviceList, i);
int familyID;
MTDeviceGetFamilyID(device, &familyID);
uint64_t deviceID = 0;
MTDeviceGetDeviceID(device, &deviceID);
NSLog(@"start device %li %"PRIu64" family %d %s", (long)i, deviceID, familyID, (MTDeviceIsRunning(device)) ? "running" : "not running");
if (familyID == 98 || familyID == 99 || familyID == 100 // built-in trackpad
|| familyID == 101 // retina mbp
|| familyID == 102 // retina macbook with the Force Touch trackpad (2015)
|| familyID == 103 // retina mbp 13" with the Force Touch trackpad (2015)
|| familyID == 104
|| familyID == 105) { // macbook with touch bar
MTRegisterContactFrameCallback(device, trackpadCallback);
MTDeviceStart(device, 0);
} else if (familyID == 112 // magic mouse & magic mouse 2
|| familyID == 113 // magic mouse 3?
) {
MTRegisterContactFrameCallback(device, magicMouseCallback);
MTDeviceStart(device, 0);
} else if (familyID == 128 // magic trackpad
|| familyID == 129 // magic trackpad 2
|| familyID == 130 // magic trackpad 3?
) {
MTRegisterContactFrameCallback(device, trackpadCallback);
MTDeviceStart(device, 0);
} else if (familyID >= 98) { // Unknown ID. Assume it's a trackpad.
MTRegisterContactFrameCallback(device, trackpadCallback);
MTDeviceStart(device, 0);
}
}
}

#pragma mark - Character Recognizer

Expand Down Expand Up @@ -3959,4 +4015,11 @@ static void trackpadRecognizerTwo(const Finger *data, int nFingers, double times
}
}

#pragma mark -

- (void) dealloc {
CFRelease(deviceList);
[super dealloc];
}

@end
2 changes: 2 additions & 0 deletions jitouch/Jitouch/JitouchAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

@property (assign) IBOutlet NSWindow *window;

- (void)reload;

@end

extern CursorWindow *cursorWindow;
Expand Down
25 changes: 6 additions & 19 deletions jitouch/Jitouch/JitouchAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -252,25 +252,12 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
}

- (void)wokeUp:(NSNotification *)aNotification {
//restart outselves
NSString *killArg1AndOpenArg2Script = @"kill -9 $1 \n open \"$2\"";

//NSTask needs its arguments to be strings
NSString *ourPID = [NSString stringWithFormat:@"%d",
[[NSProcessInfo processInfo] processIdentifier]];

//this will be the path to the .app bundle,
//not the executable inside it; exactly what `open` wants
NSString * pathToUs = [[NSBundle mainBundle] bundlePath];

NSArray *shArgs = [NSArray arrayWithObjects:@"-c", // -c tells sh to execute the next argument, passing it the remaining arguments.
killArg1AndOpenArg2Script,
@"", //$0 path to script (ignored)
ourPID, //$1 in restartScript
pathToUs, //$2 in the restartScript
nil];
NSTask *restartTask = [NSTask launchedTaskWithLaunchPath:@"/bin/sh" arguments:shArgs];
[restartTask waitUntilExit]; //wait for killArg1AndOpenArg2Script to finish
NSLog(@"Woke up.");
[self reload];
}

- (void)reload {
[gesture reload];
}

#pragma mark -
Expand Down
19 changes: 19 additions & 0 deletions jitouch/Jitouch/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,30 @@
// Jitouch
//
// Copyright 2021 Supasorn Suwajanakorn and Sukolsak Sakshuwong. All rights reserved.
// Modified work Copyright 2021 Aaron Kollasch. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#import "signal.h"
#include "JitouchAppDelegate.h"

int main(int argc, const char * argv[])
{
// Trap SIGHUP and do reload
// see https://stackoverflow.com/questions/50225548/trap-sigint-in-cocoa-macos-application
// see https://www.mikeash.com/pyblog/friday-qa-2011-04-01-signal-handling.html
// see https://fossies.org/linux/HandBrake/macosx/main.m
dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_SIGNAL, SIGHUP, 0, dispatch_get_global_queue(0, 0));
dispatch_source_set_event_handler(source, ^{
NSLog(@"Received SIGHUP.");
dispatch_async(dispatch_get_main_queue(), ^{
[((JitouchAppDelegate*)[NSApp delegate]) reload];
});
});
dispatch_resume(source);

// Ignore the SIGHUP signal because we handle it.
signal(SIGHUP, SIG_IGN);

return NSApplicationMain(argc, argv);
}

0 comments on commit 415bea4

Please sign in to comment.