2 * Copyright (C) 2014, 2015 Canonical, Ltd.
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.
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.
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/>.
18#include "dbusunitysessionservice.h"
27#include <QDBusPendingCall>
29#include <QElapsedTimer>
31#include <QDBusUnixFileDescriptor>
33#define LOGIN1_SERVICE QStringLiteral("org.freedesktop.login1")
34#define LOGIN1_PATH QStringLiteral("/org/freedesktop/login1")
35#define LOGIN1_IFACE QStringLiteral("org.freedesktop.login1.Manager")
36#define LOGIN1_SESSION_IFACE QStringLiteral("org.freedesktop.login1.Session")
38#define ACTIVE_KEY QStringLiteral("Active")
39#define IDLE_SINCE_KEY QStringLiteral("IdleSinceHint")
41class DBusUnitySessionServicePrivate: public QObject
45 QString logindSessionPath;
46 bool isSessionActive = true;
47 QElapsedTimer screensaverActiveTimer;
48 QDBusUnixFileDescriptor m_systemdInhibitFd;
50 DBusUnitySessionServicePrivate(): QObject() {
57 // get our logind session path
58 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
61 QStringLiteral("GetSessionByPID"));
62 msg << (quint32) getpid();
64 QDBusReply<QDBusObjectPath> reply = QDBusConnection::systemBus().call(msg);
65 if (reply.isValid()) {
66 logindSessionPath = reply.value().path();
68 // start watching the Active property
69 QDBusConnection::systemBus().connect(LOGIN1_SERVICE, logindSessionPath, QStringLiteral("org.freedesktop.DBus.Properties"), QStringLiteral("PropertiesChanged"),
70 this, SLOT(onPropertiesChanged(QString,QVariantMap,QStringList)));
72 setupSystemdInhibition();
74 // re-enable the inhibition upon resume from sleep
75 QDBusConnection::systemBus().connect(LOGIN1_SERVICE, LOGIN1_PATH, LOGIN1_IFACE, QStringLiteral("PrepareForSleep"),
76 this, SLOT(onResuming(bool)));
78 qWarning() << "Failed to get logind session path" << reply.error().message();
82 void setupSystemdInhibition()
84 if (m_systemdInhibitFd.isValid())
87 // inhibit systemd handling of power/sleep/hibernate buttons
88 // http://www.freedesktop.org/wiki/Software/systemd/inhibit
90 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE, LOGIN1_PATH, LOGIN1_IFACE, QStringLiteral("Inhibit"));
91 msg << "handle-power-key:handle-suspend-key:handle-hibernate-key"; // what
92 msg << "Unity"; // who
93 msg << "Unity8 handles power events"; // why
94 msg << "block"; // mode
96 QDBusPendingCall pendingCall = QDBusConnection::systemBus().asyncCall(msg);
97 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this);
98 connect(watcher, &QDBusPendingCallWatcher::finished,
99 this, [this](QDBusPendingCallWatcher* watcher) {
100 QDBusPendingReply<QDBusUnixFileDescriptor> reply = *watcher;
101 watcher->deleteLater();
102 if (reply.isError()) {
103 qWarning() << "Failed to inhibit systemd powersave handling" << reply.error().message();
107 m_systemdInhibitFd = reply.value();
111 bool checkLogin1Call(const QString &method) const
113 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE, LOGIN1_PATH, LOGIN1_IFACE, method);
114 QDBusReply<QString> reply = QDBusConnection::systemBus().call(msg);
115 return reply.isValid() && (reply == QStringLiteral("yes") || reply == QStringLiteral("challenge"));
118 void makeLogin1Call(const QString &method, const QVariantList &args)
120 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
124 msg.setArguments(args);
125 QDBusConnection::systemBus().asyncCall(msg);
128 void setActive(bool active)
130 isSessionActive = active;
132 Q_EMIT screensaverActiveChanged(!isSessionActive);
134 if (isSessionActive) {
135 screensaverActiveTimer.invalidate();
138 screensaverActiveTimer.start();
145 if (logindSessionPath.isEmpty()) {
146 qWarning() << "Invalid session path";
150 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
152 QStringLiteral("org.freedesktop.DBus.Properties"),
153 QStringLiteral("Get"));
154 msg << LOGIN1_SESSION_IFACE;
157 QDBusPendingCall pendingCall = QDBusConnection::systemBus().asyncCall(msg);
158 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this);
159 connect(watcher, &QDBusPendingCallWatcher::finished,
160 this, [this](QDBusPendingCallWatcher* watcher) {
162 QDBusPendingReply<QVariant> reply = *watcher;
163 watcher->deleteLater();
164 if (reply.isError()) {
165 qWarning() << "Failed to get Active property" << reply.error().message();
169 setActive(reply.value().toBool());
173 quint32 screensaverActiveTime() const
175 if (!isSessionActive && screensaverActiveTimer.isValid()) {
176 return screensaverActiveTimer.elapsed() / 1000;
182 quint64 idleSinceUSecTimestamp() const
184 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
186 QStringLiteral("org.freedesktop.DBus.Properties"),
187 QStringLiteral("Get"));
188 msg << LOGIN1_SESSION_IFACE;
189 msg << IDLE_SINCE_KEY;
191 QDBusReply<QVariant> reply = QDBusConnection::systemBus().call(msg);
192 if (reply.isValid()) {
193 return reply.value().value<quint64>();
195 qWarning() << "Failed to get IdleSinceHint property" << reply.error().message();
201 void setIdleHint(bool idle)
203 QDBusMessage msg = QDBusMessage::createMethodCall(LOGIN1_SERVICE,
205 LOGIN1_SESSION_IFACE,
206 QStringLiteral("SetIdleHint"));
208 QDBusConnection::systemBus().asyncCall(msg);
212 void onPropertiesChanged(const QString &iface, const QVariantMap &changedProps, const QStringList &invalidatedProps)
216 if (changedProps.contains(ACTIVE_KEY)) {
217 setActive(changedProps.value(ACTIVE_KEY).toBool());
218 } else if (invalidatedProps.contains(ACTIVE_KEY)) {
223 void onResuming(bool active)
226 setupSystemdInhibition();
231 void screensaverActiveChanged(bool active);
234Q_GLOBAL_STATIC(DBusUnitySessionServicePrivate, d)
236DBusUnitySessionService::DBusUnitySessionService()
237 : UnityDBusObject(QStringLiteral("/com/canonical/Unity/Session"), QStringLiteral("com.canonical.Unity"))
239 if (!d->logindSessionPath.isEmpty()) {
240 // connect our Lock() slot to the logind's session Lock() signal
241 QDBusConnection::systemBus().connect(LOGIN1_SERVICE, d->logindSessionPath, LOGIN1_SESSION_IFACE, QStringLiteral("Lock"), this, SLOT(Lock()));
242 // ... and our Unlocked() signal to the logind's session Unlock() signal
243 // (lightdm handles the unlocking by calling logind's Unlock method which in turn emits this signal we connect to)
244 QDBusConnection::systemBus().connect(LOGIN1_SERVICE, d->logindSessionPath, LOGIN1_SESSION_IFACE, QStringLiteral("Unlock"), this, SIGNAL(Unlocked()));
246 qWarning() << "Failed to connect to logind's session Lock/Unlock signals";
250void DBusUnitySessionService::Logout()
252 // TODO ask the apps to quit and then emit the signal
253 Q_EMIT LogoutReady();
254 Q_EMIT logoutReady();
257void DBusUnitySessionService::EndSession()
259 const QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("com.ubuntu.Upstart"),
260 QStringLiteral("/com/ubuntu/Upstart"),
261 QStringLiteral("com.ubuntu.Upstart0_6"),
262 QStringLiteral("EndSession"));
263 QDBusConnection::sessionBus().asyncCall(msg);
266bool DBusUnitySessionService::CanHibernate() const
268 return d->checkLogin1Call(QStringLiteral("CanHibernate"));
271bool DBusUnitySessionService::CanSuspend() const
273 return d->checkLogin1Call(QStringLiteral("CanSuspend"));
276bool DBusUnitySessionService::CanHybridSleep() const
278 return d->checkLogin1Call(QStringLiteral("CanHybridSleep"));
281bool DBusUnitySessionService::CanReboot() const
283 return d->checkLogin1Call(QStringLiteral("CanReboot"));
286bool DBusUnitySessionService::CanShutdown() const
288 return d->checkLogin1Call(QStringLiteral("CanPowerOff"));
291bool DBusUnitySessionService::CanLock() const
293 return true; // FIXME
296QString DBusUnitySessionService::UserName() const
298 struct passwd *p = getpwuid(geteuid());
300 return QString::fromUtf8(p->pw_name);
306QString DBusUnitySessionService::RealName() const
308 struct passwd *p = getpwuid(geteuid());
310 const QString gecos = QString::fromLocal8Bit(p->pw_gecos);
311 if (!gecos.isEmpty()) {
312 const QStringList splitGecos = gecos.split(QLatin1Char(','));
313 return splitGecos.first();
320QString DBusUnitySessionService::HostName() const
323 if (gethostname(hostName, sizeof(hostName)) == -1) {
324 qWarning() << "Could not determine local hostname";
327 hostName[sizeof(hostName) - 1] = '\0';
328 return QString::fromLocal8Bit(hostName);
331void DBusUnitySessionService::PromptLock()
333 Q_EMIT LockRequested();
334 Q_EMIT lockRequested();
337void DBusUnitySessionService::Lock()
339 // lock the session using the org.freedesktop.DisplayManager system DBUS service
340 const QString sessionPath = QString::fromLocal8Bit(qgetenv("XDG_SESSION_PATH"));
341 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.DisplayManager"),
343 QStringLiteral("org.freedesktop.DisplayManager.Session"),
344 QStringLiteral("Lock"));
346 QDBusPendingCall pendingCall = QDBusConnection::systemBus().asyncCall(msg);
347 QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(pendingCall, this);
348 connect(watcher, &QDBusPendingCallWatcher::finished,
349 this, [this](QDBusPendingCallWatcher* watcher) {
351 QDBusPendingReply<QVariant> reply = *watcher;
352 watcher->deleteLater();
353 if (reply.isError()) {
354 qWarning() << "Lock call failed" << reply.error().message();
358 // emit Locked when the call succeeds
363bool DBusUnitySessionService::IsLocked() const
365 return !d->isSessionActive;
368void DBusUnitySessionService::RequestLogout()
370 Q_EMIT LogoutRequested(false);
371 Q_EMIT logoutRequested(false);
374void DBusUnitySessionService::Reboot()
376 d->makeLogin1Call(QStringLiteral("Reboot"), {false});
379void DBusUnitySessionService::RequestReboot()
381 Q_EMIT RebootRequested(false);
382 Q_EMIT rebootRequested(false);
385void DBusUnitySessionService::Shutdown()
387 d->makeLogin1Call(QStringLiteral("PowerOff"), {false});
390void DBusUnitySessionService::Suspend()
392 d->makeLogin1Call(QStringLiteral("Suspend"), {false});
395void DBusUnitySessionService::Hibernate()
397 d->makeLogin1Call(QStringLiteral("Hibernate"), {false});
400void DBusUnitySessionService::HybridSleep()
402 d->makeLogin1Call(QStringLiteral("HybridSleep"), {false});
405void DBusUnitySessionService::RequestShutdown()
407 Q_EMIT ShutdownRequested(false);
408 Q_EMIT shutdownRequested(false);
411enum class Action : unsigned
420void performAsyncUnityCall(const QString &method)
422 const QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("com.canonical.Unity"),
423 QStringLiteral("/com/canonical/Unity/Session"),
424 QStringLiteral("com.canonical.Unity.Session"),
426 QDBusConnection::sessionBus().asyncCall(msg);
430DBusGnomeSessionManagerWrapper::DBusGnomeSessionManagerWrapper()
431 : UnityDBusObject(QStringLiteral("/org/gnome/SessionManager/EndSessionDialog"), QStringLiteral("com.canonical.Unity"))
435void DBusGnomeSessionManagerWrapper::Open(const unsigned type, const unsigned arg_1, const unsigned max_wait, const QList<QDBusObjectPath> &inhibitors)
439 Q_UNUSED(inhibitors);
441 switch (static_cast<Action>(type))
444 performAsyncUnityCall(QStringLiteral("RequestLogout"));
448 performAsyncUnityCall(QStringLiteral("RequestReboot"));
451 case Action::SHUTDOWN:
452 performAsyncUnityCall(QStringLiteral("RequestShutdown"));
461DBusGnomeScreensaverWrapper::DBusGnomeScreensaverWrapper()
462 : UnityDBusObject(QStringLiteral("/org/gnome/ScreenSaver"), QStringLiteral("org.gnome.ScreenSaver"))
464 connect(d, &DBusUnitySessionServicePrivate::screensaverActiveChanged, this, &DBusGnomeScreensaverWrapper::ActiveChanged);
467bool DBusGnomeScreensaverWrapper::GetActive() const
469 return !d->isSessionActive; // return whether the session is not active
472void DBusGnomeScreensaverWrapper::SetActive(bool lock)
479void DBusGnomeScreensaverWrapper::Lock()
481 performAsyncUnityCall(QStringLiteral("Lock"));
484quint32 DBusGnomeScreensaverWrapper::GetActiveTime() const
486 return d->screensaverActiveTime();
489void DBusGnomeScreensaverWrapper::SimulateUserActivity()
491 d->setIdleHint(false);
495DBusScreensaverWrapper::DBusScreensaverWrapper()
496 : UnityDBusObject(QStringLiteral("/org/freedesktop/ScreenSaver"), QStringLiteral("org.freedesktop.ScreenSaver"))
498 QDBusConnection::sessionBus().registerObject(QStringLiteral("/ScreenSaver"), this, QDBusConnection::ExportScriptableContents); // compat path, also register here
499 connect(d, &DBusUnitySessionServicePrivate::screensaverActiveChanged, this, &DBusScreensaverWrapper::ActiveChanged);
502bool DBusScreensaverWrapper::GetActive() const
504 return !d->isSessionActive; // return whether the session is not active
507bool DBusScreensaverWrapper::SetActive(bool lock)
516void DBusScreensaverWrapper::Lock()
518 performAsyncUnityCall(QStringLiteral("Lock"));
521quint32 DBusScreensaverWrapper::GetActiveTime() const
523 return d->screensaverActiveTime();
526quint32 DBusScreensaverWrapper::GetSessionIdleTime() const
528 return QDateTime::fromMSecsSinceEpoch(d->idleSinceUSecTimestamp()/1000).secsTo(QDateTime::currentDateTime());
531void DBusScreensaverWrapper::SimulateUserActivity()
533 d->setIdleHint(false);
536#include "dbusunitysessionservice.moc"
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list