emulated_controller.cpp 49 KB

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