N#19140 System alerts still audible despite being turned off
[hildon] / hildon-widgets / hildon-system-sound.c
1 /*
2  * This file is part of hildon-libs
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Luc Pionchon <luc.pionchon@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; either 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 #include <gconf/gconf-client.h>
26 #include <esd.h>
27 #include "hildon-system-sound.h"
28
29 #define ALARM_GCONF_PATH "/apps/osso/sound/system_alert_volume"
30
31 /**
32  * hildon_play_system_sound:
33  * @sample: Sound file to play
34  * 
35  * Plays the given sample using esd sound daemon.
36  * Volume level is received from gconf. 
37  */
38 void hildon_play_system_sound(const gchar *sample)
39 {
40   GConfClient *client;
41   GConfValue *value;
42   gint volume, scale, sock, sample_id;
43
44   client = gconf_client_get_default();
45   value = gconf_client_get(client, ALARM_GCONF_PATH, NULL);
46
47   /* We want error cases to match full volume, not silence, so
48      we do not want to use gconf_client_get_int */
49   if (!value || value->type != GCONF_VALUE_INT) {
50     g_message("Failed to get volume level. Using default");
51     volume = 2;
52   }
53   else
54     volume = gconf_value_get_int(value);
55
56   if (value)
57     gconf_value_free(value);
58   g_object_unref(client);
59
60   switch (volume)
61   {
62     case 0:
63       g_message("System sounds are off");
64       return;
65     case 1:
66       scale = 0x80;
67       break;
68     case 2:
69     default:
70       scale = 0xff;
71       break;
72   };
73     
74   sock = esd_open_sound(NULL);
75   if (sock <= 0) {
76     g_warning("Failed to setup ESD");
77     return;
78   }
79
80   sample_id = esd_file_cache(sock, g_get_prgname(), sample);
81   if (sample_id < 0) {
82     close(sock);
83     g_warning("error while caching sample.");
84     return;
85   }
86   
87   g_message("Playing sample %s at volume %d", sample, scale);
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 }