From 65e59966bc1ac86f516d445b4ff47b6c563647b6 Mon Sep 17 00:00:00 2001 From: Nicolas Abosch Date: Fri, 2 Oct 2026 12:03:34 -0300 Subject: [PATCH] feat(libSceIme): implement parameter initialization and panel size queries --- core/libs/prx/libSceIme/CMakeLists.txt | 21 +++++ core/libs/prx/libSceIme/Export.cpp | 23 ++++-- core/libs/tests/GuestImeParameters.cpp | 109 +++++++++++++++++++++++++ 3 files changed, 147 insertions(+), 6 deletions(-) create mode 100644 core/libs/prx/libSceIme/CMakeLists.txt create mode 100644 core/libs/tests/GuestImeParameters.cpp diff --git a/core/libs/prx/libSceIme/CMakeLists.txt b/core/libs/prx/libSceIme/CMakeLists.txt new file mode 100644 index 000000000..77f03e7ea --- /dev/null +++ b/core/libs/prx/libSceIme/CMakeLists.txt @@ -0,0 +1,21 @@ +add_library(libSceIme SHARED EXCLUDE_FROM_ALL ${CMAKE_CURRENT_SOURCE_DIR}/Export.cpp) +target_include_directories(libSceIme PRIVATE ${LIBS_INCLUDE_DIR}) +configure_windows_unwind(libSceIme) + +if(MINGW) + foreach(runtimeLinkFlag IN ITEMS -static-libgcc -static-libstdc++ -static) + string(REPLACE "${runtimeLinkFlag}" "" CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}") + endforeach() +endif() + +if(BUILD_TESTING) + add_test_executable(guest_ime_parameters_tests ${LIBS_INCLUDE_DIR}/tests/GuestImeParameters.cpp) + target_include_directories(guest_ime_parameters_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_ime_parameters_tests PRIVATE libSceIme libc) + configure_windows_unwind(guest_ime_parameters_tests) + add_test(NAME guest_ime_parameters COMMAND guest_ime_parameters_tests) + if(WIN32) + set_tests_properties(guest_ime_parameters PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/prx/libSceIme/Export.cpp b/core/libs/prx/libSceIme/Export.cpp index b9d2bda7d..150180112 100644 --- a/core/libs/prx/libSceIme/Export.cpp +++ b/core/libs/prx/libSceIme/Export.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -9,6 +10,13 @@ namespace { +constexpr int ErrorInvalidType = static_cast(0x80bc0011u); +constexpr int ErrorInvalidOption = static_cast(0x80bc0015u); +constexpr int ErrorInvalidAddress = static_cast(0x80bc0031u); +constexpr uint32_t TypeNumber = 4; +constexpr uint32_t ValidOptions = 0x00007bff; +constexpr uint32_t OptionUseOver2K = 0x00004000; + std::mutex g_keyboardMutex; std::set g_openKeyboards; @@ -22,10 +30,12 @@ int APS5_VABI sceImeClose_nid_postfix(void) { } int APS5_VABI sceImeGetPanelSize(const Param* param, uint32_t* width, uint32_t* height) { - (void)param; - (void)width; - (void)height; - NotImplemented_nid_no_patch(__func__); + if (!param || !width || !height) return ErrorInvalidAddress; + if (param->type > TypeNumber) return ErrorInvalidType; + if ((param->option & ~ValidOptions) != 0) return ErrorInvalidOption; + const uint32_t scale = (param->option & OptionUseOver2K) != 0 ? 2 : 1; + *width = (param->type == TypeNumber ? 370u : 793u) * scale; + *height = (param->type == TypeNumber ? 402u : 408u) * scale; return 0; } @@ -71,8 +81,9 @@ int APS5_VABI sceImeOpen_nid_postfix(const Param* param, const ExtendedParam* ex } void APS5_VABI sceImeParamInit(Param* param) { - (void)param; - NotImplemented_nid_no_patch(__func__); + if (!param) return; + std::memset(param, 0, sizeof(*param)); + param->user_id = -1; } int APS5_VABI sceImeSetCaret(const Caret* caret) { diff --git a/core/libs/tests/GuestImeParameters.cpp b/core/libs/tests/GuestImeParameters.cpp new file mode 100644 index 000000000..880ebb9e0 --- /dev/null +++ b/core/libs/tests/GuestImeParameters.cpp @@ -0,0 +1,109 @@ +#include "SceTypes.hpp" +#include +#include +#include +#include + +extern "C" { +void APS5_VABI sceImeParamInit(Param* param); +int APS5_VABI sceImeGetPanelSize(const Param* param, uint32_t* width, uint32_t* height); +} + +static_assert(sizeof(Param) == 96); +static_assert(offsetof(Param, option) == 32); +static_assert(offsetof(Param, reserved) == 88); + +static void Require(bool value, const char* message) { + if (value) return; + std::fprintf(stderr, "%s\n", message); + std::abort(); +} + +static void CheckInitialization() { + struct GuardedParam { + std::array before; + Param param; + std::array after; + } guarded; + std::memset(&guarded, 0xa5, sizeof(guarded)); + sceImeParamInit(nullptr); + sceImeParamInit(&guarded.param); + Require(guarded.param.user_id == -1, "initial user must be invalid"); + const auto* bytes = reinterpret_cast(&guarded.param); + for (size_t i = sizeof(guarded.param.user_id); i < sizeof(Param); ++i) { + Require(bytes[i] == 0, "all remaining parameter bytes must be cleared"); + } + for (unsigned char value : guarded.before) Require(value == 0xa5, "parameter underrun"); + for (unsigned char value : guarded.after) Require(value == 0xa5, "parameter overrun"); + guarded.param.type = 4; + guarded.param.option = 0x4000; + guarded.param.max_text_length = 32; + sceImeParamInit(&guarded.param); + Require(guarded.param.user_id == -1 && guarded.param.type == 0 && + guarded.param.option == 0 && guarded.param.max_text_length == 0, + "reinitialization must reset previous values"); +} + +static void CheckPanelSizes() { + Param param; + std::memset(¶m, 0xa5, sizeof(param)); + for (uint32_t type = 0; type <= 4; ++type) { + for (uint32_t options : {0u, 0x4000u, 0x7bffu}) { + param.type = type; + param.option = options; + const Param original = param; + struct GuardedSize { uint32_t before; uint32_t value; uint32_t after; }; + GuardedSize width{0x12345678, 0, 0x87654321}; + GuardedSize height = width; + Require(sceImeGetPanelSize(¶m, &width.value, &height.value) == 0, + "valid panel query failed"); + const bool scaled = (options & 0x4000) != 0; + Require(width.value == (type == 4 ? (scaled ? 740u : 370u) : (scaled ? 1586u : 793u)), + "incorrect panel width"); + Require(height.value == (type == 4 ? (scaled ? 804u : 402u) : (scaled ? 816u : 408u)), + "incorrect panel height"); + Require(width.before == 0x12345678 && width.after == 0x87654321 && + height.before == 0x12345678 && height.after == 0x87654321, + "panel query wrote outside its outputs"); + Require(std::memcmp(¶m, &original, sizeof(param)) == 0, + "panel query modified parameters"); + } + } +} + +static void CheckErrors() { + constexpr int InvalidAddress = static_cast(0x80bc0031u); + constexpr int InvalidType = static_cast(0x80bc0011u); + constexpr int InvalidOption = static_cast(0x80bc0015u); + Param param{}; + uint32_t width = 0x12345678; + uint32_t height = 0x87654321; + Require(sceImeGetPanelSize(nullptr, &width, &height) == InvalidAddress, "null parameter"); + Require(sceImeGetPanelSize(¶m, nullptr, &height) == InvalidAddress, "null width"); + Require(sceImeGetPanelSize(¶m, &width, nullptr) == InvalidAddress, "null height"); + for (uint32_t type : {5u, 0xffffffffu}) { + param.type = type; + Require(sceImeGetPanelSize(¶m, &width, &height) == InvalidType, "invalid type"); + } + param.type = 0; + for (uint32_t bit = 0; bit < 32; ++bit) { + param.option = 1u << bit; + const bool valid = (param.option & 0x7bffu) != 0; + uint32_t queriedWidth = width; + uint32_t queriedHeight = height; + Require(sceImeGetPanelSize(¶m, &queriedWidth, &queriedHeight) == (valid ? 0 : InvalidOption), + "option bit validation"); + if (!valid) Require(queriedWidth == width && queriedHeight == height, "outputs changed on invalid option"); + } + param.type = 5; + param.option = 0x80000000; + Require(sceImeGetPanelSize(¶m, &width, &height) == InvalidType, "type error precedence"); + Require(sceImeGetPanelSize(¶m, nullptr, &height) == InvalidAddress, "address error precedence"); + Require(width == 0x12345678 && height == 0x87654321, "outputs changed on error"); +} + +int main() { + CheckInitialization(); + CheckPanelSizes(); + CheckErrors(); +}