summaryrefslogtreecommitdiffstats
path: root/src/launchers/qml/main.cpp
blob: c6dac50bf97950d04216eee2c54a300fad25380c (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/****************************************************************************
**
** Copyright (C) 2017 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Pelagicore Application Manager.
**
** $QT_BEGIN_LICENSE:LGPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: LGPL-3.0
**
****************************************************************************/


#include <QQmlApplicationEngine>
#include <QQmlComponent>
#include <QQmlContext>
#include <QQmlIncubationController>
#include <QQmlDebuggingEnabler>

#include <QSocketNotifier>
#include <QFile>
#include <QFileInfo>
#include <QDir>
#include <QtEndian>
#include <QTimer>
#include <QRegularExpression>
#include <QCommandLineParser>

#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusArgument>
#include <QLoggingCategory>

#include <qplatformdefs.h>

#if !defined(AM_HEADLESS)
#  include <QGuiApplication>
#  include <QQuickItem>
#  include <QQuickView>
#  include <QQuickWindow>

#  include <QtAppManLauncher/private/applicationmanagerwindow_p.h>
#else
#  include <QCoreApplication>
#endif

#include "qmlapplicationinterface.h"
#include "qmlapplicationinterfaceextension.h"
#include "qmlnotification.h"
#include "notification.h"
#include "qtyaml.h"
#include "global.h"
#include "utilities.h"
#include "yamlapplicationscanner.h"
#include "application.h"
#include "startupinterface.h"
#include "dbus-utilities.h"
#include "startuptimer.h"

#if defined(Q_OS_LINUX)
#  include <sys/prctl.h>
#  include <sys/syscall.h>
#endif

QT_BEGIN_NAMESPACE_AM

/*! \internal
    Sadly, Linux has no setproctitle() call like the BSDs have...
    You could just overwrite argv[0], but writing beyond the original length will overwrite the
    environment. The workaround is to reset argv via the prctl() in the kernel, but we need to pass
    a lot of things that aren't really changing, but are needed for this specific prctl call.
*/
bool setProcessTitle(const QByteArray &title)
{
// We need at least kernel 3.18 to support PR_SET_MM_MAP (as of 2017,
// the Qt CI still has a RHEL 6 node, which is running kernel 2.6.32)
#if defined(Q_OS_LINUX) && defined(PR_SET_MM_MAP)
    QFile f(qSL("/proc/self/stat"));

    if (!f.open(QFile::ReadOnly))
        return false;
    QByteArray line = f.readLine(4096);
    if (line.isEmpty())
        return false;

    // retrieve the following fields:
    //  [25] start_code
    //  [26] end_code
    //  [27] start_stack
    //  [44] start_data
    //  [45] end_data
    //  [46] start_brk
    //  [49] env_start
    //  [50] env_end

    QList<QByteArray> fields = line.split(' ');
    if (fields.size() < 49)
        return false;

    char *arg_start = strdup(title.constData());
    char *arg_end = arg_start + title.size() + 1;

    long int brk_start = syscall(__NR_brk, 0);

    prctl_mm_map map;
    map.start_code  = fields.at(25).toULongLong();
    map.end_code    = fields.at(26).toULongLong();
    map.start_stack = fields.at(27).toULongLong();
    map.start_data  = fields.at(44).toULongLong();
    map.end_data    = fields.at(45).toULongLong();
    map.start_brk   = fields.at(46).toULongLong();
    map.brk         = brk_start;
    map.arg_start   = (uintptr_t) arg_start;
    map.arg_end     = (uintptr_t) arg_end;
    map.env_start   = fields.at(49).toULongLong();
    map.env_end     = fields.at(50).toULongLong();
    map.auxv        = nullptr;
    map.auxv_size   = 0;
    map.exe_fd      = (uint) -1;

    int result = prctl(PR_SET_MM, PR_SET_MM_MAP, &map, sizeof(map), 0);
    if (result)
        qCWarning(LogSystem) << "Cannot set Linux process title:" << strerror(errno);
    return result;
#else
    return false;
#endif
}

// maybe make this configurable for specific workloads?
class HeadlessIncubationController : public QObject, public QQmlIncubationController // clazy:exclude=missing-qobject-macro
{
public:
    HeadlessIncubationController(QObject *parent)
        : QObject(parent)
    {
        startTimer(50);
    }

protected:
    void timerEvent(QTimerEvent *) override
    {
        incubateFor(25);
    }
};



// copied straight from Qt 5.1.0 qmlscene/main.cpp for now - needs to be revised
static void loadDummyDataFiles(QQmlEngine &engine, const QString& directory)
{
    QDir dir(directory + qSL("/dummydata"), qSL("*.qml"));
    QStringList list = dir.entryList();
    for (int i = 0; i < list.size(); ++i) {
        QString qml = list.at(i);
        QQmlComponent comp(&engine, dir.filePath(qml));
        QObject *dummyData = comp.create();

        if (comp.isError()) {
            QList<QQmlError> errors = comp.errors();
            foreach (const QQmlError &error, errors)
                qWarning() << error;
        }

        if (dummyData) {
            qml.truncate(qml.length()-4);
            engine.rootContext()->setContextProperty(qml, dummyData);
            dummyData->setParent(&engine);
        }
    }
}

class Controller : public QObject
{
    Q_OBJECT

public:
    Controller(QCoreApplication *a, const QString &directLoad = QString());

public slots:
    void startApplication(const QString &baseDir, const QString &qmlFile, const QString &document,
                          const QString &mimeType, const QVariantMap &application, const QVariantMap systemProperties);

private:
    QQmlApplicationEngine m_engine;
    QmlApplicationInterface *m_applicationInterface = nullptr;
    QVariantMap m_configuration;
    bool m_launched = false;
#if !defined(AM_HEADLESS)
    QQuickWindow *m_window = nullptr;
#endif
#if !defined(QT_NO_QML_DEBUGGER)
    QQmlDebuggingEnabler *debuggingEnabler = nullptr;
#endif
};

static QString p2pBusName = qSL("am");
static QString notificationBusName = qSL("am_notification_bus");
static StartupTimer startupTimer;
static QCommandLineParser cp;
static QCommandLineOption qmlDebugOption(qSL("qml-debug"), qSL("Enables QML debugging and profiling."));
static QCommandLineOption quickLaunchOption(qSL("quicklaunch"), qSL("Starts the launcher in the quicklaunching mode."));
static QCommandLineOption directLoadOption(qSL("directload") , qSL("The info.yaml to start."), qSL("info.yaml"));


QT_END_NAMESPACE_AM

QT_USE_NAMESPACE_AM


int main(int argc, char *argv[])
{
    if (qEnvironmentVariableIsSet("AM_NO_DLT_LOGGING"))
        dltLoggingEnabled = false;

    // The common-lib is already registering the DLT Application for the application manager.
    // As the appID needs to be unique within the system, we cannot use the same appID and
    // need to change it as early as possible.

    // As we don't know the app-id yet, we are registering a place holder so we are able to see
    // something in the dlt logs if general errors occur.
    changeDLTApplication("PCLQ", "Pelagicore Application-Manager Launcher QML");

    colorLogApplicationId = "qml-launcher";
    installMessageHandlers();
    QLoggingCategory::setFilterRules(QString::fromUtf8(qgetenv("AM_LOGGING_RULES")));

#if defined(AM_HEADLESS)
    QCoreApplication a(argc, argv);
#else
#  if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
    // this is needed for WebEngine
    QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
#  endif

    QGuiApplication a(argc, argv);
    a.setApplicationDisplayName(qL1S("appman-launcher-qml"));
    cp.addHelpOption();
    cp.addOption(directLoadOption);
    cp.addOption(qmlDebugOption);
    cp.addOption(quickLaunchOption);

    cp.process(a);

    qmlRegisterType<ApplicationManagerWindow>("QtApplicationManager", 1, 0, "ApplicationManagerWindow");
#endif

    qmlRegisterType<QmlNotification>("QtApplicationManager", 1, 0, "Notification");
    qmlRegisterType<QmlApplicationInterfaceExtension>("QtApplicationManager", 1, 0, "ApplicationInterfaceExtension");

    startupTimer.checkpoint("after logging and qml register initialization");

    if (cp.isSet(directLoadOption)) {
        QFileInfo fi = cp.value(directLoadOption);

        if (!fi.exists() || fi.fileName() != "info.yaml") {
            qCCritical(LogQmlRuntime) << "ERROR: --directload needs a valid info.yaml file as parameter";
            return 2;
        }

        new Controller(&a, fi.absoluteFilePath());
    } else {
        QByteArray dbusAddress = qgetenv("AM_DBUS_PEER_ADDRESS");
        if (dbusAddress.isEmpty()) {
            qCCritical(LogQmlRuntime) << "ERROR: $AM_DBUS_PEER_ADDRESS is empty";
            return 2;
        }
        QDBusConnection dbusConnection = QDBusConnection::connectToPeer(QString::fromUtf8(dbusAddress), p2pBusName);

        if (!dbusConnection.isConnected()) {
            qCCritical(LogQmlRuntime) << "ERROR: could not connect to the P2P D-Bus via:" << dbusAddress;
            return 3;
        }
        qCDebug(LogQmlRuntime) << "Connected to the P2P D-Bus via:" << dbusAddress;

        dbusAddress = qgetenv("AM_DBUS_NOTIFICATION_BUS_ADDRESS");
        if (dbusAddress.isEmpty())
            dbusAddress = "session";

        if (dbusAddress == "system")
            dbusConnection = QDBusConnection::connectToBus(QDBusConnection::SystemBus, notificationBusName);
        else if (dbusAddress == "session")
            dbusConnection = QDBusConnection::connectToBus(QDBusConnection::SessionBus, notificationBusName);
        else
            dbusConnection = QDBusConnection::connectToBus(QString::fromUtf8(dbusAddress), notificationBusName);

        if (!dbusConnection.isConnected()) {
            qCCritical(LogQmlRuntime) << "ERROR: could not connect to the Notification D-Bus via:" << dbusAddress;
            return 3;
        }
        qCDebug(LogQmlRuntime) << "Connected to the Notification D-Bus via:" << dbusAddress;

        new Controller(&a);
    }

    startupTimer.checkpoint("after dbus initialization");
    return a.exec();
}

Controller::Controller(QCoreApplication *a, const QString &directLoad)
    : QObject(a)
{
    connect(&m_engine, &QObject::destroyed, &QCoreApplication::quit);
    connect(&m_engine, &QQmlEngine::quit, &QCoreApplication::quit);

    if (cp.isSet(qmlDebugOption)) {
#if !defined(QT_NO_QML_DEBUGGER)
        debuggingEnabler = new QQmlDebuggingEnabler(true);
#else
        qCWarning(LogQmlRuntime) << "The --qml-debug option is ignored, because Qt was built without support for QML Debugging!";
#endif
    }

    auto docs = QtYaml::variantDocumentsFromYaml(qgetenv("AM_RUNTIME_CONFIGURATION"));
    if (docs.size() == 1)
        m_configuration = docs.first().toMap();

    setCrashActionConfiguration(m_configuration.value(qSL("crashAction")).toMap());

    const QString baseDir = QString::fromLocal8Bit(qgetenv("AM_BASE_DIR") + "/");

    QStringList importPaths = variantToStringList(m_configuration.value(qSL("importPaths")));
    for (QString &path : importPaths) {
        if (QFileInfo(path).isRelative()) {
            path.prepend(baseDir);
        } else {
            qCWarning(LogQmlRuntime) << "Absolute import path in config file can lead to problems inside containers:"
                                     << path;
        }
        m_engine.addImportPath(path);
    }

    startupTimer.checkpoint("after application config initialization");

    // This is a bit of a hack to make ApplicationManagerWindow known as a sub-class
    // of QWindow. Without this, assigning an ApplicationManagerWindow to a QWindow*
    // property will fail with [unknown property type]. First seen when trying to
    // write a nested Wayland-compositor where assigning to WaylandOutput.window failed.
    {
        static const char registerWindowQml[] = "import QtQuick 2.0\nimport QtQuick.Window 2.2\nQtObject { }\n";
        QQmlComponent registerWindowComp(&m_engine);
        registerWindowComp.setData(QByteArray::fromRawData(registerWindowQml, sizeof(registerWindowQml) - 1), QUrl());
        QScopedPointer<QObject> dummy(registerWindowComp.create());
        registerWindowComp.completeCreate();
    }

    QString quicklaunchQml = m_configuration.value((qSL("quicklaunchQml"))).toString();
    if (!quicklaunchQml.isEmpty() && cp.isSet(quickLaunchOption)) {
        if (QFileInfo(quicklaunchQml).isRelative())
            quicklaunchQml.prepend(baseDir);

        QQmlComponent quicklaunchComp(&m_engine, quicklaunchQml);
        if (!quicklaunchComp.isError()) {
            QScopedPointer<QObject> quicklaunchInstance(quicklaunchComp.create());
            quicklaunchComp.completeCreate();
        } else {
            const QList<QQmlError> errors = quicklaunchComp.errors();
            for (const QQmlError &error : errors)
                qCCritical(LogQmlRuntime) << error;
        }
    }

    startupTimer.checkpoint("after quick launch qml initialization");

    if (directLoad.isEmpty()) {
        m_applicationInterface = new QmlApplicationInterface(p2pBusName, notificationBusName, this);
        connect(m_applicationInterface, &QmlApplicationInterface::startApplication,
                this, &Controller::startApplication);
        if (!m_applicationInterface->initialize()) {
            qCritical("ERROR: could not connect to the application manager's interface on the peer D-Bus");
            qApp->exit(4);
        }
    } else {
        QTimer::singleShot(0, [this, directLoad]() {
            QFileInfo fi(directLoad);
            YamlApplicationScanner yas;
            try {
                const Application *a = yas.scan(directLoad);
                startApplication(fi.absolutePath(), a->codeFilePath(), QString(), QString(), a->toVariantMap(), QVariantMap());
            } catch (const Exception &e) {
                qCritical("ERROR: could not parse info.yaml file: %s", e.what());
                qApp->exit(5);
            }
        });
    }

    startupTimer.checkpoint("after application interface initialization");
}

void Controller::startApplication(const QString &baseDir, const QString &qmlFile, const QString &document,
                                  const QString &mimeType, const QVariantMap &application,
                                  const QVariantMap systemProperties)
{
    if (m_launched)
        return;
    m_launched = true;

    QString applicationId = application.value("id").toString();
    QVariantMap runtimeParameters = qdbus_cast<QVariantMap>(application.value("runtimeParameters"));

    startupTimer.checkpoint("starting application");

    //Change the DLT Application description, to easily identify the application on the DLT logs.
    char dltAppId[5];
    qsnprintf(dltAppId, 5, "A%03d", application.value("uniqueNumber").toInt());
    changeDLTApplication(dltAppId, QString("Application-Manager App: %1").arg(applicationId).toLocal8Bit());
    registerUnregisteredDLTContexts();

    // Dress up the ps output to make it easier to correlate all the launcher processes
    QString newTitle = qApp->arguments().at(0) + qSL(" [") + applicationId + qSL("]");
    if (qApp->arguments().size() > 1)
        newTitle.append(qApp->arguments().mid(1).join(qL1C(' ')));
    setProcessTitle(newTitle.toLocal8Bit());

    qCDebug(LogQmlRuntime) << "loading" << applicationId << "- main:" << qmlFile << "- document:" << document
                           << "- mimeType:" << mimeType << "- parameters:" << runtimeParameters
                           << "- baseDir:" << baseDir;

    if (!QDir::setCurrent(baseDir)) {
        qCCritical(LogQmlRuntime) << "could not set the current directory to" << baseDir;
        QCoreApplication::exit(2);
        return;
    }

    if (!applicationId.isEmpty()) {
        // shorten application id to make the debug output more readable

        auto sl = applicationId.split(qL1C('.'));
        applicationId.clear();
        for (int i = 0; i < sl.size() - 1; ++i) {
            applicationId.append(sl.at(i).at(0));
            applicationId.append(qL1C('.'));
        }
        applicationId.append(sl.last());
        colorLogApplicationId = applicationId.toLocal8Bit();
    } else {
        qCCritical(LogQmlRuntime) << "did not receive an application id";
        QCoreApplication::exit(2);
        return;
    }

    if (!QFile::exists(qmlFile)) {
        qCCritical(LogQmlRuntime) << "could not load" << qmlFile << ": file does not exist";
        QCoreApplication::exit(2);
        return;
    }

    QStringList startupPluginFiles = variantToStringList(m_configuration.value(qSL("plugins")).toMap().value(qSL("startup")));
    auto startupPlugins = loadPlugins<StartupInterface>("startup", startupPluginFiles);
    foreach (StartupInterface *iface, startupPlugins)
        iface->initialize(m_applicationInterface->systemProperties());

    bool loadDummyData = runtimeParameters.value(qSL("loadDummyData")).toBool()
            || m_configuration.value(qSL("loadDummydata")).toBool();

    if (loadDummyData) {
        qCDebug(LogQmlRuntime) << "loading dummy-data";
        loadDummyDataFiles(m_engine, QFileInfo(qmlFile).path());
    }

    QVariant imports = runtimeParameters.value(qSL("importPaths"));
    const QVariantList vl = (imports.type() == QVariant::String) ? QVariantList{imports}
                                                                 : qdbus_cast<QVariantList>(imports);
    for (const QVariant &v : vl) {
        QString path = v.toString();
        if (QFileInfo(path).isRelative())
            path = QDir().absoluteFilePath(path);
        else
            qCWarning(LogQmlRuntime) << "Omitting absolute import path in info file for safety reasons:" << path;
        m_engine.addImportPath(path);
    }
    qCDebug(LogQmlRuntime) << "Qml import paths:" << m_engine.importPathList();

    if (m_applicationInterface) {
        m_engine.rootContext()->setContextProperty(qSL("ApplicationInterface"), m_applicationInterface);

        QVariantMap &svm = m_applicationInterface->m_systemProperties;
        svm = qdbus_cast<QVariantMap>(systemProperties);
        for (auto it = svm.begin(); it != svm.end(); ++it)
            it.value() = convertFromDBusVariant(it.value());

        QVariantMap &avm = m_applicationInterface->m_applicationProperties;
        avm = qdbus_cast<QVariantMap>(application.value(qSL("applicationProperties")));
        for (auto it = avm.begin(); it != avm.end(); ++it)
            it.value() = convertFromDBusVariant(it.value());
    }

    foreach (StartupInterface *iface, startupPlugins)
        iface->beforeQmlEngineLoad(&m_engine);

    startupTimer.checkpoint("after loading plugins and import paths");

    QUrl qmlFileUrl = QUrl::fromLocalFile(qmlFile);
    m_engine.rootContext()->setContextProperty("StartupTimer", &startupTimer);
    m_engine.load(qmlFileUrl);

    startupTimer.checkpoint("after engine loading main qml file");

    auto topLevels = m_engine.rootObjects();

    if (Q_UNLIKELY(topLevels.isEmpty() || !topLevels.at(0))) {
        qCCritical(LogSystem) << "could not load" << qmlFile << ": no root object";
        QCoreApplication::exit(3);
        return;
    }

    foreach (StartupInterface *iface, startupPlugins)
        iface->afterQmlEngineLoad(&m_engine);

    QObject *topLevel = topLevels.at(0);

#if !defined(AM_HEADLESS)
    m_window = qobject_cast<QQuickWindow *>(topLevel);
    if (!m_window) {
        QQuickItem *contentItem = qobject_cast<QQuickItem *>(topLevel);
        if (contentItem) {
            QQuickView* view = new QQuickView(&m_engine, 0);
            m_window = view;
            view->setContent(qmlFileUrl, 0, topLevel);
        } else {
            qCCritical(LogSystem) << "could not load" << qmlFile << ": root object is not a QQuickItem.";
            QCoreApplication::exit(4);
            return;
        }
    } else {
        if (!m_engine.incubationController())
            m_engine.setIncubationController(m_window->incubationController());
    }

    startupTimer.checkpoint("after creating and setting application window");

    Q_ASSERT(m_window);
    QObject::connect(&m_engine, &QQmlEngine::quit, m_window, &QObject::deleteLater); // not sure if this is needed .. or even the best thing to do ... see connects above, they seem to work better

    if (m_configuration.contains(qSL("backgroundColor"))) {
        QSurfaceFormat surfaceFormat = m_window->format();
        surfaceFormat.setAlphaBufferSize(8);
        m_window->setFormat(surfaceFormat);
        m_window->setClearBeforeRendering(true);
        m_window->setColor(QColor(m_configuration.value(qSL("backgroundColor")).toString()));
    }

    foreach (StartupInterface *iface, startupPlugins)
        iface->beforeWindowShow(m_window);

    m_window->show();

    startupTimer.checkpoint("after showing application window");

    foreach (StartupInterface *iface, startupPlugins)
        iface->afterWindowShow(m_window);

#else
    m_engine.setIncubationController(new HeadlessIncubationController(&m_engine));
#endif
    qCDebug(LogQmlRuntime) << "component loading and creating complete.";

    startupTimer.checkpoint("component loading and creating complete.");
    startupTimer.createReport(application.value("id").toString());

    if (!document.isEmpty() && m_applicationInterface)
        emit m_applicationInterface->openDocument(document, mimeType);
}

#include "main.moc"