summaryrefslogtreecommitdiff
path: root/backends/oss/oss-switch.c
blob: 40527b9830ad06352a776982e0a36cc8a767e15b (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
/*
 * 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-object.h>
#include <libmatemixer/matemixer.h>
#include <libmatemixer/matemixer-private.h>

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

struct _OssSwitchPrivate
{
    gint   fd;
    GList *options;
};

static void oss_switch_class_init (OssSwitchClass *klass);
static void oss_switch_init       (OssSwitch      *swtch);
static void oss_switch_dispose    (GObject        *object);
static void oss_switch_finalize   (GObject        *object);

G_DEFINE_TYPE (OssSwitch, oss_switch, MATE_MIXER_TYPE_STREAM_SWITCH)

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

static const GList *    oss_switch_list_options      (MateMixerSwitch       *mms);

static OssSwitchOption *choose_default_option        (OssSwitch             *swtch);

static void
oss_switch_class_init (OssSwitchClass *klass)
{
    GObjectClass         *object_class;
    MateMixerSwitchClass *switch_class;

    object_class = G_OBJECT_CLASS (klass);
    object_class->dispose  = oss_switch_dispose;
    object_class->finalize = oss_switch_finalize;

    switch_class = MATE_MIXER_SWITCH_CLASS (klass);
    switch_class->set_active_option = oss_switch_set_active_option;
    switch_class->list_options      = oss_switch_list_options;

    g_type_class_add_private (G_OBJECT_CLASS (klass), sizeof (OssSwitchPrivate));
}

static void
oss_switch_init (OssSwitch *swtch)
{
    swtch->priv = G_TYPE_INSTANCE_GET_PRIVATE (swtch,
                                               OSS_TYPE_SWITCH,
                                               OssSwitchPrivate);
}

static void
oss_switch_dispose (GObject *object)
{
    OssSwitch *swtch;

    swtch = OSS_SWITCH (object);

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

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

static void
oss_switch_finalize (GObject *object)
{
    OssSwitch *swtch;

    swtch = OSS_SWITCH (object);

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

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

OssSwitch *
oss_switch_new (OssStream   *stream,
                const gchar *name,
                const gchar *label,
                gint         fd,
                GList       *options)
{
    OssSwitch *swtch;

    g_return_val_if_fail (OSS_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 (fd != -1, NULL);
    g_return_val_if_fail (options != NULL, NULL);

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

    /* Takes ownership of options */
    swtch->priv->fd      = dup (fd);
    swtch->priv->options = options;

    return swtch;
}

void
oss_switch_load (OssSwitch *swtch)
{
    OssSwitchOption *option;
    gint             recsrc;
    gint             ret;

    g_return_if_fail (OSS_IS_SWITCH (swtch));

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

    /* Recsrc contains a bitmask of currently enabled recording sources */
    ret = ioctl (swtch->priv->fd, MIXER_READ (SOUND_MIXER_RECSRC), &recsrc);
    if (ret == -1)
        return;

    if (recsrc != 0) {
        GList *list = swtch->priv->options;

        /* Find out which option is currently selected */
        while (list != NULL) {
            gint devnum;

            option = OSS_SWITCH_OPTION (list->data);
            devnum = oss_switch_option_get_devnum (option);

            if (recsrc & (1 << devnum)) {
                /* It is possible that some hardware might allow and have more recording
                 * sources active at the same time, but we only support one active
                 * source at a time */
                _mate_mixer_switch_set_active_option (MATE_MIXER_SWITCH (swtch),
                                                      MATE_MIXER_SWITCH_OPTION (option));
                return;
            }
            list = list->next;
        }

        g_debug ("Switch %s has an unknown device as the active option",
                 mate_mixer_switch_get_name (MATE_MIXER_SWITCH (swtch)));

        /* OSS shouldn't let a non-record device be selected, let's step in and select
         * something reasonable instead... */
    } else {
         g_debug ("Switch %s has no active device",
                  mate_mixer_switch_get_name (MATE_MIXER_SWITCH (swtch)));

        /* According to the OSS Programmer's Guide, if the recsrc value is 0, the
         * microphone will be selected implicitly.
         * Let's not assume that's true everywhere and select something explicitly... */
    }

    option = choose_default_option (swtch);

    g_debug ("Selecting default device %s as active for switch %s",
             mate_mixer_switch_option_get_name (MATE_MIXER_SWITCH_OPTION (option)),
             mate_mixer_switch_get_name (MATE_MIXER_SWITCH (swtch)));

    if (mate_mixer_switch_set_active_option (MATE_MIXER_SWITCH (swtch),
                                             MATE_MIXER_SWITCH_OPTION (option)) == FALSE) {
        g_debug ("Failed to set the default device, assuming it is selected anyway");

        _mate_mixer_switch_set_active_option (MATE_MIXER_SWITCH (swtch),
                                              MATE_MIXER_SWITCH_OPTION (option));
    }
}

void
oss_switch_close (OssSwitch *swtch)
{
    g_return_if_fail (OSS_IS_SWITCH (swtch));

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

    close (swtch->priv->fd);
    swtch->priv->fd = -1;
}

static gboolean
oss_switch_set_active_option (MateMixerSwitch *mms, MateMixerSwitchOption *mmso)
{
    OssSwitch *swtch;
    gint       ret;
    gint       recsrc;

    g_return_val_if_fail (OSS_IS_SWITCH (mms), FALSE);
    g_return_val_if_fail (OSS_IS_SWITCH_OPTION (mmso), FALSE);

    swtch = OSS_SWITCH (mms);

    if G_UNLIKELY (swtch->priv->fd == -1)
        return FALSE;

    recsrc = 1 << oss_switch_option_get_devnum (OSS_SWITCH_OPTION (mmso));

    ret = ioctl (swtch->priv->fd, MIXER_WRITE (SOUND_MIXER_RECSRC), &recsrc);
    if (ret == -1)
        return FALSE;

    return TRUE;
}

static const GList *
oss_switch_list_options (MateMixerSwitch *mms)
{
    g_return_val_if_fail (OSS_IS_SWITCH (mms), NULL);

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

#define OSS_SWITCH_PREFERRED_DEFAULT_DEVNUM 7 /* Microphone */

static OssSwitchOption *
choose_default_option (OssSwitch *swtch)
{
    GList *list = swtch->priv->options;

    /* Search for the preferred device */
    while (list != NULL) {
        OssSwitchOption *option;
        gint             devnum;

        option = OSS_SWITCH_OPTION (list->data);
        devnum = oss_switch_option_get_devnum (option);

        if (devnum == OSS_SWITCH_PREFERRED_DEFAULT_DEVNUM)
            return option;

        list = list->next;
    }
    /* If the preferred device is not present, use the first available one */
    return OSS_SWITCH_OPTION (swtch->priv->options->data);
}