Advertisement
KukuRuzo

work with idle_x11

Aug 12th, 2022 (edited)
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.88 KB | None | 0 0
  1. diff --git a/tools/idle/idle.cpp b/tools/idle/idle.cpp
  2. index a974fcc2..dbe37dc0 100644
  3. --- a/tools/idle/idle.cpp
  4. +++ b/tools/idle/idle.cpp
  5. @@ -83,8 +83,8 @@ void Idle::start()
  6.          d->idleSince    = QDateTime::currentDateTime();
  7.      }
  8.  
  9. -    // poll every second (use a lower value if you need more accuracy)
  10. -    d->checkTimer.start(1000);
  11. +    // poll every 5 seconds (use a lower value if you need more accuracy)
  12. +    d->checkTimer.start(5000);
  13.  }
  14.  
  15.  void Idle::stop() { d->checkTimer.stop(); }
  16. diff --git a/tools/idle/idle_x11.cpp b/tools/idle/idle_x11.cpp
  17. index 578bcbb1..ebab3d91 100644
  18. --- a/tools/idle/idle_x11.cpp
  19. +++ b/tools/idle/idle_x11.cpp
  20. @@ -19,19 +19,78 @@
  21.  
  22.  #include "idle.h"
  23.  
  24. -#ifndef HAVE_XSS
  25. +#if !defined(HAVE_XSS) && !defined(USE_DBUS)
  26.  
  27.  IdlePlatform::IdlePlatform() { d = nullptr; }
  28.  IdlePlatform::~IdlePlatform() { }
  29.  bool IdlePlatform::init() { return false; }
  30.  int  IdlePlatform::secondsIdle() { return 0; }
  31.  
  32. -#else
  33. +#elif defined(USE_DBUS) && !defined(HAVE_X11) && !defined(LIMIT_X11_USAGE)
  34. +
  35. +#include <QDBusConnection>
  36. +#include <QDBusConnectionInterface>
  37. +#include <QDBusInterface>
  38. +#include <QDBusMessage>
  39. +#include <QDBusReply>
  40. +
  41. +// Screen Saver dbus services
  42. +static const QLatin1String COMMON_SS_SERV("org.freedesktop.ScreenSaver");
  43. +static const QLatin1String COMMON_SS_PATH("/ScreenSaver");
  44. +static const QLatin1String KDE_SS_SERV("org.kde.screensaver");
  45. +static const QLatin1String GNOME_SS_SERV("org.gnome.Mutter.IdleMonitor");
  46. +static const QLatin1String GNOME_SS_PATH("/org/gnome/Mutter/IdleMonitor/Core");
  47. +// Screen saver functions
  48. +static const QLatin1String GNOME_SS_F("GetIdletime");
  49. +static const QLatin1String COMMON_SS_F("GetSessionIdleTime");
  50.  
  51. -// gajim uses 'org.gnome.Mutter.IdleMonitor'.
  52. -// we can also use
  53. -//   in short qdbus org.kde.screensaver /ScreenSaver GetSessionIdleTime
  54. -//   but on kde it returns in ms while gnome is in secs
  55. +class IdlePlatform::Private {
  56. +public:
  57. +    Private() { }
  58. +    QString getServicesAvailable() const
  59. +    {
  60. +        const auto        services     = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
  61. +        const QStringList idleServices = { COMMON_SS_SERV, KDE_SS_SERV, GNOME_SS_SERV };
  62. +        // find first available dbus-service
  63. +        for (const auto &service : idleServices) {
  64. +            if (services.contains(service)) {
  65. +                return service;
  66. +            }
  67. +        }
  68. +        return QString();
  69. +    }
  70. +    int sendDBusCall() const
  71. +    {
  72. +        const auto serviceName = getServicesAvailable();
  73. +        if (!serviceName.isEmpty()) {
  74. +            // KDE and freedesktop uses the same path interface and method but gnome uses other
  75. +            bool                isNotGnome = serviceName == COMMON_SS_SERV || serviceName == KDE_SS_SERV;
  76. +            const QLatin1String iface      = isNotGnome ? COMMON_SS_SERV : GNOME_SS_SERV;
  77. +            const QLatin1String path       = isNotGnome ? COMMON_SS_PATH : GNOME_SS_PATH;
  78. +            const QLatin1String method     = isNotGnome ? COMMON_SS_F : GNOME_SS_F;
  79. +            auto                interface  = QDBusInterface(serviceName, path, iface);
  80. +            if (interface.isValid()) {
  81. +                QDBusReply<int> reply = interface.call(method);
  82. +                // probably reply value for freedesktop and kde need to be converted to seconds
  83. +                if (reply.isValid())
  84. +                    return isNotGnome ? reply.value() / 1000 : reply.value();
  85. +            }
  86. +        }
  87. +        return -1;
  88. +    }
  89. +};
  90. +
  91. +IdlePlatform::IdlePlatform() { d = new Private; }
  92. +IdlePlatform::~IdlePlatform() { delete d; }
  93. +bool IdlePlatform::init() { return d->sendDBusCall() > 0; }
  94. +
  95. +int IdlePlatform::secondsIdle()
  96. +{
  97. +    const int result = d->sendDBusCall();
  98. +    return (result > 0) ? result : 0;
  99. +}
  100. +
  101. +#else
  102.  
  103.  #include <QApplication>
  104.  #include <QDesktopWidget>
  105.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement