registerShortcut
[apps/homescreen.git] / homescreen / src / main.cpp
1 /*
2  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3  * Copyright (c) 2017, 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 <weather.h>
29 #include <bluetooth.h>
30 #include "applicationlauncher.h"
31 #include "statusbarmodel.h"
32 #include "afm_user_daemon_proxy.h"
33 #include "mastervolume.h"
34 #include "homescreenhandler.h"
35 #include "toucharea.h"
36 #include "shortcutappmodel.h"
37 #include "hmi-debug.h"
38
39 // XXX: We want this DBus connection to be shared across the different
40 // QML objects, is there another way to do this, a nice way, perhaps?
41 org::AGL::afm::user *afm_user_daemon_proxy;
42
43 namespace {
44
45 struct Cleanup {
46     static inline void cleanup(org::AGL::afm::user *p) {
47         delete p;
48         afm_user_daemon_proxy = Q_NULLPTR;
49     }
50 };
51
52 void noOutput(QtMsgType, const QMessageLogContext &, const QString &)
53 {
54 }
55
56 }
57
58 int main(int argc, char *argv[])
59 {
60     QGuiApplication a(argc, argv);
61
62     // use launch process
63     QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
64                                                                                                "/org/AGL/afm/user",
65                                                                                                QDBusConnection::sessionBus(),
66                                                                                                0));
67     ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
68
69     QCoreApplication::setOrganizationDomain("LinuxFoundation");
70     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
71     QCoreApplication::setApplicationName("HomeScreen");
72     QCoreApplication::setApplicationVersion("0.7.0");
73
74     QCommandLineParser parser;
75     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
76     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
77     parser.addHelpOption();
78     parser.addVersionOption();
79     parser.process(a);
80     QStringList positionalArguments = parser.positionalArguments();
81
82     int port = 1700;
83     QString token = "wm";
84     QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
85
86     if (positionalArguments.length() == 2) {
87         port = positionalArguments.takeFirst().toInt();
88         token = positionalArguments.takeFirst();
89     }
90
91     HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
92
93     // import C++ class to QML
94     // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
95     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
96     qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
97     qmlRegisterType<ShortcutAppModel>("ShortcutAppModel", 1, 0, "ShortcutAppModel");
98
99     ApplicationLauncher *launcher = new ApplicationLauncher();
100     QLibWindowmanager* layoutHandler = new QLibWindowmanager();
101     HomescreenHandler* homescreenHandler = new HomescreenHandler();
102     ShortcutAppModel* shortcutAppModel = new ShortcutAppModel();
103     if(layoutHandler->init(port,token) != 0){
104         exit(EXIT_FAILURE);
105     }
106
107     AGLScreenInfo screenInfo(layoutHandler->get_scale_factor());
108
109     if (layoutHandler->requestSurface(graphic_role) != 0) {
110         exit(EXIT_FAILURE);
111     }
112
113     QUrl bindingAddress;
114     bindingAddress.setScheme(QStringLiteral("ws"));
115     bindingAddress.setHost(QStringLiteral("localhost"));
116     bindingAddress.setPort(port);
117     bindingAddress.setPath(QStringLiteral("/api"));
118
119     QUrlQuery query;
120     query.addQueryItem(QStringLiteral("token"), token);
121     bindingAddress.setQuery(query);
122
123     TouchArea* touchArea = new TouchArea();
124     homescreenHandler->init(port, token.toStdString().c_str(), layoutHandler, graphic_role);
125
126     // mail.qml loading
127     QQmlApplicationEngine engine;
128     engine.rootContext()->setContextProperty("bindingAddress", bindingAddress);
129     engine.rootContext()->setContextProperty("layoutHandler", layoutHandler);
130     engine.rootContext()->setContextProperty("homescreenHandler", homescreenHandler);
131     engine.rootContext()->setContextProperty("touchArea", touchArea);
132     engine.rootContext()->setContextProperty("shortcutAppModel", shortcutAppModel);
133     engine.rootContext()->setContextProperty("launcher", launcher);
134     engine.rootContext()->setContextProperty("weather", new Weather(bindingAddress));
135     engine.rootContext()->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
136     engine.rootContext()->setContextProperty("screenInfo", &screenInfo);
137     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
138
139     QObject *root = engine.rootObjects().first();
140     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
141     homescreenHandler->setQuickWindow(window);
142
143     layoutHandler->set_event_handler(QLibWindowmanager::Event_SyncDraw, [layoutHandler, &graphic_role](json_object *object) {
144         layoutHandler->endDraw(graphic_role);
145     });
146
147     layoutHandler->set_event_handler(QLibWindowmanager::Event_ScreenUpdated, [layoutHandler, launcher, homescreenHandler, root](json_object *object) {
148         json_object *jarray = json_object_object_get(object, "ids");
149         HMI_DEBUG("HomeScreen","ids=%s", json_object_to_json_string(object));
150         int arrLen = json_object_array_length(jarray);
151         QString label = QString("");
152         for( int idx = 0; idx < arrLen; idx++)
153         {
154             label = QString(json_object_get_string(     json_object_array_get_idx(jarray, idx) ));
155             HMI_DEBUG("HomeScreen","Event_ScreenUpdated application11: %s.", label.toStdString().c_str());
156             homescreenHandler->setCurrentApplication(label);
157             QMetaObject::invokeMethod(launcher, "setCurrent", Qt::QueuedConnection, Q_ARG(QString, label));
158         }
159         if((arrLen == 1) && (QString("navigation") == label)){
160             QMetaObject::invokeMethod(root, "changeSwitchState", Q_ARG(QVariant, true));
161         }else{
162             QMetaObject::invokeMethod(root, "changeSwitchState", Q_ARG(QVariant, false));
163         }
164     });
165
166     touchArea->setWindow(window);
167     QThread* thread = new QThread;
168     touchArea->moveToThread(thread);
169     QObject::connect(thread, &QThread::started, touchArea, &TouchArea::init);
170
171     thread->start();
172
173     QList<QObject *> sobjs = engine.rootObjects();
174     StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
175     statusBar->init(bindingAddress, engine.rootContext());
176
177     QObject::connect(homescreenHandler, SIGNAL(shortcutChanged(QString, QString, QString)), shortcutAppModel, SLOT(changeShortcut(QString, QString, QString)));
178     QObject::connect(shortcutAppModel, SIGNAL(shortcutUpdated(QString, struct json_object*)), homescreenHandler, SLOT(updateShortcut(QString, struct json_object*)));
179
180     shortcutAppModel->screenUpdated();
181
182     return a.exec();
183 }