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