From f136f6475cceb7a84c85c0607540aaec70079f0a Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Thu, 22 Aug 2024 21:22:56 -0400 Subject: [PATCH] Inhibit sleep on Linux when playing the game This fixes a "deficiency" in KWin, where controller input does not wake up the screen. You have to manually block the screen locking or else you need to move the mouse every so often. The system could also sleep while patching, which is really bad. This is a really simple implementation that can be expanded upon later. --- CMakeLists.txt | 4 +++ launcher/CMakeLists.txt | 5 ++++ launcher/include/launchercore.h | 8 ++++++ launcher/src/launchercore.cpp | 48 +++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 007b068..4469260 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -50,6 +50,10 @@ find_package(Qt6 ${QT_MIN_VERSION} REQUIRED COMPONENTS if (BUILD_WEBVIEW) find_package(Qt6WebView ${QT_MIN_VERSION} REQUIRED) endif () +if (LINUX) + find_package(Qt6 ${QT_MIN_VERSION} REQUIRED NO_MODULE COMPONENTS DBus) +endif () + find_package(KF6 ${KF_MIN_VERSION} REQUIRED COMPONENTS Kirigami I18n Config CoreAddons Archive) find_package(KF6KirigamiAddons 1.2.1 REQUIRED) find_package(QCoro6 REQUIRED COMPONENTS Core Network Qml) diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 3a8b490..16b0321 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -120,6 +120,11 @@ if (BUILD_SYNC) target_link_libraries(astra_static PRIVATE QuotientQt6) endif() +if (TARGET Qt6::DBus) + target_link_libraries(astra_static PRIVATE Qt6::DBus) + target_compile_definitions(astra_static PRIVATE -DHAS_DBUS) +endif () + add_executable(astra) target_sources(astra PRIVATE diff --git a/launcher/include/launchercore.h b/launcher/include/launchercore.h index 3052663..eaa9ea2 100755 --- a/launcher/include/launchercore.h +++ b/launcher/include/launchercore.h @@ -167,6 +167,12 @@ private: /// Updates FFXIV.cfg with some recommended options like turning the opening cutscene movie off void updateConfig(const Account *account); + /// Tell the system to keep the screen on and don't go to sleep + void inhibitSleep(); + + /// Tell the system we can allow the screen to turn off + void uninhibitSleep(); + SteamAPI *m_steamApi = nullptr; bool m_loadingFinished = false; @@ -188,4 +194,6 @@ private: #endif int m_currentProfileIndex = 0; + + unsigned int screenSaverDbusCookie = 0; }; diff --git a/launcher/src/launchercore.cpp b/launcher/src/launchercore.cpp index 5aa861b..a2af5f5 100755 --- a/launcher/src/launchercore.cpp +++ b/launcher/src/launchercore.cpp @@ -27,6 +27,12 @@ #include "syncmanager.h" #endif +#ifdef HAS_DBUS +#include +#include +#include +#endif + using namespace Qt::StringLiterals; LauncherCore::LauncherCore() @@ -78,6 +84,8 @@ void LauncherCore::login(Profile *profile, const QString &username, const QStrin { Q_ASSERT(profile != nullptr); + inhibitSleep(); + const auto loginInformation = new LoginInformation(this); loginInformation->profile = profile; @@ -572,6 +580,10 @@ QCoro::Task<> LauncherCore::fetchNews() QCoro::Task<> LauncherCore::handleGameExit(const Profile *profile) { + qCDebug(ASTRA_LOG) << "Game has closed."; + + uninhibitSleep(); + #ifdef BUILD_SYNC // TODO: once we have Steam API support we can tell Steam to delay putting the Deck to sleep until our upload is complete if (m_settings->enableSync()) { @@ -630,4 +642,40 @@ void LauncherCore::updateConfig(const Account *account) file.close(); } +void LauncherCore::inhibitSleep() +{ +#ifdef HAS_DBUS + if (screenSaverDbusCookie != 0) + return; + + QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.ScreenSaver"), + QStringLiteral("/ScreenSaver"), + QStringLiteral("org.freedesktop.ScreenSaver"), + QStringLiteral("Inhibit")); + message << QGuiApplication::desktopFileName(); + message << i18n("Playing FFXIV"); + + const QDBusReply reply = QDBusConnection::sessionBus().call(message); + if (reply.isValid()) { + screenSaverDbusCookie = reply.value(); + } +#endif +} + +void LauncherCore::uninhibitSleep() +{ +#ifdef HAS_DBUS + if (screenSaverDbusCookie == 0) + return; + + QDBusMessage message = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.ScreenSaver"), + QStringLiteral("/ScreenSaver"), + QStringLiteral("org.freedesktop.ScreenSaver"), + QStringLiteral("UnInhibit")); + message << static_cast(screenSaverDbusCookie); + screenSaverDbusCookie = 0; + QDBusConnection::sessionBus().send(message); +#endif +} + #include "moc_launchercore.cpp" \ No newline at end of file