From 8b433beff34c382e50334bb59c4f71394845558c Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Mon, 12 Nov 2018 11:08:09 -0500 Subject: [PATCH] software_keyboard: Make GetText asynchronous a --- .../frontend/applets/software_keyboard.cpp | 7 ++--- src/core/frontend/applets/software_keyboard.h | 7 +++-- src/core/hle/service/am/am.cpp | 2 +- .../service/am/applets/software_keyboard.cpp | 19 +++++++++---- .../service/am/applets/software_keyboard.h | 5 ++++ src/yuzu/applets/software_keyboard.cpp | 27 ++++++++++++------- src/yuzu/applets/software_keyboard.h | 15 ++++++++--- src/yuzu/main.cpp | 6 ++--- src/yuzu/main.h | 5 ++-- 9 files changed, 64 insertions(+), 29 deletions(-) diff --git a/src/core/frontend/applets/software_keyboard.cpp b/src/core/frontend/applets/software_keyboard.cpp index 2445a980d1..4105101b36 100644 --- a/src/core/frontend/applets/software_keyboard.cpp +++ b/src/core/frontend/applets/software_keyboard.cpp @@ -9,12 +9,13 @@ namespace Core::Frontend { SoftwareKeyboardApplet::~SoftwareKeyboardApplet() = default; -std::optional DefaultSoftwareKeyboardApplet::GetText( +void DefaultSoftwareKeyboardApplet::RequestText( + std::function)> out, SoftwareKeyboardParameters parameters) const { if (parameters.initial_text.empty()) - return u"yuzu"; + out(u"yuzu"); - return parameters.initial_text; + out(parameters.initial_text); } void DefaultSoftwareKeyboardApplet::SendTextCheckDialog(std::u16string error_message) const { diff --git a/src/core/frontend/applets/software_keyboard.h b/src/core/frontend/applets/software_keyboard.h index 7387ee8d70..5420ea8833 100644 --- a/src/core/frontend/applets/software_keyboard.h +++ b/src/core/frontend/applets/software_keyboard.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include "common/bit_field.h" @@ -36,13 +37,15 @@ class SoftwareKeyboardApplet { public: virtual ~SoftwareKeyboardApplet(); - virtual std::optional GetText(SoftwareKeyboardParameters parameters) const = 0; + virtual void RequestText(std::function)> out, + SoftwareKeyboardParameters parameters) const = 0; virtual void SendTextCheckDialog(std::u16string error_message) const = 0; }; class DefaultSoftwareKeyboardApplet final : public SoftwareKeyboardApplet { public: - std::optional GetText(SoftwareKeyboardParameters parameters) const override; + void RequestText(std::function)> out, + SoftwareKeyboardParameters parameters) const override; void SendTextCheckDialog(std::u16string error_message) const override; }; diff --git a/src/core/hle/service/am/am.cpp b/src/core/hle/service/am/am.cpp index 3f8d97d31b..d040d4776c 100644 --- a/src/core/hle/service/am/am.cpp +++ b/src/core/hle/service/am/am.cpp @@ -718,7 +718,7 @@ void IStorageAccessor::Write(Kernel::HLERequestContext& ctx) { const u64 offset{rp.Pop()}; const std::vector data{ctx.ReadBuffer()}; - const auto size = std::min(data.size(), backing.buffer.size() - offset); + const auto size = std::min(data.size(), backing.buffer.size() - offset); std::memcpy(&backing.buffer[offset], data.data(), size); diff --git a/src/core/hle/service/am/applets/software_keyboard.cpp b/src/core/hle/service/am/applets/software_keyboard.cpp index 7352f3bdf6..66b34d5ac8 100644 --- a/src/core/hle/service/am/applets/software_keyboard.cpp +++ b/src/core/hle/service/am/applets/software_keyboard.cpp @@ -43,6 +43,10 @@ SoftwareKeyboard::SoftwareKeyboard() = default; SoftwareKeyboard::~SoftwareKeyboard() = default; void SoftwareKeyboard::Initialize(std::vector> storage_) { + complete = false; + initial_text.clear(); + final_data.clear(); + Applet::Initialize(std::move(storage_)); ASSERT(storage_stack.size() >= 2); @@ -96,20 +100,25 @@ void SoftwareKeyboard::Execute(AppletStorageProxyFunction out_data, const auto parameters = ConvertToFrontendParameters(config, initial_text); - const auto res = frontend.GetText(parameters); + this->out_data = out_data; + this->out_interactive_data = out_interactive_data; + frontend.RequestText([this](std::optional text) { WriteText(text); }, + parameters); +} +void SoftwareKeyboard::WriteText(std::optional text) { std::vector output(SWKBD_OUTPUT_BUFFER_SIZE); - if (res.has_value()) { + if (text.has_value()) { if (config.text_check) { - const auto size = static_cast(res->size() * 2 + 4); + const auto size = static_cast(text->size() * 2 + 4); std::memcpy(output.data(), &size, sizeof(u32)); } else { output[0] = 1; } - std::memcpy(output.data() + 4, res->data(), - std::min(res->size() * 2, SWKBD_OUTPUT_BUFFER_SIZE - 4)); + std::memcpy(output.data() + 4, text->data(), + std::min(text->size() * 2, SWKBD_OUTPUT_BUFFER_SIZE - 4)); } else { complete = true; out_data(IStorage{output}); diff --git a/src/core/hle/service/am/applets/software_keyboard.h b/src/core/hle/service/am/applets/software_keyboard.h index 66de4bc592..b08bff3d74 100644 --- a/src/core/hle/service/am/applets/software_keyboard.h +++ b/src/core/hle/service/am/applets/software_keyboard.h @@ -57,11 +57,16 @@ public: void Execute(AppletStorageProxyFunction out_data, AppletStorageProxyFunction out_interactive_data) override; + void WriteText(std::optional text); + private: KeyboardConfig config; std::u16string initial_text; bool complete = false; std::vector final_data; + + AppletStorageProxyFunction out_data; + AppletStorageProxyFunction out_interactive_data; }; } // namespace Service::AM::Applets diff --git a/src/yuzu/applets/software_keyboard.cpp b/src/yuzu/applets/software_keyboard.cpp index 92992ef872..9fb179f5c0 100644 --- a/src/yuzu/applets/software_keyboard.cpp +++ b/src/yuzu/applets/software_keyboard.cpp @@ -105,20 +105,27 @@ bool QtSoftwareKeyboardDialog::GetStatus() const { return ok; } -QtSoftwareKeyboard::QtSoftwareKeyboard(GMainWindow& parent) : main_window(parent) {} +QtSoftwareKeyboard::QtSoftwareKeyboard(GMainWindow& main_window) { + connect(this, &QtSoftwareKeyboard::MainWindowGetText, &main_window, + &GMainWindow::SoftwareKeyboardGetText, Qt::QueuedConnection); + connect(this, &QtSoftwareKeyboard::MainWindowTextCheckDialog, &main_window, + &GMainWindow::SoftwareKeyboardInvokeCheckDialog, Qt::BlockingQueuedConnection); + connect(&main_window, &GMainWindow::SoftwareKeyboardFinishedText, this, + &QtSoftwareKeyboard::MainWindowFinishedText, Qt::QueuedConnection); +} QtSoftwareKeyboard::~QtSoftwareKeyboard() = default; -std::optional QtSoftwareKeyboard::GetText( - Core::Frontend::SoftwareKeyboardParameters parameters) const { - std::optional success; - QMetaObject::invokeMethod(&main_window, "SoftwareKeyboardGetText", Qt::BlockingQueuedConnection, - Q_RETURN_ARG(std::optional, success), - Q_ARG(Core::Frontend::SoftwareKeyboardParameters, parameters)); - return success; +void QtSoftwareKeyboard::RequestText(std::function)> out, + Core::Frontend::SoftwareKeyboardParameters parameters) const { + text_output = out; + emit MainWindowGetText(parameters); } void QtSoftwareKeyboard::SendTextCheckDialog(std::u16string error_message) const { - QMetaObject::invokeMethod(&main_window, "SoftwareKeyboardInvokeCheckDialog", - Qt::BlockingQueuedConnection, Q_ARG(std::u16string, error_message)); + emit MainWindowTextCheckDialog(error_message); +} + +void QtSoftwareKeyboard::MainWindowFinishedText(std::optional text) { + text_output(text); } diff --git a/src/yuzu/applets/software_keyboard.h b/src/yuzu/applets/software_keyboard.h index 8d95ca511f..670b05dc98 100644 --- a/src/yuzu/applets/software_keyboard.h +++ b/src/yuzu/applets/software_keyboard.h @@ -54,14 +54,23 @@ private: }; class QtSoftwareKeyboard final : public QObject, public Core::Frontend::SoftwareKeyboardApplet { + Q_OBJECT + public: explicit QtSoftwareKeyboard(GMainWindow& parent); ~QtSoftwareKeyboard() override; - std::optional GetText( - Core::Frontend::SoftwareKeyboardParameters parameters) const override; + void RequestText(std::function)> out, + Core::Frontend::SoftwareKeyboardParameters parameters) const override; void SendTextCheckDialog(std::u16string error_message) const override; +signals: + void MainWindowGetText(Core::Frontend::SoftwareKeyboardParameters parameters) const; + void MainWindowTextCheckDialog(std::u16string error_message) const; + +public slots: + void MainWindowFinishedText(std::optional text); + private: - GMainWindow& main_window; + mutable std::function)> text_output; }; diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index a11eb7f865..28c53cc876 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -207,7 +207,7 @@ GMainWindow::~GMainWindow() { delete render_window; } -std::optional GMainWindow::SoftwareKeyboardGetText( +void GMainWindow::SoftwareKeyboardGetText( const Core::Frontend::SoftwareKeyboardParameters& parameters) { QtSoftwareKeyboardDialog dialog(this, parameters); dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | @@ -216,9 +216,9 @@ std::optional GMainWindow::SoftwareKeyboardGetText( dialog.exec(); if (!dialog.GetStatus()) - return std::nullopt; + emit SoftwareKeyboardFinishedText(std::nullopt); - return dialog.GetText(); + emit SoftwareKeyboardFinishedText(dialog.GetText()); } void GMainWindow::SoftwareKeyboardInvokeCheckDialog(std::u16string error_message) { diff --git a/src/yuzu/main.h b/src/yuzu/main.h index 8fbe998eaf..d83169805e 100644 --- a/src/yuzu/main.h +++ b/src/yuzu/main.h @@ -99,9 +99,10 @@ signals: // Signal that tells widgets to update icons to use the current theme void UpdateThemedIcons(); + void SoftwareKeyboardFinishedText(std::optional text); + public slots: - std::optional SoftwareKeyboardGetText( - const Core::Frontend::SoftwareKeyboardParameters& parameters); + void SoftwareKeyboardGetText(const Core::Frontend::SoftwareKeyboardParameters& parameters); void SoftwareKeyboardInvokeCheckDialog(std::u16string error_message); private: