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