npad.cpp 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include <chrono>
  6. #include <cstring>
  7. #include "common/assert.h"
  8. #include "common/bit_field.h"
  9. #include "common/common_types.h"
  10. #include "common/logging/log.h"
  11. #include "common/settings.h"
  12. #include "core/core_timing.h"
  13. #include "core/hid/emulated_controller.h"
  14. #include "core/hid/hid_core.h"
  15. #include "core/hle/kernel/k_event.h"
  16. #include "core/hle/kernel/k_readable_event.h"
  17. #include "core/hle/service/hid/controllers/npad.h"
  18. #include "core/hle/service/hid/errors.h"
  19. #include "core/hle/service/kernel_helpers.h"
  20. namespace Service::HID {
  21. constexpr std::size_t NPAD_OFFSET = 0x9A00;
  22. constexpr std::array<Core::HID::NpadIdType, 10> npad_id_list{
  23. Core::HID::NpadIdType::Player1, Core::HID::NpadIdType::Player2, Core::HID::NpadIdType::Player3,
  24. Core::HID::NpadIdType::Player4, Core::HID::NpadIdType::Player5, Core::HID::NpadIdType::Player6,
  25. Core::HID::NpadIdType::Player7, Core::HID::NpadIdType::Player8, Core::HID::NpadIdType::Other,
  26. Core::HID::NpadIdType::Handheld,
  27. };
  28. bool Controller_NPad::IsNpadIdValid(Core::HID::NpadIdType npad_id) {
  29. switch (npad_id) {
  30. case Core::HID::NpadIdType::Player1:
  31. case Core::HID::NpadIdType::Player2:
  32. case Core::HID::NpadIdType::Player3:
  33. case Core::HID::NpadIdType::Player4:
  34. case Core::HID::NpadIdType::Player5:
  35. case Core::HID::NpadIdType::Player6:
  36. case Core::HID::NpadIdType::Player7:
  37. case Core::HID::NpadIdType::Player8:
  38. case Core::HID::NpadIdType::Other:
  39. case Core::HID::NpadIdType::Handheld:
  40. return true;
  41. default:
  42. LOG_ERROR(Service_HID, "Invalid npad id {}", npad_id);
  43. return false;
  44. }
  45. }
  46. Result Controller_NPad::IsDeviceHandleValid(const Core::HID::VibrationDeviceHandle& device_handle) {
  47. const auto npad_id = IsNpadIdValid(static_cast<Core::HID::NpadIdType>(device_handle.npad_id));
  48. const bool npad_type = device_handle.npad_type < Core::HID::NpadStyleIndex::MaxNpadType;
  49. const bool device_index = device_handle.device_index < Core::HID::DeviceIndex::MaxDeviceIndex;
  50. if (!npad_type) {
  51. return VibrationInvalidStyleIndex;
  52. }
  53. if (!npad_id) {
  54. return VibrationInvalidNpadId;
  55. }
  56. if (!device_index) {
  57. return VibrationDeviceIndexOutOfRange;
  58. }
  59. return ResultSuccess;
  60. }
  61. Result Controller_NPad::VerifyValidSixAxisSensorHandle(
  62. const Core::HID::SixAxisSensorHandle& device_handle) {
  63. const auto npad_id = IsNpadIdValid(static_cast<Core::HID::NpadIdType>(device_handle.npad_id));
  64. const bool device_index = device_handle.device_index < Core::HID::DeviceIndex::MaxDeviceIndex;
  65. const bool npad_type = device_handle.npad_type < Core::HID::NpadStyleIndex::MaxNpadType;
  66. if (!npad_id) {
  67. return InvalidNpadId;
  68. }
  69. if (!device_index) {
  70. return NpadDeviceIndexOutOfRange;
  71. }
  72. // This doesn't get validated on nnsdk
  73. if (!npad_type) {
  74. return NpadInvalidHandle;
  75. }
  76. return ResultSuccess;
  77. }
  78. Controller_NPad::Controller_NPad(Core::HID::HIDCore& hid_core_, u8* raw_shared_memory_,
  79. KernelHelpers::ServiceContext& service_context_)
  80. : ControllerBase{hid_core_}, service_context{service_context_} {
  81. static_assert(NPAD_OFFSET + (NPAD_COUNT * sizeof(NpadInternalState)) < shared_memory_size);
  82. for (std::size_t i = 0; i < controller_data.size(); ++i) {
  83. auto& controller = controller_data[i];
  84. controller.shared_memory = std::construct_at(reinterpret_cast<NpadInternalState*>(
  85. raw_shared_memory_ + NPAD_OFFSET + (i * sizeof(NpadInternalState))));
  86. controller.device = hid_core.GetEmulatedControllerByIndex(i);
  87. controller.vibration[Core::HID::EmulatedDeviceIndex::LeftIndex].latest_vibration_value =
  88. Core::HID::DEFAULT_VIBRATION_VALUE;
  89. controller.vibration[Core::HID::EmulatedDeviceIndex::RightIndex].latest_vibration_value =
  90. Core::HID::DEFAULT_VIBRATION_VALUE;
  91. Core::HID::ControllerUpdateCallback engine_callback{
  92. .on_change = [this,
  93. i](Core::HID::ControllerTriggerType type) { ControllerUpdate(type, i); },
  94. .is_npad_service = true,
  95. };
  96. controller.callback_key = controller.device->SetCallback(engine_callback);
  97. }
  98. }
  99. Controller_NPad::~Controller_NPad() {
  100. for (std::size_t i = 0; i < controller_data.size(); ++i) {
  101. auto& controller = controller_data[i];
  102. controller.device->DeleteCallback(controller.callback_key);
  103. }
  104. OnRelease();
  105. }
  106. void Controller_NPad::ControllerUpdate(Core::HID::ControllerTriggerType type,
  107. std::size_t controller_idx) {
  108. if (type == Core::HID::ControllerTriggerType::All) {
  109. ControllerUpdate(Core::HID::ControllerTriggerType::Connected, controller_idx);
  110. ControllerUpdate(Core::HID::ControllerTriggerType::Battery, controller_idx);
  111. return;
  112. }
  113. if (controller_idx >= controller_data.size()) {
  114. return;
  115. }
  116. auto& controller = controller_data[controller_idx];
  117. const auto is_connected = controller.device->IsConnected();
  118. const auto npad_type = controller.device->GetNpadStyleIndex();
  119. const auto npad_id = controller.device->GetNpadIdType();
  120. switch (type) {
  121. case Core::HID::ControllerTriggerType::Connected:
  122. case Core::HID::ControllerTriggerType::Disconnected:
  123. if (is_connected == controller.is_connected) {
  124. return;
  125. }
  126. UpdateControllerAt(npad_type, npad_id, is_connected);
  127. break;
  128. case Core::HID::ControllerTriggerType::Battery: {
  129. if (!controller.device->IsConnected()) {
  130. return;
  131. }
  132. auto* shared_memory = controller.shared_memory;
  133. const auto& battery_level = controller.device->GetBattery();
  134. shared_memory->battery_level_dual = battery_level.dual.battery_level;
  135. shared_memory->battery_level_left = battery_level.left.battery_level;
  136. shared_memory->battery_level_right = battery_level.right.battery_level;
  137. break;
  138. }
  139. default:
  140. break;
  141. }
  142. }
  143. void Controller_NPad::InitNewlyAddedController(Core::HID::NpadIdType npad_id) {
  144. auto& controller = GetControllerFromNpadIdType(npad_id);
  145. if (!IsControllerSupported(controller.device->GetNpadStyleIndex())) {
  146. return;
  147. }
  148. LOG_DEBUG(Service_HID, "Npad connected {}", npad_id);
  149. const auto controller_type = controller.device->GetNpadStyleIndex();
  150. const auto& body_colors = controller.device->GetColors();
  151. const auto& battery_level = controller.device->GetBattery();
  152. auto* shared_memory = controller.shared_memory;
  153. if (controller_type == Core::HID::NpadStyleIndex::None) {
  154. controller.styleset_changed_event->Signal();
  155. return;
  156. }
  157. // Reset memory values
  158. shared_memory->style_tag.raw = Core::HID::NpadStyleSet::None;
  159. shared_memory->device_type.raw = 0;
  160. shared_memory->system_properties.raw = 0;
  161. shared_memory->joycon_color.attribute = ColorAttribute::NoController;
  162. shared_memory->joycon_color.attribute = ColorAttribute::NoController;
  163. shared_memory->fullkey_color = {};
  164. shared_memory->joycon_color.left = {};
  165. shared_memory->joycon_color.right = {};
  166. shared_memory->battery_level_dual = {};
  167. shared_memory->battery_level_left = {};
  168. shared_memory->battery_level_right = {};
  169. switch (controller_type) {
  170. case Core::HID::NpadStyleIndex::None:
  171. ASSERT(false);
  172. break;
  173. case Core::HID::NpadStyleIndex::ProController:
  174. shared_memory->fullkey_color.attribute = ColorAttribute::Ok;
  175. shared_memory->fullkey_color.fullkey = body_colors.fullkey;
  176. shared_memory->battery_level_dual = battery_level.dual.battery_level;
  177. shared_memory->style_tag.fullkey.Assign(1);
  178. shared_memory->device_type.fullkey.Assign(1);
  179. shared_memory->system_properties.is_vertical.Assign(1);
  180. shared_memory->system_properties.use_plus.Assign(1);
  181. shared_memory->system_properties.use_minus.Assign(1);
  182. shared_memory->system_properties.is_charging_joy_dual.Assign(
  183. battery_level.dual.is_charging);
  184. shared_memory->applet_nfc_xcd.applet_footer.type = AppletFooterUiType::SwitchProController;
  185. shared_memory->sixaxis_fullkey_properties.is_newly_assigned.Assign(1);
  186. break;
  187. case Core::HID::NpadStyleIndex::Handheld:
  188. shared_memory->fullkey_color.attribute = ColorAttribute::Ok;
  189. shared_memory->joycon_color.attribute = ColorAttribute::Ok;
  190. shared_memory->fullkey_color.fullkey = body_colors.fullkey;
  191. shared_memory->joycon_color.left = body_colors.left;
  192. shared_memory->joycon_color.right = body_colors.right;
  193. shared_memory->style_tag.handheld.Assign(1);
  194. shared_memory->device_type.handheld_left.Assign(1);
  195. shared_memory->device_type.handheld_right.Assign(1);
  196. shared_memory->system_properties.is_vertical.Assign(1);
  197. shared_memory->system_properties.use_plus.Assign(1);
  198. shared_memory->system_properties.use_minus.Assign(1);
  199. shared_memory->system_properties.use_directional_buttons.Assign(1);
  200. shared_memory->system_properties.is_charging_joy_dual.Assign(
  201. battery_level.left.is_charging);
  202. shared_memory->system_properties.is_charging_joy_left.Assign(
  203. battery_level.left.is_charging);
  204. shared_memory->system_properties.is_charging_joy_right.Assign(
  205. battery_level.right.is_charging);
  206. shared_memory->assignment_mode = NpadJoyAssignmentMode::Dual;
  207. shared_memory->applet_nfc_xcd.applet_footer.type =
  208. AppletFooterUiType::HandheldJoyConLeftJoyConRight;
  209. shared_memory->sixaxis_handheld_properties.is_newly_assigned.Assign(1);
  210. break;
  211. case Core::HID::NpadStyleIndex::JoyconDual:
  212. shared_memory->fullkey_color.attribute = ColorAttribute::Ok;
  213. shared_memory->joycon_color.attribute = ColorAttribute::Ok;
  214. shared_memory->style_tag.joycon_dual.Assign(1);
  215. if (controller.is_dual_left_connected) {
  216. shared_memory->joycon_color.left = body_colors.left;
  217. shared_memory->battery_level_left = battery_level.left.battery_level;
  218. shared_memory->device_type.joycon_left.Assign(1);
  219. shared_memory->system_properties.use_minus.Assign(1);
  220. shared_memory->system_properties.is_charging_joy_left.Assign(
  221. battery_level.left.is_charging);
  222. shared_memory->sixaxis_dual_left_properties.is_newly_assigned.Assign(1);
  223. }
  224. if (controller.is_dual_right_connected) {
  225. shared_memory->joycon_color.right = body_colors.right;
  226. shared_memory->battery_level_right = battery_level.right.battery_level;
  227. shared_memory->device_type.joycon_right.Assign(1);
  228. shared_memory->system_properties.use_plus.Assign(1);
  229. shared_memory->system_properties.is_charging_joy_right.Assign(
  230. battery_level.right.is_charging);
  231. shared_memory->sixaxis_dual_right_properties.is_newly_assigned.Assign(1);
  232. }
  233. shared_memory->system_properties.use_directional_buttons.Assign(1);
  234. shared_memory->system_properties.is_vertical.Assign(1);
  235. shared_memory->assignment_mode = NpadJoyAssignmentMode::Dual;
  236. if (controller.is_dual_left_connected && controller.is_dual_right_connected) {
  237. shared_memory->applet_nfc_xcd.applet_footer.type = AppletFooterUiType::JoyDual;
  238. shared_memory->fullkey_color.fullkey = body_colors.left;
  239. shared_memory->battery_level_dual = battery_level.left.battery_level;
  240. shared_memory->system_properties.is_charging_joy_dual.Assign(
  241. battery_level.left.is_charging);
  242. } else if (controller.is_dual_left_connected) {
  243. shared_memory->applet_nfc_xcd.applet_footer.type = AppletFooterUiType::JoyDualLeftOnly;
  244. shared_memory->fullkey_color.fullkey = body_colors.left;
  245. shared_memory->battery_level_dual = battery_level.left.battery_level;
  246. shared_memory->system_properties.is_charging_joy_dual.Assign(
  247. battery_level.left.is_charging);
  248. } else {
  249. shared_memory->applet_nfc_xcd.applet_footer.type = AppletFooterUiType::JoyDualRightOnly;
  250. shared_memory->fullkey_color.fullkey = body_colors.right;
  251. shared_memory->battery_level_dual = battery_level.right.battery_level;
  252. shared_memory->system_properties.is_charging_joy_dual.Assign(
  253. battery_level.right.is_charging);
  254. }
  255. break;
  256. case Core::HID::NpadStyleIndex::JoyconLeft:
  257. shared_memory->joycon_color.attribute = ColorAttribute::Ok;
  258. shared_memory->joycon_color.left = body_colors.left;
  259. shared_memory->battery_level_dual = battery_level.left.battery_level;
  260. shared_memory->style_tag.joycon_left.Assign(1);
  261. shared_memory->device_type.joycon_left.Assign(1);
  262. shared_memory->system_properties.is_horizontal.Assign(1);
  263. shared_memory->system_properties.use_minus.Assign(1);
  264. shared_memory->system_properties.is_charging_joy_left.Assign(
  265. battery_level.left.is_charging);
  266. shared_memory->applet_nfc_xcd.applet_footer.type = AppletFooterUiType::JoyLeftHorizontal;
  267. shared_memory->sixaxis_left_properties.is_newly_assigned.Assign(1);
  268. break;
  269. case Core::HID::NpadStyleIndex::JoyconRight:
  270. shared_memory->joycon_color.attribute = ColorAttribute::Ok;
  271. shared_memory->joycon_color.right = body_colors.right;
  272. shared_memory->battery_level_right = battery_level.right.battery_level;
  273. shared_memory->style_tag.joycon_right.Assign(1);
  274. shared_memory->device_type.joycon_right.Assign(1);
  275. shared_memory->system_properties.is_horizontal.Assign(1);
  276. shared_memory->system_properties.use_plus.Assign(1);
  277. shared_memory->system_properties.is_charging_joy_right.Assign(
  278. battery_level.right.is_charging);
  279. shared_memory->applet_nfc_xcd.applet_footer.type = AppletFooterUiType::JoyRightHorizontal;
  280. shared_memory->sixaxis_right_properties.is_newly_assigned.Assign(1);
  281. break;
  282. case Core::HID::NpadStyleIndex::GameCube:
  283. shared_memory->style_tag.gamecube.Assign(1);
  284. shared_memory->device_type.fullkey.Assign(1);
  285. shared_memory->system_properties.is_vertical.Assign(1);
  286. shared_memory->system_properties.use_plus.Assign(1);
  287. break;
  288. case Core::HID::NpadStyleIndex::Pokeball:
  289. shared_memory->style_tag.palma.Assign(1);
  290. shared_memory->device_type.palma.Assign(1);
  291. shared_memory->sixaxis_fullkey_properties.is_newly_assigned.Assign(1);
  292. break;
  293. case Core::HID::NpadStyleIndex::NES:
  294. shared_memory->style_tag.lark.Assign(1);
  295. shared_memory->device_type.fullkey.Assign(1);
  296. break;
  297. case Core::HID::NpadStyleIndex::SNES:
  298. shared_memory->style_tag.lucia.Assign(1);
  299. shared_memory->device_type.fullkey.Assign(1);
  300. shared_memory->applet_nfc_xcd.applet_footer.type = AppletFooterUiType::Lucia;
  301. break;
  302. case Core::HID::NpadStyleIndex::N64:
  303. shared_memory->style_tag.lagoon.Assign(1);
  304. shared_memory->device_type.fullkey.Assign(1);
  305. shared_memory->applet_nfc_xcd.applet_footer.type = AppletFooterUiType::Lagon;
  306. break;
  307. case Core::HID::NpadStyleIndex::SegaGenesis:
  308. shared_memory->style_tag.lager.Assign(1);
  309. shared_memory->device_type.fullkey.Assign(1);
  310. break;
  311. default:
  312. break;
  313. }
  314. controller.is_connected = true;
  315. controller.device->Connect();
  316. SignalStyleSetChangedEvent(npad_id);
  317. WriteEmptyEntry(controller.shared_memory);
  318. }
  319. void Controller_NPad::OnInit() {
  320. if (!IsControllerActivated()) {
  321. return;
  322. }
  323. for (std::size_t i = 0; i < controller_data.size(); ++i) {
  324. auto& controller = controller_data[i];
  325. controller.styleset_changed_event =
  326. service_context.CreateEvent(fmt::format("npad:NpadStyleSetChanged_{}", i));
  327. }
  328. supported_npad_id_types.resize(npad_id_list.size());
  329. std::memcpy(supported_npad_id_types.data(), npad_id_list.data(),
  330. npad_id_list.size() * sizeof(Core::HID::NpadIdType));
  331. // Prefill controller buffers
  332. for (auto& controller : controller_data) {
  333. auto* npad = controller.shared_memory;
  334. npad->fullkey_color = {
  335. .attribute = ColorAttribute::NoController,
  336. .fullkey = {},
  337. };
  338. npad->joycon_color = {
  339. .attribute = ColorAttribute::NoController,
  340. .left = {},
  341. .right = {},
  342. };
  343. // HW seems to initialize the first 19 entries
  344. for (std::size_t i = 0; i < 19; ++i) {
  345. WriteEmptyEntry(npad);
  346. }
  347. }
  348. }
  349. void Controller_NPad::WriteEmptyEntry(NpadInternalState* npad) {
  350. NPadGenericState dummy_pad_state{};
  351. NpadGcTriggerState dummy_gc_state{};
  352. dummy_pad_state.sampling_number = npad->fullkey_lifo.ReadCurrentEntry().sampling_number + 1;
  353. npad->fullkey_lifo.WriteNextEntry(dummy_pad_state);
  354. dummy_pad_state.sampling_number = npad->handheld_lifo.ReadCurrentEntry().sampling_number + 1;
  355. npad->handheld_lifo.WriteNextEntry(dummy_pad_state);
  356. dummy_pad_state.sampling_number = npad->joy_dual_lifo.ReadCurrentEntry().sampling_number + 1;
  357. npad->joy_dual_lifo.WriteNextEntry(dummy_pad_state);
  358. dummy_pad_state.sampling_number = npad->joy_left_lifo.ReadCurrentEntry().sampling_number + 1;
  359. npad->joy_left_lifo.WriteNextEntry(dummy_pad_state);
  360. dummy_pad_state.sampling_number = npad->joy_right_lifo.ReadCurrentEntry().sampling_number + 1;
  361. npad->joy_right_lifo.WriteNextEntry(dummy_pad_state);
  362. dummy_pad_state.sampling_number = npad->palma_lifo.ReadCurrentEntry().sampling_number + 1;
  363. npad->palma_lifo.WriteNextEntry(dummy_pad_state);
  364. dummy_pad_state.sampling_number = npad->system_ext_lifo.ReadCurrentEntry().sampling_number + 1;
  365. npad->system_ext_lifo.WriteNextEntry(dummy_pad_state);
  366. dummy_gc_state.sampling_number = npad->gc_trigger_lifo.ReadCurrentEntry().sampling_number + 1;
  367. npad->gc_trigger_lifo.WriteNextEntry(dummy_gc_state);
  368. }
  369. void Controller_NPad::OnRelease() {
  370. is_controller_initialized = false;
  371. for (std::size_t i = 0; i < controller_data.size(); ++i) {
  372. auto& controller = controller_data[i];
  373. service_context.CloseEvent(controller.styleset_changed_event);
  374. for (std::size_t device_idx = 0; device_idx < controller.vibration.size(); ++device_idx) {
  375. VibrateControllerAtIndex(controller.device->GetNpadIdType(), device_idx, {});
  376. }
  377. }
  378. }
  379. void Controller_NPad::RequestPadStateUpdate(Core::HID::NpadIdType npad_id) {
  380. std::scoped_lock lock{mutex};
  381. auto& controller = GetControllerFromNpadIdType(npad_id);
  382. const auto controller_type = controller.device->GetNpadStyleIndex();
  383. if (!controller.device->IsConnected()) {
  384. return;
  385. }
  386. auto& pad_entry = controller.npad_pad_state;
  387. auto& trigger_entry = controller.npad_trigger_state;
  388. const auto button_state = controller.device->GetNpadButtons();
  389. const auto stick_state = controller.device->GetSticks();
  390. using btn = Core::HID::NpadButton;
  391. pad_entry.npad_buttons.raw = btn::None;
  392. if (controller_type != Core::HID::NpadStyleIndex::JoyconLeft) {
  393. constexpr btn right_button_mask = btn::A | btn::B | btn::X | btn::Y | btn::StickR | btn::R |
  394. btn::ZR | btn::Plus | btn::StickRLeft | btn::StickRUp |
  395. btn::StickRRight | btn::StickRDown;
  396. pad_entry.npad_buttons.raw = button_state.raw & right_button_mask;
  397. pad_entry.r_stick = stick_state.right;
  398. }
  399. if (controller_type != Core::HID::NpadStyleIndex::JoyconRight) {
  400. constexpr btn left_button_mask =
  401. btn::Left | btn::Up | btn::Right | btn::Down | btn::StickL | btn::L | btn::ZL |
  402. btn::Minus | btn::StickLLeft | btn::StickLUp | btn::StickLRight | btn::StickLDown;
  403. pad_entry.npad_buttons.raw |= button_state.raw & left_button_mask;
  404. pad_entry.l_stick = stick_state.left;
  405. }
  406. if (controller_type == Core::HID::NpadStyleIndex::JoyconLeft) {
  407. pad_entry.npad_buttons.left_sl.Assign(button_state.left_sl);
  408. pad_entry.npad_buttons.left_sr.Assign(button_state.left_sr);
  409. }
  410. if (controller_type == Core::HID::NpadStyleIndex::JoyconRight) {
  411. pad_entry.npad_buttons.right_sl.Assign(button_state.right_sl);
  412. pad_entry.npad_buttons.right_sr.Assign(button_state.right_sr);
  413. }
  414. if (controller_type == Core::HID::NpadStyleIndex::GameCube) {
  415. const auto& trigger_state = controller.device->GetTriggers();
  416. trigger_entry.l_analog = trigger_state.left;
  417. trigger_entry.r_analog = trigger_state.right;
  418. pad_entry.npad_buttons.zl.Assign(false);
  419. pad_entry.npad_buttons.zr.Assign(button_state.r);
  420. pad_entry.npad_buttons.l.Assign(button_state.zl);
  421. pad_entry.npad_buttons.r.Assign(button_state.zr);
  422. }
  423. }
  424. void Controller_NPad::OnUpdate(const Core::Timing::CoreTiming& core_timing) {
  425. if (!IsControllerActivated()) {
  426. return;
  427. }
  428. for (std::size_t i = 0; i < controller_data.size(); ++i) {
  429. auto& controller = controller_data[i];
  430. auto* npad = controller.shared_memory;
  431. const auto& controller_type = controller.device->GetNpadStyleIndex();
  432. if (controller_type == Core::HID::NpadStyleIndex::None ||
  433. !controller.device->IsConnected()) {
  434. continue;
  435. }
  436. RequestPadStateUpdate(controller.device->GetNpadIdType());
  437. auto& pad_state = controller.npad_pad_state;
  438. auto& libnx_state = controller.npad_libnx_state;
  439. auto& trigger_state = controller.npad_trigger_state;
  440. // LibNX exclusively uses this section, so we always update it since LibNX doesn't activate
  441. // any controllers.
  442. libnx_state.connection_status.raw = 0;
  443. libnx_state.connection_status.is_connected.Assign(1);
  444. switch (controller_type) {
  445. case Core::HID::NpadStyleIndex::None:
  446. ASSERT(false);
  447. break;
  448. case Core::HID::NpadStyleIndex::ProController:
  449. case Core::HID::NpadStyleIndex::NES:
  450. case Core::HID::NpadStyleIndex::SNES:
  451. case Core::HID::NpadStyleIndex::N64:
  452. case Core::HID::NpadStyleIndex::SegaGenesis:
  453. pad_state.connection_status.raw = 0;
  454. pad_state.connection_status.is_connected.Assign(1);
  455. pad_state.connection_status.is_wired.Assign(1);
  456. libnx_state.connection_status.is_wired.Assign(1);
  457. pad_state.sampling_number =
  458. npad->fullkey_lifo.ReadCurrentEntry().state.sampling_number + 1;
  459. npad->fullkey_lifo.WriteNextEntry(pad_state);
  460. break;
  461. case Core::HID::NpadStyleIndex::Handheld:
  462. pad_state.connection_status.raw = 0;
  463. pad_state.connection_status.is_connected.Assign(1);
  464. pad_state.connection_status.is_wired.Assign(1);
  465. pad_state.connection_status.is_left_connected.Assign(1);
  466. pad_state.connection_status.is_right_connected.Assign(1);
  467. pad_state.connection_status.is_left_wired.Assign(1);
  468. pad_state.connection_status.is_right_wired.Assign(1);
  469. libnx_state.connection_status.is_wired.Assign(1);
  470. libnx_state.connection_status.is_left_connected.Assign(1);
  471. libnx_state.connection_status.is_right_connected.Assign(1);
  472. libnx_state.connection_status.is_left_wired.Assign(1);
  473. libnx_state.connection_status.is_right_wired.Assign(1);
  474. pad_state.sampling_number =
  475. npad->handheld_lifo.ReadCurrentEntry().state.sampling_number + 1;
  476. npad->handheld_lifo.WriteNextEntry(pad_state);
  477. break;
  478. case Core::HID::NpadStyleIndex::JoyconDual:
  479. pad_state.connection_status.raw = 0;
  480. pad_state.connection_status.is_connected.Assign(1);
  481. if (controller.is_dual_left_connected) {
  482. pad_state.connection_status.is_left_connected.Assign(1);
  483. libnx_state.connection_status.is_left_connected.Assign(1);
  484. }
  485. if (controller.is_dual_right_connected) {
  486. pad_state.connection_status.is_right_connected.Assign(1);
  487. libnx_state.connection_status.is_right_connected.Assign(1);
  488. }
  489. pad_state.sampling_number =
  490. npad->joy_dual_lifo.ReadCurrentEntry().state.sampling_number + 1;
  491. npad->joy_dual_lifo.WriteNextEntry(pad_state);
  492. break;
  493. case Core::HID::NpadStyleIndex::JoyconLeft:
  494. pad_state.connection_status.raw = 0;
  495. pad_state.connection_status.is_connected.Assign(1);
  496. pad_state.connection_status.is_left_connected.Assign(1);
  497. libnx_state.connection_status.is_left_connected.Assign(1);
  498. pad_state.sampling_number =
  499. npad->joy_left_lifo.ReadCurrentEntry().state.sampling_number + 1;
  500. npad->joy_left_lifo.WriteNextEntry(pad_state);
  501. break;
  502. case Core::HID::NpadStyleIndex::JoyconRight:
  503. pad_state.connection_status.raw = 0;
  504. pad_state.connection_status.is_connected.Assign(1);
  505. pad_state.connection_status.is_right_connected.Assign(1);
  506. libnx_state.connection_status.is_right_connected.Assign(1);
  507. pad_state.sampling_number =
  508. npad->joy_right_lifo.ReadCurrentEntry().state.sampling_number + 1;
  509. npad->joy_right_lifo.WriteNextEntry(pad_state);
  510. break;
  511. case Core::HID::NpadStyleIndex::GameCube:
  512. pad_state.connection_status.raw = 0;
  513. pad_state.connection_status.is_connected.Assign(1);
  514. pad_state.connection_status.is_wired.Assign(1);
  515. libnx_state.connection_status.is_wired.Assign(1);
  516. pad_state.sampling_number =
  517. npad->fullkey_lifo.ReadCurrentEntry().state.sampling_number + 1;
  518. trigger_state.sampling_number =
  519. npad->gc_trigger_lifo.ReadCurrentEntry().state.sampling_number + 1;
  520. npad->fullkey_lifo.WriteNextEntry(pad_state);
  521. npad->gc_trigger_lifo.WriteNextEntry(trigger_state);
  522. break;
  523. case Core::HID::NpadStyleIndex::Pokeball:
  524. pad_state.connection_status.raw = 0;
  525. pad_state.connection_status.is_connected.Assign(1);
  526. pad_state.sampling_number =
  527. npad->palma_lifo.ReadCurrentEntry().state.sampling_number + 1;
  528. npad->palma_lifo.WriteNextEntry(pad_state);
  529. break;
  530. default:
  531. break;
  532. }
  533. libnx_state.npad_buttons.raw = pad_state.npad_buttons.raw;
  534. libnx_state.l_stick = pad_state.l_stick;
  535. libnx_state.r_stick = pad_state.r_stick;
  536. npad->system_ext_lifo.WriteNextEntry(pad_state);
  537. press_state |= static_cast<u64>(pad_state.npad_buttons.raw);
  538. }
  539. }
  540. void Controller_NPad::OnMotionUpdate(const Core::Timing::CoreTiming& core_timing) {
  541. if (!IsControllerActivated()) {
  542. return;
  543. }
  544. for (std::size_t i = 0; i < controller_data.size(); ++i) {
  545. auto& controller = controller_data[i];
  546. const auto& controller_type = controller.device->GetNpadStyleIndex();
  547. if (controller_type == Core::HID::NpadStyleIndex::None ||
  548. !controller.device->IsConnected()) {
  549. continue;
  550. }
  551. auto* npad = controller.shared_memory;
  552. const auto& motion_state = controller.device->GetMotions();
  553. auto& sixaxis_fullkey_state = controller.sixaxis_fullkey_state;
  554. auto& sixaxis_handheld_state = controller.sixaxis_handheld_state;
  555. auto& sixaxis_dual_left_state = controller.sixaxis_dual_left_state;
  556. auto& sixaxis_dual_right_state = controller.sixaxis_dual_right_state;
  557. auto& sixaxis_left_lifo_state = controller.sixaxis_left_lifo_state;
  558. auto& sixaxis_right_lifo_state = controller.sixaxis_right_lifo_state;
  559. // Clear previous state
  560. sixaxis_fullkey_state = {};
  561. sixaxis_handheld_state = {};
  562. sixaxis_dual_left_state = {};
  563. sixaxis_dual_right_state = {};
  564. sixaxis_left_lifo_state = {};
  565. sixaxis_right_lifo_state = {};
  566. if (controller.sixaxis_sensor_enabled && Settings::values.motion_enabled.GetValue()) {
  567. controller.sixaxis_at_rest = true;
  568. for (std::size_t e = 0; e < motion_state.size(); ++e) {
  569. controller.sixaxis_at_rest =
  570. controller.sixaxis_at_rest && motion_state[e].is_at_rest;
  571. }
  572. }
  573. const auto set_motion_state = [&](SixAxisSensorState& state,
  574. const Core::HID::ControllerMotion& hid_state) {
  575. using namespace std::literals::chrono_literals;
  576. static constexpr SixAxisSensorState default_motion_state = {
  577. .delta_time = std::chrono::nanoseconds(5ms).count(),
  578. .accel = {0, 0, -1.0f},
  579. .orientation =
  580. {
  581. Common::Vec3f{1.0f, 0, 0},
  582. Common::Vec3f{0, 1.0f, 0},
  583. Common::Vec3f{0, 0, 1.0f},
  584. },
  585. .attribute = {1},
  586. };
  587. if (!controller.sixaxis_sensor_enabled) {
  588. state = default_motion_state;
  589. return;
  590. }
  591. if (!Settings::values.motion_enabled.GetValue()) {
  592. state = default_motion_state;
  593. return;
  594. }
  595. state.attribute.is_connected.Assign(1);
  596. state.delta_time = std::chrono::nanoseconds(5ms).count();
  597. state.accel = hid_state.accel;
  598. state.gyro = hid_state.gyro;
  599. state.rotation = hid_state.rotation;
  600. state.orientation = hid_state.orientation;
  601. };
  602. switch (controller_type) {
  603. case Core::HID::NpadStyleIndex::None:
  604. ASSERT(false);
  605. break;
  606. case Core::HID::NpadStyleIndex::ProController:
  607. set_motion_state(sixaxis_fullkey_state, motion_state[0]);
  608. break;
  609. case Core::HID::NpadStyleIndex::Handheld:
  610. set_motion_state(sixaxis_handheld_state, motion_state[0]);
  611. break;
  612. case Core::HID::NpadStyleIndex::JoyconDual:
  613. set_motion_state(sixaxis_dual_left_state, motion_state[0]);
  614. set_motion_state(sixaxis_dual_right_state, motion_state[1]);
  615. break;
  616. case Core::HID::NpadStyleIndex::JoyconLeft:
  617. set_motion_state(sixaxis_left_lifo_state, motion_state[0]);
  618. break;
  619. case Core::HID::NpadStyleIndex::JoyconRight:
  620. set_motion_state(sixaxis_right_lifo_state, motion_state[1]);
  621. break;
  622. case Core::HID::NpadStyleIndex::Pokeball:
  623. using namespace std::literals::chrono_literals;
  624. set_motion_state(sixaxis_fullkey_state, motion_state[0]);
  625. sixaxis_fullkey_state.delta_time = std::chrono::nanoseconds(15ms).count();
  626. break;
  627. default:
  628. break;
  629. }
  630. sixaxis_fullkey_state.sampling_number =
  631. npad->sixaxis_fullkey_lifo.ReadCurrentEntry().state.sampling_number + 1;
  632. sixaxis_handheld_state.sampling_number =
  633. npad->sixaxis_handheld_lifo.ReadCurrentEntry().state.sampling_number + 1;
  634. sixaxis_dual_left_state.sampling_number =
  635. npad->sixaxis_dual_left_lifo.ReadCurrentEntry().state.sampling_number + 1;
  636. sixaxis_dual_right_state.sampling_number =
  637. npad->sixaxis_dual_right_lifo.ReadCurrentEntry().state.sampling_number + 1;
  638. sixaxis_left_lifo_state.sampling_number =
  639. npad->sixaxis_left_lifo.ReadCurrentEntry().state.sampling_number + 1;
  640. sixaxis_right_lifo_state.sampling_number =
  641. npad->sixaxis_right_lifo.ReadCurrentEntry().state.sampling_number + 1;
  642. if (Core::HID::IndexToNpadIdType(i) == Core::HID::NpadIdType::Handheld) {
  643. // This buffer only is updated on handheld on HW
  644. npad->sixaxis_handheld_lifo.WriteNextEntry(sixaxis_handheld_state);
  645. } else {
  646. // Handheld doesn't update this buffer on HW
  647. npad->sixaxis_fullkey_lifo.WriteNextEntry(sixaxis_fullkey_state);
  648. }
  649. npad->sixaxis_dual_left_lifo.WriteNextEntry(sixaxis_dual_left_state);
  650. npad->sixaxis_dual_right_lifo.WriteNextEntry(sixaxis_dual_right_state);
  651. npad->sixaxis_left_lifo.WriteNextEntry(sixaxis_left_lifo_state);
  652. npad->sixaxis_right_lifo.WriteNextEntry(sixaxis_right_lifo_state);
  653. }
  654. }
  655. void Controller_NPad::SetSupportedStyleSet(Core::HID::NpadStyleTag style_set) {
  656. hid_core.SetSupportedStyleTag(style_set);
  657. if (is_controller_initialized) {
  658. return;
  659. }
  660. // Once SetSupportedStyleSet is called controllers are fully initialized
  661. is_controller_initialized = true;
  662. // Connect all active controllers
  663. for (auto& controller : controller_data) {
  664. const auto& device = controller.device;
  665. if (device->IsConnected()) {
  666. AddNewControllerAt(device->GetNpadStyleIndex(), device->GetNpadIdType());
  667. }
  668. }
  669. }
  670. Core::HID::NpadStyleTag Controller_NPad::GetSupportedStyleSet() const {
  671. if (!is_controller_initialized) {
  672. return {Core::HID::NpadStyleSet::None};
  673. }
  674. return hid_core.GetSupportedStyleTag();
  675. }
  676. void Controller_NPad::SetSupportedNpadIdTypes(u8* data, std::size_t length) {
  677. ASSERT(length > 0 && (length % sizeof(u32)) == 0);
  678. supported_npad_id_types.clear();
  679. supported_npad_id_types.resize(length / sizeof(u32));
  680. std::memcpy(supported_npad_id_types.data(), data, length);
  681. }
  682. void Controller_NPad::GetSupportedNpadIdTypes(u32* data, std::size_t max_length) {
  683. const auto copy_amount = supported_npad_id_types.size() * sizeof(u32);
  684. ASSERT(max_length <= copy_amount);
  685. std::memcpy(data, supported_npad_id_types.data(), copy_amount);
  686. }
  687. std::size_t Controller_NPad::GetSupportedNpadIdTypesSize() const {
  688. return supported_npad_id_types.size();
  689. }
  690. void Controller_NPad::SetHoldType(NpadJoyHoldType joy_hold_type) {
  691. if (joy_hold_type != NpadJoyHoldType::Horizontal &&
  692. joy_hold_type != NpadJoyHoldType::Vertical) {
  693. LOG_ERROR(Service_HID, "Npad joy hold type needs to be valid, joy_hold_type={}",
  694. joy_hold_type);
  695. return;
  696. }
  697. hold_type = joy_hold_type;
  698. }
  699. Controller_NPad::NpadJoyHoldType Controller_NPad::GetHoldType() const {
  700. return hold_type;
  701. }
  702. void Controller_NPad::SetNpadHandheldActivationMode(NpadHandheldActivationMode activation_mode) {
  703. if (activation_mode >= NpadHandheldActivationMode::MaxActivationMode) {
  704. ASSERT_MSG(false, "Activation mode should be always None, Single or Dual");
  705. return;
  706. }
  707. handheld_activation_mode = activation_mode;
  708. }
  709. Controller_NPad::NpadHandheldActivationMode Controller_NPad::GetNpadHandheldActivationMode() const {
  710. return handheld_activation_mode;
  711. }
  712. void Controller_NPad::SetNpadCommunicationMode(NpadCommunicationMode communication_mode_) {
  713. communication_mode = communication_mode_;
  714. }
  715. Controller_NPad::NpadCommunicationMode Controller_NPad::GetNpadCommunicationMode() const {
  716. return communication_mode;
  717. }
  718. Result Controller_NPad::SetNpadMode(Core::HID::NpadIdType npad_id,
  719. NpadJoyDeviceType npad_device_type,
  720. NpadJoyAssignmentMode assignment_mode) {
  721. if (!IsNpadIdValid(npad_id)) {
  722. LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
  723. return InvalidNpadId;
  724. }
  725. auto& controller = GetControllerFromNpadIdType(npad_id);
  726. if (controller.shared_memory->assignment_mode != assignment_mode) {
  727. controller.shared_memory->assignment_mode = assignment_mode;
  728. }
  729. if (!controller.device->IsConnected()) {
  730. return ResultSuccess;
  731. }
  732. if (assignment_mode == NpadJoyAssignmentMode::Dual) {
  733. if (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconLeft) {
  734. DisconnectNpad(npad_id);
  735. controller.is_dual_left_connected = true;
  736. controller.is_dual_right_connected = false;
  737. UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconDual, npad_id, true);
  738. return ResultSuccess;
  739. }
  740. if (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconRight) {
  741. DisconnectNpad(npad_id);
  742. controller.is_dual_left_connected = false;
  743. controller.is_dual_right_connected = true;
  744. UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconDual, npad_id, true);
  745. return ResultSuccess;
  746. }
  747. return ResultSuccess;
  748. }
  749. // This is for NpadJoyAssignmentMode::Single
  750. // Only JoyconDual get affected by this function
  751. if (controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::JoyconDual) {
  752. return ResultSuccess;
  753. }
  754. if (controller.is_dual_left_connected && !controller.is_dual_right_connected) {
  755. DisconnectNpad(npad_id);
  756. UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconLeft, npad_id, true);
  757. return ResultSuccess;
  758. }
  759. if (!controller.is_dual_left_connected && controller.is_dual_right_connected) {
  760. DisconnectNpad(npad_id);
  761. UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconRight, npad_id, true);
  762. return ResultSuccess;
  763. }
  764. // We have two controllers connected to the same npad_id we need to split them
  765. const auto npad_id_2 = hid_core.GetFirstDisconnectedNpadId();
  766. auto& controller_2 = GetControllerFromNpadIdType(npad_id_2);
  767. DisconnectNpad(npad_id);
  768. if (npad_device_type == NpadJoyDeviceType::Left) {
  769. UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconLeft, npad_id, true);
  770. controller_2.is_dual_left_connected = false;
  771. controller_2.is_dual_right_connected = true;
  772. UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconDual, npad_id_2, true);
  773. } else {
  774. UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconRight, npad_id, true);
  775. controller_2.is_dual_left_connected = true;
  776. controller_2.is_dual_right_connected = false;
  777. UpdateControllerAt(Core::HID::NpadStyleIndex::JoyconDual, npad_id_2, true);
  778. }
  779. return ResultSuccess;
  780. }
  781. bool Controller_NPad::VibrateControllerAtIndex(Core::HID::NpadIdType npad_id,
  782. std::size_t device_index,
  783. const Core::HID::VibrationValue& vibration_value) {
  784. auto& controller = GetControllerFromNpadIdType(npad_id);
  785. if (!controller.device->IsConnected()) {
  786. return false;
  787. }
  788. if (!controller.device->IsVibrationEnabled()) {
  789. if (controller.vibration[device_index].latest_vibration_value.low_amplitude != 0.0f ||
  790. controller.vibration[device_index].latest_vibration_value.high_amplitude != 0.0f) {
  791. // Send an empty vibration to stop any vibrations.
  792. Core::HID::VibrationValue vibration{0.0f, 160.0f, 0.0f, 320.0f};
  793. controller.device->SetVibration(device_index, vibration);
  794. // Then reset the vibration value to its default value.
  795. controller.vibration[device_index].latest_vibration_value =
  796. Core::HID::DEFAULT_VIBRATION_VALUE;
  797. }
  798. return false;
  799. }
  800. if (!Settings::values.enable_accurate_vibrations.GetValue()) {
  801. using std::chrono::duration_cast;
  802. using std::chrono::milliseconds;
  803. using std::chrono::steady_clock;
  804. const auto now = steady_clock::now();
  805. // Filter out non-zero vibrations that are within 15ms of each other.
  806. if ((vibration_value.low_amplitude != 0.0f || vibration_value.high_amplitude != 0.0f) &&
  807. duration_cast<milliseconds>(
  808. now - controller.vibration[device_index].last_vibration_timepoint) <
  809. milliseconds(15)) {
  810. return false;
  811. }
  812. controller.vibration[device_index].last_vibration_timepoint = now;
  813. }
  814. Core::HID::VibrationValue vibration{
  815. vibration_value.low_amplitude, vibration_value.low_frequency,
  816. vibration_value.high_amplitude, vibration_value.high_frequency};
  817. return controller.device->SetVibration(device_index, vibration);
  818. }
  819. void Controller_NPad::VibrateController(
  820. const Core::HID::VibrationDeviceHandle& vibration_device_handle,
  821. const Core::HID::VibrationValue& vibration_value) {
  822. if (IsDeviceHandleValid(vibration_device_handle).IsError()) {
  823. return;
  824. }
  825. if (!Settings::values.vibration_enabled.GetValue() && !permit_vibration_session_enabled) {
  826. return;
  827. }
  828. auto& controller = GetControllerFromHandle(vibration_device_handle);
  829. const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
  830. if (!controller.vibration[device_index].device_mounted || !controller.device->IsConnected()) {
  831. return;
  832. }
  833. if (vibration_device_handle.device_index == Core::HID::DeviceIndex::None) {
  834. ASSERT_MSG(false, "DeviceIndex should never be None!");
  835. return;
  836. }
  837. // Some games try to send mismatched parameters in the device handle, block these.
  838. if ((controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconLeft &&
  839. (vibration_device_handle.npad_type == Core::HID::NpadStyleIndex::JoyconRight ||
  840. vibration_device_handle.device_index == Core::HID::DeviceIndex::Right)) ||
  841. (controller.device->GetNpadStyleIndex() == Core::HID::NpadStyleIndex::JoyconRight &&
  842. (vibration_device_handle.npad_type == Core::HID::NpadStyleIndex::JoyconLeft ||
  843. vibration_device_handle.device_index == Core::HID::DeviceIndex::Left))) {
  844. return;
  845. }
  846. // Filter out vibrations with equivalent values to reduce unnecessary state changes.
  847. if (vibration_value.low_amplitude ==
  848. controller.vibration[device_index].latest_vibration_value.low_amplitude &&
  849. vibration_value.high_amplitude ==
  850. controller.vibration[device_index].latest_vibration_value.high_amplitude) {
  851. return;
  852. }
  853. if (VibrateControllerAtIndex(controller.device->GetNpadIdType(), device_index,
  854. vibration_value)) {
  855. controller.vibration[device_index].latest_vibration_value = vibration_value;
  856. }
  857. }
  858. void Controller_NPad::VibrateControllers(
  859. const std::vector<Core::HID::VibrationDeviceHandle>& vibration_device_handles,
  860. const std::vector<Core::HID::VibrationValue>& vibration_values) {
  861. if (!Settings::values.vibration_enabled.GetValue() && !permit_vibration_session_enabled) {
  862. return;
  863. }
  864. ASSERT_OR_EXECUTE_MSG(
  865. vibration_device_handles.size() == vibration_values.size(), { return; },
  866. "The amount of device handles does not match with the amount of vibration values,"
  867. "this is undefined behavior!");
  868. for (std::size_t i = 0; i < vibration_device_handles.size(); ++i) {
  869. VibrateController(vibration_device_handles[i], vibration_values[i]);
  870. }
  871. }
  872. Core::HID::VibrationValue Controller_NPad::GetLastVibration(
  873. const Core::HID::VibrationDeviceHandle& vibration_device_handle) const {
  874. if (IsDeviceHandleValid(vibration_device_handle).IsError()) {
  875. return {};
  876. }
  877. const auto& controller = GetControllerFromHandle(vibration_device_handle);
  878. const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
  879. return controller.vibration[device_index].latest_vibration_value;
  880. }
  881. void Controller_NPad::InitializeVibrationDevice(
  882. const Core::HID::VibrationDeviceHandle& vibration_device_handle) {
  883. if (IsDeviceHandleValid(vibration_device_handle).IsError()) {
  884. return;
  885. }
  886. const auto npad_index = static_cast<Core::HID::NpadIdType>(vibration_device_handle.npad_id);
  887. const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
  888. InitializeVibrationDeviceAtIndex(npad_index, device_index);
  889. }
  890. void Controller_NPad::InitializeVibrationDeviceAtIndex(Core::HID::NpadIdType npad_id,
  891. std::size_t device_index) {
  892. auto& controller = GetControllerFromNpadIdType(npad_id);
  893. if (!Settings::values.vibration_enabled.GetValue()) {
  894. controller.vibration[device_index].device_mounted = false;
  895. return;
  896. }
  897. controller.vibration[device_index].device_mounted =
  898. controller.device->TestVibration(device_index);
  899. }
  900. void Controller_NPad::SetPermitVibrationSession(bool permit_vibration_session) {
  901. permit_vibration_session_enabled = permit_vibration_session;
  902. }
  903. bool Controller_NPad::IsVibrationDeviceMounted(
  904. const Core::HID::VibrationDeviceHandle& vibration_device_handle) const {
  905. if (IsDeviceHandleValid(vibration_device_handle).IsError()) {
  906. return false;
  907. }
  908. const auto& controller = GetControllerFromHandle(vibration_device_handle);
  909. const auto device_index = static_cast<std::size_t>(vibration_device_handle.device_index);
  910. return controller.vibration[device_index].device_mounted;
  911. }
  912. Kernel::KReadableEvent& Controller_NPad::GetStyleSetChangedEvent(Core::HID::NpadIdType npad_id) {
  913. if (!IsNpadIdValid(npad_id)) {
  914. LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
  915. // Fallback to player 1
  916. const auto& controller = GetControllerFromNpadIdType(Core::HID::NpadIdType::Player1);
  917. return controller.styleset_changed_event->GetReadableEvent();
  918. }
  919. const auto& controller = GetControllerFromNpadIdType(npad_id);
  920. return controller.styleset_changed_event->GetReadableEvent();
  921. }
  922. void Controller_NPad::SignalStyleSetChangedEvent(Core::HID::NpadIdType npad_id) const {
  923. const auto& controller = GetControllerFromNpadIdType(npad_id);
  924. controller.styleset_changed_event->Signal();
  925. }
  926. void Controller_NPad::AddNewControllerAt(Core::HID::NpadStyleIndex controller,
  927. Core::HID::NpadIdType npad_id) {
  928. UpdateControllerAt(controller, npad_id, true);
  929. }
  930. void Controller_NPad::UpdateControllerAt(Core::HID::NpadStyleIndex type,
  931. Core::HID::NpadIdType npad_id, bool connected) {
  932. auto& controller = GetControllerFromNpadIdType(npad_id);
  933. if (!connected) {
  934. DisconnectNpad(npad_id);
  935. return;
  936. }
  937. controller.device->SetNpadStyleIndex(type);
  938. InitNewlyAddedController(npad_id);
  939. }
  940. Result Controller_NPad::DisconnectNpad(Core::HID::NpadIdType npad_id) {
  941. if (!IsNpadIdValid(npad_id)) {
  942. LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
  943. return InvalidNpadId;
  944. }
  945. LOG_DEBUG(Service_HID, "Npad disconnected {}", npad_id);
  946. auto& controller = GetControllerFromNpadIdType(npad_id);
  947. for (std::size_t device_idx = 0; device_idx < controller.vibration.size(); ++device_idx) {
  948. // Send an empty vibration to stop any vibrations.
  949. VibrateControllerAtIndex(npad_id, device_idx, {});
  950. controller.vibration[device_idx].device_mounted = false;
  951. }
  952. auto* shared_memory = controller.shared_memory;
  953. // Don't reset shared_memory->assignment_mode this value is persistent
  954. shared_memory->style_tag.raw = Core::HID::NpadStyleSet::None; // Zero out
  955. shared_memory->device_type.raw = 0;
  956. shared_memory->system_properties.raw = 0;
  957. shared_memory->button_properties.raw = 0;
  958. shared_memory->sixaxis_fullkey_properties.raw = 0;
  959. shared_memory->sixaxis_handheld_properties.raw = 0;
  960. shared_memory->sixaxis_dual_left_properties.raw = 0;
  961. shared_memory->sixaxis_dual_right_properties.raw = 0;
  962. shared_memory->sixaxis_left_properties.raw = 0;
  963. shared_memory->sixaxis_right_properties.raw = 0;
  964. shared_memory->battery_level_dual = 0;
  965. shared_memory->battery_level_left = 0;
  966. shared_memory->battery_level_right = 0;
  967. shared_memory->fullkey_color = {
  968. .attribute = ColorAttribute::NoController,
  969. .fullkey = {},
  970. };
  971. shared_memory->joycon_color = {
  972. .attribute = ColorAttribute::NoController,
  973. .left = {},
  974. .right = {},
  975. };
  976. shared_memory->applet_nfc_xcd.applet_footer.type = AppletFooterUiType::None;
  977. controller.is_dual_left_connected = true;
  978. controller.is_dual_right_connected = true;
  979. controller.is_connected = false;
  980. controller.device->Disconnect();
  981. SignalStyleSetChangedEvent(npad_id);
  982. WriteEmptyEntry(shared_memory);
  983. return ResultSuccess;
  984. }
  985. Result Controller_NPad::SetGyroscopeZeroDriftMode(
  986. const Core::HID::SixAxisSensorHandle& sixaxis_handle, GyroscopeZeroDriftMode drift_mode) {
  987. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  988. if (is_valid.IsError()) {
  989. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  990. return is_valid;
  991. }
  992. auto& sixaxis = GetSixaxisState(sixaxis_handle);
  993. sixaxis.gyroscope_zero_drift_mode = drift_mode;
  994. return ResultSuccess;
  995. }
  996. Result Controller_NPad::GetGyroscopeZeroDriftMode(
  997. const Core::HID::SixAxisSensorHandle& sixaxis_handle,
  998. GyroscopeZeroDriftMode& drift_mode) const {
  999. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1000. if (is_valid.IsError()) {
  1001. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1002. return is_valid;
  1003. }
  1004. const auto& sixaxis = GetSixaxisState(sixaxis_handle);
  1005. drift_mode = sixaxis.gyroscope_zero_drift_mode;
  1006. return ResultSuccess;
  1007. }
  1008. Result Controller_NPad::IsSixAxisSensorAtRest(const Core::HID::SixAxisSensorHandle& sixaxis_handle,
  1009. bool& is_at_rest) const {
  1010. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1011. if (is_valid.IsError()) {
  1012. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1013. return is_valid;
  1014. }
  1015. const auto& controller = GetControllerFromHandle(sixaxis_handle);
  1016. is_at_rest = controller.sixaxis_at_rest;
  1017. return ResultSuccess;
  1018. }
  1019. Result Controller_NPad::IsFirmwareUpdateAvailableForSixAxisSensor(
  1020. const Core::HID::SixAxisSensorHandle& sixaxis_handle, bool& is_firmware_available) const {
  1021. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1022. if (is_valid.IsError()) {
  1023. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1024. return is_valid;
  1025. }
  1026. const auto& sixaxis_properties = GetSixaxisProperties(sixaxis_handle);
  1027. is_firmware_available = sixaxis_properties.is_firmware_update_available != 0;
  1028. return ResultSuccess;
  1029. }
  1030. Result Controller_NPad::EnableSixAxisSensorUnalteredPassthrough(
  1031. const Core::HID::SixAxisSensorHandle& sixaxis_handle, bool is_enabled) {
  1032. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1033. if (is_valid.IsError()) {
  1034. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1035. return is_valid;
  1036. }
  1037. auto& sixaxis = GetSixaxisState(sixaxis_handle);
  1038. sixaxis.unaltered_passtrough = is_enabled;
  1039. return ResultSuccess;
  1040. }
  1041. Result Controller_NPad::IsSixAxisSensorUnalteredPassthroughEnabled(
  1042. const Core::HID::SixAxisSensorHandle& sixaxis_handle, bool& is_enabled) const {
  1043. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1044. if (is_valid.IsError()) {
  1045. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1046. return is_valid;
  1047. }
  1048. const auto& sixaxis = GetSixaxisState(sixaxis_handle);
  1049. is_enabled = sixaxis.unaltered_passtrough;
  1050. return ResultSuccess;
  1051. }
  1052. Result Controller_NPad::LoadSixAxisSensorCalibrationParameter(
  1053. const Core::HID::SixAxisSensorHandle& sixaxis_handle,
  1054. Core::HID::SixAxisSensorCalibrationParameter& calibration) const {
  1055. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1056. if (is_valid.IsError()) {
  1057. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1058. return is_valid;
  1059. }
  1060. // TODO: Request this data to the controller. On error return 0xd8ca
  1061. const auto& sixaxis = GetSixaxisState(sixaxis_handle);
  1062. calibration = sixaxis.calibration;
  1063. return ResultSuccess;
  1064. }
  1065. Result Controller_NPad::GetSixAxisSensorIcInformation(
  1066. const Core::HID::SixAxisSensorHandle& sixaxis_handle,
  1067. Core::HID::SixAxisSensorIcInformation& ic_information) const {
  1068. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1069. if (is_valid.IsError()) {
  1070. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1071. return is_valid;
  1072. }
  1073. // TODO: Request this data to the controller. On error return 0xd8ca
  1074. const auto& sixaxis = GetSixaxisState(sixaxis_handle);
  1075. ic_information = sixaxis.ic_information;
  1076. return ResultSuccess;
  1077. }
  1078. Result Controller_NPad::ResetIsSixAxisSensorDeviceNewlyAssigned(
  1079. const Core::HID::SixAxisSensorHandle& sixaxis_handle) {
  1080. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1081. if (is_valid.IsError()) {
  1082. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1083. return is_valid;
  1084. }
  1085. auto& sixaxis_properties = GetSixaxisProperties(sixaxis_handle);
  1086. sixaxis_properties.is_newly_assigned.Assign(0);
  1087. return ResultSuccess;
  1088. }
  1089. Result Controller_NPad::SetSixAxisEnabled(const Core::HID::SixAxisSensorHandle& sixaxis_handle,
  1090. bool sixaxis_status) {
  1091. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1092. if (is_valid.IsError()) {
  1093. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1094. return is_valid;
  1095. }
  1096. auto& controller = GetControllerFromHandle(sixaxis_handle);
  1097. controller.sixaxis_sensor_enabled = sixaxis_status;
  1098. return ResultSuccess;
  1099. }
  1100. Result Controller_NPad::IsSixAxisSensorFusionEnabled(
  1101. const Core::HID::SixAxisSensorHandle& sixaxis_handle, bool& is_fusion_enabled) const {
  1102. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1103. if (is_valid.IsError()) {
  1104. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1105. return is_valid;
  1106. }
  1107. const auto& sixaxis = GetSixaxisState(sixaxis_handle);
  1108. is_fusion_enabled = sixaxis.is_fusion_enabled;
  1109. return ResultSuccess;
  1110. }
  1111. Result Controller_NPad::SetSixAxisFusionEnabled(
  1112. const Core::HID::SixAxisSensorHandle& sixaxis_handle, bool is_fusion_enabled) {
  1113. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1114. if (is_valid.IsError()) {
  1115. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1116. return is_valid;
  1117. }
  1118. auto& sixaxis = GetSixaxisState(sixaxis_handle);
  1119. sixaxis.is_fusion_enabled = is_fusion_enabled;
  1120. return ResultSuccess;
  1121. }
  1122. Result Controller_NPad::SetSixAxisFusionParameters(
  1123. const Core::HID::SixAxisSensorHandle& sixaxis_handle,
  1124. Core::HID::SixAxisSensorFusionParameters sixaxis_fusion_parameters) {
  1125. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1126. if (is_valid.IsError()) {
  1127. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1128. return is_valid;
  1129. }
  1130. const auto param1 = sixaxis_fusion_parameters.parameter1;
  1131. if (param1 < 0.0f || param1 > 1.0f) {
  1132. return InvalidSixAxisFusionRange;
  1133. }
  1134. auto& sixaxis = GetSixaxisState(sixaxis_handle);
  1135. sixaxis.fusion = sixaxis_fusion_parameters;
  1136. return ResultSuccess;
  1137. }
  1138. Result Controller_NPad::GetSixAxisFusionParameters(
  1139. const Core::HID::SixAxisSensorHandle& sixaxis_handle,
  1140. Core::HID::SixAxisSensorFusionParameters& parameters) const {
  1141. const auto is_valid = VerifyValidSixAxisSensorHandle(sixaxis_handle);
  1142. if (is_valid.IsError()) {
  1143. LOG_ERROR(Service_HID, "Invalid handle, error_code={}", is_valid.raw);
  1144. return is_valid;
  1145. }
  1146. const auto& sixaxis = GetSixaxisState(sixaxis_handle);
  1147. parameters = sixaxis.fusion;
  1148. return ResultSuccess;
  1149. }
  1150. Result Controller_NPad::MergeSingleJoyAsDualJoy(Core::HID::NpadIdType npad_id_1,
  1151. Core::HID::NpadIdType npad_id_2) {
  1152. if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) {
  1153. LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id_1:{}, npad_id_2:{}", npad_id_1,
  1154. npad_id_2);
  1155. return InvalidNpadId;
  1156. }
  1157. auto& controller_1 = GetControllerFromNpadIdType(npad_id_1);
  1158. auto& controller_2 = GetControllerFromNpadIdType(npad_id_2);
  1159. auto controller_style_1 = controller_1.device->GetNpadStyleIndex();
  1160. auto controller_style_2 = controller_2.device->GetNpadStyleIndex();
  1161. // Simplify this code by converting dualjoycon with only a side connected to single joycons
  1162. if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconDual) {
  1163. if (controller_1.is_dual_left_connected && !controller_1.is_dual_right_connected) {
  1164. controller_style_1 = Core::HID::NpadStyleIndex::JoyconLeft;
  1165. }
  1166. if (!controller_1.is_dual_left_connected && controller_1.is_dual_right_connected) {
  1167. controller_style_1 = Core::HID::NpadStyleIndex::JoyconRight;
  1168. }
  1169. }
  1170. if (controller_style_2 == Core::HID::NpadStyleIndex::JoyconDual) {
  1171. if (controller_2.is_dual_left_connected && !controller_2.is_dual_right_connected) {
  1172. controller_style_2 = Core::HID::NpadStyleIndex::JoyconLeft;
  1173. }
  1174. if (!controller_2.is_dual_left_connected && controller_2.is_dual_right_connected) {
  1175. controller_style_2 = Core::HID::NpadStyleIndex::JoyconRight;
  1176. }
  1177. }
  1178. // Invalid merge errors
  1179. if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconDual ||
  1180. controller_style_2 == Core::HID::NpadStyleIndex::JoyconDual) {
  1181. return NpadIsDualJoycon;
  1182. }
  1183. if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconLeft &&
  1184. controller_style_2 == Core::HID::NpadStyleIndex::JoyconLeft) {
  1185. return NpadIsSameType;
  1186. }
  1187. if (controller_style_1 == Core::HID::NpadStyleIndex::JoyconRight &&
  1188. controller_style_2 == Core::HID::NpadStyleIndex::JoyconRight) {
  1189. return NpadIsSameType;
  1190. }
  1191. // These exceptions are handled as if they where dual joycon
  1192. if (controller_style_1 != Core::HID::NpadStyleIndex::JoyconLeft &&
  1193. controller_style_1 != Core::HID::NpadStyleIndex::JoyconRight) {
  1194. return NpadIsDualJoycon;
  1195. }
  1196. if (controller_style_2 != Core::HID::NpadStyleIndex::JoyconLeft &&
  1197. controller_style_2 != Core::HID::NpadStyleIndex::JoyconRight) {
  1198. return NpadIsDualJoycon;
  1199. }
  1200. // Disconnect the joycon at the second id and connect the dual joycon at the first index.
  1201. DisconnectNpad(npad_id_2);
  1202. controller_1.is_dual_left_connected = true;
  1203. controller_1.is_dual_right_connected = true;
  1204. AddNewControllerAt(Core::HID::NpadStyleIndex::JoyconDual, npad_id_1);
  1205. return ResultSuccess;
  1206. }
  1207. void Controller_NPad::StartLRAssignmentMode() {
  1208. // Nothing internally is used for lr assignment mode. Since we have the ability to set the
  1209. // controller types from boot, it doesn't really matter about showing a selection screen
  1210. is_in_lr_assignment_mode = true;
  1211. }
  1212. void Controller_NPad::StopLRAssignmentMode() {
  1213. is_in_lr_assignment_mode = false;
  1214. }
  1215. Result Controller_NPad::SwapNpadAssignment(Core::HID::NpadIdType npad_id_1,
  1216. Core::HID::NpadIdType npad_id_2) {
  1217. if (!IsNpadIdValid(npad_id_1) || !IsNpadIdValid(npad_id_2)) {
  1218. LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id_1:{}, npad_id_2:{}", npad_id_1,
  1219. npad_id_2);
  1220. return InvalidNpadId;
  1221. }
  1222. if (npad_id_1 == Core::HID::NpadIdType::Handheld ||
  1223. npad_id_2 == Core::HID::NpadIdType::Handheld || npad_id_1 == Core::HID::NpadIdType::Other ||
  1224. npad_id_2 == Core::HID::NpadIdType::Other) {
  1225. return ResultSuccess;
  1226. }
  1227. const auto& controller_1 = GetControllerFromNpadIdType(npad_id_1).device;
  1228. const auto& controller_2 = GetControllerFromNpadIdType(npad_id_2).device;
  1229. const auto type_index_1 = controller_1->GetNpadStyleIndex();
  1230. const auto type_index_2 = controller_2->GetNpadStyleIndex();
  1231. const auto is_connected_1 = controller_1->IsConnected();
  1232. const auto is_connected_2 = controller_2->IsConnected();
  1233. if (!IsControllerSupported(type_index_1) && is_connected_1) {
  1234. return NpadNotConnected;
  1235. }
  1236. if (!IsControllerSupported(type_index_2) && is_connected_2) {
  1237. return NpadNotConnected;
  1238. }
  1239. UpdateControllerAt(type_index_2, npad_id_1, is_connected_2);
  1240. UpdateControllerAt(type_index_1, npad_id_2, is_connected_1);
  1241. return ResultSuccess;
  1242. }
  1243. Result Controller_NPad::GetLedPattern(Core::HID::NpadIdType npad_id,
  1244. Core::HID::LedPattern& pattern) const {
  1245. if (!IsNpadIdValid(npad_id)) {
  1246. LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
  1247. return InvalidNpadId;
  1248. }
  1249. const auto& controller = GetControllerFromNpadIdType(npad_id).device;
  1250. pattern = controller->GetLedPattern();
  1251. return ResultSuccess;
  1252. }
  1253. Result Controller_NPad::IsUnintendedHomeButtonInputProtectionEnabled(Core::HID::NpadIdType npad_id,
  1254. bool& is_valid) const {
  1255. if (!IsNpadIdValid(npad_id)) {
  1256. LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
  1257. return InvalidNpadId;
  1258. }
  1259. const auto& controller = GetControllerFromNpadIdType(npad_id);
  1260. is_valid = controller.unintended_home_button_input_protection;
  1261. return ResultSuccess;
  1262. }
  1263. Result Controller_NPad::SetUnintendedHomeButtonInputProtectionEnabled(
  1264. bool is_protection_enabled, Core::HID::NpadIdType npad_id) {
  1265. if (!IsNpadIdValid(npad_id)) {
  1266. LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
  1267. return InvalidNpadId;
  1268. }
  1269. auto& controller = GetControllerFromNpadIdType(npad_id);
  1270. controller.unintended_home_button_input_protection = is_protection_enabled;
  1271. return ResultSuccess;
  1272. }
  1273. void Controller_NPad::SetAnalogStickUseCenterClamp(bool use_center_clamp) {
  1274. analog_stick_use_center_clamp = use_center_clamp;
  1275. }
  1276. void Controller_NPad::ClearAllConnectedControllers() {
  1277. for (auto& controller : controller_data) {
  1278. if (controller.device->IsConnected() &&
  1279. controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::None) {
  1280. controller.device->Disconnect();
  1281. controller.device->SetNpadStyleIndex(Core::HID::NpadStyleIndex::None);
  1282. }
  1283. }
  1284. }
  1285. void Controller_NPad::DisconnectAllConnectedControllers() {
  1286. for (auto& controller : controller_data) {
  1287. controller.device->Disconnect();
  1288. }
  1289. }
  1290. void Controller_NPad::ConnectAllDisconnectedControllers() {
  1291. for (auto& controller : controller_data) {
  1292. if (controller.device->GetNpadStyleIndex() != Core::HID::NpadStyleIndex::None &&
  1293. !controller.device->IsConnected()) {
  1294. controller.device->Connect();
  1295. }
  1296. }
  1297. }
  1298. void Controller_NPad::ClearAllControllers() {
  1299. for (auto& controller : controller_data) {
  1300. controller.device->Disconnect();
  1301. controller.device->SetNpadStyleIndex(Core::HID::NpadStyleIndex::None);
  1302. }
  1303. }
  1304. Core::HID::NpadButton Controller_NPad::GetAndResetPressState() {
  1305. return static_cast<Core::HID::NpadButton>(press_state.exchange(0));
  1306. }
  1307. bool Controller_NPad::IsControllerSupported(Core::HID::NpadStyleIndex controller) const {
  1308. if (controller == Core::HID::NpadStyleIndex::Handheld) {
  1309. const bool support_handheld =
  1310. std::find(supported_npad_id_types.begin(), supported_npad_id_types.end(),
  1311. Core::HID::NpadIdType::Handheld) != supported_npad_id_types.end();
  1312. // Handheld is not even a supported type, lets stop here
  1313. if (!support_handheld) {
  1314. return false;
  1315. }
  1316. // Handheld shouldn't be supported in docked mode
  1317. if (Settings::values.use_docked_mode.GetValue()) {
  1318. return false;
  1319. }
  1320. return true;
  1321. }
  1322. if (std::any_of(supported_npad_id_types.begin(), supported_npad_id_types.end(),
  1323. [](Core::HID::NpadIdType npad_id) {
  1324. return npad_id <= Core::HID::NpadIdType::Player8;
  1325. })) {
  1326. Core::HID::NpadStyleTag style = GetSupportedStyleSet();
  1327. switch (controller) {
  1328. case Core::HID::NpadStyleIndex::ProController:
  1329. return style.fullkey;
  1330. case Core::HID::NpadStyleIndex::JoyconDual:
  1331. return style.joycon_dual;
  1332. case Core::HID::NpadStyleIndex::JoyconLeft:
  1333. return style.joycon_left;
  1334. case Core::HID::NpadStyleIndex::JoyconRight:
  1335. return style.joycon_right;
  1336. case Core::HID::NpadStyleIndex::GameCube:
  1337. return style.gamecube;
  1338. case Core::HID::NpadStyleIndex::Pokeball:
  1339. return style.palma;
  1340. case Core::HID::NpadStyleIndex::NES:
  1341. return style.lark;
  1342. case Core::HID::NpadStyleIndex::SNES:
  1343. return style.lucia;
  1344. case Core::HID::NpadStyleIndex::N64:
  1345. return style.lagoon;
  1346. case Core::HID::NpadStyleIndex::SegaGenesis:
  1347. return style.lager;
  1348. default:
  1349. return false;
  1350. }
  1351. }
  1352. return false;
  1353. }
  1354. Controller_NPad::NpadControllerData& Controller_NPad::GetControllerFromHandle(
  1355. const Core::HID::SixAxisSensorHandle& device_handle) {
  1356. const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
  1357. return GetControllerFromNpadIdType(npad_id);
  1358. }
  1359. const Controller_NPad::NpadControllerData& Controller_NPad::GetControllerFromHandle(
  1360. const Core::HID::SixAxisSensorHandle& device_handle) const {
  1361. const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
  1362. return GetControllerFromNpadIdType(npad_id);
  1363. }
  1364. Controller_NPad::NpadControllerData& Controller_NPad::GetControllerFromHandle(
  1365. const Core::HID::VibrationDeviceHandle& device_handle) {
  1366. const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
  1367. return GetControllerFromNpadIdType(npad_id);
  1368. }
  1369. const Controller_NPad::NpadControllerData& Controller_NPad::GetControllerFromHandle(
  1370. const Core::HID::VibrationDeviceHandle& device_handle) const {
  1371. const auto npad_id = static_cast<Core::HID::NpadIdType>(device_handle.npad_id);
  1372. return GetControllerFromNpadIdType(npad_id);
  1373. }
  1374. Controller_NPad::NpadControllerData& Controller_NPad::GetControllerFromNpadIdType(
  1375. Core::HID::NpadIdType npad_id) {
  1376. if (!IsNpadIdValid(npad_id)) {
  1377. LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
  1378. npad_id = Core::HID::NpadIdType::Player1;
  1379. }
  1380. const auto npad_index = Core::HID::NpadIdTypeToIndex(npad_id);
  1381. return controller_data[npad_index];
  1382. }
  1383. const Controller_NPad::NpadControllerData& Controller_NPad::GetControllerFromNpadIdType(
  1384. Core::HID::NpadIdType npad_id) const {
  1385. if (!IsNpadIdValid(npad_id)) {
  1386. LOG_ERROR(Service_HID, "Invalid NpadIdType npad_id:{}", npad_id);
  1387. npad_id = Core::HID::NpadIdType::Player1;
  1388. }
  1389. const auto npad_index = Core::HID::NpadIdTypeToIndex(npad_id);
  1390. return controller_data[npad_index];
  1391. }
  1392. Core::HID::SixAxisSensorProperties& Controller_NPad::GetSixaxisProperties(
  1393. const Core::HID::SixAxisSensorHandle& sixaxis_handle) {
  1394. auto& controller = GetControllerFromHandle(sixaxis_handle);
  1395. switch (sixaxis_handle.npad_type) {
  1396. case Core::HID::NpadStyleIndex::ProController:
  1397. case Core::HID::NpadStyleIndex::Pokeball:
  1398. return controller.shared_memory->sixaxis_fullkey_properties;
  1399. case Core::HID::NpadStyleIndex::Handheld:
  1400. return controller.shared_memory->sixaxis_handheld_properties;
  1401. case Core::HID::NpadStyleIndex::JoyconDual:
  1402. if (sixaxis_handle.device_index == Core::HID::DeviceIndex::Left) {
  1403. return controller.shared_memory->sixaxis_dual_left_properties;
  1404. }
  1405. return controller.shared_memory->sixaxis_dual_right_properties;
  1406. case Core::HID::NpadStyleIndex::JoyconLeft:
  1407. return controller.shared_memory->sixaxis_left_properties;
  1408. case Core::HID::NpadStyleIndex::JoyconRight:
  1409. return controller.shared_memory->sixaxis_right_properties;
  1410. default:
  1411. return controller.shared_memory->sixaxis_fullkey_properties;
  1412. }
  1413. }
  1414. const Core::HID::SixAxisSensorProperties& Controller_NPad::GetSixaxisProperties(
  1415. const Core::HID::SixAxisSensorHandle& sixaxis_handle) const {
  1416. const auto& controller = GetControllerFromHandle(sixaxis_handle);
  1417. switch (sixaxis_handle.npad_type) {
  1418. case Core::HID::NpadStyleIndex::ProController:
  1419. case Core::HID::NpadStyleIndex::Pokeball:
  1420. return controller.shared_memory->sixaxis_fullkey_properties;
  1421. case Core::HID::NpadStyleIndex::Handheld:
  1422. return controller.shared_memory->sixaxis_handheld_properties;
  1423. case Core::HID::NpadStyleIndex::JoyconDual:
  1424. if (sixaxis_handle.device_index == Core::HID::DeviceIndex::Left) {
  1425. return controller.shared_memory->sixaxis_dual_left_properties;
  1426. }
  1427. return controller.shared_memory->sixaxis_dual_right_properties;
  1428. case Core::HID::NpadStyleIndex::JoyconLeft:
  1429. return controller.shared_memory->sixaxis_left_properties;
  1430. case Core::HID::NpadStyleIndex::JoyconRight:
  1431. return controller.shared_memory->sixaxis_right_properties;
  1432. default:
  1433. return controller.shared_memory->sixaxis_fullkey_properties;
  1434. }
  1435. }
  1436. Controller_NPad::SixaxisParameters& Controller_NPad::GetSixaxisState(
  1437. const Core::HID::SixAxisSensorHandle& sixaxis_handle) {
  1438. auto& controller = GetControllerFromHandle(sixaxis_handle);
  1439. switch (sixaxis_handle.npad_type) {
  1440. case Core::HID::NpadStyleIndex::ProController:
  1441. case Core::HID::NpadStyleIndex::Pokeball:
  1442. return controller.sixaxis_fullkey;
  1443. case Core::HID::NpadStyleIndex::Handheld:
  1444. return controller.sixaxis_handheld;
  1445. case Core::HID::NpadStyleIndex::JoyconDual:
  1446. if (sixaxis_handle.device_index == Core::HID::DeviceIndex::Left) {
  1447. return controller.sixaxis_dual_left;
  1448. }
  1449. return controller.sixaxis_dual_right;
  1450. case Core::HID::NpadStyleIndex::JoyconLeft:
  1451. return controller.sixaxis_left;
  1452. case Core::HID::NpadStyleIndex::JoyconRight:
  1453. return controller.sixaxis_right;
  1454. default:
  1455. return controller.sixaxis_unknown;
  1456. }
  1457. }
  1458. const Controller_NPad::SixaxisParameters& Controller_NPad::GetSixaxisState(
  1459. const Core::HID::SixAxisSensorHandle& sixaxis_handle) const {
  1460. const auto& controller = GetControllerFromHandle(sixaxis_handle);
  1461. switch (sixaxis_handle.npad_type) {
  1462. case Core::HID::NpadStyleIndex::ProController:
  1463. case Core::HID::NpadStyleIndex::Pokeball:
  1464. return controller.sixaxis_fullkey;
  1465. case Core::HID::NpadStyleIndex::Handheld:
  1466. return controller.sixaxis_handheld;
  1467. case Core::HID::NpadStyleIndex::JoyconDual:
  1468. if (sixaxis_handle.device_index == Core::HID::DeviceIndex::Left) {
  1469. return controller.sixaxis_dual_left;
  1470. }
  1471. return controller.sixaxis_dual_right;
  1472. case Core::HID::NpadStyleIndex::JoyconLeft:
  1473. return controller.sixaxis_left;
  1474. case Core::HID::NpadStyleIndex::JoyconRight:
  1475. return controller.sixaxis_right;
  1476. default:
  1477. return controller.sixaxis_unknown;
  1478. }
  1479. }
  1480. } // namespace Service::HID