diff options
| author | 2018-09-20 00:28:05 -0600 | |
|---|---|---|
| committer | 2019-03-02 19:09:34 +0100 | |
| commit | 09ac66388c01187ed6a402efcad76984c8af9a2e (patch) | |
| tree | beed8a86e7b7e02e0c5526723bbf812920341d12 /src/input_common/sdl/sdl_impl.h | |
| parent | Input: Copy current SDL.h/cpp files to impl (diff) | |
| download | yuzu-09ac66388c01187ed6a402efcad76984c8af9a2e.tar.gz yuzu-09ac66388c01187ed6a402efcad76984c8af9a2e.tar.xz yuzu-09ac66388c01187ed6a402efcad76984c8af9a2e.zip | |
Input: Remove global variables from SDL Input
Changes the interface as well to remove any unique methods that
frontends needed to call such as StartJoystickEventHandler by
conditionally starting the polling thread only if the frontend hasn't
started it already. Additionally, moves all global state into a single
SDLState class in order to guarantee that the destructors are called in
the proper order
Diffstat (limited to 'src/input_common/sdl/sdl_impl.h')
| -rw-r--r-- | src/input_common/sdl/sdl_impl.h | 79 |
1 files changed, 46 insertions, 33 deletions
diff --git a/src/input_common/sdl/sdl_impl.h b/src/input_common/sdl/sdl_impl.h index c152fa747..fec82fbe6 100644 --- a/src/input_common/sdl/sdl_impl.h +++ b/src/input_common/sdl/sdl_impl.h | |||
| @@ -1,51 +1,64 @@ | |||
| 1 | // Copyright 2017 Citra Emulator Project | 1 | // Copyright 2018 Citra Emulator Project |
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <atomic> | ||
| 7 | #include <memory> | 8 | #include <memory> |
| 8 | #include <vector> | 9 | #include <thread> |
| 9 | #include "core/frontend/input.h" | 10 | #include "common/threadsafe_queue.h" |
| 11 | #include "input_common/sdl/sdl.h" | ||
| 10 | 12 | ||
| 11 | union SDL_Event; | 13 | union SDL_Event; |
| 12 | namespace Common { | 14 | using SDL_Joystick = struct _SDL_Joystick; |
| 13 | class ParamPackage; | 15 | using SDL_JoystickID = s32; |
| 14 | } | ||
| 15 | namespace InputCommon { | ||
| 16 | namespace Polling { | ||
| 17 | class DevicePoller; | ||
| 18 | enum class DeviceType; | ||
| 19 | } // namespace Polling | ||
| 20 | } // namespace InputCommon | ||
| 21 | 16 | ||
| 22 | namespace InputCommon { | 17 | namespace InputCommon::SDL { |
| 23 | namespace SDL { | ||
| 24 | 18 | ||
| 25 | /// Initializes and registers SDL device factories | 19 | class SDLJoystick; |
| 26 | void Init(); | 20 | class SDLButtonFactory; |
| 21 | class SDLAnalogFactory; | ||
| 27 | 22 | ||
| 28 | /// Unresisters SDL device factories and shut them down. | 23 | class SDLState : public State { |
| 29 | void Shutdown(); | 24 | public: |
| 25 | /// Initializes and registers SDL device factories | ||
| 26 | SDLState(); | ||
| 30 | 27 | ||
| 31 | /// Needs to be called before SDL_QuitSubSystem. | 28 | /// Unresisters SDL device factories and shut them down. |
| 32 | void CloseSDLJoysticks(); | 29 | ~SDLState() override; |
| 33 | 30 | ||
| 34 | /// Handle SDL_Events for joysticks from SDL_PollEvent | 31 | /// Handle SDL_Events for joysticks from SDL_PollEvent |
| 35 | void HandleGameControllerEvent(const SDL_Event& event); | 32 | void HandleGameControllerEvent(const SDL_Event& event); |
| 36 | 33 | ||
| 37 | /// A Loop that calls HandleGameControllerEvent until Shutdown is called | 34 | std::shared_ptr<SDLJoystick> GetSDLJoystickBySDLID(SDL_JoystickID sdl_id); |
| 38 | void PollLoop(); | 35 | std::shared_ptr<SDLJoystick> GetSDLJoystickByGUID(const std::string& guid, int port); |
| 39 | 36 | ||
| 40 | /// Creates a ParamPackage from an SDL_Event that can directly be used to create a ButtonDevice | 37 | /// Get all DevicePoller that use the SDL backend for a specific device type |
| 41 | Common::ParamPackage SDLEventToButtonParamPackage(const SDL_Event& event); | 38 | std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> GetPollers( |
| 39 | InputCommon::Polling::DeviceType type) override; | ||
| 42 | 40 | ||
| 43 | namespace Polling { | 41 | /// Used by the Pollers during config |
| 42 | std::atomic<bool> polling = false; | ||
| 43 | Common::SPSCQueue<SDL_Event> event_queue; | ||
| 44 | 44 | ||
| 45 | /// Get all DevicePoller that use the SDL backend for a specific device type | 45 | private: |
| 46 | void GetPollers(InputCommon::Polling::DeviceType type, | 46 | void InitJoystick(int joystick_index); |
| 47 | std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>>& pollers); | 47 | void CloseJoystick(SDL_Joystick* sdl_joystick); |
| 48 | 48 | ||
| 49 | } // namespace Polling | 49 | /// Needs to be called before SDL_QuitSubSystem. |
| 50 | } // namespace SDL | 50 | void CloseJoysticks(); |
| 51 | } // namespace InputCommon | 51 | |
| 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 | |||
| 56 | std::shared_ptr<SDLButtonFactory> button_factory; | ||
| 57 | std::shared_ptr<SDLAnalogFactory> analog_factory; | ||
| 58 | |||
| 59 | bool start_thread = false; | ||
| 60 | std::atomic<bool> initialized = false; | ||
| 61 | |||
| 62 | std::thread poll_thread; | ||
| 63 | }; | ||
| 64 | } // namespace InputCommon::SDL | ||