emulated_controller.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  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. is_configuring = true;
  496. tmp_is_connected = is_connected;
  497. tmp_npad_type = npad_type;
  498. }
  499. void EmulatedController::DisableConfiguration() {
  500. is_configuring = false;
  501. // Get Joycon colors before turning on the controller
  502. for (const auto& color_device : color_devices) {
  503. color_device->ForceUpdate();
  504. }
  505. // Apply temporary npad type to the real controller
  506. if (tmp_npad_type != npad_type) {
  507. if (is_connected) {
  508. Disconnect();
  509. }
  510. SetNpadStyleIndex(tmp_npad_type);
  511. original_npad_type = tmp_npad_type;
  512. }
  513. // Apply temporary connected status to the real controller
  514. if (tmp_is_connected != is_connected) {
  515. if (tmp_is_connected) {
  516. Connect();
  517. return;
  518. }
  519. Disconnect();
  520. }
  521. }
  522. void EmulatedController::EnableSystemButtons() {
  523. std::scoped_lock lock{mutex};
  524. system_buttons_enabled = true;
  525. }
  526. void EmulatedController::DisableSystemButtons() {
  527. std::scoped_lock lock{mutex};
  528. system_buttons_enabled = false;
  529. controller.home_button_state.raw = 0;
  530. controller.capture_button_state.raw = 0;
  531. }
  532. void EmulatedController::ResetSystemButtons() {
  533. std::scoped_lock lock{mutex};
  534. controller.home_button_state.home.Assign(false);
  535. controller.capture_button_state.capture.Assign(false);
  536. }
  537. bool EmulatedController::IsConfiguring() const {
  538. return is_configuring;
  539. }
  540. void EmulatedController::SaveCurrentConfig() {
  541. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  542. auto& player = Settings::values.players.GetValue()[player_index];
  543. player.connected = is_connected;
  544. player.controller_type = MapNPadToSettingsType(npad_type);
  545. for (std::size_t index = 0; index < player.buttons.size(); ++index) {
  546. player.buttons[index] = button_params[index].Serialize();
  547. }
  548. for (std::size_t index = 0; index < player.analogs.size(); ++index) {
  549. player.analogs[index] = stick_params[index].Serialize();
  550. }
  551. for (std::size_t index = 0; index < player.motions.size(); ++index) {
  552. player.motions[index] = motion_params[index].Serialize();
  553. }
  554. if (npad_id_type == NpadIdType::Player1) {
  555. Settings::values.ringcon_analogs = ring_params[0].Serialize();
  556. }
  557. }
  558. void EmulatedController::RestoreConfig() {
  559. if (!is_configuring) {
  560. return;
  561. }
  562. ReloadFromSettings();
  563. }
  564. std::vector<Common::ParamPackage> EmulatedController::GetMappedDevices() const {
  565. std::vector<Common::ParamPackage> devices;
  566. for (const auto& param : button_params) {
  567. if (!param.Has("engine")) {
  568. continue;
  569. }
  570. const auto devices_it = std::find_if(
  571. devices.begin(), devices.end(), [&param](const Common::ParamPackage& param_) {
  572. return param.Get("engine", "") == param_.Get("engine", "") &&
  573. param.Get("guid", "") == param_.Get("guid", "") &&
  574. param.Get("port", 0) == param_.Get("port", 0) &&
  575. param.Get("pad", 0) == param_.Get("pad", 0);
  576. });
  577. if (devices_it != devices.end()) {
  578. continue;
  579. }
  580. auto& device = devices.emplace_back();
  581. device.Set("engine", param.Get("engine", ""));
  582. device.Set("guid", param.Get("guid", ""));
  583. device.Set("port", param.Get("port", 0));
  584. device.Set("pad", param.Get("pad", 0));
  585. }
  586. for (const auto& param : stick_params) {
  587. if (!param.Has("engine")) {
  588. continue;
  589. }
  590. if (param.Get("engine", "") == "analog_from_button") {
  591. continue;
  592. }
  593. const auto devices_it = std::find_if(
  594. devices.begin(), devices.end(), [&param](const Common::ParamPackage& param_) {
  595. return param.Get("engine", "") == param_.Get("engine", "") &&
  596. param.Get("guid", "") == param_.Get("guid", "") &&
  597. param.Get("port", 0) == param_.Get("port", 0) &&
  598. param.Get("pad", 0) == param_.Get("pad", 0);
  599. });
  600. if (devices_it != devices.end()) {
  601. continue;
  602. }
  603. auto& device = devices.emplace_back();
  604. device.Set("engine", param.Get("engine", ""));
  605. device.Set("guid", param.Get("guid", ""));
  606. device.Set("port", param.Get("port", 0));
  607. device.Set("pad", param.Get("pad", 0));
  608. }
  609. return devices;
  610. }
  611. Common::ParamPackage EmulatedController::GetButtonParam(std::size_t index) const {
  612. if (index >= button_params.size()) {
  613. return {};
  614. }
  615. return button_params[index];
  616. }
  617. Common::ParamPackage EmulatedController::GetStickParam(std::size_t index) const {
  618. if (index >= stick_params.size()) {
  619. return {};
  620. }
  621. return stick_params[index];
  622. }
  623. Common::ParamPackage EmulatedController::GetMotionParam(std::size_t index) const {
  624. if (index >= motion_params.size()) {
  625. return {};
  626. }
  627. return motion_params[index];
  628. }
  629. void EmulatedController::SetButtonParam(std::size_t index, Common::ParamPackage param) {
  630. if (index >= button_params.size()) {
  631. return;
  632. }
  633. button_params[index] = std::move(param);
  634. ReloadInput();
  635. }
  636. void EmulatedController::SetStickParam(std::size_t index, Common::ParamPackage param) {
  637. if (index >= stick_params.size()) {
  638. return;
  639. }
  640. stick_params[index] = std::move(param);
  641. ReloadInput();
  642. }
  643. void EmulatedController::SetMotionParam(std::size_t index, Common::ParamPackage param) {
  644. if (index >= motion_params.size()) {
  645. return;
  646. }
  647. motion_params[index] = std::move(param);
  648. ReloadInput();
  649. }
  650. void EmulatedController::StartMotionCalibration() {
  651. for (ControllerMotionInfo& motion : controller.motion_values) {
  652. motion.emulated.Calibrate();
  653. }
  654. }
  655. void EmulatedController::SetButton(const Common::Input::CallbackStatus& callback, std::size_t index,
  656. Common::UUID uuid) {
  657. if (index >= controller.button_values.size()) {
  658. return;
  659. }
  660. std::unique_lock lock{mutex};
  661. bool value_changed = false;
  662. const auto new_status = TransformToButton(callback);
  663. auto& current_status = controller.button_values[index];
  664. // Only read button values that have the same uuid or are pressed once
  665. if (current_status.uuid != uuid) {
  666. if (!new_status.value) {
  667. return;
  668. }
  669. }
  670. current_status.toggle = new_status.toggle;
  671. current_status.turbo = new_status.turbo;
  672. current_status.uuid = uuid;
  673. // Update button status with current
  674. if (!current_status.toggle) {
  675. current_status.locked = false;
  676. if (current_status.value != new_status.value) {
  677. current_status.value = new_status.value;
  678. value_changed = true;
  679. }
  680. } else {
  681. // Toggle button and lock status
  682. if (new_status.value && !current_status.locked) {
  683. current_status.locked = true;
  684. current_status.value = !current_status.value;
  685. value_changed = true;
  686. }
  687. // Unlock button ready for next press
  688. if (!new_status.value && current_status.locked) {
  689. current_status.locked = false;
  690. }
  691. }
  692. if (!value_changed) {
  693. return;
  694. }
  695. if (is_configuring) {
  696. controller.npad_button_state.raw = NpadButton::None;
  697. controller.debug_pad_button_state.raw = 0;
  698. controller.home_button_state.raw = 0;
  699. controller.capture_button_state.raw = 0;
  700. lock.unlock();
  701. TriggerOnChange(ControllerTriggerType::Button, false);
  702. return;
  703. }
  704. // GC controllers have triggers not buttons
  705. if (npad_type == NpadStyleIndex::GameCube) {
  706. if (index == Settings::NativeButton::ZR) {
  707. return;
  708. }
  709. if (index == Settings::NativeButton::ZL) {
  710. return;
  711. }
  712. }
  713. switch (index) {
  714. case Settings::NativeButton::A:
  715. controller.npad_button_state.a.Assign(current_status.value);
  716. controller.debug_pad_button_state.a.Assign(current_status.value);
  717. break;
  718. case Settings::NativeButton::B:
  719. controller.npad_button_state.b.Assign(current_status.value);
  720. controller.debug_pad_button_state.b.Assign(current_status.value);
  721. break;
  722. case Settings::NativeButton::X:
  723. controller.npad_button_state.x.Assign(current_status.value);
  724. controller.debug_pad_button_state.x.Assign(current_status.value);
  725. break;
  726. case Settings::NativeButton::Y:
  727. controller.npad_button_state.y.Assign(current_status.value);
  728. controller.debug_pad_button_state.y.Assign(current_status.value);
  729. break;
  730. case Settings::NativeButton::LStick:
  731. controller.npad_button_state.stick_l.Assign(current_status.value);
  732. break;
  733. case Settings::NativeButton::RStick:
  734. controller.npad_button_state.stick_r.Assign(current_status.value);
  735. break;
  736. case Settings::NativeButton::L:
  737. controller.npad_button_state.l.Assign(current_status.value);
  738. controller.debug_pad_button_state.l.Assign(current_status.value);
  739. break;
  740. case Settings::NativeButton::R:
  741. controller.npad_button_state.r.Assign(current_status.value);
  742. controller.debug_pad_button_state.r.Assign(current_status.value);
  743. break;
  744. case Settings::NativeButton::ZL:
  745. controller.npad_button_state.zl.Assign(current_status.value);
  746. controller.debug_pad_button_state.zl.Assign(current_status.value);
  747. break;
  748. case Settings::NativeButton::ZR:
  749. controller.npad_button_state.zr.Assign(current_status.value);
  750. controller.debug_pad_button_state.zr.Assign(current_status.value);
  751. break;
  752. case Settings::NativeButton::Plus:
  753. controller.npad_button_state.plus.Assign(current_status.value);
  754. controller.debug_pad_button_state.plus.Assign(current_status.value);
  755. break;
  756. case Settings::NativeButton::Minus:
  757. controller.npad_button_state.minus.Assign(current_status.value);
  758. controller.debug_pad_button_state.minus.Assign(current_status.value);
  759. break;
  760. case Settings::NativeButton::DLeft:
  761. controller.npad_button_state.left.Assign(current_status.value);
  762. controller.debug_pad_button_state.d_left.Assign(current_status.value);
  763. break;
  764. case Settings::NativeButton::DUp:
  765. controller.npad_button_state.up.Assign(current_status.value);
  766. controller.debug_pad_button_state.d_up.Assign(current_status.value);
  767. break;
  768. case Settings::NativeButton::DRight:
  769. controller.npad_button_state.right.Assign(current_status.value);
  770. controller.debug_pad_button_state.d_right.Assign(current_status.value);
  771. break;
  772. case Settings::NativeButton::DDown:
  773. controller.npad_button_state.down.Assign(current_status.value);
  774. controller.debug_pad_button_state.d_down.Assign(current_status.value);
  775. break;
  776. case Settings::NativeButton::SL:
  777. controller.npad_button_state.left_sl.Assign(current_status.value);
  778. controller.npad_button_state.right_sl.Assign(current_status.value);
  779. break;
  780. case Settings::NativeButton::SR:
  781. controller.npad_button_state.left_sr.Assign(current_status.value);
  782. controller.npad_button_state.right_sr.Assign(current_status.value);
  783. break;
  784. case Settings::NativeButton::Home:
  785. if (!system_buttons_enabled) {
  786. break;
  787. }
  788. controller.home_button_state.home.Assign(current_status.value);
  789. break;
  790. case Settings::NativeButton::Screenshot:
  791. if (!system_buttons_enabled) {
  792. break;
  793. }
  794. controller.capture_button_state.capture.Assign(current_status.value);
  795. break;
  796. }
  797. lock.unlock();
  798. if (!is_connected) {
  799. if (npad_id_type == NpadIdType::Player1 && npad_type != NpadStyleIndex::Handheld) {
  800. Connect();
  801. }
  802. if (npad_id_type == NpadIdType::Handheld && npad_type == NpadStyleIndex::Handheld) {
  803. Connect();
  804. }
  805. }
  806. TriggerOnChange(ControllerTriggerType::Button, true);
  807. }
  808. void EmulatedController::SetStick(const Common::Input::CallbackStatus& callback, std::size_t index,
  809. Common::UUID uuid) {
  810. if (index >= controller.stick_values.size()) {
  811. return;
  812. }
  813. auto trigger_guard =
  814. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Stick, !is_configuring); });
  815. std::scoped_lock lock{mutex};
  816. const auto stick_value = TransformToStick(callback);
  817. // Only read stick values that have the same uuid or are over the threshold to avoid flapping
  818. if (controller.stick_values[index].uuid != uuid) {
  819. const bool is_tas = uuid == TAS_UUID;
  820. if (is_tas && stick_value.x.value == 0 && stick_value.y.value == 0) {
  821. trigger_guard.Cancel();
  822. return;
  823. }
  824. if (!is_tas && !stick_value.down && !stick_value.up && !stick_value.left &&
  825. !stick_value.right) {
  826. trigger_guard.Cancel();
  827. return;
  828. }
  829. }
  830. controller.stick_values[index] = stick_value;
  831. controller.stick_values[index].uuid = uuid;
  832. if (is_configuring) {
  833. controller.analog_stick_state.left = {};
  834. controller.analog_stick_state.right = {};
  835. return;
  836. }
  837. const AnalogStickState stick{
  838. .x = static_cast<s32>(controller.stick_values[index].x.value * HID_JOYSTICK_MAX),
  839. .y = static_cast<s32>(controller.stick_values[index].y.value * HID_JOYSTICK_MAX),
  840. };
  841. switch (index) {
  842. case Settings::NativeAnalog::LStick:
  843. controller.analog_stick_state.left = stick;
  844. controller.npad_button_state.stick_l_left.Assign(controller.stick_values[index].left);
  845. controller.npad_button_state.stick_l_up.Assign(controller.stick_values[index].up);
  846. controller.npad_button_state.stick_l_right.Assign(controller.stick_values[index].right);
  847. controller.npad_button_state.stick_l_down.Assign(controller.stick_values[index].down);
  848. break;
  849. case Settings::NativeAnalog::RStick:
  850. controller.analog_stick_state.right = stick;
  851. controller.npad_button_state.stick_r_left.Assign(controller.stick_values[index].left);
  852. controller.npad_button_state.stick_r_up.Assign(controller.stick_values[index].up);
  853. controller.npad_button_state.stick_r_right.Assign(controller.stick_values[index].right);
  854. controller.npad_button_state.stick_r_down.Assign(controller.stick_values[index].down);
  855. break;
  856. }
  857. }
  858. void EmulatedController::SetTrigger(const Common::Input::CallbackStatus& callback,
  859. std::size_t index, Common::UUID uuid) {
  860. if (index >= controller.trigger_values.size()) {
  861. return;
  862. }
  863. auto trigger_guard =
  864. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Trigger, !is_configuring); });
  865. std::scoped_lock lock{mutex};
  866. const auto trigger_value = TransformToTrigger(callback);
  867. // Only read trigger values that have the same uuid or are pressed once
  868. if (controller.trigger_values[index].uuid != uuid) {
  869. if (!trigger_value.pressed.value) {
  870. return;
  871. }
  872. }
  873. controller.trigger_values[index] = trigger_value;
  874. controller.trigger_values[index].uuid = uuid;
  875. if (is_configuring) {
  876. controller.gc_trigger_state.left = 0;
  877. controller.gc_trigger_state.right = 0;
  878. return;
  879. }
  880. // Only GC controllers have analog triggers
  881. if (npad_type != NpadStyleIndex::GameCube) {
  882. trigger_guard.Cancel();
  883. return;
  884. }
  885. const auto& trigger = controller.trigger_values[index];
  886. switch (index) {
  887. case Settings::NativeTrigger::LTrigger:
  888. controller.gc_trigger_state.left = static_cast<s32>(trigger.analog.value * HID_TRIGGER_MAX);
  889. controller.npad_button_state.zl.Assign(trigger.pressed.value);
  890. break;
  891. case Settings::NativeTrigger::RTrigger:
  892. controller.gc_trigger_state.right =
  893. static_cast<s32>(trigger.analog.value * HID_TRIGGER_MAX);
  894. controller.npad_button_state.zr.Assign(trigger.pressed.value);
  895. break;
  896. }
  897. }
  898. void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback,
  899. std::size_t index) {
  900. if (index >= controller.motion_values.size()) {
  901. return;
  902. }
  903. SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::Motion, !is_configuring); });
  904. std::scoped_lock lock{mutex};
  905. auto& raw_status = controller.motion_values[index].raw_status;
  906. auto& emulated = controller.motion_values[index].emulated;
  907. raw_status = TransformToMotion(callback);
  908. emulated.SetAcceleration(Common::Vec3f{
  909. raw_status.accel.x.value,
  910. raw_status.accel.y.value,
  911. raw_status.accel.z.value,
  912. });
  913. emulated.SetGyroscope(Common::Vec3f{
  914. raw_status.gyro.x.value,
  915. raw_status.gyro.y.value,
  916. raw_status.gyro.z.value,
  917. });
  918. emulated.SetUserGyroThreshold(raw_status.gyro.x.properties.threshold);
  919. emulated.UpdateRotation(raw_status.delta_timestamp);
  920. emulated.UpdateOrientation(raw_status.delta_timestamp);
  921. auto& motion = controller.motion_state[index];
  922. motion.accel = emulated.GetAcceleration();
  923. motion.gyro = emulated.GetGyroscope();
  924. motion.rotation = emulated.GetRotations();
  925. motion.euler = emulated.GetEulerAngles();
  926. motion.orientation = emulated.GetOrientation();
  927. motion.is_at_rest = !emulated.IsMoving(motion_sensitivity);
  928. }
  929. void EmulatedController::SetColors(const Common::Input::CallbackStatus& callback,
  930. std::size_t index) {
  931. if (index >= controller.color_values.size()) {
  932. return;
  933. }
  934. auto trigger_guard =
  935. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Color, !is_configuring); });
  936. std::scoped_lock lock{mutex};
  937. controller.color_values[index] = TransformToColor(callback);
  938. if (is_configuring) {
  939. return;
  940. }
  941. if (controller.color_values[index].body == 0) {
  942. trigger_guard.Cancel();
  943. return;
  944. }
  945. controller.colors_state.fullkey = {
  946. .body = GetNpadColor(controller.color_values[index].body),
  947. .button = GetNpadColor(controller.color_values[index].buttons),
  948. };
  949. if (npad_type == NpadStyleIndex::ProController) {
  950. controller.colors_state.left = {
  951. .body = GetNpadColor(controller.color_values[index].left_grip),
  952. .button = GetNpadColor(controller.color_values[index].buttons),
  953. };
  954. controller.colors_state.right = {
  955. .body = GetNpadColor(controller.color_values[index].right_grip),
  956. .button = GetNpadColor(controller.color_values[index].buttons),
  957. };
  958. } else {
  959. switch (index) {
  960. case LeftIndex:
  961. controller.colors_state.left = {
  962. .body = GetNpadColor(controller.color_values[index].body),
  963. .button = GetNpadColor(controller.color_values[index].buttons),
  964. };
  965. break;
  966. case RightIndex:
  967. controller.colors_state.right = {
  968. .body = GetNpadColor(controller.color_values[index].body),
  969. .button = GetNpadColor(controller.color_values[index].buttons),
  970. };
  971. break;
  972. }
  973. }
  974. }
  975. void EmulatedController::SetBattery(const Common::Input::CallbackStatus& callback,
  976. std::size_t index) {
  977. if (index >= controller.battery_values.size()) {
  978. return;
  979. }
  980. SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::Battery, !is_configuring); });
  981. std::scoped_lock lock{mutex};
  982. controller.battery_values[index] = TransformToBattery(callback);
  983. if (is_configuring) {
  984. return;
  985. }
  986. bool is_charging = false;
  987. bool is_powered = false;
  988. NpadBatteryLevel battery_level = 0;
  989. switch (controller.battery_values[index]) {
  990. case Common::Input::BatteryLevel::Charging:
  991. is_charging = true;
  992. is_powered = true;
  993. battery_level = 6;
  994. break;
  995. case Common::Input::BatteryLevel::Medium:
  996. battery_level = 6;
  997. break;
  998. case Common::Input::BatteryLevel::Low:
  999. battery_level = 4;
  1000. break;
  1001. case Common::Input::BatteryLevel::Critical:
  1002. battery_level = 2;
  1003. break;
  1004. case Common::Input::BatteryLevel::Empty:
  1005. battery_level = 0;
  1006. break;
  1007. case Common::Input::BatteryLevel::None:
  1008. case Common::Input::BatteryLevel::Full:
  1009. default:
  1010. is_powered = true;
  1011. battery_level = 8;
  1012. break;
  1013. }
  1014. switch (index) {
  1015. case LeftIndex:
  1016. controller.battery_state.left = {
  1017. .is_powered = is_powered,
  1018. .is_charging = is_charging,
  1019. .battery_level = battery_level,
  1020. };
  1021. break;
  1022. case RightIndex:
  1023. controller.battery_state.right = {
  1024. .is_powered = is_powered,
  1025. .is_charging = is_charging,
  1026. .battery_level = battery_level,
  1027. };
  1028. break;
  1029. case DualIndex:
  1030. controller.battery_state.dual = {
  1031. .is_powered = is_powered,
  1032. .is_charging = is_charging,
  1033. .battery_level = battery_level,
  1034. };
  1035. break;
  1036. }
  1037. }
  1038. void EmulatedController::SetCamera(const Common::Input::CallbackStatus& callback) {
  1039. SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::IrSensor, !is_configuring); });
  1040. std::scoped_lock lock{mutex};
  1041. controller.camera_values = TransformToCamera(callback);
  1042. if (is_configuring) {
  1043. return;
  1044. }
  1045. controller.camera_state.sample++;
  1046. controller.camera_state.format =
  1047. static_cast<Core::IrSensor::ImageTransferProcessorFormat>(controller.camera_values.format);
  1048. controller.camera_state.data = controller.camera_values.data;
  1049. }
  1050. void EmulatedController::SetRingAnalog(const Common::Input::CallbackStatus& callback) {
  1051. SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::RingController, !is_configuring); });
  1052. std::scoped_lock lock{mutex};
  1053. const auto force_value = TransformToStick(callback);
  1054. controller.ring_analog_value = force_value.x;
  1055. if (is_configuring) {
  1056. return;
  1057. }
  1058. controller.ring_analog_state.force = force_value.x.value;
  1059. }
  1060. void EmulatedController::SetNfc(const Common::Input::CallbackStatus& callback) {
  1061. SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::Nfc, !is_configuring); });
  1062. std::scoped_lock lock{mutex};
  1063. controller.nfc_values = TransformToNfc(callback);
  1064. if (is_configuring) {
  1065. return;
  1066. }
  1067. controller.nfc_state = controller.nfc_values;
  1068. }
  1069. bool EmulatedController::SetVibration(std::size_t device_index, VibrationValue vibration) {
  1070. if (device_index >= output_devices.size()) {
  1071. return false;
  1072. }
  1073. if (!output_devices[device_index]) {
  1074. return false;
  1075. }
  1076. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  1077. const auto& player = Settings::values.players.GetValue()[player_index];
  1078. const f32 strength = static_cast<f32>(player.vibration_strength) / 100.0f;
  1079. if (!player.vibration_enabled) {
  1080. return false;
  1081. }
  1082. // Exponential amplification is too strong at low amplitudes. Switch to a linear
  1083. // amplification if strength is set below 0.7f
  1084. const Common::Input::VibrationAmplificationType type =
  1085. strength > 0.7f ? Common::Input::VibrationAmplificationType::Exponential
  1086. : Common::Input::VibrationAmplificationType::Linear;
  1087. const Common::Input::VibrationStatus status = {
  1088. .low_amplitude = std::min(vibration.low_amplitude * strength, 1.0f),
  1089. .low_frequency = vibration.low_frequency,
  1090. .high_amplitude = std::min(vibration.high_amplitude * strength, 1.0f),
  1091. .high_frequency = vibration.high_frequency,
  1092. .type = type,
  1093. };
  1094. return output_devices[device_index]->SetVibration(status) ==
  1095. Common::Input::DriverResult::Success;
  1096. }
  1097. bool EmulatedController::IsVibrationEnabled(std::size_t device_index) {
  1098. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  1099. const auto& player = Settings::values.players.GetValue()[player_index];
  1100. if (!player.vibration_enabled) {
  1101. return false;
  1102. }
  1103. if (device_index >= output_devices.size()) {
  1104. return false;
  1105. }
  1106. if (!output_devices[device_index]) {
  1107. return false;
  1108. }
  1109. return output_devices[device_index]->IsVibrationEnabled();
  1110. }
  1111. Common::Input::DriverResult EmulatedController::SetPollingMode(
  1112. EmulatedDeviceIndex device_index, Common::Input::PollingMode polling_mode) {
  1113. LOG_INFO(Service_HID, "Set polling mode {}, device_index={}", polling_mode, device_index);
  1114. auto& left_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Left)];
  1115. auto& right_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1116. auto& nfc_output_device = output_devices[3];
  1117. if (device_index == EmulatedDeviceIndex::LeftIndex) {
  1118. return left_output_device->SetPollingMode(polling_mode);
  1119. }
  1120. if (device_index == EmulatedDeviceIndex::RightIndex) {
  1121. const auto virtual_nfc_result = nfc_output_device->SetPollingMode(polling_mode);
  1122. const auto mapped_nfc_result = right_output_device->SetPollingMode(polling_mode);
  1123. if (virtual_nfc_result == Common::Input::DriverResult::Success) {
  1124. return virtual_nfc_result;
  1125. }
  1126. return mapped_nfc_result;
  1127. }
  1128. left_output_device->SetPollingMode(polling_mode);
  1129. right_output_device->SetPollingMode(polling_mode);
  1130. nfc_output_device->SetPollingMode(polling_mode);
  1131. return Common::Input::DriverResult::Success;
  1132. }
  1133. bool EmulatedController::SetCameraFormat(
  1134. Core::IrSensor::ImageTransferProcessorFormat camera_format) {
  1135. LOG_INFO(Service_HID, "Set camera format {}", camera_format);
  1136. auto& right_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1137. auto& camera_output_device = output_devices[2];
  1138. if (right_output_device->SetCameraFormat(static_cast<Common::Input::CameraFormat>(
  1139. camera_format)) == Common::Input::DriverResult::Success) {
  1140. return true;
  1141. }
  1142. // Fallback to Qt camera if native device doesn't have support
  1143. return camera_output_device->SetCameraFormat(static_cast<Common::Input::CameraFormat>(
  1144. camera_format)) == Common::Input::DriverResult::Success;
  1145. }
  1146. Common::ParamPackage EmulatedController::GetRingParam() const {
  1147. return ring_params[0];
  1148. }
  1149. void EmulatedController::SetRingParam(Common::ParamPackage param) {
  1150. ring_params[0] = std::move(param);
  1151. ReloadInput();
  1152. }
  1153. bool EmulatedController::HasNfc() const {
  1154. const auto& nfc_output_device = output_devices[3];
  1155. switch (npad_type) {
  1156. case NpadStyleIndex::JoyconRight:
  1157. case NpadStyleIndex::JoyconDual:
  1158. case NpadStyleIndex::ProController:
  1159. case NpadStyleIndex::Handheld:
  1160. break;
  1161. default:
  1162. return false;
  1163. }
  1164. const bool has_virtual_nfc =
  1165. npad_id_type == NpadIdType::Player1 || npad_id_type == NpadIdType::Handheld;
  1166. const bool is_virtual_nfc_supported =
  1167. nfc_output_device->SupportsNfc() != Common::Input::NfcState::NotSupported;
  1168. return is_connected && (has_virtual_nfc && is_virtual_nfc_supported);
  1169. }
  1170. bool EmulatedController::AddNfcHandle() {
  1171. nfc_handles++;
  1172. return SetPollingMode(EmulatedDeviceIndex::RightIndex, Common::Input::PollingMode::NFC) ==
  1173. Common::Input::DriverResult::Success;
  1174. }
  1175. bool EmulatedController::RemoveNfcHandle() {
  1176. nfc_handles--;
  1177. if (nfc_handles <= 0) {
  1178. return SetPollingMode(EmulatedDeviceIndex::RightIndex,
  1179. Common::Input::PollingMode::Active) ==
  1180. Common::Input::DriverResult::Success;
  1181. }
  1182. return true;
  1183. }
  1184. bool EmulatedController::StartNfcPolling() {
  1185. auto& nfc_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1186. auto& nfc_virtual_output_device = output_devices[3];
  1187. return nfc_output_device->StartNfcPolling() == Common::Input::NfcState::Success ||
  1188. nfc_virtual_output_device->StartNfcPolling() == Common::Input::NfcState::Success;
  1189. }
  1190. bool EmulatedController::StopNfcPolling() {
  1191. auto& nfc_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1192. auto& nfc_virtual_output_device = output_devices[3];
  1193. return nfc_output_device->StopNfcPolling() == Common::Input::NfcState::Success ||
  1194. nfc_virtual_output_device->StopNfcPolling() == Common::Input::NfcState::Success;
  1195. }
  1196. bool EmulatedController::ReadAmiiboData(std::vector<u8>& data) {
  1197. auto& nfc_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1198. auto& nfc_virtual_output_device = output_devices[3];
  1199. if (nfc_output_device->ReadAmiiboData(data) == Common::Input::NfcState::Success) {
  1200. return true;
  1201. }
  1202. return nfc_virtual_output_device->ReadAmiiboData(data) == Common::Input::NfcState::Success;
  1203. }
  1204. bool EmulatedController::ReadMifareData(const Common::Input::MifareRequest& request,
  1205. Common::Input::MifareRequest& out_data) {
  1206. auto& nfc_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1207. auto& nfc_virtual_output_device = output_devices[3];
  1208. if (nfc_output_device->ReadMifareData(request, out_data) == Common::Input::NfcState::Success) {
  1209. return true;
  1210. }
  1211. return nfc_virtual_output_device->ReadMifareData(request, out_data) ==
  1212. Common::Input::NfcState::Success;
  1213. }
  1214. bool EmulatedController::WriteMifareData(const Common::Input::MifareRequest& request) {
  1215. auto& nfc_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1216. auto& nfc_virtual_output_device = output_devices[3];
  1217. if (nfc_output_device->WriteMifareData(request) == Common::Input::NfcState::Success) {
  1218. return true;
  1219. }
  1220. return nfc_virtual_output_device->WriteMifareData(request) == Common::Input::NfcState::Success;
  1221. }
  1222. bool EmulatedController::WriteNfc(const std::vector<u8>& data) {
  1223. auto& nfc_output_device = output_devices[static_cast<std::size_t>(DeviceIndex::Right)];
  1224. auto& nfc_virtual_output_device = output_devices[3];
  1225. if (nfc_output_device->SupportsNfc() != Common::Input::NfcState::NotSupported) {
  1226. return nfc_output_device->WriteNfcData(data) == Common::Input::NfcState::Success;
  1227. }
  1228. return nfc_virtual_output_device->WriteNfcData(data) == Common::Input::NfcState::Success;
  1229. }
  1230. void EmulatedController::SetLedPattern() {
  1231. for (auto& device : output_devices) {
  1232. if (!device) {
  1233. continue;
  1234. }
  1235. const LedPattern pattern = GetLedPattern();
  1236. const Common::Input::LedStatus status = {
  1237. .led_1 = pattern.position1 != 0,
  1238. .led_2 = pattern.position2 != 0,
  1239. .led_3 = pattern.position3 != 0,
  1240. .led_4 = pattern.position4 != 0,
  1241. };
  1242. device->SetLED(status);
  1243. }
  1244. }
  1245. void EmulatedController::SetGyroscopeZeroDriftMode(GyroscopeZeroDriftMode mode) {
  1246. for (auto& motion : controller.motion_values) {
  1247. switch (mode) {
  1248. case GyroscopeZeroDriftMode::Loose:
  1249. motion_sensitivity = motion.emulated.IsAtRestLoose;
  1250. motion.emulated.SetGyroThreshold(motion.emulated.ThresholdLoose);
  1251. break;
  1252. case GyroscopeZeroDriftMode::Tight:
  1253. motion_sensitivity = motion.emulated.IsAtRestThight;
  1254. motion.emulated.SetGyroThreshold(motion.emulated.ThresholdThight);
  1255. break;
  1256. case GyroscopeZeroDriftMode::Standard:
  1257. default:
  1258. motion_sensitivity = motion.emulated.IsAtRestStandard;
  1259. motion.emulated.SetGyroThreshold(motion.emulated.ThresholdStandard);
  1260. break;
  1261. }
  1262. }
  1263. }
  1264. void EmulatedController::SetSupportedNpadStyleTag(NpadStyleTag supported_styles) {
  1265. supported_style_tag = supported_styles;
  1266. if (!is_connected) {
  1267. return;
  1268. }
  1269. // Attempt to reconnect with the original type
  1270. if (npad_type != original_npad_type) {
  1271. Disconnect();
  1272. const auto current_npad_type = npad_type;
  1273. SetNpadStyleIndex(original_npad_type);
  1274. if (IsControllerSupported()) {
  1275. Connect();
  1276. return;
  1277. }
  1278. SetNpadStyleIndex(current_npad_type);
  1279. Connect();
  1280. }
  1281. if (IsControllerSupported()) {
  1282. return;
  1283. }
  1284. Disconnect();
  1285. // Fallback Fullkey controllers to Pro controllers
  1286. if (IsControllerFullkey() && supported_style_tag.fullkey) {
  1287. LOG_WARNING(Service_HID, "Reconnecting controller type {} as Pro controller", npad_type);
  1288. SetNpadStyleIndex(NpadStyleIndex::ProController);
  1289. Connect();
  1290. return;
  1291. }
  1292. // Fallback Dual joycon controllers to Pro controllers
  1293. if (npad_type == NpadStyleIndex::JoyconDual && supported_style_tag.fullkey) {
  1294. LOG_WARNING(Service_HID, "Reconnecting controller type {} as Pro controller", npad_type);
  1295. SetNpadStyleIndex(NpadStyleIndex::ProController);
  1296. Connect();
  1297. return;
  1298. }
  1299. // Fallback Pro controllers to Dual joycon
  1300. if (npad_type == NpadStyleIndex::ProController && supported_style_tag.joycon_dual) {
  1301. LOG_WARNING(Service_HID, "Reconnecting controller type {} as Dual Joycons", npad_type);
  1302. SetNpadStyleIndex(NpadStyleIndex::JoyconDual);
  1303. Connect();
  1304. return;
  1305. }
  1306. LOG_ERROR(Service_HID, "Controller type {} is not supported. Disconnecting controller",
  1307. npad_type);
  1308. }
  1309. bool EmulatedController::IsControllerFullkey(bool use_temporary_value) const {
  1310. std::scoped_lock lock{mutex};
  1311. const auto type = is_configuring && use_temporary_value ? tmp_npad_type : npad_type;
  1312. switch (type) {
  1313. case NpadStyleIndex::ProController:
  1314. case NpadStyleIndex::GameCube:
  1315. case NpadStyleIndex::NES:
  1316. case NpadStyleIndex::SNES:
  1317. case NpadStyleIndex::N64:
  1318. case NpadStyleIndex::SegaGenesis:
  1319. return true;
  1320. default:
  1321. return false;
  1322. }
  1323. }
  1324. bool EmulatedController::IsControllerSupported(bool use_temporary_value) const {
  1325. std::scoped_lock lock{mutex};
  1326. const auto type = is_configuring && use_temporary_value ? tmp_npad_type : npad_type;
  1327. switch (type) {
  1328. case NpadStyleIndex::ProController:
  1329. return supported_style_tag.fullkey.As<bool>();
  1330. case NpadStyleIndex::Handheld:
  1331. return supported_style_tag.handheld.As<bool>();
  1332. case NpadStyleIndex::JoyconDual:
  1333. return supported_style_tag.joycon_dual.As<bool>();
  1334. case NpadStyleIndex::JoyconLeft:
  1335. return supported_style_tag.joycon_left.As<bool>();
  1336. case NpadStyleIndex::JoyconRight:
  1337. return supported_style_tag.joycon_right.As<bool>();
  1338. case NpadStyleIndex::GameCube:
  1339. return supported_style_tag.gamecube.As<bool>();
  1340. case NpadStyleIndex::Pokeball:
  1341. return supported_style_tag.palma.As<bool>();
  1342. case NpadStyleIndex::NES:
  1343. return supported_style_tag.lark.As<bool>();
  1344. case NpadStyleIndex::SNES:
  1345. return supported_style_tag.lucia.As<bool>();
  1346. case NpadStyleIndex::N64:
  1347. return supported_style_tag.lagoon.As<bool>();
  1348. case NpadStyleIndex::SegaGenesis:
  1349. return supported_style_tag.lager.As<bool>();
  1350. default:
  1351. return false;
  1352. }
  1353. }
  1354. void EmulatedController::Connect(bool use_temporary_value) {
  1355. if (!IsControllerSupported(use_temporary_value)) {
  1356. const auto type = is_configuring && use_temporary_value ? tmp_npad_type : npad_type;
  1357. LOG_ERROR(Service_HID, "Controller type {} is not supported", type);
  1358. return;
  1359. }
  1360. auto trigger_guard =
  1361. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Connected, !is_configuring); });
  1362. std::scoped_lock lock{mutex};
  1363. if (is_configuring) {
  1364. tmp_is_connected = true;
  1365. return;
  1366. }
  1367. if (is_connected) {
  1368. trigger_guard.Cancel();
  1369. return;
  1370. }
  1371. is_connected = true;
  1372. }
  1373. void EmulatedController::Disconnect() {
  1374. auto trigger_guard =
  1375. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Disconnected, !is_configuring); });
  1376. std::scoped_lock lock{mutex};
  1377. if (is_configuring) {
  1378. tmp_is_connected = false;
  1379. return;
  1380. }
  1381. if (!is_connected) {
  1382. trigger_guard.Cancel();
  1383. return;
  1384. }
  1385. is_connected = false;
  1386. }
  1387. bool EmulatedController::IsConnected(bool get_temporary_value) const {
  1388. std::scoped_lock lock{mutex};
  1389. if (get_temporary_value && is_configuring) {
  1390. return tmp_is_connected;
  1391. }
  1392. return is_connected;
  1393. }
  1394. NpadIdType EmulatedController::GetNpadIdType() const {
  1395. std::scoped_lock lock{mutex};
  1396. return npad_id_type;
  1397. }
  1398. NpadStyleIndex EmulatedController::GetNpadStyleIndex(bool get_temporary_value) const {
  1399. std::scoped_lock lock{mutex};
  1400. if (get_temporary_value && is_configuring) {
  1401. return tmp_npad_type;
  1402. }
  1403. return npad_type;
  1404. }
  1405. void EmulatedController::SetNpadStyleIndex(NpadStyleIndex npad_type_) {
  1406. auto trigger_guard =
  1407. SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Type, !is_configuring); });
  1408. std::scoped_lock lock{mutex};
  1409. if (is_configuring) {
  1410. if (tmp_npad_type == npad_type_) {
  1411. trigger_guard.Cancel();
  1412. return;
  1413. }
  1414. tmp_npad_type = npad_type_;
  1415. return;
  1416. }
  1417. if (npad_type == npad_type_) {
  1418. trigger_guard.Cancel();
  1419. return;
  1420. }
  1421. if (is_connected) {
  1422. LOG_WARNING(Service_HID, "Controller {} type changed while it's connected",
  1423. NpadIdTypeToIndex(npad_id_type));
  1424. }
  1425. npad_type = npad_type_;
  1426. }
  1427. LedPattern EmulatedController::GetLedPattern() const {
  1428. switch (npad_id_type) {
  1429. case NpadIdType::Player1:
  1430. return LedPattern{1, 0, 0, 0};
  1431. case NpadIdType::Player2:
  1432. return LedPattern{1, 1, 0, 0};
  1433. case NpadIdType::Player3:
  1434. return LedPattern{1, 1, 1, 0};
  1435. case NpadIdType::Player4:
  1436. return LedPattern{1, 1, 1, 1};
  1437. case NpadIdType::Player5:
  1438. return LedPattern{1, 0, 0, 1};
  1439. case NpadIdType::Player6:
  1440. return LedPattern{1, 0, 1, 0};
  1441. case NpadIdType::Player7:
  1442. return LedPattern{1, 0, 1, 1};
  1443. case NpadIdType::Player8:
  1444. return LedPattern{0, 1, 1, 0};
  1445. default:
  1446. return LedPattern{0, 0, 0, 0};
  1447. }
  1448. }
  1449. ButtonValues EmulatedController::GetButtonsValues() const {
  1450. std::scoped_lock lock{mutex};
  1451. return controller.button_values;
  1452. }
  1453. SticksValues EmulatedController::GetSticksValues() const {
  1454. std::scoped_lock lock{mutex};
  1455. return controller.stick_values;
  1456. }
  1457. TriggerValues EmulatedController::GetTriggersValues() const {
  1458. std::scoped_lock lock{mutex};
  1459. return controller.trigger_values;
  1460. }
  1461. ControllerMotionValues EmulatedController::GetMotionValues() const {
  1462. std::scoped_lock lock{mutex};
  1463. return controller.motion_values;
  1464. }
  1465. ColorValues EmulatedController::GetColorsValues() const {
  1466. std::scoped_lock lock{mutex};
  1467. return controller.color_values;
  1468. }
  1469. BatteryValues EmulatedController::GetBatteryValues() const {
  1470. std::scoped_lock lock{mutex};
  1471. return controller.battery_values;
  1472. }
  1473. CameraValues EmulatedController::GetCameraValues() const {
  1474. std::scoped_lock lock{mutex};
  1475. return controller.camera_values;
  1476. }
  1477. RingAnalogValue EmulatedController::GetRingSensorValues() const {
  1478. return controller.ring_analog_value;
  1479. }
  1480. HomeButtonState EmulatedController::GetHomeButtons() const {
  1481. std::scoped_lock lock{mutex};
  1482. if (is_configuring) {
  1483. return {};
  1484. }
  1485. return controller.home_button_state;
  1486. }
  1487. CaptureButtonState EmulatedController::GetCaptureButtons() const {
  1488. std::scoped_lock lock{mutex};
  1489. if (is_configuring) {
  1490. return {};
  1491. }
  1492. return controller.capture_button_state;
  1493. }
  1494. NpadButtonState EmulatedController::GetNpadButtons() const {
  1495. std::scoped_lock lock{mutex};
  1496. if (is_configuring) {
  1497. return {};
  1498. }
  1499. return {controller.npad_button_state.raw & GetTurboButtonMask()};
  1500. }
  1501. DebugPadButton EmulatedController::GetDebugPadButtons() const {
  1502. std::scoped_lock lock{mutex};
  1503. if (is_configuring) {
  1504. return {};
  1505. }
  1506. return controller.debug_pad_button_state;
  1507. }
  1508. AnalogSticks EmulatedController::GetSticks() const {
  1509. std::scoped_lock lock{mutex};
  1510. if (is_configuring) {
  1511. return {};
  1512. }
  1513. return controller.analog_stick_state;
  1514. }
  1515. NpadGcTriggerState EmulatedController::GetTriggers() const {
  1516. std::scoped_lock lock{mutex};
  1517. if (is_configuring) {
  1518. return {};
  1519. }
  1520. return controller.gc_trigger_state;
  1521. }
  1522. MotionState EmulatedController::GetMotions() const {
  1523. std::unique_lock lock{mutex};
  1524. return controller.motion_state;
  1525. }
  1526. ControllerColors EmulatedController::GetColors() const {
  1527. std::scoped_lock lock{mutex};
  1528. return controller.colors_state;
  1529. }
  1530. BatteryLevelState EmulatedController::GetBattery() const {
  1531. std::scoped_lock lock{mutex};
  1532. return controller.battery_state;
  1533. }
  1534. const CameraState& EmulatedController::GetCamera() const {
  1535. std::scoped_lock lock{mutex};
  1536. return controller.camera_state;
  1537. }
  1538. RingSensorForce EmulatedController::GetRingSensorForce() const {
  1539. return controller.ring_analog_state;
  1540. }
  1541. const NfcState& EmulatedController::GetNfc() const {
  1542. std::scoped_lock lock{mutex};
  1543. return controller.nfc_state;
  1544. }
  1545. NpadColor EmulatedController::GetNpadColor(u32 color) {
  1546. return {
  1547. .r = static_cast<u8>((color >> 16) & 0xFF),
  1548. .g = static_cast<u8>((color >> 8) & 0xFF),
  1549. .b = static_cast<u8>(color & 0xFF),
  1550. .a = 0xff,
  1551. };
  1552. }
  1553. void EmulatedController::TriggerOnChange(ControllerTriggerType type, bool is_npad_service_update) {
  1554. std::scoped_lock lock{callback_mutex};
  1555. for (const auto& poller_pair : callback_list) {
  1556. const ControllerUpdateCallback& poller = poller_pair.second;
  1557. if (!is_npad_service_update && poller.is_npad_service) {
  1558. continue;
  1559. }
  1560. if (poller.on_change) {
  1561. poller.on_change(type);
  1562. }
  1563. }
  1564. }
  1565. int EmulatedController::SetCallback(ControllerUpdateCallback update_callback) {
  1566. std::scoped_lock lock{callback_mutex};
  1567. callback_list.insert_or_assign(last_callback_key, std::move(update_callback));
  1568. return last_callback_key++;
  1569. }
  1570. void EmulatedController::DeleteCallback(int key) {
  1571. std::scoped_lock lock{callback_mutex};
  1572. const auto& iterator = callback_list.find(key);
  1573. if (iterator == callback_list.end()) {
  1574. LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
  1575. return;
  1576. }
  1577. callback_list.erase(iterator);
  1578. }
  1579. void EmulatedController::StatusUpdate() {
  1580. turbo_button_state = (turbo_button_state + 1) % (TURBO_BUTTON_DELAY * 2);
  1581. // Some drivers like key motion need constant refreshing
  1582. for (std::size_t index = 0; index < motion_devices.size(); ++index) {
  1583. const auto& raw_status = controller.motion_values[index].raw_status;
  1584. auto& device = motion_devices[index];
  1585. if (!raw_status.force_update) {
  1586. continue;
  1587. }
  1588. if (!device) {
  1589. continue;
  1590. }
  1591. device->ForceUpdate();
  1592. }
  1593. }
  1594. NpadButton EmulatedController::GetTurboButtonMask() const {
  1595. // Apply no mask when disabled
  1596. if (turbo_button_state < TURBO_BUTTON_DELAY) {
  1597. return {NpadButton::All};
  1598. }
  1599. NpadButtonState button_mask{};
  1600. for (std::size_t index = 0; index < controller.button_values.size(); ++index) {
  1601. if (!controller.button_values[index].turbo) {
  1602. continue;
  1603. }
  1604. switch (index) {
  1605. case Settings::NativeButton::A:
  1606. button_mask.a.Assign(1);
  1607. break;
  1608. case Settings::NativeButton::B:
  1609. button_mask.b.Assign(1);
  1610. break;
  1611. case Settings::NativeButton::X:
  1612. button_mask.x.Assign(1);
  1613. break;
  1614. case Settings::NativeButton::Y:
  1615. button_mask.y.Assign(1);
  1616. break;
  1617. case Settings::NativeButton::L:
  1618. button_mask.l.Assign(1);
  1619. break;
  1620. case Settings::NativeButton::R:
  1621. button_mask.r.Assign(1);
  1622. break;
  1623. case Settings::NativeButton::ZL:
  1624. button_mask.zl.Assign(1);
  1625. break;
  1626. case Settings::NativeButton::ZR:
  1627. button_mask.zr.Assign(1);
  1628. break;
  1629. case Settings::NativeButton::DLeft:
  1630. button_mask.left.Assign(1);
  1631. break;
  1632. case Settings::NativeButton::DUp:
  1633. button_mask.up.Assign(1);
  1634. break;
  1635. case Settings::NativeButton::DRight:
  1636. button_mask.right.Assign(1);
  1637. break;
  1638. case Settings::NativeButton::DDown:
  1639. button_mask.down.Assign(1);
  1640. break;
  1641. case Settings::NativeButton::SL:
  1642. button_mask.left_sl.Assign(1);
  1643. button_mask.right_sl.Assign(1);
  1644. break;
  1645. case Settings::NativeButton::SR:
  1646. button_mask.left_sr.Assign(1);
  1647. button_mask.right_sr.Assign(1);
  1648. break;
  1649. default:
  1650. break;
  1651. }
  1652. }
  1653. return static_cast<NpadButton>(~button_mask.raw);
  1654. }
  1655. } // namespace Core::HID