From 2d3a63b28969089746e43ed232dc74630fbfc3b2 Mon Sep 17 00:00:00 2001
From: german77 <juangerman-13@hotmail.com>
Date: Wed, 27 Oct 2021 18:06:13 -0500
Subject: [PATCH] core/hid: Update structs to 13.1.0

---
 src/core/hid/hid_types.h                      | 10 ++++
 src/core/hid/input_interpreter.cpp            | 14 ++---
 src/core/hid/input_interpreter.h              |  2 +-
 .../hle/service/hid/controllers/debug_pad.cpp |  4 +-
 .../hle/service/hid/controllers/gesture.cpp   |  8 +--
 .../hle/service/hid/controllers/keyboard.cpp  |  4 +-
 .../hle/service/hid/controllers/mouse.cpp     |  6 +-
 src/core/hle/service/hid/controllers/npad.cpp | 15 ++---
 src/core/hle/service/hid/controllers/npad.h   | 60 +++++++++++++++++--
 .../service/hid/controllers/touchscreen.cpp   |  4 +-
 src/core/hle/service/hid/controllers/xpad.cpp |  4 +-
 src/core/hle/service/hid/ring_lifo.h          | 26 ++++----
 12 files changed, 107 insertions(+), 50 deletions(-)

diff --git a/src/core/hid/hid_types.h b/src/core/hid/hid_types.h
index 59ec593b8..f8a0d5edd 100644
--- a/src/core/hid/hid_types.h
+++ b/src/core/hid/hid_types.h
@@ -231,7 +231,12 @@ enum class NpadButton : u64 {
     RightSR = 1U << 27,
 
     Palma = 1U << 28,
+    Verification = 1U << 29,
     HandheldLeftB = 1U << 30,
+    LagonCLeft = 1U << 31,
+    LagonCUp = 1ULL << 32,
+    LagonCRight = 1ULL << 33,
+    LagonCDown = 1ULL << 34,
 };
 DECLARE_ENUM_FLAG_OPERATORS(NpadButton);
 
@@ -278,7 +283,12 @@ struct NpadButtonState {
         BitField<27, 1, u64> right_sr;
 
         BitField<28, 1, u64> palma;
+        BitField<29, 1, u64> verification;
         BitField<30, 1, u64> handheld_left_b;
+        BitField<31, 1, u64> lagon_c_left;
+        BitField<32, 1, u64> lagon_c_up;
+        BitField<33, 1, u64> lagon_c_right;
+        BitField<34, 1, u64> lagon_c_down;
     };
 };
 static_assert(sizeof(NpadButtonState) == 0x8, "NpadButtonState has incorrect size.");
