~ubuntu-branches/ubuntu/vivid/gnome-flashback/vivid

« back to all changes in this revision

Viewing changes to gnome-flashback/libsound-applet/gvc/gvc-mixer-card.c

  • Committer: Package Import Robot
  • Author(s): Dmitry Shachnev
  • Date: 2014-09-19 17:09:43 UTC
  • Revision ID: package-import@ubuntu.com-20140919170943-oboafsedi6z69951
Tags: upstream-3.10.0
ImportĀ upstreamĀ versionĀ 3.10.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
 * Copyright (C) 2009 Bastien Nocera
 
5
 * Copyright (C) Conor Curran 2011 <conor.curran@canonical.com>
 
6
 *
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program; if not, write to the Free Software
 
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
20
 *
 
21
 */
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include <stdlib.h>
 
26
#include <stdio.h>
 
27
#include <unistd.h>
 
28
 
 
29
#include <glib.h>
 
30
#include <glib/gi18n-lib.h>
 
31
 
 
32
#include <pulse/pulseaudio.h>
 
33
 
 
34
#include "gvc-mixer-card.h"
 
35
#include "gvc-mixer-card-private.h"
 
36
 
 
37
#define GVC_MIXER_CARD_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GVC_TYPE_MIXER_CARD, GvcMixerCardPrivate))
 
38
 
 
39
static guint32 card_serial = 1;
 
40
 
 
41
struct GvcMixerCardPrivate
 
42
{
 
43
        pa_context    *pa_context;
 
44
        guint          id;
 
45
        guint          index;
 
46
        char          *name;
 
47
        char          *icon_name;
 
48
        char          *profile;
 
49
        char          *target_profile;
 
50
        char          *human_profile;
 
51
        GList         *profiles;
 
52
        pa_operation  *profile_op;
 
53
        GList         *ports;
 
54
};
 
55
 
 
56
enum
 
57
{
 
58
        PROP_0,
 
59
        PROP_ID,
 
60
        PROP_PA_CONTEXT,
 
61
        PROP_INDEX,
 
62
        PROP_NAME,
 
63
        PROP_ICON_NAME,
 
64
        PROP_PROFILE,
 
65
        PROP_HUMAN_PROFILE,
 
66
};
 
67
 
 
68
static void     gvc_mixer_card_class_init (GvcMixerCardClass *klass);
 
69
static void     gvc_mixer_card_init       (GvcMixerCard      *mixer_card);
 
70
static void     gvc_mixer_card_finalize   (GObject            *object);
 
71
 
 
72
G_DEFINE_TYPE (GvcMixerCard, gvc_mixer_card, G_TYPE_OBJECT)
 
73
 
 
74
static guint32
 
75
get_next_card_serial (void)
 
76
{
 
77
        guint32 serial;
 
78
 
 
79
        serial = card_serial++;
 
80
 
 
81
        if ((gint32)card_serial < 0) {
 
82
                card_serial = 1;
 
83
        }
 
84
 
 
85
        return serial;
 
86
}
 
87
 
 
88
pa_context *
 
89
gvc_mixer_card_get_pa_context (GvcMixerCard *card)
 
90
{
 
91
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), 0);
 
92
        return card->priv->pa_context;
 
93
}
 
94
 
 
95
guint
 
96
gvc_mixer_card_get_index (GvcMixerCard *card)
 
97
{
 
98
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), 0);
 
99
        return card->priv->index;
 
100
}
 
101
 
 
102
guint
 
103
gvc_mixer_card_get_id (GvcMixerCard *card)
 
104
{
 
105
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), 0);
 
106
        return card->priv->id;
 
107
}
 
108
 
 
109
const char *
 
110
gvc_mixer_card_get_name (GvcMixerCard *card)
 
111
{
 
112
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
113
        return card->priv->name;
 
114
}
 
115
 
 
116
gboolean
 
117
gvc_mixer_card_set_name (GvcMixerCard *card,
 
118
                         const char     *name)
 
119
{
 
120
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
121
 
 
122
        g_free (card->priv->name);
 
123
        card->priv->name = g_strdup (name);
 
124
        g_object_notify (G_OBJECT (card), "name");
 
125
 
 
126
        return TRUE;
 
127
}
 
