Skip to content

Commit

Permalink
Merge pull request #17159 from hrydgard/port-image-selector
Browse files Browse the repository at this point in the history
Port image selector to the new request manager
  • Loading branch information
hrydgard authored Mar 22, 2023
2 parents 96012b2 + 0403bbf commit d87f03f
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 101 deletions.
5 changes: 5 additions & 0 deletions Common/System/Request.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ RequestManager g_requestManager;
const char *RequestTypeAsString(SystemRequestType type) {
switch (type) {
case SystemRequestType::INPUT_TEXT_MODAL: return "INPUT_TEXT_MODAL";
case SystemRequestType::BROWSE_FOR_IMAGE: return "BROWSE_FOR_IMAGE";
default: return "N/A";
}
}

bool RequestManager::MakeSystemRequest(SystemRequestType type, RequestCallback callback, const std::string &param1, const std::string &param2) {
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)) {
return false;
}
Expand All @@ -23,6 +25,7 @@ bool RequestManager::MakeSystemRequest(SystemRequestType type, RequestCallback c
}

std::lock_guard<std::mutex> guard(callbackMutex_);
INFO_LOG(SYSTEM, "Registering pending callback %d", requestId);
callbackMap_[requestId] = callback;
return true;
}
Expand All @@ -41,6 +44,7 @@ void RequestManager::PostSystemSuccess(int requestId, const char *responseString
response.responseString = responseString;
response.responseValue = responseValue;
pendingResponses_.push_back(response);
INFO_LOG(SYSTEM, "PostSystemSuccess: Request %d (%s, %d)", requestId, responseString, responseValue);
}

void RequestManager::PostSystemFailure(int requestId) {
Expand All @@ -50,6 +54,7 @@ void RequestManager::PostSystemFailure(int requestId) {
ERROR_LOG(SYSTEM, "PostSystemFailure: Unexpected request ID %d", requestId);
return;
}
INFO_LOG(SYSTEM, "PostSystemFailure: Request %d failed", requestId);
callbackMap_.erase(iter);
}

Expand Down
4 changes: 4 additions & 0 deletions Common/System/Request.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,7 @@ extern RequestManager g_requestManager;
inline void System_InputBoxGetString(const std::string &title, const std::string &defaultValue, RequestCallback callback) {
g_requestManager.MakeSystemRequest(SystemRequestType::INPUT_TEXT_MODAL, callback, title, defaultValue);
}

inline void System_BrowseForImage(const std::string &title, RequestCallback callback) {
g_requestManager.MakeSystemRequest(SystemRequestType::BROWSE_FOR_IMAGE, callback, title, "");
}
1 change: 1 addition & 0 deletions Common/System/System.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ void System_LaunchUrl(LaunchUrlType urlType, const char *url);

enum class SystemRequestType {
INPUT_TEXT_MODAL,
BROWSE_FOR_IMAGE,
};

// Implementations are supposed to process the request, and post the response to the g_RequestManager (see Message.h).
Expand Down
2 changes: 1 addition & 1 deletion UI/GameScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ void SetBackgroundPopupScreen::update() {
File::WriteStringToFile(false, pic->data, bgPng);
}

NativeMessageReceived("bgImage_updated", "");
UIBackgroundShutdown();

