summaryrefslogtreecommitdiff
path: root/backends/oss/oss-device.c
blob: cf517054ab1bfa8abffb6fa12141d4325b49abb0 (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
/*
 * 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 <unistd.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"

#define OSS_DEVICE_ICON "audio-card"

typedef enum
{
    OSS_DEV_ANY,
    OSS_DEV_INPUT,
    OSS_DEV_OUTPUT
} OssDevType;

typedef struct
{
    gchar                     *name;
    gchar                     *label;
    MateMixerStreamControlRole role;
    OssDevType                 type;
} OssDev;

static const OssDev oss_devices[] = {
    { "vol",     N_("Volume"),     MATE_MIXER_STREAM_CONTROL_ROLE_MASTER,  OSS_DEV_OUTPUT },
    { "bass",    N_("Bass"),       MATE_MIXER_STREAM_CONTROL_ROLE_BASS,    OSS_DEV_OUTPUT },
    { "treble",  N_("Treble"),     MATE_MIXER_STREAM_CONTROL_ROLE_TREBLE,  OSS_DEV_OUTPUT },
    { "synth",   N_("Synth"),      MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN, OSS_DEV_INPUT },
    { "pcm",     N_("PCM"),        MATE_MIXER_STREAM_CONTROL_ROLE_PCM,     OSS_DEV_OUTPUT },
    /* 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 },
    { "line",    N_("Line-in"),    MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_INPUT },
    { "mic",     N_("Microphone"), MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_INPUT },
    { "cd",      N_("CD"),         MATE_MIXER_STREAM_CONTROL_ROLE_CD,      OSS_DEV_INPUT },
    /* Recording monitor */
    { "mix",     N_("Mixer"),      MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN, OSS_DEV_OUTPUT },
    { "pcm2",    N_("PCM-2"),      MATE_MIXER_STREAM_CONTROL_ROLE_PCM,     OSS_DEV_OUTPUT },
    /* Recording level (master input) */
    { "rec",     N_("Record"),     MATE_MIXER_STREAM_CONTROL_ROLE_MASTER,  OSS_DEV_INPUT },
    { "igain",   N_("In-gain"),    MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN, OSS_DEV_INPUT },
    { "ogain",   N_("Out-gain"),   MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN, OSS_DEV_OUTPUT },
    { "line1",   N_("Line-1"),     MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_INPUT },
    { "line2",   N_("Line-2"),     MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_INPUT },
    { "line3",   N_("Line-3"),     MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_INPUT },
    { "dig1",    N_("Digital-1"),  MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_ANY },
    { "dig2",    N_("Digital-2"),  MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_ANY },
    { "dig3",    N_("Digital-3"),  MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_ANY },
    { "phin",    N_("Phone-in"),   MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_INPUT },
    { "phout",   N_("Phone-out"),  MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_OUTPUT },
    { "video",   N_("Video"),      MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_INPUT },
    { "radio",   N_("Radio"),      MATE_MIXER_STREAM_CONTROL_ROLE_PORT,    OSS_DEV_INPUT },
    { "monitor", N_("Monitor"),    MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN, OSS_DEV_OUTPUT },
    { "depth",   N_("3D-depth"),   MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN, OSS_DEV_OUTPUT },
    { "center",  N_("3D-center"),  MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN, OSS_DEV_OUTPUT },
    { "midi",    N_("MIDI"),       MATE_MIXER_STREAM_CONTROL_ROLE_UNKNOWN, OSS_DEV_INPUT }
};

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

struct _OssDevicePrivate
{
    gint       fd;
    gchar     *path;
    gint       devmask;
    gint       stereodevs;
    gint       recmask;
    gint       recsrc;
    OssStream *input;
    OssStream *output;
};

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 GList *  oss_device_list_streams    (MateMixerDevice  *device);

static gboolean read_mixer_devices         (OssDevice        *device);

