5d7e36d66a83f61e9974bbb315d2769e181c4d94
[apps/homescreen.git] / homescreen / src / main.cpp
1 // SPDX-License-Identifier: Apache-2.0
2 /*
3  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
4  * Copyright (c) 2017, 2018 TOYOTA MOTOR CORPORATION
5  * Copyright (c) 2022 Konsulko Group
6  */
7
8 #include <QGuiApplication>
9 #include <QCommandLineParser>
10 #include <QtCore/QUrlQuery>
11 #include <QtGui/QGuiApplication>
12 #include <QtQml/QQmlApplicationEngine>
13 #include <QtQml/QQmlContext>
14 #include <QtQml/QQmlComponent>
15 #include <QtQml/qqml.h>
16 #include <QQuickWindow>
17 #include <QTimer>
18
19 #include <weather.h>
20 #include <bluetooth.h>
21
22 #include "applicationlauncher.h"
23 #include "statusbarmodel.h"
24 #include "mastervolume.h"
25 #include "homescreenhandler.h"
26 #include "hmi-debug.h"
27
28 // meson will define these
29 #include QT_QPA_HEADER
30 #include <wayland-client.h>
31
32 #include "agl-shell-client-protocol.h"
33 #include "shell.h"
34
35 #ifndef MIN
36 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
37 #endif
38
39 struct shell_data {
40         struct agl_shell *shell;
41         HomescreenHandler *homescreenHandler;
42         bool wait_for_bound;
43         bool bound_ok;
44         int ver;
45 };
46
47 static void
48 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
49 {
50         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
51         shell_data->wait_for_bound = false;
52
53         shell_data->bound_ok = true;
54 }
55
56 static void
57 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
58 {
59         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
60         shell_data->wait_for_bound = false;
61
62         shell_data->bound_ok = false;
63 }
64
65 static void
66 agl_shell_app_state(void *data, struct agl_shell *agl_shell,
67                 const char *app_id, uint32_t state)
68 {
69         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
70         HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
71
72         if (!homescreenHandler)
73                 return;
74
75         switch (state) {
76         case AGL_SHELL_APP_STATE_STARTED:
77                 qDebug() << "Got AGL_SHELL_APP_STATE_STARTED for app_id " << app_id;
78                 homescreenHandler->processAppStatusEvent(app_id, "started");
79                 break;
80         case AGL_SHELL_APP_STATE_TERMINATED:
81                 qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
82                 // handled by HomescreenHandler::processAppStatusEvent
83                 break;
84         case AGL_SHELL_APP_STATE_ACTIVATED:
85                 qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
86                 homescreenHandler->addAppToStack(app_id);
87                 break;
88         default:
89                 break;
90         }
91 }
92
93
94 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
95 static const struct agl_shell_listener shell_listener = {
96         agl_shell_bound_ok,
97         agl_shell_bound_fail,
98         agl_shell_app_state,
99 };
100 #endif
101
102 static void
103 global_add(void *data, struct wl_registry *reg, uint32_t name,
104            const char *interface, uint32_t ver)
105 {
106         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
107
108         if (!shell_data)
109                 return;
110
111         if (strcmp(interface, agl_shell_interface.name) == 0) {
112                 if (ver >= 2) {
113                         shell_data->shell =
114                                 static_cast<struct agl_shell *>(
115                                         wl_registry_bind(reg, name, &agl_shell_interface, MIN(4, ver)));
116 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
117                         agl_shell_add_listener(shell_data->shell, &shell_listener, data);
118 #endif
119                 } else {
120                         shell_data->shell =
121                                 static_cast<struct agl_shell *>(
122                                         wl_registry_bind(reg, name, &agl_shell_interface, 1));
123                 }
124                 shell_data->ver = ver;
125
126         }
127 }
128
129 static void
130 global_remove(void *data, struct wl_registry *reg, uint32_t id)
131 {
132         /* Don't care */
133         (void) data;
134         (void) reg;
135         (void) id;
136 }
137
138 static const struct wl_registry_listener registry_listener = {
139         global_add,
140         global_remove,
141 };
142
143 static struct wl_surface *
144 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
145 {
146         void *surf = native->nativeResourceForWindow("surface", window);
147         return static_cast<struct ::wl_surface *>(surf);
148 }
149
150 static struct wl_output *
151 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
152 {
153         void *output = native->nativeResourceForScreen("output", screen);
154         return static_cast<struct ::wl_output*>(output);
155 }
156
157 static struct wl_display *
158 getWlDisplay(QPlatformNativeInterface *native)
159 {
160        return static_cast<struct wl_display *>(
161                native->nativeResourceForIntegration("display")
162        );
163 }
164
165
166 static void
167 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
168 {
169         struct wl_display *wl;
170         struct wl_registry *registry;
171
172         wl = getWlDisplay(native);
173         registry = wl_display_get_registry(wl);
174
175         wl_registry_add_listener(registry, &registry_listener, shell_data);
176
177         /* Roundtrip to get all globals advertised by the compositor */
178         wl_display_roundtrip(wl);
179         wl_registry_destroy(registry);
180 }
181
182 static struct wl_surface *
183 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
184                  QScreen *screen, QObject **qobj)
185 {
186         QObject *obj = comp->create();
187         obj->setParent(screen);
188
189         QWindow *win = qobject_cast<QWindow *>(obj);
190         *qobj = obj;
191
192         return getWlSurface(native, win);
193 }
194
195 static QScreen *
196 find_screen(const char *screen_name)
197 {
198         QList<QScreen *> screens = qApp->screens();
199         QScreen *found = nullptr;
200         QString qstr_name = QString::fromUtf8(screen_name, -1);
201
202         for (QScreen *xscreen : screens) {
203                 if (qstr_name == xscreen->name()) {
204                         found = xscreen;
205                         break;
206                 }
207         }
208
209         return found;
210 }
211
212 static void
213 load_agl_shell(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
214                struct agl_shell *agl_shell, QScreen *screen)
215 {
216         struct wl_surface *bg;
217         struct wl_output *output;
218         int32_t x, y;
219         int32_t width, height;
220         QObject *qobj_bg;
221         QSize size = screen->size();
222
223         // this incorporates the panels directly, but in doing so, it
224         // would also need to specify an activation area the same area
225         // in order to void overlapping any new activation window
226         QQmlComponent bg_comp(engine, QUrl("qrc:/background_with_panels.qml"));
227         qInfo() << bg_comp.errors();
228
229         bg = create_component(native, &bg_comp, screen, &qobj_bg);
230
231         output = getWlOutput(native, screen);
232
233         qDebug() << "Normal mode - with single surface";
234         qDebug() << "Setting homescreen to screen  " << screen->name();
235         agl_shell_set_background(agl_shell, bg, output);
236
237         // 216 is the width size of the panel
238         x = 0;
239         y = 216;
240
241         width  = size.width();
242         height = size.height() - (2 * y);
243
244         qDebug() << "Using custom rectangle " << width << "x" << height
245                 << "+" << x << "x" << y << " for activation";
246         qDebug() << "Panels should be embedded the background surface";
247
248 #ifdef AGL_SHELL_SET_ACTIVATE_REGION_SINCE_VERSION
249         agl_shell_set_activate_region(agl_shell, output,
250                                       x, y, width, height);
251 #endif
252 }
253
254 static void
255 load_agl_shell_for_ci(QPlatformNativeInterface *native,
256                       QQmlApplicationEngine *engine,
257                       struct agl_shell *agl_shell, QScreen *screen)
258 {
259         struct wl_surface *bg, *top, *bottom;
260         struct wl_output *output;
261         QObject *qobj_bg, *qobj_top, *qobj_bottom;
262
263         QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
264         qInfo() << bg_comp.errors();
265
266         QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
267         qInfo() << top_comp.errors();
268
269         QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
270         qInfo() << bot_comp.errors();
271
272         top = create_component(native, &top_comp, screen, &qobj_top);
273         bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
274         bg = create_component(native, &bg_comp, screen, &qobj_bg);
275
276         /* engine.rootObjects() works only if we had a load() */
277         StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
278         if (statusBar) {
279                 qDebug() << "got statusBar objectname, doing init()";
280                 statusBar->init(engine->rootContext());
281         }
282
283         output = getWlOutput(native, screen);
284
285         qDebug() << "Setting homescreen to screen  " << screen->name();
286
287         agl_shell_set_background(agl_shell, bg, output);
288         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
289         agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
290
291         qDebug() << "CI mode - with multiple surfaces";
292 }
293
294 static void
295 load_agl_shell_app(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
296                    struct agl_shell *agl_shell, const char *screen_name, bool is_demo)
297 {
298         QScreen *screen = nullptr;
299
300         if (!screen_name)
301                 screen = qApp->primaryScreen();
302         else
303                 screen = find_screen(screen_name);
304
305         if (!screen) {
306                 qDebug() << "No outputs present in the system.";
307                 return;
308         }
309
310         if (is_demo) {
311                 load_agl_shell_for_ci(native, engine, agl_shell, screen);
312         } else {
313                 load_agl_shell(native, engine, agl_shell, screen);
314         }
315
316         /* Delay the ready signal until after Qt has done all of its own setup
317          * in a.exec() */
318         QTimer::singleShot(500, [agl_shell](){
319                 qDebug() << "sending ready to compositor";
320                 agl_shell_ready(agl_shell);
321         });
322 }
323
324 int main(int argc, char *argv[])
325 {
326         setenv("QT_QPA_PLATFORM", "wayland", 1);
327         setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
328
329         QGuiApplication app(argc, argv);
330         const char *screen_name;
331         bool is_demo_val = false;
332         bool is_embedded_panels = false;
333         int ret = 0;
334         struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
335
336         QPlatformNativeInterface *native = qApp->platformNativeInterface();
337         screen_name = getenv("HOMESCREEN_START_SCREEN");
338
339         const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
340         if (is_demo && strcmp(is_demo, "1") == 0)
341                 is_demo_val = true;
342
343         const char *embedded_panels = getenv("HOMESCREEN_EMBEDDED_PANELS");
344         if (embedded_panels && strcmp(embedded_panels, "1") == 0)
345                 is_embedded_panels = true;
346
347         QCoreApplication::setOrganizationDomain("LinuxFoundation");
348         QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
349         QCoreApplication::setApplicationName("HomeScreen");
350         QCoreApplication::setApplicationVersion("0.7.0");
351
352         // we need to have an app_id
353         app.setDesktopFileName("homescreen");
354
355         register_agl_shell(native, &shell_data);
356         if (!shell_data.shell) {
357                 fprintf(stderr, "agl_shell extension is not advertised. "
358                         "Are you sure that agl-compositor is running?\n");
359                 exit(EXIT_FAILURE);
360         }
361
362         qDebug() << "agl-shell interface is at version " << shell_data.ver;
363         if (shell_data.ver >= 2) {
364                 while (ret != -1 && shell_data.wait_for_bound) {
365                         ret = wl_display_dispatch(getWlDisplay(native));
366
367                         if (shell_data.wait_for_bound)
368                                 continue;
369                 }
370
371                 if (!shell_data.bound_ok) {
372                         qInfo() << "agl_shell extension already in use by other shell client.";
373                         exit(EXIT_FAILURE);
374                 }
375         }
376
377
378         std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
379         Shell *aglShell = new Shell(agl_shell, &app);
380
381         // Import C++ class to QML
382         qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
383         qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
384
385         ApplicationLauncher *launcher = new ApplicationLauncher();
386         launcher->setCurrent(QStringLiteral("launcher"));
387
388         HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
389         shell_data.homescreenHandler = homescreenHandler;
390
391         QQmlApplicationEngine engine;
392         QQmlContext *context = engine.rootContext();
393
394         context->setContextProperty("homescreenHandler", homescreenHandler);
395         context->setContextProperty("launcher", launcher);
396         context->setContextProperty("weather", new Weather());
397         context->setContextProperty("bluetooth", new Bluetooth(false, context));
398
399         // We add it here even if we don't use it
400         context->setContextProperty("shell", aglShell);
401
402         load_agl_shell_app(native, &engine, shell_data.shell,
403                            screen_name, is_demo_val);
404
405         return app.exec();
406 }