Skip to content

Commit

Permalink
Merge pull request #18251 from hrydgard/remove-extra-event-filtering
Browse files Browse the repository at this point in the history
Control: Remove the axis event dupe filtering, batch events deeper
  • Loading branch information
hrydgard committed Sep 29, 2023
2 parents dea038a + ee93e4a commit 8eefb9f
Show file tree
Hide file tree
Showing 12 changed files with 52 additions and 63 deletions.
22 changes: 2 additions & 20 deletions Common/UI/Screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,9 @@ bool ScreenManager::key(const KeyInput &key) {
return result;
}

void ScreenManager::axis(const AxisInput &axis) {
void ScreenManager::axis(const AxisInput *axes, size_t count) {
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;
}
lastAxis_[key] = value;

// Send center axis to every screen layer.
if (axis.value == 0) {
for (auto &layer : stack_) {
layer.screen->UnsyncAxis(axis);
}
} else if (!stack_.empty()) {
stack_.back().screen->UnsyncAxis(axis);
}
stack_.back().screen->UnsyncAxis(axes, count);
}

void ScreenManager::deviceLost() {
Expand Down
4 changes: 2 additions & 2 deletions Common/UI/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Screen {
virtual bool UnsyncTouch(const TouchInput &touch) = 0;
// Return value of UnsyncKey is used to not block certain system keys like volume when unhandled, on Android.
virtual bool UnsyncKey(const KeyInput &touch) = 0;
virtual void UnsyncAxis(const AxisInput &touch) = 0;
virtual void UnsyncAxis(const AxisInput *axes, size_t count) = 0;

virtual void RecreateViews() {}

Expand Down Expand Up @@ -135,7 +135,7 @@ class ScreenManager {
// Instant touch, separate from the update() mechanism.
void touch(const TouchInput &touch);
bool key(const KeyInput &key);
void axis(const AxisInput &touch);
void axis(const AxisInput *axes, size_t count);

// Generic facility for gross hacks :P
void sendMessage(const char *msg, const char *value);
Expand Down
8 changes: 5 additions & 3 deletions Common/UI/UIScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ bool UIScreen::UnsyncTouch(const TouchInput &touch) {
return false;
}

void UIScreen::UnsyncAxis(const AxisInput &axis) {
void UIScreen::UnsyncAxis(const AxisInput *axes, size_t count) {
QueuedEvent ev{};
ev.type = QueuedEventType::AXIS;
ev.axis = axis;
std::lock_guard<std::mutex> guard(eventQueueLock_);
eventQueue_.push_back(ev);
for (size_t i = 0; i < count; i++) {
ev.axis = axes[i];
eventQueue_.push_back(ev);
}
}

bool UIScreen::UnsyncKey(const KeyInput &key) {
Expand Down
2 changes: 1 addition & 1 deletion Common/UI/UIScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class UIScreen : public Screen {

bool UnsyncTouch(const TouchInput &touch) override;
bool UnsyncKey(const KeyInput &key) override;
void UnsyncAxis(const AxisInput &axis) override;
void UnsyncAxis(const AxisInput *axes, size_t count) override;

TouchInput transformTouch(const TouchInput &touch) override;

Expand Down
11 changes: 7 additions & 4 deletions Common/VR/PPSSPPVR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,14 @@ void UpdateVRInput(bool haptics, float dp_xscale, float dp_yscale) {
}
}

bool UpdateVRAxis(const AxisInput &axis) {
if (pspAxis.find(axis.deviceId) == pspAxis.end()) {
pspAxis[axis.deviceId] = std::map<int, float>();
bool UpdateVRAxis(const AxisInput *axes, size_t count) {
for (size_t i = 0; i < count; i++) {
const AxisInput &axis = axes[i];
if (pspAxis.find(axis.deviceId) == pspAxis.end()) {
pspAxis[axis.deviceId] = std::map<int, float>();
}
pspAxis[axis.deviceId][axis.axisId] = axis.value;
}
pspAxis[axis.deviceId][axis.axisId] = axis.value;
return !pspKeys[VIRTKEY_VR_CAMERA_ADJUST];
}

Expand Down
2 changes: 1 addition & 1 deletion Common/VR/PPSSPPVR.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void SetVRCallbacks(void(*axis)(const AxisInput *axis, size_t count), bool(*key)
// VR input integration
void SetVRAppMode(VRAppMode mode);
void UpdateVRInput(bool haptics, float dp_xscale, float dp_yscale);
bool UpdateVRAxis(const AxisInput &axis);
bool UpdateVRAxis(const AxisInput *axes, size_t count);
bool UpdateVRKeys(const KeyInput &key);

// VR games compatibility
Expand Down
41 changes: 22 additions & 19 deletions Core/ControlMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,28 +476,31 @@ void ControlMapper::ToggleSwapAxes() {
UpdateAnalogOutput(1);
}

void ControlMapper::Axis(const AxisInput &axis) {
void ControlMapper::Axis(const AxisInput *axes, size_t count) {
double now = time_now_d();

std::lock_guard<std::mutex> guard(mutex_);
size_t deviceIndex = (size_t)axis.deviceId; // this'll wrap around ANY (-1) to max, which will eliminate it on the next line, if such an event appears by mistake.
if (deviceIndex < (size_t)DEVICE_ID_COUNT) {
deviceTimestamps_[deviceIndex] = now;
}
if (axis.value >= 0.0f) {
InputMapping mapping(axis.deviceId, axis.axisId, 1);
InputMapping opposite(axis.deviceId, axis.axisId, -1);
curInput_[mapping] = { axis.value, now };
curInput_[opposite] = { 0.0f, now };
UpdatePSPState(mapping, now);
UpdatePSPState(opposite, now);
} else if (axis.value < 0.0f) {
InputMapping mapping(axis.deviceId, axis.axisId, -1);
InputMapping opposite(axis.deviceId, axis.axisId, 1);
curInput_[mapping] = { -axis.value, now };
curInput_[opposite] = { 0.0f, now };
UpdatePSPState(mapping, now);
UpdatePSPState(opposite, now);
for (size_t i = 0; i < count; i++) {
const AxisInput &axis = axes[i];
size_t deviceIndex = (size_t)axis.deviceId; // this wraps -1 up high, so will get rejected on the next line.
if (deviceIndex < (size_t)DEVICE_ID_COUNT) {
deviceTimestamps_[deviceIndex] = now;
}
if (axis.value >= 0.0f) {
InputMapping mapping(axis.deviceId, axis.axisId, 1);
InputMapping opposite(axis.deviceId, axis.axisId, -1);
curInput_[mapping] = { axis.value, now };
curInput_[opposite] = { 0.0f, now };
UpdatePSPState(mapping, now);
UpdatePSPState(opposite, now);
} else if (axis.value < 0.0f) {
InputMapping mapping(axis.deviceId, axis.axisId, -1);
InputMapping opposite(axis.deviceId, axis.axisId, 1);
curInput_[mapping] = { -axis.value, now };
curInput_[opposite] = { 0.0f, now };
UpdatePSPState(mapping, now);
UpdatePSPState(opposite, now);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion Core/ControlMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ControlMapper {
// Inputs to the table-based mapping
// These functions are free-threaded.
bool Key(const KeyInput &key, bool *pauseTrigger);
void Axis(const AxisInput &axis);
void Axis(const AxisInput *axes, size_t count);

// Required callbacks.
// TODO: These are so many now that a virtual interface might be more appropriate..
Expand Down Expand Up @@ -76,6 +76,8 @@ class ControlMapper {
bool swapAxes_ = false;

// Protects basically all the state.
// TODO: Maybe we should piggyback on the screenmanager mutex - it's always locked
// when events come in here.
std::mutex mutex_;

std::map<InputMapping, InputSample> curInput_;
Expand Down
2 changes: 1 addition & 1 deletion UI/ControlMappingScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ void AnalogSetupScreen::axis(const AxisInput &axis) {
// UIScreen::axis(axis);

// Instead we just send the input directly to the mapper, that we'll visualize.
mapper_.Axis(axis);
mapper_.Axis(&axis, 1);
}

void AnalogSetupScreen::CreateViews() {
Expand Down
4 changes: 2 additions & 2 deletions UI/EmuScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -865,9 +865,9 @@ bool EmuScreen::key(const KeyInput &key) {
return retval;
}

void EmuScreen::UnsyncAxis(const AxisInput &axis) {
void EmuScreen::UnsyncAxis(const AxisInput *axes, size_t count) {
System_Notify(SystemNotification::ACTIVITY);
return controlMapper_.Axis(axis);
return controlMapper_.Axis(axes, count);
}

class GameInfoBGView : public UI::InertView {
Expand Down
2 changes: 1 addition & 1 deletion UI/EmuScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class EmuScreen : public UIScreen {
// to get minimal latency and full control. We forward to UIScreen when needed.
bool UnsyncTouch(const TouchInput &touch) override;
bool UnsyncKey(const KeyInput &key) override;
void UnsyncAxis(const AxisInput &axis) override;
void UnsyncAxis(const AxisInput *axes, size_t count) override;

// We also need to do some special handling of queued UI events to handle closing the chat window.
bool key(const KeyInput &key) override;
Expand Down
13 changes: 5 additions & 8 deletions UI/NativeApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,9 +1319,9 @@ bool NativeKey(const KeyInput &key) {
return retval;
}

static void ProcessOneAxisEvent(const AxisInput &axis) {
void NativeAxis(const AxisInput *axes, size_t count) {
// VR actions
if (IsVREnabled() && !UpdateVRAxis(axis)) {
if (IsVREnabled() && !UpdateVRAxis(axes, count)) {
return;
}

Expand All @@ -1330,14 +1330,11 @@ static void ProcessOneAxisEvent(const AxisInput &axis) {
return;
}

// only do special handling of tilt events if tilt is enabled.
HLEPlugins::PluginDataAxis[axis.axisId] = axis.value;
g_screenManager->axis(axis);
}
g_screenManager->axis(axes, count);

void NativeAxis(const AxisInput *axes, size_t count) {
for (size_t i = 0; i < count; i++) {
ProcessOneAxisEvent(axes[i]);
const AxisInput &axis = axes[i];
HLEPlugins::PluginDataAxis[axis.axisId] = axis.value;
}
}

Expand Down

0 comments on commit 8eefb9f

Please sign in to comment.