2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2016,2017 Konsulko Group
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "pacontrolmodel.h"
21 #include <QtCore/QDebug>
22 #include <QtCore/QCommandLineParser>
23 #include <QtCore/QUrlQuery>
24 #include <QtCore/QDir>
25 #include <QtCore/QStandardPaths>
26 #include <QtCore/QThread>
27 #include <QtGui/QGuiApplication>
28 #include <QtQml/QQmlApplicationEngine>
29 #include <QtQml/QQmlContext>
30 #include <QtQuickControls2/QQuickStyle>
31 #include <QtQml/qqml.h>
32 #include <QtQuick/qquickitem.h>
33 #include <QtQuick/qquickview.h>
34 #include <QQuickWindow>
35 #include <libhomescreen.hpp>
36 #include <qlibwindowmanager.h>
38 int main(int argc, char *argv[])
40 QString myname = QString("Mixer");
42 QGuiApplication app(argc, argv);
44 QQuickStyle::setStyle("AGL");
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();
52 QStringList positionalArguments = parser.positionalArguments();
54 // Fire up PA client QThread
55 QThread* pat = new QThread;
56 PaClient* client = new PaClient();
57 client->moveToThread(pat);
60 // Register the PA Control Model
61 qmlRegisterType<PaControlModel>("PaControlModel", 1, 0, "PaControlModel");
63 QQmlApplicationEngine engine;
64 if (positionalArguments.length() == 2) {
65 int port = positionalArguments.takeFirst().toInt();
66 QString secret = positionalArguments.takeFirst();
68 bindingAddress.setScheme(QStringLiteral("ws"));
69 bindingAddress.setHost(QStringLiteral("localhost"));
70 bindingAddress.setPort(port);
71 bindingAddress.setPath(QStringLiteral("/api"));
73 query.addQueryItem(QStringLiteral("token"), secret);
74 bindingAddress.setQuery(query);
75 QQmlContext *context = engine.rootContext();
76 context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
78 std::string token = secret.toStdString();
79 LibHomeScreen* hs = new LibHomeScreen();
80 QLibWindowmanager* qwm = new QLibWindowmanager();
83 if(qwm->init(port,secret) != 0){
86 // Request a surface as described in layers.json windowmanager’s file
87 if (qwm->requestSurface(myname) != 0) {
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 *object) {
92 fprintf(stderr, "Surface got syncDraw!\n");
97 hs->init(port, token.c_str());
98 // Set the event handler for Event_TapShortcut which will activate the surface for windowmanager
99 hs->set_event_handler(LibHomeScreen::Event_TapShortcut, [qwm, myname](json_object *object){
100 json_object *appnameJ = nullptr;
101 if(json_object_object_get_ex(object, "application_name", &appnameJ))
103 const char *appname = json_object_get_string(appnameJ);
104 if(myname == appname)
106 qDebug("Surface %s got tapShortcut\n", appname);
107 qwm->activateSurface(myname);
112 engine.load(QUrl(QStringLiteral("qrc:/Mixer.qml")));
114 // Find the instantiated model QObject and connect the signals/slots
115 QList<QObject *> mobjs = engine.rootObjects();
117 QQuickWindow *window = qobject_cast<QQuickWindow *>(mobjs.first());
118 QObject::connect(window, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateSurface()));
120 PaControlModel *pacm = mobjs.first()->findChild<PaControlModel *>("pacm");
121 QObject::connect(client, SIGNAL(controlAdded(int, QString, int, int, const char *, int)),
122 pacm, SLOT(addOneControl(int, QString, int, int, const char *, int)));
123 QObject::connect(client, SIGNAL(volumeExternallyChanged(uint32_t, uint32_t, uint32_t, uint32_t)),
124 pacm, SLOT(changeExternalVolume(uint32_t, uint32_t, uint32_t, uint32_t)));
125 QObject::connect(pacm, SIGNAL(volumeChanged(uint32_t, uint32_t, uint32_t, uint32_t)),
126 client, SLOT(setVolume(uint32_t, uint32_t, uint32_t, uint32_t)));
128 // Initalize PA client