frontend/applets: frontend swkbd base

Original commits by @jroweboy:

* Rebase out the other commit

* changing branches

* More work on stuff and things ecks DEE

Changes by @zhaowenlan1779:

* Removed #include of result.h
This commit is contained in:
James Rowe 2018-03-21 20:07:11 -06:00 committed by zhupengfei
parent f9a89ff410
commit caacefcc2e
No known key found for this signature in database
GPG key ID: 85B82A3E62174206
7 changed files with 487 additions and 18 deletions

View file

@ -0,0 +1,20 @@
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <unordered_map>
#include "core/frontend/interface.h"
namespace Frontend {
std::unordered_map<AppletType, std::shared_ptr<AppletInterface>> registered_applets;
void RegisterFrontendApplet(std::shared_ptr<AppletInterface> applet, AppletType type) {
registered_applets[type] = applet;
}
void UnregisterFrontendApplet(AppletType type) {
registered_applets.erase(type);
}
} // namespace Frontend

View file

@ -0,0 +1,66 @@
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <atomic>
#include <memory>
namespace Frontend {
enum class AppletType {
SoftwareKeyboard,
};
class AppletConfig {};
class AppletData {};
class AppletInterface {
public:
virtual ~AppletInterface() = default;
/**
* On applet start, the applet specific configuration will be passed in along with the
* framebuffer.
*/
// virtual void Setup(const Config* /*, framebuffer */) = 0;
/**
* Called on a fixed schedule to have the applet update any state such as the framebuffer.
*/
virtual void Update() = 0;
/**
* Checked every update to see if the applet is still running. When the applet is done, the core
* will call ReceiveData
*/
virtual bool IsRunning() {
return running;
}
private:
// framebuffer;
std::atomic_bool running = false;
};
/**
* Frontends call this method to pass a frontend applet implementation to the core. If the core
* already has a applet registered, then this replaces the old applet
*
* @param applet - Frontend Applet implementation that the HLE applet code will launch
* @param type - Which type of applet
*/
void RegisterFrontendApplet(std::shared_ptr<AppletInterface> applet, AppletType type);
/**
* Frontends call this to prevent future requests
*/
void UnregisterFrontendApplet(AppletType type);
/**
* Returns the Frontend Applet for the provided type
*/
std::shared_ptr<AppletInterface> GetRegisteredApplet(AppletType type);
} // namespace Frontend

View file

@ -0,0 +1,127 @@
// Copyright 2018 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include "core/frontend/applet/swkbd.h"
namespace Frontend {
ValidationError SoftwareKeyboard::ValidateFilters(const std::string& input) {
if (config.filters.prevent_digit) {
if (std::any_of(input.begin(), input.end(), std::isdigit)) {
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.filter.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 valid;
}
ValidationError SoftwareKeyboard::ValidateInput(const std::string& input) {
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;
}
auto is_blank = [&] { return std::all_of(input.begin(), input.end(), std::isspace); };
auto is_empty = [&] { return input.empty(); };
switch (config.valid_input) {
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?
NGLOG_CRITICAL(Frontend, "Application requested unknown validation method. Method: {}",
static_cast<u32>(config.valid_input));
UNREACHABLE();
}
return ValidationError::None;
} // namespace Frontend
ValidationError SoftwareKeyboard::ValidateButton(u8 button) {
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 Finalize(cosnt 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};
running = false;
}
} // namespace Frontend

View file

@ -0,0 +1,123 @@
// 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 "core/frontend/applet/interface.h"
namespace Frontend {
enum class AcceptedInput {
Anything = 0, /// 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 = 0, /// 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.
static const char* BUTTON_OKAY = "Ok";
static const char* BUTTON_CANCEL = "Cancel";
static const char* BUTTON_FORGOT = "I Forgot";
static const std::unordered_map<ButtonConfig, std::vector<std::string>> DEFAULT_BUTTON_MAPPING = {
{ButtonConfig::Single, {BUTTON_OKAY}},
{ButtonConfig::Dual, {BUTTON_CANCEL, BUTTON_OKAY}},
{ButtonConfig::Triple, {BUTTON_CANCEL, BUTTON_FORGOT, BUTTON_OKAY}},
};
/// 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 backed 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;
};
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,
BlankInputNotAllowed,
EmptyInputNotAllowed,
};
class SoftwareKeyboard : public AppletInterface {
public:
explict SoftwareKeyboard(KeyboardConfig config) : AppletInterface(), config(config) {}
protected:
/**
* 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);
/**
* 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);
/**
* Verifies that the selected button is valid. This should be used as the last check before
* closing.
*/
ValidationError ValidateButton(u8 button);
/**
* Runs all validation phases. If successful, stores the data so that the HLE applet in core can
* send this to the calling application
*/
ValidationError Finialize(const std::string&, u8 button);
private:
KeyboardData ReceiveData() override {
return data;
}
KeyboardConfig config;
KeyboardData data;
};
} // namespace Frontend