summaryrefslogtreecommitdiff
path: root/src/hid_core/resources/npad
diff options
context:
space:
mode:
authorGravatar Narr the Reg2024-01-04 20:37:43 -0600
committerGravatar Narr the Reg2024-01-05 11:41:15 -0600
commitee847f8ff0b1b0aec39c1b78c010bc0c08a0a613 (patch)
tree3b95cbb74be05f0ce7a007353f1f9f95e1ed3901 /src/hid_core/resources/npad
parentMerge pull request #12437 from ameerj/gl-amd-fixes (diff)
downloadyuzu-ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613.tar.gz
yuzu-ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613.tar.xz
yuzu-ee847f8ff0b1b0aec39c1b78c010bc0c08a0a613.zip
hid_core: Move hid to it's own subproject
Diffstat (limited to 'src/hid_core/resources/npad')
-rw-r--r--src/hid_core/resources/npad/npad.cpp1342
-rw-r--r--src/hid_core/resources/npad/npad.h214
-rw-r--r--src/hid_core/resources/npad/npad_data.cpp228
-rw-r--r--src/hid_core/resources/npad/npad_data.h88
-rw-r--r--src/hid_core/resources/npad/npad_resource.cpp685
-rw-r--r--src/hid_core/resources/npad/npad_resource.h132
-rw-r--r--src/hid_core/resources/npad/npad_types.h255
7 files changed, 2944 insertions, 0 deletions
diff --git a/src/hid_core/resources/npad/npad.cpp b/src/hid_core/resources/npad/npad.cpp
new file mode 100644
index 000000000..e6c035628
--- /dev/null
+++ b/src/hid_core/resources/npad/npad.cpp
@@ -0,0 +1,1342 @@
1// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#include <algorithm>
5#include <array>
6#include <chrono>
7#include <cstring>
8
9#include "common/assert.h"
10#include "common/bit_field.h"
11#include "common/common_types.h"
12#include "common/logging/log.h"
13#include "common/settings.h"
14#include "core/core_timing.h"
15#include "core/hle/kernel/k_event.h"
16#include "core/hle/kernel/k_readable_event.h"
17#include "core/hle/service/kernel_helpers.h"
18#include "hid_core/frontend/emulated_controller.h"
19#include "hid_core/hid_core.h"
20#include "hid_core/hid_result.h"
21#include "hid_core/hid_util.h"
22#include "hid_core/resources/applet_resource.h"
23#include "hid_core/resources/npad/npad.h"
24#include "hid_core/resources/shared_memory_format.h"
25
26namespace Service::HID {
27
28NPad::NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_)
29 : hid_core{hid_core_}, service_context{service_context_}, npad_resource{service_context} {
30 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; ++aruid_index) {
31 for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) {
32 auto& controller = controller_data[aruid_index][i];
33 controller.device = hid_core.GetEmulatedControllerByIndex(i);
34 controller.vibration[Core::HID::EmulatedDeviceIndex::LeftIndex].latest_vibration_value =
35 Core::HID::DEFAULT_VIBRATION_VALUE;
36 controller.vibration[Core::HID::EmulatedDeviceIndex::RightIndex]
37 .latest_vibration_value = Core::HID::DEFAULT_VIBRATION_VALUE;
38 Core::HID::ControllerUpdateCallback engine_callback{
39 .on_change =
40 [this, i](Core::HID::ControllerTriggerType type) { ControllerUpdate(type, i); },
41 .is_npad_service = true,
42 };
43 controller.callback_key = controller.device->SetCallback(engine_callback);
44 }
45 }
46}
47
48NPad::~NPad() {
49 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; ++aruid_index) {
50 for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) {
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 }
104 }
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);
115}
116
117void NPad::ControllerUpdate(Core::HID::ControllerTriggerType type, std::size_t controller_idx) {
118 if (type == Core::HID::ControllerTriggerType::All) {
119 ControllerUpdate(Core::HID::ControllerTriggerType::Connected, controller_idx);
120 ControllerUpdate(Core::HID::ControllerTriggerType::Battery, controller_idx);
121 return;
122 }
123
124 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
125 if (controller_idx >= controller_data[aruid_index].size()) {
126 return;
127 }
128
129 auto* data = applet_resource_holder.applet_resource->GetAruidDataByIndex(aruid_index);
130
131 if (!data->flag.is_assigned) {
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;
160 }
161 }
162}
163
164void NPad::InitNewlyAddedController(u64 aruid, Core::HID::NpadIdType npad_id) {
165 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
166 if (!npad_resource.IsControllerSupported(aruid, controller.device->GetNpadStyleIndex())) {
167 return;
168 }
169 LOG_DEBUG(Service_HID, "Npad connected {}", npad_id);
170 const auto controller_type = controller.device->GetNpadStyleIndex();
171 const auto& body_colors = controller.device->GetColors();
172 const auto& battery_level = controller.device->GetBattery();
173 auto* shared_memory = controller.shared_memory;
174 if (controller_type == Core::HID::NpadStyleIndex::None) {
175 npad_resource.SignalStyleSetUpdateEvent(aruid, npad_id);
176 return;
177 }
178
179 // Reset memory values
180 shared_memory->style_tag.raw = Core::HID::NpadStyleSet::None;
181 shared_memory->device_type.raw = 0;
182 shared_memory->system_properties.raw = 0;
183 shared_memory->joycon_color.attribute = ColorAttribute::NoController;
184 shared_memory->joycon_color.attribute = ColorAttribute::NoController;
185 shared_memory->fullkey_color = {};
186 shared_memory->joycon_color.left = {};
187 shared_memory->joycon_color.right = {};
188 shared_memory->battery_level_dual = {};
189 shared_memory->battery_level_left = {};
190 shared_memory->battery_level_right = {};
191
192 switch (controller_type) {
193 case Core::HID::NpadStyleIndex::None:
194 ASSERT(false);
195 break;
196 case Core::HID::NpadStyleIndex::ProController:
197 shared_memory->fullkey_color.attribute = ColorAttribute::Ok;
198 shared_memory->fullkey_color.fullkey = body_colors.fullkey;
199 shared_memory->battery_level_dual = battery_level.dual.battery_level;
200 shared_memory->style_tag.fullkey.Assign(1);
201 shared_memory->device_type.fullkey.Assign(1);
202 shared_memory->system_properties.is_vertical.Assign(1);
203 shared_memory->system_properties.use_plus.Assign(1);
204 shared_memory->system_properties.use_minus.Assign(1);
205 shared_memory->system_properties.is_charging_joy_dual.Assign(
206 battery_level.dual.is_charging);
207 shared_memory->applet_footer_type = AppletFooterUiType::SwitchProController;
208 shared_memory->sixaxis_fullkey_properties.is_newly_assigned.Assign(1);
209 break;
210 case Core::HID::NpadStyleIndex::Handheld:
211 shared_memory->fullkey_color.attribute = ColorAttribute::Ok;
212 shared_memory->joycon_color.attribute = ColorAttribute::Ok;
213 shared_memory->fullkey_color.fullkey = body_colors.fullkey;
214 shared_memory->joycon_color.left = body_colors.left;
215 shared_memory->joycon_color.right = body_colors.right;
216 shared_memory->style_tag.handheld.Assign(1);
217 shared_memory->device_type.handheld_left.Assign(1);
218 shared_memory->device_type.handheld_right.Assign(1);
219 shared_memory->system_properties.is_vertical.Assign(1);
220 shared_memory->system_properties.use_plus.Assign(1);
221 shared_memory->system_properties.use_minus.Assign(1);
222 shared_memory->system_properties.use_directional_buttons.Assign(1);
223 shared_memory->system_properties.is_charging_joy_dual.Assign(
224 battery_level.left.is_charging);
225 shared_memory->system_properties.is_charging_joy_left.Assign(
226 battery_level.left.is_charging);
227 shared_memory->system_properties.is_charging_joy_right.Assign(
228 battery_level.right.is_charging);
229 shared_memory->assignment_mode = NpadJoyAssignmentMode::Dual;
230 shared_memory->applet_footer_type = AppletFooterUiType::HandheldJoyConLeftJoyConRight;
231 shared_memory->sixaxis_handheld_properties.is_newly_assigned.Assign(1);
232 break;
233 case Core::HID::NpadStyleIndex::JoyconDual:
234 shared_memory->fullkey_color.attribute = ColorAttribute::Ok;
235 shared_memory->joycon_color.attribute = ColorAttribute::Ok;
236 shared_memory->style_tag.joycon_dual.Assign(1);
237 if (controller.is_dual_left_connected) {
238 shared_memory->joycon_color.left = body_colors.left;
239 shared_memory->battery_level_left = battery_level.left.battery_level;
240 shared_memory->device_type.joycon_left.Assign(1);
241 shared_memory->system_properties.use_minus.Assign(1);
242 shared_memory->system_properties.is_charging_joy_left.Assign(
243 battery_level.left.is_charging);
244 shared_memory->sixaxis_dual_left_properties.is_newly_assigned.Assign(1);
245 }
246 if (controller.is_dual_right_connected) {
247 shared_memory->joycon_color.right = body_colors.right;
248 shared_memory->battery_level_right = battery_level.right.battery_level;
249 shared_memory->device_type.joycon_right.Assign(1);
250 shared_memory->system_properties.use_plus.Assign(1);
251 shared_memory->system_properties.is_charging_joy_right.Assign(
252 battery_level.right.is_charging);
253 shared_memory->sixaxis_dual_right_properties.is_newly_assigned.Assign(1);
254 }
255 shared_memory->system_properties.use_directional_buttons.Assign(1);
256 shared_memory->system_properties.is_vertical.Assign(1);
257 shared_memory->assignment_mode = NpadJoyAssignmentMode::Dual;
258
259 if (controller.is_dual_left_connected && controller.is_dual_right_connected) {
260 shared_memory->applet_footer_type = AppletFooterUiType::JoyDual;
261 shared_memory->fullkey_color.fullkey = body_colors.left;
262 shared_memory->battery_level_dual = battery_level.left.battery_level;
263 shared_memory->system_properties.is_charging_joy_dual.Assign(
264 battery_level.left.is_charging);
265 } else if (controller.is_dual_left_connected) {
266 shared_memory->applet_footer_type = AppletFooterUiType::JoyDualLeftOnly;
267 shared_memory->fullkey_color.fullkey = body_colors.left;
268 shared_memory->battery_level_dual = battery_level.left.battery_level;
269 shared_memory->system_properties.is_charging_joy_dual.Assign(
270 battery_level.left.is_charging);
271 } else {
272 shared_memory->applet_footer_type = AppletFooterUiType::JoyDualRightOnly;
273 shared_memory->fullkey_color.fullkey = body_colors.right;
274 shared_memory->battery_level_dual = battery_level.right.battery_level;
275 shared_memory->system_properties.is_charging_joy_dual.Assign(
276 battery_level.right.is_charging);
277 }
278 break;
279 case Core::HID::NpadStyleIndex::JoyconLeft:
280 shared_memory->fullkey_color.attribute = ColorAttribute::Ok;
281 shared_memory->fullkey_color.fullkey = body_colors.left;
282 shared_memory->joycon_color.attribute = ColorAttribute::Ok;
283 shared_memory->joycon_color.left = body_colors.left;
284 shared_memory->battery_level_dual = battery_level.left.battery_level;
285 shared_memory->style_tag.joycon_left.Assign(1);
286 shared_memory->device_type.joycon_left.Assign(1);
287 shared_memory->system_properties.is_horizontal.Assign(1);
288 shared_memory->system_properties.use_minus.Assign(1);
289 shared_memory->system_properties.is_charging_joy_left.Assign(
290 battery_level.left.is_charging);
291 shared_memory->applet_footer_type = AppletFooterUiType::JoyLeftHorizontal;
292 shared_memory->sixaxis_left_properties.is_newly_assigned.Assign(1);
293 break;
294 case Core::HID::NpadStyleIndex::JoyconRight:
295 shared_memory->fullkey_color.attribute = ColorAttribute::Ok;
296 shared_memory->fullkey_color.fullkey = body_colors.right;
297 shared_memory->joycon_color.attribute = ColorAttribute::Ok;
298 shared_memory->joycon_color.right = body_colors.right;
299 shared_memory->battery_level_right = battery_level.right.battery_level;
300 shared_memory->style_tag.joycon_right.Assign(1);
301 shared_memory->device_type.joycon_right.Assign(1);
302 shared_memory->system_properties.is_horizontal.Assign(1);
303 shared_memory->system_properties.use_plus.Assign(1);
304 shared_memory->system_properties.is_charging_joy_right.Assign(
305 battery_level.right.is_charging);
306 shared_memory->applet_footer_type = AppletFooterUiType::JoyRightHorizontal;
307 shared_memory->sixaxis_right_properties.is_newly_assigned.Assign(1);
308 break;
309 case Core::HID::NpadStyleIndex::GameCube:
310 shared_memory->style_tag.gamecube.Assign(1);
311 shared_memory->device_type.fullkey.Assign(1);
312 shared_memory->system_properties.is_vertical.Assign(1);
313 shared_memory->system_properties.use_plus.Assign(1);
314 break;
315 case Core::HID::NpadStyleIndex::Pokeball:
316 shared_memory->style_tag.palma.Assign(1);
317 shared_memory->device_type.palma.Assign(1);
318 shared_memory->sixaxis_fullkey_properties.is_newly_assigned.Assign(1);
319 break;
320 case Core::HID::NpadStyleIndex::NES:
321 shared_memory->style_tag.lark.Assign(1);
322 shared_memory->device_type.fullkey.Assign(1);
323 break;
324 case Core::HID::NpadStyleIndex::SNES:
325 shared_memory->style_tag.lucia.Assign(1);
326 shared_memory->device_type.fullkey.Assign(1);
327 shared_memory->applet_footer_type = AppletFooterUiType::Lucia;
328 break;
329 case Core::HID::NpadStyleIndex::N64:
330 shared_memory->style_tag.lagoon.Assign(1);
331 shared_memory->device_type.fullkey.Assign(1);
332 shared_memory->applet_footer_type = AppletFooterUiType::Lagon;
333 break;
334 case Core::HID::NpadStyleIndex::SegaGenesis:
335 shared_memory->style_tag.lager.Assign(1);
336 shared_memory->device_type.fullkey.Assign(1);
337 break;
338 default:
339 break;
340 }
341
342 controller.is_connected = true;
343 controller.device->Connect();
344 controller.device->SetLedPattern();
345 if (controller_type == Core::HID::NpadStyleIndex::JoyconDual) {
346 if (controller.is_dual_left_connected) {
347 controller.device->SetPollingMode(Core::HID::EmulatedDeviceIndex::LeftIndex,
348 Common::Input::PollingMode::Active);
349 }
350 if (controller.is_dual_right_connected) {
351 controller.device->SetPollingMode(Core::HID::EmulatedDeviceIndex::RightIndex,
352 Common::Input::PollingMode::Active);
353 }
354 } else {
355 controller.device->SetPollingMode(Core::HID::EmulatedDeviceIndex::AllDevices,
356 Common::Input::PollingMode::Active);
357 }
358
359 npad_resource.SignalStyleSetUpdateEvent(aruid, npad_id);
360 WriteEmptyEntry(controller.shared_memory);
361 hid_core.SetLastActiveController(npad_id);
362}
363
364void NPad::WriteEmptyEntry(NpadInternalState* npad) {
365 NPadGenericState dummy_pad_state{};
366 NpadGcTriggerState dummy_gc_state{};
367 dummy_pad_state.sampling_number = npad->fullkey_lifo.ReadCurrentEntry().sampling_number + 1;
368 npad->fullkey_lifo.WriteNextEntry(dummy_pad_state);
369 dummy_pad_state.sampling_number = npad->handheld_lifo.ReadCurrentEntry().sampling_number + 1;
370 npad->handheld_lifo.WriteNextEntry(dummy_pad_state);
371 dummy_pad_state.sampling_number = npad->joy_dual_lifo.ReadCurrentEntry().sampling_number + 1;
372 npad->joy_dual_lifo.WriteNextEntry(dummy_pad_state);
373 dummy_pad_state.sampling_number = npad->joy_left_lifo.ReadCurrentEntry().sampling_number + 1;
374 npad->joy_left_lifo.WriteNextEntry(dummy_pad_state);
375 dummy_pad_state.sampling_number = npad->joy_right_lifo.ReadCurrentEntry().sampling_number + 1;
376 npad->joy_right_lifo.WriteNextEntry(dummy_pad_state);
377 dummy_pad_state.sampling_number = npad->palma_lifo.ReadCurrentEntry().sampling_number + 1;
378 npad->palma_lifo.WriteNextEntry(dummy_pad_state);
379 dummy_pad_state.sampling_number = npad->system_ext_lifo.ReadCurrentEntry().sampling_number + 1;
380 npad->system_ext_lifo.WriteNextEntry(dummy_pad_state);
381 dummy_gc_state.sampling_number = npad->gc_trigger_lifo.ReadCurrentEntry().sampling_number + 1;
382 npad->gc_trigger_lifo.WriteNextEntry(dummy_gc_state);
383}
384
385void NPad::RequestPadStateUpdate(u64 aruid, Core::HID::NpadIdType npad_id) {
386 std::scoped_lock lock{*applet_resource_holder.shared_mutex};
387 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
388 const auto controller_type = controller.device->GetNpadStyleIndex();
389
390 if (!controller.device->IsConnected() && controller.is_connected) {
391 DisconnectNpad(aruid, npad_id);
392 return;
393 }
394 if (!controller.device->IsConnected()) {
395 return;
396 }
397 if (controller.device->IsConnected() && !controller.is_connected) {
398 InitNewlyAddedController(aruid, npad_id);
399 }
400
401 // This function is unique to yuzu for the turbo buttons and motion to work properly
402 controller.device->StatusUpdate();
403
404 auto& pad_entry = controller.npad_pad_state;
405 auto& trigger_entry = controller.npad_trigger_state;
406 const auto button_state = controller.device->GetNpadButtons();
407 const auto stick_state = controller.device->GetSticks();
408
409 using btn = Core::HID::NpadButton;
410 pad_entry.npad_buttons.raw = btn::None;
411 if (controller_type != Core::HID::NpadStyleIndex::JoyconLeft) {
412 constexpr btn right_button_mask = btn::A | btn::B | btn::X | btn::Y | btn::StickR | btn::R |
413 btn::ZR | btn::Plus | btn::StickRLeft | btn::StickRUp |
414 btn::StickRRight | btn::StickRDown;
415 pad_entry.npad_buttons.raw = button_state.raw & right_button_mask;
416 pad_entry.r_stick = stick_state.right;
417 }
418
419 if (controller_type != Core::HID::NpadStyleIndex::JoyconRight) {
420 constexpr btn left_button_mask =
421 btn::Left | btn::Up | btn::Right | btn::Down | btn::StickL | btn::L | btn::ZL |
422 btn::Minus | btn::StickLLeft | btn::StickLUp | btn::StickLRight | btn::StickLDown;
423 pad_entry.npad_buttons.raw |= button_state.raw & left_button_mask;
424 pad_entry.l_stick = stick_state.left;
425 }
426
427 if (controller_type == Core::HID::NpadStyleIndex::JoyconLeft ||
428 controller_type == Core::HID::NpadStyleIndex::JoyconDual) {
429 pad_entry.npad_buttons.left_sl.Assign(button_state.left_sl);
430 pad_entry.npad_buttons.left_sr.Assign(button_state.left_sr);
431 }
432
433 if (controller_type == Core::HID::NpadStyleIndex::JoyconRight ||
434 controller_type == Core::HID::NpadStyleIndex::JoyconDual) {
435 pad_entry.npad_buttons.right_sl.Assign(button_state.right_sl);
436 pad_entry.npad_buttons.right_sr.Assign(button_state.right_sr);
437 }
438
439 if (controller_type == Core::HID::NpadStyleIndex::GameCube) {
440 const auto& trigger_state = controller.device->GetTriggers();
441 trigger_entry.l_analog = trigger_state.left;
442 trigger_entry.r_analog = trigger_state.right;
443 pad_entry.npad_buttons.zl.Assign(false);
444 pad_entry.npad_buttons.zr.Assign(button_state.r);
445 pad_entry.npad_buttons.l.Assign(button_state.zl);
446 pad_entry.npad_buttons.r.Assign(button_state.zr);
447 }
448
449 if (pad_entry.npad_buttons.raw != Core::HID::NpadButton::None) {
450 hid_core.SetLastActiveController(npad_id);
451 }
452}
453
454void NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
455 if (ref_counter == 0) {
456 return;
457 }
458
459 std::scoped_lock lock{*applet_resource_holder.shared_mutex};
460 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; ++aruid_index) {
461 const auto* data = applet_resource_holder.applet_resource->GetAruidDataByIndex(aruid_index);
462 const auto aruid = data->aruid;
463
464 if (!data->flag.is_assigned) {
465 continue;
466 }
467
468 for (std::size_t i = 0; i < controller_data[aruid_index].size(); ++i) {
469 auto& controller = controller_data[aruid_index][i];
470 controller.shared_memory =
471 &data->shared_memory_format->npad.npad_entry[i].internal_state;
472 auto* npad = controller.shared_memory;
473
474 const auto& controller_type = controller.device->GetNpadStyleIndex();
475
476 if (controller_type == Core::HID::NpadStyleIndex::None ||
477 !controller.device->IsConnected()) {
478 continue;
479 }
480
481 RequestPadStateUpdate(aruid, controller.device->GetNpadIdType());
482 auto& pad_state = controller.npad_pad_state;
483 auto& libnx_state = controller.npad_libnx_state;
484 auto& trigger_state = controller.npad_trigger_state;
485
486 // LibNX exclusively uses this section, so we always update it since LibNX doesn't
487 // activate any controllers.
488 libnx_state.connection_status.raw = 0;
489 libnx_state.connection_status.is_connected.Assign(1);
490 switch (controller_type) {
491 case Core::HID::NpadStyleIndex::None:
492 ASSERT(false);
493 break;
494 case Core::HID::NpadStyleIndex::ProController:
495 case Core::HID::NpadStyleIndex::NES:
496 case Core::HID::NpadStyleIndex::SNES:
497 case Core::HID::NpadStyleIndex::N64:
498 case Core::HID::NpadStyleIndex::SegaGenesis:
499 pad_state.connection_status.raw = 0;
500 pad_state.connection_status.is_connected.Assign(1);
501 pad_state.connection_status.is_wired.Assign(1);
502
503 libnx_state.connection_status.is_wired.Assign(1);
504 pad_state.sampling_number =
505 npad->fullkey_lifo.ReadCurrentEntry().state.sampling_number + 1;
506 npad->fullkey_lifo.WriteNextEntry(pad_state);
507 break;
508 case Core::HID::NpadStyleIndex::Handheld:
509 pad_state.connection_status.raw = 0;
510 pad_state.connection_status.is_connected.Assign(1);
511 pad_state.connection_status.is_wired.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);
518 libnx_state.connection_status.is_left_connected.Assign(1);
519 libnx_state.connection_status.is_right_connected.Assign(1);
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);
555 pad_state.connection_status.is_right_connected.Assign(1);
556
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;
584 }
585
586 libnx_state.npad_buttons.raw = pad_state.npad_buttons.raw;
587 libnx_state.l_stick = pad_state.l_stick;
588 libnx_state.r_stick = pad_state.r_stick;
589 npad->system_ext_lifo.WriteNextEntry(pad_state);
590
591 press_state |= static_cast<u64>(pad_state.npad_buttons.raw);
592 }
593 }
594}
595
596Result NPad::SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet supported_style_set) {
597 std::scoped_lock lock{mutex};
598 hid_core.SetSupportedStyleTag({supported_style_set});
599 const Result result = npad_resource.SetSupportedNpadStyleSet(aruid, supported_style_set);
600 if (result.IsSuccess()) {
601 OnUpdate({});
602 }
603 return result;
604}
605
606Result NPad::GetSupportedNpadStyleSet(u64 aruid,
607 Core::HID::NpadStyleSet& out_supported_style_set) const {
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;
614 }
615
616 return result;
617}
618
619Result NPad::GetMaskedSupportedNpadStyleSet(
620 u64 aruid, Core::HID::NpadStyleSet& out_supported_style_set) const {
621 std::scoped_lock lock{mutex};
622 const Result result =
623 npad_resource.GetMaskedSupportedNpadStyleSet(out_supported_style_set, aruid);
624
625 if (result == ResultUndefinedStyleset) {
626 out_supported_style_set = Core::HID::NpadStyleSet::None;
627 return ResultSuccess;
628 }
629
630 return result;
631}
632
633Result NPad::SetSupportedNpadIdType(u64 aruid,
634 std::span<const Core::HID::NpadIdType> supported_npad_list) {
635 std::scoped_lock lock{mutex};
636 if (supported_npad_list.size() > MaxSupportedNpadIdTypes) {
637 return ResultInvalidArraySize;
638 }
639
640 Result result = npad_resource.SetSupportedNpadIdType(aruid, supported_npad_list);
641
642 if (result.IsSuccess()) {
643 OnUpdate({});
644 }
645
646 return result;
647}
648
649Result NPad::SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type) {
650 std::scoped_lock lock{mutex};
651 return npad_resource.SetNpadJoyHoldType(aruid, hold_type);
652}
653
654Result NPad::GetNpadJoyHoldType(u64 aruid, NpadJoyHoldType& out_hold_type) const {
655 std::scoped_lock lock{mutex};
656 return npad_resource.GetNpadJoyHoldType(out_hold_type, aruid);
657}
658
659Result NPad::SetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode 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;
666}
667
668Result NPad::GetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode& out_mode) const {
669 std::scoped_lock lock{mutex};
670 return npad_resource.GetNpadHandheldActivationMode(out_mode, aruid);
671}
672
673bool NPad::SetNpadMode(u64 aruid, Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType npad_id,
674 NpadJoyDeviceType npad_device_type, NpadJoyAssignmentMode assignment_mode) {
675 if (!IsNpadIdValid(npad_id)) {
676 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
677 return false;
678 }
679
680 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
681 if (controller.shared_memory->assignment_mode != assignment_mode) {
682 controller.shared_memory->assignment_mode = assignment_mode;
683 }
684
685 if (!controller.device->IsConnected()) {
686 return false;
687 }
688
689 if (assignment_mode == NpadJoyAssignmentMode::Dual) {
690 if (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconLeft) {
691 DisconnectNpad(aruid, npad_id);
692 controller.is_dual_left_connected = true;
693 controller.is_dual_right_connected = false;
694 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, npad_id, true);
695 return false;
696 }
697 if (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconRight) {
698 DisconnectNpad(aruid, npad_id);
699 controller.is_dual_left_connected = false;
700 controller.is_dual_right_connected = true;
701 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, npad_id, true);
702 return false;
703 }
704 return false;
705 }
706
707 // This is for NpadJoyAssignmentMode::Single
708
709 // Only JoyconDual get affected by this function
710 if (controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::JoyconDual) {
711 return false;
712 }
713
714 if (controller.is_dual_left_connected && !controller.is_dual_right_connected) {
715 DisconnectNpad(aruid, npad_id);
716 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconLeft, npad_id, true);
717 return false;
718 }
719 if (!controller.is_dual_left_connected && controller.is_dual_right_connected) {
720 DisconnectNpad(aruid, npad_id);
721 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconRight, npad_id, true);
722 return false;
723 }
724
725 // We have two controllers connected to the same npad_id we need to split them
726 new_npad_id = hid_core.GetFirstDisconnectedNpadId();
727 auto& controller_2 = GetControllerFromNpadIdType(aruid, new_npad_id);
728 DisconnectNpad(aruid, npad_id);
729 if (npad_device_type == NpadJoyDeviceType::Left) {
730 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconLeft, npad_id, true);
731 controller_2.is_dual_left_connected = false;
732 controller_2.is_dual_right_connected = true;
733 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, new_npad_id, true);
734 } else {
735 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconRight, npad_id, true);
736 controller_2.is_dual_left_connected = true;
737 controller_2.is_dual_right_connected = false;
738 UpdateControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, new_npad_id, true);
739 }
740 return true;
741}
742
743bool NPad::VibrateControllerAtIndex(u64 aruid, Core::HID::NpadIdType npad_id,
744 std::size_t device_index,
745 const Core::HID::VibrationValue& vibration_value) {
746 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
747 if (!controller.device->IsConnected()) {
748 return false;
749 }
750
751 if (!controller.device->IsVibrationEnabled(device_index)) {
752 if (controller.vibration[device_index].latest_vibration_value.low_amplitude != 0.0f ||
753 controller.vibration[device_index].latest_vibration_value.high_amplitude != 0.0f) {
754 // Send an empty vibration to stop any vibrations.
755 Core::HID::VibrationValue vibration{0.0f, 160.0f, 0.0f, 320.0f};
756 controller.device->SetVibration(device_index, vibration);
757 // Then reset the vibration value to its default value.
758 controller.vibration[device_index].latest_vibration_value =
759 Core::HID::DEFAULT_VIBRATION_VALUE;
760 }
761
762 return false;
763 }
764
765 if (!Settings::values.enable_accurate_vibrations.GetValue()) {
766 using std::chrono::duration_cast;
767 using std::chrono::milliseconds;
768 using std::chrono::steady_clock;
769
770 const auto now = steady_clock::now();
771
772 // Filter out non-zero vibrations that are within 15ms of each other.
773 if ((vibration_value.low_amplitude != 0.0f || vibration_value.high_amplitude != 0.0f) &&
774 duration_cast<milliseconds>(
775 now - controller.vibration[device_index].last_vibration_timepoint) <
776 milliseconds(15)) {
777 return false;
778 }
779
780 controller.vibration[device_index].last_vibration_timepoint = now;
781 }
782
783 Core::HID::VibrationValue vibration{
784 vibration_value.low_amplitude, vibration_value.low_frequency,
785 vibration_value.high_amplitude, vibration_value.high_frequency};
786 return controller.device->SetVibration(device_index, vibration);
787}
788
789void NPad::VibrateController(u64 aruid,
790 const Core::HID::VibrationDeviceHandle& vibration_device_handle,
791 const Core::HID::VibrationValue& vibration_value) {
792 if (IsVibrationHandleValid(vibration_device_handle).IsError()) {
793 return;
794 }
795
796 if (!Settings::values.vibration_enabled.GetValue() && !permit_vibration_session_enabled) {
797 return;
798 }
799
800 auto& controller = GetControllerFromHandle(aruid, vibration_device_handle);
801 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
802
803 if (!controller.vibration[device_index].device_mounted || !controller.device->IsConnected()) {
804 return;
805 }
806
807 if (vibration_device_handle.device_index == Core::HID::DeviceIndex::None) {
808 ASSERT_MSG(false, "DeviceIndex should never be None!");
809 return;
810 }
811
812 // Some games try to send mismatched parameters in the device handle, block these.
813 if ((controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconLeft &&
814 (vibration_device_handle.npad_type == Core::HID::NpadStyleIndex::JoyconRight ||
815 vibration_device_handle.device_index == Core::HID::DeviceIndex::Right)) ||
816 (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconRight &&
817 (vibration_device_handle.npad_type == Core::HID::NpadStyleIndex::JoyconLeft ||
818 vibration_device_handle.device_index == Core::HID::DeviceIndex::Left))) {
819 return;
820 }
821
822 // Filter out vibrations with equivalent values to reduce unnecessary state changes.
823 if (vibration_value.low_amplitude ==
824 controller.vibration[device_index].latest_vibration_value.low_amplitude &&
825 vibration_value.high_amplitude ==
826 controller.vibration[device_index].latest_vibration_value.high_amplitude) {
827 return;
828 }
829
830 if (VibrateControllerAtIndex(aruid, controller.device->GetNpadIdType(), device_index,
831 vibration_value)) {
832 controller.vibration[device_index].latest_vibration_value = vibration_value;
833 }
834}
835
836void NPad::VibrateControllers(
837 u64 aruid, std::span<const Core::HID::VibrationDeviceHandle> vibration_device_handles,
838 std::span<const Core::HID::VibrationValue> vibration_values) {
839 if (!Settings::values.vibration_enabled.GetValue() && !permit_vibration_session_enabled) {
840 return;
841 }
842
843 ASSERT_OR_EXECUTE_MSG(
844 vibration_device_handles.size() == vibration_values.size(), { return; },
845 "The amount of device handles does not match with the amount of vibration values,"
846 "this is undefined behavior!");
847
848 for (std::size_t i = 0; i < vibration_device_handles.size(); ++i) {
849 VibrateController(aruid, vibration_device_handles[i], vibration_values[i]);
850 }
851}
852
853Core::HID::VibrationValue NPad::GetLastVibration(
854 u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const {
855 if (IsVibrationHandleValid(vibration_device_handle).IsError()) {
856 return {};
857 }
858
859 const auto& controller = GetControllerFromHandle(aruid, vibration_device_handle);
860 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
861 return controller.vibration[device_index].latest_vibration_value;
862}
863
864void NPad::InitializeVibrationDevice(
865 const Core::HID::VibrationDeviceHandle& vibration_device_handle) {
866 if (IsVibrationHandleValid(vibration_device_handle).IsError()) {
867 return;
868 }
869
870 const auto aruid = applet_resource_holder.applet_resource->GetActiveAruid();
871 const auto npad_index = static_cast<Core::HID::NpadIdType>(vibration_device_handle.npad_id);
872 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
873 InitializeVibrationDeviceAtIndex(aruid, npad_index, device_index);
874}
875
876void NPad::InitializeVibrationDeviceAtIndex(u64 aruid, Core::HID::NpadIdType npad_id,
877 std::size_t device_index) {
878 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
879 if (!Settings::values.vibration_enabled.GetValue()) {
880 controller.vibration[device_index].device_mounted = false;
881 return;
882 }
883
884 controller.vibration[device_index].device_mounted =
885 controller.device->IsVibrationEnabled(device_index);
886}
887
888void NPad::SetPermitVibrationSession(bool permit_vibration_session) {
889 permit_vibration_session_enabled = permit_vibration_session;
890}
891
892bool NPad::IsVibrationDeviceMounted(
893 u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const {
894 if (IsVibrationHandleValid(vibration_device_handle).IsError()) {
895 return false;
896 }
897
898 const auto& controller = GetControllerFromHandle(aruid, vibration_device_handle);
899 const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
900 return controller.vibration[device_index].device_mounted;
901}
902
903Result NPad::AcquireNpadStyleSetUpdateEventHandle(u64 aruid, Kernel::KReadableEvent** out_event,
904 Core::HID::NpadIdType npad_id) {
905 std::scoped_lock lock{mutex};
906 return npad_resource.AcquireNpadStyleSetUpdateEventHandle(aruid, out_event, npad_id);
907}
908
909void NPad::AddNewControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller,
910 Core::HID::NpadIdType npad_id) {
911 UpdateControllerAt(aruid, controller, npad_id, true);
912}
913
914void NPad::UpdateControllerAt(u64 aruid, Core::HID::NpadStyleIndex type,
915 Core::HID::NpadIdType npad_id, bool connected) {
916 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
917 if (!connected) {
918 DisconnectNpad(aruid, npad_id);
919 return;
920 }
921
922 controller.device->SetNpadStyleIndex(type);
923 InitNewlyAddedController(aruid, npad_id);
924}
925
926Result NPad::DisconnectNpad(u64 aruid, Core::HID::NpadIdType npad_id) {
927 if (!IsNpadIdValid(npad_id)) {
928 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
929 return ResultInvalidNpadId;
930 }
931
932 LOG_DEBUG(Service_HID, "Npad disconnected {}", npad_id);
933 auto& controller = GetControllerFromNpadIdType(aruid, npad_id);
934 for (std::size_t device_idx = 0; device_idx < controller.vibration.size(); ++device_idx) {
935 // Send an empty vibration to stop any vibrations.
936 VibrateControllerAtIndex(aruid, npad_id, device_idx, {});
937 controller.vibration[device_idx].device_mounted = false;
938 }
939
940 auto* shared_memory = controller.shared_memory;
941 // Don't reset shared_memory->assignment_mode this value is persistent
942 shared_memory->style_tag.raw = Core::HID::NpadStyleSet::None; // Zero out
943 shared_memory->device_type.raw = 0;
944 shared_memory->system_properties.raw = 0;
945 shared_memory->button_properties.raw = 0;
946 shared_memory->sixaxis_fullkey_properties.raw = 0;
947 shared_memory->sixaxis_handheld_properties.raw = 0;
948 shared_memory->sixaxis_dual_left_properties.raw = 0;
949 shared_memory->sixaxis_dual_right_properties.raw = 0;
950 shared_memory->sixaxis_left_properties.raw = 0;
951 shared_memory->sixaxis_right_properties.raw = 0;
952 shared_memory->battery_level_dual = Core::HID::NpadBatteryLevel::Empty;
953 shared_memory->battery_level_left = Core::HID::NpadBatteryLevel::Empty;
954 shared_memory->battery_level_right = Core::HID::NpadBatteryLevel::Empty;
955 shared_memory->fullkey_color = {
956 .attribute = ColorAttribute::NoController,
957 .fullkey = {},
958 };
959 shared_memory->joycon_color = {
960 .attribute = ColorAttribute::NoController,
961 .left = {},
962 .right = {},
963 };
964 shared_memory->applet_footer_type = AppletFooterUiType::None;
965
966 controller.is_dual_left_connected = true;
967 controller.is_dual_right_connected = true;
968 controller.is_connected = false;
969 controller.device->Disconnect();
970 npad_resource.SignalStyleSetUpdateEvent(aruid, npad_id);
971 WriteEmptyEntry(shared_memory);
972 return ResultSuccess;
973}
974
975Result NPad::IsFirmwareUpdateAvailableForSixAxisSensor(
976 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle,
977 bool& is_firmware_available) const {
978 const auto is_valid = IsSixaxisHandleValid(sixaxis_handle);
979 if (is_valid.IsError()) {
980 LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
981 return is_valid;
982 }
983
984 const auto& sixaxis_properties = GetSixaxisProperties(aruid, sixaxis_handle);
985 is_firmware_available = sixaxis_properties.is_firmware_update_available != 0;
986 return ResultSuccess;
987}
988
989Result NPad::ResetIsSixAxisSensorDeviceNewlyAssigned(
990 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle) {
991 const auto is_valid = IsSixaxisHandleValid(sixaxis_handle);
992 if (is_valid.IsError()) {
993 LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
994 return is_valid;
995 }
996
997 auto& sixaxis_properties = GetSixaxisProperties(aruid, sixaxis_handle);
998 sixaxis_properties.is_newly_assigned.Assign(0);
999
1000 return ResultSuccess;
1001}
1002
1003Result NPad::MergeSingleJoyAsDualJoy(u64 aruid, Core::HID::NpadIdType npad_id_1,
1004 Core::HID::NpadIdType npad_id_2) {
1005 if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) {
1006 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id_1:{}, npad_id_2:{}", npad_id_1,
1007 npad_id_2);
1008 return ResultInvalidNpadId;
1009 }
1010 auto& controller_1 = GetControllerFromNpadIdType(aruid, npad_id_1);
1011 auto& controller_2 = GetControllerFromNpadIdType(aruid, npad_id_2);
1012 auto controller_style_1 = controller_1.device->GetNpadStyleIndex();
1013 auto controller_style_2 = controller_2.device->GetNpadStyleIndex();
1014
1015 // Simplify this code by converting dualjoycon with only a side connected to single joycons
1016 if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconDual) {
1017 if (controller_1.is_dual_left_connected && !controller_1.is_dual_right_connected) {
1018 controller_style_1 = Core::HID::NpadStyleIndex::JoyconLeft;
1019 }
1020 if (!controller_1.is_dual_left_connected && controller_1.is_dual_right_connected) {
1021 controller_style_1 = Core::HID::NpadStyleIndex::JoyconRight;
1022 }
1023 }
1024 if (controller_style_2 == Core::HID::NpadStyleIndex::JoyconDual) {
1025 if (controller_2.is_dual_left_connected && !controller_2.is_dual_right_connected) {
1026 controller_style_2 = Core::HID::NpadStyleIndex::JoyconLeft;
1027 }
1028 if (!controller_2.is_dual_left_connected && controller_2.is_dual_right_connected) {
1029 controller_style_2 = Core::HID::NpadStyleIndex::JoyconRight;
1030 }
1031 }
1032
1033 // Invalid merge errors
1034 if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconDual ||
1035 controller_style_2 == Core::HID::NpadStyleIndex::JoyconDual) {
1036 return NpadIsDualJoycon;
1037 }
1038 if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconLeft &&
1039 controller_style_2 == Core::HID::NpadStyleIndex::JoyconLeft) {
1040 return NpadIsSameType;
1041 }
1042 if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconRight &&
1043 controller_style_2 == Core::HID::NpadStyleIndex::JoyconRight) {
1044 return NpadIsSameType;
1045 }
1046
1047 // These exceptions are handled as if they where dual joycon
1048 if (controller_style_1 != Core::HID::NpadStyleIndex::JoyconLeft &&
1049 controller_style_1 != Core::HID::NpadStyleIndex::JoyconRight) {
1050 return NpadIsDualJoycon;
1051 }
1052 if (controller_style_2 != Core::HID::NpadStyleIndex::JoyconLeft &&
1053 controller_style_2 != Core::HID::NpadStyleIndex::JoyconRight) {
1054 return NpadIsDualJoycon;
1055 }
1056
1057 // Disconnect the joycons and connect them as dual joycon at the first index.
1058 DisconnectNpad(aruid, npad_id_1);
1059 DisconnectNpad(aruid, npad_id_2);
1060 controller_1.is_dual_left_connected = true;
1061 controller_1.is_dual_right_connected = true;
1062 AddNewControllerAt(aruid, Core::HID::NpadStyleIndex::JoyconDual, npad_id_1);
1063 return ResultSuccess;
1064}
1065
1066Result NPad::StartLrAssignmentMode(u64 aruid) {
1067 std::scoped_lock lock{mutex};
1068 bool is_enabled{};
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;
1074}
1075
1076Result NPad::StopLrAssignmentMode(u64 aruid) {
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;
1084}
1085
1086Result NPad::SwapNpadAssignment(u64 aruid, Core::HID::NpadIdType npad_id_1,
1087 Core::HID::NpadIdType npad_id_2) {
1088 if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) {
1089 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id_1:{}, npad_id_2:{}", npad_id_1,
1090 npad_id_2);
1091 return ResultInvalidNpadId;
1092 }
1093 if (npad_id_1 == Core::HID::NpadIdType::Handheld ||
1094 npad_id_2 == Core::HID::NpadIdType::Handheld || npad_id_1 == Core::HID::NpadIdType::Other ||
1095 npad_id_2 == Core::HID::NpadIdType::Other) {
1096 return ResultSuccess;
1097 }
1098 const auto& controller_1 = GetControllerFromNpadIdType(aruid, npad_id_1).device;
1099 const auto& controller_2 = GetControllerFromNpadIdType(aruid, npad_id_2).device;
1100 const auto type_index_1 = controller_1->GetNpadStyleIndex();
1101 const auto type_index_2 = controller_2->GetNpadStyleIndex();
1102 const auto is_connected_1 = controller_1->IsConnected();
1103 const auto is_connected_2 = controller_2->IsConnected();
1104
1105 if (!npad_resource.IsControllerSupported(aruid, type_index_1) && is_connected_1) {
1106 return ResultNpadNotConnected;
1107 }
1108 if (!npad_resource.IsControllerSupported(aruid, type_index_2) && is_connected_2) {
1109 return ResultNpadNotConnected;
1110 }
1111
1112 UpdateControllerAt(aruid, type_index_2, npad_id_1, is_connected_2);
1113 UpdateControllerAt(aruid, type_index_1, npad_id_2, is_connected_1);
1114
1115 return ResultSuccess;
1116}
1117
1118Result NPad::GetLedPattern(Core::HID::NpadIdType npad_id, Core::HID::LedPattern& pattern) const {
1119 if (!IsNpadIdValid(npad_id)) {
1120 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
1121 return ResultInvalidNpadId;
1122 }
1123 const auto aruid = applet_resource_holder.applet_resource->GetActiveAruid();
1124 const auto& controller = GetControllerFromNpadIdType(aruid, npad_id).device;
1125 pattern = controller->GetLedPattern();
1126 return ResultSuccess;
1127}
1128
1129Result NPad::IsUnintendedHomeButtonInputProtectionEnabled(bool& out_is_enabled, u64 aruid,
1130 Core::HID::NpadIdType npad_id) const {
1131 std::scoped_lock lock{mutex};
1132 return npad_resource.GetHomeProtectionEnabled(out_is_enabled, aruid, npad_id);
1133}
1134
1135Result NPad::EnableUnintendedHomeButtonInputProtection(u64 aruid, Core::HID::NpadIdType npad_id,
1136 bool is_enabled) {
1137 std::scoped_lock lock{mutex};
1138 return npad_resource.SetHomeProtectionEnabled(aruid, npad_id, is_enabled);
1139}
1140
1141void NPad::SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled) {
1142 std::scoped_lock lock{mutex};
1143 npad_resource.SetNpadAnalogStickUseCenterClamp(aruid, is_enabled);
1144}
1145
1146void NPad::ClearAllConnectedControllers() {
1147 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
1148 for (auto& controller : controller_data[aruid_index]) {
1149 if (controller.device->IsConnected() &&
1150 controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::None) {
1151 controller.device->Disconnect();
1152 controller.device->SetNpadStyleIndex(Core::HID::NpadStyleIndex::None);
1153 }
1154 }
1155 }
1156}
1157
1158void NPad::DisconnectAllConnectedControllers() {
1159 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
1160 for (auto& controller : controller_data[aruid_index]) {
1161 controller.device->Disconnect();
1162 }
1163 }
1164}
1165
1166void NPad::ConnectAllDisconnectedControllers() {
1167 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
1168 for (auto& controller : controller_data[aruid_index]) {
1169 if (controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::None &&
1170 !controller.device->IsConnected()) {
1171 controller.device->Connect();
1172 }
1173 }
1174 }
1175}
1176
1177void NPad::ClearAllControllers() {
1178 for (std::size_t aruid_index = 0; aruid_index < AruidIndexMax; aruid_index++) {
1179 for (auto& controller : controller_data[aruid_index]) {
1180 controller.device->Disconnect();
1181 controller.device->SetNpadStyleIndex(Core::HID::NpadStyleIndex::None);
1182 }
1183 }
1184}
1185
1186Core::HID::NpadButton NPad::GetAndResetPressState() {
1187 return static_cast<Core::HID::NpadButton>(press_state.exchange(0));
1188}
1189
1190Result NPad::ApplyNpadSystemCommonPolicy(u64 aruid) {
1191 std::scoped_lock lock{mutex};
1192 const Result result = npad_resource.ApplyNpadSystemCommonPolicy(aruid, false);
1193 if (result.IsSuccess()) {
1194 OnUpdate({});
1195 }
1196 return result;
1197}
1198
1199Result NPad::ApplyNpadSystemCommonPolicyFull(u64 aruid) {
1200 std::scoped_lock lock{mutex};
1201 const Result result = npad_resource.ApplyNpadSystemCommonPolicy(aruid, true);
1202 if (result.IsSuccess()) {
1203 OnUpdate({});
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}
1232
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;
1238}
1239
1240NPad::NpadControllerData& NPad::GetControllerFromHandle(
1241 u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle) {
1242 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
1243 return GetControllerFromNpadIdType(aruid, npad_id);
1244}
1245
1246const NPad::NpadControllerData& NPad::GetControllerFromHandle(
1247 u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle) const {
1248 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
1249 return GetControllerFromNpadIdType(aruid, npad_id);
1250}
1251
1252NPad::NpadControllerData& NPad::GetControllerFromHandle(
1253 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) {
1254 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
1255 return GetControllerFromNpadIdType(aruid, npad_id);
1256}
1257
1258const NPad::NpadControllerData& NPad::GetControllerFromHandle(
1259 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const {
1260 const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
1261 return GetControllerFromNpadIdType(aruid, npad_id);
1262}
1263
1264NPad::NpadControllerData& NPad::GetControllerFromNpadIdType(u64 aruid,
1265 Core::HID::NpadIdType npad_id) {
1266 if (!IsNpadIdValid(npad_id)) {
1267 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
1268 npad_id = Core::HID::NpadIdType::Player1;
1269 }
1270 const auto npad_index = NpadIdTypeToIndex(npad_id);
1271 const auto aruid_index = applet_resource_holder.applet_resource->GetIndexFromAruid(aruid);
1272 return controller_data[aruid_index][npad_index];
1273}
1274
1275const NPad::NpadControllerData& NPad::GetControllerFromNpadIdType(
1276 u64 aruid, Core::HID::NpadIdType npad_id) const {
1277 if (!IsNpadIdValid(npad_id)) {
1278 LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
1279 npad_id = Core::HID::NpadIdType::Player1;
1280 }
1281 const auto npad_index = NpadIdTypeToIndex(npad_id);
1282 const auto aruid_index = applet_resource_holder.applet_resource->GetIndexFromAruid(aruid);
1283 return controller_data[aruid_index][npad_index];
1284}
1285
1286Core::HID::SixAxisSensorProperties& NPad::GetSixaxisProperties(
1287 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle) {
1288 auto& controller = GetControllerFromHandle(aruid, sixaxis_handle);
1289 switch (sixaxis_handle.npad_type) {
1290 case Core::HID::NpadStyleIndex::ProController:
1291 case Core::HID::NpadStyleIndex::Pokeball:
1292 return controller.shared_memory->sixaxis_fullkey_properties;
1293 case Core::HID::NpadStyleIndex::Handheld:
1294 return controller.shared_memory->sixaxis_handheld_properties;
1295 case Core::HID::NpadStyleIndex::JoyconDual:
1296 if (sixaxis_handle.device_index == Core::HID::DeviceIndex::Left) {
1297 return controller.shared_memory->sixaxis_dual_left_properties;
1298 }
1299 return controller.shared_memory->sixaxis_dual_right_properties;
1300 case Core::HID::NpadStyleIndex::JoyconLeft:
1301 return controller.shared_memory->sixaxis_left_properties;
1302 case Core::HID::NpadStyleIndex::JoyconRight:
1303 return controller.shared_memory->sixaxis_right_properties;
1304 default:
1305 return controller.shared_memory->sixaxis_fullkey_properties;
1306 }
1307}
1308
1309const Core::HID::SixAxisSensorProperties& NPad::GetSixaxisProperties(
1310 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle) const {
1311 const auto& controller = GetControllerFromHandle(aruid, sixaxis_handle);
1312 switch (sixaxis_handle.npad_type) {
1313 case Core::HID::NpadStyleIndex::ProController:
1314 case Core::HID::NpadStyleIndex::Pokeball:
1315 return controller.shared_memory->sixaxis_fullkey_properties;
1316 case Core::HID::NpadStyleIndex::Handheld:
1317 return controller.shared_memory->sixaxis_handheld_properties;
1318 case Core::HID::NpadStyleIndex::JoyconDual:
1319 if (sixaxis_handle.device_index == Core::HID::DeviceIndex::Left) {
1320 return controller.shared_memory->sixaxis_dual_left_properties;
1321 }
1322 return controller.shared_memory->sixaxis_dual_right_properties;
1323 case Core::HID::NpadStyleIndex::JoyconLeft:
1324 return controller.shared_memory->sixaxis_left_properties;
1325 case Core::HID::NpadStyleIndex::JoyconRight:
1326 return controller.shared_memory->sixaxis_right_properties;
1327 default:
1328 return controller.shared_memory->sixaxis_fullkey_properties;
1329 }
1330}
1331
1332AppletDetailedUiType NPad::GetAppletDetailedUiType(Core::HID::NpadIdType npad_id) {
1333 const auto aruid = applet_resource_holder.applet_resource->GetActiveAruid();
1334 const auto& shared_memory = GetControllerFromNpadIdType(aruid, npad_id).shared_memory;
1335
1336 return {
1337 .ui_variant = 0,
1338 .footer = shared_memory->applet_footer_type,
1339 };
1340}
1341
1342} // namespace Service::HID
diff --git a/src/hid_core/resources/npad/npad.h b/src/hid_core/resources/npad/npad.h
new file mode 100644
index 000000000..58f8c7acf
--- /dev/null
+++ b/src/hid_core/resources/npad/npad.h
@@ -0,0 +1,214 @@
1// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-2.0-or-later
3
4#pragma once
5
6#include <array>
7#include <atomic>
8#include <mutex>
9#include <span>
10
11#include "common/common_types.h"
12#include "hid_core/hid_types.h"
13#include "hid_core/resources/controller_base.h"
14#include "hid_core/resources/npad/npad_resource.h"
15#include "hid_core/resources/npad/npad_types.h"
16
17namespace Core::HID {
18class EmulatedController;
19enum class ControllerTriggerType;
20} // namespace Core::HID
21
22namespace Kernel {
23class KEvent;
24class KReadableEvent;
25} // namespace Kernel
26
27namespace Service::KernelHelpers {
28class ServiceContext;
29} // namespace Service::KernelHelpers
30
31union Result;
32
33namespace Service::HID {
34class AppletResource;
35struct NpadInternalState;
36struct NpadSixAxisSensorLifo;
37struct NpadSharedMemoryFormat;
38
39class NPad final {
40public:
41 explicit NPad(Core::HID::HIDCore& hid_core_, KernelHelpers::ServiceContext& service_context_);
42 ~NPad();
43
44 Result Activate();
45 Result Activate(u64 aruid);
46
47 Result ActivateNpadResource();
48 Result ActivateNpadResource(u64 aruid);
49
50 // When the controller is requesting an update for the shared memory
51 void OnUpdate(const Core::Timing::CoreTiming& core_timing);
52
53 Result SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet supported_style_set);
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;
58
59 Result SetSupportedNpadIdType(u64 aruid,
60 std::span<const Core::HID::NpadIdType> supported_npad_list);
61
62 Result SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type);
63 Result GetNpadJoyHoldType(u64 aruid, NpadJoyHoldType& out_hold_type) const;
64
65 Result SetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode mode);
66 Result GetNpadHandheldActivationMode(u64 aruid, NpadHandheldActivationMode& out_mode) const;
67
68 bool SetNpadMode(u64 aruid, Core::HID::NpadIdType& new_npad_id, Core::HID::NpadIdType npad_id,
69 NpadJoyDeviceType npad_device_type, NpadJoyAssignmentMode assignment_mode);
70
71 bool VibrateControllerAtIndex(u64 aruid, Core::HID::NpadIdType npad_id,
72 std::size_t device_index,
73 const Core::HID::VibrationValue& vibration_value);
74
75 void VibrateController(u64 aruid,
76 const Core::HID::VibrationDeviceHandle& vibration_device_handle,
77 const Core::HID::VibrationValue& vibration_value);
78
79 void VibrateControllers(
80 u64 aruid, std::span<const Core::HID::VibrationDeviceHandle> vibration_device_handles,
81 std::span<const Core::HID::VibrationValue> vibration_values);
82
83 Core::HID::VibrationValue GetLastVibration(
84 u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const;
85
86 void InitializeVibrationDevice(const Core::HID::VibrationDeviceHandle& vibration_device_handle);
87
88 void InitializeVibrationDeviceAtIndex(u64 aruid, Core::HID::NpadIdType npad_id,
89 std::size_t device_index);
90
91 void SetPermitVibrationSession(bool permit_vibration_session);
92
93 bool IsVibrationDeviceMounted(
94 u64 aruid, const Core::HID::VibrationDeviceHandle& vibration_device_handle) const;
95
96 Result AcquireNpadStyleSetUpdateEventHandle(u64 aruid, Kernel::KReadableEvent** out_event,
97 Core::HID::NpadIdType npad_id);
98
99 // Adds a new controller at an index.
100 void AddNewControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller,
101 Core::HID::NpadIdType npad_id);
102 // Adds a new controller at an index with connection status.
103 void UpdateControllerAt(u64 aruid, Core::HID::NpadStyleIndex controller,
104 Core::HID::NpadIdType npad_id, bool connected);
105
106 Result DisconnectNpad(u64 aruid, Core::HID::NpadIdType npad_id);
107
108 Result IsFirmwareUpdateAvailableForSixAxisSensor(
109 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle,
110 bool& is_firmware_available) const;
111 Result ResetIsSixAxisSensorDeviceNewlyAssigned(
112 u64 aruid, const Core::HID::SixAxisSensorHandle& sixaxis_handle);
113
114 Result GetLedPattern(Core::HID::NpadIdType npad_id, Core::HID::LedPattern& pattern) const;
115
116 Result IsUnintendedHomeButtonInputProtectionEnabled(bool& out_is_enabled, u64 aruid,
117 Core::HID::NpadIdType npad_id) const;
118 Result EnableUnintendedHomeButtonInputProtection(u64 aruid, Core::HID::NpadIdType npad_id,
119 bool is_enabled);
120
121 void SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled);
122 void ClearAllConnectedControllers();
123 void DisconnectAllConnectedControllers();
124 void ConnectAllDisconnectedControllers();
125 void ClearAllControllers();
126
127 Result MergeSingleJoyAsDualJoy(u64 aruid, Core::HID::NpadIdType npad_id_1,
128 Core::HID::NpadIdType npad_id_2);
129 Result StartLrAssignmentMode(u64 aruid);
130 Result StopLrAssignmentMode(u64 aruid);
131 Result SwapNpadAssignment(u64 aruid, Core::HID::NpadIdType npad_id_1,
132 Core::HID::NpadIdType npad_id_2);
133
134 // Logical OR for all buttons presses on all controllers
135 // Specifically for cheat engine and other features.
136 Core::HID::NpadButton GetAndResetPressState();
137
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);
149
150 AppletDetailedUiType GetAppletDetailedUiType(Core::HID::NpadIdType npad_id);
151
152private:
153 struct VibrationData {
154 bool device_mounted{};
155 Core::HID::VibrationValue latest_vibration_value{};
156 std::chrono::steady_clock::time_point last_vibration_timepoint{};
157 };
158
159 struct NpadControllerData {
160 NpadInternalState* shared_memory = nullptr;
161 Core::HID::EmulatedController* device = nullptr;
162
163 std::array<VibrationData, 2> vibration{};
164 bool is_connected{};
165
166 // Dual joycons can have only one side connected
167 bool is_dual_left_connected{true};
168 bool is_dual_right_connected{true};
169
170 // Current pad state
171 NPadGenericState npad_pad_state{};
172 NPadGenericState npad_libnx_state{};
173 NpadGcTriggerState npad_trigger_state{};
174 int callback_key{};
175 };
176
177 void ControllerUpdate(Core::HID::ControllerTriggerType type, std::size_t controller_idx);
178 void InitNewlyAddedController(u64 aruid, Core::HID::NpadIdType npad_id);
179 void RequestPadStateUpdate(u64 aruid, Core::HID::NpadIdType npad_id);
180 void WriteEmptyEntry(NpadInternalState* npad);
181
182 NpadControllerData& GetControllerFromHandle(
183 u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle);
184 const NpadControllerData& GetControllerFromHandle(
185 u64 aruid, const Core::HID::VibrationDeviceHandle& device_handle) const;
186 NpadControllerData& GetControllerFromHandle(
187 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle);
188 const NpadControllerData& GetControllerFromHandle(
189 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const;
190 NpadControllerData& GetControllerFromNpadIdType(u64 aruid, Core::HID::NpadIdType npad_id);
191 const NpadControllerData& GetControllerFromNpadIdType(u64 aruid,
192 Core::HID::NpadIdType npad_id) const;
193
194 Core::HID::SixAxisSensorProperties& GetSixaxisProperties(
195 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle);
196 const Core::HID::SixAxisSensorProperties& GetSixaxisProperties(
197 u64 aruid, const Core::HID::SixAxisSensorHandle& device_handle) const;
198
199 Core::HID::HIDCore& hid_core;
200 KernelHelpers::ServiceContext& service_context;
201
202 s32 ref_counter{};
203 mutable std::mutex mutex;
204 NPadResource npad_resource;
205 AppletResourceHolder applet_resource_holder{};
206 Kernel::KEvent* input_event{nullptr};
207 std::mutex* input_mutex{nullptr};
208
209 std::atomic<u64> press_state{};
210 bool permit_vibration_session_enabled;
211 std::array<std::array<NpadControllerData, MaxSupportedNpadIdTypes>, AruidIndexMax>
212 controller_data{};
213};
214} // namespace Service::HID
diff --git a/src/hid_core/resources/npad/npad_data.cpp b/src/hid_core/resources/npad/npad_data.cpp
new file mode 100644
index 000000000..c7e9760cb
--- /dev/null
+++ b/src/hid_core/resources/npad/npad_data.cpp
@@ -0,0 +1,228 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#include "hid_core/hid_util.h"
5#include "hid_core/resources/npad/npad_data.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/hid_core/resources/npad/npad_data.h b/src/hid_core/resources/npad/npad_data.h
new file mode 100644
index 000000000..86bd3b81c
--- /dev/null
+++ b/src/hid_core/resources/npad/npad_data.h
@@ -0,0 +1,88 @@
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/hle/result.h"
11#include "hid_core/hid_types.h"
12#include "hid_core/resources/npad/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/hid_core/resources/npad/npad_resource.cpp b/src/hid_core/resources/npad/npad_resource.cpp
new file mode 100644
index 000000000..b0255a05c
--- /dev/null
+++ b/src/hid_core/resources/npad/npad_resource.cpp
@@ -0,0 +1,685 @@
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 "hid_core/hid_result.h"
7#include "hid_core/hid_util.h"
8#include "hid_core/resources/npad/npad_resource.h"
9#include "hid_core/resources/npad/npad_types.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/hid_core/resources/npad/npad_resource.h b/src/hid_core/resources/npad/npad_resource.h
new file mode 100644
index 000000000..aed89eec6
--- /dev/null
+++ b/src/hid_core/resources/npad/npad_resource.h
@@ -0,0 +1,132 @@
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/hle/result.h"
12#include "core/hle/service/kernel_helpers.h"
13#include "hid_core/hid_types.h"
14#include "hid_core/resources/applet_resource.h"
15#include "hid_core/resources/npad/npad_data.h"
16#include "hid_core/resources/npad/npad_types.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/hid_core/resources/npad/npad_types.h b/src/hid_core/resources/npad/npad_types.h
new file mode 100644
index 000000000..a02f9cf16
--- /dev/null
+++ b/src/hid_core/resources/npad/npad_types.h
@@ -0,0 +1,255 @@
1// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
2// SPDX-License-Identifier: GPL-3.0-or-later
3
4#pragma once
5
6#include "common/bit_field.h"
7#include "common/common_funcs.h"
8#include "common/common_types.h"
9#include "hid_core/hid_types.h"
10
11namespace Service::HID {
12static constexpr std::size_t MaxSupportedNpadIdTypes = 10;
13static constexpr std::size_t StyleIndexCount = 7;
14
15// This is nn::hid::NpadJoyHoldType
16enum class NpadJoyHoldType : u64 {
17 Vertical = 0,
18 Horizontal = 1,
19};
20
21// This is nn::hid::NpadJoyAssignmentMode
22enum class NpadJoyAssignmentMode : u32 {
23 Dual = 0,
24 Single = 1,
25};
26
27// This is nn::hid::NpadJoyDeviceType
28enum class NpadJoyDeviceType : s64 {
29 Left = 0,
30 Right = 1,
31};
32
33// This is nn::hid::NpadHandheldActivationMode
34enum class NpadHandheldActivationMode : u64 {
35 Dual = 0,
36 Single = 1,
37 None = 2,
38 MaxActivationMode = 3,
39};
40
41// This is nn::hid::system::AppletFooterUiAttributesSet
42struct AppletFooterUiAttributes {
43 INSERT_PADDING_BYTES(0x4);
44};
45
46// This is nn::hid::system::AppletFooterUiType
47enum class AppletFooterUiType : u8 {
48 None = 0,
49 HandheldNone = 1,
50 HandheldJoyConLeftOnly = 2,
51 HandheldJoyConRightOnly = 3,
52 HandheldJoyConLeftJoyConRight = 4,
53 JoyDual = 5,
54 JoyDualLeftOnly = 6,
55 JoyDualRightOnly = 7,
56 JoyLeftHorizontal = 8,
57 JoyLeftVertical = 9,
58 JoyRightHorizontal = 10,
59 JoyRightVertical = 11,
60 SwitchProController = 12,
61 CompatibleProController = 13,
62 CompatibleJoyCon = 14,
63 LarkHvc1 = 15,
64 LarkHvc2 = 16,
65 LarkNesLeft = 17,
66 LarkNesRight = 18,
67 Lucia = 19,
68 Verification = 20,
69 Lagon = 21,
70};
71
72using AppletFooterUiVariant = u8;
73
74// This is "nn::hid::system::AppletDetailedUiType".
75struct AppletDetailedUiType {
76 AppletFooterUiVariant ui_variant;
77 INSERT_PADDING_BYTES(0x2);
78 AppletFooterUiType footer;
79};
80static_assert(sizeof(AppletDetailedUiType) == 0x4, "AppletDetailedUiType is an invalid size");
81// This is nn::hid::NpadCommunicationMode
82enum class NpadCommunicationMode : u64 {
83 Mode_5ms = 0,
84 Mode_10ms = 1,
85 Mode_15ms = 2,
86 Default = 3,
87};
88
89enum class NpadRevision : u32 {
90 Revision0 = 0,
91 Revision1 = 1,
92 Revision2 = 2,
93 Revision3 = 3,
94};
95
96// This is nn::hid::detail::ColorAttribute
97enum class ColorAttribute : u32 {
98 Ok = 0,
99 ReadError = 1,
100 NoController = 2,
101};
102static_assert(sizeof(ColorAttribute) == 4, "ColorAttribute is an invalid size");
103
104// This is nn::hid::detail::NpadFullKeyColorState
105struct NpadFullKeyColorState {
106 ColorAttribute attribute{ColorAttribute::NoController};
107 Core::HID::NpadControllerColor fullkey{};
108};
109static_assert(sizeof(NpadFullKeyColorState) == 0xC, "NpadFullKeyColorState is an invalid size");
110
111// This is nn::hid::detail::NpadJoyColorState
112struct NpadJoyColorState {
113 ColorAttribute attribute{ColorAttribute::NoController};
114 Core::HID::NpadControllerColor left{};
115 Core::HID::NpadControllerColor right{};
116};
117static_assert(sizeof(NpadJoyColorState) == 0x14, "NpadJoyColorState is an invalid size");
118
119// This is nn::hid::NpadAttribute
120struct NpadAttribute {
121 union {
122 u32 raw{};
123 BitField<0, 1, u32> is_connected;
124 BitField<1, 1, u32> is_wired;
125 BitField<2, 1, u32> is_left_connected;
126 BitField<3, 1, u32> is_left_wired;
127 BitField<4, 1, u32> is_right_connected;
128 BitField<5, 1, u32> is_right_wired;
129 };
130};
131static_assert(sizeof(NpadAttribute) == 4, "NpadAttribute is an invalid size");
132
133// This is nn::hid::NpadFullKeyState
134// This is nn::hid::NpadHandheldState
135// This is nn::hid::NpadJoyDualState
136// This is nn::hid::NpadJoyLeftState
137// This is nn::hid::NpadJoyRightState
138// This is nn::hid::NpadPalmaState
139// This is nn::hid::NpadSystemExtState
140struct NPadGenericState {
141 s64_le sampling_number{};
142 Core::HID::NpadButtonState npad_buttons{};
143 Core::HID::AnalogStickState l_stick{};
144 Core::HID::AnalogStickState r_stick{};
145 NpadAttribute connection_status{};
146 INSERT_PADDING_BYTES(4); // Reserved
147};
148static_assert(sizeof(NPadGenericState) == 0x28, "NPadGenericState is an invalid size");
149
150// This is nn::hid::server::NpadGcTriggerState
151struct NpadGcTriggerState {
152 s64 sampling_number{};
153 s32 l_analog{};
154 s32 r_analog{};
155};
156static_assert(sizeof(NpadGcTriggerState) == 0x10, "NpadGcTriggerState is an invalid size");
157
158// This is nn::hid::NpadSystemProperties
159struct NPadSystemProperties {
160 union {
161 s64 raw{};
162 BitField<0, 1, s64> is_charging_joy_dual;
163 BitField<1, 1, s64> is_charging_joy_left;
164 BitField<2, 1, s64> is_charging_joy_right;
165 BitField<3, 1, s64> is_powered_joy_dual;
166 BitField<4, 1, s64> is_powered_joy_left;
167 BitField<5, 1, s64> is_powered_joy_right;
168 BitField<9, 1, s64> is_system_unsupported_button;
169 BitField<10, 1, s64> is_system_ext_unsupported_button;
170 BitField<11, 1, s64> is_vertical;
171 BitField<12, 1, s64> is_horizontal;
172 BitField<13, 1, s64> use_plus;
173 BitField<14, 1, s64> use_minus;
174 BitField<15, 1, s64> use_directional_buttons;
175 };
176};
177static_assert(sizeof(NPadSystemProperties) == 0x8, "NPadSystemProperties is an invalid size");
178
179// This is nn::hid::NpadSystemButtonProperties
180struct NpadSystemButtonProperties {
181 union {
182 s32 raw{};
183 BitField<0, 1, s32> is_home_button_protection_enabled;
184 };
185};
186static_assert(sizeof(NpadSystemButtonProperties) == 0x4, "NPadButtonProperties is an invalid size");
187
188// This is nn::hid::system::DeviceType
189struct DeviceType {
190 union {
191 u32 raw{};
192 BitField<0, 1, s32> fullkey;
193 BitField<1, 1, s32> debug_pad;
194 BitField<2, 1, s32> handheld_left;
195 BitField<3, 1, s32> handheld_right;
196 BitField<4, 1, s32> joycon_left;
197 BitField<5, 1, s32> joycon_right;
198 BitField<6, 1, s32> palma;
199 BitField<7, 1, s32> lark_hvc_left;
200 BitField<8, 1, s32> lark_hvc_right;
201 BitField<9, 1, s32> lark_nes_left;
202 BitField<10, 1, s32> lark_nes_right;
203 BitField<11, 1, s32> handheld_lark_hvc_left;
204 BitField<12, 1, s32> handheld_lark_hvc_right;
205 BitField<13, 1, s32> handheld_lark_nes_left;
206 BitField<14, 1, s32> handheld_lark_nes_right;
207 BitField<15, 1, s32> lucia;
208 BitField<16, 1, s32> lagon;
209 BitField<17, 1, s32> lager;
210 BitField<31, 1, s32> system;
211 };
212};
213
214// This is nn::hid::detail::NfcXcdDeviceHandleStateImpl
215struct NfcXcdDeviceHandleStateImpl {
216 u64 handle{};
217 bool is_available{};
218 bool is_activated{};
219 INSERT_PADDING_BYTES(0x6); // Reserved
220 u64 sampling_number{};
221};
222static_assert(sizeof(NfcXcdDeviceHandleStateImpl) == 0x18,
223 "NfcXcdDeviceHandleStateImpl is an invalid size");
224
225// This is nn::hid::NpadLarkType
226enum class NpadLarkType : u32 {
227 Invalid,
228 H1,
229 H2,
230 NL,
231 NR,
232};
233
234// This is nn::hid::NpadLuciaType
235enum class NpadLuciaType : u32 {
236 Invalid,
237 J,
238 E,
239 U,
240};
241
242// This is nn::hid::NpadLagonType
243enum class NpadLagonType : u32 {
244 Invalid,
245};
246
247// This is nn::hid::NpadLagerType
248enum class NpadLagerType : u32 {
249 Invalid,
250 J,
251 E,
252 U,
253};
254
255} // namespace Service::HID