main.h 5.5 KB

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