1 // SPDX-License-Identifier: Apache-2.0
3 * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
4 * Copyright (c) 2017, 2018 TOYOTA MOTOR CORPORATION
5 * Copyright (c) 2022 Konsulko Group
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>
20 #include <bluetooth.h>
22 #include "applicationlauncher.h"
23 #include "statusbarmodel.h"
24 #include "mastervolume.h"
25 #include "homescreenhandler.h"
26 #include "hmi-debug.h"
28 #include <qpa/qplatformnativeinterface.h>
29 #include <wayland-client.h>
31 #include "wayland-agl-shell-client-protocol.h"
32 #include "wayland-agl-shell-desktop-client-protocol.h"
36 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
40 struct agl_shell *shell;
41 struct agl_shell_desktop *shell_desktop;
48 agl_shell_desktop_application(void *data,
49 struct agl_shell_desktop *agl_shell_desktop,
52 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
54 if (homescreenHandler)
55 homescreenHandler->addAppToStack(app_id);
59 agl_shell_desktop_state_app(void *data,
60 struct agl_shell_desktop *agl_shell_desktop,
66 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
68 if (homescreenHandler && state == AGL_SHELL_DESKTOP_APP_STATE_DESTROYED)
69 homescreenHandler->deactivateApp(app_id);
73 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
75 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
76 shell_data->wait_for_bound = false;
78 shell_data->bound_ok = true;
82 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
84 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
85 shell_data->wait_for_bound = false;
87 shell_data->bound_ok = false;
90 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
91 static const struct agl_shell_listener shell_listener = {
97 static const struct agl_shell_desktop_listener shell_desktop_listener = {
98 agl_shell_desktop_application,
99 agl_shell_desktop_state_app
103 global_add(void *data, struct wl_registry *reg, uint32_t name,
104 const char *interface, uint32_t ver)
106 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
111 if (strcmp(interface, agl_shell_interface.name) == 0) {
114 static_cast<struct agl_shell *>(
115 wl_registry_bind(reg, name, &agl_shell_interface, MIN(2, ver)));
116 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
117 agl_shell_add_listener(shell_data->shell, &shell_listener, data);
121 static_cast<struct agl_shell *>(
122 wl_registry_bind(reg, name, &agl_shell_interface, 1));
124 shell_data->ver = ver;
128 if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
129 shell_data->shell_desktop = static_cast<struct agl_shell_desktop *>(
130 wl_registry_bind(reg, name, &agl_shell_desktop_interface, 1)
136 global_remove(void *data, struct wl_registry *reg, uint32_t id)
144 static const struct wl_registry_listener registry_listener = {
149 static struct wl_surface *
150 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
152 void *surf = native->nativeResourceForWindow("surface", window);
153 return static_cast<struct ::wl_surface *>(surf);
156 static struct wl_output *
157 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
159 void *output = native->nativeResourceForScreen("output", screen);
160 return static_cast<struct ::wl_output*>(output);
163 static struct wl_display *
164 getWlDisplay(QPlatformNativeInterface *native)
166 return static_cast<struct wl_display *>(
167 native->nativeResourceForIntegration("display")
173 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
175 struct wl_display *wl;
176 struct wl_registry *registry;
178 wl = getWlDisplay(native);
179 registry = wl_display_get_registry(wl);
181 wl_registry_add_listener(registry, ®istry_listener, shell_data);
183 /* Roundtrip to get all globals advertised by the compositor */
184 wl_display_roundtrip(wl);
185 wl_registry_destroy(registry);
188 static struct wl_surface *
189 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
190 QScreen *screen, QObject **qobj)
192 QObject *obj = comp->create();
193 obj->setParent(screen);
195 QWindow *win = qobject_cast<QWindow *>(obj);
198 return getWlSurface(native, win);
202 find_screen(const char *screen_name)
204 QList<QScreen *> screens = qApp->screens();
205 QScreen *found = nullptr;
206 QString qstr_name = QString::fromUtf8(screen_name, -1);
208 for (QScreen *xscreen : screens) {
209 if (qstr_name == xscreen->name()) {
219 load_agl_shell_app(QPlatformNativeInterface *native,
220 QQmlApplicationEngine *engine,
221 struct agl_shell *agl_shell,
222 const char *screen_name,
225 struct wl_surface *bg, *top, *bottom;
226 struct wl_output *output;
227 QObject *qobj_bg, *qobj_top, *qobj_bottom;
228 QScreen *screen = nullptr;
231 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
232 qInfo() << bg_comp.errors();
234 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
235 qInfo() << top_comp.errors();
237 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
238 qInfo() << bot_comp.errors();
240 top = create_component(native, &top_comp, screen, &qobj_top);
241 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
242 bg = create_component(native, &bg_comp, screen, &qobj_bg);
244 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
245 qInfo() << bg_comp.errors();
247 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
248 qInfo() << top_comp.errors();
250 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
251 qInfo() << bot_comp.errors();
253 top = create_component(native, &top_comp, screen, &qobj_top);
254 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
255 bg = create_component(native, &bg_comp, screen, &qobj_bg);
259 screen = qApp->primaryScreen();
261 screen = find_screen(screen_name);
264 qDebug() << "No outputs present in the system.";
268 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
269 "first screen " << qApp->screens().first()->name();
270 output = getWlOutput(native, screen);
272 /* engine.rootObjects() works only if we had a load() */
273 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
275 qDebug() << "got statusBar objectname, doing init()";
276 statusBar->init(engine->rootContext());
279 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
280 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
281 qDebug() << "Setting homescreen to screen " << screen->name();
283 agl_shell_set_background(agl_shell, bg, output);
285 /* Delay the ready signal until after Qt has done all of its own setup
287 QTimer::singleShot(500, [agl_shell](){
288 agl_shell_ready(agl_shell);
292 int main(int argc, char *argv[])
294 setenv("QT_QPA_PLATFORM", "wayland", 1);
295 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
297 QGuiApplication app(argc, argv);
298 const char *screen_name;
299 bool is_demo_val = false;
301 struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
303 QPlatformNativeInterface *native = qApp->platformNativeInterface();
304 screen_name = getenv("HOMESCREEN_START_SCREEN");
306 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
307 if (is_demo && strcmp(is_demo, "1") == 0)
310 QCoreApplication::setOrganizationDomain("LinuxFoundation");
311 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
312 QCoreApplication::setApplicationName("HomeScreen");
313 QCoreApplication::setApplicationVersion("0.7.0");
315 // we need to have an app_id
316 app.setDesktopFileName("homescreen");
318 register_agl_shell(native, &shell_data);
319 if (!shell_data.shell) {
320 fprintf(stderr, "agl_shell extension is not advertised. "
321 "Are you sure that agl-compositor is running?\n");
324 if (!shell_data.shell_desktop) {
325 fprintf(stderr, "agl_shell_desktop extension is not advertised. "
326 "Are you sure that agl-compositor is running?\n");
330 qDebug() << "agl-shell interface is at version " << shell_data.ver;
331 if (shell_data.ver >= 2) {
332 while (ret != -1 && shell_data.wait_for_bound) {
333 ret = wl_display_dispatch(getWlDisplay(native));
335 if (shell_data.wait_for_bound)
339 if (!shell_data.bound_ok) {
340 qInfo() << "agl_shell extension already in use by other shell client.";
346 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
347 Shell *aglShell = new Shell(agl_shell, &app);
349 // Import C++ class to QML
350 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
351 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
353 ApplicationLauncher *launcher = new ApplicationLauncher();
354 launcher->setCurrent(QStringLiteral("launcher"));
355 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
357 agl_shell_desktop_add_listener(shell_data.shell_desktop, &shell_desktop_listener, homescreenHandler);
359 QQmlApplicationEngine engine;
360 QQmlContext *context = engine.rootContext();
362 context->setContextProperty("homescreenHandler", homescreenHandler);
363 context->setContextProperty("launcher", launcher);
364 context->setContextProperty("weather", new Weather());
365 context->setContextProperty("bluetooth", new Bluetooth(false, context));
367 // We add it here even if we don't use it
368 context->setContextProperty("shell", aglShell);
370 // Instead of loading main.qml we load one-by-one each of the QMLs,
371 // divided now between several surfaces: panels, background.
372 load_agl_shell_app(native, &engine, shell_data.shell, screen_name, is_demo_val);