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 struct agl_shell *shell;
37 struct agl_shell_desktop *shell_desktop;
41 agl_shell_desktop_application(void *data,
42 struct agl_shell_desktop *agl_shell_desktop,
45 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
47 if (homescreenHandler)
48 homescreenHandler->addAppToStack(app_id);
52 agl_shell_desktop_state_app(void *data,
53 struct agl_shell_desktop *agl_shell_desktop,
59 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
61 if (homescreenHandler && state == AGL_SHELL_DESKTOP_APP_STATE_DESTROYED)
62 homescreenHandler->deactivateApp(app_id);
65 static const struct agl_shell_desktop_listener shell_desktop_listener = {
66 agl_shell_desktop_application,
67 agl_shell_desktop_state_app
71 global_add(void *data, struct wl_registry *reg, uint32_t name,
72 const char *interface, uint32_t)
74 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
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)
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)
93 global_remove(void *data, struct wl_registry *reg, uint32_t id)
101 static const struct wl_registry_listener registry_listener = {
106 static struct wl_surface *
107 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
109 void *surf = native->nativeResourceForWindow("surface", window);
110 return static_cast<struct ::wl_surface *>(surf);
113 static struct wl_output *
114 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
116 void *output = native->nativeResourceForScreen("output", screen);
117 return static_cast<struct ::wl_output*>(output);
122 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
124 struct wl_display *wl;
125 struct wl_registry *registry;
127 wl = static_cast<struct wl_display *>(
128 native->nativeResourceForIntegration("display")
130 registry = wl_display_get_registry(wl);
132 wl_registry_add_listener(registry, ®istry_listener, shell_data);
134 /* Roundtrip to get all globals advertised by the compositor */
135 wl_display_roundtrip(wl);
136 wl_registry_destroy(registry);
139 static struct wl_surface *
140 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
141 QScreen *screen, QObject **qobj)
143 QObject *obj = comp->create();
144 obj->setParent(screen);
146 QWindow *win = qobject_cast<QWindow *>(obj);
149 return getWlSurface(native, win);
153 find_screen(const char *screen_name)
155 QList<QScreen *> screens = qApp->screens();
156 QScreen *found = nullptr;
157 QString qstr_name = QString::fromUtf8(screen_name, -1);
159 for (QScreen *xscreen : screens) {
160 if (qstr_name == xscreen->name()) {
170 load_agl_shell_app(QPlatformNativeInterface *native,
171 QQmlApplicationEngine *engine,
172 struct agl_shell *agl_shell,
173 const char *screen_name,
176 struct wl_surface *bg, *top, *bottom;
177 struct wl_output *output;
178 QObject *qobj_bg, *qobj_top, *qobj_bottom;
179 QScreen *screen = nullptr;
182 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
183 qInfo() << bg_comp.errors();
185 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
186 qInfo() << top_comp.errors();
188 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
189 qInfo() << bot_comp.errors();
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);
195 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
196 qInfo() << bg_comp.errors();
198 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
199 qInfo() << top_comp.errors();
201 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
202 qInfo() << bot_comp.errors();
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);
210 screen = qApp->primaryScreen();
212 screen = find_screen(screen_name);
215 qDebug() << "No outputs present in the system.";
219 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
220 "first screen " << qApp->screens().first()->name();
221 output = getWlOutput(native, screen);
223 /* engine.rootObjects() works only if we had a load() */
224 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
226 qDebug() << "got statusBar objectname, doing init()";
227 statusBar->init(engine->rootContext());
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();
234 agl_shell_set_background(agl_shell, bg, output);
236 /* Delay the ready signal until after Qt has done all of its own setup
238 QTimer::singleShot(500, [agl_shell](){
239 agl_shell_ready(agl_shell);
243 int main(int argc, char *argv[])
245 setenv("QT_QPA_PLATFORM", "wayland", 1);
246 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
248 QGuiApplication app(argc, argv);
249 const char *screen_name;
250 bool is_demo_val = false;
251 struct shell_data shell_data = { nullptr, nullptr };
253 QPlatformNativeInterface *native = qApp->platformNativeInterface();
254 screen_name = getenv("HOMESCREEN_START_SCREEN");
256 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
257 if (is_demo && strcmp(is_demo, "1") == 0)
260 QCoreApplication::setOrganizationDomain("LinuxFoundation");
261 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
262 QCoreApplication::setApplicationName("HomeScreen");
263 QCoreApplication::setApplicationVersion("0.7.0");
265 // we need to have an app_id
266 app.setDesktopFileName("homescreen");
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");
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");
280 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
281 Shell *aglShell = new Shell(agl_shell, &app);
283 // Import C++ class to QML
284 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
285 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
287 ApplicationLauncher *launcher = new ApplicationLauncher();
288 launcher->setCurrent(QStringLiteral("launcher"));
289 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
291 agl_shell_desktop_add_listener(shell_data.shell_desktop, &shell_desktop_listener, homescreenHandler);
293 QQmlApplicationEngine engine;
294 QQmlContext *context = engine.rootContext();
296 context->setContextProperty("homescreenHandler", homescreenHandler);
297 context->setContextProperty("launcher", launcher);
298 context->setContextProperty("weather", new Weather());
299 context->setContextProperty("bluetooth", new Bluetooth(false, context));
301 // We add it here even if we don't use it
302 context->setContextProperty("shell", aglShell);
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);