sdl_impl.h 2.1 KB

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