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"
35 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
39 struct agl_shell *shell;
40 HomescreenHandler *homescreenHandler;
47 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
49 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
50 shell_data->wait_for_bound = false;
52 shell_data->bound_ok = true;
56 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
58 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
59 shell_data->wait_for_bound = false;
61 shell_data->bound_ok = false;
65 agl_shell_app_state(void *data, struct agl_shell *agl_shell,
66 const char *app_id, uint32_t state)
68 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
69 HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
71 if (!homescreenHandler)
75 case AGL_SHELL_APP_STATE_STARTED:
76 qDebug() << "Got AGL_SHELL_APP_STATE_STARTED for app_id " << app_id;
77 homescreenHandler->processAppStatusEvent(app_id, "started");
79 case AGL_SHELL_APP_STATE_TERMINATED:
80 qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
81 // handled by HomescreenHandler::processAppStatusEvent
83 case AGL_SHELL_APP_STATE_ACTIVATED:
84 qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
85 homescreenHandler->addAppToStack(app_id);
93 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
94 static const struct agl_shell_listener shell_listener = {
102 global_add(void *data, struct wl_registry *reg, uint32_t name,
103 const char *interface, uint32_t ver)
105 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
110 if (strcmp(interface, agl_shell_interface.name) == 0) {
113 static_cast<struct agl_shell *>(
114 wl_registry_bind(reg, name, &agl_shell_interface, MIN(3, ver)));
115 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
116 agl_shell_add_listener(shell_data->shell, &shell_listener, data);
120 static_cast<struct agl_shell *>(
121 wl_registry_bind(reg, name, &agl_shell_interface, 1));
123 shell_data->ver = ver;
129 global_remove(void *data, struct wl_registry *reg, uint32_t id)
137 static const struct wl_registry_listener registry_listener = {
142 static struct wl_surface *
143 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
145 void *surf = native->nativeResourceForWindow("surface", window);
146 return static_cast<struct ::wl_surface *>(surf);
149 static struct wl_output *
150 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
152 void *output = native->nativeResourceForScreen("output", screen);
153 return static_cast<struct ::wl_output*>(output);
156 static struct wl_display *
157 getWlDisplay(QPlatformNativeInterface *native)
159 return static_cast<struct wl_display *>(
160 native->nativeResourceForIntegration("display")
166 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
168 struct wl_display *wl;
169 struct wl_registry *registry;
171 wl = getWlDisplay(native);
172 registry = wl_display_get_registry(wl);
174 wl_registry_add_listener(registry, ®istry_listener, shell_data);
176 /* Roundtrip to get all globals advertised by the compositor */
177 wl_display_roundtrip(wl);
178 wl_registry_destroy(registry);
181 static struct wl_surface *
182 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
183 QScreen *screen, QObject **qobj)
185 QObject *obj = comp->create();
186 obj->setParent(screen);
188 QWindow *win = qobject_cast<QWindow *>(obj);
191 return getWlSurface(native, win);
195 find_screen(const char *screen_name)
197 QList<QScreen *> screens = qApp->screens();
198 QScreen *found = nullptr;
199 QString qstr_name = QString::fromUtf8(screen_name, -1);
201 for (QScreen *xscreen : screens) {
202 if (qstr_name == xscreen->name()) {
212 load_agl_shell_app(QPlatformNativeInterface *native,
213 QQmlApplicationEngine *engine,
214 struct agl_shell *agl_shell,
215 const char *screen_name,
218 struct wl_surface *bg, *top, *bottom;
219 struct wl_output *output;
220 QObject *qobj_bg, *qobj_top, *qobj_bottom;
221 QScreen *screen = nullptr;
224 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
225 qInfo() << bg_comp.errors();
227 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
228 qInfo() << top_comp.errors();
230 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
231 qInfo() << bot_comp.errors();
233 top = create_component(native, &top_comp, screen, &qobj_top);
234 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
235 bg = create_component(native, &bg_comp, screen, &qobj_bg);
237 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
238 qInfo() << bg_comp.errors();
240 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
241 qInfo() << top_comp.errors();
243 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
244 qInfo() << bot_comp.errors();
246 top = create_component(native, &top_comp, screen, &qobj_top);
247 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
248 bg = create_component(native, &bg_comp, screen, &qobj_bg);
252 screen = qApp->primaryScreen();
254 screen = find_screen(screen_name);
257 qDebug() << "No outputs present in the system.";
261 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
262 "first screen " << qApp->screens().first()->name();
263 output = getWlOutput(native, screen);
265 /* engine.rootObjects() works only if we had a load() */
266 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
268 qDebug() << "got statusBar objectname, doing init()";
269 statusBar->init(engine->rootContext());
272 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
273 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
274 qDebug() << "Setting homescreen to screen " << screen->name();
276 agl_shell_set_background(agl_shell, bg, output);
278 /* Delay the ready signal until after Qt has done all of its own setup
280 QTimer::singleShot(500, [agl_shell](){
281 agl_shell_ready(agl_shell);
285 int main(int argc, char *argv[])
287 setenv("QT_QPA_PLATFORM", "wayland", 1);
288 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
290 QGuiApplication app(argc, argv);
291 const char *screen_name;
292 bool is_demo_val = false;
294 struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
296 QPlatformNativeInterface *native = qApp->platformNativeInterface();
297 screen_name = getenv("HOMESCREEN_START_SCREEN");
299 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
300 if (is_demo && strcmp(is_demo, "1") == 0)
303 QCoreApplication::setOrganizationDomain("LinuxFoundation");
304 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
305 QCoreApplication::setApplicationName("HomeScreen");
306 QCoreApplication::setApplicationVersion("0.7.0");
308 // we need to have an app_id
309 app.setDesktopFileName("homescreen");
311 register_agl_shell(native, &shell_data);
312 if (!shell_data.shell) {
313 fprintf(stderr, "agl_shell extension is not advertised. "
314 "Are you sure that agl-compositor is running?\n");
318 qDebug() << "agl-shell interface is at version " << shell_data.ver;
319 if (shell_data.ver >= 2) {
320 while (ret != -1 && shell_data.wait_for_bound) {
321 ret = wl_display_dispatch(getWlDisplay(native));
323 if (shell_data.wait_for_bound)
327 if (!shell_data.bound_ok) {
328 qInfo() << "agl_shell extension already in use by other shell client.";
334 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
335 Shell *aglShell = new Shell(agl_shell, &app);
337 // Import C++ class to QML
338 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
339 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
341 ApplicationLauncher *launcher = new ApplicationLauncher();
342 launcher->setCurrent(QStringLiteral("launcher"));
344 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
345 shell_data.homescreenHandler = homescreenHandler;
347 QQmlApplicationEngine engine;
348 QQmlContext *context = engine.rootContext();
350 context->setContextProperty("homescreenHandler", homescreenHandler);
351 context->setContextProperty("launcher", launcher);
352 context->setContextProperty("weather", new Weather());
353 context->setContextProperty("bluetooth", new Bluetooth(false, context));
355 // We add it here even if we don't use it
356 context->setContextProperty("shell", aglShell);
358 // Instead of loading main.qml we load one-by-one each of the QMLs,
359 // divided now between several surfaces: panels, background.
360 load_agl_shell_app(native, &engine, shell_data.shell, screen_name, is_demo_val);