From ac48e059bc3b00b83ba02a527869f105d4a6bfef Mon Sep 17 00:00:00 2001
From: german77 <juangerman-13@hotmail.com>
Date: Sun, 30 May 2021 10:57:20 -0500
Subject: [PATCH] settings: Disable controller preview if controller is not
 active

---
 .../configure_input_player_widget.cpp         | 19 +++++++++++++++++++
 .../configure_input_player_widget.h           |  2 ++
 src/yuzu/debugger/controller.cpp              |  6 ++++--
 3 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/src/yuzu/configuration/configure_input_player_widget.cpp b/src/yuzu/configuration/configure_input_player_widget.cpp
index 61ba91cefb..f50cda2f3c 100644
--- a/src/yuzu/configuration/configure_input_player_widget.cpp
+++ b/src/yuzu/configuration/configure_input_player_widget.cpp
@@ -85,6 +85,8 @@ void PlayerControlPreview::SetConnectedStatus(bool checked) {
     led_color[1] = led_pattern.position2 ? colors.led_on : colors.led_off;
     led_color[2] = led_pattern.position3 ? colors.led_on : colors.led_off;
     led_color[3] = led_pattern.position4 ? colors.led_on : colors.led_off;
+    is_enabled = checked;
+    ResetInputs();
 }
 
 void PlayerControlPreview::SetControllerType(const Settings::ControllerType type) {
@@ -108,6 +110,7 @@ void PlayerControlPreview::EndMapping() {
     analog_mapping_index = Settings::NativeAnalog::NumAnalogs;
     mapping_active = false;
     blink_counter = 0;
+    ResetInputs();
 }
 
 void PlayerControlPreview::UpdateColors() {
@@ -156,7 +159,23 @@ void PlayerControlPreview::UpdateColors() {
     // colors.right = QColor(Settings::values.players.GetValue()[player_index].body_color_right);
 }
 
+void PlayerControlPreview::ResetInputs() {
+    for (std::size_t index = 0; index < button_values.size(); ++index) {
+        button_values[index] = false;
+    }
+
+    for (std::size_t index = 0; index < axis_values.size(); ++index) {
+        axis_values[index].properties = {0, 1, 0};
+        axis_values[index].value = {0, 0};
+        axis_values[index].raw_value = {0, 0};
+    }
+    update();
+}
+
 void PlayerControlPreview::UpdateInput() {
+    if (!is_enabled && !mapping_active) {
+        return;
+    }
     bool input_changed = false;
     const auto& button_state = buttons;
     for (std::size_t index = 0; index < button_values.size(); ++index) {
diff --git a/src/yuzu/configuration/configure_input_player_widget.h b/src/yuzu/configuration/configure_input_player_widget.h
index 51bb84eb67..5fc16d8af0 100644
--- a/src/yuzu/configuration/configure_input_player_widget.h
+++ b/src/yuzu/configuration/configure_input_player_widget.h
@@ -100,6 +100,7 @@ private:
 
     static LedPattern GetColorPattern(std::size_t index, bool player_on);
     void UpdateColors();
+    void ResetInputs();
 
     // Draw controller functions
     void DrawHandheldController(QPainter& p, QPointF center);
@@ -176,6 +177,7 @@ private:
     using StickArray =
         std::array<std::unique_ptr<Input::AnalogDevice>, Settings::NativeAnalog::NUM_STICKS_HID>;
 
+    bool is_enabled{};
     bool mapping_active{};
     int blink_counter{};
     QColor button_color{};
diff --git a/src/yuzu/debugger/controller.cpp b/src/yuzu/debugger/controller.cpp
index d85408ac6d..c1fc695782 100644
--- a/src/yuzu/debugger/controller.cpp
+++ b/src/yuzu/debugger/controller.cpp
@@ -28,6 +28,7 @@ ControllerDialog::ControllerDialog(QWidget* parent) : QWidget(parent, Qt::Dialog
     // Configure focus so that widget is focusable and the dialog automatically forwards focus to
     // it.
     setFocusProxy(widget);
+    widget->SetConnectedStatus(false);
     widget->setFocusPolicy(Qt::StrongFocus);
     widget->setFocus();
 }
@@ -36,9 +37,8 @@ void ControllerDialog::refreshConfiguration() {
     const auto& players = Settings::values.players.GetValue();
     constexpr std::size_t player = 0;
     widget->SetPlayerInputRaw(player, players[player].buttons, players[player].analogs);
-    widget->SetConnectedStatus(players[player].connected);
     widget->SetControllerType(players[player].controller_type);
-    widget->repaint();
+    widget->SetConnectedStatus(players[player].connected);
 }
 
 QAction* ControllerDialog::toggleViewAction() {
@@ -56,6 +56,7 @@ void ControllerDialog::showEvent(QShowEvent* ev) {
     if (toggle_view_action) {
         toggle_view_action->setChecked(isVisible());
     }
+    refreshConfiguration();
     QWidget::showEvent(ev);
 }
 
@@ -63,5 +64,6 @@ void ControllerDialog::hideEvent(QHideEvent* ev) {
     if (toggle_view_action) {
         toggle_view_action->setChecked(isVisible());
     }
+    widget->SetConnectedStatus(false);
     QWidget::hideEvent(ev);
 }