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;
51 agl_shell_desktop_application(void *data,
52 struct agl_shell_desktop *agl_shell_desktop,
55 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
57 if (homescreenHandler)
58 homescreenHandler->addAppToStack(app_id);
62 agl_shell_desktop_state_app(void *data,
63 struct agl_shell_desktop *agl_shell_desktop,
69 HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
71 if (homescreenHandler && state == AGL_SHELL_DESKTOP_APP_STATE_DESTROYED)
72 homescreenHandler->appTerminated(app_id);
75 static const struct agl_shell_desktop_listener shell_desktop_listener = {
76 agl_shell_desktop_application,
77 agl_shell_desktop_state_app
81 global_add(void *data, struct wl_registry *reg, uint32_t name,
82 const char *interface, uint32_t)
84 struct shell_data *shell_data = static_cast<struct shell_data *>(data);
89 if (strcmp(interface, agl_shell_interface.name) == 0) {
90 shell_data->shell = static_cast<struct agl_shell *>(
91 wl_registry_bind(reg, name, &agl_shell_interface, 1)
95 if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
96 shell_data->shell_desktop = static_cast<struct agl_shell_desktop *>(
97 wl_registry_bind(reg, name, &agl_shell_desktop_interface, 1)
103 global_remove(void *data, struct wl_registry *reg, uint32_t id)
111 static const struct wl_registry_listener registry_listener = {
116 static struct wl_surface *
117 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
119 void *surf = native->nativeResourceForWindow("surface", window);
120 return static_cast<struct ::wl_surface *>(surf);
123 static struct wl_output *
124 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
126 void *output = native->nativeResourceForScreen("output", screen);
127 return static_cast<struct ::wl_output*>(output);
132 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
134 struct wl_display *wl;
135 struct wl_registry *registry;
137 wl = static_cast<struct wl_display *>(
138 native->nativeResourceForIntegration("display")
140 registry = wl_display_get_registry(wl);
142 wl_registry_add_listener(registry, ®istry_listener, shell_data);
144 /* Roundtrip to get all globals advertised by the compositor */
145 wl_display_roundtrip(wl);
146 wl_registry_destroy(registry);
149 static struct wl_surface *
150 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
151 QScreen *screen, QObject **qobj)
153 QObject *obj = comp->create();
154 obj->setParent(screen);
156 QWindow *win = qobject_cast<QWindow *>(obj);
159 return getWlSurface(native, win);
163 find_screen(const char *screen_name)
165 QList<QScreen *> screens = qApp->screens();
166 QScreen *found = nullptr;
167 QString qstr_name = QString::fromUtf8(screen_name, -1);
169 for (QScreen *xscreen : screens) {
170 if (qstr_name == xscreen->name()) {
180 load_agl_shell_app(QPlatformNativeInterface *native,
181 QQmlApplicationEngine *engine,
182 struct agl_shell *agl_shell,
183 const char *screen_name,
186 struct wl_surface *bg, *top, *bottom;
187 struct wl_output *output;
188 QObject *qobj_bg, *qobj_top, *qobj_bottom;
189 QScreen *screen = nullptr;
192 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
193 qInfo() << bg_comp.errors();
195 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
196 qInfo() << top_comp.errors();
198 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
199 qInfo() << bot_comp.errors();
201 top = create_component(native, &top_comp, screen, &qobj_top);
202 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
203 bg = create_component(native, &bg_comp, screen, &qobj_bg);
205 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
206 qInfo() << bg_comp.errors();
208 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
209 qInfo() << top_comp.errors();
211 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
212 qInfo() << bot_comp.errors();
214 top = create_component(native, &top_comp, screen, &qobj_top);
215 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
216 bg = create_component(native, &bg_comp, screen, &qobj_bg);
220 screen = qApp->primaryScreen();
222 screen = find_screen(screen_name);
225 qDebug() << "No outputs present in the system.";
229 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
230 "first screen " << qApp->screens().first()->name();
231 output = getWlOutput(native, screen);
233 /* engine.rootObjects() works only if we had a load() */
234 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
236 qDebug() << "got statusBar objectname, doing init()";
237 statusBar->init(engine->rootContext());
240 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
241 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
242 qDebug() << "Setting homescreen to screen " << screen->name();
244 agl_shell_set_background(agl_shell, bg, output);
246 /* Delay the ready signal until after Qt has done all of its own setup
248 QTimer::singleShot(500, [agl_shell](){
249 agl_shell_ready(agl_shell);
253 int main(int argc, char *argv[])
255 setenv("QT_QPA_PLATFORM", "wayland", 1);
256 setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
257 QGuiApplication a(argc, argv);
258 const char *screen_name;
259 bool is_demo_val = false;
260 struct shell_data shell_data = { nullptr, nullptr };
262 QPlatformNativeInterface *native = qApp->platformNativeInterface();
263 screen_name = getenv("HOMESCREEN_START_SCREEN");
265 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
266 if (is_demo && strcmp(is_demo, "1") == 0)
269 QCoreApplication::setOrganizationDomain("LinuxFoundation");
270 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
271 QCoreApplication::setApplicationName("HomeScreen");
272 QCoreApplication::setApplicationVersion("0.7.0");
273 /* we need to have an app_id */
274 a.setDesktopFileName("homescreen");
276 register_agl_shell(native, &shell_data);
277 if (!shell_data.shell) {
278 fprintf(stderr, "agl_shell extension is not advertised. "
279 "Are you sure that agl-compositor is running?\n");
282 if (!shell_data.shell_desktop) {
283 fprintf(stderr, "agl_shell_desktop extension is not advertised. "
284 "Are you sure that agl-compositor is running?\n");
288 std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
289 Shell *aglShell = new Shell(agl_shell, &a);
291 // import C++ class to QML
292 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
293 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
295 ApplicationLauncher *launcher = new ApplicationLauncher();
296 launcher->setCurrent(QStringLiteral("launcher"));
297 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
298 homescreenHandler->init();
300 agl_shell_desktop_add_listener(shell_data.shell_desktop, &shell_desktop_listener, homescreenHandler);
302 QQmlApplicationEngine engine;
303 QQmlContext *context = engine.rootContext();
305 context->setContextProperty("homescreenHandler", homescreenHandler);
306 context->setContextProperty("launcher", launcher);
307 context->setContextProperty("weather", new Weather());
308 context->setContextProperty("bluetooth", new Bluetooth(false, context));
310 // we add it here even if we don't use it
311 context->setContextProperty("shell", aglShell);
313 /* instead of loading main.qml we load one-by-one each of the QMLs,
314 * divided now between several surfaces: panels, background.
316 load_agl_shell_app(native, &engine, shell_data.shell, screen_name, is_demo_val);