homescreen/homescreen: Bind to agl_shell_desktop
[apps/homescreen.git] / homescreen / src / main.cpp
1 /*
2  * Copyright (C) 2016, 2017 Mentor Graphics Development (Deutschland) GmbH
3  * Copyright (c) 2017, 2018 TOYOTA MOTOR CORPORATION
4  * Copyright (c) 2020 Collabora, Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <QGuiApplication>
20 #include <QCommandLineParser>
21 #include <QtCore/QUrlQuery>
22 #include <QtGui/QGuiApplication>
23 #include <QtQml/QQmlApplicationEngine>
24 #include <QtQml/QQmlContext>
25 #include <QtQml/QQmlComponent>
26 #include <QtQml/qqml.h>
27 #include <QQuickWindow>
28 #include <QTimer>
29
30 #include <weather.h>
31 #include <bluetooth.h>
32 #include "applicationlauncher.h"
33 #include "statusbarmodel.h"
34 #include "mastervolume.h"
35 #include "homescreenhandler.h"
36 #include "hmi-debug.h"
37 #include "chromecontroller.h"
38
39 #include <qpa/qplatformnativeinterface.h>
40 #include <wayland-client.h>
41
42 #include "wayland-agl-shell-client-protocol.h"
43 #include "wayland-agl-shell-desktop-client-protocol.h"
44 #include "shell.h"
45
46 struct shell_container {
47         struct agl_shell *agl_shell;
48         struct agl_shell_desktop *agl_shell_desktop;
49         // used to propagate events from C/C++ event handlers from the protocol
50         // to the QML in case we need them
51         Shell *a_shell;
52 };
53
54 static void
55 application_id_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
56                 const char *app_id)
57 {
58 }
59
60 static void
61 application_state_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
62                      const char *app_id, const char *app_data,
63                      uint32_t app_state, uint32_t app_role)
64 {
65 }
66
67 static const struct agl_shell_desktop_listener agl_shell_desktop_listener = {
68         application_id_event,
69         application_state_event,
70 };
71
72
73 static void
74 global_add(void *data, struct wl_registry *reg, uint32_t name,
75            const char *interface, uint32_t version)
76 {
77         struct shell_container *sc = static_cast<struct shell_container *>(data);
78
79         if (strcmp(interface, agl_shell_interface.name) == 0) {
80                 sc->agl_shell = static_cast<struct agl_shell *>(
81                         wl_registry_bind(reg, name, &agl_shell_interface, 1)
82                 );
83         } else if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
84                 sc->agl_shell_desktop = static_cast<struct agl_shell_desktop *>(
85                         wl_registry_bind(reg, name, &agl_shell_desktop_interface, 1)
86                 );
87
88                 agl_shell_desktop_add_listener(sc->agl_shell_desktop,
89                                                &agl_shell_desktop_listener, sc);
90         }
91 }
92
93 static void
94 global_remove(void *data, struct wl_registry *reg, uint32_t id)
95 {
96         /* Don't care */
97         (void) data;
98         (void) reg;
99         (void) id;
100 }
101
102 static const struct wl_registry_listener registry_listener = {
103         global_add,
104         global_remove,
105 };
106
107 static struct wl_surface *
108 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
109 {
110         void *surf = native->nativeResourceForWindow("surface", window);
111         return static_cast<struct ::wl_surface *>(surf);
112 }
113
114 static struct wl_output *
115 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
116 {
117         void *output = native->nativeResourceForScreen("output", screen);
118         return static_cast<struct ::wl_output*>(output);
119 }
120
121
122 static struct shell_container *
123 register_agl_shell(QPlatformNativeInterface *native)
124 {
125         struct wl_display *wl;
126         struct wl_registry *registry;
127         struct shell_container *sc = new shell_container();
128
129         wl = static_cast<struct wl_display *>(
130                         native->nativeResourceForIntegration("display")
131         );
132         registry = wl_display_get_registry(wl);
133
134         wl_registry_add_listener(registry, &registry_listener, sc);
135
136         /* Roundtrip to get all globals advertised by the compositor */
137         wl_display_roundtrip(wl);
138         wl_registry_destroy(registry);
139
140         if (!sc->agl_shell) {
141                 delete sc;
142         }
143
144         if (!sc->agl_shell_desktop) {
145                 delete sc;
146         }
147
148         return sc;
149 }
150
151 static struct wl_surface *
152 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
153                  QScreen *screen, QObject **qobj)
154 {
155         QObject *obj = comp->create();
156         obj->setParent(screen);
157
158         QWindow *win = qobject_cast<QWindow *>(obj);
159         *qobj = obj;
160
161         return getWlSurface(native, win);
162 }
163
164 static QScreen *
165 find_screen(const char *screen_name)
166 {
167         QList<QScreen *> screens = qApp->screens();
168         QScreen *found = nullptr;
169         QString qstr_name = QString::fromUtf8(screen_name, -1);
170
171         for (QScreen *xscreen : screens) {
172                 if (qstr_name == xscreen->name()) {
173                         found = xscreen;
174                         break;
175                 }
176         }
177
178         return found;
179 }
180
181 static void
182 load_agl_shell_app(QPlatformNativeInterface *native,
183                    QQmlApplicationEngine *engine,
184                    struct shell_container *sc, QUrl &bindingAddress,
185                    const char *screen_name)
186 {
187         struct wl_surface *bg, *top, *bottom;
188         struct wl_output *output;
189         QObject *qobj_bg, *qobj_top, *qobj_bottom;
190         QScreen *screen = nullptr;
191         struct agl_shell *agl_shell = sc->agl_shell;
192
193         QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
194         qInfo() << bg_comp.errors();
195
196         QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
197         qInfo() << top_comp.errors();
198
199         QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
200         qInfo() << bot_comp.errors();
201
202         if (!screen_name)
203                 screen = qApp->primaryScreen();
204         else
205                 screen = find_screen(screen_name);
206
207         qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
208                 "first screen " << qApp->screens().first()->name();
209         output = getWlOutput(native, screen);
210
211         top = create_component(native, &top_comp, screen, &qobj_top);
212         bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
213         bg = create_component(native, &bg_comp, screen, &qobj_bg);
214
215         /* engine.rootObjects() works only if we had a load() */
216         StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
217         if (statusBar) {
218                 qDebug() << "got statusBar objectname, doing init()";
219                 statusBar->init(bindingAddress, engine->rootContext());
220         }
221
222         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
223         agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
224         qDebug() << "Setting homescreen to screen  " << screen->name();
225
226         agl_shell_set_background(agl_shell, bg, output);
227
228         /* Delay the ready signal until after Qt has done all of its own setup
229          * in a.exec() */
230         QTimer::singleShot(500, [agl_shell](){
231                 agl_shell_ready(agl_shell);
232         });
233 }
234
235 int main(int argc, char *argv[])
236 {
237     setenv("QT_QPA_PLATFORM", "wayland", 1);
238     QGuiApplication a(argc, argv);
239     const char *screen_name;
240     int ret;
241
242     QPlatformNativeInterface *native = qApp->platformNativeInterface();
243     struct shell_container *sc = nullptr;
244     screen_name = getenv("HOMESCREEN_START_SCREEN");
245
246     QCoreApplication::setOrganizationDomain("LinuxFoundation");
247     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
248     QCoreApplication::setApplicationName("HomeScreen");
249     QCoreApplication::setApplicationVersion("0.7.0");
250     /* we need to have an app_id */
251     a.setDesktopFileName("homescreen");
252
253     QCommandLineParser parser;
254     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
255     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
256     parser.addHelpOption();
257     parser.addVersionOption();
258     parser.process(a);
259     QStringList positionalArguments = parser.positionalArguments();
260
261     int port = 1700;
262     QString token = "wm";
263     QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
264
265     if (positionalArguments.length() == 2) {
266         port = positionalArguments.takeFirst().toInt();
267         token = positionalArguments.takeFirst();
268     }
269
270     HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
271
272     sc = register_agl_shell(native);
273     if (!sc) {
274             exit(EXIT_FAILURE);
275     }
276
277     std::shared_ptr<struct agl_shell>
278             shell{sc->agl_shell, agl_shell_destroy};
279     std::shared_ptr<struct agl_shell_desktop>
280             shell_desktop{sc->agl_shell_desktop, agl_shell_desktop_destroy};
281     Shell *aglShell = new Shell(shell, shell_desktop, &a);
282     sc->a_shell = aglShell;
283
284     // import C++ class to QML
285     // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
286     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
287     qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
288     qmlRegisterUncreatableType<ChromeController>("SpeechChrome", 1, 0, "SpeechChromeController",
289                                                  QLatin1String("SpeechChromeController is uncreatable."));
290
291     ApplicationLauncher *launcher = new ApplicationLauncher();
292
293     HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell);
294     homescreenHandler->init(port, token.toStdString().c_str());
295
296     QUrl bindingAddress;
297     bindingAddress.setScheme(QStringLiteral("ws"));
298     bindingAddress.setHost(QStringLiteral("localhost"));
299     bindingAddress.setPort(port);
300     bindingAddress.setPath(QStringLiteral("/api"));
301
302     QUrlQuery query;
303     query.addQueryItem(QStringLiteral("token"), token);
304     bindingAddress.setQuery(query);
305
306     QQmlApplicationEngine engine;
307     QQmlContext *context = engine.rootContext();
308     context->setContextProperty("bindingAddress", bindingAddress);
309
310     context->setContextProperty("homescreenHandler", homescreenHandler);
311     context->setContextProperty("launcher", launcher);
312     context->setContextProperty("weather", new Weather(bindingAddress));
313     context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, context));
314     context->setContextProperty("speechChromeController", new ChromeController(bindingAddress, &engine));
315     // we add it here even if we don't use it
316     context->setContextProperty("shell", aglShell);
317
318     /* instead of loading main.qml we load one-by-one each of the QMLs,
319      * divided now between several surfaces: panels, background.
320      */
321     load_agl_shell_app(native, &engine, sc, bindingAddress, screen_name);
322
323     ret = a.exec();
324
325     delete sc;
326     return ret;
327 }