File indexing completed on 2024-04-28 05:30:30

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #include "screenlockerwatcher.h"
0010 #include "wayland_server.h"
0011 
0012 // dbus generated
0013 #include "kscreenlocker_interface.h"
0014 #include "screenlocker_interface.h"
0015 
0016 namespace KWin
0017 {
0018 
0019 static const QString SCREEN_LOCKER_SERVICE_NAME = QStringLiteral("org.freedesktop.ScreenSaver");
0020 
0021 ScreenLockerWatcher::ScreenLockerWatcher()
0022     : m_serviceWatcher(new QDBusServiceWatcher(this))
0023     , m_locked(false)
0024 {
0025     if (waylandServer() && waylandServer()->hasScreenLockerIntegration()) {
0026         connect(waylandServer(), &WaylandServer::initialized, this, &ScreenLockerWatcher::initialize);
0027     } else {
0028         initialize();
0029     }
0030 }
0031 
0032 void ScreenLockerWatcher::initialize()
0033 {
0034     connect(m_serviceWatcher, &QDBusServiceWatcher::serviceOwnerChanged, this, &ScreenLockerWatcher::serviceOwnerChanged);
0035     m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange);
0036     m_serviceWatcher->addWatchedService(SCREEN_LOCKER_SERVICE_NAME);
0037 
0038     m_interface = new OrgFreedesktopScreenSaverInterface(SCREEN_LOCKER_SERVICE_NAME, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this);
0039     m_kdeInterface = new OrgKdeScreensaverInterface(SCREEN_LOCKER_SERVICE_NAME, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this);
0040     connect(m_interface, &OrgFreedesktopScreenSaverInterface::ActiveChanged,
0041             this, &ScreenLockerWatcher::setLocked);
0042     connect(m_kdeInterface, &OrgKdeScreensaverInterface::AboutToLock, this, &ScreenLockerWatcher::aboutToLock);
0043 
0044     queryActive();
0045 }
0046 
0047 void ScreenLockerWatcher::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
0048 {
0049     m_locked = false;
0050 
0051     if (!newOwner.isEmpty()) {
0052         queryActive();
0053     }
0054 }
0055 
0056 void ScreenLockerWatcher::queryActive()
0057 {
0058     QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(m_interface->GetActive(), this);
0059     connect(watcher, &QDBusPendingCallWatcher::finished,
0060             this, &ScreenLockerWatcher::activeQueried);
0061 }
0062 
0063 void ScreenLockerWatcher::activeQueried(QDBusPendingCallWatcher *watcher)
0064 {
0065     QDBusPendingReply<bool> reply = *watcher;
0066     if (!reply.isError()) {
0067         setLocked(reply.value());
0068     }
0069     watcher->deleteLater();
0070 }
0071 
0072 void ScreenLockerWatcher::setLocked(bool activated)
0073 {
0074     if (m_locked == activated) {
0075         return;
0076     }
0077     m_locked = activated;
0078     Q_EMIT locked(m_locked);
0079 }
0080 
0081 bool ScreenLockerWatcher::isLocked() const
0082 {
0083     return m_locked;
0084 }
0085 }
0086 
0087 #include "moc_screenlockerwatcher.cpp"