From efceb031ce0ea86f8ed33fcd304a72a1efef0be2 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 8 Mar 2020 18:59:17 -0700 Subject: [PATCH] UI: Make System_InputBox API asynchronous. Doing this as a precursor to cleaning up the Android madness. --- Core/Dialog/PSPOskDialog.cpp | 22 ++++++---- Qt/QtMain.cpp | 14 +++--- UI/ChatScreen.cpp | 12 +++--- UI/GameSettingsScreen.cpp | 84 ++++++++++++++++-------------------- Windows/main.cpp | 18 ++------ ext/native/base/NativeApp.h | 4 +- headless/Headless.cpp | 2 +- unittest/JitHarness.cpp | 2 +- 8 files changed, 70 insertions(+), 88 deletions(-) diff --git a/Core/Dialog/PSPOskDialog.cpp b/Core/Dialog/PSPOskDialog.cpp index 683aaf135a..96b36814fb 100755 --- a/Core/Dialog/PSPOskDialog.cpp +++ b/Core/Dialog/PSPOskDialog.cpp @@ -15,9 +15,10 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. +#include #include "i18n/i18n.h" #include "math/math_util.h" -#include +#include "util/text/utf8.h" #include "Core/Dialog/PSPOskDialog.h" #include "Core/Util/PPGeDraw.h" @@ -853,14 +854,19 @@ int PSPOskDialog::NativeKeyboard() { if (defaultText.empty()) defaultText.assign(L"VALUE"); - if (System_InputBoxGetWString(titleText.c_str(), defaultText, inputChars)) { - u32 maxLength = FieldMaxLength(); - if (inputChars.length() > maxLength) { - ERROR_LOG(SCEUTILITY, "NativeKeyboard: input text too long(%d characters/glyphs max), truncating to game-requested length.", maxLength); - inputChars.erase(maxLength, std::string::npos); + // TODO: This is USING_WIN_UI only, so we rely on it being synchronous... + // But we should really have this set some state that is checked each time NativeKeyboard is called. + System_InputBoxGetString(ConvertWStringToUTF8(titleText), ConvertWStringToUTF8(defaultText), [&](bool result, const std::string &value) { + if (result) { + inputChars = ConvertUTF8ToWString(value); + u32 maxLength = FieldMaxLength(); + if (inputChars.length() > maxLength) { + ERROR_LOG(SCEUTILITY, "NativeKeyboard: input text too long(%d characters/glyphs max), truncating to game-requested length.", maxLength); + inputChars.erase(maxLength, std::string::npos); + } } - } - ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0); + ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0); + }); u16_le *outText = oskParams->fields[0].outtext; diff --git a/Qt/QtMain.cpp b/Qt/QtMain.cpp index 7dd7469163..9b3345678a 100644 --- a/Qt/QtMain.cpp +++ b/Qt/QtMain.cpp @@ -227,13 +227,13 @@ void System_SendMessage(const char *command, const char *parameter) { void System_AskForPermission(SystemPermission permission) {} PermissionStatus System_GetPermissionStatus(SystemPermission permission) { return PERMISSION_STATUS_GRANTED; } -bool System_InputBoxGetString(const char *title, const char *defaultValue, char *outValue, size_t outLength) -{ - QString text = emugl->InputBoxGetQString(QString(title), QString(defaultValue)); - if (text.isEmpty()) - return false; - strcpy(outValue, text.toStdString().c_str()); - return true; +void System_InputBoxGetString(const std::string &title, const std::string &defaultValue, std::function cb) { + QString text = emugl->InputBoxGetQString(QString::fromStdString(title), QString::fromStdString(defaultValue)); + if (text.isEmpty()) { + cb(false, ""); + } else { + cb(true, text.toStdString()); + } } void Vibrate(int length_ms) { diff --git a/UI/ChatScreen.cpp b/UI/ChatScreen.cpp index 21d4dfba5c..2f903bb774 100644 --- a/UI/ChatScreen.cpp +++ b/UI/ChatScreen.cpp @@ -26,13 +26,11 @@ void ChatMenu::CreatePopupContents(UI::ViewGroup *parent) { #if defined(USING_WIN_UI) //freeze the ui when using ctrl + C hotkey need workaround if (g_Config.bBypassOSKWithKeyboard && !g_Config.bFullScreen) { - std::wstring titleText = ConvertUTF8ToWString(n->T("Chat")); - std::wstring defaultText = ConvertUTF8ToWString(n->T("Chat Here")); - std::wstring inputChars; - if (System_InputBoxGetWString(titleText.c_str(), defaultText, inputChars)) { - //chatEdit_->SetText(ConvertWStringToUTF8(inputChars)); - sendChat(ConvertWStringToUTF8(inputChars)); - } + System_InputBoxGetString(n->T("Chat"), n->T("Chat Here"), [](bool result, const std::string &value) { + if (result) { + sendChat(value); + } + }); } #endif chatEdit_->OnEnter.Handle(this, &ChatMenu::OnSubmit); diff --git a/UI/GameSettingsScreen.cpp b/UI/GameSettingsScreen.cpp index d7e995f288..cc61e7969a 100644 --- a/UI/GameSettingsScreen.cpp +++ b/UI/GameSettingsScreen.cpp @@ -1290,14 +1290,12 @@ UI::EventReturn GameSettingsScreen::OnAudioDevice(UI::EventParams &e) { UI::EventReturn GameSettingsScreen::OnChangeQuickChat0(UI::EventParams &e) { #if PPSSPP_PLATFORM(WINDOWS) || defined(USING_QT_UI) - const size_t chat_len = 64; - - char chat[chat_len]; - memset(chat, 0, sizeof(chat)); - - if (System_InputBoxGetString("Enter Quick Chat 1", g_Config.sQuickChat0.c_str(), chat, chat_len)) { - g_Config.sQuickChat0 = chat; - } + auto n = GetI18NCategory("Networking"); + System_InputBoxGetString(n->T("Enter Quick Chat 1"), g_Config.sQuickChat0, [](bool result, const std::string &value) { + if (result) { + g_Config.sQuickChat0 = value; + } + }); #elif defined(__ANDROID__) System_SendMessage("inputbox", ("quickchat0:" + g_Config.sQuickChat0).c_str()); #endif @@ -1306,14 +1304,12 @@ UI::EventReturn GameSettingsScreen::OnChangeQuickChat0(UI::EventParams &e) { UI::EventReturn GameSettingsScreen::OnChangeQuickChat1(UI::EventParams &e) { #if PPSSPP_PLATFORM(WINDOWS) || defined(USING_QT_UI) - const size_t chat_len = 64; - - char chat[chat_len]; - memset(chat, 0, sizeof(chat)); - - if (System_InputBoxGetString("Enter Quick Chat 2", g_Config.sQuickChat1.c_str(), chat, chat_len)) { - g_Config.sQuickChat1 = chat; - } + auto n = GetI18NCategory("Networking"); + System_InputBoxGetString(n->T("Enter Quick Chat 2"), g_Config.sQuickChat1, [](bool result, const std::string &value) { + if (result) { + g_Config.sQuickChat1 = value; + } + }); #elif defined(__ANDROID__) System_SendMessage("inputbox", ("quickchat1:" + g_Config.sQuickChat1).c_str()); #endif @@ -1322,14 +1318,12 @@ UI::EventReturn GameSettingsScreen::OnChangeQuickChat1(UI::EventParams &e) { UI::EventReturn GameSettingsScreen::OnChangeQuickChat2(UI::EventParams &e) { #if PPSSPP_PLATFORM(WINDOWS) || defined(USING_QT_UI) - const size_t chat_len = 64; - - char chat[chat_len]; - memset(chat, 0, sizeof(chat)); - - if (System_InputBoxGetString("Enter Quick Chat 3", g_Config.sQuickChat2.c_str(), chat, chat_len)) { - g_Config.sQuickChat2 = chat; - } + auto n = GetI18NCategory("Networking"); + System_InputBoxGetString(n->T("Enter Quick Chat 3"), g_Config.sQuickChat2, [](bool result, const std::string &value) { + if (result) { + g_Config.sQuickChat2 = value; + } + }); #elif defined(__ANDROID__) System_SendMessage("inputbox", ("quickchat2:" + g_Config.sQuickChat2).c_str()); #endif @@ -1338,14 +1332,12 @@ UI::EventReturn GameSettingsScreen::OnChangeQuickChat2(UI::EventParams &e) { UI::EventReturn GameSettingsScreen::OnChangeQuickChat3(UI::EventParams &e) { #if PPSSPP_PLATFORM(WINDOWS) || defined(USING_QT_UI) - const size_t chat_len = 64; - - char chat[chat_len]; - memset(chat, 0, sizeof(chat)); - - if (System_InputBoxGetString("Enter Quick Chat 4", g_Config.sQuickChat3.c_str(), chat, chat_len)) { - g_Config.sQuickChat3 = chat; - } + auto n = GetI18NCategory("Networking"); + System_InputBoxGetString(n->T("Enter Quick Chat 4"), g_Config.sQuickChat3, [](bool result, const std::string &value) { + if (result) { + g_Config.sQuickChat3 = value; + } + }); #elif defined(__ANDROID__) System_SendMessage("inputbox", ("quickchat3:" + g_Config.sQuickChat3).c_str()); #endif @@ -1354,14 +1346,12 @@ UI::EventReturn GameSettingsScreen::OnChangeQuickChat3(UI::EventParams &e) { UI::EventReturn GameSettingsScreen::OnChangeQuickChat4(UI::EventParams &e) { #if PPSSPP_PLATFORM(WINDOWS) || defined(USING_QT_UI) - const size_t chat_len = 64; - - char chat[chat_len]; - memset(chat, 0, sizeof(chat)); - - if (System_InputBoxGetString("Enter Quick Chat 5", g_Config.sQuickChat4.c_str(), chat, chat_len)) { - g_Config.sQuickChat4 = chat; - } + auto n = GetI18NCategory("Networking"); + System_InputBoxGetString(n->T("Enter Quick Chat 5"), g_Config.sQuickChat4, [](bool result, const std::string &value) { + if (result) { + g_Config.sQuickChat4 = value; + } + }); #elif defined(__ANDROID__) System_SendMessage("inputbox", ("quickchat4:" + g_Config.sQuickChat4).c_str()); #endif @@ -1370,14 +1360,12 @@ UI::EventReturn GameSettingsScreen::OnChangeQuickChat4(UI::EventParams &e) { UI::EventReturn GameSettingsScreen::OnChangeNickname(UI::EventParams &e) { #if PPSSPP_PLATFORM(WINDOWS) || defined(USING_QT_UI) - const size_t name_len = 256; - - char name[name_len]; - memset(name, 0, sizeof(name)); - - if (System_InputBoxGetString("Enter a new PSP nickname", g_Config.sNickName.c_str(), name, name_len)) { - g_Config.sNickName = StripSpaces(name); - } + auto n = GetI18NCategory("Networking"); + System_InputBoxGetString(n->T("Enter a new PSP nickname"), g_Config.sNickName, [](bool result, const std::string &value) { + if (result) { + g_Config.sNickName = StripSpaces(value); + } + }); #elif defined(__ANDROID__) // TODO: The return value is handled in NativeApp::inputbox_completed. This is horrific. System_SendMessage("inputbox", ("nickname:" + g_Config.sNickName).c_str()); diff --git a/Windows/main.cpp b/Windows/main.cpp index 7acdd73713..8a8d4e616a 100644 --- a/Windows/main.cpp +++ b/Windows/main.cpp @@ -18,6 +18,7 @@ #include "stdafx.h" #include #include +#include #include "Common/CommonWindows.h" #include "Common/OSVersion.h" @@ -337,23 +338,12 @@ void EnableCrashingOnCrashes() { FreeLibrary(kernel32); } -bool System_InputBoxGetString(const char *title, const char *defaultValue, char *outValue, size_t outLength) -{ +void System_InputBoxGetString(const std::string &title, const std::string &defaultValue, std::function cb) { std::string out; if (InputBox_GetString(MainWindow::GetHInstance(), MainWindow::GetHWND(), ConvertUTF8ToWString(title).c_str(), defaultValue, out)) { - strcpy(outValue, out.c_str()); - return true; + cb(true, out); } else { - return false; - } -} - -bool System_InputBoxGetWString(const wchar_t *title, const std::wstring &defaultvalue, std::wstring &outvalue) -{ - if (InputBox_GetWString(MainWindow::GetHInstance(), MainWindow::GetHWND(), title, defaultvalue, outvalue)) { - return true; - } else { - return false; + cb(false, ""); } } diff --git a/ext/native/base/NativeApp.h b/ext/native/base/NativeApp.h index f1f348acc7..2af0c90ce9 100644 --- a/ext/native/base/NativeApp.h +++ b/ext/native/base/NativeApp.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -119,8 +120,7 @@ void OpenDirectory(const char *path); void LaunchBrowser(const char *url); void LaunchMarket(const char *url); void LaunchEmail(const char *email_address); -bool System_InputBoxGetString(const char *title, const char *defaultValue, char *outValue, size_t outlength); -bool System_InputBoxGetWString(const wchar_t *title, const std::wstring &defaultValue, std::wstring &outValue); +void System_InputBoxGetString(const std::string &title, const std::string &defaultValue, std::function cb); void System_SendMessage(const char *command, const char *parameter); PermissionStatus System_GetPermissionStatus(SystemPermission permission); void System_AskForPermission(SystemPermission permission); diff --git a/headless/Headless.cpp b/headless/Headless.cpp index b92f36d6ba..c0494eaf70 100644 --- a/headless/Headless.cpp +++ b/headless/Headless.cpp @@ -81,7 +81,7 @@ bool System_GetPropertyBool(SystemProperty prop) { return false; } void System_SendMessage(const char *command, const char *parameter) {} -bool System_InputBoxGetWString(const wchar_t *title, const std::wstring &defaultvalue, std::wstring &outvalue) { 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/unittest/JitHarness.cpp b/unittest/JitHarness.cpp index a34f3f4952..1569048b18 100644 --- a/unittest/JitHarness.cpp +++ b/unittest/JitHarness.cpp @@ -37,7 +37,7 @@ void NativeRender(GraphicsContext *graphicsContext) { } void NativeResized() { } void System_SendMessage(const char *command, const char *parameter) {} -bool System_InputBoxGetWString(const wchar_t *title, const std::wstring &defaultvalue, std::wstring &outvalue) { 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; }