summaryrefslogtreecommitdiff
path: root/backends/pulse/pulse-port-switch.c
blob: 46871a0e09f3f1c7172651cddf0d56b3f0cf074e (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
/*
 * 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 <string.h>
#include <glib.h>
#include <glib-object.h>

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

#include "pulse-connection.h"
#include "pulse-port.h"
#include "pulse-port-switch.h"
#include "pulse-stream.h"

struct _PulsePortSwitchPrivate
{
    GList *ports;
};

static void pulse_port_switch_dispose      (GObject                 *object);

G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (PulsePortSwitch, pulse_port_switch, MATE_MIXER_TYPE_STREAM_SWITCH)

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

static const GList *pulse_port_switch_list_options      (MateMixerSwitch       *mms);

static gint         compare_ports                       (gconstpointer          a,
                                                         gconstpointer          b);
static gint         compare_port_name                   (gconstpointer          a,
                                                         gconstpointer          b);

static void
pulse_port_switch_class_init (PulsePortSwitchClass *klass)
{
    GObjectClass         *object_class;
    MateMixerSwitchClass *switch_class;

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

    switch_class = MATE_MIXER_SWITCH_CLASS (klass);
    switch_class->set_active_option = pulse_port_switch_set_active_option;
    switch_class->list_options      = pulse_port_switch_list_options;
}

static void
pulse_port_switch_init (PulsePortSwitch *swtch)
{
    swtch->priv = pulse_port_switch_get_instance_private (swtch);
}

static void
pulse_port_switch_dispose (GObject *object)
{
    PulsePortSwitch *swtch;

    swtch = PULSE_PORT_SWITCH (object);

    if (swtch->priv->ports != NULL) {
        g_list_free_full (swtch->priv->ports, g_object_unref);
        swtch->priv->ports = NULL;
    }
    G_OBJECT_CLASS (pulse_port_switch_parent_class)->dispose (object);
}

PulseStream *
pulse_port_switch_get_stream (PulsePortSwitch *swtch)
{
    g_return_val_if_fail (PULSE_IS_PORT_SWITCH (swtch), NULL);

    return PULSE_STREAM (mate_mixer_stream_switch_get_stream (MATE_MIXER_STREAM_SWITCH (swtch)));
}

void
pulse_port_switch_add_port (PulsePortSwitch *swtch, PulsePort *port)
{
    g_return_if_fail (PULSE_IS_PORT_SWITCH (swtch));
    g_return_if_fail (PULSE_IS_PORT (port));

    swtch->priv->ports = g_list_insert_sorted (swtch->priv->ports,
                                               port,
                                               compare_ports);
}

void
pulse_port_switch_set_active_port (PulsePortSwitch *swtch, PulsePort *port)
{
    g_return_if_fail (PULSE_IS_PORT_SWITCH (swtch));
    g_return_if_fail (PULSE_IS_PORT (port));

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

void
pulse_port_switch_set_active_port_by_name (PulsePortSwitch *swtch, const gchar *name)
{
    GList *item;

    g_return_if_fail (PULSE_IS_PORT_SWITCH (swtch));
    g_return_if_fail (name != NULL);

    item = g_list_find_custom (swtch->priv->ports, name, compare_port_name);
    if G_UNLIKELY (item == NULL) {
        g_debug ("Invalid switch port name %s", name);
        return;
    }
    pulse_port_switch_set_active_port (swtch, PULSE_PORT (item->data));
}

static gboolean
pulse_port_switch_set_active_option (MateMixerSwitch *mms, MateMixerSwitchOption *mmso)
{
    PulsePortSwitchClass *klass;

    g_return_val_if_fail (PULSE_IS_PORT_SWITCH (mms), FALSE);
    g_return_val_if_fail (PULSE_IS_PORT (mmso), FALSE);

    klass = PULSE_PORT_SWITCH_GET_CLASS (PULSE_PORT_SWITCH (mms));

    return klass->set_active_port (PULSE_PORT_SWITCH (mms),
                                   PULSE_PORT (mmso));
}

static const GList *
pulse_port_switch_list_options (MateMixerSwitch *swtch)
{
    g_return_val_if_fail (PULSE_IS_PORT_SWITCH (swtch), NULL);

    return PULSE_PORT_SWITCH (swtch)->priv->ports;
}

static gint
compare_ports (gconstpointer a, gconstpointer b)
{
    return pulse_port_get_priority (PULSE_PORT (b)) -
           pulse_port_get_priority (PULSE_PORT (a));
}

static gint
compare_port_name (gconstpointer a, gconstpointer b)
{
    PulsePort   *port = PULSE_PORT (a);
    const gchar *name = (const gchar *) b;

    return strcmp (pulse_port_get_name (port), name);
}