fix(info): fix segmentation fault on macOS

This commit is contained in:
Federico Terzi 2021-05-20 19:16:17 +02:00
parent 8c2e02030a
commit fc69b7a79f

View File

@ -23,54 +23,56 @@
int32_t info_get_title(char *buffer, int32_t buffer_size) int32_t info_get_title(char *buffer, int32_t buffer_size)
{ {
CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID); @autoreleasepool {
int32_t result = 0; CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID);
int32_t result = 0;
if (windows) { if (windows) {
for (NSDictionary *window in (NSArray *)windows) { for (NSDictionary *window in (NSArray *)windows) {
NSNumber *ownerPid = window[(id) kCGWindowOwnerPID]; NSNumber *ownerPid = window[(id) kCGWindowOwnerPID];
NSRunningApplication *currentApp = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]]; NSRunningApplication *currentApp = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]];
if ([currentApp isActive]) { if ([currentApp isActive]) {
NSString *name = window[(id) kCGWindowName]; NSString *name = window[(id) kCGWindowName];
if (name.length > 0) { if (name.length > 0) {
const char * title = [name UTF8String]; const char * title = [name UTF8String];
snprintf(buffer, buffer_size, "%s", title); snprintf(buffer, buffer_size, "%s", title);
result = 1; result = 1;
}
break;
} }
break;
} }
}
CFRelease(windows); CFRelease(windows);
}
} }
return result; return 0;
} }
int32_t info_get_exec(char *buffer, int32_t buffer_size) int32_t info_get_exec(char *buffer, int32_t buffer_size)
{ {
NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; @autoreleasepool {
NSString *bundlePath = [frontApp bundleURL].path; NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
const char * path = [bundlePath UTF8String]; NSString *bundlePath = [frontApp bundleURL].path;
const char * path = [bundlePath UTF8String];
snprintf(buffer, buffer_size, "%s", path); snprintf(buffer, buffer_size, "%s", path);
}
[bundlePath release];
return 1; return 1;
} }
int32_t info_get_class(char *buffer, int32_t buffer_size) int32_t info_get_class(char *buffer, int32_t buffer_size)
{ {
NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; @autoreleasepool {
NSString *bundleId = frontApp.bundleIdentifier; NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication];
const char * bundle = [bundleId UTF8String]; NSString *bundleId = frontApp.bundleIdentifier;
const char * bundle = [bundleId UTF8String];
snprintf(buffer, buffer_size, "%s", bundle); snprintf(buffer, buffer_size, "%s", bundle);
}
[bundleId release];
return 1; return 1;
} }