summaryrefslogtreecommitdiff
path: root/backends/alsa/alsa-stream-output-control.c
blob: 1f4faf8ee9b8773042556a7bc2f11e9554c85770 (plain)
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
/*
 * 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-constants.h"
#include "alsa-element.h"
#include "alsa-stream-control.h"
#include "alsa-stream-output-control.h"

static void alsa_stream_output_control_class_init (AlsaStreamOutputControlClass *klass);
static void alsa_stream_output_control_init       (AlsaStreamOutputControl      *control);

G_DEFINE_TYPE (AlsaStreamOutputControl, alsa_stream_output_control, ALSA_TYPE_STREAM_CONTROL)

static gboolean alsa_stream_output_control_load                    (AlsaStreamControl           *control);

static gboolean alsa_stream_output_control_set_mute                (AlsaStreamControl           *control,
                                                                    gboolean                     mute);

static gboolean alsa_stream_output_control_set_volume              (AlsaStreamControl           *control,
                                                                    guint                        volume);

static gboolean alsa_stream_output_control_set_channel_volume      (AlsaStreamControl           *control,
                                                                    snd_mixer_selem_channel_id_t channel,
                                                                    guint                        volume);

static gboolean alsa_stream_output_control_get_volume_from_decibel (AlsaStreamControl           *control,
                                                                    gdouble                      decibel,
                                                                    guint                       *volume);

static gboolean alsa_stream_output_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_output_control_class_init (AlsaStreamOutputControlClass *klass)
{
    AlsaStreamControlClass *control_class;

    control_class = ALSA_STREAM_CONTROL_CLASS (klass);

    control_class->load                    = alsa_stream_output_control_load;
    control_class->set_mute                = alsa_stream_output_control_set_mute;
    control_class->set_volume              = alsa_stream_output_control_set_volume;
    control_class->set_channel_volume      = alsa_stream_output_control_set_channel_volume;
    control_class->get_volume_from_decibel = alsa_stream_output_control_get_volume_from_decibel;
    control_class->get_decibel_from_volume = alsa_stream_output_control_get_decibel_from_volume;
}

static void
alsa_stream_output_control_init (AlsaStreamOutputControl *control)
{
}

AlsaStreamControl *
alsa_stream_output_control_new (const gchar               *name,
                                const gchar               *label,
                                MateMixerStreamControlRole role,
                                AlsaStream                *stream)
{
    return g_object_new (ALSA_TYPE_STREAM_OUTPUT_CONTROL,
                         "name", name,
                         "label", label,
                         "role", role,
                         "stream", stream,
                         NULL);
}

static gboolean
alsa_stream_output_control_load (AlsaStreamControl *control)
{
    AlsaControlData   data;
    snd_mixer_elem_t *el;

    g_return_val_if_fail (ALSA_IS_STREAM_OUTPUT_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_playback_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_playback_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_output_control_set_mute (AlsaStreamControl *control, gboolean mute)
{
    snd_mixer_elem_t *el;
    gint              ret;

    g_return_val_if_fail (ALSA_IS_STREAM_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_playback_switch_all (el, !mute);
    if (ret < 0) {
        g_warning ("Failed to set playback switch: %s", snd_strerror (ret));
        return FALSE;
    }
    return TRUE;
}

static gboolean
alsa_stream_output_control_set_volume (AlsaStreamControl *control, guint volume)
{
    snd_mixer_elem_t *el;
    gint              ret;

    g_return_val_if_fail (ALSA_IS_STREAM_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_playback_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_output_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_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_playback_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_output_control_get_volume_from_decibel (AlsaStreamControl *control,
                                                    gdouble            decibel,
                                                    guint             *volume)
{
    snd_mixer_elem_t *el;
    glong             value;
    gint              ret;

    g_return_val_if_fail (ALSA_IS_STREAM_CONTROL (control), FALSE);

    el = alsa_element_get_snd_element (ALSA_ELEMENT (control));
    if G_UNLIKELY (el == NULL)
        return FALSE;

    ret = snd_mixer_selem_ask_playback_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;
}

static gboolean
alsa_stream_output_control_get_decibel_from_volume (AlsaStreamControl *control,
                                                    guint              volume,
                                                    gdouble           *decibel)
{
    snd_mixer_elem_t *el;
    glong             value;
    gint              ret;

    g_return_val_if_fail (ALSA_IS_STREAM_CONTROL (control), FALSE);

    el = alsa_element_get_snd_element (ALSA_ELEMENT (control));
    if G_UNLIKELY (el == NULL)
        return FALSE;

    ret = snd_mixer_selem_ask_playback_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;
}

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 */
    ret = snd_mixer_selem_get_playback_volume_range (el, &min, &max);
    if G_UNLIKELY (ret < 0) {
        g_warning ("Failed to read playback volume range: %s", snd_strerror (ret));
        return;
    }
    data->min = (guint) min;
    data->max = (guint) max;

    /* This fails when decibels are not supported */
    ret = snd_mixer_selem_get_playback_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;

    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_playback_volume_joined (el);

    if (data->switch_usable == TRUE)
        data->switch_joined = snd_mixer_selem_has_playback_switch_joined (el);

    if (snd_mixer_selem_is_playback_mono (el) == 1) {
        /* Special handling for single channel controls */
        ret = snd_mixer_selem_get_playback_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 playback volume: %s", snd_strerror (ret));
        }

        if (data->switch_usable == TRUE) {
            gint value;

            ret = snd_mixer_selem_get_playback_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_playback_channel (el, channel) == 0)
                continue;

            if (data->switch_usable == TRUE) {
                gint value;

                ret = snd_mixer_selem_get_playback_switch (el, channel, &value);
                if (ret == 0)
                    data->m[channel] = !value;
            }

            ret = snd_mixer_selem_get_playback_volume (el, channel, &volume);
            if (ret < 0) {
                g_warning ("Failed to read playback 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;
        }
    }
}