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>
29 #include <qpa/qplatformnativeinterface.h>
34 #include <wayland-client.h>
37 #include <bluetooth.h>
38 #include "applicationlauncher.h"
39 #include "statusbarmodel.h"
40 #include "mastervolume.h"
42 #include "hmi-debug.h"
44 #include "wayland-agl-shell-client-protocol.h"
46 #define CONNECT_STR "unix:/run/platform/apis/ws/afm-main"
48 static void global_add(void *data, struct wl_registry *reg, uint32_t name,
49 const char *interface, uint32_t)
51 struct agl_shell **shell = static_cast<struct agl_shell **>(data);
52 if (strcmp(interface, agl_shell_interface.name) == 0) {
53 *shell = static_cast<struct agl_shell *>(wl_registry_bind(reg, name, &agl_shell_interface, 1));
57 static void global_remove(void *, struct wl_registry *, uint32_t)
62 static const struct wl_registry_listener registry_listener = {
67 static struct wl_surface *create_component(QPlatformNativeInterface *native,
68 QQmlComponent *comp, QScreen *screen)
70 QObject *obj = comp->create();
71 obj->setParent(screen);
73 QWindow *win = qobject_cast<QWindow *>(obj);
74 return static_cast<struct wl_surface *>(native->nativeResourceForWindow("surface", win));
77 int main(int argc, char *argv[])
79 setenv("QT_QPA_PLATFORM", "wayland", 1);
80 QGuiApplication a(argc, argv);
81 QPlatformNativeInterface *native = qApp->platformNativeInterface();
82 struct wl_display *wl;
83 struct wl_registry *registry;
84 struct agl_shell *agl_shell = nullptr;
86 wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
87 registry = wl_display_get_registry(wl);
89 wl_registry_add_listener(registry, ®istry_listener, &agl_shell);
90 // Roundtrip to get all globals advertised by the compositor
91 wl_display_roundtrip(wl);
92 wl_registry_destroy(registry);
95 qFatal("Compositor does not support AGL shell protocol");
98 std::shared_ptr<struct agl_shell> shell{agl_shell, agl_shell_destroy};
100 QCoreApplication::setOrganizationDomain("LinuxFoundation");
101 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
102 QCoreApplication::setApplicationName("HomeScreen");
103 QCoreApplication::setApplicationVersion("0.7.0");
105 QCommandLineParser parser;
106 parser.addPositionalArgument("port", a.translate("main", "port for binding"));
107 parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
108 parser.addHelpOption();
109 parser.addVersionOption();
111 QStringList positionalArguments = parser.positionalArguments();
114 QString token = "wm";
115 QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
117 if (positionalArguments.length() == 2) {
118 port = positionalArguments.takeFirst().toInt();
119 token = positionalArguments.takeFirst();
122 HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
124 // import C++ class to QML
125 // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
126 qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
127 qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
129 ApplicationLauncher *launcher = new ApplicationLauncher(CONNECT_STR, &a);
132 bindingAddress.setScheme(QStringLiteral("ws"));
133 bindingAddress.setHost(QStringLiteral("localhost"));
134 bindingAddress.setPort(port);
135 bindingAddress.setPath(QStringLiteral("/api"));
138 query.addQueryItem(QStringLiteral("token"), token);
139 bindingAddress.setQuery(query);
142 QQmlContext *context = engine.rootContext();
143 context->setContextProperty("bindingAddress", bindingAddress);
144 context->setContextProperty("launcher", launcher);
145 context->setContextProperty("weather", new Weather(bindingAddress));
146 context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
147 context->setContextProperty("shell", new Shell(shell, &a));
149 QQmlComponent bg_comp(&engine, QUrl("qrc:/background.qml"));
150 qInfo() << bg_comp.errors();
152 QQmlComponent top_comp(&engine, QUrl("qrc:/toppanel.qml"));
153 qInfo() << top_comp.errors();
155 QQmlComponent bot_comp(&engine, QUrl("qrc:/bottompanel.qml"));
156 qInfo() << bot_comp.errors();
158 for (QScreen *screen : qApp->screens()) {
159 struct wl_output *output;
161 output = static_cast<struct wl_output *>(native->nativeResourceForScreen("output", screen));
163 struct wl_surface *bg = create_component(native, &bg_comp, screen);
164 struct wl_surface *top = create_component(native, &top_comp, screen);
165 struct wl_surface *bot = create_component(native, &bot_comp, screen);
167 wl_display_dispatch(wl);
169 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
170 agl_shell_set_panel(agl_shell, bot, output, AGL_SHELL_EDGE_BOTTOM);
171 agl_shell_set_background(agl_shell, bg, output);
174 // Delay the ready signal until after Qt has done all of its own setup in a.exec()
175 QTimer::singleShot(0, [shell](){
176 agl_shell_ready(shell.get());