~ubuntu-branches/ubuntu/precise/gnome-control-center/precise-updates

« back to all changes in this revision

Viewing changes to panels/sound/gvc-mixer-card.c

Tags: upstream-3.0.1.1
ImportĀ upstreamĀ versionĀ 3.0.1.1

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
 * Copyright (C) 2009 Bastien Nocera
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 *
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <stdlib.h>
 
25
#include <stdio.h>
 
26
#include <unistd.h>
 
27
 
 
28
#include <glib.h>
 
29
#include <glib/gi18n-lib.h>
 
30
 
 
31
#include <pulse/pulseaudio.h>
 
32
 
 
33
#include "gvc-mixer-card.h"
 
34
#include "gvc-mixer-card-private.h"
 
35
 
 
36
#define GVC_MIXER_CARD_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GVC_TYPE_MIXER_CARD, GvcMixerCardPrivate))
 
37
 
 
38
static guint32 card_serial = 1;
 
39
 
 
40
struct GvcMixerCardPrivate
 
41
{
 
42
        pa_context    *pa_context;
 
43
        guint          id;
 
44
        guint          index;
 
45
        char          *name;
 
46
        char          *icon_name;
 
47
        char          *profile;
 
48
        char          *target_profile;
 
49
        char          *human_profile;
 
50
        GList         *profiles;
 
51
        pa_operation  *profile_op;
 
52
};
 
53
 
 
54
enum
 
55
{
 
56
        PROP_0,
 
57
        PROP_ID,
 
58
        PROP_PA_CONTEXT,
 
59
        PROP_INDEX,
 
60
        PROP_NAME,
 
61
        PROP_ICON_NAME,
 
62
        PROP_PROFILE,
 
63
        PROP_HUMAN_PROFILE,
 
64
};
 
65
 
 
66
static void     gvc_mixer_card_class_init (GvcMixerCardClass *klass);
 
67
static void     gvc_mixer_card_init       (GvcMixerCard      *mixer_card);
 
68
static void     gvc_mixer_card_finalize   (GObject            *object);
 
69
 
 
70
G_DEFINE_TYPE (GvcMixerCard, gvc_mixer_card, G_TYPE_OBJECT)
 
71
 
 
72
static guint32
 
73
get_next_card_serial (void)
 
74
{
 
75
        guint32 serial;
 
76
 
 
77
        serial = card_serial++;
 
78
 
 
79
        if ((gint32)card_serial < 0) {
 
80
                card_serial = 1;
 
81
        }
 
82
 
 
83
        return serial;
 
84
}
 
85
 
 
86
pa_context *
 
87
gvc_mixer_card_get_pa_context (GvcMixerCard *card)
 
88
{
 
89
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), 0);
 
90
        return card->priv->pa_context;
 
91
}
 
92
 
 
93
guint
 
94
gvc_mixer_card_get_index (GvcMixerCard *card)
 
95
{
 
96
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), 0);
 
97
        return card->priv->index;
 
98
}
 
99
 
 
100
guint
 
101
gvc_mixer_card_get_id (GvcMixerCard *card)
 
102
{
 
103
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), 0);
 
104
        return card->priv->id;
 
105
}
 
106
 
 
107
const char *
 
108
gvc_mixer_card_get_name (GvcMixerCard *card)
 
109
{
 
110
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
111
        return card->priv->name;
 
112
}
 
113
 
 
114
gboolean
 
115
gvc_mixer_card_set_name (GvcMixerCard *card,
 
116
                         const char     *name)
 
117
{
 
118
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
119
 
 
120
        g_free (card->priv->name);
 
121
        card->priv->name = g_strdup (name);
 
122
        g_object_notify (G_OBJECT (card), "name");
 
123
 
 
124
        return TRUE;
 
125
}
 
126
 
 
127
const char *
 
128
gvc_mixer_card_get_icon_name (GvcMixerCard *card)
 
129
{
 
130
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
131
        return card->priv->icon_name;
 
132
}
 
133
 
 
134
gboolean
 
135
gvc_mixer_card_set_icon_name (GvcMixerCard *card,
 
136
                              const char     *icon_name)
 
137
{
 
138
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
139
 
 
140
        g_free (card->priv->icon_name);
 
141
        card->priv->icon_name = g_strdup (icon_name);
 
142
        g_object_notify (G_OBJECT (card), "icon-name");
 
143
 
 
144
        return TRUE;
 
145
}
 
146
 
 
147
/**
 
148
 * gvc_mixer_card_get_profile: (skip)
 
149
 *
 
150
 * @card:
 
151
 *
 
152
 * Returns:
 
153
 */
 
154
GvcMixerCardProfile *
 
