diff options
-rw-r--r-- | src/volume-control.vala | 167 |
1 files changed, 156 insertions, 11 deletions
diff --git a/src/volume-control.vala b/src/volume-control.vala index e994922..c5c11ca 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -1,4 +1,5 @@ /* + * -*- Mode:Vala; indent-tabs-mode:t; tab-width:4; encoding:utf8 -*- * Copyright 2013 Canonical Ltd. * * This program is free software; you can redistribute it and/or modify @@ -34,6 +35,11 @@ public class VolumeControl : Object private double _volume = 0.0; private double _mic_volume = 0.0; + private DBusProxy _user_proxy; + private DBusProxy _greeter_proxy; + private Cancellable _mute_cancellable; + private Cancellable _volume_cancellable; + public signal void volume_changed (double v); public signal void mic_volume_changed (double v); @@ -48,6 +54,10 @@ public class VolumeControl : Object if (loop == null) loop = new PulseAudio.GLibMainLoop (); + _mute_cancellable = new Cancellable (); + _volume_cancellable = new Cancellable (); + setup_accountsservice.begin (); + this.reconnect_to_pulse (); } @@ -212,14 +222,25 @@ public class VolumeControl : Object } /* Mute operations */ - public void set_mute (bool mute) + bool set_mute_internal (bool mute) { - return_if_fail (context.get_state () == Context.State.READY); + return_val_if_fail (context.get_state () == Context.State.READY, false); + + if (_mute != mute) { + if (mute) + context.get_sink_info_list (sink_info_list_callback_set_mute); + else + context.get_sink_info_list (sink_info_list_callback_unset_mute); + return true; + } else { + return false; + } + } - if (mute) - context.get_sink_info_list (sink_info_list_callback_set_mute); - else - context.get_sink_info_list (sink_info_list_callback_unset_mute); + public void set_mute (bool mute) + { + if (set_mute_internal (mute)) + sync_mute_to_accountsservice.begin (mute); } public void toggle_mute () @@ -274,13 +295,23 @@ public class VolumeControl : Object context.get_sink_info_by_name (i.default_sink_name, sink_info_set_volume_cb); } - public void set_volume (double volume) + bool set_volume_internal (double volume) { - return_if_fail (context.get_state () == Context.State.READY); - - _volume = volume; + return_val_if_fail (context.get_state () == Context.State.READY, false); + + if (_volume != volume) { + _volume = volume; + context.get_server_info (server_info_cb_for_set_volume); + return true; + } else { + return false; + } + } - context.get_server_info (server_info_cb_for_set_volume); + public void set_volume (double volume) + { + if (set_volume_internal (volume)) + sync_volume_to_accountsservice.begin (volume); } void set_mic_volume_success_cb (Context c, int success) @@ -315,4 +346,118 @@ public class VolumeControl : Object { return _mic_volume; } + + /* AccountsService operations */ + private void accountsservice_props_changed_cb (DBusProxy proxy, Variant changed_properties, string[] invalidated_properties) + { + Variant volume_variant = changed_properties.lookup_value ("Volume", new VariantType ("d")); + if (volume_variant != null) { + var volume = volume_variant.get_double (); + if (volume >= 0) + set_volume_internal (volume); + } + + Variant mute_variant = changed_properties.lookup_value ("Muted", new VariantType ("b")); + if (mute_variant != null) { + var mute = mute_variant.get_boolean (); + set_mute_internal (mute); + } + } + + private async void setup_user_proxy (string? username = null) + { + _user_proxy = null; + + // Look up currently selected greeter user, if asked + if (username == null) { + try { + var username_variant = yield _greeter_proxy.call ("GetActiveEntry", null, DBusCallFlags.NONE, -1); + username_variant.get ("(s)", out username); + if (username == "" || username == null) + return; + } catch (GLib.Error e) { + warning ("unable to find Accounts path for user %s: %s", username, e.message); + return; + } + } + + // Get master AccountsService object + DBusProxy accounts_proxy; + try { + accounts_proxy = yield DBusProxy.create_for_bus (BusType.SYSTEM, DBusProxyFlags.DO_NOT_LOAD_PROPERTIES | DBusProxyFlags.DO_NOT_CONNECT_SIGNALS, null, "org.freedesktop.Accounts", "/org/freedesktop/Accounts", "org.freedesktop.Accounts"); + } catch (GLib.Error e) { + warning ("unable to get greeter proxy: %s", e.message); + return; + } + + // Find user's AccountsService object + try { + var user_path_variant = yield accounts_proxy.call ("FindUserByName", new Variant ("(s)", username), DBusCallFlags.NONE, -1); + string user_path; + user_path_variant.get ("(o)", out user_path); + _user_proxy = yield DBusProxy.create_for_bus (BusType.SYSTEM, DBusProxyFlags.GET_INVALIDATED_PROPERTIES, null, "org.freedesktop.Accounts", user_path, "com.ubuntu.AccountsService.Sound"); + } catch (GLib.Error e) { + warning ("unable to find Accounts path for user %s: %s", username, e.message); + return; + } + + // Listen for property changes, this will get current volume (since getting proxy loads properties) + _user_proxy.g_properties_changed.connect (accountsservice_props_changed_cb); + } + + private void greeter_user_changed (string username) + { + setup_user_proxy.begin (username); + } + + private async void setup_accountsservice () + { + try { + _greeter_proxy = yield DBusProxy.create_for_bus (BusType.SESSION, DBusProxyFlags.NONE, null, "com.canonical.UnityGreeter", "/list", "com.canonical.UnityGreeter.List"); + } catch (GLib.Error e) { + warning ("unable to get greeter proxy: %s", e.message); + return; + } + + if (_greeter_proxy.get_name_owner () != null) { + _greeter_proxy.connect ("EntrySelected", greeter_user_changed); + yield setup_user_proxy (); + } else { + // We are in a user session. We just need our own proxy + var username = Environment.get_variable ("USER"); + if (username != "" && username != null) { + yield setup_user_proxy (username); + } + } + } + + private async void sync_mute_to_accountsservice (bool mute) + { + if (_user_proxy == null) + return; + + _mute_cancellable.cancel (); + _mute_cancellable.reset (); + + try { + yield _user_proxy.get_connection ().call (_user_proxy.get_name (), _user_proxy.get_object_path (), "org.freedesktop.DBus.Properties", "Set", new Variant ("(ssv)", _user_proxy.get_interface_name (), "Muted", new Variant ("b", mute)), null, DBusCallFlags.NONE, -1, _mute_cancellable); + } catch (GLib.Error e) { + warning ("unable to sync mute to AccountsService: %s", e.message); + } + } + + private async void sync_volume_to_accountsservice (double volume) + { + if (_user_proxy == null) + return; + + _volume_cancellable.cancel (); + _volume_cancellable.reset (); + + try { + yield _user_proxy.get_connection ().call (_user_proxy.get_name (), _user_proxy.get_object_path (), "org.freedesktop.DBus.Properties", "Set", new Variant ("(ssv)", _user_proxy.get_interface_name (), "Volume", new Variant ("d", volume)), null, DBusCallFlags.NONE, -1, _volume_cancellable); + } catch (GLib.Error e) { + warning ("unable to sync volume to AccountsService: %s", e.message); + } + } } |