more work in mouse lib

This commit is contained in:
georgemoralis 2024-08-28 20:47:08 +03:00
parent 4f9caa1e73
commit da6fa8cc6c
8 changed files with 201 additions and 11 deletions

View File

@ -549,6 +549,8 @@ set(VIDEO_CORE src/video_core/amdgpu/liverpool.cpp
set(INPUT src/input/controller.cpp set(INPUT src/input/controller.cpp
src/input/controller.h src/input/controller.h
src/input/mouse.cpp
src/input/mouse.h
) )
set(EMULATOR src/emulator.cpp set(EMULATOR src/emulator.cpp

View File

@ -37,6 +37,7 @@
#include "src/core/libraries/dialogs/error_dialog.h" #include "src/core/libraries/dialogs/error_dialog.h"
#include "src/core/libraries/dialogs/ime_dialog.h" #include "src/core/libraries/dialogs/ime_dialog.h"
#include "src/core/libraries/libpng/pngdec.h" #include "src/core/libraries/libpng/pngdec.h"
#include "src/core/libraries/mouse/mouse.h"
namespace Libraries { namespace Libraries {
@ -75,6 +76,7 @@ void InitHLELibs(Core::Loader::SymbolsResolver* sym) {
Libraries::ErrorDialog::RegisterlibSceErrorDialog(sym); Libraries::ErrorDialog::RegisterlibSceErrorDialog(sym);
Libraries::ImeDialog::RegisterlibSceImeDialog(sym); Libraries::ImeDialog::RegisterlibSceImeDialog(sym);
Libraries::AvPlayer::RegisterlibSceAvPlayer(sym); Libraries::AvPlayer::RegisterlibSceAvPlayer(sym);
Libraries::Mouse::RegisterlibSceMouse(sym);
} }
} // namespace Libraries } // namespace Libraries

View File

@ -2,6 +2,8 @@
// SPDX-License-Identifier: GPL-2.0-or-later // SPDX-License-Identifier: GPL-2.0-or-later
// Generated By moduleGenerator // Generated By moduleGenerator
#include <common/singleton.h>
#include <input/mouse.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"
@ -45,7 +47,7 @@ int PS4_SYSV_ABI sceMouseGetDeviceInfo() {
} }
int PS4_SYSV_ABI sceMouseInit() { int PS4_SYSV_ABI sceMouseInit() {
LOG_ERROR(Lib_Mouse, "(STUBBED) called"); LOG_INFO(Lib_Mouse, "called");
return ORBIS_OK; return ORBIS_OK;
} }
@ -54,14 +56,31 @@ int PS4_SYSV_ABI sceMouseMbusInit() {
return ORBIS_OK; return ORBIS_OK;
} }
int PS4_SYSV_ABI sceMouseOpen() { int PS4_SYSV_ABI sceMouseOpen(s32 userId, s32 type, s32 index, OrbisMouseOpenParam* pParam) {
LOG_ERROR(Lib_Mouse, "(STUBBED) called"); LOG_INFO(Lib_Mouse, "(DUMMY) called");
return ORBIS_OK; return 2; // dummy
} }
int PS4_SYSV_ABI sceMouseRead() { int PS4_SYSV_ABI sceMouseRead(s32 handle, OrbisMouseData* pData, s32 num) {
LOG_ERROR(Lib_Mouse, "(STUBBED) called"); bool connected = false;
return ORBIS_OK; Input::MouseState states[64];
auto* mouse = Common::Singleton<Input::GameMouse>::Instance();
int ret_num = mouse->ReadStates(states, num, &connected);
if (!connected) {
ret_num = 1;
}
for (int i = 0; i < ret_num; i++) {
pData[i].buttons = states[i].buttonsState;
pData[i].connected = true;
pData[i].timestamp = states[i].time;
pData[i].xAxis = 0;
pData[i].yAxis = 0;
pData[i].wheel = 0;
pData[i].tilt = 0;
}
return ret_num;
} }
int PS4_SYSV_ABI sceMouseSetHandType() { int PS4_SYSV_ABI sceMouseSetHandType() {

View File

@ -10,6 +10,27 @@ class SymbolsResolver;
namespace Libraries::Mouse { namespace Libraries::Mouse {
struct OrbisMouseOpenParam {
u8 behaviorFlag;
u8 reserve[7];
};
struct OrbisMouseData {
u64 timestamp;
bool connected;
u32 buttons;
s32 xAxis;
s32 yAxis;
s32 wheel;
s32 tilt;
u8 reserve[8];
};
enum OrbisMouseButtonDataOffset {
ORBIS_MOUSE_BUTTON_PRIMARY = 0x00000001,
ORBIS_MOUSE_BUTTON_SECONDARY = 0x00000002
};
int PS4_SYSV_ABI sceMouseClose(); int PS4_SYSV_ABI sceMouseClose();
int PS4_SYSV_ABI sceMouseConnectPort(); int PS4_SYSV_ABI sceMouseConnectPort();
int PS4_SYSV_ABI sceMouseDebugGetDeviceId(); int PS4_SYSV_ABI sceMouseDebugGetDeviceId();
@ -19,8 +40,8 @@ int PS4_SYSV_ABI sceMouseDisconnectPort();
int PS4_SYSV_ABI sceMouseGetDeviceInfo(); int PS4_SYSV_ABI sceMouseGetDeviceInfo();
int PS4_SYSV_ABI sceMouseInit(); int PS4_SYSV_ABI sceMouseInit();
int PS4_SYSV_ABI sceMouseMbusInit(); int PS4_SYSV_ABI sceMouseMbusInit();
int PS4_SYSV_ABI sceMouseOpen(); int PS4_SYSV_ABI sceMouseOpen(s32 userId, s32 type, s32 index, OrbisMouseOpenParam* pParam);
int PS4_SYSV_ABI sceMouseRead(); int PS4_SYSV_ABI sceMouseRead(s32 handle, OrbisMouseData* pData, s32 num);
int PS4_SYSV_ABI sceMouseSetHandType(); int PS4_SYSV_ABI sceMouseSetHandType();
int PS4_SYSV_ABI sceMouseSetPointerSpeed(); int PS4_SYSV_ABI sceMouseSetPointerSpeed();
int PS4_SYSV_ABI sceMouseSetProcessPrivilege(); int PS4_SYSV_ABI sceMouseSetProcessPrivilege();

80
src/input/mouse.cpp Normal file
View File

@ -0,0 +1,80 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "core/libraries/kernel/time_management.h"
#include "input/mouse.h"
namespace Input {
GameMouse::GameMouse() {
m_states_num = 0;
m_last_state = MouseState();
}
int GameMouse::ReadStates(MouseState* states, int states_num, bool* isConnected) {
std::scoped_lock lock{m_mutex};
*isConnected = m_connected;
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_MOUSE_STATES;
if (!m_private[index].obtained) {
m_private[index].obtained = true;
states[ret_num++] = m_states[index];
}
}
}
}
return ret_num;
}
MouseState GameMouse::GetLastState() const {
if (m_states_num == 0) {
return m_last_state;
}
auto last = (m_first_state + m_states_num - 1) % MAX_MOUSE_STATES;
return m_states[last];
}
void GameMouse::AddState(const MouseState& state) {
if (m_states_num >= MAX_MOUSE_STATES) {
m_states_num = MAX_MOUSE_STATES - 1;
m_first_state = (m_first_state + 1) % MAX_MOUSE_STATES;
}
auto index = (m_first_state + m_states_num) % MAX_MOUSE_STATES;
m_states[index] = state;
m_last_state = state;
m_private[index].obtained = false;
m_states_num++;
}
void GameMouse::CheckButton(int id, u32 button, bool isPressed) {
std::scoped_lock lock{m_mutex};
auto state = GetLastState();
state.time = Libraries::Kernel::sceKernelGetProcessTime();
if (isPressed) {
state.buttonsState |= button;
} else {
state.buttonsState &= ~button;
}
AddState(state);
}
}; // namespace Input

43
src/input/mouse.h Normal file
View File

@ -0,0 +1,43 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <mutex>
#include "common/types.h"
namespace Input {
struct MouseState {
u32 buttonsState = 0;
u64 time = 0;
};
constexpr u32 MAX_MOUSE_STATES = 64;
class GameMouse {
public:
GameMouse();
virtual ~GameMouse() = default;
int ReadStates(MouseState* states, int states_num, bool* isConnected);
MouseState GetLastState() const;
void CheckButton(int id, u32 button, bool isPressed);
void AddState(const MouseState& state);
private:
struct StateInternal {
bool obtained = false;
};
std::mutex m_mutex;
bool m_connected = true;
MouseState m_last_state;
int m_connected_count = 0;
u32 m_states_num = 0;
u32 m_first_state = 0;
std::array<MouseState, MAX_MOUSE_STATES> m_states;
std::array<StateInternal, MAX_MOUSE_STATES> m_private;
};
} // namespace Input

View File

@ -16,6 +16,9 @@
#ifdef __APPLE__ #ifdef __APPLE__
#include <SDL3/SDL_metal.h> #include <SDL3/SDL_metal.h>
#endif #endif
#include <common/singleton.h>
#include <core/libraries/mouse/mouse.h>
#include <input/mouse.h>
namespace Frontend { namespace Frontend {
@ -108,11 +111,31 @@ void WindowSDL::waitEvent() {
case SDL_EVENT_QUIT: case SDL_EVENT_QUIT:
is_open = false; is_open = false;
break; break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP:
onMouseAction(&event);
break;
default: default:
break; break;
} }
} }
void WindowSDL::onMouseAction(const SDL_Event* event) {
auto* mouse = Common::Singleton<Input::GameMouse>::Instance();
using Libraries::Mouse::OrbisMouseButtonDataOffset;
u32 button = 0;
switch (event->button.button) {
case SDL_BUTTON_LEFT:
button = OrbisMouseButtonDataOffset::ORBIS_MOUSE_BUTTON_PRIMARY;
break;
case SDL_BUTTON_RIGHT:
button = OrbisMouseButtonDataOffset::ORBIS_MOUSE_BUTTON_SECONDARY;
break;
}
if (button != 0) {
mouse->CheckButton(0, button, event->type == SDL_EVENT_MOUSE_BUTTON_DOWN);
}
}
void WindowSDL::onResize() { void WindowSDL::onResize() {
SDL_GetWindowSizeInPixels(window, &width, &height); SDL_GetWindowSizeInPixels(window, &width, &height);
} }

View File

@ -68,7 +68,7 @@ private:
void onResize(); void onResize();
void onKeyPress(const SDL_Event* event); void onKeyPress(const SDL_Event* event);
void onGamepadEvent(const SDL_Event* event); void onGamepadEvent(const SDL_Event* event);
void onMouseAction(const SDL_Event* event);
int sdlGamepadToOrbisButton(u8 button); int sdlGamepadToOrbisButton(u8 button);
private: private: