emulated_controller.h 17 KB

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