diff --git a/Common/System/Request.h b/Common/System/Request.h index 856fbb9e41..0d32e8c491 100644 --- a/Common/System/Request.h +++ b/Common/System/Request.h @@ -79,3 +79,11 @@ inline void System_BrowseForFile(const std::string &title, BrowseFileType type, inline void System_BrowseForFolder(const std::string &title, RequestCallback callback) { g_requestManager.MakeSystemRequest(SystemRequestType::BROWSE_FOR_FOLDER, callback, title, "", 0); } + +inline void System_CopyStringToClipboard(const std::string &string) { + g_requestManager.MakeSystemRequest(SystemRequestType::COPY_TO_CLIPBOARD, nullptr, string, "", 0); +} + +inline void System_ExitApp() { + g_requestManager.MakeSystemRequest(SystemRequestType::EXIT_APP, nullptr, "", "", 0); +} diff --git a/Common/System/System.h b/Common/System/System.h index 1b05cf4960..f4f8a5c294 100644 --- a/Common/System/System.h +++ b/Common/System/System.h @@ -51,17 +51,25 @@ void System_Vibrate(int length_ms); void System_ShowFileInFolder(const char *path); void System_LaunchUrl(LaunchUrlType urlType, const char *url); +// It's sometimes a little unclear what should be a request, and what should be a separate function. +// Going forward, "optional" things (PPSSPP will still function alright without it) will be requests, +// to make implementations simpler in the default case. + enum class SystemRequestType { INPUT_TEXT_MODAL, BROWSE_FOR_IMAGE, BROWSE_FOR_FILE, BROWSE_FOR_FOLDER, + + EXIT_APP, + COPY_TO_CLIPBOARD, }; // Implementations are supposed to process the request, and post the response to the g_RequestManager (see Message.h). // This is not to be used directly by applications, instead use the g_RequestManager to make the requests. // This can return false if it's known that the platform doesn't support the request, the app is supposed to handle // or ignore that cleanly. +// Some requests don't use responses. bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3); // TODO: To be separated into requests, see Request.h, and a way to post "UI messages". diff --git a/Common/UI/View.cpp b/Common/UI/View.cpp index c48e7eca16..5a3f53c68f 100644 --- a/Common/UI/View.cpp +++ b/Common/UI/View.cpp @@ -13,6 +13,7 @@ #include "Common/UI/Tween.h" #include "Common/UI/Root.h" #include "Common/GPU/thin3d.h" +#include "Common/System/Request.h" #include "Common/System/System.h" #include "Common/TimeUtil.h" #include "Common/StringUtils.h" @@ -1195,7 +1196,7 @@ bool TextEdit::Key(const KeyInput &input) { switch (input.keyCode) { case NKCODE_C: // Just copy the entire text contents, until we get selection support. - System_SendMessage("setclipboardtext", text_.c_str()); + System_CopyStringToClipboard(text_.c_str()); break; case NKCODE_V: { diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index 28ca9d75ef..2b4ba23a66 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -331,6 +331,12 @@ bool MainUI::HandleCustomEvent(QEvent *e) { bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { switch (type) { + case SystemRequestType::EXIT_APP: + qApp->exit(0); + return true; + case SystemRequestType::COPY_TO_CLIPBOARD: + QApplication::clipboard()->setText(parameter); + return true; case SystemRequestType::INPUT_TEXT_MODAL: { g_requestId = requestId; @@ -362,9 +368,7 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string } void System_SendMessage(const char *command, const char *parameter) { - if (!strcmp(command, "finish")) { - qApp->exit(0); - } else if (!strcmp(command, "graphics_restart")) { + if (!strcmp(command, "graphics_restart")) { // Should find a way to properly restart the app. qApp->exit(0); } else if (!strcmp(command, "camera_command")) { @@ -375,8 +379,6 @@ void System_SendMessage(const char *command, const char *parameter) { } else if (!strcmp(parameter, "stopVideo")) { emit(qtcamera->onStopCamera()); } - } else if (!strcmp(command, "setclipboardtext")) { - QApplication::clipboard()->setText(parameter); #if defined(SDL) } else if (!strcmp(command, "audio_resetDevice")) { StopSDLAudioDevice(); diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index ad80509a44..c998f79c9b 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -170,6 +170,13 @@ 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::EXIT_APP: + // Do a clean exit + g_QuitRequested = true; + break; + case SystemRequestType::COPY_TO_CLIPBOARD: + SDL_SetClipboardText(param1.c_str()); + break; #if PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS) case SystemRequestType::BROWSE_FOR_FILE: { @@ -213,14 +220,9 @@ void System_SendMessage(const char *command, const char *parameter) { // Just toggle. g_ToggleFullScreenType = -1; } - } else if (!strcmp(command, "finish")) { - // Do a clean exit - g_QuitRequested = true; } else if (!strcmp(command, "graphics_restart")) { // Not sure how we best do this, but do a clean exit, better than being stuck in a bad state. g_QuitRequested = true; - } else if (!strcmp(command, "setclipboardtext")) { - SDL_SetClipboardText(parameter); } else if (!strcmp(command, "audio_resetDevice")) { StopSDLAudioDevice(); InitSDLAudioDevice(); diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index 470efbca15..e3b571aa4e 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -2107,8 +2107,8 @@ UI::EventReturn HostnameSelectScreen::OnIPClick(UI::EventParams& e) { std::string text = e.v ? e.v->Tag() : ""; if (text.length() > 0) { addrView_->SetText(text); - // TODO: Copy the IP to clipboard for the host to easily share their IP through chatting apps. - System_SendMessage("setclipboardtext", text.c_str()); // Doesn't seems to be working on windows (yet?) + // Copy the IP to clipboard for the host to easily share their IP through chatting apps. + System_CopyStringToClipboard(text); } return UI::EVENT_DONE; } diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index 768b3c4f21..69e9486621 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -1432,7 +1432,7 @@ UI::EventReturn MainScreen::OnExit(UI::EventParams &e) { } // Request the framework to exit cleanly. - System_SendMessage("finish", ""); + System_ExitApp(); UpdateUIState(UISTATE_EXIT); return UI::EVENT_DONE; diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 6ff7dd17fb..babf0e1b6d 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1289,7 +1289,7 @@ bool NativeKey(const KeyInput &key) { pspKeys.clear(); if (KeyMap::KeyToPspButton(key.deviceId, key.keyCode, &pspKeys)) { if (std::find(pspKeys.begin(), pspKeys.end(), VIRTKEY_PAUSE) != pspKeys.end()) { - System_SendMessage("finish", ""); + System_ExitApp(); return true; } } @@ -1390,8 +1390,6 @@ void NativeShutdown() { g_screenManager = nullptr; } -#if !PPSSPP_PLATFORM(UWP) -#endif g_Config.Save("NativeShutdown"); INFO_LOG(SYSTEM, "NativeShutdown called"); @@ -1404,7 +1402,9 @@ void NativeShutdown() { ShutdownWebServer(); - System_SendMessage("finish", ""); +#if PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(IOS) + System_ExitApp(); +#endif net::Shutdown(); @@ -1423,6 +1423,10 @@ void NativeShutdown() { g_threadManager.Teardown(); +#if !(PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(IOS)) + System_ExitApp(); +#endif + // Previously we did exit() here on Android but that makes it hard to do things like restart on backend change. // I think we handle most globals correctly or correct-enough now. } diff --git a/UI/PauseScreen.cpp b/UI/PauseScreen.cpp index 4738e425d9..f3405fdc89 100644 --- a/UI/PauseScreen.cpp +++ b/UI/PauseScreen.cpp @@ -28,6 +28,7 @@ #include "Common/Data/Text/I18n.h" #include "Common/StringUtils.h" #include "Common/System/System.h" +#include "Common/System/Request.h" #include "Common/VR/PPSSPPVR.h" #include "Common/UI/AsyncImageFileView.h" @@ -392,7 +393,7 @@ UI::EventReturn GamePauseScreen::OnScreenshotClicked(UI::EventParams &e) { UI::EventReturn GamePauseScreen::OnExitToMenu(UI::EventParams &e) { if (g_Config.bPauseMenuExitsEmulator) { - System_SendMessage("finish", ""); + System_ExitApp(); } else { TriggerFinish(DR_OK); } diff --git a/UWP/PPSSPP_UWPMain.cpp b/UWP/PPSSPP_UWPMain.cpp index d174437aee..9e27c82f1a 100644 --- a/UWP/PPSSPP_UWPMain.cpp +++ b/UWP/PPSSPP_UWPMain.cpp @@ -494,9 +494,7 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string void System_SendMessage(const char *command, const char *parameter) { using namespace concurrency; - if (!strcmp(command, "finish")) { - // Not really supposed to support this under UWP. - } else if (!strcmp(command, "toggle_fullscreen")) { + if (!strcmp(command, "toggle_fullscreen")) { auto view = Windows::UI::ViewManagement::ApplicationView::GetForCurrentView(); bool flag = !view->IsFullScreenMode; if (strcmp(parameter, "0") == 0) { diff --git a/Windows/main.cpp b/Windows/main.cpp index 59e83a0722..1dd21cbc74 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -443,6 +443,17 @@ std::wstring MakeFilter(std::wstring filter) { bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { switch (type) { + case SystemRequestType::EXIT_APP: + if (!NativeIsRestarting()) { + PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0); + } + return true; + case SystemRequestType::COPY_TO_CLIPBOARD: + { + std::wstring data = ConvertUTF8ToWString(param1); + W32Util::CopyTextToClipboard(MainWindow::GetDisplayHWND(), data); + return true; + } case SystemRequestType::INPUT_TEXT_MODAL: if (g_dialogRunning) { g_dialogThread.join(); @@ -522,11 +533,7 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string } void System_SendMessage(const char *command, const char *parameter) { - if (!strcmp(command, "finish")) { - if (!NativeIsRestarting()) { - PostMessage(MainWindow::GetHWND(), WM_CLOSE, 0, 0); - } - } else if (!strcmp(command, "graphics_restart")) { + if (!strcmp(command, "graphics_restart")) { restartArgs = parameter == nullptr ? "" : parameter; if (!restartArgs.empty()) AddDebugRestartArgs(); @@ -542,9 +549,6 @@ void System_SendMessage(const char *command, const char *parameter) { std::wstring full_error = ConvertUTF8ToWString(StringFromFormat("%s %s", backendSwitchError, parameter)); std::wstring title = ConvertUTF8ToWString(err->T("GenericGraphicsError", "Graphics Error")); MessageBox(MainWindow::GetHWND(), full_error.c_str(), title.c_str(), MB_OK); - } else if (!strcmp(command, "setclipboardtext")) { - std::wstring data = ConvertUTF8ToWString(parameter); - W32Util::CopyTextToClipboard(MainWindow::GetDisplayHWND(), data); } else if (!strcmp(command, "toggle_fullscreen")) { bool flag = !MainWindow::IsFullscreen(); if (strcmp(parameter, "0") == 0) { diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index b5e8ac937f..1b0e18af77 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -402,6 +402,7 @@ void System_LaunchUrl(LaunchUrlType urlType, const char *url) { } void System_SendMessage(const char *command, const char *parameter) { + // TODO: Using sendmessage this way is deprecated. PushCommand(command, parameter); } @@ -1029,6 +1030,9 @@ extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_backbufferResize(JNIEnv bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { switch (type) { + case SystemRequestType::EXIT_APP: + PushCommand("finish", ""); + return true; case SystemRequestType::INPUT_TEXT_MODAL: { std::string serialized = StringFromFormat("%d:@:%s:@:%s", requestId, param1.c_str(), param2.c_str()); diff --git a/ios/main.mm b/ios/main.mm index c879c0793d..b63b475b2e 100644 --- a/ios/main.mm +++ b/ios/main.mm @@ -177,14 +177,7 @@ void System_Notify(SystemNotification notification) { } void System_SendMessage(const char *command, const char *parameter) { - if (!strcmp(command, "finish")) { - exit(0); - // The below seems right, but causes hangs. See #12140. - // dispatch_async(dispatch_get_main_queue(), ^{ - // [sharedViewController shutdown]; - // exit(0); - // }); - } else if (!strcmp(command, "sharetext")) { + if (!strcmp(command, "sharetext")) { NSString *text = [NSString stringWithUTF8String:parameter]; [sharedViewController shareText:text]; } else if (!strcmp(command, "camera_command")) { @@ -215,6 +208,14 @@ void System_SendMessage(const char *command, const char *parameter) { bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { switch (type) { + case SystemRequestType::EXIT_APP: + exit(0); + // The below seems right, but causes hangs. See #12140. + // dispatch_async(dispatch_get_main_queue(), ^{ + // [sharedViewController shutdown]; + // exit(0); + // }); + break; case SystemRequestType::BROWSE_FOR_FILE: { DarwinDirectoryPanelCallback callback = [requestId] (bool success, Path path) {