-
Notifications
You must be signed in to change notification settings - Fork 1
/
config_handler.cpp
256 lines (217 loc) · 7.66 KB
/
config_handler.cpp
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/********************************************************************
Copyright © 2019 Roman Gilg <subdiff@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "config_handler.h"
#include "kcm_kdisplay_debug.h"
#include "output_model.h"
#include <disman/configmonitor.h>
#include <disman/getconfigoperation.h>
#include <QRect>
using namespace Disman;
ConfigHandler::ConfigHandler(QObject* parent)
: QObject(parent)
{
}
void ConfigHandler::setConfig(Disman::ConfigPtr config)
{
m_config = config;
m_initialConfig = m_config->clone();
Disman::ConfigMonitor::instance()->add_config(m_config);
m_outputs = new OutputModel(this);
connect(
m_outputs, &OutputModel::positionChanged, this, &ConfigHandler::checkScreenNormalization);
connect(m_outputs, &OutputModel::sizeChanged, this, &ConfigHandler::checkScreenNormalization);
for (auto const& [key, output] : config->outputs()) {
initOutput(output);
}
m_lastNormalizedScreenSize = screenSize();
connect(m_outputs, &OutputModel::changed, this, [this]() {
checkNeedsSave();
Q_EMIT changed();
});
connect(m_config.get(), &Disman::Config::output_added, this, [this]() {
Q_EMIT outputConnect(true);
});
connect(m_config.get(), &Disman::Config::output_removed, this, [this]() {
Q_EMIT outputConnect(false);
});
connect(m_config.get(),
&Disman::Config::primary_output_changed,
this,
&ConfigHandler::primaryOutputChanged);
Q_EMIT outputModelChanged();
}
void ConfigHandler::initOutput(const Disman::OutputPtr& output)
{
m_outputs->add(output);
}
void ConfigHandler::updateInitialData()
{
connect(
new GetConfigOperation(), &GetConfigOperation::finished, this, [this](ConfigOperation* op) {
if (op->has_error()) {
return;
}
m_initialConfig = qobject_cast<GetConfigOperation*>(op)->config();
checkNeedsSave();
});
}
void ConfigHandler::checkNeedsSave()
{
if (m_config->supported_features() & Disman::Config::Feature::PrimaryDisplay) {
if (m_config->primary_output() && m_initialConfig->primary_output()) {
if (m_config->primary_output()->hash() != m_initialConfig->primary_output()->hash()) {
Q_EMIT needsSaveChecked(true);
return;
}
} else if ((bool)m_config->primary_output() != (bool)m_initialConfig->primary_output()) {
Q_EMIT needsSaveChecked(true);
return;
}
}
for (auto const& [key, output] : m_config->outputs()) {
auto const hash = output->hash();
for (auto const& [initial_key, initialOutput] : m_initialConfig->outputs()) {
if (hash != initialOutput->hash()) {
continue;
}
bool needsSave = false;
if (output->enabled() != initialOutput->enabled()) {
needsSave = true;
}
if (output->enabled()) {
needsSave |= output->auto_mode()->id() != initialOutput->auto_mode()->id()
|| output->position() != initialOutput->position()
|| output->scale() != initialOutput->scale()
|| output->rotation() != initialOutput->rotation()
|| output->adaptive_sync() != initialOutput->adaptive_sync()
|| output->replication_source() != initialOutput->replication_source()
|| output->retention() != initialOutput->retention()
|| output->auto_resolution() != initialOutput->auto_resolution()
|| output->auto_refresh_rate() != initialOutput->auto_refresh_rate()
|| output->auto_rotate() != initialOutput->auto_rotate()
|| output->auto_rotate_only_in_tablet_mode()
!= initialOutput->auto_rotate_only_in_tablet_mode();
}
if (needsSave) {
Q_EMIT needsSaveChecked(true);
return;
}
break;
}
}
Q_EMIT needsSaveChecked(false);
}
QSize ConfigHandler::screenSize() const
{
int width = 0, height = 0;
QSize size;
for (auto const& [key, output] : m_config->outputs()) {
if (!output->positionable()) {
continue;
}
const int outputRight = output->geometry().right();
const int outputBottom = output->geometry().bottom();
if (outputRight > width) {
width = outputRight;
}
if (outputBottom > height) {
height = outputBottom;
}
}
if (width > 0 && height > 0) {
size = QSize(width, height);
} else {
size = QSize();
}
return size;
}
QSize ConfigHandler::normalizeScreen()
{
if (!m_config) {
return QSize();
}
bool changed = m_outputs->normalizePositions();
const auto currentScreenSize = screenSize();
changed |= m_lastNormalizedScreenSize != currentScreenSize;
m_lastNormalizedScreenSize = currentScreenSize;
Q_EMIT screenNormalizationUpdate(true);
return currentScreenSize;
}
void ConfigHandler::checkScreenNormalization()
{
const bool normalized = !m_config
|| (m_lastNormalizedScreenSize == screenSize() && m_outputs->positionsNormalized());
Q_EMIT screenNormalizationUpdate(normalized);
}
void ConfigHandler::primaryOutputSelected(int index)
{
Q_UNUSED(index)
// TODO
}
void ConfigHandler::primaryOutputChanged(const Disman::OutputPtr& output)
{
Q_UNUSED(output)
}
Disman::Output::Retention ConfigHandler::getRetention() const
{
using Retention = Disman::Output::Retention;
auto ret = Retention::Undefined;
if (!m_config) {
return ret;
}
const auto outputs = m_config->outputs();
if (outputs.empty()) {
return ret;
}
ret = outputs.begin()->second->retention();
for (auto const& [key, output] : outputs) {
const auto outputRet = output->retention();
if (ret != outputRet) {
// Control file with different retention values per output.
return Retention::Undefined;
}
}
if (ret == Retention::Undefined) {
// If all outputs have undefined retention,
// this should be displayed as global retention.
return Retention::Global;
}
return ret;
}
int ConfigHandler::retention() const
{
return static_cast<int>(getRetention());
}
void ConfigHandler::setRetention(int retention)
{
using Retention = Disman::Output::Retention;
if (!m_config) {
return;
}
if (retention != static_cast<int>(Retention::Global)
&& retention != static_cast<int>(Retention::Individual)) {
// We only allow setting to global or individual retention.
return;
}
if (retention == ConfigHandler::retention()) {
return;
}
auto ret = static_cast<Retention>(retention);
for (auto const& [key, output] : m_config->outputs()) {
output->set_retention(ret);
}
checkNeedsSave();
Q_EMIT retentionChanged();
Q_EMIT changed();
}