main.h 7.1 KB

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