input.h 9.7 KB

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