From c70a4965c21005ce0e0c08ac56a8b0d2f6f008dd Mon Sep 17 00:00:00 2001 From: Vinicius Rangel Date: Sun, 27 Oct 2024 17:05:02 -0300 Subject: [PATCH] libMouse: adds mutexes --- src/core/libraries/mouse/mouse.cpp | 29 ++++++++++++++++++++++++----- src/core/libraries/mouse/mouse.h | 14 +++++++------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/core/libraries/mouse/mouse.cpp b/src/core/libraries/mouse/mouse.cpp index 550acea4c..7aa674d06 100644 --- a/src/core/libraries/mouse/mouse.cpp +++ b/src/core/libraries/mouse/mouse.cpp @@ -1,23 +1,32 @@ // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later -// Generated By moduleGenerator +#include "mouse.h" + #include #include #include +#include "common/elf_info.h" #include "common/logging/log.h" #include "core/libraries/error_codes.h" #include "core/libraries/libs.h" -#include "mouse.h" + +#include + +using Common::ElfInfo; namespace Libraries::Mouse { +static std::mutex g_mtx; + static bool g_initialized = false; static bool g_mouse1_open = false; static bool g_mouse2_open = false; -constexpr auto MOUSE1_HANDLE = 0xF1; -constexpr auto MOUSE2_HANDLE = 0xF2; +constexpr int32_t MOUSE1_HANDLE = 0x72617431; // rat1 +constexpr int32_t MOUSE2_HANDLE = 0x72617432; // rat2 +static_assert(MOUSE1_HANDLE > 0); +static_assert(MOUSE2_HANDLE > 0); constexpr auto ORBIS_MOUSE_OPEN_PARAM_NORMAL = 0x00; constexpr auto ORBIS_MOUSE_OPEN_PARAM_MERGED = 0x01; @@ -27,6 +36,7 @@ int PS4_SYSV_ABI sceMouseClose(s32 handle) { if (!g_initialized) { return ORBIS_MOUSE_ERROR_NOT_INITIALIZED; } + std::lock_guard lck{g_mtx}; if (handle == MOUSE1_HANDLE && g_mouse1_open) { g_mouse1_open = false; return ORBIS_OK; @@ -71,6 +81,7 @@ int PS4_SYSV_ABI sceMouseGetDeviceInfo() { int PS4_SYSV_ABI sceMouseInit() { LOG_INFO(Lib_Mouse, "called"); + std::lock_guard lck{g_mtx}; g_initialized = true; return ORBIS_OK; } @@ -85,7 +96,13 @@ int PS4_SYSV_ABI sceMouseOpen(s32 userId, s32 type, s32 index, OrbisMouseOpenPar if (!g_initialized) { return ORBIS_MOUSE_ERROR_NOT_INITIALIZED; } - bool merge = pParam != nullptr && (pParam->behaviorFlag & ORBIS_MOUSE_OPEN_PARAM_MERGED) != 0; + if (type != 0) { + return ORBIS_MOUSE_ERROR_INVALID_ARG; + } + std::lock_guard lck{g_mtx}; + + bool merge = ElfInfo::Instance().FirmwareVer() >= ElfInfo::FW_20 && pParam != nullptr && + (pParam->behaviorFlag & ORBIS_MOUSE_OPEN_PARAM_MERGED) != 0; if (merge || index == 0) { if (g_mouse1_open) { @@ -128,6 +145,8 @@ int PS4_SYSV_ABI sceMouseRead(s32 handle, OrbisMouseData* pData, s32 num) { return ORBIS_MOUSE_ERROR_INVALID_ARG; } + std::lock_guard lck{g_mtx}; + auto* mouse = Common::Singleton::Instance(); if (handle == MOUSE1_HANDLE) { diff --git a/src/core/libraries/mouse/mouse.h b/src/core/libraries/mouse/mouse.h index 301877de4..752cf81d7 100644 --- a/src/core/libraries/mouse/mouse.h +++ b/src/core/libraries/mouse/mouse.h @@ -16,13 +16,13 @@ struct OrbisMouseOpenParam { }; struct OrbisMouseData { - u64 timestamp; - bool connected; - u32 buttons; - s32 xAxis; - s32 yAxis; - s32 wheel; - s32 tilt; + u64 timestamp{}; + bool connected{}; + u32 buttons{}; + s32 xAxis{}; + s32 yAxis{}; + s32 wheel{}; + s32 tilt{}; std::array reserve{}; };