From 1c08d532e059fab603facb43f758f37fe148c1fc Mon Sep 17 00:00:00 2001
From: Narr the Reg <juangerman-13@hotmail.com>
Date: Thu, 22 Dec 2022 20:47:51 -0600
Subject: [PATCH] core: hid: Fix input regressions

---
 src/core/hid/emulated_controller.cpp          | 55 ++++++++++++-------
 src/core/hid/emulated_controller.h            | 12 ++--
 src/core/hle/service/hid/controllers/npad.cpp |  1 +
 src/core/hle/service/hid/hidbus.cpp           | 24 ++++----
 src/input_common/helpers/joycon_driver.cpp    |  2 +-
 .../helpers/joycon_protocol/ringcon.cpp       |  3 -
 6 files changed, 56 insertions(+), 41 deletions(-)

diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp
index 1e4ec4add..1ed57f949 100644
--- a/src/core/hid/emulated_controller.cpp
+++ b/src/core/hid/emulated_controller.cpp
@@ -107,6 +107,8 @@ void EmulatedController::ReloadFromSettings() {
         .button = GetNpadColor(player.button_color_right),
     };
 
+    ring_params[0] = Common::ParamPackage(Settings::values.ringcon_analogs);
+
     // Other or debug controller should always be a pro controller
     if (npad_id_type != NpadIdType::Other) {
         SetNpadStyleIndex(MapSettingsTypeToNPad(player.controller_type));
@@ -144,14 +146,15 @@ void EmulatedController::LoadDevices() {
     battery_params[RightIndex].Set("battery", true);
 
     camera_params = Common::ParamPackage{"engine:camera,camera:1"};
-    nfc_params = right_joycon;
-    nfc_params.Set("nfc", true);
-    ring_params = Common::ParamPackage{"engine:joycon,axis_x:100,axis_y:101"};
+    ring_params[1] = Common::ParamPackage{"engine:joycon,axis_x:100,axis_y:101"};
+    nfc_params[0] = Common::ParamPackage{"engine:virtual_amiibo,nfc:1"};
+    nfc_params[1] = right_joycon;
+    nfc_params[1].Set("nfc", true);
 
     output_params[LeftIndex] = left_joycon;
     output_params[RightIndex] = right_joycon;
     output_params[2] = camera_params;
-    output_params[3] = nfc_params;
+    output_params[3] = nfc_params[0];
     output_params[LeftIndex].Set("output", true);
     output_params[RightIndex].Set("output", true);
     output_params[2].Set("output", true);
@@ -169,8 +172,9 @@ void EmulatedController::LoadDevices() {
                            Common::Input::CreateInputDevice);
     std::ranges::transform(color_params, color_devices.begin(), Common::Input::CreateInputDevice);
     camera_devices = Common::Input::CreateInputDevice(camera_params);
-    ring_analog_device = Common::Input::CreateInputDevice(ring_params);
-    nfc_devices = Common::Input::CreateInputDevice(nfc_params);
+    std::ranges::transform(ring_params, ring_analog_devices.begin(),
+                           Common::Input::CreateInputDevice);
+    std::ranges::transform(nfc_params, nfc_devices.begin(), Common::Input::CreateInputDevice);
     std::ranges::transform(output_params, output_devices.begin(),
                            Common::Input::CreateOutputDevice);
 
@@ -366,21 +370,26 @@ void EmulatedController::ReloadInput() {
         camera_devices->ForceUpdate();
     }
 
-    if (ring_analog_device) {
-        ring_analog_device->SetCallback({
+    for (std::size_t index = 0; index < ring_analog_devices.size(); ++index) {
+        if (!ring_analog_devices[index]) {
+            continue;
+        }
+        ring_analog_devices[index]->SetCallback({
             .on_change =
                 [this](const Common::Input::CallbackStatus& callback) { SetRingAnalog(callback); },
         });
+        ring_analog_devices[index]->ForceUpdate();
     }
 
-    if (nfc_devices) {
-        if (npad_id_type == NpadIdType::Handheld || npad_id_type == NpadIdType::Player1) {
-            nfc_devices->SetCallback({
-                .on_change =
-                    [this](const Common::Input::CallbackStatus& callback) { SetNfc(callback); },
-            });
-            nfc_devices->ForceUpdate();
+    for (std::size_t index = 0; index < nfc_devices.size(); ++index) {
+        if (!nfc_devices[index]) {
+            continue;
         }
+        nfc_devices[index]->SetCallback({
+            .on_change =
+                [this](const Common::Input::CallbackStatus& callback) { SetNfc(callback); },
+        });
+        nfc_devices[index]->ForceUpdate();
     }
 
     // Register TAS devices. No need to force update
@@ -469,8 +478,12 @@ void EmulatedController::UnloadInput() {
         stick.reset();
     }
     camera_devices.reset();
-    ring_analog_device.reset();
-    nfc_devices.reset();
+    for (auto& ring : ring_analog_devices) {
+        ring.reset();
+    }
+    for (auto& nfc : nfc_devices) {
+        nfc.reset();
+    }
 }
 
 void EmulatedController::EnableConfiguration() {
@@ -540,7 +553,9 @@ void EmulatedController::SaveCurrentConfig() {
     for (std::size_t index = 0; index < player.motions.size(); ++index) {
         player.motions[index] = motion_params[index].Serialize();
     }
-    Settings::values.ringcon_analogs = ring_params.Serialize();
+    if (npad_id_type == NpadIdType::Player1) {
+        Settings::values.ringcon_analogs = ring_params[0].Serialize();
+    }
 }
 
 void EmulatedController::RestoreConfig() {
@@ -1215,11 +1230,11 @@ bool EmulatedController::SetCameraFormat(
 }
 
 Common::ParamPackage EmulatedController::GetRingParam() const {
-    return ring_params;
+    return ring_params[0];
 }
 
 void EmulatedController::SetRingParam(Common::ParamPackage param) {
-    ring_params = std::move(param);
+    ring_params[0] = std::move(param);
     ReloadInput();
 }
 
diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h
index d044cc36b..c517aa5d7 100644
--- a/src/core/hid/emulated_controller.h
+++ b/src/core/hid/emulated_controller.h
@@ -40,8 +40,10 @@ using ColorDevices =
 using BatteryDevices =
     std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
 using CameraDevices = std::unique_ptr<Common::Input::InputDevice>;
-using RingAnalogDevice = std::unique_ptr<Common::Input::InputDevice>;
-using NfcDevices = std::unique_ptr<Common::Input::InputDevice>;
+using RingAnalogDevices =
+    std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
+using NfcDevices =
+    std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
 using OutputDevices = std::array<std::unique_ptr<Common::Input::OutputDevice>, output_devices_size>;
 
 using ButtonParams = std::array<Common::ParamPackage, Settings::NativeButton::NumButtons>;
@@ -51,8 +53,8 @@ using TriggerParams = std::array<Common::ParamPackage, Settings::NativeTrigger::
 using ColorParams = std::array<Common::ParamPackage, max_emulated_controllers>;
 using BatteryParams = std::array<Common::ParamPackage, max_emulated_controllers>;
 using CameraParams = Common::ParamPackage;
-using RingAnalogParams = Common::ParamPackage;
-using NfcParams = Common::ParamPackage;
+using RingAnalogParams = std::array<Common::ParamPackage, max_emulated_controllers>;
+using NfcParams = std::array<Common::ParamPackage, max_emulated_controllers>;
 using OutputParams = std::array<Common::ParamPackage, output_devices_size>;
 
 using ButtonValues = std::array<Common::Input::ButtonStatus, Settings::NativeButton::NumButtons>;
@@ -538,7 +540,7 @@ private:
     BatteryDevices battery_devices;
     ColorDevices color_devices;
     CameraDevices camera_devices;
-    RingAnalogDevice ring_analog_device;
+    RingAnalogDevices ring_analog_devices;
     NfcDevices nfc_devices;
     OutputDevices output_devices;
 
diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp
index 83b368091..fe5bf94d2 100644
--- a/src/core/hle/service/hid/controllers/npad.cpp
+++ b/src/core/hle/service/hid/controllers/npad.cpp
@@ -337,6 +337,7 @@ void Controller_NPad::InitNewlyAddedController(Core::HID::NpadIdType npad_id) {
     controller.is_connected = true;
     controller.device->Connect();
     controller.device->SetLedPattern();
+    controller.device->SetPollingMode(Common::Input::PollingMode::Active);
     SignalStyleSetChangedEvent(npad_id);
     WriteEmptyEntry(controller.shared_memory);
 }
diff --git a/src/core/hle/service/hid/hidbus.cpp b/src/core/hle/service/hid/hidbus.cpp
index e5e50845f..17252a84a 100644
--- a/src/core/hle/service/hid/hidbus.cpp
+++ b/src/core/hle/service/hid/hidbus.cpp
@@ -297,13 +297,13 @@ void HidBus::EnableExternalDevice(Kernel::HLERequestContext& ctx) {
 
     const auto parameters{rp.PopRaw<Parameters>()};
 
-    LOG_INFO(Service_HID,
-             "called, enable={}, abstracted_pad_id={}, bus_type={}, internal_index={}, "
-             "player_number={}, is_valid={}, inval={}, applet_resource_user_id{}",
-             parameters.enable, parameters.bus_handle.abstracted_pad_id,
-             parameters.bus_handle.bus_type, parameters.bus_handle.internal_index,
-             parameters.bus_handle.player_number, parameters.bus_handle.is_valid, parameters.inval,
-             parameters.applet_resource_user_id);
+    LOG_DEBUG(Service_HID,
+              "called, enable={}, abstracted_pad_id={}, bus_type={}, internal_index={}, "
+              "player_number={}, is_valid={}, inval={}, applet_resource_user_id{}",
+              parameters.enable, parameters.bus_handle.abstracted_pad_id,
+              parameters.bus_handle.bus_type, parameters.bus_handle.internal_index,
+              parameters.bus_handle.player_number, parameters.bus_handle.is_valid, parameters.inval,
+              parameters.applet_resource_user_id);
 
     const auto device_index = GetDeviceIndexFromHandle(parameters.bus_handle);
 
@@ -326,11 +326,11 @@ void HidBus::GetExternalDeviceId(Kernel::HLERequestContext& ctx) {
     IPC::RequestParser rp{ctx};
     const auto bus_handle_{rp.PopRaw<BusHandle>()};
 
-    LOG_INFO(Service_HID,
-             "called, abstracted_pad_id={}, bus_type={}, internal_index={}, player_number={}, "
-             "is_valid={}",
-             bus_handle_.abstracted_pad_id, bus_handle_.bus_type, bus_handle_.internal_index,
-             bus_handle_.player_number, bus_handle_.is_valid);
+    LOG_DEBUG(Service_HID,
+              "called, abstracted_pad_id={}, bus_type={}, internal_index={}, player_number={}, "
+              "is_valid={}",
+              bus_handle_.abstracted_pad_id, bus_handle_.bus_type, bus_handle_.internal_index,
+              bus_handle_.player_number, bus_handle_.is_valid);
 
     const auto device_index = GetDeviceIndexFromHandle(bus_handle_);
 
diff --git a/src/input_common/helpers/joycon_driver.cpp b/src/input_common/helpers/joycon_driver.cpp
index c3debffd1..db9ff4875 100644
--- a/src/input_common/helpers/joycon_driver.cpp
+++ b/src/input_common/helpers/joycon_driver.cpp
@@ -396,7 +396,7 @@ DriverResult JoyconDriver::SetActiveMode() {
 
 DriverResult JoyconDriver::SetNfcMode() {
     std::scoped_lock lock{mutex};
-    motion_enabled = false;
+    motion_enabled = true;
     hidbus_enabled = false;
     nfc_enabled = true;
     passive_enabled = false;
diff --git a/src/input_common/helpers/joycon_protocol/ringcon.cpp b/src/input_common/helpers/joycon_protocol/ringcon.cpp
index 2d137b85d..47769f344 100644
--- a/src/input_common/helpers/joycon_protocol/ringcon.cpp
+++ b/src/input_common/helpers/joycon_protocol/ringcon.cpp
@@ -55,9 +55,6 @@ DriverResult RingConProtocol::StartRingconPolling() {
     DriverResult result{DriverResult::Success};
     SetBlocking();
 
-    if (result == DriverResult::Success) {
-        result = WaitSetMCUMode(ReportMode::STANDARD_FULL_60HZ, MCUMode::Standby);
-    }
     if (result == DriverResult::Success) {
         result = IsRingConnected(is_connected);
     }