emulated_controller.cpp 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <common/scope_exit.h>
  5. #include "common/polyfill_ranges.h"
  6. #include "common/thread.h"
  7. #include "core/hid/emulated_controller.h"
  8. #include "core/hid/input_converter.h"
  9. namespace Core::HID {
  10. constexpr s32 HID_JOYSTICK_MAX = 0x7fff;
  11. constexpr s32 HID_TRIGGER_MAX = 0x7fff;
  12. constexpr u32 TURBO_BUTTON_DELAY = 4;
  13. // Use a common UUID for TAS and Virtual Gamepad
  14. constexpr Common::UUID TAS_UUID =
  15. Common::UUID{{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0xA5, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
  16. constexpr Common::UUID VIRTUAL_UUID =
  17. Common::UUID{{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0xFF, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};
  18. EmulatedController::EmulatedController(NpadIdType npad_id_type_) : npad_id_type(npad_id_type_) {}
  19. EmulatedController::~EmulatedController() = default;
  20. NpadStyleIndex EmulatedController::MapSettingsTypeToNPad(Settings::ControllerType type) {
  21. switch (type) {
  22. case Settings::ControllerType::ProController:
  23. return NpadStyleIndex::ProController;
  24. case Settings::ControllerType::DualJoyconDetached:
  25. return NpadStyleIndex::JoyconDual;
  26. case Settings::ControllerType::LeftJoycon:
  27. return NpadStyleIndex::JoyconLeft;
  28. case Settings::ControllerType::RightJoycon:
  29. return NpadStyleIndex::JoyconRight;
  30. case Settings::ControllerType::Handheld:
  31. return NpadStyleIndex::Handheld;
  32. case Settings::ControllerType::GameCube:
  33. return NpadStyleIndex::GameCube;
  34. case Settings::ControllerType::Pokeball:
  35. return NpadStyleIndex::Pokeball;
  36. case Settings::ControllerType::NES:
  37. return NpadStyleIndex::NES;
  38. case Settings::ControllerType::SNES:
  39. return NpadStyleIndex::SNES;
  40. case Settings::ControllerType::N64:
  41. return NpadStyleIndex::N64;
  42. case Settings::ControllerType::SegaGenesis:
  43. return NpadStyleIndex::SegaGenesis;
  44. default:
  45. return NpadStyleIndex::ProController;
  46. }
  47. }
  48. Settings::ControllerType EmulatedController::MapNPadToSettingsType(NpadStyleIndex type) {
  49. switch (type) {
  50. case NpadStyleIndex::ProController:
  51. return Settings::ControllerType::ProController;
  52. case NpadStyleIndex::JoyconDual:
  53. return Settings::ControllerType::DualJoyconDetached;
  54. case NpadStyleIndex::JoyconLeft:
  55. return Settings::ControllerType::LeftJoycon;
  56. case NpadStyleIndex::JoyconRight:
  57. return Settings::ControllerType::RightJoycon;
  58. case NpadStyleIndex::Handheld:
  59. return Settings::ControllerType::Handheld;
  60. case NpadStyleIndex::GameCube:
  61. return Settings::ControllerType::GameCube;
  62. case NpadStyleIndex::Pokeball:
  63. return Settings::ControllerType::Pokeball;
  64. case NpadStyleIndex::NES:
  65. return Settings::ControllerType::NES;
  66. case NpadStyleIndex::SNES:
  67. return Settings::ControllerType::SNES;
  68. case NpadStyleIndex::N64:
  69. return Settings::ControllerType::N64;
  70. case NpadStyleIndex::SegaGenesis:
  71. return Settings::ControllerType::SegaGenesis;
  72. default:
  73. return Settings::ControllerType::ProController;
  74. }
  75. }
  76. void EmulatedController::ReloadFromSettings() {
  77. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  78. const auto& player = Settings::values.players.GetValue()[player_index];
  79. for (std::size_t index = 0; index < player.buttons.size(); ++index) {
  80. button_params[index] = Common::ParamPackage(player.buttons[index]);
  81. }
  82. for (std::size_t index = 0; index < player.analogs.size(); ++index) {
  83. stick_params[index] = Common::ParamPackage(player.analogs[index]);
  84. }
  85. for (std::size_t index = 0; index < player.motions.size(); ++index) {
  86. motion_params[index] = Common::ParamPackage(player.motions[index]);
  87. }
  88. controller.color_values = {};
  89. controller.colors_state.fullkey = {
  90. .body = GetNpadColor(player.body_color_left),
  91. .button = GetNpadColor(player.button_color_left),
  92. };
  93. controller.colors_state.left = {
  94. .body = GetNpadColor(player.body_color_left),
  95. .button = GetNpadColor(player.button_color_left),
  96. };
  97. controller.colors_state.right = {
  98. .body = GetNpadColor(player.body_color_right),
  99. .button = GetNpadColor(player.button_color_right),
  100. };
  101. ring_params[0] = Common::ParamPackage(Settings::values.ringcon_analogs);
  102. // Other or debug controller should always be a pro controller
  103. if (npad_id_type != NpadIdType::Other) {
  104. SetNpadStyleIndex(MapSettingsTypeToNPad(player.controller_type));
  105. original_npad_type = npad_type;
  106. } else {
  107. SetNpadStyleIndex(NpadStyleIndex::ProController);
  108. original_npad_type = npad_type;
  109. }
  110. Disconnect();
  111. if (player.connected) {
  112. Connect();
  113. }
  114. ReloadInput();
  115. }
  116. void EmulatedController::LoadDevices() {
  117. // TODO(german77): Use more buttons to detect the correct device
  118. const auto left_joycon = button_params[Settings::NativeButton::DRight];
  119. const auto right_joycon = button_params[Settings::NativeButton::A];
  120. // Triggers for GC controllers
  121. trigger_params[LeftIndex] = button_params[Settings::NativeButton::ZL];
  122. trigger_params[RightIndex] = button_params[Settings::NativeButton::ZR];
  123. color_params[LeftIndex] = left_joycon;
  124. color_params[RightIndex] = right_joycon;
  125. color_params[LeftIndex].Set("color", true);
  126. color_params[RightIndex].Set("color", true);
  127. battery_params[LeftIndex] = left_joycon;
  128. battery_params[RightIndex] = right_joycon;
  129. battery_params[LeftIndex].Set("battery", true);
  130. battery_params[RightIndex].Set("battery", true);
  131. camera_params[0] = right_joycon;
  132. camera_params[0].Set("camera", true);
  133. camera_params[1] = Common::ParamPackage{"engine:camera,camera:1"};
  134. ring_params[1] = Common::ParamPackage{"engine:joycon,axis_x:100,axis_y:101"};
  135. nfc_params[0] = Common::ParamPackage{"engine:virtual_amiibo,nfc:1"};
  136. nfc_params[1] = right_joycon;
  137. nfc_params[1].Set("nfc", true);
  138. output_params[LeftIndex] = left_joycon;
  139. output_params[RightIndex] = right_joycon;
  140. output_params[2] = camera_params[1];
  141. output_params[3] = nfc_params[0];
  142. output_params[LeftIndex].Set("output", true);
  143. output_params[RightIndex].Set("output", true);
  144. output_params[2].Set("output", true);
  145. output_params[3].Set("output", true);
  146. LoadTASParams();
  147. LoadVirtualGamepadParams();
  148. std::ranges::transform(button_params, button_devices.begin(), Common::Input::CreateInputDevice);
  149. std::ranges::transform(stick_params, stick_devices.begin(), Common::Input::CreateInputDevice);
  150. std::ranges::transform(motion_params, motion_devices.begin(), Common::Input::CreateInputDevice);
  151. std::ranges::transform(trigger_params, trigger_devices.begin(),
  152. Common::Input::CreateInputDevice);
  153. std::ranges::transform(battery_params, battery_devices.begin(),
  154. Common::Input::CreateInputDevice);
  155. std::ranges::transform(color_params, color_devices.begin(), Common::Input::CreateInputDevice);
  156. std::ranges::transform(camera_params, camera_devices.begin(), Common::Input::CreateInputDevice);
  157. std::ranges::transform(ring_params, ring_analog_devices.begin(),
  158. Common::Input::CreateInputDevice);
  159. std::ranges::transform(nfc_params, nfc_devices.begin(), Common::Input::CreateInputDevice);
  160. std::ranges::transform(output_params, output_devices.begin(),
  161. Common::Input::CreateOutputDevice);
  162. // Initialize TAS devices
  163. std::ranges::transform(tas_button_params, tas_button_devices.begin(),
  164. Common::Input::CreateInputDevice);
  165. std::ranges::transform(tas_stick_params, tas_stick_devices.begin(),
  166. Common::Input::CreateInputDevice);
  167. // Initialize virtual gamepad devices
  168. std::ranges::transform(virtual_button_params, virtual_button_devices.begin(),
  169. Common::Input::CreateInputDevice);
  170. std::ranges::transform(virtual_stick_params, virtual_stick_devices.begin(),
  171. Common::Input::CreateInputDevice);
  172. }
  173. void EmulatedController::LoadTASParams() {
  174. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  175. Common::ParamPackage common_params{};
  176. common_params.Set("engine", "tas");
  177. common_params.Set("port", static_cast<int>(player_index));
  178. for (auto& param : tas_button_params) {
  179. param = common_params;
  180. }
  181. for (auto& param : tas_stick_params) {
  182. param = common_params;
  183. }
  184. // TODO(german77): Replace this with an input profile or something better
  185. tas_button_params[Settings::NativeButton::A].Set("button", 0);
  186. tas_button_params[Settings::NativeButton::B].Set("button", 1);
  187. tas_button_params[Settings::NativeButton::X].Set("button", 2);
  188. tas_button_params[Settings::NativeButton::Y].Set("button", 3);
  189. tas_button_params[Settings::NativeButton::LStick].Set("button", 4);
  190. tas_button_params[Settings::NativeButton::RStick].Set("button", 5);
  191. tas_button_params[Settings::NativeButton::L].Set("button", 6);
  192. tas_button_params[Settings::NativeButton::R].Set("button", 7);
  193. tas_button_params[Settings::NativeButton::ZL].Set("button", 8);
  194. tas_button_params[Settings::NativeButton::ZR].Set("button", 9);
  195. tas_button_params[Settings::NativeButton::Plus].Set("button", 10);
  196. tas_button_params[Settings::NativeButton::Minus].Set("button", 11);
  197. tas_button_params[Settings::NativeButton::DLeft].Set("button", 12);
  198. tas_button_params[Settings::NativeButton::DUp].Set("button", 13);
  199. tas_button_params[Settings::NativeButton::DRight].Set("button", 14);
  200. tas_button_params[Settings::NativeButton::DDown].Set("button", 15);
  201. tas_button_params[Settings::NativeButton::SL].Set("button", 16);
  202. tas_button_params[Settings::NativeButton::SR].Set("button", 17);
  203. tas_button_params[Settings::NativeButton::Home].Set("button", 18);
  204. tas_button_params[Settings::NativeButton::Screenshot].Set("button", 19);
  205. tas_stick_params[Settings::NativeAnalog::LStick].Set("axis_x", 0);
  206. tas_stick_params[Settings::NativeAnalog::LStick].Set("axis_y", 1);
  207. tas_stick_params[Settings::NativeAnalog::RStick].Set("axis_x", 2);
  208. tas_stick_params[Settings::NativeAnalog::RStick].Set("axis_y", 3);
  209. // set to optimal stick to avoid sanitizing the stick and tweaking the coordinates
  210. // making sure they play back in the game as originally written down in the script file
  211. tas_stick_params[Settings::NativeAnalog::LStick].Set("deadzone", 0.0f);
  212. tas_stick_params[Settings::NativeAnalog::LStick].Set("range", 1.0f);
  213. tas_stick_params[Settings::NativeAnalog::RStick].Set("deadzone", 0.0f);
  214. tas_stick_params[Settings::NativeAnalog::RStick].Set("range", 1.0f);
  215. }
  216. void EmulatedController::LoadVirtualGamepadParams() {
  217. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  218. Common::ParamPackage common_params{};
  219. common_params.Set("engine", "virtual_gamepad");
  220. common_params.Set("port", static_cast<int>(player_index));
  221. for (auto& param : virtual_button_params) {
  222. param = common_params;
  223. }
  224. for (auto& param : virtual_stick_params) {
  225. param = common_params;
  226. }
  227. // TODO(german77): Replace this with an input profile or something better
  228. virtual_button_params[Settings::NativeButton::A].Set("button", 0);
  229. virtual_button_params[Settings::NativeButton::B].Set("button", 1);
  230. virtual_button_params[Settings::NativeButton::X].Set("button", 2);
  231. virtual_button_params[Settings::NativeButton::Y].Set("button", 3);
  232. virtual_button_params[Settings::NativeButton::LStick].Set("button", 4);
  233. virtual_button_params[Settings::NativeButton::RStick].Set("button", 5);
  234. virtual_button_params[Settings::NativeButton::L].Set("button", 6);
  235. virtual_button_params[Settings::NativeButton::R].Set("button", 7);
  236. virtual_button_params[Settings::NativeButton::ZL].Set("button", 8);
  237. virtual_button_params[Settings::NativeButton::ZR].Set("button", 9);
  238. virtual_button_params[Settings::NativeButton::Plus].Set("button", 10);
  239. virtual_button_params[Settings::NativeButton::Minus].Set("button", 11);
  240. virtual_button_params[Settings::NativeButton::DLeft].Set("button", 12);
  241. virtual_button_params[Settings::NativeButton::DUp].Set("button", 13);
  242. virtual_button_params[Settings::NativeButton::DRight].Set("button", 14);
  243. virtual_button_params[Settings::NativeButton::DDown].Set("button", 15);
  244. virtual_button_params[Settings::NativeButton::SL].Set("button", 16);
  245. virtual_button_params[Settings::NativeButton::SR].Set("button", 17);
  246. virtual_button_params[Settings::NativeButton::Home].Set("button", 18);
  247. virtual_button_params[Settings::NativeButton::Screenshot].Set("button", 19);
  248. virtual_stick_params[Settings::NativeAnalog::LStick].Set("axis_x", 0);
  249. virtual_stick_params[Settings::NativeAnalog::LStick].Set("axis_y", 1);
  250. virtual_stick_params[Settings::NativeAnalog::RStick].Set("axis_x", 2);
  251. virtual_stick_params[Settings::NativeAnalog::RStick].Set("axis_y", 3);
  252. virtual_stick_params[Settings::NativeAnalog::LStick].Set("deadzone", 0.0f);
  253. virtual_stick_params[Settings::NativeAnalog::LStick].Set("range", 1.0f);
  254. virtual_stick_params[Settings::NativeAnalog::RStick].Set("deadzone", 0.0f);
  255. virtual_stick_params[Settings::NativeAnalog::RStick].Set("range", 1.0f);
  256. }
  257. void EmulatedController::ReloadInput() {
  258. // If you load any device here add the equivalent to the UnloadInput() function
  259. LoadDevices();
  260. for (std::size_t index = 0; index < button_devices.size(); ++index) {
  261. if (!button_devices[index]) {
  262. continue;
  263. }
  264. const auto uuid = Common::UUID{button_params[index].Get("guid", "")};
  265. button_devices[index]->SetCallback({
  266. .on_change =
  267. [this, index, uuid](const Common::Input::CallbackStatus& callback) {
  268. SetButton(callback, index, uuid);
  269. },
  270. });
  271. button_devices[index]->ForceUpdate();
  272. }
  273. for (std::size_t index = 0; index < stick_devices.size(); ++index) {
  274. if (!stick_devices[index]) {
  275. continue;
  276. }
  277. const auto uuid = Common::UUID{stick_params[index].Get("guid", "")};
  278. stick_devices[index]->SetCallback({
  279. .on_change =
  280. [this, index, uuid](const Common::Input::CallbackStatus& callback) {
  281. SetStick(callback, index, uuid);
  282. },
  283. });
  284. stick_devices[index]->ForceUpdate();
  285. }
  286. for (std::size_t index = 0; index < trigger_devices.size(); ++index) {
  287. if (!trigger_devices[index]) {
  288. continue;
  289. }
  290. const auto uuid = Common::UUID{trigger_params[index].Get("guid", "")};
  291. trigger_devices[index]->SetCallback({
  292. .on_change =
  293. [this, index, uuid](const Common::Input::CallbackStatus& callback) {
  294. SetTrigger(callback, index, uuid);
  295. },
  296. });
  297. trigger_devices[index]->ForceUpdate();
  298. }
  299. for (std::size_t index = 0; index < battery_devices.size(); ++index) {
  300. if (!battery_devices[index]) {
  301. continue;
  302. }
  303. battery_devices[index]->SetCallback({
  304. .on_change =
  305. [this, index](const Common::Input::CallbackStatus& callback) {
  306. SetBattery(callback, index);
  307. },
  308. });
  309. battery_devices[index]->ForceUpdate();
  310. }
  311. for (std::size_t index = 0; index < color_devices.size(); ++index) {
  312. if (!color_devices[index]) {
  313. continue;
  314. }
  315. color_devices[index]->SetCallback({
  316. .on_change =
  317. [this, index](const Common::Input::CallbackStatus& callback) {
  318. SetColors(callback, index);
  319. },
  320. });
  321. color_devices[index]->ForceUpdate();
  322. }
  323. for (std::size_t index = 0; index < motion_devices.size(); ++index) {
  324. if (!motion_devices[index]) {
  325. continue;
  326. }
  327. motion_devices[index]->SetCallback({
  328. .on_change =
  329. [this, index](const Common::Input::CallbackStatus& callback) {
  330. SetMotion(callback, index);
  331. },
  332. });
  333. // Restore motion state
  334. auto& emulated_motion = controller.motion_values[index].emulated;
  335. auto& motion = controller.motion_state[index];
  336. emulated_motion.ResetRotations();
  337. emulated_motion.ResetQuaternion();
  338. motion.accel = emulated_motion.GetAcceleration();
  339. motion.gyro = emulated_motion.GetGyroscope();
  340. motion.rotation = emulated_motion.GetRotations();
  341. motion.euler = emulated_motion.GetEulerAngles();
  342. motion.orientation = emulated_motion.GetOrientation();
  343. motion.is_at_rest = !emulated_motion.IsMoving(motion_sensitivity);
  344. }
  345. for (std::size_t index = 0; index < camera_devices.size(); ++index) {
  346. if (!camera_devices[index]) {
  347. continue;
  348. }
  349. camera_devices[index]->SetCallback({
  350. .on_change =
  351. [this](const Common::Input::CallbackStatus& callback) { SetCamera(callback); },
  352. });
  353. camera_devices[index]->ForceUpdate();
  354. }
  355. for (std::size_t index = 0; index < ring_analog_devices.size(); ++index) {
  356. if (!ring_analog_devices[index]) {
  357. continue;
  358. }
  359. ring_analog_devices[index]->SetCallback({
  360. .on_change =
  361. [this](const Common::Input::CallbackStatus& callback) { SetRingAnalog(callback); },
  362. });
  363. ring_analog_devices[index]->ForceUpdate();
  364. }
  365. for (std::size_t index = 0; index < nfc_devices.size(); ++index) {
  366. if (!nfc_devices[index]) {
  367. continue;
  368. }
  369. nfc_devices[index]->SetCallback({
  370. .on_change =
  371. [this](const Common::Input::CallbackStatus& callback) { SetNfc(callback); },
  372. });
  373. nfc_devices[index]->ForceUpdate();
  374. }
  375. // Register TAS devices. No need to force update
  376. for (std::size_t index = 0; index < tas_button_devices.size(); ++index) {
  377. if (!tas_button_devices[index]) {
  378. continue;
  379. }
  380. tas_button_devices[index]->SetCallback({
  381. .on_change =
  382. [this, index](const Common::Input::CallbackStatus& callback) {
  383. SetButton(callback, index, TAS_UUID);
  384. },
  385. });
  386. }
  387. for (std::size_t index = 0; index < tas_stick_devices.size(); ++index) {
  388. if (!tas_stick_devices[index]) {
  389. continue;
  390. }
  391. tas_stick_devices[index]->SetCallback({
  392. .on_change =
  393. [this, index](const Common::Input::CallbackStatus& callback) {
  394. SetStick(callback, index, TAS_UUID);
  395. },
  396. });
  397. }
  398. // Register virtual devices. No need to force update
  399. for (std::size_t index = 0; index < virtual_button_devices.size(); ++index) {
  400. if (!virtual_button_devices[index]) {
  401. continue;
  402. }
  403. virtual_button_devices[index]->SetCallback({
  404. .on_change =
  405. [this, index](const Common::Input::CallbackStatus& callback) {
  406. SetButton(callback, index, VIRTUAL_UUID);
  407. },
  408. });
  409. }
  410. for (std::size_t index = 0; index < virtual_stick_devices.size(); ++index) {
  411. if (!virtual_stick_devices[index]) {
  412. continue;
  413. }
  414. virtual_stick_devices[index]->SetCallback({
  415. .on_change =
  416. [this, index](const Common::Input::CallbackStatus& callback) {
  417. SetStick(callback, index, VIRTUAL_UUID);
  418. },
  419. });
  420. }
  421. turbo_button_state = 0;
  422. }
  423. void EmulatedController::UnloadInput() {
  424. for (auto& button : button_devices) {
  425. button.reset();
  426. }
  427. for (auto& stick : stick_devices) {
  428. stick.reset();
  429. }
  430. for (auto& motion : motion_devices) {
  431. motion.reset();
  432. }
  433. for (auto& trigger : trigger_devices) {
  434. trigger.reset();
  435. }
  436. for (auto& battery : battery_devices) {
  437. battery.reset();
  438. }
  439. for (auto& color : color_devices) {
  440. color.reset();
  441. }
  442. for (auto& output : output_devices) {
  443. output.reset();
  444. }
  445. for (auto& button : tas_button_devices) {
  446. button.reset();
  447. }
  448. for (auto& stick : tas_stick_devices) {
  449. stick.reset();
  450. }
  451. for (auto& button : virtual_button_devices) {
  452. button.reset();
  453. }
  454. for (auto& stick : virtual_stick_devices) {
  455. stick.reset();
  456. }
  457. for (auto& camera : camera_devices) {
  458. camera.reset();
  459. }
  460. for (auto& ring : ring_analog_devices) {
  461. ring.reset();
  462. }
  463. for (auto& nfc : nfc_devices) {
  464. nfc.reset();
  465. }
  466. }
  467. void EmulatedController::EnableConfiguration() {
  468. is_configuring = true;
  469. tmp_is_connected = is_connected;
  470. tmp_npad_type = npad_type;
  471. }
  472. void EmulatedController::DisableConfiguration() {
  473. is_configuring = false;
  474. // Get Joycon colors before turning on the controller
  475. for (const auto& color_device : color_devices) {
  476. color_device->ForceUpdate();
  477. }
  478. // Apply temporary npad type to the real controller
  479. if (tmp_npad_type != npad_type) {
  480. if (is_connected) {
  481. Disconnect();
  482. }
  483. SetNpadStyleIndex(tmp_npad_type);
  484. original_npad_type = tmp_npad_type;
  485. }
  486. // Apply temporary connected status to the real controller
  487. if (tmp_is_connected != is_connected) {
  488. if (tmp_is_connected) {
  489. Connect();
  490. return;
  491. }
  492. Disconnect();
  493. }
  494. }
  495. void EmulatedController::EnableSystemButtons() {
  496. std::scoped_lock lock{mutex};
  497. system_buttons_enabled = true;
  498. }
  499. void EmulatedController::DisableSystemButtons() {
  500. std::scoped_lock lock{mutex};
  501. system_buttons_enabled = false;
  502. controller.home_button_state.raw = 0;
  503. controller.capture_button_state.raw = 0;
  504. }
  505. void EmulatedController::ResetSystemButtons() {
  506. std::scoped_lock lock{mutex};
  507. controller.home_button_state.home.Assign(false);
  508. controller.capture_button_state.capture.Assign(false);
  509. }
  510. bool EmulatedController::IsConfiguring() const {
  511. return is_configuring;
  512. }
  513. void EmulatedController::SaveCurrentConfig() {
  514. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  515. auto& player = Settings::values.players.GetValue()[player_index];
  516. player.connected = is_connected;
  517. player.controller_type = MapNPadToSettingsType(npad_type);
  518. for (std::size_t index = 0; index < player.buttons.size(); ++index) {
  519. player.buttons[index] = button_params[index].Serialize();
  520. }
  521. for (std::size_t index = 0; index < player.analogs.size(); ++index) {
  522. player.analogs[index] = stick_params[index].Serialize();
  523. }
  524. for (std::size_t index = 0; index < player.motions.size(); ++index) {
  525. player.motions[index] = motion_params[index].Serialize();
  526. }
  527. if (npad_id_type == NpadIdType::Player1) {
  528. Settings::values.ringcon_analogs = ring_params[0].Serialize();
  529. }
  530. }
  531. void EmulatedController::RestoreConfig() {
  532. if (!is_configuring) {
  533. return;
  534. }
  535. ReloadFromSettings();
  536. }
  537. std::vector<Common::ParamPackage> EmulatedController::GetMappedDevices() const {
  538. std::vector<Common::ParamPackage> devices;
  539. for (const auto& param : button_params) {
  540. if (!param.Has("engine")) {
  541. continue;
  542. }
  543. const auto devices_it = std::find_if(
  544. devices.begin(), devices.end(), [&param](const Common::ParamPackage& param_) {
  545. return param.Get("engine", "") == param_.Get("engine", "") &&
  546. param.Get("guid", "") == param_.Get("guid", "") &&
  547. param.Get("port", 0) == param_.Get("port", 0) &&
  548. param.Get("pad", 0) == param_.Get("pad", 0);
  549. });
  550. if (devices_it != devices.end()) {
  551. continue;
  552. }
  553. auto& device = devices.emplace_back();
  554. device.Set("engine", param.Get("engine", ""));
  555. device.Set("guid", param.Get("guid", ""));
  556. device.Set("port", param.Get("port", 0));
  557. device.Set("pad", param.Get("pad", 0));
  558. }
  559. for (const auto& param : stick_params) {
  560. if (!param.Has("engine")) {
  561. continue;
  562. }
  563. if (param.Get("engine", "") == "analog_from_button") {
  564. continue;
  565. }
  566. const auto devices_it = std::find_if(
  567. devices.begin(), devices.end(), [&param](const Common::ParamPackage& param_) {
  568. return param.Get("engine", "") == param_.Get("engine", "") &&
  569. param.Get("guid", "") == param_.Get("guid", "") &&
  570. param.Get("port", 0) == param_.Get("port", 0) &&
  571. param.Get("pad", 0) == param_.Get("pad", 0);
  572. });
  573. if (devices_it != devices.end()) {
  574. continue;
  575. }
  576. auto& device = devices.emplace_back();
  577. device.Set("engine", param.Get("engine", ""));
  578. device.Set("guid", param.Get("guid", ""));
  579. device.Set("port", param.Get("port", 0));
  580. device.Set("pad", param.Get("pad", 0));
  581. }
  582. return devices;
  583. }
  584. Common::ParamPackage EmulatedController::GetButtonParam(std::size_t index) const {
  585. if (index >= button_params.size()) {
  586. return {};
  587. }
  588. return button_params[index];
  589. }
  590. Common::ParamPackage EmulatedController::GetStickParam(std::size_t index) const {
  591. if (index >= stick_params.size()) {
  592. return {};
  593. }
  594. return stick_params[index];
  595. }
  596. Common::ParamPackage EmulatedController::GetMotionParam(std::size_t index) const {
  597. if (index >= motion_params.size()) {
  598. return {};
  599. }
  600. return motion_params[index];
  601. }
  602. void EmulatedController::SetButtonParam(std::size_t index, Common::ParamPackage param) {
  603. if (index >= button_params.size()) {
  604. return;
  605. }
  606. button_params[index] = std::move(param);
  607. ReloadInput();
  608. }
  609. void EmulatedController::SetStickParam(std::size_t index, Common::ParamPackage param) {
  610. if (index >= stick_params.size()) {
  611. return;
  612. }
  613. stick_params[index] = std::move(param);
  614. ReloadInput();
  615. }
  616. void EmulatedController::SetMotionParam(std::size_t index, Common::ParamPackage param) {
  617. if (index >= motion_params.size()) {
  618. return;
  619. }
  620. motion_params[index] = std::move(param);
  621. ReloadInput();
  622. }
  623. void EmulatedController::StartMotionCalibration() {
  624. for (ControllerMotionInfo& motion : controller.motion_values) {
  625. motion.emulated.Calibrate();
  626. }
  627. }
  628. void EmulatedController::SetButton(const Common::Input::CallbackStatus& callback, std::size_t index,
  629. Common::UUID uuid) {
  630. if (index >= controller.button_values.size()) {
  631. return;
  632. }
  633. std::unique_lock lock{mutex};
  634. bool value_changed = false;
  635. const auto new_status = TransformToButton(callback);
  636. auto& current_status = controller.button_values[index];
  637. // Only read button values that have the same uuid or are pressed once
  638. if (current_status.uuid != uuid) {
  639. if (!new_status.value) {
  640. return;
  641. }
  642. }
  643. current_status.toggle = new_status.toggle;
  644. current_status.turbo = new_status.turbo;
  645. current_status.uuid = uuid;
  646. // Update button status with current
  647. if (!current_status.toggle) {
  648. current_status.locked = false;
  649. if (current_status.value != new_status.value) {
  650. current_status.value = new_status.value;
  651. value_changed = true;
  652. }
  653. } else {
  654. // Toggle button and lock status
  655. if (new_status.value && !current_status.locked) {
  656. current_status.locked = true;
  657. current_status.value = !current_status.value;
  658. value_changed = true;
  659. }
  660. // Unlock button ready for next press
  661. if (!new_status.value && current_status.locked) {
  662. current_status.locked = false;
  663. }
  664. }
  665. if (!value_changed) {
  666. return;
  667. }
  668. if (is_configuring) {
  669. controller.npad_button_state.raw = NpadButton::None;
  670. controller.debug_pad_button_state.raw = 0;
  671. controller.home_button_state.raw = 0;
  672. controller.capture_button_state.raw = 0;
  673. lock.unlock();
  674. TriggerOnChange(ControllerTriggerType::Button, false);
  675. return;
  676. }
  677. // GC controllers have triggers not buttons
  678. if (npad_type == NpadStyleIndex::GameCube) {
  679. if (index == Settings::NativeButton::ZR) {
  680. return;
  681. }
  682. if (index == Settings::NativeButton::ZL) {
  683. return;
  684. }
  685. }
  686. switch (index) {
  687. case Settings::NativeButton::A:
  688. controller.npad_button_state.a.Assign(current_status.value);
  689. controller.debug_pad_button_state.a.Assign(current_status.value);
  690. break;
  691. case Settings::NativeButton::B:
  692. controller.npad_button_state.b.Assign(current_status.value);
  693. controller.debug_pad_button_state.b.Assign(current_status.value);
  694. break;
  695. case Settings::NativeButton::X:
  696. controller.npad_button_state.x.Assign(current_status.value);
  697. controller.debug_pad_button_state.x.Assign(current_status.value);
  698. break;
  699. case Settings::NativeButton::Y:
  700. controller.npad_button_state.y.Assign(current_status.value);
  701. controller.debug_pad_button_state.y.Assign(current_status.value);
  702. break;
  703. case Settings::NativeButton::LStick:
  704. controller.npad_button_state.stick_l.Assign(current_status.value);
  705. break;
  706. case Settings::NativeButton::RStick:
  707. controller.npad_button_state.stick_r.Assign(current_status.value);
  708. break;
  709. case Settings::NativeButton::L:
  710. controller.npad_button_state.l.Assign(current_status.value);
  711. controller.debug_pad_button_state.l.Assign(current_status.value);
  712. break;
  713. case Settings::NativeButton::R:
  714. controller.npad_button_state.r.Assign(current_status.value);
  715. controller.debug_pad_button_state.r.Assign(current_status.value);
  716. break;
  717. case Settings::NativeButton::ZL:
  718. controller.npad_button_state.zl.Assign(current_status.value);
  719. controller.debug_pad_button_state.zl.Assign(current_status.value);
  720. break;
  721. case Settings::NativeButton::ZR:
  722. controller.npad_button_state.zr.Assign(current_status.value);
  723. controller.debug_pad_button_state.zr.Assign(current_status.value);
  724. break;
  725. case Settings::NativeButton::Plus:
  726. controller.npad_button_state.plus.Assign(current_status.value);
  727. controller.debug_pad_button_state.plus.Assign(current_status.value);
  728. break;
  729. case Settings::NativeButton::Minus:
  730. controller.npad_button_state.minus.Assign(current_status.value);
  731. controller.debug_pad_button_state.minus.Assign(current_status.value);
  732. break;
  733. case Settings::NativeButton::DLeft:
  734. controller.npad_button_state.left.Assign(current_status.value);
  735. controller.debug_pad_button_state.d_left.Assign(current_status.value);
  736. break;
  737. case Settings::NativeButton::DUp:
  738. controller.npad_button_state.up.Assign(current_status.value);
  739. controller.debug_pad_button_state.d_up.Assign(current_status.value);
  740. break;
  741. case Settings::NativeButton::DRight:
  742. controller.npad_button_state.right.Assign(current_status.value);
  743. controller.debug_pad_button_state.d_right.Assign(current_status.value);
  744. break;
  745. case Settings::NativeButton::DDown:
  746. controller.npad_button_state.down.Assign(current_status.value);
  747. controller.debug_pad_button_state.d_down.Assign(current_status.value);
  748. break;
  749. case Settings::NativeButton::SL:
  750. controller.npad_button_state.left_sl.Assign(current_status.value);
  751. controller.npad_button_state.right_sl.Assign(current_status.value);
  752. break;
  753. case Settings::NativeButton::SR:
  754. controller.npad_button_state.left_sr.Assign(current_status.value);
  755. controller.npad_button_state.right_sr.Assign(current_status.value);
  756. break;
  757. case Settings::NativeButton::Home:
  758. if (!system_buttons_enabled) {
  759. break;
  760. }
  761. controller.home_button_state.home.Assign(current_status.value);
  762. break;
  763. case Settings::NativeButton::Screenshot:
  764. if (!system_buttons_enabled) {
  765. break;
  766. }
  767. controller.capture_button_state.capture.Assign(current_status.value);
  768. break;
  769. }
  770. lock.unlock();
  771. if (!is_connected) {
  772. if (npad_id_type == NpadIdType::Player1 && npad_type != NpadStyleIndex::Handheld) {
  773. Connect();
  774. }
  775. if (npad_id_type == NpadIdType::Handheld && npad_type == NpadStyleIndex::Handheld) {
  776. Connect();
  777. }
  778. }
  779. TriggerOnChange(ControllerTriggerType::Button, true);
  780. }
  781. void EmulatedController::SetStick(const Common::Input::CallbackStatus& callback, std::size_t index,
  782. Common::UUID uuid) {
  783. if (index >= controller.stick_values.size()) {
  784. return;
  785. }
  786. auto trigger_guard =
  787. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Stick, !is_configuring); });
  788. std::scoped_lock lock{mutex};
  789. const auto stick_value = TransformToStick(callback);
  790. // Only read stick values that have the same uuid or are over the threshold to avoid flapping
  791. if (controller.stick_values[index].uuid != uuid) {
  792. const bool is_tas = uuid == TAS_UUID;
  793. if (is_tas && stick_value.x.value == 0 && stick_value.y.value == 0) {
  794. trigger_guard.Cancel();
  795. return;
  796. }
  797. if (!is_tas && !stick_value.down && !stick_value.up && !stick_value.left &&
  798. !stick_value.right) {
  799. trigger_guard.Cancel();
  800. return;
  801. }
  802. }
  803. controller.stick_values[index] = stick_value;
  804. controller.stick_values[index].uuid = uuid;
  805. if (is_configuring) {
  806. controller.analog_stick_state.left = {};
  807. controller.analog_stick_state.right = {};
  808. return;
  809. }
  810. const AnalogStickState stick{
  811. .x = static_cast<s32>(controller.stick_values[index].x.value * HID_JOYSTICK_MAX),
  812. .y = static_cast<s32>(controller.stick_values[index].y.value * HID_JOYSTICK_MAX),
  813. };
  814. switch (index) {
  815. case Settings::NativeAnalog::LStick:
  816. controller.analog_stick_state.left = stick;
  817. controller.npad_button_state.stick_l_left.Assign(controller.stick_values[index].left);
  818. controller.npad_button_state.stick_l_up.Assign(controller.stick_values[index].up);
  819. controller.npad_button_state.stick_l_right.Assign(controller.stick_values[index].right);
  820. controller.npad_button_state.stick_l_down.Assign(controller.stick_values[index].down);
  821. break;
  822. case Settings::NativeAnalog::RStick:
  823. controller.analog_stick_state.right = stick;
  824. controller.npad_button_state.stick_r_left.Assign(controller.stick_values[index].left);
  825. controller.npad_button_state.stick_r_up.Assign(controller.stick_values[index].up);
  826. controller.npad_button_state.stick_r_right.Assign(controller.stick_values[index].right);
  827. controller.npad_button_state.stick_r_down.Assign(controller.stick_values[index].down);
  828. break;
  829. }
  830. }
  831. void EmulatedController::SetTrigger(const Common::Input::CallbackStatus& callback,
  832. std::size_t index, Common::UUID uuid) {
  833. if (index >= controller.trigger_values.size()) {
  834. return;
  835. }
  836. auto trigger_guard =
  837. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Trigger, !is_configuring); });
  838. std::scoped_lock lock{mutex};
  839. const auto trigger_value = TransformToTrigger(callback);
  840. // Only read trigger values that have the same uuid or are pressed once
  841. if (controller.trigger_values[index].uuid != uuid) {
  842. if (!trigger_value.pressed.value) {
  843. return;
  844. }
  845. }
  846. controller.trigger_values[index] = trigger_value;
  847. controller.trigger_values[index].uuid = uuid;
  848. if (is_configuring) {
  849. controller.gc_trigger_state.left = 0;
  850. controller.gc_trigger_state.right = 0;
  851. return;
  852. }
  853. // Only GC controllers have analog triggers
  854. if (npad_type != NpadStyleIndex::GameCube) {
  855. trigger_guard.Cancel();
  856. return;
  857. }
  858. const auto& trigger = controller.trigger_values[index];
  859. switch (index) {
  860. case Settings::NativeTrigger::LTrigger:
  861. controller.gc_trigger_state.left = static_cast<s32>(trigger.analog.value * HID_TRIGGER_MAX);
  862. controller.npad_button_state.zl.Assign(trigger.pressed.value);
  863. break;
  864. case Settings::NativeTrigger::RTrigger:
  865. controller.gc_trigger_state.right =
  866. static_cast<s32>(trigger.analog.value * HID_TRIGGER_MAX);
  867. controller.npad_button_state.zr.Assign(trigger.pressed.value);
  868. break;
  869. }
  870. }
  871. void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback,
  872. std::size_t index) {
  873. if (index >= controller.motion_values.size()) {
  874. return;
  875. }
  876. SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::Motion, !is_configuring); });
  877. std::scoped_lock lock{mutex};
  878. auto& raw_status = controller.motion_values[index].raw_status;
  879. auto& emulated = controller.motion_values[index].emulated;
  880. raw_status = TransformToMotion(callback);
  881. emulated.SetAcceleration(Common::Vec3f{
  882. raw_status.accel.x.value,
  883. raw_status.accel.y.value,
  884. raw_status.accel.z.value,
  885. });
  886. emulated.SetGyroscope(Common::Vec3f{
  887. raw_status.gyro.x.value,
  888. raw_status.gyro.y.value,
  889. raw_status.gyro.z.value,
  890. });
  891. emulated.SetUserGyroThreshold(raw_status.gyro.x.properties.threshold);
  892. emulated.UpdateRotation(raw_status.delta_timestamp);
  893. emulated.UpdateOrientation(raw_status.delta_timestamp);
  894. auto& motion = controller.motion_state[index];
  895. motion.accel = emulated.GetAcceleration();
  896. motion.gyro = emulated.GetGyroscope();
  897. motion.rotation = emulated.GetRotations();
  898. motion.euler = emulated.GetEulerAngles();
  899. motion.orientation = emulated.GetOrientation();
  900. motion.is_at_rest = !emulated.IsMoving(motion_sensitivity);
  901. }
  902. void EmulatedController::SetColors(const Common::Input::CallbackStatus& callback,
  903. std::size_t index) {
  904. if (index >= controller.color_values.size()) {
  905. return;
  906. }
  907. auto trigger_guard =
  908. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Color, !is_configuring); });
  909. std::scoped_lock lock{mutex};
  910. controller.color_values[index] = TransformToColor(callback);
  911. if (is_configuring) {
  912. return;
  913. }
  914. if (controller.color_values[index].body == 0) {
  915. trigger_guard.Cancel();
  916. return;
  917. }
  918. controller.colors_state.fullkey = {
  919. .body = GetNpadColor(controller.color_values[index].body),
  920. .button = GetNpadColor(controller.color_values[index].buttons),
  921. };
  922. if (npad_type == NpadStyleIndex::ProController) {
  923. controller.colors_state.left = {
  924. .body = GetNpadColor(controller.color_values[index].left_grip),
  925. .button = GetNpadColor(controller.color_values[index].buttons),
  926. };
  927. controller.colors_state.right = {
  928. .body = GetNpadColor(controller.color_values[index].right_grip),
  929. .button = GetNpadColor(controller.color_values[index].buttons),
  930. };
  931. } else {
  932. switch (index) {
  933. case LeftIndex:
  934. controller.colors_state.left = {
  935. .body = GetNpadColor(controller.color_values[index].body),
  936. .button = GetNpadColor(controller.color_values[index].buttons),
  937. };
  938. break;
  939. case RightIndex:
  940. controller.colors_state.right = {
  941. .body = GetNpadColor(controller.color_values[index].body),
  942. .button = GetNpadColor(controller.color_values[index].buttons),
  943. };
  944. break;
  945. }
  946. }
  947. }
  948. void EmulatedController::SetBattery(const Common::Input::CallbackStatus& callback,
  949. std::size_t index) {
  950. if (index >= controller.battery_values.size()) {
  951. return;
  952. }
  953. SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::Battery, !is_configuring); });
  954. std::scoped_lock lock{mutex};
  955. controller.battery_values[index] = TransformToBattery(callback);
  956. if (is_configuring) {
  957. return;
  958. }
  959. bool is_charging = false;
  960. bool is_powered = false;
  961. NpadBatteryLevel battery_level = 0;
  962. switch (controller.battery_values[index]) {
  963. case Common::Input::BatteryLevel::Charging:
  964. is_charging = true;
  965. is_powered = true;
  966. battery_level = 6;
  967. break;
  968. case Common::Input::BatteryLevel::Medium:
  969. battery_level = 6;
  970. break;
  971. case Common::Input::BatteryLevel::Low:
  972. battery_level = 4;
  973. break;
  974. case Common::Input::BatteryLevel::Critical:
  975. battery_level = 2;
  976. break;
  977. case Common::Input::BatteryLevel::Empty:
  978. battery_level = 0;
  979. break;
  980. case Common::Input::BatteryLevel::None:
  981. case Common::Input::BatteryLevel::Full:
  982. default:
  983. is_powered = true;
  984. battery_level = 8;
  985. break;
  986. }
  987. switch (index) {
  988. case LeftIndex:
  989. controller.battery_state.left = {
  990. .is_powered = is_powered,
  991. .is_charging = is_charging,
  992. .battery_level = battery_level,
  993. };
  994. break;
  995. case RightIndex:
  996. controller.battery_state.right = {
  997. .is_powered = is_powered,
  998. .is_charging = is_charging,
  999. .battery_level = battery_level,
  1000. };
  1001. break;
  1002. case DualIndex:
  1003. controller.battery_state.dual = {
  1004. .is_powered = is_powered,
  1005. .is_charging = is_charging,
  1006. .battery_level = battery_level,
  1007. };
  1008. break;
  1009. }
  1010. }
  1011. void EmulatedController::SetCamera(const Common::Input::CallbackStatus& callback) {
  1012. SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::IrSensor, !is_configuring); });
  1013. std::scoped_lock lock{mutex};
  1014. controller.camera_values = TransformToCamera(callback);
  1015. if (is_configuring) {
  1016. return;
  1017. }
  1018. controller.camera_state.sample++;
  1019. controller.camera_state.format =
  1020. static_cast<Core::IrSensor::ImageTransferProcessorFormat>(controller.camera_values.format);
  1021. controller.camera_state.data = controller.camera_values.data;
  1022. }
  1023. void EmulatedController::SetRingAnalog(const Common::Input::CallbackStatus& callback) {
  1024. SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::RingController, !is_configuring); });
  1025. std::scoped_lock lock{mutex};
  1026. const auto force_value = TransformToStick(callback);
  1027. controller.ring_analog_value = force_value.x;
  1028. if (is_configuring) {
  1029. return;
  1030. }
  1031. controller.ring_analog_state.force = force_value.x.value;
  1032. }
  1033. void EmulatedController::SetNfc(const Common::Input::CallbackStatus& callback) {
  1034. SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::Nfc, !is_configuring); });
  1035. std::scoped_lock lock{mutex};
  1036. controller.nfc_values = TransformToNfc(callback);
  1037. if (is_configuring) {
  1038. return;
  1039. }
  1040. controller.nfc_state = {
  1041. controller.nfc_values.state,
  1042. controller.nfc_values.data,
  1043. };
  1044. }
  1045. bool EmulatedController::SetVibration(std::size_t device_index, VibrationValue vibration) {
  1046. if (device_index >= output_devices.size()) {
  1047. return false;
  1048. }
  1049. if (!output_devices[device_index]) {
  1050. return false;
  1051. }
  1052. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  1053. const auto& player = Settings::values.players.GetValue()[player_index];
  1054. const f32 strength = static_cast<f32>(player.vibration_strength) / 100.0f;
  1055. if (!player.vibration_enabled) {
  1056. return false;
  1057. }
  1058. // Exponential amplification is too strong at low amplitudes. Switch to a linear
  1059. // amplification if strength is set below 0.7f
  1060. const Common::Input::VibrationAmplificationType type =
  1061. strength > 0.7f ? Common::Input::VibrationAmplificationType::Exponential
  1062. : Common::Input::VibrationAmplificationType::Linear;
  1063. const Common::Input::VibrationStatus status = {
  1064. .low_amplitude = std::min(vibration.low_amplitude * strength, 1.0f),
  1065. .low_frequency = vibration.low_frequency,
  1066. .high_amplitude = std::min(vibration.high_amplitude * strength, 1.0f),
  1067. .high_frequency = vibration.high_frequency,
  1068. .type = type,
  1069. };
  1070. return output_devices[device_index]->SetVibration(status) ==
  1071. Common::Input::DriverResult::Success;
  1072. }
  1073. bool EmulatedController::IsVibrationEnabled(std::size_t device_index) {
  1074. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  1075. const auto& player = Settings::values.players.GetValue()[player_index];
  1076. if (!player.vibration_enabled) {
  1077. return false;
  1078. }
  1079. if (device_index >= output_devices.size()) {
  1080. return false;
  1081. }
  1082. if (!output_devices[device_index]) {
  1083. return false;
  1084. }
  1085. return output_devices[device_index]->IsVibrationEnabled();
  1086. }
  1087. Common::Input::DriverResult EmulatedController::SetPollingMode(
  1088. EmulatedDeviceIndex device_index, Common::Input::PollingMode polling_mode) {
  1089. LOG_INFO(Service_HID, "Set polling mode {}, device_index={}", polling_mode, device_index);
  1090. auto& left_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Left)];
  1091. auto& right_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1092. auto& nfc_output_device = output_devices[3];
  1093. if (device_index == EmulatedDeviceIndex::LeftIndex) {
  1094. return left_output_device->SetPollingMode(polling_mode);
  1095. }
  1096. if (device_index == EmulatedDeviceIndex::RightIndex) {
  1097. const auto virtual_nfc_result = nfc_output_device->SetPollingMode(polling_mode);
  1098. const auto mapped_nfc_result = right_output_device->SetPollingMode(polling_mode);
  1099. if (virtual_nfc_result == Common::Input::DriverResult::Success) {
  1100. return virtual_nfc_result;
  1101. }
  1102. return mapped_nfc_result;
  1103. }
  1104. left_output_device->SetPollingMode(polling_mode);
  1105. right_output_device->SetPollingMode(polling_mode);
  1106. nfc_output_device->SetPollingMode(polling_mode);
  1107. return Common::Input::DriverResult::Success;
  1108. }
  1109. bool EmulatedController::SetCameraFormat(
  1110. Core::IrSensor::ImageTransferProcessorFormat camera_format) {
  1111. LOG_INFO(Service_HID, "Set camera format {}", camera_format);
  1112. auto& right_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1113. auto& camera_output_device = output_devices[2];
  1114. if (right_output_device->SetCameraFormat(static_cast<Common::Input::CameraFormat>(
  1115. camera_format)) == Common::Input::DriverResult::Success) {
  1116. return true;
  1117. }
  1118. // Fallback to Qt camera if native device doesn't have support
  1119. return camera_output_device->SetCameraFormat(static_cast<Common::Input::CameraFormat>(
  1120. camera_format)) == Common::Input::DriverResult::Success;
  1121. }
  1122. Common::ParamPackage EmulatedController::GetRingParam() const {
  1123. return ring_params[0];
  1124. }
  1125. void EmulatedController::SetRingParam(Common::ParamPackage param) {
  1126. ring_params[0] = std::move(param);
  1127. ReloadInput();
  1128. }
  1129. bool EmulatedController::HasNfc() const {
  1130. const auto& nfc_output_device = output_devices[3];
  1131. switch (npad_type) {
  1132. case NpadStyleIndex::JoyconRight:
  1133. case NpadStyleIndex::JoyconDual:
  1134. case NpadStyleIndex::ProController:
  1135. case NpadStyleIndex::Handheld:
  1136. break;
  1137. default:
  1138. return false;
  1139. }
  1140. const bool has_virtual_nfc =
  1141. npad_id_type == NpadIdType::Player1 || npad_id_type == NpadIdType::Handheld;
  1142. const bool is_virtual_nfc_supported =
  1143. nfc_output_device->SupportsNfc() != Common::Input::NfcState::NotSupported;
  1144. return is_connected && (has_virtual_nfc && is_virtual_nfc_supported);
  1145. }
  1146. bool EmulatedController::WriteNfc(const std::vector<u8>& data) {
  1147. auto& nfc_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1148. auto& nfc_virtual_output_device = output_devices[3];
  1149. if (nfc_output_device->SupportsNfc() != Common::Input::NfcState::NotSupported) {
  1150. return nfc_output_device->WriteNfcData(data) == Common::Input::NfcState::Success;
  1151. }
  1152. return nfc_virtual_output_device->WriteNfcData(data) == Common::Input::NfcState::Success;
  1153. }
  1154. void EmulatedController::SetLedPattern() {
  1155. for (auto& device : output_devices) {
  1156. if (!device) {
  1157. continue;
  1158. }
  1159. const LedPattern pattern = GetLedPattern();
  1160. const Common::Input::LedStatus status = {
  1161. .led_1 = pattern.position1 != 0,
  1162. .led_2 = pattern.position2 != 0,
  1163. .led_3 = pattern.position3 != 0,
  1164. .led_4 = pattern.position4 != 0,
  1165. };
  1166. device->SetLED(status);
  1167. }
  1168. }
  1169. void EmulatedController::SetGyroscopeZeroDriftMode(GyroscopeZeroDriftMode mode) {
  1170. for (auto& motion : controller.motion_values) {
  1171. switch (mode) {
  1172. case GyroscopeZeroDriftMode::Loose:
  1173. motion_sensitivity = motion.emulated.IsAtRestLoose;
  1174. motion.emulated.SetGyroThreshold(motion.emulated.ThresholdLoose);
  1175. break;
  1176. case GyroscopeZeroDriftMode::Tight:
  1177. motion_sensitivity = motion.emulated.IsAtRestThight;
  1178. motion.emulated.SetGyroThreshold(motion.emulated.ThresholdThight);
  1179. break;
  1180. case GyroscopeZeroDriftMode::Standard:
  1181. default:
  1182. motion_sensitivity = motion.emulated.IsAtRestStandard;
  1183. motion.emulated.SetGyroThreshold(motion.emulated.ThresholdStandard);
  1184. break;
  1185. }
  1186. }
  1187. }
  1188. void EmulatedController::SetSupportedNpadStyleTag(NpadStyleTag supported_styles) {
  1189. supported_style_tag = supported_styles;
  1190. if (!is_connected) {
  1191. return;
  1192. }
  1193. // Attempt to reconnect with the original type
  1194. if (npad_type != original_npad_type) {
  1195. Disconnect();
  1196. const auto current_npad_type = npad_type;
  1197. SetNpadStyleIndex(original_npad_type);
  1198. if (IsControllerSupported()) {
  1199. Connect();
  1200. return;
  1201. }
  1202. SetNpadStyleIndex(current_npad_type);
  1203. Connect();
  1204. }
  1205. if (IsControllerSupported()) {
  1206. return;
  1207. }
  1208. Disconnect();
  1209. // Fallback Fullkey controllers to Pro controllers
  1210. if (IsControllerFullkey() && supported_style_tag.fullkey) {
  1211. LOG_WARNING(Service_HID, "Reconnecting controller type {} as Pro controller", npad_type);
  1212. SetNpadStyleIndex(NpadStyleIndex::ProController);
  1213. Connect();
  1214. return;
  1215. }
  1216. // Fallback Dual joycon controllers to Pro controllers
  1217. if (npad_type == NpadStyleIndex::JoyconDual && supported_style_tag.fullkey) {
  1218. LOG_WARNING(Service_HID, "Reconnecting controller type {} as Pro controller", npad_type);
  1219. SetNpadStyleIndex(NpadStyleIndex::ProController);
  1220. Connect();
  1221. return;
  1222. }
  1223. // Fallback Pro controllers to Dual joycon
  1224. if (npad_type == NpadStyleIndex::ProController && supported_style_tag.joycon_dual) {
  1225. LOG_WARNING(Service_HID, "Reconnecting controller type {} as Dual Joycons", npad_type);
  1226. SetNpadStyleIndex(NpadStyleIndex::JoyconDual);
  1227. Connect();
  1228. return;
  1229. }
  1230. LOG_ERROR(Service_HID, "Controller type {} is not supported. Disconnecting controller",
  1231. npad_type);
  1232. }
  1233. bool EmulatedController::IsControllerFullkey(bool use_temporary_value) const {
  1234. std::scoped_lock lock{mutex};
  1235. const auto type = is_configuring && use_temporary_value ? tmp_npad_type : npad_type;
  1236. switch (type) {
  1237. case NpadStyleIndex::ProController:
  1238. case NpadStyleIndex::GameCube:
  1239. case NpadStyleIndex::NES:
  1240. case NpadStyleIndex::SNES:
  1241. case NpadStyleIndex::N64:
  1242. case NpadStyleIndex::SegaGenesis:
  1243. return true;
  1244. default:
  1245. return false;
  1246. }
  1247. }
  1248. bool EmulatedController::IsControllerSupported(bool use_temporary_value) const {
  1249. std::scoped_lock lock{mutex};
  1250. const auto type = is_configuring && use_temporary_value ? tmp_npad_type : npad_type;
  1251. switch (type) {
  1252. case NpadStyleIndex::ProController:
  1253. return supported_style_tag.fullkey.As<bool>();
  1254. case NpadStyleIndex::Handheld:
  1255. return supported_style_tag.handheld.As<bool>();
  1256. case NpadStyleIndex::JoyconDual:
  1257. return supported_style_tag.joycon_dual.As<bool>();
  1258. case NpadStyleIndex::JoyconLeft:
  1259. return supported_style_tag.joycon_left.As<bool>();
  1260. case NpadStyleIndex::JoyconRight:
  1261. return supported_style_tag.joycon_right.As<bool>();
  1262. case NpadStyleIndex::GameCube:
  1263. return supported_style_tag.gamecube.As<bool>();
  1264. case NpadStyleIndex::Pokeball:
  1265. return supported_style_tag.palma.As<bool>();
  1266. case NpadStyleIndex::NES:
  1267. return supported_style_tag.lark.As<bool>();
  1268. case NpadStyleIndex::SNES:
  1269. return supported_style_tag.lucia.As<bool>();
  1270. case NpadStyleIndex::N64:
  1271. return supported_style_tag.lagoon.As<bool>();
  1272. case NpadStyleIndex::SegaGenesis:
  1273. return supported_style_tag.lager.As<bool>();
  1274. default:
  1275. return false;
  1276. }
  1277. }
  1278. void EmulatedController::Connect(bool use_temporary_value) {
  1279. if (!IsControllerSupported(use_temporary_value)) {
  1280. const auto type = is_configuring && use_temporary_value ? tmp_npad_type : npad_type;
  1281. LOG_ERROR(Service_HID, "Controller type {} is not supported", type);
  1282. return;
  1283. }
  1284. auto trigger_guard =
  1285. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Connected, !is_configuring); });
  1286. std::scoped_lock lock{mutex};
  1287. if (is_configuring) {
  1288. tmp_is_connected = true;
  1289. return;
  1290. }
  1291. if (is_connected) {
  1292. trigger_guard.Cancel();
  1293. return;
  1294. }
  1295. is_connected = true;
  1296. }
  1297. void EmulatedController::Disconnect() {
  1298. auto trigger_guard =
  1299. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Disconnected, !is_configuring); });
  1300. std::scoped_lock lock{mutex};
  1301. if (is_configuring) {
  1302. tmp_is_connected = false;
  1303. return;
  1304. }
  1305. if (!is_connected) {
  1306. trigger_guard.Cancel();
  1307. return;
  1308. }
  1309. is_connected = false;
  1310. }
  1311. bool EmulatedController::IsConnected(bool get_temporary_value) const {
  1312. std::scoped_lock lock{mutex};
  1313. if (get_temporary_value && is_configuring) {
  1314. return tmp_is_connected;
  1315. }
  1316. return is_connected;
  1317. }
  1318. NpadIdType EmulatedController::GetNpadIdType() const {
  1319. std::scoped_lock lock{mutex};
  1320. return npad_id_type;
  1321. }
  1322. NpadStyleIndex EmulatedController::GetNpadStyleIndex(bool get_temporary_value) const {
  1323. std::scoped_lock lock{mutex};
  1324. if (get_temporary_value && is_configuring) {
  1325. return tmp_npad_type;
  1326. }
  1327. return npad_type;
  1328. }
  1329. void EmulatedController::SetNpadStyleIndex(NpadStyleIndex npad_type_) {
  1330. auto trigger_guard =
  1331. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Type, !is_configuring); });
  1332. std::scoped_lock lock{mutex};
  1333. if (is_configuring) {
  1334. if (tmp_npad_type == npad_type_) {
  1335. trigger_guard.Cancel();
  1336. return;
  1337. }
  1338. tmp_npad_type = npad_type_;
  1339. return;
  1340. }
  1341. if (npad_type == npad_type_) {
  1342. trigger_guard.Cancel();
  1343. return;
  1344. }
  1345. if (is_connected) {
  1346. LOG_WARNING(Service_HID, "Controller {} type changed while it's connected",
  1347. NpadIdTypeToIndex(npad_id_type));
  1348. }
  1349. npad_type = npad_type_;
  1350. }
  1351. LedPattern EmulatedController::GetLedPattern() const {
  1352. switch (npad_id_type) {
  1353. case NpadIdType::Player1:
  1354. return LedPattern{1, 0, 0, 0};
  1355. case NpadIdType::Player2:
  1356. return LedPattern{1, 1, 0, 0};
  1357. case NpadIdType::Player3:
  1358. return LedPattern{1, 1, 1, 0};
  1359. case NpadIdType::Player4:
  1360. return LedPattern{1, 1, 1, 1};
  1361. case NpadIdType::Player5:
  1362. return LedPattern{1, 0, 0, 1};
  1363. case NpadIdType::Player6:
  1364. return LedPattern{1, 0, 1, 0};
  1365. case NpadIdType::Player7:
  1366. return LedPattern{1, 0, 1, 1};
  1367. case NpadIdType::Player8:
  1368. return LedPattern{0, 1, 1, 0};
  1369. default:
  1370. return LedPattern{0, 0, 0, 0};
  1371. }
  1372. }
  1373. ButtonValues EmulatedController::GetButtonsValues() const {
  1374. std::scoped_lock lock{mutex};
  1375. return controller.button_values;
  1376. }
  1377. SticksValues EmulatedController::GetSticksValues() const {
  1378. std::scoped_lock lock{mutex};
  1379. return controller.stick_values;
  1380. }
  1381. TriggerValues EmulatedController::GetTriggersValues() const {
  1382. std::scoped_lock lock{mutex};
  1383. return controller.trigger_values;
  1384. }
  1385. ControllerMotionValues EmulatedController::GetMotionValues() const {
  1386. std::scoped_lock lock{mutex};
  1387. return controller.motion_values;
  1388. }
  1389. ColorValues EmulatedController::GetColorsValues() const {
  1390. std::scoped_lock lock{mutex};
  1391. return controller.color_values;
  1392. }
  1393. BatteryValues EmulatedController::GetBatteryValues() const {
  1394. std::scoped_lock lock{mutex};
  1395. return controller.battery_values;
  1396. }
  1397. CameraValues EmulatedController::GetCameraValues() const {
  1398. std::scoped_lock lock{mutex};
  1399. return controller.camera_values;
  1400. }
  1401. RingAnalogValue EmulatedController::GetRingSensorValues() const {
  1402. return controller.ring_analog_value;
  1403. }
  1404. HomeButtonState EmulatedController::GetHomeButtons() const {
  1405. std::scoped_lock lock{mutex};
  1406. if (is_configuring) {
  1407. return {};
  1408. }
  1409. return controller.home_button_state;
  1410. }
  1411. CaptureButtonState EmulatedController::GetCaptureButtons() const {
  1412. std::scoped_lock lock{mutex};
  1413. if (is_configuring) {
  1414. return {};
  1415. }
  1416. return controller.capture_button_state;
  1417. }
  1418. NpadButtonState EmulatedController::GetNpadButtons() const {
  1419. std::scoped_lock lock{mutex};
  1420. if (is_configuring) {
  1421. return {};
  1422. }
  1423. return {controller.npad_button_state.raw & GetTurboButtonMask()};
  1424. }
  1425. DebugPadButton EmulatedController::GetDebugPadButtons() const {
  1426. std::scoped_lock lock{mutex};
  1427. if (is_configuring) {
  1428. return {};
  1429. }
  1430. return controller.debug_pad_button_state;
  1431. }
  1432. AnalogSticks EmulatedController::GetSticks() const {
  1433. std::scoped_lock lock{mutex};
  1434. if (is_configuring) {
  1435. return {};
  1436. }
  1437. return controller.analog_stick_state;
  1438. }
  1439. NpadGcTriggerState EmulatedController::GetTriggers() const {
  1440. std::scoped_lock lock{mutex};
  1441. if (is_configuring) {
  1442. return {};
  1443. }
  1444. return controller.gc_trigger_state;
  1445. }
  1446. MotionState EmulatedController::GetMotions() const {
  1447. std::unique_lock lock{mutex};
  1448. return controller.motion_state;
  1449. }
  1450. ControllerColors EmulatedController::GetColors() const {
  1451. std::scoped_lock lock{mutex};
  1452. return controller.colors_state;
  1453. }
  1454. BatteryLevelState EmulatedController::GetBattery() const {
  1455. std::scoped_lock lock{mutex};
  1456. return controller.battery_state;
  1457. }
  1458. const CameraState& EmulatedController::GetCamera() const {
  1459. std::scoped_lock lock{mutex};
  1460. return controller.camera_state;
  1461. }
  1462. RingSensorForce EmulatedController::GetRingSensorForce() const {
  1463. return controller.ring_analog_state;
  1464. }
  1465. const NfcState& EmulatedController::GetNfc() const {
  1466. std::scoped_lock lock{mutex};
  1467. return controller.nfc_state;
  1468. }
  1469. NpadColor EmulatedController::GetNpadColor(u32 color) {
  1470. return {
  1471. .r = static_cast<u8>((color >> 16) & 0xFF),
  1472. .g = static_cast<u8>((color >> 8) & 0xFF),
  1473. .b = static_cast<u8>(color & 0xFF),
  1474. .a = 0xff,
  1475. };
  1476. }
  1477. void EmulatedController::TriggerOnChange(ControllerTriggerType type, bool is_npad_service_update) {
  1478. std::scoped_lock lock{callback_mutex};
  1479. for (const auto& poller_pair : callback_list) {
  1480. const ControllerUpdateCallback& poller = poller_pair.second;
  1481. if (!is_npad_service_update && poller.is_npad_service) {
  1482. continue;
  1483. }
  1484. if (poller.on_change) {
  1485. poller.on_change(type);
  1486. }
  1487. }
  1488. }
  1489. int EmulatedController::SetCallback(ControllerUpdateCallback update_callback) {
  1490. std::scoped_lock lock{callback_mutex};
  1491. callback_list.insert_or_assign(last_callback_key, std::move(update_callback));
  1492. return last_callback_key++;
  1493. }
  1494. void EmulatedController::DeleteCallback(int key) {
  1495. std::scoped_lock lock{callback_mutex};
  1496. const auto& iterator = callback_list.find(key);
  1497. if (iterator == callback_list.end()) {
  1498. LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
  1499. return;
  1500. }
  1501. callback_list.erase(iterator);
  1502. }
  1503. void EmulatedController::StatusUpdate() {
  1504. turbo_button_state = (turbo_button_state + 1) % (TURBO_BUTTON_DELAY * 2);
  1505. // Some drivers like key motion need constant refreshing
  1506. for (std::size_t index = 0; index < motion_devices.size(); ++index) {
  1507. const auto& raw_status = controller.motion_values[index].raw_status;
  1508. auto& device = motion_devices[index];
  1509. if (!raw_status.force_update) {
  1510. continue;
  1511. }
  1512. if (!device) {
  1513. continue;
  1514. }
  1515. device->ForceUpdate();
  1516. }
  1517. }
  1518. NpadButton EmulatedController::GetTurboButtonMask() const {
  1519. // Apply no mask when disabled
  1520. if (turbo_button_state < TURBO_BUTTON_DELAY) {
  1521. return {NpadButton::All};
  1522. }
  1523. NpadButtonState button_mask{};
  1524. for (std::size_t index = 0; index < controller.button_values.size(); ++index) {
  1525. if (!controller.button_values[index].turbo) {
  1526. continue;
  1527. }
  1528. switch (index) {
  1529. case Settings::NativeButton::A:
  1530. button_mask.a.Assign(1);
  1531. break;
  1532. case Settings::NativeButton::B:
  1533. button_mask.b.Assign(1);
  1534. break;
  1535. case Settings::NativeButton::X:
  1536. button_mask.x.Assign(1);
  1537. break;
  1538. case Settings::NativeButton::Y:
  1539. button_mask.y.Assign(1);
  1540. break;
  1541. case Settings::NativeButton::L:
  1542. button_mask.l.Assign(1);
  1543. break;
  1544. case Settings::NativeButton::R:
  1545. button_mask.r.Assign(1);
  1546. break;
  1547. case Settings::NativeButton::ZL:
  1548. button_mask.zl.Assign(1);
  1549. break;
  1550. case Settings::NativeButton::ZR:
  1551. button_mask.zr.Assign(1);
  1552. break;
  1553. case Settings::NativeButton::DLeft:
  1554. button_mask.left.Assign(1);
  1555. break;
  1556. case Settings::NativeButton::DUp:
  1557. button_mask.up.Assign(1);
  1558. break;
  1559. case Settings::NativeButton::DRight:
  1560. button_mask.right.Assign(1);
  1561. break;
  1562. case Settings::NativeButton::DDown:
  1563. button_mask.down.Assign(1);
  1564. break;
  1565. case Settings::NativeButton::SL:
  1566. button_mask.left_sl.Assign(1);
  1567. button_mask.right_sl.Assign(1);
  1568. break;
  1569. case Settings::NativeButton::SR:
  1570. button_mask.left_sr.Assign(1);
  1571. button_mask.right_sr.Assign(1);
  1572. break;
  1573. default:
  1574. break;
  1575. }
  1576. }
  1577. return static_cast<NpadButton>(~button_mask.raw);
  1578. }
  1579. } // namespace Core::HID