From 85c0b4eff1bcd5906d2ea28290124a4be3f96097 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sat, 2 May 2026 21:30:52 +1000 Subject: [PATCH] System: Move gamescope screensaver hack to QtHost --- src/core/system.cpp | 17 ----------------- src/duckstation-qt/qthost.cpp | 16 ++++++++++++---- src/duckstation-qt/qtwindowinfo.cpp | 22 +++++++++++++++++++--- src/duckstation-qt/qtwindowinfo.h | 7 +++++++ 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/core/system.cpp b/src/core/system.cpp index 2bfccbbe7..d2bda3627 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -317,9 +317,6 @@ struct ALIGN_TO_CACHE_LINE StateVars GameHash running_game_hash = 0; bool running_game_custom_title = false; - // Prevent screensaver inhibits when running on platforms that don't have it (e.g. gamescope). - bool disable_screensaver_inhibit = false; - std::atomic_bool startup_cancelled{false}; std::unique_ptr game_settings_interface; @@ -499,17 +496,6 @@ bool System::ProcessStartup(Error* error) SetRymlCallbacks(); #ifdef __linux__ - // Disable screensaver inhibit if running on gamescope. - const char* desktop = std::getenv("XDG_CURRENT_DESKTOP"); - if (!desktop) - desktop = std::getenv("XDG_SESSION_DESKTOP"); - if (!desktop || std::strlen(desktop) == 0 || std::strstr(desktop, "gamescope")) - { - INFO_LOG("Missing XDG_CURRENT_DESKTOP ({}) or running under gamescope, disabling screensaver inhibit.", - desktop ? desktop : "null"); - s_state.disable_screensaver_inhibit = true; - } - // Running DuckStation out of /usr is not supported and makes no sense. if (std::memcmp(EmuFolders::AppRoot.data(), "/usr/", 5) == 0) return false; @@ -3934,9 +3920,6 @@ PresentSkipMode System::GetEffectivePresentSkipMode() void System::InhibitScreensaver(bool inhibit) { - if (s_state.disable_screensaver_inhibit) - return; - Error error; if (!Host::SetScreensaverInhibit(inhibit, &error)) { diff --git a/src/duckstation-qt/qthost.cpp b/src/duckstation-qt/qthost.cpp index d76f7abfe..f1df7a0de 100644 --- a/src/duckstation-qt/qthost.cpp +++ b/src/duckstation-qt/qthost.cpp @@ -347,13 +347,21 @@ bool QtHost::IsDisplayWidgetContainerNeeded() void QtHost::AdjustQtEnvironmentVariables() { - const char* desktop = std::getenv("XDG_SESSION_DESKTOP"); + // Disable screensaver inhibit if running on gamescope. + const char* desktop = std::getenv("XDG_CURRENT_DESKTOP"); if (!desktop) - return; + desktop = std::getenv("XDG_SESSION_DESKTOP"); + + std::fprintf(stderr, "XDG_SESSION_DESKTOP=%s\n", desktop ? desktop : "null"); - std::fprintf(stderr, "XDG_SESSION_DESKTOP=%s\n", desktop); + if (!desktop || std::strstr(desktop, "gamescope")) + { + INFO_LOG("Missing XDG_CURRENT_DESKTOP ({}) or running under gamescope, disabling screensaver inhibit.", + desktop ? desktop : "null"); + QtHost::DisableScreensaverInhibit(); + } - if (std::strcmp(desktop, "KDE") == 0 || std::strcmp(desktop, "GNOME") == 0) + if (desktop && (std::strstr(desktop, "KDE") == 0 || std::strstr(desktop, "GNOME") == 0)) { const char* platform_theme = std::getenv("QT_QPA_PLATFORMTHEME"); if (platform_theme) diff --git a/src/duckstation-qt/qtwindowinfo.cpp b/src/duckstation-qt/qtwindowinfo.cpp index c07c01c12..be3d47c3a 100644 --- a/src/duckstation-qt/qtwindowinfo.cpp +++ b/src/duckstation-qt/qtwindowinfo.cpp @@ -49,14 +49,16 @@ namespace { struct WindowInfoLocals { - bool screensaver_inhibited; - #if defined(__APPLE__) IOPMAssertionID screensaver_inhibit_assertion; #elif defined(__linux__) - u32 screensaver_inhibit_cookie; + // Prevent screensaver inhibits when running on platforms that don't have it (e.g. gamescope). std::optional screensaver_inhibit_interface; + u32 screensaver_inhibit_cookie = 0; + bool disable_screensaver_inhibit = false; #endif + + bool screensaver_inhibited = false; }; } // namespace @@ -257,6 +259,13 @@ bool Host::SetScreensaverInhibit(bool inhibit, Error* error) #elif defined(__linux__) + if (s_window_info_locals.disable_screensaver_inhibit) + { + // pretend it succeeded so the caller doesn't throw an error + s_window_info_locals.screensaver_inhibited = inhibit; + return true; + } + if (!s_window_info_locals.screensaver_inhibit_interface.has_value()) { const QDBusConnection connection = QDBusConnection::sessionBus(); @@ -330,3 +339,10 @@ bool QtUtils::SetWindowRoundedCornerState(QWidget* widget, bool enabled) } #endif // _WIN32 + +void QtHost::DisableScreensaverInhibit() +{ +#ifdef __linux__ + s_window_info_locals.disable_screensaver_inhibit = true; +#endif +} diff --git a/src/duckstation-qt/qtwindowinfo.h b/src/duckstation-qt/qtwindowinfo.h index d61b06768..c1998a64a 100644 --- a/src/duckstation-qt/qtwindowinfo.h +++ b/src/duckstation-qt/qtwindowinfo.h @@ -37,3 +37,10 @@ bool SetWindowRoundedCornerState(QWidget* widget, bool enabled); #endif } // namespace QtUtils + +namespace QtHost { + +/// Prevents the screensaver from being inhibited, if it is not supported on the current platform. +void DisableScreensaverInhibit(); + +} // namespace QtHost