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 // meson will define these
29 #include QT_QPA_HEADER
30 #include <wayland-client.h>
32 #include "agl-shell-client-protocol.h"
36 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
40 struct agl_shell *shell;
41 HomescreenHandler *homescreenHandler;
48 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
50 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
51 shell_data->wait_for_bound = false;
53 shell_data->bound_ok = true;
57 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
59 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
60 shell_data->wait_for_bound = false;
62 shell_data->bound_ok = false;
66 agl_shell_app_state(void *data, struct agl_shell *agl_shell,
67 const char *app_id, uint32_t state)
69 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
70 HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
72 if (!homescreenHandler)
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");
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
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);
94 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
95 static const struct agl_shell_listener shell_listener = {
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(3, 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;
130 global_remove(void *data, struct wl_registry *reg, uint32_t id)
138 static const struct wl_registry_listener registry_listener = {
143 static struct wl_surface *
144 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
146 void *surf = native->nativeResourceForWindow("surface", window);
147 return static_cast<struct ::wl_surface *>(surf);
150 static struct wl_output *
151 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
153 void *output = native->nativeResourceForScreen("output", screen);
154 return static_cast<struct ::wl_output*>(output);
157 static struct wl_display *
158 getWlDisplay(QPlatformNativeInterface *native)
160 return static_cast<struct wl_display *>(
161 native->nativeResourceForIntegration("display")
167 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
169 struct wl_display *wl;
170 struct wl_registry *registry;
172 wl = getWlDisplay(native);
173 registry = wl_display_get_registry(wl);
175 wl_registry_add_listener(registry, ®istry_listener, shell_data);
177 /* Roundtrip to get all globals advertised by the compositor */
178 wl_display_roundtrip(wl);
179 wl_registry_destroy(registry);
182 static struct wl_surface *
183 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
184 QScreen *screen, QObject **qobj)
186 QObject *obj = comp->create();
187 obj->setParent(screen);
189 QWindow *win = qobject_cast<QWindow *>(obj);
192 return getWlSurface(native, win);
196 find_screen(const char *screen_name)
198 QList<QScreen *> screens = qApp->screens();
199 QScreen *found = nullptr;
200 QString qstr_name = QString::fromUtf8(screen_name, -1);
202 for (QScreen *xscreen : screens) {
203 if (qstr_name == xscreen->name()) {
213 load_agl_shell_app(QPlatformNativeInterface *native,
214 QQmlApplicationEngine *engine,
215 struct agl_shell *agl_shell,
216 const char *screen_name,
219 struct wl_surface *bg, *top, *bottom;
220 struct wl_output *output;
221 QObject *qobj_bg, *qobj_top, *qobj_bottom;
222 QScreen *screen = nullptr;
225 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
226 qInfo() << bg_comp.errors();
228 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
229 qInfo() << top_comp.errors();
231 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
232 qInfo() << bot_comp.errors();
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);
238 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
239 qInfo() << bg_comp.errors();
241 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
242 qInfo() << top_comp.errors();
244 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
245 qInfo() << bot_comp.errors();
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);
253 screen = qApp->primaryScreen();
255 screen = find_screen(screen_name);
258 qDebug() << "No outputs present in the system.";
262 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
263 "first screen " << qApp->screens().first()->name();
264 output = getWlOutput(native, screen);
266 /* engine.rootObjects() works only if we had a load() */
267 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
269 qDebug() << "got statusBar objectname, doing init()";
270 statusBar->init(engine->rootContext());
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();
277 agl_shell_set_background(agl_shell, bg, output);
279 /* Delay the ready signal until after Qt has done all of its own setup
281 QTimer::singleShot(500, [agl_shell](){
282 agl_shell_ready(agl_shell);
286 int main(int argc, char *argv[])
288 setenv("QT_QPA_PLATFORM", "wayland", 1);
289 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
291 QGuiApplication app(argc, argv);
292 const char *screen_name;
293 bool is_demo_val = false;
295 struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
297 QPlatformNativeInterface *native = qApp->platformNativeInterface();
298 screen_name = getenv("HOMESCREEN_START_SCREEN");
300 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
301 if (is_demo && strcmp(is_demo, "1") == 0)
304 QCoreApplication::setOrganizationDomain("LinuxFoundation");
305 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
306 QCoreApplication::setApplicationName("HomeScreen");
307 QCoreApplication::setApplicationVersion("0.7.0");
309 // we need to have an app_id
310 app.setDesktopFileName("homescreen");
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");
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));
324 if (shell_data.wait_for_bound)
328 if (!shell_data.bound_ok) {
329 qInfo() << "agl_shell extension already in use by other shell client.";
335 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
336 Shell *aglShell = new Shell(agl_shell, &app);
338 // Import C++ class to QML
339 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
340 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
342 ApplicationLauncher *launcher = new ApplicationLauncher();
343 launcher->setCurrent(QStringLiteral("launcher"));
345 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
346 shell_data.homescreenHandler = homescreenHandler;
348 QQmlApplicationEngine engine;
349 QQmlContext *context = engine.rootContext();
351 context->setContextProperty("homescreenHandler", homescreenHandler);
352 context->setContextProperty("launcher", launcher);
353 context->setContextProperty("weather", new Weather());
354 context->setContextProperty("bluetooth", new Bluetooth(false, context));
356 // We add it here even if we don't use it
357 context->setContextProperty("shell", aglShell);
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);