homescreen: Bail out if no screens are found
[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         if (!screen) {
225                 qDebug() << "No outputs present in the system.";
226                 return;
227         }
228
229         qDebug() << "found primary screen " << qApp->primaryScreen()->name() <<
230                 "first screen " << qApp->screens().first()->name();
231         output = getWlOutput(native, screen);
232
233         /* engine.rootObjects() works only if we had a load() */
234         StatusBarModel *statusBar = qobj_top->findChild<StatusBarModel *>("statusBar");
235         if (statusBar) {
236                 qDebug() << "got statusBar objectname, doing init()";
237                 statusBar->init(engine->rootContext());
238         }
239
240         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
241         agl_shell_set_panel(agl_shell, bottom, output, AGL_SHELL_EDGE_BOTTOM);
242         qDebug() << "Setting homescreen to screen  " << screen->name();
243
244         agl_shell_set_background(agl_shell, bg, output);
245
246         /* Delay the ready signal until after Qt has done all of its own setup
247          * in a.exec() */
248         QTimer::singleShot(500, [agl_shell](){
249                 agl_shell_ready(agl_shell);
250         });
251 }
252
253 int main(int argc, char *argv[])
254 {
255     setenv("QT_QPA_PLATFORM", "wayland", 1);
256     setenv("QT_QUICK_CONTROLS_STYLE", "AGL", 1);
257     QGuiApplication a(argc, argv);
258     const char *screen_name;
259     bool is_demo_val = false;
260     struct shell_data shell_data = { nullptr, nullptr };
261
262     QPlatformNativeInterface *native = qApp->platformNativeInterface();
263     screen_name = getenv("HOMESCREEN_START_SCREEN");
264
265     const char *is_demo = getenv("HOMESCREEN_DEMO_CI");
266     if (is_demo && strcmp(is_demo, "1") == 0)
267         is_demo_val = true;
268
269     QCoreApplication::setOrganizationDomain("LinuxFoundation");
270     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
271     QCoreApplication::setApplicationName("HomeScreen");
272     QCoreApplication::setApplicationVersion("0.7.0");
273     /* we need to have an app_id */
274     a.setDesktopFileName("homescreen");
275
276     register_agl_shell(native, &shell_data);
277     if (!shell_data.shell) {
278         fprintf(stderr, "agl_shell extension is not advertised. "
279                 "Are you sure that agl-compositor is running?\n");
280         exit(EXIT_FAILURE);
281     }
282     if (!shell_data.shell_desktop) {
283         fprintf(stderr, "agl_shell_desktop extension is not advertised. "
284                 "Are you sure that agl-compositor is running?\n");
285         exit(EXIT_FAILURE);
286     }
287
288     std::shared_ptr<struct agl_shell> agl_shell{shell_data.shell, agl_shell_destroy};
289     Shell *aglShell = new Shell(agl_shell, &a);
290
291     // import C++ class to QML
292     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
293     qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
294
295     ApplicationLauncher *launcher = new ApplicationLauncher();
296     launcher->setCurrent(QStringLiteral("launcher"));
297     HomescreenHandler* homescreenHandler = new HomescreenHandler(aglShell, launcher);
298     homescreenHandler->init();
299
300     agl_shell_desktop_add_listener(shell_data.shell_desktop, &shell_desktop_listener, homescreenHandler);
301
302     QQmlApplicationEngine engine;
303     QQmlContext *context = engine.rootContext();
304
305     context->setContextProperty("homescreenHandler", homescreenHandler);
306     context->setContextProperty("launcher", launcher);
307     context->setContextProperty("weather", new Weather());
308     context->setContextProperty("bluetooth", new Bluetooth(false, context));
309
310     // we add it here even if we don't use it
311     context->setContextProperty("shell", aglShell);
312
313     /* instead of loading main.qml we load one-by-one each of the QMLs,
314      * divided now between several surfaces: panels, background.
315      */
316     load_agl_shell_app(native, &engine, shell_data.shell, screen_name, is_demo_val);
317
318     return a.exec();
319 }