diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/input_common/drivers/sdl_driver.cpp | 12 | ||||
| -rw-r--r-- | src/input_common/input_engine.cpp | 46 |
2 files changed, 29 insertions, 29 deletions
diff --git a/src/input_common/drivers/sdl_driver.cpp b/src/input_common/drivers/sdl_driver.cpp index c17ea305e..b3e4c3f64 100644 --- a/src/input_common/drivers/sdl_driver.cpp +++ b/src/input_common/drivers/sdl_driver.cpp | |||
| @@ -62,7 +62,7 @@ public: | |||
| 62 | 62 | ||
| 63 | bool UpdateMotion(SDL_ControllerSensorEvent event) { | 63 | bool UpdateMotion(SDL_ControllerSensorEvent event) { |
| 64 | constexpr float gravity_constant = 9.80665f; | 64 | constexpr float gravity_constant = 9.80665f; |
| 65 | std::lock_guard lock{mutex}; | 65 | std::scoped_lock lock{mutex}; |
| 66 | const u64 time_difference = event.timestamp - last_motion_update; | 66 | const u64 time_difference = event.timestamp - last_motion_update; |
| 67 | last_motion_update = event.timestamp; | 67 | last_motion_update = event.timestamp; |
| 68 | switch (event.sensor) { | 68 | switch (event.sensor) { |
| @@ -241,7 +241,7 @@ private: | |||
| 241 | }; | 241 | }; |
| 242 | 242 | ||
| 243 | std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickByGUID(const std::string& guid, int port) { | 243 | std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickByGUID(const std::string& guid, int port) { |
| 244 | std::lock_guard lock{joystick_map_mutex}; | 244 | std::scoped_lock lock{joystick_map_mutex}; |
| 245 | const auto it = joystick_map.find(guid); | 245 | const auto it = joystick_map.find(guid); |
| 246 | 246 | ||
| 247 | if (it != joystick_map.end()) { | 247 | if (it != joystick_map.end()) { |
| @@ -263,7 +263,7 @@ std::shared_ptr<SDLJoystick> SDLDriver::GetSDLJoystickBySDLID(SDL_JoystickID sdl | |||
| 263 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); | 263 | auto sdl_joystick = SDL_JoystickFromInstanceID(sdl_id); |
| 264 | const std::string guid = GetGUID(sdl_joystick); | 264 | const std::string guid = GetGUID(sdl_joystick); |
| 265 | 265 | ||
| 266 | std::lock_guard lock{joystick_map_mutex}; | 266 | std::scoped_lock lock{joystick_map_mutex}; |
| 267 | const auto map_it = joystick_map.find(guid); | 267 | const auto map_it = joystick_map.find(guid); |
| 268 | 268 | ||
| 269 | if (map_it == joystick_map.end()) { | 269 | if (map_it == joystick_map.end()) { |
| @@ -297,7 +297,7 @@ void SDLDriver::InitJoystick(int joystick_index) { | |||
| 297 | 297 | ||
| 298 | const std::string guid = GetGUID(sdl_joystick); | 298 | const std::string guid = GetGUID(sdl_joystick); |
| 299 | 299 | ||
| 300 | std::lock_guard lock{joystick_map_mutex}; | 300 | std::scoped_lock lock{joystick_map_mutex}; |
| 301 | if (joystick_map.find(guid) == joystick_map.end()) { | 301 | if (joystick_map.find(guid) == joystick_map.end()) { |
| 302 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller); | 302 | auto joystick = std::make_shared<SDLJoystick>(guid, 0, sdl_joystick, sdl_gamecontroller); |
| 303 | PreSetController(joystick->GetPadIdentifier()); | 303 | PreSetController(joystick->GetPadIdentifier()); |
| @@ -326,7 +326,7 @@ void SDLDriver::InitJoystick(int joystick_index) { | |||
| 326 | void SDLDriver::CloseJoystick(SDL_Joystick* sdl_joystick) { | 326 | void SDLDriver::CloseJoystick(SDL_Joystick* sdl_joystick) { |
| 327 | const std::string guid = GetGUID(sdl_joystick); | 327 | const std::string guid = GetGUID(sdl_joystick); |
| 328 | 328 | ||
| 329 | std::lock_guard lock{joystick_map_mutex}; | 329 | std::scoped_lock lock{joystick_map_mutex}; |
| 330 | // This call to guid is safe since the joystick is guaranteed to be in the map | 330 | // This call to guid is safe since the joystick is guaranteed to be in the map |
| 331 | const auto& joystick_guid_list = joystick_map[guid]; | 331 | const auto& joystick_guid_list = joystick_map[guid]; |
| 332 | const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), | 332 | const auto joystick_it = std::find_if(joystick_guid_list.begin(), joystick_guid_list.end(), |
| @@ -392,7 +392,7 @@ void SDLDriver::HandleGameControllerEvent(const SDL_Event& event) { | |||
| 392 | } | 392 | } |
| 393 | 393 | ||
| 394 | void SDLDriver::CloseJoysticks() { | 394 | void SDLDriver::CloseJoysticks() { |
| 395 | std::lock_guard lock{joystick_map_mutex}; | 395 | std::scoped_lock lock{joystick_map_mutex}; |
| 396 | joystick_map.clear(); | 396 | joystick_map.clear(); |
| 397 | } | 397 | } |
| 398 | 398 | ||
diff --git a/src/input_common/input_engine.cpp b/src/input_common/input_engine.cpp index 738022ece..a16cca33d 100644 --- a/src/input_common/input_engine.cpp +++ b/src/input_common/input_engine.cpp | |||
| @@ -8,37 +8,37 @@ | |||
| 8 | namespace InputCommon { | 8 | namespace InputCommon { |
| 9 | 9 | ||
| 10 | void InputEngine::PreSetController(const PadIdentifier& identifier) { | 10 | void InputEngine::PreSetController(const PadIdentifier& identifier) { |
| 11 | std::lock_guard lock{mutex}; | 11 | std::scoped_lock lock{mutex}; |
| 12 | controller_list.try_emplace(identifier); | 12 | controller_list.try_emplace(identifier); |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | void InputEngine::PreSetButton(const PadIdentifier& identifier, int button) { | 15 | void InputEngine::PreSetButton(const PadIdentifier& identifier, int button) { |
| 16 | std::lock_guard lock{mutex}; | 16 | std::scoped_lock lock{mutex}; |
| 17 | ControllerData& controller = controller_list.at(identifier); | 17 | ControllerData& controller = controller_list.at(identifier); |
| 18 | controller.buttons.try_emplace(button, false); | 18 | controller.buttons.try_emplace(button, false); |
| 19 | } | 19 | } |
| 20 | 20 | ||
| 21 | void InputEngine::PreSetHatButton(const PadIdentifier& identifier, int button) { | 21 | void InputEngine::PreSetHatButton(const PadIdentifier& identifier, int button) { |
| 22 | std::lock_guard lock{mutex}; | 22 | std::scoped_lock lock{mutex}; |
| 23 | ControllerData& controller = controller_list.at(identifier); | 23 | ControllerData& controller = controller_list.at(identifier); |
| 24 | controller.hat_buttons.try_emplace(button, u8{0}); | 24 | controller.hat_buttons.try_emplace(button, u8{0}); |
| 25 | } | 25 | } |
| 26 | 26 | ||
| 27 | void InputEngine::PreSetAxis(const PadIdentifier& identifier, int axis) { | 27 | void InputEngine::PreSetAxis(const PadIdentifier& identifier, int axis) { |
| 28 | std::lock_guard lock{mutex}; | 28 | std::scoped_lock lock{mutex}; |
| 29 | ControllerData& controller = controller_list.at(identifier); | 29 | ControllerData& controller = controller_list.at(identifier); |
| 30 | controller.axes.try_emplace(axis, 0.0f); | 30 | controller.axes.try_emplace(axis, 0.0f); |
| 31 | } | 31 | } |
| 32 | 32 | ||
| 33 | void InputEngine::PreSetMotion(const PadIdentifier& identifier, int motion) { | 33 | void InputEngine::PreSetMotion(const PadIdentifier& identifier, int motion) { |
| 34 | std::lock_guard lock{mutex}; | 34 | std::scoped_lock lock{mutex}; |
| 35 | ControllerData& controller = controller_list.at(identifier); | 35 | ControllerData& controller = controller_list.at(identifier); |
| 36 | controller.motions.try_emplace(motion); | 36 | controller.motions.try_emplace(motion); |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool value) { | 39 | void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool value) { |
| 40 | { | 40 | { |
| 41 | std::lock_guard lock{mutex}; | 41 | std::scoped_lock lock{mutex}; |
| 42 | ControllerData& controller = controller_list.at(identifier); | 42 | ControllerData& controller = controller_list.at(identifier); |
| 43 | if (!configuring) { | 43 | if (!configuring) { |
| 44 | controller.buttons.insert_or_assign(button, value); | 44 | controller.buttons.insert_or_assign(button, value); |
| @@ -49,7 +49,7 @@ void InputEngine::SetButton(const PadIdentifier& identifier, int button, bool va | |||
| 49 | 49 | ||
| 50 | void InputEngine::SetHatButton(const PadIdentifier& identifier, int button, u8 value) { | 50 | void InputEngine::SetHatButton(const PadIdentifier& identifier, int button, u8 value) { |
| 51 | { | 51 | { |
| 52 | std::lock_guard lock{mutex}; | 52 | std::scoped_lock lock{mutex}; |
| 53 | ControllerData& controller = controller_list.at(identifier); | 53 | ControllerData& controller = controller_list.at(identifier); |
| 54 | if (!configuring) { | 54 | if (!configuring) { |
| 55 | controller.hat_buttons.insert_or_assign(button, value); | 55 | controller.hat_buttons.insert_or_assign(button, value); |
| @@ -60,7 +60,7 @@ void InputEngine::SetHatButton(const PadIdentifier& identifier, int button, u8 v | |||
| 60 | 60 | ||
| 61 | void InputEngine::SetAxis(const PadIdentifier& identifier, int axis, f32 value) { | 61 | void InputEngine::SetAxis(const PadIdentifier& identifier, int axis, f32 value) { |
| 62 | { | 62 | { |
| 63 | std::lock_guard lock{mutex}; | 63 | std::scoped_lock lock{mutex}; |
| 64 | ControllerData& controller = controller_list.at(identifier); | 64 | ControllerData& controller = controller_list.at(identifier); |
| 65 | if (!configuring) { | 65 | if (!configuring) { |
| 66 | controller.axes.insert_or_assign(axis, value); | 66 | controller.axes.insert_or_assign(axis, value); |
| @@ -71,7 +71,7 @@ void InputEngine::SetAxis(const PadIdentifier& identifier, int axis, f32 value) | |||
| 71 | 71 | ||
| 72 | void InputEngine::SetBattery(const PadIdentifier& identifier, Common::Input::BatteryLevel value) { | 72 | void InputEngine::SetBattery(const PadIdentifier& identifier, Common::Input::BatteryLevel value) { |
| 73 | { | 73 | { |
| 74 | std::lock_guard lock{mutex}; | 74 | std::scoped_lock lock{mutex}; |
| 75 | ControllerData& controller = controller_list.at(identifier); | 75 | ControllerData& controller = controller_list.at(identifier); |
| 76 | if (!configuring) { | 76 | if (!configuring) { |
| 77 | controller.battery = value; | 77 | controller.battery = value; |
| @@ -82,7 +82,7 @@ void InputEngine::SetBattery(const PadIdentifier& identifier, Common::Input::Bat | |||
| 82 | 82 | ||
| 83 | void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, const BasicMotion& value) { | 83 | void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, const BasicMotion& value) { |
| 84 | { | 84 | { |
| 85 | std::lock_guard lock{mutex}; | 85 | std::scoped_lock lock{mutex}; |
| 86 | ControllerData& controller = controller_list.at(identifier); | 86 | ControllerData& controller = controller_list.at(identifier); |
| 87 | if (!configuring) { | 87 | if (!configuring) { |
| 88 | controller.motions.insert_or_assign(motion, value); | 88 | controller.motions.insert_or_assign(motion, value); |
| @@ -92,7 +92,7 @@ void InputEngine::SetMotion(const PadIdentifier& identifier, int motion, const B | |||
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | bool InputEngine::GetButton(const PadIdentifier& identifier, int button) const { | 94 | bool InputEngine::GetButton(const PadIdentifier& identifier, int button) const { |
| 95 | std::lock_guard lock{mutex}; | 95 | std::scoped_lock lock{mutex}; |
| 96 | const auto controller_iter = controller_list.find(identifier); | 96 | const auto controller_iter = controller_list.find(identifier); |
| 97 | if (controller_iter == controller_list.cend()) { | 97 | if (controller_iter == controller_list.cend()) { |
| 98 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), | 98 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), |
| @@ -109,7 +109,7 @@ bool InputEngine::GetButton(const PadIdentifier& identifier, int button) const { | |||
| 109 | } | 109 | } |
| 110 | 110 | ||
| 111 | bool InputEngine::GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const { | 111 | bool InputEngine::GetHatButton(const PadIdentifier& identifier, int button, u8 direction) const { |
| 112 | std::lock_guard lock{mutex}; | 112 | std::scoped_lock lock{mutex}; |
| 113 | const auto controller_iter = controller_list.find(identifier); | 113 | const auto controller_iter = controller_list.find(identifier); |
| 114 | if (controller_iter == controller_list.cend()) { | 114 | if (controller_iter == controller_list.cend()) { |
| 115 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), | 115 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), |
| @@ -126,7 +126,7 @@ bool InputEngine::GetHatButton(const PadIdentifier& identifier, int button, u8 d | |||
| 126 | } | 126 | } |
| 127 | 127 | ||
| 128 | f32 InputEngine::GetAxis(const PadIdentifier& identifier, int axis) const { | 128 | f32 InputEngine::GetAxis(const PadIdentifier& identifier, int axis) const { |
| 129 | std::lock_guard lock{mutex}; | 129 | std::scoped_lock lock{mutex}; |
| 130 | const auto controller_iter = controller_list.find(identifier); | 130 | const auto controller_iter = controller_list.find(identifier); |
| 131 | if (controller_iter == controller_list.cend()) { | 131 | if (controller_iter == controller_list.cend()) { |
| 132 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), | 132 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), |
| @@ -143,7 +143,7 @@ f32 InputEngine::GetAxis(const PadIdentifier& identifier, int axis) const { | |||
| 143 | } | 143 | } |
| 144 | 144 | ||
| 145 | Common::Input::BatteryLevel InputEngine::GetBattery(const PadIdentifier& identifier) const { | 145 | Common::Input::BatteryLevel InputEngine::GetBattery(const PadIdentifier& identifier) const { |
| 146 | std::lock_guard lock{mutex}; | 146 | std::scoped_lock lock{mutex}; |
| 147 | const auto controller_iter = controller_list.find(identifier); | 147 | const auto controller_iter = controller_list.find(identifier); |
| 148 | if (controller_iter == controller_list.cend()) { | 148 | if (controller_iter == controller_list.cend()) { |
| 149 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), | 149 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), |
| @@ -155,7 +155,7 @@ Common::Input::BatteryLevel InputEngine::GetBattery(const PadIdentifier& identif | |||
| 155 | } | 155 | } |
| 156 | 156 | ||
| 157 | BasicMotion InputEngine::GetMotion(const PadIdentifier& identifier, int motion) const { | 157 | BasicMotion InputEngine::GetMotion(const PadIdentifier& identifier, int motion) const { |
| 158 | std::lock_guard lock{mutex}; | 158 | std::scoped_lock lock{mutex}; |
| 159 | const auto controller_iter = controller_list.find(identifier); | 159 | const auto controller_iter = controller_list.find(identifier); |
| 160 | if (controller_iter == controller_list.cend()) { | 160 | if (controller_iter == controller_list.cend()) { |
| 161 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), | 161 | LOG_ERROR(Input, "Invalid identifier guid={}, pad={}, port={}", identifier.guid.RawString(), |
| @@ -186,7 +186,7 @@ void InputEngine::ResetAnalogState() { | |||
| 186 | } | 186 | } |
| 187 | 187 | ||
| 188 | void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value) { | 188 | void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int button, bool value) { |
| 189 | std::lock_guard lock{mutex_callback}; | 189 | std::scoped_lock lock{mutex_callback}; |
| 190 | for (const auto& poller_pair : callback_list) { | 190 | for (const auto& poller_pair : callback_list) { |
| 191 | const InputIdentifier& poller = poller_pair.second; | 191 | const InputIdentifier& poller = poller_pair.second; |
| 192 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Button, button)) { | 192 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Button, button)) { |
| @@ -214,7 +214,7 @@ void InputEngine::TriggerOnButtonChange(const PadIdentifier& identifier, int but | |||
| 214 | } | 214 | } |
| 215 | 215 | ||
| 216 | void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value) { | 216 | void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int button, u8 value) { |
| 217 | std::lock_guard lock{mutex_callback}; | 217 | std::scoped_lock lock{mutex_callback}; |
| 218 | for (const auto& poller_pair : callback_list) { | 218 | for (const auto& poller_pair : callback_list) { |
| 219 | const InputIdentifier& poller = poller_pair.second; | 219 | const InputIdentifier& poller = poller_pair.second; |
| 220 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::HatButton, button)) { | 220 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::HatButton, button)) { |
| @@ -243,7 +243,7 @@ void InputEngine::TriggerOnHatButtonChange(const PadIdentifier& identifier, int | |||
| 243 | } | 243 | } |
| 244 | 244 | ||
| 245 | void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis, f32 value) { | 245 | void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis, f32 value) { |
| 246 | std::lock_guard lock{mutex_callback}; | 246 | std::scoped_lock lock{mutex_callback}; |
| 247 | for (const auto& poller_pair : callback_list) { | 247 | for (const auto& poller_pair : callback_list) { |
| 248 | const InputIdentifier& poller = poller_pair.second; | 248 | const InputIdentifier& poller = poller_pair.second; |
| 249 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Analog, axis)) { | 249 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Analog, axis)) { |
| @@ -270,7 +270,7 @@ void InputEngine::TriggerOnAxisChange(const PadIdentifier& identifier, int axis, | |||
| 270 | 270 | ||
| 271 | void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier, | 271 | void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier, |
| 272 | [[maybe_unused]] Common::Input::BatteryLevel value) { | 272 | [[maybe_unused]] Common::Input::BatteryLevel value) { |
| 273 | std::lock_guard lock{mutex_callback}; | 273 | std::scoped_lock lock{mutex_callback}; |
| 274 | for (const auto& poller_pair : callback_list) { | 274 | for (const auto& poller_pair : callback_list) { |
| 275 | const InputIdentifier& poller = poller_pair.second; | 275 | const InputIdentifier& poller = poller_pair.second; |
| 276 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Battery, 0)) { | 276 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Battery, 0)) { |
| @@ -284,7 +284,7 @@ void InputEngine::TriggerOnBatteryChange(const PadIdentifier& identifier, | |||
| 284 | 284 | ||
| 285 | void InputEngine::TriggerOnMotionChange(const PadIdentifier& identifier, int motion, | 285 | void InputEngine::TriggerOnMotionChange(const PadIdentifier& identifier, int motion, |
| 286 | const BasicMotion& value) { | 286 | const BasicMotion& value) { |
| 287 | std::lock_guard lock{mutex_callback}; | 287 | std::scoped_lock lock{mutex_callback}; |
| 288 | for (const auto& poller_pair : callback_list) { | 288 | for (const auto& poller_pair : callback_list) { |
| 289 | const InputIdentifier& poller = poller_pair.second; | 289 | const InputIdentifier& poller = poller_pair.second; |
| 290 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Motion, motion)) { | 290 | if (!IsInputIdentifierEqual(poller, identifier, EngineInputType::Motion, motion)) { |
| @@ -346,18 +346,18 @@ const std::string& InputEngine::GetEngineName() const { | |||
| 346 | } | 346 | } |
| 347 | 347 | ||
| 348 | int InputEngine::SetCallback(InputIdentifier input_identifier) { | 348 | int InputEngine::SetCallback(InputIdentifier input_identifier) { |
| 349 | std::lock_guard lock{mutex_callback}; | 349 | std::scoped_lock lock{mutex_callback}; |
| 350 | callback_list.insert_or_assign(last_callback_key, std::move(input_identifier)); | 350 | callback_list.insert_or_assign(last_callback_key, std::move(input_identifier)); |
| 351 | return last_callback_key++; | 351 | return last_callback_key++; |
| 352 | } | 352 | } |
| 353 | 353 | ||
| 354 | void InputEngine::SetMappingCallback(MappingCallback callback) { | 354 | void InputEngine::SetMappingCallback(MappingCallback callback) { |
| 355 | std::lock_guard lock{mutex_callback}; | 355 | std::scoped_lock lock{mutex_callback}; |
| 356 | mapping_callback = std::move(callback); | 356 | mapping_callback = std::move(callback); |
| 357 | } | 357 | } |
| 358 | 358 | ||
| 359 | void InputEngine::DeleteCallback(int key) { | 359 | void InputEngine::DeleteCallback(int key) { |
| 360 | std::lock_guard lock{mutex_callback}; | 360 | std::scoped_lock lock{mutex_callback}; |
| 361 | const auto& iterator = callback_list.find(key); | 361 | const auto& iterator = callback_list.find(key); |
| 362 | if (iterator == callback_list.end()) { | 362 | if (iterator == callback_list.end()) { |
| 363 | LOG_ERROR(Input, "Tried to delete non-existent callback {}", key); | 363 | LOG_ERROR(Input, "Tried to delete non-existent callback {}", key); |