~ctf/unity-settings-daemon/bug1389099_mic_volume_icons

« back to all changes in this revision

Viewing changes to plugins/media-keys/gvc/gvc-mixer-card.c

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-02-07 11:44:36 UTC
  • Revision ID: package-import@ubuntu.com-20140207114436-7t5u3yvwc4ul7w3e
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
 * 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
/**
 
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_profile_compare:
 
294
 *
 
295
 * Return value: 1 if @a has a higher priority, -1 if @b has a higher
 
296
 * priority, 0 if @a and @b have the same priority.
 
297
 */
 
298
int
 
299
gvc_mixer_card_profile_compare (GvcMixerCardProfile *a,
 
300
                                GvcMixerCardProfile *b)
 
301
{
 
302
        if (a->priority == b->priority)
 
303
                return 0;
 
304
        if (a->priority > b->priority)
 
305
                return 1;
 
306
        return -1;
 
307
}
 
308
 
 
309
/**
 
310
 * gvc_mixer_card_set_profiles:
 
311
 * @profiles: (transfer full) (element-type GvcMixerCardProfile):
 
312
 */
 
313
gboolean
 
314
gvc_mixer_card_set_profiles (GvcMixerCard *card,
 
315
                             GList        *profiles)
 
316
{
 
317
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
318
        g_return_val_if_fail (card->priv->profiles == NULL, FALSE);
 
319
 
 
320
        card->priv->profiles = g_list_sort (profiles, (GCompareFunc) gvc_mixer_card_profile_compare);
 
321
 
 
322
        return TRUE;
 
323
}
 
324
 
 
325
/**
 
326
 * gvc_mixer_card_get_gicon:
 
327
 * @card:
 
328
 *
 
329
 * Return value: (transfer full):
 
330
 */
 
331
GIcon *
 
332
gvc_mixer_card_get_gicon (GvcMixerCard *card)
 
333
{
 
334
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), NULL);
 
335
 
 
336
        if (card->priv->icon_name == NULL)
 
337
                return NULL;
 
338
 
 
339
        return g_themed_icon_new_with_default_fallbacks (card->priv->icon_name);
 
340
}
 
341
 
 
342
/**
 
343
 * gvc_mixer_card_set_ports:
 
344
 * @ports: (transfer full) (element-type GvcMixerCardPort):
 
345
 */
 
346
gboolean
 
347
gvc_mixer_card_set_ports (GvcMixerCard *card,
 
348
                          GList        *ports)
 
349
{
 
350
        g_return_val_if_fail (GVC_IS_MIXER_CARD (card), FALSE);
 
351
        g_return_val_if_fail (card->priv->ports == NULL, FALSE);
 
352
 
 
353
        card->priv->ports = ports;
 
354
 
 
355
        return TRUE;
 
356
}
 
357
 
 
358
static void
 
359
gvc_mixer_card_set_property (GObject       *object,
 
360
                             guint          prop_id,
 
361
                             const GValue  *value,
 
362
                             GParamSpec    *pspec)
 
363
{
 
364
        GvcMixerCard *self = GVC_MIXER_CARD (object);
 
365
 
 
366
        switch (prop_id) {
 
367
        case PROP_PA_CONTEXT:
 
368
                self->priv->pa_context = g_value_get_pointer (value);
 
369
                break;
 
370
        case PROP_INDEX:
 
371
                self->priv->index = g_value_get_ulong (value);
 
372
                break;
 
373
        case PROP_ID:
 
374
                self->priv->id = g_value_get_ulong (value);
 
375
                break;
 
376
        case PROP_NAME:
 
377
                gvc_mixer_card_set_name (self, g_value_get_string (value));
 
378
                break;
 
379
        case PROP_ICON_NAME:
 
380
                gvc_mixer_card_set_icon_name (self, g_value_get_string (value));
 
381
                break;
 
382
        case PROP_PROFILE:
 
383
                gvc_mixer_card_set_profile (self, g_value_get_string (value));
 
384
                break;
 
385
        default:
 
386
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
387
                break;
 
388
        }
 
389
}
 
390
 
 
391
static void
 
392
gvc_mixer_card_get_property (GObject     *object,
 
393
                             guint        prop_id,
 
394
                             GValue      *value,
 
395
                             GParamSpec  *pspec)
 
396
{
 
397
        GvcMixerCard *self = GVC_MIXER_CARD (object);
 
398
 
 
399
        switch (prop_id) {
 
400
        case PROP_PA_CONTEXT:
 
401
                g_value_set_pointer (value, self->priv->pa_context);
 
402
                break;
 
403
        case PROP_INDEX:
 
404
                g_value_set_ulong (value, self->priv->index);
 
405
                break;
 
406
        case PROP_ID:
 
407
                g_value_set_ulong (value, self->priv->id);
 
408
                break;
 
409
        case PROP_NAME:
 
410
                g_value_set_string (value, self->priv->name);
 
411
                break;
 
412
        case PROP_ICON_NAME:
 
413
                g_value_set_string (value, self->priv->icon_name);
 
414
                break;
 
415
        case PROP_PROFILE:
 
416
                g_value_set_string (value, self->priv->profile);
 
417
                break;
 
418
        case PROP_HUMAN_PROFILE:
 
419
                g_value_set_string (value, self->priv->human_profile);
 
420
                break;
 
421
        default:
 
422
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
423
                break;
 
424
        }
 
425
}
 
426
 
 
427
static GObject *
 
428
gvc_mixer_card_constructor (GType                  type,
 
429
                            guint                  n_construct_properties,
 
430
                            GObjectConstructParam *construct_params)
 
