From ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613 Mon Sep 17 00:00:00 2001 From: Narr the Reg Date: Thu, 4 Jan 2024 20:37:43 -0600 Subject: hid_core: Move hid to it's own subproject --- src/hid_core/resources/npad/npad.cpp | 1342 +++++++++++++++++++++++++ src/hid_core/resources/npad/npad.h | 214 ++++ src/hid_core/resources/npad/npad_data.cpp | 228 +++++ src/hid_core/resources/npad/npad_data.h | 88 ++ src/hid_core/resources/npad/npad_resource.cpp | 685 +++++++++++++ src/hid_core/resources/npad/npad_resource.h | 132 +++ src/hid_core/resources/npad/npad_types.h | 255 +++++ 7 files changed, 2944 insertions(+) create mode 100644 src/hid_core/resources/npad/npad.cpp create mode 100644 src/hid_core/resources/npad/npad.h create mode 100644 src/hid_core/resources/npad/npad_data.cpp create mode 100644 src/hid_core/resources/npad/npad_data.h create mode 100644 src/hid_core/resources/npad/npad_resource.cpp create mode 100644 src/hid_core/resources/npad/npad_resource.h create mode 100644 src/hid_core/resources/npad/npad_types.h (limited to 'src/hid_core/resources/npad') diff --git a/src/hid_core/resources/npad/npad.cpp b/src/hid_core/resources/npad/npad.cpp new file mode 100644 index 000000000..e6c035628 --- /dev/null +++ b/src/hid_core/resources/npad/npad.cpp @@ -0,0 +1,1342 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include + +#include "common/assert.h" +#include "common/bit_field.h" +#include "common/common_types.h" +#include "common/logging/log.h" +#include "common/settings.h" +#include "core/core_timing.h" +#include "core/hle/kernel/k_event.h" +#include "core/hle/kernel/k_readable_event.h" +#include "core/hle/service/kernel_helpers.h" +#include "hid_core/frontend/emulated_controller.h" +#include "hid_core/hid_core.h" +#include "hid_core/hid_result.h" +#include "hid_core/hid_util.h" +#include "hid_core/resources/applet_resource.h" +#include "hid_core/resources/npad/npad.h" +#include "hid_core/resources/shared_memory_format.h" + +namespace Service::HID { + +NPad::NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_) + : hid_core{hid_core_}, service_context{service_context_}, npad_resource{service_context} { + for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; ++aruid_index) { + for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) { + auto& controller = controller_data[aruid_index][i]; + controller.device = hid_core.GetEmulatedControllerByIndex(i); + controller.vibration[Core::HID::EmulatedDeviceIndex::LeftIndex].latest_vibration_value = + Core::HID::DEFAULT_VIBRATION_VALUE; + controller.vibration[Core::HID::EmulatedDeviceIndex::RightIndex] + .latest_vibration_value = Core::HID::DEFAULT_VIBRATION_VALUE; + Core::HID::ControllerUpdateCallback engine_callback{ + .on_change = + [this, i](Core::HID::ControllerTriggerType type) { ControllerUpdate(type, i); }, + .is_npad_service = true, + }; + controller.callback_key = controller.device->SetCallback(engine_callback); + } + } +} + +NPad::~NPad() { + for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; ++aruid_index) { + for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) { + auto& controller = controller_data[aruid_index][i]; + controller.device->DeleteCallback(controller.callback_key); + } + } +} + +Result NPad::Activate() { + if (ref_counter == std::numeric_limits::max() - 1) { + return ResultNpadResourceOverflow; + } + + if (ref_counter == 0) { + std::scoped_lock lock{mutex}; + + // TODO: Activate handlers and AbstractedPad + } + + ref_counter++; + return ResultSuccess; +} + +Result NPad::Activate(u64 aruid) { + std::scoped_lock lock{mutex}; + std::scoped_lock shared_lock{*applet_resource_holder.shared_mutex}; + + auto* data = applet_resource_holder.applet_resource->GetAruidData(aruid); + const auto aruid_index = applet_resource_holder.applet_resource->GetIndexFromAruid(aruid); + + if (data == nullptr || !data->flag.is_assigned) { + return ResultSuccess; + } + + for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) { + auto& controller = controller_data[aruid_index][i]; + controller.shared_memory = &data->shared_memory_format->npad.npad_entry[i].internal_state; + } + + // Prefill controller buffers + for (auto& controller : controller_data[aruid_index]) { + auto* npad = controller.shared_memory; + npad->fullkey_color = { + .attribute = ColorAttribute::NoController, + .fullkey = {}, + }; + npad->joycon_color = { + .attribute = ColorAttribute::NoController, + .left = {}, + .right = {}, + }; + // HW seems to initialize the first 19 entries + for (std::size_t i = 0; i < 19; ++i) { + WriteEmptyEntry(npad); + } + } + + return ResultSuccess; +} + +Result NPad::ActivateNpadResource() { + return npad_resource.Activate(); +} + +Result NPad::ActivateNpadResource(u64 aruid) { + return npad_resource.Activate(aruid); +} + +void NPad::ControllerUpdate(Core::HID::ControllerTriggerType type, std::size_t controller_idx) { + if (type == Core::HID::ControllerTriggerType::All) { + ControllerUpdate(Core::HID::ControllerTriggerType::Connected, controller_idx); + ControllerUpdate(Core::HID::ControllerTriggerType::Battery, controller_idx); + return; + } + + for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) { + if (controller_idx >= controller_data[aruid_index].size()) { + return; + } + + auto* data = applet_resource_holder.applet_resource->GetAruidDataByIndex(aruid_index); + + if (!data->flag.is_assigned) { + continue; + } + + auto& controller = controller_data[aruid_index][controller_idx]; + const auto is_connected = controller.device->IsConnected(); + const auto npad_type = controller.device->GetNpadStyleIndex(); + const auto npad_id = controller.device->GetNpadIdType(); + switch (type) { + case Core::HID::ControllerTriggerType::Connected: + case Core::HID::ControllerTriggerType::Disconnected: + if (is_connected == controller.is_connected) { + return; + } + UpdateControllerAt(data->aruid, npad_type, npad_id, is_connected); + break; + case Core::HID::ControllerTriggerType::Battery: { + if (!controller.device->IsConnected()) { + return; + } + auto* shared_memory = controller.shared_memory; + const auto& battery_level = controller.device->GetBattery(); + shared_memory->battery_level_dual = battery_level.dual.battery_level; + shared_memory->battery_level_left = battery_level.left.battery_level; + shared_memory->battery_level_right = battery_level.right.battery_level; + break; + } + default: + break; + } + } +} + +void NPad::InitNewlyAddedController(u64 aruid, Core::HID::NpadIdType npad_id) { + auto& controller = GetControllerFromNpadIdType(aruid, npad_id); + if (!npad_resource.IsControllerSupported(aruid, controller.device->GetNpadStyleIndex())) { + return; + } + LOG_DEBUG(Service_HID, "Npad connected {}", npad_id); + const auto controller_type = controller.device->GetNpadStyleIndex(); + const auto& body_colors = controller.device->GetColors(); + const auto& battery_level = controller.device->GetBattery(); + auto* shared_memory = controller.shared_memory; + if (controller_type == Core::HID::NpadStyleIndex::None) { + npad_resource.SignalStyleSetUpdateEvent(aruid, npad_id); + return; + } + + // Reset memory values + shared_memory->style_tag.raw = Core::HID::NpadStyleSet::None; + shared_memory->device_type.raw = 0; + shared_memory->system_properties.raw = 0; + shared_memory->joycon_color.attribute = ColorAttribute::NoController; + shared_memory->joycon_color.attribute = ColorAttribute::NoController; + shared_memory->fullkey_color = {}; + shared_memory->joycon_color.left = {}; + shared_memory->joycon_color.right = {}; + shared_memory->battery_level_dual = {}; + shared_memory->battery_level_left = {}; + shared_memory->battery_level_right = {}; + + switch (controller_type) { + case Core::HID::NpadStyleIndex::None: + ASSERT(false); + break; + case Core::HID::NpadStyleIndex::ProController: + shared_memory->fullkey_color.attribute = ColorAttribute::Ok; + shared_memory->fullkey_color.fullkey = body_colors.fullkey; + shared_memory->battery_level_dual = battery_level.dual.battery_level; + shared_memory->style_tag.fullkey.Assign(1); + shared_memory->device_type.fullkey.Assign(1); + shared_memory->system_properties.is_vertical.Assign(1); + shared_memory->system_properties.use_plus.Assign(1); + shared_memory->system_properties.use_minus.Assign(1); + shared_memory->system_properties.is_charging_joy_dual.Assign( + battery_level.dual.is_charging); + shared_memory->applet_footer_type = AppletFooterUiType::SwitchProController; + shared_memory->sixaxis_fullkey_properties.is_newly_assigned.Assign(1); + break; + case Core::HID::NpadStyleIndex::Handheld: + shared_memory->fullkey_color.attribute = ColorAttribute::Ok; + shared_memory->joycon_color.attribute = ColorAttribute::Ok; + shared_memory->fullkey_color.fullkey = body_colors.fullkey; + shared_memory->joycon_color.left = body_colors.left; + shared_memory->joycon_color.right = body_colors.right; + shared_memory->style_tag.handheld.Assign(1); + shared_memory->device_type.handheld_left.Assign(1); + shared_memory->device_type.handheld_right.Assign(1); + shared_memory->system_properties.is_vertical.Assign(1); + shared_memory->system_properties.use_plus.Assign(1); + shared_memory->system_properties.use_minus.Assign(1); + shared_memory->system_properties.use_directional_buttons.Assign(1); + shared_memory->system_properties.is_charging_joy_dual.Assign( + battery_level.left.is_charging); + shared_memory->system_properties.is_charging_joy_left.Assign( + battery_level.left.is_charging); + shared_memory->system_properties.is_charging_joy_right.Assign( + battery_level.right.is_charging); + shared_memory->assignment_mode = NpadJoyAssignmentMode::Dual; + shared_memory->applet_footer_type = AppletFooterUiType::HandheldJoyConLeftJoyConRight; + shared_memory->sixaxis_handheld_properties.is_newly_assigned.Assign(1); + break; + case Core::HID::NpadStyleIndex::JoyconDual: + shared_memory->fullkey_color.attribute = ColorAttribute::Ok; + shared_memory->joycon_color.attribute = ColorAttribute::Ok; + shared_memory->style_tag.joycon_dual.Assign(1); + if (controller.is_dual_left_connected) { + shared_memory->joycon_color.left = body_colors.left; + shared_memory->battery_level_left = battery_level.left.battery_level; + shared_memory->device_type.joycon_left.Assign(1); + shared_memory->system_properties.use_minus.Assign(1); + shared_memory->system_properties.is_charging_joy_left.Assign( + battery_level.left.is_charging); + shared_memory->sixaxis_dual_left_properties.is_newly_assigned.Assign(1); + } + if (controller.is_dual_right_connected) { + shared_memory->joycon_color.right = body_colors.right; + shared_memory->battery_level_right = battery_level.right.battery_level; + shared_memory->device_type.joycon_right.Assign(1); + shared_memory->system_properties.use_plus.Assign(1); + shared_memory->system_properties.is_charging_joy_right.Assign( + battery_level.right.is_charging); + shared_memory->sixaxis_dual_right_properties.is_newly_assigned.Assign(1); + } + shared_memory->system_properties.use_directional_buttons.Assign(1); + shared_memory->system_properties.is_vertical.Assign(1); + shared_memory->assignment_mode = NpadJoyAssignmentMode::Dual; + + if (controller.is_dual_left_connected && controller.is_dual_right_connected) { + shared_memory->applet_footer_type = AppletFooterUiType::JoyDual; + shared_memory->fullkey_color.fullkey = body_colors.left; + shared_memory->battery_level_dual = battery_level.left.battery_level; + shared_memory->system_properties.is_charging_joy_dual.Assign( + battery_level.left.is_charging); + } else if (controller.is_dual_left_connected) { + shared_memory->applet_footer_type = AppletFooterUiType::JoyDualLeftOnly; + shared_memory->fullkey_color.fullkey = body_colors.left; + shared_memory->battery_level_dual = battery_level.left.battery_level; + shared_memory->system_properties.is_charging_joy_dual.Assign( + battery_level.left.is_charging); + } else { + shared_memory->applet_footer_type = AppletFooterUiType::JoyDualRightOnly; + shared_memory->fullkey_color.fullkey = body_colors.right; + shared_memory->battery_level_dual = battery_level.right.battery_level; + shared_memory->system_properties.is_charging_joy_dual.Assign( + battery_level.right.is_charging); + } + break; + case Core::HID::NpadStyleIndex::JoyconLeft: + shared_memory->fullkey_color.attribute = ColorAttribute::Ok; + shared_memory->fullkey_color.fullkey = body_colors.left; + shared_memory->joycon_color.attribute = ColorAttribute::Ok; + shared_memory->joycon_color.left = body_colors.left; + shared_memory->battery_level_dual = battery_level.left.battery_level; + shared_memory->style_tag.joycon_left.Assign(1); + shared_memory->device_type.joycon_left.Assign(1); + shared_memory->system_properties.is_horizontal.Assign(1); + shared_memory->system_properties.use_minus.Assign(1); + shared_memory->system_properties.is_charging_joy_left.Assign( + battery_level.left.is_charging); + shared_memory->applet_footer_type = AppletFooterUiType::JoyLeftHorizontal; + shared_memory->sixaxis_left_properties.is_newly_assigned.Assign(1); + break; + case Core::HID::NpadStyleIndex::JoyconRight: + shared_memory->fullkey_color.attribute = ColorAttribute::Ok; + shared_memory->fullkey_color.fullkey = body_colors.right; + shared_memory->joycon_color.attribute = ColorAttribute::Ok; + shared_memory->joycon_color.right = body_colors.right; + shared_memory->battery_level_right = battery_level.right.battery_level; + shared_memory->style_tag.joycon_right.Assign(1); + shared_memory->device_type.joycon_right.Assign(1); + shared_memory->system_properties.is_horizontal.Assign(1); + shared_memory->system_properties.use_plus.Assign(1); + shared_memory->system_properties.is_charging_joy_right.Assign( + battery_level.right.is_charging); + shared_memory->applet_footer_type = AppletFooterUiType::JoyRightHorizontal; + shared_memory->sixaxis_right_properties.is_newly_assigned.Assign(1); + break; + case Core::HID::NpadStyleIndex::GameCube: + shared_memory->style_tag.gamecube.Assign(1); + shared_memory->device_type.fullkey.Assign(1); + shared_memory->system_properties.is_vertical.Assign(1); + shared_memory->system_properties.use_plus.Assign(1); + break; + case Core::HID::NpadStyleIndex::Pokeball: + shared_memory->style_tag.palma.Assign(1); + shared_memory->device_type.palma.Assign(1); + shared_memory->sixaxis_fullkey_properties.is_newly_assigned.Assign(1); + break; + case Core::HID::NpadStyleIndex::NES: + shared_memory->style_tag.lark.Assign(1); + shared_memory->device_type.fullkey.Assign(1); + break; + case Core::HID::NpadStyleIndex::SNES: + shared_memory->style_tag.lucia.Assign(1); + shared_memory->device_type.fullkey.Assign(1); + shared_memory->applet_footer_type = AppletFooterUiType::Lucia; + break; + case Core::HID::NpadStyleIndex::N64: + shared_memory->style_tag.lagoon.Assign(1); + shared_memory->device_type.fullkey.Assign(1); + shared_memory->applet_footer_type = AppletFooterUiType::Lagon; + break; + case Core::HID::NpadStyleIndex::SegaGenesis: + shared_memory->style_tag.lager.Assign(1); + shared_memory->device_type.fullkey.Assign(1); + break; + default: + break; + } + + controller.is_connected = true; + controller.device->Connect(); + controller.device->SetLedPattern(); + if (controller_type == Core::HID::NpadStyleIndex::JoyconDual) { + if (controller.is_dual_left_connected) { + controller.device->SetPollingMode(Core::HID::EmulatedDeviceIndex::LeftIndex, + Common::Input::PollingMode::Active); + } + if (controller.is_dual_right_connected) { + controller.device->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex, + Common::Input::PollingMode::Active); + } + } else { + controller.device->SetPollingMode(Core::HID::EmulatedDeviceIndex::AllDevices, + Common::Input::PollingMode::Active); + } + + npad_resource.SignalStyleSetUpdateEvent(aruid, npad_id); + WriteEmptyEntry(controller.shared_memory); + hid_core.SetLastActiveController(npad_id); +} + +void NPad::WriteEmptyEntry(NpadInternalState* npad) { + NPadGenericState dummy_pad_state{}; + NpadGcTriggerState dummy_gc_state{}; + dummy_pad_state.sampling_number = npad->fullkey_lifo.ReadCurrentEntry().sampling_number + 1; + npad->fullkey_lifo.WriteNextEntry(dummy_pad_state); + dummy_pad_state.sampling_number = npad->handheld_lifo.ReadCurrentEntry().sampling_number + 1; + npad->handheld_lifo.WriteNextEntry(dummy_pad_state); + dummy_pad_state.sampling_number = npad->joy_dual_lifo.ReadCurrentEntry().sampling_number + 1; + npad->joy_dual_lifo.WriteNextEntry(dummy_pad_state); + dummy_pad_state.sampling_number = npad->joy_left_lifo.ReadCurrentEntry().sampling_number + 1; + npad->joy_left_lifo.WriteNextEntry(dummy_pad_state); + dummy_pad_state.sampling_number = npad->joy_right_lifo.ReadCurrentEntry().sampling_number + 1; + npad->joy_right_lifo.WriteNextEntry(dummy_pad_state); + dummy_pad_state.sampling_number = npad->palma_lifo.ReadCurrentEntry().sampling_number + 1; + npad->palma_lifo.WriteNextEntry(dummy_pad_state); + dummy_pad_state.sampling_number = npad->system_ext_lifo.ReadCurrentEntry().sampling_number + 1; + npad->system_ext_lifo.WriteNextEntry(dummy_pad_state); + dummy_gc_state.sampling_number = npad->gc_trigger_lifo.ReadCurrentEntry().sampling_number + 1; + npad->gc_trigger_lifo.WriteNextEntry(dummy_gc_state); +} + +void NPad::RequestPadStateUpdate(u64 aruid, Core::HID::NpadIdType npad_id) { + std::scoped_lock lock{*applet_resource_holder.shared_mutex}; + auto& controller = GetControllerFromNpadIdType(aruid, npad_id); + const auto controller_type = controller.device->GetNpadStyleIndex(); + + if (!controller.device->IsConnected() && controller.is_connected) { + DisconnectNpad(aruid, npad_id); + return; + } + if (!controller.device->IsConnected()) { + return; + } + if (controller.device->IsConnected() && !controller.is_connected) { + InitNewlyAddedController(aruid, npad_id); + } + + // This function is unique to yuzu for the turbo buttons and motion to work properly + controller.device->StatusUpdate(); + + auto& pad_entry = controller.npad_pad_state; + auto& trigger_entry = controller.npad_trigger_state; + const auto button_state = controller.device->GetNpadButtons(); + const auto stick_state = controller.device->GetSticks(); + + using btn = Core::HID::NpadButton; + pad_entry.npad_buttons.raw = btn::None; + if (controller_type != Core::HID::NpadStyleIndex::JoyconLeft) { + constexpr btn right_button_mask = btn::A | btn::B | btn::X | btn::Y | btn::StickR | btn::R | + btn::ZR | btn::Plus | btn::StickRLeft | btn::StickRUp | + btn::StickRRight | btn::StickRDown; + pad_entry.npad_buttons.raw = button_state.raw & right_button_mask; + pad_entry.r_stick = stick_state.right; + } + + if (controller_type != Core::HID::NpadStyleIndex::JoyconRight) { + constexpr btn left_button_mask = + btn::Left | btn::Up | btn::Right | btn::Down | btn::StickL | btn::L | btn::ZL | + btn::Minus | btn::StickLLeft | btn::StickLUp | btn::StickLRight | btn::StickLDown; + pad_entry.npad_buttons.raw |= button_state.raw & left_button_mask; + pad_entry.l_stick = stick_state.left; + } + + if (controller_type == Core::HID::NpadStyleIndex::JoyconLeft || + controller_type == Core::HID::NpadStyleIndex::JoyconDual) { + pad_entry.npad_buttons.left_sl.Assign(button_state.left_sl); + pad_entry.npad_buttons.left_sr.Assign(button_state.left_sr); + } + + if (controller_type == Core::HID::NpadStyleIndex::JoyconRight || + controller_type == Core::HID::NpadStyleIndex::JoyconDual) { + pad_entry.npad_buttons.right_sl.Assign(button_state.right_sl); + pad_entry.npad_buttons.right_sr.Assign(button_state.right_sr); + } + + if (controller_type == Core::HID::NpadStyleIndex::GameCube) { + const auto& trigger_state = controller.device->GetTriggers(); + trigger_entry.l_analog = trigger_state.left; + trigger_entry.r_analog = trigger_state.right; + pad_entry.npad_buttons.zl.Assign(false); + pad_entry.npad_buttons.zr.Assign(button_state.r); + pad_entry.npad_buttons.l.Assign(button_state.zl); + pad_entry.npad_buttons.r.Assign(button_state.zr); + } + + if (pad_entry.npad_buttons.raw != Core::HID::NpadButton::None) { + hid_core.SetLastActiveController(npad_id); + } +} + +void NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing) { + if (ref_counter == 0) { + return; + } + + std::scoped_lock lock{*applet_resource_holder.shared_mutex}; + for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; ++aruid_index) { + const auto* data = applet_resource_holder.applet_resource->GetAruidDataByIndex(aruid_index); + const auto aruid = data->aruid; + + if (!data->flag.is_assigned) { + continue; + } + + for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) { + auto& controller = controller_data[aruid_index][i]; + controller.shared_memory = + &data->shared_memory_format->npad.npad_entry[i].internal_state; + auto* npad = controller.shared_memory; + + const auto& controller_type = controller.device->GetNpadStyleIndex(); + + if (controller_type == Core::HID::NpadStyleIndex::None || + !controller.device->IsConnected()) { + continue; + } + + RequestPadStateUpdate(aruid, controller.device->GetNpadIdType()); + auto& pad_state = controller.npad_pad_state; + auto& libnx_state = controller.npad_libnx_state; + auto& trigger_state = controller.npad_trigger_state; + + // LibNX exclusively uses this section, so we always update it since LibNX doesn't + // activate any controllers. + libnx_state.connection_status.raw = 0; + libnx_state.connection_status.is_connected.Assign(1); + switch (controller_type) { + case Core::HID::NpadStyleIndex::None: + ASSERT(false); + break; + case Core::HID::NpadStyleIndex::ProController: + case Core::HID::NpadStyleIndex::NES: + case Core::HID::NpadStyleIndex::SNES: + case Core::HID::NpadStyleIndex::N64: + case Core::HID::NpadStyleIndex::SegaGenesis: + pad_state.connection_status.raw = 0; + pad_state.connection_status.is_connected.Assign(1); + pad_state.connection_status.is_wired.Assign(1); + + libnx_state.connection_status.is_wired.Assign(1); + pad_state.sampling_number = + npad->fullkey_lifo.ReadCurrentEntry().state.sampling_number + 1; + npad->fullkey_lifo.WriteNextEntry(pad_state); + break; + case Core::HID::NpadStyleIndex::Handheld: + pad_state.connection_status.raw = 0; + pad_state.connection_status.is_connected.Assign(1); + pad_state.connection_status.is_wired.Assign(1); + pad_state.connection_status.is_left_connected.Assign(1); + pad_state.connection_status.is_right_connected.Assign(1); + pad_state.connection_status.is_left_wired.Assign(1); + pad_state.connection_status.is_right_wired.Assign(1); + + libnx_state.connection_status.is_wired.Assign(1); + libnx_state.connection_status.is_left_connected.Assign(1); + libnx_state.connection_status.is_right_connected.Assign(1); + libnx_state.connection_status.is_left_wired.Assign(1); + libnx_state.connection_status.is_right_wired.Assign(1); + pad_state.sampling_number = + npad->handheld_lifo.ReadCurrentEntry().state.sampling_number + 1; + npad->handheld_lifo.WriteNextEntry(pad_state); + break; + case Core::HID::NpadStyleIndex::JoyconDual: + pad_state.connection_status.raw = 0; + pad_state.connection_status.is_connected.Assign(1); + if (controller.is_dual_left_connected) { + pad_state.connection_status.is_left_connected.Assign(1); + libnx_state.connection_status.is_left_connected.Assign(1); + } + if (controller.is_dual_right_connected) { + pad_state.connection_status.is_right_connected.Assign(1); + libnx_state.connection_status.is_right_connected.Assign(1); + } + + pad_state.sampling_number = + npad->joy_dual_lifo.ReadCurrentEntry().state.sampling_number + 1; + npad->joy_dual_lifo.WriteNextEntry(pad_state); + break; + case Core::HID::NpadStyleIndex::JoyconLeft: + pad_state.connection_status.raw = 0; + pad_state.connection_status.is_connected.Assign(1); + pad_state.connection_status.is_left_connected.Assign(1); + + libnx_state.connection_status.is_left_connected.Assign(1); + pad_state.sampling_number = + npad->joy_left_lifo.ReadCurrentEntry().state.sampling_number + 1; + npad->joy_left_lifo.WriteNextEntry(pad_state); + break; + case Core::HID::NpadStyleIndex::JoyconRight: + pad_state.connection_status.raw = 0; + pad_state.connection_status.is_connected.Assign(1); + pad_state.connection_status.is_right_connected.Assign(1); + + libnx_state.connection_status.is_right_connected.Assign(1); + pad_state.sampling_number = + npad->joy_right_lifo.ReadCurrentEntry().state.sampling_number + 1; + npad->joy_right_lifo.WriteNextEntry(pad_state); + break; + case Core::HID::NpadStyleIndex::GameCube: + pad_state.connection_status.raw = 0; + pad_state.connection_status.is_connected.Assign(1); + pad_state.connection_status.is_wired.Assign(1); + + libnx_state.connection_status.is_wired.Assign(1); + pad_state.sampling_number = + npad->fullkey_lifo.ReadCurrentEntry().state.sampling_number + 1; + trigger_state.sampling_number = + npad->gc_trigger_lifo.ReadCurrentEntry().state.sampling_number + 1; + npad->fullkey_lifo.WriteNextEntry(pad_state); + npad->gc_trigger_lifo.WriteNextEntry(trigger_state); + break; + case Core::HID::NpadStyleIndex::Pokeball: + pad_state.connection_status.raw = 0; + pad_state.connection_status.is_connected.Assign(1); + pad_state.sampling_number = + npad->palma_lifo.ReadCurrentEntry().state.sampling_number + 1; + npad->palma_lifo.WriteNextEntry(pad_state); + break; + default: + break; + } + + libnx_state.npad_buttons.raw = pad_state.npad_buttons.raw; + libnx_state.l_stick = pad_state.l_stick; + libnx_state.r_stick = pad_state.r_stick; + npad->system_ext_lifo.WriteNextEntry(pad_state); + + press_state |= static_cast(pad_state.npad_buttons.raw); + } + } +} + +Result NPad::SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet supported_style_set) { + std::scoped_lock lock{mutex}; + hid_core.SetSupportedStyleTag({supported_style_set}); + const Result result = npad_resource.SetSupportedNpadStyleSet(aruid, supported_style_set); + if (result.IsSuccess()) { + OnUpdate({}); + } + return result; +} + +Result NPad::GetSupportedNpadStyleSet(u64 aruid, + Core::HID::NpadStyleSet& out_supported_style_set) const { + std::scoped_lock lock{mutex}; + const Result result = npad_resource.GetSupportedNpadStyleSet(out_supported_style_set, aruid); + + if (result == ResultUndefinedStyleset) { + out_supported_style_set = Core::HID::NpadStyleSet::None; + return ResultSuccess; + } + + return result; +} + +Result NPad::GetMaskedSupportedNpadStyleSet( + u64 aruid, Core::HID::NpadStyleSet& out_supported_style_set) const { + std::scoped_lock lock{mutex}; + const Result result = + npad_resource.GetMaskedSupportedNpadStyleSet(out_supported_style_set, aruid); + + if (result == ResultUndefinedStyleset) { + out_supported_style_set = Core::HID::NpadStyleSet::None; + return ResultSuccess; + } + + return result; +} + +Result NPad::SetSupportedNpadIdType(u64 aruid, + std::span supported_npad_list) { + std::scoped_lock lock{mutex}; + if (supported_npad_list.size() > MaxSupportedNpadIdTypes) { + return ResultInvalidArraySize; + } + + Result result = npad_resource.SetSupportedNpadIdType(aruid, supported_npad_list); + + if (result.IsSuccess()) { + OnUpdate({}); + } + + return result; +} + +Result NPad::SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type) { + std::scoped_lock lock{mutex}; + return npad_resource.SetNpadJoyHoldType(aruid, hold_type); +} + +Result NPad::GetNpadJoyHoldType(u64 aruid, NpadJoyHoldType& out_hold_type) const { + std::scoped_lock lock{mutex}; + return npad_resource.GetNpadJoyHoldType(out_hold_type, aruid); +} + +Result NPad::SetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode mode) { + std::scoped_lock lock{mutex}; + Result result = npad_resource.SetNpadHandheldActivationMode(aruid, mode); + if (result.IsSuccess()) { + OnUpdate({}); + } + return result; +} + +Result NPad::GetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode& out_mode) const { + std::scoped_lock lock{mutex}; + return npad_resource.GetNpadHandheldActivationMode(out_mode, aruid); +} + +bool NPad::SetNpadMode(u64 aruid, Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType npad_id, + NpadJoyDeviceType npad_device_type, NpadJoyAssignmentMode assignment_mode) { + if (!IsNpadIdValid(npad_id)) { + LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); + return false; + } + + auto& controller = GetControllerFromNpadIdType(aruid, npad_id); + if (controller.shared_memory->assignment_mode != assignment_mode) { + controller.shared_memory->assignment_mode = assignment_mode; + } + + if (!controller.device->IsConnected()) { + return false; + } + + if (assignment_mode == NpadJoyAssignmentMode::Dual) { + if (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconLeft) { + DisconnectNpad(aruid, npad_id); + controller.is_dual_left_connected = true; + controller.is_dual_right_connected = false; + UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, npad_id, true); + return false; + } + if (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconRight) { + DisconnectNpad(aruid, npad_id); + controller.is_dual_left_connected = false; + controller.is_dual_right_connected = true; + UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, npad_id, true); + return false; + } + return false; + } + + // This is for NpadJoyAssignmentMode::Single + + // Only JoyconDual get affected by this function + if (controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::JoyconDual) { + return false; + } + + if (controller.is_dual_left_connected && !controller.is_dual_right_connected) { + DisconnectNpad(aruid, npad_id); + UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconLeft, npad_id, true); + return false; + } + if (!controller.is_dual_left_connected && controller.is_dual_right_connected) { + DisconnectNpad(aruid, npad_id); + UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconRight, npad_id, true); + return false; + } + + // We have two controllers connected to the same npad_id we need to split them + new_npad_id = hid_core.GetFirstDisconnectedNpadId(); + auto& controller_2 = GetControllerFromNpadIdType(aruid, new_npad_id); + DisconnectNpad(aruid, npad_id); + if (npad_device_type == NpadJoyDeviceType::Left) { + UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconLeft, npad_id, true); + controller_2.is_dual_left_connected = false; + controller_2.is_dual_right_connected = true; + UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, new_npad_id, true); + } else { + UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconRight, npad_id, true); + controller_2.is_dual_left_connected = true; + controller_2.is_dual_right_connected = false; + UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, new_npad_id, true); + } + return true; +} + +bool NPad::VibrateControllerAtIndex(u64 aruid, Core::HID::NpadIdType npad_id, + std::size_t device_index, + const Core::HID::VibrationValue& vibration_value) { + auto& controller = GetControllerFromNpadIdType(aruid, npad_id); + if (!controller.device->IsConnected()) { + return false; + } + + if (!controller.device->IsVibrationEnabled(device_index)) { + if (controller.vibration[device_index].latest_vibration_value.low_amplitude != 0.0f || + controller.vibration[device_index].latest_vibration_value.high_amplitude != 0.0f) { + // Send an empty vibration to stop any vibrations. + Core::HID::VibrationValue vibration{0.0f, 160.0f, 0.0f, 320.0f}; + controller.device->SetVibration(device_index, vibration); + // Then reset the vibration value to its default value. + controller.vibration[device_index].latest_vibration_value = + Core::HID::DEFAULT_VIBRATION_VALUE; + } + + return false; + } + + if (!Settings::values.enable_accurate_vibrations.GetValue()) { + using std::chrono::duration_cast; + using std::chrono::milliseconds; + using std::chrono::steady_clock; + + const auto now = steady_clock::now(); + + // Filter out non-zero vibrations that are within 15ms of each other. + if ((vibration_value.low_amplitude != 0.0f || vibration_value.high_amplitude != 0.0f) && + duration_cast( + now - controller.vibration[device_index].last_vibration_timepoint) < + milliseconds(15)) { + return false; + } + + controller.vibration[device_index].last_vibration_timepoint = now; + } + + Core::HID::VibrationValue vibration{ + vibration_value.low_amplitude, vibration_value.low_frequency, + vibration_value.high_amplitude, vibration_value.high_frequency}; + return controller.device->SetVibration(device_index, vibration); +} + +void NPad::VibrateController(u64 aruid, + const Core::HID::VibrationDeviceHandle& vibration_device_handle, + const Core::HID::VibrationValue& vibration_value) { + if (IsVibrationHandleValid(vibration_device_handle).IsError()) { + return; + } + + if (!Settings::values.vibration_enabled.GetValue() && !permit_vibration_session_enabled) { + return; + } + + auto& controller = GetControllerFromHandle(aruid, vibration_device_handle); + const auto device_index = static_cast(vibration_device_handle.device_index); + + if (!controller.vibration[device_index].device_mounted || !controller.device->IsConnected()) { + return; + } + + if (vibration_device_handle.device_index == Core::HID::DeviceIndex::None) { + ASSERT_MSG(false, "DeviceIndex should never be None!"); + return; + } + + // Some games try to send mismatched parameters in the device handle, block these. + if ((controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconLeft && + (vibration_device_handle.npad_type == Core::HID::NpadStyleIndex::JoyconRight || + vibration_device_handle.device_index == Core::HID::DeviceIndex::Right)) || + (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconRight && + (vibration_device_handle.npad_type == Core::HID::NpadStyleIndex::JoyconLeft || + vibration_device_handle.device_index == Core::HID::DeviceIndex::Left))) { + return; + } + + // Filter out vibrations with equivalent values to reduce unnecessary state changes. + if (vibration_value.low_amplitude == + controller.vibration[device_index].latest_vibration_value.low_amplitude && + vibration_value.high_amplitude == + controller.vibration[device_index].latest_vibration_value.high_amplitude) { + return; + } + + if (VibrateControllerAtIndex(aruid, controller.device->GetNpadIdType(), device_index, + vibration_value)) { + controller.vibration[device_index].latest_vibration_value = vibration_value; + } +} + +void NPad::VibrateControllers( + u64 aruid, std::span vibration_device_handles, + std::span vibration_values) { + if (!Settings::values.vibration_enabled.GetValue() && !permit_vibration_session_enabled) { + return; + } + + ASSERT_OR_EXECUTE_MSG( + vibration_device_handles.size() == vibration_values.size(), { return; }, + "The amount of device handles does not match with the amount of vibration values," + "this is undefined behavior!"); + + for (std::size_t i = 0; i < vibration_device_handles.size(); ++i) { + VibrateController(aruid, vibration_device_handles[i], vibration_values[i]); + } +} + +Core::HID::VibrationValue NPad::GetLastVibration( + u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const { + if (IsVibrationHandleValid(vibration_device_handle).IsError()) { + return {}; + } + + const auto& controller = GetControllerFromHandle(aruid, vibration_device_handle); + const auto device_index = static_cast(vibration_device_handle.device_index); + return controller.vibration[device_index].latest_vibration_value; +} + +void NPad::InitializeVibrationDevice( + const Core::HID::VibrationDeviceHandle& vibration_device_handle) { + if (IsVibrationHandleValid(vibration_device_handle).IsError()) { + return; + } + + const auto aruid = applet_resource_holder.applet_resource->GetActiveAruid(); + const auto npad_index = static_cast(vibration_device_handle.npad_id); + const auto device_index = static_cast(vibration_device_handle.device_index); + InitializeVibrationDeviceAtIndex(aruid, npad_index, device_index); +} + +void NPad::InitializeVibrationDeviceAtIndex(u64 aruid, Core::HID::NpadIdType npad_id, + std::size_t device_index) { + auto& controller = GetControllerFromNpadIdType(aruid, npad_id); + if (!Settings::values.vibration_enabled.GetValue()) { + controller.vibration[device_index].device_mounted = false; + return; + } + + controller.vibration[device_index].device_mounted = + controller.device->IsVibrationEnabled(device_index); +} + +void NPad::SetPermitVibrationSession(bool permit_vibration_session) { + permit_vibration_session_enabled = permit_vibration_session; +} + +bool NPad::IsVibrationDeviceMounted( + u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const { + if (IsVibrationHandleValid(vibration_device_handle).IsError()) { + return false; + } + + const auto& controller = GetControllerFromHandle(aruid, vibration_device_handle); + const auto device_index = static_cast(vibration_device_handle.device_index); + return controller.vibration[device_index].device_mounted; +} + +Result NPad::AcquireNpadStyleSetUpdateEventHandle(u64 aruid, Kernel::KReadableEvent** out_event, + Core::HID::NpadIdType npad_id) { + std::scoped_lock lock{mutex}; + return npad_resource.AcquireNpadStyleSetUpdateEventHandle(aruid, out_event, npad_id); +} + +void NPad::AddNewControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller, + Core::HID::NpadIdType npad_id) { + UpdateControllerAt(aruid, controller, npad_id, true); +} + +void NPad::UpdateControllerAt(u64 aruid, Core::HID::NpadStyleIndex type, + Core::HID::NpadIdType npad_id, bool connected) { + auto& controller = GetControllerFromNpadIdType(aruid, npad_id); + if (!connected) { + DisconnectNpad(aruid, npad_id); + return; + } + + controller.device->SetNpadStyleIndex(type); + InitNewlyAddedController(aruid, npad_id); +} + +Result NPad::DisconnectNpad(u64 aruid, Core::HID::NpadIdType npad_id) { + if (!IsNpadIdValid(npad_id)) { + LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); + return ResultInvalidNpadId; + } + + LOG_DEBUG(Service_HID, "Npad disconnected {}", npad_id); + auto& controller = GetControllerFromNpadIdType(aruid, npad_id); + for (std::size_t device_idx = 0; device_idx < controller.vibration.size(); ++device_idx) { + // Send an empty vibration to stop any vibrations. + VibrateControllerAtIndex(aruid, npad_id, device_idx, {}); + controller.vibration[device_idx].device_mounted = false; + } + + auto* shared_memory = controller.shared_memory; + // Don't reset shared_memory->assignment_mode this value is persistent + shared_memory->style_tag.raw = Core::HID::NpadStyleSet::None; // Zero out + shared_memory->device_type.raw = 0; + shared_memory->system_properties.raw = 0; + shared_memory->button_properties.raw = 0; + shared_memory->sixaxis_fullkey_properties.raw = 0; + shared_memory->sixaxis_handheld_properties.raw = 0; + shared_memory->sixaxis_dual_left_properties.raw = 0; + shared_memory->sixaxis_dual_right_properties.raw = 0; + shared_memory->sixaxis_left_properties.raw = 0; + shared_memory->sixaxis_right_properties.raw = 0; + shared_memory->battery_level_dual = Core::HID::NpadBatteryLevel::Empty; + shared_memory->battery_level_left = Core::HID::NpadBatteryLevel::Empty; + shared_memory->battery_level_right = Core::HID::NpadBatteryLevel::Empty; + shared_memory->fullkey_color = { + .attribute = ColorAttribute::NoController, + .fullkey = {}, + }; + shared_memory->joycon_color = { + .attribute = ColorAttribute::NoController, + .left = {}, + .right = {}, + }; + shared_memory->applet_footer_type = AppletFooterUiType::None; + + controller.is_dual_left_connected = true; + controller.is_dual_right_connected = true; + controller.is_connected = false; + controller.device->Disconnect(); + npad_resource.SignalStyleSetUpdateEvent(aruid, npad_id); + WriteEmptyEntry(shared_memory); + return ResultSuccess; +} + +Result NPad::IsFirmwareUpdateAvailableForSixAxisSensor( + u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle, + bool& is_firmware_available) const { + const auto is_valid = IsSixaxisHandleValid(sixaxis_handle); + if (is_valid.IsError()) { + LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw); + return is_valid; + } + + const auto& sixaxis_properties = GetSixaxisProperties(aruid, sixaxis_handle); + is_firmware_available = sixaxis_properties.is_firmware_update_available != 0; + return ResultSuccess; +} + +Result NPad::ResetIsSixAxisSensorDeviceNewlyAssigned( + u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle) { + const auto is_valid = IsSixaxisHandleValid(sixaxis_handle); + if (is_valid.IsError()) { + LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw); + return is_valid; + } + + auto& sixaxis_properties = GetSixaxisProperties(aruid, sixaxis_handle); + sixaxis_properties.is_newly_assigned.Assign(0); + + return ResultSuccess; +} + +Result NPad::MergeSingleJoyAsDualJoy(u64 aruid, Core::HID::NpadIdType npad_id_1, + Core::HID::NpadIdType npad_id_2) { + if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) { + LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id_1:{}, npad_id_2:{}", npad_id_1, + npad_id_2); + return ResultInvalidNpadId; + } + auto& controller_1 = GetControllerFromNpadIdType(aruid, npad_id_1); + auto& controller_2 = GetControllerFromNpadIdType(aruid, npad_id_2); + auto controller_style_1 = controller_1.device->GetNpadStyleIndex(); + auto controller_style_2 = controller_2.device->GetNpadStyleIndex(); + + // Simplify this code by converting dualjoycon with only a side connected to single joycons + if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconDual) { + if (controller_1.is_dual_left_connected && !controller_1.is_dual_right_connected) { + controller_style_1 = Core::HID::NpadStyleIndex::JoyconLeft; + } + if (!controller_1.is_dual_left_connected && controller_1.is_dual_right_connected) { + controller_style_1 = Core::HID::NpadStyleIndex::JoyconRight; + } + } + if (controller_style_2 == Core::HID::NpadStyleIndex::JoyconDual) { + if (controller_2.is_dual_left_connected && !controller_2.is_dual_right_connected) { + controller_style_2 = Core::HID::NpadStyleIndex::JoyconLeft; + } + if (!controller_2.is_dual_left_connected && controller_2.is_dual_right_connected) { + controller_style_2 = Core::HID::NpadStyleIndex::JoyconRight; + } + } + + // Invalid merge errors + if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconDual || + controller_style_2 == Core::HID::NpadStyleIndex::JoyconDual) { + return NpadIsDualJoycon; + } + if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconLeft && + controller_style_2 == Core::HID::NpadStyleIndex::JoyconLeft) { + return NpadIsSameType; + } + if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconRight && + controller_style_2 == Core::HID::NpadStyleIndex::JoyconRight) { + return NpadIsSameType; + } + + // These exceptions are handled as if they where dual joycon + if (controller_style_1 != Core::HID::NpadStyleIndex::JoyconLeft && + controller_style_1 != Core::HID::NpadStyleIndex::JoyconRight) { + return NpadIsDualJoycon; + } + if (controller_style_2 != Core::HID::NpadStyleIndex::JoyconLeft && + controller_style_2 != Core::HID::NpadStyleIndex::JoyconRight) { + return NpadIsDualJoycon; + } + + // Disconnect the joycons and connect them as dual joycon at the first index. + DisconnectNpad(aruid, npad_id_1); + DisconnectNpad(aruid, npad_id_2); + controller_1.is_dual_left_connected = true; + controller_1.is_dual_right_connected = true; + AddNewControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, npad_id_1); + return ResultSuccess; +} + +Result NPad::StartLrAssignmentMode(u64 aruid) { + std::scoped_lock lock{mutex}; + bool is_enabled{}; + Result result = npad_resource.GetLrAssignmentMode(is_enabled, aruid); + if (result.IsSuccess() && is_enabled == false) { + result = npad_resource.SetLrAssignmentMode(aruid, true); + } + return result; +} + +Result NPad::StopLrAssignmentMode(u64 aruid) { + std::scoped_lock lock{mutex}; + bool is_enabled{}; + Result result = npad_resource.GetLrAssignmentMode(is_enabled, aruid); + if (result.IsSuccess() && is_enabled == true) { + result = npad_resource.SetLrAssignmentMode(aruid, false); + } + return result; +} + +Result NPad::SwapNpadAssignment(u64 aruid, Core::HID::NpadIdType npad_id_1, + Core::HID::NpadIdType npad_id_2) { + if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) { + LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id_1:{}, npad_id_2:{}", npad_id_1, + npad_id_2); + return ResultInvalidNpadId; + } + if (npad_id_1 == Core::HID::NpadIdType::Handheld || + npad_id_2 == Core::HID::NpadIdType::Handheld || npad_id_1 == Core::HID::NpadIdType::Other || + npad_id_2 == Core::HID::NpadIdType::Other) { + return ResultSuccess; + } + const auto& controller_1 = GetControllerFromNpadIdType(aruid, npad_id_1).device; + const auto& controller_2 = GetControllerFromNpadIdType(aruid, npad_id_2).device; + const auto type_index_1 = controller_1->GetNpadStyleIndex(); + const auto type_index_2 = controller_2->GetNpadStyleIndex(); + const auto is_connected_1 = controller_1->IsConnected(); + const auto is_connected_2 = controller_2->IsConnected(); + + if (!npad_resource.IsControllerSupported(aruid, type_index_1) && is_connected_1) { + return ResultNpadNotConnected; + } + if (!npad_resource.IsControllerSupported(aruid, type_index_2) && is_connected_2) { + return ResultNpadNotConnected; + } + + UpdateControllerAt(aruid, type_index_2, npad_id_1, is_connected_2); + UpdateControllerAt(aruid, type_index_1, npad_id_2, is_connected_1); + + return ResultSuccess; +} + +Result NPad::GetLedPattern(Core::HID::NpadIdType npad_id, Core::HID::LedPattern& pattern) const { + if (!IsNpadIdValid(npad_id)) { + LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); + return ResultInvalidNpadId; + } + const auto aruid = applet_resource_holder.applet_resource->GetActiveAruid(); + const auto& controller = GetControllerFromNpadIdType(aruid, npad_id).device; + pattern = controller->GetLedPattern(); + return ResultSuccess; +} + +Result NPad::IsUnintendedHomeButtonInputProtectionEnabled(bool& out_is_enabled, u64 aruid, + Core::HID::NpadIdType npad_id) const { + std::scoped_lock lock{mutex}; + return npad_resource.GetHomeProtectionEnabled(out_is_enabled, aruid, npad_id); +} + +Result NPad::EnableUnintendedHomeButtonInputProtection(u64 aruid, Core::HID::NpadIdType npad_id, + bool is_enabled) { + std::scoped_lock lock{mutex}; + return npad_resource.SetHomeProtectionEnabled(aruid, npad_id, is_enabled); +} + +void NPad::SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled) { + std::scoped_lock lock{mutex}; + npad_resource.SetNpadAnalogStickUseCenterClamp(aruid, is_enabled); +} + +void NPad::ClearAllConnectedControllers() { + for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) { + for (auto& controller : controller_data[aruid_index]) { + if (controller.device->IsConnected() && + controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::None) { + controller.device->Disconnect(); + controller.device->SetNpadStyleIndex(Core::HID::NpadStyleIndex::None); + } + } + } +} + +void NPad::DisconnectAllConnectedControllers() { + for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) { + for (auto& controller : controller_data[aruid_index]) { + controller.device->Disconnect(); + } + } +} + +void NPad::ConnectAllDisconnectedControllers() { + for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) { + for (auto& controller : controller_data[aruid_index]) { + if (controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::None && + !controller.device->IsConnected()) { + controller.device->Connect(); + } + } + } +} + +void NPad::ClearAllControllers() { + for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) { + for (auto& controller : controller_data[aruid_index]) { + controller.device->Disconnect(); + controller.device->SetNpadStyleIndex(Core::HID::NpadStyleIndex::None); + } + } +} + +Core::HID::NpadButton NPad::GetAndResetPressState() { + return static_cast(press_state.exchange(0)); +} + +Result NPad::ApplyNpadSystemCommonPolicy(u64 aruid) { + std::scoped_lock lock{mutex}; + const Result result = npad_resource.ApplyNpadSystemCommonPolicy(aruid, false); + if (result.IsSuccess()) { + OnUpdate({}); + } + return result; +} + +Result NPad::ApplyNpadSystemCommonPolicyFull(u64 aruid) { + std::scoped_lock lock{mutex}; + const Result result = npad_resource.ApplyNpadSystemCommonPolicy(aruid, true); + if (result.IsSuccess()) { + OnUpdate({}); + } + return result; +} + +Result NPad::ClearNpadSystemCommonPolicy(u64 aruid) { + std::scoped_lock lock{mutex}; + const Result result = npad_resource.ClearNpadSystemCommonPolicy(aruid); + if (result.IsSuccess()) { + OnUpdate({}); + } + return result; +} + +void NPad::SetRevision(u64 aruid, NpadRevision revision) { + npad_resource.SetNpadRevision(aruid, revision); +} + +NpadRevision NPad::GetRevision(u64 aruid) { + return npad_resource.GetNpadRevision(aruid); +} + +Result NPad::RegisterAppletResourceUserId(u64 aruid) { + return npad_resource.RegisterAppletResourceUserId(aruid); +} + +void NPad::UnregisterAppletResourceUserId(u64 aruid) { + npad_resource.UnregisterAppletResourceUserId(aruid); +} + +void NPad::SetNpadExternals(std::shared_ptr resource, + std::recursive_mutex* shared_mutex) { + applet_resource_holder.applet_resource = resource; + applet_resource_holder.shared_mutex = shared_mutex; + applet_resource_holder.shared_npad_resource = &npad_resource; +} + +NPad::NpadControllerData& NPad::GetControllerFromHandle( + u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle) { + const auto npad_id = static_cast(device_handle.npad_id); + return GetControllerFromNpadIdType(aruid, npad_id); +} + +const NPad::NpadControllerData& NPad::GetControllerFromHandle( + u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle) const { + const auto npad_id = static_cast(device_handle.npad_id); + return GetControllerFromNpadIdType(aruid, npad_id); +} + +NPad::NpadControllerData& NPad::GetControllerFromHandle( + u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) { + const auto npad_id = static_cast(device_handle.npad_id); + return GetControllerFromNpadIdType(aruid, npad_id); +} + +const NPad::NpadControllerData& NPad::GetControllerFromHandle( + u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const { + const auto npad_id = static_cast(device_handle.npad_id); + return GetControllerFromNpadIdType(aruid, npad_id); +} + +NPad::NpadControllerData& NPad::GetControllerFromNpadIdType(u64 aruid, + Core::HID::NpadIdType npad_id) { + if (!IsNpadIdValid(npad_id)) { + LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); + npad_id = Core::HID::NpadIdType::Player1; + } + const auto npad_index = NpadIdTypeToIndex(npad_id); + const auto aruid_index = applet_resource_holder.applet_resource->GetIndexFromAruid(aruid); + return controller_data[aruid_index][npad_index]; +} + +const NPad::NpadControllerData& NPad::GetControllerFromNpadIdType( + u64 aruid, Core::HID::NpadIdType npad_id) const { + if (!IsNpadIdValid(npad_id)) { + LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); + npad_id = Core::HID::NpadIdType::Player1; + } + const auto npad_index = NpadIdTypeToIndex(npad_id); + const auto aruid_index = applet_resource_holder.applet_resource->GetIndexFromAruid(aruid); + return controller_data[aruid_index][npad_index]; +} + +Core::HID::SixAxisSensorProperties& NPad::GetSixaxisProperties( + u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle) { + auto& controller = GetControllerFromHandle(aruid, sixaxis_handle); + switch (sixaxis_handle.npad_type) { + case Core::HID::NpadStyleIndex::ProController: + case Core::HID::NpadStyleIndex::Pokeball: + return controller.shared_memory->sixaxis_fullkey_properties; + case Core::HID::NpadStyleIndex::Handheld: + return controller.shared_memory->sixaxis_handheld_properties; + case Core::HID::NpadStyleIndex::JoyconDual: + if (sixaxis_handle.device_index == Core::HID::DeviceIndex::Left) { + return controller.shared_memory->sixaxis_dual_left_properties; + } + return controller.shared_memory->sixaxis_dual_right_properties; + case Core::HID::NpadStyleIndex::JoyconLeft: + return controller.shared_memory->sixaxis_left_properties; + case Core::HID::NpadStyleIndex::JoyconRight: + return controller.shared_memory->sixaxis_right_properties; + default: + return controller.shared_memory->sixaxis_fullkey_properties; + } +} + +const Core::HID::SixAxisSensorProperties& NPad::GetSixaxisProperties( + u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle) const { + const auto& controller = GetControllerFromHandle(aruid, sixaxis_handle); + switch (sixaxis_handle.npad_type) { + case Core::HID::NpadStyleIndex::ProController: + case Core::HID::NpadStyleIndex::Pokeball: + return controller.shared_memory->sixaxis_fullkey_properties; + case Core::HID::NpadStyleIndex::Handheld: + return controller.shared_memory->sixaxis_handheld_properties; + case Core::HID::NpadStyleIndex::JoyconDual: + if (sixaxis_handle.device_index == Core::HID::DeviceIndex::Left) { + return controller.shared_memory->sixaxis_dual_left_properties; + } + return controller.shared_memory->sixaxis_dual_right_properties; + case Core::HID::NpadStyleIndex::JoyconLeft: + return controller.shared_memory->sixaxis_left_properties; + case Core::HID::NpadStyleIndex::JoyconRight: + return controller.shared_memory->sixaxis_right_properties; + default: + return controller.shared_memory->sixaxis_fullkey_properties; + } +} + +AppletDetailedUiType NPad::GetAppletDetailedUiType(Core::HID::NpadIdType npad_id) { + const auto aruid = applet_resource_holder.applet_resource->GetActiveAruid(); + const auto& shared_memory = GetControllerFromNpadIdType(aruid, npad_id).shared_memory; + + return { + .ui_variant = 0, + .footer = shared_memory->applet_footer_type, + }; +} + +} // namespace Service::HID diff --git a/src/hid_core/resources/npad/npad.h b/src/hid_core/resources/npad/npad.h new file mode 100644 index 000000000..58f8c7acf --- /dev/null +++ b/src/hid_core/resources/npad/npad.h @@ -0,0 +1,214 @@ +// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include +#include +#include + +#include "common/common_types.h" +#include "hid_core/hid_types.h" +#include "hid_core/resources/controller_base.h" +#include "hid_core/resources/npad/npad_resource.h" +#include "hid_core/resources/npad/npad_types.h" + +namespace Core::HID { +class EmulatedController; +enum class ControllerTriggerType; +} // namespace Core::HID + +namespace Kernel { +class KEvent; +class KReadableEvent; +} // namespace Kernel + +namespace Service::KernelHelpers { +class ServiceContext; +} // namespace Service::KernelHelpers + +union Result; + +namespace Service::HID { +class AppletResource; +struct NpadInternalState; +struct NpadSixAxisSensorLifo; +struct NpadSharedMemoryFormat; + +class NPad final { +public: + explicit NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_); + ~NPad(); + + Result Activate(); + Result Activate(u64 aruid); + + Result ActivateNpadResource(); + Result ActivateNpadResource(u64 aruid); + + // When the controller is requesting an update for the shared memory + void OnUpdate(const Core::Timing::CoreTiming& core_timing); + + Result SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet supported_style_set); + Result GetSupportedNpadStyleSet(u64 aruid, + Core::HID::NpadStyleSet& out_supported_style_set) const; + Result GetMaskedSupportedNpadStyleSet(u64 aruid, + Core::HID::NpadStyleSet& out_supported_style_set) const; + + Result SetSupportedNpadIdType(u64 aruid, + std::span supported_npad_list); + + Result SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type); + Result GetNpadJoyHoldType(u64 aruid, NpadJoyHoldType& out_hold_type) const; + + Result SetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode mode); + Result GetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode& out_mode) const; + + bool SetNpadMode(u64 aruid, Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType npad_id, + NpadJoyDeviceType npad_device_type, NpadJoyAssignmentMode assignment_mode); + + bool VibrateControllerAtIndex(u64 aruid, Core::HID::NpadIdType npad_id, + std::size_t device_index, + const Core::HID::VibrationValue& vibration_value); + + void VibrateController(u64 aruid, + const Core::HID::VibrationDeviceHandle& vibration_device_handle, + const Core::HID::VibrationValue& vibration_value); + + void VibrateControllers( + u64 aruid, std::span vibration_device_handles, + std::span vibration_values); + + Core::HID::VibrationValue GetLastVibration( + u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const; + + void InitializeVibrationDevice(const Core::HID::VibrationDeviceHandle& vibration_device_handle); + + void InitializeVibrationDeviceAtIndex(u64 aruid, Core::HID::NpadIdType npad_id, + std::size_t device_index); + + void SetPermitVibrationSession(bool permit_vibration_session); + + bool IsVibrationDeviceMounted( + u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const; + + Result AcquireNpadStyleSetUpdateEventHandle(u64 aruid, Kernel::KReadableEvent** out_event, + Core::HID::NpadIdType npad_id); + + // Adds a new controller at an index. + void AddNewControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller, + Core::HID::NpadIdType npad_id); + // Adds a new controller at an index with connection status. + void UpdateControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller, + Core::HID::NpadIdType npad_id, bool connected); + + Result DisconnectNpad(u64 aruid, Core::HID::NpadIdType npad_id); + + Result IsFirmwareUpdateAvailableForSixAxisSensor( + u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle, + bool& is_firmware_available) const; + Result ResetIsSixAxisSensorDeviceNewlyAssigned( + u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle); + + Result GetLedPattern(Core::HID::NpadIdType npad_id, Core::HID::LedPattern& pattern) const; + + Result IsUnintendedHomeButtonInputProtectionEnabled(bool& out_is_enabled, u64 aruid, + Core::HID::NpadIdType npad_id) const; + Result EnableUnintendedHomeButtonInputProtection(u64 aruid, Core::HID::NpadIdType npad_id, + bool is_enabled); + + void SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled); + void ClearAllConnectedControllers(); + void DisconnectAllConnectedControllers(); + void ConnectAllDisconnectedControllers(); + void ClearAllControllers(); + + Result MergeSingleJoyAsDualJoy(u64 aruid, Core::HID::NpadIdType npad_id_1, + Core::HID::NpadIdType npad_id_2); + Result StartLrAssignmentMode(u64 aruid); + Result StopLrAssignmentMode(u64 aruid); + Result SwapNpadAssignment(u64 aruid, Core::HID::NpadIdType npad_id_1, + Core::HID::NpadIdType npad_id_2); + + // Logical OR for all buttons presses on all controllers + // Specifically for cheat engine and other features. + Core::HID::NpadButton GetAndResetPressState(); + + Result ApplyNpadSystemCommonPolicy(u64 aruid); + Result ApplyNpadSystemCommonPolicyFull(u64 aruid); + Result ClearNpadSystemCommonPolicy(u64 aruid); + + void SetRevision(u64 aruid, NpadRevision revision); + NpadRevision GetRevision(u64 aruid); + + Result RegisterAppletResourceUserId(u64 aruid); + void UnregisterAppletResourceUserId(u64 aruid); + void SetNpadExternals(std::shared_ptr resource, + std::recursive_mutex* shared_mutex); + + AppletDetailedUiType GetAppletDetailedUiType(Core::HID::NpadIdType npad_id); + +private: + struct VibrationData { + bool device_mounted{}; + Core::HID::VibrationValue latest_vibration_value{}; + std::chrono::steady_clock::time_point last_vibration_timepoint{}; + }; + + struct NpadControllerData { + NpadInternalState* shared_memory = nullptr; + Core::HID::EmulatedController* device = nullptr; + + std::array vibration{}; + bool is_connected{}; + + // Dual joycons can have only one side connected + bool is_dual_left_connected{true}; + bool is_dual_right_connected{true}; + + // Current pad state + NPadGenericState npad_pad_state{}; + NPadGenericState npad_libnx_state{}; + NpadGcTriggerState npad_trigger_state{}; + int callback_key{}; + }; + + void ControllerUpdate(Core::HID::ControllerTriggerType type, std::size_t controller_idx); + void InitNewlyAddedController(u64 aruid, Core::HID::NpadIdType npad_id); + void RequestPadStateUpdate(u64 aruid, Core::HID::NpadIdType npad_id); + void WriteEmptyEntry(NpadInternalState* npad); + + NpadControllerData& GetControllerFromHandle( + u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle); + const NpadControllerData& GetControllerFromHandle( + u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle) const; + NpadControllerData& GetControllerFromHandle( + u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle); + const NpadControllerData& GetControllerFromHandle( + u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const; + NpadControllerData& GetControllerFromNpadIdType(u64 aruid, Core::HID::NpadIdType npad_id); + const NpadControllerData& GetControllerFromNpadIdType(u64 aruid, + Core::HID::NpadIdType npad_id) const; + + Core::HID::SixAxisSensorProperties& GetSixaxisProperties( + u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle); + const Core::HID::SixAxisSensorProperties& GetSixaxisProperties( + u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const; + + Core::HID::HIDCore& hid_core; + KernelHelpers::ServiceContext& service_context; + + s32 ref_counter{}; + mutable std::mutex mutex; + NPadResource npad_resource; + AppletResourceHolder applet_resource_holder{}; + Kernel::KEvent* input_event{nullptr}; + std::mutex* input_mutex{nullptr}; + + std::atomic press_state{}; + bool permit_vibration_session_enabled; + std::array, AruidIndexMax> + controller_data{}; +}; +} // namespace Service::HID diff --git a/src/hid_core/resources/npad/npad_data.cpp b/src/hid_core/resources/npad/npad_data.cpp new file mode 100644 index 000000000..c7e9760cb --- /dev/null +++ b/src/hid_core/resources/npad/npad_data.cpp @@ -0,0 +1,228 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "hid_core/hid_util.h" +#include "hid_core/resources/npad/npad_data.h" + +namespace Service::HID { + +NPadData::NPadData() { + ClearNpadSystemCommonPolicy(); +} + +NPadData::~NPadData() = default; + +NpadStatus NPadData::GetNpadStatus() const { + return status; +} + +void NPadData::SetNpadAnalogStickUseCenterClamp(bool is_enabled) { + status.use_center_clamp.Assign(is_enabled); +} + +bool NPadData::GetNpadAnalogStickUseCenterClamp() const { + return status.use_center_clamp.As(); +} + +void NPadData::SetNpadSystemExtStateEnabled(bool is_enabled) { + status.system_ext_state.Assign(is_enabled); +} + +bool NPadData::GetNpadSystemExtState() const { + return status.system_ext_state.As(); +} + +Result NPadData::SetSupportedNpadIdType(std::span list) { + // Note: Real limit is 11. But array size is 10. N's bug? + if (list.size() > MaxSupportedNpadIdTypes) { + return ResultInvalidArraySize; + } + + supported_npad_id_types_count = list.size(); + memcpy(supported_npad_id_types.data(), list.data(), + list.size() * sizeof(Core::HID::NpadIdType)); + + return ResultSuccess; +} + +std::size_t NPadData::GetSupportedNpadIdType(std::span out_list) const { + std::size_t out_size = std::min(supported_npad_id_types_count, out_list.size()); + + memcpy(out_list.data(), supported_npad_id_types.data(), + out_size * sizeof(Core::HID::NpadIdType)); + + return out_size; +} + +bool NPadData::IsNpadIdTypeSupported(Core::HID::NpadIdType npad_id) const { + for (std::size_t i = 0; i < supported_npad_id_types_count; i++) { + if (supported_npad_id_types[i] == npad_id) { + return true; + } + } + + return false; +} + +void NPadData::SetNpadSystemCommonPolicy(bool is_full_policy) { + supported_npad_style_set = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::JoyDual | + Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System; + handheld_activation_mode = NpadHandheldActivationMode::Dual; + + status.is_supported_styleset_set.Assign(true); + status.is_hold_type_set.Assign(true); + status.lr_assignment_mode.Assign(false); + status.is_policy.Assign(true); + if (is_full_policy) { + status.is_full_policy.Assign(true); + } + + supported_npad_id_types_count = 10; + supported_npad_id_types[0] = Core::HID::NpadIdType::Player1; + supported_npad_id_types[1] = Core::HID::NpadIdType::Player2; + supported_npad_id_types[2] = Core::HID::NpadIdType::Player3; + supported_npad_id_types[3] = Core::HID::NpadIdType::Player4; + supported_npad_id_types[4] = Core::HID::NpadIdType::Player5; + supported_npad_id_types[5] = Core::HID::NpadIdType::Player6; + supported_npad_id_types[6] = Core::HID::NpadIdType::Player7; + supported_npad_id_types[7] = Core::HID::NpadIdType::Player8; + supported_npad_id_types[8] = Core::HID::NpadIdType::Other; + supported_npad_id_types[9] = Core::HID::NpadIdType::Handheld; + + for (auto& input_protection : is_unintended_home_button_input_protection) { + input_protection = true; + } +} + +void NPadData::ClearNpadSystemCommonPolicy() { + status.raw = 0; + supported_npad_style_set = Core::HID::NpadStyleSet::All; + npad_hold_type = NpadJoyHoldType::Vertical; + handheld_activation_mode = NpadHandheldActivationMode::Dual; + + for (auto& button_assignment : npad_button_assignment) { + button_assignment = Core::HID::NpadButton::None; + } + + supported_npad_id_types_count = 10; + supported_npad_id_types[0] = Core::HID::NpadIdType::Player1; + supported_npad_id_types[1] = Core::HID::NpadIdType::Player2; + supported_npad_id_types[2] = Core::HID::NpadIdType::Player3; + supported_npad_id_types[3] = Core::HID::NpadIdType::Player4; + supported_npad_id_types[4] = Core::HID::NpadIdType::Player5; + supported_npad_id_types[5] = Core::HID::NpadIdType::Player6; + supported_npad_id_types[6] = Core::HID::NpadIdType::Player7; + supported_npad_id_types[7] = Core::HID::NpadIdType::Player8; + supported_npad_id_types[8] = Core::HID::NpadIdType::Other; + supported_npad_id_types[9] = Core::HID::NpadIdType::Handheld; + + for (auto& input_protection : is_unintended_home_button_input_protection) { + input_protection = true; + } +} + +void NPadData::SetNpadJoyHoldType(NpadJoyHoldType hold_type) { + npad_hold_type = hold_type; + status.is_hold_type_set.Assign(true); +} + +NpadJoyHoldType NPadData::GetNpadJoyHoldType() const { + return npad_hold_type; +} + +void NPadData::SetHandheldActivationMode(NpadHandheldActivationMode activation_mode) { + handheld_activation_mode = activation_mode; +} + +NpadHandheldActivationMode NPadData::GetHandheldActivationMode() const { + return handheld_activation_mode; +} + +void NPadData::SetSupportedNpadStyleSet(Core::HID::NpadStyleSet style_set) { + supported_npad_style_set = style_set; + status.is_supported_styleset_set.Assign(true); + status.is_hold_type_set.Assign(true); +} + +Core::HID::NpadStyleSet NPadData::GetSupportedNpadStyleSet() const { + return supported_npad_style_set; +} + +bool NPadData::IsNpadStyleIndexSupported(Core::HID::NpadStyleIndex style_index) const { + Core::HID::NpadStyleTag style = {supported_npad_style_set}; + switch (style_index) { + case Core::HID::NpadStyleIndex::ProController: + return style.fullkey.As(); + case Core::HID::NpadStyleIndex::Handheld: + return style.handheld.As(); + case Core::HID::NpadStyleIndex::JoyconDual: + return style.joycon_dual.As(); + case Core::HID::NpadStyleIndex::JoyconLeft: + return style.joycon_left.As(); + case Core::HID::NpadStyleIndex::JoyconRight: + return style.joycon_right.As(); + case Core::HID::NpadStyleIndex::GameCube: + return style.gamecube.As(); + case Core::HID::NpadStyleIndex::Pokeball: + return style.palma.As(); + case Core::HID::NpadStyleIndex::NES: + return style.lark.As(); + case Core::HID::NpadStyleIndex::SNES: + return style.lucia.As(); + case Core::HID::NpadStyleIndex::N64: + return style.lagoon.As(); + case Core::HID::NpadStyleIndex::SegaGenesis: + return style.lager.As(); + default: + return false; + } +} + +void NPadData::SetLrAssignmentMode(bool is_enabled) { + status.lr_assignment_mode.Assign(is_enabled); +} + +bool NPadData::GetLrAssignmentMode() const { + return status.lr_assignment_mode.As(); +} + +void NPadData::SetAssigningSingleOnSlSrPress(bool is_enabled) { + status.assigning_single_on_sl_sr_press.Assign(is_enabled); +} + +bool NPadData::GetAssigningSingleOnSlSrPress() const { + return status.assigning_single_on_sl_sr_press.As(); +} + +void NPadData::SetHomeProtectionEnabled(bool is_enabled, Core::HID::NpadIdType npad_id) { + is_unintended_home_button_input_protection[NpadIdTypeToIndex(npad_id)] = is_enabled; +} + +bool NPadData::GetHomeProtectionEnabled(Core::HID::NpadIdType npad_id) const { + return is_unintended_home_button_input_protection[NpadIdTypeToIndex(npad_id)]; +} + +void NPadData::SetCaptureButtonAssignment(Core::HID::NpadButton button_assignment, + std::size_t style_index) { + npad_button_assignment[style_index] = button_assignment; +} + +Core::HID::NpadButton NPadData::GetCaptureButtonAssignment(std::size_t style_index) const { + return npad_button_assignment[style_index]; +} + +std::size_t NPadData::GetNpadCaptureButtonAssignmentList( + std::span out_list) const { + for (std::size_t i = 0; i < out_list.size(); i++) { + Core::HID::NpadStyleSet style_set = GetStylesetByIndex(i); + if ((style_set & supported_npad_style_set) == Core::HID::NpadStyleSet::None || + npad_button_assignment[i] == Core::HID::NpadButton::None) { + return i; + } + out_list[i] = npad_button_assignment[i]; + } + + return out_list.size(); +} + +} // namespace Service::HID diff --git a/src/hid_core/resources/npad/npad_data.h b/src/hid_core/resources/npad/npad_data.h new file mode 100644 index 000000000..86bd3b81c --- /dev/null +++ b/src/hid_core/resources/npad/npad_data.h @@ -0,0 +1,88 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include +#include + +#include "common/common_types.h" +#include "core/hle/result.h" +#include "hid_core/hid_types.h" +#include "hid_core/resources/npad/npad_types.h" + +namespace Service::HID { + +struct NpadStatus { + union { + u32 raw{}; + + BitField<0, 1, u32> is_supported_styleset_set; + BitField<1, 1, u32> is_hold_type_set; + BitField<2, 1, u32> lr_assignment_mode; + BitField<3, 1, u32> assigning_single_on_sl_sr_press; + BitField<4, 1, u32> is_full_policy; + BitField<5, 1, u32> is_policy; + BitField<6, 1, u32> use_center_clamp; + BitField<7, 1, u32> system_ext_state; + }; +}; +static_assert(sizeof(NpadStatus) == 4, "NpadStatus is an invalid size"); + +/// Handles Npad request from HID interfaces +class NPadData final { +public: + explicit NPadData(); + ~NPadData(); + + NpadStatus GetNpadStatus() const; + + void SetNpadAnalogStickUseCenterClamp(bool is_enabled); + bool GetNpadAnalogStickUseCenterClamp() const; + + void SetNpadSystemExtStateEnabled(bool is_enabled); + bool GetNpadSystemExtState() const; + + Result SetSupportedNpadIdType(std::span list); + std::size_t GetSupportedNpadIdType(std::span out_list) const; + bool IsNpadIdTypeSupported(Core::HID::NpadIdType npad_id) const; + + void SetNpadSystemCommonPolicy(bool is_full_policy); + void ClearNpadSystemCommonPolicy(); + + void SetNpadJoyHoldType(NpadJoyHoldType hold_type); + NpadJoyHoldType GetNpadJoyHoldType() const; + + void SetHandheldActivationMode(NpadHandheldActivationMode activation_mode); + NpadHandheldActivationMode GetHandheldActivationMode() const; + + void SetSupportedNpadStyleSet(Core::HID::NpadStyleSet style_set); + Core::HID::NpadStyleSet GetSupportedNpadStyleSet() const; + bool IsNpadStyleIndexSupported(Core::HID::NpadStyleIndex style_index) const; + + void SetLrAssignmentMode(bool is_enabled); + bool GetLrAssignmentMode() const; + + void SetAssigningSingleOnSlSrPress(bool is_enabled); + bool GetAssigningSingleOnSlSrPress() const; + + void SetHomeProtectionEnabled(bool is_enabled, Core::HID::NpadIdType npad_id); + bool GetHomeProtectionEnabled(Core::HID::NpadIdType npad_id) const; + + void SetCaptureButtonAssignment(Core::HID::NpadButton button_assignment, + std::size_t style_index); + Core::HID::NpadButton GetCaptureButtonAssignment(std::size_t style_index) const; + std::size_t GetNpadCaptureButtonAssignmentList(std::span out_list) const; + +private: + NpadStatus status{}; + Core::HID::NpadStyleSet supported_npad_style_set{Core::HID::NpadStyleSet::All}; + NpadJoyHoldType npad_hold_type{NpadJoyHoldType::Vertical}; + NpadHandheldActivationMode handheld_activation_mode{}; + std::array supported_npad_id_types{}; + std::array npad_button_assignment{}; + std::size_t supported_npad_id_types_count{}; + std::array is_unintended_home_button_input_protection{}; +}; + +} // namespace Service::HID diff --git a/src/hid_core/resources/npad/npad_resource.cpp b/src/hid_core/resources/npad/npad_resource.cpp new file mode 100644 index 000000000..b0255a05c --- /dev/null +++ b/src/hid_core/resources/npad/npad_resource.cpp @@ -0,0 +1,685 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "core/hle/kernel/k_event.h" +#include "core/hle/kernel/k_readable_event.h" +#include "hid_core/hid_result.h" +#include "hid_core/hid_util.h" +#include "hid_core/resources/npad/npad_resource.h" +#include "hid_core/resources/npad/npad_types.h" + +namespace Service::HID { + +NPadResource::NPadResource(KernelHelpers::ServiceContext& context) : service_context{context} {} + +NPadResource::~NPadResource() = default; + +Result NPadResource::RegisterAppletResourceUserId(u64 aruid) { + const auto aruid_index = GetIndexFromAruid(aruid); + if (aruid_index < AruidIndexMax) { + return ResultAruidAlreadyRegistered; + } + + std::size_t data_index = AruidIndexMax; + for (std::size_t i = 0; i < AruidIndexMax; i++) { + if (!state[i].flag.is_initialized) { + data_index = i; + break; + } + } + + if (data_index == AruidIndexMax) { + return ResultAruidNoAvailableEntries; + } + + auto& aruid_data = state[data_index]; + + aruid_data.aruid = aruid; + aruid_data.flag.is_initialized.Assign(true); + + data_index = AruidIndexMax; + for (std::size_t i = 0; i < AruidIndexMax; i++) { + if (registration_list.flag[i] == RegistrationStatus::Initialized) { + if (registration_list.aruid[i] != aruid) { + continue; + } + data_index = i; + break; + } + if (registration_list.flag[i] == RegistrationStatus::None) { + data_index = i; + break; + } + } + + if (data_index == AruidIndexMax) { + return ResultSuccess; + } + + registration_list.flag[data_index] = RegistrationStatus::Initialized; + registration_list.aruid[data_index] = aruid; + + return ResultSuccess; +} + +void NPadResource::UnregisterAppletResourceUserId(u64 aruid) { + const u64 aruid_index = GetIndexFromAruid(aruid); + + DestroyStyleSetUpdateEvents(aruid); + if (aruid_index < AruidIndexMax) { + state[aruid_index] = {}; + registration_list.flag[aruid_index] = RegistrationStatus::PendingDelete; + } +} + +void NPadResource::DestroyStyleSetUpdateEvents(u64 aruid) { + const u64 aruid_index = GetIndexFromAruid(aruid); + + if (aruid_index >= AruidIndexMax) { + return; + } + + for (auto& controller_state : state[aruid_index].controller_state) { + if (!controller_state.is_styleset_update_event_initialized) { + continue; + } + service_context.CloseEvent(controller_state.style_set_update_event); + controller_state.is_styleset_update_event_initialized = false; + } +} + +Result NPadResource::Activate(u64 aruid) { + const u64 aruid_index = GetIndexFromAruid(aruid); + + if (aruid_index >= AruidIndexMax) { + return ResultSuccess; + } + + auto& state_data = state[aruid_index]; + + if (state_data.flag.is_assigned) { + return ResultAruidAlreadyRegistered; + } + + state_data.flag.is_assigned.Assign(true); + state_data.data.ClearNpadSystemCommonPolicy(); + state_data.npad_revision = NpadRevision::Revision0; + state_data.button_config = {}; + + if (active_data_aruid == aruid) { + default_hold_type = active_data.GetNpadJoyHoldType(); + active_data.SetNpadJoyHoldType(default_hold_type); + } + return ResultSuccess; +} + +Result NPadResource::Activate() { + if (ref_counter == std::numeric_limits::max() - 1) { + return ResultAppletResourceOverflow; + } + if (ref_counter == 0) { + RegisterAppletResourceUserId(SystemAruid); + Activate(SystemAruid); + } + ref_counter++; + return ResultSuccess; +} + +Result NPadResource::Deactivate() { + if (ref_counter == 0) { + return ResultAppletResourceNotInitialized; + } + + UnregisterAppletResourceUserId(SystemAruid); + ref_counter--; + return ResultSuccess; +} + +NPadData* NPadResource::GetActiveData() { + return &active_data; +} + +u64 NPadResource::GetActiveDataAruid() { + return active_data_aruid; +} + +void NPadResource::SetAppletResourceUserId(u64 aruid) { + if (active_data_aruid == aruid) { + return; + } + + active_data_aruid = aruid; + default_hold_type = active_data.GetNpadJoyHoldType(); + const u64 aruid_index = GetIndexFromAruid(aruid); + + if (aruid_index >= AruidIndexMax) { + return; + } + + auto& data = state[aruid_index].data; + if (data.GetNpadStatus().is_policy || data.GetNpadStatus().is_full_policy) { + data.SetNpadJoyHoldType(default_hold_type); + } + + active_data = data; + if (data.GetNpadStatus().is_hold_type_set) { + active_data.SetNpadJoyHoldType(default_hold_type); + } +} + +std::size_t NPadResource::GetIndexFromAruid(u64 aruid) const { + for (std::size_t i = 0; i < AruidIndexMax; i++) { + if (registration_list.flag[i] == RegistrationStatus::Initialized && + registration_list.aruid[i] == aruid) { + return i; + } + } + return AruidIndexMax; +} + +Result NPadResource::ApplyNpadSystemCommonPolicy(u64 aruid, bool is_full_policy) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + auto& data = state[aruid_index].data; + data.SetNpadSystemCommonPolicy(is_full_policy); + data.SetNpadJoyHoldType(default_hold_type); + if (active_data_aruid == aruid) { + active_data.SetNpadSystemCommonPolicy(is_full_policy); + active_data.SetNpadJoyHoldType(default_hold_type); + } + return ResultSuccess; +} + +Result NPadResource::ClearNpadSystemCommonPolicy(u64 aruid) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + state[aruid_index].data.ClearNpadSystemCommonPolicy(); + if (active_data_aruid == aruid) { + active_data.ClearNpadSystemCommonPolicy(); + } + return ResultSuccess; +} + +Result NPadResource::SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet style_set) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + auto& data = state[aruid_index].data; + data.SetSupportedNpadStyleSet(style_set); + if (active_data_aruid == aruid) { + active_data.SetSupportedNpadStyleSet(style_set); + active_data.SetNpadJoyHoldType(data.GetNpadJoyHoldType()); + } + return ResultSuccess; +} + +Result NPadResource::GetSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_Set, + u64 aruid) const { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + auto& data = state[aruid_index].data; + if (!data.GetNpadStatus().is_supported_styleset_set) { + return ResultUndefinedStyleset; + } + + out_style_Set = data.GetSupportedNpadStyleSet(); + return ResultSuccess; +} + +Result NPadResource::GetMaskedSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_set, + u64 aruid) const { + if (aruid == SystemAruid) { + out_style_set = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld | + Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft | + Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Palma | + Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System; + return ResultSuccess; + } + + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + auto& data = state[aruid_index].data; + if (!data.GetNpadStatus().is_supported_styleset_set) { + return ResultUndefinedStyleset; + } + + Core::HID::NpadStyleSet mask{Core::HID::NpadStyleSet::None}; + out_style_set = data.GetSupportedNpadStyleSet(); + + switch (state[aruid_index].npad_revision) { + case NpadRevision::Revision1: + mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld | + Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft | + Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc | + Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::SystemExt | + Core::HID::NpadStyleSet::System; + break; + case NpadRevision::Revision2: + mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld | + Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft | + Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc | + Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark | + Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System; + break; + case NpadRevision::Revision3: + mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld | + Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft | + Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc | + Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark | + Core::HID::NpadStyleSet::HandheldLark | Core::HID::NpadStyleSet::Lucia | + Core::HID::NpadStyleSet::Lagoon | Core::HID::NpadStyleSet::Lager | + Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System; + break; + default: + mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld | + Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft | + Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::SystemExt | + Core::HID::NpadStyleSet::System; + break; + } + + out_style_set = out_style_set & mask; + return ResultSuccess; +} + +Result NPadResource::GetAvailableStyleset(Core::HID::NpadStyleSet& out_style_set, u64 aruid) const { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + auto& data = state[aruid_index].data; + if (!data.GetNpadStatus().is_supported_styleset_set) { + return ResultUndefinedStyleset; + } + + Core::HID::NpadStyleSet mask{Core::HID::NpadStyleSet::None}; + out_style_set = data.GetSupportedNpadStyleSet(); + + switch (state[aruid_index].npad_revision) { + case NpadRevision::Revision1: + mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld | + Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft | + Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc | + Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::SystemExt | + Core::HID::NpadStyleSet::System; + break; + case NpadRevision::Revision2: + mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld | + Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft | + Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc | + Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark | + Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System; + break; + case NpadRevision::Revision3: + mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld | + Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft | + Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc | + Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark | + Core::HID::NpadStyleSet::HandheldLark | Core::HID::NpadStyleSet::Lucia | + Core::HID::NpadStyleSet::Lagoon | Core::HID::NpadStyleSet::Lager | + Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System; + break; + default: + mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld | + Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft | + Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::SystemExt | + Core::HID::NpadStyleSet::System; + break; + } + + out_style_set = out_style_set & mask; + return ResultSuccess; +} + +NpadRevision NPadResource::GetNpadRevision(u64 aruid) const { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return NpadRevision::Revision0; + } + + return state[aruid_index].npad_revision; +} + +Result NPadResource::IsSupportedNpadStyleSet(bool& is_set, u64 aruid) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + is_set = state[aruid_index].data.GetNpadStatus().is_supported_styleset_set.Value() != 0; + return ResultSuccess; +} + +Result NPadResource::SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + state[aruid_index].data.SetNpadJoyHoldType(hold_type); + if (active_data_aruid == aruid) { + active_data.SetNpadJoyHoldType(hold_type); + } + return ResultSuccess; +} + +Result NPadResource::GetNpadJoyHoldType(NpadJoyHoldType& hold_type, u64 aruid) const { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + auto& data = state[aruid_index].data; + if (data.GetNpadStatus().is_policy || data.GetNpadStatus().is_full_policy) { + hold_type = active_data.GetNpadJoyHoldType(); + return ResultSuccess; + } + hold_type = data.GetNpadJoyHoldType(); + return ResultSuccess; +} + +Result NPadResource::SetNpadHandheldActivationMode(u64 aruid, + NpadHandheldActivationMode activation_mode) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + state[aruid_index].data.SetHandheldActivationMode(activation_mode); + if (active_data_aruid == aruid) { + active_data.SetHandheldActivationMode(activation_mode); + } + return ResultSuccess; +} + +Result NPadResource::GetNpadHandheldActivationMode(NpadHandheldActivationMode& activation_mode, + u64 aruid) const { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + activation_mode = state[aruid_index].data.GetHandheldActivationMode(); + return ResultSuccess; +} + +Result NPadResource::SetSupportedNpadIdType( + u64 aruid, std::span supported_npad_list) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + if (supported_npad_list.size() > MaxSupportedNpadIdTypes) { + return ResultInvalidArraySize; + } + + Result result = state[aruid_index].data.SetSupportedNpadIdType(supported_npad_list); + if (result.IsSuccess() && active_data_aruid == aruid) { + result = active_data.SetSupportedNpadIdType(supported_npad_list); + } + + return result; +} + +bool NPadResource::IsControllerSupported(u64 aruid, Core::HID::NpadStyleIndex style_index) const { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return false; + } + return state[aruid_index].data.IsNpadStyleIndexSupported(style_index); +} + +Result NPadResource::SetLrAssignmentMode(u64 aruid, bool is_enabled) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + state[aruid_index].data.SetLrAssignmentMode(is_enabled); + if (active_data_aruid == aruid) { + active_data.SetLrAssignmentMode(is_enabled); + } + return ResultSuccess; +} + +Result NPadResource::GetLrAssignmentMode(bool& is_enabled, u64 aruid) const { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + is_enabled = state[aruid_index].data.GetLrAssignmentMode(); + return ResultSuccess; +} + +Result NPadResource::SetAssigningSingleOnSlSrPress(u64 aruid, bool is_enabled) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + state[aruid_index].data.SetAssigningSingleOnSlSrPress(is_enabled); + if (active_data_aruid == aruid) { + active_data.SetAssigningSingleOnSlSrPress(is_enabled); + } + return ResultSuccess; +} + +Result NPadResource::IsAssigningSingleOnSlSrPressEnabled(bool& is_enabled, u64 aruid) const { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + is_enabled = state[aruid_index].data.GetAssigningSingleOnSlSrPress(); + return ResultSuccess; +} + +Result NPadResource::AcquireNpadStyleSetUpdateEventHandle(u64 aruid, + Kernel::KReadableEvent** out_event, + Core::HID::NpadIdType npad_id) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + auto& controller_state = state[aruid_index].controller_state[NpadIdTypeToIndex(npad_id)]; + if (!controller_state.is_styleset_update_event_initialized) { + // Auto clear = true + controller_state.style_set_update_event = + service_context.CreateEvent("NpadResource:StylesetUpdateEvent"); + + // Assume creating the event succeeds otherwise crash the system here + controller_state.is_styleset_update_event_initialized = true; + } + + *out_event = &controller_state.style_set_update_event->GetReadableEvent(); + + if (controller_state.is_styleset_update_event_initialized) { + controller_state.style_set_update_event->Signal(); + } + + return ResultSuccess; +} + +Result NPadResource::SignalStyleSetUpdateEvent(u64 aruid, Core::HID::NpadIdType npad_id) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + auto controller = state[aruid_index].controller_state[NpadIdTypeToIndex(npad_id)]; + if (controller.is_styleset_update_event_initialized) { + controller.style_set_update_event->Signal(); + } + return ResultSuccess; +} + +Result NPadResource::GetHomeProtectionEnabled(bool& is_enabled, u64 aruid, + Core::HID::NpadIdType npad_id) const { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + is_enabled = state[aruid_index].data.GetHomeProtectionEnabled(npad_id); + return ResultSuccess; +} + +Result NPadResource::SetHomeProtectionEnabled(u64 aruid, Core::HID::NpadIdType npad_id, + bool is_enabled) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + state[aruid_index].data.SetHomeProtectionEnabled(is_enabled, npad_id); + if (active_data_aruid == aruid) { + active_data.SetHomeProtectionEnabled(is_enabled, npad_id); + } + return ResultSuccess; +} + +Result NPadResource::SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + state[aruid_index].data.SetNpadAnalogStickUseCenterClamp(is_enabled); + if (active_data_aruid == aruid) { + active_data.SetNpadAnalogStickUseCenterClamp(is_enabled); + } + return ResultSuccess; +} + +Result NPadResource::SetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id, std::size_t index, + Core::HID::NpadButton button_config) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + state[aruid_index].button_config[NpadIdTypeToIndex(npad_id)][index] = button_config; + return ResultSuccess; +} + +Core::HID::NpadButton NPadResource::GetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id, + std::size_t index, Core::HID::NpadButton mask, + bool is_enabled) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return Core::HID::NpadButton::None; + } + + auto& button_config = state[aruid_index].button_config[NpadIdTypeToIndex(npad_id)][index]; + if (is_enabled) { + button_config = button_config | mask; + return button_config; + } + + button_config = Core::HID::NpadButton::None; + return Core::HID::NpadButton::None; +} + +void NPadResource::ResetButtonConfig() { + for (auto& selected_state : state) { + selected_state.button_config = {}; + } +} + +Result NPadResource::SetNpadCaptureButtonAssignment(u64 aruid, + Core::HID::NpadStyleSet npad_style_set, + Core::HID::NpadButton button_assignment) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + // Must be a power of two + const auto raw_styleset = static_cast(npad_style_set); + if (raw_styleset == 0 && (raw_styleset & (raw_styleset - 1)) != 0) { + return ResultMultipleStyleSetSelected; + } + + std::size_t style_index{}; + Core::HID::NpadStyleSet style_selected{}; + for (style_index = 0; style_index < StyleIndexCount; ++style_index) { + style_selected = GetStylesetByIndex(style_index); + if (npad_style_set == style_selected) { + break; + } + } + + if (style_selected == Core::HID::NpadStyleSet::None) { + return ResultMultipleStyleSetSelected; + } + + state[aruid_index].data.SetCaptureButtonAssignment(button_assignment, style_index); + if (active_data_aruid == aruid) { + active_data.SetCaptureButtonAssignment(button_assignment, style_index); + } + return ResultSuccess; +} + +Result NPadResource::ClearNpadCaptureButtonAssignment(u64 aruid) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + for (std::size_t i = 0; i < StyleIndexCount; i++) { + state[aruid_index].data.SetCaptureButtonAssignment(Core::HID::NpadButton::None, i); + if (active_data_aruid == aruid) { + active_data.SetCaptureButtonAssignment(Core::HID::NpadButton::None, i); + } + } + return ResultSuccess; +} + +std::size_t NPadResource::GetNpadCaptureButtonAssignment(std::span out_list, + u64 aruid) const { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return 0; + } + return state[aruid_index].data.GetNpadCaptureButtonAssignmentList(out_list); +} + +void NPadResource::SetNpadRevision(u64 aruid, NpadRevision revision) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return; + } + + state[aruid_index].npad_revision = revision; +} + +Result NPadResource::SetNpadSystemExtStateEnabled(u64 aruid, bool is_enabled) { + const u64 aruid_index = GetIndexFromAruid(aruid); + if (aruid_index >= AruidIndexMax) { + return ResultNpadNotConnected; + } + + state[aruid_index].data.SetNpadAnalogStickUseCenterClamp(is_enabled); + if (active_data_aruid == aruid) { + active_data.SetNpadAnalogStickUseCenterClamp(is_enabled); + } + return ResultSuccess; +} + +} // namespace Service::HID diff --git a/src/hid_core/resources/npad/npad_resource.h b/src/hid_core/resources/npad/npad_resource.h new file mode 100644 index 000000000..aed89eec6 --- /dev/null +++ b/src/hid_core/resources/npad/npad_resource.h @@ -0,0 +1,132 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include +#include +#include + +#include "common/common_types.h" +#include "core/hle/result.h" +#include "core/hle/service/kernel_helpers.h" +#include "hid_core/hid_types.h" +#include "hid_core/resources/applet_resource.h" +#include "hid_core/resources/npad/npad_data.h" +#include "hid_core/resources/npad/npad_types.h" + +namespace Core { +class System; +} + +namespace Kernel { +class KReadableEvent; +} + +namespace Service::HID { +struct DataStatusFlag; + +struct NpadControllerState { + bool is_styleset_update_event_initialized{}; + INSERT_PADDING_BYTES(0x7); + Kernel::KEvent* style_set_update_event{nullptr}; + INSERT_PADDING_BYTES(0x27); +}; + +struct NpadState { + DataStatusFlag flag{}; + u64 aruid{}; + NPadData data{}; + std::array, MaxSupportedNpadIdTypes> + button_config; + std::array controller_state; + NpadRevision npad_revision; +}; + +/// Handles Npad request from HID interfaces +class NPadResource final { +public: + explicit NPadResource(KernelHelpers::ServiceContext& context); + ~NPadResource(); + + NPadData* GetActiveData(); + u64 GetActiveDataAruid(); + + Result RegisterAppletResourceUserId(u64 aruid); + void UnregisterAppletResourceUserId(u64 aruid); + + void DestroyStyleSetUpdateEvents(u64 aruid); + + Result Activate(u64 aruid); + Result Activate(); + Result Deactivate(); + + void SetAppletResourceUserId(u64 aruid); + std::size_t GetIndexFromAruid(u64 aruid) const; + + Result ApplyNpadSystemCommonPolicy(u64 aruid, bool is_full_policy); + Result ClearNpadSystemCommonPolicy(u64 aruid); + + Result SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet style_set); + Result GetSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_Set, u64 aruid) const; + Result GetMaskedSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_set, u64 aruid) const; + Result GetAvailableStyleset(Core::HID::NpadStyleSet& out_style_set, u64 aruid) const; + + NpadRevision GetNpadRevision(u64 aruid) const; + void SetNpadRevision(u64 aruid, NpadRevision revision); + + Result IsSupportedNpadStyleSet(bool& is_set, u64 aruid); + + Result SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type); + Result GetNpadJoyHoldType(NpadJoyHoldType& hold_type, u64 aruid) const; + + Result SetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode activation_mode); + Result GetNpadHandheldActivationMode(NpadHandheldActivationMode& activation_mode, + u64 aruid) const; + + Result SetSupportedNpadIdType(u64 aruid, + std::span supported_npad_list); + bool IsControllerSupported(u64 aruid, Core::HID::NpadStyleIndex style_index) const; + + Result SetLrAssignmentMode(u64 aruid, bool is_enabled); + Result GetLrAssignmentMode(bool& is_enabled, u64 aruid) const; + + Result SetAssigningSingleOnSlSrPress(u64 aruid, bool is_enabled); + Result IsAssigningSingleOnSlSrPressEnabled(bool& is_enabled, u64 aruid) const; + + Result AcquireNpadStyleSetUpdateEventHandle(u64 aruid, Kernel::KReadableEvent** out_event, + Core::HID::NpadIdType npad_id); + Result SignalStyleSetUpdateEvent(u64 aruid, Core::HID::NpadIdType npad_id); + + Result GetHomeProtectionEnabled(bool& is_enabled, u64 aruid, + Core::HID::NpadIdType npad_id) const; + Result SetHomeProtectionEnabled(u64 aruid, Core::HID::NpadIdType npad_id, bool is_enabled); + + Result SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled); + + Result SetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id, std::size_t index, + Core::HID::NpadButton button_config); + Core::HID::NpadButton GetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id, + std::size_t index, Core::HID::NpadButton mask, + bool is_enabled); + void ResetButtonConfig(); + + Result SetNpadCaptureButtonAssignment(u64 aruid, Core::HID::NpadStyleSet npad_style_set, + Core::HID::NpadButton button_assignment); + Result ClearNpadCaptureButtonAssignment(u64 aruid); + std::size_t GetNpadCaptureButtonAssignment(std::span out_list, + u64 aruid) const; + + Result SetNpadSystemExtStateEnabled(u64 aruid, bool is_enabled); + +private: + NPadData active_data{}; + AruidRegisterList registration_list{}; + std::array state{}; + u64 active_data_aruid{}; + NpadJoyHoldType default_hold_type{}; + s32 ref_counter{}; + + KernelHelpers::ServiceContext& service_context; +}; +} // namespace Service::HID diff --git a/src/hid_core/resources/npad/npad_types.h b/src/hid_core/resources/npad/npad_types.h new file mode 100644 index 000000000..a02f9cf16 --- /dev/null +++ b/src/hid_core/resources/npad/npad_types.h @@ -0,0 +1,255 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include "common/bit_field.h" +#include "common/common_funcs.h" +#include "common/common_types.h" +#include "hid_core/hid_types.h" + +namespace Service::HID { +static constexpr std::size_t MaxSupportedNpadIdTypes = 10; +static constexpr std::size_t StyleIndexCount = 7; + +// This is nn::hid::NpadJoyHoldType +enum class NpadJoyHoldType : u64 { + Vertical = 0, + Horizontal = 1, +}; + +// This is nn::hid::NpadJoyAssignmentMode +enum class NpadJoyAssignmentMode : u32 { + Dual = 0, + Single = 1, +}; + +// This is nn::hid::NpadJoyDeviceType +enum class NpadJoyDeviceType : s64 { + Left = 0, + Right = 1, +}; + +// This is nn::hid::NpadHandheldActivationMode +enum class NpadHandheldActivationMode : u64 { + Dual = 0, + Single = 1, + None = 2, + MaxActivationMode = 3, +}; + +// This is nn::hid::system::AppletFooterUiAttributesSet +struct AppletFooterUiAttributes { + INSERT_PADDING_BYTES(0x4); +}; + +// This is nn::hid::system::AppletFooterUiType +enum class AppletFooterUiType : u8 { + None = 0, + HandheldNone = 1, + HandheldJoyConLeftOnly = 2, + HandheldJoyConRightOnly = 3, + HandheldJoyConLeftJoyConRight = 4, + JoyDual = 5, + JoyDualLeftOnly = 6, + JoyDualRightOnly = 7, + JoyLeftHorizontal = 8, + JoyLeftVertical = 9, + JoyRightHorizontal = 10, + JoyRightVertical = 11, + SwitchProController = 12, + CompatibleProController = 13, + CompatibleJoyCon = 14, + LarkHvc1 = 15, + LarkHvc2 = 16, + LarkNesLeft = 17, + LarkNesRight = 18, + Lucia = 19, + Verification = 20, + Lagon = 21, +}; + +using AppletFooterUiVariant = u8; + +// This is "nn::hid::system::AppletDetailedUiType". +struct AppletDetailedUiType { + AppletFooterUiVariant ui_variant; + INSERT_PADDING_BYTES(0x2); + AppletFooterUiType footer; +}; +static_assert(sizeof(AppletDetailedUiType) == 0x4, "AppletDetailedUiType is an invalid size"); +// This is nn::hid::NpadCommunicationMode +enum class NpadCommunicationMode : u64 { + Mode_5ms = 0, + Mode_10ms = 1, + Mode_15ms = 2, + Default = 3, +}; + +enum class NpadRevision : u32 { + Revision0 = 0, + Revision1 = 1, + Revision2 = 2, + Revision3 = 3, +}; + +// This is nn::hid::detail::ColorAttribute +enum class ColorAttribute : u32 { + Ok = 0, + ReadError = 1, + NoController = 2, +}; +static_assert(sizeof(ColorAttribute) == 4, "ColorAttribute is an invalid size"); + +// This is nn::hid::detail::NpadFullKeyColorState +struct NpadFullKeyColorState { + ColorAttribute attribute{ColorAttribute::NoController}; + Core::HID::NpadControllerColor fullkey{}; +}; +static_assert(sizeof(NpadFullKeyColorState) == 0xC, "NpadFullKeyColorState is an invalid size"); + +// This is nn::hid::detail::NpadJoyColorState +struct NpadJoyColorState { + ColorAttribute attribute{ColorAttribute::NoController}; + Core::HID::NpadControllerColor left{}; + Core::HID::NpadControllerColor right{}; +}; +static_assert(sizeof(NpadJoyColorState) == 0x14, "NpadJoyColorState is an invalid size"); + +// This is nn::hid::NpadAttribute +struct NpadAttribute { + union { + u32 raw{}; + BitField<0, 1, u32> is_connected; + BitField<1, 1, u32> is_wired; + BitField<2, 1, u32> is_left_connected; + BitField<3, 1, u32> is_left_wired; + BitField<4, 1, u32> is_right_connected; + BitField<5, 1, u32> is_right_wired; + }; +}; +static_assert(sizeof(NpadAttribute) == 4, "NpadAttribute is an invalid size"); + +// This is nn::hid::NpadFullKeyState +// This is nn::hid::NpadHandheldState +// This is nn::hid::NpadJoyDualState +// This is nn::hid::NpadJoyLeftState +// This is nn::hid::NpadJoyRightState +// This is nn::hid::NpadPalmaState +// This is nn::hid::NpadSystemExtState +struct NPadGenericState { + s64_le sampling_number{}; + Core::HID::NpadButtonState npad_buttons{}; + Core::HID::AnalogStickState l_stick{}; + Core::HID::AnalogStickState r_stick{}; + NpadAttribute connection_status{}; + INSERT_PADDING_BYTES(4); // Reserved +}; +static_assert(sizeof(NPadGenericState) == 0x28, "NPadGenericState is an invalid size"); + +// This is nn::hid::server::NpadGcTriggerState +struct NpadGcTriggerState { + s64 sampling_number{}; + s32 l_analog{}; + s32 r_analog{}; +}; +static_assert(sizeof(NpadGcTriggerState) == 0x10, "NpadGcTriggerState is an invalid size"); + +// This is nn::hid::NpadSystemProperties +struct NPadSystemProperties { + union { + s64 raw{}; + BitField<0, 1, s64> is_charging_joy_dual; + BitField<1, 1, s64> is_charging_joy_left; + BitField<2, 1, s64> is_charging_joy_right; + BitField<3, 1, s64> is_powered_joy_dual; + BitField<4, 1, s64> is_powered_joy_left; + BitField<5, 1, s64> is_powered_joy_right; + BitField<9, 1, s64> is_system_unsupported_button; + BitField<10, 1, s64> is_system_ext_unsupported_button; + BitField<11, 1, s64> is_vertical; + BitField<12, 1, s64> is_horizontal; + BitField<13, 1, s64> use_plus; + BitField<14, 1, s64> use_minus; + BitField<15, 1, s64> use_directional_buttons; + }; +}; +static_assert(sizeof(NPadSystemProperties) == 0x8, "NPadSystemProperties is an invalid size"); + +// This is nn::hid::NpadSystemButtonProperties +struct NpadSystemButtonProperties { + union { + s32 raw{}; + BitField<0, 1, s32> is_home_button_protection_enabled; + }; +}; +static_assert(sizeof(NpadSystemButtonProperties) == 0x4, "NPadButtonProperties is an invalid size"); + +// This is nn::hid::system::DeviceType +struct DeviceType { + union { + u32 raw{}; + BitField<0, 1, s32> fullkey; + BitField<1, 1, s32> debug_pad; + BitField<2, 1, s32> handheld_left; + BitField<3, 1, s32> handheld_right; + BitField<4, 1, s32> joycon_left; + BitField<5, 1, s32> joycon_right; + BitField<6, 1, s32> palma; + BitField<7, 1, s32> lark_hvc_left; + BitField<8, 1, s32> lark_hvc_right; + BitField<9, 1, s32> lark_nes_left; + BitField<10, 1, s32> lark_nes_right; + BitField<11, 1, s32> handheld_lark_hvc_left; + BitField<12, 1, s32> handheld_lark_hvc_right; + 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"); + +// This is nn::hid::NpadLarkType +enum class NpadLarkType : u32 { + Invalid, + H1, + H2, + NL, + NR, +}; + +// This is nn::hid::NpadLuciaType +enum class NpadLuciaType : u32 { + Invalid, + J, + E, + U, +}; + +// This is nn::hid::NpadLagonType +enum class NpadLagonType : u32 { + Invalid, +}; + +// This is nn::hid::NpadLagerType +enum class NpadLagerType : u32 { + Invalid, + J, + E, + U, +}; + +} // namespace Service::HID -- cgit v1.2.3