~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/sound/gvc-mixer-event-role.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright (C) 2008 William Jon McCann
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 */
 
20
 
 
21
#include "config.h"
 
22
 
 
23
#include <stdlib.h>
 
24
#include <stdio.h>
 
25
#include <unistd.h>
 
26
 
 
27
#include <glib.h>
 
28
#include <glib/gi18n-lib.h>
 
29
 
 
30
#include <pulse/pulseaudio.h>
 
31
#include <pulse/ext-stream-restore.h>
 
32
 
 
33
#include "gvc-mixer-event-role.h"
 
34
#include "gvc-mixer-stream-private.h"
 
35
#include "gvc-channel-map-private.h"
 
36
 
 
37
#define GVC_MIXER_EVENT_ROLE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GVC_TYPE_MIXER_EVENT_ROLE, GvcMixerEventRolePrivate))
 
38
 
 
39
struct GvcMixerEventRolePrivate
 
40
{
 
41
        char          *device;
 
42
};
 
43
 
 
44
enum
 
45
{
 
46
        PROP_0,
 
47
        PROP_DEVICE
 
48
};
 
49
 
 
50
static void     gvc_mixer_event_role_class_init (GvcMixerEventRoleClass *klass);
 
51
static void     gvc_mixer_event_role_init       (GvcMixerEventRole      *mixer_event_role);
 
52
static void     gvc_mixer_event_role_finalize   (GObject            *object);
 
53
 
 
54
G_DEFINE_TYPE (GvcMixerEventRole, gvc_mixer_event_role, GVC_TYPE_MIXER_STREAM)
 
55
 
 
56
static gboolean
 
57
update_settings (GvcMixerEventRole *role,
 
58
                 gboolean           is_muted,
 
59
                 gpointer          *op)
 
60
{
 
61
        pa_operation              *o;
 
62
        const GvcChannelMap       *map;
 
63
        pa_context                *context;
 
64
        pa_ext_stream_restore_info info;
 
65
 
 
66
        map = gvc_mixer_stream_get_channel_map (GVC_MIXER_STREAM(role));
 
67
 
 
68
        info.volume = *gvc_channel_map_get_cvolume(map);
 
69
        info.name = "sink-input-by-media-role:event";
 
70
        info.channel_map = *gvc_channel_map_get_pa_channel_map(map);
 
71
        info.device = role->priv->device;
 
72
        info.mute = is_muted;
 
73
 
 
74
        context = gvc_mixer_stream_get_pa_context (GVC_MIXER_STREAM (role));
 
75
 
 
76
        o = pa_ext_stream_restore_write (context,
 
77
                                         PA_UPDATE_REPLACE,
 
78
                                         &info,
 
79
                                         1,
 
80
                                         TRUE,
 
81
                                         NULL,
 
82
                                         NULL);
 
83
 
 
84
        if (o == NULL) {
 
85
                g_warning ("pa_ext_stream_restore_write() failed");
 
86
                return FALSE;
 
87
        }
 
88
 
 
89
        if (op != NULL)
 
90
                *op = o;
 
91
 
 
92
        return TRUE;
 
93
}
 
94
 
 
95
static gboolean
 
96
gvc_mixer_event_role_push_volume (GvcMixerStream *stream, gpointer *op)
 
97
{
 
98
        return update_settings (GVC_MIXER_EVENT_ROLE (stream),
 
99
                                gvc_mixer_stream_get_is_muted (stream), op);
 
100
}
 
101
 
 
102
static gboolean
 
103
gvc_mixer_event_role_change_is_muted (GvcMixerStream *stream,
 
104
                                      gboolean        is_muted)
 
105
{
 
106
        /* Apply change straight away so that we don't get a race with
 
107
         * gvc_mixer_event_role_push_volume().
 
108
         * See https://bugs.freedesktop.org/show_bug.cgi?id=51413 */
 
109
        gvc_mixer_stream_set_is_muted (stream, is_muted);
 
110
        return update_settings (GVC_MIXER_EVENT_ROLE (stream),
 
111
                                is_muted, NULL);
 
112
}
 
113
 
 
114
static gboolean
 
115
gvc_mixer_event_role_set_device (GvcMixerEventRole *role,
 
116
                                 const char        *device)
 
117
{
 
118
        g_return_val_if_fail (GVC_IS_MIXER_EVENT_ROLE (role), FALSE);
 
119
 
 
120
        g_free (role->priv->device);
 
121
        role->priv->device = g_strdup (device);
 
122
        g_object_notify (G_OBJECT (role), "device");
 
123
 
 
124
        return TRUE;
 
125
}
 
