Qt: Add option to pause on application focus loss

This commit is contained in:
Connor McLaughlin 2020-12-05 00:16:22 +10:00
parent 2899e2d73c
commit af6b0dc189
7 changed files with 53 additions and 10 deletions

View file

@ -417,6 +417,8 @@ void HostInterface::SetDefaultSettings(SettingsInterface& si)
si.SetFloatValue("Main", "FastForwardSpeed", 0.0f); si.SetFloatValue("Main", "FastForwardSpeed", 0.0f);
si.SetBoolValue("Main", "IncreaseTimerResolution", true); si.SetBoolValue("Main", "IncreaseTimerResolution", true);
si.SetBoolValue("Main", "StartPaused", false); si.SetBoolValue("Main", "StartPaused", false);
si.SetBoolValue("Main", "StartFullscreen", false);
si.SetBoolValue("Main", "PauseOnFocusLoss", false);
si.SetBoolValue("Main", "SaveStateOnExit", true); si.SetBoolValue("Main", "SaveStateOnExit", true);
si.SetBoolValue("Main", "ConfirmPowerOff", true); si.SetBoolValue("Main", "ConfirmPowerOff", true);
si.SetBoolValue("Main", "LoadDevicesFromSaveStates", false); si.SetBoolValue("Main", "LoadDevicesFromSaveStates", false);

View file

@ -114,6 +114,7 @@ void Settings::Load(SettingsInterface& si)
increase_timer_resolution = si.GetBoolValue("Main", "IncreaseTimerResolution", true); increase_timer_resolution = si.GetBoolValue("Main", "IncreaseTimerResolution", true);
start_paused = si.GetBoolValue("Main", "StartPaused", false); start_paused = si.GetBoolValue("Main", "StartPaused", false);
start_fullscreen = si.GetBoolValue("Main", "StartFullscreen", false); start_fullscreen = si.GetBoolValue("Main", "StartFullscreen", false);
pause_on_focus_loss = si.GetBoolValue("Main", "PauseOnFocusLoss", false);
save_state_on_exit = si.GetBoolValue("Main", "SaveStateOnExit", true); save_state_on_exit = si.GetBoolValue("Main", "SaveStateOnExit", true);
confim_power_off = si.GetBoolValue("Main", "ConfirmPowerOff", true); confim_power_off = si.GetBoolValue("Main", "ConfirmPowerOff", true);
load_devices_from_save_states = si.GetBoolValue("Main", "LoadDevicesFromSaveStates", false); load_devices_from_save_states = si.GetBoolValue("Main", "LoadDevicesFromSaveStates", false);
@ -259,6 +260,7 @@ void Settings::Save(SettingsInterface& si) const
si.SetBoolValue("Main", "IncreaseTimerResolution", increase_timer_resolution); si.SetBoolValue("Main", "IncreaseTimerResolution", increase_timer_resolution);
si.SetBoolValue("Main", "StartPaused", start_paused); si.SetBoolValue("Main", "StartPaused", start_paused);
si.SetBoolValue("Main", "StartFullscreen", start_fullscreen); si.SetBoolValue("Main", "StartFullscreen", start_fullscreen);
si.SetBoolValue("Main", "PauseOnFocusLoss", pause_on_focus_loss);
si.SetBoolValue("Main", "SaveStateOnExit", save_state_on_exit); si.SetBoolValue("Main", "SaveStateOnExit", save_state_on_exit);
si.SetBoolValue("Main", "ConfirmPowerOff", confim_power_off); si.SetBoolValue("Main", "ConfirmPowerOff", confim_power_off);
si.SetBoolValue("Main", "LoadDevicesFromSaveStates", load_devices_from_save_states); si.SetBoolValue("Main", "LoadDevicesFromSaveStates", load_devices_from_save_states);

View file

@ -83,6 +83,7 @@ struct Settings
bool increase_timer_resolution = true; bool increase_timer_resolution = true;
bool start_paused = false; bool start_paused = false;
bool start_fullscreen = false; bool start_fullscreen = false;
bool pause_on_focus_loss = false;
bool save_state_on_exit = true; bool save_state_on_exit = true;
bool confim_power_off = true; bool confim_power_off = true;
bool load_devices_from_save_states = false; bool load_devices_from_save_states = false;

View file

