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 <QtGui/QGuiApplication>
21 #include <QtQml/QQmlApplicationEngine>
22 #include <QtQml/QQmlContext>
23 #include <QtQml/QQmlComponent>
24 #include <QtQml/qqml.h>
25 #include <QQuickWindow>
27 #include <qpa/qplatformnativeinterface.h>
32 #include <wayland-client.h>
35 #include <bluetooth.h>
36 #include "applicationlauncher.h"
37 #include "statusbarmodel.h"
38 #include "afm_user_daemon_proxy.h"
39 #include "mastervolume.h"
41 #include "hmi-debug.h"
43 #include "wayland-agl-shell-client-protocol.h"
45 // XXX: We want this DBus connection to be shared across the different
46 // QML objects, is there another way to do this, a nice way, perhaps?
47 org::AGL::afm::user *afm_user_daemon_proxy;
52 static inline void cleanup(org::AGL::afm::user *p) {
54 afm_user_daemon_proxy = Q_NULLPTR;
60 static void global_add(void *data, struct wl_registry *reg, uint32_t name,
61 const char *interface, uint32_t)
63 struct agl_shell **shell = static_cast<struct agl_shell **>(data);
64 if (strcmp(interface, agl_shell_interface.name) == 0) {
65 *shell = static_cast<struct agl_shell *>(wl_registry_bind(reg, name, &agl_shell_interface, 1));
69 static void global_remove(void *, struct wl_registry *, uint32_t)
74 static const struct wl_registry_listener registry_listener = {
79 static struct wl_surface *create_component(QPlatformNativeInterface *native,
80 QQmlComponent *comp, QScreen *screen)
82 QObject *obj = comp->create();
83 obj->setParent(screen);
85 QWindow *win = qobject_cast<QWindow *>(obj);
86 return static_cast<struct wl_surface *>(native->nativeResourceForWindow("surface", win));
89 int main(int argc, char *argv[])
91 setenv("QT_QPA_PLATFORM", "wayland", 1);
92 QGuiApplication a(argc, argv);
93 QPlatformNativeInterface *native = qApp->platformNativeInterface();
94 struct wl_display *wl;
95 struct wl_registry *registry;
96 struct agl_shell *agl_shell = nullptr;
98 wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
99 registry = wl_display_get_registry(wl);
101 wl_registry_add_listener(registry, ®istry_listener, &agl_shell);
102 // Roundtrip to get all globals advertised by the compositor
103 wl_display_roundtrip(wl);
104 wl_registry_destroy(registry);
107 qFatal("Compositor does not support AGL shell protocol");
110 std::shared_ptr<struct agl_shell> shell{agl_shell, agl_shell_destroy};
112 // use launch process
113 QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
115 QDBusConnection::sessionBus(),
117 ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
119 QCoreApplication::setOrganizationDomain("LinuxFoundation");
120 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
121 QCoreApplication::setApplicationName("HomeScreen");
122 QCoreApplication::setApplicationVersion("0.7.0");
124 QCommandLineParser parser;
125 parser.addPositionalArgument("port", a.translate("main", "port for binding"));
126 parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
127 parser.addHelpOption();
128 parser.addVersionOption();
130 QStringList positionalArguments = parser.positionalArguments();
133 QString token = "wm";
134 QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
136 if (positionalArguments.length() == 2) {
137 port = positionalArguments.takeFirst().toInt();
138 token = positionalArguments.takeFirst();
141 HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
143 // import C++ class to QML
144 // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
145 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
146 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
148 ApplicationLauncher *launcher = new ApplicationLauncher();
151 bindingAddress.setScheme(QStringLiteral("ws"));
152 bindingAddress.setHost(QStringLiteral("localhost"));
153 bindingAddress.setPort(port);
154 bindingAddress.setPath(QStringLiteral("/api"));
157 query.addQueryItem(QStringLiteral("token"), token);
158 bindingAddress.setQuery(query);
162 QQmlApplicationEngine engine;
163 engine.rootContext()->setContextProperty("bindingAddress", bindingAddress);
164 engine.rootContext()->setContextProperty("launcher", launcher);
165 engine.rootContext()->setContextProperty("weather", new Weather(bindingAddress));
166 engine.rootContext()->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
167 //engine.rootContext()->setContextProperty("screenInfo", &screenInfo);
168 engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
169 engine.load(QUrl(QStringLiteral("qrc:/background.qml")));
171 auto root_objects = engine.rootObjects();
172 printf("num root objects: %d\n", root_objects.length());
173 QObject *root = engine.rootObjects().first();
174 QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
176 for (auto o : root_objects) {
177 qDebug() << o->dynamicPropertyNames();
180 QList<QObject *> sobjs = engine.rootObjects();
181 StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
182 statusBar->init(bindingAddress, engine.rootContext());
186 QQmlContext *context = engine.rootContext();
187 context->setContextProperty("bindingAddress", bindingAddress);
188 context->setContextProperty("launcher", launcher);
189 context->setContextProperty("weather", new Weather(bindingAddress));
190 context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
191 context->setContextProperty("shell", new Shell(shell, &a));
193 QQmlComponent bg_comp(&engine, QUrl("qrc:/background.qml"));
194 QQmlComponent top_comp(&engine, QUrl("qrc:/toppanel.qml"));
195 QQmlComponent bot_comp(&engine, QUrl("qrc:/bottompanel.qml"));
197 for (QScreen *screen : qApp->screens()) {
198 struct wl_output *output;
200 output = static_cast<struct wl_output *>(native->nativeResourceForScreen("output", screen));
202 struct wl_surface *bg = create_component(native, &bg_comp, screen);
203 agl_shell_set_background(agl_shell, bg, output);
205 struct wl_surface *top = create_component(native, &top_comp, screen);
206 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
208 struct wl_surface *bot = create_component(native, &bot_comp, screen);
209 agl_shell_set_panel(agl_shell, bot, output, AGL_SHELL_EDGE_BOTTOM);
212 // Delay the ready signal until after Qt has done all of its own setup in a.exec()
213 QTimer::singleShot(0, [shell](){
214 agl_shell_ready(shell.get());