Mercurial > audlegacy-plugins
annotate src/bluetooth/bluetooth.c @ 2734:8a102490a8c0
compilation fix
| author | Paula Stanciu <paula.stanciu@gmail.com> |
|---|---|
| date | Mon, 30 Jun 2008 01:04:29 +0300 |
| parents | 1a27c497e526 |
| children | d3d71539d675 |
| 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 ){ |
|
1a27c497e526
indentation and some small bug fixing
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2728
diff
changeset
|
52 destroy_scan_window(); |
|
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 |
| 94 | |
| 2734 | 95 void refresh_call(void) |
| 96 { | |
| 97 printf("refresh function called\n"); | |
| 98 disconnect_dbus_signals(); | |
| 99 if(discover_finish == 0 ||discover_finish== 2){ | |
| 100 discover_finish = 0; | |
| 101 discover_devices(); | |
| 2728 | 102 } |
| 2734 | 103 else |
| 104 printf("Scanning please wait!\n"); | |
| 105 } | |
| 2644 | 106 |
| 2734 | 107 void connect_call(void) |
| 108 { | |
| 109 printf("connect function \n"); | |
| 110 } | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
111 |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
112 |
| 2734 | 113 static void remote_device_found(DBusGProxy *object, char *address, const unsigned int class, const int rssi, gpointer user_data) |
| 114 { | |
| 115 int found_in_list=FALSE; | |
| 116 g_static_mutex_lock(&mutex); | |
| 117 current_device = audio_devices; | |
| 118 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
|
119 { |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
120 while(current_device != NULL) |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
121 { |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
122 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
|
123 { |
| 2734 | 124 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
|
125 break; |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
126 } |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
127 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
|
128 } |
| 2734 | 129 if(!found_in_list) |
| 130 { | |
| 131 DeviceData *dev= g_new0(DeviceData, 1); | |
| 132 dev->class = class; | |
| 133 dev->address = g_strdup(address); | |
| 134 dev->name = NULL; | |
| 135 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
|
136 } |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
137 } |
| 2734 | 138 g_static_mutex_unlock(&mutex); |
| 139 } | |
| 140 | |
| 141 static void discovery_started(DBusGProxy *object, gpointer user_data) | |
| 142 { | |
| 143 g_print("Signal: DiscoveryStarted()\n"); | |
| 144 discover_finish = 1; | |
| 145 } | |
| 146 | |
| 147 static void remote_name_updated(DBusGProxy *object, const char *address, char *name, gpointer user_data) | |
| 148 { | |
| 149 g_static_mutex_lock(&mutex); | |
| 150 current_device=audio_devices; | |
| 151 while(current_device != NULL) | |
| 152 { | |
| 153 if(g_str_equal(address,((DeviceData*)(current_device->data))->address)) | |
| 154 { | |
| 155 ((DeviceData*)(current_device->data))->name=g_strdup(name); | |
| 156 break; | |
| 157 } | |
| 158 current_device=g_list_next(current_device); | |
| 159 } | |
| 160 g_static_mutex_unlock(&mutex); | |
| 161 } | |
| 162 | |
| 163 static void print_results() | |
| 164 { | |
| 165 int i=0; | |
| 166 g_print("Final Scan results:\n"); | |
| 167 devices_no = g_list_length(audio_devices); | |
| 168 g_print("Number of audio devices: %d \n",devices_no); | |
| 169 if(devices_no==0 ) { | |
| 170 if(config ==1) show_no_devices(); | |
| 171 } else { | |
| 172 current_device=audio_devices; | |
| 173 while(current_device != NULL) | |
| 174 { | |
| 175 g_print("Device %d: Name: %s, Class: 0x%x, Address: %s\n",++i, | |
| 176 ((DeviceData*)(current_device->data))-> name, | |
| 177 ((DeviceData*)(current_device->data))-> class, | |
| 178 ((DeviceData*)(current_device->data))-> address); | |
| 179 current_device=g_list_next(current_device); | |
| 180 } | |
| 181 destroy_scan_window(); | |
| 182 if(config==1) { | |
| 183 destroy_scan_window(); | |
| 184 results_ui(); | |
| 185 } | |
| 186 // refresh_tree(); | |
| 187 } | |
| 188 } | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
189 |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
190 |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
191 |
| 2734 | 192 static void discovery_completed(DBusGProxy *object, gpointer user_data) |
| 193 { | |
| 194 g_print("Signal: DiscoveryCompleted()\n"); | |
| 195 discover_finish =2; | |
| 196 print_results(); | |
| 197 } | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
198 |
|
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
199 |
| 2728 | 200 |
| 2734 | 201 void discover_devices(void) |
| 202 { | |
| 203 GError *error = NULL; | |
| 204 // g_type_init(); | |
| 205 g_log_set_always_fatal (G_LOG_LEVEL_WARNING); | |
| 206 bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error); | |
| 207 if (error != NULL) | |
| 208 { | |
| 209 g_printerr("Connecting to system bus failed: %s\n", error->message); | |
| 210 g_error_free(error); | |
| 211 } | |
| 212 obj = dbus_g_proxy_new_for_name(bus, "org.bluez", "/org/bluez/hci0", "org.bluez.Adapter"); | |
| 213 printf("bluetooth plugin - start discovery \n"); | |
| 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); | |
|
2646
7fbff3287a56
added device discovery in the plugin's prefs window
Paula Stanciu <paula.stanciu@gmail.com>
parents:
2644
diff
changeset
|
215 |
| 2734 | 216 dbus_g_proxy_add_signal(obj, "RemoteDeviceFound", G_TYPE_STRING, G_TYPE_UINT, G_TYPE_INT, G_TYPE_INVALID); |
| 217 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
|
218 |
| 2734 | 219 dbus_g_proxy_add_signal(obj, "DiscoveryStarted", G_TYPE_INVALID); |
| 220 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
|
221 |
| 2734 | 222 dbus_g_proxy_add_signal(obj, "DiscoveryCompleted", G_TYPE_INVALID); |
| 223 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
|
224 |
| 2734 | 225 dbus_g_object_register_marshaller(marshal_VOID__STRING_STRING, G_TYPE_NONE, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID); |
| 2728 | 226 |
| 2734 | 227 dbus_g_proxy_add_signal(obj, "RemoteNameUpdated", G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID); |
| 228 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
|
229 |
| 2734 | 230 dbus_g_proxy_call(obj, "DiscoverDevices", &error, G_TYPE_INVALID, G_TYPE_INVALID); |
| 231 if (error != NULL) | |
| 232 { | |
| 233 g_printerr("Failed to discover devices: %s\n", error->message); | |
| 234 g_error_free(error); | |
| 235 } | |
| 2728 | 236 |
| 2734 | 237 } |
