main.h 4.6 KB

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