emulated_controller.cpp 41 KB

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