Skip to content

Commit

Permalink
Merge pull request #14479 from unknownbrackets/ctrl-analog
Browse files Browse the repository at this point in the history
UI: Ignore duplicate axis events
  • Loading branch information
hrydgard authored May 24, 2021
2 parents e67c659 + b0de7ee commit dad7f20
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 39 deletions.
11 changes: 11 additions & 0 deletions Common/UI/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ bool ScreenManager::key(const KeyInput &key) {

bool ScreenManager::axis(const AxisInput &axis) {
std::lock_guard<std::recursive_mutex> guard(inputLock_);

// 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;

bool result = false;
// Send center axis to every screen layer.
if (axis.value == 0) {
Expand Down
6 changes: 5 additions & 1 deletion Common/UI/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

#pragma once

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

#include "Common/Common.h"
#include "Common/Input/InputState.h"
Expand Down Expand Up @@ -165,4 +167,6 @@ class ScreenManager {
// Used for options, in-game menus and other things you expect to be able to back out from onto something.
std::vector<Layer> stack_;
std::vector<Layer> nextStack_;

std::unordered_map<int64_t, int> lastAxis_;
};
2 changes: 1 addition & 1 deletion UI/EmuScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

#pragma once

#include <list>
#include <string>
#include <vector>
#include <list>

#include "Common/File/Path.h"
#include "Common/Input/KeyCodes.h"
Expand Down
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 dad7f20

Please sign in to comment.