DBusUtils.cpp 6.3 KB

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