From caacefcc2ea47754c56b19759de29fabb2123c77 Mon Sep 17 00:00:00 2001 From: James Rowe Date: Wed, 21 Mar 2018 20:07:11 -0600 Subject: [PATCH] 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 --- src/core/CMakeLists.txt | 4 + src/core/frontend/applet/interface.cpp | 20 +++ src/core/frontend/applet/interface.h | 66 ++++++++++ src/core/frontend/applet/swkbd.cpp | 127 +++++++++++++++++++ src/core/frontend/applet/swkbd.h | 123 +++++++++++++++++++ src/core/hle/applets/swkbd.h | 162 ++++++++++++++++++++++--- src/core/settings.h | 3 + 7 files changed, 487 insertions(+), 18 deletions(-) create mode 100644 src/core/frontend/applet/interface.cpp create mode 100644 src/core/frontend/applet/interface.h create mode 100644 src/core/frontend/applet/swkbd.cpp create mode 100644 src/core/frontend/applet/swkbd.h diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 2da700455..0505a928f 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -68,6 +68,10 @@ add_library(core STATIC file_sys/savedata_archive.h file_sys/title_metadata.cpp file_sys/title_metadata.h + frontend/applet/interface.cpp + frontend/applet/interface.h + frontend/applet/swkbd.cpp + frontend/applet/swkbd.h frontend/camera/blank_camera.cpp frontend/camera/blank_camera.h frontend/camera/factory.cpp diff --git a/src/core/frontend/applet/interface.cpp b/src/core/frontend/applet/interface.cpp new file mode 100644 index 000000000..368f180ca --- /dev/null +++ b/src/core/frontend/applet/interface.cpp @@ -0,0 +1,20 @@ +// Copyright 2018 Citra Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include +#include "core/frontend/interface.h" + +namespace Frontend { + +std::unordered_map> registered_applets; + +void RegisterFrontendApplet(std::shared_ptr applet, AppletType type) { + registered_applets[type] = applet; +} + +void UnregisterFrontendApplet(AppletType type) { + registered_applets.erase(type); +} + +} // namespace Frontend diff --git a/src/core/frontend/applet/interface.h b/src/core/frontend/applet/interface.h new file mode 100644 index 000000000..e419df8d2 --- /dev/null +++ b/src/core/frontend/applet/interface.h @@ -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 +#include + +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 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 GetRegisteredApplet(AppletType type); + +} // namespace Frontend diff --git a/src/core/frontend/applet/swkbd.cpp b/src/core/frontend/applet/swkbd.cpp new file mode 100644 index 000000000..b7a0517dd --- /dev/null +++ b/src/core/frontend/applet/swkbd.cpp @@ -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(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 diff --git a/src/core/frontend/applet/swkbd.h b/src/core/frontend/applet/swkbd.h new file mode 100644 index 000000000..9b5237422 --- /dev/null +++ b/src/core/frontend/applet/swkbd.h @@ -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 +#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> 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 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 diff --git a/src/core/hle/applets/swkbd.h b/src/core/hle/applets/swkbd.h index f0cf320a3..0324abf27 100644 --- a/src/core/hle/applets/swkbd.h +++ b/src/core/hle/applets/swkbd.h @@ -6,6 +6,7 @@ #include "common/common_funcs.h" #include "common/common_types.h" +#include "core/frontend/applet/swkbd.h" #include "core/hle/applets/applet.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/shared_memory.h" @@ -15,33 +16,156 @@ namespace HLE { namespace Applets { +/// Maximum number of buttons that can be in the keyboard. +constexpr int MAX_BUTTON = 3; +/// Maximum button text length, in UTF-16 code units. +constexpr int MAX_BUTTON_TEXT_LEN = 16; +/// Maximum hint text length, in UTF-16 code units. +constexpr int MAX_HINT_TEXT_LEN = 64; +/// Maximum filter callback error message length, in UTF-16 code units. +constexpr int MAX_CALLBACK_MSG_LEN = 256; + +/// Keyboard types +enum class SoftwareKeyboardType : u32 { + NORMAL = 0, ///< Normal keyboard with several pages (QWERTY/accents/symbol/mobile) + QWERTY, ///< QWERTY keyboard only. + NUMPAD, ///< Number pad. + WESTERN, ///< On JPN systems, a text keyboard without Japanese input capabilities, + /// otherwise same as SWKBD_TYPE_NORMAL. +}; + +/// Keyboard dialog buttons. +enum class SoftwareKeyboardButtonConfig : u32 { + SINGLE_BUTTON = 0, ///< Ok button + DUAL_BUTTON, ///< Cancel | Ok buttons + TRIPLE_BUTTON, ///< Cancel | I Forgot | Ok buttons + NO_BUTTON, ///< No button (returned by swkbdInputText in special cases) +}; + +/// Accepted input types. +enum class SoftwareKeyboardValidInput : u32 { + ANYTHING = 0, ///< All inputs are accepted. + NOTEMPTY, ///< Empty inputs are not accepted. + NOTEMPTY_NOTBLANK, ///< 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. + FIXEDLEN, ///< The input must have a fixed length (specified by maxTextLength in + /// swkbdInit). +}; + +/// Keyboard password modes. +enum class SoftwareKeyboardPasswordMode : u32 { + NONE = 0, ///< Characters are not concealed. + HIDE, ///< Characters are concealed immediately. + HIDE_DELAY, ///< Characters are concealed a second after they've been typed. +}; + +/// Keyboard input filtering flags. Allows the caller to specify what input is explicitly not +/// allowed +namespace SoftwareKeyboardFilter { +enum Filter { + DIGITS = 1, ///< Disallow the use of more than a certain number of digits (0 or more) + AT = 1 << 1, ///< Disallow the use of the @ sign. + PERCENT = 1 << 2, ///< Disallow the use of the % sign. + BACKSLASH = 1 << 3, ///< Disallow the use of the \ sign. + PROFANITY = 1 << 4, ///< Disallow profanity using Nintendo's profanity filter. + CALLBACK = 1 << 5, ///< Use a callback in order to check the input. +}; +} // namespace SoftwareKeyboardFilter + +/// Keyboard features. +namespace SoftwareKeyboardFeature { +enum Feature { + PARENTAL = 1, ///< Parental PIN mode. + DARKEN_TOP_SCREEN = 1 << 1, ///< Darken the top screen when the keyboard is shown. + PREDICTIVE_INPUT = + 1 << 2, ///< Enable predictive input (necessary for Kanji input in JPN systems). + MULTILINE = 1 << 3, ///< Enable multiline input. + FIXED_WIDTH = 1 << 4, ///< Enable fixed-width mode. + ALLOW_HOME = 1 << 5, ///< Allow the usage of the HOME button. + ALLOW_RESET = 1 << 6, ///< Allow the usage of a software-reset combination. + ALLOW_POWER = 1 << 7, ///< Allow the usage of the POWER button. + DEFAULT_QWERTY = 1 << 9, ///< Default to the QWERTY page when the keyboard is shown. +}; +} // namespace SoftwareKeyboardFeature + +/// Keyboard filter callback return values. +enum class SoftwareKeyboardCallbackResult : u32 { + OK = 0, ///< Specifies that the input is valid. + CLOSE, ///< Displays an error message, then closes the keyboard. + CONTINUE, ///< Displays an error message and continues displaying the keyboard. +}; + +/// Keyboard return values. +enum class SoftwareKeyboardResult : s32 { + NONE = -1, ///< Dummy/unused. + INVALID_INPUT = -2, ///< Invalid parameters to swkbd. + OUTOFMEM = -3, ///< Out of memory. + + D0_CLICK = 0, ///< The button was clicked in 1-button dialogs. + D1_CLICK0, ///< The left button was clicked in 2-button dialogs. + D1_CLICK1, ///< The right button was clicked in 2-button dialogs. + D2_CLICK0, ///< The left button was clicked in 3-button dialogs. + D2_CLICK1, ///< The middle button was clicked in 3-button dialogs. + D2_CLICK2, ///< The right button was clicked in 3-button dialogs. + + HOMEPRESSED = 10, ///< The HOME button was pressed. + RESETPRESSED, ///< The soft-reset key combination was pressed. + POWERPRESSED, ///< The POWER button was pressed. + + PARENTAL_OK = 20, ///< The parental PIN was verified successfully. + PARENTAL_FAIL, ///< The parental PIN was incorrect. + + BANNED_INPUT = 30, ///< The filter callback returned SoftwareKeyboardCallback::CLOSE. +}; + struct SoftwareKeyboardConfig { - INSERT_PADDING_WORDS(0x8); - - u16 max_text_length; ///< Maximum length of the input text - - INSERT_PADDING_BYTES(0x6E); - - char16_t display_text[65]; ///< Text to display when asking the user for input - - INSERT_PADDING_BYTES(0xE); - - u32 default_text_offset; ///< Offset of the default text in the output SharedMemory - - INSERT_PADDING_WORDS(0x3); + SoftwareKeyboardType type; + SoftwareKeyboardButtonConfig num_buttons_m1; + SoftwareKeyboardValidInput valid_input; + SoftwareKeyboardPasswordMode password_mode; + s32 is_parental_screen; + s32 darken_top_screen; + u32 filter_flags; + u32 save_state_flags; + u16 max_text_length; + u16 dict_word_count; + u16 max_digits; + std::array, MAX_BUTTON> button_text; + std::array numpad_keys; + std::array + hint_text; ///< Text to display when asking the user for input + bool predictive_input; + bool multiline; + bool fixed_width; + bool allow_home; + bool allow_reset; + bool allow_power; + bool unknown; + bool default_qwerty; + std::array button_submits_text; + u16 language; + u32 initial_text_offset; ///< Offset of the default text in the output SharedMemory + u32 dict_offset; + u32 initial_status_offset; + u32 initial_learning_offset; u32 shared_memory_size; ///< Size of the SharedMemory + u32 version; - INSERT_PADDING_WORDS(0x1); + SoftwareKeyboardResult return_code; - u32 return_code; ///< Return code of the SoftwareKeyboard, usually 2, other values are unknown - - INSERT_PADDING_WORDS(0x2); + u32 status_offset; + u32 learning_offset; u32 text_offset; ///< Offset in the SharedMemory where the output text starts u16 text_length; ///< Length in characters of the output text - INSERT_PADDING_BYTES(0x2B6); + int callback_result; + std::array callback_msg; + bool skip_at_check; + INSERT_PADDING_BYTES(0xAB); }; /** @@ -50,6 +174,8 @@ struct SoftwareKeyboardConfig { */ static_assert(sizeof(SoftwareKeyboardConfig) == 0x400, "Software Keyboard Config size is wrong"); +class DefaultCitraKeyboard : Frontend::AppletInterface {}; + class SoftwareKeyboard final : public Applet { public: SoftwareKeyboard(Service::APT::AppletId id, std::weak_ptr manager) diff --git a/src/core/settings.h b/src/core/settings.h index af56e5599..6014da24f 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -96,6 +96,9 @@ struct Values { std::string motion_device; std::string touch_device; + // Frontend Devices + std::string applet_swkbd; + // Core bool use_cpu_jit;