128
 
 
129
const char *
 
130
gvc_mixer_card_get_icon_name (GvcMixerCard *card)
 
131
{
 
132
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
133
        return card->priv->icon_name;
 
134
}
 
135
 
 
136
gboolean
 
137
gvc_mixer_card_set_icon_name (GvcMixerCard *card,
 
138
                              const char     *icon_name)
 
139
{
 
140
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
141
 
 
142
        g_free (card->priv->icon_name);
 
143
        card->priv->icon_name = g_strdup (icon_name);
 
144
        g_object_notify (G_OBJECT (card), "icon-name");
 
145
 
 
146
        return TRUE;
 
147
}
 
148
 
 
149
/**
 
150
 * gvc_mixer_card_get_profile: (skip)
 
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
 * gvc_mixer_card_get_ports:
 
281
 *
 
282
 * Return value: (transfer none) (element-type GvcMixerCardPort):
 
283
 */
 
284
const GList *
 
285
gvc_mixer_card_get_ports (GvcMixerCard *card)
 
286
{
 
287
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
288
        return card->priv->ports;
 
289
}
 
290
 
 
291
/**
 
292
 * gvc_mixer_card_profile_compare:
 
293
 *
 
294
 * Return value: 1 if @a has a higher priority, -1 if @b has a higher
 
295
 * priority, 0 if @a and @b have the same priority.
 
296
 */
 
297
int
 
298
gvc_mixer_card_profile_compare (GvcMixerCardProfile *a,
 
299
                                GvcMixerCardProfile *b)
 
300
{
 
301
        if (a->priority == b->priority)
 
302
                return 0;
 
303
        if (a->priority > b->priority)
 
304
                return 1;
 
305
        return -1;
 
306
}
 
307
 
 
308
/**
 
309
 * gvc_mixer_card_set_profiles:
 
310
 * @profiles: (transfer full) (element-type GvcMixerCardProfile):
 
311
 */
 
312
gboolean
 
313
gvc_mixer_card_set_profiles (GvcMixerCard *card,
 
314
                             GList        *profiles)
 
315
{
 
316
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
317
        g_return_val_if_fail (card->priv->profiles == NULL, FALSE);
 
318
 
 
319
        card->priv->profiles = g_list_sort (profiles, (GCompareFunc) gvc_mixer_card_profile_compare);
 
320
 
 
321
        return TRUE;
 
322
}
 
323
 
 
324
/**
 
325
 * gvc_mixer_card_get_gicon:
 
326
 * @card:
 
327
 *
 
328
 * Return value: (transfer full):
 
329
 */
 
330
GIcon *
 
331
gvc_mixer_card_get_gicon (GvcMixerCard *card)
 
332
{
 
333
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
334
 
 
335
        if (card->priv->icon_name == NULL)
 
336
                return NULL;
 
337
 
 
338
        return g_themed_icon_new_with_default_fallbacks (card->priv->icon_name);
 
339
}
 
340
 
 
341
static void
 
342
free_port (GvcMixerCardPort *port)
 
343
{
 
344
        g_free (port->port);
 
345
        g_free (port->human_port);
 
346
        g_free (port->icon_name);
 
347
        g_list_free (port->profiles);
 
348
 
 
349
        g_free (port);
 
350
}
 
351
 
 
352
/**
 
353
 * gvc_mixer_card_set_ports:
 
354
 * @ports: (transfer full) (element-type GvcMixerCardPort):
 
355
 */
 
356
gboolean
 
357
gvc_mixer_card_set_ports (GvcMixerCard *card,
 
358
                          GList        *ports)
 
359
{
 
360
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
361
        g_return_val_if_fail (card->priv->ports == NULL, FALSE);
 
362
 
 
363
        g_list_free_full (card->priv->ports, (GDestroyNotify) free_port);
 
364
        card->priv->ports = ports;
 
365
 
 
366
        return TRUE;
 
367
}
 
368
 
 
369
static void
 
370
gvc_mixer_card_set_property (GObject       *object,
 
371
                             guint          prop_id,
 
372
                             const GValue  *value,
 
373
                             GParamSpec    *pspec)
 
