5a44cc76ff5fd5d9f6e706d17bfcca637224c193
[apps/launcher.git] / launcher / src / main.cpp
1 /*
2  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3  * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 #include <QGuiApplication>
19 #include <QCommandLineParser>
20 #include <QtCore/QUrlQuery>
21 #include <QtGui/QGuiApplication>
22 #include <QtQml/QQmlApplicationEngine>
23 #include <QtQml/QQmlContext>
24 #include <QtQml/qqml.h>
25 #include <QQuickWindow>
26 #include <QThread>
27
28 #include <qlibwindowmanager.h>
29 #include "applicationmodel.h"
30 #include "appinfo.h"
31 #include "homescreenhandler.h"
32 #include "hmi-debug.h"
33
34 int main(int argc, char *argv[])
35 {
36     QString myname = QString("launcher");
37     QGuiApplication a(argc, argv);
38
39     QCoreApplication::setOrganizationDomain("LinuxFoundation");
40     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
41     QCoreApplication::setApplicationName(myname);
42     QCoreApplication::setApplicationVersion("0.1.0");
43
44     QCommandLineParser parser;
45     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
46     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
47     parser.addHelpOption();
48     parser.addVersionOption();
49     parser.process(a);
50     QStringList positionalArguments = parser.positionalArguments();
51
52     int port = 1700;
53     QString token = "wm";
54
55     if (positionalArguments.length() == 2) {
56         port = positionalArguments.takeFirst().toInt();
57         token = positionalArguments.takeFirst();
58     }
59
60     HMI_DEBUG("launcher","port = %d, token = %s", port, token.toStdString().c_str());
61
62     // import C++ class to QML
63     qmlRegisterType<ApplicationModel>("AppModel", 1, 0, "ApplicationModel");
64
65     QLibWindowmanager* layoutHandler = new QLibWindowmanager();
66     if(layoutHandler->init(port,token) != 0){
67         exit(EXIT_FAILURE);
68     }
69
70     AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
71
72     if (layoutHandler->requestSurface(myname) != 0) {
73         exit(EXIT_FAILURE);
74     }
75
76     layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, myname](json_object *object) {
77         layoutHandler->endDraw(myname);
78     });
79
80     HomescreenHandler* homescreenHandler = new HomescreenHandler();
81     homescreenHandler->init(port, token.toStdString().c_str(), layoutHandler, myname);
82
83     QUrl bindingAddress;
84     bindingAddress.setScheme(QStringLiteral("ws"));
85     bindingAddress.setHost(QStringLiteral("localhost"));
86     bindingAddress.setPort(port);
87     bindingAddress.setPath(QStringLiteral("/api"));
88
89     QUrlQuery query;
90     query.addQueryItem(QStringLiteral("token"), token);
91     bindingAddress.setQuery(query);
92
93     const QByteArray hack_delay = qgetenv("HMI_LAUNCHER_STARTUP_DELAY");
94     int delay_time = 1;
95
96     if (!hack_delay.isEmpty()) {
97        delay_time = (QString::fromLocal8Bit(hack_delay)).toInt();
98     }
99
100     QThread::sleep(delay_time);
101     qDebug("Sleep %d sec to resolve race condtion between HomeScreen and Launcher", delay_time);
102
103     // mail.qml loading
104     QQmlApplicationEngine engine;
105     engine.rootContext()->setContextProperty(QStringLiteral("layoutHandler"), layoutHandler);
106     engine.rootContext()->setContextProperty(QStringLiteral("homescreenHandler"), homescreenHandler);
107     engine.rootContext()->setContextProperty(QStringLiteral("screenInfo"), &screenInfo);
108     engine.load(QUrl(QStringLiteral("qrc:/Launcher.qml")));
109     homescreenHandler->getRunnables();
110
111     QObject *root = engine.rootObjects().first();
112     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
113     QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface()));
114
115     return a.exec();
116 }