summaryrefslogtreecommitdiff
path: root/accounts/gkleen@skadhi/shell/quickshell-plugins/Systemd.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'accounts/gkleen@skadhi/shell/quickshell-plugins/Systemd.hpp')
-rw-r--r--accounts/gkleen@skadhi/shell/quickshell-plugins/Systemd.hpp194
1 files changed, 194 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..52df6470
--- /dev/null
+++ b/accounts/gkleen@skadhi/shell/quickshell-plugins/Systemd.hpp
@@ -0,0 +1,194 @@
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
21class SystemdUnit : public QObject {
22 Q_OBJECT;
23 QML_ELEMENT;
24 QML_UNCREATABLE("SystemdUnits cannot be created directly");
25
26public:
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
39signals:
40 void activeStateChanged();
41
42private slots:
43 void onUnitPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties);
44
45private:
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
56class Systemd : public QObject {
57 Q_OBJECT;
58 QML_SINGLETON;
59 QML_ELEMENT;
60
61public:
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 sleep();
74 Q_INVOKABLE void suspend();
75 Q_INVOKABLE void hibernate();
76
77 bool idleHint();
78 bool lockedHint();
79 ObjectModel<SystemdUnit>* userUnits();
80
81signals:
82 void prepareForShutdown(bool before);
83 void prepareForSleep(bool before);
84 void lock();
85 void unlock();
86 void idleHintChanged();
87 void lockedHintChanged();
88
89private slots:
90 void onLogindSessionPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties);
91 void onUserUnitNew(const QString& id, const QDBusObjectPath& objectPath);
92 void onUserUnitRemoved(const QString& id, const QDBusObjectPath& objectPath);
93
94private:
95 DBusSystemdManager* systemdManager;
96 DBusLogindManager* logindManager;
97 DBusLogindSession* logindSession;
98 DBusProperties* logindSessionProperties;
99
100 ObjectModel<SystemdUnit> mUserUnits{this};
101};
102
103class SystemdInhibitorParams : public QObject {
104 Q_OBJECT;
105 QML_ELEMENT;
106 QML_SINGLETON;
107
108public:
109 enum WhatItem : uint8_t {
110 Shutdown = 0b1,
111 Sleep = 0b10,
112 Idle = 0b100,
113 HandlePowerKey = 0b1000,
114 HandleSuspendKey = 0b10000,
115 HandleHibernateKey = 0b100000,
116 HandleLidSwitch = 0b1000000,
117 };
118 Q_ENUM(WhatItem);
119 Q_DECLARE_FLAGS(What, WhatItem);
120
121 enum Mode : uint8_t {
122 Block = 1,
123 BlockWeak = 2,
124 Delay = 3,
125 };
126 Q_ENUM(Mode);
127
128 Q_INVOKABLE static std::string toString(WhatItem what);
129 Q_INVOKABLE static std::string toString(What what);
130 Q_INVOKABLE static std::string toString(Mode mode);
131
132 static constexpr WhatItem allWhatItems[] = { Shutdown, Sleep, Idle, HandlePowerKey, HandleSuspendKey, HandleHibernateKey, HandleLidSwitch };
133};
134Q_DECLARE_OPERATORS_FOR_FLAGS(SystemdInhibitorParams::What)
135
136class SystemdInhibitor : public QObject {
137 Q_OBJECT;
138 Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged);
139 Q_PROPERTY(SystemdInhibitorParams::What what READ what WRITE setWhat NOTIFY whatChanged);
140 Q_PROPERTY(QString who READ who WRITE setWho NOTIFY whoChanged);
141 Q_PROPERTY(QString why READ why WRITE setWhy NOTIFY whyChanged);
142 Q_PROPERTY(SystemdInhibitorParams::Mode mode READ mode WRITE setMode NOTIFY modeChanged);
143 QML_ELEMENT;
144
145public:
146 explicit SystemdInhibitor(QObject* parent = nullptr): QObject(parent) {}
147
148 bool enabled() const;
149 void setEnabled(bool enabled);
150
151 SystemdInhibitorParams::What what() const;
152 void setWhat(SystemdInhibitorParams::What what);
153
154 QString who() const;
155 void setWho(QString who);
156
157 QString why() const;
158 void setWhy(QString why);
159
160 SystemdInhibitorParams::Mode mode() const;
161 void setMode(SystemdInhibitorParams::Mode mode);
162
163 Q_INVOKABLE void release();
164
165signals:
166 void enabledChanged();
167 void whatChanged();
168 void whoChanged();
169 void whyChanged();
170 void modeChanged();
171
172private:
173 class ActiveSystemdInhibitor {
174 public:
175 uint32_t fd = -1;
176 SystemdInhibitorParams::What what;
177 QString who;
178 QString why;
179 SystemdInhibitorParams::Mode mode;
180
181 ActiveSystemdInhibitor(SystemdInhibitorParams::What what_, QString who_, QString why_, SystemdInhibitorParams::Mode mode_);
182 ~ActiveSystemdInhibitor();
183 };
184
185 void update();
186
187 bool mEnabled = true;
188 std::unique_ptr<ActiveSystemdInhibitor> activeInhibitor;
189 SystemdInhibitorParams::What mWhat = static_cast<SystemdInhibitorParams::What>(0);
190 QString mWho;
191 QString mWhy;
192 SystemdInhibitorParams::Mode mMode = static_cast<SystemdInhibitorParams::Mode>(0);
193};
194