diff --git a/core/libs/prx/libScePad/CMakeLists.txt b/core/libs/prx/libScePad/CMakeLists.txt index 7d82bcf8b..ccb006f0d 100644 --- a/core/libs/prx/libScePad/CMakeLists.txt +++ b/core/libs/prx/libScePad/CMakeLists.txt @@ -5,3 +5,21 @@ add_library(libScePad SHARED EXCLUDE_FROM_ALL target_include_directories(libScePad PRIVATE ${LIBS_INCLUDE_DIR}) set_target_properties(libScePad PROPERTIES CXX_EXTENSIONS OFF) configure_windows_unwind(libScePad) + +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_pad_tests ${LIBS_INCLUDE_DIR}/tests/GuestPad.cpp) + target_include_directories(guest_pad_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_pad_tests PRIVATE libScePad libc) + configure_windows_unwind(guest_pad_tests) + add_test(NAME guest_pad COMMAND guest_pad_tests) + if(WIN32) + set_tests_properties(guest_pad PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/prx/libScePad/Export.cpp b/core/libs/prx/libScePad/Export.cpp index ec4d13144..a470d2897 100644 --- a/core/libs/prx/libScePad/Export.cpp +++ b/core/libs/prx/libScePad/Export.cpp @@ -10,12 +10,27 @@ #include "prx/libScePad/include/Pad.hpp" #include "prx/libScePad/include/PadState.hpp" +namespace { + +constexpr int PAD_ERROR_DEVICE_NO_HANDLE = static_cast(0x80920008); + +bool g_opened = false; + +bool ValidPort(int userId, int type, int index) { + const bool personalPort = type == PAD_PORT_TYPE_STANDARD || type == PAD_PORT_TYPE_SPECIAL; + const bool systemRemote = userId == PAD_USER_ID_SYSTEM && type == PAD_PORT_TYPE_REMOTE; + return index == 0 && (personalPort || systemRemote); +} + +} + extern "C" { int APS5_VABI scePadClose_nid_postfix(int handle) { if (handle != PAD_HANDLE) { return PAD_ERROR_INVALID_HANDLE; } + g_opened = false; return PAD_OK; } @@ -57,11 +72,8 @@ int APS5_VABI scePadGetControllerInformation(int handle, PadControllerInformatio } int APS5_VABI scePadGetHandle(int user_id, int type, int index) { - (void)user_id; - (void)type; - (void)index; - NotImplemented_nid_no_patch(__func__); - return 0; + if (!g_opened || !ValidPort(user_id, type, index)) return PAD_ERROR_DEVICE_NO_HANDLE; + return PAD_HANDLE; } int APS5_VABI scePadGetInfo_nid_postfix(PadInfo* info) { @@ -120,14 +132,10 @@ int APS5_VABI scePadInit_nid_postfix(void) { int APS5_VABI scePadOpen_nid_postfix(int userId, int type, int index, const void* param) { (void)param; - if (index != 0) { - return PAD_ERROR_INVALID_ARG; - } - const bool personalPort = (type == PAD_PORT_TYPE_STANDARD || type == PAD_PORT_TYPE_SPECIAL); - const bool systemRemote = (userId == PAD_USER_ID_SYSTEM && type == PAD_PORT_TYPE_REMOTE); - if (!personalPort && !systemRemote) { + if (!ValidPort(userId, type, index)) { return PAD_ERROR_INVALID_ARG; } + g_opened = true; return PAD_HANDLE; } @@ -224,8 +232,7 @@ int APS5_VABI scePadSetVibrationMode(int handle, int mode) { int APS5_VABI scePadSetVibrationTriggerEffectWeakWhileEmbeddedMicInUse(bool enabled) { (void)enabled; - NotImplemented_nid_no_patch(__func__); - return 0; + return PAD_OK; } } diff --git a/core/libs/tests/GuestPad.cpp b/core/libs/tests/GuestPad.cpp new file mode 100644 index 000000000..a3ded3937 --- /dev/null +++ b/core/libs/tests/GuestPad.cpp @@ -0,0 +1,32 @@ +#include "prx/libScePad/include/Pad.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" +#include + +extern "C" { +int APS5_VABI scePadOpen_nid_postfix(int, int, int, const void*); +int APS5_VABI scePadClose_nid_postfix(int); +int APS5_VABI scePadGetHandle(int, int, int); +int APS5_VABI scePadSetVibrationTriggerEffectWeakWhileEmbeddedMicInUse(bool); +} + +static void Require(bool value) { if (!value) std::abort(); } + +int main() { + constexpr int noHandle = static_cast(0x80920008); + constexpr int user = 0x10000000; + + Require(scePadGetHandle(user, 0, 0) == noHandle); + Require(scePadOpen_nid_postfix(user, 1, 0, nullptr) == PAD_ERROR_INVALID_ARG); + Require(scePadOpen_nid_postfix(user, 0, 1, nullptr) == PAD_ERROR_INVALID_ARG); + Require(scePadGetHandle(user, 0, 0) == noHandle); + const int handle = scePadOpen_nid_postfix(user, 0, 0, nullptr); + Require(handle > 0); + Require(scePadGetHandle(user, 0, 0) == handle); + Require(scePadGetHandle(user, 2, 0) == handle); + Require(scePadGetHandle(0xff, 16, 0) == handle); + Require(scePadGetHandle(user, 16, 0) == noHandle); + Require(scePadGetHandle(user, 0, 1) == noHandle); + Require(scePadClose_nid_postfix(handle) == 0); + Require(scePadGetHandle(user, 0, 0) == noHandle); + Require(scePadSetVibrationTriggerEffectWeakWhileEmbeddedMicInUse(true) == 0); +}