Merge pull request #3850 from zhaowenlan1779/swkbd
applets/swkbd: Software Keyboard Implementationmaster
commit
bf6da61da5
@ -0,0 +1,129 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <QDialogButtonBox>
|
||||
#include <QLabel>
|
||||
#include <QLineEdit>
|
||||
#include <QMessageBox>
|
||||
#include <QString>
|
||||
#include <QVBoxLayout>
|
||||
#include "citra_qt/applets/swkbd.h"
|
||||
|
||||
QtKeyboardValidator::QtKeyboardValidator(QtKeyboard* keyboard_) : keyboard(keyboard_) {}
|
||||
|
||||
QtKeyboardValidator::State QtKeyboardValidator::validate(QString& input, int& pos) const {
|
||||
if (keyboard->ValidateFilters(input.toStdString()) == Frontend::ValidationError::None) {
|
||||
if (input.size() > keyboard->config.max_text_length)
|
||||
return State::Invalid;
|
||||
return State::Acceptable;
|
||||
} else {
|
||||
return State::Invalid;
|
||||
}
|
||||
}
|
||||
|
||||
QtKeyboardDialog::QtKeyboardDialog(QWidget* parent, QtKeyboard* keyboard_)
|
||||
: QDialog(parent), keyboard(keyboard_) {
|
||||
using namespace Frontend;
|
||||
KeyboardConfig config = keyboard->config;
|
||||
layout = new QVBoxLayout;
|
||||
label = new QLabel(QString::fromStdString(config.hint_text));
|
||||
line_edit = new QLineEdit;
|
||||
line_edit->setValidator(new QtKeyboardValidator(keyboard));
|
||||
buttons = new QDialogButtonBox;
|
||||
// Initialize buttons
|
||||
switch (config.button_config) {
|
||||
case ButtonConfig::Triple:
|
||||
buttons->addButton(config.has_custom_button_text
|
||||
? QString::fromStdString(config.button_text[2])
|
||||
: tr(BUTTON_OKAY),
|
||||
QDialogButtonBox::ButtonRole::AcceptRole);
|
||||
buttons->addButton(config.has_custom_button_text
|
||||
? QString::fromStdString(config.button_text[1])
|
||||
: tr(BUTTON_FORGOT),
|
||||
QDialogButtonBox::ButtonRole::HelpRole);
|
||||
buttons->addButton(config.has_custom_button_text
|
||||
? QString::fromStdString(config.button_text[0])
|
||||
: tr(BUTTON_CANCEL),
|
||||
QDialogButtonBox::ButtonRole::RejectRole);
|
||||
break;
|
||||
case ButtonConfig::Dual:
|
||||
buttons->addButton(config.has_custom_button_text
|
||||
? QString::fromStdString(config.button_text[1])
|
||||
: tr(BUTTON_OKAY),
|
||||
QDialogButtonBox::ButtonRole::AcceptRole);
|
||||
buttons->addButton(config.has_custom_button_text
|
||||
? QString::fromStdString(config.button_text[0])
|
||||
: tr(BUTTON_CANCEL),
|
||||
QDialogButtonBox::ButtonRole::RejectRole);
|
||||
break;
|
||||
case ButtonConfig::Single:
|
||||
buttons->addButton(config.has_custom_button_text
|
||||
? QString::fromStdString(config.button_text[0])
|
||||
: tr(BUTTON_OKAY),
|
||||
QDialogButtonBox::ButtonRole::AcceptRole);
|
||||
break;
|
||||
case ButtonConfig::None:
|
||||
break;
|
||||
}
|
||||
connect(buttons, &QDialogButtonBox::accepted, this, [=] { Submit(); });
|
||||
connect(buttons, &QDialogButtonBox::rejected, this, [=] {
|
||||
button = QtKeyboard::cancel_id;
|
||||
accept();
|
||||
});
|
||||
connect(buttons, &QDialogButtonBox::helpRequested, this, [=] {
|
||||
button = QtKeyboard::forgot_id;
|
||||
accept();
|
||||
});
|
||||
layout->addWidget(label);
|
||||
layout->addWidget(line_edit);
|
||||
layout->addWidget(buttons);
|
||||
setLayout(layout);
|
||||
}
|
||||
|
||||
void QtKeyboardDialog::Submit() {
|
||||
auto error = keyboard->ValidateInput(line_edit->text().toStdString());
|
||||
if (error != Frontend::ValidationError::None) {
|
||||
HandleValidationError(error);
|
||||
} else {
|
||||
button = keyboard->ok_id;
|
||||
text = line_edit->text();
|
||||
accept();
|
||||
}
|
||||
}
|
||||
|
||||
void QtKeyboardDialog::HandleValidationError(Frontend::ValidationError error) {
|
||||
using namespace Frontend;
|
||||
const std::unordered_map<ValidationError, QString> VALIDATION_ERROR_MESSAGES = {
|
||||
{ValidationError::FixedLengthRequired,
|
||||
tr("Text length is not correct (should be %1 characters)")
|
||||
.arg(keyboard->config.max_text_length)},
|
||||
{ValidationError::MaxLengthExceeded,
|
||||
tr("Text is too long (should be no more than %1 characters)")
|
||||
.arg(keyboard->config.max_text_length)},
|
||||
{ValidationError::BlankInputNotAllowed, tr("Blank input is not allowed")},
|
||||
{ValidationError::EmptyInputNotAllowed, tr("Empty input is not allowed")},
|
||||
};
|
||||
QMessageBox::critical(this, tr("Validation error"), VALIDATION_ERROR_MESSAGES.at(error));
|
||||
}
|
||||
|
||||
QtKeyboard::QtKeyboard(QWidget& parent_) : parent(parent_) {}
|
||||
|
||||
void QtKeyboard::Setup(const Frontend::KeyboardConfig* config) {
|
||||
SoftwareKeyboard::Setup(config);
|
||||
if (this->config.button_config != Frontend::ButtonConfig::None) {
|
||||
ok_id = static_cast<u8>(this->config.button_config);
|
||||
}
|
||||
QMetaObject::invokeMethod(this, "OpenInputDialog", Qt::BlockingQueuedConnection);
|
||||
}
|
||||
|
||||
void QtKeyboard::OpenInputDialog() {
|
||||
QtKeyboardDialog dialog(&parent, this);
|
||||
dialog.setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint |
|
||||
Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint);
|
||||
dialog.setWindowModality(Qt::WindowModal);
|
||||
dialog.exec();
|
||||
LOG_INFO(Frontend, "SWKBD input dialog finished, text={}, button={}", dialog.text.toStdString(),
|
||||
dialog.button);
|
||||
Finalize(dialog.text.toStdString(), dialog.button);
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QDialog>
|
||||
#include <QValidator>
|
||||
#include "core/frontend/applets/swkbd.h"
|
||||
|
||||
class QDialogButtonBox;
|
||||
class QLabel;
|
||||
class QLineEdit;
|
||||
class QVBoxLayout;
|
||||
class QtKeyboard;
|
||||
|
||||
class QtKeyboardValidator final : public QValidator {
|
||||
public:
|
||||
explicit QtKeyboardValidator(QtKeyboard* keyboard);
|
||||
State validate(QString& input, int& pos) const override;
|
||||
|
||||
private:
|
||||
QtKeyboard* keyboard;
|
||||
};
|
||||
|
||||
class QtKeyboardDialog final : public QDialog {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QtKeyboardDialog(QWidget* parent, QtKeyboard* keyboard);
|
||||
void Submit();
|
||||
|
||||
private:
|
||||
void HandleValidationError(Frontend::ValidationError error);
|
||||
QDialogButtonBox* buttons;
|
||||
QLabel* label;
|
||||
QLineEdit* line_edit;
|
||||
QVBoxLayout* layout;
|
||||
QtKeyboard* keyboard;
|
||||
QString text;
|
||||
u8 button;
|
||||
|
||||
friend class QtKeyboard;
|
||||
};
|
||||
|
||||
class QtKeyboard final : public QObject, public Frontend::SoftwareKeyboard {
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit QtKeyboard(QWidget& parent);
|
||||
void Setup(const Frontend::KeyboardConfig* config) override;
|
||||
|
||||
private:
|
||||
Q_INVOKABLE void OpenInputDialog();
|
||||
|
||||
/// Index of the buttons
|
||||
u8 ok_id;
|
||||
static constexpr u8 forgot_id = 1;
|
||||
static constexpr u8 cancel_id = 0;
|
||||
|
||||
QWidget& parent;
|
||||
|
||||
friend class QtKeyboardDialog;
|
||||
friend class QtKeyboardValidator;
|
||||
};
|
@ -0,0 +1,12 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include "core/frontend/applets/default_applets.h"
|
||||
#include "core/frontend/applets/swkbd.h"
|
||||
|
||||
namespace Frontend {
|
||||
void RegisterDefaultApplets() {
|
||||
RegisterSoftwareKeyboard(std::make_shared<DefaultKeyboard>());
|
||||
}
|
||||
} // namespace Frontend
|
@ -0,0 +1,13 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace Frontend {
|
||||
/**
|
||||
* Registers default, frontend-independent applet implementations.
|
||||
* Will be replaced later if any frontend-specific implementation is available.
|
||||
*/
|
||||
void RegisterDefaultApplets();
|
||||
} // namespace Frontend
|
@ -0,0 +1,162 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include "common/assert.h"
|
||||
#include "common/logging/log.h"
|
||||
#include "common/string_util.h"
|
||||
#include "core/core.h"
|
||||
#include "core/frontend/applets/swkbd.h"
|
||||
#include "core/hle/service/cfg/cfg.h"
|
||||
|
||||
namespace Frontend {
|
||||
|
||||
ValidationError SoftwareKeyboard::ValidateFilters(const std::string& input) const {
|
||||
if (config.filters.prevent_digit) {
|
||||
if (std::any_of(input.begin(), input.end(),
|
||||
[](unsigned char c) { return std::isdigit(c); })) {
|
||||
return ValidationError::DigitNotAllowed;
|
||||
}
|
||||
}
|
||||
if (config.filters.prevent_at) {
|
||||
if (input.find('@') != std::string::npos) {
|
||||
return ValidationError::AtSignNotAllowed;
|
||||
}
|
||||
}
|
||||
if (config.filters.prevent_percent) {
|
||||
if (input.find('%') != std::string::npos) {
|
||||
return ValidationError::PercentNotAllowed;
|
||||
}
|
||||
}
|
||||
if (config.filters.prevent_backslash) {
|
||||
if (input.find('\\') != std::string::npos) {
|
||||
return ValidationError::BackslashNotAllowed;
|
||||
}
|
||||
}
|
||||
if (config.filters.prevent_profanity) {
|
||||
// TODO: check the profanity filter
|
||||
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;
|
||||
}
|
||||
|
||||
ValidationError SoftwareKeyboard::ValidateInput(const std::string& input) const {
|
||||
ValidationError error;
|
||||
if ((error = ValidateFilters(input)) != ValidationError::None) {
|
||||
return error;
|
||||
}
|
||||
|
||||
// TODO(jroweboy): Is max_text_length inclusive or exclusive?
|
||||
if (input.size() > config.max_text_length) {
|
||||
return ValidationError::MaxLengthExceeded;
|
||||
}
|
||||
|
||||
bool is_blank =
|
||||
std::all_of(input.begin(), input.end(), [](unsigned char c) { return std::isspace(c); });
|
||||
bool is_empty = input.empty();
|
||||
switch (config.accept_mode) {
|
||||
case AcceptedInput::FixedLength:
|
||||
if (input.size() != config.max_text_length) {
|
||||
return ValidationError::FixedLengthRequired;
|
||||
}
|
||||
break;
|
||||
case AcceptedInput::NotEmptyAndNotBlank:
|
||||
if (is_blank) {
|
||||
return ValidationError::BlankInputNotAllowed;
|
||||
}
|
||||
if (is_empty) {
|
||||
return ValidationError::EmptyInputNotAllowed;
|
||||
}
|
||||
break;
|
||||
case AcceptedInput::NotBlank:
|
||||
if (is_blank) {
|
||||
return ValidationError::BlankInputNotAllowed;
|
||||
}
|
||||
break;
|
||||
case AcceptedInput::NotEmpty:
|
||||
if (is_empty) {
|
||||
return ValidationError::EmptyInputNotAllowed;
|
||||
}
|
||||
break;
|
||||
case AcceptedInput::Anything:
|
||||
return ValidationError::None;
|
||||
default:
|
||||
// TODO(jroweboy): What does hardware do in this case?
|
||||
LOG_CRITICAL(Frontend, "Application requested unknown validation method. Method: {}",
|
||||
static_cast<u32>(config.accept_mode));
|
||||
UNREACHABLE();
|
||||
}
|
||||
|
||||
return ValidationError::None;
|
||||
}
|
||||
|
||||
ValidationError SoftwareKeyboard::ValidateButton(u8 button) const {
|
||||
switch (config.button_config) {
|
||||
case ButtonConfig::None:
|
||||
return ValidationError::None;
|
||||
case ButtonConfig::Single:
|
||||
if (button != 0) {
|
||||
return ValidationError::ButtonOutOfRange;
|
||||
}
|
||||
break;
|
||||
case ButtonConfig::Dual:
|
||||
if (button > 1) {
|
||||
return ValidationError::ButtonOutOfRange;
|
||||
}
|
||||
break;
|
||||
case ButtonConfig::Triple:
|
||||
if (button > 2) {
|
||||
return ValidationError::ButtonOutOfRange;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
}
|
||||
return ValidationError::None;
|
||||
}
|
||||
|
||||
ValidationError SoftwareKeyboard::Finalize(const std::string& text, u8 button) {
|
||||
ValidationError error;
|
||||
if ((error = ValidateInput(text)) != ValidationError::None) {
|
||||
return error;
|
||||
}
|
||||
if ((error = ValidateButton(button)) != ValidationError::None) {
|
||||
return error;
|
||||
}
|
||||
data = {text, button};
|
||||
}
|
||||
|
||||
void DefaultKeyboard::Setup(const Frontend::KeyboardConfig* config) {
|
||||
SoftwareKeyboard::Setup(config);
|
||||
std::string username = Common::UTF16ToUTF8(Service::CFG::GetCurrentModule()->GetUsername());
|
||||
switch (this->config.button_config) {
|
||||
case ButtonConfig::None:
|
||||
case ButtonConfig::Single:
|
||||
Finalize(username, 0);
|
||||
break;
|
||||
case ButtonConfig::Dual:
|
||||
Finalize(username, 1);
|
||||
break;
|
||||
case ButtonConfig::Triple:
|
||||
Finalize(username, 2);
|
||||
break;
|
||||
default:
|
||||
UNREACHABLE();
|
||||
}
|
||||
}
|
||||
|
||||
void RegisterSoftwareKeyboard(std::shared_ptr<SoftwareKeyboard> applet) {
|
||||
Core::System::GetInstance().RegisterSoftwareKeyboard(applet);
|
||||
}
|
||||
|
||||
std::shared_ptr<SoftwareKeyboard> GetRegisteredSoftwareKeyboard() {
|
||||
return Core::System::GetInstance().GetSoftwareKeyboard();
|
||||
}
|
||||
|
||||
} // namespace Frontend
|
@ -0,0 +1,131 @@
|
||||
// Copyright 2018 Citra Emulator Project
|
||||
// Licensed under GPLv2 or any later version
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include "common/assert.h"
|
||||
|
||||
namespace Frontend {
|
||||
|
||||
enum class AcceptedInput {
|
||||
Anything, /// All inputs are accepted.
|
||||
NotEmpty, /// Empty inputs are not accepted.
|
||||
NotEmptyAndNotBlank, /// Empty or blank inputs (consisting solely of whitespace) are not
|
||||
/// accepted.
|
||||
NotBlank, /// Blank inputs (consisting solely of whitespace) are not accepted, but empty
|
||||
/// inputs are.
|
||||
FixedLength, /// The input must have a fixed length (specified by maxTextLength in
|
||||
/// swkbdInit).
|
||||
};
|
||||
|
||||
enum class ButtonConfig {
|
||||
Single, /// Ok button
|
||||
Dual, /// Cancel | Ok buttons
|
||||
Triple, /// Cancel | I Forgot | Ok buttons
|
||||
None, /// No button (returned by swkbdInputText in special cases)
|
||||
};
|
||||
|
||||
/// Default English button text mappings. Frontends may need to copy this to internationalize it.
|
||||
constexpr char BUTTON_OKAY[] = "Ok";
|
||||
constexpr char BUTTON_CANCEL[] = "Cancel";
|
||||
constexpr char BUTTON_FORGOT[] = "I Forgot";
|
||||
|
||||
/// Configuration thats relevent to frontend implementation of applets. Anything missing that we
|
||||
/// later learn is needed can be added here and filled in by the backend HLE applet
|
||||
struct KeyboardConfig {
|
||||
ButtonConfig button_config;
|
||||
AcceptedInput accept_mode; /// What kinds of input are accepted (blank/empty/fixed width)
|
||||
bool multiline_mode; /// True if the keyboard accepts multiple lines of input
|
||||
u16 max_text_length; /// Maximum number of letters allowed if its a text input
|
||||
u16 max_digits; /// Maximum number of numbers allowed if its a number input
|
||||
std::string hint_text; /// Displayed in the field as a hint before
|
||||
bool has_custom_button_text; /// If true, use the button_text instead
|
||||
std::vector<std::string> button_text; /// Contains the button text that the caller provides
|
||||
struct Filters {
|
||||
bool prevent_digit; /// Disallow the use of more than a certain number of digits
|
||||
/// TODO: how many is a certain number
|
||||
bool prevent_at; /// Disallow the use of the @ sign.
|
||||
bool prevent_percent; /// Disallow the use of the % sign.
|
||||
bool prevent_backslash; /// Disallow the use of the \ sign.
|
||||
bool prevent_profanity; /// Disallow profanity using Nintendo's profanity filter.
|
||||
bool enable_callback; /// Use a callback in order to check the input.
|
||||
};
|
||||
Filters filters;
|
||||
};
|
||||
|
||||
struct KeyboardData {
|
||||
std::string text;
|
||||
u8 button{};
|
||||
};
|
||||
|
||||
enum class ValidationError {
|
||||
None,
|
||||
// Button Selection
|
||||
ButtonOutOfRange,
|
||||
// Configured Filters
|
||||
DigitNotAllowed,
|
||||
AtSignNotAllowed,
|
||||
PercentNotAllowed,
|
||||
BackslashNotAllowed,
|
||||
ProfanityNotAllowed,
|
||||
CallbackFailed,
|
||||
// Allowed Input Type
|
||||
FixedLengthRequired,
|
||||
MaxLengthExceeded,
|
||||
BlankInputNotAllowed,
|
||||
EmptyInputNotAllowed,
|
||||
};
|
||||
|
||||
class SoftwareKeyboard {
|
||||
public:
|
||||
virtual void Setup(const KeyboardConfig* config) {
|
||||
this->config = KeyboardConfig(*config);
|
||||
}
|
||||
const KeyboardData* ReceiveData() {
|
||||
return &data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates if the provided string breaks any of the filter rules. This is meant to be called
|
||||
* whenever the user input changes to check to see if the new input is valid. Frontends can
|
||||
* decide if they want to check the input continuously or once before submission
|
||||
*/
|
||||
ValidationError ValidateFilters(const std::string& input) const;
|
||||
|
||||
/**
|
||||
* Validates the the provided string doesn't break any extra rules like "input must not be
|
||||
* empty". This will be called by Finalize but can be called earlier if the frontend needs
|
||||
*/
|
||||
ValidationError ValidateInput(const std::string& input) const;
|
||||
|
||||
/**
|
||||
* Verifies that the selected button is valid. This should be used as the last check before
|
||||
* closing.
|
||||
*/
|
||||
ValidationError ValidateButton(u8 button) const;
|
||||
|
||||
/**
|
||||
* Runs all validation phases. If successful, stores the data so that the HLE applet in core can
|
||||
* send this to the calling application
|
||||
*/
|
||||
ValidationError Finalize(const std::string& text, u8 button);
|
||||
|
||||
protected:
|
||||
KeyboardConfig config;
|
||||
KeyboardData data;
|
||||
};
|
||||
|
||||
class DefaultKeyboard final : public SoftwareKeyboard {
|
||||
public:
|
||||
void Setup(const KeyboardConfig* config) override;
|
||||
};
|
||||
|
||||
void RegisterSoftwareKeyboard(std::shared_ptr<SoftwareKeyboard> applet);
|
||||
|
||||
std::shared_ptr<SoftwareKeyboard> GetRegisteredSoftwareKeyboard();
|
||||
|
||||
} // namespace Frontend
|
Loading…
Reference in New Issue