libMouse: adds mutexes

This commit is contained in:
Vinicius Rangel 2024-10-27 17:05:02 -03:00
parent d1fdbd024a
commit c70a4965c2
No known key found for this signature in database
GPG Key ID: A5B154D904B761D9
2 changed files with 31 additions and 12 deletions

View File

@ -1,23 +1,32 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
// Generated By moduleGenerator #include "mouse.h"
#include <common/singleton.h> #include <common/singleton.h>
#include <core/libraries/system/msgdialog_ui.h> #include <core/libraries/system/msgdialog_ui.h>
#include <input/mouse.h> #include <input/mouse.h>
#include "common/elf_info.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "core/libraries/error_codes.h" #include "core/libraries/error_codes.h"
#include "core/libraries/libs.h" #include "core/libraries/libs.h"
#include "mouse.h"
#include <mutex>
using Common::ElfInfo;
namespace Libraries::Mouse { namespace Libraries::Mouse {
static std::mutex g_mtx;
static bool g_initialized = false; static bool g_initialized = false;
static bool g_mouse1_open = false; static bool g_mouse1_open = false;
static bool g_mouse2_open = false; static bool g_mouse2_open = false;
constexpr auto MOUSE1_HANDLE = 0xF1; constexpr int32_t MOUSE1_HANDLE = 0x72617431; // rat1
constexpr auto MOUSE2_HANDLE = 0xF2; 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_NORMAL = 0x00;
constexpr auto ORBIS_MOUSE_OPEN_PARAM_MERGED = 0x01; constexpr auto ORBIS_MOUSE_OPEN_PARAM_MERGED = 0x01;
@ -27,6 +36,7 @@ int PS4_SYSV_ABI sceMouseClose(s32 handle) {
if (!g_initialized) { if (!g_initialized) {
return ORBIS_MOUSE_ERROR_NOT_INITIALIZED; return ORBIS_MOUSE_ERROR_NOT_INITIALIZED;
} }
std::lock_guard lck{g_mtx};
if (handle == MOUSE1_HANDLE && g_mouse1_open) { if (handle == MOUSE1_HANDLE && g_mouse1_open) {
g_mouse1_open = false; g_mouse1_open = false;
return ORBIS_OK; return ORBIS_OK;
@ -71,6 +81,7 @@ int PS4_SYSV_ABI sceMouseGetDeviceInfo() {
int PS4_SYSV_ABI sceMouseInit() { int PS4_SYSV_ABI sceMouseInit() {
LOG_INFO(Lib_Mouse, "called"); LOG_INFO(Lib_Mouse, "called");
std::lock_guard lck{g_mtx};
g_initialized = true; g_initialized = true;
return ORBIS_OK; return ORBIS_OK;
} }
@ -85,7 +96,13 @@ int PS4_SYSV_ABI sceMouseOpen(s32 userId, s32 type, s32 index, OrbisMouseOpenPar
if (!g_initialized) { if (!g_initialized) {
return ORBIS_MOUSE_ERROR_NOT_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 (merge || index == 0) {
if (g_mouse1_open) { 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; return ORBIS_MOUSE_ERROR_INVALID_ARG;
} }
std::lock_guard lck{g_mtx};
auto* mouse = Common::Singleton<Input::GameMouse>::Instance(); auto* mouse = Common::Singleton<Input::GameMouse>::Instance();
if (handle == MOUSE1_HANDLE) { if (handle == MOUSE1_HANDLE) {

View File

@ -16,13 +16,13 @@ struct OrbisMouseOpenParam {
}; };
struct OrbisMouseData { struct OrbisMouseData {
u64 timestamp; u64 timestamp{};
bool connected; bool connected{};
u32 buttons; u32 buttons{};
s32 xAxis; s32 xAxis{};
s32 yAxis; s32 yAxis{};
s32 wheel; s32 wheel{};
s32 tilt; s32 tilt{};
std::array<u8, 8> reserve{}; std::array<u8, 8> reserve{};
}; };