Update to new weston design
[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 <QtGui/QGuiApplication>
21 #include <QtQml/QQmlApplicationEngine>
22 #include <QtQml/QQmlContext>
23 #include <QtQml/QQmlComponent>
24 #include <QtQml/qqml.h>
25 #include <QQuickWindow>
26 #include <QScreen>
27 #include <qpa/qplatformnativeinterface.h>
28
29 #include <cstdlib>
30 #include <cstring>
31 #include <wayland-client.h>
32
33 #include <weather.h>
34 #include <bluetooth.h>
35 #include "applicationlauncher.h"
36 #include "statusbarmodel.h"
37 #include "afm_user_daemon_proxy.h"
38 #include "mastervolume.h"
39 #include "hmi-debug.h"
40
41 #include "wayland-agl-shell-client-protocol.h"
42
43 // XXX: We want this DBus connection to be shared across the different
44 // QML objects, is there another way to do this, a nice way, perhaps?
45 org::AGL::afm::user *afm_user_daemon_proxy;
46
47 namespace {
48
49 struct Cleanup {
50     static inline void cleanup(org::AGL::afm::user *p) {
51         delete p;
52         afm_user_daemon_proxy = Q_NULLPTR;
53     }
54 };
55
56 }
57
58 static void global_add(void *data, struct wl_registry *reg, uint32_t name,
59                        const char *interface, uint32_t)
60 {
61     struct agl_shell **shell = static_cast<struct agl_shell **>(data);
62     if (strcmp(interface, agl_shell_interface.name) == 0) {
63         *shell = static_cast<struct agl_shell *>(wl_registry_bind(reg, name, &agl_shell_interface, 1));
64     }
65 }
66
67 static void global_remove(void *, struct wl_registry *, uint32_t)
68 {
69     // Don't care
70 }
71
72 static const struct wl_registry_listener registry_listener = {
73     global_add,
74     global_remove,
75 };
76
77 static struct wl_surface *create_component(QPlatformNativeInterface *native,
78                                            QQmlComponent *comp, QScreen *screen)
79 {
80         QObject *obj = comp->create();
81         obj->setParent(screen);
82
83         QWindow *win = qobject_cast<QWindow *>(obj);
84         return static_cast<struct wl_surface *>(native->nativeResourceForWindow("surface", win));
85 }
86
87 int main(int argc, char *argv[])
88 {
89     setenv("QT_QPA_PLATFORM", "wayland", 1);
90     QGuiApplication a(argc, argv);
91         QPlatformNativeInterface *native = qApp->platformNativeInterface();
92     struct wl_display *wl;
93     struct wl_registry *registry;
94     struct agl_shell *agl_shell = nullptr;
95
96         wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
97     registry = wl_display_get_registry(wl);
98
99     wl_registry_add_listener(registry, &registry_listener, &agl_shell);
100     // Roundtrip to get all globals advertised by the compositor
101     wl_display_roundtrip(wl);
102     wl_registry_destroy(registry);
103     
104     if (!agl_shell) {
105         qFatal("Compositor does not support AGL shell protocol");
106         return 1;
107     }
108
109     // use launch process
110     QScopedPointer<org::AGL::afm::user, Cleanup> afm_user_daemon_proxy(new org::AGL::afm::user("org.AGL.afm.user",
111                                                                                                "/org/AGL/afm/user",
112                                                                                                QDBusConnection::sessionBus(),
113                                                                                                0));
114     ::afm_user_daemon_proxy = afm_user_daemon_proxy.data();
115
116     QCoreApplication::setOrganizationDomain("LinuxFoundation");
117     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
118     QCoreApplication::setApplicationName("HomeScreen");
119     QCoreApplication::setApplicationVersion("0.7.0");
120
121     QCommandLineParser parser;
122     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
123     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
124     parser.addHelpOption();
125     parser.addVersionOption();
126     parser.process(a);
127     QStringList positionalArguments = parser.positionalArguments();
128
129     int port = 1700;
130     QString token = "wm";
131     QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
132
133     if (positionalArguments.length() == 2) {
134         port = positionalArguments.takeFirst().toInt();
135         token = positionalArguments.takeFirst();
136     }
137
138     HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
139
140     // import C++ class to QML
141     // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
142     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
143     qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
144
145     ApplicationLauncher *launcher = new ApplicationLauncher();
146
147     QUrl bindingAddress;
148     bindingAddress.setScheme(QStringLiteral("ws"));
149     bindingAddress.setHost(QStringLiteral("localhost"));
150     bindingAddress.setPort(port);
151     bindingAddress.setPath(QStringLiteral("/api"));
152
153     QUrlQuery query;
154     query.addQueryItem(QStringLiteral("token"), token);
155     bindingAddress.setQuery(query);
156
157 #if 0
158     // mail.qml loading
159     QQmlApplicationEngine engine;
160     engine.rootContext()->setContextProperty("bindingAddress", bindingAddress);
161     engine.rootContext()->setContextProperty("launcher", launcher);
162     engine.rootContext()->setContextProperty("weather", new Weather(bindingAddress));
163     engine.rootContext()->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
164     //engine.rootContext()->setContextProperty("screenInfo", &screenInfo);
165     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
166     engine.load(QUrl(QStringLiteral("qrc:/background.qml")));
167
168     auto root_objects = engine.rootObjects();
169     printf("num root objects: %d\n", root_objects.length());
170     QObject *root = engine.rootObjects().first();
171     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
172
173     for (auto o : root_objects) {
174             qDebug() << o->dynamicPropertyNames();
175     }
176
177     QList<QObject *> sobjs = engine.rootObjects();
178     StatusBarModel *statusBar = sobjs.first()->findChild<StatusBarModel *>("statusBar");
179     statusBar->init(bindingAddress, engine.rootContext());
180 #endif
181
182     QQmlEngine engine;
183     QQmlContext *context = engine.rootContext();
184     context->setContextProperty("bindingAddress", bindingAddress);
185     context->setContextProperty("launcher", launcher);
186     context->setContextProperty("weather", new Weather(bindingAddress));
187     context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
188
189     QQmlComponent bg_comp(&engine, QUrl("qrc:/background.qml"));
190     QQmlComponent top_comp(&engine, QUrl("qrc:/toppanel.qml"));
191     QQmlComponent bot_comp(&engine, QUrl("qrc:/bottompanel.qml"));
192
193     for (QScreen *screen : qApp->screens()) {
194         struct wl_output *output;
195
196         output = static_cast<struct wl_output *>(native->nativeResourceForScreen("output", screen));
197
198         struct wl_surface *bg = create_component(native, &bg_comp, screen);
199         agl_shell_set_background(agl_shell, bg, output);
200
201         struct wl_surface *top = create_component(native, &top_comp, screen);
202         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
203
204         struct wl_surface *bot = create_component(native, &bot_comp, screen);
205         agl_shell_set_panel(agl_shell, bot, output, AGL_SHELL_EDGE_BOTTOM);
206     }
207
208     // Delay the ready signal until after Qt has done all of its own setup in a.exec()
209         QTimer::singleShot(0, [agl_shell](){
210         agl_shell_ready(agl_shell);
211         agl_shell_destroy(agl_shell);
212     });
213
214     return a.exec();
215 }