XInputDevice: Dirty-track axes, for easier axis event debugging

This commit is contained in:
Henrik Rydgård 2023-07-06 15:47:36 +02:00
parent 0151d877b3
commit 77dff18701
2 changed files with 23 additions and 19 deletions

View file

@ -161,7 +161,7 @@ XinputDevice::~XinputDevice() {
} }
struct Stick { struct Stick {
Stick (float x_, float y_, float scale) : x(x_ * scale), y(y_ * scale) {} Stick(float x_, float y_, float scale) : x(x_ * scale), y(y_ * scale) {}
float x; float x;
float y; float y;
}; };
@ -202,9 +202,8 @@ int XinputDevice::UpdateState() {
} }
void XinputDevice::UpdatePad(int pad, const XINPUT_STATE &state, XINPUT_VIBRATION &vibration) { void XinputDevice::UpdatePad(int pad, const XINPUT_STATE &state, XINPUT_VIBRATION &vibration) {
static bool notified[XUSER_MAX_COUNT]{}; if (!notified_[pad]) {
if (!notified[pad]) { notified_[pad] = true;
notified[pad] = true;
#if !PPSSPP_PLATFORM(UWP) #if !PPSSPP_PLATFORM(UWP)
XINPUT_CAPABILITIES_EX caps{}; XINPUT_CAPABILITIES_EX caps{};
if (PPSSPP_XInputGetCapabilitiesEx != nullptr && PPSSPP_XInputGetCapabilitiesEx(1, pad, 0, &caps) == ERROR_SUCCESS) { if (PPSSPP_XInputGetCapabilitiesEx != nullptr && PPSSPP_XInputGetCapabilitiesEx(1, pad, 0, &caps) == ERROR_SUCCESS) {
@ -221,23 +220,26 @@ void XinputDevice::UpdatePad(int pad, const XINPUT_STATE &state, XINPUT_VIBRATIO
AxisInput axis; AxisInput axis;
axis.deviceId = (InputDeviceID)(DEVICE_ID_XINPUT_0 + pad); axis.deviceId = (InputDeviceID)(DEVICE_ID_XINPUT_0 + pad);
auto sendAxis = [&](InputAxis axisId, float value) { auto sendAxis = [&](InputAxis axisId, float value, int axisIndex) {
axis.axisId = axisId; if (value != prevAxisValue_[pad][axisIndex]) {
axis.value = value; prevAxisValue_[pad][axisIndex] = value;
NativeAxis(axis); axis.axisId = axisId;
axis.value = value;
NativeAxis(axis);
}
}; };
sendAxis(JOYSTICK_AXIS_X, (float)state.Gamepad.sThumbLX / 32767.0f); sendAxis(JOYSTICK_AXIS_X, (float)state.Gamepad.sThumbLX / 32767.0f, 0);
sendAxis(JOYSTICK_AXIS_Y, (float)state.Gamepad.sThumbLY / 32767.0f); sendAxis(JOYSTICK_AXIS_Y, (float)state.Gamepad.sThumbLY / 32767.0f, 1);
sendAxis(JOYSTICK_AXIS_Z, (float)state.Gamepad.sThumbRX / 32767.0f); sendAxis(JOYSTICK_AXIS_Z, (float)state.Gamepad.sThumbRX / 32767.0f, 2);
sendAxis(JOYSTICK_AXIS_RZ, (float)state.Gamepad.sThumbRY / 32767.0f); sendAxis(JOYSTICK_AXIS_RZ, (float)state.Gamepad.sThumbRY / 32767.0f, 3);
if (NormalizedDeadzoneDiffers(prevState[pad].Gamepad.bLeftTrigger, state.Gamepad.bLeftTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD)) { if (NormalizedDeadzoneDiffers(prevState[pad].Gamepad.bLeftTrigger, state.Gamepad.bLeftTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD)) {
sendAxis(JOYSTICK_AXIS_LTRIGGER, (float)state.Gamepad.bLeftTrigger / 255.0f); sendAxis(JOYSTICK_AXIS_LTRIGGER, (float)state.Gamepad.bLeftTrigger / 255.0f, 4);
} }
if (NormalizedDeadzoneDiffers(prevState[pad].Gamepad.bRightTrigger, state.Gamepad.bRightTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD)) { if (NormalizedDeadzoneDiffers(prevState[pad].Gamepad.bRightTrigger, state.Gamepad.bRightTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD)) {
sendAxis(JOYSTICK_AXIS_RTRIGGER, (float)state.Gamepad.bRightTrigger / 255.0f); sendAxis(JOYSTICK_AXIS_RTRIGGER, (float)state.Gamepad.bRightTrigger / 255.0f, 5);
} }
prevState[pad] = state; prevState[pad] = state;
@ -245,11 +247,11 @@ void XinputDevice::UpdatePad(int pad, const XINPUT_STATE &state, XINPUT_VIBRATIO
} }
void XinputDevice::ApplyButtons(int pad, const XINPUT_STATE &state) { void XinputDevice::ApplyButtons(int pad, const XINPUT_STATE &state) {
u32 buttons = state.Gamepad.wButtons; const u32 buttons = state.Gamepad.wButtons;
u32 downMask = buttons & (~prevButtons[pad]); const u32 downMask = buttons & (~prevButtons_[pad]);
u32 upMask = (~buttons) & prevButtons[pad]; const u32 upMask = (~buttons) & prevButtons_[pad];
prevButtons[pad] = buttons; prevButtons_[pad] = buttons;
for (int i = 0; i < xinput_ctrl_map_size; i++) { for (int i = 0; i < xinput_ctrl_map_size; i++) {
if (downMask & xinput_ctrl_map[i].from) { if (downMask & xinput_ctrl_map[i].from) {

View file

@ -18,5 +18,7 @@ private:
XINPUT_STATE prevState[4]{}; XINPUT_STATE prevState[4]{};
XINPUT_VIBRATION prevVibration[4]{}; XINPUT_VIBRATION prevVibration[4]{};
double prevVibrationTime = 0.0; double prevVibrationTime = 0.0;
u32 prevButtons[4]{}; float prevAxisValue_[4][6]{};
bool notified_[XUSER_MAX_COUNT]{};
u32 prevButtons_[4]{};
}; };