Skip to content

Commit

Permalink
More Lib mouse (shadps4-emu#1414)
Browse files Browse the repository at this point in the history
* system/msgdialog: callback available to be used by host

* sdl window: mouse capture var

* lib/pad: basic special pad impl scaffold & steering wheel

(config specialPadClass set to 6 is required)

* handle all mouse inputs & ask to capture when first opened
  • Loading branch information
viniciuslrangel committed Nov 14, 2024
1 parent 22f0c5a commit d1fdbd0
Show file tree
Hide file tree
Showing 14 changed files with 635 additions and 227 deletions.
7 changes: 7 additions & 0 deletions src/core/libraries/error_codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,13 @@ constexpr int ORBIS_PAD_ERROR_INVALID_REPORT_LENGTH = 0x80920103;
constexpr int ORBIS_PAD_ERROR_INVALID_REPORT_ID = 0x80920104;
constexpr int ORBIS_PAD_ERROR_SEND_AGAIN = 0x80920105;

// Mouse library
constexpr int ORBIS_MOUSE_ERROR_INVALID_ARG = 0x80DF0001;
constexpr int ORBIS_MOUSE_ERROR_INVALID_HANDLE = 0x80DF0003;
constexpr int ORBIS_MOUSE_ERROR_ALREADY_OPENED = 0x80DF0004;
constexpr int ORBIS_MOUSE_ERROR_NOT_INITIALIZED = 0x80DF0005;
constexpr int ORBIS_MOUSE_ERROR_FATAL = 0x80DF00FF;

// UserService library
constexpr int ORBIS_USER_SERVICE_ERROR_INTERNAL = 0x80960001;
constexpr int ORBIS_USER_SERVICE_ERROR_NOT_INITIALIZED = 0x80960002;
Expand Down
123 changes: 106 additions & 17 deletions src/core/libraries/mouse/mouse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// Generated By moduleGenerator
#include <common/singleton.h>
#include <core/libraries/system/msgdialog_ui.h>
#include <input/mouse.h>
#include "common/logging/log.h"
#include "core/libraries/error_codes.h"
Expand All @@ -11,9 +12,31 @@

namespace Libraries::Mouse {

int PS4_SYSV_ABI sceMouseClose() {
LOG_ERROR(Lib_Mouse, "(STUBBED) called");
return ORBIS_OK;
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 auto ORBIS_MOUSE_OPEN_PARAM_NORMAL = 0x00;
constexpr auto ORBIS_MOUSE_OPEN_PARAM_MERGED = 0x01;

int PS4_SYSV_ABI sceMouseClose(s32 handle) {
LOG_INFO(Lib_Mouse, "called");
if (!g_initialized) {
return ORBIS_MOUSE_ERROR_NOT_INITIALIZED;
}
if (handle == MOUSE1_HANDLE && g_mouse1_open) {
g_mouse1_open = false;
return ORBIS_OK;
}
if (handle == MOUSE2_HANDLE && g_mouse2_open) {
g_mouse2_open = false;
return ORBIS_OK;
}

return ORBIS_MOUSE_ERROR_INVALID_HANDLE;
}

int PS4_SYSV_ABI sceMouseConnectPort() {
Expand Down Expand Up @@ -48,6 +71,7 @@ int PS4_SYSV_ABI sceMouseGetDeviceInfo() {

int PS4_SYSV_ABI sceMouseInit() {
LOG_INFO(Lib_Mouse, "called");
g_initialized = true;
return ORBIS_OK;
}

Expand All @@ -57,28 +81,93 @@ int PS4_SYSV_ABI sceMouseMbusInit() {
}

int PS4_SYSV_ABI sceMouseOpen(s32 userId, s32 type, s32 index, OrbisMouseOpenParam* pParam) {
LOG_INFO(Lib_Mouse, "(DUMMY) called");
return 2; // dummy
LOG_INFO(Lib_Mouse, "called");
if (!g_initialized) {
return ORBIS_MOUSE_ERROR_NOT_INITIALIZED;
}
bool merge = pParam != nullptr && (pParam->behaviorFlag & ORBIS_MOUSE_OPEN_PARAM_MERGED) != 0;

if (merge || index == 0) {
if (g_mouse1_open) {
return ORBIS_PAD_ERROR_ALREADY_OPENED;
}
g_mouse1_open = true;
if (!Common::Singleton<Input::GameMouse>::Instance()->m_connected) {
MsgDialog::ShowMsgDialog(
MsgDialog::MsgDialogState(MsgDialog::MsgDialogState::UserState{
.type = MsgDialog::ButtonType::YESNO,
.msg = "Game wants to use your mouse.\nDo you want to allow it?",
}),
false, [](MsgDialog::DialogResult result) {
if (result.buttonId == MsgDialog::ButtonId::YES) {
auto* mouse = Common::Singleton<Input::GameMouse>::Instance();
mouse->m_connected = true;
}
});
}
return MOUSE1_HANDLE;
}
if (index == 1) {
if (g_mouse2_open) {
return ORBIS_PAD_ERROR_ALREADY_OPENED;
}
g_mouse2_open = true;
return MOUSE2_HANDLE;
}
return ORBIS_MOUSE_ERROR_INVALID_ARG;
}

int PS4_SYSV_ABI sceMouseRead(s32 handle, OrbisMouseData* pData, s32 num) {
bool connected = false;
Input::MouseState states[64];
LOG_TRACE(Lib_Mouse, "called");

if (!g_initialized) {
return ORBIS_MOUSE_ERROR_NOT_INITIALIZED;
}

if (num < 1 || num > 64 || pData == nullptr) {
return ORBIS_MOUSE_ERROR_INVALID_ARG;
}

auto* mouse = Common::Singleton<Input::GameMouse>::Instance();
int ret_num = mouse->ReadStates(states, num, &connected);

if (!connected) {
ret_num = 1;
if (handle == MOUSE1_HANDLE) {
if (!g_mouse1_open) {
return ORBIS_MOUSE_ERROR_INVALID_HANDLE;
}
} else if (handle == MOUSE2_HANDLE) {
if (!g_mouse2_open) {
return ORBIS_MOUSE_ERROR_INVALID_HANDLE;
}
// Mouse 2 will never be connected
pData[0] = OrbisMouseData{
.connected = false,
};
return 1;
} else {
return ORBIS_MOUSE_ERROR_INVALID_HANDLE;
}

if (!mouse->m_connected) {
pData[0] = OrbisMouseData{
.connected = false,
};
return 1;
}

Input::MouseState states[64];
int ret_num = mouse->ReadStates(states, num);

for (int i = 0; i < ret_num; i++) {
pData[i].buttons = states[i].buttonsState;
pData[i].connected = connected;
pData[i].timestamp = states[i].time;
pData[i].xAxis = 0;
pData[i].yAxis = 0;
pData[i].wheel = 0;
pData[i].tilt = 0;
const auto& s = states[i];
pData[i] = OrbisMouseData{
.timestamp = s.time,
.connected = true,
.buttons = s.button_state,
.xAxis = s.x_axis,
.yAxis = s.y_axis,
.wheel = s.wheel,
.tilt = s.tilt,
};
}
return ret_num;
}
Expand Down
9 changes: 6 additions & 3 deletions src/core/libraries/mouse/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ struct OrbisMouseData {
s32 yAxis;
s32 wheel;
s32 tilt;
u8 reserve[8];
std::array<u8, 8> reserve{};
};

enum OrbisMouseButtonDataOffset {
ORBIS_MOUSE_BUTTON_PRIMARY = 0x00000001,
ORBIS_MOUSE_BUTTON_SECONDARY = 0x00000002
ORBIS_MOUSE_BUTTON_SECONDARY = 0x00000002,
ORBIS_MOUSE_BUTTON_OPTIONAL = 0x00000004,
ORBIS_MOUSE_BUTTON_OPTIONAL2 = 0x00000008,
ORBIS_MOUSE_BUTTON_OPTIONAL3 = 0x00000010,
};

int PS4_SYSV_ABI sceMouseClose();
int PS4_SYSV_ABI sceMouseClose(s32 handle);
int PS4_SYSV_ABI sceMouseConnectPort();
int PS4_SYSV_ABI sceMouseDebugGetDeviceId();
int PS4_SYSV_ABI sceMouseDeviceOpen();
Expand Down
Loading

0 comments on commit d1fdbd0

Please sign in to comment.