155
gvc_mixer_card_get_profile (GvcMixerCard *card)
 
156
{
 
157
        GList *l;
 
158
 
 
159
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
160
        g_return_val_if_fail (card->priv->profiles != NULL, NULL);
 
161
 
 
162
        for (l = card->priv->profiles; l != NULL; l = l->next) {
 
163
                GvcMixerCardProfile *p = l->data;
 
164
                if (g_str_equal (card->priv->profile, p->profile)) {
 
165
                        return p;
 
166
                }
 
167
        }
 
168
 
 
169
        g_assert_not_reached ();
 
170
 
 
171
        return NULL;
 
172
}
 
173
 
 
174
gboolean
 
175
gvc_mixer_card_set_profile (GvcMixerCard *card,
 
176
                            const char     *profile)
 
177
{
 
178
        GList *l;
 
179
 
 
180
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
181
        g_return_val_if_fail (card->priv->profiles != NULL, FALSE);
 
182
 
 
183
        g_free (card->priv->profile);
 
184
        card->priv->profile = g_strdup (profile);
 
185
 
 
186
        g_free (card->priv->human_profile);
 
187
        card->priv->human_profile = NULL;
 
188
 
 
189
        for (l = card->priv->profiles; l != NULL; l = l->next) {
 
190
                GvcMixerCardProfile *p = l->data;
 
191
                if (g_str_equal (card->priv->profile, p->profile)) {
 
192
                        card->priv->human_profile = g_strdup (p->human_profile);
 
193
                        break;
 
194
                }
 
195
        }
 
196
 
 
197
        g_object_notify (G_OBJECT (card), "profile");
 
198
 
 
199
        return TRUE;
 
200
}
 
201
 
 
202
static void
 
203
_pa_context_set_card_profile_by_index_cb (pa_context                       *context,
 
204
                                          int                               success,
 
205
                                          void                             *userdata)
 
206
{
 
207
        GvcMixerCard *card = GVC_MIXER_CARD (userdata);
 
208
 
 
209
        g_assert (card->priv->target_profile);
 
210
 
 
211
        if (success > 0) {
 
212
                gvc_mixer_card_set_profile (card, card->priv->target_profile);
 
213
        } else {
 
214
                g_debug ("Failed to switch profile on '%s' from '%s' to '%s'",
 
215
                         card->priv->name,
 
216
                         card->priv->profile,
 
217
                         card->priv->target_profile);
 
218
        }
 
219
        g_free (card->priv->target_profile);
 
220
        card->priv->target_profile = NULL;
 
221
 
 
222
        pa_operation_unref (card->priv->profile_op);
 
223
        card->priv->profile_op = NULL;
 
224
}
 
225
 
 
226
gboolean
 
227
gvc_mixer_card_change_profile (GvcMixerCard *card,
 
228
                               const char *profile)
 
229
{
 
230
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
231
        g_return_val_if_fail (card->priv->profiles != NULL, FALSE);
 
232
 
 
233
        /* Same profile, or already requested? */
 
234
        if (g_strcmp0 (card->priv->profile, profile) == 0)
 
235
                return TRUE;
 
236
        if (g_strcmp0 (profile, card->priv->target_profile) == 0)
 
237
                return TRUE;
 
238
        if (card->priv->profile_op != NULL) {
 
239
                pa_operation_cancel (card->priv->profile_op);
 
240
                pa_operation_unref (card->priv->profile_op);
 
241
                card->priv->profile_op = NULL;
 
242
        }
 
243
 
 
244
        if (card->priv->profile != NULL) {
 
245
                g_free (card->priv->target_profile);
 
246
                card->priv->target_profile = g_strdup (profile);
 
247
 
 
248
                card->priv->profile_op = pa_context_set_card_profile_by_index (card->priv->pa_context,
 
249
                                                                               card->priv->index,
 
250
                                                                               card->priv->target_profile,
 
251
                                                                               _pa_context_set_card_profile_by_index_cb,
 
252
                                                                               card);
 
253
 
 
254
                if (card->priv->profile_op == NULL) {
 
255
                        g_warning ("pa_context_set_card_profile_by_index() failed");
 
256
                        return FALSE;
 
257
                }
 
258
        } else {
 
259
                g_assert (card->priv->human_profile == NULL);
 
260
                card->priv->profile = g_strdup (profile);
 
261
        }
 
262
 
 
263
        return TRUE;
 
264
}
 
265
 
 
266
const GList *
 
267
gvc_mixer_card_get_profiles (GvcMixerCard *card)
 
268
{
 
269
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
270
        return card->priv->profiles;
 
271
}
 
272
 
 
273
static int
 
