forked from KDE/kscreenlocker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.cpp
205 lines (178 loc) · 5.99 KB
/
interface.cpp
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
/*
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 "kscreensaveradaptor.h"
#include "ksldapp.h"
#include "powerdevilpolicyagent.h"
#include "screensaveradaptor.h"
// KDE
#include <KAuthorized>
#include <KIdleTime>
#include <KWindowSystem>
// Qt
#include <QDBusConnection>
#include <QDBusContext>
#include <QDBusReply>
#include <QDBusServiceWatcher>
#include <QRandomGenerator>
namespace ScreenLocker
{
const uint ChangeScreenSettings = 4;
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)
{
OrgKdeSolidPowerManagementPolicyAgentInterface policyAgent(QStringLiteral("org.kde.Solid.PowerManagement.PolicyAgent"),
QStringLiteral("/org/kde/Solid/PowerManagement/PolicyAgent"),
QDBusConnection::sessionBus());
QDBusReply<uint> reply = policyAgent.AddInhibition(ChangeScreenSettings, application_name, reason_for_inhibit);
InhibitRequest sr;
sr.cookie = m_next_cookie++;
sr.dbusid = message().service();
sr.powerdevilcookie = reply.isValid() ? reply : 0;
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) {
if (uint powerdevilcookie = it.value().powerdevilcookie) {
OrgKdeSolidPowerManagementPolicyAgentInterface policyAgent(QStringLiteral("org.kde.Solid.PowerManagement.PolicyAgent"),
QStringLiteral("/org/kde/Solid/PowerManagement/PolicyAgent"),
QDBusConnection::sessionBus());
policyAgent.ReleaseInhibition(powerdevilcookie);
}
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 : qAsConst(m_lockReplies)) {
QDBusConnection::sessionBus().send(reply);
}
m_lockReplies.clear();
}
} // namespace