input.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. };
  92. // Analog properties for calibration
  93. struct AnalogProperties {
  94. // Anything below this value will be detected as zero
  95. float deadzone{};
  96. // Anyting above this values will be detected as one
  97. float range{1.0f};
  98. // Minimum value to be detected as active
  99. float threshold{0.5f};
  100. // Drift correction applied to the raw data
  101. float offset{};
  102. // Invert direction of the sensor data
  103. bool inverted{};
  104. // Press once to activate, press again to release
  105. bool toggle{};
  106. };
  107. // Single analog sensor data
  108. struct AnalogStatus {
  109. float value{};
  110. float raw_value{};
  111. AnalogProperties properties{};
  112. };
  113. // Button data
  114. struct ButtonStatus {
  115. Common::UUID uuid{};
  116. bool value{};
  117. // Invert value of the button
  118. bool inverted{};
  119. // Press once to activate, press again to release
  120. bool toggle{};
  121. // Internal lock for the toggle status
  122. bool locked{};
  123. };
  124. // Internal battery data
  125. using BatteryStatus = BatteryLevel;
  126. // Analog and digital joystick data
  127. struct StickStatus {
  128. Common::UUID uuid{};
  129. AnalogStatus x{};
  130. AnalogStatus y{};
  131. bool left{};
  132. bool right{};
  133. bool up{};
  134. bool down{};
  135. };
  136. // Analog and digital trigger data
  137. struct TriggerStatus {
  138. Common::UUID uuid{};
  139. AnalogStatus analog{};
  140. ButtonStatus pressed{};
  141. };
  142. // 3D vector representing motion input
  143. struct MotionSensor {
  144. AnalogStatus x{};
  145. AnalogStatus y{};
  146. AnalogStatus z{};
  147. };
  148. // Motion data used to calculate controller orientation
  149. struct MotionStatus {
  150. // Gyroscope vector measurement in radians/s.
  151. MotionSensor gyro{};
  152. // Acceleration vector measurement in G force
  153. MotionSensor accel{};
  154. // Time since last measurement in microseconds
  155. u64 delta_timestamp{};
  156. // Request to update after reading the value
  157. bool force_update{};
  158. };
  159. // Data of a single point on a touch screen
  160. struct TouchStatus {
  161. ButtonStatus pressed{};
  162. AnalogStatus x{};
  163. AnalogStatus y{};
  164. int id{};
  165. };
  166. // Physical controller color in RGB format
  167. struct BodyColorStatus {
  168. u32 body{};
  169. u32 buttons{};
  170. };
  171. // HD rumble data
  172. struct VibrationStatus {
  173. f32 low_amplitude{};
  174. f32 low_frequency{};
  175. f32 high_amplitude{};
  176. f32 high_frequency{};
  177. VibrationAmplificationType type;
  178. };
  179. // Physical controller LED pattern
  180. struct LedStatus {
  181. bool led_1{};
  182. bool led_2{};
  183. bool led_3{};
  184. bool led_4{};
  185. };
  186. // Raw data fom camera
  187. struct CameraStatus {
  188. CameraFormat format{CameraFormat::None};
  189. std::vector<u8> data{};
  190. };
  191. struct NfcStatus {
  192. NfcState state{};
  193. std::vector<u8> data{};
  194. };
  195. // List of buttons to be passed to Qt that can be translated
  196. enum class ButtonNames {
  197. Undefined,
  198. Invalid,
  199. // This will display the engine name instead of the button name
  200. Engine,
  201. // This will display the button by value instead of the button name
  202. Value,
  203. ButtonLeft,
  204. ButtonRight,
  205. ButtonDown,
  206. ButtonUp,
  207. TriggerZ,
  208. TriggerR,
  209. TriggerL,
  210. ButtonA,
  211. ButtonB,
  212. ButtonX,
  213. ButtonY,
  214. ButtonStart,
  215. // DS4 button names
  216. L1,
  217. L2,
  218. L3,
  219. R1,
  220. R2,
  221. R3,
  222. Circle,
  223. Cross,
  224. Square,
  225. Triangle,
  226. Share,
  227. Options,
  228. Home,
  229. Touch,
  230. // Mouse buttons
  231. ButtonMouseWheel,
  232. ButtonBackward,
  233. ButtonForward,
  234. ButtonTask,
  235. ButtonExtra,
  236. };
  237. // Callback data consisting of an input type and the equivalent data status
  238. struct CallbackStatus {
  239. InputType type{InputType::None};
  240. ButtonStatus button_status{};
  241. StickStatus stick_status{};
  242. AnalogStatus analog_status{};
  243. TriggerStatus trigger_status{};
  244. MotionStatus motion_status{};
  245. TouchStatus touch_status{};
  246. BodyColorStatus color_status{};
  247. BatteryStatus battery_status{};
  248. VibrationStatus vibration_status{};
  249. CameraFormat camera_status{CameraFormat::None};
  250. NfcState nfc_status{NfcState::Unknown};
  251. std::vector<u8> raw_data{};
  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 bool IsVibrationEnabled() {
  287. return false;
  288. }
  289. virtual PollingError SetPollingMode([[maybe_unused]] PollingMode polling_mode) {
  290. return PollingError::NotSupported;
  291. }
  292. virtual CameraError SetCameraFormat([[maybe_unused]] CameraFormat camera_format) {
  293. return CameraError::NotSupported;
  294. }
  295. virtual NfcState SupportsNfc() const {
  296. return NfcState::NotSupported;
  297. }
  298. virtual NfcState WriteNfcData([[maybe_unused]] const std::vector<u8>& data) {
  299. return NfcState::NotSupported;
  300. }
  301. };
  302. /// An abstract class template for a factory that can create input devices.
  303. template <typename InputDeviceType>
  304. class Factory {
  305. public:
  306. virtual ~Factory() = default;
  307. virtual std::unique_ptr<InputDeviceType> Create(const Common::ParamPackage&) = 0;
  308. };
  309. namespace Impl {
  310. template <typename InputDeviceType>
  311. using FactoryListType = std::unordered_map<std::string, std::shared_ptr<Factory<InputDeviceType>>>;
  312. template <typename InputDeviceType>
  313. struct FactoryList {
  314. static FactoryListType<InputDeviceType> list;
  315. };
  316. template <typename InputDeviceType>
  317. FactoryListType<InputDeviceType> FactoryList<InputDeviceType>::list;
  318. } // namespace Impl
  319. /**
  320. * Registers an input device factory.
  321. * @tparam InputDeviceType the type of input devices the factory can create
  322. * @param name the name of the factory. Will be used to match the "engine" parameter when creating
  323. * a device
  324. * @param factory the factory object to register
  325. */
  326. template <typename InputDeviceType>
  327. void RegisterFactory(const std::string& name, std::shared_ptr<Factory<InputDeviceType>> factory) {
  328. auto pair = std::make_pair(name, std::move(factory));
  329. if (!Impl::FactoryList<InputDeviceType>::list.insert(std::move(pair)).second) {
  330. LOG_ERROR(Input, "Factory '{}' already registered", name);
  331. }
  332. }
  333. inline void RegisterInputFactory(const std::string& name,
  334. std::shared_ptr<Factory<InputDevice>> factory) {
  335. RegisterFactory<InputDevice>(name, std::move(factory));
  336. }
  337. inline void RegisterOutputFactory(const std::string& name,
  338. std::shared_ptr<Factory<OutputDevice>> factory) {
  339. RegisterFactory<OutputDevice>(name, std::move(factory));
  340. }
  341. /**
  342. * Unregisters an input device factory.
  343. * @tparam InputDeviceType the type of input devices the factory can create
  344. * @param name the name of the factory to unregister
  345. */
  346. template <typename InputDeviceType>
  347. void UnregisterFactory(const std::string& name) {
  348. if (Impl::FactoryList<InputDeviceType>::list.erase(name) == 0) {
  349. LOG_ERROR(Input, "Factory '{}' not registered", name);
  350. }
  351. }
  352. inline void UnregisterInputFactory(const std::string& name) {
  353. UnregisterFactory<InputDevice>(name);
  354. }
  355. inline void UnregisterOutputFactory(const std::string& name) {
  356. UnregisterFactory<OutputDevice>(name);
  357. }
  358. /**
  359. * Create an input device from given paramters.
  360. * @tparam InputDeviceType the type of input devices to create
  361. * @param params a serialized ParamPackage string that contains all parameters for creating the
  362. * device
  363. */
  364. template <typename InputDeviceType>
  365. std::unique_ptr<InputDeviceType> CreateDeviceFromString(const std::string& params) {
  366. const Common::ParamPackage package(params);
  367. const std::string engine = package.Get("engine", "null");
  368. const auto& factory_list = Impl::FactoryList<InputDeviceType>::list;
  369. const auto pair = factory_list.find(engine);
  370. if (pair == factory_list.end()) {
  371. if (engine != "null") {
  372. LOG_ERROR(Input, "Unknown engine name: {}", engine);
  373. }
  374. return std::make_unique<InputDeviceType>();
  375. }
  376. return pair->second->Create(package);
  377. }
  378. inline std::unique_ptr<InputDevice> CreateInputDeviceFromString(const std::string& params) {
  379. return CreateDeviceFromString<InputDevice>(params);
  380. }
  381. inline std::unique_ptr<OutputDevice> CreateOutputDeviceFromString(const std::string& params) {
  382. return CreateDeviceFromString<OutputDevice>(params);
  383. }
  384. /**
  385. * Create an input device from given parameters.
  386. * @tparam InputDeviceType the type of input devices to create
  387. * @param package A ParamPackage that contains all parameters for creating the device
  388. */
  389. template <typename InputDeviceType>
  390. std::unique_ptr<InputDeviceType> CreateDevice(const ParamPackage& package) {
  391. const std::string engine = package.Get("engine", "null");
  392. const auto& factory_list = Impl::FactoryList<InputDeviceType>::list;
  393. const auto pair = factory_list.find(engine);
  394. if (pair == factory_list.end()) {
  395. if (engine != "null") {
  396. LOG_ERROR(Input, "Unknown engine name: {}", engine);
  397. }
  398. return std::make_unique<InputDeviceType>();
  399. }
  400. return pair->second->Create(package);
  401. }
  402. inline std::unique_ptr<InputDevice> CreateInputDevice(const ParamPackage& package) {
  403. return CreateDevice<InputDevice>(package);
  404. }
  405. inline std::unique_ptr<OutputDevice> CreateOutputDevice(const ParamPackage& package) {
  406. return CreateDevice<OutputDevice>(package);
  407. }
  408. } // namespace Common::Input