~alan-griffiths/miral/fix-1645284

« back to all changes in this revision

Viewing changes to miral-qt/src/modules/Unity/Screens/qquickscreenwindow.cpp

  • Committer: Alan Griffiths
  • Date: 2016-11-07 17:59:19 UTC
  • mfrom: (436.1.1 miral2)
  • Revision ID: alan@octopull.co.uk-20161107175919-stbb64i7j1htgog2
[miral-qt] delete all as qtmir work on MirAL has shifted to lp:~unity-team/qtmir/miral-qt-integration and this is a needless distration

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2016 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
 
 */
16
 
 
17
 
#include "qquickscreenwindow.h"
18
 
 
19
 
// mirserver
20
 
#include "screen.h"
21
 
#include "screenscontroller.h"
22
 
 
23
 
// Qt
24
 
#include <QGuiApplication>
25
 
#include <QScreen>
26
 
#include <qpa/qplatformnativeinterface.h>
27
 
#include <QDebug>
28
 
 
29
 
using namespace qtmir;
30
 
 
31
 
/*
32
 
 * QQuickScreenWindow - wrapper of QQuickWindow to enable QML to specify destination screen
33
 
 * and read Mir-specific properties of that screen like scale & form factor
34
 
 **/
35
 
 
36
 
/*
37
 
 * Small implementation detail: during construction, the backing qtmir::ScreenWindow/QPlatformScreen
38
 
 * has not been created yet, so handle() returns nullptr there.
39
 
 *
40
 
 * The "scale" & "form factor" properties are obtainable only via the QPlatformNativeInterface,
41
 
 * which refers to windows by their backing handles. So need to postpone querying these values
42
 
 * until after QQuickScreenWindow construction. Handiest approach is to only fetch the value
43
 
 * on first read of the property.
44
 
 *
45
 
 * There's no problem connecting to QPlatformNativeInterface::windowPropertyChanged in the
46
 
 * constructor however.
47
 
 */
48
 
QQuickScreenWindow::QQuickScreenWindow(QQuickWindow *parent)
49
 
    : QQuickWindow(parent)
50
 
    , m_scale(-1.0) // start with invalid initial state, fetch correct value on first invokation
51
 
    , m_formFactor(FormFactorUnknown)
52
 
{
53
 
    if (qGuiApp->platformName() == QLatin1String("mirserver")) {
54
 
        connect(qGuiApp->platformNativeInterface(), &QPlatformNativeInterface::windowPropertyChanged,
55
 
                this, &QQuickScreenWindow::nativePropertyChanged);
56
 
        //m_scale = getScaleNativeProperty(); DO NOT CALL HERE - see note above
57
 
        //m_formFactor = getFormFactorNativeProperty(); ditto
58
 
    } else {
59
 
        qCritical("Not using 'mirserver' QPA plugin, the Unity.Screens plugin will be useless!");
60
 
    }
61
 
}
62
 
 
63
 
QScreen *QQuickScreenWindow::screen() const
64
 
{
65
 
    return QQuickWindow::screen();
66
 
}
67
 
 
68
 
void QQuickScreenWindow::setScreen(QScreen *screen)
69
 
{
70
 
    QQuickWindow::setScreen(screen);
71
 
 
72
 
    float scale = getScaleNativeProperty();
73
 
    if (!qFuzzyCompare(m_scale, scale)) {
74
 
        m_scale = scale;
75
 
        Q_EMIT scaleChanged(m_scale);
76
 
    }
77
 
 
78
 
    auto formFactor = getFormFactorNativeProperty();
79
 
    if (formFactor != m_formFactor) {
80
 
        m_formFactor = formFactor;
81
 
        Q_EMIT formFactorChanged(m_formFactor);
82
 
    }
83
 
}
84
 
 
85
 
qreal QQuickScreenWindow::scale()
86
 
