main.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Settings::NativeAnalog {
  13. enum Values : int;
  14. }
  15. namespace Settings::NativeButton {
  16. enum Values : int;
  17. }
  18. namespace Settings::NativeMotion {
  19. enum Values : int;
  20. }
  21. namespace MouseInput {
  22. class Mouse;
  23. }
  24. namespace InputCommon {
  25. namespace Polling {
  26. enum class DeviceType { Button, AnalogPreferred, Motion };
  27. /**
  28. * A class that can be used to get inputs from an input device like controllers without having to
  29. * poll the device's status yourself
  30. */
  31. class DevicePoller {
  32. public:
  33. virtual ~DevicePoller() = default;
  34. /// Setup and start polling for inputs, should be called before GetNextInput
  35. /// If a device_id is provided, events should be filtered to only include events from this
  36. /// device id
  37. virtual void Start(const std::string& device_id = "") = 0;
  38. /// Stop polling
  39. virtual void Stop() = 0;
  40. /**
  41. * Every call to this function returns the next input recorded since calling Start
  42. * @return A ParamPackage of the recorded input, which can be used to create an InputDevice.
  43. * If there has been no input, the package is empty
  44. */
  45. virtual Common::ParamPackage GetNextInput() = 0;
  46. };
  47. } // namespace Polling
  48. class GCAnalogFactory;
  49. class GCButtonFactory;
  50. class UDPMotionFactory;
  51. class UDPTouchFactory;
  52. class MouseButtonFactory;
  53. class MouseAnalogFactory;
  54. class MouseMotionFactory;
  55. class MouseTouchFactory;
  56. class Keyboard;
  57. /**
  58. * Given a ParamPackage for a Device returned from `GetInputDevices`, attempt to get the default
  59. * mapping for the device. This is currently only implemented for the SDL backend devices.
  60. */
  61. using AnalogMapping = std::unordered_map<Settings::NativeAnalog::Values, Common::ParamPackage>;
  62. using ButtonMapping = std::unordered_map<Settings::NativeButton::Values, Common::ParamPackage>;
  63. using MotionMapping = std::unordered_map<Settings::NativeMotion::Values, Common::ParamPackage>;
  64. class InputSubsystem {
  65. public:
  66. explicit InputSubsystem();
  67. ~InputSubsystem();
  68. InputSubsystem(const InputSubsystem&) = delete;
  69. InputSubsystem& operator=(const InputSubsystem&) = delete;
  70. InputSubsystem(InputSubsystem&&) = delete;
  71. InputSubsystem& operator=(InputSubsystem&&) = delete;
  72. /// Initializes and registers all built-in input device factories.
  73. void Initialize();
  74. /// Unregisters all built-in input device factories and shuts them down.
  75. void Shutdown();
  76. /// Retrieves the underlying keyboard device.
  77. [[nodiscard]] Keyboard* GetKeyboard();
  78. /// Retrieves the underlying keyboard device.
  79. [[nodiscard]] const Keyboard* GetKeyboard() const;
  80. /// Retrieves the underlying mouse device.
  81. [[nodiscard]] MouseInput::Mouse* GetMouse();
  82. /// Retrieves the underlying mouse device.
  83. [[nodiscard]] const MouseInput::Mouse* GetMouse() const;
  84. /**
  85. * Returns all available input devices that this Factory can create a new device with.
  86. * Each returned ParamPackage should have a `display` field used for display, a class field for
  87. * backends to determine if this backend is meant to service the request and any other
  88. * information needed to identify this in the backend later.
  89. */
  90. [[nodiscard]] std::vector<Common::ParamPackage> GetInputDevices() const;
  91. /// Retrieves the analog mappings for the given device.
  92. [[nodiscard]] AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& device) const;
  93. /// Retrieves the button mappings for the given device.
  94. [[nodiscard]] ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& device) const;
  95. /// Retrieves the motion mappings for the given device.
  96. [[nodiscard]] MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& device) const;
  97. /// Retrieves the underlying GameCube analog handler.
  98. [[nodiscard]] GCAnalogFactory* GetGCAnalogs();
  99. /// Retrieves the underlying GameCube analog handler.
  100. [[nodiscard]] const GCAnalogFactory* GetGCAnalogs() const;
  101. /// Retrieves the underlying GameCube button handler.
  102. [[nodiscard]] GCButtonFactory* GetGCButtons();
  103. /// Retrieves the underlying GameCube button handler.
  104. [[nodiscard]] const GCButtonFactory* GetGCButtons() const;
  105. /// Retrieves the underlying udp motion handler.
  106. [[nodiscard]] UDPMotionFactory* GetUDPMotions();
  107. /// Retrieves the underlying udp motion handler.
  108. [[nodiscard]] const UDPMotionFactory* GetUDPMotions() const;
  109. /// Retrieves the underlying udp touch handler.
  110. [[nodiscard]] UDPTouchFactory* GetUDPTouch();
  111. /// Retrieves the underlying udp touch handler.
  112. [[nodiscard]] const UDPTouchFactory* GetUDPTouch() const;
  113. /// Retrieves the underlying GameCube button handler.
  114. [[nodiscard]] MouseButtonFactory* GetMouseButtons();
  115. /// Retrieves the underlying GameCube button handler.
  116. [[nodiscard]] const MouseButtonFactory* GetMouseButtons() const;
  117. /// Retrieves the underlying udp touch handler.
  118. [[nodiscard]] MouseAnalogFactory* GetMouseAnalogs();
  119. /// Retrieves the underlying udp touch handler.
  120. [[nodiscard]] const MouseAnalogFactory* GetMouseAnalogs() const;
  121. /// Retrieves the underlying udp motion handler.
  122. [[nodiscard]] MouseMotionFactory* GetMouseMotions();
  123. /// Retrieves the underlying udp motion handler.
  124. [[nodiscard]] const MouseMotionFactory* GetMouseMotions() const;
  125. /// Retrieves the underlying udp touch handler.
  126. [[nodiscard]] MouseTouchFactory* GetMouseTouch();
  127. /// Retrieves the underlying udp touch handler.
  128. [[nodiscard]] const MouseTouchFactory* GetMouseTouch() const;
  129. /// Reloads the input devices
  130. void ReloadInputDevices();
  131. /// Get all DevicePoller from all backends for a specific device type
  132. [[nodiscard]] std::vector<std::unique_ptr<Polling::DevicePoller>> GetPollers(
  133. Polling::DeviceType type) const;
  134. private:
  135. struct Impl;
  136. std::unique_ptr<Impl> impl;
  137. };
  138. /// Generates a serialized param package for creating a keyboard button device
  139. std::string GenerateKeyboardParam(int key_code);
  140. /// Generates a serialized param package for creating an analog device taking input from keyboard
  141. std::string GenerateAnalogParamFromKeys(int key_up, int key_down, int key_left, int key_right,
  142. int key_modifier, float modifier_scale);
  143. } // namespace InputCommon