From b7350f3cbf485f4f658de645a518e1731e35be30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Wed, 22 Mar 2023 17:39:45 +0100 Subject: [PATCH] Fix folder browser on Android and Windows --- Common/System/Request.cpp | 19 ++++++----- Qt/QtMain.cpp | 2 -- SDL/SDLMain.cpp | 32 ++++++++++++------- UI/NativeApp.cpp | 12 +------ Windows/main.cpp | 15 ++++++--- android/jni/app-android.cpp | 3 ++ .../src/org/ppsspp/ppsspp/NativeActivity.java | 10 +++++- 7 files changed, 54 insertions(+), 39 deletions(-) diff --git a/Common/System/Request.cpp b/Common/System/Request.cpp index 7ec0a872f2..13e12f1502 100644 --- a/Common/System/Request.cpp +++ b/Common/System/Request.cpp @@ -16,19 +16,22 @@ const char *RequestTypeAsString(SystemRequestType type) { bool RequestManager::MakeSystemRequest(SystemRequestType type, RequestCallback callback, const std::string ¶m1, const std::string ¶m2, int param3) { int requestId = idCounter_++; + + // NOTE: We need to register immediately, in order to support synchronous implementations. + { + std::lock_guard guard(callbackMutex_); + callbackMap_[requestId] = callback; + } + INFO_LOG(SYSTEM, "Making system request %s: id %d, callback_valid %d", RequestTypeAsString(type), requestId, callback != nullptr); if (!System_MakeRequest(type, requestId, param1, param2, param3)) { + { + std::lock_guard guard(callbackMutex_); + callbackMap_.erase(requestId); + } return false; } - if (!callback) { - // We don't expect a response, this is a one-directional request. We're thus done. - return true; - } - - std::lock_guard guard(callbackMutex_); - INFO_LOG(SYSTEM, "Registering pending callback %d", requestId); - callbackMap_[requestId] = callback; return true; } diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index 966af70b77..28ca9d75ef 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -364,8 +364,6 @@ 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, "browse_folder")) { - QCoreApplication::postEvent(emugl, new QEvent((QEvent::Type)browseFolderEvent)); } else if (!strcmp(command, "graphics_restart")) { // Should find a way to properly restart the app. qApp->exit(0); diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index daf41c79d2..7ac7ae1868 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -166,7 +166,26 @@ void System_Vibrate(int length_ms) { // Ignore on PC } -bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { return false; } +bool System_MakeRequest(SystemRequestType type, int requestId, const std::string ¶m1, const std::string ¶m2, int param3) { + switch (type) { + case SystemRequestType::BROWSE_FOR_FOLDER: +#if PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS) + DarwinDirectoryPanelCallback callback = [](bool success, Path thePathChosen) { + if (success) { + g_requestManager.PostSystemSuccess(requestId, thePathChosen.c_str()); + } else { + g_requestManager.PostSystemFailure(requestId); + } + }; + DarwinFileSystemServices services; + services.presentDirectoryPanel(callback, /* allowFiles = */ true, /* allowDirectories = */ true); + return true; +#else + return false; +#endif + } + return false; +} void System_SendMessage(const char *command, const char *parameter) { if (!strcmp(command, "toggle_fullscreen")) { @@ -191,17 +210,6 @@ void System_SendMessage(const char *command, const char *parameter) { StopSDLAudioDevice(); InitSDLAudioDevice(); } -#if PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS) - else if (!strcmp(command, "browse_folder")) { - DarwinDirectoryPanelCallback callback = [] (Path thePathChosen) { - NativeMessageReceived("browse_folder", thePathChosen.c_str()); - }; - - DarwinFileSystemServices services; - services.presentDirectoryPanel(callback, /* allowFiles = */ true, /* allowDirectorites = */ true); - } -#endif - } void System_AskForPermission(SystemPermission permission) {} diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index ba675e4b29..6ff7dd17fb 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -1215,19 +1215,9 @@ void HandleGlobalMessage(const std::string &msg, const std::string &value) { g_Config.Reload(); PostLoadConfig(); g_Config.iGPUBackend = gpuBackend; - } - else if (msg == "app_resumed" || msg == "got_focus") { + } else if (msg == "app_resumed" || msg == "got_focus") { // Assume that the user may have modified things. MemoryStick_NotifyWrite(); - } else if (msg == "browse_folder") { - Path thePath = Path(value); - File::FileInfo info; - if (!File::GetFileInfo(thePath, &info)) - return; - if (info.isDirectory) - NativeMessageReceived("browse_folderSelect", thePath.c_str()); - else - NativeMessageReceived("browse_fileSelect", thePath.c_str()); } } diff --git a/Windows/main.cpp b/Windows/main.cpp index b5ebb2dadb..59e83a0722 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -506,6 +506,16 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string }); return true; } + case SystemRequestType::BROWSE_FOR_FOLDER: + { + std::string folder = W32Util::BrowseForFolder(MainWindow::GetHWND(), param1.c_str()); + if (folder.size()) { + g_requestManager.PostSystemSuccess(requestId, folder.c_str()); + } else { + g_requestManager.PostSystemFailure(requestId); + } + return true; + } default: return false; } @@ -535,11 +545,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_folder")) { - auto mm = GetI18NCategory("MainMenu"); - std::string folder = W32Util::BrowseForFolder(MainWindow::GetHWND(), mm->T("Choose folder")); - if (folder.size()) - NativeMessageReceived("browse_folderSelect", folder.c_str()); } 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 0674c6728d..b5e8ac937f 100644 --- a/android/jni/app-android.cpp +++ b/android/jni/app-android.cpp @@ -1041,6 +1041,9 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string case SystemRequestType::BROWSE_FOR_FILE: PushCommand("browse_file", StringFromFormat("%d", requestId)); return true; + case SystemRequestType::BROWSE_FOR_FOLDER: + PushCommand("browse_folder", StringFromFormat("%d", requestId)); + return true; default: return false; } diff --git a/android/src/org/ppsspp/ppsspp/NativeActivity.java b/android/src/org/ppsspp/ppsspp/NativeActivity.java index 288087aaec..bd6857bef4 100644 --- a/android/src/org/ppsspp/ppsspp/NativeActivity.java +++ b/android/src/org/ppsspp/ppsspp/NativeActivity.java @@ -108,6 +108,7 @@ public abstract class NativeActivity extends Activity { // These can probably be merged, but conceptually nice to have them separate. private int imageRequestId = -1; private int fileRequestId = -1; + private int folderRequestId = -1; // Allow for multiple connected gamepads but just consider them the same for now. // Actually this is not entirely true, see the code. @@ -1164,6 +1165,7 @@ public abstract class NativeActivity extends Activity { imageRequestId = -1; } else if (requestCode == RESULT_OPEN_DOCUMENT) { if (resultCode != RESULT_OK || data == null) { + NativeApp.sendRequestResult(fileRequestId, false, "", 0); return; } Uri selectedFile = data.getData(); @@ -1186,6 +1188,7 @@ public abstract class NativeActivity extends Activity { fileRequestId = -1; } else if (requestCode == RESULT_OPEN_DOCUMENT_TREE) { if (resultCode != RESULT_OK || data == null) { + NativeApp.sendRequestResult(folderRequestId, false, "", 0); return; } Uri selectedDirectoryUri = data.getData(); @@ -1208,8 +1211,9 @@ public abstract class NativeActivity extends Activity { Log.i(TAG, "Child: " + child.getUri() + " " + child.getName()); } */ - NativeApp.sendMessage("browse_folderSelect", documentFile.getUri().toString()); + NativeApp.sendRequestResult(folderRequestId, true, documentFile.getUri().toString(), 0); } + folderRequestId = -1; } } @@ -1346,6 +1350,7 @@ public abstract class NativeActivity extends Activity { startActivityForResult(i, RESULT_LOAD_IMAGE); return true; } catch (Exception e) { // For example, android.content.ActivityNotFoundException + imageRequestId = -1; Log.e(TAG, e.toString()); return false; } @@ -1362,11 +1367,13 @@ public abstract class NativeActivity extends Activity { startActivityForResult(intent, RESULT_OPEN_DOCUMENT); // intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri); } catch (Exception e) { + fileRequestId = -1; Log.e(TAG, e.toString()); return false; } } else if (command.equals("browse_folder")) { try { + folderRequestId = Integer.parseInt(params); Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); intent.addFlags(Intent.FLAG_GRANT_PREFIX_URI_PERMISSION); @@ -1375,6 +1382,7 @@ public abstract class NativeActivity extends Activity { startActivityForResult(intent, RESULT_OPEN_DOCUMENT_TREE); return true; } catch (Exception e) { + folderRequestId = -1; Log.e(TAG, e.toString()); return false; }