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