diff options
author | Lars Uebernickel <lars.uebernickel@canonical.com> | 2013-03-04 18:05:09 -0500 |
---|---|---|
committer | Lars Uebernickel <lars.uebernickel@canonical.com> | 2013-03-04 18:05:09 -0500 |
commit | a197977094bed8a424b0e5573f492d0c5ba96fb8 (patch) | |
tree | a5046b98434a0073dff86fb8cbd8bd2a3f85a094 /src | |
parent | ce02b10b74faab8a5934688b7e3759cba56864ef (diff) | |
download | ayatana-indicator-sound-a197977094bed8a424b0e5573f492d0c5ba96fb8.tar.gz ayatana-indicator-sound-a197977094bed8a424b0e5573f492d0c5ba96fb8.tar.bz2 ayatana-indicator-sound-a197977094bed8a424b0e5573f492d0c5ba96fb8.zip |
Mpris2Watcher: use match rules to avoid unnecessary wakeups
Previously, indicator-sound-service was waking up every time the owner of any
name on the bus changed.
Diffstat (limited to 'src')
-rw-r--r-- | src/mpris2-watcher.vala | 75 |
1 files changed, 52 insertions, 23 deletions
diff --git a/src/mpris2-watcher.vala b/src/mpris2-watcher.vala index 06ccb6e..cdfb9c6 100644 --- a/src/mpris2-watcher.vala +++ b/src/mpris2-watcher.vala @@ -21,8 +21,8 @@ using Xml; public class Mpris2Watcher : GLib.Object { - FreeDesktopObject fdesktop_obj; - + DBusConnection session_bus; + public signal void client_appeared ( string desktop_file_name, string dbus_name, bool use_playlists ); @@ -33,18 +33,37 @@ public class Mpris2Watcher : GLib.Object } construct - { + { + const string match_rule = "type='signal'," + + "sender='org.freedesktop.DBus'," + + "interface='org.freedesktop.DBus'," + + "member='NameOwnerChanged'," + + "path='/org/freedesktop/DBus'," + + "arg0namespace='org.mpris.MediaPlayer2'"; try { - this.fdesktop_obj = Bus.get_proxy_sync ( BusType.SESSION, - FREEDESKTOP_SERVICE, - FREEDESKTOP_OBJECT, - DBusProxyFlags.DO_NOT_LOAD_PROPERTIES ); - this.fdesktop_obj.name_owner_changed.connect (this.name_changes_detected); + this.session_bus = Bus.get_sync (BusType.SESSION); + + this.session_bus.call_sync ("org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "AddMatch", + new Variant ("(s)", match_rule), + VariantType.TUPLE, + DBusCallFlags.NONE, + -1); + + this.session_bus.signal_subscribe ("org.freedesktop.DBus", + "org.freedesktop.DBus", + "NameOwnerChanged", + "/org/freedesktop/DBus", + null, + DBusSignalFlags.NO_MATCH_RULE, + this.name_owner_changed); + this.check_for_active_clients.begin(); } - catch ( IOError e ){ - warning( "Mpris2watcher could not set up a watch for mpris clients appearing on the bus: %s", - e.message ); + catch (GLib.Error e) { + warning ("unable to set up name watch for mrpis clients: %s", e.message); } } @@ -52,16 +71,25 @@ public class Mpris2Watcher : GLib.Object // More relevant for development and daemon's like mpd. public async void check_for_active_clients() { - string[] interfaces; - try{ - interfaces = yield this.fdesktop_obj.list_names(); + Variant interfaces; + + try { + interfaces = yield this.session_bus.call ("org.freedesktop.DBus", + "/", + "org.freedesktop.DBus", + "ListNames", + null, + new VariantType ("(as)"), + DBusCallFlags.NONE, + -1); } - catch ( IOError e) { - warning( "Mpris2watcher could fetch active interfaces at startup: %s", - e.message ); + catch (GLib.Error e) { + warning ("unable to search for existing mpris clients: %s ", e.message); return; } - foreach (var address in interfaces) { + + foreach (var val in interfaces.get_child_value (0)) { + var address = (string) val; if (address.has_prefix (MPRIS_PREFIX)){ MprisRoot? mpris2_root = this.create_mpris_root(address); if (mpris2_root == null) return; @@ -71,13 +99,14 @@ public class Mpris2Watcher : GLib.Object } } - private void name_changes_detected ( FreeDesktopObject dbus_obj, - string name, - string previous_owner, - string current_owner ) + public void name_owner_changed (DBusConnection con, string sender, string object_path, + string interface_name, string signal_name, Variant parameters) { - MprisRoot? mpris2_root = this.create_mpris_root(name); + string name, previous_owner, current_owner; + + parameters.get ("(sss)", out name, out previous_owner, out current_owner); + MprisRoot? mpris2_root = this.create_mpris_root (name); if (mpris2_root == null) return; if (previous_owner != "" && current_owner == "") { |