summaryrefslogtreecommitdiff
path: root/accounts/gkleen@sif/shell/quickshell-plugins/Systemd.cpp
blob: 790f514f1682d809e7cb4a7961acfa375a423f3f (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
194
195
196
197
198
199
200
201
202
203
204
205
#include "Systemd.hpp"

#include <sstream>

#include <QDBusConnection>
#include <QDBusReply>
#include <QDebug>
#include <QDBusUnixFileDescriptor>
#include <QDBusObjectPath>

Systemd::Systemd(QObject* parent) : QObject(parent) {
  this->systemdManager = new DBusSystemdManager(
    "org.freedesktop.systemd1",
    "/org/freedesktop/systemd1",
    QDBusConnection::sessionBus(),
    this
  );
  this->logindManager = new DBusLogindManager(
    "org.freedesktop.login1",
    "/org/freedesktop/login1",
    QDBusConnection::systemBus(),
    this
  );

  QDBusReply<QDBusObjectPath> sessionPath = this->logindManager->GetSession("auto");
  qDebug() << sessionPath;
  this->logindSession = new DBusLogindSession(
    "org.freedesktop.login1",
    sessionPath.value().path(),
    QDBusConnection::systemBus(),
    this
  );
  this->logindSessionProperties = new DBusProperties(
    "org.freedesktop.login1",
    sessionPath.value().path(),
    QDBusConnection::systemBus(),
    this
  );

  QObject::connect(this->logindManager, &DBusLogindManager::PrepareForShutdown, this, &Systemd::shutdown);
  QObject::connect(this->logindManager, &DBusLogindManager::PrepareForSleep, this, &Systemd::sleep);
  QObject::connect(this->logindSession, &DBusLogindSession::Lock, this, &Systemd::lock);
  QObject::connect(this->logindSession, &DBusLogindSession::Unlock, this, &Systemd::unlock);
  QObject::connect(this->logindSessionProperties, &DBusProperties::PropertiesChanged, this, &Systemd::onLogindSessionPropertiesChanged);
}

void Systemd::onLogindSessionPropertiesChanged(const QString& interface_name, const QVariantMap& changed_properties, const QStringList& invalidated_properties) {
  if (changed_properties.contains("IdleHint") || invalidated_properties.contains("IdleHint"))
    emit this->idleHintChanged();
  if (changed_properties.contains("LockedHint") || invalidated_properties.contains("LockedHint"))
    emit this->lockedHintChanged();
}

void Systemd::stopUserUnit(const QString& unit, const QString& mode) {
  this->systemdManager->StopUnit(unit, mode);
}

void Systemd::setBrightness(const QString& subsystem, const QString& name, quint32 brightness) {
  this->logindSession->SetBrightness(subsystem, name, brightness);
}

bool Systemd::idleHint() {
  return this->logindSession->idleHint();
}
void Systemd::setIdleHint(bool idle) {
  this->logindSession->SetIdleHint(idle);
}
bool Systemd::lockedHint() {
  return this->logindSession->lockedHint();
}
void Systemd::setLockedHint(bool locked) {
  this->logindSession->SetLockedHint(locked);
}

std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::WhatItem what) {
  if (what == SystemdInhibitorParams::Shutdown)
    return "shutdown";
  else if (what == SystemdInhibitorParams::Sleep)
    return "sleep";
  else if (what == SystemdInhibitorParams::Idle)
    return "idle";
  else if (what == SystemdInhibitorParams::HandlePowerKey)
    return "handle-power-key";
  else if (what == SystemdInhibitorParams::HandleSuspendKey)
    return "handle-suspend-key";
  else if (what == SystemdInhibitorParams::HandleHibernateKey)
    return "handle-hibernate-key";
  else if (what == SystemdInhibitorParams::HandleLidSwitch)
    return "handle-lid-switch";
  return "";
}

std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::What what) {
  std::ostringstream res;
  bool first = true;
  for (const WhatItem& item: SystemdInhibitorParams::allWhatItems) {
    if (!(what & item))
      continue;

    if (!first)
      res << ":";
    else
      first = false;
    res << SystemdInhibitorParams::toString(item);
  }
  return res.str();
}

