add homescreen feature to get shortcut pushed event
[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 #include "qlibsoundmanager.h"
32 #include <libhomescreen.hpp>
33
34 static LibHomeScreen* hs;
35 static QLibWindowmanager* qwm;
36 static QLibSoundmanager* smw;
37 static std::string myname = std::string("MediaPlayer");
38
39 int main(int argc, char *argv[])
40 {
41     QGuiApplication app(argc, argv);
42
43     QQuickStyle::setStyle("AGL");
44
45     QQmlApplicationEngine engine;
46     QQmlContext *context = engine.rootContext();
47
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();
53     parser.process(app);
54     QStringList positionalArguments = parser.positionalArguments();
55
56     if (positionalArguments.length() == 2) {
57         int port = positionalArguments.takeFirst().toInt();
58         QString secret = positionalArguments.takeFirst();
59         QUrl bindingAddress;
60         bindingAddress.setScheme(QStringLiteral("ws"));
61         bindingAddress.setHost(QStringLiteral("localhost"));
62         bindingAddress.setPort(port);
63         bindingAddress.setPath(QStringLiteral("/api"));
64         QUrlQuery query;
65         query.addQueryItem(QStringLiteral("token"), secret);
66         bindingAddress.setQuery(query);
67         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
68
69         std::string token = secret.toStdString();
70
71         hs = new LibHomeScreen();
72         qwm = new QLibWindowmanager();
73         smw = new QLibSoundmanager();
74
75         // WindowManager
76         if(qwm->init(port,secret) != 0){
77             exit(EXIT_FAILURE);
78         }
79         if (qwm->requestSurface(myname.c_str()) != 0) {
80             exit(EXIT_FAILURE);
81         }
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());
85             });
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");
91             });
92         
93         // HomeScreen
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"));
98             if(myname == appname)
99             {
100                 qDebug("Surface %s got tapShortcut\n", appname);
101                 qwm->activateSurface(myname.c_str());
102             }
103         });
104
105         // SoundManager, event handler is set inside smw
106         smw->init(port, secret);
107
108         engine.rootContext()->setContextProperty("smw",smw);
109
110     }
111
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)));
121
122     return app.exec();
123 }