{
87
 
    if (m_scale < 0) {
88
 
        m_scale = getScaleNativeProperty();
89
 
    }
90
 
     // am keeping local copy, to avoid emitting changed signal if screen changes but scale doesn't.
91
 
    return m_scale;
92
 
}
93
 
 
94
 
bool QQuickScreenWindow::setScaleAndFormFactor(const float scale, const FormFactor formFactor)
95
 
{
96
 
    if (qFuzzyCompare(scale, m_scale) && formFactor == m_formFactor) {
97
 
        return true;
98
 
    }
99
 
 
100
 
    // Operates through the mirserver ScreensController API
101
 
    auto controller = static_cast<ScreensController*>(qGuiApp->platformNativeInterface()
102
 
                                                      ->nativeResourceForIntegration("ScreensController"));
103
 
    if (!controller) {
104
 
        return false;
105
 
    }
106
 
 
107
 
    auto screenHandle = static_cast<Screen *>(screen()->handle());
108
 
    if (!screenHandle) {
109
 
        return false;
110
 
    }
111
 
 
112
 
    auto id = screenHandle->outputId();
113
 
 
114
 
    auto configs = controller->configuration();
115
 
 
116
 
    auto config = configs.begin();
117
 
    while (config != configs.end()) {
118
 
        if (config->id == id) {
119
 
            config->scale = scale;
120
 
            config->formFactor = static_cast<MirFormFactor>(formFactor);
121
 
        }
122
 
        config++;
123
 
    }
124
 
 
125
 
    return controller->setConfiguration(configs);
126
 
}
127
 
 
128
 
FormFactor QQuickScreenWindow::formFactor()
129
 
{
130
 
    if (m_formFactor == FormFactorUnknown) {
131
 
        m_formFactor = getFormFactorNativeProperty();
132
 
    }
133
 
    return m_formFactor;
134
 
}
135
 
 
136
 
void QQuickScreenWindow::nativePropertyChanged(QPlatformWindow *window, const QString &propertyName)
137
 
{
138
 
    if (window != handle()) {
139
 
        return;
140
 
    }
141
 
 
142
 
    if (propertyName == QStringLiteral("scale")) {
143
 
        float scale = getScaleNativeProperty();
144
 
 
145
 
        if (qFuzzyCompare(m_scale, scale)) {
146
 
            return;
147
 
        }
148
 
        m_scale = scale;
149
 
        Q_EMIT scaleChanged(m_scale);
150
 
    } else if (propertyName == QStringLiteral("formFactor")) {
151
 
        auto formFactor = getFormFactorNativeProperty();
152
 
 
153
 
        if (formFactor == m_formFactor) {
154
 
            return;
155
 
        }
156
 
        m_formFactor = formFactor;
157
 
        Q_EMIT formFactorChanged(m_formFactor);
158
 
    }
159
 
}
160
 
 
161
 
float QQuickScreenWindow::getScaleNativeProperty() const
162
 
{
163
 
    QVariant scaleVal = qGuiApp->platformNativeInterface()
164
 
                            ->windowProperty(handle(), QStringLiteral("scale"));
165
 
    if (!scaleVal.isValid()) {
166
 
        return m_scale;
167
 
    }
168
 
    bool ok;
169
 
    float scale = scaleVal.toFloat(&ok);
170
 
    if (!ok || scale <= 0) {
171
 
        return m_scale;
172
 
    }
173
 
    return scale;
174
 
}
175
 
 
176
 
FormFactor QQuickScreenWindow::getFormFactorNativeProperty() const
177
 
{
178
 
    QVariant formFactorVal = qGuiApp->platformNativeInterface()
179
 
                                ->windowProperty(handle(), QStringLiteral("formFactor"));
180
 
    if (!formFactorVal.isValid()) {
181
 
        return m_formFactor;
182
 
    }
183
 
 
184
 
    return static_cast<FormFactor>(formFactorVal.toInt());
185
 
}