File indexing completed on 2024-04-21 16:20:32

0001 /*
0002     SPDX-FileCopyrightText: 2014-2015 Harald Sitter <sitter@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef VOLUMEOBJECT_H
0008 #define VOLUMEOBJECT_H
0009 
0010 #include <pulse/volume.h>
0011 
0012 #include "pulseobject.h"
0013 
0014 namespace QPulseAudio
0015 {
0016 class VolumeObject : public PulseObject
0017 {
0018     Q_OBJECT
0019     Q_PROPERTY(qint64 volume READ volume WRITE setVolume NOTIFY volumeChanged)
0020     Q_PROPERTY(bool muted READ isMuted WRITE setMuted NOTIFY mutedChanged)
0021     Q_PROPERTY(bool hasVolume READ hasVolume NOTIFY hasVolumeChanged)
0022     Q_PROPERTY(bool volumeWritable READ isVolumeWritable NOTIFY isVolumeWritableChanged)
0023     Q_PROPERTY(QStringList channels READ channels NOTIFY channelsChanged)
0024     Q_PROPERTY(QStringList rawChannels READ rawChannels NOTIFY rawChannelsChanged)
0025     Q_PROPERTY(QVector<qint64> channelVolumes READ channelVolumes WRITE setChannelVolumes NOTIFY channelVolumesChanged)
0026 public:
0027     explicit VolumeObject(QObject *parent);
0028     ~VolumeObject() override;
0029 
0030     template<typename PAInfo>
0031     void updateVolumeObject(PAInfo *info)
0032     {
0033         updatePulseObject(info);
0034         if (m_muted != info->mute) {
0035             m_muted = info->mute;
0036             Q_EMIT mutedChanged();
0037         }
0038         if (!pa_cvolume_equal(&m_volume, &info->volume)) {
0039             m_volume = info->volume;
0040             Q_EMIT volumeChanged();
0041             Q_EMIT channelVolumesChanged();
0042         }
0043         QStringList infoChannels;
0044         infoChannels.reserve(info->channel_map.channels);
0045         for (int i = 0; i < info->channel_map.channels; ++i) {
0046             infoChannels << QString::fromUtf8(pa_channel_position_to_pretty_string(info->channel_map.map[i]));
0047         }
0048         if (m_channels != infoChannels) {
0049             m_channels = infoChannels;
0050             Q_EMIT channelsChanged();
0051         }
0052 
0053         QStringList infoRawChannels;
0054         infoRawChannels.reserve(info->channel_map.channels);
0055         for (int i = 0; i < info->channel_map.channels; ++i) {
0056             infoRawChannels << QString::fromUtf8(pa_channel_position_to_string(info->channel_map.map[i]));
0057         }
0058         if (m_rawChannels != infoRawChannels) {
0059             m_rawChannels = infoRawChannels;
0060             Q_EMIT rawChannelsChanged();
0061         }
0062     }
0063 
0064     qint64 volume() const;
0065     virtual void setVolume(qint64 volume) = 0;
0066 
0067     bool isMuted() const;
0068     virtual void setMuted(bool muted) = 0;
0069 
0070     bool hasVolume() const;
0071     bool isVolumeWritable() const;
0072 
0073     QStringList channels() const;
0074     QStringList rawChannels() const;
0075 
0076     QVector<qint64> channelVolumes() const;
0077     virtual void setChannelVolumes(const QVector<qint64> &channelVolumes) = 0;
0078     Q_INVOKABLE virtual void setChannelVolume(int channel, qint64 volume) = 0;
0079 
0080 Q_SIGNALS:
0081     void volumeChanged();
0082     void mutedChanged();
0083     void hasVolumeChanged();
0084     void isVolumeWritableChanged();
0085     void channelsChanged();
0086     void rawChannelsChanged();
0087     void channelVolumesChanged();
0088 
0089 protected:
0090     pa_cvolume cvolume() const;
0091 
0092     pa_cvolume m_volume;
0093     bool m_muted = true;
0094     bool m_hasVolume = true;
0095     bool m_volumeWritable = true;
0096     QStringList m_channels;
0097     QStringList m_rawChannels;
0098 };
0099 
0100 } // QPulseAudio
0101 
0102 #endif // VOLUMEOBJECT_H