2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2016, 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 <QtQuickControls2/QQuickStyle>
29 #include <QQuickWindow>
30 #include "qlibwindowmanager.h"
31 #include "qlibsoundmanager.h"
32 #include <libhomescreen.hpp>
34 static LibHomeScreen* hs;
35 static QLibWindowmanager* qwm;
36 static QLibSoundmanager* smw;
37 static std::string myname = std::string("MediaPlayer");
39 int main(int argc, char *argv[])
41 QGuiApplication app(argc, argv);
43 QQuickStyle::setStyle("AGL");
45 QQmlApplicationEngine engine;
46 QQmlContext *context = engine.rootContext();
48 QCommandLineParser parser;
49 parser.addPositionalArgument("port", app.translate("main", "port for binding"));
50 parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
51 parser.addHelpOption();
52 parser.addVersionOption();
54 QStringList positionalArguments = parser.positionalArguments();
56 if (positionalArguments.length() == 2) {
57 int port = positionalArguments.takeFirst().toInt();
58 QString secret = positionalArguments.takeFirst();
60 bindingAddress.setScheme(QStringLiteral("ws"));
61 bindingAddress.setHost(QStringLiteral("localhost"));
62 bindingAddress.setPort(port);
63 bindingAddress.setPath(QStringLiteral("/api"));
65 query.addQueryItem(QStringLiteral("token"), secret);
66 bindingAddress.setQuery(query);
67 context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
69 std::string token = secret.toStdString();
71 hs = new LibHomeScreen();
72 qwm = new QLibWindowmanager();
73 smw = new QLibSoundmanager();
76 if(qwm->init(port,secret) != 0){
79 if (qwm->requestSurface(myname.c_str()) != 0) {
82 qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm](json_object *object) {
83 fprintf(stderr, "Surface got syncDraw!\n");
84 qwm->endDraw(myname.c_str());
86 qwm->set_event_handler(QLibWindowmanager::Event_FlushDraw, [&engine, smw](json_object *object) {
87 fprintf(stderr, "Surface got flushDraw!\n");
88 QObject *root = engine.rootObjects().first();
89 int sourceID = root->property("sourceID").toInt();
90 smw->connect(sourceID, "default");
94 hs->init(port, token.c_str());
95 hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [qwm](json_object *object){
96 const char *appname = json_object_get_string(
97 json_object_object_get(object, "application_name"));
100 qDebug("Surface %s got tapShortcut\n", appname);
101 qwm->activateSurface(myname.c_str());
105 // SoundManager, event handler is set inside smw
106 smw->init(port, secret);
108 engine.rootContext()->setContextProperty("smw",smw);
112 engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
113 QObject *root = engine.rootObjects().first();
114 QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
115 QObject::connect(window, SIGNAL(frameSwapped()),
116 qwm, SLOT(slotActivateSurface())); // This should be disconnected, but when...
117 QObject::connect(smw, SIGNAL(reply(QVariant)),
118 root, SLOT(slotReply(QVariant)));
119 QObject::connect(smw, SIGNAL(event(QVariant, QVariant)),
120 root, SLOT(slotEvent(QVariant, QVariant)));