Skip to content

Commit

Permalink
koekeishiya#543 resolve issue with lifetime of NSRunningApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
koekeishiya authored and unrevre committed May 28, 2020
1 parent e19f285 commit 77ea438
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Changed
- Blacklist loginwindow and ScreenSaverEngine processes [#537](https://github.com/koekeishiya/yabai/issues/537)
- Resolve some issues with the lifetime of NSRunningApplication [#543](https://github.com/koekeishiya/yabai/issues/543)

## [3.0.2] - 2020-05-22
### Changed
Expand Down
2 changes: 2 additions & 0 deletions src/process_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ struct process *process_create(ProcessSerialNumber psn)
process->terminated = false;
process->xpc = process_info.processType == 'XPC!';
GetProcessPID(&process->psn, &process->pid);
process->ns_application = workspace_application_create_running_ns_application(process);

return process;
}

void process_destroy(struct process *process)
{
workspace_application_destroy_running_ns_application(process);
free(process->name);
free(process);
}
Expand Down
1 change: 1 addition & 0 deletions src/process_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct process
char *name;
bool xpc;
bool volatile terminated;
void *ns_application;
};

struct process_manager
Expand Down
2 changes: 2 additions & 0 deletions src/workspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ void workspace_event_handler_begin(void **context);
void workspace_event_handler_end(void *context);

struct process;
void *workspace_application_create_running_ns_application(struct process *process);
void workspace_application_destroy_running_ns_application(struct process *process);
bool workspace_application_is_observable(struct process *process);
bool workspace_application_is_finished_launching(struct process *process);
void workspace_application_observe_finished_launching(void *context, struct process *process);
Expand Down
43 changes: 21 additions & 22 deletions src/workspace.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,20 @@ void workspace_event_handler_end(void *context)
[ws_context dealloc];
}

void *workspace_application_create_running_ns_application(struct process *process)
{
return [NSRunningApplication runningApplicationWithProcessIdentifier:process->pid];
}

void workspace_application_destroy_running_ns_application(struct process *process)
{
NSRunningApplication *application = process->ns_application;
if (application) [application release];
}

void workspace_application_observe_finished_launching(void *context, struct process *process)
{
NSRunningApplication *application = [NSRunningApplication runningApplicationWithProcessIdentifier:((struct process *)process)->pid];
NSRunningApplication *application = process->ns_application;
if (application) {
[application addObserver:context forKeyPath:@"finishedLaunching" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:process];
} else {
Expand All @@ -32,7 +43,7 @@ void workspace_application_observe_finished_launching(void *context, struct proc

void workspace_application_observe_activation_policy(void *context, struct process *process)
{
NSRunningApplication *application = [NSRunningApplication runningApplicationWithProcessIdentifier:process->pid];
NSRunningApplication *application = process->ns_application;
if (application) {
[application addObserver:context forKeyPath:@"activationPolicy" options:NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew context:process];
} else {
Expand All @@ -42,30 +53,22 @@ void workspace_application_observe_activation_policy(void *context, struct proce

bool workspace_application_is_observable(struct process *process)
{
NSRunningApplication *application = [NSRunningApplication runningApplicationWithProcessIdentifier:process->pid];

NSRunningApplication *application = process->ns_application;
if (application) {
bool result = [application activationPolicy] != NSApplicationActivationPolicyProhibited;
[application release];
return result;
return [application activationPolicy] != NSApplicationActivationPolicyProhibited;
} else {
return false;
}

debug("%s: could not determine observability status for %s (%d)\n", __FUNCTION__, process->name, process->pid);
return false;
}

bool workspace_application_is_finished_launching(struct process *process)
{
NSRunningApplication *application = [NSRunningApplication runningApplicationWithProcessIdentifier:process->pid];

NSRunningApplication *application = process->ns_application;
if (application) {
bool result = [application isFinishedLaunching] == YES;
[application release];
return result;
return [application isFinishedLaunching] == YES;
} else {
return false;
}

debug("%s: could not determine launch status for %s (%d)\n", __FUNCTION__, process->name, process->pid);
return false;
}

@implementation workspace_context
Expand Down Expand Up @@ -146,8 +149,6 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
@try {
[object removeObserver:self forKeyPath:@"activationPolicy"];
} @catch (NSException * __unused exception) {}

[object release];
}
}

Expand All @@ -171,8 +172,6 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
@try {
[object removeObserver:self forKeyPath:@"finishedLaunching"];
} @catch (NSException * __unused exception) {}

[object release];
}
}
}
Expand Down

0 comments on commit 77ea438

Please sign in to comment.