2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2017 Toyota Motor Corporation
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
32 #include "playlistwithmetadata.h"
33 #include "qlibwindowmanager.h"
35 static LibHomeScreen* hs;
36 static QLibWindowmanager* qwm;
37 static QLibSoundmanager* smw;
38 static std::string myname = std::string("MediaPlayer");
41 static void onRep(struct json_object* reply_contents);
42 static void onEv(const std::string& event, struct json_object* event_contents);
44 int main(int argc, char *argv[])
47 QGuiApplication app(argc, argv);
48 qwm = new QLibWindowmanager();
49 hs = new LibHomeScreen();
50 QQuickStyle::setStyle("AGL");
52 qmlRegisterType<PlaylistWithMetadata>("MediaPlayer", 1, 0, "PlaylistWithMetadata");
54 QQmlApplicationEngine engine;
55 QQmlContext *context = engine.rootContext();
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();
63 QStringList positionalArguments = parser.positionalArguments();
65 if (positionalArguments.length() == 2) {
66 int port = positionalArguments.takeFirst().toInt();
67 QString secret = positionalArguments.takeFirst();
69 bindingAddress.setScheme(QStringLiteral("ws"));
70 bindingAddress.setHost(QStringLiteral("localhost"));
71 bindingAddress.setPort(port);
72 bindingAddress.setPath(QStringLiteral("/api"));
74 query.addQueryItem(QStringLiteral("token"), secret);
75 bindingAddress.setQuery(query);
76 context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
78 /* This is window manager test */
79 std::string token = secret.toStdString();
81 if(qwm->init(port,token.c_str()) != 0){
85 if (qwm->requestSurface(myname.c_str()) != 0) {
89 // prepare to use homescreen
90 hs->init(port, token.c_str());
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"));
97 qDebug("[HS]mediaplayer: activateSurface\n");
98 qwm->activateSurface(myname.c_str());
102 // prepare to use soundmangaer
103 smw = new QLibSoundmanager();
104 smw->init(port, secret);
105 engine.rootContext()->setContextProperty("smw",smw);
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
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");
120 engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
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)));