summaryrefslogtreecommitdiff
path: root/backends/oss/oss-device.c
blob: 44ed18fc4cf3867913a17dd485a72d4e000fed97 (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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
 * 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 <errno.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <glib-object.h>

#include <libmatemixer/matemixer.h>
#include <libmatemixer/matemixer-private.h>

#include "oss-common.h"
#include "oss-device.h"
#include "oss-stream.h"
#include "oss-stream-control.h"
#include "oss-switch-option.h"

/*
 * NOTES:
 *
 * OSS has a predefined list of channels (or "devices"), which historically used
 * to be mapped to individual sound card pins. At this time, the channels are
 * chosen somehow arbitrarily by drivers.
 *
 * Each of the channels may have a record switch, which toggles between playback
 * and capture direction. OSS doesn't have mute switches and we can't really use
 * the record switch as one. For this reason all channels are modelled as
 * muteless stream controls and the record switch is modelled as a port switch.
 *
 * Also, we avoid modelling capturable channels as both input and output channels,
 * because the ones which allow capture are normally capture-only channels
 * (OSS just doesn't have the ability to make the distinction), so each channel in
 * the list contains a flag of whether it can be used as a capture source, given
 * that it's reported as capturable. Capturable channels are therefore modelled
 * as input controls and this approach avoids for example putting PCM in an input
 * stream (which makes no sense).
 *
 * OSS also has an indicator of whether the record switch is exclusive (only
 * allows one capture source at a time), to simplify the lives of applications
 * we always create a port switch and therefore assume the exclusivity is always
 * true. Ideally, we should probably model a bunch of toggles, one for each channel
 * with capture capability if they are known not to be exclusive.
 */

#define OSS_DEVICE_ICON "audio-card"

#define OSS_POLL_TIMEOUT_NORMAL   500
#define OSS_POLL_TIMEOUT_RAPID     50
#define OSS_POLL_TIMEOUT_RESTORE 3000

typedef enum {
    OSS_POLL_NORMAL,
    OSS_POLL_RAPID
} OssPollMode;

typedef enum {
    OSS_DEV_ANY,
    OSS_DEV_INPUT,
    OSS_DEV_OUTPUT
} OssDevChannelType;

typedef struct {
    gchar                     *name;
    gchar                     *label;
    MateMixerStreamControlRole role;
    OssDevChannelType          type;
    gchar                     *icon;
} OssDevChannel;

/* Index of a channel in the array corresponds to the channel number passed to ioctl()s,
 * device names are takes from soundcard.h */
static const OssDevChannel oss_devices[] = {
    { "vol",     N_("Volume"),      MATE_MIXER_STREAM_CONTROL_ROLE_MASTER,     OSS_DEV_OUTPUT,  NULL },
    { "bass",    N_("Bass"),        MATE_MIXER_STREAM_CONTROL_ROLE_BASS,       OSS_DEV_OUTPUT,  NULL },
    { "treble",  N_("Treble"),      MATE_MIXER_STREAM_CONTROL_ROLE_TREBLE,     OSS_DEV_OUTPUT,  NULL },
    { "synth",   N_("Synth"),       MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN,    OSS_DEV_INPUT,   NULL },
    { "pcm",     N_("PCM"),         MATE_MIXER_STREAM_CONTROL_ROLE_PCM,        OSS_DEV_OUTPUT,  NULL },
    /* OSS manual says this should be the beeper, but Linux OSS seems to assign it to
     * regular volume control */
    { "speaker", N_("Speaker"),     MATE_MIXER_STREAM_CONTROL_ROLE_SPEAKER,    OSS_DEV_OUTPUT,  NULL },
    { "line",    N_("Line In"),     MATE_MIXER_STREAM_CONTROL_ROLE_PORT,       OSS_DEV_INPUT,   NULL },
    { "mic",     N_("Microphone"),  MATE_MIXER_STREAM_CONTROL_ROLE_MICROPHONE, OSS_DEV_INPUT,   "audio-input-microphone" },
    { "cd",      N_("CD"),          MATE_MIXER_STREAM_CONTROL_ROLE_CD,         OSS_DEV_INPUT,   NULL },
    /* Recording monitor */
    { "mix",     N_("Mixer"),       MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN,    OSS_DEV_OUTPUT,  NULL },
    { "pcm2",    N_("PCM 2"),       MATE_MIXER_STREAM_CONTROL_ROLE_PCM,        OSS_DEV_OUTPUT,  NULL },
    /* Recording level (master input) */
    { "rec",     N_("Record"),      MATE_MIXER_STREAM_CONTROL_ROLE_MASTER,     OSS_DEV_INPUT,   NULL },
    { "igain",   N_("Input Gain"),  MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN,    OSS_DEV_INPUT,   NULL },
    { "ogain",   N_("Output Gain"), MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN,    OSS_DEV_OUTPUT,  NULL },
    { "line1",   N_("Line In 1"),   MATE_MIXER_STREAM_CONTROL_ROLE_PORT,       OSS_DEV_INPUT,   NULL },
    { "line2",   N_("Line In 2"),   MATE_MIXER_STREAM_CONTROL_ROLE_PORT,       OSS_DEV_INPUT,   NULL },
    { "line3",   N_("Line In 3"),   MATE_MIXER_STREAM_CONTROL_ROLE_PORT,       OSS_DEV_INPUT,   NULL },
    /* These 3 can be attached to either digital input or output */
    { "dig1",    N_("Digital 1"),   MATE_MIXER_STREAM_CONTROL_ROLE_PORT,       OSS_DEV_ANY,     NULL },
    { "dig2",    N_("Digital 2"),   MATE_MIXER_STREAM_CONTROL_ROLE_PORT,       OSS_DEV_ANY,     NULL },
    { "dig3",    N_("Digital 3"),   MATE_MIXER_STREAM_CONTROL_ROLE_PORT,       OSS_DEV_ANY,     NULL },
    { "phin",    N_("Phone In"),    MATE_MIXER_STREAM_CONTROL_ROLE_PORT,       OSS_DEV_INPUT,   NULL },
    { "phout",   N_("Phone Out"),   MATE_MIXER_STREAM_CONTROL_ROLE_PORT,       OSS_DEV_OUTPUT,  NULL },
    { "video",   N_("Video"),       MATE_MIXER_STREAM_CONTROL_ROLE_VIDEO,      OSS_DEV_INPUT,   NULL },
    { "radio",   N_("Radio"),       MATE_MIXER_STREAM_CONTROL_ROLE_PORT,       OSS_DEV_INPUT,   NULL },

    /* soundcard.h on some systems include more channels, but different files provide
     * different meanings for the remaining ones and the value is doubtful */
};

#define OSS_N_DEVICES MIN (G_N_ELEMENTS (oss_devices), SOUND_MIXER_NRDEVICES)

/* Priorities for selecting default controls */
static const gint oss_input_priority[] = {
    11, /* rec */
    12, /* igain */
    7,  /* mic */
    6,  /* line */
    14, /* line1 */
    15, /* line2 */
    16, /* line3 */
    17, /* dig1 */
    18, /* dig2 */
    19, /* dig3 */
    20, /* phin */
    8,  /* cd */
    22, /* video */
    23, /* radio */
    3,  /* synth */
    27  /* midi */
};

static const gint oss_output_priority[] = {
    0,  /* vol */
    4,  /* pcm */
    10, /* pcm2 */
    5,  /* speaker */
    17, /* dig1 */
    18, /* dig2 */
    19, /* dig3 */
    25, /* depth */
    26, /* center */
    21, /* phone out */
    13, /* ogain */
    9,  /* mix */
    24, /* monitor */
    1,  /* bass */
    2   /* treble */
};

struct _OssDevicePrivate
{
    gint        fd;
    gchar      *path;
    gint        devmask;
    gint        stereodevs;
    gint        recmask;
    guint       poll_tag;
    guint       poll_tag_restore;
    guint       poll_counter;
    gboolean    poll_use_counter;
    OssPollMode poll_mode;
    GList      *streams;
    OssStream  *input;
    OssStream  *output;
};

enum {
    CLOSED,
    N_SIGNALS
};

static guint signals[N_SIGNALS] = { 0, };

static void oss_device_class_init (OssDeviceClass *klass);
static void oss_device_init       (OssDevice      *device);
static void oss_device_dispose    (GObject        *object);
static void oss_device_finalize   (GObject        *object);

G_DEFINE_TYPE (OssDevice, oss_device, MATE_MIXER_TYPE_DEVICE)

static const GList *oss_device_list_streams       (MateMixerDevice *mmd);

static gboolean     poll_mixer                    (OssDevice       *device);
static gboolean     poll_mixer_restore            (OssDevice       *device);

static void         read_mixer_devices            (OssDevice       *device);
static void         read_mixer_switch             (OssDevice       *device);

static guint        create_poll_source            (OssDevice       *device,
                                                   OssPollMode      mode);
static guint        create_poll_restore_source    (OssDevice       *device);

static void         free_stream_list              (OssDevice       *device);

static gint         compare_stream_control_devnum (gconstpointer    a,
                                                   gconstpointer    b);

static void
oss_device_class_init (OssDeviceClass *klass)
{
    GObjectClass         *object_class;
    MateMixerDeviceClass *device_class;

    object_class = G_OBJECT_CLASS (klass);
    object_class->dispose  = oss_device_dispose;
    object_class->finalize = oss_device_finalize;

    device_class = MATE_MIXER_DEVICE_CLASS (klass);
    device_class->list_streams = oss_device_list_streams;

    signals[CLOSED] =
        g_signal_new ("closed",
                      G_TYPE_FROM_CLASS (object_class),
                      G_SIGNAL_RUN_FIRST,
                      G_STRUCT_OFFSET (OssDeviceClass, closed),
                      NULL,
                      NULL,
                      g_cclosure_marshal_VOID__VOID,
                      G_TYPE_NONE,
                      0,
                      G_TYPE_NONE);

    g_type_class_add_private (object_class, sizeof (OssDevicePrivate));
}

static void
oss_device_init (OssDevice *device)
{
    device->priv = G_TYPE_INSTANCE_GET_PRIVATE (device,
                                                OSS_TYPE_DEVICE,
                                                OssDevicePrivate);
}

static void
oss_device_dispose (GObject *object)
{
    OssDevice *device;

    device = OSS_DEVICE (object);

    g_clear_object (&device->priv->input);
    g_clear_object (&device->priv->output);

    free_stream_list (device);

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

static void
oss_device_finalize (GObject *object)
{
    OssDevice *device = OSS_DEVICE (object);

    if (device->priv->fd != -1)
        close (device->priv->fd);

    g_free (device->priv->path);

    G_OBJECT_CLASS (oss_device_parent_class)->finalize (object);
}

OssDevice *
oss_device_new (const gchar *name,
                const gchar *label,
                const gchar *path,
                gint         fd)
{
    OssDevice *device;
    gchar     *stream_name;

    g_return_val_if_fail (name  != NULL, NULL);
    g_return_val_if_fail (label != NULL, NULL);
    g_return_val_if_fail (path  != NULL, NULL);

    device = g_object_new (OSS_TYPE_DEVICE,
                           "name", name,
                           "label", label,
                           "icon", OSS_DEVICE_ICON,
                           NULL);

    device->priv->fd   = dup (fd);
    device->priv->path = g_strdup (path);

    stream_name = g_strdup_printf ("oss-input-%s", name);
    device->priv->input = oss_stream_new (stream_name,
                                          MATE_MIXER_DEVICE (device),
                                          MATE_MIXER_DIRECTION_INPUT);
    g_free (stream_name);

    stream_name = g_strdup_printf ("oss-output-%s", name);
    device->priv->output = oss_stream_new (stream_name,
                                           MATE_MIXER_DEVICE (device),
                                           MATE_MIXER_DIRECTION_OUTPUT);
    g_free (stream_name);

    return device;
}

gboolean
oss_device_open (OssDevice *device)
{
    gint ret;

    g_return_val_if_fail (OSS_IS_DEVICE (device), FALSE);

    g_debug ("Opening device %s (%s)",
             device->priv->path,
             mate_mixer_device_get_label (MATE_MIXER_DEVICE (device)));

    /* Read the essential information about the device, these values are not
     * expected to change and will not be queried */
    ret = ioctl (device->priv->fd, MIXER_READ (SOUND_MIXER_DEVMASK),
                 &device->priv->devmask);
    if (ret == -1)
        goto fail;

    ret = ioctl (device->priv->fd, MIXER_READ (SOUND_MIXER_STEREODEVS),
                 &device->priv->stereodevs);
    if (ret == -1)
        goto fail;

    ret = ioctl (device->priv->fd, MIXER_READ (SOUND_MIXER_RECMASK),
                 &device->priv->recmask);
    if (ret == -1)
        goto fail;

    /* NOTE: Linux also supports SOUND_MIXER_OUTSRC and SOUND_MIXER_OUTMASK which
     * inform about/enable input->output, we could potentially create toggles
     * for these, but these constants are not defined on any BSD. */

    return TRUE;

fail:
    g_warning ("Failed to read device %s: %s",
               device->priv->path,
               g_strerror (errno));

    return FALSE;
}

gboolean
oss_device_is_open (OssDevice *device)
{
    g_return_val_if_fail (OSS_IS_DEVICE (device), FALSE);

    if (device->priv->fd != -1)
        return TRUE;

    return FALSE;
}

void
oss_device_close (OssDevice *device)
{
    g_return_if_fail (OSS_IS_DEVICE (device));

    if (device->priv->fd == -1)
        return;

    /* Make each stream remove its controls and switch */
    if (oss_stream_has_controls (device->priv->input) == TRUE) {
        const gchar *name =
            mate_mixer_stream_get_name (MATE_MIXER_STREAM (device->priv->input));

        oss_stream_remove_all (device->priv->input);
        free_stream_list (device);

        g_signal_emit_by_name (G_OBJECT (device),
                               "stream-removed",
                               name);
    }

    if (oss_stream_has_controls (device->priv->output) == TRUE) {
        const gchar *name =
            mate_mixer_stream_get_name (MATE_MIXER_STREAM (device->priv->output));

        oss_stream_remove_all (device->priv->output);
        free_stream_list (device);

        g_signal_emit_by_name (G_OBJECT (device),
                               "stream-removed",
                               name);
    }

    if (device->priv->poll_tag != 0)
        g_source_remove (device->priv->poll_tag);

    if (device->priv->poll_tag_restore != 0)
        g_source_remove (device->priv->poll_tag_restore);

    close (device->priv->fd);
    device->priv->fd = -1;

    g_signal_emit (G_OBJECT (device), signals[CLOSED], 0);
}

void
oss_device_load (OssDevice *device)
{
    const GList *controls;
    guint        i;

    g_return_if_fail (OSS_IS_DEVICE (device));

    read_mixer_devices (device);

    /* Set default input control */
    if (oss_stream_has_controls (device->priv->input) == TRUE) {
        controls = mate_mixer_stream_list_controls (MATE_MIXER_STREAM (device->priv->input));

        for (i = 0; i < G_N_ELEMENTS (oss_input_priority); i++) {
            GList *item = g_list_find_custom ((GList *) controls,
                                              GINT_TO_POINTER (oss_input_priority[i]),
                                              compare_stream_control_devnum);
            if (item == NULL)
                continue;

            oss_stream_set_default_control (device->priv->input,
                                            OSS_STREAM_CONTROL (item->data));
            break;
        }
    }

    /* Set default output control */
    if (oss_stream_has_controls (device->priv->output) == TRUE) {
        controls = mate_mixer_stream_list_controls (MATE_MIXER_STREAM (device->priv->output));

        for (i = 0; i < G_N_ELEMENTS (oss_output_priority); i++) {
            GList *item = g_list_find_custom ((GList *) controls,
                                              GINT_TO_POINTER (oss_output_priority[i]),
                                              compare_stream_control_devnum);
            if (item == NULL)
                continue;

            oss_stream_set_default_control (device->priv->output,
                                            OSS_STREAM_CONTROL (item->data));
            break;
        }
    }

    if (device->priv->recmask != 0)
        read_mixer_switch (device);

    /* See if we can use the modify_counter field to optimize polling */
#ifdef SOUND_MIXER_INFO
    do {
        struct mixer_info info;
        gint   ret;

        ret = ioctl (device->priv->fd, SOUND_MIXER_INFO, &info);
        if (ret == 0) {
            device->priv->poll_counter = info.modify_counter;
            device->priv->poll_use_counter = TRUE;
        }
    } while (0);
#endif

    /*
     * Use a polling strategy inspired by KMix:
     *
     * Poll for changes with the OSS_POLL_TIMEOUT_NORMAL interval, when we
     * encounter a change in modify_counter, decrease the interval to
     * OSS_POLL_TIMEOUT_RAPID for a few seconds to allow for smoother
     * adjustments, for example when user drags a slider.
     *
     * This way is not used on systems which don't support the modify_counter
     * field, because there is no way to find out whether anything has
     * changed and therefore when to start the rapid polling.
     */
    device->priv->poll_tag = create_poll_source (device, OSS_POLL_NORMAL);
}

const gchar *
oss_device_get_path (OssDevice *device)
{
    g_return_val_if_fail (OSS_IS_DEVICE (device), NULL);

    return device->priv->path;
}

OssStream *
oss_device_get_input_stream (OssDevice *device)
{
    g_return_val_if_fail (OSS_IS_DEVICE (device), NULL);

    return device->priv->input;
}

OssStream *
oss_device_get_output_stream (OssDevice *device)
{
    g_return_val_if_fail (OSS_IS_DEVICE (device), NULL);

    return device->priv->output;
}

static const GList *
oss_device_list_streams (MateMixerDevice *mmd)
{
    OssDevice *device;

    g_return_val_if_fail (OSS_IS_DEVICE (mmd), NULL);

    device = OSS_DEVICE (mmd);

    if (device->priv->streams == NULL) {
        if (device->priv->output != NULL)
            device->priv->streams = g_list_prepend (device->priv->streams,
                                                    g_object_ref (device->priv->output));
        if (device->priv->input != NULL)
            device->priv->streams = g_list_prepend (device->priv->streams,
                                                    g_object_ref (device->priv->input));
    }
    return device->priv->streams;
}

#define OSS_MASK_HAS_DEVICE(mask,i) ((gboolean) (((mask) & (1 << (i))) > 0))

static void
read_mixer_devices (OssDevice *device)
{
    OssStreamControl *control;
    const gchar      *name;
    guint             i;

    name = mate_mixer_device_get_name (MATE_MIXER_DEVICE (device));

    for (i = 0; i < OSS_N_DEVICES; i++) {
        OssStream *stream;
        gboolean   stereo;

        /* Skip unavailable controls */
        if (OSS_MASK_HAS_DEVICE (device->priv->devmask, i) == FALSE)
            continue;

        if (OSS_MASK_HAS_DEVICE (device->priv->recmask, i) == TRUE)
            stream = device->priv->input;
        else
            stream = device->priv->output;

        stereo  = OSS_MASK_HAS_DEVICE (device->priv->stereodevs, i);
        control = oss_stream_control_new (oss_devices[i].name, gettext (oss_devices[i].label),
                                          oss_devices[i].role,
                                          stream,
                                          device->priv->fd,
                                          i,
                                          stereo);

        if (oss_stream_has_controls (stream) == FALSE) {
            const gchar *name =
                mate_mixer_stream_get_name (MATE_MIXER_STREAM (stream));

            free_stream_list (device);

            /* Pretend the stream has just been created now that we are adding
             * the first control */
            g_signal_emit_by_name (G_OBJECT (device),
                                   "stream-added",
                                   name);
        }

        g_debug ("Adding device %s control %s",
                 name,
                 mate_mixer_stream_control_get_name (MATE_MIXER_STREAM_CONTROL (control)));

        oss_stream_add_control (stream, control);
        oss_stream_control_load (control);

        g_object_unref (control);
    }
}

static void
read_mixer_switch (OssDevice *device)
{
    GList *options = NULL;
    guint  i;

    for (i = 0; i < OSS_N_DEVICES; i++) {
        OssSwitchOption *option;

        if (OSS_MASK_HAS_DEVICE (device->priv->recmask, i) == FALSE)
            continue;

        option = oss_switch_option_new (oss_devices[i].name,
                                        gettext (oss_devices[i].label),
                                        oss_devices[i].icon,
                                        i);
        options = g_list_prepend (options, option);
    }

    if G_LIKELY (options != NULL)
        oss_stream_set_switch_data (device->priv->input,
                                    device->priv->fd,
                                    options);
}

static gboolean
poll_mixer (OssDevice *device)
{
    gboolean load = TRUE;

    if G_UNLIKELY (device->priv->fd == -1)
        return G_SOURCE_REMOVE;

#ifdef SOUND_MIXER_INFO
    if (device->priv->poll_use_counter == TRUE) {
        gint   ret;
        struct mixer_info info;

        /*
         * The modify_counter field increases each time a change happens on
         * the device.
         *
         * If this ioctl() works, we use the field to only poll the controls
         * if a change actually occured and we can also adjust the poll interval.
         *
         * This works well at least on Linux, NetBSD and OpenBSD. This call is
         * not supported on FreeBSD as of version 10.
         *
         * The call is also used to detect unplugged devices early, when not
         * supported, the unplug is still caught in the backend.
         */
        ret = ioctl (device->priv->fd, SOUND_MIXER_INFO, &info);
        if (ret == -1) {
            if (errno == EINTR)
                return G_SOURCE_CONTINUE;

            oss_device_close (device);
            return G_SOURCE_REMOVE;
        }

        if (device->priv->poll_counter < info.modify_counter) {
            device->priv->poll_counter = info.modify_counter;
        } else {
            load = FALSE;
        }
    }
#endif

    if (load == TRUE) {
        oss_stream_load (device->priv->input);
        oss_stream_load (device->priv->output);

        if (device->priv->poll_use_counter == TRUE &&
            device->priv->poll_mode == OSS_POLL_NORMAL) {
            /* Create a new rapid source */
            device->priv->poll_tag = create_poll_source (device, OSS_POLL_RAPID);

            /* Also create another source to restore the poll interval to the
             * original state */
            device->priv->poll_tag_restore = create_poll_restore_source (device);

            device->priv->poll_mode = OSS_POLL_RAPID;
            return G_SOURCE_REMOVE;
        }
    }
    return G_SOURCE_CONTINUE;
}

static gboolean
poll_mixer_restore (OssDevice *device)
{
    if G_LIKELY (device->priv->poll_mode == OSS_POLL_RAPID) {
        /* Remove the current rapid source */
        g_source_remove (device->priv->poll_tag);

        device->priv->poll_tag  = create_poll_source (device, OSS_POLL_NORMAL);
        device->priv->poll_mode = OSS_POLL_NORMAL;
    }

    /* Remove the tag for this function as it is only called once, the tag
     * is only kept so we can remove it in the case the device is closed */
    device->priv->poll_tag_restore = 0;

    return G_SOURCE_REMOVE;
}

static guint
create_poll_source (OssDevice *device, OssPollMode mode)
{
    GSource *source;
    guint    timeout;
    guint    tag;

    switch (mode) {
    case OSS_POLL_NORMAL:
        timeout = OSS_POLL_TIMEOUT_NORMAL;
        break;
    case OSS_POLL_RAPID:
        timeout = OSS_POLL_TIMEOUT_RAPID;
        break;
    default:
        g_warn_if_reached ();
        return 0;
    }

    source = g_timeout_source_new (timeout);
    g_source_set_callback (source,
                           (GSourceFunc) poll_mixer,
                           device,
                           NULL);

    tag = g_source_attach (source, g_main_context_get_thread_default ());
    g_source_unref (source);

    return tag;
}

static guint
create_poll_restore_source (OssDevice *device)
{
    GSource *source;
    guint    tag;

    source = g_timeout_source_new (OSS_POLL_TIMEOUT_RESTORE);
    g_source_set_callback (source,
                           (GSourceFunc) poll_mixer_restore,
                           device,
                           NULL);

    tag = g_source_attach (source, g_main_context_get_thread_default ());
    g_source_unref (source);

    return tag;
}

static void
free_stream_list (OssDevice *device)
{
    /* This function is called each time the stream list changes */
    if (device->priv->streams == NULL)
        return;

    g_list_free_full (device->priv->streams, g_object_unref);

    device->priv->streams = NULL;
}

static gint
compare_stream_control_devnum (gconstpointer a, gconstpointer b)
{
    OssStreamControl *control = OSS_STREAM_CONTROL (a);
    guint             devnum  = GPOINTER_TO_INT (b);

    return !(oss_stream_control_get_devnum (control) == devnum);
}