@ -17,6 +17,8 @@ GeneralSettingsWidget::GeneralSettingsWidget(QtHostInterface* host_interface, QW
} }
SettingWidgetBinder::BindWidgetToBoolSetting(m_host_interface, m_ui.pauseOnStart, "Main", "StartPaused", false); SettingWidgetBinder::BindWidgetToBoolSetting(m_host_interface, m_ui.pauseOnStart, "Main", "StartPaused", false);
SettingWidgetBinder::BindWidgetToBoolSetting(m_host_interface, m_ui.pauseOnFocusLoss, "Main", "PauseOnFocusLoss",
false);
SettingWidgetBinder::BindWidgetToBoolSetting(m_host_interface, m_ui.startFullscreen, "Main", "StartFullscreen", SettingWidgetBinder::BindWidgetToBoolSetting(m_host_interface, m_ui.startFullscreen, "Main", "StartFullscreen",
false); false);
SettingWidgetBinder::BindWidgetToBoolSetting(m_host_interface, m_ui.renderToMain, "Main", "RenderToMainWindow", true); SettingWidgetBinder::BindWidgetToBoolSetting(m_host_interface, m_ui.renderToMain, "Main", "RenderToMainWindow", true);
@ -87,7 +89,7 @@ GeneralSettingsWidget::GeneralSettingsWidget(QtHostInterface* host_interface, QW
"to use XInput over SDL2 for compatibility.")); "to use XInput over SDL2 for compatibility."));
// Since this one is compile-time selected, we don't put it in the .ui file. // Since this one is compile-time selected, we don't put it in the .ui file.
int current_col = 0; int current_col = 1;
int current_row = m_ui.formLayout_4->rowCount() - current_col; int current_row = m_ui.formLayout_4->rowCount() - current_col;
#ifdef WITH_DISCORD_PRESENCE #ifdef WITH_DISCORD_PRESENCE
{ {

View file

@ -32,7 +32,7 @@
<string>Behaviour</string> <string>Behaviour</string>
</property> </property>
<layout class="QGridLayout" name="formLayout_4"> <layout class="QGridLayout" name="formLayout_4">
<item row="0" column="1"> <item row="1" column="0">
<widget class="QCheckBox" name="confirmPowerOff"> <widget class="QCheckBox" name="confirmPowerOff">
<property name="text"> <property name="text">
<string>Confirm Power Off</string> <string>Confirm Power Off</string>
@ -53,6 +53,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QCheckBox" name="pauseOnFocusLoss">
<property name="text">
<string>Pause On Focus Loss</string>
</property>
</widget>
</item>
<item row="2" column="0"> <item row="2" column="0">
<widget class="QCheckBox" name="startFullscreen"> <widget class="QCheckBox" name="startFullscreen">
<property name="text"> <property name="text">
@ -60,20 +67,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="0"> <item row="1" column="1">
<widget class="QCheckBox" name="saveStateOnExit"> <widget class="QCheckBox" name="saveStateOnExit">
<property name="text"> <property name="text">
<string>Save State On Exit</string> <string>Save State On Exit</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1">
<widget class="QCheckBox" name="loadDevicesFromSaveStates">
<property name="text">
<string>Load Devices From Save States</string>
</property>
</widget>
</item>
<item row="3" column="0"> <item row="3" column="0">
<widget class="QCheckBox" name="applyGameSettings"> <widget class="QCheckBox" name="applyGameSettings">
<property name="text"> <property name="text">
@ -88,6 +88,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="4" column="0">
<widget class="QCheckBox" name="loadDevicesFromSaveStates">
<property name="text">
<string>Load Devices From Save States</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>

View file

@ -342,6 +342,31 @@ void MainWindow::onRunningGameChanged(const QString& filename, const QString& ga
m_display_widget->setWindowTitle(windowTitle()); m_display_widget->setWindowTitle(windowTitle());
} }
void MainWindow::onApplicationStateChanged(Qt::ApplicationState state)
{
if (!m_emulation_running || !g_settings.pause_on_focus_loss)
return;
const bool focus_loss = (state != Qt::ApplicationActive);
if (focus_loss)
{
if (!m_was_paused_by_focus_loss && !System::IsPaused())
{
m_host_interface->pauseSystem(true);
m_was_paused_by_focus_loss = true;
}
}
else
{
if (m_was_paused_by_focus_loss)
{
if (System::IsPaused())
m_host_interface->pauseSystem(false);
m_was_paused_by_focus_loss = false;
}
}
}
void MainWindow::onStartDiscActionTriggered() void MainWindow::onStartDiscActionTriggered()
{ {
QString filename = QString filename =
@ -796,6 +821,8 @@ void MainWindow::connectSignals()
updateEmulationActions(false, false); updateEmulationActions(false, false);
onEmulationPaused(false); onEmulationPaused(false);
connect(qApp, &QGuiApplication::applicationStateChanged, this, &MainWindow::onApplicationStateChanged);
connect(m_ui.actionStartDisc, &QAction::triggered, this, &MainWindow::onStartDiscActionTriggered); connect(m_ui.actionStartDisc, &QAction::triggered, this, &MainWindow::onStartDiscActionTriggered);
connect(m_ui.actionStartBios, &QAction::triggered, this, &MainWindow::onStartBIOSActionTriggered); connect(m_ui.actionStartBios, &QAction::triggered, this, &MainWindow::onStartBIOSActionTriggered);
connect(m_ui.actionResumeLastState, &QAction::triggered, m_host_interface, connect(m_ui.actionResumeLastState, &QAction::triggered, m_host_interface,

View file

@ -60,6 +60,7 @@ private Q_SLOTS:
void onSystemPerformanceCountersUpdated(float speed, float fps, float vps, float average_frame_time, void onSystemPerformanceCountersUpdated(float speed, float fps, float vps, float average_frame_time,
float worst_frame_time); float worst_frame_time);
void onRunningGameChanged(const QString& filename, const QString& game_code, const QString& game_title); void onRunningGameChanged(const QString& filename, const QString& game_code, const QString& game_title);
void onApplicationStateChanged(Qt::ApplicationState state);
void onStartDiscActionTriggered(); void onStartDiscActionTriggered();
void onStartBIOSActionTriggered(); void onStartBIOSActionTriggered();
@ -137,4 +138,5 @@ private:
CheatManagerDialog* m_cheat_manager_dialog = nullptr; CheatManagerDialog* m_cheat_manager_dialog = nullptr;
bool m_emulation_running = false; bool m_emulation_running = false;
bool m_was_paused_by_focus_loss = false;
}; };