~cjcurran/gnome-control-center/fix-profiles

« back to all changes in this revision

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

  • Committer: Conor Curran
  • Date: 2012-02-02 23:14:14 UTC
  • Revision ID: conor.curran@canonical.com-20120202231414-xkpgkpevzwgdorq5
make new panel for sound redesign

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
        GList         *ports;        
 
53
};
 
54
 
 
55
enum
 
56
{
 
57
        PROP_0,
 
58
        PROP_ID,
 
59
        PROP_PA_CONTEXT,
 
60
        PROP_INDEX,
 
61
        PROP_NAME,
 
62
        PROP_ICON_NAME,
 
63
        PROP_PROFILE,
 
64
        PROP_HUMAN_PROFILE,
 
65
};
 
66
 
 
67
static void     gvc_mixer_card_class_init (GvcMixerCardClass *klass);
 
68
static void     gvc_mixer_card_init       (GvcMixerCard      *mixer_card);
 
69
static void     gvc_mixer_card_finalize   (GObject            *object);
 
70
 
 
71
G_DEFINE_TYPE (GvcMixerCard, gvc_mixer_card, G_TYPE_OBJECT)
 
72
 
 
73
static guint32
 
74
get_next_card_serial (void)
 
75
{
 
76
        guint32 serial;
 
77
 
 
78
        serial = card_serial++;
 
79
 
 
80
        if ((gint32)card_serial < 0) {
 
81
                card_serial = 1;
 
82
        }
 
83
 
 
84
        return serial;
 
85
}
 
86
 
 
87
pa_context *
 
88
gvc_mixer_card_get_pa_context (GvcMixerCard *card)
 
89
{
 
90
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), 0);
 
91
        return card->priv->pa_context;
 
92
}
 
93
 
 
94
guint
 
95
gvc_mixer_card_get_index (GvcMixerCard *card)
 
96
{
 
97
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), 0);
 
98
        return card->priv->index;
 
99
}
 
100
 
 
101
guint
 
102
gvc_mixer_card_get_id (GvcMixerCard *card)
 
103
{
 
104
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), 0);
 
105
        return card->priv->id;
 
106
}
 
107
 
 
108
const char *
 
109
gvc_mixer_card_get_name (GvcMixerCard *card)
 
110
{
 
111
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
112
        return card->priv->name;
 
113
}
 
114
 
 
115
gboolean
 
116
gvc_mixer_card_set_name (GvcMixerCard *card,
 
117
                         const char     *name)
 
118
{
 
119
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
120
 
 
121
        g_free (card->priv->name);
 
122
        card->priv->name = g_strdup (name);
 
123
        g_object_notify (G_OBJECT (card), "name");
 
124
 
 
125
        return TRUE;
 
126
}
 
127
 
 
128
const char *
 
129
gvc_mixer_card_get_icon_name (GvcMixerCard *card)
 
130
{
 
131
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
132
        return card->priv->icon_name;
 
133
}
 
134
 
 
135
gboolean
 
136
gvc_mixer_card_set_icon_name (GvcMixerCard *card,
 
137
                              const char     *icon_name)
 
138
{
 
139
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
140
 
 
141
        g_free (card->priv->icon_name);
 
142
        card->priv->icon_name = g_strdup (icon_name);
 
143
        g_object_notify (G_OBJECT (card), "icon-name");
 
144
 
 
145
        return TRUE;
 
146
}
 
147
 
 
148
/**
 
149
 * gvc_mixer_card_get_profile: (skip)
 
150
 *
 
151
 * @card:
 
152
 *
 
153
 * Returns:
 
154
 */
 
155
GvcMixerCardProfile *
 
156
gvc_mixer_card_get_profile (GvcMixerCard *card)
 
157
{
 
158
        GList *l;
 
159
 
 
160
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
161
        g_return_val_if_fail (card->priv->profiles != NULL, NULL);
 
162
 
 
163
        for (l = card->priv->profiles; l != NULL; l = l->next) {
 
164
                GvcMixerCardProfile *p = l->data;
 
165
                if (g_str_equal (card->priv->profile, p->profile)) {
 
166
                        return p;
 
167
                }
 
168
        }
 
169
 
 
170
        g_assert_not_reached ();
 
171
 
 
172
        return NULL;
 
173
}
 
174
 
 
175
gboolean
 
176
gvc_mixer_card_set_profile (GvcMixerCard *card,
 
177
                            const char     *profile)
 
