GCC Code Coverage Report


Directory: ./
File: backends/device.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 14 68 20.6%
Branches: 3 34 8.8%

Line Branch Exec Source
1 /*
2 SPDX-FileCopyrightText: 2020 Roman Gilg <subdiff@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only
5 */
6 #include "device.h"
7
8 #include "freedesktop_interface.h"
9 #include "logging.h"
10
11 #include <QDBusConnection>
12
13 namespace Disman
14 {
15
16 58 Device::Device(QObject* parent)
17 : QObject(parent)
18 58 , m_lid_timer{new QTimer}
19 {
20 58 m_lid_timer->setInterval(1000);
21 58 m_lid_timer->setSingleShot(true);
22 58 connect(m_lid_timer.get(), &QTimer::timeout, this, &Device::lid_open_changed);
23
24 116 m_upower = new OrgFreedesktopDBusPropertiesInterface(QStringLiteral("org.freedesktop.UPower"),
25 116 QStringLiteral("/org/freedesktop/UPower"),
26 58 QDBusConnection::systemBus(),
27 116 this);
28
1/2
✓ Branch 1 taken 58 times.
✗ Branch 2 not taken.
58 if (!m_upower->isValid()) {
29
2/2
✓ Branch 6 taken 58 times.
✓ Branch 7 taken 58 times.
116 qCDebug(DISMAN_BACKEND) << "UPower not available, no lid detection."
30 58 << m_upower->lastError().message();
31 58 return;
32 }
33
34 QDBusConnection::systemBus().connect(QStringLiteral("org.freedesktop.UPower"),
35 QStringLiteral("/org/freedesktop/UPower"),
36 QStringLiteral("org.freedesktop.DBus.Properties"),
37 QStringLiteral("PropertiesChanged"),
38 this,
39 SLOT(fetch_lid_closed()));
40
41 m_login1 = new QDBusInterface(QStringLiteral("org.freedesktop.login1"),
42 QStringLiteral("/org/freedesktop/login1"),
43 QStringLiteral("org.freedesktop.login1.Manager"),
44 QDBusConnection::systemBus(),
45 this);
46 if (!m_login1->isValid()) {
47 qCDebug(DISMAN_BACKEND) << "logind not available, no lid detection."
48 << m_login1->lastError().message();
49 return;
50 }
51
52 connect(m_login1, SIGNAL(PrepareForSleep(bool)), this, SLOT(prepare_for_sleep(bool)));
53 fetch_lid_present();
54 }
55
56 228 Device::~Device() = default;
57
58 bool Device::lid_present() const
59 {
60 return m_lid_present;
61 }
62
63 bool Device::lid_open() const
64 {
65 return !m_lid_closed;
66 }
67
68 void Device::fetch_lid_present()
69 {
70 auto res
71 = m_upower->Get(QStringLiteral("org.freedesktop.UPower"), QStringLiteral("LidIsPresent"));
72
73 connect(new QDBusPendingCallWatcher(res),
74 &QDBusPendingCallWatcher::finished,
75 this,
76 &Device::lid_present_fetched);
77 }
78
79 void Device::lid_present_fetched(QDBusPendingCallWatcher* watcher)
80 {
81 QDBusPendingReply<QVariant> const reply = *watcher;
82 if (reply.isError()) {
83 qCDebug(DISMAN_BACKEND) << "Error when fetching lid information: "
84 << reply.error().message();
85 return;
86 }
87
88 m_lid_present = reply.value().toBool();
89 watcher->deleteLater();
90
91 if (m_lid_present) {
92 fetch_lid_closed();
93 }
94 m_ready = true;
95 }
96
97 void Device::fetch_lid_closed()
98 {
99 auto res
100 = m_upower->Get(QStringLiteral("org.freedesktop.UPower"), QStringLiteral("LidIsClosed"));
101
102 connect(new QDBusPendingCallWatcher(res),
103 &QDBusPendingCallWatcher::finished,
104 this,
105 &Device::lid_closed_fetched);
106 }
107
108 void Device::lid_closed_fetched(QDBusPendingCallWatcher* watcher)
109 {
110 QDBusPendingReply<QVariant> const reply = *watcher;
111 if (reply.isError()) {
112 qCDebug(DISMAN_BACKEND) << "Error when fetching lid closed: " << reply.error().message();
113 return;
114 }
115
116 auto const closed = reply.value().toBool();
117 watcher->deleteLater();
118
119 if (closed == m_lid_closed) {
120 return;
121 }
122
123 m_lid_closed = closed;
124 if (m_ready && !closed) {
125 m_lid_timer->stop();
126 Q_EMIT lid_open_changed();
127 } else {
128 m_lid_timer->start();
129 }
130 }
131
132 void Device::prepare_for_sleep(bool start)
133 {
134 qCDebug(DISMAN_BACKEND) << "Device sleep change:" << (start ? "going to sleep" : "waking up");
135 if (start) {
136 m_lid_timer->stop();
137 }
138 // TODO: On start being false should we query the backend for changes in between? KDisplay
139 // daemon does that.
140 }
141
142 }
143