Merge pull request #4700 from zhaowenlan1779/swkbd-2

applets/swkbd: Add callback support
This commit is contained in:
Pengfei Zhu 2019-04-20 22:18:15 +08:00 committed by GitHub
commit 536e4de499
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 156 additions and 94 deletions

View file

@ -14,20 +14,6 @@
#include "core/file_sys/file_backend.h" #include "core/file_sys/file_backend.h"
#include "core/hle/service/ptm/ptm.h" #include "core/hle/service/ptm/ptm.h"
/**
* Converts a UTF-16 text in a container to a UTF-8 std::string.
*/
template <typename T>
std::string TextFromBuffer(const T& text) {
const auto text_end = std::find(text.begin(), text.end(), u'\0');
const std::size_t text_size = std::distance(text.begin(), text_end);
std::u16string buffer(text_size, 0);
std::transform(text.begin(), text_end, buffer.begin(), [](u16_le character) {
return static_cast<char16_t>(static_cast<u16>(character));
});
return Common::UTF16ToUTF8(buffer);
}
QtMiiSelectorDialog::QtMiiSelectorDialog(QWidget* parent, QtMiiSelector* mii_selector_) QtMiiSelectorDialog::QtMiiSelectorDialog(QWidget* parent, QtMiiSelector* mii_selector_)
: QDialog(parent), mii_selector(mii_selector_) { : QDialog(parent), mii_selector(mii_selector_) {
using namespace Frontend; using namespace Frontend;
@ -71,7 +57,7 @@ QtMiiSelectorDialog::QtMiiSelectorDialog(QWidget* parent, QtMiiSelector* mii_sel
file->Read(saved_miis_offset, sizeof(mii), mii_raw.data()); file->Read(saved_miis_offset, sizeof(mii), mii_raw.data());
std::memcpy(&mii, mii_raw.data(), sizeof(mii)); std::memcpy(&mii, mii_raw.data(), sizeof(mii));
if (mii.mii_id != 0) { if (mii.mii_id != 0) {
std::string name = TextFromBuffer(mii.mii_name); std::string name = Common::UTF16BufferToUTF8(mii.mii_name);
miis.push_back(mii); miis.push_back(mii);
combobox->addItem(QString::fromStdString(name)); combobox->addItem(QString::fromStdString(name));
} }

View file

@ -109,14 +109,20 @@ void QtKeyboardDialog::HandleValidationError(Frontend::ValidationError error) {
QtKeyboard::QtKeyboard(QWidget& parent_) : parent(parent_) {} QtKeyboard::QtKeyboard(QWidget& parent_) : parent(parent_) {}
void QtKeyboard::Setup(const Frontend::KeyboardConfig& config) { void QtKeyboard::Execute(const Frontend::KeyboardConfig& config) {
SoftwareKeyboard::Setup(config); SoftwareKeyboard::Execute(config);
if (this->config.button_config != Frontend::ButtonConfig::None) { if (this->config.button_config != Frontend::ButtonConfig::None) {
ok_id = static_cast<u8>(this->config.button_config); ok_id = static_cast<u8>(this->config.button_config);
} }
QMetaObject::invokeMethod(this, "OpenInputDialog", Qt::BlockingQueuedConnection); QMetaObject::invokeMethod(this, "OpenInputDialog", Qt::BlockingQueuedConnection);
} }
void QtKeyboard::ShowError(const std::string& error) {
QString message = QString::fromStdString(error);
QMetaObject::invokeMethod(this, "ShowErrorDialog", Qt::BlockingQueuedConnection,
Q_ARG(QString, message));
}
void QtKeyboard::OpenInputDialog() { void QtKeyboard::OpenInputDialog() {
QtKeyboardDialog dialog(&parent, this); QtKeyboardDialog dialog(&parent, this);
dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint |
@ -127,3 +133,7 @@ void QtKeyboard::OpenInputDialog() {
dialog.button); dialog.button);
Finalize(dialog.text.toStdString(), dialog.button); Finalize(dialog.text.toStdString(), dialog.button);
} }
void QtKeyboard::ShowErrorDialog(QString message) {
QMessageBox::critical(&parent, tr("Software Keyboard"), message);
}

View file

@ -48,10 +48,12 @@ class QtKeyboard final : public QObject, public Frontend::SoftwareKeyboard {
public: public:
explicit QtKeyboard(QWidget& parent); explicit QtKeyboard(QWidget& parent);
void Setup(const Frontend::KeyboardConfig& config) override; void Execute(const Frontend::KeyboardConfig& config) override;
void ShowError(const std::string& error) override;
private: private:
Q_INVOKABLE void OpenInputDialog(); Q_INVOKABLE void OpenInputDialog();
Q_INVOKABLE void ShowErrorDialog(QString message);
/// Index of the buttons /// Index of the buttons
u8 ok_id; u8 ok_id;

View file

@ -4,10 +4,12 @@
#pragma once #pragma once
#include <algorithm>
#include <cstddef> #include <cstddef>
#include <string> #include <string>
#include <vector> #include <vector>
#include "common/common_types.h" #include "common/common_types.h"
#include "common/swap.h"
namespace Common { namespace Common {
@ -58,6 +60,20 @@ bool ComparePartialString(InIt begin, InIt end, const char* other) {
return (begin == end) == (*other == '\0'); return (begin == end) == (*other == '\0');
} }
/**
* Converts a UTF-16 text in a container to a UTF-8 std::string.
*/
template <typename T>
std::string UTF16BufferToUTF8(const T& text) {
const auto text_end = std::find(text.begin(), text.end(), u'\0');
const std::size_t text_size = std::distance(text.begin(), text_end);
std::u16string buffer(text_size, 0);
std::transform(text.begin(), text_end, buffer.begin(), [](u16_le character) {
return static_cast<char16_t>(static_cast<u16>(character));
});
return UTF16ToUTF8(buffer);
}
/** /**
* Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't * Creates a std::string from a fixed-size NUL-terminated char buffer. If the buffer isn't
* NUL-terminated then the string ends at max_len characters. * NUL-terminated then the string ends at max_len characters.

View file

@ -39,10 +39,6 @@ ValidationError SoftwareKeyboard::ValidateFilters(const std::string& input) cons
// TODO: check the profanity filter // TODO: check the profanity filter
LOG_INFO(Frontend, "App requested swkbd profanity filter, but its not implemented."); LOG_INFO(Frontend, "App requested swkbd profanity filter, but its not implemented.");
} }
if (config.filters.enable_callback) {
// TODO: check the callback
LOG_INFO(Frontend, "App requested a swkbd callback, but its not implemented.");
}
return ValidationError::None; return ValidationError::None;
} }
@ -132,11 +128,21 @@ ValidationError SoftwareKeyboard::Finalize(const std::string& text, u8 button) {
return error; return error;
} }
data = {text, button}; data = {text, button};
data_ready = true;
return ValidationError::None; return ValidationError::None;
} }
void DefaultKeyboard::Setup(const Frontend::KeyboardConfig& config) { bool SoftwareKeyboard::DataReady() const {
SoftwareKeyboard::Setup(config); return data_ready;
}
const KeyboardData& SoftwareKeyboard::ReceiveData() {
data_ready = false;
return data;
}
void DefaultKeyboard::Execute(const Frontend::KeyboardConfig& config) {
SoftwareKeyboard::Execute(config);
auto cfg = Service::CFG::GetModule(Core::System::GetInstance()); auto cfg = Service::CFG::GetModule(Core::System::GetInstance());
ASSERT_MSG(cfg, "CFG Module missing!"); ASSERT_MSG(cfg, "CFG Module missing!");
@ -157,4 +163,8 @@ void DefaultKeyboard::Setup(const Frontend::KeyboardConfig& config) {
} }
} }
void DefaultKeyboard::ShowError(const std::string& error) {
LOG_ERROR(Applet_SWKBD, "Default keyboard text is unaccepted! error: {}", error);
}
} // namespace Frontend } // namespace Frontend

View file

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <atomic>
#include <unordered_map> #include <unordered_map>
#include <utility> #include <utility>
#include <vector> #include <vector>
@ -82,13 +83,27 @@ enum class ValidationError {
class SoftwareKeyboard { class SoftwareKeyboard {
public: public:
virtual void Setup(const KeyboardConfig& config) { /**
this->config = KeyboardConfig(config); * Executes the software keyboard, configured with the given parameters.
*/
virtual void Execute(const KeyboardConfig& config) {
this->config = config;
} }
const KeyboardData& ReceiveData() const { /**
return data; * Whether the result data is ready to be received.
} */
bool DataReady() const;
/**
* Receives the current result data stored in the applet, and clears the ready state.
*/
const KeyboardData& ReceiveData();
/**
* Shows an error text returned by the callback.
*/
virtual void ShowError(const std::string& error) = 0;
/** /**
* Validates if the provided string breaks any of the filter rules. This is meant to be called * Validates if the provided string breaks any of the filter rules. This is meant to be called
@ -118,11 +133,14 @@ public:
protected: protected:
KeyboardConfig config; KeyboardConfig config;
KeyboardData data; KeyboardData data;
std::atomic_bool data_ready = false;
}; };
class DefaultKeyboard final : public SoftwareKeyboard { class DefaultKeyboard final : public SoftwareKeyboard {
public: public:
void Setup(const KeyboardConfig& config) override; void Execute(const KeyboardConfig& config) override;
void ShowError(const std::string& error) override;
}; };
} // namespace Frontend } // namespace Frontend

View file

@ -19,20 +19,6 @@
namespace HLE::Applets { namespace HLE::Applets {
/**
* Converts a UTF-16 text in a container to a UTF-8 std::string.
*/
template <typename T>
std::string TextFromBuffer(const T& text) {
const auto text_end = std::find(text.begin(), text.end(), u'\0');
const std::size_t text_size = std::distance(text.begin(), text_end);
std::u16string buffer(text_size, 0);
std::transform(text.begin(), text_end, buffer.begin(), [](u16_le character) {
return static_cast<char16_t>(static_cast<u16>(character));
});
return Common::UTF16ToUTF8(buffer);
}
ResultCode MiiSelector::ReceiveParameter(const Service::APT::MessageParameter& parameter) { ResultCode MiiSelector::ReceiveParameter(const Service::APT::MessageParameter& parameter) {
if (parameter.signal != Service::APT::SignalType::Request) { if (parameter.signal != Service::APT::SignalType::Request) {
LOG_ERROR(Service_APT, "unsupported signal {}", static_cast<u32>(parameter.signal)); LOG_ERROR(Service_APT, "unsupported signal {}", static_cast<u32>(parameter.signal));
@ -156,7 +142,7 @@ MiiResult MiiSelector::GetStandardMiiResult() {
Frontend::MiiSelectorConfig MiiSelector::ToFrontendConfig(const MiiConfig& config) const { Frontend::MiiSelectorConfig MiiSelector::ToFrontendConfig(const MiiConfig& config) const {
Frontend::MiiSelectorConfig frontend_config; Frontend::MiiSelectorConfig frontend_config;
frontend_config.enable_cancel_button = config.enable_cancel_button == 1; frontend_config.enable_cancel_button = config.enable_cancel_button == 1;
frontend_config.title = TextFromBuffer(config.title); frontend_config.title = Common::UTF16BufferToUTF8(config.title);
frontend_config.initially_selected_mii_index = config.initially_selected_mii_index; frontend_config.initially_selected_mii_index = config.initially_selected_mii_index;
return frontend_config; return frontend_config;
} }

View file

@ -22,20 +22,15 @@
namespace HLE::Applets { namespace HLE::Applets {
ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter const& parameter) { ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter const& parameter) {
if (parameter.signal != Service::APT::SignalType::Request) { switch (parameter.signal) {
LOG_ERROR(Service_APT, "unsupported signal {}", static_cast<u32>(parameter.signal)); case Service::APT::SignalType::Request: {
UNIMPLEMENTED();
// TODO(Subv): Find the right error code
return ResultCode(-1);
}
// The LibAppJustStarted message contains a buffer with the size of the framebuffer shared // The LibAppJustStarted message contains a buffer with the size of the framebuffer shared
// memory. // memory.
// Create the SharedMemory that will hold the framebuffer data // Create the SharedMemory that will hold the framebuffer data
Service::APT::CaptureBufferInfo capture_info; Service::APT::CaptureBufferInfo capture_info;
ASSERT(sizeof(capture_info) == parameter.buffer.size()); ASSERT(sizeof(capture_info) == parameter.buffer.size());
memcpy(&capture_info, parameter.buffer.data(), sizeof(capture_info)); std::memcpy(&capture_info, parameter.buffer.data(), sizeof(capture_info));
using Kernel::MemoryPermission; using Kernel::MemoryPermission;
// Create a SharedMemory that directly points to this heap block. // Create a SharedMemory that directly points to this heap block.
@ -55,6 +50,48 @@ ResultCode SoftwareKeyboard::ReceiveParameter(Service::APT::MessageParameter con
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
case Service::APT::SignalType::Message: {
// Callback result
ASSERT_MSG(parameter.buffer.size() == sizeof(config),
"The size of the parameter (SoftwareKeyboardConfig) is wrong");
std::memcpy(&config, parameter.buffer.data(), parameter.buffer.size());
switch (config.callback_result) {
case SoftwareKeyboardCallbackResult::OK:
// Finish execution
Finalize();
return RESULT_SUCCESS;
case SoftwareKeyboardCallbackResult::Close:
// Let the frontend display error and quit
frontend_applet->ShowError(Common::UTF16BufferToUTF8(config.callback_msg));
config.return_code = SoftwareKeyboardResult::BannedInput;
config.text_offset = config.text_length = 0;
Finalize();
return RESULT_SUCCESS;
case SoftwareKeyboardCallbackResult::Continue:
// Let the frontend display error and get input again
// The input will be sent for validation again on next Update().
frontend_applet->ShowError(Common::UTF16BufferToUTF8(config.callback_msg));
frontend_applet->Execute(ToFrontendConfig(config));
return RESULT_SUCCESS;
default:
UNREACHABLE();
}
}
default: {
LOG_ERROR(Service_APT, "unsupported signal {}", static_cast<u32>(parameter.signal));
UNIMPLEMENTED();
// TODO(Subv): Find the right error code
return ResultCode(-1);
}
}
}
ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter const& parameter) { ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter const& parameter) {
ASSERT_MSG(parameter.buffer.size() == sizeof(config), ASSERT_MSG(parameter.buffer.size() == sizeof(config),
"The size of the parameter (SoftwareKeyboardConfig) is wrong"); "The size of the parameter (SoftwareKeyboardConfig) is wrong");
@ -71,16 +108,18 @@ ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter cons
frontend_applet = Core::System::GetInstance().GetSoftwareKeyboard(); frontend_applet = Core::System::GetInstance().GetSoftwareKeyboard();
ASSERT(frontend_applet); ASSERT(frontend_applet);
KeyboardConfig frontend_config = ToFrontendConfig(config); frontend_applet->Execute(ToFrontendConfig(config));
frontend_applet->Setup(frontend_config);
is_running = true; is_running = true;
return RESULT_SUCCESS; return RESULT_SUCCESS;
} }
void SoftwareKeyboard::Update() { void SoftwareKeyboard::Update() {
if (!frontend_applet->DataReady())
return;
using namespace Frontend; using namespace Frontend;
KeyboardData data(frontend_applet->ReceiveData()); const KeyboardData& data = frontend_applet->ReceiveData();
std::u16string text = Common::UTF8ToUTF16(data.text); std::u16string text = Common::UTF8ToUTF16(data.text);
memcpy(text_memory->GetPointer(), text.c_str(), text.length() * sizeof(char16_t)); memcpy(text_memory->GetPointer(), text.c_str(), text.length() * sizeof(char16_t));
switch (config.num_buttons_m1) { switch (config.num_buttons_m1) {
@ -114,10 +153,19 @@ void SoftwareKeyboard::Update() {
config.text_length = static_cast<u16>(text.size()); config.text_length = static_cast<u16>(text.size());
config.text_offset = 0; config.text_offset = 0;
// TODO(Subv): We're finalizing the applet immediately after it's started, if (config.filter_flags & HLE::Applets::SoftwareKeyboardFilter::Callback) {
// but we should defer this call until after all the input has been collected. // Send the message to invoke callback
Service::APT::MessageParameter message;
message.buffer.resize(sizeof(SoftwareKeyboardConfig));
std::memcpy(message.buffer.data(), &config, message.buffer.size());
message.signal = Service::APT::SignalType::Message;
message.destination_id = Service::APT::AppletId::Application;
message.sender_id = id;
SendParameter(message);
} else {
Finalize(); Finalize();
} }
}
void SoftwareKeyboard::DrawScreenKeyboard() { void SoftwareKeyboard::DrawScreenKeyboard() {
// TODO(Subv): Draw the HLE keyboard, for now just do nothing // TODO(Subv): Draw the HLE keyboard, for now just do nothing
@ -147,14 +195,7 @@ Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig(
frontend_config.multiline_mode = config.multiline; frontend_config.multiline_mode = config.multiline;
frontend_config.max_text_length = config.max_text_length; frontend_config.max_text_length = config.max_text_length;
frontend_config.max_digits = config.max_digits; frontend_config.max_digits = config.max_digits;
frontend_config.hint_text = Common::UTF16BufferToUTF8(config.hint_text);
std::size_t text_size = config.hint_text.size();
const auto text_end = std::find(config.hint_text.begin(), config.hint_text.end(), u'\0');
if (text_end != config.hint_text.end())
text_size = std::distance(config.hint_text.begin(), text_end);
std::u16string buffer(text_size, 0);
std::memcpy(buffer.data(), config.hint_text.data(), text_size * sizeof(u16));
frontend_config.hint_text = Common::UTF16ToUTF8(buffer);
frontend_config.has_custom_button_text = frontend_config.has_custom_button_text =
!std::all_of(config.button_text.begin(), config.button_text.end(), !std::all_of(config.button_text.begin(), config.button_text.end(),
[](std::array<u16, HLE::Applets::MAX_BUTTON_TEXT_LEN + 1> x) { [](std::array<u16, HLE::Applets::MAX_BUTTON_TEXT_LEN + 1> x) {
@ -162,14 +203,7 @@ Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig(
}); });
if (frontend_config.has_custom_button_text) { if (frontend_config.has_custom_button_text) {
for (const auto& text : config.button_text) { for (const auto& text : config.button_text) {
text_size = text.size(); frontend_config.button_text.push_back(Common::UTF16BufferToUTF8(text));
const auto text_end = std::find(text.begin(), text.end(), u'\0');
if (text_end != text.end())
text_size = std::distance(text.begin(), text_end);
buffer.resize(text_size);
std::memcpy(buffer.data(), text.data(), text_size * sizeof(u16));
frontend_config.button_text.push_back(Common::UTF16ToUTF8(buffer));
} }
} }
frontend_config.filters.prevent_digit = frontend_config.filters.prevent_digit =

View file

@ -161,7 +161,7 @@ struct SoftwareKeyboardConfig {
u32_le text_offset; ///< Offset in the SharedMemory where the output text starts u32_le text_offset; ///< Offset in the SharedMemory where the output text starts
u16_le text_length; ///< Length in characters of the output text u16_le text_length; ///< Length in characters of the output text
s32_le callback_result; enum_le<SoftwareKeyboardCallbackResult> callback_result;
std::array<u16_le, MAX_CALLBACK_MSG_LEN + 1> callback_msg; std::array<u16_le, MAX_CALLBACK_MSG_LEN + 1> callback_msg;
bool skip_at_check; bool skip_at_check;
INSERT_PADDING_BYTES(0xAB); INSERT_PADDING_BYTES(0xAB);