summaryrefslogtreecommitdiff
path: root/backends/pulse/pulse-source.c
blob: 1c4e01a75c1b68c84b48b1ed55755d6814421f5d (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
/*
 * 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/gi18n.h>
#include <glib-object.h>
#include <libmatemixer/matemixer.h>
#include <libmatemixer/matemixer-private.h>

#include <pulse/pulseaudio.h>

#include "pulse-connection.h"
#include "pulse-device.h"
#include "pulse-monitor.h"
#include "pulse-port.h"
#include "pulse-port-switch.h"
#include "pulse-stream.h"
#include "pulse-source.h"
#include "pulse-source-control.h"
#include "pulse-source-output.h"
#include "pulse-source-switch.h"

struct _PulseSourcePrivate
{
    GHashTable         *outputs;
    GList              *outputs_list;
    PulsePortSwitch    *pswitch;
    GList              *pswitch_list;
    PulseSourceControl *control;
};

static void pulse_source_class_init (PulseSourceClass *klass);
static void pulse_source_init       (PulseSource      *source);
static void pulse_source_dispose    (GObject          *object);
static void pulse_source_finalize   (GObject          *object);

G_DEFINE_TYPE (PulseSource, pulse_source, PULSE_TYPE_STREAM);

static const GList *pulse_source_list_controls (MateMixerStream *mms);
static const GList *pulse_source_list_switches (MateMixerStream *mms);

static void
pulse_source_class_init (PulseSourceClass *klass)
{
    GObjectClass         *object_class;
    MateMixerStreamClass *stream_class;

    object_class = G_OBJECT_CLASS (klass);
    object_class->dispose  = pulse_source_dispose;
    object_class->finalize = pulse_source_finalize;

    stream_class = MATE_MIXER_STREAM_CLASS (klass);
    stream_class->list_controls = pulse_source_list_controls;
    stream_class->list_switches = pulse_source_list_switches;

    g_type_class_add_private (klass, sizeof (PulseSourcePrivate));
}

static void
pulse_source_init (PulseSource *source)
{
    source->priv = G_TYPE_INSTANCE_GET_PRIVATE (source,
                                                PULSE_TYPE_SOURCE,
                                                PulseSourcePrivate);

    source->priv->outputs = g_hash_table_new_full (g_direct_hash,
                                                   g_direct_equal,
                                                   NULL,
                                                   g_object_unref);
}

static void
pulse_source_dispose (GObject *object)
{
    PulseSource *source;

    source = PULSE_SOURCE (object);

    g_hash_table_remove_all (source->priv->outputs);

    g_clear_object (&source->priv->control);
    g_clear_object (&source->priv->pswitch);
    g_clear_pointer (&source->priv->pswitch_list, g_list_free);

    _mate_mixer_clear_object_list (&source->priv->outputs_list);

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

static void
pulse_source_finalize (GObject *object)
{
    PulseSource *source;

    source = PULSE_SOURCE (object);

    g_hash_table_unref (source->priv->outputs);

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

PulseSource *
pulse_source_new (PulseConnection      *connection,
                  const pa_source_info *info,
                  PulseDevice          *device)
{
    PulseSource *source;

    g_return_val_if_fail (PULSE_IS_CONNECTION (connection), NULL);
    g_return_val_if_fail (info != NULL, NULL);
    g_return_val_if_fail (device == NULL || PULSE_IS_DEVICE (device), NULL);

    source = g_object_new (PULSE_TYPE_SOURCE,
                           "name", info->name,
                           "label", info->description,
                           "device", device,
                           "direction", MATE_MIXER_DIRECTION_INPUT,
                           "connection", connection,
                           "index", info->index,
                           NULL);

    source->priv->control = pulse_source_control_new (connection, info, source);

    if (info->n_ports > 0) {
        pa_source_port_info **ports = info->ports;

        /* Create the port switch */
        source->priv->pswitch = pulse_source_switch_new ("port", _("Connector"), source);

        while (*ports != NULL) {
            pa_source_port_info *p = *ports++;
            PulsePort           *port;
            const gchar         *icon = NULL;

            /* A port may include an icon but in PulseAudio sink and source ports
             * the property is not included, for this reason ports are also read from
             * devices where the icons may be present */
            if (device != NULL) {
                port = pulse_device_get_port (PULSE_DEVICE (device), p->name);
                if (port != NULL)
                    icon = mate_mixer_switch_option_get_icon (MATE_MIXER_SWITCH_OPTION (port));
            }

            port = pulse_port_new (p->name,
                                   p->description,
                                   icon,
                                   p->priority);

            pulse_port_switch_add_port (source->priv->pswitch, port);

            if (p == info->active_port)
                pulse_port_switch_set_active_port (source->priv->pswitch, port);
        }
        source->priv->pswitch_list = g_list_prepend (NULL, source->priv->pswitch);

        g_debug ("Created port list for source %s", info->name);
    }

    pulse_source_update (source, info);

    _mate_mixer_stream_set_default_control (MATE_MIXER_STREAM (source),
                                            MATE_MIXER_STREAM_CONTROL (source->priv->control));
    return source;
}

