Changeset View
Changeset View
Standalone View
Standalone View
src/qml/volumefeedback.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright 2008 Helio Chissini de Castro <helio@kde.org> | ||||
3 | Copyright 2016 David Rosca <nowrep@gmail.com> | ||||
4 | | ||||
5 | This library is free software; you can redistribute it and/or | ||||
6 | modify it under the terms of the GNU Lesser General Public | ||||
7 | License as published by the Free Software Foundation; either | ||||
8 | version 2.1 of the License, or (at your option) version 3, or any | ||||
9 | later version accepted by the membership of KDE e.V. (or its | ||||
10 | successor approved by the membership of KDE e.V.), which shall | ||||
11 | act as a proxy defined in Section 6 of version 3 of the license. | ||||
12 | | ||||
13 | This library is distributed in the hope that it will be useful, | ||||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
16 | Lesser General Public License for more details. | ||||
17 | | ||||
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/>. | ||||
20 | */ | ||||
21 | | ||||
22 | #include "volumefeedback.h" | ||||
23 | | ||||
24 | VolumeFeedback::VolumeFeedback(QObject *parent) | ||||
25 | : QObject(parent) | ||||
26 | { | ||||
27 | if (ca_context_create(&m_context) < 0) { | ||||
28 | m_context = nullptr; | ||||
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; | ||||
35 | } | ||||
36 | } | ||||
37 | | ||||
38 | VolumeFeedback::~VolumeFeedback() | ||||
39 | { | ||||
40 | if (m_context) { | ||||
41 | ca_context_destroy(m_context); | ||||
42 | } | ||||
43 | } | ||||
44 | | ||||
45 | bool VolumeFeedback::isValid() const | ||||
46 | { | ||||
47 | return m_context; | ||||
48 | } | ||||
49 | | ||||
50 | void VolumeFeedback::play(quint32 sinkIndex) | ||||
51 | { | ||||
52 | if (!m_context) { | ||||
53 | return; | ||||
54 | } | ||||
55 | | ||||
56 | int playing = 0; | ||||
57 | 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); | ||||
59 | | ||||
60 | // 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 | ||||
62 | // new one... for now, let's do the latter. | ||||
63 | if (playing) { | ||||
64 | ca_context_cancel(m_context, cindex); | ||||
65 | } | ||||
66 | | ||||
67 | char dev[64]; | ||||
68 | snprintf(dev, sizeof(dev), "%lu", (unsigned long) sinkIndex); | ||||
69 | ca_context_change_device(m_context, dev); | ||||
70 | | ||||
71 | // Ideally we'd use something like ca_gtk_play_for_widget()... | ||||
72 | ca_context_play( | ||||
73 | m_context, | ||||
74 | cindex, | ||||
75 | CA_PROP_EVENT_DESCRIPTION, "Volume Control Feedback Sound", | ||||
76 | CA_PROP_EVENT_ID, "audio-volume-change", | ||||
77 | CA_PROP_CANBERRA_CACHE_CONTROL, "permanent", | ||||
78 | CA_PROP_CANBERRA_ENABLE, "1", | ||||
79 | nullptr | ||||
80 | ); | ||||
81 | | ||||
82 | ca_context_change_device(m_context, nullptr); | ||||
83 | } |