178
{
 
179
        GList *l;
 
180
 
 
181
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
182
        g_return_val_if_fail (card->priv->profiles != NULL, FALSE);
 
183
 
 
184
        g_free (card->priv->profile);
 
185
        card->priv->profile = g_strdup (profile);
 
186
 
 
187
        g_free (card->priv->human_profile);
 
188
        card->priv->human_profile = NULL;
 
189
 
 
190
        for (l = card->priv->profiles; l != NULL; l = l->next) {
 
191
                GvcMixerCardProfile *p = l->data;
 
192
                if (g_str_equal (card->priv->profile, p->profile)) {
 
193
                        card->priv->human_profile = g_strdup (p->human_profile);
 
194
                        break;
 
195
                }
 
196
        }
 
197
 
 
198
        g_object_notify (G_OBJECT (card), "profile");
 
199
 
 
200
        return TRUE;
 
201
}
 
202
 
 
203
static void
 
204
_pa_context_set_card_profile_by_index_cb (pa_context                       *context,
 
205
                                          int                               success,
 
206
                                          void                             *userdata)
 
207
{
 
208
        GvcMixerCard *card = GVC_MIXER_CARD (userdata);
 
209
 
 
210
        g_assert (card->priv->target_profile);
 
211
 
 
212
        if (success > 0) {
 
213
                gvc_mixer_card_set_profile (card, card->priv->target_profile);
 
214
        } else {
 
215
                g_debug ("Failed to switch profile on '%s' from '%s' to '%s'",
 
216
                         card->priv->name,
 
217
                         card->priv->profile,
 
218
                         card->priv->target_profile);
 
219
        }
 
220
        g_free (card->priv->target_profile);
 
221
        card->priv->target_profile = NULL;
 
222
 
 
223
        pa_operation_unref (card->priv->profile_op);
 
224
        card->priv->profile_op = NULL;
 
225
}
 
226
 
 
227
gboolean
 
228
gvc_mixer_card_change_profile (GvcMixerCard *card,
 
229
                               const char *profile)
 
230
{
 
231
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
232
        g_return_val_if_fail (card->priv->profiles != NULL, FALSE);
 
233
 
 
234
        /* Same profile, or already requested? */
 
235
        if (g_strcmp0 (card->priv->profile, profile) == 0)
 
236
                return TRUE;
 
237
        if (g_strcmp0 (profile, card->priv->target_profile) == 0)
 
238
                return TRUE;
 
239
        if (card->priv->profile_op != NULL) {
 
240
                pa_operation_cancel (card->priv->profile_op);
 
241
                pa_operation_unref (card->priv->profile_op);
 
242
                card->priv->profile_op = NULL;
 
243
        }
 
244
 
 
245
        if (card->priv->profile != NULL) {
 
246
                g_free (card->priv->target_profile);
 
247
                card->priv->target_profile = g_strdup (profile);
 
248
 
 
249
                card->priv->profile_op = pa_context_set_card_profile_by_index (card->priv->pa_context,
 
250
                                                                               card->priv->index,
 
251
                                                                               card->priv->target_profile,
 
252
                                                                               _pa_context_set_card_profile_by_index_cb,
 
253
                                                                               card);
 
254
 
 
255
                if (card->priv->profile_op == NULL) {
 
256
                        g_warning ("pa_context_set_card_profile_by_index() failed");
 
257
                        return FALSE;
 
258
                }
 
259
        } else {
 
260
                g_assert (card->priv->human_profile == NULL);
 
261
                card->priv->profile = g_strdup (profile);
 
262
        }
 
263
 
 
264
        return TRUE;
 
265
}
 
266
 
 
267
/**
 
268
 * gvc_mixer_card_get_profiles:
 
269
 *
 
270
 * Return value: (transfer none) (element-type GvcMixerCardProfile):
 
271
 */
 
272
const GList *
 
273
gvc_mixer_card_get_profiles (GvcMixerCard *card)
 
274
{
 
275
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
276
        return card->priv->profiles;
 
277
}
 
278
 
 
279
 
 
280
/**
 
281
 * gvc_mixer_card_get_ports:
 
282
 *
 
283
 * Return value: (transfer none) (element-type GvcMixerCardPort):
 
284
 */
 
285
const GList *
 
286
gvc_mixer_card_get_ports (GvcMixerCard *card)
 
287
{
 
288
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
289
        return card->priv->ports;
 
290
}
 
291
 
 
292
/**
 
293
 * gvc_mixer_card_set_profiles:
 
294
 * @profiles: (transfer full) (element-type GvcMixerCardProfile):
 
295
 */
 
296
gboolean
 
297
gvc_mixer_card_set_profiles (GvcMixerCard *card,
 
298
                             GList        *profiles)
 
299
{
 
300
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
301
        g_return_val_if_fail (card->priv->profiles == NULL, FALSE);
 
302
 
 
303
        card->priv->profiles = g_list_sort (profiles, (GCompareFunc) sort_profiles);
 
304
 
 
305
        return TRUE;
 
306
}
 
307
 
 
308
/**
 
309
 * gvc_mixer_card_get_gicon:
 
310
 *
 
311
 * Return value: (transfer full) (element-type GIcon):
 
312
 */
 
