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>
22 #include <bluetooth.h>
24 #include "applicationlauncher.h"
25 #include "statusbarmodel.h"
26 #include "mastervolume.h"
27 #include "homescreenhandler.h"
28 #include "hmi-debug.h"
30 // meson will define these
31 #include QT_QPA_HEADER
32 #include <wayland-client.h>
34 #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)
94 agl_shell_app_on_output(void *data, struct agl_shell *agl_shell,
95 const char *app_id, const char *output_name)
97 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
98 HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
100 if (!homescreenHandler)
103 // a couple of use-cases, if there is no app_id in the app_list then it
104 // means this is a request to map the application, from the start to a
105 // different output that the default one. We'd get an
106 // AGL_SHELL_APP_STATE_STARTED which will handle activation.
108 // if there's an app_id then it means we might have gotten an event to
109 // move the application to another output; so we'd need to process it
110 // by explicitly calling processAppStatusEvent() which would ultimately
111 // activate the application on other output. We'd have to pick-up the
112 // last activated window and activate the default output.
114 // finally if the outputs are identical probably that's an user-error -
115 // but the compositor won't activate it again, so we don't handle that.
116 std::pair new_pending_app = std::pair(QString(app_id),
117 QString(output_name));
118 homescreenHandler->pending_app_list.push_back(new_pending_app);
120 if (homescreenHandler->apps_stack.contains(QString(app_id))) {
121 qDebug() << "Got event to move " << app_id <<
122 " to another output " << output_name;
124 homescreenHandler->processAppStatusEvent(app_id, "started");
129 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
130 static const struct agl_shell_listener shell_listener = {
132 agl_shell_bound_fail,
134 agl_shell_app_on_output,
139 global_add(void *data, struct wl_registry *reg, uint32_t name,
140 const char *interface, uint32_t ver)
142 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
147 if (strcmp(interface, agl_shell_interface.name) == 0) {
150 static_cast<struct agl_shell *>(
151 wl_registry_bind(reg, name, &agl_shell_interface, MIN(8, ver)));
152 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
153 agl_shell_add_listener(shell_data->shell, &shell_listener, data);
157 static_cast<struct agl_shell *>(
158 wl_registry_bind(reg, name, &agl_shell_interface, 1));
160 shell_data->ver = ver;
166 global_remove(void *data, struct wl_registry *reg, uint32_t id)
174 static const struct wl_registry_listener registry_listener = {
179 static struct wl_surface *
180 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
182 void *surf = native->nativeResourceForWindow("surface", window);
183 return static_cast<struct ::wl_surface *>(surf);
186 static struct wl_output *
187 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
189 void *output = native->nativeResourceForScreen("output", screen);
190 return static_cast<struct ::wl_output*>(output);
193 static struct wl_display *
194 getWlDisplay(QPlatformNativeInterface *native)
196 return static_cast<struct wl_display *>(
197 native->nativeResourceForIntegration("display")
203 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
205 struct wl_display *wl;
206 struct wl_registry *registry;
208 wl = getWlDisplay(native);
209 registry = wl_display_get_registry(wl);
211 wl_registry_add_listener(registry, ®istry_listener, shell_data);
213 /* Roundtrip to get all globals advertised by the compositor */
214 wl_display_roundtrip(wl);
215 wl_registry_destroy(registry);
218 static struct wl_surface *
219 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
220 QScreen *screen, QObject **qobj)
222 QObject *obj = comp->create();
223 obj->setParent(screen);
225 QWindow *win = qobject_cast<QWindow *>(obj);
228 return getWlSurface(native, win);
233 load_agl_shell(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
234 struct agl_shell *agl_shell, QScreen *screen)
236 struct wl_surface *bg;
237 struct wl_output *output;
239 int32_t width, height;
241 QSize size = screen->size();
243 // this incorporates the panels directly, but in doing so, it
244 // would also need to specify an activation area the same area
245 // in order to void overlapping any new activation window
246 QQmlComponent bg_comp(engine, QUrl("qrc:/background_with_panels.qml"));
247 qInfo() << bg_comp.errors();
249 bg = create_component(native, &bg_comp, screen, &qobj_bg);
251 output = getWlOutput(native, screen);
253 qDebug() << "Normal mode - with single surface";
254 qDebug() << "Setting homescreen to screen " << screen->name();
255 agl_shell_set_background(agl_shell, bg, output);
257 // 216 is the width size of the panel
261 width = size.width();
262 height = size.height() - (2 * y);
264 qDebug() << "Using custom rectangle " << width << "x" << height
265 << "+" << x << "x" << y << " for activation";
266 qDebug() << "Panels should be embedded the background surface";
268 #ifdef AGL_SHELL_SET_ACTIVATE_REGION_SINCE_VERSION
269 agl_shell_set_activate_region(agl_shell, output,
270 x, y, width, height);
275 load_agl_shell_for_ci(QPlatformNativeInterface *native,
276 QQmlApplicationEngine *engine,
277 struct agl_shell *agl_shell, QScreen *screen)
279 struct wl_surface *bg, *top, *bottom;
280 struct wl_output *output;
281 QObject *qobj_bg, *qobj_top, *qobj_bottom;
283 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
284 qInfo() << bg_comp.errors();
286 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
287 qInfo() << top_comp.errors();
289 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
290 qInfo() << bot_comp.errors();
292 top = create_component(native, &top_comp, screen, &qobj_top);
293 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
294 bg = create_component(native, &bg_comp, screen, &qobj_bg);
296 /* engine.rootObjects() works only if we had a load() */
297 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
299 qDebug() << "got statusBar objectname, doing init()";
300 statusBar->init(engine->rootContext());
303 output = getWlOutput(native, screen);
305 qDebug() << "Setting homescreen to screen " << screen->name();
307 agl_shell_set_background(agl_shell, bg, output);
308 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
309 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
311 qDebug() << "CI mode - with multiple surfaces";
315 app_status_callback(::agl_shell_ipc::AppStateResponse app_response, void *data);
318 run_in_thread(GrpcClient *client)
320 grpc::Status status = client->Wait();
324 load_agl_shell_app(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
325 struct shell_data shell_data, const char *screen_name, bool is_demo)
327 QScreen *screen = nullptr;
328 HomescreenHandler *homescreenHandler = shell_data.homescreenHandler;
331 screen = qApp->primaryScreen();
333 screen = find_screen(screen_name);
336 qDebug() << "No outputs present in the system.";
341 load_agl_shell_for_ci(native, engine, shell_data.shell, screen);
343 load_agl_shell(native, engine, shell_data.shell, screen);
346 /* Delay the ready signal until after Qt has done all of its own setup
348 QTimer::singleShot(500, [shell_data](){
349 qDebug() << "sending ready to compositor";
350 agl_shell_ready(shell_data.shell);
355 app_status_callback(::agl_shell_ipc::AppStateResponse app_response, void *data)
357 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
359 if (!homescreenHandler) {
363 auto app_id = QString(app_response.app_id().c_str());
364 auto state = app_response.state();
366 qDebug() << "appstateresponse: app_id " << app_id << "state " << state;
369 case AGL_SHELL_APP_STATE_STARTED:
370 qDebug() << "Got AGL_SHELL_APP_STATE_STARTED for app_id " << app_id;
371 homescreenHandler->processAppStatusEvent(app_id, "started");
373 case AGL_SHELL_APP_STATE_TERMINATED:
374 qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
375 homescreenHandler->processAppStatusEvent(app_id, "terminated");
377 case AGL_SHELL_APP_STATE_ACTIVATED:
378 qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
379 homescreenHandler->addAppToStack(app_id);
381 case AGL_SHELL_APP_STATE_DEACTIVATED:
382 qDebug() << "Got AGL_SHELL_APP_STATE_DEACTIVATED for app_id " << app_id;
383 homescreenHandler->processAppStatusEvent(app_id, "deactivated");
390 int main(int argc, char *argv[])
392 setenv("QT_QPA_PLATFORM", "wayland", 1);
393 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
395 QGuiApplication app(argc, argv);
396 const char *screen_name;
397 bool is_demo_val = false;
398 bool is_embedded_panels = false;
400 struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
402 QPlatformNativeInterface *native = qApp->platformNativeInterface();
403 screen_name = getenv("HOMESCREEN_START_SCREEN");
405 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
406 if (is_demo && strcmp(is_demo, "1") == 0)
409 const char *embedded_panels = getenv("HOMESCREEN_EMBEDDED_PANELS");
410 if (embedded_panels && strcmp(embedded_panels, "1") == 0)
411 is_embedded_panels = true;
413 QCoreApplication::setOrganizationDomain("LinuxFoundation");
414 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
415 QCoreApplication::setApplicationName("HomeScreen");
416 QCoreApplication::setApplicationVersion("0.7.0");
418 // we need to have an app_id
419 app.setDesktopFileName("homescreen");
421 GrpcClient *client = new GrpcClient();
422 // create a new thread to listner for gRPC events
423 std::thread th = std::thread(run_in_thread, client);
425 register_agl_shell(native, &shell_data);
426 if (!shell_data.shell) {
427 fprintf(stderr, "agl_shell extension is not advertised. "
428 "Are you sure that agl-compositor is running?\n");
432 qDebug() << "agl-shell interface is at version " << shell_data.ver;
433 if (shell_data.ver >= 2) {
434 while (ret != -1 && shell_data.wait_for_bound) {
435 ret = wl_display_dispatch(getWlDisplay(native));
437 if (shell_data.wait_for_bound)
441 if (!shell_data.bound_ok) {
442 qInfo() << "agl_shell extension already in use by other shell client.";
448 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
450 // Import C++ class to QML
451 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
452 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
454 ApplicationLauncher *launcher = new ApplicationLauncher();
455 launcher->setCurrent(QStringLiteral("launcher"));
457 HomescreenHandler* homescreenHandler = new HomescreenHandler(launcher);
458 shell_data.homescreenHandler = homescreenHandler;
459 shell_data.homescreenHandler->setGrpcClient(client);
461 // blocks until we're sure connected with the server
462 HMI_DEBUG("HomescreenHandler", "Checking if connected to the gRPC server...");
463 client->WaitForConnected(500, 10);
464 client->AppStatusState(app_status_callback, homescreenHandler);
466 QQmlApplicationEngine engine;
467 QQmlContext *context = engine.rootContext();
469 context->setContextProperty("homescreenHandler", homescreenHandler);
470 context->setContextProperty("launcher", launcher);
471 context->setContextProperty("weather", new Weather());
472 context->setContextProperty("bluetooth", new Bluetooth(false, context));
474 load_agl_shell_app(native, &engine, shell_data,
475 screen_name, is_demo_val);