File: MockScreens.cpp

package info (click to toggle)
lomiri 0.5.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 24,088 kB
  • sloc: cpp: 39,498; python: 2,559; javascript: 1,426; ansic: 1,012; sh: 289; xml: 252; makefile: 69
file content (234 lines) | stat: -rw-r--r-- 7,259 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Copyright (C) 2016-2017 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License version 3, as published by
 * the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
 * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "MockScreens.h"
#include "ScreenWindow.h"

// qtmirserver
#include <qtmir/screen.h>

#include <QGuiApplication>
#include <QWindow>
#include <QWeakPointer>

namespace {

#ifndef WITH_MIR2
namespace miroil = miral;
#endif

QWeakPointer<MockScreens> m_screens;

class MockScreen : public qtmir::Screen
{
    Q_OBJECT
    Q_PROPERTY(QString outputTypeName READ outputTypeName NOTIFY outputTypeNameChanged)
public:
    MockScreen()
    {
        m_sizes.append(new qtmir::ScreenMode(50, QSize(800,568)));
        m_sizes.append(new qtmir::ScreenMode(60, QSize(1280,1024)));
        m_sizes.append(new qtmir::ScreenMode(60, QSize(1440,900)));
        m_sizes.append(new qtmir::ScreenMode(60, QSize(1920,1080)));
        m_physicalSize = QSize(800,568);
    }
    ~MockScreen() {
        qDeleteAll(m_sizes);
        m_sizes.clear();
    }

    void connectToWindow(QWindow* w)
    {
        if (m_connectedWindow == w) return;

        if (m_connectedWindow) {
            disconnect(m_connectedWindow.data());
        }

        m_connectedWindow = w;

        if (w) {
            connect(w, &QWindow::heightChanged, this, [this](int height) {
                if (height == 0 || height == m_sizes.first()->size.rheight()) {
                    return;
                }
                m_sizes.first()->size.rheight() = height;
                Q_EMIT availableModesChanged();

            });
            connect(w, &QWindow::widthChanged, this, [this](int width) {
                if (width == 0 || width == m_sizes.first()->size.rwidth()) {
                    return;
                }
                m_sizes.first()->size.rwidth() = width;
                Q_EMIT availableModesChanged();
            });
            connect(w, &QWindow::activeChanged, this, [w, this]() {
                if (w->isActive()) setActive(true);
            });
            if (w->isActive()) setActive(true);

            Q_EMIT availableModesChanged();

        }
    }

    miroil::DisplayId displayId() const override { return m_id; }
    bool used() const override { return m_used; }
    QString name() const override { return m_name; }
    float scale() const override { return m_scale; }
    QSizeF physicalSize() const override { return m_physicalSize; }
    qtmir::FormFactor formFactor() const override { return m_formFactor; }
    qtmir::OutputTypes outputType() const override { return m_outputType; }
    QString outputTypeName() const { return QStringLiteral("Internal"); }
    MirPowerMode powerMode() const override { return m_powerMode; }
    Qt::ScreenOrientation orientation() const override { return m_orientation; }
    QPoint position() const override { return m_position; }
    uint currentModeIndex() const override { return m_currentModeIndex; }
    bool isActive() const override { return m_active; }

    QQmlListProperty<qtmir::ScreenMode> availableModes() override {
        return QQmlListProperty<qtmir::ScreenMode>(this, m_sizes);
    }

    void setActive(bool active) override {
        if (m_active != active) {
            m_active = active;
            Q_EMIT activeChanged(m_active);
        }
    }

    QScreen* qscreen() const override {
        if (qGuiApp->topLevelWindows().count() > 0) {
            return qGuiApp->topLevelWindows()[0]->screen();
        }
        return qGuiApp->primaryScreen();
    }

    qtmir::ScreenConfiguration *beginConfiguration() const override {
        auto config = new qtmir::ScreenConfiguration;
        config->valid = true;
        config->id = m_id.output_id;
        config->used = m_used;
        config->topLeft = m_position;
        config->currentModeIndex = m_currentModeIndex;
        config->powerMode = m_powerMode;
        config->scale = m_scale;
        config->formFactor = m_formFactor;
        return config;
    }

    bool applyConfiguration(qtmir::ScreenConfiguration *configuration) override {
        m_used = configuration->used;
        m_position = configuration->topLeft;
        m_currentModeIndex = configuration->currentModeIndex;
        m_powerMode = configuration->powerMode;
        m_scale = configuration->scale;
        m_formFactor = configuration->formFactor;
        return true;
    }

Q_SIGNALS:
    void outputTypeNameChanged();

public:
    miroil::DisplayId m_id;
    bool m_active{false};
    bool m_used{true};
    QString m_name;
    qtmir::OutputTypes m_outputType{qtmir::Unknown};
    MirPowerMode m_powerMode{mir_power_mode_on};
    Qt::ScreenOrientation m_orientation{Qt::PrimaryOrientation};
    float m_scale{1.0};
    qtmir::FormFactor m_formFactor{qtmir::FormFactorMonitor};
    QPoint m_position;
    uint m_currentModeIndex{0};
    QList<qtmir::ScreenMode*> m_sizes;
    QSizeF m_physicalSize;

    QPointer<QWindow> m_connectedWindow;
};
}

MockScreens::MockScreens()
{
    bool ok = false;
    int screenCount = qEnvironmentVariableIntValue("LOMIRI_MOCK_SCREEN_COUNT", &ok);
    if (!ok) screenCount = 1;
    QPoint lastPoint(0,0);
    for (int i = 0; i < screenCount; ++i) {
        auto screen = new MockScreen();
        screen->m_id.output_id = miroil::OutputId{i};
        screen->m_active = i == 0;
        screen->m_name = QString("Monitor %1").arg(i);
        screen->m_position = QPoint(lastPoint.x(), lastPoint.y());
        screen->m_currentModeIndex = 0;
        m_mocks.append(screen);

        lastPoint.rx() += screen->m_sizes[screen->m_currentModeIndex]->size.width();

        connect(screen, &qtmir::Screen::activeChanged, this, [=](bool active) {
            Q_FOREACH(auto otherScreen, m_mocks) {
                if (active && otherScreen != screen) {
                    otherScreen->setActive(false);
                }
            }
        });
    }
}

MockScreens::~MockScreens()
{
    qDeleteAll(m_mocks);
    m_mocks.clear();
}

QVector<qtmir::Screen *> MockScreens::screens() const
{
    return m_mocks;
}

qtmir::Screen *MockScreens::activeScreen() const
{
    Q_FOREACH(auto screen, m_mocks) {
        if (screen->isActive()) return screen;
    }
    return nullptr;
}

QSharedPointer<MockScreens> MockScreens::instance()
{
    if (!m_screens) {
        QSharedPointer<MockScreens> screens(new MockScreens());
        m_screens = screens.toWeakRef();
        return screens;
    } else {
        return m_screens.lock();
    }
}

void MockScreens::connectWindow(ScreenWindow *w)
{
    ConcreteScreen* screen = w->screenWrapper();
    if (!screen) return;

    auto mockScreen = qobject_cast<MockScreen*>(screen->wrapped());
    if (mockScreen) {
        mockScreen->connectToWindow(w);
    }
}

#include "MockScreens.moc"