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