aboutsummaryrefslogtreecommitdiffstats
path: root/src/neptune3-ui/main.cpp
blob: ff76f49d4ce16d8a96d34e4981a7e1356f49156e (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
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Neptune 3 UI.
**
** $QT_BEGIN_LICENSE:GPL-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 General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) 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.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-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/

#include <QtAppManCommon/global.h>
#include <QtAppManCommon/logging.h>
#include <QtAppManCommonVersion>
#include <QtIviCore/QtIviCoreVersion>
#include <QtAppManMain/main.h>
#include <QtAppManMain/defaultconfiguration.h>
#include <QtAppManPackage/packageutilities.h>
#include <QtAppManManager/sudo.h>
#include <QtAppManWindow/touchemulation.h>
#include <QtGui/QFontDatabase>
#include <QGuiApplication>
#include <QTranslator>
#include <QLibraryInfo>
#include <QFileInfo>
#include <QDir>
#include <QProcess>
#include <QTouchDevice>
#include <QQmlApplicationEngine>
#include <QQmlContext>

#include <QDebug>

#ifdef Q_OS_ANDROID
# include "urlinterceptor.h"
#endif

QT_USE_NAMESPACE_AM

// code to transform a macro into a string literal
#define QUOTE(name) #name
#define STR(macro) QUOTE(macro)

void startExtraProcess(const QString &name) {
#if QT_CONFIG(process)
    QProcess *serverProcess = new QProcess(qApp);
    serverProcess->setProcessEnvironment(QProcessEnvironment::systemEnvironment());
    QObject::connect(serverProcess, &QProcess::stateChanged, [name, serverProcess] (QProcess::ProcessState state) {
        if (state == QProcess::Running) {
            qCInfo(LogSystem) << "Attempted automatic start of " << name << ", pid:" << serverProcess->processId();
        }
    });
    QObject::connect(qApp, &QCoreApplication::aboutToQuit, [serverProcess] () {
        serverProcess->terminate();
    });
    serverProcess->start(name, QProcess::ReadOnly);
#endif
}

Q_DECL_EXPORT int main(int argc, char *argv[])
{
#ifdef Q_OS_ANDROID
    qputenv("QML_DISABLE_DISK_CACHE", "1");
#endif

    QCoreApplication::setApplicationName(qSL("Neptune UI"));
    QCoreApplication::setOrganizationName(qSL("Luxoft Sweden AB"));
    QCoreApplication::setOrganizationDomain(qSL("luxoft.com"));
    QCoreApplication::setApplicationVersion(STR(NEPTUNE_VERSION));

    Logging::initialize();

    PackageUtilities::ensureCorrectLocale();

    try {
        Sudo::forkServer(Sudo::DropPrivilegesPermanently);

        qputenv("QTIVIMEDIA_SIMULATOR_DATABASE", QFile::encodeName(QDir::homePath() + "/media.db"));
        qputenv("QT_IM_MODULE", "qtvirtualkeyboard");

#if defined(Q_OS_ANDROID) || defined(Q_OS_IOS)
        Q_UNUSED(argc)
        char arg1[] = "--recreate-database";
        char *_argv[] = { argv[0], arg1, nullptr };
        int _argc = 2;
        Main a(_argc, &_argv[0]);
#else
        Main a(argc, argv);

        // start the server; the server itself will ensure one instance only
        startExtraProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/ivivehiclefunctions-simulation-server");
        startExtraProcess(QLibraryInfo::location(QLibraryInfo::BinariesPath) + "/ivimedia-simulation-server");
        startExtraProcess(QCoreApplication::applicationDirPath() + "/drivedata-simulation-server");
        startExtraProcess(QCoreApplication::applicationDirPath() + "/remotesettings-server");
#endif
        QFontDatabase::addApplicationFont(QCoreApplication::applicationDirPath()
                                          + "/imports_shared/assets/fonts/DejaVuSans.ttf");

        // load the Qt translations
        QTranslator qtTranslator;
        if (qtTranslator.load(QLocale(), qSL("qt_"), QString(), QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
            a.installTranslator(&qtTranslator);
        }

#ifdef Q_OS_ANDROID
        DefaultConfiguration cfg(QStringList({ qSL("assets:/am-config-neptune.yaml"), qSL("assets:/am-config-android.yaml") }), QString());
#else
        DefaultConfiguration cfg(QStringList(QCoreApplication::applicationDirPath() + qSL("/am-config-neptune.yaml")), QString());
#endif

        cfg.parse();
        a.setup(&cfg);

#ifdef USE_QT_SAFE_RENDERER
        //Set env variables for Qt Safe Renderer for sending heartbeats
        //env variables are used to start TCP client to connect to "safe ui" part
        //qsrEnabled also switches loading of Safe Telltales in Cluster View
        bool    qsrEnabled = cfg.rawSystemProperties()["public"].toMap()["qsrEnabled"].toBool();
        if (qsrEnabled)
        {
            QString qsrIp   = cfg.rawSystemProperties()["public"].toMap()["qsrServerAddress"].toString();
            QString qsrPort = cfg.rawSystemProperties()["public"].toMap()["qsrServerPort"].toString();

            if (!qsrPort.isEmpty() && !qsrIp.isEmpty()) {
                qputenv("QT_SAFERENDER_IPADDRESS", qsrIp.toLocal8Bit());
                qputenv("QT_SAFERENDER_PORT", qsrPort.toLocal8Bit());
            }
        }
#endif

#ifdef Q_OS_ANDROID
        a.qmlEngine()->setUrlInterceptor(new UrlInterceptor);
#endif

        a.loadQml(cfg.loadDummyData());
        a.showWindow(cfg.fullscreen() && !cfg.noFullscreen());

        auto ctx = a.qmlEngine()->rootContext();
        ctx->setContextProperty("neptuneInfo", STR(NEPTUNE_INFO));
        ctx->setContextProperty("qtamVersion", QTAPPMANCOMMON_VERSION_STR);
        ctx->setContextProperty("qtiviVersion", QTIVICORE_VERSION_STR);

        return MainBase::exec();
    } catch (const std::exception &e) {
        qCCritical(LogSystem) << "ERROR:" << e.what();
        return 2;
    }
}