From f87bfda8d34bd8e55b4fc680487773669dbd75fc Mon Sep 17 00:00:00 2001 From: Serena <48022799+SerenaKit@users.noreply.github.com> Date: Thu, 27 Apr 2023 16:32:41 +0300 Subject: [PATCH] fix open in folder on macOS, avoid spawning the open command to open URLs (#17342) * fix open in folder on macOS, avoid spawning the open command to open URLs * fix indent * goober alert * no more goober * fix compiler failing on linux --- SDL/CocoaBarItems.h | 5 +++++ SDL/CocoaBarItems.mm | 10 ++++++++++ SDL/SDLMain.cpp | 17 +++++++---------- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/SDL/CocoaBarItems.h b/SDL/CocoaBarItems.h index 0964160d6d..44f9a85d66 100644 --- a/SDL/CocoaBarItems.h +++ b/SDL/CocoaBarItems.h @@ -13,6 +13,11 @@ extern "C" { void initializeOSXExtras(); +/* Yes it is awkward to put this here but I don't feel like making an entire file for 2 functions */ +/* Prefixing with `OSX` to avoid any possible header collisions in the future */ +void OSXShowInFinder(const char *path); +void OSXOpenURL(const char *url); + #ifdef __cplusplus } #endif diff --git a/SDL/CocoaBarItems.mm b/SDL/CocoaBarItems.mm index a49af6e864..d734881202 100644 --- a/SDL/CocoaBarItems.mm +++ b/SDL/CocoaBarItems.mm @@ -49,6 +49,16 @@ void initializeOSXExtras() { [[BarItemsManager sharedInstance] setupAppBarItems]; } +void OSXShowInFinder(const char *path) { + NSURL *url = [NSURL fileURLWithPath:@(path)]; + [NSWorkspace.sharedWorkspace activateFileViewerSelectingURLs:@[url]]; +} + +void OSXOpenURL(const char *url) { + NSURL *nsURL = [NSURL URLWithString:@(url)]; + [NSWorkspace.sharedWorkspace openURL:nsURL]; +} + @implementation BarItemsManager + (instancetype)sharedInstance { static BarItemsManager *stub; diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index f451f3a7c9..3d9dac1c56 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -271,20 +271,18 @@ void System_ShowFileInFolder(const char *path) { SHOpenFolderAndSelectItems(pidl, 0, NULL, 0); CoTaskMemFree(pidl); } -#elif PPSSPP_PLATFORM(MAC) || (PPSSPP_PLATFORM(LINUX) && !PPSSPP_PLATFORM(ANDROID)) +#elif PPSSPP_PLATFORM(MAC) + OSXShowInFinder(path); +#elif (PPSSPP_PLATFORM(LINUX) && !PPSSPP_PLATFORM(ANDROID)) pid_t pid = fork(); if (pid < 0) return; if (pid == 0) { -#if PPSSPP_PLATFORM(MAC) - execlp("open", "open", path, nullptr); -#else execlp("xdg-open", "xdg-open", path, nullptr); -#endif exit(1); } -#endif +#endif /* PPSSPP_PLATFORM(WINDOWS) */ } void System_LaunchUrl(LaunchUrlType urlType, const char *url) { @@ -303,8 +301,7 @@ void System_LaunchUrl(LaunchUrlType urlType, const char *url) { std::wstring wurl = ConvertUTF8ToWString(url); ShellExecute(NULL, L"open", wurl.c_str(), NULL, NULL, SW_SHOWNORMAL); #elif defined(__APPLE__) - std::string command = std::string("open ") + url; - system(command.c_str()); + OSXOpenURL(url); #else std::string command = std::string("xdg-open ") + url; int err = system(command.c_str()); @@ -322,8 +319,8 @@ void System_LaunchUrl(LaunchUrlType urlType, const char *url) { std::wstring mailto = std::wstring(L"mailto:") + ConvertUTF8ToWString(url); ShellExecute(NULL, L"open", mailto.c_str(), NULL, NULL, SW_SHOWNORMAL); #elif defined(__APPLE__) - std::string command = std::string("open mailto:") + url; - system(command.c_str()); + std::string mailToURL = std::string("mailto:") + url; + OSXOpenURL(mailToURL.c_str()); #else std::string command = std::string("xdg-email ") + url; int err = system(command.c_str());