emulated_controller.cpp 67 KB

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