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