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 "homescreenvoice.h"
36 #include "toucharea.h"
37 #include "shortcutappmodel.h"
38 #include "hmi-debug.h"
40 // XXX: We want this DBus connection to be shared across the different
41 // QML objects, is there another way to do this, a nice way, perhaps?
42 org::AGL::afm::user *afm_user_daemon_proxy;
47 static inline void cleanup(org::AGL::afm::user *p) {
49 afm_user_daemon_proxy = Q_NULLPTR;
53 void noOutput(QtMsgType, const QMessageLogContext &, const QString &)
59 int main(int argc, char *argv[])
61 QGuiApplication a(argc, argv);
64 QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
66 QDBusConnection::sessionBus(),
68 ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
70 QCoreApplication::setOrganizationDomain("LinuxFoundation");
71 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
72 QCoreApplication::setApplicationName("HomeScreen");
73 QCoreApplication::setApplicationVersion("0.7.0");
75 QCommandLineParser parser;
76 parser.addPositionalArgument("port", a.translate("main", "port for binding"));
77 parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
78 parser.addHelpOption();
79 parser.addVersionOption();
81 QStringList positionalArguments = parser.positionalArguments();
85 QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
87 if (positionalArguments.length() == 2) {
88 port = positionalArguments.takeFirst().toInt();
89 token = positionalArguments.takeFirst();
92 HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
94 // import C++ class to QML
95 // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
96 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
97 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
98 qmlRegisterType<ShortcutAppModel>("ShortcutAppModel", 1, 0, "ShortcutAppModel");
100 ApplicationLauncher *launcher = new ApplicationLauncher();
101 QLibWindowmanager* layoutHandler = new QLibWindowmanager();
102 HomescreenHandler* homescreenHandler = new HomescreenHandler();
103 ShortcutAppModel* shortcutAppModel = new ShortcutAppModel();
104 HomescreenVoice* homescreenVoice = new HomescreenVoice();
106 if(layoutHandler->init(port,token) != 0){
110 AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
112 if (layoutHandler->requestSurface(graphic_role) != 0) {
117 bindingAddress.setScheme(QStringLiteral("ws"));
118 bindingAddress.setHost(QStringLiteral("localhost"));
119 bindingAddress.setPort(port);
120 bindingAddress.setPath(QStringLiteral("/api"));
123 query.addQueryItem(QStringLiteral("token"), token);
124 bindingAddress.setQuery(query);
126 TouchArea* touchArea = new TouchArea();
127 homescreenHandler->init(port, token.toStdString().c_str(), layoutHandler, graphic_role);
128 homescreenVoice->init(port, token.toStdString().c_str());
131 QQmlApplicationEngine engine;
132 engine.rootContext()->setContextProperty("bindingAddress", bindingAddress);
133 engine.rootContext()->setContextProperty("layoutHandler", layoutHandler);
134 engine.rootContext()->setContextProperty("homescreenHandler", homescreenHandler);
135 engine.rootContext()->setContextProperty("homescreenVoice", homescreenVoice);
136 engine.rootContext()->setContextProperty("touchArea", touchArea);
137 engine.rootContext()->setContextProperty("shortcutAppModel", shortcutAppModel);
138 engine.rootContext()->setContextProperty("launcher", launcher);
139 engine.rootContext()->setContextProperty("weather", new Weather(bindingAddress));
140 engine.rootContext()->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
141 engine.rootContext()->setContextProperty("screenInfo", &screenInfo);
142 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
144 QObject *root = engine.rootObjects().first();
145 QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
146 homescreenHandler->setQuickWindow(window);
148 layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, &graphic_role](json_object *object) {
149 layoutHandler->endDraw(graphic_role);
152 layoutHandler->set_event_handler(QLibWindowmanager::Event_ScreenUpdated, [layoutHandler, launcher, homescreenHandler, shortcutAppModel, root](json_object *object) {
153 json_object *jarray = json_object_object_get(object, "ids");
154 HMI_DEBUG("HomeScreen","ids=%s", json_object_to_json_string(object));
155 int arrLen = json_object_array_length(jarray);
156 QString label = QString("");
157 static bool first_start = true;
158 for( int idx = 0; idx < arrLen; idx++)
160 label = QString(json_object_get_string( json_object_array_get_idx(jarray, idx) ));
161 HMI_DEBUG("HomeScreen","Event_ScreenUpdated application: %s.", label.toStdString().c_str());
162 homescreenHandler->setCurrentApplication(label);
163 QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label));
165 if((arrLen == 1) && (QString("navigation") == label)){
166 QMetaObject::invokeMethod(root, "changeSwitchState", Q_ARG(QVariant, true));
168 QMetaObject::invokeMethod(root, "changeSwitchState", Q_ARG(QVariant, false));
171 if((arrLen == 1) && (QString("launcher") == label)) {
173 shortcutAppModel->screenUpdated();
178 touchArea->setWindow(window);
179 QThread* thread = new QThread;
180 touchArea->moveToThread(thread);
181 QObject::connect(thread, &QThread::started, touchArea, &TouchArea::init);
185 QList<QObject *> sobjs = engine.rootObjects();
186 StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
187 statusBar->init(bindingAddress, engine.rootContext());
189 QObject::connect(homescreenHandler, SIGNAL(shortcutChanged(QString, QString, QString)), shortcutAppModel, SLOT(changeShortcut(QString, QString, QString)));
190 QObject::connect(shortcutAppModel, SIGNAL(shortcutUpdated(QString, struct json_object*)), homescreenHandler, SLOT(updateShortcut(QString, struct json_object*)));