Skip to content

Commit

Permalink
Merge branch 'features' into ipc
Browse files Browse the repository at this point in the history
  • Loading branch information
igor725 authored Jun 13, 2024
2 parents 35fd48e + 926f01d commit 7959f50
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 92 deletions.
114 changes: 55 additions & 59 deletions modules/external/libScePad/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ static ControllerType _getPadType(int32_t userId) {
return ControllerType::SDL;
};

static int _padOpen(int32_t userId, PadPortType type, int32_t index, const void* pParam) {
static int _padOpen(int32_t userId, ScePadPortType type, int32_t index, const void* pParam) {
LOG_USE_MODULE(libScePad);
if ((userId < 1 || userId > 4) && userId != 0xFF) return Err::Pad::INVALID_ARG;
if (type == PadPortType::REMOTE_CONTROL && userId != 0xFF) return Err::Pad::INVALID_ARG;
if (type != PadPortType::STANDARD && type == PadPortType::SPECIAL) {
if (type == ScePadPortType::REMOTE_CONTROL && userId != 0xFF) return Err::Pad::INVALID_ARG;
if (type != ScePadPortType::STANDARD && type == ScePadPortType::SPECIAL) {
LOG_ERR(L"todo Handle PlayStation Move");
return PSMOVE_DUMMY_HANDLE_RANGE + userId;
}
Expand Down Expand Up @@ -116,23 +116,22 @@ EXPORT SYSV_ABI int scePadInit(void) {
return Ok;
}

EXPORT SYSV_ABI int scePadOpen(int32_t userId, PadPortType type, int32_t index, const void* param) {
EXPORT SYSV_ABI int scePadOpen(int32_t userId, ScePadPortType type, int32_t index, const void* param) {
return _padOpen(userId, type, index, param);
}

EXPORT SYSV_ABI int scePadOpenExt(int userId, PadPortType type, int index, const void* param) {
EXPORT SYSV_ABI int scePadOpenExt(int userId, ScePadPortType type, int index, const void* param) {
return _padOpen(userId, type, index, param);
}

EXPORT SYSV_ABI int scePadClose(int32_t handle) {
if (handle > PSMOVE_DUMMY_HANDLE_RANGE) return Ok; // Closing PlayStation Move
std::unique_lock const lock(getData()->m_mutexInt);

LOG_USE_MODULE(libScePad);
LOG_INFO(L"<- Pad[%d]", handle);

if (auto ctl = getController(handle)) {
std::unique_lock const lock(getData()->m_mutexInt);

ctl->userId = -1;
if (ctl->backend) ctl->backend->close();
return Ok;
Expand All @@ -141,8 +140,8 @@ EXPORT SYSV_ABI int scePadClose(int32_t handle) {
return Err::Pad::INVALID_ARG;
}

EXPORT SYSV_ABI int scePadGetHandle(int32_t userId, PadPortType type, int32_t index) {
if (type == PadPortType::SPECIAL) return PSMOVE_DUMMY_HANDLE_RANGE + userId;
EXPORT SYSV_ABI int scePadGetHandle(int32_t userId, ScePadPortType type, int32_t index) {
if (type == ScePadPortType::SPECIAL) return PSMOVE_DUMMY_HANDLE_RANGE + userId;
LOG_USE_MODULE(libScePad);
LOG_DEBUG(L"");

Expand All @@ -165,10 +164,10 @@ EXPORT SYSV_ABI int scePadRead(int32_t handle, ScePadData* pPadData, int32_t num
return Ok;
}

if (auto ctl = getController(handle)) {
if (ctl->backend == nullptr) return Err::Pad::INVALID_HANDLE;
std::unique_lock const lock(getData()->m_mutexInt);

std::unique_lock const lock(getData()->m_mutexInt);
if (auto ctl = getController(handle)) {
if (ctl->backend == nullptr) return Err::Pad::FATAL;

ctl->backend->readPadData(*pPadData);

Expand All @@ -180,7 +179,7 @@ EXPORT SYSV_ABI int scePadRead(int32_t handle, ScePadData* pPadData, int32_t num
return abs(retVal);
}

return Err::Pad::INVALID_ARG;
return Err::Pad::INVALID_HANDLE;
}

EXPORT SYSV_ABI int scePadReadState(int32_t handle, ScePadData* pData) {
Expand All @@ -195,13 +194,15 @@ EXPORT SYSV_ABI int scePadSetMotionSensorState(int32_t handle, bool bEnable) {
return Ok;
}

std::unique_lock const lock(getData()->m_mutexInt);

if (auto ctl = getController(handle)) {
if (ctl->backend == nullptr) return Err::Pad::INVALID_HANDLE;
if (ctl->backend == nullptr) return Err::Pad::FATAL;
ctl->backend->setMotion(bEnable);
return Ok;
}

return Err::Pad::INVALID_ARG;
return Err::Pad::INVALID_HANDLE;
}

EXPORT SYSV_ABI int scePadSetTiltCorrectionState(int32_t handle, bool bEnable) {
Expand All @@ -211,7 +212,9 @@ EXPORT SYSV_ABI int scePadSetTiltCorrectionState(int32_t handle, bool bEnable) {
return Ok;
}

if (getController(handle) == nullptr) return Err::Pad::INVALID_ARG;
std::unique_lock const lock(getData()->m_mutexInt);
if (getController(handle) == nullptr) return Err::Pad::INVALID_HANDLE;

return Ok;
}

Expand All @@ -222,7 +225,9 @@ EXPORT SYSV_ABI int scePadSetAngularVelocityDeadbandState(int32_t handle, bool b
return Ok;
}

if (getController(handle) == nullptr) return Err::Pad::INVALID_ARG;
std::unique_lock const lock(getData()->m_mutexInt);
if (getController(handle) == nullptr) return Err::Pad::INVALID_HANDLE;

return Ok;
}

Expand All @@ -233,13 +238,15 @@ EXPORT SYSV_ABI int scePadResetOrientation(int32_t handle) {
return Ok;
}

std::unique_lock const lock(getData()->m_mutexInt);

if (auto ctl = getController(handle)) {
if (ctl->backend == nullptr) return Err::Pad::INVALID_HANDLE;
if (ctl->backend == nullptr) return Err::Pad::FATAL;
ctl->backend->resetOrientation();
return Ok;
}

return Err::Pad::INVALID_ARG;
return Err::Pad::INVALID_HANDLE;
}

EXPORT SYSV_ABI int scePadSetVibration(int32_t handle, const ScePadVibrationParam* pParam) {
Expand All @@ -251,12 +258,14 @@ EXPORT SYSV_ABI int scePadSetVibration(int32_t handle, const ScePadVibrationPara
return Ok;
}

std::unique_lock const lock(getData()->m_mutexInt);

if (auto ctl = getController(handle)) {
if (ctl->backend == nullptr) return Err::Pad::INVALID_HANDLE;
if (ctl->backend == nullptr) return Err::Pad::FATAL;
return ctl->backend->setRumble(pParam) ? Ok : Err::Pad::INVALID_ARG;
}

return Err::Pad::INVALID_ARG;
return Err::Pad::INVALID_HANDLE;
}

EXPORT SYSV_ABI int scePadSetLightBar(int32_t handle, const ScePadColor* pParam) {
Expand All @@ -268,12 +277,14 @@ EXPORT SYSV_ABI int scePadSetLightBar(int32_t handle, const ScePadColor* pParam)
return Ok;
}

std::unique_lock const lock(getData()->m_mutexInt);

if (auto ctl = getController(handle)) {
if (ctl->backend == nullptr) return Err::Pad::INVALID_HANDLE;
if (ctl->backend == nullptr) return Err::Pad::FATAL;
return ctl->backend->setLED(pParam) ? Ok : Err::Pad::INVALID_LIGHTBAR_SETTING;
}

return Err::Pad::INVALID_ARG;
return Err::Pad::INVALID_HANDLE;
}

EXPORT SYSV_ABI int scePadResetLightBar(int32_t handle) {
Expand All @@ -283,12 +294,14 @@ EXPORT SYSV_ABI int scePadResetLightBar(int32_t handle) {
return Ok;
}

std::unique_lock const lock(getData()->m_mutexInt);

if (auto ctl = getController(handle)) {
if (ctl->backend == nullptr) return Err::Pad::INVALID_HANDLE;
if (ctl->backend == nullptr) return Err::Pad::FATAL;
return ctl->backend->resetLED() ? Ok : Err::Pad::INVALID_LIGHTBAR_SETTING;
}

return Err::Pad::INVALID_ARG;
return Err::Pad::INVALID_HANDLE;
}

EXPORT SYSV_ABI int scePadGetControllerInformation(int32_t handle, ScePadControllerInformation* pInfo) {
Expand All @@ -298,65 +311,46 @@ EXPORT SYSV_ABI int scePadGetControllerInformation(int32_t handle, ScePadControl
if (handle > PSMOVE_DUMMY_HANDLE_RANGE) {
LOG_ERR(L"todo Handle PlayStation Move info");
pInfo->deviceClass = ScePadDeviceClass::STANDARD;
pInfo->connectionType = (uint8_t)PadPortType::SPECIAL;
pInfo->connectionType = (uint8_t)ScePadPortType::SPECIAL;
pInfo->connected = false;
return Ok;
}

std::unique_lock const lock(getData()->m_mutexInt);

if (auto ctl = getController(handle)) {
std::unique_lock const lock(getData()->m_mutexInt);

// Not connected -> defaults
if (ctl->userId < 0) {
pInfo->touchPadInfo.pixelDensity = 1.f;
pInfo->touchPadInfo.resolution.x = 1920;
pInfo->touchPadInfo.resolution.y = 950;

pInfo->stickInfo.deadZoneLeft = 2; // todo make config
pInfo->stickInfo.deadZoneRight = 2; // todo make config
pInfo->connectionType = (uint8_t)PadPortType::STANDARD;
pInfo->connectedCount = ctl->backend ? ctl->backend->getConnectionsCount() : 0;
pInfo->connected = false;
pInfo->deviceClass = ScePadDeviceClass::STANDARD;
if (ctl->backend == nullptr) return Err::Pad::FATAL;

if (ctl->backend->readControllerInfo(*pInfo)) {
LOG_DEBUG(L"handle:%d connected:%d", handle, pInfo->connected);
return Ok;
}
// -

pInfo->connectionType = ctl->backend->getPortType();
pInfo->connectedCount = ctl->backend->getConnectionsCount();
pInfo->connected = ctl->backend->isConnected();
pInfo->deviceClass = ctl->backend->getClass();

pInfo->touchPadInfo.pixelDensity = 44.86f;
pInfo->touchPadInfo.resolution.x = 1920;
pInfo->touchPadInfo.resolution.y = 943;

pInfo->stickInfo.deadZoneLeft = 2; // todo make config
pInfo->stickInfo.deadZoneRight = 2; // todo make config

LOG_DEBUG(L"handle:%d connected:%d", handle, pInfo->connected);
return Ok;
return Err::Pad::FATAL;
}

return Err::Pad::INVALID_ARG;
return Err::Pad::INVALID_HANDLE;
}

EXPORT SYSV_ABI int scePadGetExtControllerInformation(int32_t handle, ScePadExtControllerInformation* pInfo) {
if (getController(handle) == nullptr) return Err::Pad::INVALID_ARG;
std::unique_lock const lock(getData()->m_mutexInt);
if (getController(handle) == nullptr) return Err::Pad::INVALID_HANDLE;
LOG_USE_MODULE(libScePad);
LOG_DEBUG(L"todo %S", __FUNCTION__);
return Ok;
}

EXPORT SYSV_ABI int scePadDeviceClassParseData(int32_t handle, const ScePadData* pData, ScePadDeviceClassData* pDeviceClassData) {
if (getController(handle) == nullptr) return Err::Pad::INVALID_ARG;
std::unique_lock const lock(getData()->m_mutexInt);
if (getController(handle) == nullptr) return Err::Pad::INVALID_HANDLE;
LOG_USE_MODULE(libScePad);
LOG_DEBUG(L"todo %S", __FUNCTION__);
return Ok;
}

EXPORT SYSV_ABI int scePadDeviceClassGetExtendedInformation(int32_t handle, ScePadDeviceClassExtendedInformation* pExtInfo) {
if (getController(handle) == nullptr) return Err::Pad::INVALID_ARG;
std::unique_lock const lock(getData()->m_mutexInt);
if (getController(handle) == nullptr) return Err::Pad::INVALID_HANDLE;
LOG_USE_MODULE(libScePad);
LOG_DEBUG(L"todo %S", __FUNCTION__);
return Ok;
Expand All @@ -365,6 +359,8 @@ EXPORT SYSV_ABI int scePadDeviceClassGetExtendedInformation(int32_t handle, SceP
EXPORT SYSV_ABI void scePadTerminate() {
auto pData = getData();

std::unique_lock const lock(pData->m_mutexInt);

for (int i = 0; i < MAX_CONTROLLERS_COUNT; i++) {
if (auto backend = pData->controller[i].backend.get()) backend->close();
}
Expand Down
45 changes: 24 additions & 21 deletions modules/external/libScePad/icontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ class IController {
CLASS_NO_MOVE(IController);

protected:
ControllerType const m_type;
ControllerState m_state = ControllerState::Disconnected;
ScePadDeviceClass m_class = ScePadDeviceClass::STANDARD;
PadPortType m_port = PadPortType::STANDARD;
uint32_t m_userId;
uint8_t m_connectCount;
ScePadColor m_lastColor = {0x00, 0x00, 0xFF};
char m_guid[33];
char m_name[33];
ControllerType const m_type = ControllerType::Unknown;
ScePadPortType const m_port = ScePadPortType::STANDARD;
ScePadDeviceClass const m_class = ScePadDeviceClass::STANDARD;
ControllerState m_state = ControllerState::Disconnected;
uint32_t const m_userId = -1;
uint8_t m_connectCount = 0;
ScePadStickInformation m_stickInfo = {2, 2}; // todo make config
ScePadTouchPadInformation m_touchInfo = {44.86f, {1920, 950}};
ScePadColor m_lastColor = {0x00, 0x00, 0xFF};
char m_guid[33];
char m_name[33];

IController(ControllerType type, uint32_t userid): m_type(type), m_userId(userid) {
::memset(m_guid, '0', sizeof(m_guid));
Expand All @@ -42,23 +44,24 @@ class IController {
public:
virtual ~IController() = default;

auto getUserID() const { return m_userId; }

auto getType() const { return m_type; }

auto getClass() const { return m_class; }

auto getPortType() const { return (uint8_t)m_port; }

auto getState() const { return m_state; }

auto getName() const { return m_name; }

auto getGUID() const { return m_guid; }

auto getConnectionsCount() const { return m_connectCount; }
auto isConnected() { return m_state == ControllerState::Connected || reconnect(); }

auto readControllerInfo(ScePadControllerInformation& pInfo) {
pInfo = {
.touchPadInfo = m_touchInfo,
.stickInfo = m_stickInfo,
.connectionType = (uint8_t)m_port,
.connectedCount = m_connectCount,
.connected = isConnected(),
.deviceClass = m_class,
};

auto isConnected() const { return m_state == ControllerState::Connected; }
return true;
}

virtual bool reconnect() = 0;
virtual void close() = 0;
Expand Down
Loading

0 comments on commit 7959f50

Please sign in to comment.