313
GIcon *
 
314
gvc_mixer_card_get_gicon (GvcMixerCard *card)
 
315
{
 
316
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
317
        if (card->priv->icon_name == NULL)
 
318
                return NULL;
 
319
        return g_themed_icon_new_with_default_fallbacks (card->priv->icon_name);
 
320
}
 
321
 
 
322
/**
 
323
 * gvc_mixer_card_set_ports:
 
324
 * @profiles: (transfer full) (element-type GvcMixerCardPort):
 
325
 */
 
326
gboolean              
 
327
gvc_mixer_card_set_ports (GvcMixerCard *card,
 
328
                          GList          *ports)
 
329
{
 
330
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
331
        g_return_val_if_fail (card->priv->ports == NULL, FALSE);
 
332
 
 
333
        card->priv->ports = ports;
 
334
 
 
335
        return TRUE;    
 
336
}                                                     
 
337
 
 
338
static void
 
339
gvc_mixer_card_set_property (GObject       *object,
 
340
                             guint          prop_id,
 
341
                             const GValue  *value,
 
342
                             GParamSpec    *pspec)
 
343
{
 
344
        GvcMixerCard *self = GVC_MIXER_CARD (object);
 
345
 
 
346
        switch (prop_id) {
 
347
        case PROP_PA_CONTEXT:
 
348
                self->priv->pa_context = g_value_get_pointer (value);
 
349
                break;
 
350
        case PROP_INDEX:
 
351
                self->priv->index = g_value_get_ulong (value);
 
352
                break;
 
353
        case PROP_ID:
 
354
                self->priv->id = g_value_get_ulong (value);
 
355
                break;
 
356
        case PROP_NAME:
 
357
                gvc_mixer_card_set_name (self, g_value_get_string (value));
 
358
                break;
 
359
        case PROP_ICON_NAME:
 
360
                gvc_mixer_card_set_icon_name (self, g_value_get_string (value));
 
361
                break;
 
362
        case PROP_PROFILE:
 
363
                gvc_mixer_card_set_profile (self, g_value_get_string (value));
 
364
                break;
 
365
        default:
 
366
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
367
                break;
 
368
        }
 
369
}
 
370
 
 
371
static void
 
372
gvc_mixer_card_get_property (GObject     *object,
 
373
                             guint        prop_id,
 
374
                             GValue      *value,
 
375
                             GParamSpec  *pspec)
 
376
{
 
377
        GvcMixerCard *self = GVC_MIXER_CARD (object);
 
378
 
 
379
        switch (prop_id) {
 
380
        case PROP_PA_CONTEXT:
 
381
                g_value_set_pointer (value, self->priv->pa_context);
 
382
                break;
 
383
        case PROP_INDEX:
 
384
                g_value_set_ulong (value, self->priv->index);
 
385
                break;
 
386
        case PROP_ID:
 
387
                g_value_set_ulong (value, self->priv->id);
 
388
                break;
 
389
        case PROP_NAME:
 
390
                g_value_set_string (value, self->priv->name);
 
391
                break;
 
392
        case PROP_ICON_NAME:
 
393
                g_value_set_string (value, self->priv->icon_name);
 
394
                break;
 
395
        case PROP_PROFILE:
 
396
                g_value_set_string (value, self->priv->profile);
 
397
                break;
 
398
        case PROP_HUMAN_PROFILE:
 
399
                g_value_set_string (value, self->priv->human_profile);
 
400
                break;
 
401
        default:
 
402
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
403
                break;
 
404
        }
 
405
}
 
406
 
 
407
static GObject *
 
408
gvc_mixer_card_constructor (GType                  type,
 
409
                            guint                  n_construct_properties,
 
410
                            GObjectConstructParam *construct_params)
 
411
{
 
412
        GObject       *object;
 
413
        GvcMixerCard *self;
 
414
 
 
415
        object = G_OBJECT_CLASS (gvc_mixer_card_parent_class)->constructor (type, n_construct_properties, construct_params);
 
416
 
 
417
        self = GVC_MIXER_CARD (object);
 
418
 
 
419
        self->priv->id = get_next_card_serial ();
 
420
 
 
421
        return object;
 
422
}
 
423
 
 
424
static void
 
425
gvc_mixer_card_class_init (GvcMixerCardClass *klass)
 
