Mercurial > audlegacy-plugins
annotate src/bluetooth/bluetooth.c @ 2756:d3d71539d675
rescan functionality
| author | Paula Stanciu <paula.stanciu@gmail.com> |
|---|---|
| date | Tue, 01 Jul 2008 19:35:16 +0300 |
| parents | 8a102490a8c0 |
| children | 22a2ffe86750 |
| rev | line source |
|---|---|
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
1 #include <dbus/dbus.h> |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
2 #include <dbus/dbus-glib.h> |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
3 #include <glib-object.h> |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
4 #include <stdio.h> |
| 2644 | 5 #include "bluetooth.h" |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
6 #include "marshal.h" |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
7 #include "gui.h" |
| 2728 | 8 #include "scan_gui.h" |
| 2644 | 9 #define DEBUG 1 |
| 10 static gboolean plugin_active = FALSE,exiting=FALSE; | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
11 GList * current_device = NULL; |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
12 DBusGConnection * bus = NULL; |
| 2728 | 13 DBusGProxy * obj = NULL; |
| 14 gint config = 0; | |
| 15 gint devices_no = 0; | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
16 GStaticMutex mutex = G_STATIC_MUTEX_INIT; |
| 2644 | 17 |
| 18 void bluetooth_init ( void ); | |
| 19 void bluetooth_cleanup ( void ); | |
| 20 void bt_cfg(void); | |
| 21 void bt_about(void); | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
22 static void remote_device_found(DBusGProxy *object, char *address, const unsigned int class, const int rssi, gpointer user_data); |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
23 static void discovery_started(DBusGProxy *object, gpointer user_data); |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
24 static void remote_name_updated(DBusGProxy *object, const char *address, char *name, gpointer user_data); |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
25 static void print_results(void); |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
26 static void discovery_completed(DBusGProxy *object, gpointer user_data); |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
27 void discover_devices(void); |
| 2728 | 28 void disconnect_dbus_signals(void); |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
29 |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
30 |
| 2644 | 31 GeneralPlugin bluetooth_gp = |
| 32 { | |
|
2649
d891ba4be5a5
fixed crash when prefs window was closed while scanning
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2648
diff
changeset
|
33 .description = "Bluetooth audio support", |
| 2644 | 34 .init = bluetooth_init, |
| 35 .about = bt_about, | |
| 36 .configure = bt_cfg, | |
| 37 .cleanup = bluetooth_cleanup | |
| 38 }; | |
| 39 GeneralPlugin *bluetooth_gplist[] = { &bluetooth_gp, NULL }; | |
| 40 DECLARE_PLUGIN(bluetooth_gp, NULL, NULL, NULL, NULL, NULL, bluetooth_gplist, NULL, NULL) | |
| 2728 | 41 |
| 2644 | 42 void bluetooth_init ( void ) |
| 43 { | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
44 audio_devices = NULL; |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
45 discover_devices(); |
| 2644 | 46 } |
| 47 | |
| 48 void bluetooth_cleanup ( void ) | |
| 49 { | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
50 printf("bluetooth: exit\n"); |
|
2732
1a27c497e526
indentation and some small bug fixing
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2728
diff
changeset
|
51 if (config ==1 ){ |
|
2756
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
52 close_window(); |
|
2732
1a27c497e526
indentation and some small bug fixing
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2728
diff
changeset
|
53 config =0; |
|
2649
d891ba4be5a5
fixed crash when prefs window was closed while scanning
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2648
diff
changeset
|
54 } |
|
2732
1a27c497e526
indentation and some small bug fixing
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2728
diff
changeset
|
55 if(discover_finish == 2) { |
|
1a27c497e526
indentation and some small bug fixing
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2728
diff
changeset
|
56 dbus_g_connection_flush (bus); |
| 2734 | 57 dbus_g_connection_unref(bus); |
|
2732
1a27c497e526
indentation and some small bug fixing
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2728
diff
changeset
|
58 disconnect_dbus_signals(); |
| 2644 | 59 |
|
2732
1a27c497e526
indentation and some small bug fixing
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2728
diff
changeset
|
60 } |
| 2734 | 61 } |
|
2732
1a27c497e526
indentation and some small bug fixing
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2728
diff
changeset
|
62 |
| 2734 | 63 void bt_about( void ) |
| 64 { | |
| 65 printf("about call\n"); | |
| 66 show_scan(); | |
| 67 } | |
| 2728 | 68 |
| 2734 | 69 void bt_cfg(void) |
| 70 { | |
| 71 printf("bt_cfg\n"); | |
| 72 config =1; | |
| 73 if(discover_finish == 2){ | |
| 74 if (devices_no == 0){ | |
| 75 printf("no devs!\n"); | |
| 76 show_scan(); | |
| 77 show_no_devices(); | |
| 78 }else | |
| 79 results_ui(); | |
| 2728 | 80 } |
| 2734 | 81 else show_scan(); |
| 82 printf("end of bt_cfg\n"); | |
| 83 } | |
| 2728 | 84 |
| 2734 | 85 void disconnect_dbus_signals() |
| 86 { | |
| 87 dbus_g_proxy_disconnect_signal(obj, "RemoteDeviceFound", G_CALLBACK(remote_device_found), bus); | |
| 88 dbus_g_proxy_disconnect_signal(obj, "DiscoveryStarted", G_CALLBACK(discovery_started), bus); | |
| 89 dbus_g_proxy_disconnect_signal(obj, "DiscoveryCompleted", G_CALLBACK(discovery_completed), bus); | |
| 90 dbus_g_proxy_disconnect_signal(obj, "RemoteNameUpdated", G_CALLBACK(remote_name_updated), NULL); | |
| 2644 | 91 |
| 2734 | 92 } |
| 2644 | 93 |
|
2756
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
94 void clean_devices_list(){ |
|
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
95 g_list_free(audio_devices); |
|
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
96 dbus_g_connection_flush (bus); |
|
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
97 dbus_g_connection_unref(bus); |
|
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
98 audio_devices = NULL; |
|
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
99 //g_list_free(current_device); |
|
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
100 } |
| 2644 | 101 |
| 2734 | 102 void refresh_call(void) |
| 103 { | |
| 104 printf("refresh function called\n"); | |
| 105 disconnect_dbus_signals(); | |
|
2756
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
106 clean_devices_list(); |
| 2734 | 107 if(discover_finish == 0 ||discover_finish== 2){ |
| 108 discover_finish = 0; | |
|
2756
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
109 |
| 2734 | 110 discover_devices(); |
|
2756
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
111 close_window(); |
|
d3d71539d675
rescan functionality
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2734
diff
changeset
|
112 show_scan(); |
| 2728 | 113 } |
| 2734 | 114 else |
| 115 printf("Scanning please wait!\n"); | |
| 116 } | |
| 2644 | 117 |
| 2734 | 118 void connect_call(void) |
| 119 { | |
| 120 printf("connect function \n"); | |
| 121 } | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
122 |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
123 |
| 2734 | 124 static void remote_device_found(DBusGProxy *object, char *address, const unsigned int class, const int rssi, gpointer user_data) |
| 125 { | |
| 126 int found_in_list=FALSE; | |
| 127 g_static_mutex_lock(&mutex); | |
| 128 current_device = audio_devices; | |
| 129 if((class & 0x200404)==0x200404) | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
130 { |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
131 while(current_device != NULL) |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
132 { |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
133 if(g_str_equal(address,((DeviceData*)(current_device->data))->address)) |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
134 { |
| 2734 | 135 found_in_list = TRUE; |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
136 break; |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
137 } |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
138 current_device=g_list_next(current_device); |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
139 } |
| 2734 | 140 if(!found_in_list) |
| 141 { | |
| 142 DeviceData *dev= g_new0(DeviceData, 1); | |
| 143 dev->class = class; | |
| 144 dev->address = g_strdup(address); | |
| 145 dev->name = NULL; | |
| 146 audio_devices=g_list_prepend(audio_devices, dev); | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
147 } |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
148 } |
| 2734 | 149 g_static_mutex_unlock(&mutex); |
| 150 } | |
| 151 | |
| 152 static void discovery_started(DBusGProxy *object, gpointer user_data) | |
| 153 { | |
| 154 g_print("Signal: DiscoveryStarted()\n"); | |
| 155 discover_finish = 1; | |
| 156 } | |
| 157 | |
| 158 static void remote_name_updated(DBusGProxy *object, const char *address, char *name, gpointer user_data) | |
| 159 { | |
| 160 g_static_mutex_lock(&mutex); | |
| 161 current_device=audio_devices; | |
| 162 while(current_device != NULL) | |
| 163 { | |
| 164 if(g_str_equal(address,((DeviceData*)(current_device->data))->address)) | |
| 165 { | |
| 166 ((DeviceData*)(current_device->data))->name=g_strdup(name); | |
| 167 break; | |
| 168 } | |
| 169 current_device=g_list_next(current_device); | |
| 170 } | |
| 171 g_static_mutex_unlock(&mutex); | |
| 172 } | |
| 173 | |
| 174 static void print_results() | |
| 175 { | |
| 176 int i=0; | |
| 177 g_print("Final Scan results:\n"); | |
| 178 devices_no = g_list_length(audio_devices); | |
| 179 g_print("Number of audio devices: %d \n",devices_no); | |
| 180 if(devices_no==0 ) { | |
| 181 if(config ==1) show_no_devices(); | |
| 182 } else { | |
| 183 current_device=audio_devices; | |
| 184 while(current_device != NULL) | |
| 185 { | |
| 186 g_print("Device %d: Name: %s, Class: 0x%x, Address: %s\n",++i, | |
| 187 ((DeviceData*)(current_device->data))-> name, | |
| 188 ((DeviceData*)(current_device->data))-> class, | |
| 189 ((DeviceData*)(current_device->data))-> address); | |
| 190 current_device=g_list_next(current_device); | |
| 191 } | |
| 192 destroy_scan_window(); | |
| 193 if(config==1) { | |
| 194 destroy_scan_window(); | |
| 195 results_ui(); | |
| 196 } | |
| 197 // refresh_tree(); | |
| 198 } | |
| 199 } | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
200 |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
201 |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
202 |
| 2734 | 203 static void discovery_completed(DBusGProxy *object, gpointer user_data) |
| 204 { | |
| 205 g_print("Signal: DiscoveryCompleted()\n"); | |
| 206 discover_finish =2; | |
| 207 print_results(); | |
| 208 } | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
209 |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
210 |
| 2728 | 211 |
| 2734 | 212 void discover_devices(void) |
| 213 { | |
| 214 GError *error = NULL; | |
| 215 // g_type_init(); | |
| 216 g_log_set_always_fatal (G_LOG_LEVEL_WARNING); | |
| 217 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); | |
| 218 if (error != NULL) | |
| 219 { | |
| 220 g_printerr("Connecting to system bus failed: %s\n", error->message); | |
| 221 g_error_free(error); | |
| 222 } | |
| 223 obj = dbus_g_proxy_new_for_name(bus, "org.bluez", "/org/bluez/hci0", "org.bluez.Adapter"); | |
| 224 printf("bluetooth plugin - start discovery \n"); | |
| 225 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); | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
226 |
| 2734 | 227 dbus_g_proxy_add_signal(obj, "RemoteDeviceFound", G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID); |
| 228 dbus_g_proxy_connect_signal(obj, "RemoteDeviceFound", G_CALLBACK(remote_device_found), bus, NULL); | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
229 |
| 2734 | 230 dbus_g_proxy_add_signal(obj, "DiscoveryStarted", G_TYPE_INVALID); |
| 231 dbus_g_proxy_connect_signal(obj, "DiscoveryStarted", G_CALLBACK(discovery_started), bus, NULL); | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
232 |
| 2734 | 233 dbus_g_proxy_add_signal(obj, "DiscoveryCompleted", G_TYPE_INVALID); |
| 234 dbus_g_proxy_connect_signal(obj, "DiscoveryCompleted", G_CALLBACK(discovery_completed), bus, NULL); | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
235 |
| 2734 | 236 dbus_g_object_register_marshaller(marshal_VOID__STRING_STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID); |
| 2728 | 237 |
| 2734 | 238 dbus_g_proxy_add_signal(obj, "RemoteNameUpdated", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID); |
| 239 dbus_g_proxy_connect_signal(obj, "RemoteNameUpdated", G_CALLBACK(remote_name_updated), NULL, NULL); | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
240 |
| 2734 | 241 dbus_g_proxy_call(obj, "DiscoverDevices", &error, G_TYPE_INVALID, G_TYPE_INVALID); |
| 242 if (error != NULL) | |
| 243 { | |
| 244 g_printerr("Failed to discover devices: %s\n", error->message); | |
| 245 g_error_free(error); | |
| 246 } | |
| 2728 | 247 |
| 2734 | 248 } |
