summaryrefslogtreecommitdiff
path: root/backends/alsa/alsa-switch.c
blob: 7711e0db0689a6e6c688abc71860f4525af4da27 (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
/*
 * 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-element.h"
#include "alsa-stream.h"
#include "alsa-switch.h"
#include "alsa-switch-option.h"

struct _AlsaSwitchPrivate
{
    GList            *options;
    guint32           channel_mask;
    snd_mixer_elem_t *element;
};

static void alsa_element_interface_init (AlsaElementInterface *iface);

static void alsa_switch_class_init      (AlsaSwitchClass      *klass);
static void alsa_switch_init            (AlsaSwitch           *swtch);
static void alsa_switch_dispose         (GObject              *object);

G_DEFINE_TYPE_WITH_CODE (AlsaSwitch, alsa_switch,
                         MATE_MIXER_TYPE_STREAM_SWITCH,
                         G_ADD_PRIVATE(AlsaSwitch)
                         G_IMPLEMENT_INTERFACE (ALSA_TYPE_ELEMENT,
                                                alsa_element_interface_init))

static gboolean               alsa_switch_set_active_option (MateMixerSwitch       *mms,
                                                             MateMixerSwitchOption *mmso);

static const GList *          alsa_switch_list_options      (MateMixerSwitch       *mms);

static snd_mixer_elem_t *     alsa_switch_get_snd_element   (AlsaElement           *element);
static void                   alsa_switch_set_snd_element   (AlsaElement           *element,
                                                             snd_mixer_elem_t      *el);
static gboolean               alsa_switch_load              (AlsaElement           *element);

static void
alsa_element_interface_init (AlsaElementInterface *iface)
{
    iface->get_snd_element = alsa_switch_get_snd_element;
    iface->set_snd_element = alsa_switch_set_snd_element;
    iface->load            = alsa_switch_load;
}

static void
alsa_switch_class_init (AlsaSwitchClass *klass)
{
    GObjectClass         *object_class;
    MateMixerSwitchClass *switch_class;

    object_class = G_OBJECT_CLASS (klass);
    object_class->dispose = alsa_switch_dispose;

    switch_class = MATE_MIXER_SWITCH_CLASS (klass);
    switch_class->set_active_option = alsa_switch_set_active_option;
    switch_class->list_options      = alsa_switch_list_options;
}

static void
alsa_switch_dispose (GObject *object)
{
    AlsaSwitch *swtch;

    swtch = ALSA_SWITCH (object);

    if (swtch->priv->options != NULL) {
        g_list_free_full (swtch->priv->options, g_object_unref);
        swtch->priv->options = NULL;
    }

    G_OBJECT_CLASS (alsa_switch_parent_class)->dispose (object);
}

static void
alsa_switch_init (AlsaSwitch *swtch)
{
    swtch->priv = alsa_switch_get_instance_private (swtch);
}

AlsaSwitch *
alsa_switch_new (AlsaStream               *stream,
                 const gchar              *name,
                 const gchar              *label,
                 MateMixerStreamSwitchRole role,
                 GList                    *options)
{
    AlsaSwitch *swtch;

    g_return_val_if_fail (ALSA_IS_STREAM (stream), NULL);
    g_return_val_if_fail (name != NULL, NULL);
    g_return_val_if_fail (label != NULL, NULL);
    g_return_val_if_fail (options != NULL, NULL);

    swtch = g_object_new (ALSA_TYPE_SWITCH,
                          "name", name,
                          "label", label,
                          "role", role,
                          "stream", stream,
                          NULL);

    /* Takes ownership of options */
    swtch->priv->options = options;
    return swtch;
}

