GCC Code Coverage Report


Directory: ./
File: backends/output_filer.h
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 106 111 95.5%
Branches: 35 42 83.3%

Line Branch Exec Source
1 /*************************************************************************
2 Copyright © 2020 Roman Gilg <subdiff@gmail.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 **************************************************************************/
18 #pragma once
19
20 #include "filer_controller.h"
21 #include "filer_helpers.h"
22
23 #include <output.h>
24 #include <types.h>
25
26 #include <QObject>
27 #include <QVariantMap>
28
29 #include <memory>
30
31 namespace Disman
32 {
33
34 class Output_filer
35 {
36 public:
37 232 Output_filer(OutputPtr output, Filer_controller* controller, std::string const& dir_path)
38 232 : m_output(output)
39 232 , m_controller(controller)
40 232 , m_dir_path{dir_path}
41 {
42 232 read_file();
43 232 }
44
45 104 OutputPtr output() const
46 {
47 104 return m_output;
48 }
49
50 1024 std::string hash() const
51 {
52 1024 return m_output->hash();
53 }
54
55 640 std::string name() const
56 {
57 640 return m_output->name();
58 }
59
60 template<typename T>
61 5348 T get_value(std::string const& id,
62 T default_value,
63 std::function<T(OutputPtr const&, QVariant const&, T)> getter) const
64 {
65 5348 auto const val = m_info[QString::fromStdString(id)];
66 5348 return getter(m_output, val, default_value);
67 }
68
69 template<typename T>
70 1200 void set_value(std::string const& id,
71 T value,
72 std::function<void(QVariantMap&, std::string const&, T)> setter)
73 {
74
2/2
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 549 times.
1200 if (m_info.isEmpty()) {
75 102 m_info = create_info(m_output);
76 }
77 1200 setter(m_info, id, value);
78 }
79
80 198 static QVariantMap create_info(OutputPtr const& output)
81 {
82 396 auto metadata = [&output]() {
83 198 QVariantMap metadata;
84 396 metadata[QStringLiteral("name")] = QString::fromStdString(output->name());
85 396 metadata[QStringLiteral("description")] = QString::fromStdString(output->description());
86 198 return metadata;
87 198 };
88 198 QVariantMap outputInfo;
89 396 outputInfo[QStringLiteral("id")] = QString::fromStdString(output->hash());
90 396 outputInfo[QStringLiteral("metadata")] = metadata();
91 198 return outputInfo;
92 }
93
94 812 static ModePtr get_mode(OutputPtr const& output, QVariant const& val, ModePtr default_value)
95 {
96 812 auto const mode_map = val.toMap();
97 812 bool success = true;
98
99 812 auto get_resolution = [&mode_map, &success]() {
100 812 auto const key = QStringLiteral("resolution");
101
102
2/2
✓ Branch 1 taken 158 times.
✓ Branch 2 taken 654 times.
812 if (!mode_map.contains(key)) {
103
2/2
✓ Branch 7 taken 158 times.
✓ Branch 8 taken 158 times.
316 qCWarning(DISMAN_BACKEND) << "Output config does not contain resolution key.";
104 158 success = false;
105 158 return QSize();
106 }
107
108 654 auto const resolution_map = mode_map[key].toMap();
109
110 2616 auto get_length = [&resolution_map, &success](QString axis) {
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1308 times.
1308 if (!resolution_map.contains(axis)) {
112 qCWarning(DISMAN_BACKEND) << "Resolution entry does not contain axis:" << axis;
113 success = false;
114 return 0.;
115 }
116 bool ok;
117 1308 auto const coord = resolution_map[axis].toDouble(&ok);
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1308 times.
1308 if (!ok) {
119 qCWarning(DISMAN_BACKEND) << "Could not read value of axis:" << axis;
120 success = false;
121 }
122 1308 return coord;
123 654 };
124
125 654 auto const width = get_length(QStringLiteral("width"));
126 654 auto const height = get_length(QStringLiteral("height"));
127 654 return QSize(width, height);
128 812 };
129 812 auto const resolution = get_resolution();
130
131 812 auto get_refresh_rate = [&mode_map, &success]() -> double {
132 812 auto const key = QStringLiteral("refresh");
133
134
2/2
✓ Branch 1 taken 158 times.
✓ Branch 2 taken 654 times.
812 if (!mode_map.contains(key)) {
135
2/2
✓ Branch 7 taken 158 times.
✓ Branch 8 taken 158 times.
316 qCWarning(DISMAN_BACKEND) << "Mode entry does not contain refresh key.";
136 158 success = false;
137 158 return 0;
138 }
139
140 bool ok;
141 654 auto const refresh = mode_map[key].toInt(&ok);
142 654 success &= ok;
143 654 return refresh;
144 812 };
145 812 auto const refresh = get_refresh_rate();
146
147
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 654 times.
812 if (!success) {
148
2/2
✓ Branch 11 taken 158 times.
✓ Branch 12 taken 158 times.
316 qCWarning(DISMAN_BACKEND) << "Mode entry broken for:" << output;
149 158 return default_value;
150 }
151
152
2/2
✓ Branch 9 taken 4684 times.
✓ Branch 10 taken 60 times.
4744 for (auto const& [key, mode] : output->modes()) {
153
5/6
✓ Branch 3 taken 594 times.
✓ Branch 4 taken 4090 times.
✓ Branch 7 taken 594 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 594 times.
✓ Branch 10 taken 4090 times.
4684 if (mode->size() == resolution && mode->refresh() == refresh) {
154 594 return mode;
155 }
156
2/2
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 594 times.
654 }
157 60 return default_value;
158 812 }
159
160 782 static Output::Rotation convert_int_to_rotation(int val)
161 {
162 782 auto const rotation = static_cast<Output::Rotation>(val);
163
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 768 times.
782 switch (rotation) {
164 6 case Output::Rotation::Left:
165 6 return rotation;
166 4 case Output::Rotation::Right:
167 4 return rotation;
168 4 case Output::Rotation::Inverted:
169 4 return rotation;
170 768 default:
171 768 return Output::Rotation::None;
172 }
173 }
174
175 336 QFileInfo file_info() const
176 {
177 336 return Filer_helpers::file_info(m_dir_path + "outputs/", m_output->hash());
178 }
179
180 232 void read_file()
181 {
182 232 Filer_helpers::read_file(file_info(), m_info);
183 232 }
184
185 104 bool write_file()
186 {
187 104 return Filer_helpers::write_file(m_info, file_info());
188 }
189
190 485 void get_global_data(OutputPtr& output)
191 {
192
2/2
✓ Branch 5 taken 158 times.
✓ Branch 6 taken 327 times.
970 if (!m_info.contains(QStringLiteral("mode"))) {
193 188 return;
194 }
195 654 auto mode = get_mode(output, m_info[QStringLiteral("mode")], nullptr);
196
2/2
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 297 times.
327 if (!mode) {
197 30 return;
198 }
199
200 auto adapt_sync
201 594 = Filer_helpers::from_variant(m_info[QStringLiteral("adaptive-sync")], false);
202 297 auto rotation = convert_int_to_rotation(Filer_helpers::from_variant(
203 891 m_info[QStringLiteral("rotation")], static_cast<int>(Output::Rotation::None)));
204 594 auto scale = Filer_helpers::from_variant(m_info[QStringLiteral("scale")], 1.);
205
206 auto auto_resolution
207 594 = Filer_helpers::from_variant(m_info[QStringLiteral("auto-resolution")], true);
208 auto auto_refresh_rate
209 594 = Filer_helpers::from_variant(m_info[QStringLiteral("auto-refresh-rate")], true);
210 auto auto_rotate
211 594 = Filer_helpers::from_variant(m_info[QStringLiteral("auto-rotate")], false);
212 auto auto_rotate_only_in_tablet_mode
213 594 = Filer_helpers::from_variant(m_info[QStringLiteral("auto-rotate-tablet-only")], false);
214
215 594 output->set_global_data({mode->size(),
216 297 mode->refresh(),
217 adapt_sync,
218 rotation,
219 scale,
220 auto_resolution,
221 auto_refresh_rate,
222 auto_rotate,
223 auto_rotate_only_in_tablet_mode});
224
2/2
✓ Branch 1 taken 297 times.
✓ Branch 2 taken 30 times.
327 }
225
226 private:
227 OutputPtr m_output;
228 Filer_controller* m_controller;
229
230 std::string m_dir_path;
231 QVariantMap m_info;
232 };
233
234 }
235