sdl_driver.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2018 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <mutex>
  7. #include <thread>
  8. #include <unordered_map>
  9. #include <SDL.h>
  10. #include "common/common_types.h"
  11. #include "input_common/input_engine.h"
  12. union SDL_Event;
  13. using SDL_GameController = struct _SDL_GameController;
  14. using SDL_Joystick = struct _SDL_Joystick;
  15. using SDL_JoystickID = s32;
  16. using ButtonBindings =
  17. std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerButton>, 17>;
  18. using ZButtonBindings =
  19. std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerAxis>, 2>;
  20. namespace InputCommon {
  21. class SDLJoystick;
  22. class SDLDriver : public InputCommon::InputEngine {
  23. public:
  24. /// Initializes and registers SDL device factories
  25. SDLDriver(const std::string& input_engine_);
  26. /// Unregisters SDL device factories and shut them down.
  27. ~SDLDriver() override;
  28. /// Handle SDL_Events for joysticks from SDL_PollEvent
  29. void HandleGameControllerEvent(const SDL_Event& event);
  30. /// Get the nth joystick with the corresponding GUID
  31. std::shared_ptr<SDLJoystick> GetSDLJoystickBySDLID(SDL_JoystickID sdl_id);
  32. /**
  33. * Check how many identical joysticks (by guid) were connected before the one with sdl_id and so
  34. * tie it to a SDLJoystick with the same guid and that port
  35. */
  36. std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port);
  37. std::vector<Common::ParamPackage> GetInputDevices() const override;
  38. ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) override;
  39. AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override;
  40. MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& params) override;
  41. Common::Input::ButtonNames GetUIName(const Common::ParamPackage& params) const override;
  42. std::string GetHatButtonName(u8 direction_value) const override;
  43. u8 GetHatButtonId(const std::string& direction_name) const override;
  44. Common::Input::VibrationError SetRumble(
  45. const PadIdentifier& identifier, const Common::Input::VibrationStatus vibration) override;
  46. private:
  47. void InitJoystick(int joystick_index);
  48. void CloseJoystick(SDL_Joystick* sdl_joystick);
  49. /// Needs to be called before SDL_QuitSubSystem.
  50. void CloseJoysticks();
  51. Common::ParamPackage BuildAnalogParamPackageForButton(int port, std::string guid, s32 axis,
  52. float value = 0.1f) const;
  53. Common::ParamPackage BuildButtonParamPackageForButton(int port, std::string guid,
  54. s32 button) const;
  55. Common::ParamPackage BuildHatParamPackageForButton(int port, std::string guid, s32 hat,
  56. u8 value) const;
  57. Common::ParamPackage BuildMotionParam(int port, std::string guid) const;
  58. Common::ParamPackage BuildParamPackageForBinding(
  59. int port, const std::string& guid, const SDL_GameControllerButtonBind& binding) const;
  60. Common::ParamPackage BuildParamPackageForAnalog(PadIdentifier identifier, int axis_x,
  61. int axis_y, float offset_x,
  62. float offset_y) const;
  63. /// Returns the default button bindings list for generic controllers
  64. ButtonBindings GetDefaultButtonBinding() const;
  65. /// Returns the default button bindings list for nintendo controllers
  66. ButtonBindings GetNintendoButtonBinding(const std::shared_ptr<SDLJoystick>& joystick) const;
  67. /// Returns the button mappings from a single controller
  68. ButtonMapping GetSingleControllerMapping(const std::shared_ptr<SDLJoystick>& joystick,
  69. const ButtonBindings& switch_to_sdl_button,
  70. const ZButtonBindings& switch_to_sdl_axis) const;
  71. /// Returns the button mappings from two different controllers
  72. ButtonMapping GetDualControllerMapping(const std::shared_ptr<SDLJoystick>& joystick,
  73. const std::shared_ptr<SDLJoystick>& joystick2,
  74. const ButtonBindings& switch_to_sdl_button,
  75. const ZButtonBindings& switch_to_sdl_axis) const;
  76. /// Returns true if the button is on the left joycon
  77. bool IsButtonOnLeftSide(Settings::NativeButton::Values button) const;
  78. /// Map of GUID of a list of corresponding virtual Joysticks
  79. std::unordered_map<std::string, std::vector<std::shared_ptr<SDLJoystick>>> joystick_map;
  80. std::mutex joystick_map_mutex;
  81. bool start_thread = false;
  82. std::atomic<bool> initialized = false;
  83. std::thread poll_thread;
  84. };
  85. } // namespace InputCommon