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(4, 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, bool is_demo, bool embedded_panels)
218 struct wl_surface *bg, *top, *bottom;
219 struct wl_output *output;
220 QObject *qobj_bg, *qobj_top, *qobj_bottom;
221 QScreen *screen = nullptr;
223 if (is_demo && !embedded_panels) {
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 /* engine.rootObjects() works only if we had a load() */
238 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
240 qDebug() << "got statusBar objectname, doing init()";
241 statusBar->init(engine->rootContext());
244 qDebug() << "init debug mode";
245 } else if (!embedded_panels) {
246 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
247 qInfo() << bg_comp.errors();
249 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
250 qInfo() << top_comp.errors();
252 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
253 qInfo() << bot_comp.errors();
255 top = create_component(native, &top_comp, screen, &qobj_top);
256 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
257 bg = create_component(native, &bg_comp, screen, &qobj_bg);
259 /* engine.rootObjects() works only if we had a load() */
260 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
262 qDebug() << "got statusBar objectname, doing init()";
263 statusBar->init(engine->rootContext());
266 qDebug() << "init normal mode";
268 // this incorporates the panels directly, but in doing so, it
269 // would also need to specify an activation area the same area
270 // in order to void overlapping any new activation window
271 QQmlComponent bg_comp(engine, QUrl("qrc:/background_with_panels.qml"));
272 qInfo() << bg_comp.errors();
274 bg = create_component(native, &bg_comp, screen, &qobj_bg);
275 qDebug() << "init embedded panels mode";
279 screen = qApp->primaryScreen();
281 screen = find_screen(screen_name);
284 qDebug() << "No outputs present in the system.";
288 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
289 "first screen " << qApp->screens().first()->name();
290 output = getWlOutput(native, screen);
292 qDebug() << "Setting homescreen to screen " << screen->name();
293 agl_shell_set_background(agl_shell, bg, output);
295 if (embedded_panels) {
297 int32_t width, height;
298 QSize size = screen->size();
303 width = size.width();
304 height = size.height() - (2 * y);
306 qDebug() << "Using custom rectangle " << width << "x" << height
307 << "+" << x << "x" << y << " for activation";
308 qDebug() << "Panels should be embedded the background surface";
310 #ifdef AGL_SHELL_SET_ACTIVATE_REGION_SINCE_VERSION
311 agl_shell_set_activate_region(agl_shell, output,
312 x, y, width, height);
316 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
317 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
318 qDebug() << "Setting regular panels";
321 /* Delay the ready signal until after Qt has done all of its own setup
323 QTimer::singleShot(500, [agl_shell](){
324 qDebug() << "sending ready to compositor";
325 agl_shell_ready(agl_shell);
329 int main(int argc, char *argv[])
331 setenv("QT_QPA_PLATFORM", "wayland", 1);
332 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
334 QGuiApplication app(argc, argv);
335 const char *screen_name;
336 bool is_demo_val = false;
337 bool is_embedded_panels = false;
339 struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
341 QPlatformNativeInterface *native = qApp->platformNativeInterface();
342 screen_name = getenv("HOMESCREEN_START_SCREEN");
344 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
345 if (is_demo && strcmp(is_demo, "1") == 0)
348 const char *embedded_panels = getenv("HOMESCREEN_EMBEDDED_PANELS");
349 if (embedded_panels && strcmp(embedded_panels, "1") == 0)
350 is_embedded_panels = true;
352 QCoreApplication::setOrganizationDomain("LinuxFoundation");
353 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
354 QCoreApplication::setApplicationName("HomeScreen");
355 QCoreApplication::setApplicationVersion("0.7.0");
357 // we need to have an app_id
358 app.setDesktopFileName("homescreen");
360 register_agl_shell(native, &shell_data);
361 if (!shell_data.shell) {
362 fprintf(stderr, "agl_shell extension is not advertised. "
363 "Are you sure that agl-compositor is running?\n");
367 qDebug() << "agl-shell interface is at version " << shell_data.ver;
368 if (shell_data.ver >= 2) {
369 while (ret != -1 && shell_data.wait_for_bound) {
370 ret = wl_display_dispatch(getWlDisplay(native));
372 if (shell_data.wait_for_bound)
376 if (!shell_data.bound_ok) {
377 qInfo() << "agl_shell extension already in use by other shell client.";
383 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
384 Shell *aglShell = new Shell(agl_shell, &app);
386 // Import C++ class to QML
387 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
388 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
390 ApplicationLauncher *launcher = new ApplicationLauncher();
391 launcher->setCurrent(QStringLiteral("launcher"));
393 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
394 shell_data.homescreenHandler = homescreenHandler;
396 QQmlApplicationEngine engine;
397 QQmlContext *context = engine.rootContext();
399 context->setContextProperty("homescreenHandler", homescreenHandler);
400 context->setContextProperty("launcher", launcher);
401 context->setContextProperty("weather", new Weather());
402 context->setContextProperty("bluetooth", new Bluetooth(false, context));
404 // We add it here even if we don't use it
405 context->setContextProperty("shell", aglShell);
407 // Instead of loading main.qml we load one-by-one each of the QMLs,
408 // divided now between several surfaces: panels, background.
409 load_agl_shell_app(native, &engine, shell_data.shell,
410 screen_name, is_demo_val, is_embedded_panels);