summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.hpp
blob: 84752d763fb730902248919f595f7c9a5da401fa (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
#pragma once

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

#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_properties.h"

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_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);

  bool idleHint();
  bool lockedHint();

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);

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

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);
};