From caacefcc2ea47754c56b19759de29fabb2123c77 Mon Sep 17 00:00:00 2001 From: James Rowe Date: Wed, 21 Mar 2018 20:07:11 -0600 Subject: [PATCH 1/6] 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; From 18664c719edf9e432e98f667742295917e53ccd3 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Wed, 20 Jun 2018 12:01:54 +0800 Subject: [PATCH 2/6] frontend/applets: misc fixes * Renamed applet to applets * Added log classes Applet and Applet.SWKBD * Fixes to get it compile --- src/common/logging/backend.cpp | 2 + src/common/logging/log.h | 3 + src/core/CMakeLists.txt | 8 +- .../{applet => applets}/interface.cpp | 6 +- .../frontend/{applet => applets}/interface.h | 19 +++-- .../frontend/{applet => applets}/swkbd.cpp | 28 ++++--- src/core/frontend/{applet => applets}/swkbd.h | 41 +++++---- src/core/hle/applets/swkbd.cpp | 84 +++++++++++++++++-- src/core/hle/applets/swkbd.h | 6 +- 9 files changed, 147 insertions(+), 50 deletions(-) rename src/core/frontend/{applet => applets}/interface.cpp (75%) rename src/core/frontend/{applet => applets}/interface.h (80%) rename src/core/frontend/{applet => applets}/swkbd.cpp (81%) rename src/core/frontend/{applet => applets}/swkbd.h (79%) diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 6f831b359..0166beff0 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -158,6 +158,8 @@ void FileBackend::Write(const Entry& entry) { SUB(Debug, GDBStub) \ CLS(Kernel) \ SUB(Kernel, SVC) \ + CLS(Applet) \ + SUB(Applet, SWKBD) \ CLS(Service) \ SUB(Service, SRV) \ SUB(Service, FRD) \ diff --git a/src/common/logging/log.h b/src/common/logging/log.h index 31ba5beae..a51399dee 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h @@ -48,6 +48,9 @@ enum class Class : ClassType { Debug_GDBStub, ///< GDB Stub Kernel, ///< The HLE implementation of the CTR kernel Kernel_SVC, ///< Kernel system calls + Applet, ///< HLE implementation of system applets. Each applet + /// should have its own subclass. + Applet_SWKBD, ///< The Software Keyboard applet Service, ///< HLE implementation of system services. Each major service /// should have its own subclass. Service_SRV, ///< The SRV (Service Directory) implementation diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 0505a928f..8e291294e 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -68,10 +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/applets/interface.cpp + frontend/applets/interface.h + frontend/applets/swkbd.cpp + frontend/applets/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/applets/interface.cpp similarity index 75% rename from src/core/frontend/applet/interface.cpp rename to src/core/frontend/applets/interface.cpp index 368f180ca..d24f8a9a1 100644 --- a/src/core/frontend/applet/interface.cpp +++ b/src/core/frontend/applets/interface.cpp @@ -3,7 +3,7 @@ // Refer to the license.txt file included. #include -#include "core/frontend/interface.h" +#include "core/frontend/applets/interface.h" namespace Frontend { @@ -17,4 +17,8 @@ void UnregisterFrontendApplet(AppletType type) { registered_applets.erase(type); } +std::shared_ptr GetRegisteredApplet(AppletType type) { + return registered_applets.at(type); +} + } // namespace Frontend diff --git a/src/core/frontend/applet/interface.h b/src/core/frontend/applets/interface.h similarity index 80% rename from src/core/frontend/applet/interface.h rename to src/core/frontend/applets/interface.h index e419df8d2..3e5c0eb35 100644 --- a/src/core/frontend/applet/interface.h +++ b/src/core/frontend/applets/interface.h @@ -16,6 +16,7 @@ enum class AppletType { class AppletConfig {}; class AppletData {}; +// TODO(jroweboy) add ability to draw to framebuffer class AppletInterface { public: virtual ~AppletInterface() = default; @@ -24,12 +25,7 @@ public: * 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; + virtual void Setup(const AppletConfig*) = 0; /** * Checked every update to see if the applet is still running. When the applet is done, the core @@ -39,9 +35,14 @@ public: return running; } -private: - // framebuffer; - std::atomic_bool running = false; + /** + * Called by the core to receive the result data of this applet. + * Frontend implementation **should** block until the data is ready. + */ + virtual const AppletData* ReceiveData() = 0; + +protected: + std::atomic running = false; }; /** diff --git a/src/core/frontend/applet/swkbd.cpp b/src/core/frontend/applets/swkbd.cpp similarity index 81% rename from src/core/frontend/applet/swkbd.cpp rename to src/core/frontend/applets/swkbd.cpp index b7a0517dd..fac36077d 100644 --- a/src/core/frontend/applet/swkbd.cpp +++ b/src/core/frontend/applets/swkbd.cpp @@ -2,13 +2,16 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. -#include "core/frontend/applet/swkbd.h" +#include "common/assert.h" +#include "common/logging/log.h" +#include "core/frontend/applets/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)) { + if (std::any_of(input.begin(), input.end(), + [](unsigned char c) { return std::isdigit(c); })) { return ValidationError::DigitNotAllowed; } } @@ -22,7 +25,7 @@ ValidationError SoftwareKeyboard::ValidateFilters(const std::string& input) { return ValidationError::PercentNotAllowed; } } - if (config.filter.prevent_backslash) { + if (config.filters.prevent_backslash) { if (input.find('\\') != std::string::npos) { return ValidationError::BackslashNotAllowed; } @@ -35,7 +38,7 @@ ValidationError SoftwareKeyboard::ValidateFilters(const std::string& input) { // TODO: check the callback LOG_INFO(Frontend, "App requested a swkbd callback, but its not implemented."); } - return valid; + return ValidationError::None; } ValidationError SoftwareKeyboard::ValidateInput(const std::string& input) { @@ -49,9 +52,12 @@ ValidationError SoftwareKeyboard::ValidateInput(const std::string& input) { return ValidationError::MaxLengthExceeded; } - auto is_blank = [&] { return std::all_of(input.begin(), input.end(), std::isspace); }; + auto is_blank = [&] { + return std::all_of(input.begin(), input.end(), + [](unsigned char c) { return std::isspace(c); }); + }; auto is_empty = [&] { return input.empty(); }; - switch (config.valid_input) { + switch (config.accept_mode) { case AcceptedInput::FixedLength: if (input.size() != config.max_text_length) { return ValidationError::FixedLengthRequired; @@ -80,12 +86,12 @@ ValidationError SoftwareKeyboard::ValidateInput(const std::string& input) { default: // TODO(jroweboy): What does hardware do in this case? NGLOG_CRITICAL(Frontend, "Application requested unknown validation method. Method: {}", - static_cast(config.valid_input)); + static_cast(config.accept_mode)); UNREACHABLE(); } return ValidationError::None; -} // namespace Frontend +} ValidationError SoftwareKeyboard::ValidateButton(u8 button) { switch (config.button_config) { @@ -112,12 +118,12 @@ ValidationError SoftwareKeyboard::ValidateButton(u8 button) { return ValidationError::None; } -ValidationError Finalize(cosnt std::string& text, u8 button) { +ValidationError SoftwareKeyboard::Finalize(const std::string& text, u8 button) { ValidationError error; - if ((error = ValidateInput(text)) != ValidationError::NONE) { + if ((error = ValidateInput(text)) != ValidationError::None) { return error; } - if ((error = ValidateButton(button)) != ValidationError::NONE) { + if ((error = ValidateButton(button)) != ValidationError::None) { return error; } data = {text, button}; diff --git a/src/core/frontend/applet/swkbd.h b/src/core/frontend/applets/swkbd.h similarity index 79% rename from src/core/frontend/applet/swkbd.h rename to src/core/frontend/applets/swkbd.h index 9b5237422..aef2d7c0c 100644 --- a/src/core/frontend/applet/swkbd.h +++ b/src/core/frontend/applets/swkbd.h @@ -4,8 +4,12 @@ #pragma once +#include +#include #include -#include "core/frontend/applet/interface.h" +#include +#include +#include "core/frontend/applets/interface.h" namespace Frontend { @@ -38,8 +42,8 @@ static const std::unordered_map> DEFAULT_ }; /// 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 { +/// later learn is needed can be added here and filled in by the backend HLE applet +struct KeyboardConfig : public AppletConfig { 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 @@ -49,19 +53,23 @@ struct KeyboardConfig { 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_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 { +class KeyboardData : public AppletData { +public: std::string text; - u8 button; + u8 button{}; + + KeyboardData(std::string text, u8 button) : text(std::move(text)), button(button) {} + KeyboardData() = default; }; enum class ValidationError { @@ -77,13 +85,20 @@ enum class ValidationError { CallbackFailed, // Allowed Input Type FixedLengthRequired, + MaxLengthExceeded, BlankInputNotAllowed, EmptyInputNotAllowed, }; class SoftwareKeyboard : public AppletInterface { public: - explict SoftwareKeyboard(KeyboardConfig config) : AppletInterface(), config(config) {} + explicit SoftwareKeyboard() : AppletInterface() {} + const AppletData* ReceiveData() override { + return &data; + } + void Setup(const AppletConfig* config) override { + this->config = KeyboardConfig(*static_cast(config)); + } protected: /** @@ -109,13 +124,9 @@ protected: * 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); + ValidationError Finalize(const std::string& text, u8 button); private: - KeyboardData ReceiveData() override { - return data; - } - KeyboardConfig config; KeyboardData data; }; diff --git a/src/core/hle/applets/swkbd.cpp b/src/core/hle/applets/swkbd.cpp index 6b181de06..af782bde2 100644 --- a/src/core/hle/applets/swkbd.cpp +++ b/src/core/hle/applets/swkbd.cpp @@ -69,22 +69,51 @@ ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter cons DrawScreenKeyboard(); + using namespace Frontend; + frontend_applet = GetRegisteredApplet(AppletType::SoftwareKeyboard); + if (frontend_applet) { + KeyboardConfig frontend_config = ToFrontendConfig(config); + frontend_applet->Setup(&frontend_config); + } + is_running = true; return RESULT_SUCCESS; } void SoftwareKeyboard::Update() { - // TODO(Subv): Handle input using the touch events from the HID module - - // TODO(Subv): Remove this hardcoded text - std::u16string text = Common::UTF8ToUTF16("Citra"); + using namespace Frontend; + KeyboardData data(*static_cast(frontend_applet->ReceiveData())); + std::u16string text = Common::UTF8ToUTF16(data.text); memcpy(text_memory->GetPointer(), text.c_str(), text.length() * sizeof(char16_t)); + switch (config.num_buttons_m1) { + case SoftwareKeyboardButtonConfig::SINGLE_BUTTON: + config.return_code = SoftwareKeyboardResult::D0_CLICK; + break; + case SoftwareKeyboardButtonConfig::DUAL_BUTTON: + if (data.button == 0) + config.return_code = SoftwareKeyboardResult::D1_CLICK0; + else + config.return_code = SoftwareKeyboardResult::D1_CLICK1; + break; + case SoftwareKeyboardButtonConfig::TRIPLE_BUTTON: + if (data.button == 0) + config.return_code = SoftwareKeyboardResult::D2_CLICK0; + else if (data.button == 1) + config.return_code = SoftwareKeyboardResult::D2_CLICK1; + else + config.return_code = SoftwareKeyboardResult::D2_CLICK2; + break; + case SoftwareKeyboardButtonConfig::NO_BUTTON: + // TODO: find out what is actually returned + config.return_code = SoftwareKeyboardResult::NONE; + break; + default: + NGLOG_CRITICAL(Applet_SWKBD, "Unknown button config {}", + static_cast(config.num_buttons_m1)); + UNREACHABLE(); + } - // TODO(Subv): Ask for input and write it to the shared memory - // TODO(Subv): Find out what are the possible values for the return code, - // some games seem to check for a hardcoded 2 - config.return_code = 2; - config.text_length = 6; + config.text_length = static_cast(text.size() + 1); config.text_offset = 0; // TODO(Subv): We're finalizing the applet immediately after it's started, @@ -114,5 +143,42 @@ void SoftwareKeyboard::Finalize() { is_running = false; } + +Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig(SoftwareKeyboardConfig config) { + using namespace Frontend; + KeyboardConfig frontend_config; + frontend_config.button_config = static_cast(config.num_buttons_m1); + frontend_config.accept_mode = static_cast(config.valid_input); + frontend_config.multiline_mode = config.multiline; + frontend_config.max_text_length = config.max_text_length; + frontend_config.max_digits = config.max_digits; + std::wstring_convert, char16_t> convert; + frontend_config.hint_text = + convert.to_bytes(reinterpret_cast(config.hint_text.data())); + frontend_config.has_custom_button_text = + std::all_of(config.button_text.begin(), config.button_text.end(), + [](std::array x) { + return std::all_of(x.begin(), x.end(), [](u16 x) { return x == 0; }); + }); + if (frontend_config.has_custom_button_text) { + for (const auto& text : config.button_text) { + frontend_config.button_text.push_back( + convert.to_bytes(reinterpret_cast(text.data()))); + } + } + frontend_config.filters.prevent_digit = + static_cast(config.filter_flags & SoftwareKeyboardFilter::DIGITS); + frontend_config.filters.prevent_at = + static_cast(config.filter_flags & SoftwareKeyboardFilter::AT); + frontend_config.filters.prevent_percent = + static_cast(config.filter_flags & SoftwareKeyboardFilter::PERCENT); + frontend_config.filters.prevent_backslash = + static_cast(config.filter_flags & SoftwareKeyboardFilter::BACKSLASH); + frontend_config.filters.prevent_profanity = + static_cast(config.filter_flags & SoftwareKeyboardFilter::PROFANITY); + frontend_config.filters.enable_callback = + static_cast(config.filter_flags & SoftwareKeyboardFilter::CALLBACK); + return frontend_config; +} } // namespace Applets } // namespace HLE diff --git a/src/core/hle/applets/swkbd.h b/src/core/hle/applets/swkbd.h index 0324abf27..4ba0e36b4 100644 --- a/src/core/hle/applets/swkbd.h +++ b/src/core/hle/applets/swkbd.h @@ -6,7 +6,7 @@ #include "common/common_funcs.h" #include "common/common_types.h" -#include "core/frontend/applet/swkbd.h" +#include "core/frontend/applets/swkbd.h" #include "core/hle/applets/applet.h" #include "core/hle/kernel/kernel.h" #include "core/hle/kernel/shared_memory.h" @@ -197,6 +197,8 @@ public: void Finalize(); private: + Frontend::KeyboardConfig ToFrontendConfig(SoftwareKeyboardConfig config); + /// This SharedMemory will be created when we receive the LibAppJustStarted message. /// It holds the framebuffer info retrieved by the application with /// GSPGPU::ImportDisplayCaptureInfo @@ -207,6 +209,8 @@ private: /// Configuration of this instance of the SoftwareKeyboard, as received from the application SoftwareKeyboardConfig config; + + std::shared_ptr frontend_applet; }; } // namespace Applets } // namespace HLE From f23443b921132db0057284e40c0de8c9209405fc Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Wed, 20 Jun 2018 17:20:38 +0800 Subject: [PATCH 3/6] applets/swkbd: Implement DefaultCitraKeyboard --- src/citra/citra.cpp | 4 +++ src/citra_qt/main.cpp | 4 +++ src/core/CMakeLists.txt | 2 ++ src/core/frontend/applets/default_applets.cpp | 13 +++++++++ src/core/frontend/applets/default_applets.h | 13 +++++++++ src/core/frontend/applets/swkbd.h | 29 ++++++++++++++++--- src/core/hle/applets/swkbd.h | 2 -- 7 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/core/frontend/applets/default_applets.cpp create mode 100644 src/core/frontend/applets/default_applets.h diff --git a/src/citra/citra.cpp b/src/citra/citra.cpp index 08a05c558..8ecc1b202 100644 --- a/src/citra/citra.cpp +++ b/src/citra/citra.cpp @@ -35,6 +35,7 @@ #include "common/string_util.h" #include "core/core.h" #include "core/file_sys/cia_container.h" +#include "core/frontend/applets/default_applets.h" #include "core/gdbstub/gdbstub.h" #include "core/hle/service/am/am.h" #include "core/loader/loader.h" @@ -271,6 +272,9 @@ int main(int argc, char** argv) { Settings::values.movie_record = std::move(movie_record); Settings::Apply(); + // Register frontend applets + Frontend::RegisterDefaultApplets(); + std::unique_ptr emu_window{std::make_unique(fullscreen)}; Core::System& system{Core::System::GetInstance()}; diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp index ff16a0006..b44410be6 100644 --- a/src/citra_qt/main.cpp +++ b/src/citra_qt/main.cpp @@ -50,6 +50,7 @@ #include "common/string_util.h" #include "core/core.h" #include "core/file_sys/archive_source_sd_savedata.h" +#include "core/frontend/applets/default_applets.h" #include "core/gdbstub/gdbstub.h" #include "core/hle/service/fs/archive.h" #include "core/loader/loader.h" @@ -1467,6 +1468,9 @@ int main(int argc, char* argv[]) { Camera::RegisterFactory("qt", std::make_unique()); Camera::QtMultimediaCameraHandler::Init(); + // Register frontend applets + Frontend::RegisterDefaultApplets(); + main_window.show(); return app.exec(); } diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 8e291294e..2bf9e7fd0 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -68,6 +68,8 @@ add_library(core STATIC file_sys/savedata_archive.h file_sys/title_metadata.cpp file_sys/title_metadata.h + frontend/applets/default_applets.cpp + frontend/applets/default_applets.h frontend/applets/interface.cpp frontend/applets/interface.h frontend/applets/swkbd.cpp diff --git a/src/core/frontend/applets/default_applets.cpp b/src/core/frontend/applets/default_applets.cpp new file mode 100644 index 000000000..4edde4d69 --- /dev/null +++ b/src/core/frontend/applets/default_applets.cpp @@ -0,0 +1,13 @@ +// 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/interface.h" +#include "core/frontend/applets/swkbd.h" + +namespace Frontend { +void RegisterDefaultApplets() { + RegisterFrontendApplet(std::make_shared(), AppletType::SoftwareKeyboard); +} +} // namespace Frontend diff --git a/src/core/frontend/applets/default_applets.h b/src/core/frontend/applets/default_applets.h new file mode 100644 index 000000000..d537b7bf0 --- /dev/null +++ b/src/core/frontend/applets/default_applets.h @@ -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 diff --git a/src/core/frontend/applets/swkbd.h b/src/core/frontend/applets/swkbd.h index aef2d7c0c..d38adf43d 100644 --- a/src/core/frontend/applets/swkbd.h +++ b/src/core/frontend/applets/swkbd.h @@ -9,6 +9,7 @@ #include #include #include +#include "common/assert.h" #include "core/frontend/applets/interface.h" namespace Frontend { @@ -93,12 +94,12 @@ enum class ValidationError { class SoftwareKeyboard : public AppletInterface { public: explicit SoftwareKeyboard() : AppletInterface() {} - const AppletData* ReceiveData() override { - return &data; - } void Setup(const AppletConfig* config) override { this->config = KeyboardConfig(*static_cast(config)); } + const AppletData* ReceiveData() override { + return &data; + } protected: /** @@ -126,9 +127,29 @@ protected: */ ValidationError Finalize(const std::string& text, u8 button); -private: KeyboardConfig config; KeyboardData data; }; +class DefaultCitraKeyboard final : public SoftwareKeyboard { +public: + void Setup(const AppletConfig* config) override { + SoftwareKeyboard::Setup(config); + switch (this->config.button_config) { + case ButtonConfig::None: + case ButtonConfig::Single: + Finalize("Citra", 0); + break; + case ButtonConfig::Dual: + Finalize("Citra", 1); + break; + case ButtonConfig::Triple: + Finalize("Citra", 2); + break; + default: + UNREACHABLE(); + } + } +}; + } // namespace Frontend diff --git a/src/core/hle/applets/swkbd.h b/src/core/hle/applets/swkbd.h index 4ba0e36b4..4b9f21ce1 100644 --- a/src/core/hle/applets/swkbd.h +++ b/src/core/hle/applets/swkbd.h @@ -174,8 +174,6 @@ 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) From 5407ed8b5e6cecf111c17e4490c97541191ed832 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Wed, 20 Jun 2018 20:01:50 +0800 Subject: [PATCH 4/6] citra_qt/applets/swkbd: QtKeyboard and misc fixes * Addressed comments and removed the applet interface * swkbd: address @lioncash's comments * core: more fixes ** Moved registered_swkbd to System ** Removed an usused virtual ** Removed functionality of DrawScreenKeyboard ** Removed src/core/settings.h change * swkbd: address @lioncash's 2nd review * swkbd: update logging macro * QtKeyboard: Make dialog modal and hide help --- src/citra_qt/CMakeLists.txt | 2 + src/citra_qt/applets/swkbd.cpp | 127 ++++++++++++++++++ src/citra_qt/applets/swkbd.h | 65 +++++++++ src/citra_qt/main.cpp | 2 + src/core/CMakeLists.txt | 2 - src/core/core.cpp | 4 + src/core/core.h | 12 ++ src/core/frontend/applets/default_applets.cpp | 3 +- src/core/frontend/applets/interface.cpp | 24 ---- src/core/frontend/applets/interface.h | 67 --------- src/core/frontend/applets/swkbd.cpp | 56 +++++--- src/core/frontend/applets/swkbd.h | 74 ++++------ src/core/hle/applets/swkbd.cpp | 75 +++++------ src/core/hle/applets/swkbd.h | 108 +++++++-------- src/core/settings.h | 3 - 15 files changed, 369 insertions(+), 255 deletions(-) create mode 100644 src/citra_qt/applets/swkbd.cpp create mode 100644 src/citra_qt/applets/swkbd.h delete mode 100644 src/core/frontend/applets/interface.cpp delete mode 100644 src/core/frontend/applets/interface.h diff --git a/src/citra_qt/CMakeLists.txt b/src/citra_qt/CMakeLists.txt index 7b0d0eaac..6c1760d2c 100644 --- a/src/citra_qt/CMakeLists.txt +++ b/src/citra_qt/CMakeLists.txt @@ -7,6 +7,8 @@ add_executable(citra-qt Info.plist aboutdialog.cpp aboutdialog.h + applets/swkbd.cpp + applets/swkbd.h bootmanager.cpp bootmanager.h camera/camera_util.cpp diff --git a/src/citra_qt/applets/swkbd.cpp b/src/citra_qt/applets/swkbd.cpp new file mode 100644 index 000000000..57d88dced --- /dev/null +++ b/src/citra_qt/applets/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 +#include +#include +#include +#include +#include +#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) { + 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 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(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); +} diff --git a/src/citra_qt/applets/swkbd.h b/src/citra_qt/applets/swkbd.h new file mode 100644 index 000000000..cfcbcb45e --- /dev/null +++ b/src/citra_qt/applets/swkbd.h @@ -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 +#include +#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; +}; diff --git a/src/citra_qt/main.cpp b/src/citra_qt/main.cpp index b44410be6..7e34f0704 100644 --- a/src/citra_qt/main.cpp +++ b/src/citra_qt/main.cpp @@ -16,6 +16,7 @@ #include #include #include "citra_qt/aboutdialog.h" +#include "citra_qt/applets/swkbd.h" #include "citra_qt/bootmanager.h" #include "citra_qt/camera/qt_multimedia_camera.h" #include "citra_qt/camera/still_image_camera.h" @@ -1470,6 +1471,7 @@ int main(int argc, char* argv[]) { // Register frontend applets Frontend::RegisterDefaultApplets(); + Frontend::RegisterSoftwareKeyboard(std::make_shared(main_window)); main_window.show(); return app.exec(); diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 2bf9e7fd0..2f4ef6c94 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -70,8 +70,6 @@ add_library(core STATIC file_sys/title_metadata.h frontend/applets/default_applets.cpp frontend/applets/default_applets.h - frontend/applets/interface.cpp - frontend/applets/interface.h frontend/applets/swkbd.cpp frontend/applets/swkbd.h frontend/camera/blank_camera.cpp diff --git a/src/core/core.cpp b/src/core/core.cpp index 287014c4c..210dc8bf3 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -199,6 +199,10 @@ const Service::SM::ServiceManager& System::ServiceManager() const { return *service_manager; } +void System::RegisterSoftwareKeyboard(std::shared_ptr swkbd) { + registered_swkbd = std::move(swkbd); +} + void System::Shutdown() { // Log last frame performance stats auto perf_results = GetAndResetPerfStats(); diff --git a/src/core/core.h b/src/core/core.h index a52a01708..036179149 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -7,6 +7,7 @@ #include #include #include "common/common_types.h" +#include "core/frontend/applets/swkbd.h" #include "core/loader/loader.h" #include "core/memory.h" #include "core/perf_stats.h" @@ -150,6 +151,14 @@ public: return *app_loader; } + /// Frontend Applets + + void RegisterSoftwareKeyboard(std::shared_ptr swkbd); + + std::shared_ptr GetSoftwareKeyboard() const { + return registered_swkbd; + } + private: /** * Initialize the emulated system. @@ -180,6 +189,9 @@ private: /// Service manager std::shared_ptr service_manager; + /// Frontend applets + std::shared_ptr registered_swkbd; + static System s_instance; ResultStatus status = ResultStatus::Success; diff --git a/src/core/frontend/applets/default_applets.cpp b/src/core/frontend/applets/default_applets.cpp index 4edde4d69..2f599b79c 100644 --- a/src/core/frontend/applets/default_applets.cpp +++ b/src/core/frontend/applets/default_applets.cpp @@ -3,11 +3,10 @@ // Refer to the license.txt file included. #include "core/frontend/applets/default_applets.h" -#include "core/frontend/applets/interface.h" #include "core/frontend/applets/swkbd.h" namespace Frontend { void RegisterDefaultApplets() { - RegisterFrontendApplet(std::make_shared(), AppletType::SoftwareKeyboard); + RegisterSoftwareKeyboard(std::make_shared()); } } // namespace Frontend diff --git a/src/core/frontend/applets/interface.cpp b/src/core/frontend/applets/interface.cpp deleted file mode 100644 index d24f8a9a1..000000000 --- a/src/core/frontend/applets/interface.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2018 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#include -#include "core/frontend/applets/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); -} - -std::shared_ptr GetRegisteredApplet(AppletType type) { - return registered_applets.at(type); -} - -} // namespace Frontend diff --git a/src/core/frontend/applets/interface.h b/src/core/frontend/applets/interface.h deleted file mode 100644 index 3e5c0eb35..000000000 --- a/src/core/frontend/applets/interface.h +++ /dev/null @@ -1,67 +0,0 @@ -// 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 {}; - -// TODO(jroweboy) add ability to draw to framebuffer -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 AppletConfig*) = 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; - } - - /** - * Called by the core to receive the result data of this applet. - * Frontend implementation **should** block until the data is ready. - */ - virtual const AppletData* ReceiveData() = 0; - -protected: - std::atomic 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/applets/swkbd.cpp b/src/core/frontend/applets/swkbd.cpp index fac36077d..324ebf728 100644 --- a/src/core/frontend/applets/swkbd.cpp +++ b/src/core/frontend/applets/swkbd.cpp @@ -2,13 +2,16 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include +#include #include "common/assert.h" #include "common/logging/log.h" +#include "core/core.h" #include "core/frontend/applets/swkbd.h" namespace Frontend { -ValidationError SoftwareKeyboard::ValidateFilters(const std::string& input) { +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); })) { @@ -41,7 +44,7 @@ ValidationError SoftwareKeyboard::ValidateFilters(const std::string& input) { return ValidationError::None; } -ValidationError SoftwareKeyboard::ValidateInput(const std::string& input) { +ValidationError SoftwareKeyboard::ValidateInput(const std::string& input) const { ValidationError error; if ((error = ValidateFilters(input)) != ValidationError::None) { return error; @@ -52,11 +55,9 @@ ValidationError SoftwareKeyboard::ValidateInput(const std::string& input) { return ValidationError::MaxLengthExceeded; } - auto is_blank = [&] { - return std::all_of(input.begin(), input.end(), - [](unsigned char c) { return std::isspace(c); }); - }; - auto is_empty = [&] { return input.empty(); }; + 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) { @@ -64,20 +65,20 @@ ValidationError SoftwareKeyboard::ValidateInput(const std::string& input) { } break; case AcceptedInput::NotEmptyAndNotBlank: - if (is_blank()) { + if (is_blank) { return ValidationError::BlankInputNotAllowed; } - if (is_empty()) { + if (is_empty) { return ValidationError::EmptyInputNotAllowed; } break; case AcceptedInput::NotBlank: - if (is_blank()) { + if (is_blank) { return ValidationError::BlankInputNotAllowed; } break; case AcceptedInput::NotEmpty: - if (is_empty()) { + if (is_empty) { return ValidationError::EmptyInputNotAllowed; } break; @@ -85,15 +86,15 @@ ValidationError SoftwareKeyboard::ValidateInput(const std::string& input) { 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.accept_mode)); + LOG_CRITICAL(Frontend, "Application requested unknown validation method. Method: {}", + static_cast(config.accept_mode)); UNREACHABLE(); } return ValidationError::None; } -ValidationError SoftwareKeyboard::ValidateButton(u8 button) { +ValidationError SoftwareKeyboard::ValidateButton(u8 button) const { switch (config.button_config) { case ButtonConfig::None: return ValidationError::None; @@ -127,7 +128,32 @@ ValidationError SoftwareKeyboard::Finalize(const std::string& text, u8 button) { return error; } data = {text, button}; - running = false; +} + +void DefaultCitraKeyboard::Setup(const Frontend::KeyboardConfig* config) { + SoftwareKeyboard::Setup(config); + switch (this->config.button_config) { + case ButtonConfig::None: + case ButtonConfig::Single: + Finalize("Citra", 0); + break; + case ButtonConfig::Dual: + Finalize("Citra", 1); + break; + case ButtonConfig::Triple: + Finalize("Citra", 2); + break; + default: + UNREACHABLE(); + } +} + +void RegisterSoftwareKeyboard(std::shared_ptr applet) { + Core::System::GetInstance().RegisterSoftwareKeyboard(applet); +} + +std::shared_ptr GetRegisteredSoftwareKeyboard() { + return Core::System::GetInstance().GetSoftwareKeyboard(); } } // namespace Frontend diff --git a/src/core/frontend/applets/swkbd.h b/src/core/frontend/applets/swkbd.h index d38adf43d..cadfb725a 100644 --- a/src/core/frontend/applets/swkbd.h +++ b/src/core/frontend/applets/swkbd.h @@ -4,18 +4,15 @@ #pragma once -#include -#include #include #include #include #include "common/assert.h" -#include "core/frontend/applets/interface.h" namespace Frontend { enum class AcceptedInput { - Anything = 0, /// All inputs are accepted. + Anything, /// All inputs are accepted. NotEmpty, /// Empty inputs are not accepted. NotEmptyAndNotBlank, /// Empty or blank inputs (consisting solely of whitespace) are not /// accepted. @@ -26,25 +23,20 @@ enum class AcceptedInput { }; 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) + 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. -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}}, -}; +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 : public AppletConfig { +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 @@ -61,16 +53,13 @@ struct KeyboardConfig : public AppletConfig { 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 filters; }; -class KeyboardData : public AppletData { -public: +struct KeyboardData { std::string text; u8 button{}; - - KeyboardData(std::string text, u8 button) : text(std::move(text)), button(button) {} - KeyboardData() = default; }; enum class ValidationError { @@ -91,35 +80,33 @@ enum class ValidationError { EmptyInputNotAllowed, }; -class SoftwareKeyboard : public AppletInterface { +class SoftwareKeyboard { public: - explicit SoftwareKeyboard() : AppletInterface() {} - void Setup(const AppletConfig* config) override { - this->config = KeyboardConfig(*static_cast(config)); + virtual void Setup(const KeyboardConfig* config) { + this->config = KeyboardConfig(*config); } - const AppletData* ReceiveData() override { + const KeyboardData* ReceiveData() { return &data; } -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); + 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); + 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); + ValidationError ValidateButton(u8 button) const; /** * Runs all validation phases. If successful, stores the data so that the HLE applet in core can @@ -127,29 +114,18 @@ protected: */ ValidationError Finalize(const std::string& text, u8 button); +protected: KeyboardConfig config; KeyboardData data; }; class DefaultCitraKeyboard final : public SoftwareKeyboard { public: - void Setup(const AppletConfig* config) override { - SoftwareKeyboard::Setup(config); - switch (this->config.button_config) { - case ButtonConfig::None: - case ButtonConfig::Single: - Finalize("Citra", 0); - break; - case ButtonConfig::Dual: - Finalize("Citra", 1); - break; - case ButtonConfig::Triple: - Finalize("Citra", 2); - break; - default: - UNREACHABLE(); - } - } + void Setup(const KeyboardConfig* config) override; }; +void RegisterSoftwareKeyboard(std::shared_ptr applet); + +std::shared_ptr GetRegisteredSoftwareKeyboard(); + } // namespace Frontend diff --git a/src/core/hle/applets/swkbd.cpp b/src/core/hle/applets/swkbd.cpp index af782bde2..e7ea84572 100644 --- a/src/core/hle/applets/swkbd.cpp +++ b/src/core/hle/applets/swkbd.cpp @@ -2,6 +2,7 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include #include #include #include "common/assert.h" @@ -70,7 +71,7 @@ ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter cons DrawScreenKeyboard(); using namespace Frontend; - frontend_applet = GetRegisteredApplet(AppletType::SoftwareKeyboard); + frontend_applet = GetRegisteredSoftwareKeyboard(); if (frontend_applet) { KeyboardConfig frontend_config = ToFrontendConfig(config); frontend_applet->Setup(&frontend_config); @@ -82,38 +83,38 @@ ResultCode SoftwareKeyboard::StartImpl(Service::APT::AppletStartupParameter cons void SoftwareKeyboard::Update() { using namespace Frontend; - KeyboardData data(*static_cast(frontend_applet->ReceiveData())); + KeyboardData data(*frontend_applet->ReceiveData()); std::u16string text = Common::UTF8ToUTF16(data.text); memcpy(text_memory->GetPointer(), text.c_str(), text.length() * sizeof(char16_t)); switch (config.num_buttons_m1) { - case SoftwareKeyboardButtonConfig::SINGLE_BUTTON: - config.return_code = SoftwareKeyboardResult::D0_CLICK; + case SoftwareKeyboardButtonConfig::SingleButton: + config.return_code = SoftwareKeyboardResult::D0Click; break; - case SoftwareKeyboardButtonConfig::DUAL_BUTTON: + case SoftwareKeyboardButtonConfig::DualButton: if (data.button == 0) - config.return_code = SoftwareKeyboardResult::D1_CLICK0; + config.return_code = SoftwareKeyboardResult::D1Click0; else - config.return_code = SoftwareKeyboardResult::D1_CLICK1; + config.return_code = SoftwareKeyboardResult::D1Click1; break; - case SoftwareKeyboardButtonConfig::TRIPLE_BUTTON: + case SoftwareKeyboardButtonConfig::TripleButton: if (data.button == 0) - config.return_code = SoftwareKeyboardResult::D2_CLICK0; + config.return_code = SoftwareKeyboardResult::D2Click0; else if (data.button == 1) - config.return_code = SoftwareKeyboardResult::D2_CLICK1; + config.return_code = SoftwareKeyboardResult::D2Click1; else - config.return_code = SoftwareKeyboardResult::D2_CLICK2; + config.return_code = SoftwareKeyboardResult::D2Click2; break; - case SoftwareKeyboardButtonConfig::NO_BUTTON: + case SoftwareKeyboardButtonConfig::NoButton: // TODO: find out what is actually returned - config.return_code = SoftwareKeyboardResult::NONE; + config.return_code = SoftwareKeyboardResult::None; break; default: - NGLOG_CRITICAL(Applet_SWKBD, "Unknown button config {}", - static_cast(config.num_buttons_m1)); + LOG_CRITICAL(Applet_SWKBD, "Unknown button config {}", + static_cast(config.num_buttons_m1)); UNREACHABLE(); } - config.text_length = static_cast(text.size() + 1); + config.text_length = static_cast(text.size()); config.text_offset = 0; // TODO(Subv): We're finalizing the applet immediately after it's started, @@ -122,13 +123,7 @@ void SoftwareKeyboard::Update() { } void SoftwareKeyboard::DrawScreenKeyboard() { - auto bottom_screen = Service::GSP::GetFrameBufferInfo(0, 1); - auto info = bottom_screen->framebuffer_info[bottom_screen->index]; - - // TODO(Subv): Draw the HLE keyboard, for now just zero-fill the framebuffer - Memory::ZeroBlock(info.address_left, info.stride * 320); - - Service::GSP::SetBufferSwap(1, info); + // TODO(Subv): Draw the HLE keyboard, for now just do nothing } void SoftwareKeyboard::Finalize() { @@ -144,7 +139,8 @@ void SoftwareKeyboard::Finalize() { is_running = false; } -Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig(SoftwareKeyboardConfig config) { +Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig( + const SoftwareKeyboardConfig& config) const { using namespace Frontend; KeyboardConfig frontend_config; frontend_config.button_config = static_cast(config.num_buttons_m1); @@ -152,32 +148,33 @@ Frontend::KeyboardConfig SoftwareKeyboard::ToFrontendConfig(SoftwareKeyboardConf frontend_config.multiline_mode = config.multiline; frontend_config.max_text_length = config.max_text_length; frontend_config.max_digits = config.max_digits; - std::wstring_convert, char16_t> convert; - frontend_config.hint_text = - convert.to_bytes(reinterpret_cast(config.hint_text.data())); + std::u16string buffer(config.hint_text.size(), 0); + std::memcpy(buffer.data(), config.hint_text.data(), config.hint_text.size() * sizeof(u16)); + frontend_config.hint_text = Common::UTF16ToUTF8(buffer); frontend_config.has_custom_button_text = - std::all_of(config.button_text.begin(), config.button_text.end(), - [](std::array x) { - return std::all_of(x.begin(), x.end(), [](u16 x) { return x == 0; }); - }); + !std::all_of(config.button_text.begin(), config.button_text.end(), + [](std::array x) { + return std::all_of(x.begin(), x.end(), [](u16 x) { return x == 0; }); + }); if (frontend_config.has_custom_button_text) { for (const auto& text : config.button_text) { - frontend_config.button_text.push_back( - convert.to_bytes(reinterpret_cast(text.data()))); + 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 = - static_cast(config.filter_flags & SoftwareKeyboardFilter::DIGITS); + static_cast(config.filter_flags & SoftwareKeyboardFilter::Digits); frontend_config.filters.prevent_at = - static_cast(config.filter_flags & SoftwareKeyboardFilter::AT); + static_cast(config.filter_flags & SoftwareKeyboardFilter::At); frontend_config.filters.prevent_percent = - static_cast(config.filter_flags & SoftwareKeyboardFilter::PERCENT); + static_cast(config.filter_flags & SoftwareKeyboardFilter::Percent); frontend_config.filters.prevent_backslash = - static_cast(config.filter_flags & SoftwareKeyboardFilter::BACKSLASH); + static_cast(config.filter_flags & SoftwareKeyboardFilter::Backslash); frontend_config.filters.prevent_profanity = - static_cast(config.filter_flags & SoftwareKeyboardFilter::PROFANITY); + static_cast(config.filter_flags & SoftwareKeyboardFilter::Profanity); frontend_config.filters.enable_callback = - static_cast(config.filter_flags & SoftwareKeyboardFilter::CALLBACK); + static_cast(config.filter_flags & SoftwareKeyboardFilter::Callback); return frontend_config; } } // namespace Applets diff --git a/src/core/hle/applets/swkbd.h b/src/core/hle/applets/swkbd.h index 4b9f21ce1..d75d696ec 100644 --- a/src/core/hle/applets/swkbd.h +++ b/src/core/hle/applets/swkbd.h @@ -27,97 +27,97 @@ 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. + Normal, ///< 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) + SingleButton, ///< Ok button + DualButton, ///< Cancel | Ok buttons + TripleButton, ///< Cancel | I Forgot | Ok buttons + NoButton, ///< 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 + Anything, ///< All inputs are accepted. + NotEmpty, ///< Empty inputs are not accepted. + NotEmptyNotBlank, ///< 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 + 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. + None, ///< Characters are not concealed. + Hide, ///< Characters are concealed immediately. + HideDelay, ///< 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. + 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. + Parental = 1, ///< Parental PIN mode. + DarkenTopScreen = 1 << 1, ///< Darken the top screen when the keyboard is shown. + PredictiveInput = + 1 << 2, ///< Enable predictive input (necessary for Kanji input in JPN systems). + Multiline = 1 << 3, ///< Enable multiline input. + FixedWidth = 1 << 4, ///< Enable fixed-width mode. + AllowHome = 1 << 5, ///< Allow the usage of the HOME button. + AllowReset = 1 << 6, ///< Allow the usage of a software-reset combination. + AllowPower = 1 << 7, ///< Allow the usage of the POWER button. + DefaultQWERTY = 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. + OK, ///< 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. + None = -1, ///< Dummy/unused. + InvalidInput = -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. + D0Click = 0, ///< The button was clicked in 1-button dialogs. + D1Click0, ///< The left button was clicked in 2-button dialogs. + D1Click1, ///< The right button was clicked in 2-button dialogs. + D2Click0, ///< The left button was clicked in 3-button dialogs. + D2Click1, ///< The middle button was clicked in 3-button dialogs. + D2Click2, ///< 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. + 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. + ParentalOK = 20, ///< The parental PIN was verified successfully. + ParentalFail, ///< The parental PIN was incorrect. - BANNED_INPUT = 30, ///< The filter callback returned SoftwareKeyboardCallback::CLOSE. + BannedInput = 30, ///< The filter callback returned SoftwareKeyboardCallback::CLOSE. }; struct SoftwareKeyboardConfig { @@ -195,7 +195,7 @@ public: void Finalize(); private: - Frontend::KeyboardConfig ToFrontendConfig(SoftwareKeyboardConfig config); + Frontend::KeyboardConfig ToFrontendConfig(const SoftwareKeyboardConfig& config) const; /// This SharedMemory will be created when we receive the LibAppJustStarted message. /// It holds the framebuffer info retrieved by the application with @@ -208,7 +208,7 @@ private: /// Configuration of this instance of the SoftwareKeyboard, as received from the application SoftwareKeyboardConfig config; - std::shared_ptr frontend_applet; + std::shared_ptr frontend_applet; }; } // namespace Applets } // namespace HLE diff --git a/src/core/settings.h b/src/core/settings.h index 6014da24f..af56e5599 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -96,9 +96,6 @@ struct Values { std::string motion_device; std::string touch_device; - // Frontend Devices - std::string applet_swkbd; - // Core bool use_cpu_jit; From dcaf4a8e83a7c7fea2b1beafedb2ff1fe78454f3 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Tue, 10 Jul 2018 13:05:13 +0800 Subject: [PATCH 5/6] citra_qt: Add length validation --- src/citra_qt/applets/swkbd.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/citra_qt/applets/swkbd.cpp b/src/citra_qt/applets/swkbd.cpp index 57d88dced..a9237389a 100644 --- a/src/citra_qt/applets/swkbd.cpp +++ b/src/citra_qt/applets/swkbd.cpp @@ -14,6 +14,8 @@ QtKeyboardValidator::QtKeyboardValidator(QtKeyboard* keyboard_) : keyboard(keybo 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; From b54e3b7aa969c2e98f11babeaf23268ce1bbfbf1 Mon Sep 17 00:00:00 2001 From: zhupengfei Date: Tue, 17 Jul 2018 22:42:53 +0800 Subject: [PATCH 6/6] frontend/applets/swkbd: use system username --- src/core/frontend/applets/default_applets.cpp | 2 +- src/core/frontend/applets/swkbd.cpp | 11 +++++++---- src/core/frontend/applets/swkbd.h | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/core/frontend/applets/default_applets.cpp b/src/core/frontend/applets/default_applets.cpp index 2f599b79c..814b6381e 100644 --- a/src/core/frontend/applets/default_applets.cpp +++ b/src/core/frontend/applets/default_applets.cpp @@ -7,6 +7,6 @@ namespace Frontend { void RegisterDefaultApplets() { - RegisterSoftwareKeyboard(std::make_shared()); + RegisterSoftwareKeyboard(std::make_shared()); } } // namespace Frontend diff --git a/src/core/frontend/applets/swkbd.cpp b/src/core/frontend/applets/swkbd.cpp index 324ebf728..335d99d16 100644 --- a/src/core/frontend/applets/swkbd.cpp +++ b/src/core/frontend/applets/swkbd.cpp @@ -6,8 +6,10 @@ #include #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 { @@ -130,18 +132,19 @@ ValidationError SoftwareKeyboard::Finalize(const std::string& text, u8 button) { data = {text, button}; } -void DefaultCitraKeyboard::Setup(const Frontend::KeyboardConfig* config) { +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("Citra", 0); + Finalize(username, 0); break; case ButtonConfig::Dual: - Finalize("Citra", 1); + Finalize(username, 1); break; case ButtonConfig::Triple: - Finalize("Citra", 2); + Finalize(username, 2); break; default: UNREACHABLE(); diff --git a/src/core/frontend/applets/swkbd.h b/src/core/frontend/applets/swkbd.h index cadfb725a..5c07eb12d 100644 --- a/src/core/frontend/applets/swkbd.h +++ b/src/core/frontend/applets/swkbd.h @@ -119,7 +119,7 @@ protected: KeyboardData data; }; -class DefaultCitraKeyboard final : public SoftwareKeyboard { +class DefaultKeyboard final : public SoftwareKeyboard { public: void Setup(const KeyboardConfig* config) override; };