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