2 * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3 * Copyright (c) 2018 TOYOTA MOTOR CORPORATION
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <QGuiApplication>
19 #include <QCommandLineParser>
20 #include <QtGui/QGuiApplication>
21 #include <QtQml/QQmlApplicationEngine>
22 #include <QtQml/QQmlContext>
23 #include <QtQml/qqml.h>
24 #include <QQuickWindow>
27 #include <qlibwindowmanager.h>
28 #include "applicationlauncher.h"
29 #include "applicationmodel.h"
31 #include "afm_user_daemon_proxy.h"
32 #include "homescreenhandler.h"
33 #include "hmi-debug.h"
35 // XXX: We want this DBus connection to be shared across the different
36 // QML objects, is there another way to do this, a nice way, perhaps?
37 org::AGL::afm::user *afm_user_daemon_proxy;
42 static inline void cleanup(org::AGL::afm::user *p) {
44 afm_user_daemon_proxy = Q_NULLPTR;
48 void noOutput(QtMsgType, const QMessageLogContext &, const QString &)
54 int main(int argc, char *argv[])
56 QString myname = QString("launcher");
57 QGuiApplication a(argc, argv);
60 QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
62 QDBusConnection::sessionBus(),
64 ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
66 QCoreApplication::setOrganizationDomain("LinuxFoundation");
67 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
68 QCoreApplication::setApplicationName(myname);
69 QCoreApplication::setApplicationVersion("0.1.0");
71 QCommandLineParser parser;
72 parser.addPositionalArgument("port", a.translate("main", "port for binding"));
73 parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
74 parser.addHelpOption();
75 parser.addVersionOption();
77 QStringList positionalArguments = parser.positionalArguments();
82 if (positionalArguments.length() == 2) {
83 port = positionalArguments.takeFirst().toInt();
84 token = positionalArguments.takeFirst();
87 HMI_DEBUG("launcher","port = %d, token = %s", port, token.toStdString().c_str());
89 // import C++ class to QML
90 qmlRegisterType<ApplicationModel>("AppModel", 1, 0, "ApplicationModel");
93 qDBusRegisterMetaType<AppInfo>();
94 qDBusRegisterMetaType<QList<AppInfo> >();
96 ApplicationLauncher *launcher = new ApplicationLauncher();
97 QLibWindowmanager* layoutHandler = new QLibWindowmanager();
98 if(layoutHandler->init(port,token) != 0){
102 AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
104 if (layoutHandler->requestSurface(myname) != 0) {
108 layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, myname](json_object *object) {
109 layoutHandler->endDraw(myname);
112 layoutHandler->set_event_handler(QLibWindowmanager::Event_Visible, [layoutHandler, launcher](json_object *object) {
113 QString label = QString(json_object_get_string( json_object_object_get(object, "drawing_name") ));
115 QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label == "HomeScreen" ? "Home" : label));
118 layoutHandler->set_event_handler(QLibWindowmanager::Event_Invisible, [layoutHandler, launcher](json_object *object) {
119 const char* label = json_object_get_string( json_object_object_get(object, "drawing_name") );
120 HMI_DEBUG("launch", "surface %s Event_Invisible", label);
123 HomescreenHandler* homescreenHandler = new HomescreenHandler();
124 homescreenHandler->init(port, token.toStdString().c_str(), layoutHandler, myname);
127 bindingAddress.setScheme(QStringLiteral("ws"));
128 bindingAddress.setHost(QStringLiteral("localhost"));
129 bindingAddress.setPort(port);
130 bindingAddress.setPath(QStringLiteral("/api"));
133 query.addQueryItem(QStringLiteral("token"), token);
134 bindingAddress.setQuery(query);
136 const QByteArray hack_delay = qgetenv("HMI_LAUNCHER_STARTUP_DELAY");
139 if (!hack_delay.isEmpty()) {
140 delay_time = (QString::fromLocal8Bit(hack_delay)).toInt();
143 QThread::sleep(delay_time);
144 qDebug("Sleep %d sec to resolve race condtion between HomeScreen and Launcher", delay_time);
147 QQmlApplicationEngine engine;
148 engine.rootContext()->setContextProperty(QStringLiteral("layoutHandler"), layoutHandler);
149 engine.rootContext()->setContextProperty(QStringLiteral("homescreenHandler"), homescreenHandler);
150 engine.rootContext()->setContextProperty(QStringLiteral("launcher"), launcher);
151 engine.rootContext()->setContextProperty(QStringLiteral("screenInfo"), &screenInfo);
152 engine.load(QUrl(QStringLiteral("qrc:/Launcher.qml")));
153 homescreenHandler->getRunnables();
155 QObject *root = engine.rootObjects().first();
156 QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
157 QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface()));