npad_resource.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. #include "core/hle/kernel/k_event.h"
  4. #include "core/hle/kernel/k_readable_event.h"
  5. #include "hid_core/hid_result.h"
  6. #include "hid_core/hid_util.h"
  7. #include "hid_core/resources/npad/npad_resource.h"
  8. #include "hid_core/resources/npad/npad_types.h"
  9. namespace Service::HID {
  10. NPadResource::NPadResource(KernelHelpers::ServiceContext& context) : service_context{context} {}
  11. NPadResource::~NPadResource() = default;
  12. Result NPadResource::RegisterAppletResourceUserId(u64 aruid) {
  13. const auto aruid_index = GetIndexFromAruid(aruid);
  14. if (aruid_index < AruidIndexMax) {
  15. return ResultAruidAlreadyRegistered;
  16. }
  17. std::size_t data_index = AruidIndexMax;
  18. for (std::size_t i = 0; i < AruidIndexMax; i++) {
  19. if (!state[i].flag.is_initialized) {
  20. data_index = i;
  21. break;
  22. }
  23. }
  24. if (data_index == AruidIndexMax) {
  25. return ResultAruidNoAvailableEntries;
  26. }
  27. auto& aruid_data = state[data_index];
  28. aruid_data.aruid = aruid;
  29. aruid_data.flag.is_initialized.Assign(true);
  30. data_index = AruidIndexMax;
  31. for (std::size_t i = 0; i < AruidIndexMax; i++) {
  32. if (registration_list.flag[i] == RegistrationStatus::Initialized) {
  33. if (registration_list.aruid[i] != aruid) {
  34. continue;
  35. }
  36. data_index = i;
  37. break;
  38. }
  39. // TODO: Don't Handle pending delete here
  40. if (registration_list.flag[i] == RegistrationStatus::None ||
  41. registration_list.flag[i] == RegistrationStatus::PendingDelete) {
  42. data_index = i;
  43. break;
  44. }
  45. }
  46. if (data_index == AruidIndexMax) {
  47. return ResultSuccess;
  48. }
  49. registration_list.flag[data_index] = RegistrationStatus::Initialized;
  50. registration_list.aruid[data_index] = aruid;
  51. return ResultSuccess;
  52. }
  53. void NPadResource::UnregisterAppletResourceUserId(u64 aruid) {
  54. const u64 aruid_index = GetIndexFromAruid(aruid);
  55. DestroyStyleSetUpdateEvents(aruid);
  56. if (aruid_index < AruidIndexMax) {
  57. state[aruid_index] = {};
  58. registration_list.flag[aruid_index] = RegistrationStatus::PendingDelete;
  59. }
  60. }
  61. void NPadResource::DestroyStyleSetUpdateEvents(u64 aruid) {
  62. const u64 aruid_index = GetIndexFromAruid(aruid);
  63. if (aruid_index >= AruidIndexMax) {
  64. return;
  65. }
  66. for (auto& controller_state : state[aruid_index].controller_state) {
  67. if (!controller_state.is_styleset_update_event_initialized) {
  68. continue;
  69. }
  70. service_context.CloseEvent(controller_state.style_set_update_event);
  71. controller_state.is_styleset_update_event_initialized = false;
  72. }
  73. }
  74. Result NPadResource::Activate(u64 aruid) {
  75. const u64 aruid_index = GetIndexFromAruid(aruid);
  76. if (aruid_index >= AruidIndexMax) {
  77. return ResultSuccess;
  78. }
  79. auto& state_data = state[aruid_index];
  80. if (state_data.flag.is_assigned) {
  81. return ResultAruidAlreadyRegistered;
  82. }
  83. state_data.flag.is_assigned.Assign(true);
  84. state_data.data.ClearNpadSystemCommonPolicy();
  85. state_data.npad_revision = NpadRevision::Revision0;
  86. state_data.button_config = {};
  87. if (active_data_aruid == aruid) {
  88. default_hold_type = active_data.GetNpadJoyHoldType();
  89. active_data.SetNpadJoyHoldType(default_hold_type);
  90. }
  91. return ResultSuccess;
  92. }
  93. Result NPadResource::Activate() {
  94. if (ref_counter == std::numeric_limits<s32>::max() - 1) {
  95. return ResultAppletResourceOverflow;
  96. }
  97. if (ref_counter == 0) {
  98. RegisterAppletResourceUserId(SystemAruid);
  99. Activate(SystemAruid);
  100. }
  101. ref_counter++;
  102. return ResultSuccess;
  103. }
  104. Result NPadResource::Deactivate() {
  105. if (ref_counter == 0) {
  106. return ResultAppletResourceNotInitialized;
  107. }
  108. UnregisterAppletResourceUserId(SystemAruid);
  109. ref_counter--;
  110. return ResultSuccess;
  111. }
  112. NPadData* NPadResource::GetActiveData() {
  113. return &active_data;
  114. }
  115. u64 NPadResource::GetActiveDataAruid() {
  116. return active_data_aruid;
  117. }
  118. void NPadResource::SetAppletResourceUserId(u64 aruid) {
  119. if (active_data_aruid == aruid) {
  120. return;
  121. }
  122. active_data_aruid = aruid;
  123. default_hold_type = active_data.GetNpadJoyHoldType();
  124. const u64 aruid_index = GetIndexFromAruid(aruid);
  125. if (aruid_index >= AruidIndexMax) {
  126. return;
  127. }
  128. auto& data = state[aruid_index].data;
  129. if (data.GetNpadStatus().is_policy || data.GetNpadStatus().is_full_policy) {
  130. data.SetNpadJoyHoldType(default_hold_type);
  131. }
  132. active_data = data;
  133. if (data.GetNpadStatus().is_hold_type_set) {
  134. active_data.SetNpadJoyHoldType(default_hold_type);
  135. }
  136. }
  137. std::size_t NPadResource::GetIndexFromAruid(u64 aruid) const {
  138. for (std::size_t i = 0; i < AruidIndexMax; i++) {
  139. if (registration_list.flag[i] == RegistrationStatus::Initialized &&
  140. registration_list.aruid[i] == aruid) {
  141. return i;
  142. }
  143. }
  144. return AruidIndexMax;
  145. }
  146. Result NPadResource::ApplyNpadSystemCommonPolicy(u64 aruid, bool is_full_policy) {
  147. const u64 aruid_index = GetIndexFromAruid(aruid);
  148. if (aruid_index >= AruidIndexMax) {
  149. return ResultNpadNotConnected;
  150. }
  151. auto& data = state[aruid_index].data;
  152. data.SetNpadSystemCommonPolicy(is_full_policy);
  153. data.SetNpadJoyHoldType(default_hold_type);
  154. if (active_data_aruid == aruid) {
  155. active_data.SetNpadSystemCommonPolicy(is_full_policy);
  156. active_data.SetNpadJoyHoldType(default_hold_type);
  157. }
  158. return ResultSuccess;
  159. }
  160. Result NPadResource::ClearNpadSystemCommonPolicy(u64 aruid) {
  161. const u64 aruid_index = GetIndexFromAruid(aruid);
  162. if (aruid_index >= AruidIndexMax) {
  163. return ResultNpadNotConnected;
  164. }
  165. state[aruid_index].data.ClearNpadSystemCommonPolicy();
  166. if (active_data_aruid == aruid) {
  167. active_data.ClearNpadSystemCommonPolicy();
  168. }
  169. return ResultSuccess;
  170. }
  171. Result NPadResource::SetSupportedNpadStyleSet(u64 aruid, Core::HID::NpadStyleSet style_set) {
  172. const u64 aruid_index = GetIndexFromAruid(aruid);
  173. if (aruid_index >= AruidIndexMax) {
  174. return ResultNpadNotConnected;
  175. }
  176. auto& data = state[aruid_index].data;
  177. data.SetSupportedNpadStyleSet(style_set);
  178. if (active_data_aruid == aruid) {
  179. active_data.SetSupportedNpadStyleSet(style_set);
  180. active_data.SetNpadJoyHoldType(data.GetNpadJoyHoldType());
  181. }
  182. return ResultSuccess;
  183. }
  184. Result NPadResource::GetSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_Set,
  185. u64 aruid) const {
  186. const u64 aruid_index = GetIndexFromAruid(aruid);
  187. if (aruid_index >= AruidIndexMax) {
  188. return ResultNpadNotConnected;
  189. }
  190. auto& data = state[aruid_index].data;
  191. if (!data.GetNpadStatus().is_supported_styleset_set) {
  192. return ResultUndefinedStyleset;
  193. }
  194. out_style_Set = data.GetSupportedNpadStyleSet();
  195. return ResultSuccess;
  196. }
  197. Result NPadResource::GetMaskedSupportedNpadStyleSet(Core::HID::NpadStyleSet& out_style_set,
  198. u64 aruid) const {
  199. if (aruid == SystemAruid) {
  200. out_style_set = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
  201. Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
  202. Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Palma |
  203. Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
  204. return ResultSuccess;
  205. }
  206. const u64 aruid_index = GetIndexFromAruid(aruid);
  207. if (aruid_index >= AruidIndexMax) {
  208. return ResultNpadNotConnected;
  209. }
  210. auto& data = state[aruid_index].data;
  211. if (!data.GetNpadStatus().is_supported_styleset_set) {
  212. return ResultUndefinedStyleset;
  213. }
  214. Core::HID::NpadStyleSet mask{Core::HID::NpadStyleSet::None};
  215. out_style_set = data.GetSupportedNpadStyleSet();
  216. switch (state[aruid_index].npad_revision) {
  217. case NpadRevision::Revision1:
  218. mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
  219. Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
  220. Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
  221. Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::SystemExt |
  222. Core::HID::NpadStyleSet::System;
  223. break;
  224. case NpadRevision::Revision2:
  225. mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
  226. Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
  227. Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
  228. Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
  229. Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
  230. break;
  231. case NpadRevision::Revision3:
  232. mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
  233. Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
  234. Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
  235. Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
  236. Core::HID::NpadStyleSet::HandheldLark | Core::HID::NpadStyleSet::Lucia |
  237. Core::HID::NpadStyleSet::Lagoon | Core::HID::NpadStyleSet::Lager |
  238. Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
  239. break;
  240. default:
  241. mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
  242. Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
  243. Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::SystemExt |
  244. Core::HID::NpadStyleSet::System;
  245. break;
  246. }
  247. out_style_set = out_style_set & mask;
  248. return ResultSuccess;
  249. }
  250. Result NPadResource::GetAvailableStyleset(Core::HID::NpadStyleSet& out_style_set, u64 aruid) const {
  251. const u64 aruid_index = GetIndexFromAruid(aruid);
  252. if (aruid_index >= AruidIndexMax) {
  253. return ResultNpadNotConnected;
  254. }
  255. auto& data = state[aruid_index].data;
  256. if (!data.GetNpadStatus().is_supported_styleset_set) {
  257. return ResultUndefinedStyleset;
  258. }
  259. Core::HID::NpadStyleSet mask{Core::HID::NpadStyleSet::None};
  260. out_style_set = data.GetSupportedNpadStyleSet();
  261. switch (state[aruid_index].npad_revision) {
  262. case NpadRevision::Revision1:
  263. mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
  264. Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
  265. Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
  266. Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::SystemExt |
  267. Core::HID::NpadStyleSet::System;
  268. break;
  269. case NpadRevision::Revision2:
  270. mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
  271. Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
  272. Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
  273. Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
  274. Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
  275. break;
  276. case NpadRevision::Revision3:
  277. mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
  278. Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
  279. Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::Gc |
  280. Core::HID::NpadStyleSet::Palma | Core::HID::NpadStyleSet::Lark |
  281. Core::HID::NpadStyleSet::HandheldLark | Core::HID::NpadStyleSet::Lucia |
  282. Core::HID::NpadStyleSet::Lagoon | Core::HID::NpadStyleSet::Lager |
  283. Core::HID::NpadStyleSet::SystemExt | Core::HID::NpadStyleSet::System;
  284. break;
  285. default:
  286. mask = Core::HID::NpadStyleSet::Fullkey | Core::HID::NpadStyleSet::Handheld |
  287. Core::HID::NpadStyleSet::JoyDual | Core::HID::NpadStyleSet::JoyLeft |
  288. Core::HID::NpadStyleSet::JoyRight | Core::HID::NpadStyleSet::SystemExt |
  289. Core::HID::NpadStyleSet::System;
  290. break;
  291. }
  292. out_style_set = out_style_set & mask;
  293. return ResultSuccess;
  294. }
  295. NpadRevision NPadResource::GetNpadRevision(u64 aruid) const {
  296. const u64 aruid_index = GetIndexFromAruid(aruid);
  297. if (aruid_index >= AruidIndexMax) {
  298. return NpadRevision::Revision0;
  299. }
  300. return state[aruid_index].npad_revision;
  301. }
  302. Result NPadResource::IsSupportedNpadStyleSet(bool& is_set, u64 aruid) {
  303. const u64 aruid_index = GetIndexFromAruid(aruid);
  304. if (aruid_index >= AruidIndexMax) {
  305. return ResultNpadNotConnected;
  306. }
  307. is_set = state[aruid_index].data.GetNpadStatus().is_supported_styleset_set.Value() != 0;
  308. return ResultSuccess;
  309. }
  310. Result NPadResource::SetNpadJoyHoldType(u64 aruid, NpadJoyHoldType hold_type) {
  311. const u64 aruid_index = GetIndexFromAruid(aruid);
  312. if (aruid_index >= AruidIndexMax) {
  313. return ResultNpadNotConnected;
  314. }
  315. state[aruid_index].data.SetNpadJoyHoldType(hold_type);
  316. if (active_data_aruid == aruid) {
  317. active_data.SetNpadJoyHoldType(hold_type);
  318. }
  319. return ResultSuccess;
  320. }
  321. Result NPadResource::GetNpadJoyHoldType(NpadJoyHoldType& hold_type, u64 aruid) const {
  322. const u64 aruid_index = GetIndexFromAruid(aruid);
  323. if (aruid_index >= AruidIndexMax) {
  324. return ResultNpadNotConnected;
  325. }
  326. auto& data = state[aruid_index].data;
  327. if (data.GetNpadStatus().is_policy || data.GetNpadStatus().is_full_policy) {
  328. hold_type = active_data.GetNpadJoyHoldType();
  329. return ResultSuccess;
  330. }
  331. hold_type = data.GetNpadJoyHoldType();
  332. return ResultSuccess;
  333. }
  334. Result NPadResource::SetNpadHandheldActivationMode(u64 aruid,
  335. NpadHandheldActivationMode activation_mode) {
  336. const u64 aruid_index = GetIndexFromAruid(aruid);
  337. if (aruid_index >= AruidIndexMax) {
  338. return ResultNpadNotConnected;
  339. }
  340. state[aruid_index].data.SetHandheldActivationMode(activation_mode);
  341. if (active_data_aruid == aruid) {
  342. active_data.SetHandheldActivationMode(activation_mode);
  343. }
  344. return ResultSuccess;
  345. }
  346. Result NPadResource::GetNpadHandheldActivationMode(NpadHandheldActivationMode& activation_mode,
  347. u64 aruid) const {
  348. const u64 aruid_index = GetIndexFromAruid(aruid);
  349. if (aruid_index >= AruidIndexMax) {
  350. return ResultNpadNotConnected;
  351. }
  352. activation_mode = state[aruid_index].data.GetHandheldActivationMode();
  353. return ResultSuccess;
  354. }
  355. Result NPadResource::SetSupportedNpadIdType(
  356. u64 aruid, std::span<const Core::HID::NpadIdType> supported_npad_list) {
  357. const u64 aruid_index = GetIndexFromAruid(aruid);
  358. if (aruid_index >= AruidIndexMax) {
  359. return ResultNpadNotConnected;
  360. }
  361. if (supported_npad_list.size() > MaxSupportedNpadIdTypes) {
  362. return ResultInvalidArraySize;
  363. }
  364. Result result = state[aruid_index].data.SetSupportedNpadIdType(supported_npad_list);
  365. if (result.IsSuccess() && active_data_aruid == aruid) {
  366. result = active_data.SetSupportedNpadIdType(supported_npad_list);
  367. }
  368. return result;
  369. }
  370. bool NPadResource::IsControllerSupported(u64 aruid, Core::HID::NpadStyleIndex style_index) const {
  371. const u64 aruid_index = GetIndexFromAruid(aruid);
  372. if (aruid_index >= AruidIndexMax) {
  373. return false;
  374. }
  375. return state[aruid_index].data.IsNpadStyleIndexSupported(style_index);
  376. }
  377. Result NPadResource::SetLrAssignmentMode(u64 aruid, bool is_enabled) {
  378. const u64 aruid_index = GetIndexFromAruid(aruid);
  379. if (aruid_index >= AruidIndexMax) {
  380. return ResultNpadNotConnected;
  381. }
  382. state[aruid_index].data.SetLrAssignmentMode(is_enabled);
  383. if (active_data_aruid == aruid) {
  384. active_data.SetLrAssignmentMode(is_enabled);
  385. }
  386. return ResultSuccess;
  387. }
  388. Result NPadResource::GetLrAssignmentMode(bool& is_enabled, u64 aruid) const {
  389. const u64 aruid_index = GetIndexFromAruid(aruid);
  390. if (aruid_index >= AruidIndexMax) {
  391. return ResultNpadNotConnected;
  392. }
  393. is_enabled = state[aruid_index].data.GetLrAssignmentMode();
  394. return ResultSuccess;
  395. }
  396. Result NPadResource::SetAssigningSingleOnSlSrPress(u64 aruid, bool is_enabled) {
  397. const u64 aruid_index = GetIndexFromAruid(aruid);
  398. if (aruid_index >= AruidIndexMax) {
  399. return ResultNpadNotConnected;
  400. }
  401. state[aruid_index].data.SetAssigningSingleOnSlSrPress(is_enabled);
  402. if (active_data_aruid == aruid) {
  403. active_data.SetAssigningSingleOnSlSrPress(is_enabled);
  404. }
  405. return ResultSuccess;
  406. }
  407. Result NPadResource::IsAssigningSingleOnSlSrPressEnabled(bool& is_enabled, u64 aruid) const {
  408. const u64 aruid_index = GetIndexFromAruid(aruid);
  409. if (aruid_index >= AruidIndexMax) {
  410. return ResultNpadNotConnected;
  411. }
  412. is_enabled = state[aruid_index].data.GetAssigningSingleOnSlSrPress();
  413. return ResultSuccess;
  414. }
  415. Result NPadResource::AcquireNpadStyleSetUpdateEventHandle(u64 aruid,
  416. Kernel::KReadableEvent** out_event,
  417. Core::HID::NpadIdType npad_id) {
  418. const u64 aruid_index = GetIndexFromAruid(aruid);
  419. if (aruid_index >= AruidIndexMax) {
  420. return ResultNpadNotConnected;
  421. }
  422. auto& controller_state = state[aruid_index].controller_state[NpadIdTypeToIndex(npad_id)];
  423. if (!controller_state.is_styleset_update_event_initialized) {
  424. // Auto clear = true
  425. controller_state.style_set_update_event =
  426. service_context.CreateEvent("NpadResource:StylesetUpdateEvent");
  427. // Assume creating the event succeeds otherwise crash the system here
  428. controller_state.is_styleset_update_event_initialized = true;
  429. }
  430. *out_event = &controller_state.style_set_update_event->GetReadableEvent();
  431. if (controller_state.is_styleset_update_event_initialized) {
  432. controller_state.style_set_update_event->Signal();
  433. }
  434. return ResultSuccess;
  435. }
  436. Result NPadResource::SignalStyleSetUpdateEvent(u64 aruid, Core::HID::NpadIdType npad_id) {
  437. const u64 aruid_index = GetIndexFromAruid(aruid);
  438. if (aruid_index >= AruidIndexMax) {
  439. return ResultNpadNotConnected;
  440. }
  441. auto controller = state[aruid_index].controller_state[NpadIdTypeToIndex(npad_id)];
  442. if (controller.is_styleset_update_event_initialized) {
  443. controller.style_set_update_event->Signal();
  444. }
  445. return ResultSuccess;
  446. }
  447. Result NPadResource::GetHomeProtectionEnabled(bool& is_enabled, u64 aruid,
  448. Core::HID::NpadIdType npad_id) const {
  449. const u64 aruid_index = GetIndexFromAruid(aruid);
  450. if (aruid_index >= AruidIndexMax) {
  451. return ResultNpadNotConnected;
  452. }
  453. is_enabled = state[aruid_index].data.GetHomeProtectionEnabled(npad_id);
  454. return ResultSuccess;
  455. }
  456. Result NPadResource::SetHomeProtectionEnabled(u64 aruid, Core::HID::NpadIdType npad_id,
  457. bool is_enabled) {
  458. const u64 aruid_index = GetIndexFromAruid(aruid);
  459. if (aruid_index >= AruidIndexMax) {
  460. return ResultNpadNotConnected;
  461. }
  462. state[aruid_index].data.SetHomeProtectionEnabled(is_enabled, npad_id);
  463. if (active_data_aruid == aruid) {
  464. active_data.SetHomeProtectionEnabled(is_enabled, npad_id);
  465. }
  466. return ResultSuccess;
  467. }
  468. Result NPadResource::SetNpadAnalogStickUseCenterClamp(u64 aruid, bool is_enabled) {
  469. const u64 aruid_index = GetIndexFromAruid(aruid);
  470. if (aruid_index >= AruidIndexMax) {
  471. return ResultNpadNotConnected;
  472. }
  473. state[aruid_index].data.SetNpadAnalogStickUseCenterClamp(is_enabled);
  474. if (active_data_aruid == aruid) {
  475. active_data.SetNpadAnalogStickUseCenterClamp(is_enabled);
  476. }
  477. return ResultSuccess;
  478. }
  479. Result NPadResource::SetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id, std::size_t index,
  480. Core::HID::NpadButton button_config) {
  481. const u64 aruid_index = GetIndexFromAruid(aruid);
  482. if (aruid_index >= AruidIndexMax) {
  483. return ResultNpadNotConnected;
  484. }
  485. state[aruid_index].button_config[NpadIdTypeToIndex(npad_id)][index] = button_config;
  486. return ResultSuccess;
  487. }
  488. Core::HID::NpadButton NPadResource::GetButtonConfig(u64 aruid, Core::HID::NpadIdType npad_id,
  489. std::size_t index, Core::HID::NpadButton mask,
  490. bool is_enabled) {
  491. const u64 aruid_index = GetIndexFromAruid(aruid);
  492. if (aruid_index >= AruidIndexMax) {
  493. return Core::HID::NpadButton::None;
  494. }
  495. auto& button_config = state[aruid_index].button_config[NpadIdTypeToIndex(npad_id)][index];
  496. if (is_enabled) {
  497. button_config = button_config | mask;
  498. return button_config;
  499. }
  500. button_config = Core::HID::NpadButton::None;
  501. return Core::HID::NpadButton::None;
  502. }
  503. void NPadResource::ResetButtonConfig() {
  504. for (auto& selected_state : state) {
  505. selected_state.button_config = {};
  506. }
  507. }
  508. Result NPadResource::SetNpadCaptureButtonAssignment(u64 aruid,
  509. Core::HID::NpadStyleSet npad_style_set,
  510. Core::HID::NpadButton button_assignment) {
  511. const u64 aruid_index = GetIndexFromAruid(aruid);
  512. if (aruid_index >= AruidIndexMax) {
  513. return ResultNpadNotConnected;
  514. }
  515. // Must be a power of two
  516. const auto raw_styleset = static_cast<u32>(npad_style_set);
  517. if (raw_styleset == 0 && (raw_styleset & (raw_styleset - 1)) != 0) {
  518. return ResultMultipleStyleSetSelected;
  519. }
  520. std::size_t style_index{};
  521. Core::HID::NpadStyleSet style_selected{};
  522. for (style_index = 0; style_index < StyleIndexCount; ++style_index) {
  523. style_selected = GetStylesetByIndex(style_index);
  524. if (npad_style_set == style_selected) {
  525. break;
  526. }
  527. }
  528. if (style_selected == Core::HID::NpadStyleSet::None) {
  529. return ResultMultipleStyleSetSelected;
  530. }
  531. state[aruid_index].data.SetCaptureButtonAssignment(button_assignment, style_index);
  532. if (active_data_aruid == aruid) {
  533. active_data.SetCaptureButtonAssignment(button_assignment, style_index);
  534. }
  535. return ResultSuccess;
  536. }
  537. Result NPadResource::ClearNpadCaptureButtonAssignment(u64 aruid) {
  538. const u64 aruid_index = GetIndexFromAruid(aruid);
  539. if (aruid_index >= AruidIndexMax) {
  540. return ResultNpadNotConnected;
  541. }
  542. for (std::size_t i = 0; i < StyleIndexCount; i++) {
  543. state[aruid_index].data.SetCaptureButtonAssignment(Core::HID::NpadButton::None, i);
  544. if (active_data_aruid == aruid) {
  545. active_data.SetCaptureButtonAssignment(Core::HID::NpadButton::None, i);
  546. }
  547. }
  548. return ResultSuccess;
  549. }
  550. std::size_t NPadResource::GetNpadCaptureButtonAssignment(std::span<Core::HID::NpadButton> out_list,
  551. u64 aruid) const {
  552. const u64 aruid_index = GetIndexFromAruid(aruid);
  553. if (aruid_index >= AruidIndexMax) {
  554. return 0;
  555. }
  556. return state[aruid_index].data.GetNpadCaptureButtonAssignmentList(out_list);
  557. }
  558. void NPadResource::SetNpadRevision(u64 aruid, NpadRevision revision) {
  559. const u64 aruid_index = GetIndexFromAruid(aruid);
  560. if (aruid_index >= AruidIndexMax) {
  561. return;
  562. }
  563. state[aruid_index].npad_revision = revision;
  564. }
  565. Result NPadResource::SetNpadSystemExtStateEnabled(u64 aruid, bool is_enabled) {
  566. const u64 aruid_index = GetIndexFromAruid(aruid);
  567. if (aruid_index >= AruidIndexMax) {
  568. return ResultNpadNotConnected;
  569. }
  570. state[aruid_index].data.SetNpadAnalogStickUseCenterClamp(is_enabled);
  571. if (active_data_aruid == aruid) {
  572. active_data.SetNpadAnalogStickUseCenterClamp(is_enabled);
  573. }
  574. return ResultSuccess;
  575. }
  576. } // namespace Service::HID