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