diff options
| author | 2021-10-18 23:15:46 -0500 | |
|---|---|---|
| committer | 2021-11-24 20:30:24 -0600 | |
| commit | 4d308fd0b4fc8f14754c47811e751bf068b330b8 (patch) | |
| tree | 3ea29a0c024fdc9ad7681aaf6160e46511ceb2a6 /src/core/hid | |
| parent | core/hid: Documment some files (diff) | |
| download | yuzu-4d308fd0b4fc8f14754c47811e751bf068b330b8.tar.gz yuzu-4d308fd0b4fc8f14754c47811e751bf068b330b8.tar.xz yuzu-4d308fd0b4fc8f14754c47811e751bf068b330b8.zip | |
hid: Fix controller connection/disconnection
Diffstat (limited to 'src/core/hid')
| -rw-r--r-- | src/core/hid/emulated_console.cpp | 1 | ||||
| -rw-r--r-- | src/core/hid/emulated_controller.cpp | 98 | ||||
| -rw-r--r-- | src/core/hid/emulated_controller.h | 18 | ||||
| -rw-r--r-- | src/core/hid/hid_types.h | 8 |
4 files changed, 95 insertions, 30 deletions
diff --git a/src/core/hid/emulated_console.cpp b/src/core/hid/emulated_console.cpp index c65d05041..7f7c8fd59 100644 --- a/src/core/hid/emulated_console.cpp +++ b/src/core/hid/emulated_console.cpp | |||
| @@ -53,7 +53,6 @@ void EmulatedConsole::ReloadInput() { | |||
| 53 | touch_button_params.Set("x", x); | 53 | touch_button_params.Set("x", x); |
| 54 | touch_button_params.Set("y", y); | 54 | touch_button_params.Set("y", y); |
| 55 | touch_button_params.Set("touch_id", static_cast<int>(index)); | 55 | touch_button_params.Set("touch_id", static_cast<int>(index)); |
| 56 | LOG_ERROR(Common, "{} ", touch_button_params.Serialize()); | ||
| 57 | touch_devices[index] = | 56 | touch_devices[index] = |
| 58 | Input::CreateDeviceFromString<Input::InputDevice>(touch_button_params.Serialize()); | 57 | Input::CreateDeviceFromString<Input::InputDevice>(touch_button_params.Serialize()); |
| 59 | if (!touch_devices[index]) { | 58 | if (!touch_devices[index]) { |
diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp index b04ab4cd8..7ef6ef118 100644 --- a/src/core/hid/emulated_controller.cpp +++ b/src/core/hid/emulated_controller.cpp | |||
| @@ -54,6 +54,7 @@ Settings::ControllerType EmulatedController::MapNPadToSettingsType(NpadType type | |||
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | void EmulatedController::ReloadFromSettings() { | 56 | void EmulatedController::ReloadFromSettings() { |
| 57 | //LOG_ERROR(Service_HID, "reload config from settings {}", NpadIdTypeToIndex(npad_id_type)); | ||
| 57 | const auto player_index = NpadIdTypeToIndex(npad_id_type); | 58 | const auto player_index = NpadIdTypeToIndex(npad_id_type); |
| 58 | const auto& player = Settings::values.players.GetValue()[player_index]; | 59 | const auto& player = Settings::values.players.GetValue()[player_index]; |
| 59 | 60 | ||
| @@ -91,6 +92,7 @@ void EmulatedController::ReloadFromSettings() { | |||
| 91 | } | 92 | } |
| 92 | 93 | ||
| 93 | void EmulatedController::ReloadInput() { | 94 | void EmulatedController::ReloadInput() { |
| 95 | //LOG_ERROR(Service_HID, "reload config {}", NpadIdTypeToIndex(npad_id_type)); | ||
| 94 | // If you load any device here add the equivalent to the UnloadInput() function | 96 | // If you load any device here add the equivalent to the UnloadInput() function |
| 95 | const auto player_index = NpadIdTypeToIndex(npad_id_type); | 97 | const auto player_index = NpadIdTypeToIndex(npad_id_type); |
| 96 | const auto left_side = button_params[Settings::NativeButton::ZL]; | 98 | const auto left_side = button_params[Settings::NativeButton::ZL]; |
| @@ -187,11 +189,29 @@ void EmulatedController::UnloadInput() { | |||
| 187 | 189 | ||
| 188 | void EmulatedController::EnableConfiguration() { | 190 | void EmulatedController::EnableConfiguration() { |
| 189 | is_configuring = true; | 191 | is_configuring = true; |
| 190 | SaveCurrentConfig(); | 192 | temporary_is_connected = is_connected; |
| 193 | temporary_npad_type = npad_type; | ||
| 191 | } | 194 | } |
| 192 | 195 | ||
| 193 | void EmulatedController::DisableConfiguration() { | 196 | void EmulatedController::DisableConfiguration() { |
| 194 | is_configuring = false; | 197 | is_configuring = false; |
| 198 | |||
| 199 | // Apply temporary npad type to the real controller | ||
| 200 | if (temporary_npad_type != npad_type) { | ||
| 201 | if (is_connected) { | ||
| 202 | Disconnect(); | ||
| 203 | } | ||
| 204 | SetNpadType(temporary_npad_type); | ||
| 205 | } | ||
| 206 | |||
| 207 | // Apply temporary connected status to the real controller | ||
| 208 | if (temporary_is_connected != is_connected) { | ||
| 209 | if (temporary_is_connected) { | ||
| 210 | Connect(); | ||
| 211 | return; | ||
| 212 | } | ||
| 213 | Disconnect(); | ||
| 214 | } | ||
| 195 | } | 215 | } |
| 196 | 216 | ||
| 197 | bool EmulatedController::IsConfiguring() const { | 217 | bool EmulatedController::IsConfiguring() const { |
| @@ -199,10 +219,6 @@ bool EmulatedController::IsConfiguring() const { | |||
| 199 | } | 219 | } |
| 200 | 220 | ||
| 201 | void EmulatedController::SaveCurrentConfig() { | 221 | void EmulatedController::SaveCurrentConfig() { |
| 202 | if (!is_configuring) { | ||
| 203 | return; | ||
| 204 | } | ||
| 205 | |||
| 206 | const auto player_index = NpadIdTypeToIndex(npad_id_type); | 222 | const auto player_index = NpadIdTypeToIndex(npad_id_type); |
| 207 | auto& player = Settings::values.players.GetValue()[player_index]; | 223 | auto& player = Settings::values.players.GetValue()[player_index]; |
| 208 | player.connected = is_connected; | 224 | player.connected = is_connected; |
| @@ -657,26 +673,47 @@ void EmulatedController::SetLedPattern() { | |||
| 657 | } | 673 | } |
| 658 | 674 | ||
| 659 | void EmulatedController::Connect() { | 675 | void EmulatedController::Connect() { |
| 660 | std::lock_guard lock{mutex}; | 676 | { |
| 661 | if (is_connected) { | 677 | std::lock_guard lock{mutex}; |
| 662 | LOG_WARNING(Service_HID, "Tried to turn on a connected controller {}", npad_id_type); | 678 | if (is_configuring) { |
| 663 | return; | 679 | temporary_is_connected = true; |
| 680 | TriggerOnChange(ControllerTriggerType::Connected); | ||
| 681 | return; | ||
| 682 | } | ||
| 683 | |||
| 684 | if (is_connected) { | ||
| 685 | return; | ||
| 686 | } | ||
| 687 | is_connected = true; | ||
| 664 | } | 688 | } |
| 665 | is_connected = true; | 689 | LOG_ERROR(Service_HID, "Connected controller {}", NpadIdTypeToIndex(npad_id_type)); |
| 666 | TriggerOnChange(ControllerTriggerType::Connected); | 690 | TriggerOnChange(ControllerTriggerType::Connected); |
| 667 | } | 691 | } |
| 668 | 692 | ||
| 669 | void EmulatedController::Disconnect() { | 693 | void EmulatedController::Disconnect() { |
| 670 | std::lock_guard lock{mutex}; | 694 | { |
| 671 | if (!is_connected) { | 695 | std::lock_guard lock{mutex}; |
| 672 | LOG_WARNING(Service_HID, "Tried to turn off a disconnected controller {}", npad_id_type); | 696 | if (is_configuring) { |
| 673 | return; | 697 | temporary_is_connected = false; |
| 698 | LOG_ERROR(Service_HID, "Disconnected temporal controller {}", | ||
| 699 | NpadIdTypeToIndex(npad_id_type)); | ||
| 700 | TriggerOnChange(ControllerTriggerType::Disconnected); | ||
| 701 | return; | ||
| 702 | } | ||
| 703 | |||
| 704 | if (!is_connected) { | ||
| 705 | return; | ||
| 706 | } | ||
| 707 | is_connected = false; | ||
| 674 | } | 708 | } |
| 675 | is_connected = false; | 709 | LOG_ERROR(Service_HID, "Disconnected controller {}", NpadIdTypeToIndex(npad_id_type)); |
| 676 | TriggerOnChange(ControllerTriggerType::Disconnected); | 710 | TriggerOnChange(ControllerTriggerType::Disconnected); |
| 677 | } | 711 | } |
| 678 | 712 | ||
| 679 | bool EmulatedController::IsConnected() const { | 713 | bool EmulatedController::IsConnected(bool temporary) const { |
| 714 | if (temporary) { | ||
| 715 | return temporary_is_connected; | ||
| 716 | } | ||
| 680 | return is_connected; | 717 | return is_connected; |
| 681 | } | 718 | } |
| 682 | 719 | ||
| @@ -688,16 +725,35 @@ NpadIdType EmulatedController::GetNpadIdType() const { | |||
| 688 | return npad_id_type; | 725 | return npad_id_type; |
| 689 | } | 726 | } |
| 690 | 727 | ||
| 691 | NpadType EmulatedController::GetNpadType() const { | 728 | NpadType EmulatedController::GetNpadType(bool temporary) const { |
| 729 | if (temporary) { | ||
| 730 | return temporary_npad_type; | ||
| 731 | } | ||
| 692 | return npad_type; | 732 | return npad_type; |
| 693 | } | 733 | } |
| 694 | 734 | ||
| 695 | void EmulatedController::SetNpadType(NpadType npad_type_) { | 735 | void EmulatedController::SetNpadType(NpadType npad_type_) { |
| 696 | std::lock_guard lock{mutex}; | 736 | { |
| 697 | if (npad_type == npad_type_) { | 737 | std::lock_guard lock{mutex}; |
| 698 | return; | 738 | |
| 739 | if (is_configuring) { | ||
| 740 | if (temporary_npad_type == npad_type_) { | ||
| 741 | return; | ||
| 742 | } | ||
| 743 | temporary_npad_type = npad_type_; | ||
| 744 | TriggerOnChange(ControllerTriggerType::Type); | ||
| 745 | return; | ||
| 746 | } | ||
| 747 | |||
| 748 | if (npad_type == npad_type_) { | ||
| 749 | return; | ||
| 750 | } | ||
| 751 | if (is_connected) { | ||
| 752 | LOG_WARNING(Service_HID, "Controller {} type changed while it's connected", | ||
| 753 | NpadIdTypeToIndex(npad_id_type)); | ||
| 754 | } | ||
| 755 | npad_type = npad_type_; | ||
| 699 | } | 756 | } |
| 700 | npad_type = npad_type_; | ||
| 701 | TriggerOnChange(ControllerTriggerType::Type); | 757 | TriggerOnChange(ControllerTriggerType::Type); |
| 702 | } | 758 | } |
| 703 | 759 | ||
diff --git a/src/core/hid/emulated_controller.h b/src/core/hid/emulated_controller.h index 096fe1705..6a6dc1892 100644 --- a/src/core/hid/emulated_controller.h +++ b/src/core/hid/emulated_controller.h | |||
| @@ -139,8 +139,12 @@ public: | |||
| 139 | /// Sets the NpadType for this controller | 139 | /// Sets the NpadType for this controller |
| 140 | void SetNpadType(NpadType npad_type_); | 140 | void SetNpadType(NpadType npad_type_); |
| 141 | 141 | ||
| 142 | /// Gets the NpadType for this controller | 142 | /** |
| 143 | NpadType GetNpadType() const; | 143 | * Gets the NpadType for this controller |
| 144 | * @param Returns the temporary value if true | ||
| 145 | * @return NpadType set on the controller | ||
| 146 | */ | ||
| 147 | NpadType GetNpadType(bool temporary = false) const; | ||
| 144 | 148 | ||
| 145 | /// Sets the connected status to true | 149 | /// Sets the connected status to true |
| 146 | void Connect(); | 150 | void Connect(); |
| @@ -148,8 +152,12 @@ public: | |||
| 148 | /// Sets the connected status to false | 152 | /// Sets the connected status to false |
| 149 | void Disconnect(); | 153 | void Disconnect(); |
| 150 | 154 | ||
| 151 | /// Returns true if the controller has the connected status | 155 | /** |
| 152 | bool IsConnected() const; | 156 | * Is the emulated connected |
| 157 | * @param Returns the temporary value if true | ||
| 158 | * @return true if the controller has the connected status | ||
| 159 | */ | ||
| 160 | bool IsConnected(bool temporary = false) const; | ||
| 153 | 161 | ||
| 154 | /// Returns true if vibration is enabled | 162 | /// Returns true if vibration is enabled |
| 155 | bool IsVibrationEnabled() const; | 163 | bool IsVibrationEnabled() const; |
| @@ -323,7 +331,9 @@ private: | |||
| 323 | 331 | ||
| 324 | NpadIdType npad_id_type; | 332 | NpadIdType npad_id_type; |
| 325 | NpadType npad_type{NpadType::None}; | 333 | NpadType npad_type{NpadType::None}; |
| 334 | NpadType temporary_npad_type{NpadType::None}; | ||
| 326 | bool is_connected{false}; | 335 | bool is_connected{false}; |
| 336 | bool temporary_is_connected{false}; | ||
| 327 | bool is_configuring{false}; | 337 | bool is_configuring{false}; |
| 328 | bool is_vibration_enabled{true}; | 338 | bool is_vibration_enabled{true}; |
| 329 | f32 motion_sensitivity{0.01f}; | 339 | f32 motion_sensitivity{0.01f}; |
diff --git a/src/core/hid/hid_types.h b/src/core/hid/hid_types.h index f12a14cb8..539436283 100644 --- a/src/core/hid/hid_types.h +++ b/src/core/hid/hid_types.h | |||
| @@ -47,9 +47,9 @@ constexpr size_t NpadIdTypeToIndex(NpadIdType npad_id_type) { | |||
| 47 | return 6; | 47 | return 6; |
| 48 | case NpadIdType::Player8: | 48 | case NpadIdType::Player8: |
| 49 | return 7; | 49 | return 7; |
| 50 | case NpadIdType::Other: | ||
| 51 | return 8; | ||
| 52 | case NpadIdType::Handheld: | 50 | case NpadIdType::Handheld: |
| 51 | return 8; | ||
| 52 | case NpadIdType::Other: | ||
| 53 | return 9; | 53 | return 9; |
| 54 | default: | 54 | default: |
| 55 | return 0; | 55 | return 0; |
| @@ -76,9 +76,9 @@ constexpr NpadIdType IndexToNpadIdType(size_t index) { | |||
| 76 | case 7: | 76 | case 7: |
| 77 | return NpadIdType::Player8; | 77 | return NpadIdType::Player8; |
| 78 | case 8: | 78 | case 8: |
| 79 | return NpadIdType::Other; | ||
| 80 | case 9: | ||
| 81 | return NpadIdType::Handheld; | 79 | return NpadIdType::Handheld; |
| 80 | case 9: | ||
| 81 | return NpadIdType::Other; | ||
| 82 | default: | 82 | default: |
| 83 | return NpadIdType::Invalid; | 83 | return NpadIdType::Invalid; |
| 84 | } | 84 | } |