on 06f42e7 help sign in
unity8/tests/mocks/WindowManager/MockScreens.cpp
227 lines 7.1 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
34public:
35 MockScreen()
36 {
37 m_sizes.append(new qtmir::ScreenMode(50, QSize(640,480)));
38 m_sizes.append(new qtmir::ScreenMode(60, QSize(1280,1024)));
39 m_sizes.append(new qtmir::ScreenMode(60, QSize(1440,900)));
40 m_sizes.append(new qtmir::ScreenMode(60, QSize(1920,1080)));
41 m_physicalSize = QSize(800,568);
42 }
43 ~MockScreen() {
44 qDeleteAll(m_sizes);
45 m_sizes.clear();
46 }
48 void connectToWindow(QWindow* w)
49 {
50 if (m_connectedWindow == w) return;
52 if (m_connectedWindow) {
53 disconnect(m_connectedWindow.data());
54 m_sizes.takeFirst()->deleteLater();
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 m_sizes.push_front(new qtmir::ScreenMode(50, w->size()));
81 Q_EMIT availableModesChanged();
83 }
84 }
86 miral::DisplayId displayId() const override { return m_id; }
87 bool used() const override { return m_used; }
88 QString name() const override { return m_name; }
89 float scale() const override { return m_scale; }
90 QSizeF physicalSize() const override { return m_physicalSize; }
91 qtmir::FormFactor formFactor() const override { return m_formFactor; }
92 qtmir::OutputTypes outputType() const override { return m_outputType; }
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 }
140public:
141 miral::DisplayId m_id;
142 bool m_active{false};
143 bool m_used{true};
144 QString m_name;
145 qtmir::OutputTypes m_outputType{qtmir::Unknown};
146 MirPowerMode m_powerMode{mir_power_mode_on};
147 Qt::ScreenOrientation m_orientation{Qt::PrimaryOrientation};
148 float m_scale{1.0};
149 qtmir::FormFactor m_formFactor{qtmir::FormFactorMonitor};
150 QPoint m_position;
151 uint m_currentModeIndex{0};
152 QList<qtmir::ScreenMode*> m_sizes;
153 QSizeF m_physicalSize;
155 QPointer<QWindow> m_connectedWindow;
156};
159MockScreens::MockScreens()
161 bool ok = false;
162 int screenCount = qEnvironmentVariableIntValue("UNITY_MOCK_SCREEN_COUNT", &ok);
163 if (!ok) screenCount = 1;
164 QPoint lastPoint(0,0);
165 for (int i = 0; i < screenCount; ++i) {
166 auto screen = new MockScreen();
167 screen->m_id.output_id = miral::OutputId{i};
168 screen->m_active = i == 0;
169 screen->m_name = QString("Monitor %1").arg(i);
170 screen->m_position = QPoint(lastPoint.x(), lastPoint.y());
171 screen->m_currentModeIndex = 3;
172 m_mocks.append(screen);
174 lastPoint.rx() += screen->m_sizes[screen->m_currentModeIndex]->size.width();
176 connect(screen, &qtmir::Screen::activeChanged, this, [=](bool active) {
177 Q_FOREACH(auto otherScreen, m_mocks) {
178 if (active && otherScreen != screen) {
179 otherScreen->setActive(false);
180 }
181 }
182 });
183 }
186MockScreens::~MockScreens()
188 qDeleteAll(m_mocks);
189 m_mocks.clear();
192QVector<qtmir::Screen *> MockScreens::screens() const
194 return m_mocks;
197qtmir::Screen *MockScreens::activeScreen() const
199 Q_FOREACH(auto screen, m_mocks) {
200 if (screen->isActive()) return screen;
201 }
202 return nullptr;
205QSharedPointer<MockScreens> MockScreens::instance()
207 if (!m_screens) {
208 QSharedPointer<MockScreens> screens(new MockScreens());
209 m_screens = screens.toWeakRef();
210 return screens;
211 } else {
212 return m_screens.lock();
213 }
216void MockScreens::connectWindow(ScreenWindow *w)
218 ConcreteScreen* screen = w->screenWrapper();
219 if (!screen) return;
221 auto mockScreen = qobject_cast<MockScreen*>(screen->wrapped());
222 if (mockScreen) {
223 mockScreen->connectToWindow(w);
224 }
227#include "MockScreens.moc"