summaryrefslogtreecommitdiff
path: root/backends/alsa/alsa-stream.c
blob: 7e9c89f8604f5b9efbd7262be851cf7aa50c583a (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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
 * 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 <libmatemixer/matemixer.h>
#include <libmatemixer/matemixer-private.h>

#include "alsa-device.h"
#include "alsa-element.h"
#include "alsa-stream.h"
#include "alsa-stream-control.h"
#include "alsa-switch.h"
#include "alsa-toggle.h"

struct _AlsaStreamPrivate
{
    GList *switches;
    GList *controls;
};

static void alsa_stream_class_init (AlsaStreamClass *klass);
static void alsa_stream_init       (AlsaStream      *stream);
static void alsa_stream_dispose    (GObject         *object);

G_DEFINE_TYPE (AlsaStream, alsa_stream, MATE_MIXER_TYPE_STREAM)

static const GList *alsa_stream_list_controls (MateMixerStream *mms);
static const GList *alsa_stream_list_switches (MateMixerStream *mms);

static gint         compare_control_name      (gconstpointer    a,
                                               gconstpointer    b);
static gint         compare_switch_name       (gconstpointer    a,
                                               gconstpointer    b);

static void
alsa_stream_class_init (AlsaStreamClass *klass)
{
    GObjectClass         *object_class;
    MateMixerStreamClass *stream_class;

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

    stream_class = MATE_MIXER_STREAM_CLASS (klass);
    stream_class->list_controls = alsa_stream_list_controls;
    stream_class->list_switches = alsa_stream_list_switches;

    g_type_class_add_private (object_class, sizeof (AlsaStreamPrivate));
}

static void
alsa_stream_init (AlsaStream *stream)
{
    stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
                                                ALSA_TYPE_STREAM,
                                                AlsaStreamPrivate);
}

