main.h 5.3 KB

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