on 4236ded help sign in
unity8/tests/mocks/WindowManager/MockScreens.cpp
209 lines 6.5 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, &ScreenWindow::heightChanged, this, [this](int height) {
61 m_sizes.first()->size.rheight() = height;
62 Q_EMIT availableModesChanged();
64 });
65 connect(w, &ScreenWindow::widthChanged, this, [this](int width) {
66 m_sizes.first()->size.rwidth() = width;
67 Q_EMIT availableModesChanged();
68 });
70 m_sizes.push_front(new qtmir::ScreenMode(50, w->size()));
71 Q_EMIT availableModesChanged();
72 }
73 }
75 qtmir::OutputId outputId() const override { return m_id; }
76 bool used() const override { return m_used; }
77 QString name() const override { return m_name; }
78 float scale() const override { return m_scale; }
79 QSizeF physicalSize() const override { return m_physicalSize; }
80 qtmir::FormFactor formFactor() const override { return m_formFactor; }
81 qtmir::OutputTypes outputType() const override { return m_outputType; }
82 MirPowerMode powerMode() const override { return m_powerMode; }
83 Qt::ScreenOrientation orientation() const override { return m_orientation; }
84 QPoint position() const override { return m_position; }
85 uint currentModeIndex() const override { return m_currentModeIndex; }
86 bool isActive() const override { return m_active; }
88 QQmlListProperty<qtmir::ScreenMode> availableModes() override {
89 return QQmlListProperty<qtmir::ScreenMode>(this, m_sizes);
90 }
92 void setActive(bool active) override {
93 if (m_active != active) {
94 m_active = active;
95 Q_EMIT activeChanged(m_active);
96 }
97 }
99 QScreen* qscreen() const override {
100 qDebug() << "GET SCREEN" << qGuiApp->topLevelWindows();
101 if (qGuiApp->topLevelWindows().count() > 0) {
102 return qGuiApp->topLevelWindows()[0]->screen();
103 }
104 return qGuiApp->primaryScreen();
105 }
107 qtmir::ScreenConfiguration *beginConfiguration() const override {
108 auto config = new qtmir::ScreenConfiguration;
109 config->valid = true;
110 config->id = m_id;
111 config->used = m_used;
112 config->topLeft = m_position;
113 config->currentModeIndex = m_currentModeIndex;
114 config->powerMode = m_powerMode;
115 config->scale = m_scale;
116 config->formFactor = m_formFactor;
117 return config;
118 }
120 bool applyConfiguration(qtmir::ScreenConfiguration *configuration) override {
121 m_used = configuration->used;
122 m_position = configuration->topLeft;
123 m_currentModeIndex = configuration->currentModeIndex;
124 m_powerMode = configuration->powerMode;
125 m_scale = configuration->scale;
126 m_formFactor = configuration->formFactor;
127 return true;
128 }
130public:
131 qtmir::OutputId m_id{0};
132 bool m_active{false};
133 bool m_used{true};
134 QString m_name;
135 qtmir::OutputTypes m_outputType{qtmir::Unknown};
136 MirPowerMode m_powerMode{mir_power_mode_on};
137 Qt::ScreenOrientation m_orientation{Qt::PrimaryOrientation};
138 float m_scale{1.0};
139 qtmir::FormFactor m_formFactor{qtmir::FormFactorMonitor};
140 QPoint m_position;
141 uint m_currentModeIndex{0};
142 QList<qtmir::ScreenMode*> m_sizes;
143 QSizeF m_physicalSize;
145 QPointer<QWindow> m_connectedWindow;
146};
149MockScreens::MockScreens()
151 bool ok = false;
152 int screenCount = qEnvironmentVariableIntValue("UNITY_MOCK_SCREEN_COUNT", &ok);
153 if (!ok) screenCount = 1;
154 QPoint lastPoint(0,0);
155 for (int i = 0; i < screenCount; ++i) {
156 auto screen = new MockScreen();
157 screen->m_id = qtmir::OutputId{i};
158 screen->m_active = i == 0;
159 screen->m_name = QString("Monitor %1").arg(i);
160 screen->m_position = QPoint(lastPoint.x(), lastPoint.y());
161 screen->m_currentModeIndex = 3;
162 m_mocks.append(screen);
164 lastPoint.rx() += screen->m_sizes[screen->m_currentModeIndex]->size.width();
165 }
168MockScreens::~MockScreens()
170 qDeleteAll(m_mocks);
171 m_mocks.clear();
174QVector<qtmir::Screen *> MockScreens::screens() const
176 return m_mocks;
179qtmir::Screen *MockScreens::activeScreen() const
181 Q_FOREACH(auto screen, m_mocks) {
182 if (screen->isActive()) return screen;
183 }
184 return nullptr;
187QSharedPointer<MockScreens> MockScreens::instance()
189 if (!m_screens) {
190 QSharedPointer<MockScreens> screens(new MockScreens());
191 m_screens = screens.toWeakRef();
192 return screens;
193 } else {
194 return m_screens.lock();
195 }
198void MockScreens::connectWindow(ScreenWindow *w)
200 ConcreteScreen* screen = w->screenWrapper();
201 if (!screen) return;
203 auto mockScreen = qobject_cast<MockScreen*>(screen->wrapped());
204 if (mockScreen) {
205 mockScreen->connectToWindow(w);
206 }
209#include "MockScreens.moc"