on e4499be help sign in
unity8/tests/mocks/WindowManager/MockScreens.cpp
217 lines 6.7 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, &ScreenWindow::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, &ScreenWindow::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 });
76 Q_EMIT availableModesChanged();
77 }
78 }
80 qtmir::OutputId outputId() const override { return m_id; }
81 bool used() const override { return m_used; }
82 QString name() const override { return m_name; }
83 float scale() const override { return m_scale; }
84 QSizeF physicalSize() const override { return m_physicalSize; }
85 qtmir::FormFactor formFactor() const override { return m_formFactor; }
86 qtmir::OutputTypes outputType() const override { return m_outputType; }
87 QString outputTypeName() const { return QStringLiteral("Internal"); }
88 MirPowerMode powerMode() const override { return m_powerMode; }
89 Qt::ScreenOrientation orientation() const override { return m_orientation; }
90 QPoint position() const override { return m_position; }
91 uint currentModeIndex() const override { return m_currentModeIndex; }
92 bool isActive() const override { return m_active; }
94 QQmlListProperty<qtmir::ScreenMode> availableModes() override {
95 return QQmlListProperty<qtmir::ScreenMode>(this, m_sizes);
96 }
98 void setActive(bool active) override {
99 if (m_active != active) {
100 m_active = active;
101 Q_EMIT activeChanged(m_active);
102 }
103 }
105 QScreen* qscreen() const override {
106 if (qGuiApp->topLevelWindows().count() > 0) {
107 return qGuiApp->topLevelWindows()[0]->screen();
108 }
109 return qGuiApp->primaryScreen();
110 }
112 qtmir::ScreenConfiguration *beginConfiguration() const override {
113 auto config = new qtmir::ScreenConfiguration;
114 config->valid = true;
115 config->id = m_id;
116 config->used = m_used;
117 config->topLeft = m_position;
118 config->currentModeIndex = m_currentModeIndex;
119 config->powerMode = m_powerMode;
120 config->scale = m_scale;
121 config->formFactor = m_formFactor;
122 return config;
123 }
125 bool applyConfiguration(qtmir::ScreenConfiguration *configuration) override {
126 m_used = configuration->used;
127 m_position = configuration->topLeft;
128 m_currentModeIndex = configuration->currentModeIndex;
129 m_powerMode = configuration->powerMode;
130 m_scale = configuration->scale;
131 m_formFactor = configuration->formFactor;
132 return true;
133 }
135Q_SIGNALS:
136 void outputTypeNameChanged();
138public:
139 qtmir::OutputId m_id{0};
140 bool m_active{false};
141 bool m_used{true};
142 QString m_name;
143 qtmir::OutputTypes m_outputType{qtmir::Unknown};
144 MirPowerMode m_powerMode{mir_power_mode_on};
145 Qt::ScreenOrientation m_orientation{Qt::PrimaryOrientation};
146 float m_scale{1.0};
147 qtmir::FormFactor m_formFactor{qtmir::FormFactorMonitor};
148 QPoint m_position;
149 uint m_currentModeIndex{0};
150 QList<qtmir::ScreenMode*> m_sizes;
151 QSizeF m_physicalSize;
153 QPointer<QWindow> m_connectedWindow;
154};
157MockScreens::MockScreens()
159 bool ok = false;
160 int screenCount = qEnvironmentVariableIntValue("UNITY_MOCK_SCREEN_COUNT", &ok);
161 if (!ok) screenCount = 1;
162 QPoint lastPoint(0,0);
163 for (int i = 0; i < screenCount; ++i) {
164 auto screen = new MockScreen();
165 screen->m_id = qtmir::OutputId{i};
166 screen->m_active = i == 0;
167 screen->m_name = QString("Monitor %1").arg(i);
168 screen->m_position = QPoint(lastPoint.x(), lastPoint.y());
169 screen->m_currentModeIndex = 0;
170 m_mocks.append(screen);
172 lastPoint.rx() += screen->m_sizes[screen->m_currentModeIndex]->size.width();
173 }
176MockScreens::~MockScreens()
178 qDeleteAll(m_mocks);
179 m_mocks.clear();
182QVector<qtmir::Screen *> MockScreens::screens() const
184 return m_mocks;
187qtmir::Screen *MockScreens::activeScreen() const
189 Q_FOREACH(auto screen, m_mocks) {
190 if (screen->isActive()) return screen;
191 }
192 return nullptr;
195QSharedPointer<MockScreens> MockScreens::instance()
197 if (!m_screens) {
198 QSharedPointer<MockScreens> screens(new MockScreens());
199 m_screens = screens.toWeakRef();
200 return screens;
201 } else {
202 return m_screens.lock();
203 }
206void MockScreens::connectWindow(ScreenWindow *w)
208 Screen* screen = w->screenWrapper();
209 if (!screen) return;
211 auto mockScreen = qobject_cast<MockScreen*>(screen->wrapped());
212 if (mockScreen) {
213 mockScreen->connectToWindow(w);
214 }
217#include "MockScreens.moc"