mixer: add clearer pulseaudio control naming
[apps/mixer.git] / app / main.cpp
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2016,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 "paclient.h"
19 #include "pacontrolmodel.h"
20
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>
37
38 int main(int argc, char *argv[])
39 {
40         QString myname = QString("Mixer");
41
42         QGuiApplication app(argc, argv);
43
44         QQuickStyle::setStyle("AGL");
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         // Fire up PA client QThread
55         QThread* pat = new QThread;
56         PaClient* client = new PaClient();
57         client->moveToThread(pat);
58         pat->start();
59
60         // Register the PA Control Model
61         qmlRegisterType<PaControlModel>("PaControlModel", 1, 0, "PaControlModel");
62
63         QQmlApplicationEngine engine;
64         if (positionalArguments.length() == 2) {
65                 int port = positionalArguments.takeFirst().toInt();
66                 QString secret = positionalArguments.takeFirst();
67                 QUrl bindingAddress;
68                 bindingAddress.setScheme(QStringLiteral("ws"));
69                 bindingAddress.setHost(QStringLiteral("localhost"));
70                 bindingAddress.setPort(port);
71                 bindingAddress.setPath(QStringLiteral("/api"));
72                 QUrlQuery query;
73                 query.addQueryItem(QStringLiteral("token"), secret);
74                 bindingAddress.setQuery(query);
75                 QQmlContext *context = engine.rootContext();
76                 context->setContextProperty(QStringLiteral("bindingAddress"), bindingAddress);
77
78                 std::string token = secret.toStdString();
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                 // 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 *object) {
92                                 fprintf(stderr, "Surface got syncDraw!\n");
93                                 qwm->endDraw(myname);
94                         });
95
96                 // HomeScreen
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))
102                         {
103                                 const char *appname = json_object_get_string(appnameJ);
104                                 if(myname == appname)
105                                 {
106                                         qDebug("Surface %s got tapShortcut\n", appname);
107                                         qwm->activateSurface(myname);
108                                 }
109                         }
110                 });
111
112                 engine.load(QUrl(QStringLiteral("qrc:/Mixer.qml")));
113
114                 // Find the instantiated model QObject and connect the signals/slots
115                 QList<QObject *> mobjs = engine.rootObjects();
116
117                 QQuickWindow *window = qobject_cast<QQuickWindow *>(mobjs.first());
118                 QObject::connect(window, SIGNAL(frameSwapped()), qwm, SLOT(slotActivateSurface()));
119
120                 PaControlModel *pacm = mobjs.first()->findChild<PaControlModel *>("pacm");
121                 QObject::connect(client, SIGNAL(controlAdded(int, QString, QString, int, int, const char *, int)),
122                                  pacm, SLOT(addOneControl(int, QString, 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)));
127
128                 // Initalize PA client
129                 client->init();
130         }
131         return app.exec();
132 }