Adopt window manager feature to get rendering right
[apps/mediaplayer.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016, 2017 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 <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
29 #include <QQuickWindow>
30 #include "qlibwindowmanager.h"
31
32 static QLibWindowmanager* qwm;
33 static std::string myname = std::string("MediaPlayer");
34
35 int main(int argc, char *argv[])
36 {
37     QGuiApplication app(argc, argv);
38
39     QQuickStyle::setStyle("AGL");
40
41     QQmlApplicationEngine engine;
42     QQmlContext *context = engine.rootContext();
43
44     QCommandLineParser parser;
45     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
46     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
47     parser.addHelpOption();
48     parser.addVersionOption();
49     parser.process(app);
50     QStringList positionalArguments = parser.positionalArguments();
51
52     if (positionalArguments.length() == 2) {
53         int port = positionalArguments.takeFirst().toInt();
54         QString secret = positionalArguments.takeFirst();
55         QUrl bindingAddress;
56         bindingAddress.setScheme(QStringLiteral("ws"));
57         bindingAddress.setHost(QStringLiteral("localhost"));
58         bindingAddress.setPort(port);
59         bindingAddress.setPath(QStringLiteral("/api"));
60         QUrlQuery query;
61         query.addQueryItem(QStringLiteral("token"), secret);
62         bindingAddress.setQuery(query);
63         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
64          qwm = new QLibWindowmanager();
65         // WindowManager
66         if(qwm->init(port,secret) != 0){
67             exit(EXIT_FAILURE);
68         }
69         if (qwm->requestSurface(myname.c_str()) != 0) {
70             exit(EXIT_FAILURE);
71         }
72         qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm](json_object *object) {
73             fprintf(stderr, "Surface got syncDraw!\n");
74             qwm->endDraw(myname.c_str());
75             });
76         qwm->set_event_handler(QLibWindowmanager::Event_FlushDraw, [](json_object *object) {
77             fprintf(stderr, "Surface got flushDraw!\n");;
78             });
79     }
80
81     engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
82         QObject *root = engine.rootObjects().first();
83         QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
84         QObject::connect(window, SIGNAL(frameSwapped()),
85             qwm, SLOT(slotActivateSurface()));  // This should be disconnected, but when...
86
87     return app.exec();
88 }