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