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>
29 //#include <weather.h>
30 //#include <bluetooth.h>
31 #include "applicationlauncher.h"
32 //#include "statusbarmodel.h"
33 //#include "mastervolume.h"
34 #include "homescreenhandler.h"
35 //#include "chromecontroller.h"
37 #include <qpa/qplatformnativeinterface.h>
38 #include <wayland-client.h>
40 #include "wayland-agl-shell-client-protocol.h"
44 global_add(void *data, struct wl_registry *reg, uint32_t name,
45 const char *interface, uint32_t)
47 struct agl_shell **shell = static_cast<struct agl_shell **>(data);
49 if (strcmp(interface, agl_shell_interface.name) == 0) {
50 *shell = static_cast<struct agl_shell *>(
51 wl_registry_bind(reg, name, &agl_shell_interface, 1)
57 global_remove(void *data, struct wl_registry *reg, uint32_t id)
65 static const struct wl_registry_listener registry_listener = {
70 static struct wl_surface *
71 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
73 void *surf = native->nativeResourceForWindow("surface", window);
74 return static_cast<struct ::wl_surface *>(surf);
77 static struct wl_output *
78 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
80 void *output = native->nativeResourceForScreen("output", screen);
81 return static_cast<struct ::wl_output*>(output);
85 static struct agl_shell *
86 register_agl_shell(QPlatformNativeInterface *native)
88 struct wl_display *wl;
89 struct wl_registry *registry;
90 struct agl_shell *shell = nullptr;
92 wl = static_cast<struct wl_display *>(
93 native->nativeResourceForIntegration("display")
95 registry = wl_display_get_registry(wl);
97 wl_registry_add_listener(registry, ®istry_listener, &shell);
99 /* Roundtrip to get all globals advertised by the compositor */
100 wl_display_roundtrip(wl);
101 wl_registry_destroy(registry);
106 static struct wl_surface *
107 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
108 QScreen *screen, QObject **qobj)
110 QObject *obj = comp->create();
111 obj->setParent(screen);
113 QWindow *win = qobject_cast<QWindow *>(obj);
116 return getWlSurface(native, win);
120 find_screen(const char *screen_name)
122 QList<QScreen *> screens = qApp->screens();
123 QScreen *found = nullptr;
124 QString qstr_name = QString::fromUtf8(screen_name, -1);
126 for (QScreen *xscreen : screens) {
127 if (qstr_name == xscreen->name()) {
137 load_agl_shell_app(QPlatformNativeInterface *native,
138 QQmlApplicationEngine *engine,
139 struct agl_shell *agl_shell,
140 const char *screen_name, bool is_demo)
142 struct wl_surface *bg, *top, *bottom;
143 struct wl_output *output;
144 QObject *qobj_bg, *qobj_top, *qobj_bottom;
145 QScreen *screen = nullptr;
148 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
149 qInfo() << bg_comp.errors();
151 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
152 qInfo() << top_comp.errors();
154 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
155 qInfo() << bot_comp.errors();
157 top = create_component(native, &top_comp, screen, &qobj_top);
158 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
159 bg = create_component(native, &bg_comp, screen, &qobj_bg);
161 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
162 qInfo() << bg_comp.errors();
164 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
165 qInfo() << top_comp.errors();
167 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
168 qInfo() << bot_comp.errors();
170 top = create_component(native, &top_comp, screen, &qobj_top);
171 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
172 bg = create_component(native, &bg_comp, screen, &qobj_bg);
176 screen = qApp->primaryScreen();
178 screen = find_screen(screen_name);
180 qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
181 "first screen " << qApp->screens().first()->name();
182 output = getWlOutput(native, screen);
185 /* engine.rootObjects() works only if we had a load() */
186 //StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
188 // qDebug() << "got statusBar objectname, doing init()";
189 // statusBar->init(bindingAddress, engine->rootContext());
192 agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
193 agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
194 qDebug() << "Setting homescreen to screen " << screen->name();
196 agl_shell_set_background(agl_shell, bg, output);
198 /* Delay the ready signal until after Qt has done all of its own setup
200 QTimer::singleShot(500, [agl_shell](){
201 agl_shell_ready(agl_shell);
205 int main(int argc, char *argv[])
207 // keep this as we're not going not going to have the appfw
208 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 // could be removed only for CI
219 const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
220 if (is_demo && strcmp(is_demo, "1") == 0)
223 QCoreApplication::setOrganizationDomain("LinuxFoundation");
224 QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
225 QCoreApplication::setApplicationName("HomeScreen");
226 QCoreApplication::setApplicationVersion("0.7.0");
228 // we need to have an app_id
229 a.setDesktopFileName("homescreen");
231 agl_shell = register_agl_shell(native);
233 fprintf(stderr, "agl_shell extension is not advertised. "
234 "Are you sure that agl-compositor is running?\n");
238 std::shared_ptr<struct agl_shell> shell{agl_shell, agl_shell_destroy};
239 Shell *aglShell = new Shell(shell, &a);
241 // import C++ class to QML
242 //qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
243 //qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
244 //qmlRegisterUncreatableType<ChromeController>("SpeechChrome", 1, 0, "SpeechChromeController", QLatin1String("SpeechChromeController is uncreatable."));
246 ApplicationLauncher *launcher = new ApplicationLauncher();
247 launcher->setCurrent(QStringLiteral("launcher"));
248 HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
249 homescreenHandler->init();
251 QQmlApplicationEngine engine;
252 QQmlContext *context = engine.rootContext();
254 context->setContextProperty("homescreenHandler", homescreenHandler);
255 context->setContextProperty("launcher", launcher);
256 //context->setContextProperty("weather", new Weather(bindingAddress));
257 //context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, context));
258 //context->setContextProperty("speechChromeController", new ChromeController(bindingAddress, &engine));
260 // we add it here even if we don't use it
261 context->setContextProperty("shell", aglShell);
263 /* instead of loading main.qml we load one-by-one each of the QMLs,
264 * divided now between several surfaces: panels, background. */
265 load_agl_shell_app(native, &engine, agl_shell, screen_name, is_demo_val);