From 8ddd0eb583a4f10a09549434189376e750011cfc Mon Sep 17 00:00:00 2001 From: Lander Gallastegi Date: Thu, 10 Oct 2024 20:03:49 +0200 Subject: [PATCH] Use ImGUI for encoding/decoding --- CMakeLists.txt | 4 - src/core/libraries/dialogs/ime_dialog_ui.cpp | 101 ++----------------- src/core/libraries/dialogs/ime_dialog_ui.h | 11 +- src/imgui/imgui_config.h | 7 +- 4 files changed, 13 insertions(+), 110 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bd1b3ac84..b94ae1a4f 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -770,10 +770,6 @@ if (APPLE) # Half float conversions for F16C patches target_link_libraries(shadps4 PRIVATE half) - - # Link against the iconv in macOS - find_package(Iconv REQUIRED) - target_link_libraries(shadps4 PRIVATE Iconv::Iconv) endif() if (NOT ENABLE_QT_GUI) diff --git a/src/core/libraries/dialogs/ime_dialog_ui.cpp b/src/core/libraries/dialogs/ime_dialog_ui.cpp index 689071fcb..48f5d75dc 100644 --- a/src/core/libraries/dialogs/ime_dialog_ui.cpp +++ b/src/core/libraries/dialogs/ime_dialog_ui.cpp @@ -14,13 +14,6 @@ #include "core/linker.h" #include "imgui/imgui_std.h" -#ifndef _WIN32 -#define IME_UTF8_ENCODING "UTF-8" -#define IME_ORBIS_ENCODING "UTF-16LE" -#else -#include -#endif - using namespace ImGui; static constexpr ImVec2 BUTTON_SIZE{100.0f, 30.0f}; @@ -42,14 +35,6 @@ ImeDialogState::ImeDialogState(const OrbisImeDialogParam* param, max_text_length = param->maxTextLength; text_buffer = param->inputTextBuffer; -#ifndef _WIN32 - orbis_to_utf8 = iconv_open(IME_UTF8_ENCODING, IME_ORBIS_ENCODING); - utf8_to_orbis = iconv_open(IME_ORBIS_ENCODING, IME_UTF8_ENCODING); - - ASSERT_MSG(orbis_to_utf8 != (iconv_t)-1, "Failed to open iconv orbis_to_utf8"); - ASSERT_MSG(utf8_to_orbis != (iconv_t)-1, "Failed to open iconv utf8_to_orbis"); -#endif - if (param->title) { std::size_t title_len = std::char_traits::length(param->title); title.resize(title_len * 4 + 1); @@ -78,10 +63,6 @@ ImeDialogState::ImeDialogState(const OrbisImeDialogParam* param, } } -ImeDialogState::~ImeDialogState() { - Free(); -} - ImeDialogState::ImeDialogState(ImeDialogState&& other) noexcept : input_changed(other.input_changed), userId(other.userId), is_multiLine(other.is_multiLine), is_numeric(other.is_numeric), type(other.type), enter_label(other.enter_label), @@ -90,21 +71,11 @@ ImeDialogState::ImeDialogState(ImeDialogState&& other) noexcept title(std::move(other.title)), placeholder(std::move(other.placeholder)), current_text(other.current_text) { -#ifndef _WIN32 - orbis_to_utf8 = other.orbis_to_utf8; - utf8_to_orbis = other.utf8_to_orbis; - - other.orbis_to_utf8 = (iconv_t)-1; - other.utf8_to_orbis = (iconv_t)-1; -#endif - other.text_buffer = nullptr; } ImeDialogState& ImeDialogState::operator=(ImeDialogState&& other) { if (this != &other) { - Free(); - input_changed = other.input_changed; userId = other.userId; is_multiLine = other.is_multiLine; @@ -119,14 +90,6 @@ ImeDialogState& ImeDialogState::operator=(ImeDialogState&& other) { placeholder = std::move(other.placeholder); current_text = other.current_text; -#ifndef _WIN32 - orbis_to_utf8 = other.orbis_to_utf8; - utf8_to_orbis = other.utf8_to_orbis; - - other.orbis_to_utf8 = (iconv_t)-1; - other.utf8_to_orbis = (iconv_t)-1; -#endif - other.text_buffer = nullptr; } @@ -177,17 +140,6 @@ bool ImeDialogState::CallTextFilter() { return true; } -void ImeDialogState::Free() { -#ifndef _WIN32 - if (orbis_to_utf8 != (iconv_t)-1) { - iconv_close(orbis_to_utf8); - } - if (utf8_to_orbis != (iconv_t)-1) { - iconv_close(utf8_to_orbis); - } -#endif -} - bool ImeDialogState::CallKeyboardFilter(const OrbisImeKeycode* src_keycode, u16* out_keycode, u32* out_status) { if (!keyboard_filter) { @@ -202,60 +154,21 @@ bool ImeDialogState::CallKeyboardFilter(const OrbisImeKeycode* src_keycode, u16* bool ImeDialogState::ConvertOrbisToUTF8(const char16_t* orbis_text, std::size_t orbis_text_len, char* utf8_text, std::size_t utf8_text_len) { + std::fill(utf8_text, utf8_text + utf8_text_len, '\0'); -#ifndef _WIN32 - std::size_t orbis_text_len_bytes = orbis_text_len * sizeof(char16_t); - std::size_t utf8_text_len_bytes = utf8_text_len * sizeof(char); + const ImWchar* orbis_text_ptr = reinterpret_cast(orbis_text); + ImTextStrToUtf8(utf8_text, utf8_text_len, orbis_text_ptr, orbis_text_ptr + orbis_text_len); - char16_t* orbis_text_ptr = const_cast(orbis_text); - char* utf8_text_ptr = utf8_text; - - std::size_t result = iconv(orbis_to_utf8, (char**)&orbis_text_ptr, &orbis_text_len_bytes, - (char**)&utf8_text_ptr, &utf8_text_len_bytes); - - return result != (std::size_t)-1; -#else - int required_size = - WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast(orbis_text), - orbis_text_len, nullptr, 0, nullptr, nullptr); - if (required_size > utf8_text_len) { - return false; - } - - int converted_size = - WideCharToMultiByte(CP_UTF8, 0, reinterpret_cast(orbis_text), - orbis_text_len, utf8_text, utf8_text_len, nullptr, nullptr); - - return converted_size != 0; -#endif + return true; } bool ImeDialogState::ConvertUTF8ToOrbis(const char* utf8_text, std::size_t utf8_text_len, char16_t* orbis_text, std::size_t orbis_text_len) { + std::fill(orbis_text, orbis_text + orbis_text_len, u'\0'); -#ifndef _WIN32 - std::size_t utf8_text_len_bytes = utf8_text_len * sizeof(char); - std::size_t orbis_text_len_bytes = orbis_text_len * sizeof(char16_t); + ImTextStrFromUtf8(reinterpret_cast(orbis_text), orbis_text_len, utf8_text, nullptr); - char* utf8_text_ptr = const_cast(utf8_text); - char16_t* orbis_text_ptr = orbis_text; - - std::size_t result = iconv(utf8_to_orbis, (char**)&utf8_text_ptr, &utf8_text_len_bytes, - (char**)&orbis_text_ptr, &orbis_text_len_bytes); - - return result != (std::size_t)-1; -#else - int required_size = MultiByteToWideChar(CP_UTF8, 0, utf8_text, utf8_text_len, nullptr, 0); - if (required_size > orbis_text_len) { - return false; - } - - int converted_size = - MultiByteToWideChar(CP_UTF8, 0, utf8_text, utf8_text_len, - reinterpret_cast(orbis_text), orbis_text_len); - - return converted_size != 0; -#endif + return true; } ImeDialogUi::ImeDialogUi(ImeDialogState* state, OrbisImeDialogStatus* status, diff --git a/src/core/libraries/dialogs/ime_dialog_ui.h b/src/core/libraries/dialogs/ime_dialog_ui.h index 5d169e619..96c83954a 100644 --- a/src/core/libraries/dialogs/ime_dialog_ui.h +++ b/src/core/libraries/dialogs/ime_dialog_ui.h @@ -5,9 +5,6 @@ #include #include -#ifndef _WIN32 -#include -#endif #include #include "common/cstring.h" #include "common/types.h" @@ -37,10 +34,7 @@ class ImeDialogState final { // A character can hold up to 4 bytes in UTF-8 Common::CString current_text; -#ifndef _WIN32 - iconv_t orbis_to_utf8 = (iconv_t)-1; - iconv_t utf8_to_orbis = (iconv_t)-1; -#endif + public: ImeDialogState(const OrbisImeDialogParam* param = nullptr, const OrbisImeParamExtended* extended = nullptr); @@ -48,13 +42,10 @@ public: ImeDialogState(ImeDialogState&& other) noexcept; ImeDialogState& operator=(ImeDialogState&& other); - ~ImeDialogState(); - bool CopyTextToOrbisBuffer(); bool CallTextFilter(); private: - void Free(); bool CallKeyboardFilter(const OrbisImeKeycode* src_keycode, u16* out_keycode, u32* out_status); bool ConvertOrbisToUTF8(const char16_t* orbis_text, std::size_t orbis_text_len, char* utf8_text, diff --git a/src/imgui/imgui_config.h b/src/imgui/imgui_config.h index 2094d56bc..ccb084d94 100644 --- a/src/imgui/imgui_config.h +++ b/src/imgui/imgui_config.h @@ -21,7 +21,6 @@ extern void assert_fail_debug_msg(const char* msg); } \ }()) -#define IMGUI_USE_WCHAR32 #define IMGUI_ENABLE_STB_TRUETYPE #define IMGUI_DEFINE_MATH_OPERATORS @@ -29,4 +28,8 @@ extern void assert_fail_debug_msg(const char* msg); constexpr ImVec2(float _v) : x(_v), y(_v) {} #define IM_VEC4_CLASS_EXTRA \ - constexpr ImVec4(float _v) : x(_v), y(_v), z(_v), w(_v) {} \ No newline at end of file + constexpr ImVec4(float _v) : x(_v), y(_v), z(_v), w(_v) {} + +#ifdef IMGUI_USE_WCHAR32 +#error "This project uses 16 bits wchar standard like Orbis" +#endif \ No newline at end of file