9c6339fa8469100d80bccdc12a9446a6647ff2cb
[apps/mixer.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016,2017 Konsulko Group
4  * Copyright (C) 2018 IoT.bzh
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <QtCore/QDebug>
20 #include <QtCore/QCommandLineParser>
21 #include <QtCore/QUrlQuery>
22 #include <QtCore/QDir>
23 #include <QtCore/QStandardPaths>
24 #include <QtCore/QThread>
25 #include <QtGui/QGuiApplication>
26 #include <QtQml/QQmlApplicationEngine>
27 #include <QtQml/QQmlContext>
28 #include <QtQuickControls2/QQuickStyle>
29 #include <QtQml/qqml.h>
30 #include <QtQuick/qquickitem.h>
31 #include <QtQuick/qquickview.h>
32 #include <QQuickWindow>
33 #include <libhomescreen.hpp>
34 #include <qlibwindowmanager.h>
35 #include "mixer.h"
36
37 int main(int argc, char *argv[])
38 {
39         QString myname = QString("Mixer");
40
41         QGuiApplication app(argc, argv);
42
43         QQuickStyle::setStyle("AGL");
44
45         QCommandLineParser parser;
46         parser.addPositionalArgument("port", app.translate("main", "port for binding"));
47         parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
48         parser.addHelpOption();
49         parser.addVersionOption();
50         parser.process(app);
51         QStringList positionalArguments = parser.positionalArguments();
52
53     qmlRegisterType<Mixer>("Mixer", 1, 0, "Mixer");
54
55         QQmlApplicationEngine engine;
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                 QQmlContext *context = engine.rootContext();
68                 context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
69
70                 std::string token = secret.toStdString();
71                 LibHomeScreen* hs = new LibHomeScreen();
72                 QLibWindowmanager* qwm = new QLibWindowmanager();
73
74                 // WindowManager
75                 if(qwm->init(port,secret) != 0){
76                         exit(EXIT_FAILURE);
77                 }
78                 // Request a surface as described in layers.json windowmanager’s file
79                 if (qwm->requestSurface(myname) != 0) {
80                         exit(EXIT_FAILURE);
81                 }
82                 // Create an event callbnewack against an event type. Here a lambda is called when SyncDraw event occurs
83             qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm, myname](json_object*) {
84                                 fprintf(stderr, "Surface got syncDraw!\n");
85                                 qwm->endDraw(myname);
86                         });
87
88                 // HomeScreen
89                 hs->init(port, token.c_str());
90                 // Set the event handler for Event_TapShortcut which will activate the surface for windowmanager
91                 hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [qwm, myname](json_object *object){
92                         json_object *appnameJ = nullptr;
93                         if(json_object_object_get_ex(object, "application_name", &appnameJ))
94                         {
95                                 const char *appname = json_object_get_string(appnameJ);
96                                 if(myname == appname)
97                                 {
98                                         qDebug("Surface %s got tapShortcut\n", appname);
99                                         qwm->activateSurface(myname);
100                                 }
101                         }
102                 });
103
104                 engine.load(QUrl(QStringLiteral("qrc:/Mixer.qml")));
105
106                 // Find the instantiated model QObject and connect the signals/slots
107                 QList<QObject *> mobjs = engine.rootObjects();
108
109                 QQuickWindow *window = qobject_cast<QQuickWindow *>(mobjs.first());
110                 QObject::connect(window, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateSurface()));
111         }
112         return app.exec();
113 }