changeset 2646:7fbff3287a56

added device discovery in the plugin's prefs window
author Paula Stanciu <paula.stanciu@gmail.com>
date Sun, 25 May 2008 21:29:07 +0300
parents 5c769ade286a
children 5808b64d3f68
files src/bluetooth/Makefile src/bluetooth/bluetooth.c src/bluetooth/bluetooth.h src/bluetooth/gui.c src/bluetooth/gui.h
diffstat 5 files changed, 253 insertions(+), 64 deletions(-) [+]
line wrap: on
line diff
--- a/src/bluetooth/Makefile	Wed May 21 16:04:24 2008 +0300
+++ b/src/bluetooth/Makefile	Sun May 25 21:29:07 2008 +0300
@@ -1,6 +1,6 @@
 PLUGIN = bluetooth${PLUGIN_SUFFIX}
 
-SRCS = bluetooth.c gui.c
+SRCS = bluetooth.c gui.c marshal.c
 include ../../buildsys.mk
 include ../../extra.mk
 
--- a/src/bluetooth/bluetooth.c	Wed May 21 16:04:24 2008 +0300
+++ b/src/bluetooth/bluetooth.c	Sun May 25 21:29:07 2008 +0300
@@ -1,11 +1,31 @@
+#include <dbus/dbus.h>
+#include <dbus/dbus-glib.h>
+#include <glib-object.h>
+#include <stdio.h>
 #include "bluetooth.h"
+#include "marshal.h"
+#include "gui.h"
+
 #define DEBUG 1
 static gboolean plugin_active = FALSE,exiting=FALSE;
+GList * current_device = NULL;
+DBusGConnection * bus = NULL;
+gint discover_finish =0;
+GStaticMutex mutex = G_STATIC_MUTEX_INIT;
 
 void bluetooth_init ( void );
 void bluetooth_cleanup ( void );
 void bt_cfg(void);
 void bt_about(void);
+static void remote_device_found(DBusGProxy *object, char *address, const unsigned int class, const int rssi, gpointer user_data);
+static void discovery_started(DBusGProxy *object, gpointer user_data);
+static void remote_name_updated(DBusGProxy *object, const char *address,  char *name, gpointer user_data);
+static void print_results(void);
+static void discovery_completed(DBusGProxy *object, gpointer user_data);
+void discover_devices(void);
+
+
+
 GeneralPlugin bluetooth_gp =
 {
     .description = "Bluetooth audio suport",
@@ -16,21 +36,24 @@
 };
 GeneralPlugin *bluetooth_gplist[] = { &bluetooth_gp, NULL };
 DECLARE_PLUGIN(bluetooth_gp, NULL, NULL, NULL, NULL, NULL, bluetooth_gplist, NULL, NULL)
