homescreen/: Plug-in the launcher in the applicationlauncher
[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 <QUrlQuery>
28 #include <QTimer>
29 #include <qpa/qplatformnativeinterface.h>
30
31 #include <cstdlib>
32 #include <cstring>
33 #include <memory>
34 #include <wayland-client.h>
35
36 #include <weather.h>
37 #include <bluetooth.h>
38 #include "applicationlauncher.h"
39 #include "statusbarmodel.h"
40 #include "mastervolume.h"
41 #include "shell.h"
42 #include "hmi-debug.h"
43
44 #include "wayland-agl-shell-client-protocol.h"
45
46 #define CONNECT_STR     "unix:/run/platform/apis/ws/afm-main"
47
48 static void global_add(void *data, struct wl_registry *reg, uint32_t name,
49                        const char *interface, uint32_t)
50 {
51     struct agl_shell **shell = static_cast<struct agl_shell **>(data);
52     if (strcmp(interface, agl_shell_interface.name) == 0) {
53         *shell = static_cast<struct agl_shell *>(wl_registry_bind(reg, name, &agl_shell_interface, 1));
54     }
55 }
56
57 static void global_remove(void *, struct wl_registry *, uint32_t)
58 {
59     // Don't care
60 }
61
62 static const struct wl_registry_listener registry_listener = {
63     global_add,
64     global_remove,
65 };
66
67 static struct wl_surface *create_component(QPlatformNativeInterface *native,
68                                            QQmlComponent *comp, QScreen *screen)
69 {
70     QObject *obj = comp->create();
71     obj->setParent(screen);
72
73     QWindow *win = qobject_cast<QWindow *>(obj);
74     return static_cast<struct wl_surface *>(native->nativeResourceForWindow("surface", win));
75 }
76
77 int main(int argc, char *argv[])
78 {
79     setenv("QT_QPA_PLATFORM", "wayland", 1);
80     QGuiApplication a(argc, argv);
81     QPlatformNativeInterface *native = qApp->platformNativeInterface();
82     struct wl_display *wl;
83     struct wl_registry *registry;
84     struct agl_shell *agl_shell = nullptr;
85
86     wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
87     registry = wl_display_get_registry(wl);
88
89     wl_registry_add_listener(registry, &registry_listener, &agl_shell);
90     // Roundtrip to get all globals advertised by the compositor
91     wl_display_roundtrip(wl);
92     wl_registry_destroy(registry);
93
94     if (!agl_shell) {
95         qFatal("Compositor does not support AGL shell protocol");
96         return 1;
97     }
98     std::shared_ptr<struct agl_shell> shell{agl_shell, agl_shell_destroy};
99
100     QCoreApplication::setOrganizationDomain("LinuxFoundation");
101     QCoreApplication::setOrganizationName("AutomotiveGradeLinux");
102     QCoreApplication::setApplicationName("HomeScreen");
103     QCoreApplication::setApplicationVersion("0.7.0");
104
105     QCommandLineParser parser;
106     parser.addPositionalArgument("port", a.translate("main", "port for binding"));
107     parser.addPositionalArgument("secret", a.translate("main", "secret for binding"));
108     parser.addHelpOption();
109     parser.addVersionOption();
110     parser.process(a);
111     QStringList positionalArguments = parser.positionalArguments();
112
113     int port = 1700;
114     QString token = "wm";
115     QString graphic_role = "homescreen"; // defined in layers.json in Window Manager
116
117     if (positionalArguments.length() == 2) {
118         port = positionalArguments.takeFirst().toInt();
119         token = positionalArguments.takeFirst();
120     }
121
122     HMI_DEBUG("HomeScreen","port = %d, token = %s", port, token.toStdString().c_str());
123
124     // import C++ class to QML
125     // qmlRegisterType<ApplicationLauncher>("HomeScreen", 1, 0, "ApplicationLauncher");
126     qmlRegisterType<StatusBarModel>("HomeScreen", 1, 0, "StatusBarModel");
127     qmlRegisterType<MasterVolume>("MasterVolume", 1, 0, "MasterVolume");
128
129     ApplicationLauncher *launcher = new ApplicationLauncher(CONNECT_STR, &a);
130
131     QUrl bindingAddress;
132     bindingAddress.setScheme(QStringLiteral("ws"));
133     bindingAddress.setHost(QStringLiteral("localhost"));
134     bindingAddress.setPort(port);
135     bindingAddress.setPath(QStringLiteral("/api"));
136
137     QUrlQuery query;
138     query.addQueryItem(QStringLiteral("token"), token);
139     bindingAddress.setQuery(query);
140
141     QQmlEngine engine;
142     QQmlContext *context = engine.rootContext();
143     context->setContextProperty("bindingAddress", bindingAddress);
144     context->setContextProperty("launcher", launcher);
145     context->setContextProperty("weather", new Weather(bindingAddress));
146     context->setContextProperty("bluetooth", new Bluetooth(bindingAddress, engine.rootContext()));
147     context->setContextProperty("shell", new Shell(shell, &a));
148
149     QQmlComponent bg_comp(&engine, QUrl("qrc:/background.qml"));
150     qInfo() << bg_comp.errors();
151
152     QQmlComponent top_comp(&engine, QUrl("qrc:/toppanel.qml"));
153     qInfo() << top_comp.errors();
154
155     QQmlComponent bot_comp(&engine, QUrl("qrc:/bottompanel.qml"));
156     qInfo() << bot_comp.errors();
157
158     for (QScreen *screen : qApp->screens()) {
159         struct wl_output *output;
160
161         output = static_cast<struct wl_output *>(native->nativeResourceForScreen("output", screen));
162
163         struct wl_surface *bg = create_component(native, &bg_comp, screen);
164         struct wl_surface *top = create_component(native, &top_comp, screen);
165         struct wl_surface *bot = create_component(native, &bot_comp, screen);
166
167         wl_display_dispatch(wl);
168
169         agl_shell_set_panel(agl_shell, top, output, AGL_SHELL_EDGE_TOP);
170         agl_shell_set_panel(agl_shell, bot, output, AGL_SHELL_EDGE_BOTTOM);
171         agl_shell_set_background(agl_shell, bg, output);
172     }
173
174     // Delay the ready signal until after Qt has done all of its own setup in a.exec()
175     QTimer::singleShot(0, [shell](){
176         agl_shell_ready(shell.get());
177     });
178
179     return a.exec();
180 }