gboolean
pulse_source_add_output (PulseSource *source, const pa_source_output_info *info)
{
    PulseSourceOutput *output;

    g_return_val_if_fail (PULSE_IS_SOURCE (source), FALSE);
    g_return_val_if_fail (info != NULL, FALSE);

    /* This function is used for both creating and refreshing source outputs */
    output = g_hash_table_lookup (source->priv->outputs, GUINT_TO_POINTER (info->index));
    if (output == NULL) {
        PulseConnection *connection;

        connection = pulse_stream_get_connection (PULSE_STREAM (source));
        output = pulse_source_output_new (connection,
                                          info,
                                          source);
        g_hash_table_insert (source->priv->outputs,
                             GUINT_TO_POINTER (info->index),
                             output);

        _mate_mixer_clear_object_list (&source->priv->outputs_list);

        g_signal_emit_by_name (G_OBJECT (source),
                               "control-added",
                               MATE_MIXER_STREAM_CONTROL (output));
        return TRUE;
    }

    pulse_source_output_update (output, info);
    return FALSE;
}

void
pulse_source_remove_output (PulseSource *source, guint32 index)
{
    PulseSourceOutput *output;

    g_return_if_fail (PULSE_IS_SOURCE (source));

    output = g_hash_table_lookup (source->priv->outputs, GUINT_TO_POINTER (index));
    if G_UNLIKELY (output == NULL)
        return;

    g_object_ref (output);
    g_hash_table_remove (source->priv->outputs, GUINT_TO_POINTER (index));

    _mate_mixer_clear_object_list (&source->priv->outputs_list);
    g_signal_emit_by_name (G_OBJECT (source),
                           "control-removed",
                           MATE_MIXER_STREAM_CONTROL (output));
    g_object_unref (output);
}

void
pulse_source_update (PulseSource          *source,
                     const pa_source_info *info)
{
    g_return_if_fail (PULSE_IS_SOURCE (source));
    g_return_if_fail (info != NULL);

    /* The switch doesn't allow being unset, PulseAudio should always include
     * the active port name if the are any ports available */
    if (info->active_port != NULL)
        pulse_port_switch_set_active_port_by_name (source->priv->pswitch,
                                                   info->active_port->name);

    pulse_source_control_update (source->priv->control, info);
}

static const GList *
pulse_source_list_controls (MateMixerStream *mms)
{
    PulseSource *source;

    g_return_val_if_fail (PULSE_IS_SOURCE (mms), NULL);

    source = PULSE_SOURCE (mms);

    if (source->priv->outputs_list == NULL) {
        source->priv->outputs_list = g_hash_table_get_values (source->priv->outputs);
        if (source->priv->outputs_list != NULL)
            g_list_foreach (source->priv->outputs_list, (GFunc) g_object_ref, NULL);

        source->priv->outputs_list = g_list_prepend (source->priv->outputs_list,
                                                     g_object_ref (source->priv->control));
    }
    return source->priv->outputs_list;
}

static const GList *
pulse_source_list_switches (MateMixerStream *mms)
{
    g_return_val_if_fail (PULSE_IS_SOURCE (mms), NULL);

    return PULSE_SOURCE (mms)->priv->pswitch_list;
}