input.h 10 KB

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