emulated_controller.cpp 52 KB

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