app: Testing split type of surfaces
[apps/mediaplayer.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2017 Konsulko Group
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 <QtCore/QCommandLineParser>
19 #include <QtCore/QDebug>
20 #include <QtCore/QDir>
21 #include <QtCore/QStandardPaths>
22 #include <QtCore/QUrlQuery>
23 #include <QtGui/QGuiApplication>
24 #include <QtQml/QQmlApplicationEngine>
25 #include <QtQml/QQmlContext>
26 #include <QtQml/qqml.h>
27 #include <QtQuickControls2/QQuickStyle>
28 #include <QQuickWindow>
29
30 #include <mediaplayer.h>
31
32 #include <qpa/qplatformnativeinterface.h>
33 #include <wayland-client.h>
34 #include "wayland-agl-shell-desktop-client-protocol.h"
35
36 #include <unistd.h>
37
38 static struct wl_output *
39 getWlOutput(QScreen *screen)
40 {
41         QPlatformNativeInterface *native = qApp->platformNativeInterface();
42         void *output = native->nativeResourceForScreen("output", screen);
43         return static_cast<struct ::wl_output*>(output);
44 }
45
46 static void
47 global_add(void *data, struct wl_registry *reg, uint32_t name,
48            const char *interface, uint32_t version)
49 {
50         struct agl_shell_desktop **shell =
51                 static_cast<struct agl_shell_desktop **>(data);
52
53         if (strcmp(interface, agl_shell_desktop_interface.name) == 0) {
54                 *shell = static_cast<struct agl_shell_desktop *>(
55                         wl_registry_bind(reg, name, &agl_shell_desktop_interface, version)
56                 );
57         }
58 }
59
60 static void global_remove(void *data, struct wl_registry *reg, uint32_t id)
61 {
62         (void) data;
63         (void) reg;
64         (void) id;
65 }
66
67 static const struct wl_registry_listener registry_listener = {
68         global_add,
69         global_remove,
70 };
71
72 static void
73 application_id_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
74                 const char *app_id)
75 {
76         (void) data;
77         (void) app_id;
78         (void) agl_shell_desktop;
79 }
80
81 static void
82 application_state_event(void *data, struct agl_shell_desktop *agl_shell_desktop,
83                         const char *app_id, const char *app_data,
84                         uint32_t app_state, uint32_t app_role)
85 {
86         (void) data;
87         (void) app_role;
88         (void) app_state;
89         (void) app_data;
90         (void) app_id;
91         (void) agl_shell_desktop;
92 }
93
94 static const struct agl_shell_desktop_listener agl_shell_desk_listener = {
95         application_id_event,
96         application_state_event,
97 };
98
99 static struct agl_shell_desktop *
100 register_agl_shell_desktop(void)
101 {
102         struct wl_display *wl;
103         struct wl_registry *registry;
104         struct agl_shell_desktop *shell = nullptr;
105
106         QPlatformNativeInterface *native = qApp->platformNativeInterface();
107
108         wl = static_cast<struct wl_display *>(native->nativeResourceForIntegration("display"));
109         registry = wl_display_get_registry(wl);
110
111         wl_registry_add_listener(registry, &registry_listener, &shell);
112         // Roundtrip to get all globals advertised by the compositor
113         wl_display_roundtrip(wl);
114         wl_registry_destroy(registry);
115
116         return shell;
117 }
118
119 static void
120 setup_window_vertical(int type, const QString &myname)
121 {
122         struct agl_shell_desktop *shell_desktop = nullptr;
123         struct wl_output *output;
124
125         if (type == 0)
126                 return;
127
128         shell_desktop = register_agl_shell_desktop();
129         output = getWlOutput(qApp->screens().first());
130
131         // not necessary
132         if (shell_desktop)
133                 agl_shell_desktop_add_listener(shell_desktop,
134                                                &agl_shell_desk_listener,
135                                                NULL);
136
137         if (type == 1) {
138                 agl_shell_desktop_set_app_property(shell_desktop,
139                                 myname.toStdString().c_str(),
140                                 AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_VERTICAL,
141                                 0, 0, output);
142                 qDebug() << "setting vertical";
143         } else {
144                 agl_shell_desktop_set_app_property(shell_desktop,
145                                 myname.toStdString().c_str(),
146                                 AGL_SHELL_DESKTOP_APP_ROLE_SPLIT_HORIZONTAL,
147                                 0, 0, output);
148                 qDebug() << "setting horizontal";
149         }
150 }
151
152 int main(int argc, char *argv[])
153 {
154     QString graphic_role = QString("music");
155     QString myname = QString("mediaplayer");
156
157     QGuiApplication app(argc, argv);
158
159     QQuickStyle::setStyle("AGL");
160
161     QQmlApplicationEngine engine;
162     QQmlContext *context = engine.rootContext();
163
164     QCommandLineParser parser;
165     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
166     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
167     parser.addPositionalArgument("split", app.translate("main", "split type"));
168     parser.addHelpOption();
169     parser.addVersionOption();
170     parser.process(app);
171     QStringList positionalArguments = parser.positionalArguments();
172
173     if (positionalArguments.length() >= 2) {
174         int port = positionalArguments.takeFirst().toInt();
175         QString secret = positionalArguments.takeFirst();
176
177         // 0, no split, 1 splitv, 2 splith
178         if (positionalArguments.length() == 1) {
179                 int split_type = positionalArguments.takeFirst().toInt();
180                 setup_window_vertical(split_type, myname);
181         }
182
183
184         QUrl bindingAddress;
185         bindingAddress.setScheme(QStringLiteral("ws"));
186         bindingAddress.setHost(QStringLiteral("localhost"));
187         bindingAddress.setPort(port);
188         bindingAddress.setPath(QStringLiteral("/api"));
189         QUrlQuery query;
190         query.addQueryItem(QStringLiteral("token"), secret);
191         bindingAddress.setQuery(query);
192         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
193
194         context->setContextProperty("AlbumArt", "");
195         context->setContextProperty("mediaplayer", new Mediaplayer(bindingAddress, context));
196
197         engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
198     }
199
200     return app.exec();
201 }