| 1 | // Copyright 2024 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "UICommon/DBusUtils.h" |
| 5 | |
| 6 | #include <QDBusConnection> |
| 7 | #include <QDBusInterface> |
| 8 | #include <QDBusReply> |
| 9 | #include <QHash> |
| 10 | #include <QObject> |
| 11 | #include <QString> |
| 12 | |
| 13 | #include "Common/Logging/Log.h" |
| 14 | |
| 15 | namespace DBusUtils |
| 16 | { |
| 17 | static bool InhibitFDO(); |
| 18 | static bool InhibitXfce(); |
| 19 | static bool InhibitMate(); |
| 20 | static bool InhibitPortal(); |
| 21 | static void Uninhibit(); |
| 22 | |
| 23 | static constexpr char s_app_id[] = "org.DolphinEmu.dolphin-emu"; |
| 24 | |
| 25 | // Cookie for the org.freedesktop.ScreenSaver interface |
| 26 | static uint32_t s_fdo_cookie = 0; |
| 27 | // Cookie for the org.xfce.ScreenSaver interface |
| 28 | static uint32_t s_xfce_cookie = 0; |
| 29 | // Cookie for the org.mate.ScreenSaver interface |
| 30 | static uint32_t s_mate_cookie = 0; |
| 31 | // Return handle for the org.freedesktop.portal.Desktop interface |
| 32 | static QString s_portal_handle; |
| 33 | |
| 34 | // Uses D-Bus to inhibit the screensaver |
| 35 | // Tries various D-Bus interfaces until it finds one that works |
| 36 | void InhibitScreenSaver(bool inhibit) |
| 37 | { |
| 38 | if (inhibit) |
| 39 | { |
| 40 | if (s_fdo_cookie || s_xfce_cookie || s_mate_cookie || !s_portal_handle.isEmpty()) |
| 41 | return; |
| 42 | if (!InhibitFDO() && !InhibitXfce() && !InhibitMate() && !InhibitPortal()) |
| 43 | INFO_LOG_FMT(VIDEO, "Could not inhibit screensaver: No services available"); |
| 44 | } |
| 45 | else |
| 46 | Uninhibit(); |
| 47 | } |
| 48 | |
| 49 | // Inhibits screensaver on Xfce desktop |
| 50 | static bool InhibitXfce() |
| 51 | { |
| 52 | QDBusInterface interface("org.xfce.ScreenSaver", "/", "org.xfce.ScreenSaver"); |
| 53 | if (!interface.isValid()) |
| 54 | return false; |
| 55 | |
| 56 | QDBusReply<uint32_t> reply = interface.call("Inhibit", s_app_id, QObject::tr("Playing a game")); |
| 57 | if (interface.lastError().isValid()) |
| 58 | { |
| 59 | WARN_LOG_FMT(VIDEO, "org.xfce.ScreenSaver::Inhibit failed: {}", |
| 60 | interface.lastError().message().toStdString()); |
| 61 | return false; |
| 62 | } |
| 63 | INFO_LOG_FMT(VIDEO, "org.xfce.ScreenSaver::Inhibit succeeded"); |
| 64 | s_xfce_cookie = reply; |
| 65 | return true; |
| 66 | } |
| 67 | |
| 68 | // Inhibits screensaver on the MATE desktop |
| 69 | // MATE advertises support for xdg-desktop-portal Inhibit, |
| 70 | // but it doesn't work as of Fedora 40 |
| 71 | static bool InhibitMate() |
| 72 | { |
| 73 | QDBusInterface interface("org.mate.ScreenSaver", "/org/mate/ScreenSaver", "org.mate.ScreenSaver"); |
| 74 | if (!interface.isValid()) |
| 75 | return false; |
| 76 | |
| 77 | QDBusReply<uint32_t> reply = interface.call("Inhibit", s_app_id, QObject::tr("Playing a game")); |
| 78 | if (interface.lastError().isValid()) |
| 79 | { |
| 80 | WARN_LOG_FMT(VIDEO, "org.mate.ScreenSaver::Inhibit failed: {}", |
| 81 | interface.lastError().message().toStdString()); |
| 82 | return false; |
| 83 | } |
| 84 | INFO_LOG_FMT(VIDEO, "org.mate.ScreenSaver::Inhibit succeeded"); |
| 85 | s_mate_cookie = reply; |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | // Inhibits screensaver on GNOME, KDE and Cinnamon |
| 90 | static bool InhibitFDO() |
| 91 | { |
| 92 | QDBusInterface interface("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", |
| 93 | "org.freedesktop.ScreenSaver"); |
| 94 | if (!interface.isValid()) |
| 95 | return false; |
| 96 | |
| 97 | QDBusReply<uint32_t> reply = interface.call("Inhibit", s_app_id, QObject::tr("Playing a game")); |
| 98 | if (interface.lastError().isValid()) |
| 99 | { |
| 100 | WARN_LOG_FMT(VIDEO, "org.freedesktop.ScreenSaver::Inhibit failed: {}", |
| 101 | interface.lastError().message().toStdString()); |
| 102 | return false; |
| 103 | } |
| 104 | INFO_LOG_FMT(VIDEO, "org.freedesktop.ScreenSaver::Inhibit succeeded"); |
| 105 | s_fdo_cookie = reply; |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | // Inhibits screensaver when sandboxed through Flatpak |
| 110 | // Does not work on KDE Plasma versions before 6.1.5 |
| 111 | static bool InhibitPortal() |
| 112 | { |
| 113 | QDBusInterface interface("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop", |
| 114 | "org.freedesktop.portal.Inhibit"); |
| 115 | if (!interface.isValid()) |
| 116 | return false; |
| 117 | |
| 118 | QHash<QString, QVariant> options; |
| 119 | options["handle_token"] = "dolphin_" + QString::number(std::rand(), 0x10); |
| 120 | options["reason"] = QObject::tr("Playing a game"); |
| 121 | uint32_t flags = 9; // logout | idle |
| 122 | QDBusReply<QDBusObjectPath> reply = interface.call("Inhibit", "", flags, options); |
| 123 | if (interface.lastError().isValid()) |
| 124 | { |
| 125 | WARN_LOG_FMT(VIDEO, "org.freedesktop.portal.Desktop::Inhibit failed: {}", |
| 126 | interface.lastError().message().toStdString()); |
| 127 | return false; |
| 128 | } |
| 129 | INFO_LOG_FMT(VIDEO, "org.freedesktop.portal.Desktop::Inhibit succeeded"); |
| 130 | s_portal_handle = reply.value().path(); |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | static void Uninhibit() |
| 135 | { |
| 136 | if (s_fdo_cookie) |
| 137 | { |
| 138 | QDBusInterface interface("org.freedesktop.ScreenSaver", "/org/freedesktop/ScreenSaver", |
| 139 | "org.freedesktop.ScreenSaver"); |
| 140 | interface.call("UnInhibit", s_fdo_cookie); |
| 141 | if (interface.lastError().isValid()) |
| 142 | { |
| 143 | WARN_LOG_FMT(VIDEO, "org.freedesktop.ScreenSaver::UnInhibit failed: {}", |
| 144 | interface.lastError().message().toStdString()); |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | INFO_LOG_FMT(VIDEO, "org.freedesktop.ScreenSaver::UnInhibit succeeded"); |
| 149 | } |
| 150 | s_fdo_cookie = 0; |
| 151 | } |
| 152 | |
| 153 | if (s_xfce_cookie) |
| 154 | { |
| 155 | QDBusInterface interface("org.xfce.ScreenSaver", "/org/xfce/ScreenSaver", |
| 156 | "org.xfce.ScreenSaver"); |
| 157 | interface.call("UnInhibit", s_xfce_cookie); |
| 158 | if (interface.lastError().isValid()) |
| 159 | { |
| 160 | WARN_LOG_FMT(VIDEO, "org.xfce.ScreenSaver::UnInhibit failed: {}", |
| 161 | interface.lastError().message().toStdString()); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | INFO_LOG_FMT(VIDEO, "org.xfce.ScreenSaver::UnInhibit succeeded"); |
| 166 | } |
| 167 | s_xfce_cookie = 0; |
| 168 | } |
| 169 | |
| 170 | if (s_mate_cookie) |
| 171 | { |
| 172 | QDBusInterface interface("org.mate.ScreenSaver", "/", "org.mate.ScreenSaver"); |
| 173 | interface.call("UnInhibit", s_mate_cookie); |
| 174 | if (interface.lastError().isValid()) |
| 175 | { |
| 176 | WARN_LOG_FMT(VIDEO, "org.mate.ScreenSaver::UnInhibit failed: {}", |
| 177 | interface.lastError().message().toStdString()); |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | INFO_LOG_FMT(VIDEO, "org.mate.ScreenSaver::UnInhibit succeeded"); |
| 182 | } |
| 183 | s_mate_cookie = 0; |
| 184 | } |
| 185 | |
| 186 | if (!s_portal_handle.isEmpty()) |
| 187 | { |
| 188 | QDBusInterface interface("org.freedesktop.portal.Desktop", s_portal_handle, |
| 189 | "org.freedesktop.portal.Request"); |
| 190 | interface.call("Close"); |
| 191 | if (interface.lastError().isValid()) |
| 192 | { |
| 193 | WARN_LOG_FMT(VIDEO, "org.freedesktop.portal.Request::Close failed: {}", |
| 194 | interface.lastError().message().toStdString()); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | INFO_LOG_FMT(VIDEO, "org.freedesktop.portal.Request::Close succeeded"); |
| 199 | } |
| 200 | s_portal_handle = QString(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | } // namespace DBusUtils |