Fix master volume slider
[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 <QJsonObject>
19 #include <QTimer>
20 #include <QtDebug>
21
22 MasterVolume::MasterVolume(QObject* parent)
23         : QObject(parent)
24         , m_volume{50}
25 {
26         connect(&m_client, SIGNAL(connected()), this, SLOT(onClientConnected()));
27         connect(&m_client, SIGNAL(disconnected()), this, SLOT(onClientDisconnected()));
28         connect(&m_client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onClientError(QAbstractSocket::SocketError)));
29         connect(&m_client, SIGNAL(eventReceived(QString, const QJsonValue&)), this, SLOT(onClientEventReceived(QString, const QJsonValue&)));
30 }
31
32 void MasterVolume::open(const QUrl& url)
33 {
34         m_url = url;
35         TryOpen();
36 }
37
38 qint32 MasterVolume::getVolume() const
39 {
40         return m_volume;
41 }
42
43 void MasterVolume::setVolume(qint32 volume)
44 {
45         if (m_volume != volume)
46         {
47                 m_volume = volume;
48                 QJsonObject arg;
49                 arg.insert("control", "Master");
50                 double v = (double) volume / 100.0;
51                 arg.insert("value", v);
52                 m_client.call("audiomixer", "volume", arg);
53         }
54 }
55
56 void MasterVolume::onClientConnected()
57 {
58         QJsonObject arg;
59         arg.insert("control", "Master");
60         m_client.call("audiomixer", "volume", arg, [this](bool r, const QJsonValue& v) {
61                 if (r && v.isObject()) {
62                         int volume = v.toObject()["response"].toObject()["volume"].toDouble() * 100;
63                         volume = qBound(0, volume, 100);
64                         if (m_volume != volume)
65                         {
66                                 m_volume = volume;
67                                 emit VolumeChanged();
68                         }
69                 }
70
71                 QJsonObject arg;
72                 arg.insert("event", "volume_changed");
73                 m_client.call("audiomixer", "subscribe", arg);
74         });
75 }
76
77 void MasterVolume::onClientDisconnected()
78 {
79         qDebug() << "MasterVolume::onClientDisconnected!";
80         QTimer::singleShot(1000, this, SLOT(TryOpen()));
81 }
82
83 void MasterVolume::onClientError(QAbstractSocket::SocketError se)
84 {
85         qDebug() << "MasterVolume::onClientError: " << se;
86 }
87
88 void MasterVolume::onClientEventReceived(QString name, const QJsonValue& data)
89 {
90         qDebug() << "MasterVolume::onClientEventReceived[" << name << "]: " << data;
91         if (name == "audiomixer/volume_changed")
92         {
93                 QString ctlName = data.toObject()["control"].toString();
94
95                 if (ctlName != "Master")
96                         return;
97
98                 int volume = data.toObject()["value"].toDouble() * 100;
99                 volume = qBound(0, volume, 100);
100                 if (m_volume != volume)
101                 {
102                         m_volume = volume;
103                         emit VolumeChanged();
104                 }
105         }
106 }
107
108 void MasterVolume::TryOpen()
109 {
110         m_client.open(m_url);
111 }