main.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <memory>
  6. #include <string>
  7. #include <unordered_map>
  8. #include <vector>
  9. namespace Common {
  10. class ParamPackage;
  11. }
  12. namespace Common::Input {
  13. enum class ButtonNames;
  14. }
  15. namespace Settings::NativeAnalog {
  16. enum Values : int;
  17. }
  18. namespace Settings::NativeButton {
  19. enum Values : int;
  20. }
  21. namespace Settings::NativeMotion {
  22. enum Values : int;
  23. }
  24. namespace InputCommon {
  25. class Camera;
  26. class Keyboard;
  27. class Mouse;
  28. class TouchScreen;
  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. /**
  79. * Returns all available input devices that this Factory can create a new device with.
  80. * Each returned ParamPackage should have a `display` field used for display, a `engine` field
  81. * for backends to determine if this backend is meant to service the request and any other
  82. * information needed to identify this in the backend later.
  83. */
  84. [[nodiscard]] std::vector<Common::ParamPackage> GetInputDevices() const;
  85. /// Retrieves the analog mappings for the given device.
  86. [[nodiscard]] AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& device) const;
  87. /// Retrieves the button mappings for the given device.
  88. [[nodiscard]] ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& device) const;
  89. /// Retrieves the motion mappings for the given device.
  90. [[nodiscard]] MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& device) const;
  91. /// Returns an enum contaning the name to be displayed from the input engine.
  92. [[nodiscard]] Common::Input::ButtonNames GetButtonName(
  93. const Common::ParamPackage& params) const;
  94. /// Returns true if device is a controller.
  95. [[nodiscard]] bool IsController(const Common::ParamPackage& params) const;
  96. /// Returns true if axis of a stick aren't mapped in the correct direction
  97. [[nodiscard]] bool IsStickInverted(const Common::ParamPackage& device) const;
  98. /// Reloads the input devices.
  99. void ReloadInputDevices();
  100. /// Start polling from all backends for a desired input type.
  101. void BeginMapping(Polling::InputType type);
  102. /// Returns an input event with mapping information.
  103. [[nodiscard]] Common::ParamPackage GetNextInput() const;
  104. /// Stop polling from all backends.
  105. void StopMapping() const;
  106. private:
  107. struct Impl;
  108. std::unique_ptr<Impl> impl;
  109. };
  110. /// Generates a serialized param package for creating a keyboard button device.
  111. std::string GenerateKeyboardParam(int key_code);
  112. /// Generates a serialized param package for creating an analog device taking input from keyboard.
  113. std::string GenerateAnalogParamFromKeys(int key_up, int key_down, int key_left, int key_right,
  114. int key_modifier, float modifier_scale);
  115. } // namespace InputCommon