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>
31 #include <bluetooth.h>
33 #include "applicationlauncher.h"
34 #include "statusbarmodel.h"
35 #include "mastervolume.h"
36 #include "homescreenhandler.h"
37 #include "hmi-debug.h"
39 #include <qpa/qplatformnativeinterface.h>
40 #include <wayland-client.h>
42 #include "wayland-agl-shell-client-protocol.h"
43 #include "wayland-agl-shell-desktop-client-protocol.h"
47 struct agl_shell *shell;
48 struct agl_shell_desktop *shell_desktop;
52 agl_shell_desktop_application(void *data,
53 struct agl_shell_desktop *agl_shell_desktop,
56 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
58 if (homescreenHandler)
59 homescreenHandler->addAppToStack(app_id);
63 agl_shell_desktop_state_app(void *data,
64 struct agl_shell_desktop *agl_shell_desktop,
70 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
72 if (homescreenHandler && state == AGL_SHELL_DESKTOP_APP_STATE_DESTROYED)
73 homescreenHandler->appTerminated(app_id);
76 static const struct agl_shell_desktop_listener shell_desktop_listener = {
77 agl_shell_desktop_application,
78 agl_shell_desktop_state_app
82 global_add(void *data, struct wl_registry *reg, uint32_t name,
83 const char *interface, uint32_t)
85 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
90 if (strcmp(interface, agl_shell_interface.name) == 0) {
91 shell_data->shell = static_cast<struct agl_shell *>(
92 wl_registry_bind(reg, name, &agl_shell_interface, 1)
96 if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
97 shell_data->shell_desktop = static_cast<struct agl_shell_desktop *>(
98 wl_registry_bind(reg, name, &agl_shell_desktop_interface, 1)
104 global_remove(void *data, struct wl_registry *reg, uint32_t id)
112 static const struct wl_registry_listener registry_listener = {
117 static struct wl_surface *
118 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
120 void *surf = native->nativeResourceForWindow("surface", window);
121 return static_cast<struct ::wl_surface *>(surf);
124 static struct wl_output *
125 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
127 void *output = native->nativeResourceForScreen("output", screen);
128 return static_cast<struct ::wl_output*>(output);
133 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
135 struct wl_display *wl;
136 struct wl_registry *registry;
138 wl = static_cast<struct wl_display *>(
139 native->nativeResourceForIntegration("display")
141 registry = wl_display_get_registry(wl);
143 wl_registry_add_listener(registry, ®istry_listener, shell_data);
145 /* Roundtrip to get all globals advertised by the compositor */
146 wl_display_roundtrip(wl);
147 wl_registry_destroy(registry);
150 static struct wl_surface *
151 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
152 QScreen *screen, QObject **qobj)
154 QObject *obj = comp->create();
155 obj->setParent(screen);
157 QWindow *win = qobject_cast<QWindow *>(obj);
160 return getWlSurface(native, win);
164 find_screen(const char *screen_name)
166 QList<QScreen *> screens = qApp->screens();
167 QScreen *found = nullptr;
168 QString qstr_name = QString::fromUtf8(screen_name, -1);
170 for (QScreen *xscreen : screens) {
171 if (qstr_name == xscreen->name()) {
181 load_agl_shell_app(QPlatformNativeInterface *native,
182 QQmlApplicationEngine *engine,
183 struct agl_shell *agl_shell,
184 const char *screen_name,
187 struct wl_surface *bg, *top, *bottom;
188 struct wl_output *output;
189 QObject *qobj_bg, *qobj_top, *qobj_bottom;
190 QScreen *screen = nullptr;
193 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
194 qInfo() << bg_comp.errors();
196 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
197 qInfo() << top_comp.errors();
199 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
200 qInfo() << bot_comp.errors();
202 top = create_component(native, &top_comp, screen, &qobj_top);
203 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
204 bg = create_component(native, &bg_comp, screen, &qobj_bg);
206 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
207 qInfo() << bg_comp.errors();
209 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
210 qInfo() << top_comp.errors();
212 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
213 qInfo() << bot_comp.errors();
215 top = create_component(native, &top_comp, screen, &qobj_top);
216 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
217 bg = create_component(native, &bg_comp, screen, &qobj_bg);
221 screen = qApp->primaryScreen();
223 screen = find_screen(screen_name);
225 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
226 "first screen " << qApp->screens().first()->name();
227 output = getWlOutput(native, screen);
229 /* engine.rootObjects() works only if we had a load() */
230 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
232 qDebug() << "got statusBar objectname, doing init()";
233 statusBar->init(engine->rootContext());
236 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
237 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
238 qDebug() << "Setting homescreen to screen " << screen->name();
240 agl_shell_set_background(agl_shell, bg, output);
242 /* Delay the ready signal until after Qt has done all of its own setup
244 QTimer::singleShot(500, [agl_shell](){
245 agl_shell_ready(agl_shell);
249 int main(int argc, char *argv[])
251 setenv("QT_QPA_PLATFORM", "wayland", 1);
252 QGuiApplication a(argc, argv);
253 const char *screen_name;
254 bool is_demo_val = false;
255 struct shell_data shell_data = { nullptr, nullptr };
257 QPlatformNativeInterface *native = qApp->platformNativeInterface();
258 screen_name = getenv("HOMESCREEN_START_SCREEN");
260 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
261 if (is_demo && strcmp(is_demo, "1") == 0)
264 QCoreApplication::setOrganizationDomain("LinuxFoundation");
265 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
266 QCoreApplication::setApplicationName("HomeScreen");
267 QCoreApplication::setApplicationVersion("0.7.0");
268 /* we need to have an app_id */
269 a.setDesktopFileName("homescreen");
271 register_agl_shell(native, &shell_data);
272 if (!shell_data.shell) {
273 fprintf(stderr, "agl_shell extension is not advertised. "
274 "Are you sure that agl-compositor is running?\n");
277 if (!shell_data.shell_desktop) {
278 fprintf(stderr, "agl_shell_desktop extension is not advertised. "
279 "Are you sure that agl-compositor is running?\n");
283 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
284 Shell *aglShell = new Shell(agl_shell, &a);
286 // import C++ class to QML
287 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
288 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
290 ApplicationLauncher *launcher = new ApplicationLauncher();
291 launcher->setCurrent(QStringLiteral("launcher"));
292 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
293 homescreenHandler->init();
295 agl_shell_desktop_add_listener(shell_data.shell_desktop, &shell_desktop_listener, homescreenHandler);
297 QQmlApplicationEngine engine;
298 QQmlContext *context = engine.rootContext();
300 context->setContextProperty("homescreenHandler", homescreenHandler);
301 context->setContextProperty("launcher", launcher);
303 context->setContextProperty("weather", new Weather(bindingAddress));
304 context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, context));
306 // we add it here even if we don't use it
307 context->setContextProperty("shell", aglShell);
309 /* instead of loading main.qml we load one-by-one each of the QMLs,
310 * divided now between several surfaces: panels, background.
312 load_agl_shell_app(native, &engine, shell_data.shell, screen_name, is_demo_val);