50f13037d75d9bf3ac86ca44855fdd56ad250f2f
[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
26 #include <qlibwindowmanager.h>
27 #include "applicationlauncher.h"
28 #include "applicationmodel.h"
29 #include "appinfo.h"
30 #include "afm_user_daemon_proxy.h"
31 #include "qlibhomescreen.h"
32 #include "hmi-debug.h"
33
34 // XXX: We want this DBus connection to be shared across the different
35 // QML objects, is there another way to do this, a nice way, perhaps?
36 org::AGL::afm::user *afm_user_daemon_proxy;
37
38 namespace {
39
40 struct Cleanup {
41     static inline void cleanup(org::AGL::afm::user *p) {
42         delete p;
43         afm_user_daemon_proxy = Q_NULLPTR;
44     }
45 };
46
47 void noOutput(QtMsgType, const QMessageLogContext &, const QString &)
48 {
49 }
50
51 }
52
53 int main(int argc, char *argv[])
54 {
55     QString myname = QString("launcher");
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(myname);
68     QCoreApplication::setApplicationVersion("0.1.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("launcher","port = %d, token = %s", port, token.toStdString().c_str());
87
88     // import C++ class to QML
89     qmlRegisterType<ApplicationModel>("AppModel", 1, 0, "ApplicationModel");
90
91     // DBus
92     qDBusRegisterMetaType<AppInfo>();
93     qDBusRegisterMetaType<QList<AppInfo> >();
94
95     QLibHomeScreen* homescreenHandler = new QLibHomeScreen();
96     ApplicationLauncher *launcher = new ApplicationLauncher();
97     QLibWindowmanager* layoutHandler = new QLibWindowmanager();
98     if(layoutHandler->init(port,token) != 0){
99         exit(EXIT_FAILURE);
100     }
101
102     if (layoutHandler->requestSurface(myname) != 0) {
103         exit(EXIT_FAILURE);
104     }
105
106     layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, myname](json_object *object) {
107         layoutHandler->endDraw(myname);
108     });
109
110     layoutHandler->set_event_handler(QLibWindowmanager::Event_Visible, [layoutHandler, launcher](json_object *object) {
111         QString label = QString(json_object_get_string( json_object_object_get(object, "drawing_name") ));
112         qDebug() << label;
113         QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label == "HomeScreen" ? "Home" : label));
114     });
115
116     layoutHandler->set_event_handler(QLibWindowmanager::Event_Invisible, [layoutHandler, launcher](json_object *object) {
117         const char* label = json_object_get_string(     json_object_object_get(object, "drawing_name") );
118         HMI_DEBUG("launch", "surface %s Event_Invisible", label);
119     });
120
121     homescreenHandler->init(port, token.toStdString().c_str());
122
123     homescreenHandler->set_event_handler(QLibHomeScreen::Event_TapShortcut, [layoutHandler, myname](json_object *object){
124         json_object *appnameJ = nullptr;
125         if(json_object_object_get_ex(object, "application_name", &appnameJ))
126         {
127             const char *appname = json_object_get_string(appnameJ);
128             if(myname == appname)
129             {
130                 qDebug("Surface %s got tapShortcut\n", appname);
131                 layoutHandler->activateSurface(myname);
132             }
133         }
134     });
135
136     QUrl bindingAddress;
137     bindingAddress.setScheme(QStringLiteral("ws"));
138     bindingAddress.setHost(QStringLiteral("localhost"));
139     bindingAddress.setPort(port);
140     bindingAddress.setPath(QStringLiteral("/api"));
141
142     QUrlQuery query;
143     query.addQueryItem(QStringLiteral("token"), token);
144     bindingAddress.setQuery(query);
145
146     // mail.qml loading
147     QQmlApplicationEngine engine;
148     engine.rootContext()->setContextProperty("layoutHandler", layoutHandler);
149     engine.rootContext()->setContextProperty("homescreenHandler", homescreenHandler);
150     engine.rootContext()->setContextProperty("launcher", launcher);
151     engine.load(QUrl(QStringLiteral("qrc:/Launcher.qml")));
152
153     QObject *root = engine.rootObjects().first();
154     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
155     QObject::connect(window, SIGNAL(frameSwapped()), layoutHandler, SLOT(slotActivateSurface()));
156
157     return a.exec();
158 }