sdl_impl.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 "common/common_types.h"
  11. #include "common/threadsafe_queue.h"
  12. #include "input_common/sdl/sdl.h"
  13. union SDL_Event;
  14. using SDL_Joystick = struct _SDL_Joystick;
  15. using SDL_JoystickID = s32;
  16. namespace InputCommon::SDL {
  17. class SDLAnalogFactory;
  18. class SDLButtonFactory;
  19. class SDLMotionFactory;
  20. class SDLVibrationFactory;
  21. class SDLJoystick;
  22. class SDLState : public State {
  23. public:
  24. /// Initializes and registers SDL device factories
  25. SDLState();
  26. /// Unregisters SDL device factories and shut them down.
  27. ~SDLState() 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. /// Get all DevicePoller that use the SDL backend for a specific device type
  38. Pollers GetPollers(Polling::DeviceType type) override;
  39. /// Used by the Pollers during config
  40. std::atomic<bool> polling = false;
  41. Common::SPSCQueue<SDL_Event> event_queue;
  42. std::vector<Common::ParamPackage> GetInputDevices() override;
  43. ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) override;
  44. AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override;
  45. private:
  46. void InitJoystick(int joystick_index);
  47. void CloseJoystick(SDL_Joystick* sdl_joystick);
  48. /// Needs to be called before SDL_QuitSubSystem.
  49. void CloseJoysticks();
  50. // Set to true if SDL supports game controller subsystem
  51. bool has_gamecontroller = false;
  52. /// Map of GUID of a list of corresponding virtual Joysticks
  53. std::unordered_map<std::string, std::vector<std::shared_ptr<SDLJoystick>>> joystick_map;
  54. std::mutex joystick_map_mutex;
  55. std::shared_ptr<SDLButtonFactory> button_factory;
  56. std::shared_ptr<SDLAnalogFactory> analog_factory;
  57. std::shared_ptr<SDLVibrationFactory> vibration_factory;
  58. std::shared_ptr<SDLMotionFactory> motion_factory;
  59. bool start_thread = false;
  60. std::atomic<bool> initialized = false;
  61. std::thread poll_thread;
  62. };
  63. } // namespace InputCommon::SDL