Skip to content

Commit

Permalink
Updater:Mac: Delay launch of new application until the old one exits
Browse files Browse the repository at this point in the history
Prevents duplicate dock icons
  • Loading branch information
TellowKrinkle authored and refractionpcsx2 committed Jun 24, 2023
1 parent a17a7ad commit 8d27c32
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 23 deletions.
4 changes: 2 additions & 2 deletions common/CocoaTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace CocoaTools
std::optional<std::string> GetNonTranslocatedBundlePath();
/// Move the given file to the trash, and return the path to its new location
std::optional<std::string> MoveToTrash(std::string_view file);
/// Launch the given application
bool LaunchApplication(std::string_view file);
/// Launch the given application once this one quits
bool DelayedLaunch(std::string_view file);
}

#endif // __APPLE__
29 changes: 9 additions & 20 deletions common/CocoaTools.mm
Original file line number Diff line number Diff line change
Expand Up @@ -175,27 +175,16 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
return std::string([new_url fileSystemRepresentation]);
}

bool CocoaTools::LaunchApplication(std::string_view file)
bool CocoaTools::DelayedLaunch(std::string_view file)
{
NSURL* url = [NSURL fileURLWithPath:[[NSString alloc] initWithBytes:file.data() length:file.size() encoding:NSUTF8StringEncoding]];
if (@available(macOS 10.15, *))
{
// replacement api is async which isn't great for us
std::mutex done;
bool output;
done.lock();
NSWorkspaceOpenConfiguration* config = [NSWorkspaceOpenConfiguration new];
[config setCreatesNewApplicationInstance:YES];
[[NSWorkspace sharedWorkspace] openApplicationAtURL:url configuration:config completionHandler:[&](NSRunningApplication*_Nullable app, NSError*_Nullable error) {
output = app != nullptr;
done.unlock();
@autoreleasepool {
NSTask* task = [NSTask new];
[task setExecutableURL:[NSURL fileURLWithPath:@"/bin/sh"]];
[task setEnvironment:@{
@"WAITPID": [NSString stringWithFormat:@"%d", [[NSProcessInfo processInfo] processIdentifier]],
@"LAUNCHAPP": [[NSString alloc] initWithBytes:file.data() length:file.size() encoding:NSUTF8StringEncoding],
}];
done.lock();
done.unlock();
return output;
}
else
{
return [[NSWorkspace sharedWorkspace] launchApplicationAtURL:url options:NSWorkspaceLaunchNewInstance configuration:@{} error:nil];
[task setArguments:@[@"-c", @"while /bin/ps -p $WAITPID > /dev/null; do /bin/sleep 0.1; done; exec /usr/bin/open \"$LAUNCHAPP\";"]];
return [task launchAndReturnError:nil];
}
}
3 changes: 2 additions & 1 deletion pcsx2-qt/AutoUpdaterDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,8 @@ bool AutoUpdaterDialog::processUpdate(const QByteArray& update_data, QProgressDi
}
QDir(QString::fromStdString(*trashed_path)).removeRecursively();
}
if (!CocoaTools::LaunchApplication(open_path.toStdString()))
// For some reason if I use QProcess the shell gets killed immediately with SIGKILL, but NSTask is fine...
if (!CocoaTools::DelayedLaunch(open_path.toStdString()))
{
reportError("Failed to start new application");
return false;
Expand Down

0 comments on commit 8d27c32

Please sign in to comment.