on ec409dc help sign in
unity8/plugins/Unity/Session/dbusunitysessionservice.cpp
473 lines 14 kB history raw
1/*
2 * Copyright (C) 2014, 2015 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17// local
18#include "dbusunitysessionservice.h"
20// system
21#include <sys/types.h>
22#include <unistd.h>
23#include <pwd.h>
25// Qt
26#include <QDebug>
27#include <QDBusPendingCall>
28#include <QDBusReply>
29#include <QElapsedTimer>
30#include <QDateTime>
32#define LOGIN1_SERVICE QStringLiteral("org.freedesktop.login1")
33#define LOGIN1_PATH QStringLiteral("/org/freedesktop/login1")
34#define LOGIN1_IFACE QStringLiteral("org.freedesktop.login1.Manager")
35#define LOGIN1_SESSION_IFACE QStringLiteral("org.freedesktop.login1.Session")
37#define ACTIVE_KEY QStringLiteral("Active")
38#define IDLE_SINCE_KEY QStringLiteral("IdleSinceHint")
40class DBusUnitySessionServicePrivate: public QObject
42 Q_OBJECT
43public:
44 QString logindSessionPath;
45 bool isSessionActive = true;
46 QElapsedTimer screensaverActiveTimer;
48 DBusUnitySessionServicePrivate(): QObject() {
49 init();
50 checkActive();
51 }
53 void init()
54 {
55 // get our logind session path
56 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
57 LOGIN1_PATH,
58 LOGIN1_IFACE,
59 "GetSessionByPID");
60 msg << (quint32) getpid();
62 QDBusReply<QDBusObjectPath> reply = QDBusConnection::systemBus().asyncCall(msg);
63 if (reply.isValid()) {
64 logindSessionPath = reply.value().path();
66 // start watching the Active property
67 QDBusConnection::systemBus().connect(LOGIN1_SERVICE, logindSessionPath, "org.freedesktop.DBus.Properties", "PropertiesChanged",
68 this, SLOT(onPropertiesChanged(QString,QVariantMap,QStringList)));
69 } else {
70 qWarning() << "Failed to get logind session path" << reply.error().message();
71 }
72 }
74 bool checkLogin1Call(const QString &method) const
75 {
76 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE, LOGIN1_PATH, LOGIN1_IFACE, method);
77 QDBusReply<QString> reply = QDBusConnection::systemBus().asyncCall(msg);
78 return reply.isValid() && (reply == QStringLiteral("yes") || reply == QStringLiteral("challenge"));
79 }
81 void makeLogin1Call(const QString &method, const QVariantList &args)
82 {
83 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
84 LOGIN1_PATH,
85 LOGIN1_IFACE,
86 method);
87 msg.setArguments(args);
88 QDBusConnection::systemBus().asyncCall(msg);
89 }
91 void checkActive()
92 {
93 if (logindSessionPath.isEmpty()) {
94 qWarning() << "Invalid session path";
95 return;
96 }
98 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
99 logindSessionPath,
100 "org.freedesktop.DBus.Properties",
101 "Get");
102 msg << LOGIN1_SESSION_IFACE;
103 msg << ACTIVE_KEY;
105 QDBusReply<QVariant> reply = QDBusConnection::systemBus().asyncCall(msg);
106 if (reply.isValid()) {
107 isSessionActive = reply.value().toBool();
108 qDebug() << "Session" << logindSessionPath << "is active:" << isSessionActive;
109 } else {
110 qWarning() << "Failed to get Active property" << reply.error().message();
111 }
112 }
114 quint32 screensaverActiveTime() const
115 {
116 if (!isSessionActive && screensaverActiveTimer.isValid()) {
117 return screensaverActiveTimer.elapsed() / 1000;
118 }
120 return 0;
121 }
123 quint64 idleSinceUSecTimestamp() const
124 {
125 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
126 logindSessionPath,
127 "org.freedesktop.DBus.Properties",
128 "Get");
129 msg << LOGIN1_SESSION_IFACE;
130 msg << IDLE_SINCE_KEY;
132 QDBusReply<QVariant> reply = QDBusConnection::systemBus().asyncCall(msg);
133 if (reply.isValid()) {
134 return reply.value().value<quint64>();
135 } else {
136 qWarning() << "Failed to get IdleSinceHint property" << reply.error().message();
137 }
139 return 0;
140 }
142 void setIdleHint(bool idle)
143 {
144 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
145 logindSessionPath,
146 LOGIN1_SESSION_IFACE,
147 "SetIdleHint");
148 msg << idle;
149 QDBusConnection::systemBus().asyncCall(msg);
150 }
152private Q_SLOTS:
153 void onPropertiesChanged(const QString &iface, const QVariantMap &changedProps, const QStringList &invalidatedProps)
154 {
155 Q_UNUSED(iface)
157 if (changedProps.contains(ACTIVE_KEY) || invalidatedProps.contains(ACTIVE_KEY)) {
158 if (changedProps.value(ACTIVE_KEY).isValid()) {
159 isSessionActive = changedProps.value(ACTIVE_KEY).toBool();
160 } else {
161 checkActive();
162 }
164 Q_EMIT screensaverActiveChanged(!isSessionActive);
166 if (isSessionActive) {
167 screensaverActiveTimer.invalidate();
168 setIdleHint(false);
169 } else {
170 screensaverActiveTimer.start();
171 setIdleHint(true);
172 }
173 }
174 }
176Q_SIGNALS:
177 void screensaverActiveChanged(bool active);
178};
180Q_GLOBAL_STATIC(DBusUnitySessionServicePrivate, d)
182DBusUnitySessionService::DBusUnitySessionService()
183 : UnityDBusObject("/com/canonical/Unity/Session", "com.canonical.Unity")
185 if (!d->logindSessionPath.isEmpty()) {
186 // connect our Lock() slot to the logind's session Lock() signal
187 QDBusConnection::systemBus().connect(LOGIN1_SERVICE, d->logindSessionPath, LOGIN1_SESSION_IFACE, "Lock", this, SLOT(Lock()));
188 // ... and our Unlocked() signal to the logind's session Unlock() signal
189 // (lightdm handles the unlocking by calling logind's Unlock method which in turn emits this signal we connect to)
190 QDBusConnection::systemBus().connect(LOGIN1_SERVICE, d->logindSessionPath, LOGIN1_SESSION_IFACE, "Unlock", this, SIGNAL(Unlocked()));
191 } else {
192 qWarning() << "Failed to connect to logind's session Lock/Unlock signals";
193 }
196void DBusUnitySessionService::Logout()
198 // TODO ask the apps to quit and then emit the signal
199 Q_EMIT LogoutReady();
200 Q_EMIT logoutReady();
203void DBusUnitySessionService::EndSession()
205 const QDBusMessage msg = QDBusMessage::createMethodCall("com.ubuntu.Upstart",
206 "/com/ubuntu/Upstart",
207 "com.ubuntu.Upstart0_6",
208 "EndSession");
209 QDBusConnection::sessionBus().asyncCall(msg);
212bool DBusUnitySessionService::CanHibernate() const
214 return d->checkLogin1Call("CanHibernate");
217bool DBusUnitySessionService::CanSuspend() const
219 return d->checkLogin1Call("CanSuspend");
222bool DBusUnitySessionService::CanHybridSleep() const
224 return d->checkLogin1Call("CanHybridSleep");
227bool DBusUnitySessionService::CanReboot() const
229 return d->checkLogin1Call("CanReboot");
232bool DBusUnitySessionService::CanShutdown() const
234 return d->checkLogin1Call("CanPowerOff");
237bool DBusUnitySessionService::CanLock() const
239 return true; // FIXME
242QString DBusUnitySessionService::UserName() const
244 struct passwd *p = getpwuid(geteuid());
245 if (p) {
246 return QString::fromUtf8(p->pw_name);
247 }
249 return QString();
252QString DBusUnitySessionService::RealName() const
254 struct passwd *p = getpwuid(geteuid());
255 if (p) {
256 const QString gecos = QString::fromLocal8Bit(p->pw_gecos);
257 if (!gecos.isEmpty()) {
258 return gecos.split(QLatin1Char(',')).first();
259 }
260 }
262 return QString();
265QString DBusUnitySessionService::HostName() const
267 char hostName[512];
268 if (gethostname(hostName, sizeof(hostName)) == -1) {
269 qWarning() << "Could not determine local hostname";
270 return QString();
271 }
272 hostName[sizeof(hostName) - 1] = '\0';
273 return QString::fromLocal8Bit(hostName);
276void DBusUnitySessionService::PromptLock()
278 Q_EMIT LockRequested();
279 Q_EMIT lockRequested();
282void DBusUnitySessionService::Lock()
284 // lock the session using the org.freedesktop.DisplayManager system DBUS service
285 const QString sessionPath = QString::fromLocal8Bit(qgetenv("XDG_SESSION_PATH"));
286 QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.DisplayManager",
287 sessionPath,
288 "org.freedesktop.DisplayManager.Session",
289 "Lock");
290 qDebug() << "Locking session" << msg.path();
291 QDBusReply<void> reply = QDBusConnection::systemBus().asyncCall(msg);
292 if (!reply.isValid()) {
293 qWarning() << "Lock call failed" << reply.error().message();
294 } else {
295 // emit Locked when the call succeeds
296 Q_EMIT Locked();
297 }
300bool DBusUnitySessionService::IsLocked() const
302 return !d->isSessionActive;
305void DBusUnitySessionService::RequestLogout()
307 Q_EMIT LogoutRequested(false);
308 Q_EMIT logoutRequested(false);
311void DBusUnitySessionService::Reboot()
313 d->makeLogin1Call("Reboot", {false});
316void DBusUnitySessionService::RequestReboot()
318 Q_EMIT RebootRequested(false);
319 Q_EMIT rebootRequested(false);
322void DBusUnitySessionService::Shutdown()
324 d->makeLogin1Call("PowerOff", {false});
327void DBusUnitySessionService::Suspend()
329 d->makeLogin1Call("Suspend", {false});
332void DBusUnitySessionService::Hibernate()
334 d->makeLogin1Call("Hibernate", {false});
337void DBusUnitySessionService::HybridSleep()
339 d->makeLogin1Call("HybridSleep", {false});
342void DBusUnitySessionService::RequestShutdown()
344 Q_EMIT ShutdownRequested(false);
345 Q_EMIT shutdownRequested(false);
348enum class Action : unsigned
350 LOGOUT = 0,
351 SHUTDOWN,
352 REBOOT,
353 NONE
354};
357void performAsyncUnityCall(const QString &method)
359 const QDBusMessage msg = QDBusMessage::createMethodCall("com.canonical.Unity",
360 "/com/canonical/Unity/Session",
361 "com.canonical.Unity.Session",
362 method);
363 QDBusConnection::sessionBus().asyncCall(msg);
367DBusGnomeSessionManagerWrapper::DBusGnomeSessionManagerWrapper()
368 : UnityDBusObject("/org/gnome/SessionManager/EndSessionDialog", "com.canonical.Unity")
372void DBusGnomeSessionManagerWrapper::Open(const unsigned type, const unsigned arg_1, const unsigned max_wait, const QList<QDBusObjectPath> &inhibitors)
374 Q_UNUSED(arg_1);
375 Q_UNUSED(max_wait);
376 Q_UNUSED(inhibitors);
378 switch (static_cast<Action>(type))
379 {
380 case Action::LOGOUT:
381 performAsyncUnityCall("RequestLogout");
382 break;
384 case Action::REBOOT:
385 performAsyncUnityCall("RequestReboot");
386 break;
388 case Action::SHUTDOWN:
389 performAsyncUnityCall("RequestShutdown");
390 break;
392 default:
393 break;
394 }
398DBusGnomeScreensaverWrapper::DBusGnomeScreensaverWrapper()
399 : UnityDBusObject("/org/gnome/ScreenSaver", "org.gnome.ScreenSaver")
401 connect(d, &DBusUnitySessionServicePrivate::screensaverActiveChanged, this, &DBusGnomeScreensaverWrapper::ActiveChanged);
404bool DBusGnomeScreensaverWrapper::GetActive() const
406 return !d->isSessionActive; // return whether the session is not active
409void DBusGnomeScreensaverWrapper::SetActive(bool lock)
411 if (lock) {
412 Lock();
413 }
416void DBusGnomeScreensaverWrapper::Lock()
418 performAsyncUnityCall("Lock");
421quint32 DBusGnomeScreensaverWrapper::GetActiveTime() const
423 return d->screensaverActiveTime();
426void DBusGnomeScreensaverWrapper::SimulateUserActivity()
428 d->setIdleHint(false);
432DBusScreensaverWrapper::DBusScreensaverWrapper()
433 : UnityDBusObject("/org/freedesktop/ScreenSaver", "org.freedesktop.ScreenSaver")
435 QDBusConnection::sessionBus().registerObject("/ScreenSaver", this, QDBusConnection::ExportScriptableContents); // compat path, also register here
436 connect(d, &DBusUnitySessionServicePrivate::screensaverActiveChanged, this, &DBusScreensaverWrapper::ActiveChanged);
439bool DBusScreensaverWrapper::GetActive() const
441 return !d->isSessionActive; // return whether the session is not active
444bool DBusScreensaverWrapper::SetActive(bool lock)
446 if (lock) {
447 Lock();
448 return true;
449 }
450 return false;
453void DBusScreensaverWrapper::Lock()
455 performAsyncUnityCall("Lock");
458quint32 DBusScreensaverWrapper::GetActiveTime() const
460 return d->screensaverActiveTime();
463quint32 DBusScreensaverWrapper::GetSessionIdleTime() const
465 return QDateTime::fromMSecsSinceEpoch(d->idleSinceUSecTimestamp()/1000).secsTo(QDateTime::currentDateTime());
468void DBusScreensaverWrapper::SimulateUserActivity()
470 d->setIdleHint(false);
473#include "dbusunitysessionservice.moc"