diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2026-09-23 21:08:35 +0200 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2026-09-23 21:08:35 +0200 |
| commit | ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71 (patch) | |
| tree | 141f738276cf84c6b7e5633c2bd5f2b44e9b0b75 /accounts/gkleen@skadhi/shell/quickshell-plugins/Systemd.hpp | |
| parent | 6a763272038e637a8a03dd9059d43a1ff44921dd (diff) | |
| download | nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar.gz nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar.bz2 nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.tar.xz nixos-ae9a80c1f012686c1f65fce7ce76fdd1b77b0b71.zip | |
...
Diffstat (limited to 'accounts/gkleen@skadhi/shell/quickshell-plugins/Systemd.hpp')
| -rw-r--r-- | accounts/gkleen@skadhi/shell/quickshell-plugins/Systemd.hpp | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/accounts/gkleen@skadhi/shell/quickshell-plugins/Systemd.hpp b/accounts/gkleen@skadhi/shell/quickshell-plugins/Systemd.hpp new file mode 100644 index 00000000..98f73d8f --- /dev/null +++ b/accounts/gkleen@skadhi/shell/quickshell-plugins/Systemd.hpp | |||
| @@ -0,0 +1,193 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <cstdint> | ||
| 4 | #include <memory> | ||
| 5 | #include <string> | ||
| 6 | #include <optional> | ||
| 7 | |||
| 8 | #include <QObject> | ||
| 9 | #include <QDBusInterface> | ||
| 10 | #include <QtQmlIntegration/qqmlintegration.h> | ||
| 11 | |||
| 12 | #include "dbus_systemd_manager.h" | ||
| 13 | #include "dbus_logind_manager.h" | ||
| 14 | #include "dbus_logind_session.h" | ||
| 15 | #include "dbus_systemd_unit.h" | ||
| 16 | #include "dbus_systemd_service.h" | ||
| 17 | #include "dbus_properties.h" | ||
| 18 | |||
| 19 | #include "core/model.hpp" | ||
| 20 | |||
| 21 | class SystemdUnit : public QObject { | ||
| 22 | Q_OBJECT; | ||
| 23 | QML_ELEMENT; | ||
| 24 | QML_UNCREATABLE("SystemdUnits cannot be created directly"); | ||
| 25 | |||
| 26 | public: | ||
| 27 | explicit SystemdUnit(QObject* parent, const QDBusObjectPath& path, const QString& id); | ||
| 28 | |||
| 29 | Q_PROPERTY(QDBusObjectPath path READ path CONSTANT); | ||
| 30 | Q_PROPERTY(QString id READ id CONSTANT); | ||
| 31 | Q_PROPERTY(QList<QString> runtimeDirectory READ runtimeDirectory CONSTANT); | ||
| 32 | Q_PROPERTY(QString activeState READ activeState NOTIFY activeStateChanged); | ||
| 33 | |||
| 34 | const QDBusObjectPath& path() const; | ||
| 35 | const QString& id() const; | ||
| 36 | QList<QString> runtimeDirectory(); | ||
| 37 | QString activeState(); | ||
| 38 | |||
| 39 | signals: | ||
| 40 | void activeStateChanged(); | ||
| 41 | |||
| 42 | private slots: | ||
| 43 | void onUnitPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties); | ||
| 44 | |||
| 45 | private: | ||
| 46 | std::unique_ptr<DBusSystemdUnit> systemdUnit(); | ||
| 47 | std::unique_ptr<DBusSystemdService> systemdService(); | ||
| 48 | |||
| 49 | DBusProperties* mUnitProperties; | ||
| 50 | QDBusObjectPath mPath; | ||
| 51 | QString mId; | ||
| 52 | std::optional<QString> mActiveState; | ||
| 53 | std::optional<QList<QString>> mRuntimeDirectory; | ||
| 54 | }; | ||
| 55 | |||
| 56 | class Systemd : public QObject { | ||
| 57 | Q_OBJECT; | ||
| 58 | QML_SINGLETON; | ||
| 59 | QML_ELEMENT; | ||
| 60 | |||
| 61 | public: | ||
| 62 | explicit Systemd(QObject* parent = nullptr); | ||
| 63 | |||
| 64 | Q_PROPERTY(bool idleHint READ idleHint WRITE setIdleHint NOTIFY idleHintChanged) | ||
| 65 | Q_PROPERTY(bool lockedHint READ lockedHint WRITE setLockedHint NOTIFY lockedHintChanged) | ||
| 66 | Q_PROPERTY(UntypedObjectModel* userUnits READ userUnits CONSTANT) | ||
| 67 | |||
| 68 | Q_INVOKABLE void stopUserUnit(const QString& unit, const QString& mode); | ||
| 69 | Q_INVOKABLE void setBrightness(const QString& subsystem, const QString& name, quint32 brightness); | ||
| 70 | Q_INVOKABLE void setIdleHint(bool idle); | ||
| 71 | Q_INVOKABLE void setLockedHint(bool locked); | ||
| 72 | Q_INVOKABLE void lockSession(); | ||
| 73 | Q_INVOKABLE void suspend(); | ||
| 74 | Q_INVOKABLE void hibernate(); | ||
| 75 | |||
| 76 | bool idleHint(); | ||
| 77 | bool lockedHint(); | ||
| 78 | ObjectModel<SystemdUnit>* userUnits(); | ||
| 79 | |||
| 80 | signals: | ||
| 81 | void shutdown(bool before); | ||
| 82 | void sleep(bool before); | ||
| 83 | void lock(); | ||
| 84 | void unlock(); | ||
| 85 | void idleHintChanged(); | ||
| 86 | void lockedHintChanged(); | ||
| 87 | |||
| 88 | private slots: | ||
| 89 | void onLogindSessionPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties); | ||
| 90 | void onUserUnitNew(const QString& id, const QDBusObjectPath& objectPath); | ||
| 91 | void onUserUnitRemoved(const QString& id, const QDBusObjectPath& objectPath); | ||
| 92 | |||
| 93 | private: | ||
| 94 | DBusSystemdManager* systemdManager; | ||
| 95 | DBusLogindManager* logindManager; | ||
| 96 | DBusLogindSession* logindSession; | ||
| 97 | DBusProperties* logindSessionProperties; | ||
| 98 | |||
| 99 | ObjectModel<SystemdUnit> mUserUnits{this}; | ||
| 100 | }; | ||
| 101 | |||
| 102 | class SystemdInhibitorParams : public QObject { | ||
| 103 | Q_OBJECT; | ||
| 104 | QML_ELEMENT; | ||
| 105 | QML_SINGLETON; | ||
| 106 | |||
| 107 | public: | ||
| 108 | enum WhatItem : uint8_t { | ||
| 109 | Shutdown = 0b1, | ||
| 110 | Sleep = 0b10, | ||
| 111 | Idle = 0b100, | ||
| 112 | HandlePowerKey = 0b1000, | ||
| 113 | HandleSuspendKey = 0b10000, | ||
| 114 | HandleHibernateKey = 0b100000, | ||
| 115 | HandleLidSwitch = 0b1000000, | ||
| 116 | }; | ||
| 117 | Q_ENUM(WhatItem); | ||
| 118 | Q_DECLARE_FLAGS(What, WhatItem); | ||
| 119 | |||
| 120 | enum Mode : uint8_t { | ||
| 121 | Block = 1, | ||
| 122 | BlockWeak = 2, | ||
| 123 | Delay = 3, | ||
| 124 | }; | ||
| 125 | Q_ENUM(Mode); | ||
| 126 | |||
| 127 | Q_INVOKABLE static std::string toString(WhatItem what); | ||
| 128 | Q_INVOKABLE static std::string toString(What what); | ||
| 129 | Q_INVOKABLE static std::string toString(Mode mode); | ||
| 130 | |||
| 131 | static constexpr WhatItem allWhatItems[] = { Shutdown, Sleep, Idle, HandlePowerKey, HandleSuspendKey, HandleHibernateKey, HandleLidSwitch }; | ||
| 132 | }; | ||
| 133 | Q_DECLARE_OPERATORS_FOR_FLAGS(SystemdInhibitorParams::What) | ||
| 134 | |||
| 135 | class SystemdInhibitor : public QObject { | ||
| 136 | Q_OBJECT; | ||
| 137 | Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged); | ||
| 138 | Q_PROPERTY(SystemdInhibitorParams::What what READ what WRITE setWhat NOTIFY whatChanged); | ||
| 139 | Q_PROPERTY(QString who READ who WRITE setWho NOTIFY whoChanged); | ||
| 140 | Q_PROPERTY(QString why READ why WRITE setWhy NOTIFY whyChanged); | ||
| 141 | Q_PROPERTY(SystemdInhibitorParams::Mode mode READ mode WRITE setMode NOTIFY modeChanged); | ||
| 142 | QML_ELEMENT; | ||
| 143 | |||
| 144 | public: | ||
| 145 | explicit SystemdInhibitor(QObject* parent = nullptr): QObject(parent) {} | ||
| 146 | |||
| 147 | bool enabled() const; | ||
| 148 | void setEnabled(bool enabled); | ||
| 149 | |||
| 150 | SystemdInhibitorParams::What what() const; | ||
| 151 | void setWhat(SystemdInhibitorParams::What what); | ||
| 152 | |||
| 153 | QString who() const; | ||
| 154 | void setWho(QString who); | ||
| 155 | |||
| 156 | QString why() const; | ||
| 157 | void setWhy(QString why); | ||
| 158 | |||
| 159 | SystemdInhibitorParams::Mode mode() const; | ||
| 160 | void setMode(SystemdInhibitorParams::Mode mode); | ||
| 161 | |||
| 162 | Q_INVOKABLE void release(); | ||
| 163 | |||
| 164 | signals: | ||
| 165 | void enabledChanged(); | ||
| 166 | void whatChanged(); | ||
| 167 | void whoChanged(); | ||
| 168 | void whyChanged(); | ||
| 169 | void modeChanged(); | ||
| 170 | |||
| 171 | private: | ||
| 172 | class ActiveSystemdInhibitor { | ||
| 173 | public: | ||
| 174 | uint32_t fd = -1; | ||
| 175 | SystemdInhibitorParams::What what; | ||
| 176 | QString who; | ||
| 177 | QString why; | ||
| 178 | SystemdInhibitorParams::Mode mode; | ||
| 179 | |||
| 180 | ActiveSystemdInhibitor(SystemdInhibitorParams::What what_, QString who_, QString why_, SystemdInhibitorParams::Mode mode_); | ||
| 181 | ~ActiveSystemdInhibitor(); | ||
| 182 | }; | ||
| 183 | |||
| 184 | void update(); | ||
| 185 | |||
| 186 | bool mEnabled = true; | ||
| 187 | std::unique_ptr<ActiveSystemdInhibitor> activeInhibitor; | ||
| 188 | SystemdInhibitorParams::What mWhat = static_cast<SystemdInhibitorParams::What>(0); | ||
| 189 | QString mWho; | ||
| 190 | QString mWhy; | ||
| 191 | SystemdInhibitorParams::Mode mMode = static_cast<SystemdInhibitorParams::Mode>(0); | ||
| 192 | }; | ||
| 193 | |||
