homescreen: Add support for defining an activation area
[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_app(QPlatformNativeInterface *native,
214                    QQmlApplicationEngine *engine,
215                    struct agl_shell *agl_shell,
216                    const char *screen_name, bool is_demo, bool embedded_panels)
217 {
218         struct wl_surface *bg, *top, *bottom;
219         struct wl_output *output;
220         QObject *qobj_bg, *qobj_top, *qobj_bottom;
221         QScreen *screen = nullptr;
222
223         if (is_demo && !embedded_panels) {
224                 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
225                 qInfo() << bg_comp.errors();
226
227                 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
228                 qInfo() << top_comp.errors();
229
230                 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
231                 qInfo() << bot_comp.errors();
232
233                 top = create_component(native, &top_comp, screen, &qobj_top);
234                 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
235                 bg = create_component(native, &bg_comp, screen, &qobj_bg);
236
237                 /* engine.rootObjects() works only if we had a load() */
238                 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
239                 if (statusBar) {
240                         qDebug() << "got statusBar objectname, doing init()";
241                         statusBar->init(engine->rootContext());
242                 }
243
244                 qDebug() << "init debug mode";
245         } else if (!embedded_panels) {
246                 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
247                 qInfo() << bg_comp.errors();
248
249                 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
250                 qInfo() << top_comp.errors();
251
252                 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
253                 qInfo() << bot_comp.errors();
254
255                 top = create_component(native, &top_comp, screen, &qobj_top);
256                 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
257                 bg = create_component(native, &bg_comp, screen, &qobj_bg);
258
259                 /* engine.rootObjects() works only if we had a load() */
260                 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
261                 if (statusBar) {
262                         qDebug() << "got statusBar objectname, doing init()";
263                         statusBar->init(engine->rootContext());
264                 }
265
266                 qDebug() << "init normal mode";
267         } else {
268                 // this incorporates the panels directly, but in doing so, it
269                 // would also need to specify an activation area the same area
270                 // in order to void overlapping any new activation window
271                 QQmlComponent bg_comp(engine, QUrl("qrc:/background_with_panels.qml"));
272                 qInfo() << bg_comp.errors();
273
274                 bg = create_component(native, &bg_comp, screen, &qobj_bg);
275                 qDebug() << "init embedded panels mode";
276         }
277
278         if (!screen_name)
279                 screen = qApp->primaryScreen();
280         else
281                 screen = find_screen(screen_name);
282
283         if (!screen) {
284                 qDebug() << "No outputs present in the system.";
285                 return;
286         }
287
288         qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
289                 "first screen " << qApp->screens().first()->name();
290         output = getWlOutput(native, screen);
291
292         qDebug() << "Setting homescreen to screen  " << screen->name();
293         agl_shell_set_background(agl_shell, bg, output);
294
295         if (embedded_panels) {
296                 int32_t x, y;
297                 int32_t width, height;
298                 QSize size = screen->size();
299
300                 x = 0;
301                 y = 216;
302
303                 width   = size.width();
304                 height = size.height() - (2 * y);
305
306                 qDebug() << "Using custom rectangle " << width << "x" << height
307                         << "+" << x << "x" << y << " for activation";
308                 qDebug() << "Panels should be embedded the background surface";
309
310 #ifdef AGL_SHELL_SET_ACTIVATE_REGION_SINCE_VERSION
311                 agl_shell_set_activate_region(agl_shell, output,
312                                               x, y, width, height);
313 #endif
314
315         } else {
316                 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
317                 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
318                 qDebug() << "Setting regular panels";
319         }
320
321         /* Delay the ready signal until after Qt has done all of its own setup
322          * in a.exec() */
323         QTimer::singleShot(500, [agl_shell](){
324                 qDebug() << "sending ready to compositor";
325                 agl_shell_ready(agl_shell);
326         });
327 }
328
329 int main(int argc, char *argv[])
330 {
331         setenv("QT_QPA_PLATFORM", "wayland", 1);
332         setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
333
334         QGuiApplication app(argc, argv);
335         const char *screen_name;
336         bool is_demo_val = false;
337         bool is_embedded_panels = false;
338         int ret = 0;
339         struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
340
341         QPlatformNativeInterface *native = qApp->platformNativeInterface();
342         screen_name = getenv("HOMESCREEN_START_SCREEN");
343
344         const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
345         if (is_demo && strcmp(is_demo, "1") == 0)
346                 is_demo_val = true;
347
348         const char *embedded_panels = getenv("HOMESCREEN_EMBEDDED_PANELS");
349         if (embedded_panels && strcmp(embedded_panels, "1") == 0)
350                 is_embedded_panels = true;
351
352         QCoreApplication::setOrganizationDomain("LinuxFoundation");
353         QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
354         QCoreApplication::setApplicationName("HomeScreen");
355         QCoreApplication::setApplicationVersion("0.7.0");
356
357         // we need to have an app_id
358         app.setDesktopFileName("homescreen");
359
360         register_agl_shell(native, &shell_data);
361         if (!shell_data.shell) {
362                 fprintf(stderr, "agl_shell extension is not advertised. "
363                         "Are you sure that agl-compositor is running?\n");
364                 exit(EXIT_FAILURE);
365         }
366
367         qDebug() << "agl-shell interface is at version " << shell_data.ver;
368         if (shell_data.ver >= 2) {
369                 while (ret != -1 && shell_data.wait_for_bound) {
370                         ret = wl_display_dispatch(getWlDisplay(native));
371
372                         if (shell_data.wait_for_bound)
373                                 continue;
374                 }
375
376                 if (!shell_data.bound_ok) {
377                         qInfo() << "agl_shell extension already in use by other shell client.";
378                         exit(EXIT_FAILURE);
379                 }
380         }
381
382
383         std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
384         Shell *aglShell = new Shell(agl_shell, &app);
385
386         // Import C++ class to QML
387         qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
388         qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
389
390         ApplicationLauncher *launcher = new ApplicationLauncher();
391         launcher->setCurrent(QStringLiteral("launcher"));
392
393         HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
394         shell_data.homescreenHandler = homescreenHandler;
395
396         QQmlApplicationEngine engine;
397         QQmlContext *context = engine.rootContext();
398
399         context->setContextProperty("homescreenHandler", homescreenHandler);
400         context->setContextProperty("launcher", launcher);
401         context->setContextProperty("weather", new Weather());
402         context->setContextProperty("bluetooth", new Bluetooth(false, context));
403
404         // We add it here even if we don't use it
405         context->setContextProperty("shell", aglShell);
406
407         // Instead of loading main.qml we load one-by-one each of the QMLs,
408         // divided now between several surfaces: panels, background.
409         load_agl_shell_app(native, &engine, shell_data.shell,
410                            screen_name, is_demo_val, is_embedded_panels);
411
412         return app.exec();
413 }