static gboolean set_stream_default_control (OssStream        *stream,
                                            OssStreamControl *control,
                                            gboolean          force);

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;

    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);

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

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

    close (device->priv->fd);

    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_STREAM_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_STREAM_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 != 0)
        goto fail;

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

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

    /* The recording source mask may change at any time, here we just read
     * the initial value */
    ret = ioctl (device->priv->fd,
                 MIXER_READ (SOUND_MIXER_RECSRC),
                 &device->priv->recsrc);
    if (ret < 0)
        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_load (OssDevice *device)
{
    MateMixerStreamControl *control;

    g_return_val_if_fail (OSS_IS_DEVICE (device), FALSE);

    read_mixer_devices (device);

    control = mate_mixer_stream_get_default_control (MATE_MIXER_STREAM (device->priv->input));
    if (control == NULL) {
        // XXX pick something
    }

    if (control != NULL)
        g_debug ("Default input stream control is %s",
                 mate_mixer_stream_control_get_label (control));

    control = mate_mixer_stream_get_default_control (MATE_MIXER_STREAM (device->priv->output));
    if (control == NULL) {
        // XXX pick something
    }

    if (control != NULL)
        g_debug ("Default output stream control is %s",
                 mate_mixer_stream_control_get_label (control));

    return TRUE;
}

gint
oss_device_get_fd (OssDevice *device)
{
    g_return_val_if_fail (OSS_IS_DEVICE (device), -1);

    return device->priv->fd;
}

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 GList *
oss_device_list_streams (MateMixerDevice *mmd)
{
    OssDevice *device;
    GList     *list = NULL;

    g_return_val_if_fail (OSS_IS_DEVICE (mmd), NULL);

    device = OSS_DEVICE (mmd);

    if (device->priv->output != NULL)
        list = g_list_prepend (list, g_object_ref (device->priv->output));
    if (device->priv->input != NULL)
        list = g_list_prepend (list, g_object_ref (device->priv->input));

    return list;
}

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

static gboolean
read_mixer_devices (OssDevice *device)
{
    gint i;

    for (i = 0; i < OSS_N_DEVICES; i++) {
        OssStreamControl *control;
        gboolean          input = FALSE;

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

        if (oss_devices[i].type == OSS_DEV_ANY) {
            input = OSS_MASK_HAS_DEVICE (device->priv->recmask, i);
        }
        else if (oss_devices[i].type == OSS_DEV_INPUT) {
            input = TRUE;
        }

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

        if (input == TRUE) {
            oss_stream_add_control (OSS_STREAM (device->priv->input), control);

            if (i == SOUND_MIXER_RECLEV || i == SOUND_MIXER_IGAIN) {
                if (i == SOUND_MIXER_RECLEV)
                    set_stream_default_control (OSS_STREAM (device->priv->input),
                                                control,
                                                TRUE);
                else
                    set_stream_default_control (OSS_STREAM (device->priv->input),
                                                control,
                                                FALSE);
            }
        } else {
            oss_stream_add_control (OSS_STREAM (device->priv->output), control);

            if (i == SOUND_MIXER_VOLUME || i == SOUND_MIXER_PCM) {
                if (i == SOUND_MIXER_VOLUME)
                    set_stream_default_control (OSS_STREAM (device->priv->output),
                                                control,
                                                TRUE);
                else
                    set_stream_default_control (OSS_STREAM (device->priv->output),
                                                control,
                                                FALSE);
            }
        }

        g_debug ("Added control %s",
                 mate_mixer_stream_control_get_label (MATE_MIXER_STREAM_CONTROL (control)));

        oss_stream_control_update (control);
    }
    return TRUE;
}

static gboolean
set_stream_default_control (OssStream *stream, OssStreamControl *control, gboolean force)
{
    MateMixerStreamControl *current;

    current = mate_mixer_stream_get_default_control (MATE_MIXER_STREAM (stream));
    if (current == NULL || force == TRUE) {
        oss_stream_set_default_control (stream, OSS_STREAM_CONTROL (control));
        return TRUE;
    }
    return FALSE;
}