Changeset View
Changeset View
Standalone View
Standalone View
src/qml/volumefeedback.cpp
Show All 14 Lines | 1 | /* | |||
---|---|---|---|---|---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | Lesser General Public License for more details. | 16 | Lesser General Public License for more details. | ||
17 | 17 | | |||
18 | You should have received a copy of the GNU Lesser General Public | 18 | You should have received a copy of the GNU Lesser General Public | ||
19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | 19 | License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||
20 | */ | 20 | */ | ||
21 | 21 | | |||
22 | #include "volumefeedback.h" | 22 | #include "volumefeedback.h" | ||
23 | #include "canberracontext.h" | ||||
23 | 24 | | |||
24 | VolumeFeedback::VolumeFeedback(QObject *parent) | 25 | VolumeFeedback::VolumeFeedback(QObject *parent) | ||
25 | : QObject(parent) | 26 | : QObject(parent) | ||
26 | { | 27 | { | ||
27 | if (ca_context_create(&m_context) < 0) { | 28 | QPulseAudio::CanberraContext::instance()->ref(); | ||
28 | m_context = nullptr; | 29 | if (ca_context_set_driver(QPulseAudio::CanberraContext::instance()->canberra(), "pulse") < 0) { | ||
29 | return; | 30 | QPulseAudio::CanberraContext::instance()->unref(); | ||
drosca: If this happens, you will unref again in destructor. | |||||
30 | } | | |||
31 | if (ca_context_set_driver(m_context, "pulse") < 0) { | | |||
32 | ca_context_destroy(m_context); | | |||
33 | m_context = nullptr; | | |||
34 | return; | 31 | return; | ||
35 | } | 32 | } | ||
36 | } | 33 | } | ||
37 | 34 | | |||
38 | VolumeFeedback::~VolumeFeedback() | 35 | VolumeFeedback::~VolumeFeedback() | ||
39 | { | 36 | { | ||
40 | if (m_context) { | 37 | QPulseAudio::CanberraContext::instance()->unref(); | ||
What if there is multiple VolumeFeedback instances? It will crash then. drosca: What if there is multiple VolumeFeedback instances? It will crash then.
You should either move… | |||||
I'd rather not move it into context so that pulseaudio-qt does not need Canberra. This way only plasma-pa would depend on Canberra nicolasfella: I'd rather not move it into context so that pulseaudio-qt does not need Canberra. This way… | |||||
41 | ca_context_destroy(m_context); | | |||
42 | } | | |||
43 | } | 38 | } | ||
44 | 39 | | |||
45 | bool VolumeFeedback::isValid() const | 40 | bool VolumeFeedback::isValid() const | ||
46 | { | 41 | { | ||
47 | return m_context; | 42 | return QPulseAudio::CanberraContext::instance()->canberra(); | ||
48 | } | 43 | } | ||
49 | 44 | | |||
50 | void VolumeFeedback::play(quint32 sinkIndex) | 45 | void VolumeFeedback::play(quint32 sinkIndex) | ||
51 | { | 46 | { | ||
52 | if (!m_context) { | 47 | if (!QPulseAudio::CanberraContext::instance()->canberra()) { | ||
Just save it into variable instead of copy pasting it. auto context = QPulseAudio::CanberraContext::instance()->canberra(); drosca: Just save it into variable instead of copy pasting it.
`auto context = QPulseAudio… | |||||
53 | return; | 48 | return; | ||
54 | } | 49 | } | ||
55 | 50 | | |||
56 | int playing = 0; | 51 | int playing = 0; | ||
57 | const int cindex = 2; // Note "2" is simply the index we've picked. It's somewhat irrelevant. | 52 | const int cindex = 2; // Note "2" is simply the index we've picked. It's somewhat irrelevant. | ||
58 | ca_context_playing(m_context, cindex, &playing); | 53 | ca_context_playing(QPulseAudio::CanberraContext::instance()->canberra(), cindex, &playing); | ||
59 | 54 | | |||
60 | // NB Depending on how this is desired to work, we may want to simply | 55 | // NB Depending on how this is desired to work, we may want to simply | ||
61 | // skip playing, or cancel the currently playing sound and play our | 56 | // skip playing, or cancel the currently playing sound and play our | ||
62 | // new one... for now, let's do the latter. | 57 | // new one... for now, let's do the latter. | ||
63 | if (playing) { | 58 | if (playing) { | ||
64 | ca_context_cancel(m_context, cindex); | 59 | ca_context_cancel(QPulseAudio::CanberraContext::instance()->canberra(), cindex); | ||
65 | } | 60 | } | ||
66 | 61 | | |||
67 | char dev[64]; | 62 | char dev[64]; | ||
68 | snprintf(dev, sizeof(dev), "%lu", (unsigned long) sinkIndex); | 63 | snprintf(dev, sizeof(dev), "%lu", (unsigned long) sinkIndex); | ||
69 | ca_context_change_device(m_context, dev); | 64 | ca_context_change_device(QPulseAudio::CanberraContext::instance()->canberra(), dev); | ||
70 | 65 | | |||
71 | // Ideally we'd use something like ca_gtk_play_for_widget()... | 66 | // Ideally we'd use something like ca_gtk_play_for_widget()... | ||
72 | ca_context_play( | 67 | ca_context_play( | ||
73 | m_context, | 68 | QPulseAudio::CanberraContext::instance()->canberra(), | ||
74 | cindex, | 69 | cindex, | ||
75 | CA_PROP_EVENT_DESCRIPTION, "Volume Control Feedback Sound", | 70 | CA_PROP_EVENT_DESCRIPTION, "Volume Control Feedback Sound", | ||
76 | CA_PROP_EVENT_ID, "audio-volume-change", | 71 | CA_PROP_EVENT_ID, "audio-volume-change", | ||
77 | CA_PROP_CANBERRA_CACHE_CONTROL, "permanent", | 72 | CA_PROP_CANBERRA_CACHE_CONTROL, "permanent", | ||
78 | CA_PROP_CANBERRA_ENABLE, "1", | 73 | CA_PROP_CANBERRA_ENABLE, "1", | ||
79 | nullptr | 74 | nullptr | ||
80 | ); | 75 | ); | ||
81 | 76 | | |||
82 | ca_context_change_device(m_context, nullptr); | 77 | ca_context_change_device(QPulseAudio::CanberraContext::instance()->canberra(), nullptr); | ||
83 | } | 78 | } |
If this happens, you will unref again in destructor.