File indexing completed on 2024-07-14 15:48:30

0001 /*
0002     SPDX-FileCopyrightText: 2008 Helio Chissini de Castro <helio@kde.org>
0003     SPDX-FileCopyrightText: 2016 David Rosca <nowrep@gmail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "volumefeedback.h"
0009 #include "canberracontext.h"
0010 
0011 VolumeFeedback::VolumeFeedback(QObject *parent)
0012     : QObject(parent)
0013 {
0014     QPulseAudio::CanberraContext::instance()->ref();
0015     if (ca_context_set_driver(QPulseAudio::CanberraContext::instance()->canberra(), "pulse") != CA_SUCCESS) {
0016         return;
0017     }
0018 }
0019 
0020 VolumeFeedback::~VolumeFeedback()
0021 {
0022     QPulseAudio::CanberraContext::instance()->unref();
0023 }
0024 
0025 bool VolumeFeedback::isValid() const
0026 {
0027     return QPulseAudio::CanberraContext::instance()->canberra();
0028 }
0029 
0030 void VolumeFeedback::play(quint32 sinkIndex)
0031 {
0032     auto context = QPulseAudio::CanberraContext::instance()->canberra();
0033 
0034     if (!context) {
0035         return;
0036     }
0037 
0038     int playing = 0;
0039     const int cindex = 2; // Note "2" is simply the index we've picked. It's somewhat irrelevant.
0040     ca_context_playing(context, cindex, &playing);
0041 
0042     // NB Depending on how this is desired to work, we may want to simply
0043     // skip playing, or cancel the currently playing sound and play our
0044     // new one... for now, let's do the latter.
0045     if (playing) {
0046         ca_context_cancel(context, cindex);
0047     }
0048 
0049     char dev[64];
0050     snprintf(dev, sizeof(dev), "%lu", (unsigned long)sinkIndex);
0051     ca_context_change_device(context, dev);
0052 
0053     // Ideally we'd use something like ca_gtk_play_for_widget()...
0054     ca_context_play(context,
0055                     cindex,
0056                     CA_PROP_EVENT_DESCRIPTION,
0057                     "Volume Control Feedback Sound",
0058                     CA_PROP_EVENT_ID,
0059                     "audio-volume-change",
0060                     CA_PROP_CANBERRA_CACHE_CONTROL,
0061                     "permanent",
0062                     CA_PROP_CANBERRA_ENABLE,
0063                     "1",
0064                     nullptr);
0065 
0066     ca_context_change_device(context, nullptr);
0067 }