374
{
 
375
        GvcMixerCard *self = GVC_MIXER_CARD (object);
 
376
 
 
377
        switch (prop_id) {
 
378
        case PROP_PA_CONTEXT:
 
379
                self->priv->pa_context = g_value_get_pointer (value);
 
380
                break;
 
381
        case PROP_INDEX:
 
382
                self->priv->index = g_value_get_ulong (value);
 
383
                break;
 
384
        case PROP_ID:
 
385
                self->priv->id = g_value_get_ulong (value);
 
386
                break;
 
387
        case PROP_NAME:
 
388
                gvc_mixer_card_set_name (self, g_value_get_string (value));
 
389
                break;
 
390
        case PROP_ICON_NAME:
 
391
                gvc_mixer_card_set_icon_name (self, g_value_get_string (value));
 
392
                break;
 
393
        case PROP_PROFILE:
 
394
                gvc_mixer_card_set_profile (self, g_value_get_string (value));
 
395
                break;
 
396
        default:
 
397
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
398
                break;
 
399
        }
 
400
}
 
401
 
 
402
static void
 
403
gvc_mixer_card_get_property (GObject     *object,
 
404
                             guint        prop_id,
 
405
                             GValue      *value,
 
406
                             GParamSpec  *pspec)
 
407
{
 
408
        GvcMixerCard *self = GVC_MIXER_CARD (object);
 
409
 
 
410
        switch (prop_id) {
 
411
        case PROP_PA_CONTEXT:
 
412
                g_value_set_pointer (value, self->priv->pa_context);
 
413
                break;
 
414
        case PROP_INDEX:
 
415
                g_value_set_ulong (value, self->priv->index);
 
416
                break;
 
417
        case PROP_ID:
 
418
                g_value_set_ulong (value, self->priv->id);
 
419
                break;
 
420
        case PROP_NAME:
 
421
                g_value_set_string (value, self->priv->name);
 
422
                break;
 
423
        case PROP_ICON_NAME:
 
424
                g_value_set_string (value, self->priv->icon_name);
 
425
                break;
 
426
        case PROP_PROFILE:
 
427
                g_value_set_string (value, self->priv->profile);
 
428
                break;
 
429
        case PROP_HUMAN_PROFILE:
 
430
                g_value_set_string (value, self->priv->human_profile);
 
431
                break;
 
432
        default:
 
433
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
434
                break;
 
435
        }
 
436
}
 
437
 
 
438
static GObject *
 
439
gvc_mixer_card_constructor (GType                  type,
 
440
                            guint                  n_construct_properties,
 
441
                            GObjectConstructParam *construct_params)
 
442
{
 
443
        GObject       *object;
 
444
        GvcMixerCard *self;
 
445
 
 
446
        object = G_OBJECT_CLASS (gvc_mixer_card_parent_class)->constructor (type, n_construct_properties, construct_params);
 
447
 
 
448
        self = GVC_MIXER_CARD (object);
 
449
 
 
450
        self->priv->id = get_next_card_serial ();
 
451
 
 
452
        return object;
 
453
}
 
454
 
 
455
static void
 
456
gvc_mixer_card_class_init (GvcMixerCardClass *klass)
 
