homescreen: Add support for v3 of agl-shell protocol
[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 #include <qpa/qplatformnativeinterface.h>
29 #include <wayland-client.h>
30
31 #include "wayland-agl-shell-client-protocol.h"
32 #include "shell.h"
33
34 #ifndef MIN
35 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
36 #endif
37
38 struct shell_data {
39         struct agl_shell *shell;
40         HomescreenHandler *homescreenHandler;
41         bool wait_for_bound;
42         bool bound_ok;
43         int ver;
44 };
45
46 static void
47 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
48 {
49         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
50         shell_data->wait_for_bound = false;
51
52         shell_data->bound_ok = true;
53 }
54
55 static void
56 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
57 {
58         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
59         shell_data->wait_for_bound = false;
60
61         shell_data->bound_ok = false;
62 }
63
64 static void
65 agl_shell_app_state(void *data, struct agl_shell *agl_shell,
66                 const char *app_id, uint32_t state)
67 {
68         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
69         HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
70
71         if (!homescreenHandler)
72                 return;
73
74         switch (state) {
75         case AGL_SHELL_APP_STATE_STARTED:
76                 qDebug() << "Got AGL_SHELL_APP_STATE_STARTED for app_id " << app_id;
77                 homescreenHandler->processAppStatusEvent(app_id, "started");
78                 break;
79         case AGL_SHELL_APP_STATE_TERMINATED:
80                 qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
81                 // handled by HomescreenHandler::processAppStatusEvent
82                 break;
83         case AGL_SHELL_APP_STATE_ACTIVATED:
84                 qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
85                 homescreenHandler->addAppToStack(app_id);
86                 break;
87         default:
88                 break;
89         }
90 }
91
92
93 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
94 static const struct agl_shell_listener shell_listener = {
95         agl_shell_bound_ok,
96         agl_shell_bound_fail,
97         agl_shell_app_state,
98 };
99 #endif
100
101 static void
102 global_add(void *data, struct wl_registry *reg, uint32_t name,
103            const char *interface, uint32_t ver)
104 {
105         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
106
107         if (!shell_data)
108                 return;
109
110         if (strcmp(interface, agl_shell_interface.name) == 0) {
111                 if (ver >= 2) {
112                         shell_data->shell =
113                                 static_cast<struct agl_shell *>(
114                                         wl_registry_bind(reg, name, &agl_shell_interface, MIN(3, ver)));
115 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
116                         agl_shell_add_listener(shell_data->shell, &shell_listener, data);
117 #endif
118                 } else {
119                         shell_data->shell =
120                                 static_cast<struct agl_shell *>(
121                                         wl_registry_bind(reg, name, &agl_shell_interface, 1));
122                 }
123                 shell_data->ver = ver;
124
125         }
126 }
127
128 static void
129 global_remove(void *data, struct wl_registry *reg, uint32_t id)
130 {
131         /* Don't care */
132         (void) data;
133         (void) reg;
134         (void) id;
135 }
136
137 static const struct wl_registry_listener registry_listener = {
138         global_add,
139         global_remove,
140 };
141
142 static struct wl_surface *
143 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
144 {
145         void *surf = native->nativeResourceForWindow("surface", window);
146         return static_cast<struct ::wl_surface *>(surf);
147 }
148
149 static struct wl_output *
150 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
151 {
152         void *output = native->nativeResourceForScreen("output", screen);
153         return static_cast<struct ::wl_output*>(output);
154 }
155
156 static struct wl_display *
157 getWlDisplay(QPlatformNativeInterface *native)
158 {
159        return static_cast<struct wl_display *>(
160                native->nativeResourceForIntegration("display")
161        );
162 }
163
164
165 static void
166 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
167 {
168         struct wl_display *wl;
169         struct wl_registry *registry;
170
171         wl = getWlDisplay(native);
172         registry = wl_display_get_registry(wl);
173
174         wl_registry_add_listener(registry, &registry_listener, shell_data);
175
176         /* Roundtrip to get all globals advertised by the compositor */
177         wl_display_roundtrip(wl);
178         wl_registry_destroy(registry);
179 }
180
181 static struct wl_surface *
182 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
183                  QScreen *screen, QObject **qobj)
184 {
185         QObject *obj = comp->create();
186         obj->setParent(screen);
187
188         QWindow *win = qobject_cast<QWindow *>(obj);
189         *qobj = obj;
190
191         return getWlSurface(native, win);
192 }
193
194 static QScreen *
195 find_screen(const char *screen_name)
196 {
197         QList<QScreen *> screens = qApp->screens();
198         QScreen *found = nullptr;
199         QString qstr_name = QString::fromUtf8(screen_name, -1);
200
201         for (QScreen *xscreen : screens) {
202                 if (qstr_name == xscreen->name()) {
203                         found = xscreen;
204                         break;
205                 }
206         }
207
208         return found;
209 }
210
211 static void
212 load_agl_shell_app(QPlatformNativeInterface *native,
213                    QQmlApplicationEngine *engine,
214                    struct agl_shell *agl_shell,
215                    const char *screen_name,
216                     bool is_demo)
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) {
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         } else {
237                 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
238                 qInfo() << bg_comp.errors();
239
240                 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
241                 qInfo() << top_comp.errors();
242
243                 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
244                 qInfo() << bot_comp.errors();
245
246                 top = create_component(native, &top_comp, screen, &qobj_top);
247                 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
248                 bg = create_component(native, &bg_comp, screen, &qobj_bg);
249         }
250
251         if (!screen_name)
252                 screen = qApp->primaryScreen();
253         else
254                 screen = find_screen(screen_name);
255
256         if (!screen) {
257                 qDebug() << "No outputs present in the system.";
258                 return;
259         }
260
261         qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
262                 "first screen " << qApp->screens().first()->name();
263         output = getWlOutput(native, screen);
264
265         /* engine.rootObjects() works only if we had a load() */
266         StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
267         if (statusBar) {
268                 qDebug() << "got statusBar objectname, doing init()";
269                 statusBar->init(engine->rootContext());
270         }
271
272         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
273         agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
274         qDebug() << "Setting homescreen to screen  " << screen->name();
275
276         agl_shell_set_background(agl_shell, bg, output);
277
278         /* Delay the ready signal until after Qt has done all of its own setup
279          * in a.exec() */
280         QTimer::singleShot(500, [agl_shell](){
281                 agl_shell_ready(agl_shell);
282         });
283 }
284
285 int main(int argc, char *argv[])
286 {
287         setenv("QT_QPA_PLATFORM", "wayland", 1);
288         setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
289
290         QGuiApplication app(argc, argv);
291         const char *screen_name;
292         bool is_demo_val = false;
293         int ret = 0;
294         struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
295
296         QPlatformNativeInterface *native = qApp->platformNativeInterface();
297         screen_name = getenv("HOMESCREEN_START_SCREEN");
298
299         const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
300         if (is_demo && strcmp(is_demo, "1") == 0)
301                 is_demo_val = true;
302
303         QCoreApplication::setOrganizationDomain("LinuxFoundation");
304         QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
305         QCoreApplication::setApplicationName("HomeScreen");
306         QCoreApplication::setApplicationVersion("0.7.0");
307
308         // we need to have an app_id
309         app.setDesktopFileName("homescreen");
310
311         register_agl_shell(native, &shell_data);
312         if (!shell_data.shell) {
313                 fprintf(stderr, "agl_shell extension is not advertised. "
314                         "Are you sure that agl-compositor is running?\n");
315                 exit(EXIT_FAILURE);
316         }
317
318         qDebug() << "agl-shell interface is at version " << shell_data.ver;
319         if (shell_data.ver >= 2) {
320                 while (ret != -1 && shell_data.wait_for_bound) {
321                         ret = wl_display_dispatch(getWlDisplay(native));
322
323                         if (shell_data.wait_for_bound)
324                                 continue;
325                 }
326
327                 if (!shell_data.bound_ok) {
328                         qInfo() << "agl_shell extension already in use by other shell client.";
329                         exit(EXIT_FAILURE);
330                 }
331         }
332
333
334         std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
335         Shell *aglShell = new Shell(agl_shell, &app);
336
337         // Import C++ class to QML
338         qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
339         qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
340
341         ApplicationLauncher *launcher = new ApplicationLauncher();
342         launcher->setCurrent(QStringLiteral("launcher"));
343
344         HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
345         shell_data.homescreenHandler = homescreenHandler;
346
347         QQmlApplicationEngine engine;
348         QQmlContext *context = engine.rootContext();
349
350         context->setContextProperty("homescreenHandler", homescreenHandler);
351         context->setContextProperty("launcher", launcher);
352         context->setContextProperty("weather", new Weather());
353         context->setContextProperty("bluetooth", new Bluetooth(false, context));
354
355         // We add it here even if we don't use it
356         context->setContextProperty("shell", aglShell);
357
358         // Instead of loading main.qml we load one-by-one each of the QMLs,
359         // divided now between several surfaces: panels, background.
360         load_agl_shell_app(native, &engine, shell_data.shell, screen_name, is_demo_val);
361
362         return app.exec();
363 }