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