Initial rework to run without the app framework
[apps/homescreen.git] / homescreen / src / mastervolume.cpp
1 /*
2  * Copyright (C) 2017 Konsulko Group
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "mastervolume.h"
18 #include <QTimer>
19 #include <QtDebug>
20
21 #define MASTER_CONTROL "Master Playback"
22
23 MasterVolume::MasterVolume(QObject* parent)
24         : QObject(parent)
25         , m_volume{50}
26 {
27 #if 0
28         connect(&m_client, SIGNAL(connected()), this, SLOT(onClientConnected()));
29         connect(&m_client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
30         connect(&m_client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onClientError(QAbstractSocket::SocketError)));
31         connect(&m_client, SIGNAL(eventReceived(QString, const QJsonValue&)), this, SLOT(onClientEventReceived(QString, const QJsonValue&)));
32 #endif
33 }
34
35 #if 0
36 void MasterVolume::open(const QUrl& url)
37 {
38 }
39 #endif
40
41 qint32 MasterVolume::getVolume() const
42 {
43         return m_volume;
44 }
45
46 void MasterVolume::setVolume(qint32 volume)
47 {
48         if (m_volume != volume)
49         {
50                 m_volume = volume;
51 #if 0
52                 QJsonObject arg;
53                 arg.insert("control", MASTER_CONTROL);
54                 double v = (double) volume / 100.0;
55                 arg.insert("value", v);
56                 m_client.call("audiomixer", "volume", arg);
57 #endif
58         }
59 }
60
61 #if 0
62
63 void MasterVolume::onClientConnected()
64 {
65
66         QJsonObject arg;
67         arg.insert("control", MASTER_CONTROL);
68         m_client.call("audiomixer", "volume", arg, [this](bool r, const QJsonValue& v) {
69                 if (r && v.isObject()) {
70                         int volume = v.toObject()["response"].toObject()["volume"].toDouble() * 100;
71                         volume = qBound(0, volume, 100);
72                         if (m_volume != volume)
73                         {
74                                 m_volume = volume;
75                                 emit VolumeChanged();
76                         }
77                 }
78
79                 QJsonObject arg;
80                 arg.insert("event", "volume_changed");
81                 m_client.call("audiomixer", "subscribe", arg);
82         });
83 }
84
85 void MasterVolume::onClientDisconnected()
86 {
87         qDebug() << "MasterVolume::onClientDisconnected!";
88         QTimer::singleShot(1000, this, SLOT(TryOpen()));
89 }
90
91 void MasterVolume::onClientError(QAbstractSocket::SocketError se)
92 {
93         qDebug() << "MasterVolume::onClientError: " << se;
94 }
95
96 void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data)
97 {
98         qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data;
99         if (name == "audiomixer/volume_changed")
100         {
101                 QString ctlName = data.toObject()["control"].toString();
102
103                 if (ctlName != MASTER_CONTROL)
104                         return;
105
106                 int volume = data.toObject()["value"].toDouble() * 100;
107                 volume = qBound(0, volume, 100);
108                 if (m_volume != volume)
109                 {
110                         m_volume = volume;
111                         emit VolumeChanged();
112                 }
113         }
114 }
115
116 #endif