Skip to content

Commit

Permalink
UI: Ignore duplicate axis events.
Browse files Browse the repository at this point in the history
We already ignore these on Windows, so this makes other platforms follow
the same logic.
  • Loading branch information
unknownbrackets committed May 23, 2021
1 parent e67c659 commit e1e5a8b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 38 deletions.
10 changes: 10 additions & 0 deletions UI/EmuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,16 @@ void EmuScreen::pspKey(int pspKeyCode, int flags) {
bool EmuScreen::axis(const AxisInput &axis) {
Core_NotifyActivity();

// Ignore duplicate values to prevent axis values overwriting each other.
uint64_t key = ((uint64_t)axis.axisId << 32) | axis.deviceId;
// Center value far from zero just to ensure we send the first zero.
// PSP games can't see higher resolution than this.
int value = 128 + ceilf(axis.value * 127.5f + 127.5f);
if (lastAxis_[key] == value) {
return false;
}
lastAxis_[key] = value;

if (axis.value > 0) {
processAxis(axis, 1);
return true;
Expand Down
5 changes: 4 additions & 1 deletion UI/EmuScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

#pragma once

#include <cstdint>
#include <list>
#include <string>
#include <unordered_map>
#include <vector>
#include <list>

#include "Common/File/Path.h"
#include "Common/Input/KeyCodes.h"
Expand Down Expand Up @@ -113,4 +115,5 @@ class EmuScreen : public UIScreen {

bool autoRotatingAnalogCW_ = false;
bool autoRotatingAnalogCCW_ = false;
std::unordered_map<int64_t, int> lastAxis_;
};
3 changes: 0 additions & 3 deletions Windows/DinputDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ DinputDevice::~DinputDevice() {
}

void SendNativeAxis(int deviceId, int value, int &lastValue, int axisId) {
if (value == lastValue)
return;

AxisInput axis;
axis.deviceId = deviceId;
axis.axisId = axisId;
Expand Down
48 changes: 14 additions & 34 deletions Windows/XinputDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,54 +282,34 @@ void XinputDevice::UpdatePad(int pad, const XINPUT_STATE &state, XINPUT_VIBRATIO
const float STICK_INV_DEADZONE = g_Config.fXInputAnalogInverseDeadzone;
const float STICK_SENSITIVITY = g_Config.fXInputAnalogSensitivity;

AxisInput axis;
axis.deviceId = DEVICE_ID_X360_0 + pad;
auto sendAxis = [&](AndroidJoystickAxis axisId, float value) {
axis.axisId = axisId;
axis.value = value;
NativeAxis(axis);
};

if (NormalizedDeadzoneDiffers(prevState[pad].Gamepad.sThumbLX, prevState[pad].Gamepad.sThumbLY, state.Gamepad.sThumbLX, state.Gamepad.sThumbLY, STICK_DEADZONE)) {
Stick left = NormalizedDeadzoneFilter(state.Gamepad.sThumbLX, state.Gamepad.sThumbLY, STICK_DEADZONE, STICK_INV_MODE, STICK_INV_DEADZONE, STICK_SENSITIVITY);

AxisInput axis;
axis.deviceId = DEVICE_ID_X360_0 + pad;
axis.axisId = JOYSTICK_AXIS_X;
axis.value = left.x;
if (prevState[pad].Gamepad.sThumbLX != state.Gamepad.sThumbLX) {
NativeAxis(axis);
}
axis.axisId = JOYSTICK_AXIS_Y;
axis.value = left.y;
if (prevState[pad].Gamepad.sThumbLY != state.Gamepad.sThumbLY) {
NativeAxis(axis);
}
sendAxis(JOYSTICK_AXIS_X, left.x);
sendAxis(JOYSTICK_AXIS_Y, left.y);
}

if (NormalizedDeadzoneDiffers(prevState[pad].Gamepad.sThumbRX, prevState[pad].Gamepad.sThumbRY, state.Gamepad.sThumbRX, state.Gamepad.sThumbRY, STICK_DEADZONE)) {
Stick right = NormalizedDeadzoneFilter(state.Gamepad.sThumbRX, state.Gamepad.sThumbRY, STICK_DEADZONE, STICK_INV_MODE, STICK_INV_DEADZONE, STICK_SENSITIVITY);

AxisInput axis;
axis.deviceId = DEVICE_ID_X360_0 + pad;
axis.axisId = JOYSTICK_AXIS_Z;
axis.value = right.x;
if (prevState[pad].Gamepad.sThumbRX != state.Gamepad.sThumbRX) {
NativeAxis(axis);
}
axis.axisId = JOYSTICK_AXIS_RZ;
axis.value = right.y;
if (prevState[pad].Gamepad.sThumbRY != state.Gamepad.sThumbRY) {
NativeAxis(axis);
}
sendAxis(JOYSTICK_AXIS_Z, right.x);
sendAxis(JOYSTICK_AXIS_RZ, right.y);
}

if (NormalizedDeadzoneDiffers(prevState[pad].Gamepad.bLeftTrigger, state.Gamepad.bLeftTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD)) {
AxisInput axis;
axis.deviceId = DEVICE_ID_X360_0 + pad;
axis.axisId = JOYSTICK_AXIS_LTRIGGER;
axis.value = (float)state.Gamepad.bLeftTrigger / 255.0f;
NativeAxis(axis);
sendAxis(JOYSTICK_AXIS_LTRIGGER, (float)state.Gamepad.bLeftTrigger / 255.0f);
}

if (NormalizedDeadzoneDiffers(prevState[pad].Gamepad.bRightTrigger, state.Gamepad.bRightTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD)) {
AxisInput axis;
axis.deviceId = DEVICE_ID_X360_0 + pad;
axis.axisId = JOYSTICK_AXIS_RTRIGGER;
axis.value = (float)state.Gamepad.bRightTrigger / 255.0f;
NativeAxis(axis);
sendAxis(JOYSTICK_AXIS_RTRIGGER, (float)state.Gamepad.bRightTrigger / 255.0f);
}

prevState[pad] = state;
Expand Down

0 comments on commit e1e5a8b

Please sign in to comment.