// It's worse if it flickers, stay open for at least 1s.
timeDone_ = timeStart_ + 1.0;
Expand Down
15 changes: 10 additions & 5 deletions UI/GameSettingsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1312,12 +1312,17 @@ UI::EventReturn GameSettingsScreen::OnChangeBackground(UI::EventParams &e) {
if (File::Exists(bgPng) || File::Exists(bgJpg)) {
File::Delete(bgPng);
File::Delete(bgJpg);

NativeMessageReceived("bgImage_updated", "");
UIBackgroundShutdown();
} else {
if (System_GetPropertyBool(SYSPROP_HAS_IMAGE_BROWSER)) {
System_SendMessage("bgImage_browse", "");
}
auto sy = GetI18NCategory("System");
System_BrowseForImage(sy->T("Set UI background..."), [](const std::string &value, int) {
if (!value.empty()) {
Path dest = GetSysDirectory(DIRECTORY_SYSTEM) / (endsWithNoCase(value, ".jpg") ? "background.jpg" : "background.png");
File::Copy(Path(value), dest);
}
// It will init again automatically. We can't init outside a frame on Vulkan.
UIBackgroundShutdown();
});
}

// Change to a browse or clear button.
Expand Down
8 changes: 0 additions & 8 deletions UI/NativeApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1167,14 +1167,6 @@ void HandleGlobalMessage(const std::string &msg, const std::string &value) {
else if (msg == "inputDeviceConnected") {
KeyMap::NotifyPadConnected(nextInputDeviceID, value);
}
else if (msg == "bgImage_updated") {
if (!value.empty()) {
Path dest = GetSysDirectory(DIRECTORY_SYSTEM) / (endsWithNoCase(value, ".jpg") ? "background.jpg" : "background.png");
File::Copy(Path(value), dest);
}
UIBackgroundShutdown();
// It will init again automatically. We can't init outside a frame on Vulkan.
}
else if (msg == "savestate_displayslot") {
auto sy = GetI18NCategory("System");
std::string msg = StringFromFormat("%s: %d", sy->T("Savestate Slot"), SaveState::GetCurrentSlot() + 1);
Expand Down
4 changes: 0 additions & 4 deletions Windows/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1064,10 +1064,6 @@ namespace MainWindow
BrowseAndBootDone();
break;

case WM_USER_BROWSE_BG_DONE:
BrowseBackgroundDone();
break;

case WM_USER_RESTART_EMUTHREAD:
NativeSetRestarting();
InputDevice::StopPolling();
Expand Down
1 change: 0 additions & 1 deletion Windows/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ namespace MainWindow
enum {
WM_USER_SAVESTATE_FINISH = WM_USER + 100,
WM_USER_UPDATE_UI = WM_USER + 101,
WM_USER_BROWSE_BG_DONE = WM_USER + 102,
WM_USER_WINDOW_TITLE_CHANGED = WM_USER + 103,
WM_USER_BROWSE_BOOT_DONE = WM_USER + 104,
WM_USER_TOGGLE_FULLSCREEN = WM_USER + 105,
Expand Down
32 changes: 0 additions & 32 deletions Windows/MainWindowMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,38 +347,6 @@ namespace MainWindow {
browseDialog = 0;
}

void BrowseBackground() {
static std::wstring filter = L"All supported images (*.jpg *.jpeg *.png)|*.jpg;*.jpeg;*.png|All files (*.*)|*.*||";
for (size_t i = 0; i < filter.length(); i++) {
if (filter[i] == '|')
filter[i] = '\0';
}

W32Util::MakeTopMost(GetHWND(), false);
browseImageDialog = new W32Util::AsyncBrowseDialog(W32Util::AsyncBrowseDialog::OPEN, GetHWND(), WM_USER_BROWSE_BG_DONE, L"LoadFile", L"", filter, L"*.jpg;*.jpeg;*.png;");
}

void BrowseBackgroundDone() {
std::string filename;
if (browseImageDialog->GetResult(filename)) {
std::wstring src = ConvertUTF8ToWString(filename);
std::wstring dest;
if (filename.size() >= 5 && (filename.substr(filename.size() - 4) == ".jpg" || filename.substr(filename.size() - 5) == ".jpeg")) {
dest = (GetSysDirectory(DIRECTORY_SYSTEM) / "background.jpg").ToWString();
} else {
dest = (GetSysDirectory(DIRECTORY_SYSTEM) / "background.png").ToWString();
}

CopyFileW(src.c_str(), dest.c_str(), FALSE);
NativeMessageReceived("bgImage_updated", "");
}

W32Util::MakeTopMost(GetHWND(), g_Config.bTopMost);

delete browseImageDialog;
browseImageDialog = nullptr;
}

static void UmdSwitchAction() {
std::string fn;
std::string filter = "PSP ROMs (*.iso *.cso *.pbp *.elf)|*.pbp;*.elf;*.iso;*.cso;*.prx|All files (*.*)|*.*||";
Expand Down
2 changes: 0 additions & 2 deletions Windows/MainWindowMenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace MainWindow {
void TranslateMenus(HWND hWnd, HMENU menu);
void BrowseAndBoot(std::string defaultPath, bool browseDirectory = false);
void BrowseAndBootDone();
void BrowseBackground();
void BrowseBackgroundDone();
void setTexScalingMultiplier(int level);
void SetIngameMenuItemStates(HMENU menu, const GlobalUIState state);
}
47 changes: 36 additions & 11 deletions Windows/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#include "Common/Data/Encoding/Utf8.h"
#include "Common/Net/Resolve.h"
#include "W32Util/DarkMode.h"
#include "W32Util/ShellUtil.h"

#include "Core/Config.h"
#include "Core/ConfigValues.h"
Expand Down Expand Up @@ -110,8 +111,10 @@ static std::string restartArgs;

int g_activeWindow = 0;

static std::thread g_inputBoxThread;
static bool g_inputBoxRunning = false;
// Used for all the system dialogs.
static std::thread g_dialogThread;
static bool g_dialogRunning = false;

int g_lastNumInstances = 0;

void System_ShowFileInFolder(const char *path) {
Expand Down Expand Up @@ -430,15 +433,23 @@ void System_Notify(SystemNotification notification) {
}
}

std::wstring MakeFilter(std::wstring filter) {
for (size_t i = 0; i < filter.length(); i++) {
if (filter[i] == '|')
filter[i] = '\0';
}
return filter;
}

bool System_MakeRequest(SystemRequestType type, int requestId, const std::string &param1, const std::string &param2) {
switch (type) {
case SystemRequestType::INPUT_TEXT_MODAL:
if (g_inputBoxRunning) {
g_inputBoxThread.join();
if (g_dialogRunning) {
g_dialogThread.join();
}

g_inputBoxRunning = true;
g_inputBoxThread = std::thread([=] {
g_dialogRunning = true;
g_dialogThread = std::thread([=] {
std::string out;
if (InputBox_GetString(MainWindow::GetHInstance(), MainWindow::GetHWND(), ConvertUTF8ToWString(param1).c_str(), param2, out)) {
g_requestManager.PostSystemSuccess(requestId, out.c_str());
Expand All @@ -447,6 +458,22 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
}
});
return true;
case SystemRequestType::BROWSE_FOR_IMAGE:
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,
MakeFilter(L"All supported images (*.jpg *.jpeg *.png)|*.jpg;*.jpeg;*.png|All files (*.*)|*.*||").c_str(), L"jpg", out)) {
g_requestManager.PostSystemSuccess(requestId, out.c_str());
} else {
g_requestManager.PostSystemFailure(requestId);
}
});
return true;
default:
return false;
}
Expand Down Expand Up @@ -483,8 +510,6 @@ void System_SendMessage(const char *command, const char *parameter) {
std::string folder = W32Util::BrowseForFolder(MainWindow::GetHWND(), mm->T("Choose folder"));
if (folder.size())
NativeMessageReceived("browse_folderSelect", folder.c_str());
} else if (!strcmp(command, "bgImage_browse")) {
MainWindow::BrowseBackground();
} else if (!strcmp(command, "toggle_fullscreen")) {
bool flag = !MainWindow::IsFullscreen();
if (strcmp(parameter, "0") == 0) {
Expand Down Expand Up @@ -627,9 +652,9 @@ static void WinMainInit() {
}

static void WinMainCleanup() {
if (g_inputBoxRunning) {
g_inputBoxThread.join();
g_inputBoxRunning = false;
if (g_dialogRunning) {
g_dialogThread.join();
g_dialogRunning = false;
}
net::Shutdown();
CoUninitialize();
Expand Down
32 changes: 12 additions & 20 deletions android/jni/app-android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,6 @@ bool System_GetPropertyBool(SystemProperty prop) {
}

void System_Notify(SystemNotification notification) {
switch (notification) {
}
}

std::string Android_GetInputDeviceDebugString() {
Expand Down Expand Up @@ -1037,33 +1035,29 @@ bool System_MakeRequest(SystemRequestType type, int requestId, const std::string
PushCommand("inputbox", serialized.c_str());
return true;
}
case SystemRequestType::BROWSE_FOR_IMAGE:
PushCommand("browse_image", StringFromFormat("%d", requestId));
return true;
default:
return false;
}
}

extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendInputBox(JNIEnv *env, jclass, jstring jseqID, jboolean result, jstring jvalue) {
std::string seqID = GetJavaString(env, jseqID);
extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendRequestResult(JNIEnv *env, jclass, jint jrequestID, jboolean result, jstring jvalue, jint jintValue) {
std::string value = GetJavaString(env, jvalue);

static std::string lastSeqID = "";
if (lastSeqID == seqID) {
static jint lastSeqID = -1;
if (lastSeqID == jrequestID) {
// We send this on dismiss, so twice in many cases.
DEBUG_LOG(SYSTEM, "Ignoring duplicate sendInputBox");
return;
}
lastSeqID = seqID;

int seq = 0;
if (!TryParse(seqID, &seq)) {
ERROR_LOG(SYSTEM, "Invalid inputbox seqID value: %s", seqID.c_str());
WARN_LOG(SYSTEM, "Ignoring duplicate sendInputBox");
return;
}
lastSeqID = jrequestID;

if (result) {
g_requestManager.PostSystemSuccess(seq, value.c_str());
g_requestManager.PostSystemSuccess(jrequestID, value.c_str());
} else {
g_requestManager.PostSystemFailure(seq);
g_requestManager.PostSystemFailure(jrequestID);
}
}

Expand Down Expand Up @@ -1225,7 +1219,7 @@ extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_sendMessage(JNIEnv *env
} else if (msg == "sustained_perf_supported") {
sustainedPerfSupported = true;
} else if (msg == "safe_insets") {
INFO_LOG(SYSTEM, "Got insets: %s", prm.c_str());
// INFO_LOG(SYSTEM, "Got insets: %s", prm.c_str());
// We don't bother with supporting exact rectangular regions. Safe insets are good enough.
int left, right, top, bottom;
if (4 == sscanf(prm.c_str(), "%d:%d:%d:%d", &left, &right, &top, &bottom)) {
Expand Down Expand Up @@ -1316,9 +1310,7 @@ extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_setSatInfoAndroid(JNIEn
GPS::setSatInfo(index, id, elevation, azimuth, snr, good);
}

extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_pushCameraImageAndroid(JNIEnv *env, jclass,
jbyteArray image) {

extern "C" void JNICALL Java_org_ppsspp_ppsspp_NativeApp_pushCameraImageAndroid(JNIEnv *env, jclass, jbyteArray image) {
if (image != NULL) {
jlong size = env->GetArrayLength(image);
jbyte* buffer = env->GetByteArrayElements(image, NULL);
Expand Down
Loading

0 comments on commit d87f03f

Please sign in to comment.