Files
AnyPS5/core/libs/tests/GuestPad.cpp
T
alfalfa-starandClaude Opus 5.5 b6176624aa fix(pad): return INVALID_HANDLE from scePadReadState for an unknown handle
PPSA14632 calls scePadReadState (eboot 0x4593a8) for the open pad and
then for a second pad slot it never opened, whose handle is 0. The
title skips only negative handles and ignores the result, but
scePadReadState threw for any handle other than the open one. It now
returns PAD_ERROR_INVALID_HANDLE (0x80920003), as the other pad
functions do and as shadPS4's scePadRead does for an unknown handle;
scePadRead passes it on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 14:43:00 +02:00

94 lines
3.8 KiB
C++

#include "prx/libScePad/include/Pad.hpp"
#include "prx/libScePad/include/PadInputTypes.hpp"
#include "prx/libScePad/include/PadState.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include <chrono>
#include <cstdlib>
#include <thread>
extern "C" {
int APS5_VABI scePadOpen_nid_postfix(int, int, int, const void*);
int APS5_VABI scePadClose_nid_postfix(int);
int APS5_VABI scePadGetHandle(int, int, int);
int APS5_VABI scePadSetVibrationTriggerEffectWeakWhileEmbeddedMicInUse(bool);
int APS5_VABI scePadInit_nid_postfix(void);
int APS5_VABI scePadRead_nid_postfix(int, PadData*, int);
int APS5_VABI scePadReadState(int, PadData*);
int APS5_VABI scePadSetTiltCorrectionState(int, bool);
int APS5_VABI scePadResetOrientation(int);
int APS5_VABI scePadSetAngularVelocityDeadbandState(int, bool);
}
static void Require(bool value) { if (!value) std::abort(); }
static float SettleOrientationW() {
float w = 1.0f;
for (int i = 0; i < 10; ++i) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
w = Pad::ReadState().orientation_w;
}
return w;
}
static void CheckTiltCorrection(int handle) {
Require(scePadInit_nid_postfix() == 0);
PadInputState tilted;
tilted.hasMotion = true;
tilted.accel = {9.80665f, 0.0f, 0.0f};
PadPublishInput_nid_postfix(tilted);
Require(scePadSetTiltCorrectionState(handle + 1, false) == PAD_ERROR_INVALID_HANDLE);
Require(scePadSetTiltCorrectionState(handle, false) == PAD_OK);
Require(scePadResetOrientation(handle) == PAD_OK);
Require(SettleOrientationW() == 1.0f);
Require(scePadSetTiltCorrectionState(handle, true) == PAD_OK);
Require(SettleOrientationW() < 0.999f);
PadPublishInput_nid_postfix(PadInputState{});
}
static void CheckTouchContact() {
PadInputState touch;
touch.buttons = static_cast<std::uint32_t>(Pad::PadButton::TouchPad);
touch.touch[0] = {true, 960, 471, 0};
PadPublishInput_nid_postfix(touch);
const auto data = Pad::ReadState();
Require((data.buttons & static_cast<std::uint32_t>(Pad::PadButton::TouchPad)) != 0);
Require(data.touch_data_touch_num == 1);
Require(data.touch_data_touch0_x == 960);
Require(data.touch_data_touch0_y == 471);
PadPublishInput_nid_postfix(PadInputState{});
Pad::ReadState();
}
static void CheckReadStateHandle(int handle) {
PadData data{};
Require(scePadReadState(0, &data) == PAD_ERROR_INVALID_HANDLE);
Require(scePadReadState(handle + 1, &data) == PAD_ERROR_INVALID_HANDLE);
Require(scePadRead_nid_postfix(0, &data, 1) == PAD_ERROR_INVALID_HANDLE);
Require(scePadReadState(handle, &data) == PAD_OK);
}
int main() {
constexpr int noHandle = static_cast<int>(0x80920008);
constexpr int user = 0x10000000;
Require(scePadGetHandle(user, 0, 0) == noHandle);
Require(scePadOpen_nid_postfix(user, 1, 0, nullptr) == PAD_ERROR_INVALID_ARG);
Require(scePadOpen_nid_postfix(user, 0, 1, nullptr) == PAD_ERROR_INVALID_ARG);
Require(scePadGetHandle(user, 0, 0) == noHandle);
const int handle = scePadOpen_nid_postfix(user, 0, 0, nullptr);
Require(handle > 0);
Require(scePadGetHandle(user, 0, 0) == handle);
Require(scePadGetHandle(user, 2, 0) == handle);
CheckTiltCorrection(handle);
CheckTouchContact();
CheckReadStateHandle(handle);
Require(scePadGetHandle(0xff, 16, 0) == handle);
Require(scePadGetHandle(user, 16, 0) == noHandle);
Require(scePadGetHandle(user, 0, 1) == noHandle);
Require(scePadClose_nid_postfix(handle) == 0);
Require(scePadGetHandle(user, 0, 0) == noHandle);
Require(scePadSetVibrationTriggerEffectWeakWhileEmbeddedMicInUse(true) == 0);
Require(scePadSetAngularVelocityDeadbandState(handle, false) == 0);
Require(scePadSetAngularVelocityDeadbandState(handle + 1, false) == PAD_ERROR_INVALID_HANDLE);
}