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