diff options
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
| -rw-r--r-- | src/input_common/sdl/sdl_impl.cpp | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index 934339d3b..5949ecbae 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp | |||
| @@ -55,22 +55,22 @@ public: | |||
| 55 | : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, deleter} {} | 55 | : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, deleter} {} |
| 56 | 56 | ||
| 57 | void SetButton(int button, bool value) { | 57 | void SetButton(int button, bool value) { |
| 58 | std::lock_guard<std::mutex> lock(mutex); | 58 | std::lock_guard lock{mutex}; |
| 59 | state.buttons[button] = value; | 59 | state.buttons[button] = value; |
| 60 | } | 60 | } |
| 61 | 61 | ||
| 62 | bool GetButton(int button) const { | 62 | bool GetButton(int button) const { |
| 63 | std::lock_guard<std::mutex> lock(mutex); | 63 | std::lock_guard lock{mutex}; |
| 64 | return state.buttons.at(button); | 64 | return state.buttons.at(button); |
| 65 | } | 65 | } |
| 66 | 66 | ||
| 67 | void SetAxis(int axis, Sint16 value) { | 67 | void SetAxis(int axis, Sint16 value) { |
| 68 | std::lock_guard<std::mutex> lock(mutex); | 68 | std::lock_guard lock{mutex}; |
| 69 | state.axes[axis] = value; | 69 | state.axes[axis] = value; |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | float GetAxis(int axis) const { | 72 | float GetAxis(int axis) const { |
| 73 | std::lock_guard<std::mutex> lock(mutex); | 73 | std::lock_guard lock{mutex}; |
| 74 | return state.axes.at(axis) / 32767.0f; | 74 | return state.axes.at(axis) / 32767.0f; |
| 75 | } | 75 | } |
| 76 | 76 | ||
| @@ -92,12 +92,12 @@ public: | |||
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | void SetHat(int hat, Uint8 direction) { | 94 | void SetHat(int hat, Uint8 direction) { |
| 95 | std::lock_guard<std::mutex> lock(mutex); | 95 | std::lock_guard lock{mutex}; |
| 96 | state.hats[hat] = direction; | 96 | state.hats[hat] = direction; |
| 97 | } | 97 | } |
| 98 | 98 | ||
| 99 | bool GetHatDirection(int hat, Uint8 direction) const { | 99 | bool GetHatDirection(int hat, Uint8 direction) const { |
| 100 | std::lock_guard<std::mutex> lock(mutex); | 100 | std::lock_guard lock{mutex}; |
| 101 | return (state.hats.at(hat) & direction) != 0; | 101 | return (state.hats.at(hat) & direction) != 0; |
| 102 | } | 102 | } |
| 103 | /** | 103 | /** |
| @@ -140,7 +140,7 @@ private: | |||
| 140 | * Get the nth joystick with the corresponding GUID | 140 | * Get the nth joystick with the corresponding GUID |
| 141 | */ | 141 | */ |
| 142 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { | 142 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& guid, int port) { |
| 143 | std::lock_guard<std::mutex> lock(joystick_map_mutex); | 143 | std::lock_guard lock{joystick_map_mutex}; |
| 144 | const auto it = joystick_map.find(guid); | 144 | const auto it = joystick_map.find(guid); |
| 145 | if (it != joystick_map.end()) { | 145 | if (it != joystick_map.end()) { |
| 146 | while (it->second.size() <= port) { | 146 | while (it->second.size() <= port) { |
| @@ -161,7 +161,8 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& g | |||
| 161 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) { | 161 | std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) { |
| 162 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); | 162 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); |
| 163 | const std::string guid = GetGUID(sdl_joystick); | 163 | const std::string guid = GetGUID(sdl_joystick); |
| 164 | std::lock_guard<std::mutex> lock(joystick_map_mutex); | 164 | |
| 165 | std::lock_guard lock{joystick_map_mutex}; | ||
| 165 | auto map_it = joystick_map.find(guid); | 166 | auto map_it = joystick_map.find(guid); |
| 166 | if (map_it != joystick_map.end()) { | 167 | if (map_it != joystick_map.end()) { |
| 167 | auto vec_it = std::find_if(map_it->second.begin(), map_it->second.end(), | 168 | auto vec_it = std::find_if(map_it->second.begin(), map_it->second.end(), |
| @@ -198,8 +199,9 @@ void SDLState::InitJoystick(int joystick_index) { | |||
| 198 | LOG_ERROR(Input, "failed to open joystick {}", joystick_index); | 199 | LOG_ERROR(Input, "failed to open joystick {}", joystick_index); |
| 199 | return; | 200 | return; |
| 200 | } | 201 | } |
| 201 | std::string guid = GetGUID(sdl_joystick); | 202 | const std::string guid = GetGUID(sdl_joystick); |
| 202 | std::lock_guard<std::mutex> lock(joystick_map_mutex); | 203 | |
| 204 | std::lock_guard lock{joystick_map_mutex}; | ||
| 203 | if (joystick_map.find(guid) == joystick_map.end()) { | 205 | if (joystick_map.find(guid) == joystick_map.end()) { |
| 204 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick); | 206 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick); |
| 205 | joystick_map[guid].emplace_back(std::move(joystick)); | 207 | joystick_map[guid].emplace_back(std::move(joystick)); |
| @@ -221,7 +223,7 @@ void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { | |||
| 221 | std::string guid = GetGUID(sdl_joystick); | 223 | std::string guid = GetGUID(sdl_joystick); |
| 222 | std::shared_ptr<SDLJoystick> joystick; | 224 | std::shared_ptr<SDLJoystick> joystick; |
| 223 | { | 225 | { |
| 224 | std::lock_guard<std::mutex> lock(joystick_map_mutex); | 226 | std::lock_guard lock{joystick_map_mutex}; |
| 225 | // This call to guid is safe since the joystick is guaranteed to be in the map | 227 | // This call to guid is safe since the joystick is guaranteed to be in the map |
| 226 | auto& joystick_guid_list = joystick_map[guid]; | 228 | auto& joystick_guid_list = joystick_map[guid]; |
| 227 | const auto joystick_it = | 229 | const auto joystick_it = |
| @@ -274,7 +276,7 @@ void SDLState::HandleGameControllerEvent(const SDL_Event& event) { | |||
| 274 | } | 276 | } |
| 275 | 277 | ||
| 276 | void SDLState::CloseJoysticks() { | 278 | void SDLState::CloseJoysticks() { |
| 277 | std::lock_guard<std::mutex> lock(joystick_map_mutex); | 279 | std::lock_guard lock{joystick_map_mutex}; |
| 278 | joystick_map.clear(); | 280 | joystick_map.clear(); |
| 279 | } | 281 | } |
| 280 | 282 | ||
| @@ -475,12 +477,11 @@ SDLState::SDLState() { | |||
| 475 | 477 | ||
| 476 | initialized = true; | 478 | initialized = true; |
| 477 | if (start_thread) { | 479 | if (start_thread) { |
| 478 | poll_thread = std::thread([&] { | 480 | poll_thread = std::thread([this] { |
| 479 | using namespace std::chrono_literals; | 481 | using namespace std::chrono_literals; |
| 480 | SDL_Event event; | ||
| 481 | while (initialized) { | 482 | while (initialized) { |
| 482 | SDL_PumpEvents(); | 483 | SDL_PumpEvents(); |
| 483 | std::this_thread::sleep_for(std::chrono::duration(10ms)); | 484 | std::this_thread::sleep_for(10ms); |
| 484 | } | 485 | } |
| 485 | }); | 486 | }); |
| 486 | } | 487 | } |
| @@ -651,9 +652,9 @@ private: | |||
| 651 | }; | 652 | }; |
| 652 | } // namespace Polling | 653 | } // namespace Polling |
| 653 | 654 | ||
| 654 | std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> SDLState::GetPollers( | 655 | SDLState::Pollers SDLState::GetPollers(InputCommon::Polling::DeviceType type) { |
| 655 | InputCommon::Polling::DeviceType type) { | 656 | Pollers pollers; |
| 656 | std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> pollers; | 657 | |
| 657 | switch (type) { | 658 | switch (type) { |
| 658 | case InputCommon::Polling::DeviceType::Analog: | 659 | case InputCommon::Polling::DeviceType::Analog: |
| 659 | pollers.emplace_back(std::make_unique<Polling::SDLAnalogPoller>(*this)); | 660 | pollers.emplace_back(std::make_unique<Polling::SDLAnalogPoller>(*this)); |
| @@ -661,8 +662,9 @@ std::vector<std::unique_ptr<InputCommon::Polling::DevicePoller>> SDLState::GetPo | |||
| 661 | case InputCommon::Polling::DeviceType::Button: | 662 | case InputCommon::Polling::DeviceType::Button: |
| 662 | pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this)); | 663 | pollers.emplace_back(std::make_unique<Polling::SDLButtonPoller>(*this)); |
| 663 | break; | 664 | break; |
| 664 | return pollers; | ||
| 665 | } | 665 | } |
| 666 | |||
| 667 | return pollers; | ||
| 666 | } | 668 | } |
| 667 | 669 | ||
| 668 | } // namespace SDL | 670 | } // namespace SDL |