emulated_controller.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <array>
  5. #include <functional>
  6. #include <memory>
  7. #include <mutex>
  8. #include <unordered_map>
  9. #include <vector>
  10. #include "common/common_types.h"
  11. #include "common/input.h"
  12. #include "common/param_package.h"
  13. #include "common/settings.h"
  14. #include "common/vector_math.h"
  15. #include "core/hid/hid_types.h"
  16. #include "core/hid/irs_types.h"
  17. #include "core/hid/motion_input.h"
  18. namespace Core::HID {
  19. const std::size_t max_emulated_controllers = 2;
  20. const std::size_t output_devices_size = 4;
  21. struct ControllerMotionInfo {
  22. Common::Input::MotionStatus raw_status{};
  23. MotionInput emulated{};
  24. };
  25. using ButtonDevices =
  26. std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeButton::NumButtons>;
  27. using StickDevices =
  28. std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeAnalog::NumAnalogs>;
  29. using ControllerMotionDevices =
  30. std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeMotion::NumMotions>;
  31. using TriggerDevices =
  32. std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeTrigger::NumTriggers>;
  33. using ColorDevices =
  34. std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
  35. using BatteryDevices =
  36. std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
  37. using CameraDevices = std::unique_ptr<Common::Input::InputDevice>;
  38. using RingAnalogDevice = std::unique_ptr<Common::Input::InputDevice>;
  39. using NfcDevices = std::unique_ptr<Common::Input::InputDevice>;
  40. using OutputDevices = std::array<std::unique_ptr<Common::Input::OutputDevice>, output_devices_size>;
  41. using ButtonParams = std::array<Common::ParamPackage, Settings::NativeButton::NumButtons>;
  42. using StickParams = std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs>;
  43. using ControllerMotionParams = std::array<Common::ParamPackage, Settings::NativeMotion::NumMotions>;
  44. using TriggerParams = std::array<Common::ParamPackage, Settings::NativeTrigger::NumTriggers>;
  45. using ColorParams = std::array<Common::ParamPackage, max_emulated_controllers>;
  46. using BatteryParams = std::array<Common::ParamPackage, max_emulated_controllers>;
  47. using CameraParams = Common::ParamPackage;
  48. using RingAnalogParams = Common::ParamPackage;
  49. using NfcParams = Common::ParamPackage;
  50. using OutputParams = std::array<Common::ParamPackage, output_devices_size>;
  51. using ButtonValues = std::array<Common::Input::ButtonStatus, Settings::NativeButton::NumButtons>;
  52. using SticksValues = std::array<Common::Input::StickStatus, Settings::NativeAnalog::NumAnalogs>;
  53. using TriggerValues =
  54. std::array<Common::Input::TriggerStatus, Settings::NativeTrigger::NumTriggers>;
  55. using ControllerMotionValues = std::array<ControllerMotionInfo, Settings::NativeMotion::NumMotions>;
  56. using ColorValues = std::array<Common::Input::BodyColorStatus, max_emulated_controllers>;
  57. using BatteryValues = std::array<Common::Input::BatteryStatus, max_emulated_controllers>;
  58. using CameraValues = Common::Input::CameraStatus;
  59. using RingAnalogValue = Common::Input::AnalogStatus;
  60. using NfcValues = Common::Input::NfcStatus;
  61. using VibrationValues = std::array<Common::Input::VibrationStatus, max_emulated_controllers>;
  62. struct AnalogSticks {
  63. AnalogStickState left{};
  64. AnalogStickState right{};
  65. };
  66. struct ControllerColors {
  67. NpadControllerColor fullkey{};
  68. NpadControllerColor left{};
  69. NpadControllerColor right{};
  70. };
  71. struct BatteryLevelState {
  72. NpadPowerInfo dual{};
  73. NpadPowerInfo left{};
  74. NpadPowerInfo right{};
  75. };
  76. struct CameraState {
  77. Core::IrSensor::ImageTransferProcessorFormat format{};
  78. std::vector<u8> data{};
  79. std::size_t sample{};
  80. };
  81. struct RingSensorForce {
  82. f32 force;
  83. };
  84. struct NfcState {
  85. Common::Input::NfcState state{};
  86. std::vector<u8> data{};
  87. };
  88. struct ControllerMotion {
  89. Common::Vec3f accel{};
  90. Common::Vec3f gyro{};
  91. Common::Vec3f rotation{};
  92. std::array<Common::Vec3f, 3> orientation{};
  93. bool is_at_rest{};
  94. };
  95. enum EmulatedDeviceIndex : u8 {
  96. LeftIndex,
  97. RightIndex,
  98. DualIndex,
  99. AllDevices,
  100. };
  101. using MotionState = std::array<ControllerMotion, 2>;
  102. struct ControllerStatus {
  103. // Data from input_common
  104. ButtonValues button_values{};
  105. SticksValues stick_values{};
  106. ControllerMotionValues motion_values{};
  107. TriggerValues trigger_values{};
  108. ColorValues color_values{};
  109. BatteryValues battery_values{};
  110. VibrationValues vibration_values{};
  111. CameraValues camera_values{};
  112. RingAnalogValue ring_analog_value{};
  113. NfcValues nfc_values{};
  114. // Data for HID serices
  115. HomeButtonState home_button_state{};
  116. CaptureButtonState capture_button_state{};
  117. NpadButtonState npad_button_state{};
  118. DebugPadButton debug_pad_button_state{};
  119. AnalogSticks analog_stick_state{};
  120. MotionState motion_state{};
  121. NpadGcTriggerState gc_trigger_state{};
  122. ControllerColors colors_state{};
  123. BatteryLevelState battery_state{};
  124. CameraState camera_state{};
  125. RingSensorForce ring_analog_state{};
  126. NfcState nfc_state{};
  127. };
  128. enum class ControllerTriggerType {
  129. Button,
  130. Stick,
  131. Trigger,
  132. Motion,
  133. Color,
  134. Battery,
  135. Vibration,
  136. IrSensor,
  137. RingController,
  138. Nfc,
  139. Connected,
  140. Disconnected,
  141. Type,
  142. All,
  143. };
  144. struct ControllerUpdateCallback {
  145. std::function<void(ControllerTriggerType)> on_change;
  146. bool is_npad_service;
  147. };
  148. class EmulatedController {
  149. public:
  150. /**
  151. * Contains all input data (buttons, joysticks, vibration, and motion) within this controller.
  152. * @param npad_id_type npad id type for this specific controller
  153. */
  154. explicit EmulatedController(NpadIdType npad_id_type_);
  155. ~EmulatedController();
  156. YUZU_NON_COPYABLE(EmulatedController);
  157. YUZU_NON_MOVEABLE(EmulatedController);
  158. /// Converts the controller type from settings to npad type
  159. static NpadStyleIndex MapSettingsTypeToNPad(Settings::ControllerType type);
  160. /// Converts npad type to the equivalent of controller type from settings
  161. static Settings::ControllerType MapNPadToSettingsType(NpadStyleIndex type);
  162. /// Gets the NpadIdType for this controller
  163. NpadIdType GetNpadIdType() const;
  164. /// Sets the NpadStyleIndex for this controller
  165. void SetNpadStyleIndex(NpadStyleIndex npad_type_);
  166. /**
  167. * Gets the NpadStyleIndex for this controller
  168. * @param get_temporary_value If true tmp_npad_type will be returned
  169. * @return NpadStyleIndex set on the controller
  170. */
  171. NpadStyleIndex GetNpadStyleIndex(bool get_temporary_value = false) const;
  172. /**
  173. * Sets the supported controller types. Disconnects the controller if current type is not
  174. * supported
  175. * @param supported_styles bitflag with supported types
  176. */
  177. void SetSupportedNpadStyleTag(NpadStyleTag supported_styles);
  178. /**
  179. * Sets the connected status to true
  180. * @param use_temporary_value If true tmp_npad_type will be used
  181. */
  182. void Connect(bool use_temporary_value = false);
  183. /// Sets the connected status to false
  184. void Disconnect();
  185. /**
  186. * Is the emulated connected
  187. * @param get_temporary_value If true tmp_is_connected will be returned
  188. * @return true if the controller has the connected status
  189. */
  190. bool IsConnected(bool get_temporary_value = false) const;
  191. /// Removes all callbacks created from input devices
  192. void UnloadInput();
  193. /**
  194. * Sets the emulated controller into configuring mode
  195. * This prevents the modification of the HID state of the emulated controller by input commands
  196. */
  197. void EnableConfiguration();
  198. /// Returns the emulated controller into normal mode, allowing the modification of the HID state
  199. void DisableConfiguration();
  200. /// Enables Home and Screenshot buttons
  201. void EnableSystemButtons();
  202. /// Disables Home and Screenshot buttons
  203. void DisableSystemButtons();
  204. /// Sets Home and Screenshot buttons to false
  205. void ResetSystemButtons();
  206. /// Returns true if the emulated controller is in configuring mode
  207. bool IsConfiguring() const;
  208. /// Reload all input devices
  209. void ReloadInput();
  210. /// Overrides current mapped devices with the stored configuration and reloads all input devices
  211. void ReloadFromSettings();
  212. /// Saves the current mapped configuration
  213. void SaveCurrentConfig();
  214. /// Reverts any mapped changes made that weren't saved
  215. void RestoreConfig();
  216. /// Returns a vector of mapped devices from the mapped button and stick parameters
  217. std::vector<Common::ParamPackage> GetMappedDevices() const;
  218. // Returns the current mapped button device
  219. Common::ParamPackage GetButtonParam(std::size_t index) const;
  220. // Returns the current mapped stick device
  221. Common::ParamPackage GetStickParam(std::size_t index) const;
  222. // Returns the current mapped motion device
  223. Common::ParamPackage GetMotionParam(std::size_t index) const;
  224. /**
  225. * Updates the current mapped button device
  226. * @param param ParamPackage with controller data to be mapped
  227. */
  228. void SetButtonParam(std::size_t index, Common::ParamPackage param);
  229. /**
  230. * Updates the current mapped stick device
  231. * @param param ParamPackage with controller data to be mapped
  232. */
  233. void SetStickParam(std::size_t index, Common::ParamPackage param);
  234. /**
  235. * Updates the current mapped motion device
  236. * @param param ParamPackage with controller data to be mapped
  237. */
  238. void SetMotionParam(std::size_t index, Common::ParamPackage param);
  239. /// Returns the latest button status from the controller with parameters
  240. ButtonValues GetButtonsValues() const;
  241. /// Returns the latest analog stick status from the controller with parameters
  242. SticksValues GetSticksValues() const;
  243. /// Returns the latest trigger status from the controller with parameters
  244. TriggerValues GetTriggersValues() const;
  245. /// Returns the latest motion status from the controller with parameters
  246. ControllerMotionValues GetMotionValues() const;
  247. /// Returns the latest color status from the controller with parameters
  248. ColorValues GetColorsValues() const;
  249. /// Returns the latest battery status from the controller with parameters
  250. BatteryValues GetBatteryValues() const;
  251. /// Returns the latest camera status from the controller with parameters
  252. CameraValues GetCameraValues() const;
  253. /// Returns the latest status of analog input from the ring sensor with parameters
  254. RingAnalogValue GetRingSensorValues() const;
  255. /// Returns the latest status of button input for the hid::HomeButton service
  256. HomeButtonState GetHomeButtons() const;
  257. /// Returns the latest status of button input for the hid::CaptureButton service
  258. CaptureButtonState GetCaptureButtons() const;
  259. /// Returns the latest status of button input for the hid::Npad service
  260. NpadButtonState GetNpadButtons() const;
  261. /// Returns the latest status of button input for the debug pad service
  262. DebugPadButton GetDebugPadButtons() const;
  263. /// Returns the latest status of stick input from the mouse
  264. AnalogSticks GetSticks() const;
  265. /// Returns the latest status of trigger input from the mouse
  266. NpadGcTriggerState GetTriggers() const;
  267. /// Returns the latest status of motion input from the mouse
  268. MotionState GetMotions() const;
  269. /// Returns the latest color value from the controller
  270. ControllerColors GetColors() const;
  271. /// Returns the latest battery status from the controller
  272. BatteryLevelState GetBattery() const;
  273. /// Returns the latest camera status from the controller
  274. const CameraState& GetCamera() const;
  275. /// Returns the latest ringcon force sensor value
  276. RingSensorForce GetRingSensorForce() const;
  277. /// Returns the latest ntag status from the controller
  278. const NfcState& GetNfc() const;
  279. /**
  280. * Sends a specific vibration to the output device
  281. * @return true if vibration had no errors
  282. */
  283. bool SetVibration(std::size_t device_index, VibrationValue vibration);
  284. /**
  285. * Sends a small vibration to the output device
  286. * @return true if SetVibration was successfull
  287. */
  288. bool IsVibrationEnabled(std::size_t device_index);
  289. /**
  290. * Sets the desired data to be polled from a controller
  291. * @param polling_mode type of input desired buttons, gyro, nfc, ir, etc.
  292. * @return true if SetPollingMode was successfull
  293. */
  294. bool SetPollingMode(Common::Input::PollingMode polling_mode);
  295. /**
  296. * Sets the desired camera format to be polled from a controller
  297. * @param camera_format size of each frame
  298. * @return true if SetCameraFormat was successfull
  299. */
  300. bool SetCameraFormat(Core::IrSensor::ImageTransferProcessorFormat camera_format);
  301. // Returns the current mapped ring device
  302. Common::ParamPackage GetRingParam() const;
  303. /**
  304. * Updates the current mapped ring device
  305. * @param param ParamPackage with ring sensor data to be mapped
  306. */
  307. void SetRingParam(Common::ParamPackage param);
  308. /// Returns true if the device has nfc support
  309. bool HasNfc() const;
  310. /// Returns true if the nfc tag was written
  311. bool WriteNfc(const std::vector<u8>& data);
  312. /// Returns the led pattern corresponding to this emulated controller
  313. LedPattern GetLedPattern() const;
  314. /// Asks the output device to change the player led pattern
  315. void SetLedPattern();
  316. /**
  317. * Adds a callback to the list of events
  318. * @param update_callback A ConsoleUpdateCallback that will be triggered
  319. * @return an unique key corresponding to the callback index in the list
  320. */
  321. int SetCallback(ControllerUpdateCallback update_callback);
  322. /**
  323. * Removes a callback from the list stopping any future events to this object
  324. * @param key Key corresponding to the callback index in the list
  325. */
  326. void DeleteCallback(int key);
  327. private:
  328. /// creates input devices from params
  329. void LoadDevices();
  330. /// Set the params for TAS devices
  331. void LoadTASParams();
  332. /// Set the params for virtual pad devices
  333. void LoadVirtualGamepadParams();
  334. /**
  335. * @param use_temporary_value If true tmp_npad_type will be used
  336. * @return true if the controller style is fullkey
  337. */
  338. bool IsControllerFullkey(bool use_temporary_value = false) const;
  339. /**
  340. * Checks the current controller type against the supported_style_tag
  341. * @param use_temporary_value If true tmp_npad_type will be used
  342. * @return true if the controller is supported
  343. */
  344. bool IsControllerSupported(bool use_temporary_value = false) const;
  345. /**
  346. * Updates the button status of the controller
  347. * @param callback A CallbackStatus containing the button status
  348. * @param index Button ID of the to be updated
  349. */
  350. void SetButton(const Common::Input::CallbackStatus& callback, std::size_t index,
  351. Common::UUID uuid);
  352. /**
  353. * Updates the analog stick status of the controller
  354. * @param callback A CallbackStatus containing the analog stick status
  355. * @param index stick ID of the to be updated
  356. */
  357. void SetStick(const Common::Input::CallbackStatus& callback, std::size_t index,
  358. Common::UUID uuid);
  359. /**
  360. * Updates the trigger status of the controller
  361. * @param callback A CallbackStatus containing the trigger status
  362. * @param index trigger ID of the to be updated
  363. */
  364. void SetTrigger(const Common::Input::CallbackStatus& callback, std::size_t index,
  365. Common::UUID uuid);
  366. /**
  367. * Updates the motion status of the controller
  368. * @param callback A CallbackStatus containing gyro and accelerometer data
  369. * @param index motion ID of the to be updated
  370. */
  371. void SetMotion(const Common::Input::CallbackStatus& callback, std::size_t index);
  372. /**
  373. * Updates the color status of the controller
  374. * @param callback A CallbackStatus containing the color status
  375. * @param index color ID of the to be updated
  376. */
  377. void SetColors(const Common::Input::CallbackStatus& callback, std::size_t index);
  378. /**
  379. * Updates the battery status of the controller
  380. * @param callback A CallbackStatus containing the battery status
  381. * @param index battery ID of the to be updated
  382. */
  383. void SetBattery(const Common::Input::CallbackStatus& callback, std::size_t index);
  384. /**
  385. * Updates the camera status of the controller
  386. * @param callback A CallbackStatus containing the camera status
  387. */
  388. void SetCamera(const Common::Input::CallbackStatus& callback);
  389. /**
  390. * Updates the ring analog sensor status of the ring controller
  391. * @param callback A CallbackStatus containing the force status
  392. */
  393. void SetRingAnalog(const Common::Input::CallbackStatus& callback);
  394. /**
  395. * Updates the nfc status of the controller
  396. * @param callback A CallbackStatus containing the nfc status
  397. */
  398. void SetNfc(const Common::Input::CallbackStatus& callback);
  399. /**
  400. * Converts a color format from bgra to rgba
  401. * @param color in bgra format
  402. * @return NpadColor in rgba format
  403. */
  404. NpadColor GetNpadColor(u32 color);
  405. /**
  406. * Triggers a callback that something has changed on the controller status
  407. * @param type Input type of the event to trigger
  408. * @param is_service_update indicates if this event should only be sent to HID services
  409. */
  410. void TriggerOnChange(ControllerTriggerType type, bool is_service_update);
  411. const NpadIdType npad_id_type;
  412. NpadStyleIndex npad_type{NpadStyleIndex::None};
  413. NpadStyleIndex original_npad_type{NpadStyleIndex::None};
  414. NpadStyleTag supported_style_tag{NpadStyleSet::All};
  415. bool is_connected{false};
  416. bool is_configuring{false};
  417. bool system_buttons_enabled{true};
  418. f32 motion_sensitivity{0.01f};
  419. bool force_update_motion{false};
  420. // Temporary values to avoid doing changes while the controller is in configuring mode
  421. NpadStyleIndex tmp_npad_type{NpadStyleIndex::None};
  422. bool tmp_is_connected{false};
  423. ButtonParams button_params;
  424. StickParams stick_params;
  425. ControllerMotionParams motion_params;
  426. TriggerParams trigger_params;
  427. BatteryParams battery_params;
  428. ColorParams color_params;
  429. CameraParams camera_params;
  430. RingAnalogParams ring_params;
  431. NfcParams nfc_params;
  432. OutputParams output_params;
  433. ButtonDevices button_devices;
  434. StickDevices stick_devices;
  435. ControllerMotionDevices motion_devices;
  436. TriggerDevices trigger_devices;
  437. BatteryDevices battery_devices;
  438. ColorDevices color_devices;
  439. CameraDevices camera_devices;
  440. RingAnalogDevice ring_analog_device;
  441. NfcDevices nfc_devices;
  442. OutputDevices output_devices;
  443. // TAS related variables
  444. ButtonParams tas_button_params;
  445. StickParams tas_stick_params;
  446. ButtonDevices tas_button_devices;
  447. StickDevices tas_stick_devices;
  448. // Virtual gamepad related variables
  449. ButtonParams virtual_button_params;
  450. StickParams virtual_stick_params;
  451. ButtonDevices virtual_button_devices;
  452. StickDevices virtual_stick_devices;
  453. mutable std::mutex mutex;
  454. mutable std::mutex callback_mutex;
  455. std::unordered_map<int, ControllerUpdateCallback> callback_list;
  456. int last_callback_key = 0;
  457. // Stores the current status of all controller input
  458. ControllerStatus controller;
  459. };
  460. } // namespace Core::HID