diff options
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
| -rw-r--r-- | src/input_common/sdl/sdl_impl.cpp | 29 |
1 files changed, 14 insertions, 15 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp index be1a77b30..f19a353c3 100644 --- a/src/input_common/sdl/sdl_impl.cpp +++ b/src/input_common/sdl/sdl_impl.cpp | |||
| @@ -46,9 +46,8 @@ static int SDLEventWatcher(void* userdata, SDL_Event* event) { | |||
| 46 | 46 | ||
| 47 | class SDLJoystick { | 47 | class SDLJoystick { |
| 48 | public: | 48 | public: |
| 49 | SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick, | 49 | SDLJoystick(std::string guid_, int port_, SDL_Joystick* joystick) |
| 50 | decltype(&SDL_JoystickClose) deleter = &SDL_JoystickClose) | 50 | : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, &SDL_JoystickClose} {} |
| 51 | : guid{std::move(guid_)}, port{port_}, sdl_joystick{joystick, deleter} {} | ||
| 52 | 51 | ||
| 53 | void SetButton(int button, bool value) { | 52 | void SetButton(int button, bool value) { |
| 54 | std::lock_guard lock{mutex}; | 53 | std::lock_guard lock{mutex}; |
| @@ -114,10 +113,8 @@ public: | |||
| 114 | return sdl_joystick.get(); | 113 | return sdl_joystick.get(); |
| 115 | } | 114 | } |
| 116 | 115 | ||
| 117 | void SetSDLJoystick(SDL_Joystick* joystick, | 116 | void SetSDLJoystick(SDL_Joystick* joystick) { |
| 118 | decltype(&SDL_JoystickClose) deleter = &SDL_JoystickClose) { | 117 | sdl_joystick.reset(joystick); |
| 119 | sdl_joystick = | ||
| 120 | std::unique_ptr<SDL_Joystick, decltype(&SDL_JoystickClose)>(joystick, deleter); | ||
| 121 | } | 118 | } |
| 122 | 119 | ||
| 123 | private: | 120 | private: |
| @@ -140,13 +137,13 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& g | |||
| 140 | const auto it = joystick_map.find(guid); | 137 | const auto it = joystick_map.find(guid); |
| 141 | if (it != joystick_map.end()) { | 138 | if (it != joystick_map.end()) { |
| 142 | while (it->second.size() <= static_cast<std::size_t>(port)) { | 139 | while (it->second.size() <= static_cast<std::size_t>(port)) { |
| 143 | auto joystick = std::make_shared<SDLJoystick>(guid, static_cast<int>(it->second.size()), | 140 | auto joystick = |
| 144 | nullptr, [](SDL_Joystick*) {}); | 141 | std::make_shared<SDLJoystick>(guid, static_cast<int>(it->second.size()), nullptr); |
| 145 | it->second.emplace_back(std::move(joystick)); | 142 | it->second.emplace_back(std::move(joystick)); |
| 146 | } | 143 | } |
| 147 | return it->second[port]; | 144 | return it->second[port]; |
| 148 | } | 145 | } |
| 149 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr, [](SDL_Joystick*) {}); | 146 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, nullptr); |
| 150 | return joystick_map[guid].emplace_back(std::move(joystick)); | 147 | return joystick_map[guid].emplace_back(std::move(joystick)); |
| 151 | } | 148 | } |
| 152 | 149 | ||
| @@ -222,12 +219,13 @@ void SDLState::InitJoystick(int joystick_index) { | |||
| 222 | } | 219 | } |
| 223 | 220 | ||
| 224 | void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { | 221 | void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { |
| 225 | std::string guid = GetGUID(sdl_joystick); | 222 | const std::string guid = GetGUID(sdl_joystick); |
| 223 | |||
| 226 | std::shared_ptr<SDLJoystick> joystick; | 224 | std::shared_ptr<SDLJoystick> joystick; |
| 227 | { | 225 | { |
| 228 | std::lock_guard lock{joystick_map_mutex}; | 226 | std::lock_guard lock{joystick_map_mutex}; |
| 229 | // 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 |
| 230 | auto& joystick_guid_list = joystick_map[guid]; | 228 | const auto& joystick_guid_list = joystick_map[guid]; |
| 231 | const auto joystick_it = | 229 | const auto joystick_it = |
| 232 | std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), | 230 | std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), |
| 233 | [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) { | 231 | [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) { |
| @@ -235,9 +233,10 @@ void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { | |||
| 235 | }); | 233 | }); |
| 236 | joystick = *joystick_it; | 234 | joystick = *joystick_it; |
| 237 | } | 235 | } |
| 238 | // Destruct SDL_Joystick outside the lock guard because SDL can internally call event calback | 236 | |
| 239 | // which locks the mutex again | 237 | // Destruct SDL_Joystick outside the lock guard because SDL can internally call the |
| 240 | joystick->SetSDLJoystick(nullptr, [](SDL_Joystick*) {}); | 238 | // event callback which locks the mutex again. |
| 239 | joystick->SetSDLJoystick(nullptr); | ||
| 241 | } | 240 | } |
| 242 | 241 | ||
| 243 | void SDLState::HandleGameControllerEvent(const SDL_Event& event) { | 242 | void SDLState::HandleGameControllerEvent(const SDL_Event& event) { |