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