e40cf981cdb25cea4f276b7b3bbc636f7a303440
[apps/mediaplayer.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2017 Konsulko Group
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 #include <QQuickWindow>
29 #include <libhomescreen.hpp>
30 #include <qlibwindowmanager.h>
31 #include <mediaplayer.h>
32
33 #include <unistd.h>
34
35 int main(int argc, char *argv[])
36 {
37     QString graphic_role = QString("music");
38
39     QGuiApplication app(argc, argv);
40
41     QQuickStyle::setStyle("AGL");
42
43     QQmlApplicationEngine engine;
44     QQmlContext *context = engine.rootContext();
45
46     QCommandLineParser parser;
47     parser.addPositionalArgument("port", app.translate("main", "port for binding"));
48     parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
49     parser.addHelpOption();
50     parser.addVersionOption();
51     parser.process(app);
52     QStringList positionalArguments = parser.positionalArguments();
53
54     if (positionalArguments.length() == 2) {
55         int port = positionalArguments.takeFirst().toInt();
56         QString secret = positionalArguments.takeFirst();
57         QUrl bindingAddress;
58         bindingAddress.setScheme(QStringLiteral("ws"));
59         bindingAddress.setHost(QStringLiteral("localhost"));
60         bindingAddress.setPort(port);
61         bindingAddress.setPath(QStringLiteral("/api"));
62         QUrlQuery query;
63         query.addQueryItem(QStringLiteral("token"), secret);
64         bindingAddress.setQuery(query);
65         context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
66         std::string token = secret.toStdString();
67         LibHomeScreen* hs = new LibHomeScreen();
68         QLibWindowmanager* qwm = new QLibWindowmanager();
69
70         // WindowManager
71         if(qwm->init(port,secret) != 0){
72             exit(EXIT_FAILURE);
73         }
74         AGLScreenInfo screenInfo(qwm->get_scale_factor());
75         // Request a surface as described in layers.json windowmanager’s file
76         if (qwm->requestSurface(graphic_role) != 0) {
77             exit(EXIT_FAILURE);
78         }
79         // Create an event callback against an event type. Here a lambda is called when SyncDraw event occurs
80         qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm, &graphic_role](json_object *object) {
81             fprintf(stderr, "Surface got syncDraw!\n");
82             qwm->endDraw(graphic_role);
83         });
84
85         // HomeScreen
86         hs->init(port, token.c_str());
87         // Set the event handler for Event_ShowWindow which will activate the surface for windowmanager
88         hs->set_event_handler(LibHomeScreen::Event_ShowWindow, [qwm, &graphic_role](json_object *object){
89             qDebug("Surface %s got showWindow\n", graphic_role.toStdString().c_str());
90             qwm->activateWindow(graphic_role);
91         });
92
93         context->setContextProperty("AlbumArt", "");
94         context->setContextProperty("mediaplayer", new Mediaplayer(bindingAddress, context));
95         context->setContextProperty(QStringLiteral("screenInfo"), &screenInfo);
96
97         engine.load(QUrl(QStringLiteral("qrc:/MediaPlayer.qml")));
98         QObject *root = engine.rootObjects().first();
99         QQuickWindow *window = qobject_cast<QQuickWindow *>(root);
100         QObject::connect(window, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateWindow()
101         ));
102     }
103     return app.exec();
104 }