emulated_controller.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. struct ControllerMotionInfo {
  18. Input::MotionStatus raw_status;
  19. MotionInput emulated{};
  20. };
  21. using ButtonDevices =
  22. std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeButton::NumButtons>;
  23. using StickDevices =
  24. std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeAnalog::NumAnalogs>;
  25. using ControllerMotionDevices =
  26. std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeMotion::NumMotions>;
  27. using TriggerDevices =
  28. std::array<std::unique_ptr<Input::InputDevice>, Settings::NativeTrigger::NumTriggers>;
  29. using BatteryDevices = std::array<std::unique_ptr<Input::InputDevice>, 2>;
  30. using OutputDevices = std::array<std::unique_ptr<Input::OutputDevice>, 2>;
  31. using ButtonParams = std::array<Common::ParamPackage, Settings::NativeButton::NumButtons>;
  32. using StickParams = std::array<Common::ParamPackage, Settings::NativeAnalog::NumAnalogs>;
  33. using ControllerMotionParams = std::array<Common::ParamPackage, Settings::NativeMotion::NumMotions>;
  34. using TriggerParams = std::array<Common::ParamPackage, Settings::NativeTrigger::NumTriggers>;
  35. using BatteryParams = std::array<Common::ParamPackage, 2>;
  36. using OutputParams = std::array<Common::ParamPackage, 2>;
  37. using ButtonValues = std::array<Input::ButtonStatus, Settings::NativeButton::NumButtons>;
  38. using SticksValues = std::array<Input::StickStatus, Settings::NativeAnalog::NumAnalogs>;
  39. using TriggerValues = std::array<Input::TriggerStatus, Settings::NativeTrigger::NumTriggers>;
  40. using ControllerMotionValues = std::array<ControllerMotionInfo, Settings::NativeMotion::NumMotions>;
  41. using ColorValues = std::array<Input::BodyColorStatus, 3>;
  42. using BatteryValues = std::array<Input::BatteryStatus, 3>;
  43. using VibrationValues = std::array<Input::VibrationStatus, 2>;
  44. struct AnalogSticks {
  45. AnalogStickState left;
  46. AnalogStickState right;
  47. };
  48. struct ControllerColors {
  49. NpadControllerColor fullkey;
  50. NpadControllerColor left;
  51. NpadControllerColor right;
  52. };
  53. struct BatteryLevelState {
  54. NpadPowerInfo dual;
  55. NpadPowerInfo left;
  56. NpadPowerInfo right;
  57. };
  58. struct ControllerMotion {
  59. bool is_at_rest;
  60. Common::Vec3f accel{};
  61. Common::Vec3f gyro{};
  62. Common::Vec3f rotation{};
  63. std::array<Common::Vec3f, 3> orientation{};
  64. };
  65. using MotionState = std::array<ControllerMotion, 2>;
  66. struct ControllerStatus {
  67. // Data from input_common
  68. ButtonValues button_values{};
  69. SticksValues stick_values{};
  70. ControllerMotionValues motion_values{};
  71. TriggerValues trigger_values{};
  72. ColorValues color_values{};
  73. BatteryValues battery_values{};
  74. VibrationValues vibration_values{};
  75. // Data for Nintendo devices
  76. NpadButtonState npad_button_state{};
  77. DebugPadButton debug_pad_button_state{};
  78. AnalogSticks analog_stick_state{};
  79. MotionState motion_state{};
  80. NpadGcTriggerState gc_trigger_state{};
  81. ControllerColors colors_state{};
  82. BatteryLevelState battery_state{};
  83. };
  84. enum class ControllerTriggerType {
  85. Button,
  86. Stick,
  87. Trigger,
  88. Motion,
  89. Color,
  90. Battery,
  91. Vibration,
  92. Connected,
  93. Disconnected,
  94. Type,
  95. All,
  96. };
  97. struct ControllerUpdateCallback {
  98. std::function<void(ControllerTriggerType)> on_change;
  99. };
  100. class EmulatedController {
  101. public:
  102. /**
  103. * TODO: Write description
  104. *
  105. * @param npad_id_type
  106. */
  107. explicit EmulatedController(NpadIdType npad_id_type_);
  108. ~EmulatedController();
  109. YUZU_NON_COPYABLE(EmulatedController);
  110. YUZU_NON_MOVEABLE(EmulatedController);
  111. static NpadType MapSettingsTypeToNPad(Settings::ControllerType type);
  112. static Settings::ControllerType MapNPadToSettingsType(NpadType type);
  113. /// Gets the NpadIdType for this controller.
  114. NpadIdType GetNpadIdType() const;
  115. /// Sets the NpadType for this controller.
  116. void SetNpadType(NpadType npad_type_);
  117. /// Gets the NpadType for this controller.
  118. NpadType GetNpadType() const;
  119. /// Gets the NpadType for this controller.
  120. LedPattern GetLedPattern() const;
  121. void Connect();
  122. void Disconnect();
  123. bool IsConnected() const;
  124. bool IsVibrationEnabled() const;
  125. void ReloadFromSettings();
  126. void ReloadInput();
  127. void UnloadInput();
  128. void EnableConfiguration();
  129. void DisableConfiguration();
  130. bool IsConfiguring() const;
  131. void SaveCurrentConfig();
  132. void RestoreConfig();
  133. std::vector<Common::ParamPackage> GetMappedDevices() const;
  134. Common::ParamPackage GetButtonParam(std::size_t index) const;
  135. Common::ParamPackage GetStickParam(std::size_t index) const;
  136. Common::ParamPackage GetMotionParam(std::size_t index) const;
  137. void SetButtonParam(std::size_t index, Common::ParamPackage param);
  138. void SetStickParam(std::size_t index, Common::ParamPackage param);
  139. void SetMotionParam(std::size_t index, Common::ParamPackage param);
  140. ButtonValues GetButtonsValues() const;
  141. SticksValues GetSticksValues() const;
  142. TriggerValues GetTriggersValues() const;
  143. ControllerMotionValues GetMotionValues() const;
  144. ColorValues GetColorsValues() const;
  145. BatteryValues GetBatteryValues() const;
  146. NpadButtonState GetNpadButtons() const;
  147. DebugPadButton GetDebugPadButtons() const;
  148. AnalogSticks GetSticks() const;
  149. NpadGcTriggerState GetTriggers() const;
  150. MotionState GetMotions() const;
  151. ControllerColors GetColors() const;
  152. BatteryLevelState GetBattery() const;
  153. bool SetVibration(std::size_t device_index, VibrationValue vibration);
  154. bool TestVibration(std::size_t device_index);
  155. void SetLedPattern();
  156. int SetCallback(ControllerUpdateCallback update_callback);
  157. void DeleteCallback(int key);
  158. private:
  159. /**
  160. * Sets the status of a button. Applies toggle properties to the output.
  161. *
  162. * @param A CallbackStatus and a button index number
  163. */
  164. void SetButton(Input::CallbackStatus callback, std::size_t index);
  165. void SetStick(Input::CallbackStatus callback, std::size_t index);
  166. void SetTrigger(Input::CallbackStatus callback, std::size_t index);
  167. void SetMotion(Input::CallbackStatus callback, std::size_t index);
  168. void SetBattery(Input::CallbackStatus callback, std::size_t index);
  169. /**
  170. * Triggers a callback that something has changed
  171. *
  172. * @param Input type of the trigger
  173. */
  174. void TriggerOnChange(ControllerTriggerType type);
  175. NpadIdType npad_id_type;
  176. NpadType npad_type{NpadType::None};
  177. bool is_connected{false};
  178. bool is_configuring{false};
  179. bool is_vibration_enabled{true};
  180. f32 motion_sensitivity{0.01f};
  181. ButtonParams button_params;
  182. StickParams stick_params;
  183. ControllerMotionParams motion_params;
  184. TriggerParams trigger_params;
  185. BatteryParams battery_params;
  186. OutputParams output_params;
  187. ButtonDevices button_devices;
  188. StickDevices stick_devices;
  189. ControllerMotionDevices motion_devices;
  190. TriggerDevices trigger_devices;
  191. BatteryDevices battery_devices;
  192. OutputDevices output_devices;
  193. mutable std::mutex mutex;
  194. std::unordered_map<int, ControllerUpdateCallback> callback_list;
  195. int last_callback_key = 0;
  196. ControllerStatus controller;
  197. };
  198. } // namespace Core::HID