Advertisement
KukuRuzo

idle_x11

Aug 11th, 2022
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.99 KB | None | 0 0
  1. diff --git a/src/libpsi/tools/idle/idle_x11.cpp b/src/libpsi/tools/idle/idle_x11.cpp
  2. index 578bcbb1..4fff6803 100644
  3. --- a/src/libpsi/tools/idle/idle_x11.cpp
  4. +++ b/src/libpsi/tools/idle/idle_x11.cpp
  5. @@ -19,20 +19,78 @@
  6.  
  7.  #include "idle.h"
  8.  
  9. -#ifndef HAVE_XSS
  10. +#if !defined(HAVE_XSS) && !defined(USE_DBUS)
  11.  
  12.  IdlePlatform::IdlePlatform() { d = nullptr; }
  13.  IdlePlatform::~IdlePlatform() { }
  14.  bool IdlePlatform::init() { return false; }
  15.  int  IdlePlatform::secondsIdle() { return 0; }
  16.  
  17. -#else
  18. +#elif defined(USE_DBUS) && (!defined(HAVE_X11) || !defined(LIMIT_X11_USAGE))
  19.  
  20. +#include <QDBusConnection>
  21. +#include <QDBusConnectionInterface>
  22. +#include <QDBusInterface>
  23. +#include <QDBusMessage>
  24. +#include <QDBusReply>
  25. +
  26. +// Screen Saver dbus services
  27. +static const QLatin1String COMMON_SS_SERV("org.freedesktop.ScreenSaver");
  28. +static const QLatin1String COMMON_SS_PATH("/ScreenSaver");
  29. +static const QLatin1String KDE_SS_SERV("org.kde.screensaver");
  30. +static const QLatin1String GNOME_SS_SERV("org.gnome.Mutter.IdleMonitor");
  31. +static const QLatin1String GNOME_SS_PATH("/org/gnome/Mutter/IdleMonitor/Core");
  32. +// Screen saver functions
  33. +static const QLatin1String GNOME_SS_F("GetIdletime");
  34. +static const QLatin1String COMMON_SS_F("GetSessionIdleTime");
  35. +
  36. +class IdlePlatform::Private {
  37. +public:
  38. +    Private() { }
  39. +    QString availableService;
  40. +    bool    getServicesAvailable()
  41. +    {
  42. +        const auto        services     = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
  43. +        const QStringList idleServices = { KDE_SS_SERV, GNOME_SS_SERV };
  44. +        // find first available dbus-service
  45. +        for (const auto &service : idleServices) {
  46. +            if (services.contains(service)) {
  47. +                availableService = service;
  48. +                return true;
  49. +            }
  50. +        }
  51. +        return false;
  52. +    }
  53. +};
  54. +
  55. +IdlePlatform::IdlePlatform() { d = new Private; }
  56. +IdlePlatform::~IdlePlatform() { delete d; }
  57. +bool IdlePlatform::init() { return !d->getServicesAvailable(); }
  58. +
  59. +int IdlePlatform::secondsIdle()
  60. +{
  61. +    // KDE and freedesktop uses the same path interface and method but gnome uses other
  62. +    bool                isNotGnome = d->availableService == COMMON_SS_SERV || d->availableService == KDE_SS_SERV;
  63. +    const QLatin1String iface      = isNotGnome ? COMMON_SS_SERV : GNOME_SS_SERV;
  64. +    const QLatin1String path       = isNotGnome ? COMMON_SS_PATH : GNOME_SS_PATH;
  65. +    const QLatin1String method     = isNotGnome ? COMMON_SS_F : GNOME_SS_F;
  66. +    auto                interface  = QDBusInterface(d->availableService, path, iface);
  67. +    if (interface.isValid()) {
  68. +        QDBusReply<int> reply = interface.call(method);
  69. +        // probably reply value for freedesktop and kde need to be converted to seconds
  70. +        if (reply.isValid())
  71. +            return isNotGnome ? reply.value() / 1000 : reply.value();
  72. +    }
  73. +    return 0;
  74. +}
  75. +
  76. +#else
  77. +
  78.  #include <QApplication>
  79.  #include <QDesktopWidget>
  80.  #include <QX11Info>
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement