Allow to set notifications for the progress bar
[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  * Copyright (c) 2020 Collabora, Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <QGuiApplication>
20 #include <QCommandLineParser>
21 #include <QtCore/QUrlQuery>
22 #include <QtGui/QGuiApplication>
23 #include <QtQml/QQmlApplicationEngine>
24 #include <QtQml/QQmlContext>
25 #include <QtQml/QQmlComponent>
26 #include <QtQml/qqml.h>
27 #include <QQuickWindow>
28 #include <QTimer>
29
30 #include <weather.h>
31 #include <bluetooth.h>
32 #include "applicationlauncher.h"
33 #include "statusbarmodel.h"
34 #include "mastervolume.h"
35 #include "homescreenhandler.h"
36 #include "hmi-debug.h"
37 #include "chromecontroller.h"
38
39 #include <qpa/qplatformnativeinterface.h>
40 #include <wayland-client.h>
41
42 #include "wayland-agl-shell-client-protocol.h"
43 #include "wayland-agl-shell-desktop-client-protocol.h"
44 #include "shell.h"
45
46 struct shell_container {
47         struct agl_shell *agl_shell;
48         struct agl_shell_desktop *agl_shell_desktop;
49         // used to propagate events from C/C++ event handlers from the protocol
50         // to the QML in case we need them
51         Shell *a_shell;
52         ApplicationLauncher *launcher;
53 };
54
55 static void
56 application_id_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
57                      const char *app_id)
58 {
59         // this event is sent  only when surfaces are being created.  we call
60         // setCurrent() to take care of this the first time, with later state
61         // event changes being handled by the application_state_event()
62         struct shell_container *sc = static_cast<struct shell_container *>(data);
63         sc->launcher->setCurrent(QString(app_id));
64 }
65
66 static void
67 application_state_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
68                         const char *app_id, const char *app_data,
69                         uint32_t app_state, uint32_t app_role)
70 {
71         struct shell_container *sc = static_cast<struct shell_container *>(data);
72         sc->launcher->setCurrent(QString(app_id));
73 }
74
75 static const struct agl_shell_desktop_listener agl_shell_desktop_listener = {
76         application_id_event,
77         application_state_event,
78 };
79
80
81 static void
82 global_add(void *data, struct wl_registry *reg, uint32_t name,
83            const char *interface, uint32_t version)
84 {
85         struct shell_container *sc = static_cast<struct shell_container *>(data);
86
87         if (strcmp(interface, agl_shell_interface.name) == 0) {
88                 sc->agl_shell = static_cast<struct agl_shell *>(
89                         wl_registry_bind(reg, name, &agl_shell_interface, 1)
90                 );
91         } else if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
92                 sc->agl_shell_desktop = static_cast<struct agl_shell_desktop *>(
93                         wl_registry_bind(reg, name, &agl_shell_desktop_interface, 1)
94                 );
95
96                 agl_shell_desktop_add_listener(sc->agl_shell_desktop,
97                                                &agl_shell_desktop_listener, sc);
98         }
99 }
100
101 static void
102 global_remove(void *data, struct wl_registry *reg, uint32_t id)
103 {
104         /* Don't care */
105         (void) data;
106         (void) reg;
107         (void) id;
108 }
109
110 static const struct wl_registry_listener registry_listener = {
111         global_add,
112         global_remove,
113 };
114
115 static struct wl_surface *
116 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
117 {
118         void *surf = native->nativeResourceForWindow("surface", window);
119         return static_cast<struct ::wl_surface *>(surf);
120 }
121
122 static struct wl_output *
123 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
124 {
125         void *output = native->nativeResourceForScreen("output", screen);
126         return static_cast<struct ::wl_output*>(output);
127 }
128
129
130 static struct shell_container *
131 register_agl_shell(QPlatformNativeInterface *native)
132 {
133         struct wl_display *wl;
134         struct wl_registry *registry;
135         struct shell_container *sc = new shell_container();
136
137         wl = static_cast<struct wl_display *>(
138                         native->nativeResourceForIntegration("display")
139         );
140         registry = wl_display_get_registry(wl);
141
142         wl_registry_add_listener(registry, &registry_listener, sc);
143
144         /* Roundtrip to get all globals advertised by the compositor */
145         wl_display_roundtrip(wl);
146         wl_registry_destroy(registry);
147
148         if (!sc->agl_shell) {
149                 delete sc;
150         }
151
152         if (!sc->agl_shell_desktop) {
153                 delete sc;
154         }
155
156         return sc;
157 }
158
159 static struct wl_surface *
160 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
161                  QScreen *screen, QObject **qobj)
162 {
163         QObject *obj = comp->create();
164         obj->setParent(screen);
165
166         QWindow *win = qobject_cast<QWindow *>(obj);
167         *qobj = obj;
168
169         return getWlSurface(native, win);
170 }
171
172 static QScreen *
173 find_screen(const char *screen_name)
174 {
175         QList<QScreen *> screens = qApp->screens();
176         QScreen *found = nullptr;
177         QString qstr_name = QString::fromUtf8(screen_name, -1);
178
179         for (QScreen *xscreen : screens) {
180                 if (qstr_name == xscreen->name()) {
181                         found = xscreen;
182                         break;
183                 }
184         }
185
186         return found;
187 }
188
189 static void
190 load_agl_shell_app(QPlatformNativeInterface *native,
191                    QQmlApplicationEngine *engine,
192                    struct shell_container *sc, QUrl &bindingAddress,
193                    const char *screen_name)
194 {
195         struct wl_surface *bg, *top, *bottom;
196         struct wl_output *output;
197         QObject *qobj_bg, *qobj_top, *qobj_bottom;
198         QScreen *screen = nullptr;
199         struct agl_shell *agl_shell = sc->agl_shell;
200
201         QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
202         qInfo() << bg_comp.errors();
203
204         QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
205         qInfo() << top_comp.errors();
206
207         QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
208         qInfo() << bot_comp.errors();
209
210         if (!screen_name)
211                 screen = qApp->primaryScreen();
212         else
213                 screen = find_screen(screen_name);
214
215         qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
216                 "first screen " << qApp->screens().first()->name();
217         output = getWlOutput(native, screen);
218
219         top = create_component(native, &top_comp, screen, &qobj_top);
220         bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
221         bg = create_component(native, &bg_comp, screen, &qobj_bg);
222
223         /* engine.rootObjects() works only if we had a load() */
224         StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
225         if (statusBar) {
226                 qDebug() << "got statusBar objectname, doing init()";
227                 statusBar->init(bindingAddress, engine->rootContext());
228         }
229
230         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
231         agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
232         qDebug() << "Setting homescreen to screen  " << screen->name();
233
234         agl_shell_set_background(agl_shell, bg, output);
235
236         /* Delay the ready signal until after Qt has done all of its own setup
237          * in a.exec() */
238         QTimer::singleShot(500, [agl_shell](){
239                 agl_shell_ready(agl_shell);
240         });
241 }
242
243 int main(int argc, char *argv[])
244 {
245     setenv("QT_QPA_PLATFORM", "wayland", 1);
246     QGuiApplication a(argc, argv);
247     const char *screen_name;
248     int ret;
249
250     QPlatformNativeInterface *native = qApp->platformNativeInterface();
251     struct shell_container *sc = nullptr;
252     screen_name = getenv("HOMESCREEN_START_SCREEN");
253
254     QCoreApplication::setOrganizationDomain("LinuxFoundation");
255     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
256     QCoreApplication::setApplicationName("HomeScreen");
257     QCoreApplication::setApplicationVersion("0.7.0");
258     /* we need to have an app_id */
259     a.setDesktopFileName("homescreen");
260
261     QCommandLineParser parser;
262     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
263     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
264     parser.addHelpOption();
265     parser.addVersionOption();
266     parser.process(a);
267     QStringList positionalArguments = parser.positionalArguments();
268
269     int port = 1700;
270     QString token = "wm";
271     QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
272
273     if (positionalArguments.length() == 2) {
274         port = positionalArguments.takeFirst().toInt();
275         token = positionalArguments.takeFirst();
276     }
277
278     HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
279     ApplicationLauncher *launcher = new ApplicationLauncher();
280
281     sc = register_agl_shell(native);
282     if (!sc) {
283             exit(EXIT_FAILURE);
284     }
285
286     std::shared_ptr<struct agl_shell>
287             shell{sc->agl_shell, agl_shell_destroy};
288     std::shared_ptr<struct agl_shell_desktop>
289             shell_desktop{sc->agl_shell_desktop, agl_shell_desktop_destroy};
290     Shell *aglShell = new Shell(shell, shell_desktop, &a);
291     sc->a_shell = aglShell;
292     sc->launcher = launcher;
293
294     // import C++ class to QML
295     // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
296     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
297     qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
298     qmlRegisterUncreatableType<ChromeController>("SpeechChrome", 1, 0, "SpeechChromeController",
299                                                  QLatin1String("SpeechChromeController is uncreatable."));
300
301
302     HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
303     homescreenHandler->init(port, token.toStdString().c_str());
304
305     QUrl bindingAddress;
306     bindingAddress.setScheme(QStringLiteral("ws"));
307     bindingAddress.setHost(QStringLiteral("localhost"));
308     bindingAddress.setPort(port);
309     bindingAddress.setPath(QStringLiteral("/api"));
310
311     QUrlQuery query;
312     query.addQueryItem(QStringLiteral("token"), token);
313     bindingAddress.setQuery(query);
314
315     QQmlApplicationEngine engine;
316     QQmlContext *context = engine.rootContext();
317     context->setContextProperty("bindingAddress", bindingAddress);
318
319     context->setContextProperty("homescreenHandler", homescreenHandler);
320     context->setContextProperty("launcher", launcher);
321     context->setContextProperty("weather", new Weather(bindingAddress));
322     context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, context));
323     context->setContextProperty("speechChromeController", new ChromeController(bindingAddress, &engine));
324     // we add it here even if we don't use it
325     context->setContextProperty("shell", aglShell);
326
327     /* instead of loading main.qml we load one-by-one each of the QMLs,
328      * divided now between several surfaces: panels, background.
329      */
330     load_agl_shell_app(native, &engine, sc, bindingAddress, screen_name);
331
332     ret = a.exec();
333
334     delete sc;
335     return ret;
336 }