static gboolean
alsa_switch_set_active_option (MateMixerSwitch *mms, MateMixerSwitchOption *mmso)
{
    AlsaSwitch                  *swtch;
    guint                        index;
    gboolean                     set_item = FALSE;
    snd_mixer_selem_channel_id_t channel;

    g_return_val_if_fail (ALSA_IS_SWITCH (mms), FALSE);
    g_return_val_if_fail (ALSA_IS_SWITCH_OPTION (mmso), FALSE);

    swtch = ALSA_SWITCH (mms);

    if G_UNLIKELY (swtch->priv->element == NULL)
        return FALSE;

    /* The channel mask is created when reading the active option the first
     * time, so a successful load must be done before changing the option */
    if G_UNLIKELY (swtch->priv->channel_mask == 0) {
        g_debug ("Not setting active switch option, channel mask unknown");
        return FALSE;
    }

    index = alsa_switch_option_get_id (ALSA_SWITCH_OPTION (mmso));

    for (channel = 0; channel < SND_MIXER_SCHN_LAST; channel++) {
        /* The option is set per-channel, make sure to set it only for channels
         * we successfully read the value from */
        if (swtch->priv->channel_mask & (1 << channel)) {
            gint ret = snd_mixer_selem_set_enum_item (swtch->priv->element,
                                                      channel,
                                                      index);
            if (ret == 0)
                set_item = TRUE;
            else
                g_warning ("Failed to set active option of switch %s: %s",
                           snd_mixer_selem_get_name (swtch->priv->element),
                           snd_strerror (ret));
        }
    }
    return set_item;
}

static const GList *
alsa_switch_list_options (MateMixerSwitch *mms)
{
    g_return_val_if_fail (ALSA_IS_SWITCH (mms), NULL);

    return ALSA_SWITCH (mms)->priv->options;
}

static snd_mixer_elem_t *
alsa_switch_get_snd_element (AlsaElement *element)
{
    g_return_val_if_fail (ALSA_IS_SWITCH (element), NULL);

    return ALSA_SWITCH (element)->priv->element;
}

static void
alsa_switch_set_snd_element (AlsaElement *element, snd_mixer_elem_t *el)
{
    g_return_if_fail (ALSA_IS_SWITCH (element));

    ALSA_SWITCH (element)->priv->element = el;
}

static gboolean
alsa_switch_load (AlsaElement *element)
{
    AlsaSwitch                  *swtch;
    GList                       *list;
    guint                        item;
    gint                         ret;
    snd_mixer_selem_channel_id_t c;

    g_return_val_if_fail (ALSA_IS_SWITCH (element), FALSE);

    swtch = ALSA_SWITCH (element);

    if G_UNLIKELY (swtch->priv->element == NULL)
        return FALSE;

    /* When reading the first time we try all the channels, otherwise only the
     * ones which returned success before */
    if (swtch->priv->channel_mask == 0) {
        for (c = 0; c < SND_MIXER_SCHN_LAST; c++) {
            ret = snd_mixer_selem_get_enum_item (swtch->priv->element, c, &item);

            /* The active enum option is set per-channel, so when reading it the
             * first time, create a mask of all channels for which we read the
             * value successfully */
            if (ret == 0)
                swtch->priv->channel_mask |= 1 << c;
        }

        /* The last ALSA call might have failed, but it doesn't matter if we have
         * a channel mask */
        if (swtch->priv->channel_mask > 0)
            ret = 0;
    } else {
        for (c = 0; !(swtch->priv->channel_mask & (1 << c)); c++)
            ;

        /* When not reading the mask, the first usable channel is enough, we don't
         * support per-channel selections anyway */
        ret = snd_mixer_selem_get_enum_item (swtch->priv->element, c, &item);
    }

    if (ret < 0) {
        g_warning ("Failed to read active option of switch %s: %s",
                   snd_mixer_selem_get_name (swtch->priv->element),
                   snd_strerror (ret));
        return FALSE;
    }

    list = swtch->priv->options;
    while (list != NULL) {
        AlsaSwitchOption *option = ALSA_SWITCH_OPTION (list->data);

        /* Mark the selected option when we find it, ALSA indentifies them
         * by numeric indices */
        if (alsa_switch_option_get_id (option) == item) {
            _mate_mixer_switch_set_active_option (MATE_MIXER_SWITCH (swtch),
                                                  MATE_MIXER_SWITCH_OPTION (option));
            return TRUE;
        }
        list = list->next;
    }

    g_warning ("Unknown active option of switch %s: %d",
               mate_mixer_switch_get_name (MATE_MIXER_SWITCH (swtch)),
               item);

    return FALSE;
}