GCC Code Coverage Report


Directory: ./
File: backends/qscreen/qscreenconfig.cpp
Date: 2023-04-20 22:59:23
Exec Total Coverage
Lines: 40 58 69.0%
Branches: 14 32 43.8%

Line Branch Exec Source
1 /*************************************************************************************
2 * Copyright 2014 Sebastian K├╝gler <sebas@kde.org> *
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 #include "qscreenconfig.h"
19 #include "qscreenbackend.h"
20 #include "qscreenoutput.h"
21 #include "qscreenscreen.h"
22
23 #include "qscreen_logging.h"
24
25 #include "mode.h"
26 #include "output.h"
27
28 #include <QGuiApplication>
29 #include <QRect>
30 #include <QScreen>
31
32 using namespace Disman;
33
34 3 QScreenConfig::QScreenConfig(QObject* parent)
35 : QObject(parent)
36 3 , m_screen(new QScreenScreen(this))
37 3 , m_blockSignals(true)
38 {
39
2/2
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 3 times.
5 foreach (const QScreen* qscreen, QGuiApplication::screens()) {
40 2 screenAdded(qscreen);
41 3 }
42 3 m_blockSignals = false;
43 3 connect(qApp, &QGuiApplication::screenAdded, this, &QScreenConfig::screenAdded);
44 3 connect(qApp, &QGuiApplication::screenRemoved, this, &QScreenConfig::screenRemoved);
45 3 }
46
47 QScreenConfig::~QScreenConfig()
48 {
49 for (auto& [key, out] : m_outputMap) {
50 delete out;
51 }
52 }
53
54 2 int QScreenConfig::outputId(const QScreen* qscreen)
55 {
56
1/2
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
2 for (auto& [key, output] : m_outputMap) {
57 if (qscreen == output->qscreen()) {
58 return output->id();
59 }
60 }
61 2 m_lastOutputId++;
62 2 return m_lastOutputId;
63 }
64
65 2 void QScreenConfig::screenAdded(const QScreen* qscreen)
66 {
67
2/2
✓ Branch 14 taken 2 times.
✓ Branch 15 taken 2 times.
4 qCDebug(DISMAN_QSCREEN) << "Screen added" << qscreen << qscreen->name();
68 2 QScreenOutput* qscreenoutput = new QScreenOutput(qscreen, this);
69 2 qscreenoutput->set_id(outputId(qscreen));
70 2 m_outputMap.insert({qscreenoutput->id(), qscreenoutput});
71
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!m_blockSignals) {
73 Q_EMIT config_changed();
74 }
75 2 }
76
77 void QScreenConfig::screenRemoved(QScreen* qscreen)
78 {
79 qCDebug(DISMAN_QSCREEN) << "Screen removed" << qscreen << QGuiApplication::screens().count();
80 // Find output matching the QScreen object and remove it
81 int removedOutputId = -1;
82 for (auto& [key, output] : m_outputMap) {
83 if (output->qscreen() == qscreen) {
84 removedOutputId = output->id();
85 m_outputMap.erase(removedOutputId);
86 delete output;
87 }
88 }
89 Q_EMIT config_changed();
90 }
91
92 20 void QScreenConfig::update_config(ConfigPtr& config) const
93 {
94 20 config->setScreen(m_screen->toDismanScreen());
95
96 // Removing removed outputs.
97
2/2
✓ Branch 9 taken 8 times.
✓ Branch 10 taken 20 times.
28 for (auto const& [key, output] : config->outputs()) {
98
1/2
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
8 if (m_outputMap.find(output->id()) == m_outputMap.end()) {
99 config->remove_output(output->id());
100 }
101 20 }
102
103 // Add Disman::Outputs that aren't in the list yet.
104 20 auto dismanOutputs = config->outputs();
105
106
2/2
✓ Branch 7 taken 16 times.
✓ Branch 8 taken 20 times.
36 for (auto& [key, output] : m_outputMap) {
107 16 Disman::OutputPtr dismanOutput;
108
109 16 auto it = dismanOutputs.find(output->id());
110
2/2
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 8 times.
16 if (it == dismanOutputs.end()) {
111 8 dismanOutput = output->toDismanOutput();
112 8 dismanOutputs.insert({dismanOutput->id(), dismanOutput});
113 } else {
114 8 dismanOutput = it->second;
115 }
116
117 16 output->updateDismanOutput(dismanOutput);
118
1/2
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
16 if (QGuiApplication::primaryScreen() == output->qscreen()) {
119 16 config->set_primary_output(dismanOutput);
120 }
121 16 }
122
123 20 config->set_outputs(dismanOutputs);
124 20 }
125
126 std::map<int, QScreenOutput*> QScreenConfig::outputMap() const
127 {
128 return m_outputMap;
129 }
130