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 | } | | |||
31 | if (ca_context_set_driver(m_context, "pulse") < 0) { | | |||
32 | ca_context_destroy(m_context); | | |||
33 | m_context = nullptr; | | |||
34 | return; | 30 | return; | ||
drosca: If this happens, you will unref again in destructor. | |||||
35 | } | 31 | } | ||
36 | } | 32 | } | ||
37 | 33 | | |||
38 | VolumeFeedback::~VolumeFeedback() | 34 | VolumeFeedback::~VolumeFeedback() | ||
39 | { | 35 | { | ||
40 | if (m_context) { | 36 | 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 | } | 37 | } | ||
44 | 38 | | |||
45 | bool VolumeFeedback::isValid() const | 39 | bool VolumeFeedback::isValid() const | ||
46 | { | 40 | { | ||
47 | return m_context; | 41 | return QPulseAudio::CanberraContext::instance()->canberra(); | ||
48 | } | 42 | } | ||
49 | 43 | | |||
50 | void VolumeFeedback::play(quint32 sinkIndex) | 44 | void VolumeFeedback::play(quint32 sinkIndex) | ||
51 | { | 45 | { | ||
52 | if (!m_context) { | 46 | auto context = QPulseAudio::CanberraContext::instance()->canberra(); | ||
47 | | ||||
48 | if (!context) { | ||||
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; | 49 | return; | ||
54 | } | 50 | } | ||
55 | 51 | | |||
56 | int playing = 0; | 52 | int playing = 0; | ||
57 | const int cindex = 2; // Note "2" is simply the index we've picked. It's somewhat irrelevant. | 53 | 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); | 54 | ca_context_playing(context, cindex, &playing); | ||
59 | 55 | | |||
60 | // NB Depending on how this is desired to work, we may want to simply | 56 | // 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 | 57 | // skip playing, or cancel the currently playing sound and play our | ||
62 | // new one... for now, let's do the latter. | 58 | // new one... for now, let's do the latter. | ||
63 | if (playing) { | 59 | if (playing) { | ||
64 | ca_context_cancel(m_context, cindex); | 60 | ca_context_cancel(context, cindex); | ||
65 | } | 61 | } | ||
66 | 62 | | |||
67 | char dev[64]; | 63 | char dev[64]; | ||
68 | snprintf(dev, sizeof(dev), "%lu", (unsigned long) sinkIndex); | 64 | snprintf(dev, sizeof(dev), "%lu", (unsigned long) sinkIndex); | ||
69 | ca_context_change_device(m_context, dev); | 65 | ca_context_change_device(context, dev); | ||
70 | 66 | | |||
71 | // Ideally we'd use something like ca_gtk_play_for_widget()... | 67 | // Ideally we'd use something like ca_gtk_play_for_widget()... | ||
72 | ca_context_play( | 68 | ca_context_play( | ||
73 | m_context, | 69 | context, | ||
74 | cindex, | 70 | cindex, | ||
75 | CA_PROP_EVENT_DESCRIPTION, "Volume Control Feedback Sound", | 71 | CA_PROP_EVENT_DESCRIPTION, "Volume Control Feedback Sound", | ||
76 | CA_PROP_EVENT_ID, "audio-volume-change", | 72 | CA_PROP_EVENT_ID, "audio-volume-change", | ||
77 | CA_PROP_CANBERRA_CACHE_CONTROL, "permanent", | 73 | CA_PROP_CANBERRA_CACHE_CONTROL, "permanent", | ||
78 | CA_PROP_CANBERRA_ENABLE, "1", | 74 | CA_PROP_CANBERRA_ENABLE, "1", | ||
79 | nullptr | 75 | nullptr | ||
80 | ); | 76 | ); | ||
81 | 77 | | |||
82 | ca_context_change_device(m_context, nullptr); | 78 | ca_context_change_device(context, nullptr); | ||
83 | } | 79 | } |
If this happens, you will unref again in destructor.