on ccd07fa help sign in
unity8/tests/mocks/WindowManager/MockScreens.cpp
213 lines 6.6 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(640,480)));
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 m_sizes.takeFirst()->deleteLater();
56 }
58 m_connectedWindow = w;
60 if (w) {
61 connect(w, &ScreenWindow::heightChanged, this, [this](int height) {
62 m_sizes.first()->size.rheight() = height;
63 Q_EMIT availableModesChanged();
65 });
66 connect(w, &ScreenWindow::widthChanged, this, [this](int width) {
67 m_sizes.first()->size.rwidth() = width;
68 Q_EMIT availableModesChanged();
69 });
71 m_sizes.push_front(new qtmir::ScreenMode(50, w->size()));
72 Q_EMIT availableModesChanged();
73 }
74 }
76 qtmir::OutputId outputId() const override { return m_id; }
77 bool used() const override { return m_used; }
78 QString name() const override { return m_name; }
79 float scale() const override { return m_scale; }
80 QSizeF physicalSize() const override { return m_physicalSize; }
81 qtmir::FormFactor formFactor() const override { return m_formFactor; }
82 qtmir::OutputTypes outputType() const override { return m_outputType; }
83 QString outputTypeName() const { return QStringLiteral("Internal"); }
84 MirPowerMode powerMode() const override { return m_powerMode; }
85 Qt::ScreenOrientation orientation() const override { return m_orientation; }
86 QPoint position() const override { return m_position; }
87 uint currentModeIndex() const override { return m_currentModeIndex; }
88 bool isActive() const override { return m_active; }
90 QQmlListProperty<qtmir::ScreenMode> availableModes() override {
91 return QQmlListProperty<qtmir::ScreenMode>(this, m_sizes);
92 }
94 void setActive(bool active) override {
95 if (m_active != active) {
96 m_active = active;
97 Q_EMIT activeChanged(m_active);
98 }
99 }
101 QScreen* qscreen() const override {
102 if (qGuiApp->topLevelWindows().count() > 0) {
103 return qGuiApp->topLevelWindows()[0]->screen();
104 }
105 return qGuiApp->primaryScreen();
106 }
108 qtmir::ScreenConfiguration *beginConfiguration() const override {
109 auto config = new qtmir::ScreenConfiguration;
110 config->valid = true;
111 config->id = m_id;
112 config->used = m_used;
113 config->topLeft = m_position;
114 config->currentModeIndex = m_currentModeIndex;
115 config->powerMode = m_powerMode;
116 config->scale = m_scale;
117 config->formFactor = m_formFactor;
118 return config;
119 }
121 bool applyConfiguration(qtmir::ScreenConfiguration *configuration) override {
122 m_used = configuration->used;
123 m_position = configuration->topLeft;
124 m_currentModeIndex = configuration->currentModeIndex;
125 m_powerMode = configuration->powerMode;
126 m_scale = configuration->scale;
127 m_formFactor = configuration->formFactor;
128 return true;
129 }
131Q_SIGNALS:
132 void outputTypeNameChanged();
134public:
135 qtmir::OutputId m_id{0};
136 bool m_active{false};
137 bool m_used{true};
138 QString m_name;
139 qtmir::OutputTypes m_outputType{qtmir::Unknown};
140 MirPowerMode m_powerMode{mir_power_mode_on};
141 Qt::ScreenOrientation m_orientation{Qt::PrimaryOrientation};
142 float m_scale{1.0};
143 qtmir::FormFactor m_formFactor{qtmir::FormFactorMonitor};
144 QPoint m_position;
145 uint m_currentModeIndex{0};
146 QList<qtmir::ScreenMode*> m_sizes;
147 QSizeF m_physicalSize;
149 QPointer<QWindow> m_connectedWindow;
150};
153MockScreens::MockScreens()
155 bool ok = false;
156 int screenCount = qEnvironmentVariableIntValue("UNITY_MOCK_SCREEN_COUNT", &ok);
157 if (!ok) screenCount = 1;
158 QPoint lastPoint(0,0);
159 for (int i = 0; i < screenCount; ++i) {
160 auto screen = new MockScreen();
161 screen->m_id = qtmir::OutputId{i};
162 screen->m_active = i == 0;
163 screen->m_name = QString("Monitor %1").arg(i);
164 screen->m_position = QPoint(lastPoint.x(), lastPoint.y());
165 screen->m_currentModeIndex = 3;
166 m_mocks.append(screen);
168 lastPoint.rx() += screen->m_sizes[screen->m_currentModeIndex]->size.width();
169 }
172MockScreens::~MockScreens()
174 qDeleteAll(m_mocks);
175 m_mocks.clear();
178QVector<qtmir::Screen *> MockScreens::screens() const
180 return m_mocks;
183qtmir::Screen *MockScreens::activeScreen() const
185 Q_FOREACH(auto screen, m_mocks) {
186 if (screen->isActive()) return screen;
187 }
188 return nullptr;
191QSharedPointer<MockScreens> MockScreens::instance()
193 if (!m_screens) {
194 QSharedPointer<MockScreens> screens(new MockScreens());
195 m_screens = screens.toWeakRef();
196 return screens;
197 } else {
198 return m_screens.lock();
199 }
202void MockScreens::connectWindow(ScreenWindow *w)
204 Screen* screen = w->screenWrapper();
205 if (!screen) return;
207 auto mockScreen = qobject_cast<MockScreen*>(screen->wrapped());
208 if (mockScreen) {
209 mockScreen->connectToWindow(w);
210 }
213#include "MockScreens.moc"