diff options
| author | 2021-11-02 22:50:30 -0600 | |
|---|---|---|
| committer | 2021-11-24 20:30:27 -0600 | |
| commit | 157e0b85fdd805e02d234dccf1ce578e3159adee (patch) | |
| tree | 3916ae9628d53df0cfff3667ecbfb4dbd94f8e0b /src/core/hid/emulated_controller.cpp | |
| parent | core/hid: Fully emulate motion from button (diff) | |
| download | yuzu-157e0b85fdd805e02d234dccf1ce578e3159adee.tar.gz yuzu-157e0b85fdd805e02d234dccf1ce578e3159adee.tar.xz yuzu-157e0b85fdd805e02d234dccf1ce578e3159adee.zip | |
core/hid: Prevent Emulated controller from flapping with multiple inputs devices
Diffstat (limited to 'src/core/hid/emulated_controller.cpp')
| -rw-r--r-- | src/core/hid/emulated_controller.cpp | 68 |
1 files changed, 58 insertions, 10 deletions
diff --git a/src/core/hid/emulated_controller.cpp b/src/core/hid/emulated_controller.cpp index 2db2b4da0..6fe3744fd 100644 --- a/src/core/hid/emulated_controller.cpp +++ b/src/core/hid/emulated_controller.cpp | |||
| @@ -183,8 +183,11 @@ void EmulatedController::ReloadInput() { | |||
| 183 | if (!button_devices[index]) { | 183 | if (!button_devices[index]) { |
| 184 | continue; | 184 | continue; |
| 185 | } | 185 | } |
| 186 | const auto uuid = Common::UUID{button_params[index].Get("guid", "")}; | ||
| 186 | Common::Input::InputCallback button_callback{ | 187 | Common::Input::InputCallback button_callback{ |
| 187 | [this, index](Common::Input::CallbackStatus callback) { SetButton(callback, index); }}; | 188 | [this, index, uuid](Common::Input::CallbackStatus callback) { |
| 189 | SetButton(callback, index, uuid); | ||
| 190 | }}; | ||
| 188 | button_devices[index]->SetCallback(button_callback); | 191 | button_devices[index]->SetCallback(button_callback); |
| 189 | button_devices[index]->ForceUpdate(); | 192 | button_devices[index]->ForceUpdate(); |
| 190 | } | 193 | } |
| @@ -193,8 +196,11 @@ void EmulatedController::ReloadInput() { | |||
| 193 | if (!stick_devices[index]) { | 196 | if (!stick_devices[index]) { |
| 194 | continue; | 197 | continue; |
| 195 | } | 198 | } |
| 199 | const auto uuid = Common::UUID{stick_params[index].Get("guid", "")}; | ||
| 196 | Common::Input::InputCallback stick_callback{ | 200 | Common::Input::InputCallback stick_callback{ |
| 197 | [this, index](Common::Input::CallbackStatus callback) { SetStick(callback, index); }}; | 201 | [this, index, uuid](Common::Input::CallbackStatus callback) { |
| 202 | SetStick(callback, index, uuid); | ||
| 203 | }}; | ||
| 198 | stick_devices[index]->SetCallback(stick_callback); | 204 | stick_devices[index]->SetCallback(stick_callback); |
| 199 | stick_devices[index]->ForceUpdate(); | 205 | stick_devices[index]->ForceUpdate(); |
| 200 | } | 206 | } |
| @@ -203,8 +209,11 @@ void EmulatedController::ReloadInput() { | |||
| 203 | if (!trigger_devices[index]) { | 209 | if (!trigger_devices[index]) { |
| 204 | continue; | 210 | continue; |
| 205 | } | 211 | } |
| 212 | const auto uuid = Common::UUID{trigger_params[index].Get("guid", "")}; | ||
| 206 | Common::Input::InputCallback trigger_callback{ | 213 | Common::Input::InputCallback trigger_callback{ |
| 207 | [this, index](Common::Input::CallbackStatus callback) { SetTrigger(callback, index); }}; | 214 | [this, index, uuid](Common::Input::CallbackStatus callback) { |
| 215 | SetTrigger(callback, index, uuid); | ||
| 216 | }}; | ||
| 208 | trigger_devices[index]->SetCallback(trigger_callback); | 217 | trigger_devices[index]->SetCallback(trigger_callback); |
| 209 | trigger_devices[index]->ForceUpdate(); | 218 | trigger_devices[index]->ForceUpdate(); |
| 210 | } | 219 | } |
| @@ -229,13 +238,18 @@ void EmulatedController::ReloadInput() { | |||
| 229 | motion_devices[index]->ForceUpdate(); | 238 | motion_devices[index]->ForceUpdate(); |
| 230 | } | 239 | } |
| 231 | 240 | ||
| 241 | // Use a common UUID for TAS | ||
| 242 | const auto tas_uuid = Common::UUID{0x0, 0x7A5}; | ||
| 243 | |||
| 232 | // Register TAS devices. No need to force update | 244 | // Register TAS devices. No need to force update |
| 233 | for (std::size_t index = 0; index < tas_button_devices.size(); ++index) { | 245 | for (std::size_t index = 0; index < tas_button_devices.size(); ++index) { |
| 234 | if (!tas_button_devices[index]) { | 246 | if (!tas_button_devices[index]) { |
| 235 | continue; | 247 | continue; |
| 236 | } | 248 | } |
| 237 | Common::Input::InputCallback button_callback{ | 249 | Common::Input::InputCallback button_callback{ |
| 238 | [this, index](Common::Input::CallbackStatus callback) { SetButton(callback, index); }}; | 250 | [this, index, tas_uuid](Common::Input::CallbackStatus callback) { |
| 251 | SetButton(callback, index, tas_uuid); | ||
| 252 | }}; | ||
| 239 | tas_button_devices[index]->SetCallback(button_callback); | 253 | tas_button_devices[index]->SetCallback(button_callback); |
| 240 | } | 254 | } |
| 241 | 255 | ||
| @@ -244,7 +258,9 @@ void EmulatedController::ReloadInput() { | |||
| 244 | continue; | 258 | continue; |
| 245 | } | 259 | } |
| 246 | Common::Input::InputCallback stick_callback{ | 260 | Common::Input::InputCallback stick_callback{ |
| 247 | [this, index](Common::Input::CallbackStatus callback) { SetStick(callback, index); }}; | 261 | [this, index, tas_uuid](Common::Input::CallbackStatus callback) { |
| 262 | SetStick(callback, index, tas_uuid); | ||
| 263 | }}; | ||
| 248 | tas_stick_devices[index]->SetCallback(stick_callback); | 264 | tas_stick_devices[index]->SetCallback(stick_callback); |
| 249 | } | 265 | } |
| 250 | } | 266 | } |
| @@ -423,7 +439,8 @@ void EmulatedController::SetMotionParam(std::size_t index, Common::ParamPackage | |||
| 423 | ReloadInput(); | 439 | ReloadInput(); |
| 424 | } | 440 | } |
| 425 | 441 | ||
| 426 | void EmulatedController::SetButton(Common::Input::CallbackStatus callback, std::size_t index) { | 442 | void EmulatedController::SetButton(Common::Input::CallbackStatus callback, std::size_t index, |
| 443 | Common::UUID uuid) { | ||
| 427 | if (index >= controller.button_values.size()) { | 444 | if (index >= controller.button_values.size()) { |
| 428 | return; | 445 | return; |
| 429 | } | 446 | } |
| @@ -432,7 +449,16 @@ void EmulatedController::SetButton(Common::Input::CallbackStatus callback, std:: | |||
| 432 | bool value_changed = false; | 449 | bool value_changed = false; |
| 433 | const auto new_status = TransformToButton(callback); | 450 | const auto new_status = TransformToButton(callback); |
| 434 | auto& current_status = controller.button_values[index]; | 451 | auto& current_status = controller.button_values[index]; |
| 452 | |||
| 453 | // Only read button values that have the same uuid or are pressed once | ||
| 454 | if (current_status.uuid != uuid) { | ||
| 455 | if (!new_status.value) { | ||
| 456 | return; | ||
| 457 | } | ||
| 458 | } | ||
| 459 | |||
| 435 | current_status.toggle = new_status.toggle; | 460 | current_status.toggle = new_status.toggle; |
| 461 | current_status.uuid = uuid; | ||
| 436 | 462 | ||
| 437 | // Update button status with current | 463 | // Update button status with current |
| 438 | if (!current_status.toggle) { | 464 | if (!current_status.toggle) { |
| @@ -553,12 +579,23 @@ void EmulatedController::SetButton(Common::Input::CallbackStatus callback, std:: | |||
| 553 | TriggerOnChange(ControllerTriggerType::Button, true); | 579 | TriggerOnChange(ControllerTriggerType::Button, true); |
| 554 | } | 580 | } |
| 555 | 581 | ||
| 556 | void EmulatedController::SetStick(Common::Input::CallbackStatus callback, std::size_t index) { | 582 | void EmulatedController::SetStick(Common::Input::CallbackStatus callback, std::size_t index, |
| 583 | Common::UUID uuid) { | ||
| 557 | if (index >= controller.stick_values.size()) { | 584 | if (index >= controller.stick_values.size()) { |
| 558 | return; | 585 | return; |
| 559 | } | 586 | } |
| 560 | std::lock_guard lock{mutex}; | 587 | std::lock_guard lock{mutex}; |
| 561 | controller.stick_values[index] = TransformToStick(callback); | 588 | const auto stick_value = TransformToStick(callback); |
| 589 | |||
| 590 | // Only read stick values that have the same uuid or are over the threshold to avoid flapping | ||
| 591 | if (controller.stick_values[index].uuid != uuid) { | ||
| 592 | if (!stick_value.down && !stick_value.up && !stick_value.left && !stick_value.right) { | ||
| 593 | return; | ||
| 594 | } | ||
| 595 | } | ||
| 596 | |||
| 597 | controller.stick_values[index] = stick_value; | ||
| 598 | controller.stick_values[index].uuid = uuid; | ||
| 562 | 599 | ||
| 563 | if (is_configuring) { | 600 | if (is_configuring) { |
| 564 | controller.analog_stick_state.left = {}; | 601 | controller.analog_stick_state.left = {}; |
| @@ -592,12 +629,23 @@ void EmulatedController::SetStick(Common::Input::CallbackStatus callback, std::s | |||
| 592 | TriggerOnChange(ControllerTriggerType::Stick, true); | 629 | TriggerOnChange(ControllerTriggerType::Stick, true); |
| 593 | } | 630 | } |
| 594 | 631 | ||
| 595 | void EmulatedController::SetTrigger(Common::Input::CallbackStatus callback, std::size_t index) { | 632 | void EmulatedController::SetTrigger(Common::Input::CallbackStatus callback, std::size_t index, |
| 633 | Common::UUID uuid) { | ||
| 596 | if (index >= controller.trigger_values.size()) { | 634 | if (index >= controller.trigger_values.size()) { |
| 597 | return; | 635 | return; |
| 598 | } | 636 | } |
| 599 | std::lock_guard lock{mutex}; | 637 | std::lock_guard lock{mutex}; |
| 600 | controller.trigger_values[index] = TransformToTrigger(callback); | 638 | const auto trigger_value = TransformToTrigger(callback); |
| 639 | |||
| 640 | // Only read trigger values that have the same uuid or are pressed once | ||
| 641 | if (controller.stick_values[index].uuid != uuid) { | ||
| 642 | if (!trigger_value.pressed.value) { | ||
| 643 | return; | ||
| 644 | } | ||
| 645 | } | ||
| 646 | |||
| 647 | controller.trigger_values[index] = trigger_value; | ||
| 648 | controller.trigger_values[index].uuid = uuid; | ||
| 601 | 649 | ||
| 602 | if (is_configuring) { | 650 | if (is_configuring) { |
| 603 | controller.gc_trigger_state.left = 0; | 651 | controller.gc_trigger_state.left = 0; |