2 * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3 * Copyright (c) 2017, 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 <QtCore/QUrlQuery>
21 #include <QtGui/QGuiApplication>
22 #include <QtQml/QQmlApplicationEngine>
23 #include <QtQml/QQmlContext>
24 #include <QtQml/qqml.h>
25 #include <QQuickWindow>
27 #include <qlibwindowmanager.h>
29 #include <bluetooth.h>
30 #include "applicationlauncher.h"
31 #include "statusbarmodel.h"
32 #include "mastervolume.h"
33 #include "homescreenhandler.h"
34 #include "hmi-debug.h"
35 #include "chromecontroller.h"
37 int main(int argc, char *argv[])
39 QGuiApplication a(argc, argv);
41 QCoreApplication::setOrganizationDomain("LinuxFoundation");
42 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
43 QCoreApplication::setApplicationName("HomeScreen");
44 QCoreApplication::setApplicationVersion("0.7.0");
46 QCommandLineParser parser;
47 parser.addPositionalArgument("port", a.translate("main", "port for binding"));
48 parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
49 parser.addHelpOption();
50 parser.addVersionOption();
52 QStringList positionalArguments = parser.positionalArguments();
56 QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
58 if (positionalArguments.length() == 2) {
59 port = positionalArguments.takeFirst().toInt();
60 token = positionalArguments.takeFirst();
63 HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
65 // import C++ class to QML
66 // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
67 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
68 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
69 qmlRegisterUncreatableType<ChromeController>("SpeechChrome", 1, 0, "SpeechChromeController",
70 QLatin1String("SpeechChromeController is uncreatable."));
72 ApplicationLauncher *launcher = new ApplicationLauncher();
73 QLibWindowmanager* layoutHandler = new QLibWindowmanager();
74 if(layoutHandler->init(port,token) != 0){
78 AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
80 if (layoutHandler->requestSurface(graphic_role) != 0) {
84 layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, &graphic_role](json_object *object) {
85 layoutHandler->endDraw(graphic_role);
88 layoutHandler->set_event_handler(QLibWindowmanager::Event_ScreenUpdated, [layoutHandler, launcher](json_object *object) {
89 json_object *jarray = json_object_object_get(object, "ids");
90 int arrLen = json_object_array_length(jarray);
91 for( int idx = 0; idx < arrLen; idx++)
93 QString label = QString(json_object_get_string( json_object_array_get_idx(jarray, idx) ));
94 HMI_DEBUG("HomeScreen","Event_ScreenUpdated application: %s.", label.toStdString().c_str());
95 QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label));
99 HomescreenHandler* homescreenHandler = new HomescreenHandler();
100 homescreenHandler->init(port, token.toStdString().c_str());
103 bindingAddress.setScheme(QStringLiteral("ws"));
104 bindingAddress.setHost(QStringLiteral("localhost"));
105 bindingAddress.setPort(port);
106 bindingAddress.setPath(QStringLiteral("/api"));
109 query.addQueryItem(QStringLiteral("token"), token);
110 bindingAddress.setQuery(query);
113 QQmlApplicationEngine engine;
114 engine.rootContext()->setContextProperty("bindingAddress", bindingAddress);
115 engine.rootContext()->setContextProperty("layoutHandler", layoutHandler);
116 engine.rootContext()->setContextProperty("homescreenHandler", homescreenHandler);
117 engine.rootContext()->setContextProperty("launcher", launcher);
118 engine.rootContext()->setContextProperty("weather", new Weather(bindingAddress));
119 engine.rootContext()->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
120 engine.rootContext()->setContextProperty("speechChromeController", new ChromeController(bindingAddress, &engine));
121 engine.rootContext()->setContextProperty("screenInfo", &screenInfo);
122 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
124 QObject *root = engine.rootObjects().first();
125 QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
126 QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface()));
128 QList<QObject *> sobjs = engine.rootObjects();
129 StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
130 statusBar->init(bindingAddress, engine.rootContext());