274
sort_profiles (GvcMixerCardProfile *a,
 
275
               GvcMixerCardProfile *b)
 
276
{
 
277
        if (a->priority == b->priority)
 
278
                return 0;
 
279
        if (a->priority > b->priority)
 
280
                return 1;
 
281
        return -1;
 
282
}
 
283
 
 
284
gboolean
 
285
gvc_mixer_card_set_profiles (GvcMixerCard *card,
 
286
                             GList        *profiles)
 
287
{
 
288
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
289
        g_return_val_if_fail (card->priv->profiles == NULL, FALSE);
 
290
 
 
291
        card->priv->profiles = g_list_sort (profiles, (GCompareFunc) sort_profiles);
 
292
 
 
293
        return TRUE;
 
294
}
 
295
 
 
296
static void
 
297
gvc_mixer_card_set_property (GObject       *object,
 
298
                             guint          prop_id,
 
299
                             const GValue  *value,
 
300
                             GParamSpec    *pspec)
 
301
{
 
302
        GvcMixerCard *self = GVC_MIXER_CARD (object);
 
303
 
 
304
        switch (prop_id) {
 
305
        case PROP_PA_CONTEXT:
 
306
                self->priv->pa_context = g_value_get_pointer (value);
 
307
                break;
 
308
        case PROP_INDEX:
 
309
                self->priv->index = g_value_get_ulong (value);
 
310
                break;
 
311
        case PROP_ID:
 
312
                self->priv->id = g_value_get_ulong (value);
 
313
                break;
 
314
        case PROP_NAME:
 
315
                gvc_mixer_card_set_name (self, g_value_get_string (value));
 
316
                break;
 
317
        case PROP_ICON_NAME:
 
318
                gvc_mixer_card_set_icon_name (self, g_value_get_string (value));
 
319
                break;
 
320
        case PROP_PROFILE:
 
321
                gvc_mixer_card_set_profile (self, g_value_get_string (value));
 
322
                break;
 
323
        default:
 
324
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
325
                break;
 
326
        }
 
327
}
 
328
 
 
329
static void
 
330
gvc_mixer_card_get_property (GObject     *object,
 
331
                             guint        prop_id,
 
332
                             GValue      *value,
 
333
                             GParamSpec  *pspec)
 
334
{
 
335
        GvcMixerCard *self = GVC_MIXER_CARD (object);
 
336
 
 
337
        switch (prop_id) {
 
338
        case PROP_PA_CONTEXT:
 
339
                g_value_set_pointer (value, self->priv->pa_context);
 
340
                break;
 
341
        case PROP_INDEX:
 
342
                g_value_set_ulong (value, self->priv->index);
 
343
                break;
 
344
        case PROP_ID:
 
345
                g_value_set_ulong (value, self->priv->id);
 
346
                break;
 
347
        case PROP_NAME:
 
348
                g_value_set_string (value, self->priv->name);
 
349
                break;
 
350
        case PROP_ICON_NAME:
 
351
                g_value_set_string (value, self->priv->icon_name);
 
352
                break;
 
353
        case PROP_PROFILE:
 
354
                g_value_set_string (value, self->priv->profile);
 
355
                break;
 
356
        case PROP_HUMAN_PROFILE:
 
357
                g_value_set_string (value, self->priv->human_profile);
 
358
                break;
 
359
        default:
 
360
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
361
                break;
 
362
        }
 
363
}
 
364
 
 
365
static GObject *
 
366
gvc_mixer_card_constructor (GType                  type,
 
367
                            guint                  n_construct_properties,
 
368
                            GObjectConstructParam *construct_params)
 
369
{
 
370
        GObject       *object;
 
371
        GvcMixerCard *self;
 
372
 
 
373
        object = G_OBJECT_CLASS (gvc_mixer_card_parent_class)->constructor (type, n_construct_properties, construct_params);
 
374
 
 
375
        self = GVC_MIXER_CARD (object);
 
376
 
 
377
        self->priv->id = get_next_card_serial ();
 
378
 
 
379
        return object;
 
380
}
 
381
 
 
382
static void
 
383
gvc_mixer_card_class_init (GvcMixerCardClass *klass)
 
