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"
46 global_add(void *data, struct wl_registry *reg, uint32_t name,
47 const char *interface, uint32_t)
49 struct agl_shell **shell = static_cast<struct agl_shell **>(data);
51 if (strcmp(interface, agl_shell_interface.name) == 0) {
52 *shell = static_cast<struct agl_shell *>(
53 wl_registry_bind(reg, name, &agl_shell_interface, 1)
59 global_remove(void *data, struct wl_registry *reg, uint32_t id)
67 static const struct wl_registry_listener registry_listener = {
72 static struct wl_surface *
73 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
75 void *surf = native->nativeResourceForWindow("surface", window);
76 return static_cast<struct ::wl_surface *>(surf);
79 static struct wl_output *
80 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
82 void *output = native->nativeResourceForScreen("output", screen);
83 return static_cast<struct ::wl_output*>(output);
87 static struct agl_shell *
88 register_agl_shell(QPlatformNativeInterface *native)
90 struct wl_display *wl;
91 struct wl_registry *registry;
92 struct agl_shell *shell = nullptr;
94 wl = static_cast<struct wl_display *>(
95 native->nativeResourceForIntegration("display")
97 registry = wl_display_get_registry(wl);
99 wl_registry_add_listener(registry, ®istry_listener, &shell);
101 /* Roundtrip to get all globals advertised by the compositor */
102 wl_display_roundtrip(wl);
103 wl_registry_destroy(registry);
108 static struct wl_surface *
109 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
110 QScreen *screen, QObject **qobj)
112 QObject *obj = comp->create();
113 obj->setParent(screen);
115 QWindow *win = qobject_cast<QWindow *>(obj);
118 return getWlSurface(native, win);
122 find_screen(const char *screen_name)
124 QList<QScreen *> screens = qApp->screens();
125 QScreen *found = nullptr;
126 QString qstr_name = QString::fromUtf8(screen_name, -1);
128 for (QScreen *xscreen : screens) {
129 if (qstr_name == xscreen->name()) {
139 load_agl_shell_app(QPlatformNativeInterface *native,
140 QQmlApplicationEngine *engine,
141 struct agl_shell *agl_shell,
142 const char *screen_name,
145 struct wl_surface *bg, *top, *bottom;
146 struct wl_output *output;
147 QObject *qobj_bg, *qobj_top, *qobj_bottom;
148 QScreen *screen = nullptr;
151 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
152 qInfo() << bg_comp.errors();
154 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
155 qInfo() << top_comp.errors();
157 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
158 qInfo() << bot_comp.errors();
160 top = create_component(native, &top_comp, screen, &qobj_top);
161 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
162 bg = create_component(native, &bg_comp, screen, &qobj_bg);
164 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
165 qInfo() << bg_comp.errors();
167 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
168 qInfo() << top_comp.errors();
170 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
171 qInfo() << bot_comp.errors();
173 top = create_component(native, &top_comp, screen, &qobj_top);
174 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
175 bg = create_component(native, &bg_comp, screen, &qobj_bg);
179 screen = qApp->primaryScreen();
181 screen = find_screen(screen_name);
183 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
184 "first screen " << qApp->screens().first()->name();
185 output = getWlOutput(native, screen);
187 /* engine.rootObjects() works only if we had a load() */
188 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
190 qDebug() << "got statusBar objectname, doing init()";
191 statusBar->init(engine->rootContext());
194 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
195 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
196 qDebug() << "Setting homescreen to screen " << screen->name();
198 agl_shell_set_background(agl_shell, bg, output);
200 /* Delay the ready signal until after Qt has done all of its own setup
202 QTimer::singleShot(500, [agl_shell](){
203 agl_shell_ready(agl_shell);
207 int main(int argc, char *argv[])
209 setenv("QT_QPA_PLATFORM", "wayland", 1);
210 QGuiApplication a(argc, argv);
211 const char *screen_name;
212 bool is_demo_val = false;
214 QPlatformNativeInterface *native = qApp->platformNativeInterface();
215 struct agl_shell *agl_shell = nullptr;
216 screen_name = getenv("HOMESCREEN_START_SCREEN");
218 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
219 if (is_demo && strcmp(is_demo, "1") == 0)
222 QCoreApplication::setOrganizationDomain("LinuxFoundation");
223 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
224 QCoreApplication::setApplicationName("HomeScreen");
225 QCoreApplication::setApplicationVersion("0.7.0");
226 /* we need to have an app_id */
227 a.setDesktopFileName("homescreen");
229 agl_shell = register_agl_shell(native);
231 fprintf(stderr, "agl_shell extension is not advertised. "
232 "Are you sure that agl-compositor is running?\n");
236 std::shared_ptr<struct agl_shell> shell{agl_shell, agl_shell_destroy};
237 Shell *aglShell = new Shell(shell, &a);
239 // import C++ class to QML
240 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
241 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
243 ApplicationLauncher *launcher = new ApplicationLauncher();
244 launcher->setCurrent(QStringLiteral("launcher"));
245 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
246 homescreenHandler->init();
248 QQmlApplicationEngine engine;
249 QQmlContext *context = engine.rootContext();
251 context->setContextProperty("homescreenHandler", homescreenHandler);
252 context->setContextProperty("launcher", launcher);
254 context->setContextProperty("weather", new Weather(bindingAddress));
255 context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, context));
257 // we add it here even if we don't use it
258 context->setContextProperty("shell", aglShell);
260 /* instead of loading main.qml we load one-by-one each of the QMLs,
261 * divided now between several surfaces: panels, background.
263 load_agl_shell_app(native, &engine, agl_shell, screen_name, is_demo_val);