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

[macOS] Always execute editor instances using NSWorkspace to ensure app window is registered and activated correctly. #54474

Merged
merged 1 commit into from
Nov 1, 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
[macOS] Always execute editor instances using NSWorkspace to ensure a…
…pp window is registered and activated correctly.
  • Loading branch information
bruvzg committed Nov 1, 2021
commit ed25a160ea3aeeccc98cc9bde3e11dcacacdf655
1 change: 1 addition & 0 deletions platform/osx/os_osx.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ class OS_OSX : public OS_Unix {
virtual void get_fullscreen_mode_list(List<VideoMode> *p_list, int p_screen = 0) const;

virtual String get_executable_path() const;
virtual Error execute(const String &p_path, const List<String> &p_arguments, bool p_blocking = true, ProcessID *r_child_id = nullptr, String *r_pipe = nullptr, int *r_exitcode = nullptr, bool read_stderr = false, Mutex *p_pipe_mutex = nullptr);

virtual LatinKeyboardVariant get_latin_keyboard_variant() const;
virtual int keyboard_get_layout_count() const;
Expand Down
54 changes: 54 additions & 0 deletions platform/osx/os_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2955,6 +2955,60 @@ static int get_screen_index(NSScreen *screen) {
}
}

Error OS_OSX::execute(const String &p_path, const List<String> &p_arguments, bool p_blocking, ProcessID *r_child_id, String *r_pipe, int *r_exitcode, bool read_stderr, Mutex *p_pipe_mutex) {
if (@available(macOS 10.15, *)) {
NSString *nsappname = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
// If executable is bundled, always execute editor instances using NSWorkspace to ensure app window is registered and activated correctly.
if (p_path == get_executable_path() && nsappname != nil) {
if (p_blocking && r_pipe) {
ERR_FAIL_V_MSG(ERR_CANT_OPEN, "Cannot pipe stream from process running as app bundle.");
}
NSMutableArray *arguments = [[NSMutableArray alloc] init];
for (const List<String>::Element *E = p_arguments.front(); E; E = E->next()) {
[arguments addObject:[NSString stringWithUTF8String:E->get().utf8().get_data()]];
}
NSWorkspaceOpenConfiguration *configuration = [[NSWorkspaceOpenConfiguration alloc] init];
[configuration setArguments:arguments];
[configuration setCreatesNewApplicationInstance:YES];
__block dispatch_semaphore_t lock = dispatch_semaphore_create(0);
__block Error err = ERR_TIMEOUT;
__block pid_t pid = 0;
[[NSWorkspace sharedWorkspace] openApplicationAtURL:[[NSBundle mainBundle] bundleURL]
configuration:configuration
completionHandler:^(NSRunningApplication *app, NSError *error) {
if (error) {
err = ERR_CANT_FORK;
NSLog(@"Failed to execute: %@", error.localizedDescription);
} else {
pid = [app processIdentifier];
err = OK;
}
dispatch_semaphore_signal(lock);
}];
dispatch_semaphore_wait(lock, dispatch_time(DISPATCH_TIME_NOW, 20000000000)); // 20 sec timeout, wait for app to launch.
dispatch_release(lock);

if (err == OK) {
if (p_blocking) {
int status;
waitpid(pid, &status, 0);
if (r_exitcode) {
*r_exitcode = WIFEXITED(status) ? WEXITSTATUS(status) : status;
}
} else if (r_child_id) {
*r_child_id = (ProcessID)pid;
}
}

return err;
} else {
return OS_Unix::execute(p_path, p_arguments, p_blocking, r_child_id, r_pipe, r_exitcode, read_stderr, p_pipe_mutex);
}
} else {
return OS_Unix::execute(p_path, p_arguments, p_blocking, r_child_id, r_pipe, r_exitcode, read_stderr, p_pipe_mutex);
}
}

// Returns string representation of keys, if they are printable.
//
static NSString *createStringForKeys(const CGKeyCode *keyCode, int length) {
Expand Down