From c81d996475f8b09e014e181f70ed2f41dd634b6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 29 Apr 2023 11:17:10 +0200 Subject: [PATCH] Implement the RESTART_APP system request for Mac --- SDL/SDLMain.cpp | 21 +++++++++++++++------ SDL/SDLMain.mm | 2 -- UI/DarwinFileSystemServices.h | 1 + UI/DarwinFileSystemServices.mm | 10 ++++++++++ 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index 3d9dac1c56..bb6fd39896 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -75,7 +75,8 @@ SDLJoystick *joystick = NULL; GlobalUIState lastUIState = UISTATE_MENU; GlobalUIState GetUIState(); -static int g_QuitRequested = 0; +static bool g_QuitRequested = false; +static bool g_RestartRequested = false; static int g_DesktopWidth = 0; static int g_DesktopHeight = 0; @@ -189,8 +190,10 @@ void System_Vibrate(int length_ms) { bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { switch (type) { + case SystemRequestType::RESTART_APP: + g_RestartRequested = true; + return true; case SystemRequestType::EXIT_APP: - case SystemRequestType::RESTART_APP: // Not sure how we best do this, but do a clean exit, better than being stuck in a bad state. // Do a clean exit g_QuitRequested = true; return true; @@ -1277,14 +1280,15 @@ int main(int argc, char *argv[]) { break; } } - if (g_QuitRequested) + if (g_QuitRequested || g_RestartRequested) break; const uint8_t *keys = SDL_GetKeyboardState(NULL); if (emuThreadState == (int)EmuThreadState::DISABLED) { UpdateRunLoop(); } - if (g_QuitRequested) + if (g_QuitRequested || g_RestartRequested) break; + #if !defined(MOBILE_DEVICE) if (lastUIState != GetUIState()) { lastUIState = GetUIState(); @@ -1334,10 +1338,8 @@ int main(int argc, char *argv[]) { break; } - graphicsContext->SwapBuffers(); - { std::lock_guard guard(g_mutexWindow); if (g_windowState.update) { @@ -1393,5 +1395,12 @@ int main(int argc, char *argv[]) { #ifdef HAVE_LIBNX socketExit(); #endif + + // If a restart was requested (and supported on this platform), respawn the executable. + if (g_RestartRequested) { +#if PPSSPP_PLATFORM(MAC) + RestartMacApp(); +#endif + } return 0; } diff --git a/SDL/SDLMain.mm b/SDL/SDLMain.mm index a53ccc3752..63909bf611 100644 --- a/SDL/SDLMain.mm +++ b/SDL/SDLMain.mm @@ -351,8 +351,6 @@ static void CustomApplicationMain (int argc, char **argv) @end - - #ifdef main # undef main #endif diff --git a/UI/DarwinFileSystemServices.h b/UI/DarwinFileSystemServices.h index a7e406fdc9..6a76c3e8df 100644 --- a/UI/DarwinFileSystemServices.h +++ b/UI/DarwinFileSystemServices.h @@ -33,3 +33,4 @@ private: #endif // PPSSPP_PLATFORM(IOS) }; +void RestartMacApp(); diff --git a/UI/DarwinFileSystemServices.mm b/UI/DarwinFileSystemServices.mm index 1162884242..09daa933b7 100644 --- a/UI/DarwinFileSystemServices.mm +++ b/UI/DarwinFileSystemServices.mm @@ -115,3 +115,13 @@ void DarwinFileSystemServices::setUserPreferredMemoryStickDirectory(Path path) { g_Config.memStickDirectory = path; } +void RestartMacApp() { +#if PPSSPP_PLATFORM(MAC) + NSURL *bundleURL = NSBundle.mainBundle.bundleURL; + NSTask *task = [[NSTask alloc] init]; + task.executableURL = [NSURL fileURLWithPath:@"/usr/bin/open"]; + task.arguments = @[@"-n", bundleURL.path]; + [task launch]; + exit(0); +#endif +} \ No newline at end of file