diff --git a/pcsx2-qt/DisplayWidget.cpp b/pcsx2-qt/DisplayWidget.cpp index 5adc9407a5..0a6523212c 100644 --- a/pcsx2-qt/DisplayWidget.cpp +++ b/pcsx2-qt/DisplayWidget.cpp @@ -397,22 +397,6 @@ bool DisplayWidget::event(QEvent* event) return true; } - case QEvent::FocusIn: - { - QWidget::event(event); - emit windowFocusEvent(); - return true; - } - - case QEvent::ActivationChange: - { - QWidget::event(event); - if (isActiveWindow()) - emit windowFocusEvent(); - - return true; - } - default: return QWidget::event(event); } @@ -477,19 +461,6 @@ bool DisplayContainer::event(QEvent* event) } break; - case QEvent::FocusIn: - { - emit m_display_widget->windowFocusEvent(); - } - break; - - case QEvent::ActivationChange: - { - if (isActiveWindow()) - emit m_display_widget->windowFocusEvent(); - } - break; - default: break; } diff --git a/pcsx2-qt/DisplayWidget.h b/pcsx2-qt/DisplayWidget.h index 69ee0ecfcc..45d7f2c008 100644 --- a/pcsx2-qt/DisplayWidget.h +++ b/pcsx2-qt/DisplayWidget.h @@ -42,7 +42,6 @@ public: void updateCursor(bool master_enable); Q_SIGNALS: - void windowFocusEvent(); void windowResizedEvent(int width, int height, float scale); void windowRestoredEvent(); diff --git a/pcsx2-qt/EmuThread.cpp b/pcsx2-qt/EmuThread.cpp index 43247b6e4b..ad52de4717 100644 --- a/pcsx2-qt/EmuThread.cpp +++ b/pcsx2-qt/EmuThread.cpp @@ -247,6 +247,7 @@ void EmuThread::run() reloadInputSources(); createBackgroundControllerPollTimer(); startBackgroundControllerPollTimer(); + connectSignals(); while (!m_shutdown_flag.load()) { @@ -277,6 +278,7 @@ void EmuThread::destroyVM() m_last_video_fps = 0.0f; m_last_internal_width = 0; m_last_internal_height = 0; + m_was_paused_by_focus_loss = false; VMManager::Shutdown(m_save_state_on_shutdown); } @@ -436,6 +438,12 @@ void EmuThread::updateEmuFolders() void EmuThread::loadOurSettings() { m_verbose_status = Host::GetBaseBoolSettingValue("UI", "VerboseStatusBar", false); + m_pause_on_focus_loss = Host::GetBaseBoolSettingValue("UI", "PauseOnFocusLoss", false); +} + +void EmuThread::connectSignals() +{ + connect(qApp, &QGuiApplication::applicationStateChanged, this, &EmuThread::onApplicationStateChanged); } void EmuThread::checkForSettingChanges() @@ -605,9 +613,8 @@ void EmuThread::connectDisplaySignals(DisplayWidget* widget) { widget->disconnect(this); - connect(widget, &DisplayWidget::windowFocusEvent, this, &EmuThread::onDisplayWindowFocused); connect(widget, &DisplayWidget::windowResizedEvent, this, &EmuThread::onDisplayWindowResized); - // connect(widget, &DisplayWidget::windowRestoredEvent, this, &EmuThread::redrawDisplayWindow); + connect(widget, &DisplayWidget::windowRestoredEvent, this, &EmuThread::redrawDisplayWindow); } void EmuThread::onDisplayWindowResized(int width, int height, float scale) @@ -618,7 +625,31 @@ void EmuThread::onDisplayWindowResized(int width, int height, float scale) GetMTGS().ResizeDisplayWindow(width, height, scale); } -void EmuThread::onDisplayWindowFocused() {} +void EmuThread::onApplicationStateChanged(Qt::ApplicationState state) +{ + // NOTE: This is executed on the emu thread, not UI thread. + if (!m_pause_on_focus_loss || !VMManager::HasValidVM()) + return; + + const bool focus_loss = (state != Qt::ApplicationActive); + if (focus_loss) + { + if (!m_was_paused_by_focus_loss && VMManager::GetState() == VMState::Running) + { + m_was_paused_by_focus_loss = true; + VMManager::SetPaused(true); + } + } + else + { + if (m_was_paused_by_focus_loss) + { + m_was_paused_by_focus_loss = false; + if (VMManager::GetState() == VMState::Paused) + VMManager::SetPaused(false); + } + } +} void EmuThread::redrawDisplayWindow() { diff --git a/pcsx2-qt/EmuThread.h b/pcsx2-qt/EmuThread.h index bd0ee0dcd1..602000ac92 100644 --- a/pcsx2-qt/EmuThread.h +++ b/pcsx2-qt/EmuThread.h @@ -140,12 +140,13 @@ private: void createBackgroundControllerPollTimer(); void destroyBackgroundControllerPollTimer(); void loadOurSettings(); + void connectSignals(); private Q_SLOTS: void stopInThread(); void doBackgroundControllerPoll(); void onDisplayWindowResized(int width, int height, float scale); - void onDisplayWindowFocused(); + void onApplicationStateChanged(Qt::ApplicationState state); void redrawDisplayWindow(); private: @@ -161,6 +162,9 @@ private: bool m_is_fullscreen = false; bool m_is_surfaceless = false; bool m_save_state_on_shutdown = false; + bool m_pause_on_focus_loss = false; + + bool m_was_paused_by_focus_loss = false; float m_last_speed = 0.0f; float m_last_game_fps = 0.0f; diff --git a/pcsx2-qt/Settings/InterfaceSettingsWidget.cpp b/pcsx2-qt/Settings/InterfaceSettingsWidget.cpp index b78b14c5d2..478b66c562 100644 --- a/pcsx2-qt/Settings/InterfaceSettingsWidget.cpp +++ b/pcsx2-qt/Settings/InterfaceSettingsWidget.cpp @@ -117,7 +117,6 @@ InterfaceSettingsWidget::InterfaceSettingsWidget(SettingsDialog* dialog, QWidget tr("Hides the main window (with the game list) when a game is running, requires Render To Separate Window to be enabled.")); // Not yet used, disable the options - m_ui.pauseOnFocusLoss->setDisabled(true); m_ui.language->setDisabled(true); onRenderToSeparateWindowChanged();