summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Narr the Reg2024-01-01 15:23:56 -0600
committerGravatar Narr the Reg2024-01-03 20:21:14 -0600
commit6a244465cef86d7329f12dd1dfd5d6fdd415a0ed (patch)
tree0b69faca9aee4ab31705d07d57e55a75b1b2a809
parentMerge pull request #12554 from german77/directconnect (diff)
downloadyuzu-6a244465cef86d7329f12dd1dfd5d6fdd415a0ed.tar.gz
yuzu-6a244465cef86d7329f12dd1dfd5d6fdd415a0ed.tar.xz
yuzu-6a244465cef86d7329f12dd1dfd5d6fdd415a0ed.zip
service: hid: Implement NpadResource and NpadData
-rw-r--r--src/core/CMakeLists.txt4
-rw-r--r--src/core/hid/hid_types.h1
-rw-r--r--src/core/hle/service/hid/controllers/applet_resource.h1
-rw-r--r--src/core/hle/service/hid/controllers/npad.cpp988
-rw-r--r--src/core/hle/service/hid/controllers/npad.h155
-rw-r--r--src/core/hle/service/hid/controllers/npad/npad_data.cpp228
-rw-r--r--src/core/hle/service/hid/controllers/npad/npad_data.h88
-rw-r--r--src/core/hle/service/hid/controllers/npad/npad_resource.cpp685
-rw-r--r--src/core/hle/service/hid/controllers/npad/npad_resource.h132
-rw-r--r--src/core/hle/service/hid/controllers/types/npad_types.h3
-rw-r--r--src/core/hle/service/hid/controllers/types/shared_memory_format.h2
-rw-r--r--src/core/hle/service/hid/errors.h32
-rw-r--r--src/core/hle/service/hid/hid.cpp1
-rw-r--r--src/core/hle/service/hid/hid_server.cpp222
-rw-r--r--src/core/hle/service/hid/hid_system_server.cpp38
-rw-r--r--src/core/hle/service/hid/hid_util.h8
-rw-r--r--src/core/hle/service/hid/irs.cpp2
-rw-r--r--src/core/hle/service/hid/resource_manager.cpp14
-rw-r--r--src/core/hle/service/hid/resource_manager.h2
19 files changed, 1930 insertions, 676 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt
index dfba79267..e2ec2164c 100644
--- a/src/core/CMakeLists.txt
+++ b/src/core/CMakeLists.txt
@@ -549,6 +549,10 @@ add_library(core STATIC
549 hle/service/hid/xcd.cpp 549 hle/service/hid/xcd.cpp
550 hle/service/hid/xcd.h 550 hle/service/hid/xcd.h
551 hle/service/hid/errors.h 551 hle/service/hid/errors.h
552 hle/service/hid/controllers/npad/npad_data.cpp
553 hle/service/hid/controllers/npad/npad_data.h
554 hle/service/hid/controllers/npad/npad_resource.cpp
555 hle/service/hid/controllers/npad/npad_resource.h
552 hle/service/hid/controllers/types/debug_pad_types.h 556 hle/service/hid/controllers/types/debug_pad_types.h
553 hle/service/hid/controllers/types/keyboard_types.h 557 hle/service/hid/controllers/types/keyboard_types.h
554 hle/service/hid/controllers/types/mouse_types.h 558 hle/service/hid/controllers/types/mouse_types.h
diff --git a/src/core/hid/hid_types.h b/src/core/hid/hid_types.h
index 4bf285f36..a81ed6af0 100644
--- a/src/core/hid/hid_types.h
+++ b/src/core/hid/hid_types.h
@@ -267,6 +267,7 @@ enum class NpadStyleSet : u32 {
267 All = 0xFFFFFFFFU, 267 All = 0xFFFFFFFFU,
268}; 268};
269static_assert(sizeof(NpadStyleSet) == 4, "NpadStyleSet is an invalid size"); 269static_assert(sizeof(NpadStyleSet) == 4, "NpadStyleSet is an invalid size");
270DECLARE_ENUM_FLAG_OPERATORS(NpadStyleSet)
270 271
271// This is nn::hid::VibrationDevicePosition 272// This is nn::hid::VibrationDevicePosition
272enum class VibrationDevicePosition : u32 { 273enum class VibrationDevicePosition : u32 {
diff --git a/src/core/hle/service/hid/controllers/applet_resource.h b/src/core/hle/service/hid/controllers/applet_resource.h
index 52cc4cf42..0862fdc2f 100644
--- a/src/core/hle/service/hid/controllers/applet_resource.h
+++ b/src/core/hle/service/hid/controllers/applet_resource.h
@@ -25,6 +25,7 @@ class AppletResource;
25class NPadResource; 25class NPadResource;
26 26
27static constexpr std::size_t AruidIndexMax = 0x20; 27static constexpr std::size_t AruidIndexMax = 0x20;
28static constexpr u64 SystemAruid = 0;
28 29
29enum class RegistrationStatus : u32 { 30enum class RegistrationStatus : u32 {
30 None, 31 None,
diff --git a/src/core/hle/service/hid/controllers/npad.cpp b/src/core/hle/service/hid/controllers/npad.cpp
index c7aa606bc..bf387e22d 100644
--- a/src/core/hle/service/hid/controllers/npad.cpp
+++ b/src/core/hle/service/hid/controllers/npad.cpp
@@ -24,37 +24,94 @@
24#include "core/hle/service/kernel_helpers.h" 24#include "core/hle/service/kernel_helpers.h"
25 25
26namespace Service::HID { 26namespace Service::HID {
27constexpr std::array<Core::HID::NpadIdType, 10> npad_id_list{
28 Core::HID::NpadIdType::Player1, Core::HID::NpadIdType::Player2, Core::HID::NpadIdType::Player3,
29 Core::HID::NpadIdType::Player4, Core::HID::NpadIdType::Player5, Core::HID::NpadIdType::Player6,
30 Core::HID::NpadIdType::Player7, Core::HID::NpadIdType::Player8, Core::HID::NpadIdType::Other,
31 Core::HID::NpadIdType::Handheld,
32};
33 27
34NPad::NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_) 28NPad::NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_)
35 : ControllerBase{hid_core_}, service_context{service_context_} { 29 : hid_core{hid_core_}, service_context{service_context_}, npad_resource{service_context} {
36 for (std::size_t i = 0; i < controller_data.size(); ++i) { 30 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; ++aruid_index) {
37 auto& controller = controller_data[i]; 31 for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) {
38 controller.device = hid_core.GetEmulatedControllerByIndex(i); 32 auto& controller = controller_data[aruid_index][i];
39 controller.vibration[Core::HID::EmulatedDeviceIndex::LeftIndex].latest_vibration_value = 33 controller.device = hid_core.GetEmulatedControllerByIndex(i);
40 Core::HID::DEFAULT_VIBRATION_VALUE; 34 controller.vibration[Core::HID::EmulatedDeviceIndex::LeftIndex].latest_vibration_value =
41 controller.vibration[Core::HID::EmulatedDeviceIndex::RightIndex].latest_vibration_value = 35 Core::HID::DEFAULT_VIBRATION_VALUE;
42 Core::HID::DEFAULT_VIBRATION_VALUE; 36 controller.vibration[Core::HID::EmulatedDeviceIndex::RightIndex]
43 Core::HID::ControllerUpdateCallback engine_callback{ 37 .latest_vibration_value = Core::HID::DEFAULT_VIBRATION_VALUE;
44 .on_change = [this, 38 Core::HID::ControllerUpdateCallback engine_callback{
45 i](Core::HID::ControllerTriggerType type) { ControllerUpdate(type, i); }, 39 .on_change =
46 .is_npad_service = true, 40 [this, i](Core::HID::ControllerTriggerType type) { ControllerUpdate(type, i); },
47 }; 41 .is_npad_service = true,
48 controller.callback_key = controller.device->SetCallback(engine_callback); 42 };
43 controller.callback_key = controller.device->SetCallback(engine_callback);
44 }
49 } 45 }
50} 46}
51 47
52NPad::~NPad() { 48NPad::~NPad() {
53 for (std::size_t i = 0; i < controller_data.size(); ++i) { 49 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; ++aruid_index) {
54 auto& controller = controller_data[i]; 50 for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) {
55 controller.device->DeleteCallback(controller.callback_key); 51 auto& controller = controller_data[aruid_index][i];
52 controller.device->DeleteCallback(controller.callback_key);
53 }
54 }
55}
56
57Result NPad::Activate() {
58 if (ref_counter == std::numeric_limits<s32>::max() - 1) {
59 return ResultNpadResourceOverflow;
60 }
61
62 if (ref_counter == 0) {
63 std::scoped_lock lock{mutex};
64
65 // TODO: Activate handlers and AbstractedPad
66 }
67
68 ref_counter++;
69 return ResultSuccess;
70}
71
72Result NPad::Activate(u64 aruid) {
73 std::scoped_lock lock{mutex};
74 std::scoped_lock shared_lock{*applet_resource_holder.shared_mutex};
75
76 auto* data = applet_resource_holder.applet_resource->GetAruidData(aruid);
77 const auto aruid_index = applet_resource_holder.applet_resource->GetIndexFromAruid(aruid);
78
79 if (data == nullptr || !data->flag.is_assigned) {
80 return ResultSuccess;
81 }
82
83 for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) {
84 auto& controller = controller_data[aruid_index][i];
85 controller.shared_memory = &data->shared_memory_format->npad.npad_entry[i].internal_state;
86 }
87
88 // Prefill controller buffers
89 for (auto& controller : controller_data[aruid_index]) {
90 auto* npad = controller.shared_memory;
91 npad->fullkey_color = {
92 .attribute = ColorAttribute::NoController,
93 .fullkey = {},
94 };
95 npad->joycon_color = {
96 .attribute = ColorAttribute::NoController,
97 .left = {},
98 .right = {},
99 };
100 // HW seems to initialize the first 19 entries
101 for (std::size_t i = 0; i < 19; ++i) {
102 WriteEmptyEntry(npad);
103 }
56 } 104 }
57 OnRelease(); 105
106 return ResultSuccess;
107}
108
109Result NPad::ActivateNpadResource() {
110 return npad_resource.Activate();
111}
112
113Result NPad::ActivateNpadResource(u64 aruid) {
114 return npad_resource.Activate(aruid);
58} 115}
59 116
60void NPad::ControllerUpdate(Core::HID::ControllerTriggerType type, std::size_t controller_idx) { 117void NPad::ControllerUpdate(Core::HID::ControllerTriggerType type, std::size_t controller_idx) {
@@ -63,41 +120,50 @@ void NPad::ControllerUpdate(Core::HID::ControllerTriggerType type, std::size_t c
63 ControllerUpdate(Core::HID::ControllerTriggerType::Battery, controller_idx); 120 ControllerUpdate(Core::HID::ControllerTriggerType::Battery, controller_idx);
64 return; 121 return;
65 } 122 }
66 if (controller_idx >= controller_data.size()) {
67 return;
68 }
69 123
70 auto& controller = controller_data[controller_idx]; 124 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
71 const auto is_connected = controller.device->IsConnected(); 125 if (controller_idx >= controller_data[aruid_index].size()) {
72 const auto npad_type = controller.device->GetNpadStyleIndex();
73 const auto npad_id = controller.device->GetNpadIdType();
74 switch (type) {
75 case Core::HID::ControllerTriggerType::Connected:
76 case Core::HID::ControllerTriggerType::Disconnected:
77 if (is_connected == controller.is_connected) {
78 return; 126 return;
79 } 127 }
80 UpdateControllerAt(npad_type, npad_id, is_connected); 128
81 break; 129 auto* data = applet_resource_holder.applet_resource->GetAruidDataByIndex(aruid_index);
82 case Core::HID::ControllerTriggerType::Battery: { 130
83 if (!controller.device->IsConnected()) { 131 if (data->flag.is_assigned) {
84 return; 132 continue;
133 }
134
135 auto& controller = controller_data[aruid_index][controller_idx];
136 const auto is_connected = controller.device->IsConnected();
137 const auto npad_type = controller.device->GetNpadStyleIndex();
138 const auto npad_id = controller.device->GetNpadIdType();
139 switch (type) {
140 case Core::HID::ControllerTriggerType::Connected:
141 case Core::HID::ControllerTriggerType::Disconnected:
142 if (is_connected == controller.is_connected) {
143 return;
144 }
145 UpdateControllerAt(data->aruid, npad_type, npad_id, is_connected);
146 break;
147 case Core::HID::ControllerTriggerType::Battery: {
148 if (!controller.device->IsConnected()) {
149 return;
150 }
151 auto* shared_memory = controller.shared_memory;
152 const auto& battery_level = controller.device->GetBattery();
153 shared_memory->battery_level_dual = battery_level.dual.battery_level;
154 shared_memory->battery_level_left = battery_level.left.battery_level;
155 shared_memory->battery_level_right = battery_level.right.battery_level;
156 break;
157 }
158 default:
159 break;
85 } 160 }
86 auto* shared_memory = controller.shared_memory;
87 const auto& battery_level = controller.device->GetBattery();
88 shared_memory->battery_level_dual = battery_level.dual.battery_level;
89 shared_memory->battery_level_left = battery_level.left.battery_level;
90 shared_memory->battery_level_right = battery_level.right.battery_level;
91 break;
92 }
93 default:
94 break;
95 } 161 }
96} 162}
97 163
98void NPad::InitNewlyAddedController(Core::HID::NpadIdType npad_id) { 164void NPad::InitNewlyAddedController(u64 aruid, Core::HID::NpadIdType npad_id) {
99 auto& controller = GetControllerFromNpadIdType(npad_id); 165 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
100 if (!IsControllerSupported(controller.device->GetNpadStyleIndex())) { 166 if (!npad_resource.IsControllerSupported(aruid, controller.device->GetNpadStyleIndex())) {
101 return; 167 return;
102 } 168 }
103 LOG_DEBUG(Service_HID, "Npad connected {}", npad_id); 169 LOG_DEBUG(Service_HID, "Npad connected {}", npad_id);
@@ -106,7 +172,7 @@ void NPad::InitNewlyAddedController(Core::HID::NpadIdType npad_id) {
106 const auto& battery_level = controller.device->GetBattery(); 172 const auto& battery_level = controller.device->GetBattery();
107 auto* shared_memory = controller.shared_memory; 173 auto* shared_memory = controller.shared_memory;
108 if (controller_type == Core::HID::NpadStyleIndex::None) { 174 if (controller_type == Core::HID::NpadStyleIndex::None) {
109 controller.styleset_changed_event->Signal(); 175 npad_resource.SignalStyleSetUpdateEvent(aruid, npad_id);
110 return; 176 return;
111 } 177 }
112 178
@@ -290,53 +356,11 @@ void NPad::InitNewlyAddedController(Core::HID::NpadIdType npad_id) {
290 Common::Input::PollingMode::Active); 356 Common::Input::PollingMode::Active);
291 } 357 }
292 358
293 SignalStyleSetChangedEvent(npad_id); 359 npad_resource.SignalStyleSetUpdateEvent(aruid, npad_id);
294 WriteEmptyEntry(controller.shared_memory); 360 WriteEmptyEntry(controller.shared_memory);
295 hid_core.SetLastActiveController(npad_id); 361 hid_core.SetLastActiveController(npad_id);
296} 362}
297 363
298void NPad::OnInit() {
299 const u64 aruid = applet_resource->GetActiveAruid();
300 auto* data = applet_resource->GetAruidData(aruid);
301
302 if (data == nullptr) {
303 return;
304 }
305
306 if (!IsControllerActivated()) {
307 return;
308 }
309
310 for (std::size_t i = 0; i < controller_data.size(); ++i) {
311 auto& controller = controller_data[i];
312 controller.shared_memory = &data->shared_memory_format->npad.npad_entry[i].internal_state;
313 controller.styleset_changed_event =
314 service_context.CreateEvent(fmt::format("npad:NpadStyleSetChanged_{}", i));
315 }
316
317 supported_npad_id_types.resize(npad_id_list.size());
318 std::memcpy(supported_npad_id_types.data(), npad_id_list.data(),
319 npad_id_list.size() * sizeof(Core::HID::NpadIdType));
320
321 // Prefill controller buffers
322 for (auto& controller : controller_data) {
323 auto* npad = controller.shared_memory;
324 npad->fullkey_color = {
325 .attribute = ColorAttribute::NoController,
326 .fullkey = {},
327 };
328 npad->joycon_color = {
329 .attribute = ColorAttribute::NoController,
330 .left = {},
331 .right = {},
332 };
333 // HW seems to initialize the first 19 entries
334 for (std::size_t i = 0; i < 19; ++i) {
335 WriteEmptyEntry(npad);
336 }
337 }
338}
339
340void NPad::WriteEmptyEntry(NpadInternalState* npad) { 364void NPad::WriteEmptyEntry(NpadInternalState* npad) {
341 NPadGenericState dummy_pad_state{}; 365 NPadGenericState dummy_pad_state{};
342 NpadGcTriggerState dummy_gc_state{}; 366 NpadGcTriggerState dummy_gc_state{};
@@ -358,33 +382,20 @@ void NPad::WriteEmptyEntry(NpadInternalState* npad) {
358 npad->gc_trigger_lifo.WriteNextEntry(dummy_gc_state); 382 npad->gc_trigger_lifo.WriteNextEntry(dummy_gc_state);
359} 383}
360 384
361void NPad::OnRelease() { 385void NPad::RequestPadStateUpdate(u64 aruid, Core::HID::NpadIdType npad_id) {
362 is_controller_initialized = false; 386 std::scoped_lock lock{*applet_resource_holder.shared_mutex};
363 for (std::size_t i = 0; i < controller_data.size(); ++i) { 387 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
364 auto& controller = controller_data[i];
365 if (controller.styleset_changed_event) {
366 service_context.CloseEvent(controller.styleset_changed_event);
367 }
368 for (std::size_t device_idx = 0; device_idx < controller.vibration.size(); ++device_idx) {
369 VibrateControllerAtIndex(controller.device->GetNpadIdType(), device_idx, {});
370 }
371 }
372}
373
374void NPad::RequestPadStateUpdate(Core::HID::NpadIdType npad_id) {
375 std::scoped_lock lock{mutex};
376 auto& controller = GetControllerFromNpadIdType(npad_id);
377 const auto controller_type = controller.device->GetNpadStyleIndex(); 388 const auto controller_type = controller.device->GetNpadStyleIndex();
378 389
379 if (!controller.device->IsConnected() && controller.is_connected) { 390 if (!controller.device->IsConnected() && controller.is_connected) {
380 DisconnectNpad(npad_id); 391 DisconnectNpad(aruid, npad_id);
381 return; 392 return;
382 } 393 }
383 if (!controller.device->IsConnected()) { 394 if (!controller.device->IsConnected()) {
384 return; 395 return;
385 } 396 }
386 if (controller.device->IsConnected() && !controller.is_connected) { 397 if (controller.device->IsConnected() && !controller.is_connected) {
387 InitNewlyAddedController(npad_id); 398 InitNewlyAddedController(aruid, npad_id);
388 } 399 }
389 400
390 // This function is unique to yuzu for the turbo buttons and motion to work properly 401 // This function is unique to yuzu for the turbo buttons and motion to work properly
@@ -441,230 +452,232 @@ void NPad::RequestPadStateUpdate(Core::HID::NpadIdType npad_id) {
441} 452}
442 453
443void NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing) { 454void NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
444 const u64 aruid = applet_resource->GetActiveAruid(); 455 if (ref_counter == 0) {
445 auto* data = applet_resource->GetAruidData(aruid);
446
447 if (data == nullptr) {
448 return;
449 }
450
451 if (!IsControllerActivated()) {
452 return; 456 return;
453 } 457 }
454 458
455 for (std::size_t i = 0; i < controller_data.size(); ++i) { 459 std::scoped_lock lock{*applet_resource_holder.shared_mutex};
456 auto& controller = controller_data[i]; 460 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; ++aruid_index) {
457 controller.shared_memory = &data->shared_memory_format->npad.npad_entry[i].internal_state; 461 const auto* data = applet_resource_holder.applet_resource->GetAruidDataByIndex(aruid_index);
458 auto* npad = controller.shared_memory; 462 const auto aruid = data->aruid;
459 463
460 const auto& controller_type = controller.device->GetNpadStyleIndex(); 464 if (!data->flag.is_assigned) {
461
462 if (controller_type == Core::HID::NpadStyleIndex::None ||
463 !controller.device->IsConnected()) {
464 continue; 465 continue;
465 } 466 }
466 467
467 RequestPadStateUpdate(controller.device->GetNpadIdType()); 468 for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) {
468 auto& pad_state = controller.npad_pad_state; 469 auto& controller = controller_data[aruid_index][i];
469 auto& libnx_state = controller.npad_libnx_state; 470 controller.shared_memory =
470 auto& trigger_state = controller.npad_trigger_state; 471 &data->shared_memory_format->npad.npad_entry[i].internal_state;
471 472 auto* npad = controller.shared_memory;
472 // LibNX exclusively uses this section, so we always update it since LibNX doesn't activate 473
473 // any controllers. 474 const auto& controller_type = controller.device->GetNpadStyleIndex();
474 libnx_state.connection_status.raw = 0; 475
475 libnx_state.connection_status.is_connected.Assign(1); 476 if (controller_type == Core::HID::NpadStyleIndex::None ||
476 switch (controller_type) { 477 !controller.device->IsConnected()) {
477 case Core::HID::NpadStyleIndex::None: 478 continue;
478 ASSERT(false); 479 }
479 break; 480
480 case Core::HID::NpadStyleIndex::ProController: 481 RequestPadStateUpdate(aruid, controller.device->GetNpadIdType());
481 case Core::HID::NpadStyleIndex::NES: 482 auto& pad_state = controller.npad_pad_state;
482 case Core::HID::NpadStyleIndex::SNES: 483 auto& libnx_state = controller.npad_libnx_state;
483 case Core::HID::NpadStyleIndex::N64: 484 auto& trigger_state = controller.npad_trigger_state;
484 case Core::HID::NpadStyleIndex::SegaGenesis: 485
485 pad_state.connection_status.raw = 0; 486 // LibNX exclusively uses this section, so we always update it since LibNX doesn't
486 pad_state.connection_status.is_connected.Assign(1); 487 // activate any controllers.
487 pad_state.connection_status.is_wired.Assign(1); 488 libnx_state.connection_status.raw = 0;
488 489 libnx_state.connection_status.is_connected.Assign(1);
489 libnx_state.connection_status.is_wired.Assign(1); 490 switch (controller_type) {
490 pad_state.sampling_number = 491 case Core::HID::NpadStyleIndex::None:
491 npad->fullkey_lifo.ReadCurrentEntry().state.sampling_number + 1; 492 ASSERT(false);
492 npad->fullkey_lifo.WriteNextEntry(pad_state); 493 break;
493 break; 494 case Core::HID::NpadStyleIndex::ProController:
494 case Core::HID::NpadStyleIndex::Handheld: 495 case Core::HID::NpadStyleIndex::NES:
495 pad_state.connection_status.raw = 0; 496 case Core::HID::NpadStyleIndex::SNES:
496 pad_state.connection_status.is_connected.Assign(1); 497 case Core::HID::NpadStyleIndex::N64:
497 pad_state.connection_status.is_wired.Assign(1); 498 case Core::HID::NpadStyleIndex::SegaGenesis:
498 pad_state.connection_status.is_left_connected.Assign(1); 499 pad_state.connection_status.raw = 0;
499 pad_state.connection_status.is_right_connected.Assign(1); 500 pad_state.connection_status.is_connected.Assign(1);
500 pad_state.connection_status.is_left_wired.Assign(1); 501 pad_state.connection_status.is_wired.Assign(1);
501 pad_state.connection_status.is_right_wired.Assign(1); 502
502 503 libnx_state.connection_status.is_wired.Assign(1);
503 libnx_state.connection_status.is_wired.Assign(1); 504 pad_state.sampling_number =
504 libnx_state.connection_status.is_left_connected.Assign(1); 505 npad->fullkey_lifo.ReadCurrentEntry().state.sampling_number + 1;
505 libnx_state.connection_status.is_right_connected.Assign(1); 506 npad->fullkey_lifo.WriteNextEntry(pad_state);
506 libnx_state.connection_status.is_left_wired.Assign(1); 507 break;
507 libnx_state.connection_status.is_right_wired.Assign(1); 508 case Core::HID::NpadStyleIndex::Handheld:
508 pad_state.sampling_number = 509 pad_state.connection_status.raw = 0;
509 npad->handheld_lifo.ReadCurrentEntry().state.sampling_number + 1; 510 pad_state.connection_status.is_connected.Assign(1);
510 npad->handheld_lifo.WriteNextEntry(pad_state); 511 pad_state.connection_status.is_wired.Assign(1);
511 break;
512 case Core::HID::NpadStyleIndex::JoyconDual:
513 pad_state.connection_status.raw = 0;
514 pad_state.connection_status.is_connected.Assign(1);
515 if (controller.is_dual_left_connected) {
516 pad_state.connection_status.is_left_connected.Assign(1); 512 pad_state.connection_status.is_left_connected.Assign(1);
513 pad_state.connection_status.is_right_connected.Assign(1);
514 pad_state.connection_status.is_left_wired.Assign(1);
515 pad_state.connection_status.is_right_wired.Assign(1);
516
517 libnx_state.connection_status.is_wired.Assign(1);
517 libnx_state.connection_status.is_left_connected.Assign(1); 518 libnx_state.connection_status.is_left_connected.Assign(1);
518 } 519 libnx_state.connection_status.is_right_connected.Assign(1);
519 if (controller.is_dual_right_connected) { 520 libnx_state.connection_status.is_left_wired.Assign(1);
521 libnx_state.connection_status.is_right_wired.Assign(1);
522 pad_state.sampling_number =
523 npad->handheld_lifo.ReadCurrentEntry().state.sampling_number + 1;
524 npad->handheld_lifo.WriteNextEntry(pad_state);
525 break;
526 case Core::HID::NpadStyleIndex::JoyconDual:
527 pad_state.connection_status.raw = 0;
528 pad_state.connection_status.is_connected.Assign(1);
529 if (controller.is_dual_left_connected) {
530 pad_state.connection_status.is_left_connected.Assign(1);
531 libnx_state.connection_status.is_left_connected.Assign(1);
532 }
533 if (controller.is_dual_right_connected) {
534 pad_state.connection_status.is_right_connected.Assign(1);
535 libnx_state.connection_status.is_right_connected.Assign(1);
536 }
537
538 pad_state.sampling_number =
539 npad->joy_dual_lifo.ReadCurrentEntry().state.sampling_number + 1;
540 npad->joy_dual_lifo.WriteNextEntry(pad_state);
541 break;
542 case Core::HID::NpadStyleIndex::JoyconLeft:
543 pad_state.connection_status.raw = 0;
544 pad_state.connection_status.is_connected.Assign(1);
545 pad_state.connection_status.is_left_connected.Assign(1);
546
547 libnx_state.connection_status.is_left_connected.Assign(1);
548 pad_state.sampling_number =
549 npad->joy_left_lifo.ReadCurrentEntry().state.sampling_number + 1;
550 npad->joy_left_lifo.WriteNextEntry(pad_state);
551 break;
552 case Core::HID::NpadStyleIndex::JoyconRight:
553 pad_state.connection_status.raw = 0;
554 pad_state.connection_status.is_connected.Assign(1);
520 pad_state.connection_status.is_right_connected.Assign(1); 555 pad_state.connection_status.is_right_connected.Assign(1);
556
521 libnx_state.connection_status.is_right_connected.Assign(1); 557 libnx_state.connection_status.is_right_connected.Assign(1);
558 pad_state.sampling_number =
559 npad->joy_right_lifo.ReadCurrentEntry().state.sampling_number + 1;
560 npad->joy_right_lifo.WriteNextEntry(pad_state);
561 break;
562 case Core::HID::NpadStyleIndex::GameCube:
563 pad_state.connection_status.raw = 0;
564 pad_state.connection_status.is_connected.Assign(1);
565 pad_state.connection_status.is_wired.Assign(1);
566
567 libnx_state.connection_status.is_wired.Assign(1);
568 pad_state.sampling_number =
569 npad->fullkey_lifo.ReadCurrentEntry().state.sampling_number + 1;
570 trigger_state.sampling_number =
571 npad->gc_trigger_lifo.ReadCurrentEntry().state.sampling_number + 1;
572 npad->fullkey_lifo.WriteNextEntry(pad_state);
573 npad->gc_trigger_lifo.WriteNextEntry(trigger_state);
574 break;
575 case Core::HID::NpadStyleIndex::Pokeball:
576 pad_state.connection_status.raw = 0;
577 pad_state.connection_status.is_connected.Assign(1);
578 pad_state.sampling_number =
579 npad->palma_lifo.ReadCurrentEntry().state.sampling_number + 1;
580 npad->palma_lifo.WriteNextEntry(pad_state);
581 break;
582 default:
583 break;
522 } 584 }
523 585
524 pad_state.sampling_number = 586 libnx_state.npad_buttons.raw = pad_state.npad_buttons.raw;
525 npad->joy_dual_lifo.ReadCurrentEntry().state.sampling_number + 1; 587 libnx_state.l_stick = pad_state.l_stick;
526 npad->joy_dual_lifo.WriteNextEntry(pad_state); 588 libnx_state.r_stick = pad_state.r_stick;
527 break; 589 npad->system_ext_lifo.WriteNextEntry(pad_state);
528 case Core::HID::NpadStyleIndex::JoyconLeft:
529 pad_state.connection_status.raw = 0;
530 pad_state.connection_status.is_connected.Assign(1);
531 pad_state.connection_status.is_left_connected.Assign(1);
532
533 libnx_state.connection_status.is_left_connected.Assign(1);
534 pad_state.sampling_number =
535 npad->joy_left_lifo.ReadCurrentEntry().state.sampling_number + 1;
536 npad->joy_left_lifo.WriteNextEntry(pad_state);
537 break;
538 case Core::HID::NpadStyleIndex::JoyconRight:
539 pad_state.connection_status.raw = 0;
540 pad_state.connection_status.is_connected.Assign(1);
541 pad_state.connection_status.is_right_connected.Assign(1);
542
543 libnx_state.connection_status.is_right_connected.Assign(1);
544 pad_state.sampling_number =
545 npad->joy_right_lifo.ReadCurrentEntry().state.sampling_number + 1;
546 npad->joy_right_lifo.WriteNextEntry(pad_state);
547 break;
548 case Core::HID::NpadStyleIndex::GameCube:
549 pad_state.connection_status.raw = 0;
550 pad_state.connection_status.is_connected.Assign(1);
551 pad_state.connection_status.is_wired.Assign(1);
552
553 libnx_state.connection_status.is_wired.Assign(1);
554 pad_state.sampling_number =
555 npad->fullkey_lifo.ReadCurrentEntry().state.sampling_number + 1;
556 trigger_state.sampling_number =
557 npad->gc_trigger_lifo.ReadCurrentEntry().state.sampling_number + 1;
558 npad->fullkey_lifo.WriteNextEntry(pad_state);
559 npad->gc_trigger_lifo.WriteNextEntry(trigger_state);
560 break;
561 case Core::HID::NpadStyleIndex::Pokeball:
562 pad_state.connection_status.raw = 0;
563 pad_state.connection_status.is_connected.Assign(1);
564 pad_state.sampling_number =
565 npad->palma_lifo.ReadCurrentEntry().state.sampling_number + 1;
566 npad->palma_lifo.WriteNextEntry(pad_state);
567 break;
568 default:
569 break;
570 }
571
572 libnx_state.npad_buttons.raw = pad_state.npad_buttons.raw;
573 libnx_state.l_stick = pad_state.l_stick;
574 libnx_state.r_stick = pad_state.r_stick;
575 npad->system_ext_lifo.WriteNextEntry(pad_state);
576 590
577 press_state |= static_cast<u64>(pad_state.npad_buttons.raw); 591 press_state |= static_cast<u64>(pad_state.npad_buttons.raw);
592 }
578 } 593 }
579} 594}
580 595
581void NPad::SetSupportedStyleSet(Core::HID::NpadStyleTag style_set) { 596Result NPad::SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet supported_style_set) {
582 hid_core.SetSupportedStyleTag(style_set); 597 std::scoped_lock lock{mutex};
583 598 hid_core.SetSupportedStyleTag({supported_style_set});
584 if (is_controller_initialized) { 599 const Result result = npad_resource.SetSupportedNpadStyleSet(aruid, supported_style_set);
585 return; 600 if (result.IsSuccess()) {
601 OnUpdate({});
586 } 602 }
587 603 return result;
588 // Once SetSupportedStyleSet is called controllers are fully initialized
589 is_controller_initialized = true;
590} 604}
591 605
592Core::HID::NpadStyleTag NPad::GetSupportedStyleSet() const { 606Result NPad::GetSupportedNpadStyleSet(u64 aruid,
593 if (!is_controller_initialized) { 607 Core::HID::NpadStyleSet& out_supported_style_set) const {
594 return {Core::HID::NpadStyleSet::None}; 608 std::scoped_lock lock{mutex};
609 const Result result = npad_resource.GetSupportedNpadStyleSet(out_supported_style_set, aruid);
610
611 if (result == ResultUndefinedStyleset) {
612 out_supported_style_set = Core::HID::NpadStyleSet::None;
613 return ResultSuccess;
595 } 614 }
596 return hid_core.GetSupportedStyleTag(); 615
616 return result;
597} 617}
598 618
599Result NPad::SetSupportedNpadIdTypes(std::span<const u8> data) { 619Result NPad::GetMaskedSupportedNpadStyleSet(
600 constexpr std::size_t max_number_npad_ids = 0xa; 620 u64 aruid, Core::HID::NpadStyleSet& out_supported_style_set) const {
601 const auto length = data.size(); 621 std::scoped_lock lock{mutex};
602 ASSERT(length > 0 && (length % sizeof(u32)) == 0); 622 const Result result =
603 const std::size_t elements = length / sizeof(u32); 623 npad_resource.GetMaskedSupportedNpadStyleSet(out_supported_style_set, aruid);
604 624
605 if (elements > max_number_npad_ids) { 625 if (result == ResultUndefinedStyleset) {
606 return InvalidArraySize; 626 out_supported_style_set = Core::HID::NpadStyleSet::None;
627 return ResultSuccess;
607 } 628 }
608 629
609 supported_npad_id_types.clear(); 630 return result;
610 supported_npad_id_types.resize(elements);
611 std::memcpy(supported_npad_id_types.data(), data.data(), length);
612 return ResultSuccess;
613} 631}
614 632
615void NPad::GetSupportedNpadIdTypes(u32* data, std::size_t max_length) { 633Result NPad::SetSupportedNpadIdType(u64 aruid,
616 const auto copy_amount = supported_npad_id_types.size() * sizeof(u32); 634 std::span<const Core::HID::NpadIdType> supported_npad_list) {
617 ASSERT(max_length <= copy_amount); 635 std::scoped_lock lock{mutex};
618 std::memcpy(data, supported_npad_id_types.data(), copy_amount); 636 if (supported_npad_list.size() > MaxSupportedNpadIdTypes) {
619} 637 return ResultInvalidArraySize;
638 }
620 639
621std::size_t NPad::GetSupportedNpadIdTypesSize() const { 640 Result result = npad_resource.SetSupportedNpadIdType(aruid, supported_npad_list);
622 return supported_npad_id_types.size();
623}
624 641
625void NPad::SetHoldType(NpadJoyHoldType joy_hold_type) { 642 if (result.IsSuccess()) {
626 if (joy_hold_type != NpadJoyHoldType::Horizontal && 643 OnUpdate({});
627 joy_hold_type != NpadJoyHoldType::Vertical) {
628 LOG_ERROR(Service_HID, "Npad joy hold type needs to be valid, joy_hold_type={}",
629 joy_hold_type);
630 return;
631 } 644 }
632 hold_type = joy_hold_type;
633}
634 645
635NpadJoyHoldType NPad::GetHoldType() const { 646 return result;
636 return hold_type;
637} 647}
638 648
639void NPad::SetNpadHandheldActivationMode(NpadHandheldActivationMode activation_mode) { 649Result NPad::SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type) {
640 if (activation_mode >= NpadHandheldActivationMode::MaxActivationMode) { 650 std::scoped_lock lock{mutex};
641 ASSERT_MSG(false, "Activation mode should be always None, Single or Dual"); 651 return npad_resource.SetNpadJoyHoldType(aruid, hold_type);
642 return;
643 }
644
645 handheld_activation_mode = activation_mode;
646} 652}
647 653
648NpadHandheldActivationMode NPad::GetNpadHandheldActivationMode() const { 654Result NPad::GetNpadJoyHoldType(u64 aruid, NpadJoyHoldType& out_hold_type) const {
649 return handheld_activation_mode; 655 std::scoped_lock lock{mutex};
656 return npad_resource.GetNpadJoyHoldType(out_hold_type, aruid);
650} 657}
651 658
652void NPad::SetNpadCommunicationMode(NpadCommunicationMode communication_mode_) { 659Result NPad::SetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode mode) {
653 communication_mode = communication_mode_; 660 std::scoped_lock lock{mutex};
661 Result result = npad_resource.SetNpadHandheldActivationMode(aruid, mode);
662 if (result.IsSuccess()) {
663 OnUpdate({});
664 }
665 return result;
654} 666}
655 667
656NpadCommunicationMode NPad::GetNpadCommunicationMode() const { 668Result NPad::GetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode& out_mode) const {
657 return communication_mode; 669 std::scoped_lock lock{mutex};
670 return npad_resource.GetNpadHandheldActivationMode(out_mode, aruid);
658} 671}
659 672
660bool NPad::SetNpadMode(Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType npad_id, 673bool NPad::SetNpadMode(u64 aruid, Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType npad_id,
661 NpadJoyDeviceType npad_device_type, NpadJoyAssignmentMode assignment_mode) { 674 NpadJoyDeviceType npad_device_type, NpadJoyAssignmentMode assignment_mode) {
662 if (!IsNpadIdValid(npad_id)) { 675 if (!IsNpadIdValid(npad_id)) {
663 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); 676 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
664 return false; 677 return false;
665 } 678 }
666 679
667 auto& controller = GetControllerFromNpadIdType(npad_id); 680 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
668 if (controller.shared_memory->assignment_mode != assignment_mode) { 681 if (controller.shared_memory->assignment_mode != assignment_mode) {
669 controller.shared_memory->assignment_mode = assignment_mode; 682 controller.shared_memory->assignment_mode = assignment_mode;
670 } 683 }
@@ -675,17 +688,17 @@ bool NPad::SetNpadMode(Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType
675 688
676 if (assignment_mode == NpadJoyAssignmentMode::Dual) { 689 if (assignment_mode == NpadJoyAssignmentMode::Dual) {
677 if (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconLeft) { 690 if (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconLeft) {
678 DisconnectNpad(npad_id); 691 DisconnectNpad(aruid, npad_id);
679 controller.is_dual_left_connected = true; 692 controller.is_dual_left_connected = true;
680 controller.is_dual_right_connected = false; 693 controller.is_dual_right_connected = false;
681 UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconDual, npad_id, true); 694 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, npad_id, true);
682 return false; 695 return false;
683 } 696 }
684 if (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconRight) { 697 if (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconRight) {
685 DisconnectNpad(npad_id); 698 DisconnectNpad(aruid, npad_id);
686 controller.is_dual_left_connected = false; 699 controller.is_dual_left_connected = false;
687 controller.is_dual_right_connected = true; 700 controller.is_dual_right_connected = true;
688 UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconDual, npad_id, true); 701 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, npad_id, true);
689 return false; 702 return false;
690 } 703 }
691 return false; 704 return false;
@@ -699,37 +712,38 @@ bool NPad::SetNpadMode(Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType
699 } 712 }
700 713
701 if (controller.is_dual_left_connected && !controller.is_dual_right_connected) { 714 if (controller.is_dual_left_connected && !controller.is_dual_right_connected) {
702 DisconnectNpad(npad_id); 715 DisconnectNpad(aruid, npad_id);
703 UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconLeft, npad_id, true); 716 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconLeft, npad_id, true);
704 return false; 717 return false;
705 } 718 }
706 if (!controller.is_dual_left_connected && controller.is_dual_right_connected) { 719 if (!controller.is_dual_left_connected && controller.is_dual_right_connected) {
707 DisconnectNpad(npad_id); 720 DisconnectNpad(aruid, npad_id);
708 UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconRight, npad_id, true); 721 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconRight, npad_id, true);
709 return false; 722 return false;
710 } 723 }
711 724
712 // We have two controllers connected to the same npad_id we need to split them 725 // We have two controllers connected to the same npad_id we need to split them
713 new_npad_id = hid_core.GetFirstDisconnectedNpadId(); 726 new_npad_id = hid_core.GetFirstDisconnectedNpadId();
714 auto& controller_2 = GetControllerFromNpadIdType(new_npad_id); 727 auto& controller_2 = GetControllerFromNpadIdType(aruid, new_npad_id);
715 DisconnectNpad(npad_id); 728 DisconnectNpad(aruid, npad_id);
716 if (npad_device_type == NpadJoyDeviceType::Left) { 729 if (npad_device_type == NpadJoyDeviceType::Left) {
717 UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconLeft, npad_id, true); 730 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconLeft, npad_id, true);
718 controller_2.is_dual_left_connected = false; 731 controller_2.is_dual_left_connected = false;
719 controller_2.is_dual_right_connected = true; 732 controller_2.is_dual_right_connected = true;
720 UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconDual, new_npad_id, true); 733 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, new_npad_id, true);
721 } else { 734 } else {
722 UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconRight, npad_id, true); 735 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconRight, npad_id, true);
723 controller_2.is_dual_left_connected = true; 736 controller_2.is_dual_left_connected = true;
724 controller_2.is_dual_right_connected = false; 737 controller_2.is_dual_right_connected = false;
725 UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconDual, new_npad_id, true); 738 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, new_npad_id, true);
726 } 739 }
727 return true; 740 return true;
728} 741}
729 742
730bool NPad::VibrateControllerAtIndex(Core::HID::NpadIdType npad_id, std::size_t device_index, 743bool NPad::VibrateControllerAtIndex(u64 aruid, Core::HID::NpadIdType npad_id,
744 std::size_t device_index,
731 const Core::HID::VibrationValue& vibration_value) { 745 const Core::HID::VibrationValue& vibration_value) {
732 auto& controller = GetControllerFromNpadIdType(npad_id); 746 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
733 if (!controller.device->IsConnected()) { 747 if (!controller.device->IsConnected()) {
734 return false; 748 return false;
735 } 749 }
@@ -772,7 +786,8 @@ bool NPad::VibrateControllerAtIndex(Core::HID::NpadIdType npad_id, std::size_t d
772 return controller.device->SetVibration(device_index, vibration); 786 return controller.device->SetVibration(device_index, vibration);
773} 787}
774 788
775void NPad::VibrateController(const Core::HID::VibrationDeviceHandle& vibration_device_handle, 789void NPad::VibrateController(u64 aruid,
790 const Core::HID::VibrationDeviceHandle& vibration_device_handle,
776 const Core::HID::VibrationValue& vibration_value) { 791 const Core::HID::VibrationValue& vibration_value) {
777 if (IsVibrationHandleValid(vibration_device_handle).IsError()) { 792 if (IsVibrationHandleValid(vibration_device_handle).IsError()) {
778 return; 793 return;
@@ -782,7 +797,7 @@ void NPad::VibrateController(const Core::HID::VibrationDeviceHandle& vibration_d
782 return; 797 return;
783 } 798 }
784 799
785 auto& controller = GetControllerFromHandle(vibration_device_handle); 800 auto& controller = GetControllerFromHandle(aruid, vibration_device_handle);
786 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index); 801 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
787 802
788 if (!controller.vibration[device_index].device_mounted || !controller.device->IsConnected()) { 803 if (!controller.vibration[device_index].device_mounted || !controller.device->IsConnected()) {
@@ -812,14 +827,14 @@ void NPad::VibrateController(const Core::HID::VibrationDeviceHandle& vibration_d
812 return; 827 return;
813 } 828 }
814 829
815 if (VibrateControllerAtIndex(controller.device->GetNpadIdType(), device_index, 830 if (VibrateControllerAtIndex(aruid, controller.device->GetNpadIdType(), device_index,
816 vibration_value)) { 831 vibration_value)) {
817 controller.vibration[device_index].latest_vibration_value = vibration_value; 832 controller.vibration[device_index].latest_vibration_value = vibration_value;
818 } 833 }
819} 834}
820 835
821void NPad::VibrateControllers( 836void NPad::VibrateControllers(
822 std::span<const Core::HID::VibrationDeviceHandle> vibration_device_handles, 837 u64 aruid, std::span<const Core::HID::VibrationDeviceHandle> vibration_device_handles,
823 std::span<const Core::HID::VibrationValue> vibration_values) { 838 std::span<const Core::HID::VibrationValue> vibration_values) {
824 if (!Settings::values.vibration_enabled.GetValue() && !permit_vibration_session_enabled) { 839 if (!Settings::values.vibration_enabled.GetValue() && !permit_vibration_session_enabled) {
825 return; 840 return;
@@ -831,17 +846,17 @@ void NPad::VibrateControllers(
831 "this is undefined behavior!"); 846 "this is undefined behavior!");
832 847
833 for (std::size_t i = 0; i < vibration_device_handles.size(); ++i) { 848 for (std::size_t i = 0; i < vibration_device_handles.size(); ++i) {
834 VibrateController(vibration_device_handles[i], vibration_values[i]); 849 VibrateController(aruid, vibration_device_handles[i], vibration_values[i]);
835 } 850 }
836} 851}
837 852
838Core::HID::VibrationValue NPad::GetLastVibration( 853Core::HID::VibrationValue NPad::GetLastVibration(
839 const Core::HID::VibrationDeviceHandle& vibration_device_handle) const { 854 u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const {
840 if (IsVibrationHandleValid(vibration_device_handle).IsError()) { 855 if (IsVibrationHandleValid(vibration_device_handle).IsError()) {
841 return {}; 856 return {};
842 } 857 }
843 858
844 const auto& controller = GetControllerFromHandle(vibration_device_handle); 859 const auto& controller = GetControllerFromHandle(aruid, vibration_device_handle);
845 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index); 860 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
846 return controller.vibration[device_index].latest_vibration_value; 861 return controller.vibration[device_index].latest_vibration_value;
847} 862}
@@ -852,14 +867,15 @@ void NPad::InitializeVibrationDevice(
852 return; 867 return;
853 } 868 }
854 869
870 const auto aruid = applet_resource_holder.applet_resource->GetActiveAruid();
855 const auto npad_index = static_cast<Core::HID::NpadIdType>(vibration_device_handle.npad_id); 871 const auto npad_index = static_cast<Core::HID::NpadIdType>(vibration_device_handle.npad_id);
856 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index); 872 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
857 InitializeVibrationDeviceAtIndex(npad_index, device_index); 873 InitializeVibrationDeviceAtIndex(aruid, npad_index, device_index);
858} 874}
859 875
860void NPad::InitializeVibrationDeviceAtIndex(Core::HID::NpadIdType npad_id, 876void NPad::InitializeVibrationDeviceAtIndex(u64 aruid, Core::HID::NpadIdType npad_id,
861 std::size_t device_index) { 877 std::size_t device_index) {
862 auto& controller = GetControllerFromNpadIdType(npad_id); 878 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
863 if (!Settings::values.vibration_enabled.GetValue()) { 879 if (!Settings::values.vibration_enabled.GetValue()) {
864 controller.vibration[device_index].device_mounted = false; 880 controller.vibration[device_index].device_mounted = false;
865 return; 881 return;
@@ -874,60 +890,50 @@ void NPad::SetPermitVibrationSession(bool permit_vibration_session) {
874} 890}
875 891
876bool NPad::IsVibrationDeviceMounted( 892bool NPad::IsVibrationDeviceMounted(
877 const Core::HID::VibrationDeviceHandle& vibration_device_handle) const { 893 u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const {
878 if (IsVibrationHandleValid(vibration_device_handle).IsError()) { 894 if (IsVibrationHandleValid(vibration_device_handle).IsError()) {
879 return false; 895 return false;
880 } 896 }
881 897
882 const auto& controller = GetControllerFromHandle(vibration_device_handle); 898 const auto& controller = GetControllerFromHandle(aruid, vibration_device_handle);
883 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index); 899 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
884 return controller.vibration[device_index].device_mounted; 900 return controller.vibration[device_index].device_mounted;
885} 901}
886 902
887Kernel::KReadableEvent& NPad::GetStyleSetChangedEvent(Core::HID::NpadIdType npad_id) { 903Result NPad::AcquireNpadStyleSetUpdateEventHandle(u64 aruid, Kernel::KReadableEvent** out_event,
888 if (!IsNpadIdValid(npad_id)) { 904 Core::HID::NpadIdType npad_id) {
889 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); 905 std::scoped_lock lock{mutex};
890 // Fallback to player 1 906 return npad_resource.AcquireNpadStyleSetUpdateEventHandle(aruid, out_event, npad_id);
891 const auto& controller = GetControllerFromNpadIdType(Core::HID::NpadIdType::Player1);
892 return controller.styleset_changed_event->GetReadableEvent();
893 }
894
895 const auto& controller = GetControllerFromNpadIdType(npad_id);
896 return controller.styleset_changed_event->GetReadableEvent();
897}
898
899void NPad::SignalStyleSetChangedEvent(Core::HID::NpadIdType npad_id) const {
900 const auto& controller = GetControllerFromNpadIdType(npad_id);
901 controller.styleset_changed_event->Signal();
902} 907}
903 908
904void NPad::AddNewControllerAt(Core::HID::NpadStyleIndex controller, Core::HID::NpadIdType npad_id) { 909void NPad::AddNewControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller,
905 UpdateControllerAt(controller, npad_id, true); 910 Core::HID::NpadIdType npad_id) {
911 UpdateControllerAt(aruid, controller, npad_id, true);
906} 912}
907 913
908void NPad::UpdateControllerAt(Core::HID::NpadStyleIndex type, Core::HID::NpadIdType npad_id, 914void NPad::UpdateControllerAt(u64 aruid, Core::HID::NpadStyleIndex type,
909 bool connected) { 915 Core::HID::NpadIdType npad_id, bool connected) {
910 auto& controller = GetControllerFromNpadIdType(npad_id); 916 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
911 if (!connected) { 917 if (!connected) {
912 DisconnectNpad(npad_id); 918 DisconnectNpad(aruid, npad_id);
913 return; 919 return;
914 } 920 }
915 921
916 controller.device->SetNpadStyleIndex(type); 922 controller.device->SetNpadStyleIndex(type);
917 InitNewlyAddedController(npad_id); 923 InitNewlyAddedController(aruid, npad_id);
918} 924}
919 925
920Result NPad::DisconnectNpad(Core::HID::NpadIdType npad_id) { 926Result NPad::DisconnectNpad(u64 aruid, Core::HID::NpadIdType npad_id) {
921 if (!IsNpadIdValid(npad_id)) { 927 if (!IsNpadIdValid(npad_id)) {
922 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); 928 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
923 return InvalidNpadId; 929 return ResultInvalidNpadId;
924 } 930 }
925 931
926 LOG_DEBUG(Service_HID, "Npad disconnected {}", npad_id); 932 LOG_DEBUG(Service_HID, "Npad disconnected {}", npad_id);
927 auto& controller = GetControllerFromNpadIdType(npad_id); 933 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
928 for (std::size_t device_idx = 0; device_idx < controller.vibration.size(); ++device_idx) { 934 for (std::size_t device_idx = 0; device_idx < controller.vibration.size(); ++device_idx) {
929 // Send an empty vibration to stop any vibrations. 935 // Send an empty vibration to stop any vibrations.
930 VibrateControllerAtIndex(npad_id, device_idx, {}); 936 VibrateControllerAtIndex(aruid, npad_id, device_idx, {});
931 controller.vibration[device_idx].device_mounted = false; 937 controller.vibration[device_idx].device_mounted = false;
932 } 938 }
933 939
@@ -961,47 +967,48 @@ Result NPad::DisconnectNpad(Core::HID::NpadIdType npad_id) {
961 controller.is_dual_right_connected = true; 967 controller.is_dual_right_connected = true;
962 controller.is_connected = false; 968 controller.is_connected = false;
963 controller.device->Disconnect(); 969 controller.device->Disconnect();
964 SignalStyleSetChangedEvent(npad_id); 970 npad_resource.SignalStyleSetUpdateEvent(aruid, npad_id);
965 WriteEmptyEntry(shared_memory); 971 WriteEmptyEntry(shared_memory);
966 return ResultSuccess; 972 return ResultSuccess;
967} 973}
968 974
969Result NPad::IsFirmwareUpdateAvailableForSixAxisSensor( 975Result NPad::IsFirmwareUpdateAvailableForSixAxisSensor(
970 const Core::HID::SixAxisSensorHandle& sixaxis_handle, bool& is_firmware_available) const { 976 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle,
977 bool& is_firmware_available) const {
971 const auto is_valid = IsSixaxisHandleValid(sixaxis_handle); 978 const auto is_valid = IsSixaxisHandleValid(sixaxis_handle);
972 if (is_valid.IsError()) { 979 if (is_valid.IsError()) {
973 LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw); 980 LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
974 return is_valid; 981 return is_valid;
975 } 982 }
976 983
977 const auto& sixaxis_properties = GetSixaxisProperties(sixaxis_handle); 984 const auto& sixaxis_properties = GetSixaxisProperties(aruid, sixaxis_handle);
978 is_firmware_available = sixaxis_properties.is_firmware_update_available != 0; 985 is_firmware_available = sixaxis_properties.is_firmware_update_available != 0;
979 return ResultSuccess; 986 return ResultSuccess;
980} 987}
981 988
982Result NPad::ResetIsSixAxisSensorDeviceNewlyAssigned( 989Result NPad::ResetIsSixAxisSensorDeviceNewlyAssigned(
983 const Core::HID::SixAxisSensorHandle& sixaxis_handle) { 990 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle) {
984 const auto is_valid = IsSixaxisHandleValid(sixaxis_handle); 991 const auto is_valid = IsSixaxisHandleValid(sixaxis_handle);
985 if (is_valid.IsError()) { 992 if (is_valid.IsError()) {
986 LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw); 993 LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
987 return is_valid; 994 return is_valid;
988 } 995 }
989 996
990 auto& sixaxis_properties = GetSixaxisProperties(sixaxis_handle); 997 auto& sixaxis_properties = GetSixaxisProperties(aruid, sixaxis_handle);
991 sixaxis_properties.is_newly_assigned.Assign(0); 998 sixaxis_properties.is_newly_assigned.Assign(0);
992 999
993 return ResultSuccess; 1000 return ResultSuccess;
994} 1001}
995 1002
996Result NPad::MergeSingleJoyAsDualJoy(Core::HID::NpadIdType npad_id_1, 1003Result NPad::MergeSingleJoyAsDualJoy(u64 aruid, Core::HID::NpadIdType npad_id_1,
997 Core::HID::NpadIdType npad_id_2) { 1004 Core::HID::NpadIdType npad_id_2) {
998 if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) { 1005 if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) {
999 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id_1:{}, npad_id_2:{}", npad_id_1, 1006 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id_1:{}, npad_id_2:{}", npad_id_1,
1000 npad_id_2); 1007 npad_id_2);
1001 return InvalidNpadId; 1008 return ResultInvalidNpadId;
1002 } 1009 }
1003 auto& controller_1 = GetControllerFromNpadIdType(npad_id_1); 1010 auto& controller_1 = GetControllerFromNpadIdType(aruid, npad_id_1);
1004 auto& controller_2 = GetControllerFromNpadIdType(npad_id_2); 1011 auto& controller_2 = GetControllerFromNpadIdType(aruid, npad_id_2);
1005 auto controller_style_1 = controller_1.device->GetNpadStyleIndex(); 1012 auto controller_style_1 = controller_1.device->GetNpadStyleIndex();
1006 auto controller_style_2 = controller_2.device->GetNpadStyleIndex(); 1013 auto controller_style_2 = controller_2.device->GetNpadStyleIndex();
1007 1014
@@ -1048,51 +1055,62 @@ Result NPad::MergeSingleJoyAsDualJoy(Core::HID::NpadIdType npad_id_1,
1048 } 1055 }
1049 1056
1050 // Disconnect the joycons and connect them as dual joycon at the first index. 1057 // Disconnect the joycons and connect them as dual joycon at the first index.
1051 DisconnectNpad(npad_id_1); 1058 DisconnectNpad(aruid, npad_id_1);
1052 DisconnectNpad(npad_id_2); 1059 DisconnectNpad(aruid, npad_id_2);
1053 controller_1.is_dual_left_connected = true; 1060 controller_1.is_dual_left_connected = true;
1054 controller_1.is_dual_right_connected = true; 1061 controller_1.is_dual_right_connected = true;
1055 AddNewControllerAt(Core::HID::NpadStyleIndex::JoyconDual, npad_id_1); 1062 AddNewControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, npad_id_1);
1056 return ResultSuccess; 1063 return ResultSuccess;
1057} 1064}
1058 1065
1059void NPad::StartLRAssignmentMode() { 1066Result NPad::StartLrAssignmentMode(u64 aruid) {
1060 // Nothing internally is used for lr assignment mode. Since we have the ability to set the 1067 std::scoped_lock lock{mutex};
1061 // controller types from boot, it doesn't really matter about showing a selection screen 1068 bool is_enabled{};
1062 is_in_lr_assignment_mode = true; 1069 Result result = npad_resource.GetLrAssignmentMode(is_enabled, aruid);
1070 if (result.IsSuccess() && is_enabled == false) {
1071 result = npad_resource.SetLrAssignmentMode(aruid, true);
1072 }
1073 return result;
1063} 1074}
1064 1075
1065void NPad::StopLRAssignmentMode() { 1076Result NPad::StopLrAssignmentMode(u64 aruid) {
1066 is_in_lr_assignment_mode = false; 1077 std::scoped_lock lock{mutex};
1078 bool is_enabled{};
1079 Result result = npad_resource.GetLrAssignmentMode(is_enabled, aruid);
1080 if (result.IsSuccess() && is_enabled == true) {
1081 result = npad_resource.SetLrAssignmentMode(aruid, false);
1082 }
1083 return result;
1067} 1084}
1068 1085
1069Result NPad::SwapNpadAssignment(Core::HID::NpadIdType npad_id_1, Core::HID::NpadIdType npad_id_2) { 1086Result NPad::SwapNpadAssignment(u64 aruid, Core::HID::NpadIdType npad_id_1,
1087 Core::HID::NpadIdType npad_id_2) {
1070 if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) { 1088 if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) {
1071 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id_1:{}, npad_id_2:{}", npad_id_1, 1089 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id_1:{}, npad_id_2:{}", npad_id_1,
1072 npad_id_2); 1090 npad_id_2);
1073 return InvalidNpadId; 1091 return ResultInvalidNpadId;
1074 } 1092 }
1075 if (npad_id_1 == Core::HID::NpadIdType::Handheld || 1093 if (npad_id_1 == Core::HID::NpadIdType::Handheld ||
1076 npad_id_2 == Core::HID::NpadIdType::Handheld || npad_id_1 == Core::HID::NpadIdType::Other || 1094 npad_id_2 == Core::HID::NpadIdType::Handheld || npad_id_1 == Core::HID::NpadIdType::Other ||
1077 npad_id_2 == Core::HID::NpadIdType::Other) { 1095 npad_id_2 == Core::HID::NpadIdType::Other) {
1078 return ResultSuccess; 1096 return ResultSuccess;
1079 } 1097 }
1080 const auto& controller_1 = GetControllerFromNpadIdType(npad_id_1).device; 1098 const auto& controller_1 = GetControllerFromNpadIdType(aruid, npad_id_1).device;
1081 const auto& controller_2 = GetControllerFromNpadIdType(npad_id_2).device; 1099 const auto& controller_2 = GetControllerFromNpadIdType(aruid, npad_id_2).device;
1082 const auto type_index_1 = controller_1->GetNpadStyleIndex(); 1100 const auto type_index_1 = controller_1->GetNpadStyleIndex();
1083 const auto type_index_2 = controller_2->GetNpadStyleIndex(); 1101 const auto type_index_2 = controller_2->GetNpadStyleIndex();
1084 const auto is_connected_1 = controller_1->IsConnected(); 1102 const auto is_connected_1 = controller_1->IsConnected();
1085 const auto is_connected_2 = controller_2->IsConnected(); 1103 const auto is_connected_2 = controller_2->IsConnected();
1086 1104
1087 if (!IsControllerSupported(type_index_1) && is_connected_1) { 1105 if (!npad_resource.IsControllerSupported(aruid, type_index_1) && is_connected_1) {
1088 return NpadNotConnected; 1106 return ResultNpadNotConnected;
1089 } 1107 }
1090 if (!IsControllerSupported(type_index_2) && is_connected_2) { 1108 if (!npad_resource.IsControllerSupported(aruid, type_index_2) && is_connected_2) {
1091 return NpadNotConnected; 1109 return ResultNpadNotConnected;
1092 } 1110 }
1093 1111
1094 UpdateControllerAt(type_index_2, npad_id_1, is_connected_2); 1112 UpdateControllerAt(aruid, type_index_2, npad_id_1, is_connected_2);
1095 UpdateControllerAt(type_index_1, npad_id_2, is_connected_1); 1113 UpdateControllerAt(aruid, type_index_1, npad_id_2, is_connected_1);
1096 1114
1097 return ResultSuccess; 1115 return ResultSuccess;
1098} 1116}
@@ -1100,68 +1118,68 @@ Result NPad::SwapNpadAssignment(Core::HID::NpadIdType npad_id_1, Core::HID::Npad
1100Result NPad::GetLedPattern(Core::HID::NpadIdType npad_id, Core::HID::LedPattern& pattern) const { 1118Result NPad::GetLedPattern(Core::HID::NpadIdType npad_id, Core::HID::LedPattern& pattern) const {
1101 if (!IsNpadIdValid(npad_id)) { 1119 if (!IsNpadIdValid(npad_id)) {
1102 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); 1120 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
1103 return InvalidNpadId; 1121 return ResultInvalidNpadId;
1104 } 1122 }
1105 const auto& controller = GetControllerFromNpadIdType(npad_id).device; 1123 const auto aruid = applet_resource_holder.applet_resource->GetActiveAruid();
1124 const auto& controller = GetControllerFromNpadIdType(aruid, npad_id).device;
1106 pattern = controller->GetLedPattern(); 1125 pattern = controller->GetLedPattern();
1107 return ResultSuccess; 1126 return ResultSuccess;
1108} 1127}
1109 1128
1110Result NPad::IsUnintendedHomeButtonInputProtectionEnabled(Core::HID::NpadIdType npad_id, 1129Result NPad::IsUnintendedHomeButtonInputProtectionEnabled(bool& out_is_enabled, u64 aruid,
1111 bool& is_valid) const { 1130 Core::HID::NpadIdType npad_id) const {
1112 if (!IsNpadIdValid(npad_id)) { 1131 std::scoped_lock lock{mutex};
1113 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); 1132 return npad_resource.GetHomeProtectionEnabled(out_is_enabled, aruid, npad_id);
1114 return InvalidNpadId;
1115 }
1116 const auto& controller = GetControllerFromNpadIdType(npad_id);
1117 is_valid = controller.unintended_home_button_input_protection;
1118 return ResultSuccess;
1119} 1133}
1120 1134
1121Result NPad::SetUnintendedHomeButtonInputProtectionEnabled(bool is_protection_enabled, 1135Result NPad::EnableUnintendedHomeButtonInputProtection(u64 aruid, Core::HID::NpadIdType npad_id,
1122 Core::HID::NpadIdType npad_id) { 1136 bool is_enabled) {
1123 if (!IsNpadIdValid(npad_id)) { 1137 std::scoped_lock lock{mutex};
1124 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); 1138 return npad_resource.SetHomeProtectionEnabled(aruid, npad_id, is_enabled);
1125 return InvalidNpadId;
1126 }
1127 auto& controller = GetControllerFromNpadIdType(npad_id);
1128 controller.unintended_home_button_input_protection = is_protection_enabled;
1129 return ResultSuccess;
1130} 1139}
1131 1140
1132void NPad::SetAnalogStickUseCenterClamp(bool use_center_clamp) { 1141void NPad::SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled) {
1133 analog_stick_use_center_clamp = use_center_clamp; 1142 std::scoped_lock lock{mutex};
1143 npad_resource.SetNpadAnalogStickUseCenterClamp(aruid, is_enabled);
1134} 1144}
1135 1145
1136void NPad::ClearAllConnectedControllers() { 1146void NPad::ClearAllConnectedControllers() {
1137 for (auto& controller : controller_data) { 1147 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
1138 if (controller.device->IsConnected() && 1148 for (auto& controller : controller_data[aruid_index]) {
1139 controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::None) { 1149 if (controller.device->IsConnected() &&
1140 controller.device->Disconnect(); 1150 controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::None) {
1141 controller.device->SetNpadStyleIndex(Core::HID::NpadStyleIndex::None); 1151 controller.device->Disconnect();
1152 controller.device->SetNpadStyleIndex(Core::HID::NpadStyleIndex::None);
1153 }
1142 } 1154 }
1143 } 1155 }
1144} 1156}
1145 1157
1146void NPad::DisconnectAllConnectedControllers() { 1158void NPad::DisconnectAllConnectedControllers() {
1147 for (auto& controller : controller_data) { 1159 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
1148 controller.device->Disconnect(); 1160 for (auto& controller : controller_data[aruid_index]) {
1161 controller.device->Disconnect();
1162 }
1149 } 1163 }
1150} 1164}
1151 1165
1152void NPad::ConnectAllDisconnectedControllers() { 1166void NPad::ConnectAllDisconnectedControllers() {
1153 for (auto& controller : controller_data) { 1167 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
1154 if (controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::None && 1168 for (auto& controller : controller_data[aruid_index]) {
1155 !controller.device->IsConnected()) { 1169 if (controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::None &&
1156 controller.device->Connect(); 1170 !controller.device->IsConnected()) {
1171 controller.device->Connect();
1172 }
1157 } 1173 }
1158 } 1174 }
1159} 1175}
1160 1176
1161void NPad::ClearAllControllers() { 1177void NPad::ClearAllControllers() {
1162 for (auto& controller : controller_data) { 1178 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
1163 controller.device->Disconnect(); 1179 for (auto& controller : controller_data[aruid_index]) {
1164 controller.device->SetNpadStyleIndex(Core::HID::NpadStyleIndex::None); 1180 controller.device->Disconnect();
1181 controller.device->SetNpadStyleIndex(Core::HID::NpadStyleIndex::None);
1182 }
1165 } 1183 }
1166} 1184}
1167 1185
@@ -1169,128 +1187,105 @@ Core::HID::NpadButton NPad::GetAndResetPressState() {
1169 return static_cast<Core::HID::NpadButton>(press_state.exchange(0)); 1187 return static_cast<Core::HID::NpadButton>(press_state.exchange(0));
1170} 1188}
1171 1189
1172void NPad::ApplyNpadSystemCommonPolicy() { 1190Result NPad::ApplyNpadSystemCommonPolicy(u64 aruid) {
1173 Core::HID::NpadStyleTag styletag{}; 1191 std::scoped_lock lock{mutex};
1174 styletag.fullkey.Assign(1); 1192 const Result result = npad_resource.ApplyNpadSystemCommonPolicy(aruid, false);
1175 styletag.handheld.Assign(1); 1193 if (result.IsSuccess()) {
1176 styletag.joycon_dual.Assign(1); 1194 OnUpdate({});
1177 styletag.system_ext.Assign(1); 1195 }
1178 styletag.system.Assign(1); 1196 return result;
1179 SetSupportedStyleSet(styletag); 1197}
1180
1181 SetNpadHandheldActivationMode(NpadHandheldActivationMode::Dual);
1182
1183 supported_npad_id_types.clear();
1184 supported_npad_id_types.resize(10);
1185 supported_npad_id_types[0] = Core::HID::NpadIdType::Player1;
1186 supported_npad_id_types[1] = Core::HID::NpadIdType::Player2;
1187 supported_npad_id_types[2] = Core::HID::NpadIdType::Player3;
1188 supported_npad_id_types[3] = Core::HID::NpadIdType::Player4;
1189 supported_npad_id_types[4] = Core::HID::NpadIdType::Player5;
1190 supported_npad_id_types[5] = Core::HID::NpadIdType::Player6;
1191 supported_npad_id_types[6] = Core::HID::NpadIdType::Player7;
1192 supported_npad_id_types[7] = Core::HID::NpadIdType::Player8;
1193 supported_npad_id_types[8] = Core::HID::NpadIdType::Other;
1194 supported_npad_id_types[9] = Core::HID::NpadIdType::Handheld;
1195}
1196
1197bool NPad::IsControllerSupported(Core::HID::NpadStyleIndex controller) const {
1198 if (controller == Core::HID::NpadStyleIndex::Handheld) {
1199 const bool support_handheld =
1200 std::find(supported_npad_id_types.begin(), supported_npad_id_types.end(),
1201 Core::HID::NpadIdType::Handheld) != supported_npad_id_types.end();
1202 // Handheld is not even a supported type, lets stop here
1203 if (!support_handheld) {
1204 return false;
1205 }
1206 // Handheld shouldn't be supported in docked mode
1207 if (Settings::IsDockedMode()) {
1208 return false;
1209 }
1210 1198
1211 return true; 1199Result NPad::ApplyNpadSystemCommonPolicyFull(u64 aruid) {
1212 } 1200 std::scoped_lock lock{mutex};
1213 1201 const Result result = npad_resource.ApplyNpadSystemCommonPolicy(aruid, true);
1214 if (std::any_of(supported_npad_id_types.begin(), supported_npad_id_types.end(), 1202 if (result.IsSuccess()) {
1215 [](Core::HID::NpadIdType npad_id) { 1203 OnUpdate({});
1216 return npad_id <= Core::HID::NpadIdType::Player8;
1217 })) {
1218 Core::HID::NpadStyleTag style = GetSupportedStyleSet();
1219 switch (controller) {
1220 case Core::HID::NpadStyleIndex::ProController:
1221 return style.fullkey.As<bool>();
1222 case Core::HID::NpadStyleIndex::JoyconDual:
1223 return style.joycon_dual.As<bool>();
1224 case Core::HID::NpadStyleIndex::JoyconLeft:
1225 return style.joycon_left.As<bool>();
1226 case Core::HID::NpadStyleIndex::JoyconRight:
1227 return style.joycon_right.As<bool>();
1228 case Core::HID::NpadStyleIndex::GameCube:
1229 return style.gamecube.As<bool>();
1230 case Core::HID::NpadStyleIndex::Pokeball:
1231 return style.palma.As<bool>();
1232 case Core::HID::NpadStyleIndex::NES:
1233 return style.lark.As<bool>();
1234 case Core::HID::NpadStyleIndex::SNES:
1235 return style.lucia.As<bool>();
1236 case Core::HID::NpadStyleIndex::N64:
1237 return style.lagoon.As<bool>();
1238 case Core::HID::NpadStyleIndex::SegaGenesis:
1239 return style.lager.As<bool>();
1240 default:
1241 return false;
1242 }
1243 } 1204 }
1205 return result;
1206}
1207
1208Result NPad::ClearNpadSystemCommonPolicy(u64 aruid) {
1209 std::scoped_lock lock{mutex};
1210 const Result result = npad_resource.ClearNpadSystemCommonPolicy(aruid);
1211 if (result.IsSuccess()) {
1212 OnUpdate({});
1213 }
1214 return result;
1215}
1216
1217void NPad::SetRevision(u64 aruid, NpadRevision revision) {
1218 npad_resource.SetNpadRevision(aruid, revision);
1219}
1220
1221NpadRevision NPad::GetRevision(u64 aruid) {
1222 return npad_resource.GetNpadRevision(aruid);
1223}
1224
1225Result NPad::RegisterAppletResourceUserId(u64 aruid) {
1226 return npad_resource.RegisterAppletResourceUserId(aruid);
1227}
1228
1229void NPad::UnregisterAppletResourceUserId(u64 aruid) {
1230 npad_resource.UnregisterAppletResourceUserId(aruid);
1231}
1244 1232
1245 return false; 1233void NPad::SetNpadExternals(std::shared_ptr<AppletResource> resource,
1234 std::recursive_mutex* shared_mutex) {
1235 applet_resource_holder.applet_resource = resource;
1236 applet_resource_holder.shared_mutex = shared_mutex;
1237 applet_resource_holder.shared_npad_resource = &npad_resource;
1246} 1238}
1247 1239
1248NPad::NpadControllerData& NPad::GetControllerFromHandle( 1240NPad::NpadControllerData& NPad::GetControllerFromHandle(
1249 const Core::HID::VibrationDeviceHandle& device_handle) { 1241 u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle) {
1250 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id); 1242 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
1251 return GetControllerFromNpadIdType(npad_id); 1243 return GetControllerFromNpadIdType(aruid, npad_id);
1252} 1244}
1253 1245
1254const NPad::NpadControllerData& NPad::GetControllerFromHandle( 1246const NPad::NpadControllerData& NPad::GetControllerFromHandle(
1255 const Core::HID::VibrationDeviceHandle& device_handle) const { 1247 u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle) const {
1256 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id); 1248 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
1257 return GetControllerFromNpadIdType(npad_id); 1249 return GetControllerFromNpadIdType(aruid, npad_id);
1258} 1250}
1259 1251
1260NPad::NpadControllerData& NPad::GetControllerFromHandle( 1252NPad::NpadControllerData& NPad::GetControllerFromHandle(
1261 const Core::HID::SixAxisSensorHandle& device_handle) { 1253 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) {
1262 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id); 1254 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
1263 return GetControllerFromNpadIdType(npad_id); 1255 return GetControllerFromNpadIdType(aruid, npad_id);
1264} 1256}
1265 1257
1266const NPad::NpadControllerData& NPad::GetControllerFromHandle( 1258const NPad::NpadControllerData& NPad::GetControllerFromHandle(
1267 const Core::HID::SixAxisSensorHandle& device_handle) const { 1259 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const {
1268 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id); 1260 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
1269 return GetControllerFromNpadIdType(npad_id); 1261 return GetControllerFromNpadIdType(aruid, npad_id);
1270} 1262}
1271 1263
1272NPad::NpadControllerData& NPad::GetControllerFromNpadIdType(Core::HID::NpadIdType npad_id) { 1264NPad::NpadControllerData& NPad::GetControllerFromNpadIdType(u64 aruid,
1265 Core::HID::NpadIdType npad_id) {
1273 if (!IsNpadIdValid(npad_id)) { 1266 if (!IsNpadIdValid(npad_id)) {
1274 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); 1267 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
1275 npad_id = Core::HID::NpadIdType::Player1; 1268 npad_id = Core::HID::NpadIdType::Player1;
1276 } 1269 }
1277 const auto npad_index = NpadIdTypeToIndex(npad_id); 1270 const auto npad_index = NpadIdTypeToIndex(npad_id);
1278 return controller_data[npad_index]; 1271 const auto aruid_index = applet_resource_holder.applet_resource->GetIndexFromAruid(aruid);
1272 return controller_data[aruid_index][npad_index];
1279} 1273}
1280 1274
1281const NPad::NpadControllerData& NPad::GetControllerFromNpadIdType( 1275const NPad::NpadControllerData& NPad::GetControllerFromNpadIdType(
1282 Core::HID::NpadIdType npad_id) const { 1276 u64 aruid, Core::HID::NpadIdType npad_id) const {
1283 if (!IsNpadIdValid(npad_id)) { 1277 if (!IsNpadIdValid(npad_id)) {
1284 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id); 1278 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
1285 npad_id = Core::HID::NpadIdType::Player1; 1279 npad_id = Core::HID::NpadIdType::Player1;
1286 } 1280 }
1287 const auto npad_index = NpadIdTypeToIndex(npad_id); 1281 const auto npad_index = NpadIdTypeToIndex(npad_id);
1288 return controller_data[npad_index]; 1282 const auto aruid_index = applet_resource_holder.applet_resource->GetIndexFromAruid(aruid);
1283 return controller_data[aruid_index][npad_index];
1289} 1284}
1290 1285
1291Core::HID::SixAxisSensorProperties& NPad::GetSixaxisProperties( 1286Core::HID::SixAxisSensorProperties& NPad::GetSixaxisProperties(
1292 const Core::HID::SixAxisSensorHandle& sixaxis_handle) { 1287 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle) {
1293 auto& controller = GetControllerFromHandle(sixaxis_handle); 1288 auto& controller = GetControllerFromHandle(aruid, sixaxis_handle);
1294 switch (sixaxis_handle.npad_type) { 1289 switch (sixaxis_handle.npad_type) {
1295 case Core::HID::NpadStyleIndex::ProController: 1290 case Core::HID::NpadStyleIndex::ProController:
1296 case Core::HID::NpadStyleIndex::Pokeball: 1291 case Core::HID::NpadStyleIndex::Pokeball:
@@ -1312,8 +1307,8 @@ Core::HID::SixAxisSensorProperties& NPad::GetSixaxisProperties(
1312} 1307}
1313 1308
1314const Core::HID::SixAxisSensorProperties& NPad::GetSixaxisProperties( 1309const Core::HID::SixAxisSensorProperties& NPad::GetSixaxisProperties(
1315 const Core::HID::SixAxisSensorHandle& sixaxis_handle) const { 1310 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle) const {
1316 const auto& controller = GetControllerFromHandle(sixaxis_handle); 1311 const auto& controller = GetControllerFromHandle(aruid, sixaxis_handle);
1317 switch (sixaxis_handle.npad_type) { 1312 switch (sixaxis_handle.npad_type) {
1318 case Core::HID::NpadStyleIndex::ProController: 1313 case Core::HID::NpadStyleIndex::ProController:
1319 case Core::HID::NpadStyleIndex::Pokeball: 1314 case Core::HID::NpadStyleIndex::Pokeball:
@@ -1335,7 +1330,8 @@ const Core::HID::SixAxisSensorProperties& NPad::GetSixaxisProperties(
1335} 1330}
1336 1331
1337AppletDetailedUiType NPad::GetAppletDetailedUiType(Core::HID::NpadIdType npad_id) { 1332AppletDetailedUiType NPad::GetAppletDetailedUiType(Core::HID::NpadIdType npad_id) {
1338 const auto& shared_memory = GetControllerFromNpadIdType(npad_id).shared_memory; 1333 const auto aruid = applet_resource_holder.applet_resource->GetActiveAruid();
1334 const auto& shared_memory = GetControllerFromNpadIdType(aruid, npad_id).shared_memory;
1339 1335
1340 return { 1336 return {
1341 .ui_variant = 0, 1337 .ui_variant = 0,
diff --git a/src/core/hle/service/hid/controllers/npad.h b/src/core/hle/service/hid/controllers/npad.h
index 80cfcb2bb..8ab333064 100644
--- a/src/core/hle/service/hid/controllers/npad.h
+++ b/src/core/hle/service/hid/controllers/npad.h
@@ -11,6 +11,7 @@
11#include "common/common_types.h" 11#include "common/common_types.h"
12#include "core/hid/hid_types.h" 12#include "core/hid/hid_types.h"
13#include "core/hle/service/hid/controllers/controller_base.h" 13#include "core/hle/service/hid/controllers/controller_base.h"
14#include "core/hle/service/hid/controllers/npad/npad_resource.h"
14#include "core/hle/service/hid/controllers/types/npad_types.h" 15#include "core/hle/service/hid/controllers/types/npad_types.h"
15 16
16namespace Core::HID { 17namespace Core::HID {
@@ -35,99 +36,116 @@ struct NpadInternalState;
35struct NpadSixAxisSensorLifo; 36struct NpadSixAxisSensorLifo;
36struct NpadSharedMemoryFormat; 37struct NpadSharedMemoryFormat;
37 38
38class NPad final : public ControllerBase { 39class NPad final {
39public: 40public:
40 explicit NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_); 41 explicit NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_);
41 ~NPad() override; 42 ~NPad();
42 43
43 // Called when the controller is initialized 44 Result Activate();
44 void OnInit() override; 45 Result Activate(u64 aruid);
45 46
46 // When the controller is released 47 Result ActivateNpadResource();
47 void OnRelease() override; 48 Result ActivateNpadResource(u64 aruid);
48 49
49 // When the controller is requesting an update for the shared memory 50 // When the controller is requesting an update for the shared memory
50 void OnUpdate(const Core::Timing::CoreTiming& core_timing) override; 51 void OnUpdate(const Core::Timing::CoreTiming& core_timing);
51 52
52 void SetSupportedStyleSet(Core::HID::NpadStyleTag style_set); 53 Result SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet supported_style_set);
53 Core::HID::NpadStyleTag GetSupportedStyleSet() const; 54 Result GetSupportedNpadStyleSet(u64 aruid,
55 Core::HID::NpadStyleSet& out_supported_style_set) const;
56 Result GetMaskedSupportedNpadStyleSet(u64 aruid,
57 Core::HID::NpadStyleSet& out_supported_style_set) const;
54 58
55 Result SetSupportedNpadIdTypes(std::span<const u8> data); 59 Result SetSupportedNpadIdType(u64 aruid,
56 void GetSupportedNpadIdTypes(u32* data, std::size_t max_length); 60 std::span<const Core::HID::NpadIdType> supported_npad_list);
57 std::size_t GetSupportedNpadIdTypesSize() const;
58 61
59 void SetHoldType(NpadJoyHoldType joy_hold_type); 62 Result SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type);
60 NpadJoyHoldType GetHoldType() const; 63 Result GetNpadJoyHoldType(u64 aruid, NpadJoyHoldType& out_hold_type) const;
61 64
62 void SetNpadHandheldActivationMode(NpadHandheldActivationMode activation_mode); 65 Result SetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode mode);
63 NpadHandheldActivationMode GetNpadHandheldActivationMode() const; 66 Result GetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode& out_mode) const;
64 67
65 void SetNpadCommunicationMode(NpadCommunicationMode communication_mode_); 68 bool SetNpadMode(u64 aruid, Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType npad_id,
66 NpadCommunicationMode GetNpadCommunicationMode() const;
67
68 bool SetNpadMode(Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType npad_id,
69 NpadJoyDeviceType npad_device_type, NpadJoyAssignmentMode assignment_mode); 69 NpadJoyDeviceType npad_device_type, NpadJoyAssignmentMode assignment_mode);
70 70
71 bool VibrateControllerAtIndex(Core::HID::NpadIdType npad_id, std::size_t device_index, 71 bool VibrateControllerAtIndex(u64 aruid, Core::HID::NpadIdType npad_id,
72 std::size_t device_index,
72 const Core::HID::VibrationValue& vibration_value); 73 const Core::HID::VibrationValue& vibration_value);
73 74
74 void VibrateController(const Core::HID::VibrationDeviceHandle& vibration_device_handle, 75 void VibrateController(u64 aruid,
76 const Core::HID::VibrationDeviceHandle& vibration_device_handle,
75 const Core::HID::VibrationValue& vibration_value); 77 const Core::HID::VibrationValue& vibration_value);
76 78
77 void VibrateControllers( 79 void VibrateControllers(
78 std::span<const Core::HID::VibrationDeviceHandle> vibration_device_handles, 80 u64 aruid, std::span<const Core::HID::VibrationDeviceHandle> vibration_device_handles,
79 std::span<const Core::HID::VibrationValue> vibration_values); 81 std::span<const Core::HID::VibrationValue> vibration_values);
80 82
81 Core::HID::VibrationValue GetLastVibration( 83 Core::HID::VibrationValue GetLastVibration(
82 const Core::HID::VibrationDeviceHandle& vibration_device_handle) const; 84 u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const;
83 85
84 void InitializeVibrationDevice(const Core::HID::VibrationDeviceHandle& vibration_device_handle); 86 void InitializeVibrationDevice(const Core::HID::VibrationDeviceHandle& vibration_device_handle);
85 87
86 void InitializeVibrationDeviceAtIndex(Core::HID::NpadIdType npad_id, std::size_t device_index); 88 void InitializeVibrationDeviceAtIndex(u64 aruid, Core::HID::NpadIdType npad_id,
89 std::size_t device_index);
87 90
88 void SetPermitVibrationSession(bool permit_vibration_session); 91 void SetPermitVibrationSession(bool permit_vibration_session);
89 92
90 bool IsVibrationDeviceMounted( 93 bool IsVibrationDeviceMounted(
91 const Core::HID::VibrationDeviceHandle& vibration_device_handle) const; 94 u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const;
92 95
93 Kernel::KReadableEvent& GetStyleSetChangedEvent(Core::HID::NpadIdType npad_id); 96 Result AcquireNpadStyleSetUpdateEventHandle(u64 aruid, Kernel::KReadableEvent** out_event,
94 void SignalStyleSetChangedEvent(Core::HID::NpadIdType npad_id) const; 97 Core::HID::NpadIdType npad_id);
95 98
96 // Adds a new controller at an index. 99 // Adds a new controller at an index.
97 void AddNewControllerAt(Core::HID::NpadStyleIndex controller, Core::HID::NpadIdType npad_id); 100 void AddNewControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller,
101 Core::HID::NpadIdType npad_id);
98 // Adds a new controller at an index with connection status. 102 // Adds a new controller at an index with connection status.
99 void UpdateControllerAt(Core::HID::NpadStyleIndex controller, Core::HID::NpadIdType npad_id, 103 void UpdateControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller,
100 bool connected); 104 Core::HID::NpadIdType npad_id, bool connected);
101 105
102 Result DisconnectNpad(Core::HID::NpadIdType npad_id); 106 Result DisconnectNpad(u64 aruid, Core::HID::NpadIdType npad_id);
103 107
104 Result IsFirmwareUpdateAvailableForSixAxisSensor( 108 Result IsFirmwareUpdateAvailableForSixAxisSensor(
105 const Core::HID::SixAxisSensorHandle& sixaxis_handle, bool& is_firmware_available) const; 109 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle,
110 bool& is_firmware_available) const;
106 Result ResetIsSixAxisSensorDeviceNewlyAssigned( 111 Result ResetIsSixAxisSensorDeviceNewlyAssigned(
107 const Core::HID::SixAxisSensorHandle& sixaxis_handle); 112 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle);
108 113
109 Result GetLedPattern(Core::HID::NpadIdType npad_id, Core::HID::LedPattern& pattern) const; 114 Result GetLedPattern(Core::HID::NpadIdType npad_id, Core::HID::LedPattern& pattern) const;
110 Result IsUnintendedHomeButtonInputProtectionEnabled(Core::HID::NpadIdType npad_id, 115
111 bool& is_enabled) const; 116 Result IsUnintendedHomeButtonInputProtectionEnabled(bool& out_is_enabled, u64 aruid,
112 Result SetUnintendedHomeButtonInputProtectionEnabled(bool is_protection_enabled, 117 Core::HID::NpadIdType npad_id) const;
113 Core::HID::NpadIdType npad_id); 118 Result EnableUnintendedHomeButtonInputProtection(u64 aruid, Core::HID::NpadIdType npad_id,
114 void SetAnalogStickUseCenterClamp(bool use_center_clamp); 119 bool is_enabled);
120
121 void SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled);
115 void ClearAllConnectedControllers(); 122 void ClearAllConnectedControllers();
116 void DisconnectAllConnectedControllers(); 123 void DisconnectAllConnectedControllers();
117 void ConnectAllDisconnectedControllers(); 124 void ConnectAllDisconnectedControllers();
118 void ClearAllControllers(); 125 void ClearAllControllers();
119 126
120 Result MergeSingleJoyAsDualJoy(Core::HID::NpadIdType npad_id_1, 127 Result MergeSingleJoyAsDualJoy(u64 aruid, Core::HID::NpadIdType npad_id_1,
121 Core::HID::NpadIdType npad_id_2); 128 Core::HID::NpadIdType npad_id_2);
122 void StartLRAssignmentMode(); 129 Result StartLrAssignmentMode(u64 aruid);
123 void StopLRAssignmentMode(); 130 Result StopLrAssignmentMode(u64 aruid);
124 Result SwapNpadAssignment(Core::HID::NpadIdType npad_id_1, Core::HID::NpadIdType npad_id_2); 131 Result SwapNpadAssignment(u64 aruid, Core::HID::NpadIdType npad_id_1,
132 Core::HID::NpadIdType npad_id_2);
125 133
126 // Logical OR for all buttons presses on all controllers 134 // Logical OR for all buttons presses on all controllers
127 // Specifically for cheat engine and other features. 135 // Specifically for cheat engine and other features.
128 Core::HID::NpadButton GetAndResetPressState(); 136 Core::HID::NpadButton GetAndResetPressState();
129 137
130 void ApplyNpadSystemCommonPolicy(); 138 Result ApplyNpadSystemCommonPolicy(u64 aruid);
139 Result ApplyNpadSystemCommonPolicyFull(u64 aruid);
140 Result ClearNpadSystemCommonPolicy(u64 aruid);
141
142 void SetRevision(u64 aruid, NpadRevision revision);
143 NpadRevision GetRevision(u64 aruid);
144
145 Result RegisterAppletResourceUserId(u64 aruid);
146 void UnregisterAppletResourceUserId(u64 aruid);
147 void SetNpadExternals(std::shared_ptr<AppletResource> resource,
148 std::recursive_mutex* shared_mutex);
131 149
132 AppletDetailedUiType GetAppletDetailedUiType(Core::HID::NpadIdType npad_id); 150 AppletDetailedUiType GetAppletDetailedUiType(Core::HID::NpadIdType npad_id);
133 151
@@ -139,12 +157,10 @@ private:
139 }; 157 };
140 158
141 struct NpadControllerData { 159 struct NpadControllerData {
142 Kernel::KEvent* styleset_changed_event{};
143 NpadInternalState* shared_memory = nullptr; 160 NpadInternalState* shared_memory = nullptr;
144 Core::HID::EmulatedController* device = nullptr; 161 Core::HID::EmulatedController* device = nullptr;
145 162
146 std::array<VibrationData, 2> vibration{}; 163 std::array<VibrationData, 2> vibration{};
147 bool unintended_home_button_input_protection{};
148 bool is_connected{}; 164 bool is_connected{};
149 165
150 // Dual joycons can have only one side connected 166 // Dual joycons can have only one side connected
@@ -159,39 +175,40 @@ private:
159 }; 175 };
160 176
161 void ControllerUpdate(Core::HID::ControllerTriggerType type, std::size_t controller_idx); 177 void ControllerUpdate(Core::HID::ControllerTriggerType type, std::size_t controller_idx);
162 void InitNewlyAddedController(Core::HID::NpadIdType npad_id); 178 void InitNewlyAddedController(u64 aruid, Core::HID::NpadIdType npad_id);
163 bool IsControllerSupported(Core::HID::NpadStyleIndex controller) const; 179 void RequestPadStateUpdate(u64 aruid, Core::HID::NpadIdType npad_id);
164 void RequestPadStateUpdate(Core::HID::NpadIdType npad_id);
165 void WriteEmptyEntry(NpadInternalState* npad); 180 void WriteEmptyEntry(NpadInternalState* npad);
166 181
167 NpadControllerData& GetControllerFromHandle( 182 NpadControllerData& GetControllerFromHandle(
168 const Core::HID::VibrationDeviceHandle& device_handle); 183 u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle);
169 const NpadControllerData& GetControllerFromHandle( 184 const NpadControllerData& GetControllerFromHandle(
170 const Core::HID::VibrationDeviceHandle& device_handle) const; 185 u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle) const;
171 NpadControllerData& GetControllerFromHandle( 186 NpadControllerData& GetControllerFromHandle(
172 const Core::HID::SixAxisSensorHandle& device_handle); 187 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle);
173 const NpadControllerData& GetControllerFromHandle( 188 const NpadControllerData& GetControllerFromHandle(
174 const Core::HID::SixAxisSensorHandle& device_handle) const; 189 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const;
175 NpadControllerData& GetControllerFromNpadIdType(Core::HID::NpadIdType npad_id); 190 NpadControllerData& GetControllerFromNpadIdType(u64 aruid, Core::HID::NpadIdType npad_id);
176 const NpadControllerData& GetControllerFromNpadIdType(Core::HID::NpadIdType npad_id) const; 191 const NpadControllerData& GetControllerFromNpadIdType(u64 aruid,
192 Core::HID::NpadIdType npad_id) const;
177 193
178 Core::HID::SixAxisSensorProperties& GetSixaxisProperties( 194 Core::HID::SixAxisSensorProperties& GetSixaxisProperties(
179 const Core::HID::SixAxisSensorHandle& device_handle); 195 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle);
180 const Core::HID::SixAxisSensorProperties& GetSixaxisProperties( 196 const Core::HID::SixAxisSensorProperties& GetSixaxisProperties(
181 const Core::HID::SixAxisSensorHandle& device_handle) const; 197 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const;
182 198
183 std::atomic<u64> press_state{}; 199 Core::HID::HIDCore& hid_core;
184
185 std::array<NpadControllerData, NpadCount> controller_data{};
186 KernelHelpers::ServiceContext& service_context; 200 KernelHelpers::ServiceContext& service_context;
187 std::mutex mutex; 201
188 std::vector<Core::HID::NpadIdType> supported_npad_id_types{}; 202 s32 ref_counter{};
189 NpadJoyHoldType hold_type{NpadJoyHoldType::Vertical}; 203 mutable std::mutex mutex;
190 NpadHandheldActivationMode handheld_activation_mode{NpadHandheldActivationMode::Dual}; 204 NPadResource npad_resource;
191 NpadCommunicationMode communication_mode{NpadCommunicationMode::Default}; 205 AppletResourceHolder applet_resource_holder{};
192 bool permit_vibration_session_enabled{false}; 206 Kernel::KEvent* input_event{nullptr};
193 bool analog_stick_use_center_clamp{false}; 207 std::mutex* input_mutex{nullptr};
194 bool is_in_lr_assignment_mode{false}; 208
195 bool is_controller_initialized{false}; 209 std::atomic<u64> press_state{};
210 bool permit_vibration_session_enabled;
211 std::array<std::array<NpadControllerData, MaxSupportedNpadIdTypes>, AruidIndexMax>
212 controller_data{};
196}; 213};
197} // namespace Service::HID 214} // namespace Service::HID
diff --git a/src/core/hle/service/hid/controllers/npad/npad_data.cpp b/src/core/hle/service/hid/controllers/npad/npad_data.cpp
new file mode 100644
index 000000000..d2423b6d3
--- /dev/null
+++ b/src/core/hle/service/hid/controllers/npad/npad_data.cpp
@@ -0,0 +1,228 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#include "core/hle/service/hid/controllers/npad/npad_data.h"
5#include "core/hle/service/hid/hid_util.h"
6
7namespace Service::HID {
8
9NPadData::NPadData() {
10 ClearNpadSystemCommonPolicy();
11}
12
13NPadData::~NPadData() = default;
14
15NpadStatus NPadData::GetNpadStatus() const {
16 return status;
17}
18
19void NPadData::SetNpadAnalogStickUseCenterClamp(bool is_enabled) {
20 status.use_center_clamp.Assign(is_enabled);
21}
22
23bool NPadData::GetNpadAnalogStickUseCenterClamp() const {
24 return status.use_center_clamp.As<bool>();
25}
26
27void NPadData::SetNpadSystemExtStateEnabled(bool is_enabled) {
28 status.system_ext_state.Assign(is_enabled);
29}
30
31bool NPadData::GetNpadSystemExtState() const {
32 return status.system_ext_state.As<bool>();
33}
34
35Result NPadData::SetSupportedNpadIdType(std::span<const Core::HID::NpadIdType> list) {
36 // Note: Real limit is 11. But array size is 10. N's bug?
37 if (list.size() > MaxSupportedNpadIdTypes) {
38 return ResultInvalidArraySize;
39 }
40
41 supported_npad_id_types_count = list.size();
42 memcpy(supported_npad_id_types.data(), list.data(),
43 list.size() * sizeof(Core::HID::NpadIdType));
44
45 return ResultSuccess;
46}
47
48std::size_t NPadData::GetSupportedNpadIdType(std::span<Core::HID::NpadIdType> out_list) const {
49 std::size_t out_size = std::min(supported_npad_id_types_count, out_list.size());
50
51 memcpy(out_list.data(), supported_npad_id_types.data(),
52 out_size * sizeof(Core::HID::NpadIdType));
53
54 return out_size;
55}
56
57bool NPadData::IsNpadIdTypeSupported(Core::HID::NpadIdType npad_id) const {
58 for (std::size_t i = 0; i < supported_npad_id_types_count; i++) {
59 if (supported_npad_id_types[i] == npad_id) {
60 return true;
61 }
62 }
63
64 return false;
65}
66
67void NPadData::SetNpadSystemCommonPolicy(bool is_full_policy) {
68 supported_npad_style_set = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::JoyDual |
69 Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
70 handheld_activation_mode = NpadHandheldActivationMode::Dual;
71
72 status.is_supported_styleset_set.Assign(true);
73 status.is_hold_type_set.Assign(true);
74 status.lr_assignment_mode.Assign(false);
75 status.is_policy.Assign(true);
76 if (is_full_policy) {
77 status.is_full_policy.Assign(true);
78 }
79
80 supported_npad_id_types_count = 10;
81 supported_npad_id_types[0] = Core::HID::NpadIdType::Player1;
82 supported_npad_id_types[1] = Core::HID::NpadIdType::Player2;
83 supported_npad_id_types[2] = Core::HID::NpadIdType::Player3;
84 supported_npad_id_types[3] = Core::HID::NpadIdType::Player4;
85 supported_npad_id_types[4] = Core::HID::NpadIdType::Player5;
86 supported_npad_id_types[5] = Core::HID::NpadIdType::Player6;
87 supported_npad_id_types[6] = Core::HID::NpadIdType::Player7;
88 supported_npad_id_types[7] = Core::HID::NpadIdType::Player8;
89 supported_npad_id_types[8] = Core::HID::NpadIdType::Other;
90 supported_npad_id_types[9] = Core::HID::NpadIdType::Handheld;
91
92 for (auto& input_protection : is_unintended_home_button_input_protection) {
93 input_protection = true;
94 }
95}
96
97void NPadData::ClearNpadSystemCommonPolicy() {
98 status.raw = 0;
99 supported_npad_style_set = Core::HID::NpadStyleSet::All;
100 npad_hold_type = NpadJoyHoldType::Vertical;
101 handheld_activation_mode = NpadHandheldActivationMode::Dual;
102
103 for (auto& button_assignment : npad_button_assignment) {
104 button_assignment = Core::HID::NpadButton::None;
105 }
106
107 supported_npad_id_types_count = 10;
108 supported_npad_id_types[0] = Core::HID::NpadIdType::Player1;
109 supported_npad_id_types[1] = Core::HID::NpadIdType::Player2;
110 supported_npad_id_types[2] = Core::HID::NpadIdType::Player3;
111 supported_npad_id_types[3] = Core::HID::NpadIdType::Player4;
112 supported_npad_id_types[4] = Core::HID::NpadIdType::Player5;
113 supported_npad_id_types[5] = Core::HID::NpadIdType::Player6;
114 supported_npad_id_types[6] = Core::HID::NpadIdType::Player7;
115 supported_npad_id_types[7] = Core::HID::NpadIdType::Player8;
116 supported_npad_id_types[8] = Core::HID::NpadIdType::Other;
117 supported_npad_id_types[9] = Core::HID::NpadIdType::Handheld;
118
119 for (auto& input_protection : is_unintended_home_button_input_protection) {
120 input_protection = true;
121 }
122}
123
124void NPadData::SetNpadJoyHoldType(NpadJoyHoldType hold_type) {
125 npad_hold_type = hold_type;
126 status.is_hold_type_set.Assign(true);
127}
128
129NpadJoyHoldType NPadData::GetNpadJoyHoldType() const {
130 return npad_hold_type;
131}
132
133void NPadData::SetHandheldActivationMode(NpadHandheldActivationMode activation_mode) {
134 handheld_activation_mode = activation_mode;
135}
136
137NpadHandheldActivationMode NPadData::GetHandheldActivationMode() const {
138 return handheld_activation_mode;
139}
140
141void NPadData::SetSupportedNpadStyleSet(Core::HID::NpadStyleSet style_set) {
142 supported_npad_style_set = style_set;
143 status.is_supported_styleset_set.Assign(true);
144 status.is_hold_type_set.Assign(true);
145}
146
147Core::HID::NpadStyleSet NPadData::GetSupportedNpadStyleSet() const {
148 return supported_npad_style_set;
149}
150
151bool NPadData::IsNpadStyleIndexSupported(Core::HID::NpadStyleIndex style_index) const {
152 Core::HID::NpadStyleTag style = {supported_npad_style_set};
153 switch (style_index) {
154 case Core::HID::NpadStyleIndex::ProController:
155 return style.fullkey.As<bool>();
156 case Core::HID::NpadStyleIndex::Handheld:
157 return style.handheld.As<bool>();
158 case Core::HID::NpadStyleIndex::JoyconDual:
159 return style.joycon_dual.As<bool>();
160 case Core::HID::NpadStyleIndex::JoyconLeft:
161 return style.joycon_left.As<bool>();
162 case Core::HID::NpadStyleIndex::JoyconRight:
163 return style.joycon_right.As<bool>();
164 case Core::HID::NpadStyleIndex::GameCube:
165 return style.gamecube.As<bool>();
166 case Core::HID::NpadStyleIndex::Pokeball:
167 return style.palma.As<bool>();
168 case Core::HID::NpadStyleIndex::NES:
169 return style.lark.As<bool>();
170 case Core::HID::NpadStyleIndex::SNES:
171 return style.lucia.As<bool>();
172 case Core::HID::NpadStyleIndex::N64:
173 return style.lagoon.As<bool>();
174 case Core::HID::NpadStyleIndex::SegaGenesis:
175 return style.lager.As<bool>();
176 default:
177 return false;
178 }
179}
180
181void NPadData::SetLrAssignmentMode(bool is_enabled) {
182 status.lr_assignment_mode.Assign(is_enabled);
183}
184
185bool NPadData::GetLrAssignmentMode() const {
186 return status.lr_assignment_mode.As<bool>();
187}
188
189void NPadData::SetAssigningSingleOnSlSrPress(bool is_enabled) {
190 status.assigning_single_on_sl_sr_press.Assign(is_enabled);
191}
192
193bool NPadData::GetAssigningSingleOnSlSrPress() const {
194 return status.assigning_single_on_sl_sr_press.As<bool>();
195}
196
197void NPadData::SetHomeProtectionEnabled(bool is_enabled, Core::HID::NpadIdType npad_id) {
198 is_unintended_home_button_input_protection[NpadIdTypeToIndex(npad_id)] = is_enabled;
199}
200
201bool NPadData::GetHomeProtectionEnabled(Core::HID::NpadIdType npad_id) const {
202 return is_unintended_home_button_input_protection[NpadIdTypeToIndex(npad_id)];
203}
204
205void NPadData::SetCaptureButtonAssignment(Core::HID::NpadButton button_assignment,
206 std::size_t style_index) {
207 npad_button_assignment[style_index] = button_assignment;
208}
209
210Core::HID::NpadButton NPadData::GetCaptureButtonAssignment(std::size_t style_index) const {
211 return npad_button_assignment[style_index];
212}
213
214std::size_t NPadData::GetNpadCaptureButtonAssignmentList(
215 std::span<Core::HID::NpadButton> out_list) const {
216 for (std::size_t i = 0; i < out_list.size(); i++) {
217 Core::HID::NpadStyleSet style_set = GetStylesetByIndex(i);
218 if ((style_set & supported_npad_style_set) == Core::HID::NpadStyleSet::None ||
219 npad_button_assignment[i] == Core::HID::NpadButton::None) {
220 return i;
221 }
222 out_list[i] = npad_button_assignment[i];
223 }
224
225 return out_list.size();
226}
227
228} // namespace Service::HID
diff --git a/src/core/hle/service/hid/controllers/npad/npad_data.h b/src/core/hle/service/hid/controllers/npad/npad_data.h
new file mode 100644
index 000000000..f799a9f9c
--- /dev/null
+++ b/src/core/hle/service/hid/controllers/npad/npad_data.h
@@ -0,0 +1,88 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#pragma once
5
6#include <array>
7#include <span>
8
9#include "common/common_types.h"
10#include "core/hid/hid_types.h"
11#include "core/hle/result.h"
12#include "core/hle/service/hid/controllers/types/npad_types.h"
13
14namespace Service::HID {
15
16struct NpadStatus {
17 union {
18 u32 raw{};
19
20 BitField<0, 1, u32> is_supported_styleset_set;
21 BitField<1, 1, u32> is_hold_type_set;
22 BitField<2, 1, u32> lr_assignment_mode;
23 BitField<3, 1, u32> assigning_single_on_sl_sr_press;
24 BitField<4, 1, u32> is_full_policy;
25 BitField<5, 1, u32> is_policy;
26 BitField<6, 1, u32> use_center_clamp;
27 BitField<7, 1, u32> system_ext_state;
28 };
29};
30static_assert(sizeof(NpadStatus) == 4, "NpadStatus is an invalid size");
31
32/// Handles Npad request from HID interfaces
33class NPadData final {
34public:
35 explicit NPadData();
36 ~NPadData();
37
38 NpadStatus GetNpadStatus() const;
39
40 void SetNpadAnalogStickUseCenterClamp(bool is_enabled);
41 bool GetNpadAnalogStickUseCenterClamp() const;
42
43 void SetNpadSystemExtStateEnabled(bool is_enabled);
44 bool GetNpadSystemExtState() const;
45
46 Result SetSupportedNpadIdType(std::span<const Core::HID::NpadIdType> list);
47 std::size_t GetSupportedNpadIdType(std::span<Core::HID::NpadIdType> out_list) const;
48 bool IsNpadIdTypeSupported(Core::HID::NpadIdType npad_id) const;
49
50 void SetNpadSystemCommonPolicy(bool is_full_policy);
51 void ClearNpadSystemCommonPolicy();
52
53 void SetNpadJoyHoldType(NpadJoyHoldType hold_type);
54 NpadJoyHoldType GetNpadJoyHoldType() const;
55
56 void SetHandheldActivationMode(NpadHandheldActivationMode activation_mode);
57 NpadHandheldActivationMode GetHandheldActivationMode() const;
58
59 void SetSupportedNpadStyleSet(Core::HID::NpadStyleSet style_set);
60 Core::HID::NpadStyleSet GetSupportedNpadStyleSet() const;
61 bool IsNpadStyleIndexSupported(Core::HID::NpadStyleIndex style_index) const;
62
63 void SetLrAssignmentMode(bool is_enabled);
64 bool GetLrAssignmentMode() const;
65
66 void SetAssigningSingleOnSlSrPress(bool is_enabled);
67 bool GetAssigningSingleOnSlSrPress() const;
68
69 void SetHomeProtectionEnabled(bool is_enabled, Core::HID::NpadIdType npad_id);
70 bool GetHomeProtectionEnabled(Core::HID::NpadIdType npad_id) const;
71
72 void SetCaptureButtonAssignment(Core::HID::NpadButton button_assignment,
73 std::size_t style_index);
74 Core::HID::NpadButton GetCaptureButtonAssignment(std::size_t style_index) const;
75 std::size_t GetNpadCaptureButtonAssignmentList(std::span<Core::HID::NpadButton> out_list) const;
76
77private:
78 NpadStatus status{};
79 Core::HID::NpadStyleSet supported_npad_style_set{Core::HID::NpadStyleSet::All};
80 NpadJoyHoldType npad_hold_type{NpadJoyHoldType::Vertical};
81 NpadHandheldActivationMode handheld_activation_mode{};
82 std::array<Core::HID::NpadIdType, MaxSupportedNpadIdTypes> supported_npad_id_types{};
83 std::array<Core::HID::NpadButton, StyleIndexCount> npad_button_assignment{};
84 std::size_t supported_npad_id_types_count{};
85 std::array<bool, MaxSupportedNpadIdTypes> is_unintended_home_button_input_protection{};
86};
87
88} // namespace Service::HID
diff --git a/src/core/hle/service/hid/controllers/npad/npad_resource.cpp b/src/core/hle/service/hid/controllers/npad/npad_resource.cpp
new file mode 100644
index 000000000..0a9341a39
--- /dev/null
+++ b/src/core/hle/service/hid/controllers/npad/npad_resource.cpp
@@ -0,0 +1,685 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#include "core/hle/kernel/k_event.h"
5#include "core/hle/kernel/k_readable_event.h"
6#include "core/hle/service/hid/controllers/npad/npad_resource.h"
7#include "core/hle/service/hid/controllers/types/npad_types.h"
8#include "core/hle/service/hid/errors.h"
9#include "core/hle/service/hid/hid_util.h"
10
11namespace Service::HID {
12
13NPadResource::NPadResource(KernelHelpers::ServiceContext& context) : service_context{context} {}
14
15NPadResource::~NPadResource() = default;
16
17Result NPadResource::RegisterAppletResourceUserId(u64 aruid) {
18 const auto aruid_index = GetIndexFromAruid(aruid);
19 if (aruid_index < AruidIndexMax) {
20 return ResultAruidAlreadyRegistered;
21 }
22
23 std::size_t data_index = AruidIndexMax;
24 for (std::size_t i = 0; i < AruidIndexMax; i++) {
25 if (!state[i].flag.is_initialized) {
26 data_index = i;
27 break;
28 }
29 }
30
31 if (data_index == AruidIndexMax) {
32 return ResultAruidNoAvailableEntries;
33 }
34
35 auto& aruid_data = state[data_index];
36
37 aruid_data.aruid = aruid;
38 aruid_data.flag.is_initialized.Assign(true);
39
40 data_index = AruidIndexMax;
41 for (std::size_t i = 0; i < AruidIndexMax; i++) {
42 if (registration_list.flag[i] == RegistrationStatus::Initialized) {
43 if (registration_list.aruid[i] != aruid) {
44 continue;
45 }
46 data_index = i;
47 break;
48 }
49 if (registration_list.flag[i] == RegistrationStatus::None) {
50 data_index = i;
51 break;
52 }
53 }
54
55 if (data_index == AruidIndexMax) {
56 return ResultSuccess;
57 }
58
59 registration_list.flag[data_index] = RegistrationStatus::Initialized;
60 registration_list.aruid[data_index] = aruid;
61
62 return ResultSuccess;
63}
64
65void NPadResource::UnregisterAppletResourceUserId(u64 aruid) {
66 const u64 aruid_index = GetIndexFromAruid(aruid);
67
68 DestroyStyleSetUpdateEvents(aruid);
69 if (aruid_index < AruidIndexMax) {
70 state[aruid_index] = {};
71 registration_list.flag[aruid_index] = RegistrationStatus::PendingDelete;
72 }
73}
74
75void NPadResource::DestroyStyleSetUpdateEvents(u64 aruid) {
76 const u64 aruid_index = GetIndexFromAruid(aruid);
77
78 if (aruid_index >= AruidIndexMax) {
79 return;
80 }
81
82 for (auto& controller_state : state[aruid_index].controller_state) {
83 if (!controller_state.is_styleset_update_event_initialized) {
84 continue;
85 }
86 service_context.CloseEvent(controller_state.style_set_update_event);
87 controller_state.is_styleset_update_event_initialized = false;
88 }
89}
90
91Result NPadResource::Activate(u64 aruid) {
92 const u64 aruid_index = GetIndexFromAruid(aruid);
93
94 if (aruid_index >= AruidIndexMax) {
95 return ResultSuccess;
96 }
97
98 auto& state_data = state[aruid_index];
99
100 if (state_data.flag.is_assigned) {
101 return ResultAruidAlreadyRegistered;
102 }
103
104 state_data.flag.is_assigned.Assign(true);
105 state_data.data.ClearNpadSystemCommonPolicy();
106 state_data.npad_revision = NpadRevision::Revision0;
107 state_data.button_config = {};
108
109 if (active_data_aruid == aruid) {
110 default_hold_type = active_data.GetNpadJoyHoldType();
111 active_data.SetNpadJoyHoldType(default_hold_type);
112 }
113 return ResultSuccess;
114}
115
116Result NPadResource::Activate() {
117 if (ref_counter == std::numeric_limits<s32>::max() - 1) {
118 return ResultAppletResourceOverflow;
119 }
120 if (ref_counter == 0) {
121 RegisterAppletResourceUserId(SystemAruid);
122 Activate(SystemAruid);
123 }
124 ref_counter++;
125 return ResultSuccess;
126}
127
128Result NPadResource::Deactivate() {
129 if (ref_counter == 0) {
130 return ResultAppletResourceNotInitialized;
131 }
132
133 UnregisterAppletResourceUserId(SystemAruid);
134 ref_counter--;
135 return ResultSuccess;
136}
137
138NPadData* NPadResource::GetActiveData() {
139 return &active_data;
140}
141
142u64 NPadResource::GetActiveDataAruid() {
143 return active_data_aruid;
144}
145
146void NPadResource::SetAppletResourceUserId(u64 aruid) {
147 if (active_data_aruid == aruid) {
148 return;
149 }
150
151 active_data_aruid = aruid;
152 default_hold_type = active_data.GetNpadJoyHoldType();
153 const u64 aruid_index = GetIndexFromAruid(aruid);
154
155 if (aruid_index >= AruidIndexMax) {
156 return;
157 }
158
159 auto& data = state[aruid_index].data;
160 if (data.GetNpadStatus().is_policy || data.GetNpadStatus().is_full_policy) {
161 data.SetNpadJoyHoldType(default_hold_type);
162 }
163
164 active_data = data;
165 if (data.GetNpadStatus().is_hold_type_set) {
166 active_data.SetNpadJoyHoldType(default_hold_type);
167 }
168}
169
170std::size_t NPadResource::GetIndexFromAruid(u64 aruid) const {
171 for (std::size_t i = 0; i < AruidIndexMax; i++) {
172 if (registration_list.flag[i] == RegistrationStatus::Initialized &&
173 registration_list.aruid[i] == aruid) {
174 return i;
175 }
176 }
177 return AruidIndexMax;
178}
179
180Result NPadResource::ApplyNpadSystemCommonPolicy(u64 aruid, bool is_full_policy) {
181 const u64 aruid_index = GetIndexFromAruid(aruid);
182 if (aruid_index >= AruidIndexMax) {
183 return ResultNpadNotConnected;
184 }
185
186 auto& data = state[aruid_index].data;
187 data.SetNpadSystemCommonPolicy(is_full_policy);
188 data.SetNpadJoyHoldType(default_hold_type);
189 if (active_data_aruid == aruid) {
190 active_data.SetNpadSystemCommonPolicy(is_full_policy);
191 active_data.SetNpadJoyHoldType(default_hold_type);
192 }
193 return ResultSuccess;
194}
195
196Result NPadResource::ClearNpadSystemCommonPolicy(u64 aruid) {
197 const u64 aruid_index = GetIndexFromAruid(aruid);
198 if (aruid_index >= AruidIndexMax) {
199 return ResultNpadNotConnected;
200 }
201
202 state[aruid_index].data.ClearNpadSystemCommonPolicy();
203 if (active_data_aruid == aruid) {
204 active_data.ClearNpadSystemCommonPolicy();
205 }
206 return ResultSuccess;
207}
208
209Result NPadResource::SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet style_set) {
210 const u64 aruid_index = GetIndexFromAruid(aruid);
211 if (aruid_index >= AruidIndexMax) {
212 return ResultNpadNotConnected;
213 }
214
215 auto& data = state[aruid_index].data;
216 data.SetSupportedNpadStyleSet(style_set);
217 if (active_data_aruid == aruid) {
218 active_data.SetSupportedNpadStyleSet(style_set);
219 active_data.SetNpadJoyHoldType(data.GetNpadJoyHoldType());
220 }
221 return ResultSuccess;
222}
223
224Result NPadResource::GetSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_Set,
225 u64 aruid) const {
226 const u64 aruid_index = GetIndexFromAruid(aruid);
227 if (aruid_index >= AruidIndexMax) {
228 return ResultNpadNotConnected;
229 }
230
231 auto& data = state[aruid_index].data;
232 if (!data.GetNpadStatus().is_supported_styleset_set) {
233 return ResultUndefinedStyleset;
234 }
235
236 out_style_Set = data.GetSupportedNpadStyleSet();
237 return ResultSuccess;
238}
239
240Result NPadResource::GetMaskedSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_set,
241 u64 aruid) const {
242 if (aruid == SystemAruid) {
243 out_style_set = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
244 Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
245 Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Palma |
246 Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
247 return ResultSuccess;
248 }
249
250 const u64 aruid_index = GetIndexFromAruid(aruid);
251 if (aruid_index >= AruidIndexMax) {
252 return ResultNpadNotConnected;
253 }
254
255 auto& data = state[aruid_index].data;
256 if (!data.GetNpadStatus().is_supported_styleset_set) {
257 return ResultUndefinedStyleset;
258 }
259
260 Core::HID::NpadStyleSet mask{Core::HID::NpadStyleSet::None};
261 out_style_set = data.GetSupportedNpadStyleSet();
262
263 switch (state[aruid_index].npad_revision) {
264 case NpadRevision::Revision1:
265 mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
266 Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
267 Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
268 Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::SystemExt |
269 Core::HID::NpadStyleSet::System;
270 break;
271 case NpadRevision::Revision2:
272 mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
273 Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
274 Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
275 Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
276 Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
277 break;
278 case NpadRevision::Revision3:
279 mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
280 Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
281 Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
282 Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
283 Core::HID::NpadStyleSet::HandheldLark | Core::HID::NpadStyleSet::Lucia |
284 Core::HID::NpadStyleSet::Lagoon | Core::HID::NpadStyleSet::Lager |
285 Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
286 break;
287 default:
288 mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
289 Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
290 Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::SystemExt |
291 Core::HID::NpadStyleSet::System;
292 break;
293 }
294
295 out_style_set = out_style_set & mask;
296 return ResultSuccess;
297}
298
299Result NPadResource::GetAvailableStyleset(Core::HID::NpadStyleSet& out_style_set, u64 aruid) const {
300 const u64 aruid_index = GetIndexFromAruid(aruid);
301 if (aruid_index >= AruidIndexMax) {
302 return ResultNpadNotConnected;
303 }
304
305 auto& data = state[aruid_index].data;
306 if (!data.GetNpadStatus().is_supported_styleset_set) {
307 return ResultUndefinedStyleset;
308 }
309
310 Core::HID::NpadStyleSet mask{Core::HID::NpadStyleSet::None};
311 out_style_set = data.GetSupportedNpadStyleSet();
312
313 switch (state[aruid_index].npad_revision) {
314 case NpadRevision::Revision1:
315 mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
316 Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
317 Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
318 Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::SystemExt |
319 Core::HID::NpadStyleSet::System;
320 break;
321 case NpadRevision::Revision2:
322 mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
323 Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
324 Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
325 Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
326 Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
327 break;
328 case NpadRevision::Revision3:
329 mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
330 Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
331 Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
332 Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
333 Core::HID::NpadStyleSet::HandheldLark | Core::HID::NpadStyleSet::Lucia |
334 Core::HID::NpadStyleSet::Lagoon | Core::HID::NpadStyleSet::Lager |
335 Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
336 break;
337 default:
338 mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
339 Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
340 Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::SystemExt |
341 Core::HID::NpadStyleSet::System;
342 break;
343 }
344
345 out_style_set = out_style_set & mask;
346 return ResultSuccess;
347}
348
349NpadRevision NPadResource::GetNpadRevision(u64 aruid) const {
350 const u64 aruid_index = GetIndexFromAruid(aruid);
351 if (aruid_index >= AruidIndexMax) {
352 return NpadRevision::Revision0;
353 }
354
355 return state[aruid_index].npad_revision;
356}
357
358Result NPadResource::IsSupportedNpadStyleSet(bool& is_set, u64 aruid) {
359 const u64 aruid_index = GetIndexFromAruid(aruid);
360 if (aruid_index >= AruidIndexMax) {
361 return ResultNpadNotConnected;
362 }
363
364 is_set = state[aruid_index].data.GetNpadStatus().is_supported_styleset_set.Value() != 0;
365 return ResultSuccess;
366}
367
368Result NPadResource::SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type) {
369 const u64 aruid_index = GetIndexFromAruid(aruid);
370 if (aruid_index >= AruidIndexMax) {
371 return ResultNpadNotConnected;
372 }
373
374 state[aruid_index].data.SetNpadJoyHoldType(hold_type);
375 if (active_data_aruid == aruid) {
376 active_data.SetNpadJoyHoldType(hold_type);
377 }
378 return ResultSuccess;
379}
380
381Result NPadResource::GetNpadJoyHoldType(NpadJoyHoldType& hold_type, u64 aruid) const {
382 const u64 aruid_index = GetIndexFromAruid(aruid);
383 if (aruid_index >= AruidIndexMax) {
384 return ResultNpadNotConnected;
385 }
386
387 auto& data = state[aruid_index].data;
388 if (data.GetNpadStatus().is_policy || data.GetNpadStatus().is_full_policy) {
389 hold_type = active_data.GetNpadJoyHoldType();
390 return ResultSuccess;
391 }
392 hold_type = data.GetNpadJoyHoldType();
393 return ResultSuccess;
394}
395
396Result NPadResource::SetNpadHandheldActivationMode(u64 aruid,
397 NpadHandheldActivationMode activation_mode) {
398 const u64 aruid_index = GetIndexFromAruid(aruid);
399 if (aruid_index >= AruidIndexMax) {
400 return ResultNpadNotConnected;
401 }
402
403 state[aruid_index].data.SetHandheldActivationMode(activation_mode);
404 if (active_data_aruid == aruid) {
405 active_data.SetHandheldActivationMode(activation_mode);
406 }
407 return ResultSuccess;
408}
409
410Result NPadResource::GetNpadHandheldActivationMode(NpadHandheldActivationMode& activation_mode,
411 u64 aruid) const {
412 const u64 aruid_index = GetIndexFromAruid(aruid);
413 if (aruid_index >= AruidIndexMax) {
414 return ResultNpadNotConnected;
415 }
416
417 activation_mode = state[aruid_index].data.GetHandheldActivationMode();
418 return ResultSuccess;
419}
420
421Result NPadResource::SetSupportedNpadIdType(
422 u64 aruid, std::span<const Core::HID::NpadIdType> supported_npad_list) {
423 const u64 aruid_index = GetIndexFromAruid(aruid);
424 if (aruid_index >= AruidIndexMax) {
425 return ResultNpadNotConnected;
426 }
427 if (supported_npad_list.size() > MaxSupportedNpadIdTypes) {
428 return ResultInvalidArraySize;
429 }
430
431 Result result = state[aruid_index].data.SetSupportedNpadIdType(supported_npad_list);
432 if (result.IsSuccess() && active_data_aruid == aruid) {
433 result = active_data.SetSupportedNpadIdType(supported_npad_list);
434 }
435
436 return result;
437}
438
439bool NPadResource::IsControllerSupported(u64 aruid, Core::HID::NpadStyleIndex style_index) const {
440 const u64 aruid_index = GetIndexFromAruid(aruid);
441 if (aruid_index >= AruidIndexMax) {
442 return false;
443 }
444 return state[aruid_index].data.IsNpadStyleIndexSupported(style_index);
445}
446
447Result NPadResource::SetLrAssignmentMode(u64 aruid, bool is_enabled) {
448 const u64 aruid_index = GetIndexFromAruid(aruid);
449 if (aruid_index >= AruidIndexMax) {
450 return ResultNpadNotConnected;
451 }
452
453 state[aruid_index].data.SetLrAssignmentMode(is_enabled);
454 if (active_data_aruid == aruid) {
455 active_data.SetLrAssignmentMode(is_enabled);
456 }
457 return ResultSuccess;
458}
459
460Result NPadResource::GetLrAssignmentMode(bool& is_enabled, u64 aruid) const {
461 const u64 aruid_index = GetIndexFromAruid(aruid);
462 if (aruid_index >= AruidIndexMax) {
463 return ResultNpadNotConnected;
464 }
465
466 is_enabled = state[aruid_index].data.GetLrAssignmentMode();
467 return ResultSuccess;
468}
469
470Result NPadResource::SetAssigningSingleOnSlSrPress(u64 aruid, bool is_enabled) {
471 const u64 aruid_index = GetIndexFromAruid(aruid);
472 if (aruid_index >= AruidIndexMax) {
473 return ResultNpadNotConnected;
474 }
475
476 state[aruid_index].data.SetAssigningSingleOnSlSrPress(is_enabled);
477 if (active_data_aruid == aruid) {
478 active_data.SetAssigningSingleOnSlSrPress(is_enabled);
479 }
480 return ResultSuccess;
481}
482
483Result NPadResource::IsAssigningSingleOnSlSrPressEnabled(bool& is_enabled, u64 aruid) const {
484 const u64 aruid_index = GetIndexFromAruid(aruid);
485 if (aruid_index >= AruidIndexMax) {
486 return ResultNpadNotConnected;
487 }
488
489 is_enabled = state[aruid_index].data.GetAssigningSingleOnSlSrPress();
490 return ResultSuccess;
491}
492
493Result NPadResource::AcquireNpadStyleSetUpdateEventHandle(u64 aruid,
494 Kernel::KReadableEvent** out_event,
495 Core::HID::NpadIdType npad_id) {
496 const u64 aruid_index = GetIndexFromAruid(aruid);
497 if (aruid_index >= AruidIndexMax) {
498 return ResultNpadNotConnected;
499 }
500
501 auto& controller_state = state[aruid_index].controller_state[NpadIdTypeToIndex(npad_id)];
502 if (!controller_state.is_styleset_update_event_initialized) {
503 // Auto clear = true
504 controller_state.style_set_update_event =
505 service_context.CreateEvent("NpadResource:StylesetUpdateEvent");
506
507 // Assume creating the event succeeds otherwise crash the system here
508 controller_state.is_styleset_update_event_initialized = true;
509 }
510
511 *out_event = &controller_state.style_set_update_event->GetReadableEvent();
512
513 if (controller_state.is_styleset_update_event_initialized) {
514 controller_state.style_set_update_event->Signal();
515 }
516
517 return ResultSuccess;
518}
519
520Result NPadResource::SignalStyleSetUpdateEvent(u64 aruid, Core::HID::NpadIdType npad_id) {
521 const u64 aruid_index = GetIndexFromAruid(aruid);
522 if (aruid_index >= AruidIndexMax) {
523 return ResultNpadNotConnected;
524 }
525 auto controller = state[aruid_index].controller_state[NpadIdTypeToIndex(npad_id)];
526 if (controller.is_styleset_update_event_initialized) {
527 controller.style_set_update_event->Signal();
528 }
529 return ResultSuccess;
530}
531
532Result NPadResource::GetHomeProtectionEnabled(bool& is_enabled, u64 aruid,
533 Core::HID::NpadIdType npad_id) const {
534 const u64 aruid_index = GetIndexFromAruid(aruid);
535 if (aruid_index >= AruidIndexMax) {
536 return ResultNpadNotConnected;
537 }
538
539 is_enabled = state[aruid_index].data.GetHomeProtectionEnabled(npad_id);
540 return ResultSuccess;
541}
542
543Result NPadResource::SetHomeProtectionEnabled(u64 aruid, Core::HID::NpadIdType npad_id,
544 bool is_enabled) {
545 const u64 aruid_index = GetIndexFromAruid(aruid);
546 if (aruid_index >= AruidIndexMax) {
547 return ResultNpadNotConnected;
548 }
549
550 state[aruid_index].data.SetHomeProtectionEnabled(is_enabled, npad_id);
551 if (active_data_aruid == aruid) {
552 active_data.SetHomeProtectionEnabled(is_enabled, npad_id);
553 }
554 return ResultSuccess;
555}
556
557Result NPadResource::SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled) {
558 const u64 aruid_index = GetIndexFromAruid(aruid);
559 if (aruid_index >= AruidIndexMax) {
560 return ResultNpadNotConnected;
561 }
562
563 state[aruid_index].data.SetNpadAnalogStickUseCenterClamp(is_enabled);
564 if (active_data_aruid == aruid) {
565 active_data.SetNpadAnalogStickUseCenterClamp(is_enabled);
566 }
567 return ResultSuccess;
568}
569
570Result NPadResource::SetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id, std::size_t index,
571 Core::HID::NpadButton button_config) {
572 const u64 aruid_index = GetIndexFromAruid(aruid);
573 if (aruid_index >= AruidIndexMax) {
574 return ResultNpadNotConnected;
575 }
576
577 state[aruid_index].button_config[NpadIdTypeToIndex(npad_id)][index] = button_config;
578 return ResultSuccess;
579}
580
581Core::HID::NpadButton NPadResource::GetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id,
582 std::size_t index, Core::HID::NpadButton mask,
583 bool is_enabled) {
584 const u64 aruid_index = GetIndexFromAruid(aruid);
585 if (aruid_index >= AruidIndexMax) {
586 return Core::HID::NpadButton::None;
587 }
588
589 auto& button_config = state[aruid_index].button_config[NpadIdTypeToIndex(npad_id)][index];
590 if (is_enabled) {
591 button_config = button_config | mask;
592 return button_config;
593 }
594
595 button_config = Core::HID::NpadButton::None;
596 return Core::HID::NpadButton::None;
597}
598
599void NPadResource::ResetButtonConfig() {
600 for (auto& selected_state : state) {
601 selected_state.button_config = {};
602 }
603}
604
605Result NPadResource::SetNpadCaptureButtonAssignment(u64 aruid,
606 Core::HID::NpadStyleSet npad_style_set,
607 Core::HID::NpadButton button_assignment) {
608 const u64 aruid_index = GetIndexFromAruid(aruid);
609 if (aruid_index >= AruidIndexMax) {
610 return ResultNpadNotConnected;
611 }
612
613 // Must be a power of two
614 const auto raw_styleset = static_cast<u32>(npad_style_set);
615 if (raw_styleset == 0 && (raw_styleset & (raw_styleset - 1)) != 0) {
616 return ResultMultipleStyleSetSelected;
617 }
618
619 std::size_t style_index{};
620 Core::HID::NpadStyleSet style_selected{};
621 for (style_index = 0; style_index < StyleIndexCount; ++style_index) {
622 style_selected = GetStylesetByIndex(style_index);
623 if (npad_style_set == style_selected) {
624 break;
625 }
626 }
627
628 if (style_selected == Core::HID::NpadStyleSet::None) {
629 return ResultMultipleStyleSetSelected;
630 }
631
632 state[aruid_index].data.SetCaptureButtonAssignment(button_assignment, style_index);
633 if (active_data_aruid == aruid) {
634 active_data.SetCaptureButtonAssignment(button_assignment, style_index);
635 }
636 return ResultSuccess;
637}
638
639Result NPadResource::ClearNpadCaptureButtonAssignment(u64 aruid) {
640 const u64 aruid_index = GetIndexFromAruid(aruid);
641 if (aruid_index >= AruidIndexMax) {
642 return ResultNpadNotConnected;
643 }
644
645 for (std::size_t i = 0; i < StyleIndexCount; i++) {
646 state[aruid_index].data.SetCaptureButtonAssignment(Core::HID::NpadButton::None, i);
647 if (active_data_aruid == aruid) {
648 active_data.SetCaptureButtonAssignment(Core::HID::NpadButton::None, i);
649 }
650 }
651 return ResultSuccess;
652}
653
654std::size_t NPadResource::GetNpadCaptureButtonAssignment(std::span<Core::HID::NpadButton> out_list,
655 u64 aruid) const {
656 const u64 aruid_index = GetIndexFromAruid(aruid);
657 if (aruid_index >= AruidIndexMax) {
658 return 0;
659 }
660 return state[aruid_index].data.GetNpadCaptureButtonAssignmentList(out_list);
661}
662
663void NPadResource::SetNpadRevision(u64 aruid, NpadRevision revision) {
664 const u64 aruid_index = GetIndexFromAruid(aruid);
665 if (aruid_index >= AruidIndexMax) {
666 return;
667 }
668
669 state[aruid_index].npad_revision = revision;
670}
671
672Result NPadResource::SetNpadSystemExtStateEnabled(u64 aruid, bool is_enabled) {
673 const u64 aruid_index = GetIndexFromAruid(aruid);
674 if (aruid_index >= AruidIndexMax) {
675 return ResultNpadNotConnected;
676 }
677
678 state[aruid_index].data.SetNpadAnalogStickUseCenterClamp(is_enabled);
679 if (active_data_aruid == aruid) {
680 active_data.SetNpadAnalogStickUseCenterClamp(is_enabled);
681 }
682 return ResultSuccess;
683}
684
685} // namespace Service::HID
diff --git a/src/core/hle/service/hid/controllers/npad/npad_resource.h b/src/core/hle/service/hid/controllers/npad/npad_resource.h
new file mode 100644
index 000000000..4c7e6ab0e
--- /dev/null
+++ b/src/core/hle/service/hid/controllers/npad/npad_resource.h
@@ -0,0 +1,132 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#pragma once
5
6#include <array>
7#include <mutex>
8#include <span>
9
10#include "common/common_types.h"
11#include "core/hid/hid_types.h"
12#include "core/hle/result.h"
13#include "core/hle/service/hid/controllers/applet_resource.h"
14#include "core/hle/service/hid/controllers/npad/npad_data.h"
15#include "core/hle/service/hid/controllers/types/npad_types.h"
16#include "core/hle/service/kernel_helpers.h"
17
18namespace Core {
19class System;
20}
21
22namespace Kernel {
23class KReadableEvent;
24}
25
26namespace Service::HID {
27struct DataStatusFlag;
28
29struct NpadControllerState {
30 bool is_styleset_update_event_initialized{};
31 INSERT_PADDING_BYTES(0x7);
32 Kernel::KEvent* style_set_update_event{nullptr};
33 INSERT_PADDING_BYTES(0x27);
34};
35
36struct NpadState {
37 DataStatusFlag flag{};
38 u64 aruid{};
39 NPadData data{};
40 std::array<std::array<Core::HID::NpadButton, StyleIndexCount>, MaxSupportedNpadIdTypes>
41 button_config;
42 std::array<NpadControllerState, MaxSupportedNpadIdTypes> controller_state;
43 NpadRevision npad_revision;
44};
45
46/// Handles Npad request from HID interfaces
47class NPadResource final {
48public:
49 explicit NPadResource(KernelHelpers::ServiceContext& context);
50 ~NPadResource();
51
52 NPadData* GetActiveData();
53 u64 GetActiveDataAruid();
54
55 Result RegisterAppletResourceUserId(u64 aruid);
56 void UnregisterAppletResourceUserId(u64 aruid);
57
58 void DestroyStyleSetUpdateEvents(u64 aruid);
59
60 Result Activate(u64 aruid);
61 Result Activate();
62 Result Deactivate();
63
64 void SetAppletResourceUserId(u64 aruid);
65 std::size_t GetIndexFromAruid(u64 aruid) const;
66
67 Result ApplyNpadSystemCommonPolicy(u64 aruid, bool is_full_policy);
68 Result ClearNpadSystemCommonPolicy(u64 aruid);
69
70 Result SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet style_set);
71 Result GetSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_Set, u64 aruid) const;
72 Result GetMaskedSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_set, u64 aruid) const;
73 Result GetAvailableStyleset(Core::HID::NpadStyleSet& out_style_set, u64 aruid) const;
74
75 NpadRevision GetNpadRevision(u64 aruid) const;
76 void SetNpadRevision(u64 aruid, NpadRevision revision);
77
78 Result IsSupportedNpadStyleSet(bool& is_set, u64 aruid);
79
80 Result SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type);
81 Result GetNpadJoyHoldType(NpadJoyHoldType& hold_type, u64 aruid) const;
82
83 Result SetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode activation_mode);
84 Result GetNpadHandheldActivationMode(NpadHandheldActivationMode& activation_mode,
85 u64 aruid) const;
86
87 Result SetSupportedNpadIdType(u64 aruid,
88 std::span<const Core::HID::NpadIdType> supported_npad_list);
89 bool IsControllerSupported(u64 aruid, Core::HID::NpadStyleIndex style_index) const;
90
91 Result SetLrAssignmentMode(u64 aruid, bool is_enabled);
92 Result GetLrAssignmentMode(bool& is_enabled, u64 aruid) const;
93
94 Result SetAssigningSingleOnSlSrPress(u64 aruid, bool is_enabled);
95 Result IsAssigningSingleOnSlSrPressEnabled(bool& is_enabled, u64 aruid) const;
96
97 Result AcquireNpadStyleSetUpdateEventHandle(u64 aruid, Kernel::KReadableEvent** out_event,
98 Core::HID::NpadIdType npad_id);
99 Result SignalStyleSetUpdateEvent(u64 aruid, Core::HID::NpadIdType npad_id);
100
101 Result GetHomeProtectionEnabled(bool& is_enabled, u64 aruid,
102 Core::HID::NpadIdType npad_id) const;
103 Result SetHomeProtectionEnabled(u64 aruid, Core::HID::NpadIdType npad_id, bool is_enabled);
104
105 Result SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled);
106
107 Result SetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id, std::size_t index,
108 Core::HID::NpadButton button_config);
109 Core::HID::NpadButton GetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id,
110 std::size_t index, Core::HID::NpadButton mask,
111 bool is_enabled);
112 void ResetButtonConfig();
113
114 Result SetNpadCaptureButtonAssignment(u64 aruid, Core::HID::NpadStyleSet npad_style_set,
115 Core::HID::NpadButton button_assignment);
116 Result ClearNpadCaptureButtonAssignment(u64 aruid);
117 std::size_t GetNpadCaptureButtonAssignment(std::span<Core::HID::NpadButton> out_list,
118 u64 aruid) const;
119
120 Result SetNpadSystemExtStateEnabled(u64 aruid, bool is_enabled);
121
122private:
123 NPadData active_data{};
124 AruidRegisterList registration_list{};
125 std::array<NpadState, AruidIndexMax> state{};
126 u64 active_data_aruid{};
127 NpadJoyHoldType default_hold_type{};
128 s32 ref_counter{};
129
130 KernelHelpers::ServiceContext& service_context;
131};
132} // namespace Service::HID
diff --git a/src/core/hle/service/hid/controllers/types/npad_types.h b/src/core/hle/service/hid/controllers/types/npad_types.h
index a5ce2562b..419c33a8c 100644
--- a/src/core/hle/service/hid/controllers/types/npad_types.h
+++ b/src/core/hle/service/hid/controllers/types/npad_types.h
@@ -9,7 +9,8 @@
9#include "core/hid/hid_types.h" 9#include "core/hid/hid_types.h"
10 10
11namespace Service::HID { 11namespace Service::HID {
12static constexpr std::size_t NpadCount = 10; 12static constexpr std::size_t MaxSupportedNpadIdTypes = 10;
13static constexpr std::size_t StyleIndexCount = 7;
13 14
14// This is nn::hid::NpadJoyHoldType 15// This is nn::hid::NpadJoyHoldType
15enum class NpadJoyHoldType : u64 { 16enum class NpadJoyHoldType : u64 {
diff --git a/src/core/hle/service/hid/controllers/types/shared_memory_format.h b/src/core/hle/service/hid/controllers/types/shared_memory_format.h
index 2986c113e..976043b9c 100644
--- a/src/core/hle/service/hid/controllers/types/shared_memory_format.h
+++ b/src/core/hle/service/hid/controllers/types/shared_memory_format.h
@@ -171,7 +171,7 @@ static_assert(sizeof(NpadSharedMemoryEntry) == 0x5000, "NpadSharedMemoryEntry is
171 171
172// This is nn::hid::detail::NpadSharedMemoryFormat 172// This is nn::hid::detail::NpadSharedMemoryFormat
173struct NpadSharedMemoryFormat { 173struct NpadSharedMemoryFormat {
174 std::array<NpadSharedMemoryEntry, NpadCount> npad_entry; 174 std::array<NpadSharedMemoryEntry, MaxSupportedNpadIdTypes> npad_entry;
175}; 175};
176static_assert(sizeof(NpadSharedMemoryFormat) == 0x32000, 176static_assert(sizeof(NpadSharedMemoryFormat) == 0x32000,
177 "NpadSharedMemoryFormat is an invalid size"); 177 "NpadSharedMemoryFormat is an invalid size");
diff --git a/src/core/hle/service/hid/errors.h b/src/core/hle/service/hid/errors.h
index 6dc976fe1..bb14aa61e 100644
--- a/src/core/hle/service/hid/errors.h
+++ b/src/core/hle/service/hid/errors.h
@@ -10,15 +10,32 @@ namespace Service::HID {
10constexpr Result PalmaResultSuccess{ErrorModule::HID, 0}; 10constexpr Result PalmaResultSuccess{ErrorModule::HID, 0};
11constexpr Result NpadInvalidHandle{ErrorModule::HID, 100}; 11constexpr Result NpadInvalidHandle{ErrorModule::HID, 100};
12constexpr Result NpadDeviceIndexOutOfRange{ErrorModule::HID, 107}; 12constexpr Result NpadDeviceIndexOutOfRange{ErrorModule::HID, 107};
13constexpr Result VibrationInvalidStyleIndex{ErrorModule::HID, 122}; 13
14constexpr Result VibrationInvalidNpadId{ErrorModule::HID, 123}; 14constexpr Result ResultVibrationNotInitialized{ErrorModule::HID, 121};
15constexpr Result VibrationDeviceIndexOutOfRange{ErrorModule::HID, 124}; 15constexpr Result ResultVibrationInvalidStyleIndex{ErrorModule::HID, 122};
16constexpr Result ResultVibrationInvalidNpadId{ErrorModule::HID, 123};
17constexpr Result ResultVibrationDeviceIndexOutOfRange{ErrorModule::HID, 124};
18constexpr Result ResultVibrationStrenghtOutOfRange{ErrorModule::HID, 126};
19constexpr Result ResultVibrationArraySizeMismatch{ErrorModule::HID, 131};
20
16constexpr Result InvalidSixAxisFusionRange{ErrorModule::HID, 423}; 21constexpr Result InvalidSixAxisFusionRange{ErrorModule::HID, 423};
22
23constexpr Result ResultNfcIsNotReady{ErrorModule::HID, 461};
24constexpr Result ResultNfcXcdHandleIsNotInitialized{ErrorModule::HID, 464};
25constexpr Result ResultIrSensorIsNotReady{ErrorModule::HID, 501};
26constexpr Result ResultMcuIsNotReady{ErrorModule::HID, 541};
27
17constexpr Result NpadIsDualJoycon{ErrorModule::HID, 601}; 28constexpr Result NpadIsDualJoycon{ErrorModule::HID, 601};
18constexpr Result NpadIsSameType{ErrorModule::HID, 602}; 29constexpr Result NpadIsSameType{ErrorModule::HID, 602};
19constexpr Result InvalidNpadId{ErrorModule::HID, 709}; 30constexpr Result ResultNpadIsNotProController{ErrorModule::HID, 604};
20constexpr Result NpadNotConnected{ErrorModule::HID, 710}; 31
21constexpr Result InvalidArraySize{ErrorModule::HID, 715}; 32constexpr Result ResultInvalidNpadId{ErrorModule::HID, 709};
33constexpr Result ResultNpadNotConnected{ErrorModule::HID, 710};
34constexpr Result ResultNpadHandlerOverflow{ErrorModule::HID, 711};
35constexpr Result ResultNpadHandlerNotInitialized{ErrorModule::HID, 712};
36constexpr Result ResultInvalidArraySize{ErrorModule::HID, 715};
37constexpr Result ResultUndefinedStyleset{ErrorModule::HID, 716};
38constexpr Result ResultMultipleStyleSetSelected{ErrorModule::HID, 717};
22 39
23constexpr Result ResultAppletResourceOverflow{ErrorModule::HID, 1041}; 40constexpr Result ResultAppletResourceOverflow{ErrorModule::HID, 1041};
24constexpr Result ResultAppletResourceNotInitialized{ErrorModule::HID, 1042}; 41constexpr Result ResultAppletResourceNotInitialized{ErrorModule::HID, 1042};
@@ -27,6 +44,9 @@ constexpr Result ResultAruidNoAvailableEntries{ErrorModule::HID, 1044};
27constexpr Result ResultAruidAlreadyRegistered{ErrorModule::HID, 1046}; 44constexpr Result ResultAruidAlreadyRegistered{ErrorModule::HID, 1046};
28constexpr Result ResultAruidNotRegistered{ErrorModule::HID, 1047}; 45constexpr Result ResultAruidNotRegistered{ErrorModule::HID, 1047};
29 46
47constexpr Result ResultNpadResourceOverflow{ErrorModule::HID, 2001};
48constexpr Result ResultNpadResourceNotInitialized{ErrorModule::HID, 2002};
49
30constexpr Result InvalidPalmaHandle{ErrorModule::HID, 3302}; 50constexpr Result InvalidPalmaHandle{ErrorModule::HID, 3302};
31 51
32} // namespace Service::HID 52} // namespace Service::HID
diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp
index afbcb019f..bd2873181 100644
--- a/src/core/hle/service/hid/hid.cpp
+++ b/src/core/hle/service/hid/hid.cpp
@@ -25,6 +25,7 @@ void LoopProcess(Core::System& system) {
25 // TODO: Remove this hack until this service is emulated properly. 25 // TODO: Remove this hack until this service is emulated properly.
26 const auto process_list = system.Kernel().GetProcessList(); 26 const auto process_list = system.Kernel().GetProcessList();
27 if (!process_list.empty()) { 27 if (!process_list.empty()) {
28 resouce_manager->Initialize();
28 resouce_manager->RegisterAppletResourceUserId(process_list[0]->GetId(), true); 29 resouce_manager->RegisterAppletResourceUserId(process_list[0]->GetId(), true);
29 } 30 }
30 31
diff --git a/src/core/hle/service/hid/hid_server.cpp b/src/core/hle/service/hid/hid_server.cpp
index 3174672af..a953c92b3 100644
--- a/src/core/hle/service/hid/hid_server.cpp
+++ b/src/core/hle/service/hid/hid_server.cpp
@@ -785,8 +785,8 @@ void IHidServer::IsFirmwareUpdateAvailableForSixAxisSensor(HLERequestContext& ct
785 785
786 bool is_firmware_available{}; 786 bool is_firmware_available{};
787 auto controller = GetResourceManager()->GetNpad(); 787 auto controller = GetResourceManager()->GetNpad();
788 controller->IsFirmwareUpdateAvailableForSixAxisSensor(parameters.sixaxis_handle, 788 controller->IsFirmwareUpdateAvailableForSixAxisSensor(
789 is_firmware_available); 789 parameters.applet_resource_user_id, parameters.sixaxis_handle, is_firmware_available);
790 790
791 LOG_WARNING( 791 LOG_WARNING(
792 Service_HID, 792 Service_HID,
@@ -924,8 +924,8 @@ void IHidServer::ResetIsSixAxisSensorDeviceNewlyAssigned(HLERequestContext& ctx)
924 const auto parameters{rp.PopRaw<Parameters>()}; 924 const auto parameters{rp.PopRaw<Parameters>()};
925 925
926 auto controller = GetResourceManager()->GetNpad(); 926 auto controller = GetResourceManager()->GetNpad();
927 const auto result = 927 const auto result = controller->ResetIsSixAxisSensorDeviceNewlyAssigned(
928 controller->ResetIsSixAxisSensorDeviceNewlyAssigned(parameters.sixaxis_handle); 928 parameters.applet_resource_user_id, parameters.sixaxis_handle);
929 929
930 LOG_WARNING( 930 LOG_WARNING(
931 Service_HID, 931 Service_HID,
@@ -970,7 +970,7 @@ void IHidServer::ActivateGesture(HLERequestContext& ctx) {
970void IHidServer::SetSupportedNpadStyleSet(HLERequestContext& ctx) { 970void IHidServer::SetSupportedNpadStyleSet(HLERequestContext& ctx) {
971 IPC::RequestParser rp{ctx}; 971 IPC::RequestParser rp{ctx};
972 struct Parameters { 972 struct Parameters {
973 Core::HID::NpadStyleSet supported_styleset; 973 Core::HID::NpadStyleSet supported_style_set;
974 INSERT_PADDING_WORDS_NOINIT(1); 974 INSERT_PADDING_WORDS_NOINIT(1);
975 u64 applet_resource_user_id; 975 u64 applet_resource_user_id;
976 }; 976 };
@@ -978,13 +978,25 @@ void IHidServer::SetSupportedNpadStyleSet(HLERequestContext& ctx) {
978 978
979 const auto parameters{rp.PopRaw<Parameters>()}; 979 const auto parameters{rp.PopRaw<Parameters>()};
980 980
981 GetResourceManager()->GetNpad()->SetSupportedStyleSet({parameters.supported_styleset}); 981 LOG_DEBUG(Service_HID, "called, supported_style_set={}, applet_resource_user_id={}",
982 parameters.supported_style_set, parameters.applet_resource_user_id);
983
984 const auto npad = GetResourceManager()->GetNpad();
985 const Result result = npad->SetSupportedNpadStyleSet(parameters.applet_resource_user_id,
986 parameters.supported_style_set);
982 987
983 LOG_DEBUG(Service_HID, "called, supported_styleset={}, applet_resource_user_id={}", 988 if (result.IsSuccess()) {
984 parameters.supported_styleset, parameters.applet_resource_user_id); 989 Core::HID::NpadStyleTag style_tag{parameters.supported_style_set};
990 const auto revision = npad->GetRevision(parameters.applet_resource_user_id);
991
992 if (style_tag.palma != 0 && revision < NpadRevision::Revision3) {
993 // GetResourceManager()->GetPalma()->EnableBoostMode(parameters.applet_resource_user_id,
994 // true);
995 }
996 }
985 997
986 IPC::ResponseBuilder rb{ctx, 2}; 998 IPC::ResponseBuilder rb{ctx, 2};
987 rb.Push(ResultSuccess); 999 rb.Push(result);
988} 1000}
989 1001
990void IHidServer::GetSupportedNpadStyleSet(HLERequestContext& ctx) { 1002void IHidServer::GetSupportedNpadStyleSet(HLERequestContext& ctx) {
@@ -993,19 +1005,31 @@ void IHidServer::GetSupportedNpadStyleSet(HLERequestContext& ctx) {
993 1005
994 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id); 1006 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
995 1007
1008 Core::HID::NpadStyleSet supported_style_set{};
1009 const auto npad = GetResourceManager()->GetNpad();
1010 const auto result =
1011 npad->GetSupportedNpadStyleSet(applet_resource_user_id, supported_style_set);
1012
996 IPC::ResponseBuilder rb{ctx, 3}; 1013 IPC::ResponseBuilder rb{ctx, 3};
997 rb.Push(ResultSuccess); 1014 rb.Push(result);
998 rb.PushEnum(GetResourceManager()->GetNpad()->GetSupportedStyleSet().raw); 1015 rb.PushEnum(supported_style_set);
999} 1016}
1000 1017
1001void IHidServer::SetSupportedNpadIdType(HLERequestContext& ctx) { 1018void IHidServer::SetSupportedNpadIdType(HLERequestContext& ctx) {
1002 IPC::RequestParser rp{ctx}; 1019 IPC::RequestParser rp{ctx};
1003 const auto applet_resource_user_id{rp.Pop<u64>()}; 1020 const auto applet_resource_user_id{rp.Pop<u64>()};
1004 1021 const auto buffer = ctx.ReadBuffer();
1005 const auto result = GetResourceManager()->GetNpad()->SetSupportedNpadIdTypes(ctx.ReadBuffer()); 1022 const std::size_t elements = ctx.GetReadBufferNumElements<Core::HID::NpadIdType>();
1006 1023
1007 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id); 1024 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
1008 1025
1026 std::vector<Core::HID::NpadIdType> supported_npad_list(elements);
1027 memcpy(supported_npad_list.data(), buffer.data(), buffer.size());
1028
1029 const auto npad = GetResourceManager()->GetNpad();
1030 const Result result =
1031 npad->SetSupportedNpadIdType(applet_resource_user_id, supported_npad_list);
1032
1009 IPC::ResponseBuilder rb{ctx, 2}; 1033 IPC::ResponseBuilder rb{ctx, 2};
1010 rb.Push(result); 1034 rb.Push(result);
1011} 1035}
@@ -1018,7 +1042,7 @@ void IHidServer::ActivateNpad(HLERequestContext& ctx) {
1018 1042
1019 auto npad = GetResourceManager()->GetNpad(); 1043 auto npad = GetResourceManager()->GetNpad();
1020 1044
1021 // TODO: npad->SetRevision(applet_resource_user_id, NpadRevision::Revision0); 1045 npad->SetRevision(applet_resource_user_id, NpadRevision::Revision0);
1022 const Result result = npad->Activate(applet_resource_user_id); 1046 const Result result = npad->Activate(applet_resource_user_id);
1023 1047
1024 IPC::ResponseBuilder rb{ctx, 2}; 1048 IPC::ResponseBuilder rb{ctx, 2};
@@ -1052,13 +1076,13 @@ void IHidServer::AcquireNpadStyleSetUpdateEventHandle(HLERequestContext& ctx) {
1052 LOG_DEBUG(Service_HID, "called, npad_id={}, applet_resource_user_id={}, unknown={}", 1076 LOG_DEBUG(Service_HID, "called, npad_id={}, applet_resource_user_id={}, unknown={}",
1053 parameters.npad_id, parameters.applet_resource_user_id, parameters.unknown); 1077 parameters.npad_id, parameters.applet_resource_user_id, parameters.unknown);
1054 1078
1055 // Games expect this event to be signaled after calling this function 1079 Kernel::KReadableEvent* style_set_update_event;
1056 GetResourceManager()->GetNpad()->SignalStyleSetChangedEvent(parameters.npad_id); 1080 const auto result = GetResourceManager()->GetNpad()->AcquireNpadStyleSetUpdateEventHandle(
1081 parameters.applet_resource_user_id, &style_set_update_event, parameters.npad_id);
1057 1082
1058 IPC::ResponseBuilder rb{ctx, 2, 1}; 1083 IPC::ResponseBuilder rb{ctx, 2, 1};
1059 rb.Push(ResultSuccess); 1084 rb.Push(result);
1060 rb.PushCopyObjects( 1085 rb.PushCopyObjects(style_set_update_event);
1061 GetResourceManager()->GetNpad()->GetStyleSetChangedEvent(parameters.npad_id));
1062} 1086}
1063 1087
1064void IHidServer::DisconnectNpad(HLERequestContext& ctx) { 1088void IHidServer::DisconnectNpad(HLERequestContext& ctx) {
@@ -1073,7 +1097,7 @@ void IHidServer::DisconnectNpad(HLERequestContext& ctx) {
1073 const auto parameters{rp.PopRaw<Parameters>()}; 1097 const auto parameters{rp.PopRaw<Parameters>()};
1074 1098
1075 auto controller = GetResourceManager()->GetNpad(); 1099 auto controller = GetResourceManager()->GetNpad();
1076 controller->DisconnectNpad(parameters.npad_id); 1100 controller->DisconnectNpad(parameters.applet_resource_user_id, parameters.npad_id);
1077 1101
1078 LOG_DEBUG(Service_HID, "called, npad_id={}, applet_resource_user_id={}", parameters.npad_id, 1102 LOG_DEBUG(Service_HID, "called, npad_id={}, applet_resource_user_id={}", parameters.npad_id,
1079 parameters.applet_resource_user_id); 1103 parameters.applet_resource_user_id);
@@ -1113,7 +1137,7 @@ void IHidServer::ActivateNpadWithRevision(HLERequestContext& ctx) {
1113 1137
1114 auto npad = GetResourceManager()->GetNpad(); 1138 auto npad = GetResourceManager()->GetNpad();
1115 1139
1116 // TODO: npad->SetRevision(applet_resource_user_id, revision); 1140 npad->SetRevision(parameters.applet_resource_user_id, parameters.revision);
1117 const auto result = npad->Activate(parameters.applet_resource_user_id); 1141 const auto result = npad->Activate(parameters.applet_resource_user_id);
1118 1142
1119 IPC::ResponseBuilder rb{ctx, 2}; 1143 IPC::ResponseBuilder rb{ctx, 2};
@@ -1125,13 +1149,19 @@ void IHidServer::SetNpadJoyHoldType(HLERequestContext& ctx) {
1125 const auto applet_resource_user_id{rp.Pop<u64>()}; 1149 const auto applet_resource_user_id{rp.Pop<u64>()};
1126 const auto hold_type{rp.PopEnum<NpadJoyHoldType>()}; 1150 const auto hold_type{rp.PopEnum<NpadJoyHoldType>()};
1127 1151
1128 GetResourceManager()->GetNpad()->SetHoldType(hold_type);
1129
1130 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}, hold_type={}", 1152 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}, hold_type={}",
1131 applet_resource_user_id, hold_type); 1153 applet_resource_user_id, hold_type);
1132 1154
1155 if (hold_type != NpadJoyHoldType::Horizontal && hold_type != NpadJoyHoldType::Vertical) {
1156 // This should crash console
1157 ASSERT_MSG(false, "Invalid npad joy hold type");
1158 }
1159
1160 const auto npad = GetResourceManager()->GetNpad();
1161 const auto result = npad->SetNpadJoyHoldType(applet_resource_user_id, hold_type);
1162
1133 IPC::ResponseBuilder rb{ctx, 2}; 1163 IPC::ResponseBuilder rb{ctx, 2};
1134 rb.Push(ResultSuccess); 1164 rb.Push(result);
1135} 1165}
1136 1166
1137void IHidServer::GetNpadJoyHoldType(HLERequestContext& ctx) { 1167void IHidServer::GetNpadJoyHoldType(HLERequestContext& ctx) {
@@ -1140,9 +1170,13 @@ void IHidServer::GetNpadJoyHoldType(HLERequestContext& ctx) {
1140 1170
1141 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id); 1171 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
1142 1172
1173 NpadJoyHoldType hold_type{};
1174 const auto npad = GetResourceManager()->GetNpad();
1175 const auto result = npad->GetNpadJoyHoldType(applet_resource_user_id, hold_type);
1176
1143 IPC::ResponseBuilder rb{ctx, 4}; 1177 IPC::ResponseBuilder rb{ctx, 4};
1144 rb.Push(ResultSuccess); 1178 rb.Push(result);
1145 rb.PushEnum(GetResourceManager()->GetNpad()->GetHoldType()); 1179 rb.PushEnum(hold_type);
1146} 1180}
1147 1181
1148void IHidServer::SetNpadJoyAssignmentModeSingleByDefault(HLERequestContext& ctx) { 1182void IHidServer::SetNpadJoyAssignmentModeSingleByDefault(HLERequestContext& ctx) {
@@ -1158,8 +1192,8 @@ void IHidServer::SetNpadJoyAssignmentModeSingleByDefault(HLERequestContext& ctx)
1158 1192
1159 Core::HID::NpadIdType new_npad_id{}; 1193 Core::HID::NpadIdType new_npad_id{};
1160 auto controller = GetResourceManager()->GetNpad(); 1194 auto controller = GetResourceManager()->GetNpad();
1161 controller->SetNpadMode(new_npad_id, parameters.npad_id, NpadJoyDeviceType::Left, 1195 controller->SetNpadMode(parameters.applet_resource_user_id, new_npad_id, parameters.npad_id,
1162 NpadJoyAssignmentMode::Single); 1196 NpadJoyDeviceType::Left, NpadJoyAssignmentMode::Single);
1163 1197
1164 LOG_INFO(Service_HID, "called, npad_id={}, applet_resource_user_id={}", parameters.npad_id, 1198 LOG_INFO(Service_HID, "called, npad_id={}, applet_resource_user_id={}", parameters.npad_id,
1165 parameters.applet_resource_user_id); 1199 parameters.applet_resource_user_id);
@@ -1182,8 +1216,8 @@ void IHidServer::SetNpadJoyAssignmentModeSingle(HLERequestContext& ctx) {
1182 1216
1183 Core::HID::NpadIdType new_npad_id{}; 1217 Core::HID::NpadIdType new_npad_id{};
1184 auto controller = GetResourceManager()->GetNpad(); 1218 auto controller = GetResourceManager()->GetNpad();
1185 controller->SetNpadMode(new_npad_id, parameters.npad_id, parameters.npad_joy_device_type, 1219 controller->SetNpadMode(parameters.applet_resource_user_id, new_npad_id, parameters.npad_id,
1186 NpadJoyAssignmentMode::Single); 1220 parameters.npad_joy_device_type, NpadJoyAssignmentMode::Single);
1187 1221
1188 LOG_INFO(Service_HID, "called, npad_id={}, applet_resource_user_id={}, npad_joy_device_type={}", 1222 LOG_INFO(Service_HID, "called, npad_id={}, applet_resource_user_id={}, npad_joy_device_type={}",
1189 parameters.npad_id, parameters.applet_resource_user_id, 1223 parameters.npad_id, parameters.applet_resource_user_id,
@@ -1206,7 +1240,8 @@ void IHidServer::SetNpadJoyAssignmentModeDual(HLERequestContext& ctx) {
1206 1240
1207 Core::HID::NpadIdType new_npad_id{}; 1241 Core::HID::NpadIdType new_npad_id{};
1208 auto controller = GetResourceManager()->GetNpad(); 1242 auto controller = GetResourceManager()->GetNpad();
1209 controller->SetNpadMode(new_npad_id, parameters.npad_id, {}, NpadJoyAssignmentMode::Dual); 1243 controller->SetNpadMode(parameters.applet_resource_user_id, new_npad_id, parameters.npad_id, {},
1244 NpadJoyAssignmentMode::Dual);
1210 1245
1211 LOG_DEBUG(Service_HID, "called, npad_id={}, applet_resource_user_id={}", parameters.npad_id, 1246 LOG_DEBUG(Service_HID, "called, npad_id={}, applet_resource_user_id={}", parameters.npad_id,
1212 parameters.applet_resource_user_id); // Spams a lot when controller applet is open 1247 parameters.applet_resource_user_id); // Spams a lot when controller applet is open
@@ -1222,7 +1257,8 @@ void IHidServer::MergeSingleJoyAsDualJoy(HLERequestContext& ctx) {
1222 const auto applet_resource_user_id{rp.Pop<u64>()}; 1257 const auto applet_resource_user_id{rp.Pop<u64>()};
1223 1258
1224 auto controller = GetResourceManager()->GetNpad(); 1259 auto controller = GetResourceManager()->GetNpad();
1225 const auto result = controller->MergeSingleJoyAsDualJoy(npad_id_1, npad_id_2); 1260 const auto result =
1261 controller->MergeSingleJoyAsDualJoy(applet_resource_user_id, npad_id_1, npad_id_2);
1226 1262
1227 LOG_DEBUG(Service_HID, "called, npad_id_1={}, npad_id_2={}, applet_resource_user_id={}", 1263 LOG_DEBUG(Service_HID, "called, npad_id_1={}, npad_id_2={}, applet_resource_user_id={}",
1228 npad_id_1, npad_id_2, applet_resource_user_id); 1264 npad_id_1, npad_id_2, applet_resource_user_id);
@@ -1235,10 +1271,10 @@ void IHidServer::StartLrAssignmentMode(HLERequestContext& ctx) {
1235 IPC::RequestParser rp{ctx}; 1271 IPC::RequestParser rp{ctx};
1236 const auto applet_resource_user_id{rp.Pop<u64>()}; 1272 const auto applet_resource_user_id{rp.Pop<u64>()};
1237 1273
1238 GetResourceManager()->GetNpad()->StartLRAssignmentMode();
1239
1240 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id); 1274 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
1241 1275
1276 GetResourceManager()->GetNpad()->StartLrAssignmentMode(applet_resource_user_id);
1277
1242 IPC::ResponseBuilder rb{ctx, 2}; 1278 IPC::ResponseBuilder rb{ctx, 2};
1243 rb.Push(ResultSuccess); 1279 rb.Push(ResultSuccess);
1244} 1280}
@@ -1247,10 +1283,10 @@ void IHidServer::StopLrAssignmentMode(HLERequestContext& ctx) {
1247 IPC::RequestParser rp{ctx}; 1283 IPC::RequestParser rp{ctx};
1248 const auto applet_resource_user_id{rp.Pop<u64>()}; 1284 const auto applet_resource_user_id{rp.Pop<u64>()};
1249 1285
1250 GetResourceManager()->GetNpad()->StopLRAssignmentMode();
1251
1252 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id); 1286 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
1253 1287
1288 GetResourceManager()->GetNpad()->StopLrAssignmentMode(applet_resource_user_id);
1289
1254 IPC::ResponseBuilder rb{ctx, 2}; 1290 IPC::ResponseBuilder rb{ctx, 2};
1255 rb.Push(ResultSuccess); 1291 rb.Push(ResultSuccess);
1256} 1292}
@@ -1260,13 +1296,23 @@ void IHidServer::SetNpadHandheldActivationMode(HLERequestContext& ctx) {
1260 const auto applet_resource_user_id{rp.Pop<u64>()}; 1296 const auto applet_resource_user_id{rp.Pop<u64>()};
1261 const auto activation_mode{rp.PopEnum<NpadHandheldActivationMode>()}; 1297 const auto activation_mode{rp.PopEnum<NpadHandheldActivationMode>()};
1262 1298
1263 GetResourceManager()->GetNpad()->SetNpadHandheldActivationMode(activation_mode);
1264
1265 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}, activation_mode={}", 1299 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}, activation_mode={}",
1266 applet_resource_user_id, activation_mode); 1300 applet_resource_user_id, activation_mode);
1267 1301
1302 if (activation_mode >= NpadHandheldActivationMode::MaxActivationMode) {
1303 // Console should crash here
1304 ASSERT_MSG(false, "Activation mode should be always None, Single or Dual");
1305 IPC::ResponseBuilder rb{ctx, 2};
1306 rb.Push(ResultSuccess);
1307 return;
1308 }
1309
1310 const auto npad = GetResourceManager()->GetNpad();
1311 const auto result =
1312 npad->SetNpadHandheldActivationMode(applet_resource_user_id, activation_mode);
1313
1268 IPC::ResponseBuilder rb{ctx, 2}; 1314 IPC::ResponseBuilder rb{ctx, 2};
1269 rb.Push(ResultSuccess); 1315 rb.Push(result);
1270} 1316}
1271 1317
1272void IHidServer::GetNpadHandheldActivationMode(HLERequestContext& ctx) { 1318void IHidServer::GetNpadHandheldActivationMode(HLERequestContext& ctx) {
@@ -1275,9 +1321,14 @@ void IHidServer::GetNpadHandheldActivationMode(HLERequestContext& ctx) {
1275 1321
1276 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id); 1322 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
1277 1323
1324 NpadHandheldActivationMode activation_mode{};
1325 const auto npad = GetResourceManager()->GetNpad();
1326 const auto result =
1327 npad->GetNpadHandheldActivationMode(applet_resource_user_id, activation_mode);
1328
1278 IPC::ResponseBuilder rb{ctx, 4}; 1329 IPC::ResponseBuilder rb{ctx, 4};
1279 rb.Push(ResultSuccess); 1330 rb.Push(result);
1280 rb.PushEnum(GetResourceManager()->GetNpad()->GetNpadHandheldActivationMode()); 1331 rb.PushEnum(activation_mode);
1281} 1332}
1282 1333
1283void IHidServer::SwapNpadAssignment(HLERequestContext& ctx) { 1334void IHidServer::SwapNpadAssignment(HLERequestContext& ctx) {
@@ -1286,12 +1337,12 @@ void IHidServer::SwapNpadAssignment(HLERequestContext& ctx) {
1286 const auto npad_id_2{rp.PopEnum<Core::HID::NpadIdType>()}; 1337 const auto npad_id_2{rp.PopEnum<Core::HID::NpadIdType>()};
1287 const auto applet_resource_user_id{rp.Pop<u64>()}; 1338 const auto applet_resource_user_id{rp.Pop<u64>()};
1288 1339
1289 auto controller = GetResourceManager()->GetNpad();
1290 const auto result = controller->SwapNpadAssignment(npad_id_1, npad_id_2);
1291
1292 LOG_DEBUG(Service_HID, "called, npad_id_1={}, npad_id_2={}, applet_resource_user_id={}", 1340 LOG_DEBUG(Service_HID, "called, npad_id_1={}, npad_id_2={}, applet_resource_user_id={}",
1293 npad_id_1, npad_id_2, applet_resource_user_id); 1341 npad_id_1, npad_id_2, applet_resource_user_id);
1294 1342
1343 const auto npad = GetResourceManager()->GetNpad();
1344 const auto result = npad->SwapNpadAssignment(applet_resource_user_id, npad_id_1, npad_id_2);
1345
1295 IPC::ResponseBuilder rb{ctx, 2}; 1346 IPC::ResponseBuilder rb{ctx, 2};
1296 rb.Push(result); 1347 rb.Push(result);
1297} 1348}
@@ -1307,13 +1358,19 @@ void IHidServer::IsUnintendedHomeButtonInputProtectionEnabled(HLERequestContext&
1307 1358
1308 const auto parameters{rp.PopRaw<Parameters>()}; 1359 const auto parameters{rp.PopRaw<Parameters>()};
1309 1360
1310 bool is_enabled = false; 1361 LOG_INFO(Service_HID, "called, npad_id={}, applet_resource_user_id={}", parameters.npad_id,
1311 auto controller = GetResourceManager()->GetNpad(); 1362 parameters.applet_resource_user_id);
1312 const auto result =
1313 controller->IsUnintendedHomeButtonInputProtectionEnabled(parameters.npad_id, is_enabled);
1314 1363
1315 LOG_WARNING(Service_HID, "(STUBBED) called, npad_id={}, applet_resource_user_id={}", 1364 if (!IsNpadIdValid(parameters.npad_id)) {
1316 parameters.npad_id, parameters.applet_resource_user_id); 1365 IPC::ResponseBuilder rb{ctx, 3};
1366 rb.Push(ResultInvalidNpadId);
1367 return;
1368 }
1369
1370 bool is_enabled{};
1371 const auto npad = GetResourceManager()->GetNpad();
1372 const auto result = npad->IsUnintendedHomeButtonInputProtectionEnabled(
1373 is_enabled, parameters.applet_resource_user_id, parameters.npad_id);
1317 1374
1318 IPC::ResponseBuilder rb{ctx, 3}; 1375 IPC::ResponseBuilder rb{ctx, 3};
1319 rb.Push(result); 1376 rb.Push(result);
@@ -1332,13 +1389,18 @@ void IHidServer::EnableUnintendedHomeButtonInputProtection(HLERequestContext& ct
1332 1389
1333 const auto parameters{rp.PopRaw<Parameters>()}; 1390 const auto parameters{rp.PopRaw<Parameters>()};
1334 1391
1335 auto controller = GetResourceManager()->GetNpad(); 1392 LOG_INFO(Service_HID, "called, is_enabled={}, npad_id={}, applet_resource_user_id={}",
1336 const auto result = controller->SetUnintendedHomeButtonInputProtectionEnabled( 1393 parameters.is_enabled, parameters.npad_id, parameters.applet_resource_user_id);
1337 parameters.is_enabled, parameters.npad_id);
1338 1394
1339 LOG_DEBUG(Service_HID, 1395 if (!IsNpadIdValid(parameters.npad_id)) {
1340 "(STUBBED) called, is_enabled={}, npad_id={}, applet_resource_user_id={}", 1396 IPC::ResponseBuilder rb{ctx, 3};
1341 parameters.is_enabled, parameters.npad_id, parameters.applet_resource_user_id); 1397 rb.Push(ResultInvalidNpadId);
1398 return;
1399 }
1400
1401 const auto npad = GetResourceManager()->GetNpad();
1402 const auto result = npad->EnableUnintendedHomeButtonInputProtection(
1403 parameters.applet_resource_user_id, parameters.npad_id, parameters.is_enabled);
1342 1404
1343 IPC::ResponseBuilder rb{ctx, 2}; 1405 IPC::ResponseBuilder rb{ctx, 2};
1344 rb.Push(result); 1406 rb.Push(result);
@@ -1359,8 +1421,8 @@ void IHidServer::SetNpadJoyAssignmentModeSingleWithDestination(HLERequestContext
1359 Core::HID::NpadIdType new_npad_id{}; 1421 Core::HID::NpadIdType new_npad_id{};
1360 auto controller = GetResourceManager()->GetNpad(); 1422 auto controller = GetResourceManager()->GetNpad();
1361 const auto is_reassigned = 1423 const auto is_reassigned =
1362 controller->SetNpadMode(new_npad_id, parameters.npad_id, parameters.npad_joy_device_type, 1424 controller->SetNpadMode(parameters.applet_resource_user_id, new_npad_id, parameters.npad_id,
1363 NpadJoyAssignmentMode::Single); 1425 parameters.npad_joy_device_type, NpadJoyAssignmentMode::Single);
1364 1426
1365 LOG_INFO(Service_HID, "called, npad_id={}, applet_resource_user_id={}, npad_joy_device_type={}", 1427 LOG_INFO(Service_HID, "called, npad_id={}, applet_resource_user_id={}, npad_joy_device_type={}",
1366 parameters.npad_id, parameters.applet_resource_user_id, 1428 parameters.npad_id, parameters.applet_resource_user_id,
@@ -1375,7 +1437,7 @@ void IHidServer::SetNpadJoyAssignmentModeSingleWithDestination(HLERequestContext
1375void IHidServer::SetNpadAnalogStickUseCenterClamp(HLERequestContext& ctx) { 1437void IHidServer::SetNpadAnalogStickUseCenterClamp(HLERequestContext& ctx) {
1376 IPC::RequestParser rp{ctx}; 1438 IPC::RequestParser rp{ctx};
1377 struct Parameters { 1439 struct Parameters {
1378 bool analog_stick_use_center_clamp; 1440 bool use_center_clamp;
1379 INSERT_PADDING_BYTES_NOINIT(7); 1441 INSERT_PADDING_BYTES_NOINIT(7);
1380 u64 applet_resource_user_id; 1442 u64 applet_resource_user_id;
1381 }; 1443 };
@@ -1383,12 +1445,11 @@ void IHidServer::SetNpadAnalogStickUseCenterClamp(HLERequestContext& ctx) {
1383 1445
1384 const auto parameters{rp.PopRaw<Parameters>()}; 1446 const auto parameters{rp.PopRaw<Parameters>()};
1385 1447
1386 GetResourceManager()->GetNpad()->SetAnalogStickUseCenterClamp( 1448 LOG_WARNING(Service_HID, "(STUBBED) called, use_center_clamp={}, applet_resource_user_id={}",
1387 parameters.analog_stick_use_center_clamp); 1449 parameters.use_center_clamp, parameters.applet_resource_user_id);
1388 1450
1389 LOG_WARNING(Service_HID, 1451 GetResourceManager()->GetNpad()->SetNpadAnalogStickUseCenterClamp(
1390 "(STUBBED) called, analog_stick_use_center_clamp={}, applet_resource_user_id={}", 1452 parameters.applet_resource_user_id, parameters.use_center_clamp);
1391 parameters.analog_stick_use_center_clamp, parameters.applet_resource_user_id);
1392 1453
1393 IPC::ResponseBuilder rb{ctx, 2}; 1454 IPC::ResponseBuilder rb{ctx, 2};
1394 rb.Push(ResultSuccess); 1455 rb.Push(ResultSuccess);
@@ -1496,7 +1557,8 @@ void IHidServer::SendVibrationValue(HLERequestContext& ctx) {
1496 1557
1497 const auto parameters{rp.PopRaw<Parameters>()}; 1558 const auto parameters{rp.PopRaw<Parameters>()};
1498 1559
1499 GetResourceManager()->GetNpad()->VibrateController(parameters.vibration_device_handle, 1560 GetResourceManager()->GetNpad()->VibrateController(parameters.applet_resource_user_id,
1561 parameters.vibration_device_handle,
1500 parameters.vibration_value); 1562 parameters.vibration_value);
1501 1563
1502 LOG_DEBUG(Service_HID, 1564 LOG_DEBUG(Service_HID,
@@ -1528,8 +1590,8 @@ void IHidServer::GetActualVibrationValue(HLERequestContext& ctx) {
1528 1590
1529 IPC::ResponseBuilder rb{ctx, 6}; 1591 IPC::ResponseBuilder rb{ctx, 6};
1530 rb.Push(ResultSuccess); 1592 rb.Push(ResultSuccess);
1531 rb.PushRaw( 1593 rb.PushRaw(GetResourceManager()->GetNpad()->GetLastVibration(
1532 GetResourceManager()->GetNpad()->GetLastVibration(parameters.vibration_device_handle)); 1594 parameters.applet_resource_user_id, parameters.vibration_device_handle));
1533} 1595}
1534 1596
1535void IHidServer::CreateActiveVibrationDeviceList(HLERequestContext& ctx) { 1597void IHidServer::CreateActiveVibrationDeviceList(HLERequestContext& ctx) {
@@ -1580,7 +1642,8 @@ void IHidServer::SendVibrationValues(HLERequestContext& ctx) {
1580 auto vibration_values = std::span( 1642 auto vibration_values = std::span(
1581 reinterpret_cast<const Core::HID::VibrationValue*>(vibration_data.data()), vibration_count); 1643 reinterpret_cast<const Core::HID::VibrationValue*>(vibration_data.data()), vibration_count);
1582 1644
1583 GetResourceManager()->GetNpad()->VibrateControllers(vibration_device_handles, vibration_values); 1645 GetResourceManager()->GetNpad()->VibrateControllers(applet_resource_user_id,
1646 vibration_device_handles, vibration_values);
1584 1647
1585 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id); 1648 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
1586 1649
@@ -1634,8 +1697,8 @@ void IHidServer::SendVibrationGcErmCommand(HLERequestContext& ctx) {
1634 } 1697 }
1635 }(); 1698 }();
1636 1699
1637 GetResourceManager()->GetNpad()->VibrateController(parameters.vibration_device_handle, 1700 GetResourceManager()->GetNpad()->VibrateController(
1638 vibration_value); 1701 parameters.applet_resource_user_id, parameters.vibration_device_handle, vibration_value);
1639 1702
1640 LOG_DEBUG(Service_HID, 1703 LOG_DEBUG(Service_HID,
1641 "called, npad_type={}, npad_id={}, device_index={}, applet_resource_user_id={}, " 1704 "called, npad_type={}, npad_id={}, device_index={}, applet_resource_user_id={}, "
@@ -1659,8 +1722,8 @@ void IHidServer::GetActualVibrationGcErmCommand(HLERequestContext& ctx) {
1659 1722
1660 const auto parameters{rp.PopRaw<Parameters>()}; 1723 const auto parameters{rp.PopRaw<Parameters>()};
1661 1724
1662 const auto last_vibration = 1725 const auto last_vibration = GetResourceManager()->GetNpad()->GetLastVibration(
1663 GetResourceManager()->GetNpad()->GetLastVibration(parameters.vibration_device_handle); 1726 parameters.applet_resource_user_id, parameters.vibration_device_handle);
1664 1727
1665 const auto gc_erm_command = [last_vibration] { 1728 const auto gc_erm_command = [last_vibration] {
1666 if (last_vibration.low_amplitude != 0.0f || last_vibration.high_amplitude != 0.0f) { 1729 if (last_vibration.low_amplitude != 0.0f || last_vibration.high_amplitude != 0.0f) {
@@ -1732,7 +1795,7 @@ void IHidServer::IsVibrationDeviceMounted(HLERequestContext& ctx) {
1732 IPC::ResponseBuilder rb{ctx, 3}; 1795 IPC::ResponseBuilder rb{ctx, 3};
1733 rb.Push(ResultSuccess); 1796 rb.Push(ResultSuccess);
1734 rb.Push(GetResourceManager()->GetNpad()->IsVibrationDeviceMounted( 1797 rb.Push(GetResourceManager()->GetNpad()->IsVibrationDeviceMounted(
1735 parameters.vibration_device_handle)); 1798 parameters.applet_resource_user_id, parameters.vibration_device_handle));
1736} 1799}
1737 1800
1738void IHidServer::ActivateConsoleSixAxisSensor(HLERequestContext& ctx) { 1801void IHidServer::ActivateConsoleSixAxisSensor(HLERequestContext& ctx) {
@@ -2315,10 +2378,10 @@ void IHidServer::SetNpadCommunicationMode(HLERequestContext& ctx) {
2315 const auto applet_resource_user_id{rp.Pop<u64>()}; 2378 const auto applet_resource_user_id{rp.Pop<u64>()};
2316 const auto communication_mode{rp.PopEnum<NpadCommunicationMode>()}; 2379 const auto communication_mode{rp.PopEnum<NpadCommunicationMode>()};
2317 2380
2318 GetResourceManager()->GetNpad()->SetNpadCommunicationMode(communication_mode); 2381 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}, communication_mode={}",
2382 applet_resource_user_id, communication_mode);
2319 2383
2320 LOG_WARNING(Service_HID, "(STUBBED) called, applet_resource_user_id={}, communication_mode={}", 2384 // This function has been stubbed since 2.0.0+
2321 applet_resource_user_id, communication_mode);
2322 2385
2323 IPC::ResponseBuilder rb{ctx, 2}; 2386 IPC::ResponseBuilder rb{ctx, 2};
2324 rb.Push(ResultSuccess); 2387 rb.Push(ResultSuccess);
@@ -2326,12 +2389,15 @@ void IHidServer::SetNpadCommunicationMode(HLERequestContext& ctx) {
2326 2389
2327void IHidServer::GetNpadCommunicationMode(HLERequestContext& ctx) { 2390void IHidServer::GetNpadCommunicationMode(HLERequestContext& ctx) {
2328 IPC::RequestParser rp{ctx}; 2391 IPC::RequestParser rp{ctx};
2392 const auto applet_resource_user_id{rp.Pop<u64>()};
2329 2393
2330 LOG_WARNING(Service_HID, "(STUBBED) called"); 2394 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
2395
2396 // This function has been stubbed since 2.0.0+
2331 2397
2332 IPC::ResponseBuilder rb{ctx, 4}; 2398 IPC::ResponseBuilder rb{ctx, 4};
2333 rb.Push(ResultSuccess); 2399 rb.Push(ResultSuccess);
2334 rb.PushEnum(GetResourceManager()->GetNpad()->GetNpadCommunicationMode()); 2400 rb.PushEnum(NpadCommunicationMode::Default);
2335} 2401}
2336 2402
2337void IHidServer::SetTouchScreenConfiguration(HLERequestContext& ctx) { 2403void IHidServer::SetTouchScreenConfiguration(HLERequestContext& ctx) {
diff --git a/src/core/hle/service/hid/hid_system_server.cpp b/src/core/hle/service/hid/hid_system_server.cpp
index 5cc88c4a1..4823de743 100644
--- a/src/core/hle/service/hid/hid_system_server.cpp
+++ b/src/core/hle/service/hid/hid_system_server.cpp
@@ -240,9 +240,12 @@ IHidSystemServer::~IHidSystemServer() {
240}; 240};
241 241
242void IHidSystemServer::ApplyNpadSystemCommonPolicy(HLERequestContext& ctx) { 242void IHidSystemServer::ApplyNpadSystemCommonPolicy(HLERequestContext& ctx) {
243 LOG_WARNING(Service_HID, "called"); 243 IPC::RequestParser rp{ctx};
244 const auto applet_resource_user_id{rp.Pop<u64>()};
245
246 LOG_INFO(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
244 247
245 GetResourceManager()->GetNpad()->ApplyNpadSystemCommonPolicy(); 248 GetResourceManager()->GetNpad()->ApplyNpadSystemCommonPolicy(applet_resource_user_id);
246 249
247 IPC::ResponseBuilder rb{ctx, 2}; 250 IPC::ResponseBuilder rb{ctx, 2};
248 rb.Push(ResultSuccess); 251 rb.Push(ResultSuccess);
@@ -271,9 +274,12 @@ void IHidSystemServer::GetLastActiveNpad(HLERequestContext& ctx) {
271} 274}
272 275
273void IHidSystemServer::ApplyNpadSystemCommonPolicyFull(HLERequestContext& ctx) { 276void IHidSystemServer::ApplyNpadSystemCommonPolicyFull(HLERequestContext& ctx) {
274 LOG_WARNING(Service_HID, "called"); 277 IPC::RequestParser rp{ctx};
278 const auto applet_resource_user_id{rp.Pop<u64>()};
275 279
276 GetResourceManager()->GetNpad()->ApplyNpadSystemCommonPolicy(); 280 LOG_INFO(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
281
282 GetResourceManager()->GetNpad()->ApplyNpadSystemCommonPolicyFull(applet_resource_user_id);
277 283
278 IPC::ResponseBuilder rb{ctx, 2}; 284 IPC::ResponseBuilder rb{ctx, 2};
279 rb.Push(ResultSuccess); 285 rb.Push(ResultSuccess);
@@ -298,28 +304,32 @@ void IHidSystemServer::GetNpadFullKeyGripColor(HLERequestContext& ctx) {
298 304
299void IHidSystemServer::GetMaskedSupportedNpadStyleSet(HLERequestContext& ctx) { 305void IHidSystemServer::GetMaskedSupportedNpadStyleSet(HLERequestContext& ctx) {
300 IPC::RequestParser rp{ctx}; 306 IPC::RequestParser rp{ctx};
307 const auto applet_resource_user_id{rp.Pop<u64>()};
301 308
302 LOG_INFO(Service_HID, "(STUBBED) called"); 309 LOG_INFO(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
303 310
304 Core::HID::NpadStyleSet supported_styleset = 311 Core::HID::NpadStyleSet supported_styleset{};
305 GetResourceManager()->GetNpad()->GetSupportedStyleSet().raw; 312 const auto& npad = GetResourceManager()->GetNpad();
313 const Result result =
314 npad->GetMaskedSupportedNpadStyleSet(applet_resource_user_id, supported_styleset);
306 315
307 IPC::ResponseBuilder rb{ctx, 3}; 316 IPC::ResponseBuilder rb{ctx, 3};
308 rb.Push(ResultSuccess); 317 rb.Push(result);
309 rb.PushEnum(supported_styleset); 318 rb.PushEnum(supported_styleset);
310} 319}
311 320
312void IHidSystemServer::SetSupportedNpadStyleSetAll(HLERequestContext& ctx) { 321void IHidSystemServer::SetSupportedNpadStyleSetAll(HLERequestContext& ctx) {
313 IPC::RequestParser rp{ctx}; 322 IPC::RequestParser rp{ctx};
323 const auto applet_resource_user_id{rp.Pop<u64>()};
314 324
315 LOG_INFO(Service_HID, "(STUBBED) called"); 325 LOG_DEBUG(Service_HID, "called, applet_resource_user_id={}", applet_resource_user_id);
316 326
317 Core::HID::NpadStyleSet supported_styleset = 327 const auto& npad = GetResourceManager()->GetNpad();
318 GetResourceManager()->GetNpad()->GetSupportedStyleSet().raw; 328 const auto result =
329 npad->SetSupportedNpadStyleSet(applet_resource_user_id, Core::HID::NpadStyleSet::All);
319 330
320 IPC::ResponseBuilder rb{ctx, 3}; 331 IPC::ResponseBuilder rb{ctx, 2};
321 rb.Push(ResultSuccess); 332 rb.Push(result);
322 rb.PushEnum(supported_styleset);
323} 333}
324 334
325void IHidSystemServer::GetAppletDetailedUiType(HLERequestContext& ctx) { 335void IHidSystemServer::GetAppletDetailedUiType(HLERequestContext& ctx) {
diff --git a/src/core/hle/service/hid/hid_util.h b/src/core/hle/service/hid/hid_util.h
index b87cc10e3..6a2ed287a 100644
--- a/src/core/hle/service/hid/hid_util.h
+++ b/src/core/hle/service/hid/hid_util.h
@@ -31,7 +31,7 @@ constexpr Result IsSixaxisHandleValid(const Core::HID::SixAxisSensorHandle& hand
31 const bool device_index = handle.device_index < Core::HID::DeviceIndex::MaxDeviceIndex; 31 const bool device_index = handle.device_index < Core::HID::DeviceIndex::MaxDeviceIndex;
32 32
33 if (!npad_id) { 33 if (!npad_id) {
34 return InvalidNpadId; 34 return ResultInvalidNpadId;
35 } 35 }
36 if (!device_index) { 36 if (!device_index) {
37 return NpadDeviceIndexOutOfRange; 37 return NpadDeviceIndexOutOfRange;
@@ -54,15 +54,15 @@ constexpr Result IsVibrationHandleValid(const Core::HID::VibrationDeviceHandle&
54 // These support vibration 54 // These support vibration
55 break; 55 break;
56 default: 56 default:
57 return VibrationInvalidStyleIndex; 57 return ResultVibrationInvalidStyleIndex;
58 } 58 }
59 59
60 if (!IsNpadIdValid(static_cast<Core::HID::NpadIdType>(handle.npad_id))) { 60 if (!IsNpadIdValid(static_cast<Core::HID::NpadIdType>(handle.npad_id))) {
61 return VibrationInvalidNpadId; 61 return ResultVibrationInvalidNpadId;
62 } 62 }
63 63
64 if (handle.device_index >= Core::HID::DeviceIndex::MaxDeviceIndex) { 64 if (handle.device_index >= Core::HID::DeviceIndex::MaxDeviceIndex) {
65 return VibrationDeviceIndexOutOfRange; 65 return ResultVibrationDeviceIndexOutOfRange;
66 } 66 }
67 67
68 return ResultSuccess; 68 return ResultSuccess;
diff --git a/src/core/hle/service/hid/irs.cpp b/src/core/hle/service/hid/irs.cpp
index 008debfd1..05ed31273 100644
--- a/src/core/hle/service/hid/irs.cpp
+++ b/src/core/hle/service/hid/irs.cpp
@@ -315,7 +315,7 @@ void IRS::GetNpadIrCameraHandle(HLERequestContext& ctx) {
315 if (npad_id > Core::HID::NpadIdType::Player8 && npad_id != Core::HID::NpadIdType::Invalid && 315 if (npad_id > Core::HID::NpadIdType::Player8 && npad_id != Core::HID::NpadIdType::Invalid &&
316 npad_id != Core::HID::NpadIdType::Handheld) { 316 npad_id != Core::HID::NpadIdType::Handheld) {
317 IPC::ResponseBuilder rb{ctx, 2}; 317 IPC::ResponseBuilder rb{ctx, 2};
318 rb.Push(Service::HID::InvalidNpadId); 318 rb.Push(Service::HID::ResultInvalidNpadId);
319 return; 319 return;
320 } 320 }
321 321
diff --git a/src/core/hle/service/hid/resource_manager.cpp b/src/core/hle/service/hid/resource_manager.cpp
index 84b4be3ed..ab49259ac 100644
--- a/src/core/hle/service/hid/resource_manager.cpp
+++ b/src/core/hle/service/hid/resource_manager.cpp
@@ -129,12 +129,12 @@ std::shared_ptr<UniquePad> ResourceManager::GetUniquePad() const {
129} 129}
130 130
131Result ResourceManager::CreateAppletResource(u64 aruid) { 131Result ResourceManager::CreateAppletResource(u64 aruid) {
132 if (aruid == 0) { 132 if (aruid == SystemAruid) {
133 const auto result = RegisterCoreAppletResource(); 133 const auto result = RegisterCoreAppletResource();
134 if (result.IsError()) { 134 if (result.IsError()) {
135 return result; 135 return result;
136 } 136 }
137 return GetNpad()->Activate(); 137 return GetNpad()->ActivateNpadResource();
138 } 138 }
139 139
140 const auto result = CreateAppletResourceImpl(aruid); 140 const auto result = CreateAppletResourceImpl(aruid);
@@ -147,7 +147,7 @@ Result ResourceManager::CreateAppletResource(u64 aruid) {
147 six_axis->Activate(); 147 six_axis->Activate();
148 touch_screen->Activate(); 148 touch_screen->Activate();
149 149
150 return GetNpad()->Activate(aruid); 150 return GetNpad()->ActivateNpadResource(aruid);
151} 151}
152 152
153Result ResourceManager::CreateAppletResourceImpl(u64 aruid) { 153Result ResourceManager::CreateAppletResourceImpl(u64 aruid) {
@@ -174,7 +174,7 @@ void ResourceManager::InitializeHidCommonSampler() {
174 debug_pad->SetAppletResource(applet_resource); 174 debug_pad->SetAppletResource(applet_resource);
175 digitizer->SetAppletResource(applet_resource); 175 digitizer->SetAppletResource(applet_resource);
176 keyboard->SetAppletResource(applet_resource); 176 keyboard->SetAppletResource(applet_resource);
177 npad->SetAppletResource(applet_resource); 177 npad->SetNpadExternals(applet_resource, &shared_mutex);
178 six_axis->SetAppletResource(applet_resource); 178 six_axis->SetAppletResource(applet_resource);
179 mouse->SetAppletResource(applet_resource); 179 mouse->SetAppletResource(applet_resource);
180 debug_mouse->SetAppletResource(applet_resource); 180 debug_mouse->SetAppletResource(applet_resource);
@@ -214,7 +214,11 @@ Result ResourceManager::UnregisterCoreAppletResource() {
214 214
215Result ResourceManager::RegisterAppletResourceUserId(u64 aruid, bool bool_value) { 215Result ResourceManager::RegisterAppletResourceUserId(u64 aruid, bool bool_value) {
216 std::scoped_lock lock{shared_mutex}; 216 std::scoped_lock lock{shared_mutex};
217 return applet_resource->RegisterAppletResourceUserId(aruid, bool_value); 217 auto result = applet_resource->RegisterAppletResourceUserId(aruid, bool_value);
218 if (result.IsSuccess()) {
219 result = npad->RegisterAppletResourceUserId(aruid);
220 }
221 return result;
218} 222}
219 223
220void ResourceManager::UnregisterAppletResourceUserId(u64 aruid) { 224void ResourceManager::UnregisterAppletResourceUserId(u64 aruid) {
diff --git a/src/core/hle/service/hid/resource_manager.h b/src/core/hle/service/hid/resource_manager.h
index 70d9b6550..7a21d8eb8 100644
--- a/src/core/hle/service/hid/resource_manager.h
+++ b/src/core/hle/service/hid/resource_manager.h
@@ -93,7 +93,7 @@ private:
93 93
94 bool is_initialized{false}; 94 bool is_initialized{false};
95 95
96 mutable std::mutex shared_mutex; 96 mutable std::recursive_mutex shared_mutex;
97 std::shared_ptr<AppletResource> applet_resource = nullptr; 97 std::shared_ptr<AppletResource> applet_resource = nullptr;
98 98
99 std::shared_ptr<CaptureButton> capture_button = nullptr; 99 std::shared_ptr<CaptureButton> capture_button = nullptr;