add vui button
[apps/homescreen.git] / homescreen / src / main.cpp
1 /*
2  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3  * Copyright (c) 2017, 2018 TOYOTA MOTOR CORPORATION
4  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
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>
25 #include <QThread>
26
27 #include <qlibwindowmanager.h>
28 #include <weather.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"
39
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;
43
44 namespace {
45
46 struct Cleanup {
47     static inline void cleanup(org::AGL::afm::user *p) {
48         delete p;
49         afm_user_daemon_proxy = Q_NULLPTR;
50     }
51 };
52
53 void noOutput(QtMsgType, const QMessageLogContext &, const QString &)
54 {
55 }
56
57 }
58
59 int main(int argc, char *argv[])
60 {
61     QGuiApplication a(argc, argv);
62
63     // use launch process
64     QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
65                                                                                                "/org/AGL/afm/user",
66                                                                                                QDBusConnection::sessionBus(),
67                                                                                                0));
68     ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
69
70     QCoreApplication::setOrganizationDomain("LinuxFoundation");
71     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
72     QCoreApplication::setApplicationName("HomeScreen");
73     QCoreApplication::setApplicationVersion("0.7.0");
74
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();
80     parser.process(a);
81     QStringList positionalArguments = parser.positionalArguments();
82
83     int port = 1700;
84     QString token = "wm";
85     QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
86
87     if (positionalArguments.length() == 2) {
88         port = positionalArguments.takeFirst().toInt();
89         token = positionalArguments.takeFirst();
90     }
91
92     HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
93
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");
99
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();
105
106     if(layoutHandler->init(port,token) != 0){
107         exit(EXIT_FAILURE);
108     }
109
110     AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
111
112     if (layoutHandler->requestSurface(graphic_role) != 0) {
113         exit(EXIT_FAILURE);
114     }
115
116     QUrl bindingAddress;
117     bindingAddress.setScheme(QStringLiteral("ws"));
118     bindingAddress.setHost(QStringLiteral("localhost"));
119     bindingAddress.setPort(port);
120     bindingAddress.setPath(QStringLiteral("/api"));
121
122     QUrlQuery query;
123     query.addQueryItem(QStringLiteral("token"), token);
124     bindingAddress.setQuery(query);
125
126     TouchArea* touchArea = new TouchArea();
127     homescreenHandler->init(port, token.toStdString().c_str(), layoutHandler, graphic_role);
128         homescreenVoice->init(port, token.toStdString().c_str());
129
130     // mail.qml loading
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")));
143
144     QObject *root = engine.rootObjects().first();
145     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
146     homescreenHandler->setQuickWindow(window);
147
148     layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, &graphic_role](json_object *object) {
149         layoutHandler->endDraw(graphic_role);
150     });
151
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++)
159         {
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));
164         }
165         if((arrLen == 1) && (QString("navigation") == label)){
166             QMetaObject::invokeMethod(root, "changeSwitchState", Q_ARG(QVariant, true));
167         }else{
168             QMetaObject::invokeMethod(root, "changeSwitchState", Q_ARG(QVariant, false));
169         }
170         if(first_start) {
171             if((arrLen == 1) && (QString("launcher") == label)) {
172                 first_start = false;
173                 shortcutAppModel->screenUpdated();
174             }
175         }
176     });
177
178     touchArea->setWindow(window);
179     QThread* thread = new QThread;
180     touchArea->moveToThread(thread);
181     QObject::connect(thread, &QThread::started, touchArea, &TouchArea::init);
182
183     thread->start();
184
185     QList<QObject *> sobjs = engine.rootObjects();
186     StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
187     statusBar->init(bindingAddress, engine.rootContext());
188
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*)));
191
192     return a.exec();
193 }