457
{
 
458
        GObjectClass   *gobject_class = G_OBJECT_CLASS (klass);
 
459
 
 
460
        gobject_class->constructor = gvc_mixer_card_constructor;
 
461
        gobject_class->finalize = gvc_mixer_card_finalize;
 
462
 
 
463
        gobject_class->set_property = gvc_mixer_card_set_property;
 
464
        gobject_class->get_property = gvc_mixer_card_get_property;
 
465
 
 
466
        g_object_class_install_property (gobject_class,
 
467
                                         PROP_INDEX,
 
468
                                         g_param_spec_ulong ("index",
 
469
                                                             "Index",
 
470
                                                             "The index for this card",
 
471
                                                             0, G_MAXULONG, 0,
 
472
                                                             G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
473
        g_object_class_install_property (gobject_class,
 
474
                                         PROP_ID,
 
475
                                         g_param_spec_ulong ("id",
 
476
                                                             "id",
 
477
                                                             "The id for this card",
 
478
                                                             0, G_MAXULONG, 0,
 
479
                                                             G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
480
        g_object_class_install_property (gobject_class,
 
481
                                         PROP_PA_CONTEXT,
 
482
                                         g_param_spec_pointer ("pa-context",
 
483
                                                               "PulseAudio context",
 
484
                                                               "The PulseAudio context for this card",
 
485
                                                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
486
        g_object_class_install_property (gobject_class,
 
487
                                         PROP_NAME,
 
488
                                         g_param_spec_string ("name",
 
489
                                                              "Name",
 
490
                                                              "Name to display for this card",
 
491
                                                              NULL,
 
492
                                                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
 
493
        g_object_class_install_property (gobject_class,
 
494
                                         PROP_ICON_NAME,
 
495
                                         g_param_spec_string ("icon-name",
 
496
                                                              "Icon Name",
 
497
                                                              "Name of icon to display for this card",
 
498
                                                              NULL,
 
499
                                                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
 
500
        g_object_class_install_property (gobject_class,
 
501
                                         PROP_PROFILE,
 
502
                                         g_param_spec_string ("profile",
 
503
                                                              "Profile",
 
504
                                                              "Name of current profile for this card",
 
505
                                                              NULL,
 
506
                                                              G_PARAM_READWRITE));
 
507
        g_object_class_install_property (gobject_class,
 
508
                                         PROP_HUMAN_PROFILE,
 
509
                                         g_param_spec_string ("human-profile",
 
510
                                                              "Profile (Human readable)",
 
511
                                                              "Name of current profile for this card in human readable form",
 
512
                                                              NULL,
 
513
                                                              G_PARAM_READABLE));
 
514
 
 
515
        g_type_class_add_private (klass, sizeof (GvcMixerCardPrivate));
 
516
}
 
517
 
 
518
static void
 
519
gvc_mixer_card_init (GvcMixerCard *card)
 
520
{
 
521
        card->priv = GVC_MIXER_CARD_GET_PRIVATE (card);
 
522
}
 
523
 
 
524
GvcMixerCard *
 
525
gvc_mixer_card_new (pa_context *context,
 
526
                    guint       index)
 
527
{
 
528
        GObject *object;
 
529
 
 
530
        object = g_object_new (GVC_TYPE_MIXER_CARD,
 
531
                               "index", index,
 
532
                               "pa-context", context,
 
533
                               NULL);
 
534
        return GVC_MIXER_CARD (object);
 
535
}
 
536
 
 
537
static void
 
538
free_profile (GvcMixerCardProfile *p)
 
539
{
 
540
        g_free (p->profile);
 
541
        g_free (p->human_profile);
 
542
        g_free (p->status);
 
543
        g_free (p);
 
544
}
 
545
 
 
546
static void
 
547
gvc_mixer_card_finalize (GObject *object)
 
548
{
 
549
        GvcMixerCard *mixer_card;
 
550
 
 
551
        g_return_if_fail (object != NULL);
 
552
        g_return_if_fail (GVC_IS_MIXER_CARD (object));
 
553
 
 
554
        mixer_card = GVC_MIXER_CARD (object);
 
555
 
 
556
        g_return_if_fail (mixer_card->priv != NULL);
 
557
 
 
558
        g_free (mixer_card->priv->name);
 
559
        mixer_card->priv->name = NULL;
 
560
 
 
561
        g_free (mixer_card->priv->icon_name);
 
562
        mixer_card->priv->icon_name = NULL;
 
563
 
 
564
        g_free (mixer_card->priv->target_profile);
 
565
        mixer_card->priv->target_profile = NULL;
 
566
 
 
567
        g_free (mixer_card->priv->profile);
 
568
        mixer_card->priv->profile = NULL;
 
569
 
 
570
        g_free (mixer_card->priv->human_profile);
 
571
        mixer_card->priv->human_profile = NULL;
 
572
 
 
573
        g_list_foreach (mixer_card->priv->profiles, (GFunc) free_profile, NULL);
 
574
        g_list_free (mixer_card->priv->profiles);
 
575
        mixer_card->priv->profiles = NULL;
 
576
 
 
577
        g_list_free_full (mixer_card->priv->ports, (GDestroyNotify) free_port);
 
578
        mixer_card->priv->ports = NULL;
 
579
 
 
580
        G_OBJECT_CLASS (gvc_mixer_card_parent_class)->finalize (object);
 
581
}
 
582