From 70f0ac04af43f0cb71db1a55e9d8d65ebb8f014b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 22 Mar 2023 15:21:03 +0100 Subject: [PATCH] Port the file browser spawned by the Load button to the new request stuff --- Common/System/Request.cpp | 4 ++-- Common/System/Request.h | 16 +++++++++++++--- Common/System/System.h | 3 ++- Qt/QtMain.cpp | 2 +- SDL/SDLMain.cpp | 2 +- UI/MainScreen.cpp | 7 ++++++- UWP/PPSSPP_UWPMain.cpp | 2 +- Windows/main.cpp | 36 +++++++++++++++++++++++++++++++++--- android/jni/app-android.cpp | 2 +- headless/Headless.cpp | 2 +- libretro/libretro.cpp | 2 +- unittest/JitHarness.cpp | 2 +- 12 files changed, 63 insertions(+), 17 deletions(-) diff --git a/Common/System/Request.cpp b/Common/System/Request.cpp index 0effd87f1c..0e4e53b97b 100644 --- a/Common/System/Request.cpp +++ b/Common/System/Request.cpp @@ -12,10 +12,10 @@ const char *RequestTypeAsString(SystemRequestType type) { } } -bool RequestManager::MakeSystemRequest(SystemRequestType type, RequestCallback callback, const std::string ¶m1, const std::string ¶m2) { +bool RequestManager::MakeSystemRequest(SystemRequestType type, RequestCallback callback, const std::string ¶m1, const std::string ¶m2, int param3) { int requestId = idCounter_++; INFO_LOG(SYSTEM, "Making system request %s: id %d, callback_valid %d", RequestTypeAsString(type), requestId, callback != nullptr); - if (!System_MakeRequest(type, requestId, param1, param2)) { + if (!System_MakeRequest(type, requestId, param1, param2, param3)) { return false; } diff --git a/Common/System/Request.h b/Common/System/Request.h index 99e42eb500..b0797e4ff8 100644 --- a/Common/System/Request.h +++ b/Common/System/Request.h @@ -16,7 +16,7 @@ class RequestManager { public: // These requests are to be handled by platform implementations. // The callback you pass in will be called on the main thread later. - bool MakeSystemRequest(SystemRequestType type, RequestCallback callback, const std::string ¶m1, const std::string ¶m2); + bool MakeSystemRequest(SystemRequestType type, RequestCallback callback, const std::string ¶m1, const std::string ¶m2, int param3); // Called by the platform implementation, when it's finished with a request. void PostSystemSuccess(int requestId, const char *responseString, int responseValue = 0); @@ -56,9 +56,19 @@ extern RequestManager g_requestManager; // Wrappers for easy requests. // NOTE: Semantics have changed - this no longer calls the callback on cancellation. inline void System_InputBoxGetString(const std::string &title, const std::string &defaultValue, RequestCallback callback) { - g_requestManager.MakeSystemRequest(SystemRequestType::INPUT_TEXT_MODAL, callback, title, defaultValue); + g_requestManager.MakeSystemRequest(SystemRequestType::INPUT_TEXT_MODAL, callback, title, defaultValue, 0); } inline void System_BrowseForImage(const std::string &title, RequestCallback callback) { - g_requestManager.MakeSystemRequest(SystemRequestType::BROWSE_FOR_IMAGE, callback, title, ""); + g_requestManager.MakeSystemRequest(SystemRequestType::BROWSE_FOR_IMAGE, callback, title, "", 0); +} + +enum class BrowseFileType { + BOOTABLE, + INI, + ANY, +}; + +inline void System_BrowseForFile(const std::string &title, BrowseFileType type, RequestCallback callback) { + g_requestManager.MakeSystemRequest(SystemRequestType::BROWSE_FOR_FILE, callback, title, "", (int)type); } diff --git a/Common/System/System.h b/Common/System/System.h index dbb79edaab..1e1c04ea4a 100644 --- a/Common/System/System.h +++ b/Common/System/System.h @@ -54,13 +54,14 @@ void System_LaunchUrl(LaunchUrlType urlType, const char *url); enum class SystemRequestType { INPUT_TEXT_MODAL, BROWSE_FOR_IMAGE, + BROWSE_FOR_FILE, }; // 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. -bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2); +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". void System_SendMessage(const char *command, const char *parameter); diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index ba475e5141..e52a563915 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -311,7 +311,7 @@ bool MainUI::HandleCustomEvent(QEvent *e) { return true; } -bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2) { +bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { switch (type) { case SystemRequestType::INPUT_TEXT_MODAL: { diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index 3bda836e98..daf41c79d2 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -166,7 +166,7 @@ void System_Vibrate(int length_ms) { // Ignore on PC } -bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2) { return false; } +bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { return false; } void System_SendMessage(const char *command, const char *parameter) { if (!strcmp(command, "toggle_fullscreen")) { diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index 682fe85074..78015eadfc 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -23,6 +23,8 @@ #include "Common/System/Display.h" #include "Common/System/System.h" +#include "Common/System/Request.h" +#include "Common/System/NativeApp.h" #include "Common/Render/TextureAtlas.h" #include "Common/Render/DrawBuffer.h" #include "Common/UI/Root.h" @@ -1278,7 +1280,10 @@ void MainScreen::update() { UI::EventReturn MainScreen::OnLoadFile(UI::EventParams &e) { if (System_GetPropertyBool(SYSPROP_HAS_FILE_BROWSER)) { - System_SendMessage("browse_file", ""); + auto mm = GetI18NCategory("MainMenu"); + System_BrowseForFile(mm->T("Load"), BrowseFileType::BOOTABLE, [](const std::string &value, int) { + NativeMessageReceived("boot", value.c_str()); + }); } return UI::EVENT_DONE; } diff --git a/UWP/PPSSPP_UWPMain.cpp b/UWP/PPSSPP_UWPMain.cpp index 39e27cbf56..7811986fa0 100644 --- a/UWP/PPSSPP_UWPMain.cpp +++ b/UWP/PPSSPP_UWPMain.cpp @@ -443,7 +443,7 @@ void System_Notify(SystemNotification notification) { } } -bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2) { return false; } +bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { return false; } void System_SendMessage(const char *command, const char *parameter) { using namespace concurrency; diff --git a/Windows/main.cpp b/Windows/main.cpp index 4314acda19..b5ebb2dadb 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -441,7 +441,7 @@ std::wstring MakeFilter(std::wstring filter) { return filter; } -bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2) { +bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { switch (type) { case SystemRequestType::INPUT_TEXT_MODAL: if (g_dialogRunning) { @@ -474,6 +474,38 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string } }); return true; + case SystemRequestType::BROWSE_FOR_FILE: + { + BrowseFileType type = (BrowseFileType)param3; + std::wstring filter; + switch (type) { + case BrowseFileType::BOOTABLE: + filter = MakeFilter(L"All supported file types (*.iso *.cso *.pbp *.elf *.prx *.zip *.ppdmp)|*.pbp;*.elf;*.iso;*.cso;*.prx;*.zip;*.ppdmp|PSP ROMs (*.iso *.cso *.pbp *.elf *.prx)|*.pbp;*.elf;*.iso;*.cso;*.prx|Homebrew/Demos installers (*.zip)|*.zip|All files (*.*)|*.*||"); + break; + case BrowseFileType::INI: + filter = MakeFilter(L"Ini files (*.ini)|*.ini|All files (*.*)|*.*||"); + break; + case BrowseFileType::ANY: + filter = MakeFilter(L"All files (*.*)|*.*||"); + break; + default: + return false; + } + if (g_dialogRunning) { + g_dialogThread.join(); + } + + g_dialogRunning = true; + g_dialogThread = std::thread([=] { + std::string out; + if (W32Util::BrowseForFileName(true, MainWindow::GetHWND(), ConvertUTF8ToWString(param1).c_str(), nullptr, filter.c_str(), L"", out)) { + g_requestManager.PostSystemSuccess(requestId, out.c_str()); + } else { + g_requestManager.PostSystemFailure(requestId); + } + }); + return true; + } default: return false; } @@ -503,8 +535,6 @@ void System_SendMessage(const char *command, const char *parameter) { } else if (!strcmp(command, "setclipboardtext")) { std::wstring data = ConvertUTF8ToWString(parameter); W32Util::CopyTextToClipboard(MainWindow::GetDisplayHWND(), data); - } else if (!strcmp(command, "browse_file")) { - MainWindow::BrowseAndBoot(""); } else if (!strcmp(command, "browse_folder")) { auto mm = GetI18NCategory("MainMenu"); std::string folder = W32Util::BrowseForFolder(MainWindow::GetHWND(), mm->T("Choose folder")); diff --git a/android/jni/app-android.cpp b/android/jni/app-android.cpp index 60a4aa46d7..730671b2f0 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -1027,7 +1027,7 @@ 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) { +bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { switch (type) { case SystemRequestType::INPUT_TEXT_MODAL: { diff --git a/headless/Headless.cpp b/headless/Headless.cpp index 8d6ae20519..137b3ae1f3 100644 --- a/headless/Headless.cpp +++ b/headless/Headless.cpp @@ -116,7 +116,7 @@ bool System_GetPropertyBool(SystemProperty prop) { } void System_Notify(SystemNotification notification) {} void System_SendMessage(const char *command, const char *parameter) {} -bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2) { return false; } +bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { return false; } void System_InputBoxGetString(const std::string &title, const std::string &defaultValue, std::function cb) { cb(false, ""); } void System_AskForPermission(SystemPermission permission) {} PermissionStatus System_GetPermissionStatus(SystemPermission permission) { return PERMISSION_STATUS_GRANTED; } diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index cdb699bb56..2289b1fb97 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -1874,7 +1874,7 @@ void System_Notify(SystemNotification notification) { break; } } -bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2) { return false; } +bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { return false; } void System_SendMessage(const char *command, const char *parameter) {} void NativeUpdate() {} void NativeRender(GraphicsContext *graphicsContext) {} diff --git a/unittest/JitHarness.cpp b/unittest/JitHarness.cpp index f730cc44d6..d578c0af13 100644 --- a/unittest/JitHarness.cpp +++ b/unittest/JitHarness.cpp @@ -42,7 +42,7 @@ void NativeRender(GraphicsContext *graphicsContext) { } void NativeResized() { } void System_SendMessage(const char *command, const char *parameter) {} -bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2) { return false; } +bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { return false; } void System_InputBoxGetString(const std::string &title, const std::string &defaultValue, std::function cb) { cb(false, ""); } void System_AskForPermission(SystemPermission permission) {} PermissionStatus System_GetPermissionStatus(SystemPermission permission) { return PERMISSION_STATUS_GRANTED; }