Advertisement
KukuRuzo

idle_dbus

Aug 11th, 2022 (edited)
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. /*
  2.  * idle_dbus.cpp - detect desktop idle time
  3.  * Copyright (C) 2022  Vitaly Tonkacheyev
  4.  *
  5.  * This library is free software; you can redistribute it and/or
  6.  * modify it under the terms of the GNU Lesser General Public
  7.  * License as published by the Free Software Foundation; either
  8.  * version 2.1 of the License, or (at your option) any later version.
  9.  *
  10.  * This library is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * Lesser General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Lesser General Public License
  16.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  17.  *
  18.  */
  19.  
  20. #include "idle.h"
  21.  
  22. #include <QDBusConnection>
  23. #include <QDBusConnectionInterface>
  24. #include <QDBusInterface>
  25. #include <QDBusMessage>
  26. #include <QDBusMetaType>
  27. #include <QDBusReply>
  28.  
  29. // Screen Saver dbus services
  30. static const QLatin1String COMMON_SS_SERV("org.freedesktop.ScreenSaver");
  31. static const QLatin1String COMMON_SS_PATH("/ScreenSaver");
  32. static const QLatin1String KDE_SS_SERV("org.kde.screensaver");
  33. static const QLatin1String GNOME_SS_SERV("org.gnome.Mutter.IdleMonitor");
  34. static const QLatin1String GNOME_SS_PATH("/org/gnome/Mutter/IdleMonitor/Core");
  35. // Screen saver functions
  36. static const QLatin1String GNOME_SS_F("GetIdletime");
  37. static const QLatin1String COMMON_SS_F("GetSessionIdleTime");
  38.  
  39. class IdlePlatform::Private {
  40. public:
  41.     Private() { }
  42.     QString availableService;
  43.     bool    getServicesAvailable()
  44.     {
  45.         const auto        services     = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
  46.         const QStringList idleServices = { COMMON_SS_SERV, KDE_SS_SERV, GNOME_SS_SERV };
  47.         // find first available dbus-service
  48.         for (const auto &service : idleServices) {
  49.             if (services.contains(service)) {
  50.                 availableService = service;
  51.                 return true;
  52.             }
  53.         }
  54.         return false;
  55.     }
  56. };
  57.  
  58. IdlePlatform::IdlePlatform() { d = new Private; }
  59. IdlePlatform::~IdlePlatform() { delete d; }
  60. bool IdlePlatform::init() { return !d->getServicesAvailable(); }
  61.  
  62. int IdlePlatform::secondsIdle()
  63. {
  64.     // KDE and freedesktop uses the same path interface and method but gnome uses other
  65.     bool                isNotGnome = d->availableService == COMMON_SS_SERV || d->availableService == KDE_SS_SERV;
  66.     const QLatin1String iface      = isNotGnome ? COMMON_SS_SERV : GNOME_SS_SERV;
  67.     const QLatin1String path       = isNotGnome ? COMMON_SS_PATH : GNOME_SS_PATH;
  68.     const QLatin1String method     = isNotGnome ? COMMON_SS_F : GNOME_SS_F;
  69.     auto                interface  = QDBusInterface(d->availableService, path, iface);
  70.     if (interface.isValid()) {
  71.         QDBusReply<int> reply = interface.call(method);
  72.         // probably reply value for freedesktop and kde need to be converted to seconds
  73.         if (reply.isValid())
  74.             return isNotGnome ? reply.value() / 1000 : reply.value();
  75.     }
  76.     return 0;
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement