From ab93b4c66de04db92ada60d41975bb8c111a7f87 Mon Sep 17 00:00:00 2001 From: german77 Date: Sun, 6 Feb 2022 20:46:22 -0600 Subject: [PATCH] yuzu: Mute audio when in background --- src/yuzu/configuration/config.cpp | 2 ++ src/yuzu/configuration/configure_general.cpp | 2 ++ src/yuzu/configuration/configure_general.ui | 7 +++++++ src/yuzu/main.cpp | 18 ++++++++++++++---- src/yuzu/main.h | 1 + src/yuzu/uisettings.h | 1 + 6 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/yuzu/configuration/config.cpp b/src/yuzu/configuration/config.cpp index 68facbce46..9ee7992e7f 100644 --- a/src/yuzu/configuration/config.cpp +++ b/src/yuzu/configuration/config.cpp @@ -769,6 +769,7 @@ void Config::ReadUIValues() { ReadBasicSetting(UISettings::values.callout_flags); ReadBasicSetting(UISettings::values.show_console); ReadBasicSetting(UISettings::values.pause_when_in_background); + ReadBasicSetting(UISettings::values.mute_when_in_background); ReadBasicSetting(UISettings::values.hide_mouse); qt_config->endGroup(); @@ -1297,6 +1298,7 @@ void Config::SaveUIValues() { WriteBasicSetting(UISettings::values.callout_flags); WriteBasicSetting(UISettings::values.show_console); WriteBasicSetting(UISettings::values.pause_when_in_background); + WriteBasicSetting(UISettings::values.mute_when_in_background); WriteBasicSetting(UISettings::values.hide_mouse); qt_config->endGroup(); diff --git a/src/yuzu/configuration/configure_general.cpp b/src/yuzu/configuration/configure_general.cpp index 5668793174..978a29fe66 100644 --- a/src/yuzu/configuration/configure_general.cpp +++ b/src/yuzu/configuration/configure_general.cpp @@ -46,6 +46,7 @@ void ConfigureGeneral::SetConfiguration() { ui->toggle_check_exit->setChecked(UISettings::values.confirm_before_closing.GetValue()); ui->toggle_user_on_boot->setChecked(UISettings::values.select_user_on_boot.GetValue()); ui->toggle_background_pause->setChecked(UISettings::values.pause_when_in_background.GetValue()); + ui->toggle_background_mute->setChecked(UISettings::values.mute_when_in_background.GetValue()); ui->toggle_hide_mouse->setChecked(UISettings::values.hide_mouse.GetValue()); ui->toggle_speed_limit->setChecked(Settings::values.use_speed_limit.GetValue()); @@ -95,6 +96,7 @@ void ConfigureGeneral::ApplyConfiguration() { UISettings::values.confirm_before_closing = ui->toggle_check_exit->isChecked(); UISettings::values.select_user_on_boot = ui->toggle_user_on_boot->isChecked(); UISettings::values.pause_when_in_background = ui->toggle_background_pause->isChecked(); + UISettings::values.mute_when_in_background = ui->toggle_background_mute->isChecked(); UISettings::values.hide_mouse = ui->toggle_hide_mouse->isChecked(); Settings::values.fps_cap.SetValue(ui->fps_cap->value()); diff --git a/src/yuzu/configuration/configure_general.ui b/src/yuzu/configuration/configure_general.ui index 112dc72b38..bfc7711357 100644 --- a/src/yuzu/configuration/configure_general.ui +++ b/src/yuzu/configuration/configure_general.ui @@ -163,6 +163,13 @@ + + + + Mute audio when in background + + + diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 68cd9f6eaa..cc5a8f65ef 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -1034,14 +1034,14 @@ void GMainWindow::RestoreUIState() { } void GMainWindow::OnAppFocusStateChanged(Qt::ApplicationState state) { - if (!UISettings::values.pause_when_in_background) { - return; - } if (state != Qt::ApplicationHidden && state != Qt::ApplicationInactive && state != Qt::ApplicationActive) { LOG_DEBUG(Frontend, "ApplicationState unusual flag: {} ", state); } - if (emulation_running) { + if (!emulation_running) { + return; + } + if (UISettings::values.pause_when_in_background) { if (emu_thread->IsRunning() && (state & (Qt::ApplicationHidden | Qt::ApplicationInactive))) { auto_paused = true; @@ -1051,6 +1051,16 @@ void GMainWindow::OnAppFocusStateChanged(Qt::ApplicationState state) { OnStartGame(); } } + if (UISettings::values.mute_when_in_background) { + if (!Settings::values.audio_muted && + (state & (Qt::ApplicationHidden | Qt::ApplicationInactive))) { + Settings::values.audio_muted = true; + auto_muted = true; + } else if (auto_muted && state == Qt::ApplicationActive) { + Settings::values.audio_muted = false; + auto_muted = false; + } + } } void GMainWindow::ConnectWidgetEvents() { diff --git a/src/yuzu/main.h b/src/yuzu/main.h index eb939bb29c..6a35b9e3d9 100644 --- a/src/yuzu/main.h +++ b/src/yuzu/main.h @@ -372,6 +372,7 @@ private: QString game_path; bool auto_paused = false; + bool auto_muted = false; QTimer mouse_hide_timer; // FS diff --git a/src/yuzu/uisettings.h b/src/yuzu/uisettings.h index f7298ddadc..06e8b46daf 100644 --- a/src/yuzu/uisettings.h +++ b/src/yuzu/uisettings.h @@ -73,6 +73,7 @@ struct Values { Settings::BasicSetting confirm_before_closing{true, "confirmClose"}; Settings::BasicSetting first_start{true, "firstStart"}; Settings::BasicSetting pause_when_in_background{false, "pauseWhenInBackground"}; + Settings::BasicSetting mute_when_in_background{false, "muteWhenInBackground"}; Settings::BasicSetting hide_mouse{true, "hideInactiveMouse"}; Settings::BasicSetting select_user_on_boot{false, "select_user_on_boot"};