File: interface.cpp

package info (click to toggle)
kscreenlocker 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,172 kB
  • sloc: cpp: 5,698; xml: 88; sh: 32; makefile: 5
file content (253 lines) | stat: -rw-r--r-- 7,253 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
SPDX-FileCopyrightText: 1999 Martin R. Jones <mjones@kde.org>
SPDX-FileCopyrightText: 2011 Martin Gräßlin <mgraesslin@kde.org>

SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "interface.h"
#include "kscreenlocker_logging.h"
#include "kscreensaveradaptor.h"
#include "ksldapp.h"
#include "powerdevilpolicyagent.h"
#include "screensaveradaptor.h"
// KDE
#include <KAuthorized>
#include <KIdleTime>
#include <KWindowSystem>
// Qt
#include <QDBusConnection>
#include <QDBusContext>
#include <QDBusPendingCallWatcher>
#include <QDBusReply>
#include <QDBusServiceWatcher>
#include <QRandomGenerator>

namespace ScreenLocker
{
const uint ChangeScreenSettings = 4;

PowerInhibitor::PowerInhibitor(const QString &applicationName, const QString &reason)
{
    inhibit(applicationName, reason);
}

PowerInhibitor::~PowerInhibitor()
{
}

void PowerInhibitor::release()
{
    m_released = true;
    if (m_cookie.has_value()) {
        uninhibit();
    }
}

void PowerInhibitor::inhibit(const QString &applicationName, const QString &reason)
{
    OrgKdeSolidPowerManagementPolicyAgentInterface policyAgent(QStringLiteral("org.kde.Solid.PowerManagement.PolicyAgent"),
                                                               QStringLiteral("/org/kde/Solid/PowerManagement/PolicyAgent"),
                                                               QDBusConnection::sessionBus());
    QDBusPendingReply<uint> reply = policyAgent.AddInhibition(ChangeScreenSettings, applicationName, reason);

    auto watcher = new QDBusPendingCallWatcher(reply, this);
    connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, watcher]() {
        QDBusPendingReply<uint> reply = *watcher;
        watcher->deleteLater();

        if (!reply.isValid()) {
            qCWarning(KSCREENLOCKER) << "org.kde.Solid.PowerManagement.PolicyAgent.AddInhibition failed:" << reply.error();
            m_cookie = std::unexpected(reply.error());
        } else {
            m_cookie = reply.value();
        }

        if (m_released) {
            uninhibit();
        }
    });
}

void PowerInhibitor::uninhibit()
{
    if (const auto &result = m_cookie.value()) {
        OrgKdeSolidPowerManagementPolicyAgentInterface policyAgent(QStringLiteral("org.kde.Solid.PowerManagement.PolicyAgent"),
                                                                   QStringLiteral("/org/kde/Solid/PowerManagement/PolicyAgent"),
                                                                   QDBusConnection::sessionBus());
        policyAgent.ReleaseInhibition(result.value());
    }

    deleteLater();
}

Interface::Interface(KSldApp *parent)
    : QObject(parent)
    , m_daemon(parent)
    , m_serviceWatcher(new QDBusServiceWatcher(this))
    , m_next_cookie(0)
{
    (void)new ScreenSaverAdaptor(this);
    QDBusConnection::sessionBus().registerService(QStringLiteral("org.freedesktop.ScreenSaver"));
    (void)new KScreenSaverAdaptor(this);
    QDBusConnection::sessionBus().registerService(QStringLiteral("org.kde.screensaver"));
    QDBusConnection::sessionBus().registerObject(QStringLiteral("/ScreenSaver"), this);
    QDBusConnection::sessionBus().registerObject(QStringLiteral("/org/freedesktop/ScreenSaver"), this);
    connect(m_daemon, &KSldApp::locked, this, &Interface::slotLocked);
    connect(m_daemon, &KSldApp::unlocked, this, &Interface::slotUnlocked);
    connect(m_daemon, &KSldApp::aboutToLock, this, &Interface::AboutToLock);

    m_serviceWatcher->setConnection(QDBusConnection::sessionBus());
    m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
    connect(m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, &Interface::serviceUnregistered);

    // I make it a really random number to avoid
    // some assumptions in clients, but just increase
    // while gnome-ss creates a random number every time
    m_next_cookie = QRandomGenerator::global()->bounded(19999);
}