384
{
 
385
        GObjectClass   *gobject_class = G_OBJECT_CLASS (klass);
 
386
 
 
387
        gobject_class->constructor = gvc_mixer_card_constructor;
 
388
        gobject_class->finalize = gvc_mixer_card_finalize;
 
389
 
 
390
        gobject_class->set_property = gvc_mixer_card_set_property;
 
391
        gobject_class->get_property = gvc_mixer_card_get_property;
 
392
 
 
393
        g_object_class_install_property (gobject_class,
 
394
                                         PROP_INDEX,
 
395
                                         g_param_spec_ulong ("index",
 
396
                                                             "Index",
 
397
                                                             "The index for this card",
 
398
                                                             0, G_MAXULONG, 0,
 
399
                                                             G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
400
        g_object_class_install_property (gobject_class,
 
401
                                         PROP_ID,
 
402
                                         g_param_spec_ulong ("id",
 
403
                                                             "id",
 
404
                                                             "The id for this card",
 
405
                                                             0, G_MAXULONG, 0,
 
406
                                                             G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
407
        g_object_class_install_property (gobject_class,
 
408
                                         PROP_PA_CONTEXT,
 
409
                                         g_param_spec_pointer ("pa-context",
 
410
                                                               "PulseAudio context",
 
411
                                                               "The PulseAudio context for this card",
 
412
                                                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
413
        g_object_class_install_property (gobject_class,
 
414
                                         PROP_NAME,
 
415
                                         g_param_spec_string ("name",
 
416
                                                              "Name",
 
417
                                                              "Name to display for this card",
 
418
                                                              NULL,
 
419
                                                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
 
420
        g_object_class_install_property (gobject_class,
 
421
                                         PROP_ICON_NAME,
 
422
                                         g_param_spec_string ("icon-name",
 
423
                                                              "Icon Name",
 
424
                                                              "Name of icon to display for this card",
 
425
                                                              NULL,
 
426
                                                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
 
427
        g_object_class_install_property (gobject_class,
 
428
                                         PROP_PROFILE,
 
429
                                         g_param_spec_string ("profile",
 
430
                                                              "Profile",
 
431
                                                              "Name of current profile for this card",
 
432
                                                              NULL,
 
433
                                                              G_PARAM_READWRITE));
 
434
        g_object_class_install_property (gobject_class,
 
435
                                         PROP_HUMAN_PROFILE,
 
436
                                         g_param_spec_string ("human-profile",
 
437
                                                              "Profile (Human readable)",
 
438
                                                              "Name of current profile for this card in human readable form",
 
439
                                                              NULL,
 
440
                                                              G_PARAM_READABLE));
 
441
 
 
442
        g_type_class_add_private (klass, sizeof (GvcMixerCardPrivate));
 
443
}
 
444
 
 
445
static void
 
446
gvc_mixer_card_init (GvcMixerCard *card)
 
447
{
 
448
        card->priv = GVC_MIXER_CARD_GET_PRIVATE (card);
 
449
}
 
450
 
 
451
GvcMixerCard *
 
452
gvc_mixer_card_new (pa_context *context,
 
453
                    guint       index)
 
454
{
 
455
        GObject *object;
 
456
 
 
457
        object = g_object_new (GVC_TYPE_MIXER_CARD,
 
458
                               "index", index,
 
459
                               "pa-context", context,
 
460
                               NULL);
 
461
        return GVC_MIXER_CARD (object);
 
462
}
 
463
 
 
464
static void
 
465
free_profile (GvcMixerCardProfile *p)
 
466
{
 
467
        g_free (p->profile);
 
468
        g_free (p->human_profile);
 
469
        g_free (p->status);
 
470
        g_free (p);
 
471
}
 
472
 
 
473
static void
 
474
gvc_mixer_card_finalize (GObject *object)
 
475
{
 
476
        GvcMixerCard *mixer_card;
 
477
 
 
478
        g_return_if_fail (object != NULL);
 
479
        g_return_if_fail (GVC_IS_MIXER_CARD (object));
 
480
 
 
481
        mixer_card = GVC_MIXER_CARD (object);
 
482
 
 
483
        g_return_if_fail (mixer_card->priv != NULL);
 
484
 
 
485
        g_free (mixer_card->priv->name);
 
486
        mixer_card->priv->name = NULL;
 
487
 
 
488
        g_free (mixer_card->priv->icon_name);
 
489
        mixer_card->priv->icon_name = NULL;
 
490
 
 
491
        g_free (mixer_card->priv->target_profile);
 
492
        mixer_card->priv->target_profile = NULL;
 
493
 
 
494
        g_free (mixer_card->priv->profile);
 
495
        mixer_card->priv->profile = NULL;
 
496
 
 
497
        g_free (mixer_card->priv->human_profile);
 
498
        mixer_card->priv->human_profile = NULL;
 
499
 
 
500
        g_list_foreach (mixer_card->priv->profiles, (GFunc) free_profile, NULL);
 
501
        g_list_free (mixer_card->priv->profiles);
 
502
        mixer_card->priv->profiles = NULL;
 
503
 
 
504
        G_OBJECT_CLASS (gvc_mixer_card_parent_class)->finalize (object);
 
505
}
 
506