sdl_impl.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 SDLJoystick;
  21. class SDLState : public State {
  22. public:
  23. /// Initializes and registers SDL device factories
  24. SDLState();
  25. /// Unregisters SDL device factories and shut them down.
  26. ~SDLState() override;
  27. /// Handle SDL_Events for joysticks from SDL_PollEvent
  28. void HandleGameControllerEvent(const SDL_Event& event);
  29. /// Get the nth joystick with the corresponding GUID
  30. std::shared_ptr<SDLJoystick> GetSDLJoystickBySDLID(SDL_JoystickID sdl_id);
  31. /**
  32. * Check how many identical joysticks (by guid) were connected before the one with sdl_id and so
  33. * tie it to a SDLJoystick with the same guid and that port
  34. */
  35. std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port);
  36. /// Get all DevicePoller that use the SDL backend for a specific device type
  37. Pollers GetPollers(Polling::DeviceType type) override;
  38. /// Used by the Pollers during config
  39. std::atomic<bool> polling = false;
  40. Common::SPSCQueue<SDL_Event> event_queue;
  41. std::vector<Common::ParamPackage> GetInputDevices() override;
  42. ButtonMapping GetButtonMappingForDevice(const Common::ParamPackage& params) override;
  43. AnalogMapping GetAnalogMappingForDevice(const Common::ParamPackage& params) override;
  44. private:
  45. void InitJoystick(int joystick_index);
  46. void CloseJoystick(SDL_Joystick* sdl_joystick);
  47. /// Needs to be called before SDL_QuitSubSystem.
  48. void CloseJoysticks();
  49. // Set to true if SDL supports game controller subsystem
  50. bool has_gamecontroller = false;
  51. /// Map of GUID of a list of corresponding virtual Joysticks
  52. std::unordered_map<std::string, std::vector<std::shared_ptr<SDLJoystick>>> joystick_map;
  53. std::mutex joystick_map_mutex;
  54. std::shared_ptr<SDLButtonFactory> button_factory;
  55. std::shared_ptr<SDLAnalogFactory> analog_factory;
  56. std::shared_ptr<SDLMotionFactory> motion_factory;
  57. bool start_thread = false;
  58. std::atomic<bool> initialized = false;
  59. std::thread poll_thread;
  60. };
  61. } // namespace InputCommon::SDL