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