homescreenhandler: Add support for split
[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  *
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
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>
27 #include <QTimer>
28
29 #include <weather.h>
30 #include <bluetooth.h>
31
32 #include "applicationlauncher.h"
33 #include "statusbarmodel.h"
34 #include "mastervolume.h"
35 #include "homescreenhandler.h"
36 #include "hmi-debug.h"
37
38 #include <qpa/qplatformnativeinterface.h>
39 #include <wayland-client.h>
40
41 #include "wayland-agl-shell-client-protocol.h"
42 #include "wayland-agl-shell-desktop-client-protocol.h"
43 #include "shell.h"
44
45 struct shell_data {
46         struct agl_shell *shell;
47         struct agl_shell_desktop *shell_desktop;
48
49         bool wait_for_bound;
50         bool bound_ok;
51 };
52
53 static void
54 agl_shell_desktop_application(void *data,
55                               struct agl_shell_desktop *agl_shell_desktop,
56                               const char *app_id)
57 {
58         HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
59
60         if (homescreenHandler)
61                 homescreenHandler->addAppToStack(app_id);
62 }
63
64 static void
65 agl_shell_desktop_state_app(void *data,
66                             struct agl_shell_desktop *agl_shell_desktop,
67                             const char *app_id,
68                             const char *app_data,
69                             uint32_t state,
70                             uint32_t role)
71 {
72         HomescreenHandler *homescreenHandler = static_cast<HomescreenHandler *>(data);
73
74         if (homescreenHandler && state == AGL_SHELL_DESKTOP_APP_STATE_DESTROYED)
75                 homescreenHandler->appTerminated(app_id);
76 }
77
78 static void
79 agl_shell_bound_ok(void *data, struct agl_shell *agl_shell)
80 {
81         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
82         shell_data->wait_for_bound = false;
83
84         shell_data->bound_ok = true;
85 }
86
87 static void
88 agl_shell_bound_fail(void *data, struct agl_shell *agl_shell)
89 {
90         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
91         shell_data->wait_for_bound = false;
92
93         shell_data->bound_ok = false;
94 }
95
96 static const struct agl_shell_listener shell_listener = {
97    agl_shell_bound_ok,
98    agl_shell_bound_fail,
99 };
100
101 static const struct agl_shell_desktop_listener shell_desktop_listener = {
102    agl_shell_desktop_application,
103    agl_shell_desktop_state_app
104 };
105
106 static void
107 global_add(void *data, struct wl_registry *reg, uint32_t name,
108            const char *interface, uint32_t ver)
109 {
110         struct shell_data *shell_data = static_cast<struct shell_data *>(data);
111
112         if (!shell_data)
113                 return;
114
115         if (strcmp(interface, agl_shell_interface.name) == 0) {
116                 shell_data->shell = static_cast<struct agl_shell *>(
117                         wl_registry_bind(reg, name, &agl_shell_interface, 3));
118                         agl_shell_add_listener(shell_data->shell, &shell_listener, data);
119         }
120
121         if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
122                 shell_data->shell_desktop = static_cast<struct agl_shell_desktop *>(
123                         wl_registry_bind(reg, name, &agl_shell_desktop_interface, 1)
124                 );
125         }
126 }
127
128 static void
129 global_remove(void *data, struct wl_registry *reg, uint32_t id)
130 {
131         /* Don't care */
132         (void) data;
133         (void) reg;
134         (void) id;
135 }
136
137 static const struct wl_registry_listener registry_listener = {
138         global_add,
139         global_remove,
140 };
141
142 static struct wl_surface *
143 getWlSurface(QPlatformNativeInterface *native, QWindow *window)
144 {
145         void *surf = native->nativeResourceForWindow("surface", window);
146         return static_cast<struct ::wl_surface *>(surf);
147 }
148
149 static struct wl_output *
150 getWlOutput(QPlatformNativeInterface *native, QScreen *screen)
151 {
152         void *output = native->nativeResourceForScreen("output", screen);
153         return static_cast<struct ::wl_output*>(output);
154 }
155
156 static struct wl_display *
157 getWlDisplay(QPlatformNativeInterface *native)
158 {
159         return static_cast<struct wl_display *>(
160                 native->nativeResourceForIntegration("display")
161         );
162 }
163
164 static void
165 register_agl_shell(QPlatformNativeInterface *native, struct shell_data *shell_data)
166 {
167         struct wl_display *wl;
168         struct wl_registry *registry;
169
170         wl = getWlDisplay(native);
171         registry = wl_display_get_registry(wl);
172
173         wl_registry_add_listener(registry, &registry_listener, shell_data);
174
175         /* Roundtrip to get all globals advertised by the compositor */
176         wl_display_roundtrip(wl);
177         wl_registry_destroy(registry);
178 }
179
180 static struct wl_surface *
181 create_component(QPlatformNativeInterface *native, QQmlComponent *comp,
182                  QScreen *screen, QObject **qobj)
183 {
184         QObject *obj = comp->create();
185         obj->setParent(screen);
186
187         QWindow *win = qobject_cast<QWindow *>(obj);
188         *qobj = obj;
189
190         return getWlSurface(native, win);
191 }
192
193 static QScreen *
194 find_screen(const char *screen_name)
195 {
196         QList<QScreen *> screens = qApp->screens();
197         QScreen *found = nullptr;
198         QString qstr_name = QString::fromUtf8(screen_name, -1);
199
200         for (QScreen *xscreen : screens) {
201                 if (qstr_name == xscreen->name()) {
202                         found = xscreen;
203                         break;
204                 }
205         }
206
207         return found;
208 }
209
210 static void
211 load_agl_shell_app(QPlatformNativeInterface *native,
212                    QQmlApplicationEngine *engine,
213                    struct agl_shell *agl_shell,
214                    const char *screen_name,
215                     bool is_demo)
216 {
217         struct wl_surface *bg, *top, *bottom;
218         struct wl_output *output;
219         QObject *qobj_bg, *qobj_top, *qobj_bottom;
220         QScreen *screen = nullptr;
221
222         if (is_demo) {
223                 QQmlComponent bg_comp(engine, QUrl("qrc:/background_demo.qml"));
224                 qInfo() << bg_comp.errors();
225
226                 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel_demo.qml"));
227                 qInfo() << top_comp.errors();
228
229                 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel_demo.qml"));
230                 qInfo() << bot_comp.errors();
231
232                 top = create_component(native, &top_comp, screen, &qobj_top);
233                 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
234                 bg = create_component(native, &bg_comp, screen, &qobj_bg);
235         } else {
236                 QQmlComponent bg_comp(engine, QUrl("qrc:/background.qml"));
237                 qInfo() << bg_comp.errors();
238
239                 QQmlComponent top_comp(engine, QUrl("qrc:/toppanel.qml"));
240                 qInfo() << top_comp.errors();
241
242                 QQmlComponent bot_comp(engine, QUrl("qrc:/bottompanel.qml"));
243                 qInfo() << bot_comp.errors();
244
245                 top = create_component(native, &top_comp, screen, &qobj_top);
246                 bottom = create_component(native, &bot_comp, screen, &qobj_bottom);
247                 bg = create_component(native, &bg_comp, screen, &qobj_bg);
248         }
249
250         if (!screen_name)
251                 screen = qApp->primaryScreen();
252         else
253                 screen = find_screen(screen_name);
254
255         if (!screen) {
256                 qDebug() << "No outputs present in the system.";
257                 return;
258         }
259
260         qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
261                 "first screen " << qApp->screens().first()->name();
262         output = getWlOutput(native, screen);
263
264         /* engine.rootObjects() works only if we had a load() */
265         StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
266         if (statusBar) {
267                 qDebug() << "got statusBar objectname, doing init()";
268                 statusBar->init(engine->rootContext());
269         }
270
271         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
272         agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
273         qDebug() << "Setting homescreen to screen  " << screen->name();
274
275         agl_shell_set_background(agl_shell, bg, output);
276
277         /* Delay the ready signal until after Qt has done all of its own setup
278          * in a.exec() */
279         QTimer::singleShot(500, [agl_shell](){
280                 agl_shell_ready(agl_shell);
281         });
282 }
283
284 int main(int argc, char *argv[])
285 {
286     setenv("QT_QPA_PLATFORM", "wayland", 1);
287     setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
288     QGuiApplication a(argc, argv);
289     const char *screen_name;
290     bool is_demo_val = false;
291     int ret = 0;
292     struct shell_data shell_data = { nullptr, nullptr, true, false };
293
294     QPlatformNativeInterface *native = qApp->platformNativeInterface();
295     screen_name = getenv("HOMESCREEN_START_SCREEN");
296
297     const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
298     if (is_demo && strcmp(is_demo, "1") == 0)
299         is_demo_val = true;
300
301     QCoreApplication::setOrganizationDomain("LinuxFoundation");
302     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
303     QCoreApplication::setApplicationName("HomeScreen");
304     QCoreApplication::setApplicationVersion("0.7.0");
305     /* we need to have an app_id */
306     a.setDesktopFileName("homescreen");
307
308     register_agl_shell(native, &shell_data);
309     if (!shell_data.shell) {
310         fprintf(stderr, "agl_shell extension is not advertised. "
311                 "Are you sure that agl-compositor is running?\n");
312         exit(EXIT_FAILURE);
313     }
314     if (!shell_data.shell_desktop) {
315         fprintf(stderr, "agl_shell_desktop extension is not advertised. "
316                 "Are you sure that agl-compositor is running?\n");
317         exit(EXIT_FAILURE);
318     }
319
320     while (ret != -1 && shell_data.wait_for_bound) {
321             ret = wl_display_dispatch(getWlDisplay(native));
322
323             if (shell_data.wait_for_bound)
324                     continue;
325     }
326
327     // verify that did bound successfully
328     if (!shell_data.bound_ok) {
329         fprintf(stderr, "agl_shell extension already in use by other shell client.\n");
330         exit(EXIT_FAILURE);
331     }
332
333     std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
334     Shell *aglShell = new Shell(agl_shell, &a);
335
336     // import C++ class to QML
337     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
338     qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
339
340     ApplicationLauncher *launcher = new ApplicationLauncher();
341     launcher->setCurrent(QStringLiteral("launcher"));
342     HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
343     homescreenHandler->init();
344
345     agl_shell_desktop_add_listener(shell_data.shell_desktop, &shell_desktop_listener, homescreenHandler);
346
347     QQmlApplicationEngine engine;
348     QQmlContext *context = engine.rootContext();
349
350     context->setContextProperty("homescreenHandler", homescreenHandler);
351     context->setContextProperty("launcher", launcher);
352     context->setContextProperty("weather", new Weather());
353     context->setContextProperty("bluetooth", new Bluetooth(false, context));
354
355     // we add it here even if we don't use it
356     context->setContextProperty("shell", aglShell);
357
358     /* instead of loading main.qml we load one-by-one each of the QMLs,
359      * divided now between several surfaces: panels, background.
360      */
361     load_agl_shell_app(native, &engine, shell_data.shell, screen_name, is_demo_val);
362
363     return a.exec();
364 }