2009-03-24 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-sound.c
index 7c1a78c..86de034 100644 (file)
@@ -1,14 +1,14 @@
 /*
- * This file is a part of hildon
+ * This file is a part of libhildon
  *
- * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
+ * Copyright (C) 2005-2008 Nokia Corporation. All rights reserved.
  *
- * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
+ * Contact: Kimmo Hämäläinen <kimmo.hamalainen@nokia.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
  * as published by the Free Software Foundation; version 2.1 of
- * the License.
+ * the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 
 /**
  * SECTION:hildon-sound
- * @short_description: An esd-based utility function for playing a sound
+ * @short_description: libcanberra-based utility function for playing a sound.
  * 
  */
 
-#include                                        "hildon-sound.h"
-#include                                        <gconf/gconf-client.h>
-#include                                        <esd.h>
-#include                                        <unistd.h>
+#include <unistd.h>
+#include <gconf/gconf-client.h>
+#include <canberra.h>
 
-#define                                         ALARM_GCONF_PATH \
-                                                "/apps/osso/sound/system_alert_volume"
+#include "hildon-sound.h"
+
+#define ALARM_GCONF_PATH "/apps/osso/sound/system_alert_volume"
 
 /**
  * hildon_play_system_sound:
  * @sample: sound file to play
  * 
- * Plays the given sample using esd sound daemon.
+ * Plays the given sample using libcanberra.
  * Volume level is received from gconf. 
  */
 void 
-hildon_play_system_sound                        (const gchar *sample)
+hildon_play_system_sound(const gchar *sample)
 {
+    float volume = 0;
+    int ret;
+    ca_context *ca_con = NULL;
+    ca_proplist *pl = NULL;
+
+#if 0 /* FIXME: Check volume handling. Would be great not to use Gconf... */
     GConfClient *client;
     GConfValue *value;
-    gint volume, scale, sock, sample_id;
+    gint gconf_vol;
 
+    /*
+     * The volume is from -0dB to -6dB,
+       The full volume is marked as 2 in gconf */
     client = gconf_client_get_default ();
     value = gconf_client_get (client, ALARM_GCONF_PATH, NULL);
 
     /* We want error cases to match full volume, not silence, so
        we do not want to use gconf_client_get_int */
     if (!value || value->type != GCONF_VALUE_INT)
-        volume = 2;
+        gconf_vol = 2;
     else
-        volume = gconf_value_get_int(value);
+        gconf_vol = gconf_value_get_int(value);
 
     if (value)
         gconf_value_free(value);
     g_object_unref (client);
 
-    switch (volume)
-    {
-        case 0:
-            return;
-        case 1:
-            scale = 0x80;
-            break;
-        case 2:
-        default:
-            scale = 0xff;
-            break;
-    };
+    volume = ((1.0 - (float)gconf_vol / 2.0)) * (-6.0);
+#endif
 
-    sock = esd_open_sound (NULL);
-    if (sock <= 0)
+    if ((ret = ca_context_create(&ca_con)) != CA_SUCCESS) {
+        g_warning("ca_context_create: %s\n", ca_strerror(ret));
         return;
-
-    sample_id = esd_file_cache (sock, g_get_prgname(), sample);
-    if (sample_id < 0) {
-        close(sock);
+    }
+    if ((ret = ca_context_open(ca_con)) != CA_SUCCESS) {
+        g_warning("ca_context_open: %s\n", ca_strerror(ret));
+        ca_context_destroy(ca_con);
         return;
     }
+    ca_proplist_create(&pl);
+    ca_proplist_sets(pl, CA_PROP_MEDIA_FILENAME, sample);
+    ca_proplist_setf(pl, CA_PROP_CANBERRA_VOLUME, "%f", volume);
+
+    ret = ca_context_play_full(ca_con, 0, pl, NULL, NULL);
+    g_debug("ca_context_play_full (vol %f): %s\n", volume, ca_strerror(ret));
 
-    esd_set_default_sample_pan (sock, sample_id, scale, scale);
-    esd_sample_play(sock, sample_id);
-    esd_sample_free(sock, sample_id);
-    close (sock);
+    ca_proplist_destroy(pl);
+    ca_context_destroy(ca_con);
 }