main.h 4.6 KB

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