diff options
Diffstat (limited to 'homescreen/src/mastervolume.cpp')
-rw-r--r-- | homescreen/src/mastervolume.cpp | 127 |
1 files changed, 121 insertions, 6 deletions
diff --git a/homescreen/src/mastervolume.cpp b/homescreen/src/mastervolume.cpp index 5ef78c5..a4f8d28 100644 --- a/homescreen/src/mastervolume.cpp +++ b/homescreen/src/mastervolume.cpp @@ -15,17 +15,132 @@ */ #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) { - int volume_delta = volume - m_volume; - m_volume = volume; - emit sliderVolumeChanged(volume_delta); + 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::changeExternalVolume(int volume) +void MasterVolume::onCallCtlSet() { - m_volume = volume; - emit volumeChanged(); + 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(); + } } |