From 81143b4a9e6bd2fc87e74137bd71755944536ce4 Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Fri, 5 Apr 2024 09:27:13 +0300 Subject: [PATCH] scePadRead,scePthreadYield implementations --- src/Emulator/Host/controller.cpp | 33 ++++++++++++- src/Emulator/Host/controller.h | 6 +++ .../libraries/libkernel/thread_management.cpp | 6 +++ .../libraries/libkernel/thread_management.h | 5 +- src/core/hle/libraries/libpad/pad.cpp | 47 +++++++++++++++++++ 5 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/Emulator/Host/controller.cpp b/src/Emulator/Host/controller.cpp index fddac073..302e7eac 100644 --- a/src/Emulator/Host/controller.cpp +++ b/src/Emulator/Host/controller.cpp @@ -19,6 +19,37 @@ void GameController::readState(State* state, bool* isConnected, int* connectedCo *state = getLastState(); } +int GameController::ReadStates(State* states, int states_num, bool* isConnected, + int* connectedCount) { + std::scoped_lock lock{m_mutex}; + + *isConnected = m_connected; + *connectedCount = m_connected_count; + + int ret_num = 0; + + if (m_connected) { + if (m_states_num == 0) { + ret_num = 1; + states[0] = m_last_state; + } else { + for (uint32_t i = 0; i < m_states_num; i++) { + if (ret_num >= states_num) { + break; + } + auto index = (m_first_state + i) % MAX_STATES; + if (!m_private[index].obtained) { + m_private[index].obtained = true; + + states[ret_num++] = m_states[index]; + } + } + } + } + + return ret_num; +} + State GameController::getLastState() const { if (m_states_num == 0) { return m_last_state; @@ -39,7 +70,7 @@ void GameController::addState(const State& state) { m_states[index] = state; m_last_state = state; - + m_private[index].obtained = false; m_states_num++; } diff --git a/src/Emulator/Host/controller.h b/src/Emulator/Host/controller.h index de8490d1..8d8236c5 100644 --- a/src/Emulator/Host/controller.h +++ b/src/Emulator/Host/controller.h @@ -20,11 +20,16 @@ public: virtual ~GameController() = default; void readState(State* state, bool* isConnected, int* connectedCount); + int ReadStates(State* states, int states_num, bool* isConnected, int* connectedCount); State getLastState() const; void checKButton(int id, u32 button, bool isPressed); void addState(const State& state); private: + struct StateInternal { + bool obtained = false; + }; + std::mutex m_mutex; bool m_connected = false; State m_last_state; @@ -32,6 +37,7 @@ private: u32 m_states_num = 0; u32 m_first_state = 0; State m_states[MAX_STATES]; + StateInternal m_private[MAX_STATES]; }; } // namespace Emulator::Host::Controller diff --git a/src/core/hle/libraries/libkernel/thread_management.cpp b/src/core/hle/libraries/libkernel/thread_management.cpp index 9a0e86e4..180077c5 100644 --- a/src/core/hle/libraries/libkernel/thread_management.cpp +++ b/src/core/hle/libraries/libkernel/thread_management.cpp @@ -881,6 +881,10 @@ ScePthread PThreadPool::Create() { return ret; } +void PS4_SYSV_ABI scePthreadYield() { + sched_yield(); +} + void pthreadSymbolsRegister(Loader::SymbolsResolver* sym) { LIB_FUNCTION("4+h9EzwKF4I", "libkernel", 1, "libkernel", 1, 1, scePthreadAttrSetschedpolicy); LIB_FUNCTION("-Wreprtu0Qs", "libkernel", 1, "libkernel", 1, 1, scePthreadAttrSetdetachstate); @@ -896,6 +900,8 @@ void pthreadSymbolsRegister(Loader::SymbolsResolver* sym) { LIB_FUNCTION("bt3CTBKmGyI", "libkernel", 1, "libkernel", 1, 1, scePthreadSetaffinity); LIB_FUNCTION("6UgtwV+0zb4", "libkernel", 1, "libkernel", 1, 1, scePthreadCreate); + LIB_FUNCTION("T72hz6ffq08", "libkernel", 1, "libkernel", 1, 1, scePthreadYield); + // mutex calls LIB_FUNCTION("cmo1RIYva9o", "libkernel", 1, "libkernel", 1, 1, scePthreadMutexInit); LIB_FUNCTION("2Of0f+3mhhE", "libkernel", 1, "libkernel", 1, 1, scePthreadMutexDestroy); diff --git a/src/core/hle/libraries/libkernel/thread_management.h b/src/core/hle/libraries/libkernel/thread_management.h index fcfb2dcb..3f779a45 100644 --- a/src/core/hle/libraries/libkernel/thread_management.h +++ b/src/core/hle/libraries/libkernel/thread_management.h @@ -4,11 +4,11 @@ #pragma once #define _TIMESPEC_DEFINED +#include #include +#include #include #include -#include -#include #include "common/types.h" @@ -88,6 +88,7 @@ struct PthreadCondAttrInternal { class PThreadPool { public: ScePthread Create(); + private: std::vector m_threads; std::mutex m_mutex; diff --git a/src/core/hle/libraries/libpad/pad.cpp b/src/core/hle/libraries/libpad/pad.cpp index f224367f..47134473 100644 --- a/src/core/hle/libraries/libpad/pad.cpp +++ b/src/core/hle/libraries/libpad/pad.cpp @@ -48,6 +48,51 @@ int PS4_SYSV_ABI scePadReadState(int32_t handle, ScePadData* pData) { return SCE_OK; } +int PS4_SYSV_ABI scePadRead(int handle, ScePadData* pData, int num) { + int connected_count = 0; + bool connected = false; + Emulator::Host::Controller::State states[64]; + auto* controller = Common::Singleton::Instance(); + int ret_num = controller->ReadStates(states, num, &connected, &connected_count); + + if (!connected) { + ret_num = 1; + } + + for (int i = 0; i < ret_num; i++) { + pData[i].buttons = states[i].buttonsState; + pData[i].leftStick.x = 128; // dummy + pData[i].leftStick.y = 128; // dummy + pData[i].rightStick.x = 0; // dummy + pData[i].rightStick.y = 0; // dummy + pData[i].analogButtons.l2 = 0; // dummy + pData[i].analogButtons.r2 = 0; // dummy + pData[i].orientation.x = 0.0f; + pData[i].orientation.y = 0.0f; + pData[i].orientation.z = 0.0f; + pData[i].orientation.w = 1.0f; + pData[i].acceleration.x = 0.0f; + pData[i].acceleration.y = 0.0f; + pData[i].acceleration.z = 0.0f; + pData[i].angularVelocity.x = 0.0f; + pData[i].angularVelocity.y = 0.0f; + pData[i].angularVelocity.z = 0.0f; + pData[i].touchData.touchNum = 0; + pData[i].touchData.touch[0].x = 0; + pData[i].touchData.touch[0].y = 0; + pData[i].touchData.touch[0].id = 1; + pData[i].touchData.touch[1].x = 0; + pData[i].touchData.touch[1].y = 0; + pData[i].touchData.touch[1].id = 2; + pData[i].connected = connected; + pData[i].timestamp = states[i].time; + pData[i].connectedCount = connected_count; + pData[i].deviceUniqueDataLen = 0; + } + + return ret_num; +} + s32 PS4_SYSV_ABI scePadGetControllerInformation(s32 handle, OrbisPadInformation* info) { LOG_INFO(Lib_Pad, "called handle = {}", handle); info->touchpadDensity = 1; @@ -72,6 +117,8 @@ void padSymbolsRegister(Core::Loader::SymbolsResolver* sym) { LIB_FUNCTION("hv1luiJrqQM", "libScePad", 1, "libScePad", 1, 1, scePadInit); LIB_FUNCTION("xk0AcarP3V4", "libScePad", 1, "libScePad", 1, 1, scePadOpen); LIB_FUNCTION("YndgXqQVV7c", "libScePad", 1, "libScePad", 1, 1, scePadReadState); + LIB_FUNCTION("q1cHNfGycLI", "libScePad", 1, "libScePad", 1, 1, scePadRead); + LIB_FUNCTION("gjP9-KQzoUk", "libScePad", 1, "libScePad", 1, 1, scePadGetControllerInformation); LIB_FUNCTION("clVvL4ZDntw", "libScePad", 1, "libScePad", 1, 1, scePadSetMotionSensorState); }