emulated_controller.h 15 KB

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