From 1e386423679aec9cf5c94c7dc1218ea468e66d6d Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 14 Feb 2016 14:45:23 -0600 Subject: in bluez.vala, watch for org.bluez to be owned/unowned on the system bus --- src/bluez.vala | 45 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/bluez.vala b/src/bluez.vala index 80682cc..808a1cf 100644 --- a/src/bluez.vala +++ b/src/bluez.vala @@ -24,6 +24,7 @@ */ public class Bluez: Bluetooth, Object { + uint name_watch_id = 0; uint next_device_id = 1; ObjectManager manager; @@ -55,15 +56,6 @@ public class Bluez: Bluetooth, Object public Bluez (KillSwitch? killswitch) { - try - { - bus = Bus.get_sync (BusType.SYSTEM); - } - catch (Error e) - { - critical (@"$(e.message)"); - } - if ((killswitch != null) && (killswitch.is_valid())) { this.killswitch = killswitch; @@ -71,17 +63,44 @@ public class Bluez: Bluetooth, Object update_enabled (); } - reset_manager (); + name_watch_id = Bus.watch_name(BusType.SYSTEM, + "org.bluez", + BusNameWatcherFlags.AUTO_START, + on_bluez_appeared, + on_bluez_vanished); + } + + ~Bluez() + { + Bus.unwatch_name(name_watch_id); } - private void reset_manager () + private void on_bluez_appeared (GLib.DBusConnection connection, string name, string name_owner) + { + debug(@"name $name owned by $name_owner, setting up bluez proxies"); + + bus = connection; + + reset_bluez_lookup_vars(); + reset_manager(); + } + + private void on_bluez_vanished (GLib.DBusConnection connection, string name) + { + reset_bluez_lookup_vars(); + } + + private void reset_bluez_lookup_vars () { id_to_path = new HashTable (direct_hash, direct_equal); id_to_device = new HashTable (direct_hash, direct_equal); path_to_id = new HashTable (str_hash, str_equal); path_to_adapter_proxy = new HashTable (str_hash, str_equal); path_to_device_proxy = new HashTable (str_hash, str_equal); + } + private void reset_manager() + { try { manager = bus.get_proxy_sync ("org.bluez", "/"); @@ -134,6 +153,8 @@ public class Bluez: Bluetooth, Object private void update_adapter (ObjectPath object_path) { + debug(@"bluez5 calling update_adapter for $object_path"); + // Create a proxy if we don't have one var adapter_proxy = path_to_adapter_proxy.lookup (object_path); if (adapter_proxy == null) @@ -232,6 +253,8 @@ public class Bluez: Bluetooth, Object */ private void update_device (ObjectPath object_path) { + debug(@"bluez5 calling update_device for $object_path"); + // Create a proxy if we don't have one var device_proxy = path_to_device_proxy.lookup (object_path); if (device_proxy == null) -- cgit v1.2.3