431
{
 
432
        GObject       *object;
 
433
        GvcMixerCard *self;
 
434
 
 
435
        object = G_OBJECT_CLASS (gvc_mixer_card_parent_class)->constructor (type, n_construct_properties, construct_params);
 
436
 
 
437
        self = GVC_MIXER_CARD (object);
 
438
 
 
439
        self->priv->id = get_next_card_serial ();
 
440
 
 
441
        return object;
 
442
}
 
443
 
 
444
static void
 
445
gvc_mixer_card_class_init (GvcMixerCardClass *klass)
 
446
{
 
447
        GObjectClass   *gobject_class = G_OBJECT_CLASS (klass);
 
448
 
 
449
        gobject_class->constructor = gvc_mixer_card_constructor;
 
450
        gobject_class->finalize = gvc_mixer_card_finalize;
 
451
 
 
452
        gobject_class->set_property = gvc_mixer_card_set_property;
 
453
        gobject_class->get_property = gvc_mixer_card_get_property;
 
454
 
 
455
        g_object_class_install_property (gobject_class,
 
456
                                         PROP_INDEX,
 
457
                                         g_param_spec_ulong ("index",
 
458
                                                             "Index",
 
459
                                                             "The index for this card",
 
460
                                                             0, G_MAXULONG, 0,
 
461
                                                             G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
462
        g_object_class_install_property (gobject_class,
 
463
                                         PROP_ID,
 
464
                                         g_param_spec_ulong ("id",
 
465
                                                             "id",
 
466
                                                             "The id for this card",
 
467
                                                             0, G_MAXULONG, 0,
 
468
                                                             G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
469
        g_object_class_install_property (gobject_class,
 
470
                                         PROP_PA_CONTEXT,
 
471
                                         g_param_spec_pointer ("pa-context",
 
472
                                                               "PulseAudio context",
 
473
                                                               "The PulseAudio context for this card",
 
474
                                                               G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
 
475
        g_object_class_install_property (gobject_class,
 
476
                                         PROP_NAME,
 
477
                                         g_param_spec_string ("name",
 
478
                                                              "Name",
 
479
                                                              "Name to display for this card",
 
480
                                                              NULL,
 
481
                                                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
 
482
        g_object_class_install_property (gobject_class,
 
483
                                         PROP_ICON_NAME,
 
484
                                         g_param_spec_string ("icon-name",
 
485
                                                              "Icon Name",
 
486
                                                              "Name of icon to display for this card",
 
487
                                                              NULL,
 
488
                                                              G_PARAM_READWRITE|G_PARAM_CONSTRUCT));
 
489
        g_object_class_install_property (gobject_class,
 
490
                                         PROP_PROFILE,
 
491
                                         g_param_spec_string ("profile",
 
492
                                                              "Profile",
 
493
                                                              "Name of current profile for this card",
 
494
                                                              NULL,
 
495
                                                              G_PARAM_READWRITE));
 
496
        g_object_class_install_property (gobject_class,
 
497
                                         PROP_HUMAN_PROFILE,
 
498
                                         g_param_spec_string ("human-profile",
 
499
                                                              "Profile (Human readable)",
 
500
                                                              "Name of current profile for this card in human readable form",
 
501
                                                              NULL,
 
502
                                                              G_PARAM_READABLE));
 
503
 
 
504
        g_type_class_add_private (klass, sizeof (GvcMixerCardPrivate));
 
505
}
 
506
 
 
507
static void
 
508
gvc_mixer_card_init (GvcMixerCard *card)
 
509
{
 
510
        card->priv = GVC_MIXER_CARD_GET_PRIVATE (card);
 
511
}
 
512
 
 
513
GvcMixerCard *
 
514
gvc_mixer_card_new (pa_context *context,
 
515
                    guint       index)
 
516
{
 
517
        GObject *object;
 
518
 
 
519
        object = g_object_new (GVC_TYPE_MIXER_CARD,
 
520
                               "index", index,
 
521
                               "pa-context", context,
 
522
                               NULL);
 
523
        return GVC_MIXER_CARD (object);
 
524
}
 
525
 
 
526
static void
 
527
free_profile (GvcMixerCardProfile *p)
 
528
{
 
529
        g_free (p->profile);
 
530
        g_free (p->human_profile);
 
531
        g_free (p->status);
 
532
        g_free (p);
 
533
}
 
534
 
 
535
static void
 
536
gvc_mixer_card_finalize (GObject *object)
 
537
{
 
538
        GvcMixerCard *mixer_card;
 
539
 
 
540
        g_return_if_fail (object != NULL);
 
541
        g_return_if_fail (GVC_IS_MIXER_CARD (object));
 
542
 
 
543
        mixer_card = GVC_MIXER_CARD (object);
 
544
 
 
545
        g_return_if_fail (mixer_card->priv != NULL);
 
546
 
 
547
        g_free (mixer_card->priv->name);
 
548
        mixer_card->priv->name = NULL;
 
549
 
 
550
        g_free (mixer_card->priv->icon_name);
 
551
        mixer_card->priv->icon_name = NULL;
 
552
 
 
553
        g_free (mixer_card->priv->target_profile);
 
554
        mixer_card->priv->target_profile = NULL;
 
555
 
 
556
        g_free (mixer_card->priv->profile);
 
557
        mixer_card->priv->profile = NULL;
 
558
 
 
559
        g_free (mixer_card->priv->human_profile);
 
560
        mixer_card->priv->human_profile = NULL;
 
561
 
 
562
        g_list_foreach (mixer_card->priv->profiles, (GFunc) free_profile, NULL);
 
563
        g_list_free (mixer_card->priv->profiles);
 
564
        mixer_card->priv->profiles = NULL;
 
565
 
 
566
        G_OBJECT_CLASS (gvc_mixer_card_parent_class)->finalize (object);
 
567
}
 
568