sdl_impl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <memory>
  7. #include <mutex>
  8. #include <thread>
  9. #include <unordered_map>
  10. #include <SDL.h>
  11. #include "common/common_types.h"
  12. #include "common/threadsafe_queue.h"
  13. #include "input_common/sdl/sdl.h"
  14. union SDL_Event;
  15. using SDL_GameController = struct _SDL_GameController;
  16. using SDL_Joystick = struct _SDL_Joystick;
  17. using SDL_JoystickID = s32;
  18. using ButtonBindings =
  19. std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerButton>, 17>;
  20. using ZButtonBindings =
  21. std::array<std::pair<Settings::NativeButton::Values, SDL_GameControllerAxis>, 2>;
  22. namespace InputCommon::SDL {
  23. class SDLAnalogFactory;
  24. class SDLButtonFactory;
  25. class SDLMotionFactory;
  26. class SDLVibrationFactory;
  27. class SDLJoystick;
  28. class SDLState : public State {
  29. public:
  30. /// Initializes and registers SDL device factories
  31. SDLState();
  32. /// Unregisters SDL device factories and shut them down.
  33. ~SDLState() override;
  34. /// Handle SDL_Events for joysticks from SDL_PollEvent
  35. void HandleGameControllerEvent(const SDL_Event& event);
  36. /// Get the nth joystick with the corresponding GUID
  37. std::shared_ptr<SDLJoystick> GetSDLJoystickBySDLID(SDL_JoystickID sdl_id);
  38. /**
  39. * Check how many identical joysticks (by guid) were connected before the one with sdl_id and so
  40. * tie it to a SDLJoystick with the same guid and that port
  41. */
  42. std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port);
  43. /// Get all DevicePoller that use the SDL backend for a specific device type
  44. Pollers GetPollers(Polling::DeviceType type) override;
  45. /// Used by the Pollers during config
  46. std::atomic<bool> polling = false;
  47. Common::SPSCQueue<SDL_Event> event_queue;
  48. std::vector<Common::ParamPackage> GetInputDevices() override;
  49. ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) override;
  50. AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override;
  51. MotionMapping GetMotionMappingForDevice(const Common::ParamPackage& params) override;
  52. private:
  53. void InitJoystick(int joystick_index);
  54. void CloseJoystick(SDL_Joystick* sdl_joystick);
  55. /// Needs to be called before SDL_QuitSubSystem.
  56. void CloseJoysticks();
  57. /// Returns the default button bindings list for generic controllers
  58. ButtonBindings GetDefaultButtonBinding() const;
  59. /// Returns the default button bindings list for nintendo controllers
  60. ButtonBindings GetNintendoButtonBinding(const std::shared_ptr<SDLJoystick>& joystick) const;
  61. /// Returns the button mappings from a single controller
  62. ButtonMapping GetSingleControllerMapping(const std::shared_ptr<SDLJoystick>& joystick,
  63. const ButtonBindings& switch_to_sdl_button,
  64. const ZButtonBindings& switch_to_sdl_axis) const;
  65. /// Returns the button mappings from two different controllers
  66. ButtonMapping GetDualControllerMapping(const std::shared_ptr<SDLJoystick>& joystick,
  67. const std::shared_ptr<SDLJoystick>& joystick2,
  68. const ButtonBindings& switch_to_sdl_button,
  69. const ZButtonBindings& switch_to_sdl_axis) const;
  70. /// Returns true if the button is on the left joycon
  71. bool IsButtonOnLeftSide(Settings::NativeButton::Values button) const;
  72. // Set to true if SDL supports game controller subsystem
  73. bool has_gamecontroller = false;
  74. /// Map of GUID of a list of corresponding virtual Joysticks
  75. std::unordered_map<std::string, std::vector<std::shared_ptr<SDLJoystick>>> joystick_map;
  76. std::mutex joystick_map_mutex;
  77. std::shared_ptr<SDLButtonFactory> button_factory;
  78. std::shared_ptr<SDLAnalogFactory> analog_factory;
  79. std::shared_ptr<SDLVibrationFactory> vibration_factory;
  80. std::shared_ptr<SDLMotionFactory> motion_factory;
  81. bool start_thread = false;
  82. std::atomic<bool> initialized = false;
  83. std::thread poll_thread;
  84. };
  85. } // namespace InputCommon::SDL