std::string SystemdInhibitorParams::toString(SystemdInhibitorParams::Mode mode) {
  if (mode == SystemdInhibitorParams::Block)
    return "block";
  else if (mode == SystemdInhibitorParams::BlockWeak)
    return "block-weak";
  else if (mode == SystemdInhibitorParams::Delay)
    return "delay";
  return "";
}

bool SystemdInhibitor::enabled() const { return static_cast<bool>(this->activeInhibitor); }
void SystemdInhibitor::setEnabled(bool enabled) {
  if (enabled)
    this->update();
  else
    this->release();
}

SystemdInhibitorParams::What SystemdInhibitor::what() const { return this->mWhat; }
void SystemdInhibitor::setWhat(SystemdInhibitorParams::What what) {
  this->mWhat = what;
  this->update();
}

QString SystemdInhibitor::who() const { return this->mWho; }
void SystemdInhibitor::setWho(QString who) {
  this->mWho = who;
  this->update();
}

QString SystemdInhibitor::why() const { return this->mWhy; }
void SystemdInhibitor::setWhy(QString why) {
  this->mWhy = why;
  this->update();
}

SystemdInhibitorParams::Mode SystemdInhibitor::mode() const { return this->mMode; }
void SystemdInhibitor::setMode(SystemdInhibitorParams::Mode mode) {
  this->mMode = mode;
  this->update();
}

SystemdInhibitor::ActiveSystemdInhibitor::ActiveSystemdInhibitor(SystemdInhibitorParams::What what_, QString who_, QString why_, SystemdInhibitorParams::Mode mode_): what(what_), who(who_), why(why_), mode(mode_) {
  DBusLogindManager logindManager(
    "org.freedesktop.login1",
    "/org/freedesktop/login1",
    QDBusConnection::systemBus()
  );
  QDBusReply<QDBusUnixFileDescriptor> fd_ = logindManager.Inhibit(QString::fromStdString(SystemdInhibitorParams::toString(what)), who, why, QString::fromStdString(SystemdInhibitorParams::toString(mode)));
  if (fd_.error().isValid())
    throw fd_.error();
  this->fd = ::dup(fd_.value().fileDescriptor());
}
SystemdInhibitor::ActiveSystemdInhibitor::~ActiveSystemdInhibitor() {
  if (this->fd != -1)
    ::close(this->fd);
}

void SystemdInhibitor::release() {
  if (!this->activeInhibitor)
    return;

  this->activeInhibitor.reset();
  emit this->enabledChanged();
}

void SystemdInhibitor::update() {
  if (!this->mWhat || this->mWho.isEmpty() || this->mWhy.isEmpty() || !this->mMode)
    if (this->activeInhibitor)
      this->release();
    else
      return;

  if (this->activeInhibitor && this->mWhat == this->activeInhibitor->what && this->mWho == this->activeInhibitor->who && this->mWhy == this->activeInhibitor->why && this->mMode == this->activeInhibitor->mode)
    return;

  std::unique_ptr<ActiveSystemdInhibitor> otherInhibitor;
  try {
    otherInhibitor.reset(new SystemdInhibitor::ActiveSystemdInhibitor(this->mWhat, this->mWho, this->mWhy, this->mMode));
  } catch (const QDBusError& err) {
    qCritical().noquote()
      << err.name().toStdString() << " " << err.message().toStdString();
    return;
  }
  this->activeInhibitor.swap(otherInhibitor);

  if (!otherInhibitor && this->activeInhibitor)
    emit this->enabledChanged();
  if (otherInhibitor && otherInhibitor->what != this->activeInhibitor->what)
    emit this->whatChanged();
  if (otherInhibitor && otherInhibitor->who != this->activeInhibitor->who)
    emit this->whoChanged();
  if (otherInhibitor && otherInhibitor->why != this->activeInhibitor->why)
    emit this->whyChanged();
  if (otherInhibitor && otherInhibitor->mode != this->activeInhibitor->mode)
    emit this->modeChanged();
}