Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a LaunchAgent to manage the Jitouch process #8

Merged
merged 2 commits into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
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
20 changes: 10 additions & 10 deletions jitouch/Jitouch/Gesture.m
Original file line number Diff line number Diff line change
Expand Up @@ -2825,11 +2825,11 @@ static CGEventRef CGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEve
CFMachPortRef newEventTap = nil;
int i = 0;
while (newEventTap == nil) {
if (i > 360) {
NSLog(@"Could not create CGEventTap");
exit(1);
if (i < 360) {
sleep(1);
} else {
sleep(360);
}
sleep(1);
newEventTap = [me createEventTap];
i++;
}
Expand Down Expand Up @@ -2962,7 +2962,7 @@ - (id)init {
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");
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 @@ -3038,11 +3038,11 @@ - (id)init {
CFMachPortRef newEventTap = nil;
int i = 0;
while (newEventTap == nil) {
if (i > 360) {
NSLog(@"Could not create CGEventTap");
exit(1);
if (i < 360) {
sleep(1);
} else {
sleep(360);
}
sleep(1);
newEventTap = [me createEventTap];
i++;
}
Expand Down Expand Up @@ -3083,7 +3083,7 @@ - (void)reload {
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");
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
65 changes: 9 additions & 56 deletions jitouch/Jitouch/JitouchAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Jitouch
//
// Copyright 2021 Supasorn Suwajanakorn and Sukolsak Sakshuwong. All rights reserved.
// Modified work Copyright 2021 Aaron Kollasch. All rights reserved.
//

#import "JitouchAppDelegate.h"
Expand All @@ -20,65 +21,18 @@ @implementation JitouchAppDelegate

@synthesize window;

- (void)addJitouchToLoginItems{
NSString *jitouchPath = [[NSString stringWithFormat:@"file://%@",[[NSBundle mainBundle] bundlePath]] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *jitouchURL = [NSURL URLWithString:jitouchPath];


LSSharedFileListRef loginListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginListRef) {
// delete all shortcuts to jitouch in the login items
UInt32 seedValue;
NSArray *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(loginListRef, &seedValue);
for (id item in loginItemsArray) {
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
CFURLRef thePath;
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr) {
NSRange range = [[(NSURL*)thePath path] rangeOfString:@"Jitouch"];
if (range.location != NSNotFound)
LSSharedFileListItemRemove(loginListRef, itemRef);
}
}
[loginItemsArray release];

if (![settings objectForKey:@"StartAtLogin"] || [[settings objectForKey:@"StartAtLogin"] intValue]) {
// add shortcut to jitouch in the login items (there should be only one shortcut)
LSSharedFileListItemRef loginItemRef = LSSharedFileListInsertItemURL(loginListRef, kLSSharedFileListItemLast, NULL, NULL, (CFURLRef)jitouchURL, NULL, NULL);

if (loginItemRef) {
CFRelease(loginItemRef);
}
}

CFRelease(loginListRef);
}
}

- (void)removeJitouchFromLoginItems{
LSSharedFileListRef loginListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginListRef) {
// delete all shortcuts to jitouch in the login items
UInt32 seedValue;
NSArray *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(loginListRef, &seedValue);
for (id item in loginItemsArray) {
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
CFURLRef thePath;
if (LSSharedFileListItemResolve(itemRef, 0, (CFURLRef*) &thePath, NULL) == noErr) {
NSRange range = [[(NSURL*)thePath path] rangeOfString:@"Jitouch"];
if (range.location != NSNotFound)
LSSharedFileListItemRemove(loginListRef, itemRef);
}
}
[loginItemsArray release];
CFRelease(loginListRef);
}
- (void)unloadJitouchLaunchAgent {
NSString *plistPath = [@"~/Library/LaunchAgents/com.jitouch.Jitouch.plist" stringByStandardizingPath];
NSArray *unloadArgs = [NSArray arrayWithObjects:@"unload",
plistPath,
nil];
NSTask *unloadTask = [NSTask launchedTaskWithLaunchPath:@"/bin/launchctl" arguments:unloadArgs];
[unloadTask waitUntilExit];
}

#pragma mark - Menu

- (BOOL)validateMenuItem:(NSMenuItem *)item {
if ([item action] == @selector(switchChange:))
return NO;
return YES;
}

Expand Down Expand Up @@ -142,8 +96,7 @@ - (void)preferences:(id)sender {


- (void)quit:(id)sender {
// Remove from login item
[self removeJitouchFromLoginItems];
[self unloadJitouchLaunchAgent];

// Quit
[NSApp terminate: sender];
Expand Down
4 changes: 3 additions & 1 deletion prefpane/Jitouch.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@
COMBINE_HIDPI_IMAGES = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 2.78;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEPLOYMENT_POSTPROCESSING = YES;
DEVELOPMENT_TEAM = 5UQY3B3594;
FRAMEWORK_SEARCH_PATHS = (
Expand All @@ -347,6 +348,7 @@
);
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = "DEBUG=1";
INFOPLIST_FILE = Info.plist;
INSTALL_PATH = "$(HOME)/Library/PreferencePanes";
LIBRARY_SEARCH_PATHS = (
Expand All @@ -359,7 +361,7 @@
PRODUCT_BUNDLE_IDENTIFIER = "com.jitouch.${PRODUCT_NAME:identifier}";
PRODUCT_NAME = Jitouch;
PROVISIONING_PROFILE_SPECIFIER = "";
STRIP_INSTALLED_PRODUCT = YES;
STRIP_INSTALLED_PRODUCT = NO;
SYSTEM_FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(SYSTEM_LIBRARY_DIR)/PrivateFrameworks",
Expand Down
109 changes: 90 additions & 19 deletions prefpane/JitouchPref.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Jitouch
//
// Copyright 2021 Supasorn Suwajanakorn and Sukolsak Sakshuwong. All rights reserved.
// Modified work Copyright 2021 Aaron Kollasch. All rights reserved.
//

#import "JitouchPref.h"
Expand All @@ -18,6 +19,20 @@ @implementation JitouchPref

CFMachPortRef eventTap;

#ifdef DEBUG
- (void)redirectLog
{
NSString *pathForLog = [@"~/Library/Logs/com.jitouch.Jitouch.prefpane.log" stringByStandardizingPath];
freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
NSLog(@"Jitouch prefPane opened");
}
#else
- (void)redirectLog
{
return;
}
#endif

- (void)enUpdated {
[trackpadTab enUpdated];
[magicMouseTab enUpdated];
Expand All @@ -38,6 +53,9 @@ - (IBAction)change:(id)sender {
[Settings setKey:@"enAll" withInt:value];

[self enUpdated];
if (enAll) {
[self loadJitouchLaunchAgent];
}
} else if (sender == cbShowIcon) {
int value = [sender state] == NSOnState ? 1: 0;
[Settings setKey:@"ShowIcon" withInt:value];
Expand Down Expand Up @@ -103,16 +121,12 @@ static CGEventRef CGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEve
return event;
}


- (void)addJitouchToLoginItems{
NSString *jitouchPath = [NSString stringWithFormat:@"file://%@", [[self bundle] pathForResource:@"Jitouch" ofType:@"app"]];
NSURL *jitouchURL = [NSURL URLWithString:jitouchPath];

- (void)removeJitouchFromLoginItems{
LSSharedFileListRef loginListRef = LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL);
if (loginListRef) {
// delete all shortcuts to jitouch in the login items
UInt32 seedValue;
NSArray *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(loginListRef, &seedValue);
NSArray *loginItemsArray = (NSArray *)LSSharedFileListCopySnapshot(loginListRef, &seedValue);
for (id item in loginItemsArray) {
LSSharedFileListItemRef itemRef = (LSSharedFileListItemRef)item;
CFURLRef thePath;
Expand All @@ -123,22 +137,83 @@ - (void)addJitouchToLoginItems{
}
}
[loginItemsArray release];
CFRelease(loginListRef);
}
}

- (NSString *)generateJitouchLaunchAgent {
NSString *pathToUs = [[self bundle] bundlePath];
NSString *home = NSHomeDirectory();
NSString *launchAgentFmt = @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n"
"<plist version=\"1.0\">\n"
"<dict>\n"
" <key>Label</key>\n"
" <string>com.jitouch.Jitouch.agent</string>\n"
" <key>Program</key>\n"
" <string>%@/Contents/Resources/Jitouch.app/Contents/MacOS/Jitouch</string>\n"
" <key>RunAtLoad</key>\n"
" <true/>\n"
" <key>KeepAlive</key>\n"
" <true/>\n"
" <key>ProcessType</key>\n"
" <string>Interactive</string>\n"
" <key>StandardErrorPath</key>\n"
" <string>%@/Library/Logs/com.jitouch.Jitouch.log</string>\n"
" <key>StandardOutPath</key>\n"
" <string>/dev/null</string>\n"
" <key>Umask</key>\n"
" <integer>63</integer>\n"
"</dict>\n"
"</plist>";
NSString *launchAgent = [NSString stringWithFormat:launchAgentFmt, pathToUs, home];
return launchAgent;
}

if (![settings objectForKey:@"StartAtLogin"] || [[settings objectForKey:@"StartAtLogin"] intValue]) {
// add shortcut to jitouch in the login items (there should be only one shortcut)
LSSharedFileListItemRef loginItemRef = LSSharedFileListInsertItemURL(loginListRef, kLSSharedFileListItemLast, NULL, NULL, (CFURLRef)jitouchURL, NULL, NULL);
- (void)loadJitouchLaunchAgent {
NSString *plistPath = [@"~/Library/LaunchAgents/com.jitouch.Jitouch.plist" stringByStandardizingPath];
NSArray *loadArgs = [NSArray arrayWithObjects:@"load",
plistPath,
nil];
NSTask *loadTask = [NSTask launchedTaskWithLaunchPath:@"/bin/launchctl" arguments:loadArgs];
[loadTask waitUntilExit];
}

if (loginItemRef) {
CFRelease(loginItemRef);
}
}
- (void)unloadJitouchLaunchAgent {
NSString *plistPath = [@"~/Library/LaunchAgents/com.jitouch.Jitouch.plist" stringByStandardizingPath];
NSArray *unloadArgs = [NSArray arrayWithObjects:@"unload",
plistPath,
nil];
NSTask *unloadTask = [NSTask launchedTaskWithLaunchPath:@"/bin/launchctl" arguments:unloadArgs];
[unloadTask waitUntilExit];
}

CFRelease(loginListRef);
- (void)addJitouchLaunchAgent {
NSString *launchAgent = [self generateJitouchLaunchAgent];
NSString *plistPath = [@"~/Library/LaunchAgents/com.jitouch.Jitouch.plist" stringByStandardizingPath];
NSError *error;
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath] &&
[launchAgent isEqualToString:
[NSString stringWithContentsOfFile:plistPath encoding:NSUTF8StringEncoding error:&error]
]) {
return;
}
[launchAgent writeToFile:plistPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
NSLog(@"Updated LaunchAgent at %@", plistPath);

[self unloadJitouchLaunchAgent];

// in case an older Jitouch is still around
[self removeJitouchFromLoginItems];
[self killAllJitouchs];

[self loadJitouchLaunchAgent];

NSLog(@"Reloaded LaunchAgent at %@", plistPath);
}

- (void)mainViewDidLoad {
[self redirectLog];
isPrefPane = YES;
[Settings loadSettings:self];

Expand All @@ -160,11 +235,7 @@ - (void)mainViewDidLoad {
[self killAllJitouchs];
running = NO;
}
[self addJitouchToLoginItems];
//if (!running) {
NSString *pathToJitouchInBundle = [[self bundle] pathForResource:@"Jitouch" ofType:@"app"];
[[NSWorkspace sharedWorkspace] openFile:pathToJitouchInBundle];
//}
[self addJitouchLaunchAgent];


NSInteger tabIndex;
Expand Down
2 changes: 1 addition & 1 deletion scripts/bump-version.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/sh
CURRENT_PROJECT_VERSION="$1" # eg 2.75
CURRENT_PROJECT_VERSION="$1" # e.g. 2.75

sed -i '' "s/VERSION = [0-9.]*;$/VERSION = $CURRENT_PROJECT_VERSION;/g" jitouch/Jitouch/Jitouch.xcodeproj/project.pbxproj
sed -i '' "s/VERSION = [0-9.]*;$/VERSION = $CURRENT_PROJECT_VERSION;/g" prefpane/Jitouch.xcodeproj/project.pbxproj
Expand Down
4 changes: 3 additions & 1 deletion scripts/sign-installer.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#!/bin/sh
CURRENT_PROJECT_VERSION="$1" # eg 2.75
CURRENT_PROJECT_VERSION="$1" # e.g. 2.75
BUILT_PRODUCTS_DIR="build/Jitouch_${CURRENT_PROJECT_VERSION}"
OBJROOT=build/staging
PRODUCT_BUNDLE_IDENTIFIER="com.jitouch.Jitouch"
signing_cert="Developer ID Installer: Aaron Kollasch (5UQY3B3594)"

rm -rf "$OBJROOT"/*
mkdir -p "$OBJROOT/pkg_staging/"
cp -rp ${BUILT_PRODUCTS_DIR}/Jitouch.prefPane ${OBJROOT}/pkg_staging/Jitouch.prefPane
Expand All @@ -12,3 +13,4 @@ pkgbuild --root ${OBJROOT}/pkg_staging/ --component-plist prefpane/components.pl
productbuild --distribution prefpane/distribution.xml --package-path ${OBJROOT} --identifier ${PRODUCT_BUNDLE_IDENTIFIER} --version ${CURRENT_PROJECT_VERSION} --sign "${signing_cert}" --timestamp "${BUILT_PRODUCTS_DIR}/Install-Jitouch.pkg"
#xcrun altool --notarize-app --primary-bundle-id ${PRODUCT_BUNDLE_IDENTIFIER} --username "" --password "@keychain:Developer-xcrun" --asc-provider "5UQY3B3594" --file "${BUILT_PRODUCTS_DIR}/Install-Jitouch.pkg"
#xcrun altool --notarization-info <UUID> --username "" --password "@keychain:Developer-xcrun"