input.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // Copyright 2017 Citra 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 <memory>
  7. #include <string>
  8. #include <unordered_map>
  9. #include <utility>
  10. #include "common/logging/log.h"
  11. #include "common/param_package.h"
  12. #include "common/uuid.h"
  13. namespace Common::Input {
  14. // Type of data that is expected to recieve or send
  15. enum class InputType {
  16. None,
  17. Battery,
  18. Button,
  19. Stick,
  20. Analog,
  21. Trigger,
  22. Motion,
  23. Touch,
  24. Color,
  25. Vibration,
  26. Nfc,
  27. Ir,
  28. };
  29. // Internal battery charge level
  30. enum class BatteryLevel : u32 {
  31. None,
  32. Empty,
  33. Critical,
  34. Low,
  35. Medium,
  36. Full,
  37. Charging,
  38. };
  39. enum class PollingMode {
  40. // Constant polling of buttons, analogs and motion data
  41. Active,
  42. // Only update on button change, digital analogs
  43. Pasive,
  44. // Enable near field communication polling
  45. NFC,
  46. // Enable infrared camera polling
  47. IR,
  48. };
  49. // Vibration reply from the controller
  50. enum class VibrationError {
  51. None,
  52. NotSupported,
  53. Disabled,
  54. Unknown,
  55. };
  56. // Polling mode reply from the controller
  57. enum class PollingError {
  58. None,
  59. NotSupported,
  60. Unknown,
  61. };
  62. // Hint for amplification curve to be used
  63. enum class VibrationAmplificationType {
  64. Linear,
  65. Exponential,
  66. };
  67. // Analog properties for calibration
  68. struct AnalogProperties {
  69. // Anything below this value will be detected as zero
  70. float deadzone{};
  71. // Anyting above this values will be detected as one
  72. float range{1.0f};
  73. // Minimum value to be detected as active
  74. float threshold{0.5f};
  75. // Drift correction applied to the raw data
  76. float offset{};
  77. // Invert direction of the sensor data
  78. bool inverted{};
  79. };
  80. // Single analog sensor data
  81. struct AnalogStatus {
  82. float value{};
  83. float raw_value{};
  84. AnalogProperties properties{};
  85. };
  86. // Button data
  87. struct ButtonStatus {
  88. Common::UUID uuid{};
  89. bool value{};
  90. bool inverted{};
  91. bool toggle{};
  92. bool locked{};
  93. };
  94. // Internal battery data
  95. using BatteryStatus = BatteryLevel;
  96. // Analog and digital joystick data
  97. struct StickStatus {
  98. Common::UUID uuid{};
  99. AnalogStatus x{};
  100. AnalogStatus y{};
  101. bool left{};
  102. bool right{};
  103. bool up{};
  104. bool down{};
  105. };
  106. // Analog and digital trigger data
  107. struct TriggerStatus {
  108. Common::UUID uuid{};
  109. AnalogStatus analog{};
  110. ButtonStatus pressed{};
  111. };
  112. // 3D vector representing motion input
  113. struct MotionSensor {
  114. AnalogStatus x{};
  115. AnalogStatus y{};
  116. AnalogStatus z{};
  117. };
  118. // Motion data used to calculate controller orientation
  119. struct MotionStatus {
  120. // Gyroscope vector measurement in radians/s.
  121. MotionSensor gyro{};
  122. // Acceleration vector measurement in G force
  123. MotionSensor accel{};
  124. // Time since last measurement in microseconds
  125. u64 delta_timestamp{};
  126. // Request to update after reading the value
  127. bool force_update{};
  128. };
  129. // Data of a single point on a touch screen
  130. struct TouchStatus {
  131. ButtonStatus pressed{};
  132. AnalogStatus x{};
  133. AnalogStatus y{};
  134. int id{};
  135. };
  136. // Physical controller color in RGB format
  137. struct BodyColorStatus {
  138. u32 body{};
  139. u32 buttons{};
  140. };
  141. // HD rumble data
  142. struct VibrationStatus {
  143. f32 low_amplitude{};
  144. f32 low_frequency{};
  145. f32 high_amplitude{};
  146. f32 high_frequency{};
  147. VibrationAmplificationType type;
  148. };
  149. // Physical controller LED pattern
  150. struct LedStatus {
  151. bool led_1{};
  152. bool led_2{};
  153. bool led_3{};
  154. bool led_4{};
  155. };
  156. // List of buttons to be passed to Qt that can be translated
  157. enum class ButtonNames {
  158. Undefined,
  159. Invalid,
  160. // This will display the engine name instead of the button name
  161. Engine,
  162. // This will display the button by value instead of the button name
  163. Value,
  164. ButtonLeft,
  165. ButtonRight,
  166. ButtonDown,
  167. ButtonUp,
  168. TriggerZ,
  169. TriggerR,
  170. TriggerL,
  171. ButtonA,
  172. ButtonB,
  173. ButtonX,
  174. ButtonY,
  175. ButtonStart,
  176. // DS4 button names
  177. L1,
  178. L2,
  179. L3,
  180. R1,
  181. R2,
  182. R3,
  183. Circle,
  184. Cross,
  185. Square,
  186. Triangle,
  187. Share,
  188. Options,
  189. // Mouse buttons
  190. ButtonMouseWheel,
  191. ButtonBackward,
  192. ButtonForward,
  193. ButtonTask,
  194. ButtonExtra,
  195. };
  196. // Callback data consisting of an input type and the equivalent data status
  197. struct CallbackStatus {
  198. InputType type{InputType::None};
  199. ButtonStatus button_status{};
  200. StickStatus stick_status{};
  201. AnalogStatus analog_status{};
  202. TriggerStatus trigger_status{};
  203. MotionStatus motion_status{};
  204. TouchStatus touch_status{};
  205. BodyColorStatus color_status{};
  206. BatteryStatus battery_status{};
  207. VibrationStatus vibration_status{};
  208. };
  209. // Triggered once every input change
  210. struct InputCallback {
  211. std::function<void(const CallbackStatus&)> on_change;
  212. };
  213. /// An abstract class template for an input device (a button, an analog input, etc.).
  214. class InputDevice {
  215. public:
  216. virtual ~InputDevice() = default;
  217. // Request input device to update if necessary
  218. virtual void SoftUpdate() {}
  219. // Force input device to update data regardless of the current state
  220. virtual void ForceUpdate() {}
  221. // Sets the function to be triggered when input changes
  222. void SetCallback(InputCallback callback_) {
  223. callback = std::move(callback_);
  224. }
  225. // Triggers the function set in the callback
  226. void TriggerOnChange(const CallbackStatus& status) {
  227. if (callback.on_change) {
  228. callback.on_change(status);
  229. }
  230. }
  231. private:
  232. InputCallback callback;
  233. };
  234. /// An abstract class template for an output device (rumble, LED pattern, polling mode).
  235. class OutputDevice {
  236. public:
  237. virtual ~OutputDevice() = default;
  238. virtual void SetLED([[maybe_unused]] const LedStatus& led_status) {}
  239. virtual VibrationError SetVibration([[maybe_unused]] const VibrationStatus& vibration_status) {
  240. return VibrationError::NotSupported;
  241. }
  242. virtual PollingError SetPollingMode([[maybe_unused]] PollingMode polling_mode) {
  243. return PollingError::NotSupported;
  244. }
  245. };
  246. /// An abstract class template for a factory that can create input devices.
  247. template <typename InputDeviceType>
  248. class Factory {
  249. public:
  250. virtual ~Factory() = default;
  251. virtual std::unique_ptr<InputDeviceType> Create(const Common::ParamPackage&) = 0;
  252. };
  253. namespace Impl {
  254. template <typename InputDeviceType>
  255. using FactoryListType = std::unordered_map<std::string, std::shared_ptr<Factory<InputDeviceType>>>;
  256. template <typename InputDeviceType>
  257. struct FactoryList {
  258. static FactoryListType<InputDeviceType> list;
  259. };
  260. template <typename InputDeviceType>
  261. FactoryListType<InputDeviceType> FactoryList<InputDeviceType>::list;
  262. } // namespace Impl
  263. /**
  264. * Registers an input device factory.
  265. * @tparam InputDeviceType the type of input devices the factory can create
  266. * @param name the name of the factory. Will be used to match the "engine" parameter when creating
  267. * a device
  268. * @param factory the factory object to register
  269. */
  270. template <typename InputDeviceType>
  271. void RegisterFactory(const std::string& name, std::shared_ptr<Factory<InputDeviceType>> factory) {
  272. auto pair = std::make_pair(name, std::move(factory));
  273. if (!Impl::FactoryList<InputDeviceType>::list.insert(std::move(pair)).second) {
  274. LOG_ERROR(Input, "Factory '{}' already registered", name);
  275. }
  276. }
  277. /**
  278. * Unregisters an input device factory.
  279. * @tparam InputDeviceType the type of input devices the factory can create
  280. * @param name the name of the factory to unregister
  281. */
  282. template <typename InputDeviceType>
  283. void UnregisterFactory(const std::string& name) {
  284. if (Impl::FactoryList<InputDeviceType>::list.erase(name) == 0) {
  285. LOG_ERROR(Input, "Factory '{}' not registered", name);
  286. }
  287. }
  288. /**
  289. * Create an input device from given paramters.
  290. * @tparam InputDeviceType the type of input devices to create
  291. * @param params a serialized ParamPackage string that contains all parameters for creating the
  292. * device
  293. */
  294. template <typename InputDeviceType>
  295. std::unique_ptr<InputDeviceType> CreateDeviceFromString(const std::string& params) {
  296. const Common::ParamPackage package(params);
  297. const std::string engine = package.Get("engine", "null");
  298. const auto& factory_list = Impl::FactoryList<InputDeviceType>::list;
  299. const auto pair = factory_list.find(engine);
  300. if (pair == factory_list.end()) {
  301. if (engine != "null") {
  302. LOG_ERROR(Input, "Unknown engine name: {}", engine);
  303. }
  304. return std::make_unique<InputDeviceType>();
  305. }
  306. return pair->second->Create(package);
  307. }
  308. /**
  309. * Create an input device from given paramters.
  310. * @tparam InputDeviceType the type of input devices to create
  311. * @param A ParamPackage that contains all parameters for creating the device
  312. */
  313. template <typename InputDeviceType>
  314. std::unique_ptr<InputDeviceType> CreateDevice(const Common::ParamPackage package) {
  315. const std::string engine = package.Get("engine", "null");
  316. const auto& factory_list = Impl::FactoryList<InputDeviceType>::list;
  317. const auto pair = factory_list.find(engine);
  318. if (pair == factory_list.end()) {
  319. if (engine != "null") {
  320. LOG_ERROR(Input, "Unknown engine name: {}", engine);
  321. }
  322. return std::make_unique<InputDeviceType>();
  323. }
  324. return pair->second->Create(package);
  325. }
  326. } // namespace Common::Input