KWin
Loading...
Searching...
No Matches
screenlockerwatcher.cpp
Go to the documentation of this file.
1/*
2 KWin - the KDE window manager
3 This file is part of the KDE project.
4
5 SPDX-FileCopyrightText: 2013 Martin Gräßlin <mgraesslin@kde.org>
6
7 SPDX-License-Identifier: GPL-2.0-or-later
8*/
10#include "wayland_server.h"
11
12// dbus generated
13#include "kscreenlocker_interface.h"
14#include "screenlocker_interface.h"
15
16namespace KWin
17{
18
19static const QString SCREEN_LOCKER_SERVICE_NAME = QStringLiteral("org.freedesktop.ScreenSaver");
20
22 : m_serviceWatcher(new QDBusServiceWatcher(this))
23 , m_locked(false)
24{
25 if (waylandServer() && waylandServer()->hasScreenLockerIntegration()) {
26 connect(waylandServer(), &WaylandServer::initialized, this, &ScreenLockerWatcher::initialize);
27 } else {
28 initialize();
29 }
30}
31
32void ScreenLockerWatcher::initialize()
33{
34 connect(m_serviceWatcher, &QDBusServiceWatcher::serviceOwnerChanged, this, &ScreenLockerWatcher::serviceOwnerChanged);
35 m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange);
36 m_serviceWatcher->addWatchedService(SCREEN_LOCKER_SERVICE_NAME);
37
38 m_interface = new OrgFreedesktopScreenSaverInterface(SCREEN_LOCKER_SERVICE_NAME, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this);
39 m_kdeInterface = new OrgKdeScreensaverInterface(SCREEN_LOCKER_SERVICE_NAME, QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this);
40 connect(m_interface, &OrgFreedesktopScreenSaverInterface::ActiveChanged,
41 this, &ScreenLockerWatcher::setLocked);
42 connect(m_kdeInterface, &OrgKdeScreensaverInterface::AboutToLock, this, &ScreenLockerWatcher::aboutToLock);
43
44 queryActive();
45}
46
47void ScreenLockerWatcher::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
48{
49 m_locked = false;
50
51 if (!newOwner.isEmpty()) {
52 queryActive();
53 }
54}
55
56void ScreenLockerWatcher::queryActive()
57{
58 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(m_interface->GetActive(), this);
59 connect(watcher, &QDBusPendingCallWatcher::finished,
60 this, &ScreenLockerWatcher::activeQueried);
61}
62
63void ScreenLockerWatcher::activeQueried(QDBusPendingCallWatcher *watcher)
64{
65 QDBusPendingReply<bool> reply = *watcher;
66 if (!reply.isError()) {
67 setLocked(reply.value());
68 }
69 watcher->deleteLater();
70}
71
72void ScreenLockerWatcher::setLocked(bool activated)
73{
74 if (m_locked == activated) {
75 return;
76 }
77 m_locked = activated;
78 Q_EMIT locked(m_locked);
79}
80
82{
83 return m_locked;
84}
85}
86
87#include "moc_screenlockerwatcher.cpp"
void locked(bool locked)
WaylandServer * waylandServer()