use appid instead of appname in "tap_shortcut"
[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 "qlibhomescreen.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     QLibHomeScreen* homescreenHandler = new QLibHomeScreen();
97     ApplicationLauncher *launcher = new ApplicationLauncher();
98     QLibWindowmanager* layoutHandler = new QLibWindowmanager();
99     if(layoutHandler->init(port,token) != 0){
100         exit(EXIT_FAILURE);
101     }
102
103     AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
104
105     if (layoutHandler->requestSurface(myname) != 0) {
106         exit(EXIT_FAILURE);
107     }
108
109     layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, myname](json_object *object) {
110         layoutHandler->endDraw(myname);
111     });
112
113     layoutHandler->set_event_handler(QLibWindowmanager::Event_Visible, [layoutHandler, launcher](json_object *object) {
114         QString label = QString(json_object_get_string( json_object_object_get(object, "drawing_name") ));
115         qDebug() << label;
116         QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label == "HomeScreen" ? "Home" : label));
117     });
118
119     layoutHandler->set_event_handler(QLibWindowmanager::Event_Invisible, [layoutHandler, launcher](json_object *object) {
120         const char* label = json_object_get_string(     json_object_object_get(object, "drawing_name") );
121         HMI_DEBUG("launch", "surface %s Event_Invisible", label);
122     });
123
124     homescreenHandler->init(port, token.toStdString().c_str());
125
126     homescreenHandler->set_event_handler(QLibHomeScreen::Event_TapShortcut, [layoutHandler, myname](json_object *object){
127         qDebug("Surface %s got tapShortcut\n", myname);
128         layoutHandler->activateSurface(myname);
129     });
130
131     QUrl bindingAddress;
132     bindingAddress.setScheme(QStringLiteral("ws"));
133     bindingAddress.setHost(QStringLiteral("localhost"));
134     bindingAddress.setPort(port);
135     bindingAddress.setPath(QStringLiteral("/api"));
136
137     QUrlQuery query;
138     query.addQueryItem(QStringLiteral("token"), token);
139     bindingAddress.setQuery(query);
140
141     const QByteArray hack_delay = qgetenv("HMI_LAUNCHER_STARTUP_DELAY");
142     int delay_time = 1;
143
144     if (!hack_delay.isEmpty()) {
145        delay_time = (QString::fromLocal8Bit(hack_delay)).toInt();
146     }
147
148     QThread::sleep(delay_time);
149     qDebug("Sleep %d sec to resolve race condtion between HomeScreen and Launcher", delay_time);
150
151     // mail.qml loading
152     QQmlApplicationEngine engine;
153     engine.rootContext()->setContextProperty(QStringLiteral("layoutHandler"), layoutHandler);
154     engine.rootContext()->setContextProperty(QStringLiteral("homescreenHandler"), homescreenHandler);
155     engine.rootContext()->setContextProperty(QStringLiteral("launcher"), launcher);
156     engine.rootContext()->setContextProperty(QStringLiteral("screenInfo"), &screenInfo);
157     engine.load(QUrl(QStringLiteral("qrc:/Launcher.qml")));
158
159     QObject *root = engine.rootObjects().first();
160     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
161     QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface()));
162
163     return a.exec();
164 }