software_keyboard: Make GetText asynchronous

a
This commit is contained in:
Zach Hilman 2018-11-12 11:08:09 -05:00
parent 7cfb29de23
commit 8b433beff3
9 changed files with 64 additions and 29 deletions

View File

@ -9,12 +9,13 @@
namespace Core::Frontend { namespace Core::Frontend {
SoftwareKeyboardApplet::~SoftwareKeyboardApplet() = default; SoftwareKeyboardApplet::~SoftwareKeyboardApplet() = default;
std::optional<std::u16string> DefaultSoftwareKeyboardApplet::GetText( void DefaultSoftwareKeyboardApplet::RequestText(
std::function<void(std::optional<std::u16string>)> out,
SoftwareKeyboardParameters parameters) const { SoftwareKeyboardParameters parameters) const {
if (parameters.initial_text.empty()) 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 { void DefaultSoftwareKeyboardApplet::SendTextCheckDialog(std::u16string error_message) const {

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <functional>
#include <optional> #include <optional>
#include <string> #include <string>
#include "common/bit_field.h" #include "common/bit_field.h"
@ -36,13 +37,15 @@ class SoftwareKeyboardApplet {
public: public:
virtual ~SoftwareKeyboardApplet(); virtual ~SoftwareKeyboardApplet();
virtual std::optional<std::u16string> GetText(SoftwareKeyboardParameters parameters) const = 0; virtual void RequestText(std::function<void(std::optional<std::u16string>)> out,
SoftwareKeyboardParameters parameters) const = 0;
virtual void SendTextCheckDialog(std::u16string error_message) const = 0; virtual void SendTextCheckDialog(std::u16string error_message) const = 0;
}; };
class DefaultSoftwareKeyboardApplet final : public SoftwareKeyboardApplet { class DefaultSoftwareKeyboardApplet final : public SoftwareKeyboardApplet {
public: public:
std::optional<std::u16string> GetText(SoftwareKeyboardParameters parameters) const override; void RequestText(std::function<void(std::optional<std::u16string>)> out,
SoftwareKeyboardParameters parameters) const override;
void SendTextCheckDialog(std::u16string error_message) const override; void SendTextCheckDialog(std::u16string error_message) const override;
}; };

View File

@ -718,7 +718,7 @@ void IStorageAccessor::Write(Kernel::HLERequestContext& ctx) {
const u64 offset{rp.Pop<u64>()}; const u64 offset{rp.Pop<u64>()};
const std::vector<u8> data{ctx.ReadBuffer()}; const std::vector<u8> data{ctx.ReadBuffer()};
const auto size = std::min(data.size(), backing.buffer.size() - offset); const auto size = std::min<std::size_t>(data.size(), backing.buffer.size() - offset);
std::memcpy(&backing.buffer[offset], data.data(), size); std::memcpy(&backing.buffer[offset], data.data(), size);

View File

@ -43,6 +43,10 @@ SoftwareKeyboard::SoftwareKeyboard() = default;
SoftwareKeyboard::~SoftwareKeyboard() = default; SoftwareKeyboard::~SoftwareKeyboard() = default;
void SoftwareKeyboard::Initialize(std::vector<std::shared_ptr<IStorage>> storage_) { void SoftwareKeyboard::Initialize(std::vector<std::shared_ptr<IStorage>> storage_) {
complete = false;
initial_text.clear();
final_data.clear();
Applet::Initialize(std::move(storage_)); Applet::Initialize(std::move(storage_));
ASSERT(storage_stack.size() >= 2); ASSERT(storage_stack.size() >= 2);
@ -96,20 +100,25 @@ void SoftwareKeyboard::Execute(AppletStorageProxyFunction out_data,
const auto parameters = ConvertToFrontendParameters(config, initial_text); 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<std::u16string> text) { WriteText(text); },
parameters);
}
void SoftwareKeyboard::WriteText(std::optional<std::u16string> text) {
std::vector<u8> output(SWKBD_OUTPUT_BUFFER_SIZE); std::vector<u8> output(SWKBD_OUTPUT_BUFFER_SIZE);
if (res.has_value()) { if (text.has_value()) {
if (config.text_check) { if (config.text_check) {
const auto size = static_cast<u32>(res->size() * 2 + 4); const auto size = static_cast<u32>(text->size() * 2 + 4);
std::memcpy(output.data(), &size, sizeof(u32)); std::memcpy(output.data(), &size, sizeof(u32));
} else { } else {
output[0] = 1; output[0] = 1;
} }
std::memcpy(output.data() + 4, res->data(), std::memcpy(output.data() + 4, text->data(),
std::min(res->size() * 2, SWKBD_OUTPUT_BUFFER_SIZE - 4)); std::min(text->size() * 2, SWKBD_OUTPUT_BUFFER_SIZE - 4));
} else { } else {
complete = true; complete = true;
out_data(IStorage{output}); out_data(IStorage{output});

View File

@ -57,11 +57,16 @@ public:
void Execute(AppletStorageProxyFunction out_data, void Execute(AppletStorageProxyFunction out_data,
AppletStorageProxyFunction out_interactive_data) override; AppletStorageProxyFunction out_interactive_data) override;
void WriteText(std::optional<std::u16string> text);
private: private:
KeyboardConfig config; KeyboardConfig config;
std::u16string initial_text; std::u16string initial_text;
bool complete = false; bool complete = false;
std::vector<u8> final_data; std::vector<u8> final_data;
AppletStorageProxyFunction out_data;
AppletStorageProxyFunction out_interactive_data;
}; };
} // namespace Service::AM::Applets } // namespace Service::AM::Applets

View File

@ -105,20 +105,27 @@ bool QtSoftwareKeyboardDialog::GetStatus() const {
return ok; 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; QtSoftwareKeyboard::~QtSoftwareKeyboard() = default;
std::optional<std::u16string> QtSoftwareKeyboard::GetText( void QtSoftwareKeyboard::RequestText(std::function<void(std::optional<std::u16string>)> out,
Core::Frontend::SoftwareKeyboardParameters parameters) const { Core::Frontend::SoftwareKeyboardParameters parameters) const {
std::optional<std::u16string> success; text_output = out;
QMetaObject::invokeMethod(&main_window, "SoftwareKeyboardGetText", Qt::BlockingQueuedConnection, emit MainWindowGetText(parameters);
Q_RETURN_ARG(std::optional<std::u16string>, success),
Q_ARG(Core::Frontend::SoftwareKeyboardParameters, parameters));
return success;
} }
void QtSoftwareKeyboard::SendTextCheckDialog(std::u16string error_message) const { void QtSoftwareKeyboard::SendTextCheckDialog(std::u16string error_message) const {
QMetaObject::invokeMethod(&main_window, "SoftwareKeyboardInvokeCheckDialog", emit MainWindowTextCheckDialog(error_message);
Qt::BlockingQueuedConnection, Q_ARG(std::u16string, error_message)); }
void QtSoftwareKeyboard::MainWindowFinishedText(std::optional<std::u16string> text) {
text_output(text);
} }

View File

@ -54,14 +54,23 @@ private:
}; };
class QtSoftwareKeyboard final : public QObject, public Core::Frontend::SoftwareKeyboardApplet { class QtSoftwareKeyboard final : public QObject, public Core::Frontend::SoftwareKeyboardApplet {
Q_OBJECT
public: public:
explicit QtSoftwareKeyboard(GMainWindow& parent); explicit QtSoftwareKeyboard(GMainWindow& parent);
~QtSoftwareKeyboard() override; ~QtSoftwareKeyboard() override;
std::optional<std::u16string> GetText( void RequestText(std::function<void(std::optional<std::u16string>)> out,
Core::Frontend::SoftwareKeyboardParameters parameters) const override; Core::Frontend::SoftwareKeyboardParameters parameters) const override;
void SendTextCheckDialog(std::u16string error_message) 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<std::u16string> text);
private: private:
GMainWindow& main_window; mutable std::function<void(std::optional<std::u16string>)> text_output;
}; };

View File

@ -207,7 +207,7 @@ GMainWindow::~GMainWindow() {
delete render_window; delete render_window;
} }
std::optional<std::u16string> GMainWindow::SoftwareKeyboardGetText( void GMainWindow::SoftwareKeyboardGetText(
const Core::Frontend::SoftwareKeyboardParameters& parameters) { const Core::Frontend::SoftwareKeyboardParameters& parameters) {
QtSoftwareKeyboardDialog dialog(this, parameters); QtSoftwareKeyboardDialog dialog(this, parameters);
dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint |
@ -216,9 +216,9 @@ std::optional<std::u16string> GMainWindow::SoftwareKeyboardGetText(
dialog.exec(); dialog.exec();
if (!dialog.GetStatus()) if (!dialog.GetStatus())
return std::nullopt; emit SoftwareKeyboardFinishedText(std::nullopt);
return dialog.GetText(); emit SoftwareKeyboardFinishedText(dialog.GetText());
} }
void GMainWindow::SoftwareKeyboardInvokeCheckDialog(std::u16string error_message) { void GMainWindow::SoftwareKeyboardInvokeCheckDialog(std::u16string error_message) {

View File

@ -99,9 +99,10 @@ signals:
// Signal that tells widgets to update icons to use the current theme // Signal that tells widgets to update icons to use the current theme
void UpdateThemedIcons(); void UpdateThemedIcons();
void SoftwareKeyboardFinishedText(std::optional<std::u16string> text);
public slots: public slots:
std::optional<std::u16string> SoftwareKeyboardGetText( void SoftwareKeyboardGetText(const Core::Frontend::SoftwareKeyboardParameters& parameters);
const Core::Frontend::SoftwareKeyboardParameters& parameters);
void SoftwareKeyboardInvokeCheckDialog(std::u16string error_message); void SoftwareKeyboardInvokeCheckDialog(std::u16string error_message);
private: private: