e56ff5a32a7def40608f23ec4382dd88ae02f789
[apps/launcher.git] / launcher / src / main.cpp
1 /*
2  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3  * Copyright (c) 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 "applicationlauncher.h"
29 #include "applicationmodel.h"
30 #include "appinfo.h"
31 #include "afm_user_daemon_proxy.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     QString myname = QString("launcher");
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(myname);
69     QCoreApplication::setApplicationVersion("0.1.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
82     if (positionalArguments.length() == 2) {
83         port = positionalArguments.takeFirst().toInt();
84         token = positionalArguments.takeFirst();
85     }
86
87     HMI_DEBUG("launcher","port = %d, token = %s", port, token.toStdString().c_str());
88
89     // import C++ class to QML
90     qmlRegisterType<ApplicationModel>("AppModel", 1, 0, "ApplicationModel");
91
92     // DBus
93     qDBusRegisterMetaType<AppInfo>();
94     qDBusRegisterMetaType<QList<AppInfo> >();
95
96     ApplicationLauncher *launcher = new ApplicationLauncher();
97     QLibWindowmanager* layoutHandler = new QLibWindowmanager();
98     if(layoutHandler->init(port,token) != 0){
99         exit(EXIT_FAILURE);
100     }
101
102     AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
103
104     if (layoutHandler->requestSurface(myname) != 0) {
105         exit(EXIT_FAILURE);
106     }
107
108     layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, myname](json_object *object) {
109         layoutHandler->endDraw(myname);
110     });
111
112     layoutHandler->set_event_handler(QLibWindowmanager::Event_Visible, [layoutHandler, launcher](json_object *object) {
113         QString label = QString(json_object_get_string( json_object_object_get(object, "drawing_name") ));
114         qDebug() << label;
115         QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label == "HomeScreen" ? "Home" : label));
116     });
117
118     layoutHandler->set_event_handler(QLibWindowmanager::Event_Invisible, [layoutHandler, launcher](json_object *object) {
119         const char* label = json_object_get_string(     json_object_object_get(object, "drawing_name") );
120         HMI_DEBUG("launch", "surface %s Event_Invisible", label);
121     });
122
123     HomescreenHandler* homescreenHandler = new HomescreenHandler();
124     homescreenHandler->init(port, token.toStdString().c_str(), layoutHandler, myname);
125
126     QUrl bindingAddress;
127     bindingAddress.setScheme(QStringLiteral("ws"));
128     bindingAddress.setHost(QStringLiteral("localhost"));
129     bindingAddress.setPort(port);
130     bindingAddress.setPath(QStringLiteral("/api"));
131
132     QUrlQuery query;
133     query.addQueryItem(QStringLiteral("token"), token);
134     bindingAddress.setQuery(query);
135
136     const QByteArray hack_delay = qgetenv("HMI_LAUNCHER_STARTUP_DELAY");
137     int delay_time = 1;
138
139     if (!hack_delay.isEmpty()) {
140        delay_time = (QString::fromLocal8Bit(hack_delay)).toInt();
141     }
142
143     QThread::sleep(delay_time);
144     qDebug("Sleep %d sec to resolve race condtion between HomeScreen and Launcher", delay_time);
145
146     // mail.qml loading
147     QQmlApplicationEngine engine;
148     engine.rootContext()->setContextProperty(QStringLiteral("layoutHandler"), layoutHandler);
149     engine.rootContext()->setContextProperty(QStringLiteral("homescreenHandler"), homescreenHandler);
150     engine.rootContext()->setContextProperty(QStringLiteral("launcher"), launcher);
151     engine.rootContext()->setContextProperty(QStringLiteral("screenInfo"), &screenInfo);
152     engine.load(QUrl(QStringLiteral("qrc:/Launcher.qml")));
153     homescreenHandler->getRunnables();
154
155     QObject *root = engine.rootObjects().first();
156     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
157     QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface()));
158
159     return a.exec();
160 }