emulated_controller.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 "common/common_types.h"
  10. #include "common/input.h"
  11. #include "common/param_package.h"
  12. #include "common/settings.h"
  13. #include "common/vector_math.h"
  14. #include "core/hid/hid_types.h"
  15. #include "core/hid/motion_input.h"
  16. namespace Core::HID {
  17. const std::size_t max_emulated_controllers = 2;
  18. struct ControllerMotionInfo {
  19. Common::Input::MotionStatus raw_status{};
  20. MotionInput emulated{};
  21. };
  22. using ButtonDevices =
  23. std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeButton::NumButtons>;
  24. using StickDevices =
  25. std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeAnalog::NumAnalogs>;
  26. using ControllerMotionDevices =
  27. std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeMotion::NumMotions>;
  28. using TriggerDevices =
  29. std::array<std::unique_ptr<Common::Input::InputDevice>, Settings::NativeTrigger::NumTriggers>;
  30. using BatteryDevices =
  31. std::array<std::unique_ptr<Common::Input::InputDevice>, max_emulated_controllers>;
  32. using OutputDevices =
  33. std::array<std::unique_ptr<Common::Input::OutputDevice>, max_emulated_controllers>;
  34. using ButtonParams = std::array<Common::ParamPackage, Settings::NativeButton::NumButtons>;
  35. using StickParams = std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs>;
  36. using ControllerMotionParams = std::array<Common::ParamPackage, Settings::NativeMotion::NumMotions>;
  37. using TriggerParams = std::array<Common::ParamPackage, Settings::NativeTrigger::NumTriggers>;
  38. using BatteryParams = std::array<Common::ParamPackage, max_emulated_controllers>;
  39. using OutputParams = std::array<Common::ParamPackage, max_emulated_controllers>;
  40. using ButtonValues = std::array<Common::Input::ButtonStatus, Settings::NativeButton::NumButtons>;
  41. using SticksValues = std::array<Common::Input::StickStatus, Settings::NativeAnalog::NumAnalogs>;
  42. using TriggerValues =
  43. std::array<Common::Input::TriggerStatus, Settings::NativeTrigger::NumTriggers>;
  44. using ControllerMotionValues = std::array<ControllerMotionInfo, Settings::NativeMotion::NumMotions>;
  45. using ColorValues = std::array<Common::Input::BodyColorStatus, max_emulated_controllers>;
  46. using BatteryValues = std::array<Common::Input::BatteryStatus, max_emulated_controllers>;
  47. using VibrationValues = std::array<Common::Input::VibrationStatus, max_emulated_controllers>;
  48. struct AnalogSticks {
  49. AnalogStickState left{};
  50. AnalogStickState right{};
  51. };
  52. struct ControllerColors {
  53. NpadControllerColor fullkey{};
  54. NpadControllerColor left{};
  55. NpadControllerColor right{};
  56. };
  57. struct BatteryLevelState {
  58. NpadPowerInfo dual{};
  59. NpadPowerInfo left{};
  60. NpadPowerInfo right{};
  61. };
  62. struct ControllerMotion {
  63. Common::Vec3f accel{};
  64. Common::Vec3f gyro{};
  65. Common::Vec3f rotation{};
  66. std::array<Common::Vec3f, 3> orientation{};
  67. bool is_at_rest{};
  68. };
  69. enum EmulatedDeviceIndex : u8 {
  70. LeftIndex,
  71. RightIndex,
  72. DualIndex,
  73. AllDevices,
  74. };
  75. using MotionState = std::array<ControllerMotion, 2>;
  76. struct ControllerStatus {
  77. // Data from input_common
  78. ButtonValues button_values{};
  79. SticksValues stick_values{};
  80. ControllerMotionValues motion_values{};
  81. TriggerValues trigger_values{};
  82. ColorValues color_values{};
  83. BatteryValues battery_values{};
  84. VibrationValues vibration_values{};
  85. // Data for HID serices
  86. HomeButtonState home_button_state{};
  87. CaptureButtonState capture_button_state{};
  88. NpadButtonState npad_button_state{};
  89. DebugPadButton debug_pad_button_state{};
  90. AnalogSticks analog_stick_state{};
  91. MotionState motion_state{};
  92. NpadGcTriggerState gc_trigger_state{};
  93. ControllerColors colors_state{};
  94. BatteryLevelState battery_state{};
  95. };
  96. enum class ControllerTriggerType {
  97. Button,
  98. Stick,
  99. Trigger,
  100. Motion,
  101. Color,
  102. Battery,
  103. Vibration,
  104. Connected,
  105. Disconnected,
  106. Type,
  107. All,
  108. };
  109. struct ControllerUpdateCallback {
  110. std::function<void(ControllerTriggerType)> on_change;
  111. bool is_npad_service;
  112. };
  113. class EmulatedController {
  114. public:
  115. /**
  116. * Contains all input data (buttons, joysticks, vibration, and motion) within this controller.
  117. * @param npad_id_type npad id type for this specific controller
  118. */
  119. explicit EmulatedController(NpadIdType npad_id_type_);
  120. ~EmulatedController();
  121. YUZU_NON_COPYABLE(EmulatedController);
  122. YUZU_NON_MOVEABLE(EmulatedController);
  123. /// Converts the controller type from settings to npad type
  124. static NpadStyleIndex MapSettingsTypeToNPad(Settings::ControllerType type);
  125. /// Converts npad type to the equivalent of controller type from settings
  126. static Settings::ControllerType MapNPadToSettingsType(NpadStyleIndex type);
  127. /// Gets the NpadIdType for this controller
  128. NpadIdType GetNpadIdType() const;
  129. /// Sets the NpadStyleIndex for this controller
  130. void SetNpadStyleIndex(NpadStyleIndex npad_type_);
  131. /**
  132. * Gets the NpadStyleIndex for this controller
  133. * @param get_temporary_value If true tmp_npad_type will be returned
  134. * @return NpadStyleIndex set on the controller
  135. */
  136. NpadStyleIndex GetNpadStyleIndex(bool get_temporary_value = false) const;
  137. /**
  138. * Sets the supported controller types. Disconnects the controller if current type is not
  139. * supported
  140. * @param supported_styles bitflag with supported types
  141. */
  142. void SetSupportedNpadStyleTag(NpadStyleTag supported_styles);
  143. /**
  144. * Sets the connected status to true
  145. * @param use_temporary_value If true tmp_npad_type will be used
  146. */
  147. void Connect(bool use_temporary_value = false);
  148. /// Sets the connected status to false
  149. void Disconnect();
  150. /**
  151. * Is the emulated connected
  152. * @param get_temporary_value If true tmp_is_connected will be returned
  153. * @return true if the controller has the connected status
  154. */
  155. bool IsConnected(bool get_temporary_value = false) const;
  156. /// Returns true if vibration is enabled
  157. bool IsVibrationEnabled() const;
  158. /// Removes all callbacks created from input devices
  159. void UnloadInput();
  160. /**
  161. * Sets the emulated controller into configuring mode
  162. * This prevents the modification of the HID state of the emulated controller by input commands
  163. */
  164. void EnableConfiguration();
  165. /// Returns the emulated controller into normal mode, allowing the modification of the HID state
  166. void DisableConfiguration();
  167. /// Enables Home and Screenshot buttons
  168. void EnableSystemButtons();
  169. /// Disables Home and Screenshot buttons
  170. void DisableSystemButtons();
  171. /// Sets Home and Screenshot buttons to false
  172. void ResetSystemButtons();
  173. /// Returns true if the emulated controller is in configuring mode
  174. bool IsConfiguring() const;
  175. /// Reload all input devices
  176. void ReloadInput();
  177. /// Overrides current mapped devices with the stored configuration and reloads all input devices
  178. void ReloadFromSettings();
  179. /// Saves the current mapped configuration
  180. void SaveCurrentConfig();
  181. /// Reverts any mapped changes made that weren't saved
  182. void RestoreConfig();
  183. /// Returns a vector of mapped devices from the mapped button and stick parameters
  184. std::vector<Common::ParamPackage> GetMappedDevices(EmulatedDeviceIndex device_index) const;
  185. // Returns the current mapped button device
  186. Common::ParamPackage GetButtonParam(std::size_t index) const;
  187. // Returns the current mapped stick device
  188. Common::ParamPackage GetStickParam(std::size_t index) const;
  189. // Returns the current mapped motion device
  190. Common::ParamPackage GetMotionParam(std::size_t index) const;
  191. /**
  192. * Updates the current mapped button device
  193. * @param param ParamPackage with controller data to be mapped
  194. */
  195. void SetButtonParam(std::size_t index, Common::ParamPackage param);
  196. /**
  197. * Updates the current mapped stick device
  198. * @param param ParamPackage with controller data to be mapped
  199. */
  200. void SetStickParam(std::size_t index, Common::ParamPackage param);
  201. /**
  202. * Updates the current mapped motion device
  203. * @param param ParamPackage with controller data to be mapped
  204. */
  205. void SetMotionParam(std::size_t index, Common::ParamPackage param);
  206. /// Returns the latest button status from the controller with parameters
  207. ButtonValues GetButtonsValues() const;
  208. /// Returns the latest analog stick status from the controller with parameters
  209. SticksValues GetSticksValues() const;
  210. /// Returns the latest trigger status from the controller with parameters
  211. TriggerValues GetTriggersValues() const;
  212. /// Returns the latest motion status from the controller with parameters
  213. ControllerMotionValues GetMotionValues() const;
  214. /// Returns the latest color status from the controller with parameters
  215. ColorValues GetColorsValues() const;
  216. /// Returns the latest battery status from the controller with parameters
  217. BatteryValues GetBatteryValues() const;
  218. /// Returns the latest status of button input for the hid::HomeButton service
  219. HomeButtonState GetHomeButtons() const;
  220. /// Returns the latest status of button input for the hid::CaptureButton service
  221. CaptureButtonState GetCaptureButtons() const;
  222. /// Returns the latest status of button input for the hid::Npad service
  223. NpadButtonState GetNpadButtons() const;
  224. /// Returns the latest status of button input for the debug pad service
  225. DebugPadButton GetDebugPadButtons() const;
  226. /// Returns the latest status of stick input from the mouse
  227. AnalogSticks GetSticks() const;
  228. /// Returns the latest status of trigger input from the mouse
  229. NpadGcTriggerState GetTriggers() const;
  230. /// Returns the latest status of motion input from the mouse
  231. MotionState GetMotions() const;
  232. /// Returns the latest color value from the controller
  233. ControllerColors GetColors() const;
  234. /// Returns the latest battery status from the controller
  235. BatteryLevelState GetBattery() const;
  236. /**
  237. * Sends a specific vibration to the output device
  238. * @return true if vibration had no errors
  239. */
  240. bool SetVibration(std::size_t device_index, VibrationValue vibration);
  241. /**
  242. * Sends a small vibration to the output device
  243. * @return true if SetVibration was successfull
  244. */
  245. bool TestVibration(std::size_t device_index);
  246. /**
  247. * Sets the desired data to be polled from a controller
  248. * @param polling_mode type of input desired buttons, gyro, nfc, ir, etc.
  249. * @return true if SetPollingMode was successfull
  250. */
  251. bool SetPollingMode(Common::Input::PollingMode polling_mode);
  252. /// Returns the led pattern corresponding to this emulated controller
  253. LedPattern GetLedPattern() const;
  254. /// Asks the output device to change the player led pattern
  255. void SetLedPattern();
  256. /**
  257. * Adds a callback to the list of events
  258. * @param update_callback A ConsoleUpdateCallback that will be triggered
  259. * @return an unique key corresponding to the callback index in the list
  260. */
  261. int SetCallback(ControllerUpdateCallback update_callback);
  262. /**
  263. * Removes a callback from the list stopping any future events to this object
  264. * @param key Key corresponding to the callback index in the list
  265. */
  266. void DeleteCallback(int key);
  267. private:
  268. /// creates input devices from params
  269. void LoadDevices();
  270. /// Set the params for TAS devices
  271. void LoadTASParams();
  272. /**
  273. * @param use_temporary_value If true tmp_npad_type will be used
  274. * @return true if the controller style is fullkey
  275. */
  276. bool IsControllerFullkey(bool use_temporary_value = false) const;
  277. /**
  278. * Checks the current controller type against the supported_style_tag
  279. * @param use_temporary_value If true tmp_npad_type will be used
  280. * @return true if the controller is supported
  281. */
  282. bool IsControllerSupported(bool use_temporary_value = false) const;
  283. /**
  284. * Updates the button status of the controller
  285. * @param callback A CallbackStatus containing the button status
  286. * @param index Button ID of the to be updated
  287. */
  288. void SetButton(const Common::Input::CallbackStatus& callback, std::size_t index,
  289. Common::UUID uuid);
  290. /**
  291. * Updates the analog stick status of the controller
  292. * @param callback A CallbackStatus containing the analog stick status
  293. * @param index stick ID of the to be updated
  294. */
  295. void SetStick(const Common::Input::CallbackStatus& callback, std::size_t index,
  296. Common::UUID uuid);
  297. /**
  298. * Updates the trigger status of the controller
  299. * @param callback A CallbackStatus containing the trigger status
  300. * @param index trigger ID of the to be updated
  301. */
  302. void SetTrigger(const Common::Input::CallbackStatus& callback, std::size_t index,
  303. Common::UUID uuid);
  304. /**
  305. * Updates the motion status of the controller
  306. * @param callback A CallbackStatus containing gyro and accelerometer data
  307. * @param index motion ID of the to be updated
  308. */
  309. void SetMotion(const Common::Input::CallbackStatus& callback, std::size_t index);
  310. /**
  311. * Updates the battery status of the controller
  312. * @param callback A CallbackStatus containing the battery status
  313. * @param index Button ID of the to be updated
  314. */
  315. void SetBattery(const Common::Input::CallbackStatus& callback, std::size_t index);
  316. /**
  317. * Triggers a callback that something has changed on the controller status
  318. * @param type Input type of the event to trigger
  319. * @param is_service_update indicates if this event should only be sent to HID services
  320. */
  321. void TriggerOnChange(ControllerTriggerType type, bool is_service_update);
  322. const NpadIdType npad_id_type;
  323. NpadStyleIndex npad_type{NpadStyleIndex::None};
  324. NpadStyleTag supported_style_tag{NpadStyleSet::All};
  325. bool is_connected{false};
  326. bool is_configuring{false};
  327. bool system_buttons_enabled{true};
  328. f32 motion_sensitivity{0.01f};
  329. bool force_update_motion{false};
  330. // Temporary values to avoid doing changes while the controller is in configuring mode
  331. NpadStyleIndex tmp_npad_type{NpadStyleIndex::None};
  332. bool tmp_is_connected{false};
  333. ButtonParams button_params;
  334. StickParams stick_params;
  335. ControllerMotionParams motion_params;
  336. TriggerParams trigger_params;
  337. BatteryParams battery_params;
  338. OutputParams output_params;
  339. ButtonDevices button_devices;
  340. StickDevices stick_devices;
  341. ControllerMotionDevices motion_devices;
  342. TriggerDevices trigger_devices;
  343. BatteryDevices battery_devices;
  344. OutputDevices output_devices;
  345. // TAS related variables
  346. ButtonParams tas_button_params;
  347. StickParams tas_stick_params;
  348. ButtonDevices tas_button_devices;
  349. StickDevices tas_stick_devices;
  350. mutable std::mutex mutex;
  351. mutable std::mutex callback_mutex;
  352. std::unordered_map<int, ControllerUpdateCallback> callback_list;
  353. int last_callback_key = 0;
  354. // Stores the current status of all controller input
  355. ControllerStatus controller;
  356. };
  357. } // namespace Core::HID