Bug 469635. Silence ALSA warnings when there is no sound device. r+sr=roc
authorMichael Ventnor <ventnor.bugzilla@gmail.com>
Wed, 17 Dec 2008 15:16:01 +1300
changeset 22877 bb18fee38c6a8113f82ebed293b1bc90857bb11b
parent 22876 62c3545aabadfefe90c98e3fd2e39a7567f48d9f
child 22878 05bf7c30877e68432efedaed9fc75852a4e4649a
push idunknown
push userunknown
push dateunknown
bugs469635
milestone1.9.2a1pre
Bug 469635. Silence ALSA warnings when there is no sound device. r+sr=roc
widget/src/gtk2/nsSound.cpp
--- a/widget/src/gtk2/nsSound.cpp
+++ b/widget/src/gtk2/nsSound.cpp
@@ -95,16 +95,36 @@ typedef int (*ca_context_play_fn) (ca_co
 typedef int (*ca_context_change_props_fn) (ca_context *c,
                                            ...);
 
 static ca_context_create_fn ca_context_create;
 static ca_context_destroy_fn ca_context_destroy;
 static ca_context_play_fn ca_context_play;
 static ca_context_change_props_fn ca_context_change_props;
 
+/* we provide a blank error handler to silence ALSA's stderr
+   messages on computers with no sound devices */
+typedef void (*snd_lib_error_handler_t) (const char* file,
+                                         int         line,
+                                         const char* function,
+                                         int         err,
+                                         const char* format,
+                                         ...);
+typedef int (*snd_lib_error_set_handler_fn) (snd_lib_error_handler_t handler);
+
+static void
+quiet_error_handler(const char* file,
+                    int         line,
+                    const char* function,
+                    int         err,
+                    const char* format,
+                    ...)
+{
+}
+
 NS_IMPL_ISUPPORTS2(nsSound, nsISound, nsIStreamLoaderObserver)
 
 ////////////////////////////////////////////////////////////////////////
 nsSound::nsSound()
 {
     mInited = PR_FALSE;
 }
 
@@ -161,16 +181,26 @@ nsSound::Init()
                 // at this point we know we have a good libcanberra library
                 ca_context_destroy = (ca_context_destroy_fn) PR_FindFunctionSymbol(libcanberra, "ca_context_destroy");
                 ca_context_play = (ca_context_play_fn) PR_FindFunctionSymbol(libcanberra, "ca_context_play");
                 ca_context_change_props = (ca_context_change_props_fn) PR_FindFunctionSymbol(libcanberra, "ca_context_change_props");
             }
         }
     }
 
+    PRLibrary* libasound = PR_LoadLibrary("libasound.so.2");
+    if (libasound) {
+        snd_lib_error_set_handler_fn snd_lib_error_set_handler =
+             (snd_lib_error_set_handler_fn) PR_FindFunctionSymbol(libasound, "snd_lib_error_set_handler");
+        if (snd_lib_error_set_handler)
+            snd_lib_error_set_handler(quiet_error_handler);
+
+        PR_UnloadLibrary(libasound);
+    }
+
     return NS_OK;
 }
 
 /* static */ void
 nsSound::Shutdown()
 {
     if (elib) {
         PR_UnloadLibrary(elib);