1f30cee
diff -Nur audacious-plugins-2.2-orig/src/bluetooth/bluetooth.c audacious-plugins-2.2-bluetooth/src/bluetooth/bluetooth.c
1f30cee
--- audacious-plugins-2.2-orig/src/bluetooth/bluetooth.c	2009-11-22 23:49:52.000000000 +0100
1f30cee
+++ audacious-plugins-2.2-bluetooth/src/bluetooth/bluetooth.c	2009-12-19 19:48:34.000000000 +0100
57b40bd
@@ -29,6 +29,7 @@
57b40bd
 GList * current_device = NULL;
57b40bd
 gint config = 0;
57b40bd
 gint devices_no = 0;
57b40bd
+gboolean pcmdev_modified = FALSE;
57b40bd
 GStaticMutex mutex = G_STATIC_MUTEX_INIT;
57b40bd
 static gchar *current_address=NULL;
57b40bd
 static GThread *connect_th;
a5fc85d
@@ -58,44 +59,53 @@
57b40bd
 
57b40bd
 void bluetooth_init ( void )
57b40bd
 {
57b40bd
+    bonded_dev = NULL;
57b40bd
+    discover_finish = 0;
57b40bd
+    pcmdev_modified = FALSE;
57b40bd
     audio_devices = NULL;
57b40bd
     bus = NULL;
57b40bd
     obj = NULL;
57b40bd
-    gchar* bonded_addr="zz";
57b40bd
+    gchar* bonded_addr = NULL;
57b40bd
     discover_devices();
57b40bd
     mcs_handle_t *cfgfile ;
57b40bd
     cfgfile = aud_cfg_db_open();
57b40bd
     if(!aud_cfg_db_get_string(cfgfile, "BLUETOOTH_PLUGIN", "bonded",
57b40bd
-				  &bonded_addr))
57b40bd
+                              &bonded_addr)) {
57b40bd
+        aud_cfg_db_close(cfgfile);
57b40bd
         return;
57b40bd
+    }
57b40bd
     if(bonded_addr!=NULL && g_strcmp0(bonded_addr,"no")!=0)
57b40bd
         {
57b40bd
              remove_bonding(bonded_addr);
57b40bd
         }
57b40bd
+    free(bonded_addr);
57b40bd
     aud_cfg_db_close(cfgfile);
57b40bd
 
57b40bd
 }
a5fc85d
 
a5fc85d
 void bluetooth_cleanup ( void )
a5fc85d
 {
a5fc85d
-    printf("bluetooth: exit\n");
a5fc85d
     if (config ==1 )
a5fc85d
     {
57b40bd
         close_window();
57b40bd
         config =0;
57b40bd
     }
57b40bd
-    remove_bonding(bonded_dev);
57b40bd
+    if (bonded_dev) {
a5fc85d
+        printf("bluetooth: exit\n");
57b40bd
+        remove_bonding(bonded_dev);
57b40bd
+    }
57b40bd
     if(discover_finish == 2) {
57b40bd
         dbus_g_connection_flush (bus);
57b40bd
         dbus_g_connection_unref(bus);
57b40bd
         disconnect_dbus_signals();
57b40bd
 
57b40bd
     }
57b40bd
+    if (pcmdev_modified) {
57b40bd
     /* switching back to default pcm device at cleanup */
57b40bd
     mcs_handle_t *cfgfile = aud_cfg_db_open();
57b40bd
-    aud_cfg_db_set_string(cfgfile,"ALSA","pcm_device", "default");
1f30cee
+    aud_cfg_db_set_string(cfgfile,"alsa-gapless","pcm", "default");
57b40bd
     aud_cfg_db_close(cfgfile);
57b40bd
-
57b40bd
+    }
57b40bd
 }
57b40bd
 
57b40bd
 void bt_about( void )
57b40bd
@@ -243,8 +253,9 @@
57b40bd
     g_free(device_line);
57b40bd
     g_free(file_name);
57b40bd
     g_free(temp_file_name);
57b40bd
+    pcmdev_modified = TRUE;
57b40bd
     mcs_handle_t *cfgfile = aud_cfg_db_open();
57b40bd
-    aud_cfg_db_set_string(cfgfile,"ALSA","pcm_device", "audacious_bt");
1f30cee
+    aud_cfg_db_set_string(cfgfile,"alsa-gapless","pcm", "audacious_bt");
57b40bd
     aud_cfg_db_close(cfgfile);
57b40bd
 
57b40bd
     printf("play callback\n");
57b40bd
@@ -360,12 +371,13 @@
57b40bd
 {
57b40bd
     GError *error = NULL;
57b40bd
     //  g_type_init();
57b40bd
-    g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
57b40bd
+    /*    g_log_set_always_fatal (G_LOG_LEVEL_WARNING); */ /* too invasive */
57b40bd
     bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
57b40bd
     if (error != NULL)
57b40bd
     {
57b40bd
         g_printerr("Connecting to system bus failed: %s\n", error->message);
57b40bd
         g_error_free(error);
57b40bd
+        return;
57b40bd
     }
57b40bd
     obj = dbus_g_proxy_new_for_name(bus, "org.bluez", "/org/bluez/hci0", "org.bluez.Adapter");
57b40bd
     printf("bluetooth plugin - start discovery \n");