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