Test for Mediaplayer of HMI Framework at dab version
[apps/mediaplayer.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 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 <QQuickWindow>
28 #include <QtQuickControls2/QQuickStyle>
29 #include "qlibsoundmanager.h"
30 #include <libhomescreen.hpp>
31
32 #include "playlistwithmetadata.h"
33 #include "qlibwindowmanager.h"
34
35 static LibHomeScreen* hs;
36 static QLibWindowmanager* qwm;
37 static QLibSoundmanager* smw;
38 static std::string myname = std::string("MediaPlayer");
39
40 using namespace std;
41 static void onRep(struct json_object* reply_contents);
42 static void onEv(const std::string& event, struct json_object* event_contents);
43
44 int main(int argc, char *argv[])
45 {
46
47     QGuiApplication app(argc, argv);
48     qwm = new QLibWindowmanager();
49     hs = new LibHomeScreen();
50     QQuickStyle::setStyle("AGL");
51
52     qmlRegisterType<PlaylistWithMetadata>("MediaPlayer", 1, 0, "PlaylistWithMetadata");
53
54     QQmlApplicationEngine engine;
55     QQmlContext *context = engine.rootContext();
56
57     QCommandLineParser parser;
58     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
59     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
60     parser.addHelpOption();
61     parser.addVersionOption();
62     parser.process(app);
63     QStringList positionalArguments = parser.positionalArguments();
64
65     if (positionalArguments.length() == 2) {
66         int port = positionalArguments.takeFirst().toInt();
67         QString secret = positionalArguments.takeFirst();
68         QUrl bindingAddress;
69         bindingAddress.setScheme(QStringLiteral("ws"));
70         bindingAddress.setHost(QStringLiteral("localhost"));
71         bindingAddress.setPort(port);
72         bindingAddress.setPath(QStringLiteral("/api"));
73         QUrlQuery query;
74         query.addQueryItem(QStringLiteral("token"), secret);
75         bindingAddress.setQuery(query);
76         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
77
78         /* This is window manager test */
79         std::string token = secret.toStdString();
80
81         if(qwm->init(port,token.c_str()) != 0){
82             exit(EXIT_FAILURE);
83         }
84
85         if (qwm->requestSurface(myname.c_str()) != 0) {
86             exit(EXIT_FAILURE);
87         }
88
89         // prepare to use homescreen
90         hs->init(port, token.c_str());
91
92         hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [qwm](json_object *object){
93             const char *appname = json_object_get_string(
94                 json_object_object_get(object, "application_name"));
95             if(myname == appname)
96             {
97                 qDebug("[HS]mediaplayer: activateSurface\n");
98                 qwm->activateSurface(myname.c_str());
99             }
100         });
101
102         // prepare to use soundmangaer
103         smw = new QLibSoundmanager();
104         smw->init(port, secret);
105         engine.rootContext()->setContextProperty("smw",smw);
106
107         qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [smw, qwm](json_object *object) {
108             fprintf(stderr, "[WM]Surface got syncDraw!\n");
109             qwm->endDraw(myname.c_str());
110             // Something to to if needed
111         });
112         qwm->set_event_handler(QLibWindowmanager::Event_FlushDraw, [smw, &engine](json_object *object) {
113             fprintf(stderr, "[WM]Surface got FlushDraw!\n");
114             // Something to to if needed
115             QObject *root = engine.rootObjects().first();
116             int sourceID = root->property("sourceID").toInt();
117             smw->connect(sourceID, "default");
118         });
119     }
120     engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
121
122     QObject *root = engine.rootObjects().first();
123     QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
124     QObject::connect(window, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateSurface()));
125     QObject::connect(smw, SIGNAL(reply(QVariant)),
126         root, SLOT(slotReply(QVariant)));
127     QObject::connect(smw, SIGNAL(event(QVariant, QVariant)),
128         root, SLOT(slotEvent(QVariant, QVariant)));
129         
130     return app.exec();
131 }