Skip to content

Commit

Permalink
more work in mouse lib
Browse files Browse the repository at this point in the history
  • Loading branch information
georgemoralis committed Aug 28, 2024
1 parent 4f9caa1 commit da6fa8c
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ set(VIDEO_CORE src/video_core/amdgpu/liverpool.cpp

set(INPUT src/input/controller.cpp
src/input/controller.h
src/input/mouse.cpp
src/input/mouse.h
)

set(EMULATOR src/emulator.cpp
Expand Down
2 changes: 2 additions & 0 deletions src/core/libraries/libs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "src/core/libraries/dialogs/error_dialog.h"
#include "src/core/libraries/dialogs/ime_dialog.h"
#include "src/core/libraries/libpng/pngdec.h"
#include "src/core/libraries/mouse/mouse.h"

namespace Libraries {

Expand Down Expand Up @@ -75,6 +76,7 @@ void InitHLELibs(Core::Loader::SymbolsResolver* sym) {
Libraries::ErrorDialog::RegisterlibSceErrorDialog(sym);
Libraries::ImeDialog::RegisterlibSceImeDialog(sym);
Libraries::AvPlayer::RegisterlibSceAvPlayer(sym);
Libraries::Mouse::RegisterlibSceMouse(sym);
}

} // namespace Libraries
33 changes: 26 additions & 7 deletions src/core/libraries/mouse/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: GPL-2.0-or-later

// Generated By moduleGenerator
#include <common/singleton.h>
#include <input/mouse.h>
#include "common/logging/log.h"
#include "core/libraries/error_codes.h"
#include "core/libraries/libs.h"
Expand Down Expand Up @@ -45,7 +47,7 @@ int PS4_SYSV_ABI sceMouseGetDeviceInfo() {
}

int PS4_SYSV_ABI sceMouseInit() {
LOG_ERROR(Lib_Mouse, "(STUBBED) called");
LOG_INFO(Lib_Mouse, "called");
return ORBIS_OK;
}

Expand All @@ -54,14 +56,31 @@ int PS4_SYSV_ABI sceMouseMbusInit() {
return ORBIS_OK;
}

int PS4_SYSV_ABI sceMouseOpen() {
LOG_ERROR(Lib_Mouse, "(STUBBED) called");
return ORBIS_OK;
int PS4_SYSV_ABI sceMouseOpen(s32 userId, s32 type, s32 index, OrbisMouseOpenParam* pParam) {
LOG_INFO(Lib_Mouse, "(DUMMY) called");
return 2; // dummy
}

int PS4_SYSV_ABI sceMouseRead() {
LOG_ERROR(Lib_Mouse, "(STUBBED) called");
return ORBIS_OK;
int PS4_SYSV_ABI sceMouseRead(s32 handle, OrbisMouseData* pData, s32 num) {
bool connected = false;
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() {
Expand Down
25 changes: 23 additions & 2 deletions src/core/libraries/mouse/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,27 @@ class SymbolsResolver;

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 sceMouseConnectPort();
int PS4_SYSV_ABI sceMouseDebugGetDeviceId();
Expand All @@ -19,8 +40,8 @@ int PS4_SYSV_ABI sceMouseDisconnectPort();
int PS4_SYSV_ABI sceMouseGetDeviceInfo();
int PS4_SYSV_ABI sceMouseInit();
int PS4_SYSV_ABI sceMouseMbusInit();
int PS4_SYSV_ABI sceMouseOpen();
int PS4_SYSV_ABI sceMouseRead();
int PS4_SYSV_ABI sceMouseOpen(s32 userId, s32 type, s32 index, OrbisMouseOpenParam* pParam);
int PS4_SYSV_ABI sceMouseRead(s32 handle, OrbisMouseData* pData, s32 num);
int PS4_SYSV_ABI sceMouseSetHandType();
int PS4_SYSV_ABI sceMouseSetPointerSpeed();
int PS4_SYSV_ABI sceMouseSetProcessPrivilege();
Expand Down
80 changes: 80 additions & 0 deletions src/input/mouse.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 43 additions & 0 deletions src/input/mouse.h
Original file line number Diff line number Diff line change
@@ -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
25 changes: 24 additions & 1 deletion src/sdl_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#ifdef __APPLE__
#include <SDL3/SDL_metal.h>
#endif
#include <common/singleton.h>
#include <core/libraries/mouse/mouse.h>
#include <input/mouse.h>

namespace Frontend {

Expand Down Expand Up @@ -108,11 +111,31 @@ void WindowSDL::waitEvent() {
case SDL_EVENT_QUIT:
is_open = false;
break;

case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP:
onMouseAction(&event);
break;
default:
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() {
SDL_GetWindowSizeInPixels(window, &width, &height);
}
Expand Down
2 changes: 1 addition & 1 deletion src/sdl_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class WindowSDL {
void onResize();
void onKeyPress(const SDL_Event* event);
void onGamepadEvent(const SDL_Event* event);

void onMouseAction(const SDL_Event* event);
int sdlGamepadToOrbisButton(u8 button);

private:
Expand Down

0 comments on commit da6fa8c

Please sign in to comment.