meson.build: Use 0.0.24 as the agl compositor version
[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  * Copyright (c) 2023 Collabora, Ltd.
7  */
8
9 #include <QGuiApplication>
10 #include <QCommandLineParser>
11 #include <QtCore/QUrlQuery>
12 #include <QtGui/QGuiApplication>
13 #include <QtQml/QQmlApplicationEngine>
14 #include <QtQml/QQmlContext>
15 #include <QtQml/QQmlComponent>
16 #include <QtQml/qqml.h>
17 #include <QQuickWindow>
18 #include <QTimer>
19 #include <QScreen>
20
21 #include <weather.h>
22 #include <bluetooth.h>
23
24 #include "applicationlauncher.h"
25 #include "statusbarmodel.h"
26 #include "mastervolume.h"
27 #include "homescreenhandler.h"
28 #include "hmi-debug.h"
29
30 // meson will define these
31 #include QT_QPA_HEADER
32 #include <wayland-client.h>
33
34 #include "agl-shell-client-protocol.h"
35
36 #include <thread>
37 #include "AglShellGrpcClient.h"
38
39 #ifndef MIN
40 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
41 #endif
42
43 QScreen *
44 find_screen(const char *screen_name)
45 {
46         QList<QScreen *> screens = qApp->screens();
47         QScreen *found = nullptr;
48         QString qstr_name = QString::fromUtf8(screen_name, -1);
49
50         for (QScreen *xscreen : screens) {
51                 if (qstr_name == xscreen->name()) {
52                         found = xscreen;
53                         break;
54                 }
55         }
56
57         return found;
58 }
59
60 struct shell_data {
61         struct agl_shell *shell;
62         HomescreenHandler *homescreenHandler;
63         bool wait_for_bound;
64         bool bound_ok;
65         int ver;
66 };
67
68 static void
69 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
70 {
71         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
72         shell_data->wait_for_bound = false;
73
74         shell_data->bound_ok = true;
75 }
76
77 static void
78 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
79 {
80         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
81         shell_data->wait_for_bound = false;
82
83         shell_data->bound_ok = false;
84 }
85
86 static void
87 agl_shell_app_state(void *data, struct agl_shell *agl_shell,
88                 const char *app_id, uint32_t state)
89 {
90         /* unused */
91 }
92
93 static void
94 agl_shell_app_on_output(void *data, struct agl_shell *agl_shell,
95                 const char *app_id, const char *output_name)
96 {
97         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
98         HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
99
100         if (!homescreenHandler)
101                 return;
102
103         // a couple of use-cases, if there is no app_id in the app_list then it
104         // means this is a request to map the application, from the start to a
105         // different output that the default one. We'd get an
106         // AGL_SHELL_APP_STATE_STARTED which will handle activation.
107         //
108         // if there's an app_id then it means we might have gotten an event to
109         // move the application to another output; so we'd need to process it
110         // by explicitly calling processAppStatusEvent() which would ultimately
111         // activate the application on other output. We'd have to pick-up the
112         // last activated window and activate the default output.
113         //
114         // finally if the outputs are identical probably that's an user-error -
115         // but the compositor won't activate it again, so we don't handle that.
116         std::pair new_pending_app = std::pair(QString(app_id),
117                                               QString(output_name));
118         homescreenHandler->pending_app_list.push_back(new_pending_app);
119
120         if (homescreenHandler->apps_stack.contains(QString(app_id))) {
121                 qDebug() << "Got event to move " << app_id <<
122                         " to another output " << output_name;
123
124                 homescreenHandler->processAppStatusEvent(app_id, "started");
125         }
126 }
127
128
129 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
130 static const struct agl_shell_listener shell_listener = {
131         agl_shell_bound_ok,
132         agl_shell_bound_fail,
133         agl_shell_app_state,
134         agl_shell_app_on_output,
135 };
136 #endif
137
138 static void
139 global_add(void *data, struct wl_registry *reg, uint32_t name,
140            const char *interface, uint32_t ver)
141 {
142         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
143
144         if (!shell_data)
145                 return;
146
147         if (strcmp(interface, agl_shell_interface.name) == 0) {
148                 if (ver >= 2) {
149                         shell_data->shell =
150                                 static_cast<struct agl_shell *>(
151                                         wl_registry_bind(reg, name, &agl_shell_interface, MIN(8, ver)));
152 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
153                         agl_shell_add_listener(shell_data->shell, &shell_listener, data);
154 #endif
155                 } else {
156                         shell_data->shell =
157                                 static_cast<struct agl_shell *>(
158                                         wl_registry_bind(reg, name, &agl_shell_interface, 1));
159                 }
160                 shell_data->ver = ver;
161
162         }
163 }
164
165 static void
166 global_remove(void *data, struct wl_registry *reg, uint32_t id)
167 {
168         /* Don't care */
169         (void) data;
170         (void) reg;
171         (void) id;
172 }
173
174 static const struct wl_registry_listener registry_listener = {
175         global_add,
176         global_remove,
177 };
178
179 static struct wl_surface *
180 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
181 {
182         void *surf = native->nativeResourceForWindow("surface", window);
183         return static_cast<struct ::wl_surface *>(surf);
184 }
185
186 static struct wl_output *
187 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
188 {
189         void *output = native->nativeResourceForScreen("output", screen);
190         return static_cast<struct ::wl_output*>(output);
191 }
192
193 static struct wl_display *
194 getWlDisplay(QPlatformNativeInterface *native)
195 {
196        return static_cast<struct wl_display *>(
197                native->nativeResourceForIntegration("display")
198        );
199 }
200
201
202 static void
203 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
204 {
205         struct wl_display *wl;
206         struct wl_registry *registry;
207
208         wl = getWlDisplay(native);
209         registry = wl_display_get_registry(wl);
210
211         wl_registry_add_listener(registry, &registry_listener, shell_data);
212
213         /* Roundtrip to get all globals advertised by the compositor */
214         wl_display_roundtrip(wl);
215         wl_registry_destroy(registry);
216 }
217
218 static struct wl_surface *
219 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
220                  QScreen *screen, QObject **qobj)
221 {
222         QObject *obj = comp->create();
223         obj->setParent(screen);
224
225         QWindow *win = qobject_cast<QWindow *>(obj);
226         *qobj = obj;
227
228         return getWlSurface(native, win);
229 }
230
231
232 static void
233 load_agl_shell(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
234                struct agl_shell *agl_shell, QScreen *screen)
235 {
236         struct wl_surface *bg;
237         struct wl_output *output;
238         int32_t x, y;
239         int32_t width, height;
240         QObject *qobj_bg;
241         QSize size = screen->size();
242
243         // this incorporates the panels directly, but in doing so, it
244         // would also need to specify an activation area the same area
245         // in order to void overlapping any new activation window
246         QQmlComponent bg_comp(engine, QUrl("qrc:/background_with_panels.qml"));
247         qInfo() << bg_comp.errors();
248
249         bg = create_component(native, &bg_comp, screen, &qobj_bg);
250
251         output = getWlOutput(native, screen);
252
253         qDebug() << "Normal mode - with single surface";
254         qDebug() << "Setting homescreen to screen  " << screen->name();
255         agl_shell_set_background(agl_shell, bg, output);
256
257         // 216 is the width size of the panel
258         x = 0;
259         y = 216;
260
261         width  = size.width();
262         height = size.height() - (2 * y);
263
264         qDebug() << "Using custom rectangle " << width << "x" << height
265                 << "+" << x << "x" << y << " for activation";
266         qDebug() << "Panels should be embedded the background surface";
267
268 #ifdef AGL_SHELL_SET_ACTIVATE_REGION_SINCE_VERSION
269         agl_shell_set_activate_region(agl_shell, output,
270                                       x, y, width, height);
271 #endif
272 }
273
274 static void
275 load_agl_shell_for_ci(QPlatformNativeInterface *native,
276                       QQmlApplicationEngine *engine,
277                       struct agl_shell *agl_shell, QScreen *screen)
278 {
279         struct wl_surface *bg, *top, *bottom;
280         struct wl_output *output;
281         QObject *qobj_bg, *qobj_top, *qobj_bottom;
282
283         QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
284         qInfo() << bg_comp.errors();
285
286         QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
287         qInfo() << top_comp.errors();
288
289         QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
290         qInfo() << bot_comp.errors();
291
292         top = create_component(native, &top_comp, screen, &qobj_top);
293         bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
294         bg = create_component(native, &bg_comp, screen, &qobj_bg);
295
296         /* engine.rootObjects() works only if we had a load() */
297         StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
298         if (statusBar) {
299                 qDebug() << "got statusBar objectname, doing init()";
300                 statusBar->init(engine->rootContext());
301         }
302
303         output = getWlOutput(native, screen);
304
305         qDebug() << "Setting homescreen to screen  " << screen->name();
306
307         agl_shell_set_background(agl_shell, bg, output);
308         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
309         agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
310
311         qDebug() << "CI mode - with multiple surfaces";
312 }
313
314 static void
315 app_status_callback(::agl_shell_ipc::AppStateResponse app_response, void *data);
316
317 static void
318 run_in_thread(GrpcClient *client)
319 {
320         grpc::Status status = client->Wait();
321 }
322
323 static void
324 load_agl_shell_app(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
325                    struct shell_data shell_data, const char *screen_name, bool is_demo)
326 {
327         QScreen *screen = nullptr;
328         HomescreenHandler *homescreenHandler = shell_data.homescreenHandler;
329
330         if (!screen_name)
331                 screen = qApp->primaryScreen();
332         else
333                 screen = find_screen(screen_name);
334
335         if (!screen) {
336                 qDebug() << "No outputs present in the system.";
337                 return;
338         }
339
340         if (is_demo) {
341                 load_agl_shell_for_ci(native, engine, shell_data.shell, screen);
342         } else {
343                 load_agl_shell(native, engine, shell_data.shell, screen);
344         }
345
346         /* Delay the ready signal until after Qt has done all of its own setup
347          * in a.exec() */
348         QTimer::singleShot(500, [shell_data](){
349                 qDebug() << "sending ready to compositor";
350                 agl_shell_ready(shell_data.shell);
351         });
352 }
353
354 static void
355 app_status_callback(::agl_shell_ipc::AppStateResponse app_response, void *data)
356 {
357         HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
358
359         if (!homescreenHandler) {
360                 return;
361         }
362
363         auto app_id = QString(app_response.app_id().c_str());
364         auto state = app_response.state();
365
366         qDebug() << "appstateresponse: app_id " << app_id << "state " << state;
367
368         switch (state) {
369         case AGL_SHELL_APP_STATE_STARTED:
370                 qDebug() << "Got AGL_SHELL_APP_STATE_STARTED for app_id " << app_id;
371                 homescreenHandler->processAppStatusEvent(app_id, "started");
372                 break;
373         case AGL_SHELL_APP_STATE_TERMINATED:
374                 qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
375                 homescreenHandler->processAppStatusEvent(app_id, "terminated");
376                 break;
377         case AGL_SHELL_APP_STATE_ACTIVATED:
378                 qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
379                 homescreenHandler->addAppToStack(app_id);
380                 break;
381         case AGL_SHELL_APP_STATE_DEACTIVATED:
382                 qDebug() << "Got AGL_SHELL_APP_STATE_DEACTIVATED for app_id " << app_id;
383                 homescreenHandler->processAppStatusEvent(app_id, "deactivated");
384                 break;
385         default:
386                 break;
387         }
388 }
389
390 int main(int argc, char *argv[])
391 {
392         setenv("QT_QPA_PLATFORM", "wayland", 1);
393         setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
394
395         QGuiApplication app(argc, argv);
396         const char *screen_name;
397         bool is_demo_val = false;
398         bool is_embedded_panels = false;
399         int ret = 0;
400         struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
401
402         QPlatformNativeInterface *native = qApp->platformNativeInterface();
403         screen_name = getenv("HOMESCREEN_START_SCREEN");
404
405         const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
406         if (is_demo && strcmp(is_demo, "1") == 0)
407                 is_demo_val = true;
408
409         const char *embedded_panels = getenv("HOMESCREEN_EMBEDDED_PANELS");
410         if (embedded_panels && strcmp(embedded_panels, "1") == 0)
411                 is_embedded_panels = true;
412
413         QCoreApplication::setOrganizationDomain("LinuxFoundation");
414         QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
415         QCoreApplication::setApplicationName("HomeScreen");
416         QCoreApplication::setApplicationVersion("0.7.0");
417
418         // we need to have an app_id
419         app.setDesktopFileName("homescreen");
420
421         GrpcClient *client = new GrpcClient();
422         // create a new thread to listner for gRPC events
423         std::thread th = std::thread(run_in_thread, client);
424
425         register_agl_shell(native, &shell_data);
426         if (!shell_data.shell) {
427                 fprintf(stderr, "agl_shell extension is not advertised. "
428                         "Are you sure that agl-compositor is running?\n");
429                 exit(EXIT_FAILURE);
430         }
431
432         qDebug() << "agl-shell interface is at version " << shell_data.ver;
433         if (shell_data.ver >= 2) {
434                 while (ret != -1 && shell_data.wait_for_bound) {
435                         ret = wl_display_dispatch(getWlDisplay(native));
436
437                         if (shell_data.wait_for_bound)
438                                 continue;
439                 }
440
441                 if (!shell_data.bound_ok) {
442                         qInfo() << "agl_shell extension already in use by other shell client.";
443                         exit(EXIT_FAILURE);
444                 }
445         }
446
447
448         std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
449
450         // Import C++ class to QML
451         qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
452         qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
453
454         ApplicationLauncher *launcher = new ApplicationLauncher();
455         launcher->setCurrent(QStringLiteral("launcher"));
456
457         HomescreenHandler* homescreenHandler = new HomescreenHandler(launcher);
458         shell_data.homescreenHandler = homescreenHandler;
459         shell_data.homescreenHandler->setGrpcClient(client);
460
461         // blocks until we're sure connected with the server
462         HMI_DEBUG("HomescreenHandler", "Checking if connected to the gRPC server...");
463         client->WaitForConnected(500, 10);
464         client->AppStatusState(app_status_callback, homescreenHandler);
465
466         QQmlApplicationEngine engine;
467         QQmlContext *context = engine.rootContext();
468
469         context->setContextProperty("homescreenHandler", homescreenHandler);
470         context->setContextProperty("launcher", launcher);
471         context->setContextProperty("weather", new Weather());
472         context->setContextProperty("bluetooth", new Bluetooth(false, context));
473
474         load_agl_shell_app(native, &engine, shell_data,
475                            screen_name, is_demo_val);
476
477         return app.exec();
478 }