diff options
author | Gregor Kleen <gkleen@yggdrasil.li> | 2025-09-11 22:52:35 +0200 |
---|---|---|
committer | Gregor Kleen <gkleen@yggdrasil.li> | 2025-09-11 22:52:35 +0200 |
commit | dd2df931a3be1a6518c1e9fbff438de4274456cd (patch) | |
tree | 50d203bcaa0e2d3af8f2d88536e38e0f0ee81c67 /accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp | |
parent | aebd3235d755cb1ff95995b461e497fea2d52e8b (diff) | |
download | nixos-dd2df931a3be1a6518c1e9fbff438de4274456cd.tar nixos-dd2df931a3be1a6518c1e9fbff438de4274456cd.tar.gz nixos-dd2df931a3be1a6518c1e9fbff438de4274456cd.tar.bz2 nixos-dd2df931a3be1a6518c1e9fbff438de4274456cd.tar.xz nixos-dd2df931a3be1a6518c1e9fbff438de4274456cd.zip |
...
Diffstat (limited to 'accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp')
-rw-r--r-- | accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp | 207 |
1 files changed, 194 insertions, 13 deletions
diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp b/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp index 5e607709..790f514f 100644 --- a/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp +++ b/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp | |||
@@ -1,24 +1,205 @@ | |||
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 | ||
6 | void Systemd::stopUserUnit(const QString& unit, const QString& mode) { | 11 | Systemd::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 | ) << unit << mode; | 17 | ); |
13 | QDBusConnection::sessionBus().send(m); | 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) { | ||
55 | this->systemdManager->StopUnit(unit, mode); | ||
14 | } | 56 | } |
15 | 57 | ||
16 | void Systemd::setBrightness(const QString& subsystem, const QString& name, quint32 brightness) { | 58 | void Systemd::setBrightness(const QString& subsystem, const QString& name, quint32 brightness) { |
17 | QDBusMessage m = QDBusMessage::createMethodCall( | 59 | this->logindSession->SetBrightness(subsystem, name, brightness); |
60 | } | ||
61 | |||
62 | bool Systemd::idleHint() { | ||
63 | return this->logindSession->idleHint(); | ||
64 | } | ||
65 | void Systemd::setIdleHint(bool idle) { | ||
66 | this->logindSession->SetIdleHint(idle); | ||
67 | } | ||
68 | bool Systemd::lockedHint() { | ||
69 | return this->logindSession->lockedHint(); | ||
70 | } | ||
71 | void Systemd::setLockedHint(bool locked) { | ||
72 | this->logindSession->SetLockedHint(locked); | ||
73 | } | ||
74 | |||
75 | std::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 | |||
93 | std::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 | |||
109 | std::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 | |||
119 | bool SystemdInhibitor::enabled() const { return static_cast<bool>(this->activeInhibitor); } | ||
120 | void SystemdInhibitor::setEnabled(bool enabled) { | ||
121 | if (enabled) | ||
122 | this->update(); | ||
123 | else | ||
124 | this->release(); | ||
125 | } | ||
126 | |||
127 | SystemdInhibitorParams::What SystemdInhibitor::what() const { return this->mWhat; } | ||
128 | void SystemdInhibitor::setWhat(SystemdInhibitorParams::What what) { | ||
129 | this->mWhat = what; | ||
130 | this->update(); | ||
131 | } | ||
132 | |||
133 | QString SystemdInhibitor::who() const { return this->mWho; } | ||
134 | void SystemdInhibitor::setWho(QString who) { | ||
135 | this->mWho = who; | ||
136 | this->update(); | ||
137 | } | ||
138 | |||
139 | QString SystemdInhibitor::why() const { return this->mWhy; } | ||
140 | void SystemdInhibitor::setWhy(QString why) { | ||
141 | this->mWhy = why; | ||
142 | this->update(); | ||
143 | } | ||
144 | |||
145 | SystemdInhibitorParams::Mode SystemdInhibitor::mode() const { return this->mMode; } | ||
146 | void SystemdInhibitor::setMode(SystemdInhibitorParams::Mode mode) { | ||
147 | this->mMode = mode; | ||
148 | this->update(); | ||
149 | } | ||
150 | |||
151 | SystemdInhibitor::ActiveSystemdInhibitor::ActiveSystemdInhibitor(SystemdInhibitorParams::What what_, QString who_, QString why_, SystemdInhibitorParams::Mode mode_): what(what_), who(who_), why(why_), mode(mode_) { | ||
152 | DBusLogindManager logindManager( | ||
18 | "org.freedesktop.login1", | 153 | "org.freedesktop.login1", |
19 | "/org/freedesktop/login1/session/auto", | 154 | "/org/freedesktop/login1", |
20 | "org.freedesktop.login1.Session", | 155 | QDBusConnection::systemBus() |
21 | "SetBrightness" | 156 | ); |
22 | ) << subsystem << name << brightness; | 157 | QDBusReply<QDBusUnixFileDescriptor> fd_ = logindManager.Inhibit(QString::fromStdString(SystemdInhibitorParams::toString(what)), who, why, QString::fromStdString(SystemdInhibitorParams::toString(mode))); |
23 | QDBusConnection::systemBus().send(m); | 158 | if (fd_.error().isValid()) |
159 | throw fd_.error(); | ||
160 | this->fd = ::dup(fd_.value().fileDescriptor()); | ||
161 | } | ||
162 | SystemdInhibitor::ActiveSystemdInhibitor::~ActiveSystemdInhibitor() { | ||
163 | if (this->fd != -1) | ||
164 | ::close(this->fd); | ||
165 | } | ||
166 | |||
167 | void SystemdInhibitor::release() { | ||
168 | if (!this->activeInhibitor) | ||
169 | return; | ||
170 | |||
171 | this->activeInhibitor.reset(); | ||
172 | emit this->enabledChanged(); | ||
173 | } | ||
174 | |||
175 | void SystemdInhibitor::update() { | ||
176 | if (!this->mWhat || this->mWho.isEmpty() || this->mWhy.isEmpty() || !this->mMode) | ||
177 | if (this->activeInhibitor) | ||
178 | this->release(); | ||
179 | else | ||
180 | return; | ||
181 | |||
182 | if (this->activeInhibitor && this->mWhat == this->activeInhibitor->what && this->mWho == this->activeInhibitor->who && this->mWhy == this->activeInhibitor->why && this->mMode == this->activeInhibitor->mode) | ||
183 | return; | ||
184 | |||
185 | std::unique_ptr<ActiveSystemdInhibitor> otherInhibitor; | ||
186 | try { | ||
187 | otherInhibitor.reset(new SystemdInhibitor::ActiveSystemdInhibitor(this->mWhat, this->mWho, this->mWhy, this->mMode)); | ||
188 | } catch (const QDBusError& err) { | ||
189 | qCritical().noquote() | ||
190 | << err.name().toStdString() << " " << err.message().toStdString(); | ||
191 | return; | ||
192 | } | ||
193 | this->activeInhibitor.swap(otherInhibitor); | ||
194 | |||
195 | if (!otherInhibitor && this->activeInhibitor) | ||
196 | emit this->enabledChanged(); | ||
197 | if (otherInhibitor && otherInhibitor->what != this->activeInhibitor->what) | ||
198 | emit this->whatChanged(); | ||
199 | if (otherInhibitor && otherInhibitor->who != this->activeInhibitor->who) | ||
200 | emit this->whoChanged(); | ||
201 | if (otherInhibitor && otherInhibitor->why != this->activeInhibitor->why) | ||
202 | emit this->whyChanged(); | ||
203 | if (otherInhibitor && otherInhibitor->mode != this->activeInhibitor->mode) | ||
204 | emit this->modeChanged(); | ||
24 | } | 205 | } |