76ce0ccd1edfb530458f40fccd7000f7c5128bdd
[hildon] / src / hildon-sound.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
5  *
6  * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@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.
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: An esd-based utility function for playing a sound
28  * 
29  */
30
31 #include <gconf/gconf-client.h>
32 #include <esd.h>
33 #include "hildon-sound.h"
34 #include <unistd.h>
35
36 #define ALARM_GCONF_PATH "/apps/osso/sound/system_alert_volume"
37
38 /**
39  * hildon_play_system_sound:
40  * @sample: sound file to play
41  * 
42  * Plays the given sample using esd sound daemon.
43  * Volume level is received from gconf. 
44  */
45 void hildon_play_system_sound(const gchar *sample)
46 {
47   GConfClient *client;
48   GConfValue *value;
49   gint volume, scale, sock, sample_id;
50
51   client = gconf_client_get_default();
52   value = gconf_client_get(client, ALARM_GCONF_PATH, NULL);
53
54   /* We want error cases to match full volume, not silence, so
55      we do not want to use gconf_client_get_int */
56   if (!value || value->type != GCONF_VALUE_INT)
57     volume = 2;
58   else
59     volume = gconf_value_get_int(value);
60
61   if (value)
62     gconf_value_free(value);
63   g_object_unref(client);
64
65   switch (volume)
66   {
67     case 0:
68       return;
69     case 1:
70       scale = 0x80;
71       break;
72     case 2:
73     default:
74       scale = 0xff;
75       break;
76   };
77     
78   sock = esd_open_sound(NULL);
79   if (sock <= 0)
80     return;
81
82   sample_id = esd_file_cache(sock, g_get_prgname(), sample);
83   if (sample_id < 0) {
84     close(sock);
85     return;
86   }
87   
88   esd_set_default_sample_pan(sock, sample_id, scale, scale);
89   esd_sample_play(sock, sample_id);
90   esd_sample_free(sock, sample_id);
91   close(sock);
92 }