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>
31 #include "applicationlauncher.h"
32 #include "statusbarmodel.h"
33 #include "mastervolume.h"
34 #include "homescreenhandler.h"
35 #include "hmi-debug.h"
36 #include "chromecontroller.h"
38 #include <qpa/qplatformnativeinterface.h>
39 #include <wayland-client.h>
41 #include "wayland-agl-shell-client-protocol.h"
45 global_add(void *data, struct wl_registry *reg, uint32_t name,
46 const char *interface, uint32_t)
48 struct agl_shell **shell = static_cast<struct agl_shell **>(data);
50 if (strcmp(interface, agl_shell_interface.name) == 0) {
51 *shell = static_cast<struct agl_shell *>(
52 wl_registry_bind(reg, name, &agl_shell_interface, 1)
58 global_remove(void *data, struct wl_registry *reg, uint32_t id)
66 static const struct wl_registry_listener registry_listener = {
71 static struct wl_surface *
72 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
74 void *surf = native->nativeResourceForWindow("surface", window);
75 return static_cast<struct ::wl_surface *>(surf);
78 static struct wl_output *
79 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
81 void *output = native->nativeResourceForScreen("output", screen);
82 return static_cast<struct ::wl_output*>(output);
86 static struct agl_shell *
87 register_agl_shell(QPlatformNativeInterface *native)
89 struct wl_display *wl;
90 struct wl_registry *registry;
91 struct agl_shell *shell = nullptr;
93 wl = static_cast<struct wl_display *>(
94 native->nativeResourceForIntegration("display")
96 registry = wl_display_get_registry(wl);
98 wl_registry_add_listener(registry, ®istry_listener, &shell);
100 /* Roundtrip to get all globals advertised by the compositor */
101 wl_display_roundtrip(wl);
102 wl_registry_destroy(registry);
107 static struct wl_surface *
108 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
109 QScreen *screen, QObject **qobj)
111 QObject *obj = comp->create();
112 obj->setParent(screen);
114 QWindow *win = qobject_cast<QWindow *>(obj);
117 return getWlSurface(native, win);
121 find_screen(const char *screen_name)
123 QList<QScreen *> screens = qApp->screens();
124 QScreen *found = nullptr;
125 QString qstr_name = QString::fromUtf8(screen_name, -1);
127 for (QScreen *xscreen : screens) {
128 if (qstr_name == xscreen->name()) {
138 load_agl_shell_app(QPlatformNativeInterface *native,
139 QQmlApplicationEngine *engine,
140 struct agl_shell *agl_shell, QUrl &bindingAddress,
141 const char *screen_name)
143 struct wl_surface *bg, *top, *bottom;
144 struct wl_output *output;
145 QObject *qobj_bg, *qobj_top, *qobj_bottom;
146 QScreen *screen = nullptr;
148 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
149 qInfo() << bg_comp.errors();
151 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
152 qInfo() << top_comp.errors();
154 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
155 qInfo() << bot_comp.errors();
158 screen = qApp->primaryScreen();
160 screen = find_screen(screen_name);
162 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
163 "first screen " << qApp->screens().first()->name();
164 output = getWlOutput(native, screen);
166 top = create_component(native, &top_comp, screen, &qobj_top);
167 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
168 bg = create_component(native, &bg_comp, screen, &qobj_bg);
170 /* engine.rootObjects() works only if we had a load() */
171 StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
173 qDebug() << "got statusBar objectname, doing init()";
174 statusBar->init(bindingAddress, engine->rootContext());
177 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
178 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
179 qDebug() << "Setting homescreen to screen " << screen->name();
181 agl_shell_set_background(agl_shell, bg, output);
183 /* Delay the ready signal until after Qt has done all of its own setup
185 QTimer::singleShot(500, [agl_shell](){
186 agl_shell_ready(agl_shell);
190 int main(int argc, char *argv[])
192 setenv("QT_QPA_PLATFORM", "wayland", 1);
193 QGuiApplication a(argc, argv);
194 const char *screen_name;
196 QPlatformNativeInterface *native = qApp->platformNativeInterface();
197 struct agl_shell *agl_shell = nullptr;
198 screen_name = getenv("HOMESCREEN_START_SCREEN");
200 QCoreApplication::setOrganizationDomain("LinuxFoundation");
201 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
202 QCoreApplication::setApplicationName("HomeScreen");
203 QCoreApplication::setApplicationVersion("0.7.0");
204 /* we need to have an app_id */
205 a.setDesktopFileName("homescreen");
207 QCommandLineParser parser;
208 parser.addPositionalArgument("port", a.translate("main", "port for binding"));
209 parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
210 parser.addHelpOption();
211 parser.addVersionOption();
213 QStringList positionalArguments = parser.positionalArguments();
216 QString token = "wm";
217 QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
219 if (positionalArguments.length() == 2) {
220 port = positionalArguments.takeFirst().toInt();
221 token = positionalArguments.takeFirst();
224 HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
226 agl_shell = register_agl_shell(native);
228 fprintf(stderr, "agl_shell extension is not advertised. "
229 "Are you sure that agl-compositor is running?\n");
233 std::shared_ptr<struct agl_shell> shell{agl_shell, agl_shell_destroy};
234 Shell *aglShell = new Shell(shell, &a);
236 // import C++ class to QML
237 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
238 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
239 qmlRegisterUncreatableType<ChromeController>("SpeechChrome", 1, 0, "SpeechChromeController",
240 QLatin1String("SpeechChromeController is uncreatable."));
242 ApplicationLauncher *launcher = new ApplicationLauncher();
243 launcher->setCurrent(QStringLiteral("launcher"));
244 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
245 homescreenHandler->init(port, token.toStdString().c_str());
248 bindingAddress.setScheme(QStringLiteral("ws"));
249 bindingAddress.setHost(QStringLiteral("localhost"));
250 bindingAddress.setPort(port);
251 bindingAddress.setPath(QStringLiteral("/api"));
254 query.addQueryItem(QStringLiteral("token"), token);
255 bindingAddress.setQuery(query);
257 QQmlApplicationEngine engine;
258 QQmlContext *context = engine.rootContext();
259 context->setContextProperty("bindingAddress", bindingAddress);
261 context->setContextProperty("homescreenHandler", homescreenHandler);
262 context->setContextProperty("launcher", launcher);
263 context->setContextProperty("weather", new Weather(bindingAddress));
264 context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, context));
265 context->setContextProperty("speechChromeController", new ChromeController(bindingAddress, &engine));
266 // we add it here even if we don't use it
267 context->setContextProperty("shell", aglShell);
269 /* instead of loading main.qml we load one-by-one each of the QMLs,
270 * divided now between several surfaces: panels, background.
272 load_agl_shell_app(native, &engine, agl_shell, bindingAddress, screen_name);