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 <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 "mastervolume.h"
34 #include "homescreenhandler.h"
35 #include "toucharea.h"
36 #include "shortcutappmodel.h"
37 #include "hmi-debug.h"
39 // XXX: We want this DBus connection to be shared across the different
40 // QML objects, is there another way to do this, a nice way, perhaps?
41 org::AGL::afm::user *afm_user_daemon_proxy;
46 static inline void cleanup(org::AGL::afm::user *p) {
48 afm_user_daemon_proxy = Q_NULLPTR;
52 void noOutput(QtMsgType, const QMessageLogContext &, const QString &)
58 int main(int argc, char *argv[])
60 QGuiApplication a(argc, argv);
63 QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
65 QDBusConnection::sessionBus(),
67 ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
69 QCoreApplication::setOrganizationDomain("LinuxFoundation");
70 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
71 QCoreApplication::setApplicationName("HomeScreen");
72 QCoreApplication::setApplicationVersion("0.7.0");
74 QCommandLineParser parser;
75 parser.addPositionalArgument("port", a.translate("main", "port for binding"));
76 parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
77 parser.addHelpOption();
78 parser.addVersionOption();
80 QStringList positionalArguments = parser.positionalArguments();
84 QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
86 if (positionalArguments.length() == 2) {
87 port = positionalArguments.takeFirst().toInt();
88 token = positionalArguments.takeFirst();
91 HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
93 // import C++ class to QML
94 // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
95 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
96 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
97 qmlRegisterType<ShortcutAppModel>("ShortcutAppModel", 1, 0, "ShortcutAppModel");
99 ApplicationLauncher *launcher = new ApplicationLauncher();
100 QLibWindowmanager* layoutHandler = new QLibWindowmanager();
101 HomescreenHandler* homescreenHandler = new HomescreenHandler();
102 ShortcutAppModel* shortcutAppModel = new ShortcutAppModel();
103 if(layoutHandler->init(port,token) != 0){
107 AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
109 if (layoutHandler->requestSurface(graphic_role) != 0) {
114 bindingAddress.setScheme(QStringLiteral("ws"));
115 bindingAddress.setHost(QStringLiteral("localhost"));
116 bindingAddress.setPort(port);
117 bindingAddress.setPath(QStringLiteral("/api"));
120 query.addQueryItem(QStringLiteral("token"), token);
121 bindingAddress.setQuery(query);
123 TouchArea* touchArea = new TouchArea();
124 homescreenHandler->init(port, token.toStdString().c_str(), layoutHandler, graphic_role);
127 QQmlApplicationEngine engine;
128 engine.rootContext()->setContextProperty("bindingAddress", bindingAddress);
129 engine.rootContext()->setContextProperty("layoutHandler", layoutHandler);
130 engine.rootContext()->setContextProperty("homescreenHandler", homescreenHandler);
131 engine.rootContext()->setContextProperty("touchArea", touchArea);
132 engine.rootContext()->setContextProperty("shortcutAppModel", shortcutAppModel);
133 engine.rootContext()->setContextProperty("launcher", launcher);
134 engine.rootContext()->setContextProperty("weather", new Weather(bindingAddress));
135 engine.rootContext()->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
136 engine.rootContext()->setContextProperty("screenInfo", &screenInfo);
137 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
139 QObject *root = engine.rootObjects().first();
140 QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
141 homescreenHandler->setQuickWindow(window);
143 layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, &graphic_role](json_object *object) {
144 layoutHandler->endDraw(graphic_role);
147 layoutHandler->set_event_handler(QLibWindowmanager::Event_ScreenUpdated, [layoutHandler, launcher, homescreenHandler, root](json_object *object) {
148 json_object *jarray = json_object_object_get(object, "ids");
149 HMI_DEBUG("HomeScreen","ids=%s", json_object_to_json_string(object));
150 int arrLen = json_object_array_length(jarray);
151 QString label = QString("");
152 for( int idx = 0; idx < arrLen; idx++)
154 label = QString(json_object_get_string( json_object_array_get_idx(jarray, idx) ));
155 HMI_DEBUG("HomeScreen","Event_ScreenUpdated application11: %s.", label.toStdString().c_str());
156 homescreenHandler->setCurrentApplication(label);
157 QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label));
159 if((arrLen == 1) && (QString("navigation") == label)){
160 QMetaObject::invokeMethod(root, "changeSwitchState", Q_ARG(QVariant, true));
162 QMetaObject::invokeMethod(root, "changeSwitchState", Q_ARG(QVariant, false));
166 touchArea->setWindow(window);
167 QThread* thread = new QThread;
168 touchArea->moveToThread(thread);
169 QObject::connect(thread, &QThread::started, touchArea, &TouchArea::init);
173 QList<QObject *> sobjs = engine.rootObjects();
174 StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
175 statusBar->init(bindingAddress, engine.rootContext());
177 QObject::connect(homescreenHandler, SIGNAL(shortcutChanged(QString, QString, QString)), shortcutAppModel, SLOT(changeShortcut(QString, QString, QString)));
178 QObject::connect(shortcutAppModel, SIGNAL(shortcutUpdated(QString, struct json_object*)), homescreenHandler, SLOT(updateShortcut(QString, struct json_object*)));
180 shortcutAppModel->screenUpdated();