2 * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3 * Copyright (c) 2017, 2018 TOYOTA MOTOR CORPORATION
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include <QGuiApplication>
19 #include <QCommandLineParser>
20 #include <QtCore/QUrlQuery>
21 #include <QtGui/QGuiApplication>
22 #include <QtQml/QQmlApplicationEngine>
23 #include <QtQml/QQmlContext>
24 #include <QtQml/QQmlComponent>
25 #include <QtQml/qqml.h>
26 #include <QQuickWindow>
30 #include <bluetooth.h>
32 #include "applicationlauncher.h"
33 #include "statusbarmodel.h"
34 #include "mastervolume.h"
35 #include "homescreenhandler.h"
36 #include "hmi-debug.h"
38 #include <qpa/qplatformnativeinterface.h>
39 #include <wayland-client.h>
41 #include "wayland-agl-shell-client-protocol.h"
42 #include "wayland-agl-shell-desktop-client-protocol.h"
46 struct agl_shell *shell;
47 struct agl_shell_desktop *shell_desktop;
54 agl_shell_desktop_application(void *data,
55 struct agl_shell_desktop *agl_shell_desktop,
58 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
60 if (homescreenHandler)
61 homescreenHandler->addAppToStack(app_id);
65 agl_shell_desktop_state_app(void *data,
66 struct agl_shell_desktop *agl_shell_desktop,
72 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
74 if (homescreenHandler && state == AGL_SHELL_DESKTOP_APP_STATE_DESTROYED)
75 homescreenHandler->appTerminated(app_id);
79 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
81 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
82 shell_data->wait_for_bound = false;
84 shell_data->bound_ok = true;
88 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
90 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
91 shell_data->wait_for_bound = false;
93 shell_data->bound_ok = false;
96 static const struct agl_shell_listener shell_listener = {
101 static const struct agl_shell_desktop_listener shell_desktop_listener = {
102 agl_shell_desktop_application,
103 agl_shell_desktop_state_app
107 global_add(void *data, struct wl_registry *reg, uint32_t name,
108 const char *interface, uint32_t ver)
110 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
115 if (strcmp(interface, agl_shell_interface.name) == 0) {
116 shell_data->shell = static_cast<struct agl_shell *>(
117 wl_registry_bind(reg, name, &agl_shell_interface, 3));
118 agl_shell_add_listener(shell_data->shell, &shell_listener, data);
121 if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
122 shell_data->shell_desktop = static_cast<struct agl_shell_desktop *>(
123 wl_registry_bind(reg, name, &agl_shell_desktop_interface, 1)
129 global_remove(void *data, struct wl_registry *reg, uint32_t id)
137 static const struct wl_registry_listener registry_listener = {
142 static struct wl_surface *
143 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
145 void *surf = native->nativeResourceForWindow("surface", window);
146 return static_cast<struct ::wl_surface *>(surf);
149 static struct wl_output *
150 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
152 void *output = native->nativeResourceForScreen("output", screen);
153 return static_cast<struct ::wl_output*>(output);
156 static struct wl_display *
157 getWlDisplay(QPlatformNativeInterface *native)
159 return static_cast<struct wl_display *>(
160 native->nativeResourceForIntegration("display")
165 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
167 struct wl_display *wl;
168 struct wl_registry *registry;
170 wl = getWlDisplay(native);
171 registry = wl_display_get_registry(wl);
173 wl_registry_add_listener(registry, ®istry_listener, shell_data);
175 /* Roundtrip to get all globals advertised by the compositor */
176 wl_display_roundtrip(wl);
177 wl_registry_destroy(registry);
180 static struct wl_surface *
181 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
182 QScreen *screen, QObject **qobj)
184 QObject *obj = comp->create();
185 obj->setParent(screen);
187 QWindow *win = qobject_cast<QWindow *>(obj);
190 return getWlSurface(native, win);
194 find_screen(const char *screen_name)
196 QList<QScreen *> screens = qApp->screens();
197 QScreen *found = nullptr;
198 QString qstr_name = QString::fromUtf8(screen_name, -1);
200 for (QScreen *xscreen : screens) {
201 if (qstr_name == xscreen->name()) {
211 load_agl_shell_app(QPlatformNativeInterface *native,
212 QQmlApplicationEngine *engine,
213 struct agl_shell *agl_shell,
214 const char *screen_name,
217 struct wl_surface *bg, *top, *bottom;
218 struct wl_output *output;
219 QObject *qobj_bg, *qobj_top, *qobj_bottom;
220 QScreen *screen = nullptr;
223 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
224 qInfo() << bg_comp.errors();
226 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
227 qInfo() << top_comp.errors();
229 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
230 qInfo() << bot_comp.errors();
232 top = create_component(native, &top_comp, screen, &qobj_top);
233 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
234 bg = create_component(native, &bg_comp, screen, &qobj_bg);
236 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
237 qInfo() << bg_comp.errors();
239 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
240 qInfo() << top_comp.errors();
242 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
243 qInfo() << bot_comp.errors();
245 top = create_component(native, &top_comp, screen, &qobj_top);
246 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
247 bg = create_component(native, &bg_comp, screen, &qobj_bg);
251 screen = qApp->primaryScreen();
253 screen = find_screen(screen_name);
256 qDebug() << "No outputs present in the system.";
260 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
261 "first screen " << qApp->screens().first()->name();
262 output = getWlOutput(native, screen);
264 /* engine.rootObjects() works only if we had a load() */
265 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
267 qDebug() << "got statusBar objectname, doing init()";
268 statusBar->init(engine->rootContext());
271 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
272 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
273 qDebug() << "Setting homescreen to screen " << screen->name();
275 agl_shell_set_background(agl_shell, bg, output);
277 /* Delay the ready signal until after Qt has done all of its own setup
279 QTimer::singleShot(500, [agl_shell](){
280 agl_shell_ready(agl_shell);
284 int main(int argc, char *argv[])
286 setenv("QT_QPA_PLATFORM", "wayland", 1);
287 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
288 QGuiApplication a(argc, argv);
289 const char *screen_name;
290 bool is_demo_val = false;
292 struct shell_data shell_data = { nullptr, nullptr, true, false };
294 QPlatformNativeInterface *native = qApp->platformNativeInterface();
295 screen_name = getenv("HOMESCREEN_START_SCREEN");
297 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
298 if (is_demo && strcmp(is_demo, "1") == 0)
301 QCoreApplication::setOrganizationDomain("LinuxFoundation");
302 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
303 QCoreApplication::setApplicationName("HomeScreen");
304 QCoreApplication::setApplicationVersion("0.7.0");
305 /* we need to have an app_id */
306 a.setDesktopFileName("homescreen");
308 register_agl_shell(native, &shell_data);
309 if (!shell_data.shell) {
310 fprintf(stderr, "agl_shell extension is not advertised. "
311 "Are you sure that agl-compositor is running?\n");
314 if (!shell_data.shell_desktop) {
315 fprintf(stderr, "agl_shell_desktop extension is not advertised. "
316 "Are you sure that agl-compositor is running?\n");
320 while (ret != -1 && shell_data.wait_for_bound) {
321 ret = wl_display_dispatch(getWlDisplay(native));
323 if (shell_data.wait_for_bound)
327 // verify that did bound successfully
328 if (!shell_data.bound_ok) {
329 fprintf(stderr, "agl_shell extension already in use by other shell client.\n");
333 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
334 Shell *aglShell = new Shell(agl_shell, &a);
336 // import C++ class to QML
337 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
338 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
340 ApplicationLauncher *launcher = new ApplicationLauncher();
341 launcher->setCurrent(QStringLiteral("launcher"));
342 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
343 homescreenHandler->init();
345 agl_shell_desktop_add_listener(shell_data.shell_desktop, &shell_desktop_listener, homescreenHandler);
347 QQmlApplicationEngine engine;
348 QQmlContext *context = engine.rootContext();
350 context->setContextProperty("homescreenHandler", homescreenHandler);
351 context->setContextProperty("launcher", launcher);
352 context->setContextProperty("weather", new Weather());
353 context->setContextProperty("bluetooth", new Bluetooth(false, context));
355 // we add it here even if we don't use it
356 context->setContextProperty("shell", aglShell);
358 /* instead of loading main.qml we load one-by-one each of the QMLs,
359 * divided now between several surfaces: panels, background.
361 load_agl_shell_app(native, &engine, shell_data.shell, screen_name, is_demo_val);