emulated_controller.cpp 42 KB

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