diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2026-07-08 22:51:44 +0200 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2026-07-08 22:51:44 +0200 |
| commit | f4d01d2d9f7a921f40a3b192637959ddf9129669 (patch) | |
| tree | f5f85751d7042088fd152722576f72943a26b472 /accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp | |
| parent | 4751f7390f1e2d4fd5a6a7e22ca111444e915583 (diff) | |
| download | nixos-f4d01d2d9f7a921f40a3b192637959ddf9129669.tar nixos-f4d01d2d9f7a921f40a3b192637959ddf9129669.tar.gz nixos-f4d01d2d9f7a921f40a3b192637959ddf9129669.tar.bz2 nixos-f4d01d2d9f7a921f40a3b192637959ddf9129669.tar.xz nixos-f4d01d2d9f7a921f40a3b192637959ddf9129669.zip | |
...flakes
Diffstat (limited to 'accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp')
| -rw-r--r-- | accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp | 92 |
1 files changed, 91 insertions, 1 deletions
diff --git a/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp b/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp index 308659e9..6376b527 100644 --- a/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp +++ b/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp | |||
| @@ -8,7 +8,12 @@ | |||
| 8 | #include <QDBusUnixFileDescriptor> | 8 | #include <QDBusUnixFileDescriptor> |
| 9 | #include <QDBusObjectPath> | 9 | #include <QDBusObjectPath> |
| 10 | 10 | ||
| 11 | #include <set> | ||
| 12 | |||
| 11 | Systemd::Systemd(QObject* parent) : QObject(parent) { | 13 | Systemd::Systemd(QObject* parent) : QObject(parent) { |
| 14 | ListUnitsEntry::registerMetaType(); | ||
| 15 | qDBusRegisterMetaType<ListUnitsResult>(); | ||
| 16 | |||
| 12 | this->systemdManager = new DBusSystemdManager( | 17 | this->systemdManager = new DBusSystemdManager( |
| 13 | "org.freedesktop.systemd1", | 18 | "org.freedesktop.systemd1", |
| 14 | "/org/freedesktop/systemd1", | 19 | "/org/freedesktop/systemd1", |
| @@ -22,8 +27,9 @@ Systemd::Systemd(QObject* parent) : QObject(parent) { | |||
| 22 | this | 27 | this |
| 23 | ); | 28 | ); |
| 24 | 29 | ||
| 30 | |||
| 25 | QDBusReply<QDBusObjectPath> sessionPath = this->logindManager->GetSession("auto"); | 31 | QDBusReply<QDBusObjectPath> sessionPath = this->logindManager->GetSession("auto"); |
| 26 | qDebug() << sessionPath; | 32 | // qDebug() << sessionPath; |
| 27 | this->logindSession = new DBusLogindSession( | 33 | this->logindSession = new DBusLogindSession( |
| 28 | "org.freedesktop.login1", | 34 | "org.freedesktop.login1", |
| 29 | sessionPath.value().path(), | 35 | sessionPath.value().path(), |
| @@ -42,6 +48,12 @@ Systemd::Systemd(QObject* parent) : QObject(parent) { | |||
| 42 | QObject::connect(this->logindSession, &DBusLogindSession::Lock, this, &Systemd::lock); | 48 | QObject::connect(this->logindSession, &DBusLogindSession::Lock, this, &Systemd::lock); |
| 43 | QObject::connect(this->logindSession, &DBusLogindSession::Unlock, this, &Systemd::unlock); | 49 | QObject::connect(this->logindSession, &DBusLogindSession::Unlock, this, &Systemd::unlock); |
| 44 | QObject::connect(this->logindSessionProperties, &DBusProperties::PropertiesChanged, this, &Systemd::onLogindSessionPropertiesChanged); | 50 | QObject::connect(this->logindSessionProperties, &DBusProperties::PropertiesChanged, this, &Systemd::onLogindSessionPropertiesChanged); |
| 51 | this->systemdManager->Subscribe(); | ||
| 52 | QObject::connect(this->systemdManager, &DBusSystemdManager::UnitNew, this, &Systemd::onUserUnitNew); | ||
| 53 | QObject::connect(this->systemdManager, &DBusSystemdManager::UnitRemoved, this, &Systemd::onUserUnitRemoved); | ||
| 54 | |||
| 55 | for (const auto& unit: this->systemdManager->ListUnits().value()) | ||
| 56 | this->onUserUnitNew(unit.primaryName, unit.unitPath); | ||
| 45 | } | 57 | } |
| 46 | 58 | ||
| 47 | void Systemd::onLogindSessionPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties) { | 59 | void Systemd::onLogindSessionPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties) { |
| @@ -50,6 +62,82 @@ void Systemd::onLogindSessionPropertiesChanged(const QString& interface_name, co | |||
| 50 | if (changed_properties.contains("LockedHint") || invalidated_properties.contains("LockedHint")) | 62 | if (changed_properties.contains("LockedHint") || invalidated_properties.contains("LockedHint")) |
| 51 | emit this->lockedHintChanged(); | 63 | emit this->lockedHintChanged(); |
| 52 | } | 64 | } |
| 65 | void Systemd::onUserUnitNew(const QString& id, const QDBusObjectPath& object_path) { | ||
| 66 | for (auto unit: this->mUserUnits.valueList()) | ||
| 67 | if (unit->path() == object_path) | ||
| 68 | return; | ||
| 69 | |||
| 70 | for (const auto& unit: this->systemdManager->ListUnits().value()) | ||
| 71 | if (unit.unitPath == object_path) { | ||
| 72 | this->mUserUnits.insertObject(new SystemdUnit(this, object_path, id)); | ||
| 73 | return; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | void Systemd::onUserUnitRemoved(const QString& id, const QDBusObjectPath& object_path) { | ||
| 77 | std::set<SystemdUnit*> toRemove; | ||
| 78 | for (auto unit: this->mUserUnits.valueList()) | ||
| 79 | if (unit->path() == object_path) | ||
| 80 | toRemove.insert(unit); | ||
| 81 | |||
| 82 | for (auto unit: toRemove) { | ||
| 83 | this->mUserUnits.removeObject(unit); | ||
| 84 | delete unit; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | SystemdUnit::SystemdUnit(QObject* parent, const QDBusObjectPath& path, const QString& id): QObject(parent), mPath(path), mId(id) { | ||
| 89 | this->mUnitProperties = new DBusProperties( | ||
| 90 | "org.freedesktop.systemd1", | ||
| 91 | this->path().path(), | ||
| 92 | QDBusConnection::sessionBus(), | ||
| 93 | this | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | std::unique_ptr<DBusSystemdUnit> SystemdUnit::systemdUnit() { | ||
| 97 | return std::make_unique<DBusSystemdUnit>( | ||
| 98 | "org.freedesktop.systemd1", | ||
| 99 | this->path().path(), | ||
| 100 | QDBusConnection::sessionBus(), | ||
| 101 | nullptr | ||
| 102 | ); | ||
| 103 | } | ||
| 104 | std::unique_ptr<DBusSystemdService> SystemdUnit::systemdService() { | ||
| 105 | if (!this->id().endsWith(".service")) | ||
| 106 | return std::unique_ptr<DBusSystemdService>(); | ||
| 107 | |||
| 108 | return std::make_unique<DBusSystemdService>( | ||
| 109 | "org.freedesktop.systemd1", | ||
| 110 | this->path().path(), | ||
| 111 | QDBusConnection::sessionBus(), | ||
| 112 | nullptr | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | QList<QString> SystemdUnit::runtimeDirectory() { | ||
| 116 | if (std::unique_ptr<DBusSystemdService> systemdService = this->systemdService()) { | ||
| 117 | if (!this->mRuntimeDirectory) | ||
| 118 | this->mRuntimeDirectory = systemdService->runtimeDirectory(); | ||
| 119 | return *this->mRuntimeDirectory; | ||
| 120 | } | ||
| 121 | |||
| 122 | return QList<QString>{}; | ||
| 123 | } | ||
| 124 | QString SystemdUnit::activeState() { | ||
| 125 | if (!this->mActiveState) { | ||
| 126 | QObject::connect(this->mUnitProperties, &DBusProperties::PropertiesChanged, this, &SystemdUnit::onUnitPropertiesChanged); | ||
| 127 | this->mActiveState = this->systemdUnit()->activeState(); | ||
| 128 | } | ||
| 129 | |||
| 130 | return *this->mActiveState; | ||
| 131 | } | ||
| 132 | |||
| 133 | const QDBusObjectPath& SystemdUnit::path() const { return this->mPath; } | ||
| 134 | const QString& SystemdUnit::id() const { return this->mId; } | ||
| 135 | void SystemdUnit::onUnitPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties) { | ||
| 136 | if (changed_properties.contains("ActiveState") || invalidated_properties.contains("ActiveState")) { | ||
| 137 | this->mActiveState = this->systemdUnit()->activeState(); | ||
| 138 | emit this->activeStateChanged(); | ||
| 139 | } | ||
| 140 | } | ||
| 53 | 141 | ||
| 54 | void Systemd::stopUserUnit(const QString& unit, const QString& mode) { this->systemdManager->StopUnit(unit, mode); } | 142 | void Systemd::stopUserUnit(const QString& unit, const QString& mode) { this->systemdManager->StopUnit(unit, mode); } |
| 55 | 143 | ||
| @@ -63,6 +151,8 @@ void Systemd::lockSession() { this->logindSession->call("Lock"); } | |||
| 63 | void Systemd::suspend() { this->logindManager->Suspend(true); } | 151 | void Systemd::suspend() { this->logindManager->Suspend(true); } |
| 64 | void Systemd::hibernate() { this->logindManager->Hibernate(true); } | 152 | void Systemd::hibernate() { this->logindManager->Hibernate(true); } |
| 65 | 153 | ||
| 154 | ObjectModel<SystemdUnit>* Systemd::userUnits() { return &this->mUserUnits; } | ||
| 155 | |||
| 66 | std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::WhatItem what) { | 156 | std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::WhatItem what) { |
| 67 | if (what == SystemdInhibitorParams::Shutdown) | 157 | if (what == SystemdInhibitorParams::Shutdown) |
| 68 | return "shutdown"; | 158 | return "shutdown"; |
