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
6 * Copyright (c) 2023 Collabora, Ltd.
9 #include <QGuiApplication>
10 #include <QCommandLineParser>
11 #include <QtCore/QUrlQuery>
12 #include <QtGui/QGuiApplication>
13 #include <QtQml/QQmlApplicationEngine>
14 #include <QtQml/QQmlContext>
15 #include <QtQml/QQmlComponent>
16 #include <QtQml/qqml.h>
17 #include <QQuickWindow>
21 #include <bluetooth.h>
23 #include "applicationlauncher.h"
24 #include "statusbarmodel.h"
25 #include "mastervolume.h"
26 #include "homescreenhandler.h"
27 #include "hmi-debug.h"
29 // meson will define these
30 #include QT_QPA_HEADER
31 #include <wayland-client.h>
33 #include "agl-shell-client-protocol.h"
37 #include "AglShellGrpcClient.h"
40 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
44 find_screen(const char *screen_name)
46 QList<QScreen *> screens = qApp->screens();
47 QScreen *found = nullptr;
48 QString qstr_name = QString::fromUtf8(screen_name, -1);
50 for (QScreen *xscreen : screens) {
51 if (qstr_name == xscreen->name()) {
61 struct agl_shell *shell;
62 HomescreenHandler *homescreenHandler;
69 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
71 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
72 shell_data->wait_for_bound = false;
74 shell_data->bound_ok = true;
78 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
80 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
81 shell_data->wait_for_bound = false;
83 shell_data->bound_ok = false;
87 agl_shell_app_state(void *data, struct agl_shell *agl_shell,
88 const char *app_id, uint32_t state)
91 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
92 HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
94 if (!homescreenHandler)
98 case AGL_SHELL_APP_STATE_STARTED:
99 qDebug() << "Got AGL_SHELL_APP_STATE_STARTED for app_id " << app_id;
100 homescreenHandler->processAppStatusEvent(app_id, "started");
102 case AGL_SHELL_APP_STATE_TERMINATED:
103 qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
104 // handled by HomescreenHandler::processAppStatusEvent
106 case AGL_SHELL_APP_STATE_ACTIVATED:
107 qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
108 homescreenHandler->addAppToStack(app_id);
110 case AGL_SHELL_APP_STATE_DEACTIVATED:
111 qDebug() << "Got AGL_SHELL_APP_STATE_DEACTIVATED for app_id " << app_id;
112 homescreenHandler->processAppStatusEvent(app_id, "deactivated");
121 agl_shell_app_on_output(void *data, struct agl_shell *agl_shell,
122 const char *app_id, const char *output_name)
124 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
125 HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
127 if (!homescreenHandler)
130 // a couple of use-cases, if there is no app_id in the app_list then it
131 // means this is a request to map the application, from the start to a
132 // different output that the default one. We'd get an
133 // AGL_SHELL_APP_STATE_STARTED which will handle activation.
135 // if there's an app_id then it means we might have gotten an event to
136 // move the application to another output; so we'd need to process it
137 // by explicitly calling processAppStatusEvent() which would ultimately
138 // activate the application on other output. We'd have to pick-up the
139 // last activated window and activate the default output.
141 // finally if the outputs are identical probably that's an user-error -
142 // but the compositor won't activate it again, so we don't handle that.
143 std::pair new_pending_app = std::pair(QString(app_id),
144 QString(output_name));
145 homescreenHandler->pending_app_list.push_back(new_pending_app);
147 if (homescreenHandler->apps_stack.contains(QString(app_id))) {
148 qDebug() << "Gove event to move " << app_id <<
149 " to another output " << output_name;
150 homescreenHandler->processAppStatusEvent(app_id, "started");
155 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
156 static const struct agl_shell_listener shell_listener = {
158 agl_shell_bound_fail,
160 agl_shell_app_on_output,
165 global_add(void *data, struct wl_registry *reg, uint32_t name,
166 const char *interface, uint32_t ver)
168 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
173 if (strcmp(interface, agl_shell_interface.name) == 0) {
176 static_cast<struct agl_shell *>(
177 wl_registry_bind(reg, name, &agl_shell_interface, MIN(8, ver)));
178 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
179 agl_shell_add_listener(shell_data->shell, &shell_listener, data);
183 static_cast<struct agl_shell *>(
184 wl_registry_bind(reg, name, &agl_shell_interface, 1));
186 shell_data->ver = ver;
192 global_remove(void *data, struct wl_registry *reg, uint32_t id)
200 static const struct wl_registry_listener registry_listener = {
205 static struct wl_surface *
206 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
208 void *surf = native->nativeResourceForWindow("surface", window);
209 return static_cast<struct ::wl_surface *>(surf);
212 static struct wl_output *
213 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
215 void *output = native->nativeResourceForScreen("output", screen);
216 return static_cast<struct ::wl_output*>(output);
219 static struct wl_display *
220 getWlDisplay(QPlatformNativeInterface *native)
222 return static_cast<struct wl_display *>(
223 native->nativeResourceForIntegration("display")
229 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
231 struct wl_display *wl;
232 struct wl_registry *registry;
234 wl = getWlDisplay(native);
235 registry = wl_display_get_registry(wl);
237 wl_registry_add_listener(registry, ®istry_listener, shell_data);
239 /* Roundtrip to get all globals advertised by the compositor */
240 wl_display_roundtrip(wl);
241 wl_registry_destroy(registry);
244 static struct wl_surface *
245 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
246 QScreen *screen, QObject **qobj)
248 QObject *obj = comp->create();
249 obj->setParent(screen);
251 QWindow *win = qobject_cast<QWindow *>(obj);
254 return getWlSurface(native, win);
259 load_agl_shell(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
260 struct agl_shell *agl_shell, QScreen *screen)
262 struct wl_surface *bg;
263 struct wl_output *output;
265 int32_t width, height;
267 QSize size = screen->size();
269 // this incorporates the panels directly, but in doing so, it
270 // would also need to specify an activation area the same area
271 // in order to void overlapping any new activation window
272 QQmlComponent bg_comp(engine, QUrl("qrc:/background_with_panels.qml"));
273 qInfo() << bg_comp.errors();
275 bg = create_component(native, &bg_comp, screen, &qobj_bg);
277 output = getWlOutput(native, screen);
279 qDebug() << "Normal mode - with single surface";
280 qDebug() << "Setting homescreen to screen " << screen->name();
281 agl_shell_set_background(agl_shell, bg, output);
283 // 216 is the width size of the panel
287 width = size.width();
288 height = size.height() - (2 * y);
290 qDebug() << "Using custom rectangle " << width << "x" << height
291 << "+" << x << "x" << y << " for activation";
292 qDebug() << "Panels should be embedded the background surface";
294 #ifdef AGL_SHELL_SET_ACTIVATE_REGION_SINCE_VERSION
295 agl_shell_set_activate_region(agl_shell, output,
296 x, y, width, height);
301 load_agl_shell_for_ci(QPlatformNativeInterface *native,
302 QQmlApplicationEngine *engine,
303 struct agl_shell *agl_shell, QScreen *screen)
305 struct wl_surface *bg, *top, *bottom;
306 struct wl_output *output;
307 QObject *qobj_bg, *qobj_top, *qobj_bottom;
309 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
310 qInfo() << bg_comp.errors();
312 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
313 qInfo() << top_comp.errors();
315 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
316 qInfo() << bot_comp.errors();
318 top = create_component(native, &top_comp, screen, &qobj_top);
319 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
320 bg = create_component(native, &bg_comp, screen, &qobj_bg);
322 /* engine.rootObjects() works only if we had a load() */
323 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
325 qDebug() << "got statusBar objectname, doing init()";
326 statusBar->init(engine->rootContext());
329 output = getWlOutput(native, screen);
331 qDebug() << "Setting homescreen to screen " << screen->name();
333 agl_shell_set_background(agl_shell, bg, output);
334 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
335 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
337 qDebug() << "CI mode - with multiple surfaces";
341 app_status_callback(::agl_shell_ipc::AppStateResponse app_response, void *data);
344 run_in_thread(GrpcClient *client)
346 grpc::Status status = client->Wait();
350 load_agl_shell_app(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
351 struct shell_data shell_data, const char *screen_name,
352 bool is_demo, GrpcClient *client)
354 QScreen *screen = nullptr;
355 HomescreenHandler *homescreenHandler = shell_data.homescreenHandler;
358 screen = qApp->primaryScreen();
360 screen = find_screen(screen_name);
363 qDebug() << "No outputs present in the system.";
368 load_agl_shell_for_ci(native, engine, shell_data.shell, screen);
370 load_agl_shell(native, engine, shell_data.shell, screen);
373 /* Delay the ready signal until after Qt has done all of its own setup
375 QTimer::singleShot(500, [shell_data](){
376 qDebug() << "sending ready to compositor";
377 agl_shell_ready(shell_data.shell);
382 app_status_callback(::agl_shell_ipc::AppStateResponse app_response, void *data)
384 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
386 if (!homescreenHandler) {
390 auto app_id = QString(app_response.app_id().c_str());
391 auto state = app_response.state();
393 qDebug() << "appstateresponse: app_id " << app_id << "state " << state;
396 case AGL_SHELL_APP_STATE_STARTED:
397 qDebug() << "Got AGL_SHELL_APP_STATE_STARTED for app_id " << app_id;
398 homescreenHandler->processAppStatusEvent(app_id, "started");
400 case AGL_SHELL_APP_STATE_TERMINATED:
401 qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
402 // handled by HomescreenHandler::processAppStatusEvent
404 case AGL_SHELL_APP_STATE_ACTIVATED:
405 qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
406 homescreenHandler->addAppToStack(app_id);
408 case AGL_SHELL_APP_STATE_DEACTIVATED:
409 qDebug() << "Got AGL_SHELL_APP_STATE_DEACTIVATED for app_id " << app_id;
410 homescreenHandler->processAppStatusEvent(app_id, "deactivated");
417 int main(int argc, char *argv[])
419 setenv("QT_QPA_PLATFORM", "wayland", 1);
420 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
422 QGuiApplication app(argc, argv);
423 const char *screen_name;
424 bool is_demo_val = false;
425 bool is_embedded_panels = false;
427 struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
429 QPlatformNativeInterface *native = qApp->platformNativeInterface();
430 screen_name = getenv("HOMESCREEN_START_SCREEN");
432 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
433 if (is_demo && strcmp(is_demo, "1") == 0)
436 const char *embedded_panels = getenv("HOMESCREEN_EMBEDDED_PANELS");
437 if (embedded_panels && strcmp(embedded_panels, "1") == 0)
438 is_embedded_panels = true;
440 QCoreApplication::setOrganizationDomain("LinuxFoundation");
441 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
442 QCoreApplication::setApplicationName("HomeScreen");
443 QCoreApplication::setApplicationVersion("0.7.0");
445 // we need to have an app_id
446 app.setDesktopFileName("homescreen");
449 register_agl_shell(native, &shell_data);
450 if (!shell_data.shell) {
451 fprintf(stderr, "agl_shell extension is not advertised. "
452 "Are you sure that agl-compositor is running?\n");
456 qDebug() << "agl-shell interface is at version " << shell_data.ver;
457 if (shell_data.ver >= 2) {
458 while (ret != -1 && shell_data.wait_for_bound) {
459 ret = wl_display_dispatch(getWlDisplay(native));
461 if (shell_data.wait_for_bound)
465 if (!shell_data.bound_ok) {
466 qInfo() << "agl_shell extension already in use by other shell client.";
472 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
474 GrpcClient *client = new GrpcClient();
475 // create a new thread to listner for gRPC events
476 std::thread th = std::thread(run_in_thread, client);
478 // Import C++ class to QML
479 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
480 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
482 ApplicationLauncher *launcher = new ApplicationLauncher();
483 launcher->setCurrent(QStringLiteral("launcher"));
485 HomescreenHandler* homescreenHandler = new HomescreenHandler(launcher);
486 shell_data.homescreenHandler = homescreenHandler;
487 shell_data.homescreenHandler->setGrpcClient(client);
489 client->AppStatusState(app_status_callback, homescreenHandler);
491 QQmlApplicationEngine engine;
492 QQmlContext *context = engine.rootContext();
494 context->setContextProperty("homescreenHandler", homescreenHandler);
495 context->setContextProperty("launcher", launcher);
496 context->setContextProperty("weather", new Weather());
497 context->setContextProperty("bluetooth", new Bluetooth(false, context));
499 load_agl_shell_app(native, &engine, shell_data,
500 screen_name, is_demo_val, client);