main.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // SPDX-FileCopyrightText: 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <memory>
  5. #include <string>
  6. #include <unordered_map>
  7. #include <vector>
  8. namespace Common {
  9. class ParamPackage;
  10. }
  11. namespace Common::Input {
  12. enum class ButtonNames;
  13. }
  14. namespace Settings::NativeAnalog {
  15. enum Values : int;
  16. }
  17. namespace Settings::NativeButton {
  18. enum Values : int;
  19. }
  20. namespace Settings::NativeMotion {
  21. enum Values : int;
  22. }
  23. namespace InputCommon {
  24. class Android;
  25. class Camera;
  26. class Keyboard;
  27. class Mouse;
  28. class TouchScreen;
  29. class VirtualAmiibo;
  30. class VirtualGamepad;
  31. struct MappingData;
  32. } // namespace InputCommon
  33. namespace InputCommon::TasInput {
  34. class Tas;
  35. } // namespace InputCommon::TasInput
  36. namespace InputCommon {
  37. namespace Polling {
  38. /// Type of input desired for mapping purposes
  39. enum class InputType { None, Button, Stick, Motion, Touch };
  40. } // namespace Polling
  41. /**
  42. * Given a ParamPackage for a Device returned from `GetInputDevices`, attempt to get the default
  43. * mapping for the device.
  44. */
  45. using AnalogMapping = std::unordered_map<Settings::NativeAnalog::Values, Common::ParamPackage>;
  46. using ButtonMapping = std::unordered_map<Settings::NativeButton::Values, Common::ParamPackage>;
  47. using MotionMapping = std::unordered_map<Settings::NativeMotion::Values, Common::ParamPackage>;
  48. class InputSubsystem {
  49. public:
  50. explicit InputSubsystem();
  51. ~InputSubsystem();
  52. InputSubsystem(const InputSubsystem&) = delete;
  53. InputSubsystem& operator=(const InputSubsystem&) = delete;
  54. InputSubsystem(InputSubsystem&&) = delete;
  55. InputSubsystem& operator=(InputSubsystem&&) = delete;
  56. /// Initializes and registers all built-in input device factories.
  57. void Initialize();
  58. /// Unregisters all built-in input device factories and shuts them down.
  59. void Shutdown();
  60. /// Retrieves the underlying keyboard device.
  61. [[nodiscard]] Keyboard* GetKeyboard();
  62. /// Retrieves the underlying keyboard device.
  63. [[nodiscard]] const Keyboard* GetKeyboard() const;
  64. /// Retrieves the underlying mouse device.
  65. [[nodiscard]] Mouse* GetMouse();
  66. /// Retrieves the underlying mouse device.
  67. [[nodiscard]] const Mouse* GetMouse() const;
  68. /// Retrieves the underlying touch screen device.
  69. [[nodiscard]] TouchScreen* GetTouchScreen();
  70. /// Retrieves the underlying touch screen device.
  71. [[nodiscard]] const TouchScreen* GetTouchScreen() const;
  72. /// Retrieves the underlying tas input device.
  73. [[nodiscard]] TasInput::Tas* GetTas();
  74. /// Retrieves the underlying tas input device.
  75. [[nodiscard]] const TasInput::Tas* GetTas() const;
  76. /// Retrieves the underlying camera input device.
  77. [[nodiscard]] Camera* GetCamera();
  78. /// Retrieves the underlying camera input device.
  79. [[nodiscard]] const Camera* GetCamera() const;
  80. /// Retrieves the underlying android input device.
  81. [[nodiscard]] Android* GetAndroid();
  82. /// Retrieves the underlying android input device.
  83. [[nodiscard]] const Android* GetAndroid() const;
  84. /// Retrieves the underlying virtual amiibo input device.
  85. [[nodiscard]] VirtualAmiibo* GetVirtualAmiibo();
  86. /// Retrieves the underlying virtual amiibo input device.
  87. [[nodiscard]] const VirtualAmiibo* GetVirtualAmiibo() const;
  88. /// Retrieves the underlying virtual gamepad input device.
  89. [[nodiscard]] VirtualGamepad* GetVirtualGamepad();
  90. /// Retrieves the underlying virtual gamepad input device.
  91. [[nodiscard]] const VirtualGamepad* GetVirtualGamepad() const;
  92. /**
  93. * Returns all available input devices that this Factory can create a new device with.
  94. * Each returned ParamPackage should have a `display` field used for display, a `engine` field
  95. * for backends to determine if this backend is meant to service the request and any other
  96. * information needed to identify this in the backend later.
  97. */
  98. [[nodiscard]] std::vector<Common::ParamPackage> GetInputDevices() const;
  99. /// Retrieves the analog mappings for the given device.
  100. [[nodiscard]] AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& device) const;
  101. /// Retrieves the button mappings for the given device.
  102. [[nodiscard]] ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& device) const;
  103. /// Retrieves the motion mappings for the given device.
  104. [[nodiscard]] MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& device) const;
  105. /// Returns an enum containing the name to be displayed from the input engine.
  106. [[nodiscard]] Common::Input::ButtonNames GetButtonName(
  107. const Common::ParamPackage& params) const;
  108. /// Returns true if device is a controller.
  109. [[nodiscard]] bool IsController(const Common::ParamPackage& params) const;
  110. /// Returns true if axis of a stick aren't mapped in the correct direction
  111. [[nodiscard]] bool IsStickInverted(const Common::ParamPackage& device) const;
  112. /// Reloads the input devices.
  113. void ReloadInputDevices();
  114. /// Start polling from all backends for a desired input type.
  115. void BeginMapping(Polling::InputType type);
  116. /// Returns an input event with mapping information.
  117. [[nodiscard]] Common::ParamPackage GetNextInput() const;
  118. /// Stop polling from all backends.
  119. void StopMapping() const;
  120. /// Signals SDL driver for new input events
  121. void PumpEvents() const;
  122. private:
  123. struct Impl;
  124. std::unique_ptr<Impl> impl;
  125. };
  126. /// Generates a serialized param package for creating a keyboard button device.
  127. std::string GenerateKeyboardParam(int key_code);
  128. /// Generates a serialized param package for creating an analog device taking input from keyboard.
  129. std::string GenerateAnalogParamFromKeys(int key_up, int key_down, int key_left, int key_right,
  130. int key_modifier, float modifier_scale);
  131. } // namespace InputCommon