980d65f65bd11a2d47b48c912407a0e644c0a709
[hildon] / hildon / 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  * Please note that this method is only provided for backwards compatibility,
30  * but we highly recommend you to use canberra-gtk directly instead.
31  * 
32  */
33
34 #include <unistd.h>
35 #include <gconf/gconf-client.h>
36 #include <canberra.h>
37
38 #include "hildon-sound.h"
39
40 #define ALARM_GCONF_PATH "/apps/osso/sound/system_alert_volume"
41
42 static ca_context *hildon_ca_context_get (void);
43
44 /*
45  * hildon_ca_context_get:
46  *
47  * hildon maintains a single application-global ca_context object.
48  *
49  * This functions is based on ca_gtk_context_get
50  *
51  * Returns: a ca_context object
52  */
53 static ca_context *
54 hildon_ca_context_get (void)
55 {
56     static GStaticPrivate context_private = G_STATIC_PRIVATE_INIT;
57     ca_context *c = NULL;
58     const gchar *name = NULL;
59     gint ret;
60
61     if ((c = g_static_private_get(&context_private)))
62         return c;
63
64     if ((ret = ca_context_create(&c)) != CA_SUCCESS) {
65         g_warning("ca_context_create: %s\n", ca_strerror(ret));
66         return NULL;
67     }
68     if ((ret = ca_context_open(c)) != CA_SUCCESS) {
69         g_warning("ca_context_open: %s\n", ca_strerror(ret));
70         ca_context_destroy(c);
71         return NULL;
72     }
73
74     if ((name = g_get_application_name()))
75         ca_context_change_props(c, CA_PROP_APPLICATION_NAME, name, NULL);
76
77     g_static_private_set(&context_private, c, (GDestroyNotify) ca_context_destroy);
78
79     return c;
80 }
81
82 /**
83  * hildon_play_system_sound:
84  * @sample: sound file to play
85  * 
86  * Plays the given sample using libcanberra.
87  * Volume level is received from gconf.
88  *
89  * This method sets the "dialog-information" role for the sound played,
90  * so you need to keep this into account when using it. For any purpose, it
91  * is highly recommended that you use canberra-gtk instead of this method.
92  */
93 void 
94 hildon_play_system_sound(const gchar *sample)
95 {
96     float volume = 0;
97     int ret;
98     ca_context *ca_con = NULL;
99     ca_proplist *pl = NULL;
100
101 #if 0 /* FIXME: Check volume handling. Would be great not to use Gconf... */
102     GConfClient *client;
103     GConfValue *value;
104     gint gconf_vol;
105
106     /*
107      * The volume is from -0dB to -6dB,
108        The full volume is marked as 2 in gconf */
109     client = gconf_client_get_default ();
110     value = gconf_client_get (client, ALARM_GCONF_PATH, NULL);
111
112     /* We want error cases to match full volume, not silence, so
113        we do not want to use gconf_client_get_int */
114     if (!value || value->type != GCONF_VALUE_INT)
115         gconf_vol = 2;
116     else
117         gconf_vol = gconf_value_get_int(value);
118
119     if (value)
120         gconf_value_free(value);
121     g_object_unref (client);
122
123     volume = ((1.0 - (float)gconf_vol / 2.0)) * (-6.0);
124 #endif
125
126     ca_con = hildon_ca_context_get ();
127
128     ca_proplist_create(&pl);
129     ca_proplist_sets(pl, CA_PROP_MEDIA_FILENAME, sample);
130     ca_proplist_sets(pl, CA_PROP_MEDIA_ROLE, "dialog-information");
131     ca_proplist_setf(pl, CA_PROP_CANBERRA_VOLUME, "%f", volume);
132
133     ret = ca_context_play_full(ca_con, 0, pl, NULL, NULL);
134     g_debug("ca_context_play_full (vol %f): %s\n", volume, ca_strerror(ret));
135
136     ca_proplist_destroy(pl);
137 }