static void
alsa_stream_dispose (GObject *object)
{
    AlsaStream *stream;

    stream = ALSA_STREAM (object);

    if (stream->priv->controls != NULL) {
        g_list_free_full (stream->priv->controls, g_object_unref);
        stream->priv->controls = NULL;
    }
    if (stream->priv->switches != NULL) {
        g_list_free_full (stream->priv->switches, g_object_unref);
        stream->priv->switches = NULL;
    }

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

AlsaStream *
alsa_stream_new (const gchar       *name,
                 MateMixerDevice   *device,
                 MateMixerDirection direction)
{
    const gchar *label;

    g_return_val_if_fail (name != NULL, NULL);
    g_return_val_if_fail (ALSA_IS_DEVICE (device), NULL);

    label = mate_mixer_device_get_label (device);

    return g_object_new (ALSA_TYPE_STREAM,
                         "name", name,
                         "label", label,
                         "device", device,
                         "direction", direction,
                         NULL);
}

void
alsa_stream_add_control (AlsaStream *stream, AlsaStreamControl *control)
{
    g_return_if_fail (ALSA_IS_STREAM (stream));
    g_return_if_fail (ALSA_IS_STREAM_CONTROL (control));

    stream->priv->controls =
        g_list_append (stream->priv->controls, g_object_ref (control));

    g_signal_emit_by_name (G_OBJECT (stream),
                           "control-added",
                           MATE_MIXER_STREAM_CONTROL (control));

    if (alsa_stream_has_default_control (stream) == FALSE)
        alsa_stream_set_default_control (stream, control);
}

void
alsa_stream_add_switch (AlsaStream *stream, AlsaSwitch *swtch)
{
    g_return_if_fail (ALSA_IS_STREAM (stream));
    g_return_if_fail (ALSA_IS_SWITCH (swtch));

    stream->priv->switches =
        g_list_append (stream->priv->switches, g_object_ref (swtch));

    g_signal_emit_by_name (G_OBJECT (stream),
                           "switch-added",
                           MATE_MIXER_STREAM_SWITCH (swtch));
}

void
alsa_stream_add_toggle (AlsaStream *stream, AlsaToggle *toggle)
{
    g_return_if_fail (ALSA_IS_STREAM (stream));
    g_return_if_fail (ALSA_IS_TOGGLE (toggle));

    /* Toggle is MateMixerSwitch, but not AlsaSwitch */
    stream->priv->switches =
        g_list_append (stream->priv->switches, g_object_ref (toggle));

    g_signal_emit_by_name (G_OBJECT (stream),
                           "switch-added",
                           MATE_MIXER_STREAM_SWITCH (toggle));
}

gboolean
alsa_stream_has_controls (AlsaStream *stream)
{
    g_return_val_if_fail (ALSA_IS_STREAM (stream), FALSE);

    if (stream->priv->controls != NULL)
        return TRUE;

    return FALSE;
}

gboolean
alsa_stream_has_switches (AlsaStream *stream)
{
    g_return_val_if_fail (ALSA_IS_STREAM (stream), FALSE);

    if (stream->priv->switches != NULL)
        return TRUE;

    return FALSE;
}

gboolean
alsa_stream_has_controls_or_switches (AlsaStream *stream)
{
    g_return_val_if_fail (ALSA_IS_STREAM (stream), FALSE);

    if (stream->priv->controls != NULL ||
        stream->priv->switches != NULL)
        return TRUE;

    return FALSE;
}

gboolean
alsa_stream_has_default_control (AlsaStream *stream)
{
    g_return_val_if_fail (ALSA_IS_STREAM (stream), FALSE);

    if (mate_mixer_stream_get_default_control (MATE_MIXER_STREAM (stream)) != NULL)
        return TRUE;

    return FALSE;
}

AlsaStreamControl *
alsa_stream_get_default_control (AlsaStream *stream)
{
    MateMixerStreamControl *control;

    g_return_val_if_fail (ALSA_IS_STREAM (stream), NULL);

    control = mate_mixer_stream_get_default_control (MATE_MIXER_STREAM (stream));
    if (control != NULL)
        return ALSA_STREAM_CONTROL (control);

    return NULL;
}

void
alsa_stream_set_default_control (AlsaStream *stream, AlsaStreamControl *control)
{
    g_return_if_fail (ALSA_IS_STREAM (stream));
    g_return_if_fail (control == NULL || ALSA_IS_STREAM_CONTROL (control));

    if (control == NULL)
        _mate_mixer_stream_set_default_control (MATE_MIXER_STREAM (stream), NULL);
    else
        _mate_mixer_stream_set_default_control (MATE_MIXER_STREAM (stream),
                                                MATE_MIXER_STREAM_CONTROL (control));
}

void
alsa_stream_load_elements (AlsaStream *stream, const gchar *name)
{
    GList *item;

    g_return_if_fail (ALSA_IS_STREAM (stream));
    g_return_if_fail (name != NULL);

    item = g_list_find_custom (stream->priv->controls, name, compare_control_name);
    if (item != NULL)
        alsa_element_load (ALSA_ELEMENT (item->data));

    item = g_list_find_custom (stream->priv->switches, name, compare_switch_name);
    if (item != NULL)
        alsa_element_load (ALSA_ELEMENT (item->data));
}

gboolean
alsa_stream_remove_elements (AlsaStream *stream, const gchar *name)
{
    GList   *item;
    gboolean removed = FALSE;

    g_return_val_if_fail (ALSA_IS_STREAM (stream), FALSE);
    g_return_val_if_fail (name != NULL, FALSE);

    item = g_list_find_custom (stream->priv->controls, name, compare_control_name);
    if (item != NULL) {
        MateMixerStreamControl *control = MATE_MIXER_STREAM_CONTROL (item->data);

        alsa_element_close (ALSA_ELEMENT (control));
        stream->priv->controls = g_list_delete_link (stream->priv->controls, item);

        /* Change the default control if we have just removed it */
        if (control == mate_mixer_stream_get_default_control (MATE_MIXER_STREAM (stream))) {
            AlsaStreamControl *first = NULL;

            if (stream->priv->controls != NULL)
                first = ALSA_STREAM_CONTROL (stream->priv->controls->data);

            alsa_stream_set_default_control (stream, first);
        }

        g_signal_emit_by_name (G_OBJECT (stream),
                               "control-removed",
                               control);

        g_object_unref (control);
        removed = TRUE;
    }

    item = g_list_find_custom (stream->priv->switches, name, compare_switch_name);
    if (item != NULL) {
        MateMixerStreamSwitch *swtch = MATE_MIXER_STREAM_SWITCH (item->data);

        alsa_element_close (ALSA_ELEMENT (swtch));

        stream->priv->switches = g_list_delete_link (stream->priv->switches, item);
        g_signal_emit_by_name (G_OBJECT (stream),
                               "switch-removed",
                               swtch);

        g_object_unref (swtch);
        removed = TRUE;
    }

    return removed;
}

void
alsa_stream_remove_all (AlsaStream *stream)
{
    GList *list;

    g_return_if_fail (ALSA_IS_STREAM (stream));

    /* Remove all stream controls */
    list = stream->priv->controls;
    while (list != NULL) {
        MateMixerStreamControl *control = MATE_MIXER_STREAM_CONTROL (list->data);
        GList *next = list->next;

        alsa_element_close (ALSA_ELEMENT (control));

        stream->priv->controls = g_list_delete_link (stream->priv->controls, list);
        g_signal_emit_by_name (G_OBJECT (stream),
                               "control-removed",
                               control);

        g_object_unref (control);
        list = next;
    }

    /* Unset the default stream control */
    alsa_stream_set_default_control (stream, NULL);

    /* Remove all stream switches */
    list = stream->priv->switches;
    while (list != NULL) {
        MateMixerStreamSwitch *swtch = MATE_MIXER_STREAM_SWITCH (list->data);
        GList *next = list->next;

        alsa_element_close (ALSA_ELEMENT (swtch));

        stream->priv->switches = g_list_delete_link (stream->priv->switches, list);
        g_signal_emit_by_name (G_OBJECT (stream),
                               "switch-removed",
                               swtch);

        g_object_unref (swtch);
        list = next;
    }
}

static const GList *
alsa_stream_list_controls (MateMixerStream *mms)
{
    g_return_val_if_fail (ALSA_IS_STREAM (mms), NULL);

    return ALSA_STREAM (mms)->priv->controls;
}

static const GList *
alsa_stream_list_switches (MateMixerStream *mms)
{
    g_return_val_if_fail (ALSA_IS_STREAM (mms), NULL);

    return ALSA_STREAM (mms)->priv->switches;
}

static gint
compare_control_name (gconstpointer a, gconstpointer b)
{
    MateMixerStreamControl *control = MATE_MIXER_STREAM_CONTROL (a);
    const gchar            *name    = (const gchar *) b;

    return strcmp (mate_mixer_stream_control_get_name (control), name);
}

static gint
compare_switch_name (gconstpointer a, gconstpointer b)
{
    MateMixerSwitch *swtch = MATE_MIXER_SWITCH (a);
    const gchar     *name  = (const gchar *) b;

    return strcmp (mate_mixer_switch_get_name (swtch), name);
}