Interface::~Interface()
{
}

bool Interface::GetActive()
{
    return m_daemon->lockState() == KSldApp::Locked;
}

uint Interface::GetActiveTime()
{
    return m_daemon->activeTime();
}

uint Interface::GetSessionIdleTime()
{
    if (KWindowSystem::isPlatformWayland()) {
        QDBusContext::sendErrorReply(QDBusError::NotSupported, QStringLiteral("GetSessionIdleTime is not supported on this platform"));
        return 0;
    }
    return KIdleTime::instance()->idleTime();
}

void Interface::Lock()
{
    if (!KAuthorized::authorizeAction(QStringLiteral("lock_screen"))) {
        return;
    }
    m_daemon->lock(calledFromDBus() ? EstablishLock::Immediate : EstablishLock::Delayed);

    if (calledFromDBus() && m_daemon->lockState() == KSldApp::AcquiringLock) {
        m_lockReplies << message().createReply();
        setDelayedReply(true);
    }
}

void Interface::SwitchUser()
{
    if (!KAuthorized::authorizeAction(QStringLiteral("lock_screen"))) {
        return;
    }
    m_daemon->lock(EstablishLock::DefaultToSwitchUser);

    if (calledFromDBus() && m_daemon->lockState() == KSldApp::AcquiringLock) {
        m_lockReplies << message().createReply();
        setDelayedReply(true);
    }
}

bool Interface::SetActive(bool state)
{
    // TODO: what should the return value be?
    if (state) {
        Lock();
        return true;
    }
    // set inactive is ignored
    return false;
}

uint Interface::Inhibit(const QString &application_name, const QString &reason_for_inhibit)
{
    InhibitRequest sr;
    sr.cookie = m_next_cookie++;
    sr.dbusid = message().service();
    sr.powerInhibitor = new PowerInhibitor(application_name, reason_for_inhibit);
    m_requests.append(sr);
    m_serviceWatcher->addWatchedService(sr.dbusid);
    KSldApp::self()->inhibit();
    return sr.cookie;
}

void Interface::UnInhibit(uint cookie)
{
    QMutableListIterator<InhibitRequest> it(m_requests);
    while (it.hasNext()) {
        if (it.next().cookie == cookie) {
            it.value().powerInhibitor->release();
            it.remove();
            KSldApp::self()->uninhibit();
            break;
        }
    }
}

void Interface::serviceUnregistered(const QString &name)
{
    m_serviceWatcher->removeWatchedService(name);
    QListIterator<InhibitRequest> it(m_requests);
    while (it.hasNext()) {
        const InhibitRequest &r = it.next();
        if (r.dbusid == name) {
            UnInhibit(r.cookie);
        }
    }
}

void Interface::SimulateUserActivity()
{
    KIdleTime::instance()->simulateUserActivity();
}

uint Interface::Throttle(const QString &application_name, const QString &reason_for_inhibit)
{
    Q_UNUSED(application_name)
    Q_UNUSED(reason_for_inhibit)
    // TODO: implement me
    return 0;
}

void Interface::UnThrottle(uint cookie)
{
    Q_UNUSED(cookie)
    // TODO: implement me
}

void Interface::slotLocked()
{
    sendLockReplies();
    Q_EMIT ActiveChanged(true);
}

void Interface::slotUnlocked()
{
    sendLockReplies();
    Q_EMIT ActiveChanged(false);
}

void Interface::configure()
{
    m_daemon->configure();
}

void Interface::sendLockReplies()
{
    for (const QDBusMessage &reply : std::as_const(m_lockReplies)) {
        QDBusConnection::sessionBus().send(reply);
    }

    m_lockReplies.clear();
}

} // namespace

#include "moc_interface.cpp"