From e9561c7b7a027f2d507698ad9ec803fac06b558f Mon Sep 17 00:00:00 2001 From: Patrick Robertson Date: Fri, 30 Jan 2026 16:14:47 +0000 Subject: [PATCH] Further tweaks to process checks --- .../Code-QuickStepCore/QSProcessMonitor.m | 98 ++++++++----------- 1 file changed, 39 insertions(+), 59 deletions(-) diff --git a/Quicksilver/Code-QuickStepCore/QSProcessMonitor.m b/Quicksilver/Code-QuickStepCore/QSProcessMonitor.m index cf5e7057b..adc3a7952 100644 --- a/Quicksilver/Code-QuickStepCore/QSProcessMonitor.m +++ b/Quicksilver/Code-QuickStepCore/QSProcessMonitor.m @@ -165,13 +165,12 @@ - (void)dealloc { } - (QSObject *)imbuedFileProcessForDict:(NSDictionary *)dict { - NSString *ident = [dict objectForKey:@"NSApplicationBundleIdentifier"]; NSString *appPath = [dict objectForKey:@"NSApplicationPath"]; - - // We used to call the (slow) [NSBundle bundleWithIdentifier:ident], but we can pull this from dict instead + + // We used to call the (slow) [NSBundle bundleWithIdentifier:], but we can pull BundlePath from dict instead NSString *bundlePath = [dict objectForKey:@"BundlePath"]; QSObject *newObject = nil; - if ([appPath isEqualToString: bundlePath]) { + if ([appPath isEqualToString:bundlePath]) { newObject = [QSObject fileObjectWithPath:bundlePath]; // NSLog(@"%@ %@", bundlePath, newObject); } @@ -223,11 +222,11 @@ - (QSObject *)processObjectWithDict:(NSDictionary *)dict { } - (NSDictionary *)infoForApp:(NSRunningApplication *)app { - + ProcessSerialNumber psn; GetProcessForPID(app.processIdentifier, &psn); - - + + NSDictionary *dict = [[NSDictionary dictionaryWithObjectsAndKeys: [app localizedName], @"NSApplicationName", [[app bundleURL] path], @"NSApplicationPath", @@ -238,34 +237,19 @@ - (NSDictionary *)infoForApp:(NSRunningApplication *)app { [NSNumber numberWithLong:psn.highLongOfPSN], @"NSApplicationProcessSerialNumberHigh", [NSNumber numberWithLong:psn.lowLongOfPSN], @"NSApplicationProcessSerialNumberLow", nil] mutableCopy]; - + return dict; } // Helper method to determine whether we want to include a process in the processes dict - (BOOL)includeApp:(NSRunningApplication *)app { - - // Background processes are excluded unless QSShowBackgroundProcesses is set - if ((app.activationPolicy == NSApplicationActivationPolicyProhibited) && - ![[NSUserDefaults standardUserDefaults] boolForKey:@"QSShowBackgroundProcesses"]) - return NO; - - // Get bundle info, if available - NSDictionary *bundleInfo; - if(app.bundleURL) - bundleInfo = [[NSBundle bundleWithURL:app.bundleURL] infoDictionary]; - else - bundleInfo = nil; - - // Determine bundle type - NSString *fileType; - fileType = bundleInfo ? [bundleInfo objectForKey:@"CFBundlePackageType"] : @"NIL "; - - // Exclude all processes except for APPL (regular apps) and FNDR (Finder) - if(![fileType isEqualToString:@"APPL"] && - ![fileType isEqualToString:@"FNDR"]) + if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QSShowBackgroundProcesses"]) { + return YES; + } + // Exclude XPC services + if ([[[app.bundleURL path] pathExtension] isEqualToString:@"xpc"]) { return NO; - + } return YES; } @@ -294,20 +278,22 @@ - (NSDictionary *)infoForPSN:(ProcessSerialNumber)processSerialNumber { } // Helper method to determine whether we want to include a process in the processes dict -// Calls through to the NSRunningApplication-based method -- (BOOL)includePSN:(ProcessSerialNumber)processSerialNumber { - pid_t pid; - NSRunningApplication *app; - - GetProcessPID(&processSerialNumber, &pid); - app = [NSRunningApplication runningApplicationWithProcessIdentifier: pid]; - if(app) - return [self includeApp: app]; - - return NO; +// Used in appLaunched: where we have Carbon process info from infoForPSN: +- (BOOL)includePSN:(NSDictionary *)processInfo { + if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QSShowBackgroundProcesses"]) { + return YES; + } + // Exclude background-only processes + if ([[processInfo objectForKey:@"LSBackgroundOnly"] boolValue]) { + return NO; + } + // Exclude XPC services + if ([[[processInfo objectForKey:@"NSApplicationPath"] pathExtension] isEqualToString:@"xpc"]) { + return NO; + } + return YES; } - #pragma mark - #pragma mark Process Notifications @@ -337,13 +323,8 @@ - (BOOL)handleProcessEvent:(NSEvent *)theEvent { - (void)appLaunched:(ProcessSerialNumber)psn { NSDictionary *dict = [self infoForPSN:psn]; - - // if we're including background processes (i.e. all processes) OR if it's a foreground process, reload - if ([self includePSN: psn]) { - -#ifdef DEBUG - NSLog(@"appLaunched: NSApplicationPath=%@", [dict objectForKey:@"NSApplicationPath"]); -#endif + + if ([self includePSN:dict]) { [self reloadProcesses]; } if (dict) { @@ -446,20 +427,19 @@ - (void)_reloadProcesses { NSArray *tempProcesses = [[NSWorkspace sharedWorkspace] runningApplications]; NSMutableDictionary *procs = [[NSMutableDictionary alloc] initWithCapacity:tempProcesses.count]; - - for (NSRunningApplication *app in tempProcesses) { - if ([self includeApp: app]) { - NSDictionary *info = [self infoForApp:app]; - QSObject *procObject = [self imbuedFileProcessForDict:info]; - NSNumber *pidValue = [NSNumber numberWithInt:app.processIdentifier]; - - if (procObject) { - [procs setObject:procObject forKey:pidValue]; - } + if (![self includeApp:app]) { + continue; + } + + NSDictionary *info = [self infoForApp:app]; + QSObject *procObject = [self imbuedFileProcessForDict:info]; + NSNumber *pidValue = [NSNumber numberWithInt:app.processIdentifier]; + + if (procObject) { + [procs setObject:procObject forKey:pidValue]; } } - processes = [procs copy]; #ifdef DEBUG NSLog(@"_reloadProcesses: Reload time: %f ms for %d processes", [date timeIntervalSinceNow]*-1000, (int) procs.count);