2009-03-30 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-sound.c
1 /*
2  * This file is a part of libhildon
3  *
4  * Copyright (C) 2005-2008 Nokia Corporation. All rights reserved.
5  *
6  * Contact: Kimmo Hämäläinen <kimmo.hamalainen@nokia.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24
25 /**
26  * SECTION:hildon-sound
27  * @short_description: libcanberra-based utility function for playing a sound.
28  * 
29  */
30
31 #include <unistd.h>
32 #include <gconf/gconf-client.h>
33 #include <canberra.h>
34
35 #include "hildon-sound.h"
36
37 #define ALARM_GCONF_PATH "/apps/osso/sound/system_alert_volume"
38
39 static ca_context *hildon_ca_context_get (void);
40
41 /*
42  * hildon_ca_context_get:
43  *
44  * hildon maintains a single application-global ca_context object.
45  *
46  * This functions is based on ca_gtk_context_get
47  *
48  * Returns: a ca_context object
49  */
50 static ca_context *
51 hildon_ca_context_get (void)
52 {
53     static GStaticPrivate context_private = G_STATIC_PRIVATE_INIT;
54     ca_context *c = NULL;
55     const gchar *name = NULL;
56     gint ret;
57
58     if ((c = g_static_private_get(&context_private)))
59         return c;
60
61     if ((ret = ca_context_create(&c)) != CA_SUCCESS) {
62         g_warning("ca_context_create: %s\n", ca_strerror(ret));
63         return NULL;
64     }
65     if ((ret = ca_context_open(c)) != CA_SUCCESS) {
66         g_warning("ca_context_open: %s\n", ca_strerror(ret));
67         ca_context_destroy(c);
68         return NULL;
69     }
70
71     if ((name = g_get_application_name()))
72         ca_context_change_props(c, CA_PROP_APPLICATION_NAME, name, NULL);
73
74     g_static_private_set(&context_private, c, (GDestroyNotify) ca_context_destroy);
75
76     return c;
77 }
78
79 /**
80  * hildon_play_system_sound:
81  * @sample: sound file to play
82  * 
83  * Plays the given sample using libcanberra.
84  * Volume level is received from gconf. 
85  */
86 void 
87 hildon_play_system_sound(const gchar *sample)
88 {
89     float volume = 0;
90     int ret;
91     ca_context *ca_con = NULL;
92     ca_proplist *pl = NULL;
93
94 #if 0 /* FIXME: Check volume handling. Would be great not to use Gconf... */
95     GConfClient *client;
96     GConfValue *value;
97     gint gconf_vol;
98
99     /*
100      * The volume is from -0dB to -6dB,
101        The full volume is marked as 2 in gconf */
102     client = gconf_client_get_default ();
103     value = gconf_client_get (client, ALARM_GCONF_PATH, NULL);
104
105     /* We want error cases to match full volume, not silence, so
106        we do not want to use gconf_client_get_int */
107     if (!value || value->type != GCONF_VALUE_INT)
108         gconf_vol = 2;
109     else
110         gconf_vol = gconf_value_get_int(value);
111
112     if (value)
113         gconf_value_free(value);
114     g_object_unref (client);
115
116     volume = ((1.0 - (float)gconf_vol / 2.0)) * (-6.0);
117 #endif
118
119     ca_con = hildon_ca_context_get ();
120
121     ca_proplist_create(&pl);
122     ca_proplist_sets(pl, CA_PROP_MEDIA_FILENAME, sample);
123     ca_proplist_setf(pl, CA_PROP_CANBERRA_VOLUME, "%f", volume);
124
125     ret = ca_context_play_full(ca_con, 0, pl, NULL, NULL);
126     g_debug("ca_context_play_full (vol %f): %s\n", volume, ca_strerror(ret));
127
128     ca_proplist_destroy(pl);
129 }