summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.hpp
blob: 98f73d8feee3f299fa505cd7b1af0571da9f36c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#pragma once

#include <cstdint>
#include <memory>
#include <string>
#include <optional>

#include <QObject>
#include <QDBusInterface>
#include <QtQmlIntegration/qqmlintegration.h>

#include "dbus_systemd_manager.h"
#include "dbus_logind_manager.h"
#include "dbus_logind_session.h"
#include "dbus_systemd_unit.h"
#include "dbus_systemd_service.h"
#include "dbus_properties.h"

#include "core/model.hpp"

class SystemdUnit : public QObject {
  Q_OBJECT;
  QML_ELEMENT;
  QML_UNCREATABLE("SystemdUnits cannot be created directly");

public:
  explicit SystemdUnit(QObject* parent, const QDBusObjectPath& path, const QString& id);

  Q_PROPERTY(QDBusObjectPath path READ path CONSTANT);
  Q_PROPERTY(QString id READ id CONSTANT);
  Q_PROPERTY(QList<QString> runtimeDirectory READ runtimeDirectory CONSTANT);
  Q_PROPERTY(QString activeState READ activeState NOTIFY activeStateChanged);

  const QDBusObjectPath& path() const;
  const QString& id() const;
  QList<QString> runtimeDirectory();
  QString activeState();

signals:
  void activeStateChanged();

private slots:
  void onUnitPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties);

private:
  std::unique_ptr<DBusSystemdUnit> systemdUnit();
  std::unique_ptr<DBusSystemdService> systemdService();

  DBusProperties* mUnitProperties;
  QDBusObjectPath mPath;
  QString mId;
  std::optional<QString> mActiveState;
  std::optional<QList<QString>> mRuntimeDirectory;
};

class Systemd : public QObject {
  Q_OBJECT;
  QML_SINGLETON;
  QML_ELEMENT;

public:
  explicit Systemd(QObject* parent = nullptr);

  Q_PROPERTY(bool idleHint READ idleHint WRITE setIdleHint NOTIFY idleHintChanged)
  Q_PROPERTY(bool lockedHint READ lockedHint WRITE setLockedHint NOTIFY lockedHintChanged)
  Q_PROPERTY(UntypedObjectModel* userUnits READ userUnits CONSTANT)

  Q_INVOKABLE void stopUserUnit(const QString& unit, const QString& mode);
  Q_INVOKABLE void setBrightness(const QString& subsystem, const QString& name, quint32 brightness);
  Q_INVOKABLE void setIdleHint(bool idle);
  Q_INVOKABLE void setLockedHint(bool locked);
  Q_INVOKABLE void lockSession();
  Q_INVOKABLE void suspend();
  Q_INVOKABLE void hibernate();

  bool idleHint();
  bool lockedHint();
  ObjectModel<SystemdUnit>* userUnits();

signals:
  void shutdown(bool before);
  void sleep(bool before);
  void lock();
  void unlock();
  void idleHintChanged();
  void lockedHintChanged();

private slots:
  void onLogindSessionPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties);
  void onUserUnitNew(const QString& id, const QDBusObjectPath& objectPath);
  void onUserUnitRemoved(const QString& id, const QDBusObjectPath& objectPath);

private:
  DBusSystemdManager* systemdManager;
  DBusLogindManager* logindManager;
  DBusLogindSession* logindSession;
  DBusProperties* logindSessionProperties;

  ObjectModel<SystemdUnit> mUserUnits{this};
};

class SystemdInhibitorParams : public QObject {
  Q_OBJECT;
  QML_ELEMENT;
  QML_SINGLETON;

public:
  enum WhatItem : uint8_t {
    Shutdown = 0b1,
    Sleep = 0b10,
    Idle = 0b100,
    HandlePowerKey = 0b1000,
    HandleSuspendKey = 0b10000,
    HandleHibernateKey = 0b100000,
    HandleLidSwitch = 0b1000000,
  };
  Q_ENUM(WhatItem);
  Q_DECLARE_FLAGS(What, WhatItem);

  enum Mode : uint8_t {
    Block = 1,
    BlockWeak = 2,
    Delay = 3,
  };
  Q_ENUM(Mode);

  Q_INVOKABLE static std::string toString(WhatItem what);
  Q_INVOKABLE static std::string toString(What what);
  Q_INVOKABLE static std::string toString(Mode mode);

  static constexpr WhatItem allWhatItems[] = { Shutdown, Sleep, Idle, HandlePowerKey, HandleSuspendKey, HandleHibernateKey, HandleLidSwitch };
};
Q_DECLARE_OPERATORS_FOR_FLAGS(SystemdInhibitorParams::What)

class SystemdInhibitor : public QObject {
  Q_OBJECT;
  Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged);
  Q_PROPERTY(SystemdInhibitorParams::What what READ what WRITE setWhat NOTIFY whatChanged);
  Q_PROPERTY(QString who READ who WRITE setWho NOTIFY whoChanged);
  Q_PROPERTY(QString why READ why WRITE setWhy NOTIFY whyChanged);
  Q_PROPERTY(SystemdInhibitorParams::Mode mode READ mode WRITE setMode NOTIFY modeChanged);
  QML_ELEMENT;

public:
  explicit SystemdInhibitor(QObject* parent = nullptr): QObject(parent) {}

  bool enabled() const;
  void setEnabled(bool enabled);

  SystemdInhibitorParams::What what() const;
  void setWhat(SystemdInhibitorParams::What what);

  QString who() const;
  void setWho(QString who);

  QString why() const;
  void setWhy(QString why);

  SystemdInhibitorParams::Mode mode() const;
  void setMode(SystemdInhibitorParams::Mode mode);

  Q_INVOKABLE void release();

signals:
  void enabledChanged();
  void whatChanged();
  void whoChanged();
  void whyChanged();
  void modeChanged();

private:
  class ActiveSystemdInhibitor {
  public:
    uint32_t fd = -1;
    SystemdInhibitorParams::What what;
    QString who;
    QString why;
    SystemdInhibitorParams::Mode mode;

    ActiveSystemdInhibitor(SystemdInhibitorParams::What what_, QString who_, QString why_, SystemdInhibitorParams::Mode mode_);
    ~ActiveSystemdInhibitor();
  };

  void update();

  bool mEnabled = true;
  std::unique_ptr<ActiveSystemdInhibitor> activeInhibitor;
  SystemdInhibitorParams::What mWhat = static_cast<SystemdInhibitorParams::What>(0);
  QString mWho;
  QString mWhy;
  SystemdInhibitorParams::Mode mMode = static_cast<SystemdInhibitorParams::Mode>(0);
};