1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/*
* Copyright (C) 2014 Michal Ratajsky <michal.ratajsky@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the licence, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <glib-object.h>
#include <alsa/asoundlib.h>
#include <libmatemixer/matemixer.h>
#include <libmatemixer/matemixer-private.h>
#include "alsa-compat.h"
#include "alsa-constants.h"
#include "alsa-element.h"
#include "alsa-stream.h"
#include "alsa-stream-control.h"
#include "alsa-stream-input-control.h"
static void alsa_stream_input_control_class_init (AlsaStreamInputControlClass *klass);
static void alsa_stream_input_control_init (AlsaStreamInputControl *control);
G_DEFINE_TYPE (AlsaStreamInputControl, alsa_stream_input_control, ALSA_TYPE_STREAM_CONTROL)
static gboolean alsa_stream_input_control_load (AlsaStreamControl *control);
static gboolean alsa_stream_input_control_set_mute (AlsaStreamControl *control,
gboolean mute);
static gboolean alsa_stream_input_control_set_volume (AlsaStreamControl *control,
guint volume);
static gboolean alsa_stream_input_control_set_channel_volume (AlsaStreamControl *control,
snd_mixer_selem_channel_id_t channel,
guint volume);
static gboolean alsa_stream_input_control_get_volume_from_decibel (AlsaStreamControl *control,
gdouble decibel,
guint *volume);
static gboolean alsa_stream_input_control_get_decibel_from_volume (AlsaStreamControl *control,
guint volume,
gdouble *decibel);
static void read_volume_data (snd_mixer_elem_t *el,
AlsaControlData *data);
static void
alsa_stream_input_control_class_init (AlsaStreamInputControlClass *klass)
{
AlsaStreamControlClass *control_class;
control_class = ALSA_STREAM_CONTROL_CLASS (klass);
control_class->load = alsa_stream_input_control_load;
control_class->set_mute = alsa_stream_input_control_set_mute;
control_class->set_volume = alsa_stream_input_control_set_volume;
control_class->set_channel_volume = alsa_stream_input_control_set_channel_volume;
control_class->get_volume_from_decibel = alsa_stream_input_control_get_volume_from_decibel;
control_class->get_decibel_from_volume = alsa_stream_input_control_get_decibel_from_volume;
}
static void
alsa_stream_input_control_init (AlsaStreamInputControl *control)
{
}
AlsaStreamControl *
alsa_stream_input_control_new (const gchar *name,
const gchar *label,
MateMixerStreamControlRole role,
AlsaStream *stream)
{
g_return_val_if_fail (name != NULL, NULL);
g_return_val_if_fail (label != NULL, NULL);
g_return_val_if_fail (ALSA_IS_STREAM (stream), NULL);
return g_object_new (ALSA_TYPE_STREAM_INPUT_CONTROL,
"name", name,
"label", label,
"role", role,
"stream", stream,
NULL);
}
static gboolean
alsa_stream_input_control_load (AlsaStreamControl *control)
{
AlsaControlData data;
snd_mixer_elem_t *el;
g_return_val_if_fail (ALSA_IS_STREAM_INPUT_CONTROL (control), FALSE);
el = alsa_element_get_snd_element (ALSA_ELEMENT (control));
if G_UNLIKELY (el == NULL)
return FALSE;
if G_UNLIKELY (snd_mixer_selem_has_capture_volume (el) == 0 &&
snd_mixer_selem_has_common_volume (el) == 0) {
g_warn_if_reached ();
return FALSE;
}
memset (&data, 0, sizeof (AlsaControlData));
/* We model any control switch as mute */
if (snd_mixer_selem_has_capture_switch (el) == 1 ||
snd_mixer_selem_has_common_switch (el) == 1)
data.switch_usable = TRUE;
data.active = snd_mixer_selem_is_active (el);
/* Read the volume data but do not error out if it fails, since ALSA reports
* the control to have a volume, expect the control to match what we need - slider
* with an optional mute toggle.
* If it fails to read the volume data, just treat it as a volumeless control */
read_volume_data (el, &data);
alsa_stream_control_set_data (control, &data);
return TRUE;
}
static gboolean
alsa_stream_input_control_set_mute (AlsaStreamControl *control, gboolean mute)
{
snd_mixer_elem_t *el;
gint ret;
g_return_val_if_fail (ALSA_IS_STREAM_INPUT_CONTROL (control), FALSE);
el = alsa_element_get_snd_element (ALSA_ELEMENT (control));
if G_UNLIKELY (el == NULL)
return FALSE;
/* Set the switch for all channels */
ret = snd_mixer_selem_set_capture_switch_all (el, !mute);
if (ret < 0) {
g_warning ("Failed to set capture switch: %s", snd_strerror (ret));
return FALSE;
}
return TRUE;
}
static gboolean
alsa_stream_input_control_set_volume (AlsaStreamControl *control, guint volume)
{
snd_mixer_elem_t *el;
gint ret;
g_return_val_if_fail (ALSA_IS_STREAM_INPUT_CONTROL (control), FALSE);
el = alsa_element_get_snd_element (ALSA_ELEMENT (control));
if G_UNLIKELY (el == NULL)
return FALSE;
/* Set the volume for all channels */
ret = snd_mixer_selem_set_capture_volume_all (el, volume);
if (ret < 0) {
g_warning ("Failed to set volume: %s", snd_strerror (ret));
return FALSE;
}
return TRUE;
}
static gboolean
alsa_stream_input_control_set_channel_volume (AlsaStreamControl *control,
snd_mixer_selem_channel_id_t channel,
guint volume)
{
snd_mixer_elem_t *el;
gint ret;
g_return_val_if_fail (ALSA_IS_STREAM_INPUT_CONTROL (control), FALSE);
el = alsa_element_get_snd_element (ALSA_ELEMENT (control));
if G_UNLIKELY (el == NULL)
return FALSE;
/* Set the volume for a single channel */
ret = snd_mixer_selem_set_capture_volume (el, channel, volume);
if (ret < 0) {
g_warning ("Failed to set channel volume: %s", snd_strerror (ret));
return FALSE;
}
return TRUE;
}
static gboolean
alsa_stream_input_control_get_volume_from_decibel (AlsaStreamControl *control,
gdouble decibel,
guint *volume)
{
#if SND_LIB_VERSION >= ALSA_PACK_VERSION (1, 0, 17)
snd_mixer_elem_t *el;
glong value;
gint ret;
g_return_val_if_fail (ALSA_IS_STREAM_INPUT_CONTROL (control), FALSE);
el = alsa_element_get_snd_element (ALSA_ELEMENT (control));
if G_UNLIKELY (el == NULL)
return FALSE;
ret = snd_mixer_selem_ask_capture_dB_vol (el, (glong) (decibel * 100), 0, &value);
if (ret < 0) {
g_warning ("Failed to convert volume: %s", snd_strerror (ret));
return FALSE;
}
*volume = value;
return TRUE;
#else
return FALSE;
#endif
}
static gboolean
alsa_stream_input_control_get_decibel_from_volume (AlsaStreamControl *control,
guint volume,
gdouble *decibel)
{
#if SND_LIB_VERSION >= ALSA_PACK_VERSION (1, 0, 17)
snd_mixer_elem_t *el;
glong value;
gint ret;
g_return_val_if_fail (ALSA_IS_STREAM_INPUT_CONTROL (control), FALSE);
el = alsa_element_get_snd_element (ALSA_ELEMENT (control));
if G_UNLIKELY (el == NULL)
return FALSE;
ret = snd_mixer_selem_ask_capture_vol_dB (el, (glong) volume, &value);
if (ret < 0) {
g_warning ("Failed to convert volume: %s", snd_strerror (ret));
return FALSE;
}
*decibel = value / 100.0;
return TRUE;
#else
return FALSE;
#endif
}
static void
read_volume_data (snd_mixer_elem_t *el, AlsaControlData *data)
{
glong volume;
glong min, max;
gint ret;
gint i;
/* Read volume ranges, this call should never fail on valid input */
#if SND_LIB_VERSION >= ALSA_PACK_VERSION (1, 0, 10)
ret = snd_mixer_selem_get_capture_volume_range (el, &min, &max);
if G_UNLIKELY (ret < 0) {
g_warning ("Failed to read capture volume range: %s", snd_strerror (ret));
return;
}
#else
snd_mixer_selem_get_capture_volume_range (el, &min, &max);
#endif
data->min = (guint) min;
data->max = (guint) max;
#if SND_LIB_VERSION >= ALSA_PACK_VERSION (1, 0, 10)
/* This fails when decibels are not supported */
ret = snd_mixer_selem_get_capture_dB_range (el, &min, &max);
if (ret == 0) {
data->min_decibel = min / 100.0;
data->max_decibel = max / 100.0;
} else
data->min_decibel = data->max_decibel = -MATE_MIXER_INFINITY;
#else
data->min_decibel = data->max_decibel = -MATE_MIXER_INFINITY;
#endif
for (i = 0; i < MATE_MIXER_CHANNEL_MAX; i++)
data->v[i] = data->min;
data->volume = data->min;
data->volume_joined = snd_mixer_selem_has_capture_volume_joined (el);
if (data->switch_usable == TRUE)
data->switch_joined = snd_mixer_selem_has_capture_switch_joined (el);
if (snd_mixer_selem_is_capture_mono (el) == 1) {
/* Special handling for single channel controls */
ret = snd_mixer_selem_get_capture_volume (el, SND_MIXER_SCHN_MONO, &volume);
if (ret == 0) {
data->channels = 1;
data->c[0] = MATE_MIXER_CHANNEL_MONO;
data->v[0] = data->volume = (guint) volume;
} else {
g_warning ("Failed to read capture volume: %s", snd_strerror (ret));
}
if (data->switch_usable == TRUE) {
gint value;
ret = snd_mixer_selem_get_capture_switch (el, SND_MIXER_SCHN_MONO, &value);
if G_LIKELY (ret == 0)
data->m[0] = !value;
}
} else {
snd_mixer_selem_channel_id_t channel;
/* We use numeric channel indices, but ALSA only works with channel
* positions, go over all the positions supported by ALSA and create
* a list of channels */
for (channel = 0; channel < SND_MIXER_SCHN_LAST; channel++) {
if (snd_mixer_selem_has_capture_channel (el, channel) == 0)
continue;
if (data->switch_usable == TRUE) {
gint value;
ret = snd_mixer_selem_get_capture_switch (el, channel, &value);
if (ret == 0)
data->m[channel] = !value;
}
ret = snd_mixer_selem_get_capture_volume (el, channel, &volume);
if (ret < 0) {
g_warning ("Failed to read capture volume: %s", snd_strerror (ret));
continue;
}
data->channels++;
/* The single value volume is the highest channel volume */
if (data->volume < volume)
data->volume = volume;
data->c[channel] = alsa_channel_map_from[channel];
data->v[channel] = (guint) volume;
}
}
}
|