From 9d8c4a7802e8a2b96eb40da1b36bd91a263971e4 Mon Sep 17 00:00:00 2001
From: Narr the Reg <juangerman-13@hotmail.com>
Date: Tue, 23 Jan 2024 11:10:34 -0600
Subject: [PATCH] core: hid: Skip duplicated vibrations

---
 src/hid_core/frontend/emulated_controller.cpp | 12 ++++++++++--
 src/hid_core/frontend/emulated_controller.h   |  3 ++-
 src/hid_core/hid_types.h                      |  9 +++++++++
 3 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/src/hid_core/frontend/emulated_controller.cpp b/src/hid_core/frontend/emulated_controller.cpp
index 063f5b15a6..819460eb57 100644
--- a/src/hid_core/frontend/emulated_controller.cpp
+++ b/src/hid_core/frontend/emulated_controller.cpp
@@ -1245,7 +1245,12 @@ bool EmulatedController::SetVibration(DeviceIndex device_index, const VibrationV
         return false;
     }
 
-    last_vibration_value = vibration;
+    // Skip duplicated vibrations
+    if (last_vibration_value[index] == vibration) {
+        return Settings::values.vibration_enabled.GetValue();
+    }
+
+    last_vibration_value[index] = vibration;
 
     if (!Settings::values.vibration_enabled) {
         return false;
@@ -1276,7 +1281,10 @@ bool EmulatedController::SetVibration(DeviceIndex device_index, const VibrationV
 }
 
 VibrationValue EmulatedController::GetActualVibrationValue(DeviceIndex device_index) const {
-    return last_vibration_value;
+    if (device_index >= DeviceIndex::MaxDeviceIndex) {
+        return Core::HID::DEFAULT_VIBRATION_VALUE;
+    }
+    return last_vibration_value[static_cast<std::size_t>(device_index)];
 }
 
 bool EmulatedController::IsVibrationEnabled(std::size_t device_index) {
diff --git a/src/hid_core/frontend/emulated_controller.h b/src/hid_core/frontend/emulated_controller.h
index 168abe089d..701b38300a 100644
--- a/src/hid_core/frontend/emulated_controller.h
+++ b/src/hid_core/frontend/emulated_controller.h
@@ -581,7 +581,8 @@ private:
     f32 motion_sensitivity{Core::HID::MotionInput::IsAtRestStandard};
     u32 turbo_button_state{0};
     std::size_t nfc_handles{0};
-    VibrationValue last_vibration_value{DEFAULT_VIBRATION_VALUE};
+    std::array<VibrationValue, 2> last_vibration_value{DEFAULT_VIBRATION_VALUE,
+                                                       DEFAULT_VIBRATION_VALUE};
 
     // Temporary values to avoid doing changes while the controller is in configuring mode
     NpadStyleIndex tmp_npad_type{NpadStyleIndex::None};
diff --git a/src/hid_core/hid_types.h b/src/hid_core/hid_types.h
index 2c3f02f34c..a01292a702 100644
--- a/src/hid_core/hid_types.h
+++ b/src/hid_core/hid_types.h
@@ -639,6 +639,15 @@ struct VibrationValue {
     f32 low_frequency{};
     f32 high_amplitude{};
     f32 high_frequency{};
+    bool operator==(const VibrationValue& b) {
+        if (low_amplitude != b.low_amplitude || high_amplitude != b.high_amplitude) {
+            return false;
+        }
+        if (low_frequency != b.low_amplitude || high_frequency != b.high_frequency) {
+            return false;
+        }
+        return true;
+    }
 };
 static_assert(sizeof(VibrationValue) == 0x10, "VibrationValue has incorrect size.");