Advertisement
KukuRuzo

idle work dbus with x11 fallback

May 30th, 2023 (edited)
1,064
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.58 KB | None | 0 0
  1. diff --git a/CMakeLists.txt b/CMakeLists.txt
  2. index 4fa42b61..00c2ecc1 100644
  3. --- a/CMakeLists.txt
  4. +++ b/CMakeLists.txt
  5. @@ -199,6 +199,10 @@ if(UNIX AND NOT (APPLE OR HAIKU))
  6.      if(USE_X11)
  7.          add_definitions( -DHAVE_X11 )
  8.          message(STATUS "X11 features support - ENABLED")
  9. +        if(USE_XSS)
  10. +            add_definitions( -DHAVE_XSS )
  11. +            message(STATUS "Xscreensaver support - ENABLED")
  12. +        endif()
  13.      elseif(NOT LIMIT_X11_USAGE)
  14.          set(USE_XSS OFF)
  15.      endif()
  16. @@ -211,10 +215,6 @@ if(UNIX AND NOT (APPLE OR HAIKU))
  17.          -DAPP_PREFIX=${CMAKE_INSTALL_PREFIX}
  18.          -DAPP_BIN_NAME=${PROJECT_NAME}
  19.      )
  20. -    if(USE_XSS)
  21. -        add_definitions( -DHAVE_XSS )
  22. -        message(STATUS "Xscreensaver support - ENABLED")
  23. -    endif()
  24.      if(USE_DBUS)
  25.          message(STATUS "DBus support - ENABLED")
  26.      endif()
  27. diff --git a/src/libpsi/tools/idle/idle_x11.cpp b/src/libpsi/tools/idle/idle_x11.cpp
  28. index b2f7b461..5f066f6c 100644
  29. --- a/src/libpsi/tools/idle/idle_x11.cpp
  30. +++ b/src/libpsi/tools/idle/idle_x11.cpp
  31. @@ -19,14 +19,25 @@
  32.  
  33.  #include "idle.h"
  34.  
  35. -#if !defined(HAVE_XSS) && !defined(USE_DBUS)
  36. +#ifdef HAVE_XSS
  37. +#include <QApplication>
  38. +#include <QDesktopWidget>
  39. +#include <QX11Info>
  40. +#include <X11/Xlib.h>
  41. +#include <X11/Xutil.h>
  42. +#include <X11/extensions/scrnsaver.h>
  43.  
  44. -IdlePlatform::IdlePlatform() { d = nullptr; }
  45. -IdlePlatform::~IdlePlatform() { }
  46. -bool IdlePlatform::init() { return false; }
  47. -int  IdlePlatform::secondsIdle() { return 0; }
  48. +static XErrorHandler old_handler = 0;
  49. +extern "C" int       xerrhandler(Display *dpy, XErrorEvent *err)
  50. +{
  51. +    if (err->error_code == BadDrawable)
  52. +        return 0;
  53.  
  54. -#elif defined(USE_DBUS) && !defined(HAVE_X11) && !defined(LIMIT_X11_USAGE)
  55. +    return (*old_handler)(dpy, err);
  56. +}
  57. +#endif // HAVE_XSS
  58. +
  59. +#ifdef USE_DBUS
  60.  
  61.  #include <QDBusConnection>
  62.  #include <QDBusConnectionInterface>
  63. @@ -44,92 +55,58 @@ static const QLatin1String GNOME_SS_PATH("/org/gnome/Mutter/IdleMonitor/Core");
  64.  static const QLatin1String GNOME_SS_F("GetIdletime");
  65.  static const QLatin1String COMMON_SS_F("GetSessionIdleTime");
  66.  
  67. -class IdlePlatform::Private {
  68. -public:
  69. -    Private() { }
  70. -    QString getServicesAvailable() const
  71. -    {
  72. -        const auto        services     = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
  73. -        const QStringList idleServices = { COMMON_SS_SERV, KDE_SS_SERV, GNOME_SS_SERV };
  74. -        // find first available dbus-service
  75. -        for (const auto &service : idleServices) {
  76. -            if (services.contains(service)) {
  77. -                return service;
  78. -            }
  79. -        }
  80. -        return QString();
  81. -    }
  82. -    int sendDBusCall() const
  83. -    {
  84. -        const auto serviceName = getServicesAvailable();
  85. -        if (!serviceName.isEmpty()) {
  86. -            // KDE and freedesktop uses the same path interface and method but gnome uses other
  87. -            bool                isNotGnome = serviceName == COMMON_SS_SERV || serviceName == KDE_SS_SERV;
  88. -            const QLatin1String iface      = isNotGnome ? COMMON_SS_SERV : GNOME_SS_SERV;
  89. -            const QLatin1String path       = isNotGnome ? COMMON_SS_PATH : GNOME_SS_PATH;
  90. -            const QLatin1String method     = isNotGnome ? COMMON_SS_F : GNOME_SS_F;
  91. -            auto                interface  = QDBusInterface(serviceName, path, iface);
  92. -            if (interface.isValid()) {
  93. -                QDBusReply<uint> reply = interface.call(method);
  94. -                // probably reply value for freedesktop and kde need to be converted to seconds
  95. -                if (reply.isValid())
  96. -                    return isNotGnome ? reply.value() / 1000 : reply.value();
  97. -            }
  98. -        }
  99. -        return -1;
  100. -    }
  101. -};
  102. -
  103. -IdlePlatform::IdlePlatform() { d = new Private; }
  104. -IdlePlatform::~IdlePlatform() { delete d; }
  105. -bool IdlePlatform::init() { return d->sendDBusCall() >= 0; }
  106. -
  107. -int IdlePlatform::secondsIdle()
  108. -{
  109. -    const int result = d->sendDBusCall();
  110. -    return (result > 0) ? result : 0;
  111. -}
  112. -
  113. -#else
  114. -
  115. -#include <QApplication>
  116. -#include <QDesktopWidget>
  117. -#include <QX11Info>
  118. -#include <X11/Xlib.h>
  119. -#include <X11/Xutil.h>
  120. -#include <X11/extensions/scrnsaver.h>
  121. -
  122. -static XErrorHandler old_handler = 0;
  123. -extern "C" int       xerrhandler(Display *dpy, XErrorEvent *err)
  124. -{
  125. -    if (err->error_code == BadDrawable)
  126. -        return 0;
  127. -
  128. -    return (*old_handler)(dpy, err);
  129. -}
  130. +#endif // USE_DBUS
  131.  
  132.  class IdlePlatform::Private {
  133.  public:
  134.      Private() { }
  135.  
  136. +#ifdef USE_DBUS
  137. +    QString dbusService = QString();
  138. +#endif // USE_DBUS
  139. +#ifdef HAVE_XSS
  140.      XScreenSaverInfo *ss_info = nullptr;
  141. +#endif // HAVE_XSS
  142.  };
  143.  
  144. -IdlePlatform::IdlePlatform() { d = new Private; }
  145. +IdlePlatform::IdlePlatform()
  146. +{
  147. +#if defined(HAVE_XSS) || defined(USE_DBUS)
  148. +    d = new Private;
  149. +#else
  150. +    d = nullptr;
  151. +#endif
  152. +}
  153.  
  154.  IdlePlatform::~IdlePlatform()
  155.  {
  156. +#ifdef HAVE_XSS
  157.      if (d->ss_info)
  158.          XFree(d->ss_info);
  159.      if (old_handler) {
  160.          XSetErrorHandler(old_handler);
  161.          old_handler = 0;
  162.      }
  163. -    delete d;
  164. +#endif // HAVE_XSS
  165. +    if (d)
  166. +        delete d;
  167.  }
  168.  
  169.  bool IdlePlatform::init()
  170.  {
  171. +#ifdef USE_DBUS
  172. +    // if DBUS idle is available using it else try to use XSS functions
  173. +    const auto        services     = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
  174. +    const QStringList idleServices = { COMMON_SS_SERV, KDE_SS_SERV, GNOME_SS_SERV };
  175. +    // find first available dbus-service
  176. +    for (const auto &service : idleServices) {
  177. +        if (services.contains(service)) {
  178. +            d->dbusService = service;
  179. +            return true;
  180. +        }
  181. +    }
  182. +#endif // USE_DBUS
  183. +#ifdef HAVE_XSS
  184.      if (!QX11Info::isPlatformX11())
  185.          return false;
  186.  
  187. @@ -139,24 +116,41 @@ bool IdlePlatform::init()
  188.      old_handler = XSetErrorHandler(xerrhandler);
  189.  
  190.      int event_base, error_base;
  191. -#if defined(HAVE_XSS) && !defined(LIMIT_X11_USAGE)
  192. +#ifndef LIMIT_X11_USAGE
  193.      if (XScreenSaverQueryExtension(QX11Info::display(), &event_base, &error_base)) {
  194.          d->ss_info = XScreenSaverAllocInfo();
  195.          return true;
  196.      }
  197. -#endif
  198. +#endif // LIMIT_X11_USAGE
  199. +#endif // HAVE_XSS
  200.      return false;
  201.  }
  202.  
  203.  int IdlePlatform::secondsIdle()
  204.  {
  205. +#ifdef USE_DBUS
  206. +    if (!d->dbusService.isEmpty()) {
  207. +        // KDE and freedesktop uses the same path interface and method but gnome uses other
  208. +        bool                isNotGnome = d->dbusService == COMMON_SS_SERV || d->dbusService == KDE_SS_SERV;
  209. +        const QLatin1String iface      = isNotGnome ? COMMON_SS_SERV : GNOME_SS_SERV;
  210. +        const QLatin1String path       = isNotGnome ? COMMON_SS_PATH : GNOME_SS_PATH;
  211. +        const QLatin1String method     = isNotGnome ? COMMON_SS_F : GNOME_SS_F;
  212. +        auto                interface  = QDBusInterface(d->dbusService, path, iface);
  213. +        if (interface.isValid()) {
  214. +            QDBusReply<uint> reply = interface.call(method);
  215. +            // probably reply value for freedesktop and kde need to be converted to seconds
  216. +            if (reply.isValid())
  217. +                return isNotGnome ? reply.value() / 1000 : reply.value();
  218. +        }
  219. +    }
  220. +#endif // USE_DBUS
  221. +#ifdef HAVE_XSS
  222.      if (!d->ss_info)
  223.          return 0;
  224. -#if defined(HAVE_XSS)
  225. +
  226.      if (!XScreenSaverQueryInfo(QX11Info::display(), QX11Info::appRootWindow(), d->ss_info))
  227.          return 0;
  228. -#endif
  229.      return d->ss_info->idle / 1000;
  230. +#endif // HAVE_XSS
  231. +    return 0;
  232.  }
  233. -
  234. -#endif // ! ( defined(USE_DBUS) && !defined(HAVE_X11) && !defined(LIMIT_X11_USAGE) )
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement