~ci-train-bot/unity8/unity8-ubuntu-zesty-2404

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
/*
 * Copyright (C) 2015 Canonical, Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "ShellApplication.h"

// Qt
#include <QLibrary>
#include <QProcess>
#include <QScreen>

#include <libintl.h>

// libandroid-properties
#include <hybris/properties/properties.h>

// local
#include <paths.h>
#include "CachingNetworkManagerFactory.h"
#include "UnityCommandLineParser.h"
#include "DebuggingController.h"

ShellApplication::ShellApplication(int & argc, char ** argv, bool isMirServer)
    : QGuiApplication(argc, argv)
{
    setApplicationName(QStringLiteral("unity8"));
    setOrganizationName(QStringLiteral("Canonical"));

    connect(this, &QGuiApplication::screenAdded, this, &ShellApplication::onScreenAdded);

    setupQmlEngine(isMirServer);

    UnityCommandLineParser parser(*this);

    if (!parser.deviceName().isEmpty()) {
        m_deviceName = parser.deviceName();
    } else {
        char buffer[200];
        property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
        m_deviceName = QString(buffer);
    }
    m_qmlArgs.setDeviceName(m_deviceName);

    m_qmlArgs.setMode(parser.mode());

    // The testability driver is only loaded by QApplication but not by QGuiApplication.
    // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
    if (parser.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
        QLibrary testLib(QStringLiteral("qttestability"));
        if (testLib.load()) {
            typedef void (*TasInitialize)(void);
            TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
            if (initFunction) {
                initFunction();
            } else {
                qCritical("Library qttestability resolve failed!");
            }
        } else {
            qCritical("Library qttestability load failed!");
        }
    }

    bindtextdomain("unity8", translationDirectory().toUtf8().data());
    textdomain("unity8");

    m_shellView = new ShellView(m_qmlEngine, &m_qmlArgs);

    if (parser.windowGeometry().isValid()) {
        m_shellView->setWidth(parser.windowGeometry().width());
        m_shellView->setHeight(parser.windowGeometry().height());
    }

    if (parser.hasFrameless()) {
        m_shellView->setFlags(Qt::FramelessWindowHint);
    }


    #ifdef UNITY8_ENABLE_TOUCH_EMULATION
    // You will need this if you want to interact with touch-only components using a mouse
    // Needed only when manually testing on a desktop.
    if (parser.hasMouseToTouch()) {
        m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
    }
    #endif

    new DebuggingController(this);

    // Some hard-coded policy for now.
    // NB: We don't support more than two screens at the moment
    //
    // TODO: Support an arbitrary number of screens and different policies
    //       (eg cloned desktop, several desktops, etc)
    if (isMirServer && screens().count() == 2) {
        m_shellView->setScreen(screens().at(1));
        m_qmlArgs.setDeviceName(QStringLiteral("desktop"));

        m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
        m_secondaryWindow->setScreen(screens().at(0));
        // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
        m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
        m_secondaryWindow->setVisible(true);
    }

    if (parser.mode().compare("greeter") == 0) {
        QSize primaryScreenSize = this->primaryScreen()->size();
        m_shellView->setHeight(primaryScreenSize.height());
        m_shellView->setWidth(primaryScreenSize.width());
        m_shellView->show();
        m_shellView->requestActivate();
        if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
            qDebug() << "Unable to send unity8-greeter-started event to Upstart";
        }
    } else if (isMirServer || parser.hasFullscreen()) {
        m_shellView->showFullScreen();
    } else {
        m_shellView->show();
    }
}

ShellApplication::~ShellApplication()
{
    destroyResources();
}

void ShellApplication::destroyResources()
{
    // Deletion order is important. Don't use QScopedPointers and the like
    // Otherwise the process will hang on shutdown (bug somewhere I guess).
    delete m_shellView;
    m_shellView = nullptr;

    delete m_secondaryWindow;
    m_secondaryWindow = nullptr;

    #ifdef UNITY8_ENABLE_TOUCH_EMULATION
    delete m_mouseTouchAdaptor;
    m_mouseTouchAdaptor = nullptr;
    #endif

    delete m_qmlEngine;
    m_qmlEngine = nullptr;
}

void ShellApplication::setupQmlEngine(bool isMirServer)
{
    m_qmlEngine = new QQmlEngine(this);

    m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));

    prependImportPaths(m_qmlEngine, ::overrideImportPaths());
    if (!isMirServer) {
        prependImportPaths(m_qmlEngine, ::nonMirImportPaths());
    }
    appendImportPaths(m_qmlEngine, ::fallbackImportPaths());

    m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);

    QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);
}

void ShellApplication::onScreenAdded(QScreen * /*screen*/)
{
    // TODO: Support an arbitrary number of screens and different policies
    //       (eg cloned desktop, several desktops, etc)
    if (screens().count() == 2) {
        m_shellView->setScreen(screens().at(1));
        m_qmlArgs.setDeviceName(QStringLiteral("desktop"));
        // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
        // its backing QPlatformWindow recreated). So lets refocus it.
        m_shellView->requestActivate();
        // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
        // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
        m_shellView->setVisible(true);

        m_secondaryWindow = new SecondaryWindow(m_qmlEngine);
        m_secondaryWindow->setScreen(screens().at(0));

        // QWindow::showFullScreen() also calls QWindow::requestActivate() and we don't want that!
        m_secondaryWindow->setWindowState(Qt::WindowFullScreen);
        m_secondaryWindow->setVisible(true);
    }
}

void ShellApplication::onScreenAboutToBeRemoved(QScreen *screen)
{
    // TODO: Support an arbitrary number of screens and different policies
    //       (eg cloned desktop, several desktops, etc)
    if (screen == m_shellView->screen()) {
        const QList<QScreen *> allScreens = screens();
        Q_ASSERT(allScreens.count() > 1);
        Q_ASSERT(allScreens.at(0) != screen);
        Q_ASSERT(m_secondaryWindow);
        delete m_secondaryWindow;
        m_secondaryWindow = nullptr;
        m_shellView->setScreen(allScreens.first());
        m_qmlArgs.setDeviceName(m_deviceName);
        // Changing the QScreen where a QWindow is drawn makes it also lose focus (besides having
        // its backing QPlatformWindow recreated). So lets refocus it.
        m_shellView->requestActivate();
        // QWindow::destroy() is called when it changes between screens. We have to manually make it visible again
        // <dandrader> This bug is supposedly fixed in Qt 5.5.1, although I can still reproduce it there. :-/
        m_shellView->setVisible(true);
    }
}