homescreen/src/shell: Cleanup older shell source file not needed
[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() << "Gove event to move " << app_id <<
122                         " to another output " << output_name;
123                 homescreenHandler->processAppStatusEvent(app_id, "started");
124         }
125 }
126
127
128 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
129 static const struct agl_shell_listener shell_listener = {
130         agl_shell_bound_ok,
131         agl_shell_bound_fail,
132         agl_shell_app_state,
133         agl_shell_app_on_output,
134 };
135 #endif
136
137 static void
138 global_add(void *data, struct wl_registry *reg, uint32_t name,
139            const char *interface, uint32_t ver)
140 {
141         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
142
143         if (!shell_data)
144                 return;
145
146         if (strcmp(interface, agl_shell_interface.name) == 0) {
147                 if (ver >= 2) {
148                         shell_data->shell =
149                                 static_cast<struct agl_shell *>(
150                                         wl_registry_bind(reg, name, &agl_shell_interface, MIN(8, ver)));
151 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
152                         agl_shell_add_listener(shell_data->shell, &shell_listener, data);
153 #endif
154                 } else {
155                         shell_data->shell =
156                                 static_cast<struct agl_shell *>(
157                                         wl_registry_bind(reg, name, &agl_shell_interface, 1));
158                 }
159                 shell_data->ver = ver;
160
161         }
162 }
163
164 static void
165 global_remove(void *data, struct wl_registry *reg, uint32_t id)
166 {
167         /* Don't care */
168         (void) data;
169         (void) reg;
170         (void) id;
171 }
172
173 static const struct wl_registry_listener registry_listener = {
174         global_add,
175         global_remove,
176 };
177
178 static struct wl_surface *
179 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
180 {
181         void *surf = native->nativeResourceForWindow("surface", window);
182         return static_cast<struct ::wl_surface *>(surf);
183 }
184
185 static struct wl_output *
186 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
187 {
188         void *output = native->nativeResourceForScreen("output", screen);
189         return static_cast<struct ::wl_output*>(output);
190 }
191
192 static struct wl_display *
193 getWlDisplay(QPlatformNativeInterface *native)
194 {
195        return static_cast<struct wl_display *>(
196                native->nativeResourceForIntegration("display")
197        );
198 }
199
200
201 static void
202 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
203 {
204         struct wl_display *wl;
205         struct wl_registry *registry;
206
207         wl = getWlDisplay(native);
208         registry = wl_display_get_registry(wl);
209
210         wl_registry_add_listener(registry, &registry_listener, shell_data);
211
212         /* Roundtrip to get all globals advertised by the compositor */
213         wl_display_roundtrip(wl);
214         wl_registry_destroy(registry);
215 }
216
217 static struct wl_surface *
218 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
219                  QScreen *screen, QObject **qobj)
220 {
221         QObject *obj = comp->create();
222         obj->setParent(screen);
223
224         QWindow *win = qobject_cast<QWindow *>(obj);
225         *qobj = obj;
226
227         return getWlSurface(native, win);
228 }
229
230
231 static void
232 load_agl_shell(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
233                struct agl_shell *agl_shell, QScreen *screen)
234 {
235         struct wl_surface *bg;
236         struct wl_output *output;
237         int32_t x, y;
238         int32_t width, height;
239         QObject *qobj_bg;
240         QSize size = screen->size();
241
242         // this incorporates the panels directly, but in doing so, it
243         // would also need to specify an activation area the same area
244         // in order to void overlapping any new activation window
245         QQmlComponent bg_comp(engine, QUrl("qrc:/background_with_panels.qml"));
246         qInfo() << bg_comp.errors();
247
248         bg = create_component(native, &bg_comp, screen, &qobj_bg);
249
250         output = getWlOutput(native, screen);
251
252         qDebug() << "Normal mode - with single surface";
253         qDebug() << "Setting homescreen to screen  " << screen->name();
254         agl_shell_set_background(agl_shell, bg, output);
255
256         // 216 is the width size of the panel
257         x = 0;
258         y = 216;
259
260         width  = size.width();
261         height = size.height() - (2 * y);
262
263         qDebug() << "Using custom rectangle " << width << "x" << height
264                 << "+" << x << "x" << y << " for activation";
265         qDebug() << "Panels should be embedded the background surface";
266
267 #ifdef AGL_SHELL_SET_ACTIVATE_REGION_SINCE_VERSION
268         agl_shell_set_activate_region(agl_shell, output,
269                                       x, y, width, height);
270 #endif
271 }
272
273 static void
274 load_agl_shell_for_ci(QPlatformNativeInterface *native,
275                       QQmlApplicationEngine *engine,
276                       struct agl_shell *agl_shell, QScreen *screen)
277 {
278         struct wl_surface *bg, *top, *bottom;
279         struct wl_output *output;
280         QObject *qobj_bg, *qobj_top, *qobj_bottom;
281
282         QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
283         qInfo() << bg_comp.errors();
284
285         QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
286         qInfo() << top_comp.errors();
287
288         QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
289         qInfo() << bot_comp.errors();
290
291         top = create_component(native, &top_comp, screen, &qobj_top);
292         bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
293         bg = create_component(native, &bg_comp, screen, &qobj_bg);
294
295         /* engine.rootObjects() works only if we had a load() */
296         StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
297         if (statusBar) {
298                 qDebug() << "got statusBar objectname, doing init()";
299                 statusBar->init(engine->rootContext());
300         }
301
302         output = getWlOutput(native, screen);
303
304         qDebug() << "Setting homescreen to screen  " << screen->name();
305
306         agl_shell_set_background(agl_shell, bg, output);
307         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
308         agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
309
310         qDebug() << "CI mode - with multiple surfaces";
311 }
312
313 static void
314 app_status_callback(::agl_shell_ipc::AppStateResponse app_response, void *data);
315
316 static void
317 run_in_thread(GrpcClient *client)
318 {
319         grpc::Status status = client->Wait();
320 }
321
322 static void
323 load_agl_shell_app(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
324                    struct shell_data shell_data, const char *screen_name,
325                    bool is_demo, GrpcClient *client)
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                 // handled by HomescreenHandler::processAppStatusEvent
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
422         register_agl_shell(native, &shell_data);
423         if (!shell_data.shell) {
424                 fprintf(stderr, "agl_shell extension is not advertised. "
425                         "Are you sure that agl-compositor is running?\n");
426                 exit(EXIT_FAILURE);
427         }
428
429         qDebug() << "agl-shell interface is at version " << shell_data.ver;
430         if (shell_data.ver >= 2) {
431                 while (ret != -1 && shell_data.wait_for_bound) {
432                         ret = wl_display_dispatch(getWlDisplay(native));
433
434                         if (shell_data.wait_for_bound)
435                                 continue;
436                 }
437
438                 if (!shell_data.bound_ok) {
439                         qInfo() << "agl_shell extension already in use by other shell client.";
440                         exit(EXIT_FAILURE);
441                 }
442         }
443
444
445         std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
446
447         GrpcClient *client = new GrpcClient();
448         // create a new thread to listner for gRPC events
449         std::thread th = std::thread(run_in_thread, client);
450
451         // Import C++ class to QML
452         qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
453         qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
454
455         ApplicationLauncher *launcher = new ApplicationLauncher();
456         launcher->setCurrent(QStringLiteral("launcher"));
457
458         HomescreenHandler* homescreenHandler = new HomescreenHandler(launcher);
459         shell_data.homescreenHandler = homescreenHandler;
460         shell_data.homescreenHandler->setGrpcClient(client);
461
462         client->AppStatusState(app_status_callback, homescreenHandler);
463
464         QQmlApplicationEngine engine;
465         QQmlContext *context = engine.rootContext();
466
467         context->setContextProperty("homescreenHandler", homescreenHandler);
468         context->setContextProperty("launcher", launcher);
469         context->setContextProperty("weather", new Weather());
470         context->setContextProperty("bluetooth", new Bluetooth(false, context));
471
472         load_agl_shell_app(native, &engine, shell_data,
473                            screen_name, is_demo_val, client);
474
475         return app.exec();
476 }