emulated_controller.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  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.begin(), battery_devices.end(),
  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. bool EmulatedController::IsConfiguring() const {
  317. return is_configuring;
  318. }
  319. void EmulatedController::SaveCurrentConfig() {
  320. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  321. auto& player = Settings::values.players.GetValue()[player_index];
  322. player.connected = is_connected;
  323. player.controller_type = MapNPadToSettingsType(npad_type);
  324. for (std::size_t index = 0; index < player.buttons.size(); ++index) {
  325. player.buttons[index] = button_params[index].Serialize();
  326. }
  327. for (std::size_t index = 0; index < player.analogs.size(); ++index) {
  328. player.analogs[index] = stick_params[index].Serialize();
  329. }
  330. for (std::size_t index = 0; index < player.motions.size(); ++index) {
  331. player.motions[index] = motion_params[index].Serialize();
  332. }
  333. }
  334. void EmulatedController::RestoreConfig() {
  335. if (!is_configuring) {
  336. return;
  337. }
  338. ReloadFromSettings();
  339. }
  340. std::vector<Common::ParamPackage> EmulatedController::GetMappedDevices(
  341. EmulatedDeviceIndex device_index) const {
  342. std::vector<Common::ParamPackage> devices;
  343. for (const auto& param : button_params) {
  344. if (!param.Has("engine")) {
  345. continue;
  346. }
  347. const auto devices_it = std::find_if(
  348. devices.begin(), devices.end(), [param](const Common::ParamPackage param_) {
  349. return param.Get("engine", "") == param_.Get("engine", "") &&
  350. param.Get("guid", "") == param_.Get("guid", "") &&
  351. param.Get("port", 0) == param_.Get("port", 0);
  352. });
  353. if (devices_it != devices.end()) {
  354. continue;
  355. }
  356. Common::ParamPackage device{};
  357. device.Set("engine", param.Get("engine", ""));
  358. device.Set("guid", param.Get("guid", ""));
  359. device.Set("port", param.Get("port", 0));
  360. devices.push_back(device);
  361. }
  362. for (const auto& param : stick_params) {
  363. if (!param.Has("engine")) {
  364. continue;
  365. }
  366. if (param.Get("engine", "") == "analog_from_button") {
  367. continue;
  368. }
  369. const auto devices_it = std::find_if(
  370. devices.begin(), devices.end(), [param](const Common::ParamPackage param_) {
  371. return param.Get("engine", "") == param_.Get("engine", "") &&
  372. param.Get("guid", "") == param_.Get("guid", "") &&
  373. param.Get("port", 0) == param_.Get("port", 0);
  374. });
  375. if (devices_it != devices.end()) {
  376. continue;
  377. }
  378. Common::ParamPackage device{};
  379. device.Set("engine", param.Get("engine", ""));
  380. device.Set("guid", param.Get("guid", ""));
  381. device.Set("port", param.Get("port", 0));
  382. devices.push_back(device);
  383. }
  384. return devices;
  385. }
  386. Common::ParamPackage EmulatedController::GetButtonParam(std::size_t index) const {
  387. if (index >= button_params.size()) {
  388. return {};
  389. }
  390. return button_params[index];
  391. }
  392. Common::ParamPackage EmulatedController::GetStickParam(std::size_t index) const {
  393. if (index >= stick_params.size()) {
  394. return {};
  395. }
  396. return stick_params[index];
  397. }
  398. Common::ParamPackage EmulatedController::GetMotionParam(std::size_t index) const {
  399. if (index >= motion_params.size()) {
  400. return {};
  401. }
  402. return motion_params[index];
  403. }
  404. void EmulatedController::SetButtonParam(std::size_t index, Common::ParamPackage param) {
  405. if (index >= button_params.size()) {
  406. return;
  407. }
  408. button_params[index] = std::move(param);
  409. ReloadInput();
  410. }
  411. void EmulatedController::SetStickParam(std::size_t index, Common::ParamPackage param) {
  412. if (index >= stick_params.size()) {
  413. return;
  414. }
  415. stick_params[index] = std::move(param);
  416. ReloadInput();
  417. }
  418. void EmulatedController::SetMotionParam(std::size_t index, Common::ParamPackage param) {
  419. if (index >= motion_params.size()) {
  420. return;
  421. }
  422. motion_params[index] = std::move(param);
  423. ReloadInput();
  424. }
  425. void EmulatedController::SetButton(const Common::Input::CallbackStatus& callback, std::size_t index,
  426. Common::UUID uuid) {
  427. if (index >= controller.button_values.size()) {
  428. return;
  429. }
  430. {
  431. std::lock_guard lock{mutex};
  432. bool value_changed = false;
  433. const auto new_status = TransformToButton(callback);
  434. auto& current_status = controller.button_values[index];
  435. // Only read button values that have the same uuid or are pressed once
  436. if (current_status.uuid != uuid) {
  437. if (!new_status.value) {
  438. return;
  439. }
  440. }
  441. current_status.toggle = new_status.toggle;
  442. current_status.uuid = uuid;
  443. // Update button status with current
  444. if (!current_status.toggle) {
  445. current_status.locked = false;
  446. if (current_status.value != new_status.value) {
  447. current_status.value = new_status.value;
  448. value_changed = true;
  449. }
  450. } else {
  451. // Toggle button and lock status
  452. if (new_status.value && !current_status.locked) {
  453. current_status.locked = true;
  454. current_status.value = !current_status.value;
  455. value_changed = true;
  456. }
  457. // Unlock button ready for next press
  458. if (!new_status.value && current_status.locked) {
  459. current_status.locked = false;
  460. }
  461. }
  462. if (!value_changed) {
  463. return;
  464. }
  465. if (is_configuring) {
  466. controller.npad_button_state.raw = NpadButton::None;
  467. controller.debug_pad_button_state.raw = 0;
  468. TriggerOnChange(ControllerTriggerType::Button, false);
  469. return;
  470. }
  471. switch (index) {
  472. case Settings::NativeButton::A:
  473. controller.npad_button_state.a.Assign(current_status.value);
  474. controller.debug_pad_button_state.a.Assign(current_status.value);
  475. break;
  476. case Settings::NativeButton::B:
  477. controller.npad_button_state.b.Assign(current_status.value);
  478. controller.debug_pad_button_state.b.Assign(current_status.value);
  479. break;
  480. case Settings::NativeButton::X:
  481. controller.npad_button_state.x.Assign(current_status.value);
  482. controller.debug_pad_button_state.x.Assign(current_status.value);
  483. break;
  484. case Settings::NativeButton::Y:
  485. controller.npad_button_state.y.Assign(current_status.value);
  486. controller.debug_pad_button_state.y.Assign(current_status.value);
  487. break;
  488. case Settings::NativeButton::LStick:
  489. controller.npad_button_state.stick_l.Assign(current_status.value);
  490. break;
  491. case Settings::NativeButton::RStick:
  492. controller.npad_button_state.stick_r.Assign(current_status.value);
  493. break;
  494. case Settings::NativeButton::L:
  495. controller.npad_button_state.l.Assign(current_status.value);
  496. controller.debug_pad_button_state.l.Assign(current_status.value);
  497. break;
  498. case Settings::NativeButton::R:
  499. controller.npad_button_state.r.Assign(current_status.value);
  500. controller.debug_pad_button_state.r.Assign(current_status.value);
  501. break;
  502. case Settings::NativeButton::ZL:
  503. controller.npad_button_state.zl.Assign(current_status.value);
  504. controller.debug_pad_button_state.zl.Assign(current_status.value);
  505. break;
  506. case Settings::NativeButton::ZR:
  507. controller.npad_button_state.zr.Assign(current_status.value);
  508. controller.debug_pad_button_state.zr.Assign(current_status.value);
  509. break;
  510. case Settings::NativeButton::Plus:
  511. controller.npad_button_state.plus.Assign(current_status.value);
  512. controller.debug_pad_button_state.plus.Assign(current_status.value);
  513. break;
  514. case Settings::NativeButton::Minus:
  515. controller.npad_button_state.minus.Assign(current_status.value);
  516. controller.debug_pad_button_state.minus.Assign(current_status.value);
  517. break;
  518. case Settings::NativeButton::DLeft:
  519. controller.npad_button_state.left.Assign(current_status.value);
  520. controller.debug_pad_button_state.d_left.Assign(current_status.value);
  521. break;
  522. case Settings::NativeButton::DUp:
  523. controller.npad_button_state.up.Assign(current_status.value);
  524. controller.debug_pad_button_state.d_up.Assign(current_status.value);
  525. break;
  526. case Settings::NativeButton::DRight:
  527. controller.npad_button_state.right.Assign(current_status.value);
  528. controller.debug_pad_button_state.d_right.Assign(current_status.value);
  529. break;
  530. case Settings::NativeButton::DDown:
  531. controller.npad_button_state.down.Assign(current_status.value);
  532. controller.debug_pad_button_state.d_down.Assign(current_status.value);
  533. break;
  534. case Settings::NativeButton::SL:
  535. controller.npad_button_state.left_sl.Assign(current_status.value);
  536. controller.npad_button_state.right_sl.Assign(current_status.value);
  537. break;
  538. case Settings::NativeButton::SR:
  539. controller.npad_button_state.left_sr.Assign(current_status.value);
  540. controller.npad_button_state.right_sr.Assign(current_status.value);
  541. break;
  542. case Settings::NativeButton::Home:
  543. case Settings::NativeButton::Screenshot:
  544. break;
  545. }
  546. }
  547. if (!is_connected) {
  548. if (npad_id_type == NpadIdType::Player1 && npad_type != NpadStyleIndex::Handheld) {
  549. Connect();
  550. }
  551. if (npad_id_type == NpadIdType::Handheld && npad_type == NpadStyleIndex::Handheld) {
  552. Connect();
  553. }
  554. }
  555. TriggerOnChange(ControllerTriggerType::Button, true);
  556. }
  557. void EmulatedController::SetStick(const Common::Input::CallbackStatus& callback, std::size_t index,
  558. Common::UUID uuid) {
  559. if (index >= controller.stick_values.size()) {
  560. return;
  561. }
  562. std::lock_guard lock{mutex};
  563. const auto stick_value = TransformToStick(callback);
  564. // Only read stick values that have the same uuid or are over the threshold to avoid flapping
  565. if (controller.stick_values[index].uuid != uuid) {
  566. if (!stick_value.down && !stick_value.up && !stick_value.left && !stick_value.right) {
  567. return;
  568. }
  569. }
  570. controller.stick_values[index] = stick_value;
  571. controller.stick_values[index].uuid = uuid;
  572. if (is_configuring) {
  573. controller.analog_stick_state.left = {};
  574. controller.analog_stick_state.right = {};
  575. TriggerOnChange(ControllerTriggerType::Stick, false);
  576. return;
  577. }
  578. const AnalogStickState stick{
  579. .x = static_cast<s32>(controller.stick_values[index].x.value * HID_JOYSTICK_MAX),
  580. .y = static_cast<s32>(controller.stick_values[index].y.value * HID_JOYSTICK_MAX),
  581. };
  582. switch (index) {
  583. case Settings::NativeAnalog::LStick:
  584. controller.analog_stick_state.left = stick;
  585. controller.npad_button_state.stick_l_left.Assign(controller.stick_values[index].left);
  586. controller.npad_button_state.stick_l_up.Assign(controller.stick_values[index].up);
  587. controller.npad_button_state.stick_l_right.Assign(controller.stick_values[index].right);
  588. controller.npad_button_state.stick_l_down.Assign(controller.stick_values[index].down);
  589. break;
  590. case Settings::NativeAnalog::RStick:
  591. controller.analog_stick_state.right = stick;
  592. controller.npad_button_state.stick_r_left.Assign(controller.stick_values[index].left);
  593. controller.npad_button_state.stick_r_up.Assign(controller.stick_values[index].up);
  594. controller.npad_button_state.stick_r_right.Assign(controller.stick_values[index].right);
  595. controller.npad_button_state.stick_r_down.Assign(controller.stick_values[index].down);
  596. break;
  597. }
  598. TriggerOnChange(ControllerTriggerType::Stick, true);
  599. }
  600. void EmulatedController::SetTrigger(const Common::Input::CallbackStatus& callback,
  601. std::size_t index, Common::UUID uuid) {
  602. if (index >= controller.trigger_values.size()) {
  603. return;
  604. }
  605. std::lock_guard lock{mutex};
  606. const auto trigger_value = TransformToTrigger(callback);
  607. // Only read trigger values that have the same uuid or are pressed once
  608. if (controller.trigger_values[index].uuid != uuid) {
  609. if (!trigger_value.pressed.value) {
  610. return;
  611. }
  612. }
  613. controller.trigger_values[index] = trigger_value;
  614. controller.trigger_values[index].uuid = uuid;
  615. if (is_configuring) {
  616. controller.gc_trigger_state.left = 0;
  617. controller.gc_trigger_state.right = 0;
  618. TriggerOnChange(ControllerTriggerType::Trigger, false);
  619. return;
  620. }
  621. const auto& trigger = controller.trigger_values[index];
  622. switch (index) {
  623. case Settings::NativeTrigger::LTrigger:
  624. controller.gc_trigger_state.left = static_cast<s32>(trigger.analog.value * HID_TRIGGER_MAX);
  625. controller.npad_button_state.zl.Assign(trigger.pressed.value);
  626. break;
  627. case Settings::NativeTrigger::RTrigger:
  628. controller.gc_trigger_state.right =
  629. static_cast<s32>(trigger.analog.value * HID_TRIGGER_MAX);
  630. controller.npad_button_state.zr.Assign(trigger.pressed.value);
  631. break;
  632. }
  633. TriggerOnChange(ControllerTriggerType::Trigger, true);
  634. }
  635. void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback,
  636. std::size_t index) {
  637. if (index >= controller.motion_values.size()) {
  638. return;
  639. }
  640. std::lock_guard lock{mutex};
  641. auto& raw_status = controller.motion_values[index].raw_status;
  642. auto& emulated = controller.motion_values[index].emulated;
  643. raw_status = TransformToMotion(callback);
  644. emulated.SetAcceleration(Common::Vec3f{
  645. raw_status.accel.x.value,
  646. raw_status.accel.y.value,
  647. raw_status.accel.z.value,
  648. });
  649. emulated.SetGyroscope(Common::Vec3f{
  650. raw_status.gyro.x.value,
  651. raw_status.gyro.y.value,
  652. raw_status.gyro.z.value,
  653. });
  654. emulated.UpdateRotation(raw_status.delta_timestamp);
  655. emulated.UpdateOrientation(raw_status.delta_timestamp);
  656. force_update_motion = raw_status.force_update;
  657. if (is_configuring) {
  658. TriggerOnChange(ControllerTriggerType::Motion, false);
  659. return;
  660. }
  661. auto& motion = controller.motion_state[index];
  662. motion.accel = emulated.GetAcceleration();
  663. motion.gyro = emulated.GetGyroscope();
  664. motion.rotation = emulated.GetRotations();
  665. motion.orientation = emulated.GetOrientation();
  666. motion.is_at_rest = !emulated.IsMoving(motion_sensitivity);
  667. TriggerOnChange(ControllerTriggerType::Motion, true);
  668. }
  669. void EmulatedController::SetBattery(const Common::Input::CallbackStatus& callback,
  670. std::size_t index) {
  671. if (index >= controller.battery_values.size()) {
  672. return;
  673. }
  674. std::lock_guard lock{mutex};
  675. controller.battery_values[index] = TransformToBattery(callback);
  676. if (is_configuring) {
  677. TriggerOnChange(ControllerTriggerType::Battery, false);
  678. return;
  679. }
  680. bool is_charging = false;
  681. bool is_powered = false;
  682. NpadBatteryLevel battery_level = 0;
  683. switch (controller.battery_values[index]) {
  684. case Common::Input::BatteryLevel::Charging:
  685. is_charging = true;
  686. is_powered = true;
  687. battery_level = 6;
  688. break;
  689. case Common::Input::BatteryLevel::Medium:
  690. battery_level = 6;
  691. break;
  692. case Common::Input::BatteryLevel::Low:
  693. battery_level = 4;
  694. break;
  695. case Common::Input::BatteryLevel::Critical:
  696. battery_level = 2;
  697. break;
  698. case Common::Input::BatteryLevel::Empty:
  699. battery_level = 0;
  700. break;
  701. case Common::Input::BatteryLevel::None:
  702. case Common::Input::BatteryLevel::Full:
  703. default:
  704. is_powered = true;
  705. battery_level = 8;
  706. break;
  707. }
  708. switch (index) {
  709. case LeftIndex:
  710. controller.battery_state.left = {
  711. .is_powered = is_powered,
  712. .is_charging = is_charging,
  713. .battery_level = battery_level,
  714. };
  715. break;
  716. case RightIndex:
  717. controller.battery_state.right = {
  718. .is_powered = is_powered,
  719. .is_charging = is_charging,
  720. .battery_level = battery_level,
  721. };
  722. break;
  723. case DualIndex:
  724. controller.battery_state.dual = {
  725. .is_powered = is_powered,
  726. .is_charging = is_charging,
  727. .battery_level = battery_level,
  728. };
  729. break;
  730. }
  731. TriggerOnChange(ControllerTriggerType::Battery, true);
  732. }
  733. bool EmulatedController::SetVibration(std::size_t device_index, VibrationValue vibration) {
  734. if (device_index >= output_devices.size()) {
  735. return false;
  736. }
  737. if (!output_devices[device_index]) {
  738. return false;
  739. }
  740. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  741. const auto& player = Settings::values.players.GetValue()[player_index];
  742. const f32 strength = static_cast<f32>(player.vibration_strength) / 100.0f;
  743. if (!player.vibration_enabled) {
  744. return false;
  745. }
  746. // Exponential amplification is too strong at low amplitudes. Switch to a linear
  747. // amplification if strength is set below 0.7f
  748. const Common::Input::VibrationAmplificationType type =
  749. strength > 0.7f ? Common::Input::VibrationAmplificationType::Exponential
  750. : Common::Input::VibrationAmplificationType::Linear;
  751. const Common::Input::VibrationStatus status = {
  752. .low_amplitude = std::min(vibration.low_amplitude * strength, 1.0f),
  753. .low_frequency = vibration.low_frequency,
  754. .high_amplitude = std::min(vibration.high_amplitude * strength, 1.0f),
  755. .high_frequency = vibration.high_frequency,
  756. .type = type,
  757. };
  758. return output_devices[device_index]->SetVibration(status) ==
  759. Common::Input::VibrationError::None;
  760. }
  761. bool EmulatedController::TestVibration(std::size_t device_index) {
  762. if (device_index >= output_devices.size()) {
  763. return false;
  764. }
  765. if (!output_devices[device_index]) {
  766. return false;
  767. }
  768. // Send a slight vibration to test for rumble support
  769. constexpr Common::Input::VibrationStatus status = {
  770. .low_amplitude = 0.001f,
  771. .low_frequency = 160.0f,
  772. .high_amplitude = 0.001f,
  773. .high_frequency = 320.0f,
  774. .type = Common::Input::VibrationAmplificationType::Linear,
  775. };
  776. return output_devices[device_index]->SetVibration(status) ==
  777. Common::Input::VibrationError::None;
  778. }
  779. void EmulatedController::SetLedPattern() {
  780. for (auto& device : output_devices) {
  781. if (!device) {
  782. continue;
  783. }
  784. const LedPattern pattern = GetLedPattern();
  785. const Common::Input::LedStatus status = {
  786. .led_1 = pattern.position1 != 0,
  787. .led_2 = pattern.position2 != 0,
  788. .led_3 = pattern.position3 != 0,
  789. .led_4 = pattern.position4 != 0,
  790. };
  791. device->SetLED(status);
  792. }
  793. }
  794. void EmulatedController::SetSupportedNpadStyleTag(NpadStyleTag supported_styles) {
  795. supported_style_tag = supported_styles;
  796. if (!is_connected) {
  797. return;
  798. }
  799. if (!IsControllerSupported()) {
  800. LOG_ERROR(Service_HID, "Controller type {} is not supported. Disconnecting controller",
  801. npad_type);
  802. Disconnect();
  803. }
  804. }
  805. bool EmulatedController::IsControllerSupported() const {
  806. switch (npad_type) {
  807. case NpadStyleIndex::ProController:
  808. return supported_style_tag.fullkey;
  809. case NpadStyleIndex::Handheld:
  810. return supported_style_tag.handheld;
  811. case NpadStyleIndex::JoyconDual:
  812. return supported_style_tag.joycon_dual;
  813. case NpadStyleIndex::JoyconLeft:
  814. return supported_style_tag.joycon_left;
  815. case NpadStyleIndex::JoyconRight:
  816. return supported_style_tag.joycon_right;
  817. case NpadStyleIndex::GameCube:
  818. return supported_style_tag.gamecube;
  819. case NpadStyleIndex::Pokeball:
  820. return supported_style_tag.palma;
  821. case NpadStyleIndex::NES:
  822. return supported_style_tag.lark;
  823. case NpadStyleIndex::SNES:
  824. return supported_style_tag.lucia;
  825. case NpadStyleIndex::N64:
  826. return supported_style_tag.lagoon;
  827. case NpadStyleIndex::SegaGenesis:
  828. return supported_style_tag.lager;
  829. default:
  830. return false;
  831. }
  832. }
  833. void EmulatedController::Connect() {
  834. if (!IsControllerSupported()) {
  835. LOG_ERROR(Service_HID, "Controller type {} is not supported", npad_type);
  836. return;
  837. }
  838. {
  839. std::lock_guard lock{mutex};
  840. if (is_configuring) {
  841. tmp_is_connected = true;
  842. TriggerOnChange(ControllerTriggerType::Connected, false);
  843. return;
  844. }
  845. if (is_connected) {
  846. return;
  847. }
  848. is_connected = true;
  849. }
  850. TriggerOnChange(ControllerTriggerType::Connected, true);
  851. }
  852. void EmulatedController::Disconnect() {
  853. {
  854. std::lock_guard lock{mutex};
  855. if (is_configuring) {
  856. tmp_is_connected = false;
  857. TriggerOnChange(ControllerTriggerType::Disconnected, false);
  858. return;
  859. }
  860. if (!is_connected) {
  861. return;
  862. }
  863. is_connected = false;
  864. }
  865. TriggerOnChange(ControllerTriggerType::Disconnected, true);
  866. }
  867. bool EmulatedController::IsConnected(bool get_temporary_value) const {
  868. if (get_temporary_value && is_configuring) {
  869. return tmp_is_connected;
  870. }
  871. return is_connected;
  872. }
  873. bool EmulatedController::IsVibrationEnabled() const {
  874. const auto player_index = NpadIdTypeToIndex(npad_id_type);
  875. const auto& player = Settings::values.players.GetValue()[player_index];
  876. return player.vibration_enabled;
  877. }
  878. NpadIdType EmulatedController::GetNpadIdType() const {
  879. return npad_id_type;
  880. }
  881. NpadStyleIndex EmulatedController::GetNpadStyleIndex(bool get_temporary_value) const {
  882. if (get_temporary_value && is_configuring) {
  883. return tmp_npad_type;
  884. }
  885. return npad_type;
  886. }
  887. void EmulatedController::SetNpadStyleIndex(NpadStyleIndex npad_type_) {
  888. {
  889. std::lock_guard lock{mutex};
  890. if (is_configuring) {
  891. if (tmp_npad_type == npad_type_) {
  892. return;
  893. }
  894. tmp_npad_type = npad_type_;
  895. TriggerOnChange(ControllerTriggerType::Type, false);
  896. return;
  897. }
  898. if (npad_type == npad_type_) {
  899. return;
  900. }
  901. if (is_connected) {
  902. LOG_WARNING(Service_HID, "Controller {} type changed while it's connected",
  903. NpadIdTypeToIndex(npad_id_type));
  904. }
  905. npad_type = npad_type_;
  906. }
  907. TriggerOnChange(ControllerTriggerType::Type, true);
  908. }
  909. LedPattern EmulatedController::GetLedPattern() const {
  910. switch (npad_id_type) {
  911. case NpadIdType::Player1:
  912. return LedPattern{1, 0, 0, 0};
  913. case NpadIdType::Player2:
  914. return LedPattern{1, 1, 0, 0};
  915. case NpadIdType::Player3:
  916. return LedPattern{1, 1, 1, 0};
  917. case NpadIdType::Player4:
  918. return LedPattern{1, 1, 1, 1};
  919. case NpadIdType::Player5:
  920. return LedPattern{1, 0, 0, 1};
  921. case NpadIdType::Player6:
  922. return LedPattern{1, 0, 1, 0};
  923. case NpadIdType::Player7:
  924. return LedPattern{1, 0, 1, 1};
  925. case NpadIdType::Player8:
  926. return LedPattern{0, 1, 1, 0};
  927. default:
  928. return LedPattern{0, 0, 0, 0};
  929. }
  930. }
  931. ButtonValues EmulatedController::GetButtonsValues() const {
  932. return controller.button_values;
  933. }
  934. SticksValues EmulatedController::GetSticksValues() const {
  935. return controller.stick_values;
  936. }
  937. TriggerValues EmulatedController::GetTriggersValues() const {
  938. return controller.trigger_values;
  939. }
  940. ControllerMotionValues EmulatedController::GetMotionValues() const {
  941. return controller.motion_values;
  942. }
  943. ColorValues EmulatedController::GetColorsValues() const {
  944. return controller.color_values;
  945. }
  946. BatteryValues EmulatedController::GetBatteryValues() const {
  947. return controller.battery_values;
  948. }
  949. NpadButtonState EmulatedController::GetNpadButtons() const {
  950. if (is_configuring) {
  951. return {};
  952. }
  953. return controller.npad_button_state;
  954. }
  955. DebugPadButton EmulatedController::GetDebugPadButtons() const {
  956. if (is_configuring) {
  957. return {};
  958. }
  959. return controller.debug_pad_button_state;
  960. }
  961. AnalogSticks EmulatedController::GetSticks() const {
  962. if (is_configuring) {
  963. return {};
  964. }
  965. // Some drivers like stick from buttons need constant refreshing
  966. for (auto& device : stick_devices) {
  967. if (!device) {
  968. continue;
  969. }
  970. device->SoftUpdate();
  971. }
  972. return controller.analog_stick_state;
  973. }
  974. NpadGcTriggerState EmulatedController::GetTriggers() const {
  975. if (is_configuring) {
  976. return {};
  977. }
  978. return controller.gc_trigger_state;
  979. }
  980. MotionState EmulatedController::GetMotions() const {
  981. if (force_update_motion) {
  982. for (auto& device : motion_devices) {
  983. if (!device) {
  984. continue;
  985. }
  986. device->ForceUpdate();
  987. }
  988. }
  989. return controller.motion_state;
  990. }
  991. ControllerColors EmulatedController::GetColors() const {
  992. return controller.colors_state;
  993. }
  994. BatteryLevelState EmulatedController::GetBattery() const {
  995. return controller.battery_state;
  996. }
  997. void EmulatedController::TriggerOnChange(ControllerTriggerType type, bool is_npad_service_update) {
  998. for (const auto& poller_pair : callback_list) {
  999. const ControllerUpdateCallback& poller = poller_pair.second;
  1000. if (!is_npad_service_update && poller.is_npad_service) {
  1001. continue;
  1002. }
  1003. if (poller.on_change) {
  1004. poller.on_change(type);
  1005. }
  1006. }
  1007. }
  1008. int EmulatedController::SetCallback(ControllerUpdateCallback update_callback) {
  1009. std::lock_guard lock{mutex};
  1010. callback_list.insert_or_assign(last_callback_key, std::move(update_callback));
  1011. return last_callback_key++;
  1012. }
  1013. void EmulatedController::DeleteCallback(int key) {
  1014. std::lock_guard lock{mutex};
  1015. const auto& iterator = callback_list.find(key);
  1016. if (iterator == callback_list.end()) {
  1017. LOG_ERROR(Input, "Tried to delete non-existent callback {}", key);
  1018. return;
  1019. }
  1020. callback_list.erase(iterator);
  1021. }
  1022. } // namespace Core::HID