e5509482c60f1c645255b1ce1829e8047d1ad6da
[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 }
49
50 int main(int argc, char *argv[])
51 {
52     QString myname = QString("launcher");
53     QGuiApplication a(argc, argv);
54
55     // use launch process
56     QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
57                                                                                                "/org/AGL/afm/user",
58                                                                                                QDBusConnection::sessionBus(),
59                                                                                                0));
60     ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
61
62     QCoreApplication::setOrganizationDomain("LinuxFoundation");
63     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
64     QCoreApplication::setApplicationName(myname);
65     QCoreApplication::setApplicationVersion("0.1.0");
66
67     QCommandLineParser parser;
68     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
69     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
70     parser.addHelpOption();
71     parser.addVersionOption();
72     parser.process(a);
73     QStringList positionalArguments = parser.positionalArguments();
74
75     int port = 1700;
76     QString token = "wm";
77
78     if (positionalArguments.length() == 2) {
79         port = positionalArguments.takeFirst().toInt();
80         token = positionalArguments.takeFirst();
81     }
82
83     HMI_DEBUG("launcher","port = %d, token = %s", port, token.toStdString().c_str());
84
85     // import C++ class to QML
86     qmlRegisterType<ApplicationModel>("AppModel", 1, 0, "ApplicationModel");
87
88     // DBus
89     qDBusRegisterMetaType<AppInfo>();
90     qDBusRegisterMetaType<QList<AppInfo> >();
91
92     ApplicationLauncher *launcher = new ApplicationLauncher();
93     QLibWindowmanager* layoutHandler = new QLibWindowmanager();
94     if(layoutHandler->init(port,token) != 0){
95         exit(EXIT_FAILURE);
96     }
97
98     AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
99
100     if (layoutHandler->requestSurface(myname) != 0) {
101         exit(EXIT_FAILURE);
102     }
103
104     layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, myname](json_object *object) {
105         layoutHandler->endDraw(myname);
106     });
107
108     layoutHandler->set_event_handler(QLibWindowmanager::Event_Visible, [layoutHandler, launcher](json_object *object) {
109         QString label = QString(json_object_get_string( json_object_object_get(object, "drawing_name") ));
110         qDebug() << label;
111         QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label == "HomeScreen" ? "Home" : label));
112     });
113
114     layoutHandler->set_event_handler(QLibWindowmanager::Event_Invisible, [layoutHandler, launcher](json_object *object) {
115         const char* label = json_object_get_string(     json_object_object_get(object, "drawing_name") );
116         HMI_DEBUG("launch", "surface %s Event_Invisible", label);
117     });
118
119     HomescreenHandler* homescreenHandler = new HomescreenHandler();
120     homescreenHandler->init(port, token.toStdString().c_str(), layoutHandler, myname);
121
122     QUrl bindingAddress;
123     bindingAddress.setScheme(QStringLiteral("ws"));
124     bindingAddress.setHost(QStringLiteral("localhost"));
125     bindingAddress.setPort(port);
126     bindingAddress.setPath(QStringLiteral("/api"));
127
128     QUrlQuery query;
129     query.addQueryItem(QStringLiteral("token"), token);
130     bindingAddress.setQuery(query);
131
132     const QByteArray hack_delay = qgetenv("HMI_LAUNCHER_STARTUP_DELAY");
133     int delay_time = 1;
134
135     if (!hack_delay.isEmpty()) {
136        delay_time = (QString::fromLocal8Bit(hack_delay)).toInt();
137     }
138
139     QThread::sleep(delay_time);
140     qDebug("Sleep %d sec to resolve race condtion between HomeScreen and Launcher", delay_time);
141
142     // mail.qml loading
143     QQmlApplicationEngine engine;
144     engine.rootContext()->setContextProperty(QStringLiteral("layoutHandler"), layoutHandler);
145     engine.rootContext()->setContextProperty(QStringLiteral("homescreenHandler"), homescreenHandler);
146     engine.rootContext()->setContextProperty(QStringLiteral("launcher"), launcher);
147     engine.rootContext()->setContextProperty(QStringLiteral("screenInfo"), &screenInfo);
148     engine.load(QUrl(QStringLiteral("qrc:/Launcher.qml")));
149     homescreenHandler->getRunnables();
150
151     QObject *root = engine.rootObjects().first();
152     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
153     QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface()));
154
155     return a.exec();
156 }