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