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