From 77dff18701a6872a1d8d62a735e3d90caee15431 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Thu, 6 Jul 2023 15:47:36 +0200 Subject: [PATCH] XInputDevice: Dirty-track axes, for easier axis event debugging --- Windows/XinputDevice.cpp | 38 ++++++++++++++++++++------------------ Windows/XinputDevice.h | 4 +++- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Windows/XinputDevice.cpp b/Windows/XinputDevice.cpp index d8c3e0e898..b1cffa5bfe 100644 --- a/Windows/XinputDevice.cpp +++ b/Windows/XinputDevice.cpp @@ -161,7 +161,7 @@ XinputDevice::~XinputDevice() { } 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 y; }; @@ -202,9 +202,8 @@ int XinputDevice::UpdateState() { } void XinputDevice::UpdatePad(int pad, const XINPUT_STATE &state, XINPUT_VIBRATION &vibration) { - static bool notified[XUSER_MAX_COUNT]{}; - if (!notified[pad]) { - notified[pad] = true; + if (!notified_[pad]) { + notified_[pad] = true; #if !PPSSPP_PLATFORM(UWP) XINPUT_CAPABILITIES_EX caps{}; 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; axis.deviceId = (InputDeviceID)(DEVICE_ID_XINPUT_0 + pad); - auto sendAxis = [&](InputAxis axisId, float value) { - axis.axisId = axisId; - axis.value = value; - NativeAxis(axis); + auto sendAxis = [&](InputAxis axisId, float value, int axisIndex) { + if (value != prevAxisValue_[pad][axisIndex]) { + prevAxisValue_[pad][axisIndex] = value; + axis.axisId = axisId; + axis.value = value; + NativeAxis(axis); + } }; - sendAxis(JOYSTICK_AXIS_X, (float)state.Gamepad.sThumbLX / 32767.0f); - sendAxis(JOYSTICK_AXIS_Y, (float)state.Gamepad.sThumbLY / 32767.0f); - sendAxis(JOYSTICK_AXIS_Z, (float)state.Gamepad.sThumbRX / 32767.0f); - sendAxis(JOYSTICK_AXIS_RZ, (float)state.Gamepad.sThumbRY / 32767.0f); + sendAxis(JOYSTICK_AXIS_X, (float)state.Gamepad.sThumbLX / 32767.0f, 0); + sendAxis(JOYSTICK_AXIS_Y, (float)state.Gamepad.sThumbLY / 32767.0f, 1); + sendAxis(JOYSTICK_AXIS_Z, (float)state.Gamepad.sThumbRX / 32767.0f, 2); + sendAxis(JOYSTICK_AXIS_RZ, (float)state.Gamepad.sThumbRY / 32767.0f, 3); 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)) { - sendAxis(JOYSTICK_AXIS_RTRIGGER, (float)state.Gamepad.bRightTrigger / 255.0f); + sendAxis(JOYSTICK_AXIS_RTRIGGER, (float)state.Gamepad.bRightTrigger / 255.0f, 5); } 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) { - u32 buttons = state.Gamepad.wButtons; + const u32 buttons = state.Gamepad.wButtons; - u32 downMask = buttons & (~prevButtons[pad]); - u32 upMask = (~buttons) & prevButtons[pad]; - prevButtons[pad] = buttons; + const u32 downMask = buttons & (~prevButtons_[pad]); + const u32 upMask = (~buttons) & prevButtons_[pad]; + prevButtons_[pad] = buttons; for (int i = 0; i < xinput_ctrl_map_size; i++) { if (downMask & xinput_ctrl_map[i].from) { diff --git a/Windows/XinputDevice.h b/Windows/XinputDevice.h index 93347a6722..858e3fc3ed 100644 --- a/Windows/XinputDevice.h +++ b/Windows/XinputDevice.h @@ -18,5 +18,7 @@ private: XINPUT_STATE prevState[4]{}; XINPUT_VIBRATION prevVibration[4]{}; double prevVibrationTime = 0.0; - u32 prevButtons[4]{}; + float prevAxisValue_[4][6]{}; + bool notified_[XUSER_MAX_COUNT]{}; + u32 prevButtons_[4]{}; };