426
{
 
427
        GObjectClass   *gobject_class = G_OBJECT_CLASS (klass);
 
428
 
 
429
        gobject_class->constructor = gvc_mixer_card_constructor;
 
430
        gobject_class->finalize = gvc_mixer_card_finalize;
 
431
 
 
432
        gobject_class->set_property = gvc_mixer_card_set_property;
 
433
        gobject_class->get_property = gvc_mixer_card_get_property;
 
434
 
 
435
        g_object_class_install_property (gobject_class,
 
436
                                         PROP_INDEX,
 
437
                                         g_param_spec_ulong ("index",
 
438
                                                             "Index",
 
439
                                                             "The index for this card",
 
440
                                                             0, G_MAXULONG, 0,
 
441
                                                             G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
442
        g_object_class_install_property (gobject_class,
 
443
                                         PROP_ID,
 
444
                                         g_param_spec_ulong ("id",
 
445
                                                             "id",
 
446
                                                             "The id for this card",
 
447
                                                             0, G_MAXULONG, 0,
 
448
                                                             G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
449
        g_object_class_install_property (gobject_class,
 
450
                                         PROP_PA_CONTEXT,
 
451
                                         g_param_spec_pointer ("pa-context",
 
452
                                                               "PulseAudio context",
 
453
                                                               "The PulseAudio context for this card",
 
454
                                                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
455
        g_object_class_install_property (gobject_class,
 
456
                                         PROP_NAME,
 
457
                                         g_param_spec_string ("name",
 
458
                                                              "Name",
 
459
                                                              "Name to display for this card",
 
460
                                                              NULL,
 
461
                                                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
 
462
        g_object_class_install_property (gobject_class,
 
463
                                         PROP_ICON_NAME,
 
464
                                         g_param_spec_string ("icon-name",
 
465
                                                              "Icon Name",
 
466
                                                              "Name of icon to display for this card",
 
467
                                                              NULL,
 
468
                                                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
 
469
        g_object_class_install_property (gobject_class,
 
470
                                         PROP_PROFILE,
 
471
                                         g_param_spec_string ("profile",
 
472
                                                              "Profile",
 
473
                                                              "Name of current profile for this card",
 
474
                                                              NULL,
 
475
                                                              G_PARAM_READWRITE));
 
476
        g_object_class_install_property (gobject_class,
 
477
                                         PROP_HUMAN_PROFILE,
 
478
                                         g_param_spec_string ("human-profile",
 
479
                                                              "Profile (Human readable)",
 
480
                                                              "Name of current profile for this card in human readable form",
 
481
                                                              NULL,
 
482
                                                              G_PARAM_READABLE));
 
483
 
 
484
        g_type_class_add_private (klass, sizeof (GvcMixerCardPrivate));
 
485
}
 
486
 
 
487
static void
 
488
gvc_mixer_card_init (GvcMixerCard *card)
 
489
{
 
490
        card->priv = GVC_MIXER_CARD_GET_PRIVATE (card);
 
491
}
 
492
 
 
493
 
 
494
GvcMixerCard *
 
495
gvc_mixer_card_new (pa_context *context,
 
496
                    guint       index)
 
497
{
 
498
        GObject *object;
 
499
 
 
500
        object = g_object_new (GVC_TYPE_MIXER_CARD,
 
501
                               "index", index,
 
502
                               "pa-context", context,
 
503
                               NULL);
 
504
        return GVC_MIXER_CARD (object);
 
505
}
 
506
 
 
507
static void
 
508
free_profile (GvcMixerCardProfile *p)
 
509
{
 
510
        g_free (p->profile);
 
511
        g_free (p->human_profile);
 
512
        g_free (p->status);
 
513
        g_free (p);
 
514
}
 
515
 
 
516
static void
 
517
gvc_mixer_card_finalize (GObject *object)
 
518
{
 
519
        GvcMixerCard *mixer_card;
 
520
 
 
521
        g_return_if_fail (object != NULL);
 
522
        g_return_if_fail (GVC_IS_MIXER_CARD (object));
 
523
 
 
524
        mixer_card = GVC_MIXER_CARD (object);
 
525
 
 
526
        g_return_if_fail (mixer_card->priv != NULL);
 
527
 
 
528
        g_free (mixer_card->priv->name);
 
529
        mixer_card->priv->name = NULL;
 
530
 
 
531
        g_free (mixer_card->priv->icon_name);
 
532
        mixer_card->priv->icon_name = NULL;
 
533
 
 
534
        g_free (mixer_card->priv->target_profile);
 
535
        mixer_card->priv->target_profile = NULL;
 
536
 
 
537
        g_free (mixer_card->priv->profile);
 
538
        mixer_card->priv->profile = NULL;
 
539
 
 
540
        g_free (mixer_card->priv->human_profile);
 
541
        mixer_card->priv->human_profile = NULL;
 
542
 
 
543
        g_list_foreach (mixer_card->priv->profiles, (GFunc) free_profile, NULL);
 
544
        g_list_free (mixer_card->priv->profiles);
 
545
        mixer_card->priv->profiles = NULL;
 
546
 
 
547
        G_OBJECT_CLASS (gvc_mixer_card_parent_class)->finalize (object);
 
548
}
 
549