Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * idle_dbus.cpp - detect desktop idle time
- * Copyright (C) 2022 Vitaly Tonkacheyev
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- *
- */
- #include "idle.h"
- #include <QDBusConnection>
- #include <QDBusConnectionInterface>
- #include <QDBusInterface>
- #include <QDBusMessage>
- #include <QDBusMetaType>
- #include <QDBusReply>
- // Screen Saver dbus services
- static const QLatin1String COMMON_SS_SERV("org.freedesktop.ScreenSaver");
- static const QLatin1String COMMON_SS_PATH("/ScreenSaver");
- static const QLatin1String KDE_SS_SERV("org.kde.screensaver");
- static const QLatin1String GNOME_SS_SERV("org.gnome.Mutter.IdleMonitor");
- static const QLatin1String GNOME_SS_PATH("/org/gnome/Mutter/IdleMonitor/Core");
- // Screen saver functions
- static const QLatin1String GNOME_SS_F("GetIdletime");
- static const QLatin1String COMMON_SS_F("GetSessionIdleTime");
- class IdlePlatform::Private {
- public:
- Private() { }
- QString availableService;
- bool getServicesAvailable()
- {
- const auto services = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
- const QStringList idleServices = { COMMON_SS_SERV, KDE_SS_SERV, GNOME_SS_SERV };
- // find first available dbus-service
- for (const auto &service : idleServices) {
- if (services.contains(service)) {
- availableService = service;
- return true;
- }
- }
- return false;
- }
- };
- IdlePlatform::IdlePlatform() { d = new Private; }
- IdlePlatform::~IdlePlatform() { delete d; }
- bool IdlePlatform::init() { return !d->getServicesAvailable(); }
- int IdlePlatform::secondsIdle()
- {
- // KDE and freedesktop uses the same path interface and method but gnome uses other
- bool isNotGnome = d->availableService == COMMON_SS_SERV || d->availableService == KDE_SS_SERV;
- const QLatin1String iface = isNotGnome ? COMMON_SS_SERV : GNOME_SS_SERV;
- const QLatin1String path = isNotGnome ? COMMON_SS_PATH : GNOME_SS_PATH;
- const QLatin1String method = isNotGnome ? COMMON_SS_F : GNOME_SS_F;
- auto interface = QDBusInterface(d->availableService, path, iface);
- if (interface.isValid()) {
- QDBusReply<int> reply = interface.call(method);
- // probably reply value for freedesktop and kde need to be converted to seconds
- if (reply.isValid())
- return isNotGnome ? reply.value() / 1000 : reply.value();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement