From faf7cf752d17cda63422d80a17f9f671a8474120 Mon Sep 17 00:00:00 2001 From: WaluigiWare64 <68647953+WaluigiWare64@users.noreply.github.com> Date: Fri, 22 Jan 2021 17:35:13 +0000 Subject: [PATCH] Add option to pause emulation when focus is lost Fixes #875 --- .../qt_sdl/InterfaceSettingsDialog.cpp | 3 ++ .../qt_sdl/InterfaceSettingsDialog.ui | 41 +++++++++++-------- src/frontend/qt_sdl/PlatformConfig.cpp | 5 ++- src/frontend/qt_sdl/PlatformConfig.h | 1 + src/frontend/qt_sdl/main.cpp | 18 ++++++++ src/frontend/qt_sdl/main.h | 4 ++ 6 files changed, 54 insertions(+), 18 deletions(-) diff --git a/src/frontend/qt_sdl/InterfaceSettingsDialog.cpp b/src/frontend/qt_sdl/InterfaceSettingsDialog.cpp index 7dcb421a..72bbd46c 100644 --- a/src/frontend/qt_sdl/InterfaceSettingsDialog.cpp +++ b/src/frontend/qt_sdl/InterfaceSettingsDialog.cpp @@ -34,6 +34,8 @@ InterfaceSettingsDialog::InterfaceSettingsDialog(QWidget* parent) : QDialog(pare ui->cbMouseHide->setChecked(Config::MouseHide != 0); ui->spinMouseHideSeconds->setEnabled(Config::MouseHide != 0); ui->spinMouseHideSeconds->setValue(Config::MouseHideSeconds); + ui->cbPauseLostFocus->setChecked(Config::PauseLostFocus != 0); + oldPauseLostFocusVal = ui->cbPauseLostFocus->isChecked() ? 1:0; } InterfaceSettingsDialog::~InterfaceSettingsDialog() @@ -59,6 +61,7 @@ void InterfaceSettingsDialog::done(int r) { Config::MouseHide = ui->cbMouseHide->isChecked() ? 1:0; Config::MouseHideSeconds = ui->spinMouseHideSeconds->value(); + Config::PauseLostFocus = ui->cbPauseLostFocus->isChecked() ? 1:0; Config::Save(); diff --git a/src/frontend/qt_sdl/InterfaceSettingsDialog.ui b/src/frontend/qt_sdl/InterfaceSettingsDialog.ui index 4891da90..abaaf487 100644 --- a/src/frontend/qt_sdl/InterfaceSettingsDialog.ui +++ b/src/frontend/qt_sdl/InterfaceSettingsDialog.ui @@ -6,8 +6,8 @@ 0 0 - 209 - 110 + 262 + 113 @@ -20,31 +20,31 @@ Interface settings - melonDS - - - - Hide mouse after inactivity - - - - + Hide after - - - - - + + - seconds of inactivity + Pause emulation when window is not in focus - + + + + Hide mouse after inactivity + + + + + + + Qt::Horizontal @@ -54,6 +54,13 @@ + + + + seconds of inactivity + + + diff --git a/src/frontend/qt_sdl/PlatformConfig.cpp b/src/frontend/qt_sdl/PlatformConfig.cpp index ac890ee2..0f67b9aa 100644 --- a/src/frontend/qt_sdl/PlatformConfig.cpp +++ b/src/frontend/qt_sdl/PlatformConfig.cpp @@ -80,6 +80,8 @@ int EnableCheats; int MouseHide; int MouseHideSeconds; +int PauseLostFocus; + bool EnableJIT; ConfigEntry PlatformConfigFile[] = @@ -188,8 +190,9 @@ ConfigEntry PlatformConfigFile[] = {"EnableCheats", 0, &EnableCheats, 0, NULL, 0}, - {"MouseHide", 0, &MouseHide, 0, NULL, 0}, + {"MouseHide", 0, &MouseHide, 0, NULL, 0}, {"MouseHideSeconds", 0, &MouseHideSeconds, 5, NULL, 0}, + {"PauseLostFocus", 0, &PauseLostFocus, 0, NULL, 0}, {"", -1, NULL, 0, NULL, 0} }; diff --git a/src/frontend/qt_sdl/PlatformConfig.h b/src/frontend/qt_sdl/PlatformConfig.h index a37411e4..bf5fc60e 100644 --- a/src/frontend/qt_sdl/PlatformConfig.h +++ b/src/frontend/qt_sdl/PlatformConfig.h @@ -94,6 +94,7 @@ extern int EnableCheats; extern int MouseHide; extern int MouseHideSeconds; +extern int PauseLostFocus; } diff --git a/src/frontend/qt_sdl/main.cpp b/src/frontend/qt_sdl/main.cpp index c5ce6928..7c7fe7df 100644 --- a/src/frontend/qt_sdl/main.cpp +++ b/src/frontend/qt_sdl/main.cpp @@ -1534,6 +1534,19 @@ void MainWindow::dropEvent(QDropEvent* event) } } +void MainWindow::onAppStateChanged(Qt::ApplicationState state) +{ + if (state == Qt::ApplicationInactive) + { + if (Config::PauseLostFocus && emuThread->emuIsRunning()) + emuThread->emuPause(); + } + else if (state == Qt::ApplicationActive) + { + if (Config::PauseLostFocus && !pausedManually) + emuThread->emuUnpause(); + } +} QString MainWindow::loadErrorStr(int error) { @@ -2002,11 +2015,13 @@ void MainWindow::onPause(bool checked) { emuThread->emuPause(); OSD::AddMessage(0, "Paused"); + pausedManually = true; } else { emuThread->emuUnpause(); OSD::AddMessage(0, "Resumed"); + pausedManually = false; } } @@ -2152,6 +2167,7 @@ void MainWindow::onOpenInterfaceSettings() InterfaceSettingsDialog* dlg = InterfaceSettingsDialog::openDlg(this); connect(dlg, &InterfaceSettingsDialog::finished, this, &MainWindow::onInterfaceSettingsFinished); connect(dlg, &InterfaceSettingsDialog::updateMouseTimer, this, &MainWindow::onUpdateMouseTimer); + connect(dlg, &InterfaceSettingsDialog::focusUnpauseEmu, emuThread, &EmuThread::emuUnpause); } void MainWindow::onUpdateMouseTimer() @@ -2505,6 +2521,8 @@ int main(int argc, char** argv) emuThread->start(); emuThread->emuPause(); + QObject::connect(&melon, &QApplication::applicationStateChanged, mainWindow, &MainWindow::onAppStateChanged); + if (argc > 1) { char* file = argv[1]; diff --git a/src/frontend/qt_sdl/main.h b/src/frontend/qt_sdl/main.h index 9ab4c2a8..ec7bd74d 100644 --- a/src/frontend/qt_sdl/main.h +++ b/src/frontend/qt_sdl/main.h @@ -184,6 +184,8 @@ public: bool hasOGL; QOpenGLContext* getOGLContext(); + + void onAppStateChanged(Qt::ApplicationState state); protected: void resizeEvent(QResizeEvent* event) override; @@ -262,6 +264,8 @@ private: void createScreenPanel(); QString loadErrorStr(int error); + + bool pausedManually; public: QWidget* panel;