Rework take 1
[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         ApplicationLauncher *launcher;
53 };
54
55 static void
56 application_id_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
57                      const char *app_id)
58 {
59
60 }
61
62 static void
63 application_state_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
64                         const char *app_id, const char *app_data,
65                         uint32_t app_state, uint32_t app_role)
66 {
67         struct shell_container *sc = static_cast<struct shell_container *>(data);
68
69         qDebug() << "app_id " << app_id << " app_data " << app_data
70                 << " app_state " << app_state << " app_role " << app_role;
71 }
72
73 static const struct agl_shell_desktop_listener agl_shell_desktop_listener = {
74         application_id_event,
75         application_state_event,
76 };
77
78
79 static void
80 global_add(void *data, struct wl_registry *reg, uint32_t name,
81            const char *interface, uint32_t version)
82 {
83         struct shell_container *sc = static_cast<struct shell_container *>(data);
84
85         if (strcmp(interface, agl_shell_interface.name) == 0) {
86                 sc->agl_shell = static_cast<struct agl_shell *>(
87                         wl_registry_bind(reg, name, &agl_shell_interface, 1)
88                 );
89         } else if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
90                 sc->agl_shell_desktop = static_cast<struct agl_shell_desktop *>(
91                         wl_registry_bind(reg, name, &agl_shell_desktop_interface, 1)
92                 );
93
94                 agl_shell_desktop_add_listener(sc->agl_shell_desktop,
95                                                &agl_shell_desktop_listener, sc);
96         }
97 }
98
99 static void
100 global_remove(void *data, struct wl_registry *reg, uint32_t id)
101 {
102         /* Don't care */
103         (void) data;
104         (void) reg;
105         (void) id;
106 }
107
108 static const struct wl_registry_listener registry_listener = {
109         global_add,
110         global_remove,
111 };
112
113 static struct wl_surface *
114 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
115 {
116         void *surf = native->nativeResourceForWindow("surface", window);
117         return static_cast<struct ::wl_surface *>(surf);
118 }
119
120 static struct wl_output *
121 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
122 {
123         void *output = native->nativeResourceForScreen("output", screen);
124         return static_cast<struct ::wl_output*>(output);
125 }
126
127
128 static struct shell_container *
129 register_agl_shell(QPlatformNativeInterface *native)
130 {
131         struct wl_display *wl;
132         struct wl_registry *registry;
133         struct shell_container *sc = new shell_container();
134
135         wl = static_cast<struct wl_display *>(
136                         native->nativeResourceForIntegration("display")
137         );
138         registry = wl_display_get_registry(wl);
139
140         wl_registry_add_listener(registry, &registry_listener, sc);
141
142         /* Roundtrip to get all globals advertised by the compositor */
143         wl_display_roundtrip(wl);
144         wl_registry_destroy(registry);
145
146         if (!sc->agl_shell) {
147                 delete sc;
148         }
149
150         if (!sc->agl_shell_desktop) {
151                 delete sc;
152         }
153
154         return sc;
155 }
156
157 static struct wl_surface *
158 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
159                  QScreen *screen, QObject **qobj)
160 {
161         QObject *obj = comp->create();
162         obj->setParent(screen);
163
164         QWindow *win = qobject_cast<QWindow *>(obj);
165         *qobj = obj;
166
167         return getWlSurface(native, win);
168 }
169
170 static QScreen *
171 find_screen(const char *screen_name)
172 {
173         QList<QScreen *> screens = qApp->screens();
174         QScreen *found = nullptr;
175         QString qstr_name = QString::fromUtf8(screen_name, -1);
176
177         for (QScreen *xscreen : screens) {
178                 if (qstr_name == xscreen->name()) {
179                         found = xscreen;
180                         break;
181                 }
182         }
183
184         return found;
185 }
186
187 static void
188 load_agl_shell_app(QPlatformNativeInterface *native,
189                    QQmlApplicationEngine *engine,
190                    struct shell_container *sc, QUrl &bindingAddress,
191                    const char *screen_name)
192 {
193         struct wl_surface *bg, *top, *bottom;
194         struct wl_output *output;
195         QObject *qobj_bg, *qobj_top, *qobj_bottom;
196         QScreen *screen = nullptr;
197         struct agl_shell *agl_shell = sc->agl_shell;
198
199         QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
200         qInfo() << bg_comp.errors();
201
202         QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
203         qInfo() << top_comp.errors();
204
205         QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
206         qInfo() << bot_comp.errors();
207
208         if (!screen_name)
209                 screen = qApp->primaryScreen();
210         else
211                 screen = find_screen(screen_name);
212
213         qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
214                 "first screen " << qApp->screens().first()->name();
215         output = getWlOutput(native, screen);
216
217         top = create_component(native, &top_comp, screen, &qobj_top);
218         bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
219         bg = create_component(native, &bg_comp, screen, &qobj_bg);
220
221         /* engine.rootObjects() works only if we had a load() */
222         StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
223         if (statusBar) {
224                 qDebug() << "got statusBar objectname, doing init()";
225                 statusBar->init(bindingAddress, engine->rootContext());
226         }
227
228         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
229         agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
230         qDebug() << "Setting homescreen to screen  " << screen->name();
231
232         agl_shell_set_background(agl_shell, bg, output);
233
234         /* Delay the ready signal until after Qt has done all of its own setup
235          * in a.exec() */
236         QTimer::singleShot(500, [agl_shell](){
237                 agl_shell_ready(agl_shell);
238         });
239 }
240
241 int main(int argc, char *argv[])
242 {
243     setenv("QT_QPA_PLATFORM", "wayland", 1);
244     QGuiApplication a(argc, argv);
245     const char *screen_name;
246     int ret;
247
248     QPlatformNativeInterface *native = qApp->platformNativeInterface();
249     struct shell_container *sc = nullptr;
250     screen_name = getenv("HOMESCREEN_START_SCREEN");
251
252     QCoreApplication::setOrganizationDomain("LinuxFoundation");
253     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
254     QCoreApplication::setApplicationName("HomeScreen");
255     QCoreApplication::setApplicationVersion("0.7.0");
256     /* we need to have an app_id */
257     a.setDesktopFileName("homescreen");
258
259     QCommandLineParser parser;
260     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
261     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
262     parser.addHelpOption();
263     parser.addVersionOption();
264     parser.process(a);
265     QStringList positionalArguments = parser.positionalArguments();
266
267     int port = 1700;
268     QString token = "wm";
269     QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
270
271     if (positionalArguments.length() == 2) {
272         port = positionalArguments.takeFirst().toInt();
273         token = positionalArguments.takeFirst();
274     }
275
276     HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
277     ApplicationLauncher *launcher = new ApplicationLauncher();
278
279     sc = register_agl_shell(native);
280     if (!sc) {
281             exit(EXIT_FAILURE);
282     }
283
284     std::shared_ptr<struct agl_shell>
285             shell{sc->agl_shell, agl_shell_destroy};
286     std::shared_ptr<struct agl_shell_desktop>
287             shell_desktop{sc->agl_shell_desktop, agl_shell_desktop_destroy};
288     Shell *aglShell = new Shell(shell, shell_desktop, &a);
289     sc->a_shell = aglShell;
290     sc->launcher = launcher;
291
292     // import C++ class to QML
293     // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
294     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
295     qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
296     qmlRegisterUncreatableType<ChromeController>("SpeechChrome", 1, 0, "SpeechChromeController",
297                                                  QLatin1String("SpeechChromeController is uncreatable."));
298
299
300     HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell);
301     homescreenHandler->init(port, token.toStdString().c_str());
302
303     QUrl bindingAddress;
304     bindingAddress.setScheme(QStringLiteral("ws"));
305     bindingAddress.setHost(QStringLiteral("localhost"));
306     bindingAddress.setPort(port);
307     bindingAddress.setPath(QStringLiteral("/api"));
308
309     QUrlQuery query;
310     query.addQueryItem(QStringLiteral("token"), token);
311     bindingAddress.setQuery(query);
312
313     QQmlApplicationEngine engine;
314     QQmlContext *context = engine.rootContext();
315     context->setContextProperty("bindingAddress", bindingAddress);
316
317     context->setContextProperty("homescreenHandler", homescreenHandler);
318     context->setContextProperty("launcher", launcher);
319     context->setContextProperty("weather", new Weather(bindingAddress));
320     context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, context));
321     context->setContextProperty("speechChromeController", new ChromeController(bindingAddress, &engine));
322     // we add it here even if we don't use it
323     context->setContextProperty("shell", aglShell);
324
325     /* instead of loading main.qml we load one-by-one each of the QMLs,
326      * divided now between several surfaces: panels, background.
327      */
328     load_agl_shell_app(native, &engine, sc, bindingAddress, screen_name);
329
330     ret = a.exec();
331
332     delete sc;
333     return ret;
334 }