2 * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3 * Copyright (c) 2017 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>
29 #include <bluetooth.h>
30 #include "applicationlauncher.h"
31 #include "statusbarmodel.h"
32 #include "afm_user_daemon_proxy.h"
33 #include "homescreenhandler.h"
34 #include "toucharea.h"
35 #include "hmi-debug.h"
38 // XXX: We want this DBus connection to be shared across the different
39 // QML objects, is there another way to do this, a nice way, perhaps?
40 org::AGL::afm::user *afm_user_daemon_proxy;
45 static inline void cleanup(org::AGL::afm::user *p) {
47 afm_user_daemon_proxy = Q_NULLPTR;
51 void noOutput(QtMsgType, const QMessageLogContext &, const QString &)
57 int main(int argc, char *argv[])
59 QGuiApplication a(argc, argv);
62 QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
64 QDBusConnection::sessionBus(),
66 ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
68 QCoreApplication::setOrganizationDomain("LinuxFoundation");
69 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
70 QCoreApplication::setApplicationName("HomeScreen");
71 QCoreApplication::setApplicationVersion("0.7.0");
73 QCommandLineParser parser;
74 parser.addPositionalArgument("port", a.translate("main", "port for binding"));
75 parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
76 parser.addHelpOption();
77 parser.addVersionOption();
79 QStringList positionalArguments = parser.positionalArguments();
84 if (positionalArguments.length() == 2) {
85 port = positionalArguments.takeFirst().toInt();
86 token = positionalArguments.takeFirst();
89 HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
91 // import C++ class to QML
92 // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
93 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
95 ApplicationLauncher *launcher = new ApplicationLauncher();
96 QLibWindowmanager* layoutHandler = new QLibWindowmanager();
97 if(layoutHandler->init(port,token) != 0){
101 if (layoutHandler->requestSurface(QString("homescreen")) != 0) {
105 layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler](json_object *object) {
106 layoutHandler->endDraw(QString("homescreen"));
109 layoutHandler->set_event_handler(QLibWindowmanager::Event_ScreenUpdated, [layoutHandler, launcher](json_object *object) {
110 json_object *jarray = json_object_object_get(object, "ids");
111 int arrLen = json_object_array_length(jarray);
112 for( int idx = 0; idx < arrLen; idx++)
114 QString label = QString(json_object_get_string( json_object_array_get_idx(jarray, idx) ));
115 HMI_DEBUG("HomeScreen","Event_ScreenUpdated application: %s.", label.toStdString().c_str());
116 QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label));
120 HomescreenHandler* homescreenHandler = new HomescreenHandler();
121 homescreenHandler->init(port, token.toStdString().c_str());
124 bindingAddress.setScheme(QStringLiteral("ws"));
125 bindingAddress.setHost(QStringLiteral("localhost"));
126 bindingAddress.setPort(port);
127 bindingAddress.setPath(QStringLiteral("/api"));
130 query.addQueryItem(QStringLiteral("token"), token);
131 bindingAddress.setQuery(query);
133 TouchArea* touchArea = new TouchArea();
136 QQmlApplicationEngine engine;
137 engine.rootContext()->setContextProperty("layoutHandler", layoutHandler);
138 engine.rootContext()->setContextProperty("homescreenHandler", homescreenHandler);
139 engine.rootContext()->setContextProperty("touchArea", touchArea);
140 engine.rootContext()->setContextProperty("launcher", launcher);
141 engine.rootContext()->setContextProperty("weather", new Weather(bindingAddress));
142 engine.rootContext()->setContextProperty("bluetooth", new Bluetooth(bindingAddress));
143 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
145 QObject *root = engine.rootObjects().first();
146 QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
148 touchArea->setWindow(window);
149 QThread* thread = new QThread;
150 touchArea->moveToThread(thread);
151 QObject::connect(thread, &QThread::started, touchArea, &TouchArea::init);
155 QList<QObject *> sobjs = engine.rootObjects();
156 StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
157 statusBar->init(bindingAddress, engine.rootContext());
159 QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface()));