-
 void bluetooth_init ( void )
 {
-
+    audio_devices = NULL;
+    discover_devices();
 }
 
 void bluetooth_cleanup ( void )
 {
+    printf("bluetooth: exit\n");
+    dbus_g_connection_flush (bus);
+    dbus_g_connection_unref(bus);
 
 }
 /*void bt_cfg( void )
-{
+  {
 
-}
-*/
+  }
+  */
 void bt_about( void )
 {
 
@@ -38,10 +61,129 @@
 
 
 void refresh_call(void){
+    discover_devices()
     printf("refresh function called\n");
 }
 
 void connect_call(void){
-    printf("connect function \n");
+  printf("connect function \n");
+}
+
+
+static void remote_device_found(DBusGProxy *object, char *address, const unsigned int class, const int rssi, gpointer user_data)
+{
+    int found_in_list=FALSE; 
+    g_static_mutex_lock(&mutex);
+    current_device = audio_devices;
+    if((class & 0x200404)==0x200404)
+    {
+
+        while(current_device != NULL)
+        {
+            if(g_str_equal(address,((DeviceData*)(current_device->data))->address)) 
+            {
+                found_in_list = TRUE;
+                break;
+            }
+            current_device=g_list_next(current_device);
+        }
+        if(!found_in_list) 
+        {        
+            DeviceData *dev= g_new0(DeviceData, 1);
+            dev->class = class;
+            dev->address = g_strdup(address);
+            dev->name = NULL;
+            audio_devices=g_list_prepend(audio_devices, dev);     
+
+        }
+    }
+    g_static_mutex_unlock(&mutex);
+}
+
+static void discovery_started(DBusGProxy *object, gpointer user_data)
+{
+    g_print("Signal: DiscoveryStarted()\n");
 }
 
+static void remote_name_updated(DBusGProxy *object, const char *address,  char *name, gpointer user_data)
+{
+    g_static_mutex_lock(&mutex);
+    current_device=audio_devices;
+    while(current_device != NULL)
+    {
+        if(g_str_equal(address,((DeviceData*)(current_device->data))->address)) 
+        {
+            ((DeviceData*)(current_device->data))->name=g_strdup(name);
+            break;
+        }
+        current_device=g_list_next(current_device);
+    }
+    g_static_mutex_unlock(&mutex);
+}
+static void print_results()
+{
+    int i=0;
+    g_print("Final Scan results:\n");
+    g_print("Number of audio devices: %d \n",g_list_length(audio_devices));
+    current_device=audio_devices;
+    while(current_device != NULL)
+    {
+        g_print("Device %d: Name: %s, Class: 0x%x, Address: %s\n",++i,
+                ((DeviceData*)(current_device->data))-> name,
+                ((DeviceData*)(current_device->data))-> class,
+                ((DeviceData*)(current_device->data))-> address);
+
+        current_device=g_list_next(current_device);
+    }
+
+    refresh_tree();
+}
+
+static void discovery_completed(DBusGProxy *object, gpointer user_data)
+{
+    g_print("Signal: DiscoveryCompleted()\n");
+    print_results();
+
+
+}
+
+
+void discover_devices(void){
+    GError *error = NULL;
+    DBusGProxy * obj = NULL;
+    g_type_init();
+    g_log_set_always_fatal (G_LOG_LEVEL_WARNING);
+
+    bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
+    if (error != NULL)
+    {
+        g_printerr("Connecting to system bus failed: %s\n", error->message);
+        g_error_free(error);
+        exit(EXIT_FAILURE);
+    }
+    obj = dbus_g_proxy_new_for_name(bus, "org.bluez", "/org/bluez/hci0", "org.bluez.Adapter");
+
+    dbus_g_object_register_marshaller(marshal_VOID__STRING_UINT_INT, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID);
+    dbus_g_proxy_add_signal(obj, "RemoteDeviceFound", G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID);
+    dbus_g_proxy_connect_signal(obj, "RemoteDeviceFound", G_CALLBACK(remote_device_found), bus, NULL);
+
+    dbus_g_proxy_add_signal(obj, "DiscoveryStarted", G_TYPE_INVALID);
+    dbus_g_proxy_connect_signal(obj, "DiscoveryStarted", G_CALLBACK(discovery_started), bus, NULL);
+
+    dbus_g_proxy_add_signal(obj, "DiscoveryCompleted", G_TYPE_INVALID);
+    dbus_g_proxy_connect_signal(obj, "DiscoveryCompleted", G_CALLBACK(discovery_completed), bus, NULL);
+
+    dbus_g_object_register_marshaller(marshal_VOID__STRING_STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
+    dbus_g_proxy_add_signal(obj, "RemoteNameUpdated", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
+    dbus_g_proxy_connect_signal(obj, "RemoteNameUpdated", G_CALLBACK(remote_name_updated), NULL, NULL);
+
+    dbus_g_proxy_call(obj, "DiscoverDevices", &error, G_TYPE_INVALID, G_TYPE_INVALID);
+    if (error != NULL)
+    {
+        g_printerr("Failed to discover devices: %s\n", error->message);
+        g_error_free(error);
+        exit(EXIT_FAILURE);
+    }
+    // /  dbus_g_connection_flush (bus);
+    //    dbus_g_connection_unref(bus);
+}    
--- a/src/bluetooth/bluetooth.h	Wed May 21 16:04:24 2008 +0300
+++ b/src/bluetooth/bluetooth.h	Sun May 25 21:29:07 2008 +0300
@@ -8,5 +8,15 @@
 #include <gtk/gtk.h>
 #include <audacious/util.h>
 #include "gui.h"
+typedef struct {
+    guint class;
+    gchar* address;
+    gchar* name;
+}DeviceData;
+
+
+
 void refresh_call(void);
 void connect_call(void);
+GList * audio_devices;
+
--- a/src/bluetooth/gui.c	Wed May 21 16:04:24 2008 +0300
+++ b/src/bluetooth/gui.c	Sun May 25 21:29:07 2008 +0300
@@ -1,7 +1,5 @@
 #include "gui.h"
 #include "bluetooth.h"
-#include <gtk/gtk.h>
-#include <glib.h>
 
 static GtkWidget *window = NULL;
 static GtkTreeModel *model;
@@ -18,53 +16,97 @@
 GtkWidget *close_button;   
 GtkWidget *treeview;
 GtkWidget *label_p;
-GtkWidget *label_m;
+GtkWidget *label_c;
 GtkWidget *label_a;
 GtkWidget *label_prod;
-GtkWidget *label_model;
+GtkWidget *label_class;
 GtkWidget *label_address;
-
-
-typedef struct 
-{
-    gchar *producer;
-    gchar *model;
-}Headset;
-
+GList * dev = NULL;
 enum{
     COLUMN_PRODUCER,
-    COLUMN_MODEL,
     NUM_COLUMNS
 };
+static DeviceData test_data[]=
+{
+    {0,"00:00:00:00:00","Scanning"}
+};
 
-static Headset test_data[]=
-{
-    {"Motorola", "S9"},
-    {"Nokia", "BH-503"},
-    {"Blueant","Stereo X5"}
-};
 
 static GtkTreeModel * create_model(void)
 {
-    gint i = 0;
     GtkListStore *store;
     GtkTreeIter iter;
-    /* create list store */
+    gint i=0;
+   /* create list store */
     store = gtk_list_store_new(NUM_COLUMNS,
-            G_TYPE_STRING,
             G_TYPE_STRING);
+         
     /* add data to the list store */
     for(i = 0;i<G_N_ELEMENTS(test_data);i++)
     {
         gtk_list_store_append(store,&iter);
         gtk_list_store_set(store,&iter,
-                COLUMN_PRODUCER, test_data[i].producer,
-                COLUMN_MODEL, test_data[i].model,
-                -1);
+                COLUMN_PRODUCER, test_data[i].name,-1);
+    }
+
+        return GTK_TREE_MODEL(store);                       
+}
+static GtkTreeModel * rebuild_model(void)
+{
+
+    GtkListStore *store;
+    GtkTreeIter iter;
+    gint i=0;
+    gint dev_no=0;
+    GList *dev;
+    gchar *temp;
+   /* create list store */
+    store = gtk_list_store_new(NUM_COLUMNS,
+            G_TYPE_STRING);
+         
+    /*add inf to test_data from audio_devices */
+    dev_no = g_list_length(audio_devices);
+    dev = audio_devices;
+    while(dev != NULL)
+    {
+        test_data[i].name = ((DeviceData*)(dev->data))-> name;
+        test_data[i].class = ((DeviceData*)(dev->data))-> class;
+        test_data[i].address = ((DeviceData*)(dev->data))-> address;
+        i++;
+        dev=g_list_next(dev);
     }
-    return GTK_TREE_MODEL(store);                       
+    if (dev_no == 0) 
+    {
+        test_data[0].name = "No devices found!";
+        test_data[0].class = 0;
+        test_data[0].address = "00:00:00:00:00";
+    }
+
+    /* add data to the list store */
+    for(i = 0;i<G_N_ELEMENTS(test_data);i++)
+    {
+        gtk_list_store_append(store,&iter);
+        gtk_list_store_set(store,&iter,
+        COLUMN_PRODUCER, test_data[i].name,-1);
+    }
+        //set the labels
+         temp = g_strdup_printf("0x%x",test_data[0].class);
+         gtk_label_set_text(GTK_LABEL(label_prod),test_data[0].name);
+         gtk_label_set_text(GTK_LABEL(label_class),temp);
+         gtk_label_set_text(GTK_LABEL(label_address),test_data[0].address);
+
+        return GTK_TREE_MODEL(store);          
+
 }
 
+
+void refresh_tree()
+{
+     model = rebuild_model();
+     gtk_tree_view_set_model(GTK_TREE_VIEW(treeview),GTK_TREE_MODEL(model));
+}
+
+
 static void add_columns(GtkTreeView *treeview)
 {
     GtkCellRenderer *renderer;
@@ -81,16 +123,6 @@
     gtk_tree_view_column_set_sort_column_id (column,COLUMN_PRODUCER);
     gtk_tree_view_append_column (treeview, column);
 
-    /* column for model */
-    renderer = gtk_cell_renderer_text_new ();
-    column = gtk_tree_view_column_new_with_attributes ("Model",
-            renderer,
-            "text",
-            COLUMN_MODEL,
-            NULL);
-    gtk_tree_view_column_set_sort_column_id (column,COLUMN_MODEL);
-    gtk_tree_view_append_column (treeview, column);
-
 }
 
 void close_call(void){
@@ -102,18 +134,20 @@
 
     GtkTreeIter iter;
     gint sel;
+    gchar *temp;
     printf("select\n");
     GtkTreeSelection *selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(treeview)); 
     if(gtk_tree_selection_get_selected (selection, NULL,&iter)){
-      GtkTreePath *path;
-      path = gtk_tree_model_get_path (model, &iter);
-      sel = gtk_tree_path_get_indices (path)[0];
-      printf("i=%d\n",sel);
-      gtk_label_set_text(GTK_LABEL(label_prod),test_data[sel].producer);
-      gtk_label_set_text(GTK_LABEL(label_model),test_data[sel].model);
-      gtk_label_set_text(GTK_LABEL(label_address),"00:01:02:03:04:05");
-
-      gtk_tree_path_free (path);
+        GtkTreePath *path;
+        path = gtk_tree_model_get_path (model, &iter);
+        sel = gtk_tree_path_get_indices (path)[0];
+        printf("i=%d\n",sel);
+        temp = g_strdup_printf("0x%x",test_data[sel].class);
+        gtk_label_set_text(GTK_LABEL(label_prod),test_data[sel].name);
+        gtk_label_set_text(GTK_LABEL(label_class),temp);
+        gtk_label_set_text(GTK_LABEL(label_address),test_data[sel].address);
+        gtk_tree_path_free (path);
+        g_free(temp);
 
     }
 
@@ -122,7 +156,7 @@
 }
 void bt_cfg()
 {
-    
+
     if (!window)
     {
         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
@@ -168,30 +202,31 @@
         gtk_container_add (GTK_CONTAINER (headset_frame), treeview);
         /* add columns to the tree view */
         add_columns (GTK_TREE_VIEW (treeview));
+        
         g_signal_connect(treeview,"cursor-changed",G_CALLBACK(select_row),treeview);
 
 
         box_about = gtk_hbox_new(FALSE,4);
         gtk_container_set_border_width (GTK_CONTAINER (box_about), 4); 
         gtk_container_add (GTK_CONTAINER (about_frame), box_about);
-        
+
         /*about box left - vbox */
 
         box_about_left = gtk_vbox_new(FALSE,4);
         gtk_container_set_border_width (GTK_CONTAINER (box_about_left), 4); 
         gtk_container_add (GTK_CONTAINER (box_about), box_about_left);
-        
+
         /*about box right - vbox */
         box_about_right = gtk_vbox_new(TRUE,4);
         gtk_container_set_border_width (GTK_CONTAINER (box_about_right), 4); 
         gtk_container_add (GTK_CONTAINER (box_about), box_about_right);
 
         /* Left labels  */
-        label_p = gtk_label_new("Producer:");
+        label_p = gtk_label_new("Name:");
         gtk_container_add(GTK_CONTAINER(box_about_left),label_p);
 
-        label_m = gtk_label_new("Model:");
-        gtk_container_add(GTK_CONTAINER(box_about_left),label_m);
+        label_c = gtk_label_new("Class");
+        gtk_container_add(GTK_CONTAINER(box_about_left),label_c);
 
 
         label_a = gtk_label_new("Address:");
@@ -199,16 +234,16 @@
 
 
         /*right labels */
-        label_prod = gtk_label_new("Producer:");
+        label_prod = gtk_label_new("Scanning");
         gtk_container_add(GTK_CONTAINER(box_about_right),label_prod);
 
-        label_model = gtk_label_new("Model:");
-        gtk_container_add(GTK_CONTAINER(box_about_right),label_model);
+        label_class = gtk_label_new("  ");
+        gtk_container_add(GTK_CONTAINER(box_about_right),label_class);
 
 
-        label_address = gtk_label_new("Address:");
+        label_address = gtk_label_new("   ");
         gtk_container_add(GTK_CONTAINER(box_about_right),label_address);
-        
+
         gtk_window_set_default_size (GTK_WINDOW (window), 480, 180);
         if (!GTK_WIDGET_VISIBLE (window))
             gtk_widget_show_all (window);
--- a/src/bluetooth/gui.h	Wed May 21 16:04:24 2008 +0300
+++ b/src/bluetooth/gui.h	Sun May 25 21:29:07 2008 +0300
@@ -1,1 +1,3 @@
-#include <string.h>
+#include <gtk/gtk.h>
+#include <glib.h>
+void refresh_tree(void);