emulated_controller.h 13 KB

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