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 find_screen(const char *screen_name)
42 QList<QScreen *> screens = qApp->screens();
43 QScreen *found = nullptr;
44 QString qstr_name = QString::fromUtf8(screen_name, -1);
46 for (QScreen *xscreen : screens) {
47 if (qstr_name == xscreen->name()) {
57 struct agl_shell *shell;
58 HomescreenHandler *homescreenHandler;
65 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
67 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
68 shell_data->wait_for_bound = false;
70 shell_data->bound_ok = true;
74 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
76 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
77 shell_data->wait_for_bound = false;
79 shell_data->bound_ok = false;
83 agl_shell_app_state(void *data, struct agl_shell *agl_shell,
84 const char *app_id, uint32_t state)
86 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
87 HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
89 if (!homescreenHandler)
93 case AGL_SHELL_APP_STATE_STARTED:
94 qDebug() << "Got AGL_SHELL_APP_STATE_STARTED for app_id " << app_id;
95 homescreenHandler->processAppStatusEvent(app_id, "started");
97 case AGL_SHELL_APP_STATE_TERMINATED:
98 qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
99 // handled by HomescreenHandler::processAppStatusEvent
101 case AGL_SHELL_APP_STATE_ACTIVATED:
102 qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
103 homescreenHandler->addAppToStack(app_id);
105 case AGL_SHELL_APP_STATE_DEACTIVATED:
106 qDebug() << "Got AGL_SHELL_APP_STATE_DEACTIVATED for app_id " << app_id;
107 homescreenHandler->processAppStatusEvent(app_id, "deactivated");
115 agl_shell_app_on_output(void *data, struct agl_shell *agl_shell,
116 const char *app_id, const char *output_name)
118 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
119 HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
121 if (!homescreenHandler)
124 // a couple of use-cases, if there is no app_id in the app_list then it
125 // means this is a request to map the application, from the start to a
126 // different output that the default one. We'd get an
127 // AGL_SHELL_APP_STATE_STARTED which will handle activation.
129 // if there's an app_id then it means we might have gotten an event to
130 // move the application to another output; so we'd need to process it
131 // by explicitly calling processAppStatusEvent() which would ultimately
132 // activate the application on other output. We'd have to pick-up the
133 // last activated window and activate the default output.
135 // finally if the outputs are identical probably that's an user-error -
136 // but the compositor won't activate it again, so we don't handle that.
137 std::pair new_pending_app = std::pair(QString(app_id),
138 QString(output_name));
139 homescreenHandler->pending_app_list.push_back(new_pending_app);
141 if (homescreenHandler->apps_stack.contains(QString(app_id))) {
142 qDebug() << "Gove event to move " << app_id <<
143 " to another output " << output_name;
144 homescreenHandler->processAppStatusEvent(app_id, "started");
149 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
150 static const struct agl_shell_listener shell_listener = {
152 agl_shell_bound_fail,
154 agl_shell_app_on_output,
159 global_add(void *data, struct wl_registry *reg, uint32_t name,
160 const char *interface, uint32_t ver)
162 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
167 if (strcmp(interface, agl_shell_interface.name) == 0) {
170 static_cast<struct agl_shell *>(
171 wl_registry_bind(reg, name, &agl_shell_interface, MIN(8, ver)));
172 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
173 agl_shell_add_listener(shell_data->shell, &shell_listener, data);
177 static_cast<struct agl_shell *>(
178 wl_registry_bind(reg, name, &agl_shell_interface, 1));
180 shell_data->ver = ver;
186 global_remove(void *data, struct wl_registry *reg, uint32_t id)
194 static const struct wl_registry_listener registry_listener = {
199 static struct wl_surface *
200 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
202 void *surf = native->nativeResourceForWindow("surface", window);
203 return static_cast<struct ::wl_surface *>(surf);
206 static struct wl_output *
207 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
209 void *output = native->nativeResourceForScreen("output", screen);
210 return static_cast<struct ::wl_output*>(output);
213 static struct wl_display *
214 getWlDisplay(QPlatformNativeInterface *native)
216 return static_cast<struct wl_display *>(
217 native->nativeResourceForIntegration("display")
223 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
225 struct wl_display *wl;
226 struct wl_registry *registry;
228 wl = getWlDisplay(native);
229 registry = wl_display_get_registry(wl);
231 wl_registry_add_listener(registry, ®istry_listener, shell_data);
233 /* Roundtrip to get all globals advertised by the compositor */
234 wl_display_roundtrip(wl);
235 wl_registry_destroy(registry);
238 static struct wl_surface *
239 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
240 QScreen *screen, QObject **qobj)
242 QObject *obj = comp->create();
243 obj->setParent(screen);
245 QWindow *win = qobject_cast<QWindow *>(obj);
248 return getWlSurface(native, win);
253 load_agl_shell(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
254 struct agl_shell *agl_shell, QScreen *screen)
256 struct wl_surface *bg;
257 struct wl_output *output;
259 int32_t width, height;
261 QSize size = screen->size();
263 // this incorporates the panels directly, but in doing so, it
264 // would also need to specify an activation area the same area
265 // in order to void overlapping any new activation window
266 QQmlComponent bg_comp(engine, QUrl("qrc:/background_with_panels.qml"));
267 qInfo() << bg_comp.errors();
269 bg = create_component(native, &bg_comp, screen, &qobj_bg);
271 output = getWlOutput(native, screen);
273 qDebug() << "Normal mode - with single surface";
274 qDebug() << "Setting homescreen to screen " << screen->name();
275 agl_shell_set_background(agl_shell, bg, output);
277 // 216 is the width size of the panel
281 width = size.width();
282 height = size.height() - (2 * y);
284 qDebug() << "Using custom rectangle " << width << "x" << height
285 << "+" << x << "x" << y << " for activation";
286 qDebug() << "Panels should be embedded the background surface";
288 #ifdef AGL_SHELL_SET_ACTIVATE_REGION_SINCE_VERSION
289 agl_shell_set_activate_region(agl_shell, output,
290 x, y, width, height);
295 load_agl_shell_for_ci(QPlatformNativeInterface *native,
296 QQmlApplicationEngine *engine,
297 struct agl_shell *agl_shell, QScreen *screen)
299 struct wl_surface *bg, *top, *bottom;
300 struct wl_output *output;
301 QObject *qobj_bg, *qobj_top, *qobj_bottom;
303 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
304 qInfo() << bg_comp.errors();
306 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
307 qInfo() << top_comp.errors();
309 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
310 qInfo() << bot_comp.errors();
312 top = create_component(native, &top_comp, screen, &qobj_top);
313 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
314 bg = create_component(native, &bg_comp, screen, &qobj_bg);
316 /* engine.rootObjects() works only if we had a load() */
317 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
319 qDebug() << "got statusBar objectname, doing init()";
320 statusBar->init(engine->rootContext());
323 output = getWlOutput(native, screen);
325 qDebug() << "Setting homescreen to screen " << screen->name();
327 agl_shell_set_background(agl_shell, bg, output);
328 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
329 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
331 qDebug() << "CI mode - with multiple surfaces";
335 load_agl_shell_app(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
336 struct agl_shell *agl_shell, const char *screen_name, bool is_demo)
338 QScreen *screen = nullptr;
341 screen = qApp->primaryScreen();
343 screen = find_screen(screen_name);
346 qDebug() << "No outputs present in the system.";
351 load_agl_shell_for_ci(native, engine, agl_shell, screen);
353 load_agl_shell(native, engine, agl_shell, screen);
356 /* Delay the ready signal until after Qt has done all of its own setup
358 QTimer::singleShot(500, [agl_shell](){
359 qDebug() << "sending ready to compositor";
360 agl_shell_ready(agl_shell);
364 int main(int argc, char *argv[])
366 setenv("QT_QPA_PLATFORM", "wayland", 1);
367 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
369 QGuiApplication app(argc, argv);
370 const char *screen_name;
371 bool is_demo_val = false;
372 bool is_embedded_panels = false;
374 struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
376 QPlatformNativeInterface *native = qApp->platformNativeInterface();
377 screen_name = getenv("HOMESCREEN_START_SCREEN");
379 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
380 if (is_demo && strcmp(is_demo, "1") == 0)
383 const char *embedded_panels = getenv("HOMESCREEN_EMBEDDED_PANELS");
384 if (embedded_panels && strcmp(embedded_panels, "1") == 0)
385 is_embedded_panels = true;
387 QCoreApplication::setOrganizationDomain("LinuxFoundation");
388 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
389 QCoreApplication::setApplicationName("HomeScreen");
390 QCoreApplication::setApplicationVersion("0.7.0");
392 // we need to have an app_id
393 app.setDesktopFileName("homescreen");
395 register_agl_shell(native, &shell_data);
396 if (!shell_data.shell) {
397 fprintf(stderr, "agl_shell extension is not advertised. "
398 "Are you sure that agl-compositor is running?\n");
402 qDebug() << "agl-shell interface is at version " << shell_data.ver;
403 if (shell_data.ver >= 2) {
404 while (ret != -1 && shell_data.wait_for_bound) {
405 ret = wl_display_dispatch(getWlDisplay(native));
407 if (shell_data.wait_for_bound)
411 if (!shell_data.bound_ok) {
412 qInfo() << "agl_shell extension already in use by other shell client.";
418 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
419 Shell *aglShell = new Shell(agl_shell, &app);
421 // Import C++ class to QML
422 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
423 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
425 ApplicationLauncher *launcher = new ApplicationLauncher();
426 launcher->setCurrent(QStringLiteral("launcher"));
428 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
429 shell_data.homescreenHandler = homescreenHandler;
431 QQmlApplicationEngine engine;
432 QQmlContext *context = engine.rootContext();
434 context->setContextProperty("homescreenHandler", homescreenHandler);
435 context->setContextProperty("launcher", launcher);
436 context->setContextProperty("weather", new Weather());
437 context->setContextProperty("bluetooth", new Bluetooth(false, context));
439 // We add it here even if we don't use it
440 context->setContextProperty("shell", aglShell);
442 load_agl_shell_app(native, &engine, shell_data.shell,
443 screen_name, is_demo_val);