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