Skip to content

Commit f569448

Browse files
committed
System: Centralize pause checks
1 parent fbca692 commit f569448

File tree

5 files changed

+76
-21
lines changed

5 files changed

+76
-21
lines changed

src/core/hotkeys.cpp

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,6 @@ static void HotkeyToggleOSD()
141141

142142
#ifndef __ANDROID__
143143

144-
static bool CanPause()
145-
{
146-
const u32 frames_until_pause_allowed = Achievements::GetPauseThrottleFrames();
147-
if (frames_until_pause_allowed == 0)
148-
return true;
149-
150-
const float seconds = static_cast<float>(frames_until_pause_allowed) / System::GetVideoFrameRate();
151-
Host::AddIconOSDMessage("PauseCooldown", ICON_FA_CLOCK,
152-
TRANSLATE_PLURAL_STR("Hotkeys", "You cannot pause until another %n second(s) have passed.",
153-
"", static_cast<int>(std::ceil(seconds))),
154-
std::max(seconds, Host::OSD_QUICK_DURATION));
155-
return false;
156-
}
157-
158144
#define DEFINE_NON_ANDROID_HOTKEY(name, category, display_name, handler) \
159145
DEFINE_HOTKEY(name, category, display_name, handler)
160146

@@ -168,25 +154,25 @@ BEGIN_HOTKEY_LIST(g_common_hotkeys)
168154

169155
DEFINE_NON_ANDROID_HOTKEY("OpenPauseMenu", TRANSLATE_NOOP("Hotkeys", "Interface"),
170156
TRANSLATE_NOOP("Hotkeys", "Open Pause Menu"), [](s32 pressed) {
171-
if (!pressed && CanPause())
157+
if (!pressed && System::CanPauseSystem(true))
172158
FullscreenUI::OpenPauseMenu();
173159
})
174160

175161
DEFINE_NON_ANDROID_HOTKEY("OpenCheatsMenu", TRANSLATE_NOOP("Hotkeys", "Interface"),
176162
TRANSLATE_NOOP("Hotkeys", "Open Cheat Settings"), [](s32 pressed) {
177-
if (!pressed && CanPause())
163+
if (!pressed && System::CanPauseSystem(true))
178164
FullscreenUI::OpenCheatsMenu();
179165
})
180166

181167
DEFINE_NON_ANDROID_HOTKEY("OpenAchievements", TRANSLATE_NOOP("Hotkeys", "Interface"),
182168
TRANSLATE_NOOP("Hotkeys", "Open Achievement List"), [](s32 pressed) {
183-
if (!pressed && CanPause())
169+
if (!pressed && System::CanPauseSystem(true))
184170
FullscreenUI::OpenAchievementsWindow();
185171
})
186172

187173
DEFINE_NON_ANDROID_HOTKEY("OpenLeaderboards", TRANSLATE_NOOP("Hotkeys", "Interface"),
188174
TRANSLATE_NOOP("Hotkeys", "Open Leaderboard List"), [](s32 pressed) {
189-
if (!pressed && CanPause())
175+
if (!pressed && System::CanPauseSystem(true))
190176
FullscreenUI::OpenLeaderboardsWindow();
191177
})
192178

@@ -198,7 +184,7 @@ DEFINE_NON_ANDROID_HOTKEY("Screenshot", TRANSLATE_NOOP("Hotkeys", "Interface"),
198184

199185
DEFINE_NON_ANDROID_HOTKEY("TogglePause", TRANSLATE_NOOP("Hotkeys", "Interface"),
200186
TRANSLATE_NOOP("Hotkeys", "Toggle Pause"), [](s32 pressed) {
201-
if (!pressed && CanPause())
187+
if (!pressed && System::CanPauseSystem(true))
202188
System::PauseSystem(!System::IsPaused());
203189
})
204190

@@ -235,7 +221,7 @@ DEFINE_HOTKEY("ToggleTurbo", TRANSLATE_NOOP("Hotkeys", "System"), TRANSLATE_NOOP
235221

236222
DEFINE_NON_ANDROID_HOTKEY("PowerOff", TRANSLATE_NOOP("Hotkeys", "System"),
237223
TRANSLATE_NOOP("Hotkeys", "Power Off System"), [](s32 pressed) {
238-
if (!pressed && CanPause())
224+
if (!pressed && System::CanPauseSystem(true))
239225
Host::RequestSystemShutdown(true, g_settings.save_state_on_exit, true);
240226
})
241227

src/core/system.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,24 @@ void System::PauseSystem(bool paused)
16321632
}
16331633
}
16341634

