emulated_controller.h 20 KB

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