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