on 4cbd70f help sign in
unity8/tests/mocks/WindowManager/MockScreens.cpp
230 lines 7.2 kB history raw
1/*
2 * Copyright (C) 2016-2017 Canonical, Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it under
5 * the terms of the GNU Lesser General Public License version 3, as published by
6 * the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17#include "MockScreens.h"
18#include "ScreenWindow.h"
20// qtmirserver
21#include <qtmir/screen.h>
23#include <QGuiApplication>
24#include <QWindow>
25#include <QWeakPointer>
27namespace {
29QWeakPointer<MockScreens> m_screens;
31class MockScreen : public qtmir::Screen
33 Q_OBJECT
34 Q_PROPERTY(QString outputTypeName READ outputTypeName NOTIFY outputTypeNameChanged)
35public:
36 MockScreen()
37 {
38 m_sizes.append(new qtmir::ScreenMode(50, QSize(800,568)));
39 m_sizes.append(new qtmir::ScreenMode(60, QSize(1280,1024)));
40 m_sizes.append(new qtmir::ScreenMode(60, QSize(1440,900)));
41 m_sizes.append(new qtmir::ScreenMode(60, QSize(1920,1080)));
42 m_physicalSize = QSize(800,568);
43 }
44 ~MockScreen() {
45 qDeleteAll(m_sizes);
46 m_sizes.clear();
47 }
49 void connectToWindow(QWindow* w)
50 {
51 if (m_connectedWindow == w) return;
53 if (m_connectedWindow) {
54 disconnect(m_connectedWindow.data());
55 }
57 m_connectedWindow = w;
59 if (w) {
60 connect(w, &QWindow::heightChanged, this, [this](int height) {
61 if (height == 0 || height == m_sizes.first()->size.rheight()) {
62 return;
63 }
64 m_sizes.first()->size.rheight() = height;
65 Q_EMIT availableModesChanged();
67 });
68 connect(w, &QWindow::widthChanged, this, [this](int width) {
69 if (width == 0 || width == m_sizes.first()->size.rwidth()) {
70 return;
71 }
72 m_sizes.first()->size.rwidth() = width;
73 Q_EMIT availableModesChanged();
74 });
75 connect(w, &QWindow::activeChanged, this, [w, this]() {
76 if (w->isActive()) setActive(true);
77 });
78 if (w->isActive()) setActive(true);
80 Q_EMIT availableModesChanged();
82 }
83 }
85 miral::DisplayId displayId() const override { return m_id; }
86 bool used() const override { return m_used; }
87 QString name() const override { return m_name; }
88 float scale() const override { return m_scale; }
89 QSizeF physicalSize() const override { return m_physicalSize; }
90 qtmir::FormFactor formFactor() const override { return m_formFactor; }
91 qtmir::OutputTypes outputType() const override { return m_outputType; }
92 QString outputTypeName() const { return QStringLiteral("Internal"); }
93 MirPowerMode powerMode() const override { return m_powerMode; }
94 Qt::ScreenOrientation orientation() const override { return m_orientation; }
95 QPoint position() const override { return m_position; }
96 uint currentModeIndex() const override { return m_currentModeIndex; }
97 bool isActive() const override { return m_active; }
99 QQmlListProperty<qtmir::ScreenMode> availableModes() override {
100 return QQmlListProperty<qtmir::ScreenMode>(this, m_sizes);
101 }
103 void setActive(bool active) override {
104 if (m_active != active) {
105 m_active = active;
106 Q_EMIT activeChanged(m_active);
107 }
108 }
110 QScreen* qscreen() const override {
111 if (qGuiApp->topLevelWindows().count() > 0) {
112 return qGuiApp->topLevelWindows()[0]->screen();
113 }
114 return qGuiApp->primaryScreen();
115 }
117 qtmir::ScreenConfiguration *beginConfiguration() const override {
118 auto config = new qtmir::ScreenConfiguration;
119 config->valid = true;
120 config->id = m_id.output_id;
121 config->used = m_used;
122 config->topLeft = m_position;
123 config->currentModeIndex = m_currentModeIndex;
124 config->powerMode = m_powerMode;
125 config->scale = m_scale;
126 config->formFactor = m_formFactor;
127 return config;
128 }
130 bool applyConfiguration(qtmir::ScreenConfiguration *configuration) override {
131 m_used = configuration->used;
132 m_position = configuration->topLeft;
133 m_currentModeIndex = configuration->currentModeIndex;
134 m_powerMode = configuration->powerMode;
135 m_scale = configuration->scale;
136 m_formFactor = configuration->formFactor;
137 return true;
138 }
140Q_SIGNALS:
141 void outputTypeNameChanged();
143public:
144 miral::DisplayId m_id;
145 bool m_active{false};
146 bool m_used{true};
147 QString m_name;
148 qtmir::OutputTypes m_outputType{qtmir::Unknown};
149 MirPowerMode m_powerMode{mir_power_mode_on};
150 Qt::ScreenOrientation m_orientation{Qt::PrimaryOrientation};
151 float m_scale{1.0};
152 qtmir::FormFactor m_formFactor{qtmir::FormFactorMonitor};
153 QPoint m_position;
154 uint m_currentModeIndex{0};
155 QList<qtmir::ScreenMode*> m_sizes;
156 QSizeF m_physicalSize;
158 QPointer<QWindow> m_connectedWindow;
159};
162MockScreens::MockScreens()
164 bool ok = false;
165 int screenCount = qEnvironmentVariableIntValue("UNITY_MOCK_SCREEN_COUNT", &ok);
166 if (!ok) screenCount = 1;
167 QPoint lastPoint(0,0);
168 for (int i = 0; i < screenCount; ++i) {
169 auto screen = new MockScreen();
170 screen->m_id.output_id = miral::OutputId{i};
171 screen->m_active = i == 0;
172 screen->m_name = QString("Monitor %1").arg(i);
173 screen->m_position = QPoint(lastPoint.x(), lastPoint.y());
174 screen->m_currentModeIndex = 0;
175 m_mocks.append(screen);
177 lastPoint.rx() += screen->m_sizes[screen->m_currentModeIndex]->size.width();
179 connect(screen, &qtmir::Screen::activeChanged, this, [=](bool active) {
180 Q_FOREACH(auto otherScreen, m_mocks) {
181 if (active && otherScreen != screen) {
182 otherScreen->setActive(false);
183 }
184 }
185 });
186 }
189MockScreens::~MockScreens()
191 qDeleteAll(m_mocks);
192 m_mocks.clear();
195QVector<qtmir::Screen *> MockScreens::screens() const
197 return m_mocks;
200qtmir::Screen *MockScreens::activeScreen() const
202 Q_FOREACH(auto screen, m_mocks) {
203 if (screen->isActive()) return screen;
204 }
205 return nullptr;
208QSharedPointer<MockScreens> MockScreens::instance()
210 if (!m_screens) {
211 QSharedPointer<MockScreens> screens(new MockScreens());
212 m_screens = screens.toWeakRef();
213 return screens;
214 } else {
215 return m_screens.lock();
216 }
219void MockScreens::connectWindow(ScreenWindow *w)
221 ConcreteScreen* screen = w->screenWrapper();
222 if (!screen) return;
224 auto mockScreen = qobject_cast<MockScreen*>(screen->wrapped());
225 if (mockScreen) {
226 mockScreen->connectToWindow(w);
227 }
230#include "MockScreens.moc"