1635+
bool System::CanPauseSystem(bool display_message)
1636+
{
1637+
const u32 frames_until_pause_allowed = Achievements::GetPauseThrottleFrames();
1638+
if (frames_until_pause_allowed == 0)
1639+
return true;
1640+
1641+
if (display_message)
1642+
{
1643+
const float seconds = static_cast<float>(frames_until_pause_allowed) / System::GetVideoFrameRate();
1644+
Host::AddIconOSDMessage("PauseCooldown", ICON_FA_CLOCK,
1645+
TRANSLATE_PLURAL_STR("Hotkeys", "You cannot pause until another %n second(s) have passed.",
1646+
"", static_cast<int>(std::ceil(seconds))),
1647+
std::max(seconds, Host::OSD_QUICK_DURATION));
1648+
}
1649+
1650+
return false;
1651+
}
1652+
16351653
bool System::SaveResumeState(Error* error)
16361654
{
16371655
if (s_state.running_game_serial.empty())

src/core/system.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ bool BootSystem(SystemBootParameters parameters, Error* error);
260260
void PauseSystem(bool paused);
261261
void ResetSystem();
262262

263+
/// Returns true if the system can be paused, i.e. not subject to achievement restrictions.
264+
bool CanPauseSystem(bool display_message);
265+
263266
/// Returns the maximum size of a save state, considering the current configuration.
264267
size_t GetMaxSaveStateSize();
265268

src/duckstation-qt/mainwindow.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,35 @@ void MainWindow::onFullscreenUIStartedOrStopped(bool running)
13381338
m_ui.actionStartFullscreenUI2->setText(running ? tr("Exit Big Picture") : tr("Big Picture"));
13391339
}
13401340

1341+
void MainWindow::onPauseActionToggled(bool checked)
1342+
{
1343+
if (s_system_paused == checked)
1344+
return;
1345+
1346+
if (checked && s_achievements_hardcore_mode)
1347+
{
1348+
// Need to check restrictions.
1349+
Host::RunOnCPUThread([]() {
1350+
if (System::CanPauseSystem(true))
1351+
{
1352+
g_emu_thread->setSystemPaused(true);
1353+
}
1354+
else
1355+
{
1356+
// Restore action state.
1357+
Host::RunOnUIThread([]() {
1358+
QSignalBlocker sb(g_main_window->m_ui.actionPause);
1359+
g_main_window->m_ui.actionPause->setChecked(false);
1360+
});
1361+
}
1362+
});
1363+
}
1364+
else
1365+
{
1366+
g_emu_thread->setSystemPaused(checked);
1367+
}
1368+
}
1369+
13411370
void MainWindow::onRemoveDiscActionTriggered()
13421371
{
13431372
g_emu_thread->changeDisc(QString(), false, true);
@@ -2274,7 +2303,7 @@ void MainWindow::connectSignals()
22742303
connect(m_ui.actionPowerOffWithoutSaving, &QAction::triggered, this,
22752304
[this]() { requestShutdown(false, false, false, true, false, false); });
22762305
connect(m_ui.actionReset, &QAction::triggered, this, []() { g_emu_thread->resetSystem(true); });
2277-
connect(m_ui.actionPause, &QAction::toggled, this, [](bool active) { g_emu_thread->setSystemPaused(active); });
2306+
connect(m_ui.actionPause, &QAction::toggled, this, &MainWindow::onPauseActionToggled);
22782307
connect(m_ui.actionScreenshot, &QAction::triggered, g_emu_thread, &EmuThread::saveScreenshot);
22792308
connect(m_ui.actionScanForNewGames, &QAction::triggered, this, &MainWindow::onScanForNewGamesTriggered);
22802309
connect(m_ui.actionRescanAllGames, &QAction::triggered, this, [this]() { refreshGameList(true); });
@@ -2821,6 +2850,24 @@ void MainWindow::requestShutdown(bool allow_confirm, bool allow_save_to_state, b
28212850
// Only confirm on UI thread because we need to display a msgbox.
28222851
if (!m_is_closing && s_system_valid && allow_confirm && Host::GetBoolSettingValue("Main", "ConfirmPowerOff", true))
28232852
{
2853+
// Hardcore mode restrictions.
2854+
if (!s_system_paused && s_achievements_hardcore_mode && allow_confirm)
2855+
{
2856+
Host::RunOnCPUThread(
2857+
[allow_confirm, allow_save_to_state, save_state, check_safety, exit_fullscreen_ui, quit_afterwards]() {
2858+
if (!System::CanPauseSystem(true))
2859+
return;
2860+
2861+
Host::RunOnUIThread(
2862+
[allow_confirm, allow_save_to_state, save_state, check_safety, exit_fullscreen_ui, quit_afterwards]() {
2863+
g_main_window->requestShutdown(allow_confirm, allow_save_to_state, save_state, check_safety,
2864+
exit_fullscreen_ui, quit_afterwards);
2865+
});
2866+
});
2867+
2868+
return;
2869+
}
2870+
28242871
SystemLock lock(pauseAndLockSystem());
28252872

28262873
QMessageBox msgbox(lock.getDialogParent());

src/duckstation-qt/mainwindow.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ private Q_SLOTS:
184184
void onCheatsActionTriggered();
185185
void onCheatsMenuAboutToShow();
186186
void onStartFullscreenUITriggered();
187+
void onPauseActionToggled(bool checked);
187188
void onFullscreenUIStartedOrStopped(bool running);
188189
void onRemoveDiscActionTriggered();
189190
void onScanForNewGamesTriggered();

0 commit comments

Comments
 (0)