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)
90 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
91 HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
93 if (!homescreenHandler)
97 case AGL_SHELL_APP_STATE_STARTED:
98 qDebug() << "Got AGL_SHELL_APP_STATE_STARTED for app_id " << app_id;
99 homescreenHandler->processAppStatusEvent(app_id, "started");
101 case AGL_SHELL_APP_STATE_TERMINATED:
102 qDebug() << "Got AGL_SHELL_APP_STATE_TERMINATED for app_id " << app_id;
103 // handled by HomescreenHandler::processAppStatusEvent
105 case AGL_SHELL_APP_STATE_ACTIVATED:
106 qDebug() << "Got AGL_SHELL_APP_STATE_ACTIVATED for app_id " << app_id;
107 homescreenHandler->addAppToStack(app_id);
109 case AGL_SHELL_APP_STATE_DEACTIVATED:
110 qDebug() << "Got AGL_SHELL_APP_STATE_DEACTIVATED for app_id " << app_id;
111 homescreenHandler->processAppStatusEvent(app_id, "deactivated");
119 agl_shell_app_on_output(void *data, struct agl_shell *agl_shell,
120 const char *app_id, const char *output_name)
122 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
123 HomescreenHandler *homescreenHandler = shell_data->homescreenHandler;
125 if (!homescreenHandler)
128 // a couple of use-cases, if there is no app_id in the app_list then it
129 // means this is a request to map the application, from the start to a
130 // different output that the default one. We'd get an
131 // AGL_SHELL_APP_STATE_STARTED which will handle activation.
133 // if there's an app_id then it means we might have gotten an event to
134 // move the application to another output; so we'd need to process it
135 // by explicitly calling processAppStatusEvent() which would ultimately
136 // activate the application on other output. We'd have to pick-up the
137 // last activated window and activate the default output.
139 // finally if the outputs are identical probably that's an user-error -
140 // but the compositor won't activate it again, so we don't handle that.
141 std::pair new_pending_app = std::pair(QString(app_id),
142 QString(output_name));
143 homescreenHandler->pending_app_list.push_back(new_pending_app);
145 if (homescreenHandler->apps_stack.contains(QString(app_id))) {
146 qDebug() << "Gove event to move " << app_id <<
147 " to another output " << output_name;
148 homescreenHandler->processAppStatusEvent(app_id, "started");
153 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
154 static const struct agl_shell_listener shell_listener = {
156 agl_shell_bound_fail,
158 agl_shell_app_on_output,
163 global_add(void *data, struct wl_registry *reg, uint32_t name,
164 const char *interface, uint32_t ver)
166 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
171 if (strcmp(interface, agl_shell_interface.name) == 0) {
174 static_cast<struct agl_shell *>(
175 wl_registry_bind(reg, name, &agl_shell_interface, MIN(8, ver)));
176 #ifdef AGL_SHELL_BOUND_OK_SINCE_VERSION
177 agl_shell_add_listener(shell_data->shell, &shell_listener, data);
181 static_cast<struct agl_shell *>(
182 wl_registry_bind(reg, name, &agl_shell_interface, 1));
184 shell_data->ver = ver;
190 global_remove(void *data, struct wl_registry *reg, uint32_t id)
198 static const struct wl_registry_listener registry_listener = {
203 static struct wl_surface *
204 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
206 void *surf = native->nativeResourceForWindow("surface", window);
207 return static_cast<struct ::wl_surface *>(surf);
210 static struct wl_output *
211 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
213 void *output = native->nativeResourceForScreen("output", screen);
214 return static_cast<struct ::wl_output*>(output);
217 static struct wl_display *
218 getWlDisplay(QPlatformNativeInterface *native)
220 return static_cast<struct wl_display *>(
221 native->nativeResourceForIntegration("display")
227 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
229 struct wl_display *wl;
230 struct wl_registry *registry;
232 wl = getWlDisplay(native);
233 registry = wl_display_get_registry(wl);
235 wl_registry_add_listener(registry, ®istry_listener, shell_data);
237 /* Roundtrip to get all globals advertised by the compositor */
238 wl_display_roundtrip(wl);
239 wl_registry_destroy(registry);
242 static struct wl_surface *
243 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
244 QScreen *screen, QObject **qobj)
246 QObject *obj = comp->create();
247 obj->setParent(screen);
249 QWindow *win = qobject_cast<QWindow *>(obj);
252 return getWlSurface(native, win);
257 load_agl_shell(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
258 struct agl_shell *agl_shell, QScreen *screen)
260 struct wl_surface *bg;
261 struct wl_output *output;
263 int32_t width, height;
265 QSize size = screen->size();
267 // this incorporates the panels directly, but in doing so, it
268 // would also need to specify an activation area the same area
269 // in order to void overlapping any new activation window
270 QQmlComponent bg_comp(engine, QUrl("qrc:/background_with_panels.qml"));
271 qInfo() << bg_comp.errors();
273 bg = create_component(native, &bg_comp, screen, &qobj_bg);
275 output = getWlOutput(native, screen);
277 qDebug() << "Normal mode - with single surface";
278 qDebug() << "Setting homescreen to screen " << screen->name();
279 agl_shell_set_background(agl_shell, bg, output);
281 // 216 is the width size of the panel
285 width = size.width();
286 height = size.height() - (2 * y);
288 qDebug() << "Using custom rectangle " << width << "x" << height
289 << "+" << x << "x" << y << " for activation";
290 qDebug() << "Panels should be embedded the background surface";
292 #ifdef AGL_SHELL_SET_ACTIVATE_REGION_SINCE_VERSION
293 agl_shell_set_activate_region(agl_shell, output,
294 x, y, width, height);
299 load_agl_shell_for_ci(QPlatformNativeInterface *native,
300 QQmlApplicationEngine *engine,
301 struct agl_shell *agl_shell, QScreen *screen)
303 struct wl_surface *bg, *top, *bottom;
304 struct wl_output *output;
305 QObject *qobj_bg, *qobj_top, *qobj_bottom;
307 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
308 qInfo() << bg_comp.errors();
310 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
311 qInfo() << top_comp.errors();
313 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
314 qInfo() << bot_comp.errors();
316 top = create_component(native, &top_comp, screen, &qobj_top);
317 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
318 bg = create_component(native, &bg_comp, screen, &qobj_bg);
320 /* engine.rootObjects() works only if we had a load() */
321 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
323 qDebug() << "got statusBar objectname, doing init()";
324 statusBar->init(engine->rootContext());
327 output = getWlOutput(native, screen);
329 qDebug() << "Setting homescreen to screen " << screen->name();
331 agl_shell_set_background(agl_shell, bg, output);
332 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
333 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
335 qDebug() << "CI mode - with multiple surfaces";
339 load_agl_shell_app(QPlatformNativeInterface *native, QQmlApplicationEngine *engine,
340 struct agl_shell *agl_shell, const char *screen_name, bool is_demo)
342 QScreen *screen = nullptr;
345 screen = qApp->primaryScreen();
347 screen = find_screen(screen_name);
350 qDebug() << "No outputs present in the system.";
355 load_agl_shell_for_ci(native, engine, agl_shell, screen);
357 load_agl_shell(native, engine, agl_shell, screen);
360 /* Delay the ready signal until after Qt has done all of its own setup
362 QTimer::singleShot(500, [agl_shell](){
363 qDebug() << "sending ready to compositor";
364 agl_shell_ready(agl_shell);
369 run_in_thread(GrpcClient *client)
371 grpc::Status status = client->Wait();
375 app_status_callback(::agl_shell_ipc::AppStateResponse app_response)
377 std::cout << " >> AppStateResponse app_id " <<
378 app_response.app_id() << ", with state " <<
379 app_response.state() << std::endl;
382 int main(int argc, char *argv[])
384 setenv("QT_QPA_PLATFORM", "wayland", 1);
385 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
387 QGuiApplication app(argc, argv);
388 const char *screen_name;
389 bool is_demo_val = false;
390 bool is_embedded_panels = false;
392 struct shell_data shell_data = { nullptr, nullptr, true, false, 0 };
394 QPlatformNativeInterface *native = qApp->platformNativeInterface();
395 screen_name = getenv("HOMESCREEN_START_SCREEN");
397 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
398 if (is_demo && strcmp(is_demo, "1") == 0)
401 const char *embedded_panels = getenv("HOMESCREEN_EMBEDDED_PANELS");
402 if (embedded_panels && strcmp(embedded_panels, "1") == 0)
403 is_embedded_panels = true;
405 QCoreApplication::setOrganizationDomain("LinuxFoundation");
406 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
407 QCoreApplication::setApplicationName("HomeScreen");
408 QCoreApplication::setApplicationVersion("0.7.0");
410 // we need to have an app_id
411 app.setDesktopFileName("homescreen");
413 register_agl_shell(native, &shell_data);
414 if (!shell_data.shell) {
415 fprintf(stderr, "agl_shell extension is not advertised. "
416 "Are you sure that agl-compositor is running?\n");
420 qDebug() << "agl-shell interface is at version " << shell_data.ver;
421 if (shell_data.ver >= 2) {
422 while (ret != -1 && shell_data.wait_for_bound) {
423 ret = wl_display_dispatch(getWlDisplay(native));
425 if (shell_data.wait_for_bound)
429 if (!shell_data.bound_ok) {
430 qInfo() << "agl_shell extension already in use by other shell client.";
436 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
438 // Import C++ class to QML
439 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
440 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
442 ApplicationLauncher *launcher = new ApplicationLauncher();
443 launcher->setCurrent(QStringLiteral("launcher"));
445 GrpcClient *client = new GrpcClient();
447 // create a new thread to listner for gRPC events
448 std::thread th = std::thread(run_in_thread, client);
449 client->AppStatusState(app_status_callback);
451 HomescreenHandler* homescreenHandler = new HomescreenHandler(launcher, client);
452 shell_data.homescreenHandler = homescreenHandler;
454 QQmlApplicationEngine engine;
455 QQmlContext *context = engine.rootContext();
457 context->setContextProperty("homescreenHandler", homescreenHandler);
458 context->setContextProperty("launcher", launcher);
459 context->setContextProperty("weather", new Weather());
460 context->setContextProperty("bluetooth", new Bluetooth(false, context));
462 load_agl_shell_app(native, &engine, shell_data.shell,
463 screen_name, is_demo_val);