diff options
| author | 2023-01-19 20:20:19 -0600 | |
|---|---|---|
| committer | 2023-01-20 00:51:46 -0600 | |
| commit | d9ee7c32975bb8d840cf93a086d6b4be39d7bfd2 (patch) | |
| tree | 4d5236dd770ae240acfd6e9d71bdca8d7c5c2c3d | |
| parent | input_common: Fix joycon mappings (diff) | |
| download | yuzu-d9ee7c32975bb8d840cf93a086d6b4be39d7bfd2.tar.gz yuzu-d9ee7c32975bb8d840cf93a086d6b4be39d7bfd2.tar.xz yuzu-d9ee7c32975bb8d840cf93a086d6b4be39d7bfd2.zip | |
core: hid: Make use of SCOPE_EXIT and SCOPE_GUARD where applicable
Diffstat (limited to '')
| -rw-r--r-- | src/core/hid/emulated_controller.cpp | 105 |
1 files changed, 38 insertions, 67 deletions
diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp index f83abad05..0e06468da 100644 --- a/src/core/hid/emulated_controller.cpp +++ b/src/core/hid/emulated_controller.cpp | |||
| @@ -2,6 +2,7 @@ | |||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | 3 | ||
| 4 | #include <algorithm> | 4 | #include <algorithm> |
| 5 | #include <common/scope_exit.h> | ||
| 5 | 6 | ||
| 6 | #include "common/polyfill_ranges.h" | 7 | #include "common/polyfill_ranges.h" |
| 7 | #include "common/thread.h" | 8 | #include "common/thread.h" |
| @@ -834,17 +835,21 @@ void EmulatedController::SetStick(const Common::Input::CallbackStatus& callback, | |||
| 834 | if (index >= controller.stick_values.size()) { | 835 | if (index >= controller.stick_values.size()) { |
| 835 | return; | 836 | return; |
| 836 | } | 837 | } |
| 837 | std::unique_lock lock{mutex}; | 838 | auto trigger_guard = |
| 839 | SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Stick, !is_configuring); }); | ||
| 840 | std::scoped_lock lock{mutex}; | ||
| 838 | const auto stick_value = TransformToStick(callback); | 841 | const auto stick_value = TransformToStick(callback); |
| 839 | 842 | ||
| 840 | // Only read stick values that have the same uuid or are over the threshold to avoid flapping | 843 | // Only read stick values that have the same uuid or are over the threshold to avoid flapping |
| 841 | if (controller.stick_values[index].uuid != uuid) { | 844 | if (controller.stick_values[index].uuid != uuid) { |
| 842 | const bool is_tas = uuid == TAS_UUID; | 845 | const bool is_tas = uuid == TAS_UUID; |
| 843 | if (is_tas && stick_value.x.value == 0 && stick_value.y.value == 0) { | 846 | if (is_tas && stick_value.x.value == 0 && stick_value.y.value == 0) { |
| 847 | trigger_guard.Cancel(); | ||
| 844 | return; | 848 | return; |
| 845 | } | 849 | } |
| 846 | if (!is_tas && !stick_value.down && !stick_value.up && !stick_value.left && | 850 | if (!is_tas && !stick_value.down && !stick_value.up && !stick_value.left && |
| 847 | !stick_value.right) { | 851 | !stick_value.right) { |
| 852 | trigger_guard.Cancel(); | ||
| 848 | return; | 853 | return; |
| 849 | } | 854 | } |
| 850 | } | 855 | } |
| @@ -855,8 +860,6 @@ void EmulatedController::SetStick(const Common::Input::CallbackStatus& callback, | |||
| 855 | if (is_configuring) { | 860 | if (is_configuring) { |
| 856 | controller.analog_stick_state.left = {}; | 861 | controller.analog_stick_state.left = {}; |
| 857 | controller.analog_stick_state.right = {}; | 862 | controller.analog_stick_state.right = {}; |
| 858 | lock.unlock(); | ||
| 859 | TriggerOnChange(ControllerTriggerType::Stick, false); | ||
| 860 | return; | 863 | return; |
| 861 | } | 864 | } |
| 862 | 865 | ||
| @@ -881,9 +884,6 @@ void EmulatedController::SetStick(const Common::Input::CallbackStatus& callback, | |||
| 881 | controller.npad_button_state.stick_r_down.Assign(controller.stick_values[index].down); | 884 | controller.npad_button_state.stick_r_down.Assign(controller.stick_values[index].down); |
| 882 | break; | 885 | break; |
| 883 | } | 886 | } |
| 884 | |||
| 885 | lock.unlock(); | ||
| 886 | TriggerOnChange(ControllerTriggerType::Stick, true); | ||
| 887 | } | 887 | } |
| 888 | 888 | ||
| 889 | void EmulatedController::SetTrigger(const Common::Input::CallbackStatus& callback, | 889 | void EmulatedController::SetTrigger(const Common::Input::CallbackStatus& callback, |
| @@ -891,7 +891,9 @@ void EmulatedController::SetTrigger(const Common::Input::CallbackStatus& callbac | |||
| 891 | if (index >= controller.trigger_values.size()) { | 891 | if (index >= controller.trigger_values.size()) { |
| 892 | return; | 892 | return; |
| 893 | } | 893 | } |
| 894 | std::unique_lock lock{mutex}; | 894 | auto trigger_guard = |
| 895 | SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Trigger, !is_configuring); }); | ||
| 896 | std::scoped_lock lock{mutex}; | ||
| 895 | const auto trigger_value = TransformToTrigger(callback); | 897 | const auto trigger_value = TransformToTrigger(callback); |
| 896 | 898 | ||
| 897 | // Only read trigger values that have the same uuid or are pressed once | 899 | // Only read trigger values that have the same uuid or are pressed once |
| @@ -907,13 +909,12 @@ void EmulatedController::SetTrigger(const Common::Input::CallbackStatus& callbac | |||
| 907 | if (is_configuring) { | 909 | if (is_configuring) { |
| 908 | controller.gc_trigger_state.left = 0; | 910 | controller.gc_trigger_state.left = 0; |
| 909 | controller.gc_trigger_state.right = 0; | 911 | controller.gc_trigger_state.right = 0; |
| 910 | lock.unlock(); | ||
| 911 | TriggerOnChange(ControllerTriggerType::Trigger, false); | ||
| 912 | return; | 912 | return; |
| 913 | } | 913 | } |
| 914 | 914 | ||
| 915 | // Only GC controllers have analog triggers | 915 | // Only GC controllers have analog triggers |
| 916 | if (npad_type != NpadStyleIndex::GameCube) { | 916 | if (npad_type != NpadStyleIndex::GameCube) { |
| 917 | trigger_guard.Cancel(); | ||
| 917 | return; | 918 | return; |
| 918 | } | 919 | } |
| 919 | 920 | ||
| @@ -930,9 +931,6 @@ void EmulatedController::SetTrigger(const Common::Input::CallbackStatus& callbac | |||
| 930 | controller.npad_button_state.zr.Assign(trigger.pressed.value); | 931 | controller.npad_button_state.zr.Assign(trigger.pressed.value); |
| 931 | break; | 932 | break; |
| 932 | } | 933 | } |
| 933 | |||
| 934 | lock.unlock(); | ||
| 935 | TriggerOnChange(ControllerTriggerType::Trigger, true); | ||
| 936 | } | 934 | } |
| 937 | 935 | ||
| 938 | void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback, | 936 | void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback, |
| @@ -940,7 +938,8 @@ void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback | |||
| 940 | if (index >= controller.motion_values.size()) { | 938 | if (index >= controller.motion_values.size()) { |
| 941 | return; | 939 | return; |
| 942 | } | 940 | } |
| 943 | std::unique_lock lock{mutex}; | 941 | SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::Motion, !is_configuring); }); |
| 942 | std::scoped_lock lock{mutex}; | ||
| 944 | auto& raw_status = controller.motion_values[index].raw_status; | 943 | auto& raw_status = controller.motion_values[index].raw_status; |
| 945 | auto& emulated = controller.motion_values[index].emulated; | 944 | auto& emulated = controller.motion_values[index].emulated; |
| 946 | 945 | ||
| @@ -961,8 +960,6 @@ void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback | |||
| 961 | force_update_motion = raw_status.force_update; | 960 | force_update_motion = raw_status.force_update; |
| 962 | 961 | ||
| 963 | if (is_configuring) { | 962 | if (is_configuring) { |
| 964 | lock.unlock(); | ||
| 965 | TriggerOnChange(ControllerTriggerType::Motion, false); | ||
| 966 | return; | 963 | return; |
| 967 | } | 964 | } |
| 968 | 965 | ||
| @@ -972,9 +969,6 @@ void EmulatedController::SetMotion(const Common::Input::CallbackStatus& callback | |||
| 972 | motion.rotation = emulated.GetRotations(); | 969 | motion.rotation = emulated.GetRotations(); |
| 973 | motion.orientation = emulated.GetOrientation(); | 970 | motion.orientation = emulated.GetOrientation(); |
| 974 | motion.is_at_rest = !emulated.IsMoving(motion_sensitivity); | 971 | motion.is_at_rest = !emulated.IsMoving(motion_sensitivity); |
| 975 | |||
| 976 | lock.unlock(); | ||
| 977 | TriggerOnChange(ControllerTriggerType::Motion, true); | ||
| 978 | } | 972 | } |
| 979 | 973 | ||
| 980 | void EmulatedController::SetColors(const Common::Input::CallbackStatus& callback, | 974 | void EmulatedController::SetColors(const Common::Input::CallbackStatus& callback, |
| @@ -982,16 +976,17 @@ void EmulatedController::SetColors(const Common::Input::CallbackStatus& callback | |||
| 982 | if (index >= controller.color_values.size()) { | 976 | if (index >= controller.color_values.size()) { |
| 983 | return; | 977 | return; |
| 984 | } | 978 | } |
| 985 | std::unique_lock lock{mutex}; | 979 | auto trigger_guard = |
| 980 | SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Color, !is_configuring); }); | ||
| 981 | std::scoped_lock lock{mutex}; | ||
| 986 | controller.color_values[index] = TransformToColor(callback); | 982 | controller.color_values[index] = TransformToColor(callback); |
| 987 | 983 | ||
| 988 | if (is_configuring) { | 984 | if (is_configuring) { |
| 989 | lock.unlock(); | ||
| 990 | TriggerOnChange(ControllerTriggerType::Color, false); | ||
| 991 | return; | 985 | return; |
| 992 | } | 986 | } |
| 993 | 987 | ||
| 994 | if (controller.color_values[index].body == 0) { | 988 | if (controller.color_values[index].body == 0) { |
| 989 | trigger_guard.Cancel(); | ||
| 995 | return; | 990 | return; |
| 996 | } | 991 | } |
| 997 | 992 | ||
| @@ -1024,9 +1019,6 @@ void EmulatedController::SetColors(const Common::Input::CallbackStatus& callback | |||
| 1024 | break; | 1019 | break; |
| 1025 | } | 1020 | } |
| 1026 | } | 1021 | } |
| 1027 | |||
| 1028 | lock.unlock(); | ||
| 1029 | TriggerOnChange(ControllerTriggerType::Color, true); | ||
| 1030 | } | 1022 | } |
| 1031 | 1023 | ||
| 1032 | void EmulatedController::SetBattery(const Common::Input::CallbackStatus& callback, | 1024 | void EmulatedController::SetBattery(const Common::Input::CallbackStatus& callback, |
| @@ -1034,12 +1026,11 @@ void EmulatedController::SetBattery(const Common::Input::CallbackStatus& callbac | |||
| 1034 | if (index >= controller.battery_values.size()) { | 1026 | if (index >= controller.battery_values.size()) { |
| 1035 | return; | 1027 | return; |
| 1036 | } | 1028 | } |
| 1037 | std::unique_lock lock{mutex}; | 1029 | SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::Battery, !is_configuring); }); |
| 1030 | std::scoped_lock lock{mutex}; | ||
| 1038 | controller.battery_values[index] = TransformToBattery(callback); | 1031 | controller.battery_values[index] = TransformToBattery(callback); |
| 1039 | 1032 | ||
| 1040 | if (is_configuring) { | 1033 | if (is_configuring) { |
| 1041 | lock.unlock(); | ||
| 1042 | TriggerOnChange(ControllerTriggerType::Battery, false); | ||
| 1043 | return; | 1034 | return; |
| 1044 | } | 1035 | } |
| 1045 | 1036 | ||
| @@ -1095,18 +1086,14 @@ void EmulatedController::SetBattery(const Common::Input::CallbackStatus& callbac | |||
| 1095 | }; | 1086 | }; |
| 1096 | break; | 1087 | break; |
| 1097 | } | 1088 | } |
| 1098 | |||
| 1099 | lock.unlock(); | ||
| 1100 | TriggerOnChange(ControllerTriggerType::Battery, true); | ||
| 1101 | } | 1089 | } |
| 1102 | 1090 | ||
| 1103 | void EmulatedController::SetCamera(const Common::Input::CallbackStatus& callback) { | 1091 | void EmulatedController::SetCamera(const Common::Input::CallbackStatus& callback) { |
| 1104 | std::unique_lock lock{mutex}; | 1092 | SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::IrSensor, !is_configuring); }); |
| 1093 | std::scoped_lock lock{mutex}; | ||
| 1105 | controller.camera_values = TransformToCamera(callback); | 1094 | controller.camera_values = TransformToCamera(callback); |
| 1106 | 1095 | ||
| 1107 | if (is_configuring) { | 1096 | if (is_configuring) { |
| 1108 | lock.unlock(); | ||
| 1109 | TriggerOnChange(ControllerTriggerType::IrSensor, false); | ||
| 1110 | return; | 1097 | return; |
| 1111 | } | 1098 | } |
| 1112 | 1099 | ||
| @@ -1114,36 +1101,28 @@ void EmulatedController::SetCamera(const Common::Input::CallbackStatus& callback | |||
| 1114 | controller.camera_state.format = | 1101 | controller.camera_state.format = |
| 1115 | static_cast<Core::IrSensor::ImageTransferProcessorFormat>(controller.camera_values.format); | 1102 | static_cast<Core::IrSensor::ImageTransferProcessorFormat>(controller.camera_values.format); |
| 1116 | controller.camera_state.data = controller.camera_values.data; | 1103 | controller.camera_state.data = controller.camera_values.data; |
| 1117 | |||
| 1118 | lock.unlock(); | ||
| 1119 | TriggerOnChange(ControllerTriggerType::IrSensor, true); | ||
| 1120 | } | 1104 | } |
| 1121 | 1105 | ||
| 1122 | void EmulatedController::SetRingAnalog(const Common::Input::CallbackStatus& callback) { | 1106 | void EmulatedController::SetRingAnalog(const Common::Input::CallbackStatus& callback) { |
| 1123 | std::unique_lock lock{mutex}; | 1107 | SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::RingController, !is_configuring); }); |
| 1108 | std::scoped_lock lock{mutex}; | ||
| 1124 | const auto force_value = TransformToStick(callback); | 1109 | const auto force_value = TransformToStick(callback); |
| 1125 | 1110 | ||
| 1126 | controller.ring_analog_value = force_value.x; | 1111 | controller.ring_analog_value = force_value.x; |
| 1127 | 1112 | ||
| 1128 | if (is_configuring) { | 1113 | if (is_configuring) { |
| 1129 | lock.unlock(); | ||
| 1130 | TriggerOnChange(ControllerTriggerType::RingController, false); | ||
| 1131 | return; | 1114 | return; |
| 1132 | } | 1115 | } |
| 1133 | 1116 | ||
| 1134 | controller.ring_analog_state.force = force_value.x.value; | 1117 | controller.ring_analog_state.force = force_value.x.value; |
| 1135 | |||
| 1136 | lock.unlock(); | ||
| 1137 | TriggerOnChange(ControllerTriggerType::RingController, true); | ||
| 1138 | } | 1118 | } |
| 1139 | 1119 | ||
| 1140 | void EmulatedController::SetNfc(const Common::Input::CallbackStatus& callback) { | 1120 | void EmulatedController::SetNfc(const Common::Input::CallbackStatus& callback) { |
| 1141 | std::unique_lock lock{mutex}; | 1121 | SCOPE_EXIT({ TriggerOnChange(ControllerTriggerType::Nfc, !is_configuring); }); |
| 1122 | std::scoped_lock lock{mutex}; | ||
| 1142 | controller.nfc_values = TransformToNfc(callback); | 1123 | controller.nfc_values = TransformToNfc(callback); |
| 1143 | 1124 | ||
| 1144 | if (is_configuring) { | 1125 | if (is_configuring) { |
| 1145 | lock.unlock(); | ||
| 1146 | TriggerOnChange(ControllerTriggerType::Nfc, false); | ||
| 1147 | return; | 1126 | return; |
| 1148 | } | 1127 | } |
| 1149 | 1128 | ||
| @@ -1151,9 +1130,6 @@ void EmulatedController::SetNfc(const Common::Input::CallbackStatus& callback) { | |||
| 1151 | controller.nfc_values.state, | 1130 | controller.nfc_values.state, |
| 1152 | controller.nfc_values.data, | 1131 | controller.nfc_values.data, |
| 1153 | }; | 1132 | }; |
| 1154 | |||
| 1155 | lock.unlock(); | ||
| 1156 | TriggerOnChange(ControllerTriggerType::Nfc, true); | ||
| 1157 | } | 1133 | } |
| 1158 | 1134 | ||
| 1159 | bool EmulatedController::SetVibration(std::size_t device_index, VibrationValue vibration) { | 1135 | bool EmulatedController::SetVibration(std::size_t device_index, VibrationValue vibration) { |
| @@ -1412,39 +1388,35 @@ void EmulatedController::Connect(bool use_temporary_value) { | |||
| 1412 | return; | 1388 | return; |
| 1413 | } | 1389 | } |
| 1414 | 1390 | ||
| 1415 | std::unique_lock lock{mutex}; | 1391 | auto trigger_guard = |
| 1392 | SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Connected, !is_configuring); }); | ||
| 1393 | std::scoped_lock lock{mutex}; | ||
| 1416 | if (is_configuring) { | 1394 | if (is_configuring) { |
| 1417 | tmp_is_connected = true; | 1395 | tmp_is_connected = true; |
| 1418 | lock.unlock(); | ||
| 1419 | TriggerOnChange(ControllerTriggerType::Connected, false); | ||
| 1420 | return; | 1396 | return; |
| 1421 | } | 1397 | } |
| 1422 | 1398 | ||
| 1423 | if (is_connected) { | 1399 | if (is_connected) { |
| 1400 | trigger_guard.Cancel(); | ||
| 1424 | return; | 1401 | return; |
| 1425 | } | 1402 | } |
| 1426 | is_connected = true; | 1403 | is_connected = true; |
| 1427 | |||
| 1428 | lock.unlock(); | ||
| 1429 | TriggerOnChange(ControllerTriggerType::Connected, true); | ||
| 1430 | } | 1404 | } |
| 1431 | 1405 | ||
| 1432 | void EmulatedController::Disconnect() { | 1406 | void EmulatedController::Disconnect() { |
| 1433 | std::unique_lock lock{mutex}; | 1407 | auto trigger_guard = |
| 1408 | SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Disconnected, !is_configuring); }); | ||
| 1409 | std::scoped_lock lock{mutex}; | ||
| 1434 | if (is_configuring) { | 1410 | if (is_configuring) { |
| 1435 | tmp_is_connected = false; | 1411 | tmp_is_connected = false; |
| 1436 | lock.unlock(); | ||
| 1437 | TriggerOnChange(ControllerTriggerType::Disconnected, false); | ||
| 1438 | return; | 1412 | return; |
| 1439 | } | 1413 | } |
| 1440 | 1414 | ||
| 1441 | if (!is_connected) { | 1415 | if (!is_connected) { |
| 1416 | trigger_guard.Cancel(); | ||
| 1442 | return; | 1417 | return; |
| 1443 | } | 1418 | } |
| 1444 | is_connected = false; | 1419 | is_connected = false; |
| 1445 | |||
| 1446 | lock.unlock(); | ||
| 1447 | TriggerOnChange(ControllerTriggerType::Disconnected, true); | ||
| 1448 | } | 1420 | } |
| 1449 | 1421 | ||
| 1450 | bool EmulatedController::IsConnected(bool get_temporary_value) const { | 1422 | bool EmulatedController::IsConnected(bool get_temporary_value) const { |
| @@ -1469,19 +1441,21 @@ NpadStyleIndex EmulatedController::GetNpadStyleIndex(bool get_temporary_value) c | |||
| 1469 | } | 1441 | } |
| 1470 | 1442 | ||
| 1471 | void EmulatedController::SetNpadStyleIndex(NpadStyleIndex npad_type_) { | 1443 | void EmulatedController::SetNpadStyleIndex(NpadStyleIndex npad_type_) { |
| 1472 | std::unique_lock lock{mutex}; | 1444 | auto trigger_guard = |
| 1445 | SCOPE_GUARD({ TriggerOnChange(ControllerTriggerType::Type, !is_configuring); }); | ||
| 1446 | std::scoped_lock lock{mutex}; | ||
| 1473 | 1447 | ||
| 1474 | if (is_configuring) { | 1448 | if (is_configuring) { |
| 1475 | if (tmp_npad_type == npad_type_) { | 1449 | if (tmp_npad_type == npad_type_) { |
| 1450 | trigger_guard.Cancel(); | ||
| 1476 | return; | 1451 | return; |
| 1477 | } | 1452 | } |
| 1478 | tmp_npad_type = npad_type_; | 1453 | tmp_npad_type = npad_type_; |
| 1479 | lock.unlock(); | ||
| 1480 | TriggerOnChange(ControllerTriggerType::Type, false); | ||
| 1481 | return; | 1454 | return; |
| 1482 | } | 1455 | } |
| 1483 | 1456 | ||
| 1484 | if (npad_type == npad_type_) { | 1457 | if (npad_type == npad_type_) { |
| 1458 | trigger_guard.Cancel(); | ||
| 1485 | return; | 1459 | return; |
| 1486 | } | 1460 | } |
| 1487 | if (is_connected) { | 1461 | if (is_connected) { |
| @@ -1489,9 +1463,6 @@ void EmulatedController::SetNpadStyleIndex(NpadStyleIndex npad_type_) { | |||
| 1489 | NpadIdTypeToIndex(npad_id_type)); | 1463 | NpadIdTypeToIndex(npad_id_type)); |
| 1490 | } | 1464 | } |
| 1491 | npad_type = npad_type_; | 1465 | npad_type = npad_type_; |
| 1492 | |||
| 1493 | lock.unlock(); | ||
| 1494 | TriggerOnChange(ControllerTriggerType::Type, true); | ||
| 1495 | } | 1466 | } |
| 1496 | 1467 | ||
| 1497 | LedPattern EmulatedController::GetLedPattern() const { | 1468 | LedPattern EmulatedController::GetLedPattern() const { |
| @@ -1589,7 +1560,7 @@ DebugPadButton EmulatedController::GetDebugPadButtons() const { | |||
| 1589 | } | 1560 | } |
| 1590 | 1561 | ||
| 1591 | AnalogSticks EmulatedController::GetSticks() const { | 1562 | AnalogSticks EmulatedController::GetSticks() const { |
| 1592 | std::unique_lock lock{mutex}; | 1563 | std::scoped_lock lock{mutex}; |
| 1593 | 1564 | ||
| 1594 | if (is_configuring) { | 1565 | if (is_configuring) { |
| 1595 | return {}; | 1566 | return {}; |