summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp')
-rw-r--r--accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp207
1 files changed, 199 insertions, 8 deletions
diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp b/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp
index 9ccd8ba0..884ea17f 100644
--- a/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp
+++ b/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp
@@ -1,16 +1,207 @@
1#include "Systemd.hpp" 1#include "Systemd.hpp"
2 2
3#include <sstream>
4
3#include <QDBusConnection> 5#include <QDBusConnection>
4#include <QDBusMessage> 6#include <QDBusReply>
7#include <QDebug>
8#include <QDBusUnixFileDescriptor>
9#include <QDBusObjectPath>
5 10
6void Systemd::stopUserUnit(const QString& unit, const QString& mode) { 11Systemd::Systemd(QObject* parent) : QObject(parent) {
7 QDBusMessage m = QDBusMessage::createMethodCall( 12 this->systemdManager = new DBusSystemdManager(
8 "org.freedesktop.systemd1", 13 "org.freedesktop.systemd1",
9 "/org/freedesktop/systemd1", 14 "/org/freedesktop/systemd1",
10 "org.freedesktop.systemd1.Manager", 15 QDBusConnection::sessionBus(),
11 "StopUnit" 16 this
12 ); 17 );
13 m << unit; 18 this->logindManager = new DBusLogindManager(
14 m << mode; 19 "org.freedesktop.login1",
15 QDBusConnection::sessionBus().send(m); 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
47void 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
54void Systemd::stopUserUnit(const QString& unit, const QString& mode) {
55 this->systemdManager->StopUnit(unit, mode);
56}
57
58void Systemd::setBrightness(const QString& subsystem, const QString& name, quint32 brightness) {
59 this->logindSession->SetBrightness(subsystem, name, brightness);
60}
61
62bool Systemd::idleHint() {
63 return this->logindSession->idleHint();
64}
65void Systemd::setIdleHint(bool idle) {
66 this->logindSession->SetIdleHint(idle);
67}
68bool Systemd::lockedHint() {
69 return this->logindSession->lockedHint();
70}
71void Systemd::setLockedHint(bool locked) {
72 this->logindSession->SetLockedHint(locked);
73}
74
75std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::WhatItem what) {
76 if (what == SystemdInhibitorParams::Shutdown)
77 return "shutdown";
78 else if (what == SystemdInhibitorParams::Sleep)
79 return "sleep";
80 else if (what == SystemdInhibitorParams::Idle)
81 return "idle";
82 else if (what == SystemdInhibitorParams::HandlePowerKey)
83 return "handle-power-key";
84 else if (what == SystemdInhibitorParams::HandleSuspendKey)
85 return "handle-suspend-key";
86 else if (what == SystemdInhibitorParams::HandleHibernateKey)
87 return "handle-hibernate-key";
88 else if (what == SystemdInhibitorParams::HandleLidSwitch)
89 return "handle-lid-switch";
90 return "";
91}
92
93std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::What what) {
94 std::ostringstream res;
95 bool first = true;
96 for (const WhatItem& item: SystemdInhibitorParams::allWhatItems) {
97 if (!(what & item))
98 continue;
99
100 if (!first)
101 res << ":";
102 else
103 first = false;
104 res << SystemdInhibitorParams::toString(item);
105 }
106 return res.str();
107}
108
109std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::Mode mode) {
110 if (mode == SystemdInhibitorParams::Block)
111 return "block";
112 else if (mode == SystemdInhibitorParams::BlockWeak)
113 return "block-weak";
114 else if (mode == SystemdInhibitorParams::Delay)
115 return "delay";
116 return "";
117}
118
119bool SystemdInhibitor::enabled() const { return static_cast<bool>(this->activeInhibitor); }
120void SystemdInhibitor::setEnabled(bool enabled) {
121 this->mEnabled = enabled;
122
123 if (enabled)
124 this->update();
125 else
126 this->release();
127}
128
129SystemdInhibitorParams::What SystemdInhibitor::what() const { return this->mWhat; }
130void SystemdInhibitor::setWhat(SystemdInhibitorParams::What what) {
131 this->mWhat = what;
132 this->update();
133}
134
135QString SystemdInhibitor::who() const { return this->mWho; }
136void SystemdInhibitor::setWho(QString who) {
137 this->mWho = who;
138 this->update();
139}
140
141QString SystemdInhibitor::why() const { return this->mWhy; }
142void SystemdInhibitor::setWhy(QString why) {
143 this->mWhy = why;
144 this->update();
145}
146
147SystemdInhibitorParams::Mode SystemdInhibitor::mode() const { return this->mMode; }
148void SystemdInhibitor::setMode(SystemdInhibitorParams::Mode mode) {
149 this->mMode = mode;
150 this->update();
151}
152
153SystemdInhibitor::ActiveSystemdInhibitor::ActiveSystemdInhibitor(SystemdInhibitorParams::What what_, QString who_, QString why_, SystemdInhibitorParams::Mode mode_): what(what_), who(who_), why(why_), mode(mode_) {
154 DBusLogindManager logindManager(
155 "org.freedesktop.login1",
156 "/org/freedesktop/login1",
157 QDBusConnection::systemBus()
158 );
159 QDBusReply<QDBusUnixFileDescriptor> fd_ = logindManager.Inhibit(QString::fromStdString(SystemdInhibitorParams::toString(what)), who, why, QString::fromStdString(SystemdInhibitorParams::toString(mode)));
160 if (fd_.error().isValid())
161 throw fd_.error();
162 this->fd = ::dup(fd_.value().fileDescriptor());
163}
164SystemdInhibitor::ActiveSystemdInhibitor::~ActiveSystemdInhibitor() {
165 if (this->fd != -1)
166 ::close(this->fd);
167}
168
169void SystemdInhibitor::release() {
170 if (!this->activeInhibitor)
171 return;
172
173 this->activeInhibitor.reset();
174 emit this->enabledChanged();
175}
176
177void SystemdInhibitor::update() {
178 if (!this->mWhat || this->mWho.isEmpty() || this->mWhy.isEmpty() || !this->mMode || !this->mEnabled)
179 if (this->activeInhibitor)
180 this->release();
181 else
182 return;
183
184 if (this->activeInhibitor && this->mWhat == this->activeInhibitor->what && this->mWho == this->activeInhibitor->who && this->mWhy == this->activeInhibitor->why && this->mMode == this->activeInhibitor->mode)
185 return;
186
187 std::unique_ptr<ActiveSystemdInhibitor> otherInhibitor;
188 try {
189 otherInhibitor.reset(new SystemdInhibitor::ActiveSystemdInhibitor(this->mWhat, this->mWho, this->mWhy, this->mMode));
190 } catch (const QDBusError& err) {
191 qCritical().noquote()
192 << err.name().toStdString() << " " << err.message().toStdString();
193 return;
194 }
195 this->activeInhibitor.swap(otherInhibitor);
196
197 if (!otherInhibitor && this->activeInhibitor)
198 emit this->enabledChanged();
199 if (otherInhibitor && otherInhibitor->what != this->activeInhibitor->what)
200 emit this->whatChanged();
201 if (otherInhibitor && otherInhibitor->who != this->activeInhibitor->who)
202 emit this->whoChanged();
203 if (otherInhibitor && otherInhibitor->why != this->activeInhibitor->why)
204 emit this->whyChanged();
205 if (otherInhibitor && otherInhibitor->mode != this->activeInhibitor->mode)
206 emit this->modeChanged();
16} 207}