5c819f9f3b2e78c59a1ba21e9886eec1fcd13b62
[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
26 #include <qlibwindowmanager.h>
27 #include <weather.h>
28 #include <bluetooth.h>
29 #include "applicationlauncher.h"
30 #include "statusbarmodel.h"
31 #include "afm_user_daemon_proxy.h"
32 #include "mastervolume.h"
33 #include "homescreenhandler.h"
34 #include "hmi-debug.h"
35 #include "chromecontroller.h"
36
37 // XXX: We want this DBus connection to be shared across the different
38 // QML objects, is there another way to do this, a nice way, perhaps?
39 org::AGL::afm::user *afm_user_daemon_proxy;
40
41 namespace {
42
43 struct Cleanup {
44     static inline void cleanup(org::AGL::afm::user *p) {
45         delete p;
46         afm_user_daemon_proxy = Q_NULLPTR;
47     }
48 };
49
50 void noOutput(QtMsgType, const QMessageLogContext &, const QString &)
51 {
52 }
53
54 }
55
56 int main(int argc, char *argv[])
57 {
58     QGuiApplication a(argc, argv);
59
60     // use launch process
61     QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
62                                                                                                "/org/AGL/afm/user",
63                                                                                                QDBusConnection::sessionBus(),
64                                                                                                0));
65     ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
66
67     QCoreApplication::setOrganizationDomain("LinuxFoundation");
68     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
69     QCoreApplication::setApplicationName("HomeScreen");
70     QCoreApplication::setApplicationVersion("0.7.0");
71
72     QCommandLineParser parser;
73     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
74     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
75     parser.addHelpOption();
76     parser.addVersionOption();
77     parser.process(a);
78     QStringList positionalArguments = parser.positionalArguments();
79
80     int port = 1700;
81     QString token = "wm";
82     QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
83
84     if (positionalArguments.length() == 2) {
85         port = positionalArguments.takeFirst().toInt();
86         token = positionalArguments.takeFirst();
87     }
88
89     HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
90
91     // import C++ class to QML
92     // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
93     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
94     qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
95     qmlRegisterUncreatableType<ChromeController>("SpeechChrome", 1, 0, "SpeechChromeController",
96                                                  QLatin1String("SpeechChromeController is uncreatable."));
97
98     ApplicationLauncher *launcher = new ApplicationLauncher();
99     QLibWindowmanager* layoutHandler = new QLibWindowmanager();
100     if(layoutHandler->init(port,token) != 0){
101         exit(EXIT_FAILURE);
102     }
103
104     AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
105
106     if (layoutHandler->requestSurface(graphic_role) != 0) {
107         exit(EXIT_FAILURE);
108     }
109
110     layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, &graphic_role](json_object *object) {
111         layoutHandler->endDraw(graphic_role);
112     });
113
114     layoutHandler->set_event_handler(QLibWindowmanager::Event_ScreenUpdated, [layoutHandler, launcher](json_object *object) {
115         json_object *jarray = json_object_object_get(object, "ids");
116         int arrLen = json_object_array_length(jarray);
117         for( int idx = 0; idx < arrLen; idx++)
118         {
119             QString label = QString(json_object_get_string(     json_object_array_get_idx(jarray, idx) ));
120             HMI_DEBUG("HomeScreen","Event_ScreenUpdated application: %s.", label.toStdString().c_str());
121             QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label));
122         }
123     });
124
125     HomescreenHandler* homescreenHandler = new HomescreenHandler();
126     homescreenHandler->init(port, token.toStdString().c_str());
127
128     QUrl bindingAddress;
129     bindingAddress.setScheme(QStringLiteral("ws"));
130     bindingAddress.setHost(QStringLiteral("localhost"));
131     bindingAddress.setPort(port);
132     bindingAddress.setPath(QStringLiteral("/api"));
133
134     QUrlQuery query;
135     query.addQueryItem(QStringLiteral("token"), token);
136     bindingAddress.setQuery(query);
137
138     // mail.qml loading
139     QQmlApplicationEngine engine;
140     engine.rootContext()->setContextProperty("bindingAddress", bindingAddress);
141     engine.rootContext()->setContextProperty("layoutHandler", layoutHandler);
142     engine.rootContext()->setContextProperty("homescreenHandler", homescreenHandler);
143     engine.rootContext()->setContextProperty("launcher", launcher);
144     engine.rootContext()->setContextProperty("weather", new Weather(bindingAddress));
145     engine.rootContext()->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
146     engine.rootContext()->setContextProperty("speechChromeController", new ChromeController(bindingAddress, &engine));
147     engine.rootContext()->setContextProperty("screenInfo", &screenInfo);
148     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
149
150     QObject *root = engine.rootObjects().first();
151     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
152     QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface()));
153
154     QList<QObject *> sobjs = engine.rootObjects();
155     StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
156     statusBar->init(bindingAddress, engine.rootContext());
157
158     return a.exec();
159 }