summaryrefslogtreecommitdiff
path: root/src/input_common/sdl/sdl_impl.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/input_common/sdl/sdl_impl.cpp')
-rw-r--r--src/input_common/sdl/sdl_impl.cpp28
1 files changed, 17 insertions, 11 deletions
diff --git a/src/input_common/sdl/sdl_impl.cpp b/src/input_common/sdl/sdl_impl.cpp
index 83fcf354e..f5dbd015e 100644
--- a/src/input_common/sdl/sdl_impl.cpp
+++ b/src/input_common/sdl/sdl_impl.cpp
@@ -159,9 +159,9 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickByGUID(const std::string& g
159 * it to a SDLJoystick with the same guid and that port 159 * it to a SDLJoystick with the same guid and that port
160 */ 160 */
161std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) { 161std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_id) {
162 std::lock_guard<std::mutex> lock(joystick_map_mutex);
163 auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); 162 auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id);
164 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);
165 auto map_it = joystick_map.find(guid); 165 auto map_it = joystick_map.find(guid);
166 if (map_it != joystick_map.end()) { 166 if (map_it != joystick_map.end()) {
167 auto vec_it = std::find_if(map_it->second.begin(), map_it->second.end(), 167 auto vec_it = std::find_if(map_it->second.begin(), map_it->second.end(),
@@ -193,13 +193,13 @@ std::shared_ptr<SDLJoystick> SDLState::GetSDLJoystickBySDLID(SDL_JoystickID sdl_
193} 193}
194 194
195void SDLState::InitJoystick(int joystick_index) { 195void SDLState::InitJoystick(int joystick_index) {
196 std::lock_guard<std::mutex> lock(joystick_map_mutex);
197 SDL_Joystick* sdl_joystick = SDL_JoystickOpen(joystick_index); 196 SDL_Joystick* sdl_joystick = SDL_JoystickOpen(joystick_index);
198 if (!sdl_joystick) { 197 if (!sdl_joystick) {
199 LOG_ERROR(Input, "failed to open joystick {}", joystick_index); 198 LOG_ERROR(Input, "failed to open joystick {}", joystick_index);
200 return; 199 return;
201 } 200 }
202 std::string guid = GetGUID(sdl_joystick); 201 std::string guid = GetGUID(sdl_joystick);
202 std::lock_guard<std::mutex> lock(joystick_map_mutex);
203 if (joystick_map.find(guid) == joystick_map.end()) { 203 if (joystick_map.find(guid) == joystick_map.end()) {
204 auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick); 204 auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick);
205 joystick_map[guid].emplace_back(std::move(joystick)); 205 joystick_map[guid].emplace_back(std::move(joystick));
@@ -218,16 +218,22 @@ void SDLState::InitJoystick(int joystick_index) {
218} 218}
219 219
220void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) { 220void SDLState::CloseJoystick(SDL_Joystick* sdl_joystick) {
221 std::lock_guard<std::mutex> lock(joystick_map_mutex);
222 std::string guid = GetGUID(sdl_joystick); 221 std::string guid = GetGUID(sdl_joystick);
223 // This call to guid is safe since the joystick is guaranteed to be in the map 222 std::shared_ptr<SDLJoystick> joystick;
224 auto& joystick_guid_list = joystick_map[guid]; 223 {
225 const auto joystick_it = 224 std::lock_guard<std::mutex> lock(joystick_map_mutex);
226 std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), 225 // This call to guid is safe since the joystick is guaranteed to be in the map
227 [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) { 226 auto& joystick_guid_list = joystick_map[guid];
228 return joystick->GetSDLJoystick() == sdl_joystick; 227 const auto joystick_it =
229 }); 228 std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(),
230 (*joystick_it)->SetSDLJoystick(nullptr, [](SDL_Joystick*) {}); 229 [&sdl_joystick](const std::shared_ptr<SDLJoystick>& joystick) {
230 return joystick->GetSDLJoystick() == sdl_joystick;
231 });
232 joystick = *joystick_it;
233 }
234 // Destruct SDL_Joystick outside the lock guard because SDL can internally call event calback
235 // which locks the mutex again
236 joystick->SetSDLJoystick(nullptr, [](SDL_Joystick*) {});
231} 237}
232 238
233void SDLState::HandleGameControllerEvent(const SDL_Event& event) { 239void SDLState::HandleGameControllerEvent(const SDL_Event& event) {