ac5fb24071005694907f6aeb4dba0351e41fb2bc
[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 #ifndef NATIVE_BUILD
34 #include <libhomescreen.hpp>
35 #include <qlibwindowmanager.h>
36 #else
37 #include <QScreen>
38 #endif
39 #include "mixer.h"
40
41 int main(int argc, char *argv[])
42 {
43         QString myname = QString("Mixer");
44
45         QGuiApplication app(argc, argv);
46
47         QQuickStyle::setStyle("AGL");
48
49         QCommandLineParser parser;
50         parser.addPositionalArgument("port", app.translate("main", "port for binding"));
51         parser.addPositionalArgument("secret", app.translate("main", "secret for binding"));
52         parser.addHelpOption();
53         parser.addVersionOption();
54         parser.process(app);
55         QStringList positionalArguments = parser.positionalArguments();
56
57         qmlRegisterType<Mixer>("Mixer", 1, 0, "Mixer");
58
59         QQmlApplicationEngine engine;
60         if (positionalArguments.length() == 2) {
61                 int port = positionalArguments.takeFirst().toInt();
62                 QString secret = positionalArguments.takeFirst();
63                 QUrl bindingAddress;
64                 bindingAddress.setScheme(QStringLiteral("ws"));
65                 bindingAddress.setHost(QStringLiteral("localhost"));
66                 bindingAddress.setPort(port);
67                 bindingAddress.setPath(QStringLiteral("/api"));
68                 QUrlQuery query;
69                 query.addQueryItem(QStringLiteral("token"), secret);
70                 bindingAddress.setQuery(query);
71                 QQmlContext *context = engine.rootContext();
72                 context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
73                 qDebug() << "Connect to: " << bindingAddress;
74
75
76 #ifndef NATIVE_BUILD
77                 LibHomeScreen* hs = new LibHomeScreen();
78                 QLibWindowmanager* qwm = new QLibWindowmanager();
79
80                 // WindowManager
81                 if(qwm->init(port,secret) != 0){
82                         exit(EXIT_FAILURE);
83                 }
84                 AGLScreenInfo screenInfo(qwm->get_scale_factor());
85                 engine.rootContext()->setContextProperty(QStringLiteral("screenInfo"), &screenInfo);
86                 // Request a surface as described in layers.json windowmanager’s file
87                 if (qwm->requestSurface(myname) != 0) {
88                         exit(EXIT_FAILURE);
89                 }
90                 // Create an event callbnewack against an event type. Here a lambda is called when SyncDraw event occurs
91             qwm->set_event_handler(QLibWindowmanager::Event_SyncDraw, [qwm, myname](json_object*) {
92                                 fprintf(stderr, "Surface got syncDraw!\n");
93                                 qwm->endDraw(myname);
94                         });
95
96                 // HomeScreen
97                 std::string token = secret.toStdString();
98                 hs->init(port, token.c_str());
99                 // Set the event handler for Event_TapShortcut which will activate the surface for windowmanager
100                 hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [qwm, myname](json_object *object){
101                         json_object *appnameJ = nullptr;
102                         if(json_object_object_get_ex(object, "application_name", &appnameJ))
103                         {
104                                 const char *appname = json_object_get_string(appnameJ);
105                                 if(QString::compare(myname, appname, Qt::CaseInsensitive) == 0)
106                                 {
107                                         qDebug("Surface %s got tapShortcut\n", appname);
108                                         qwm->activateSurface(myname);
109                                 }
110                         }
111                 });
112 #endif
113                 engine.load(QUrl(QStringLiteral("qrc:/Mixer.qml")));
114
115                 // Find the instantiated model QObject and connect the signals/slots
116                 QList<QObject*> mobjs = engine.rootObjects();
117                 if (mobjs.empty())
118                 {
119                     qDebug() << "[ERROR] Failed to load QML!";
120                     return -1;
121                 }
122
123                 QQuickWindow *window = qobject_cast<QQuickWindow *>(mobjs.first());
124 #ifdef NATIVE_BUILD
125                 window->setFlags(window->flags() & ~Qt::FramelessWindowHint); // Remove the borderless flag
126                 window->setHeight(QGuiApplication::primaryScreen()->geometry().height());
127 #else
128                 QObject::connect(window, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateSurface()));
129 #endif
130         }
131         else
132         {
133             qDebug() << "[ERROR] No port and token specified!";
134             return -1;
135         }
136
137         return app.exec();
138 }