diff options
Diffstat (limited to 'accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp')
| -rw-r--r-- | accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp b/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp new file mode 100644 index 00000000..308659e9 --- /dev/null +++ b/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp | |||
| @@ -0,0 +1,198 @@ | |||
| 1 | #include "Systemd.hpp" | ||
| 2 | |||
| 3 | #include <sstream> | ||
| 4 | |||
| 5 | #include <QDBusConnection> | ||
| 6 | #include <QDBusReply> | ||
| 7 | #include <QDebug> | ||
| 8 | #include <QDBusUnixFileDescriptor> | ||
| 9 | #include <QDBusObjectPath> | ||
| 10 | |||
| 11 | Systemd::Systemd(QObject* parent) : QObject(parent) { | ||
| 12 | this->systemdManager = new DBusSystemdManager( | ||
| 13 | "org.freedesktop.systemd1", | ||
| 14 | "/org/freedesktop/systemd1", | ||
| 15 | QDBusConnection::sessionBus(), | ||
| 16 | this | ||
| 17 | ); | ||
| 18 | this->logindManager = new DBusLogindManager( | ||
| 19 | "org.freedesktop.login1", | ||
| 20 | "/org/freedesktop/login1", | ||
| 21 | QDBusConnection::systemBus(), | ||
| 22 | this | ||
| 23 | ); | ||
| 24 | |||
| 25 | QDBusReply<QDBusObjectPath> sessionPath = this->logindManager->GetSession("auto"); | ||
| 26 | qDebug() << sessionPath; | ||
| 27 | this->logindSession = new DBusLogindSession( | ||
| 28 | "org.freedesktop.login1", | ||
| 29 | sessionPath.value().path(), | ||
| 30 | QDBusConnection::systemBus(), | ||
| 31 | this | ||
| 32 | ); | ||
| 33 | this->logindSessionProperties = new DBusProperties( | ||
| 34 | "org.freedesktop.login1", | ||
| 35 | sessionPath.value().path(), | ||
| 36 | QDBusConnection::systemBus(), | ||
| 37 | this | ||
| 38 | ); | ||
| 39 | |||
| 40 | QObject::connect(this->logindManager, &DBusLogindManager::PrepareForShutdown, this, &Systemd::shutdown); | ||
| 41 | QObject::connect(this->logindManager, &DBusLogindManager::PrepareForSleep, this, &Systemd::sleep); | ||
| 42 | QObject::connect(this->logindSession, &DBusLogindSession::Lock, this, &Systemd::lock); | ||
| 43 | QObject::connect(this->logindSession, &DBusLogindSession::Unlock, this, &Systemd::unlock); | ||
| 44 | QObject::connect(this->logindSessionProperties, &DBusProperties::PropertiesChanged, this, &Systemd::onLogindSessionPropertiesChanged); | ||
| 45 | } | ||
| 46 | |||
| 47 | void Systemd::onLogindSessionPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties) { | ||
| 48 | if (changed_properties.contains("IdleHint") || invalidated_properties.contains("IdleHint")) | ||
| 49 | emit this->idleHintChanged(); | ||
| 50 | if (changed_properties.contains("LockedHint") || invalidated_properties.contains("LockedHint")) | ||
| 51 | emit this->lockedHintChanged(); | ||
| 52 | } | ||
| 53 | |||
| 54 | void Systemd::stopUserUnit(const QString& unit, const QString& mode) { this->systemdManager->StopUnit(unit, mode); } | ||
| 55 | |||
| 56 | void Systemd::setBrightness(const QString& subsystem, const QString& name, quint32 brightness) { this->logindSession->SetBrightness(subsystem, name, brightness); } | ||
| 57 | |||
| 58 | bool Systemd::idleHint() { return this->logindSession->idleHint(); } | ||
| 59 | void Systemd::setIdleHint(bool idle) { this->logindSession->SetIdleHint(idle); } | ||
| 60 | bool Systemd::lockedHint() { return this->logindSession->lockedHint(); } | ||
| 61 | void Systemd::setLockedHint(bool locked) { this->logindSession->SetLockedHint(locked); } | ||
| 62 | void Systemd::lockSession() { this->logindSession->call("Lock"); } | ||
| 63 | void Systemd::suspend() { this->logindManager->Suspend(true); } | ||
| 64 | void Systemd::hibernate() { this->logindManager->Hibernate(true); } | ||
| 65 | |||
| 66 | std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::WhatItem what) { | ||
| 67 | if (what == SystemdInhibitorParams::Shutdown) | ||
| 68 | return "shutdown"; | ||
| 69 | else if (what == SystemdInhibitorParams::Sleep) | ||
| 70 | return "sleep"; | ||
| 71 | else if (what == SystemdInhibitorParams::Idle) | ||
| 72 | return "idle"; | ||
| 73 | else if (what == SystemdInhibitorParams::HandlePowerKey) | ||
| 74 | return "handle-power-key"; | ||
| 75 | else if (what == SystemdInhibitorParams::HandleSuspendKey) | ||
| 76 | return "handle-suspend-key"; | ||
| 77 | else if (what == SystemdInhibitorParams::HandleHibernateKey) | ||
| 78 | return "handle-hibernate-key"; | ||
| 79 | else if (what == SystemdInhibitorParams::HandleLidSwitch) | ||
| 80 | return "handle-lid-switch"; | ||
| 81 | return ""; | ||
| 82 | } | ||
| 83 | |||
| 84 | std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::What what) { | ||
| 85 | std::ostringstream res; | ||
| 86 | bool first = true; | ||
| 87 | for (const WhatItem& item: SystemdInhibitorParams::allWhatItems) { | ||
| 88 | if (!(what & item)) | ||
| 89 | continue; | ||
| 90 | |||
| 91 | if (!first) | ||
| 92 | res << ":"; | ||
| 93 | else | ||
| 94 | first = false; | ||
| 95 | res << SystemdInhibitorParams::toString(item); | ||
| 96 | } | ||
| 97 | return res.str(); | ||
| 98 | } | ||
| 99 | |||
| 100 | std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::Mode mode) { | ||
| 101 | if (mode == SystemdInhibitorParams::Block) | ||
| 102 | return "block"; | ||
| 103 | else if (mode == SystemdInhibitorParams::BlockWeak) | ||
| 104 | return "block-weak"; | ||
| 105 | else if (mode == SystemdInhibitorParams::Delay) | ||
| 106 | return "delay"; | ||
| 107 | return ""; | ||
| 108 | } | ||
| 109 | |||
| 110 | bool SystemdInhibitor::enabled() const { return static_cast<bool>(this->activeInhibitor); } | ||
| 111 | void SystemdInhibitor::setEnabled(bool enabled) { | ||
| 112 | this->mEnabled = enabled; | ||
| 113 | |||
| 114 | if (enabled) | ||
| 115 | this->update(); | ||
| 116 | else | ||
| 117 | this->release(); | ||
| 118 | } | ||
| 119 | |||
| 120 | SystemdInhibitorParams::What SystemdInhibitor::what() const { return this->mWhat; } | ||
| 121 | void SystemdInhibitor::setWhat(SystemdInhibitorParams::What what) { | ||
| 122 | this->mWhat = what; | ||
| 123 | this->update(); | ||
| 124 | } | ||
| 125 | |||
| 126 | QString SystemdInhibitor::who() const { return this->mWho; } | ||
| 127 | void SystemdInhibitor::setWho(QString who) { | ||
| 128 | this->mWho = who; | ||
| 129 | this->update(); | ||
| 130 | } | ||
| 131 | |||
| 132 | QString SystemdInhibitor::why() const { return this->mWhy; } | ||
| 133 | void SystemdInhibitor::setWhy(QString why) { | ||
| 134 | this->mWhy = why; | ||
| 135 | this->update(); | ||
| 136 | } | ||
| 137 | |||
| 138 | SystemdInhibitorParams::Mode SystemdInhibitor::mode() const { return this->mMode; } | ||
| 139 | void SystemdInhibitor::setMode(SystemdInhibitorParams::Mode mode) { | ||
| 140 | this->mMode = mode; | ||
| 141 | this->update(); | ||
| 142 | } | ||
| 143 | |||
| 144 | SystemdInhibitor::ActiveSystemdInhibitor::ActiveSystemdInhibitor(SystemdInhibitorParams::What what_, QString who_, QString why_, SystemdInhibitorParams::Mode mode_): what(what_), who(who_), why(why_), mode(mode_) { | ||
| 145 | DBusLogindManager logindManager( | ||
| 146 | "org.freedesktop.login1", | ||
| 147 | "/org/freedesktop/login1", | ||
| 148 | QDBusConnection::systemBus() | ||
| 149 | ); | ||
| 150 | QDBusReply<QDBusUnixFileDescriptor> fd_ = logindManager.Inhibit(QString::fromStdString(SystemdInhibitorParams::toString(what)), who, why, QString::fromStdString(SystemdInhibitorParams::toString(mode))); | ||
| 151 | if (fd_.error().isValid()) | ||
| 152 | throw fd_.error(); | ||
| 153 | this->fd = ::dup(fd_.value().fileDescriptor()); | ||
| 154 | } | ||
| 155 | SystemdInhibitor::ActiveSystemdInhibitor::~ActiveSystemdInhibitor() { | ||
| 156 | if (this->fd != -1) | ||
| 157 | ::close(this->fd); | ||
| 158 | } | ||
| 159 | |||
| 160 | void SystemdInhibitor::release() { | ||
| 161 | if (!this->activeInhibitor) | ||
| 162 | return; | ||
| 163 | |||
| 164 | this->activeInhibitor.reset(); | ||
| 165 | emit this->enabledChanged(); | ||
| 166 | } | ||
| 167 | |||
| 168 | void SystemdInhibitor::update() { | ||
| 169 | if (!this->mWhat || this->mWho.isEmpty() || this->mWhy.isEmpty() || !this->mMode || !this->mEnabled) | ||
| 170 | if (this->activeInhibitor) | ||
| 171 | this->release(); | ||
| 172 | else | ||
| 173 | return; | ||
| 174 | |||
| 175 | if (this->activeInhibitor && this->mWhat == this->activeInhibitor->what && this->mWho == this->activeInhibitor->who && this->mWhy == this->activeInhibitor->why && this->mMode == this->activeInhibitor->mode) | ||
| 176 | return; | ||
| 177 | |||
| 178 | std::unique_ptr<ActiveSystemdInhibitor> otherInhibitor; | ||
| 179 | try { | ||
| 180 | otherInhibitor.reset(new SystemdInhibitor::ActiveSystemdInhibitor(this->mWhat, this->mWho, this->mWhy, this->mMode)); | ||
| 181 | } catch (const QDBusError& err) { | ||
| 182 | qCritical().noquote() | ||
| 183 | << err.name().toStdString() << " " << err.message().toStdString(); | ||
| 184 | return; | ||
| 185 | } | ||
| 186 | this->activeInhibitor.swap(otherInhibitor); | ||
| 187 | |||
| 188 | if (!otherInhibitor && this->activeInhibitor) | ||
| 189 | emit this->enabledChanged(); | ||
| 190 | if (otherInhibitor && otherInhibitor->what != this->activeInhibitor->what) | ||
| 191 | emit this->whatChanged(); | ||
| 192 | if (otherInhibitor && otherInhibitor->who != this->activeInhibitor->who) | ||
| 193 | emit this->whoChanged(); | ||
| 194 | if (otherInhibitor && otherInhibitor->why != this->activeInhibitor->why) | ||
| 195 | emit this->whyChanged(); | ||
| 196 | if (otherInhibitor && otherInhibitor->mode != this->activeInhibitor->mode) | ||
| 197 | emit this->modeChanged(); | ||
| 198 | } | ||
