npad.cpp 72 KB

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