126
 
 
127
static void
 
128
gvc_mixer_event_role_set_property (GObject       *object,
 
129
                                   guint          prop_id,
 
130
                                   const GValue  *value,
 
131
                                   GParamSpec    *pspec)
 
132
{
 
133
        GvcMixerEventRole *self = GVC_MIXER_EVENT_ROLE (object);
 
134
 
 
135
        switch (prop_id) {
 
136
        case PROP_DEVICE:
 
137
                gvc_mixer_event_role_set_device (self, g_value_get_string (value));
 
138
                break;
 
139
        default:
 
140
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
141
                break;
 
142
        }
 
143
}
 
144
 
 
145
static void
 
146
gvc_mixer_event_role_get_property (GObject     *object,
 
147
                                   guint        prop_id,
 
148
                                   GValue      *value,
 
149
                                   GParamSpec  *pspec)
 
150
{
 
151
        GvcMixerEventRole *self = GVC_MIXER_EVENT_ROLE (object);
 
152
 
 
153
        switch (prop_id) {
 
154
        case PROP_DEVICE:
 
155
                g_value_set_string (value, self->priv->device);
 
156
                break;
 
157
        default:
 
158
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
159
                break;
 
160
        }
 
161
}
 
162
 
 
163
static void
 
164
gvc_mixer_event_role_class_init (GvcMixerEventRoleClass *klass)
 
165
{
 
166
        GObjectClass        *object_class = G_OBJECT_CLASS (klass);
 
167
        GvcMixerStreamClass *stream_class = GVC_MIXER_STREAM_CLASS (klass);
 
168
 
 
169
        object_class->finalize = gvc_mixer_event_role_finalize;
 
170
        object_class->set_property = gvc_mixer_event_role_set_property;
 
171
        object_class->get_property = gvc_mixer_event_role_get_property;
 
172
 
 
173
        stream_class->push_volume = gvc_mixer_event_role_push_volume;
 
174
        stream_class->change_is_muted = gvc_mixer_event_role_change_is_muted;
 
175
 
 
176
        g_object_class_install_property (object_class,
 
177
                                         PROP_DEVICE,
 
178
                                         g_param_spec_string ("device",
 
179
                                                              "Device",
 
180
                                                              "Device",
 
181
                                                              NULL,
 
182
                                                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
 
183
 
 
184
        g_type_class_add_private (klass, sizeof (GvcMixerEventRolePrivate));
 
185
}
 
186
 
 
187
static void
 
188
gvc_mixer_event_role_init (GvcMixerEventRole *event_role)
 
189
{
 
190
        event_role->priv = GVC_MIXER_EVENT_ROLE_GET_PRIVATE (event_role);
 
191
 
 
192
}
 
193
 
 
194
static void
 
195
gvc_mixer_event_role_finalize (GObject *object)
 
196
{
 
197
        GvcMixerEventRole *mixer_event_role;
 
198
 
 
199
        g_return_if_fail (object != NULL);
 
200
        g_return_if_fail (GVC_IS_MIXER_EVENT_ROLE (object));
 
201
 
 
202
        mixer_event_role = GVC_MIXER_EVENT_ROLE (object);
 
203
 
 
204
        g_return_if_fail (mixer_event_role->priv != NULL);
 
205
 
 
206
        g_free (mixer_event_role->priv->device);
 
207
 
 
208
        G_OBJECT_CLASS (gvc_mixer_event_role_parent_class)->finalize (object);
 
209
}
 
210
 
 
211
/**
 
212
 * gvc_mixer_event_role_new: (skip)
 
213
 *
 
214
 * @context:
 
215
 * @index:
 
216
 * @channel_map:
 
217
 *
 
218
 * Returns:
 
219
 */
 
220
GvcMixerStream *
 
221
gvc_mixer_event_role_new (pa_context *context,
 
222
                          const char *device,
 
223
                          GvcChannelMap *channel_map)
 
224
{
 
225
        GObject *object;
 
226
 
 
227
        object = g_object_new (GVC_TYPE_MIXER_EVENT_ROLE,
 
228
                               "pa-context", context,
 
229
                               "index", 0,
 
230
                               "device", device,
 
231
                               "channel-map", channel_map,
 
232
                               NULL);
 
233
 
 
234
        return GVC_MIXER_STREAM (object);
 
235
}