diff --git a/src/core/hid/input_interpreter.cpp b/src/core/hid/input_interpreter.cpp
index 7e7c1816f..870422d82 100644
--- a/src/core/hid/input_interpreter.cpp
+++ b/src/core/hid/input_interpreter.cpp
@@ -20,7 +20,7 @@ InputInterpreter::InputInterpreter(Core::System& system)
 InputInterpreter::~InputInterpreter() = default;
 
 void InputInterpreter::PollInput() {
-    const u32 button_state = npad.GetAndResetPressState();
+    const u64 button_state = npad.GetAndResetPressState();
 
     previous_index = current_index;
     current_index = (current_index + 1) % button_states.size();
@@ -32,7 +32,7 @@ void InputInterpreter::ResetButtonStates() {
     previous_index = 0;
     current_index = 0;
 
-    button_states[0] = 0xFFFFFFFF;
+    button_states[0] = 0xFFFFFFFFFFFFFFFF;
 
     for (std::size_t i = 1; i < button_states.size(); ++i) {
         button_states[i] = 0;
@@ -40,22 +40,22 @@ void InputInterpreter::ResetButtonStates() {
 }
 
 bool InputInterpreter::IsButtonPressed(Core::HID::NpadButton button) const {
-    return (button_states[current_index] & static_cast<u32>(button)) != 0;
+    return (button_states[current_index] & static_cast<u64>(button)) != 0;
 }
 
 bool InputInterpreter::IsButtonPressedOnce(Core::HID::NpadButton button) const {
-    const bool current_press = (button_states[current_index] & static_cast<u32>(button)) != 0;
-    const bool previous_press = (button_states[previous_index] & static_cast<u32>(button)) != 0;
+    const bool current_press = (button_states[current_index] & static_cast<u64>(button)) != 0;
+    const bool previous_press = (button_states[previous_index] & static_cast<u64>(button)) != 0;
 
     return current_press && !previous_press;
 }
 
 bool InputInterpreter::IsButtonHeld(Core::HID::NpadButton button) const {
-    u32 held_buttons{button_states[0]};
+    u64 held_buttons{button_states[0]};
 
     for (std::size_t i = 1; i < button_states.size(); ++i) {
         held_buttons &= button_states[i];
     }
 
-    return (held_buttons & static_cast<u32>(button)) != 0;
+    return (held_buttons & static_cast<u64>(button)) != 0;
 }
diff --git a/src/core/hid/input_interpreter.h b/src/core/hid/input_interpreter.h
index 1791cf9b7..1c2e02142 100644
--- a/src/core/hid/input_interpreter.h
+++ b/src/core/hid/input_interpreter.h
@@ -105,7 +105,7 @@ private:
     Service::HID::Controller_NPad& npad;
 
     /// Stores 9 consecutive button states polled from HID.
-    std::array<u32, 9> button_states{};
+    std::array<u64, 9> button_states{};
 
     std::size_t previous_index{};
     std::size_t current_index{};
diff --git a/src/core/hle/service/hid/controllers/debug_pad.cpp b/src/core/hle/service/hid/controllers/debug_pad.cpp
index 5b1946f13..345134357 100644
--- a/src/core/hle/service/hid/controllers/debug_pad.cpp
+++ b/src/core/hle/service/hid/controllers/debug_pad.cpp
@@ -30,8 +30,8 @@ void Controller_DebugPad::OnRelease() {}
 void Controller_DebugPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data,
                                    std::size_t size) {
     if (!IsControllerActivated()) {
-        debug_pad_lifo.entry_count = 0;
-        debug_pad_lifo.last_entry_index = 0;
+        debug_pad_lifo.buffer_count = 0;
+        debug_pad_lifo.buffer_tail = 0;
         std::memcpy(data + SHARED_MEMORY_OFFSET, &debug_pad_lifo, sizeof(debug_pad_lifo));
         return;
     }
diff --git a/src/core/hle/service/hid/controllers/gesture.cpp b/src/core/hle/service/hid/controllers/gesture.cpp
index 47760b4f8..00df50f32 100644
--- a/src/core/hle/service/hid/controllers/gesture.cpp
+++ b/src/core/hle/service/hid/controllers/gesture.cpp
@@ -31,8 +31,8 @@ Controller_Gesture::Controller_Gesture(Core::System& system_) : ControllerBase(s
 Controller_Gesture::~Controller_Gesture() = default;
 
 void Controller_Gesture::OnInit() {
-    gesture_lifo.entry_count = 0;
-    gesture_lifo.last_entry_index = 0;
+    gesture_lifo.buffer_count = 0;
+    gesture_lifo.buffer_tail = 0;
     force_update = true;
 }
 
@@ -41,8 +41,8 @@ void Controller_Gesture::OnRelease() {}
 void Controller_Gesture::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data,
                                   std::size_t size) {
     if (!IsControllerActivated()) {
-        gesture_lifo.entry_count = 0;
-        gesture_lifo.last_entry_index = 0;
+        gesture_lifo.buffer_count = 0;
+        gesture_lifo.buffer_tail = 0;
         std::memcpy(data + SHARED_MEMORY_OFFSET, &gesture_lifo, sizeof(gesture_lifo));
         return;
     }
diff --git a/src/core/hle/service/hid/controllers/keyboard.cpp b/src/core/hle/service/hid/controllers/keyboard.cpp
index 632679a17..f4d49965f 100644
--- a/src/core/hle/service/hid/controllers/keyboard.cpp
+++ b/src/core/hle/service/hid/controllers/keyboard.cpp
@@ -27,8 +27,8 @@ void Controller_Keyboard::OnRelease() {}
 void Controller_Keyboard::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data,
                                    std::size_t size) {
     if (!IsControllerActivated()) {
-        keyboard_lifo.entry_count = 0;
-        keyboard_lifo.last_entry_index = 0;
+        keyboard_lifo.buffer_count = 0;
+        keyboard_lifo.buffer_tail = 0;
         std::memcpy(data + SHARED_MEMORY_OFFSET, &keyboard_lifo, sizeof(keyboard_lifo));
         return;
     }
diff --git a/src/core/hle/service/hid/controllers/mouse.cpp b/src/core/hle/service/hid/controllers/mouse.cpp
index 6d3bd0a2b..7ec75e8c8 100644
--- a/src/core/hle/service/hid/controllers/mouse.cpp
+++ b/src/core/hle/service/hid/controllers/mouse.cpp
@@ -24,11 +24,9 @@ void Controller_Mouse::OnRelease() {}
 
 void Controller_Mouse::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data,
                                 std::size_t size) {
-    mouse_lifo.timestamp = core_timing.GetCPUTicks();
-
     if (!IsControllerActivated()) {
-        mouse_lifo.entry_count = 0;
-        mouse_lifo.last_entry_index = 0;
+        mouse_lifo.buffer_count = 0;
+        mouse_lifo.buffer_tail = 0;
         std::memcpy(data + SHARED_MEMORY_OFFSET, &mouse_lifo, sizeof(mouse_lifo));
         return;
     }
diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp
index 9f84e20c2..9f82f872a 100644
--- a/src/core/hle/service/hid/controllers/npad.cpp
+++ b/src/core/hle/service/hid/controllers/npad.cpp
@@ -101,7 +101,8 @@ Controller_NPad::Controller_NPad(Core::System& system_,
     for (std::size_t i = 0; i < controller_data.size(); ++i) {
         auto& controller = controller_data[i];
         controller.device = system.HIDCore().GetEmulatedControllerByIndex(i);
-        controller.vibration[Core::HID::DeviceIndex::LeftIndex].latest_vibration_value = DEFAULT_VIBRATION_VALUE;
+        controller.vibration[Core::HID::DeviceIndex::LeftIndex].latest_vibration_value =
+            DEFAULT_VIBRATION_VALUE;
         controller.vibration[Core::HID::DeviceIndex::RightIndex].latest_vibration_value =
             DEFAULT_VIBRATION_VALUE;
         Core::HID::ControllerUpdateCallback engine_callback{
@@ -178,7 +179,7 @@ void Controller_NPad::InitNewlyAddedController(std::size_t controller_idx) {
         shared_memory.system_properties.use_plus.Assign(1);
         shared_memory.system_properties.use_minus.Assign(1);
         shared_memory.assignment_mode = NpadJoyAssignmentMode::Single;
-        shared_memory.footer_type = AppletFooterUiType::SwitchProController;
+        shared_memory.applet_footer.type = AppletFooterUiType::SwitchProController;
         break;
     case Core::HID::NpadType::Handheld:
         shared_memory.style_set.handheld.Assign(1);
@@ -188,7 +189,7 @@ void Controller_NPad::InitNewlyAddedController(std::size_t controller_idx) {
         shared_memory.system_properties.use_plus.Assign(1);
         shared_memory.system_properties.use_minus.Assign(1);
         shared_memory.assignment_mode = NpadJoyAssignmentMode::Dual;
-        shared_memory.footer_type = AppletFooterUiType::HandheldJoyConLeftJoyConRight;
+        shared_memory.applet_footer.type = AppletFooterUiType::HandheldJoyConLeftJoyConRight;
         break;
     case Core::HID::NpadType::JoyconDual:
         shared_memory.style_set.joycon_dual.Assign(1);
@@ -198,7 +199,7 @@ void Controller_NPad::InitNewlyAddedController(std::size_t controller_idx) {
         shared_memory.system_properties.use_plus.Assign(1);
         shared_memory.system_properties.use_minus.Assign(1);
         shared_memory.assignment_mode = NpadJoyAssignmentMode::Dual;
-        shared_memory.footer_type = AppletFooterUiType::JoyDual;
+        shared_memory.applet_footer.type = AppletFooterUiType::JoyDual;
         break;
     case Core::HID::NpadType::JoyconLeft:
         shared_memory.style_set.joycon_left.Assign(1);
@@ -206,7 +207,7 @@ void Controller_NPad::InitNewlyAddedController(std::size_t controller_idx) {
         shared_memory.system_properties.is_horizontal.Assign(1);
         shared_memory.system_properties.use_minus.Assign(1);
         shared_memory.assignment_mode = NpadJoyAssignmentMode::Single;
-        shared_memory.footer_type = AppletFooterUiType::JoyLeftHorizontal;
+        shared_memory.applet_footer.type = AppletFooterUiType::JoyLeftHorizontal;
         break;
     case Core::HID::NpadType::JoyconRight:
         shared_memory.style_set.joycon_right.Assign(1);
@@ -214,7 +215,7 @@ void Controller_NPad::InitNewlyAddedController(std::size_t controller_idx) {
         shared_memory.system_properties.is_horizontal.Assign(1);
         shared_memory.system_properties.use_plus.Assign(1);
         shared_memory.assignment_mode = NpadJoyAssignmentMode::Single;
-        shared_memory.footer_type = AppletFooterUiType::JoyRightHorizontal;
+        shared_memory.applet_footer.type = AppletFooterUiType::JoyRightHorizontal;
         break;
     case Core::HID::NpadType::GameCube:
         shared_memory.style_set.gamecube.Assign(1);
@@ -919,7 +920,7 @@ void Controller_NPad::DisconnectNpadAtIndex(std::size_t npad_index) {
         .right = {},
     };
     shared_memory_entry.assignment_mode = NpadJoyAssignmentMode::Dual;
-    shared_memory_entry.footer_type = AppletFooterUiType::None;
+    shared_memory_entry.applet_footer.type = AppletFooterUiType::None;
 
     controller.is_connected = false;
     controller.device->Disconnect();
diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h
index 0a2dc6992..af4934c55 100644
--- a/src/core/hle/service/hid/controllers/npad.h
+++ b/src/core/hle/service/hid/controllers/npad.h
@@ -330,10 +330,43 @@ private:
             BitField<13, 1, s32> handheld_lark_nes_left;
             BitField<14, 1, s32> handheld_lark_nes_right;
             BitField<15, 1, s32> lucia;
+            BitField<16, 1, s32> lagon;
+            BitField<17, 1, s32> lager;
             BitField<31, 1, s32> system;
         };
     };
 
+    // This is nn::hid::detail::NfcXcdDeviceHandleStateImpl
+    struct NfcXcdDeviceHandleStateImpl {
+        u64 handle;
+        bool is_available;
+        bool is_activated;
+        INSERT_PADDING_BYTES(0x6); // Reserved
+        u64 sampling_number;
+    };
+    static_assert(sizeof(NfcXcdDeviceHandleStateImpl) == 0x18,
+                  "NfcXcdDeviceHandleStateImpl is an invalid size");
+
+    // nn::hid::detail::NfcXcdDeviceHandleStateImplAtomicStorage
+    struct NfcXcdDeviceHandleStateImplAtomicStorage {
+        u64 sampling_number;
+        NfcXcdDeviceHandleStateImpl nfc_xcd_device_handle_state;
+    };
+    static_assert(sizeof(NfcXcdDeviceHandleStateImplAtomicStorage) == 0x20,
+                  "NfcXcdDeviceHandleStateImplAtomicStorage is an invalid size");
+
+    // This is nn::hid::detail::NfcXcdDeviceHandleState
+    struct NfcXcdDeviceHandleState {
+        // TODO(german77): Make this struct a ring lifo object
+        INSERT_PADDING_BYTES(0x8); // Unused
+        s64 total_buffer_count = max_buffer_size;
+        s64 buffer_tail{};
+        s64 buffer_count{};
+        std::array<NfcXcdDeviceHandleStateImplAtomicStorage, 2> nfc_xcd_device_handle_storage;
+    };
+    static_assert(sizeof(NfcXcdDeviceHandleState) == 0x60,
+                  "NfcXcdDeviceHandleState is an invalid size");
+
     // This is nn::hid::system::AppletFooterUiAttributesSet
     struct AppletFooterUiAttributes {
         INSERT_PADDING_BYTES(0x4);
@@ -365,6 +398,14 @@ private:
         Lagon = 21,
     };
 
+    struct AppletFooterUi {
+        AppletFooterUiAttributes attributes;
+        AppletFooterUiType type;
+        INSERT_PADDING_BYTES(0x5B); // Reserved
+    };
+    static_assert(sizeof(AppletFooterUi) == 0x60,
+                  "AppletFooterUi is an invalid size");
+
     // This is nn::hid::NpadLarkType
     enum class NpadLarkType : u32 {
         Invalid,
@@ -382,6 +423,11 @@ private:
         U,
     };
 
+    // This is nn::hid::NpadLagonType
+    enum class NpadLagonType : u32 {
+        Invalid,
+    };
+
     // This is nn::hid::NpadLagerType
     enum class NpadLagerType : u32 {
         Invalid,
@@ -416,17 +462,19 @@ private:
         Core::HID::BatteryLevel battery_level_dual;
         Core::HID::BatteryLevel battery_level_left;
         Core::HID::BatteryLevel battery_level_right;
-        AppletFooterUiAttributes footer_attributes;
-        AppletFooterUiType footer_type;
-        // GetXcdHandleForNpadWithNfc needs to be checked switchbrew doesn't match with HW
-        INSERT_PADDING_BYTES(0x78); // Unknown
+        union {
+            NfcXcdDeviceHandleState nfc_xcd_device_handle;
+            AppletFooterUi applet_footer;
+        };
+        INSERT_PADDING_BYTES(0x20); // Unknown
         Lifo<NpadGcTriggerState> gc_trigger_lifo;
-        NpadLarkType lark_type_l;
+        NpadLarkType lark_type_l_and_main;
         NpadLarkType lark_type_r;
         NpadLuciaType lucia_type;
+        NpadLagonType lagon_type;
         NpadLagerType lager_type;
         INSERT_PADDING_BYTES(
-            0x8); // FW 13.x Investigate there is some sort of bitflag related to joycons
+            0x4); // FW 13.x Investigate there is some sort of bitflag related to joycons
         INSERT_PADDING_BYTES(0xc08); // Unknown
     };
     static_assert(sizeof(NpadInternalState) == 0x5000, "NpadInternalState is an invalid size");
diff --git a/src/core/hle/service/hid/controllers/touchscreen.cpp b/src/core/hle/service/hid/controllers/touchscreen.cpp
index 5ba8d96a8..9ae2bf2b1 100644
--- a/src/core/hle/service/hid/controllers/touchscreen.cpp
+++ b/src/core/hle/service/hid/controllers/touchscreen.cpp
@@ -30,8 +30,8 @@ void Controller_Touchscreen::OnUpdate(const Core::Timing::CoreTiming& core_timin
     touch_screen_lifo.timestamp = core_timing.GetCPUTicks();
 
     if (!IsControllerActivated()) {
-        touch_screen_lifo.entry_count = 0;
-        touch_screen_lifo.last_entry_index = 0;
+        touch_screen_lifo.buffer_count = 0;
+        touch_screen_lifo.buffer_tail = 0;
         std::memcpy(data, &touch_screen_lifo, sizeof(touch_screen_lifo));
         return;
     }
diff --git a/src/core/hle/service/hid/controllers/xpad.cpp b/src/core/hle/service/hid/controllers/xpad.cpp
index aa9f044f1..a2ed1e7c2 100644
--- a/src/core/hle/service/hid/controllers/xpad.cpp
+++ b/src/core/hle/service/hid/controllers/xpad.cpp
@@ -20,8 +20,8 @@ void Controller_XPad::OnRelease() {}
 void Controller_XPad::OnUpdate(const Core::Timing::CoreTiming& core_timing, u8* data,
                                std::size_t size) {
     if (!IsControllerActivated()) {
-        basic_xpad_lifo.entry_count = 0;
-        basic_xpad_lifo.last_entry_index = 0;
+        basic_xpad_lifo.buffer_count = 0;
+        basic_xpad_lifo.buffer_tail = 0;
         std::memcpy(data + SHARED_MEMORY_OFFSET, &basic_xpad_lifo, sizeof(basic_xpad_lifo));
         return;
     }
diff --git a/src/core/hle/service/hid/ring_lifo.h b/src/core/hle/service/hid/ring_lifo.h
index f68d82762..382350a2d 100644
--- a/src/core/hle/service/hid/ring_lifo.h
+++ b/src/core/hle/service/hid/ring_lifo.h
@@ -8,7 +8,7 @@
 #include "common/swap.h"
 
 namespace Service::HID {
-constexpr std::size_t max_entry_size = 17;
+constexpr std::size_t max_buffer_size = 17;
 
 template <typename State>
 struct AtomicStorage {
@@ -19,13 +19,13 @@ struct AtomicStorage {
 template <typename State>
 struct Lifo {
     s64 timestamp{};
-    s64 total_entry_count = max_entry_size;
-    s64 last_entry_index{};
-    s64 entry_count{};
-    std::array<AtomicStorage<State>, max_entry_size> entries{};
+    s64 total_buffer_count = max_buffer_size;
+    s64 buffer_tail{};
+    s64 buffer_count{};
+    std::array<AtomicStorage<State>, max_buffer_size> entries{};
 
     const AtomicStorage<State>& ReadCurrentEntry() const {
-        return entries[last_entry_index];
+        return entries[buffer_tail];
     }
 
     const AtomicStorage<State>& ReadPreviousEntry() const {
@@ -33,21 +33,21 @@ struct Lifo {
     }
 
     std::size_t GetPreviuousEntryIndex() const {
-        return (last_entry_index + total_entry_count - 1) % total_entry_count;
+        return (buffer_tail + total_buffer_count - 1) % total_buffer_count;
     }
 
     std::size_t GetNextEntryIndex() const {
-        return (last_entry_index + 1) % total_entry_count;
+        return (buffer_tail + 1) % total_buffer_count;
     }
 
     void WriteNextEntry(const State& new_state) {
-        if (entry_count < total_entry_count - 1) {
-            entry_count++;
+        if (buffer_count < total_buffer_count - 1) {
+            buffer_count++;
         }
-        last_entry_index = GetNextEntryIndex();
+        buffer_tail = GetNextEntryIndex();
         const auto& previous_entry = ReadPreviousEntry();
-        entries[last_entry_index].sampling_number = previous_entry.sampling_number + 1;
-        entries[last_entry_index].state = new_state;
+        entries[buffer_tail].sampling_number = previous_entry.sampling_number + 1;
+        entries[buffer_tail].state = new_state;
     }
 };