aboutsummaryrefslogtreecommitdiffstats
path: root/homescreen/src/mastervolume.cpp
blob: a4f8d286e0b41dbc39efdc770c5561a2d5012edd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2017 Konsulko Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mastervolume.h"
#include "qafbwsclient.h"
#include "qafbwsmsg.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonArray>
#include <QtDebug>

/*!
 * @brief Default constructor.
 * @param parent Parent's object.
 */
MasterVolume::MasterVolume(QObject* parent)
    : QObject{parent}
    , m_volume(0)
{
    connect(&m_client, SIGNAL(connected()), this, SLOT(onAfbClientConnected()));
    connect(&m_client, SIGNAL(disconnected()), this, SLOT(onAfbClientDisconnected()));
}

/*!
 * @brief Destructor.
 */
MasterVolume::~MasterVolume()
{
}

/*!
 * @brief Initialize this instance.
 * @param port AFB Port.
 * @param token AFB Token.
 */
void MasterVolume::init(quint16 port, QString token)
{
    qDebug() << "MasterVolume::init(" << port << "," << token << ")";
    m_client.open(QUrl(QString("ws://localhost:") + QString::number(port) + QString("/api?token=") + token));
}

/*!
 * \brief Get the current volume.
 * @return The volume.
 */
uint32_t MasterVolume::getVolume() const
{
    return m_volume;
}

/*!
 * @brief Set volume.
 * @param volume New desired value.
 */
void MasterVolume::setVolume(uint32_t volume)
{
    qDebug() << "MasterVolume::setVolume(" << volume << ")";
    if (volume == m_volumePending) return; // Nothing to do
    m_volumePending = volume;

    QJsonObject value;
    value["label"] = "Master_Playback_Volume";
    value["val"] = static_cast<int>(volume);

    // FIXME: card should be obtained!
    if (!m_soundcard.size()) m_soundcard = "rsnddai0ak4613h";

    m_call = m_client.call(m_soundcard, "ctlset", value);
    connect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallCtlSet()));
}

void MasterVolume::onAfbClientConnected()
{
    qDebug() << "MasterVolume::onAfbClientConnected()";
    m_call = m_client.call("alsacore", "hallist");
    connect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallHalListClosed()));
}

void MasterVolume::onAfbClientDisconnected()
{
    qDebug() << "MasterVolume::onAfbClientDisconnected()";
}

void MasterVolume::onCallHalListClosed()
{
    qDebug() << "MasterVolume::onCallHalListClosed()";
    disconnect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallHalListClosed()));
    if (m_call->messageType() == AfMsgType::RetOk && m_call->value().isObject())
    {
        m_soundcard = m_call->value().toObject()["response"].toArray()[0].toObject()["api"].toString();
        qDebug() << "MasterVolume::onCallHalListClosed() - Found soundcard api:" << m_soundcard;
    }
    //m_call.clear();

    // FIXME: card should be obtained!
    if (!m_soundcard.size()) m_soundcard = "rsnddai0ak4613h";

    // Get volume
    if (m_soundcard.size())
    {
        QJsonObject arg;
        arg["label"] = "Master_Playback_Volume";
        m_call = m_client.call(m_soundcard, "ctlget", arg);
        connect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallCtlGet()));
    }
}

void MasterVolume::onCallCtlGet()
{
    qDebug() << "MasterVolume::onCallCtlGet()";
    disconnect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallCtlGet()));
    if (m_call->messageType() == AfMsgType::RetOk && m_call->value().isObject())
    {
        setVolume(m_call->value().toObject()["response"].toObject()["val"].toArray()[0].toInt());
    }
    //m_call.clear();
}

void MasterVolume::onCallCtlSet()
{
    qDebug() << "MasterVolume::onCallCtlSet()";
    if (m_call.data())
    {
        disconnect(m_call.data(), SIGNAL(closed()), this, SLOT(onCallCtlSet()));
        if (m_call->messageType() == AfMsgType::RetOk && m_call->value().isObject())
        {
            m_volume = m_volumePending;
            emit volumeChanged(m_volume);
        }
        //m_call.clear();
    }
}