diff options
author | Ted Gould <ted@gould.cx> | 2015-01-28 16:43:18 -0600 |
---|---|---|
committer | Ted Gould <ted@gould.cx> | 2015-01-28 16:43:18 -0600 |
commit | af63886e47b8262189d54712dbb34383ecf83669 (patch) | |
tree | 4be682091795d3477cc4792d8a4d6b618f0b827c /src | |
parent | 8210778317cd8f8be803f4afa0200bd9cad0f68e (diff) | |
parent | 8fc91cb9823afc3b4dcaa03422c3dc4611ab034f (diff) | |
download | ayatana-indicator-sound-af63886e47b8262189d54712dbb34383ecf83669.tar.gz ayatana-indicator-sound-af63886e47b8262189d54712dbb34383ecf83669.tar.bz2 ayatana-indicator-sound-af63886e47b8262189d54712dbb34383ecf83669.zip |
Bring in the PA Mock
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/service.vala | 165 | ||||
-rw-r--r-- | src/sound-menu.vala | 41 | ||||
-rw-r--r-- | src/volume-control.vala | 107 |
4 files changed, 226 insertions, 89 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c18726c..b6f006a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -163,7 +163,6 @@ add_library( target_link_libraries( indicator-sound-service-lib - ${PULSEAUDIO_LIBRARIES} ${SOUNDSERVICE_LIBRARIES} ) @@ -187,6 +186,7 @@ set_target_properties( target_link_libraries( indicator-sound-service-bin indicator-sound-service-lib + ${PULSEAUDIO_LIBRARIES} ) ########################### diff --git a/src/service.vala b/src/service.vala index 2a65492..c7e29e7 100644 --- a/src/service.vala +++ b/src/service.vala @@ -19,6 +19,8 @@ public class IndicatorSound.Service: Object { public Service (MediaPlayerList playerlist) { + sync_notification = new Notify.Notification(_("Volume"), "", "audio-volume-muted"); + this.settings = new Settings ("com.canonical.indicator.sound"); this.sharedsettings = new Settings ("com.ubuntu.sound"); @@ -49,6 +51,7 @@ public class IndicatorSound.Service: Object { this.actions.add_action (this.create_mute_action ()); this.actions.add_action (this.create_volume_action ()); this.actions.add_action (this.create_mic_volume_action ()); + this.actions.add_action (this.create_high_volume_action ()); this.menus = new HashTable<string, SoundMenu> (str_hash, str_equal); this.menus.insert ("desktop_greeter", new SoundMenu (null, SoundMenu.DisplayFlags.SHOW_MUTE | SoundMenu.DisplayFlags.HIDE_PLAYERS | SoundMenu.DisplayFlags.GREETER_PLAYERS)); @@ -60,20 +63,28 @@ public class IndicatorSound.Service: Object { this.volume_control.bind_property ("active-mic", menu, "show-mic-volume", BindingFlags.SYNC_CREATE); }); + this.menus.@foreach ( (profile, menu) => { + this.volume_control.bind_property ("high-volume", menu, "show-high-volume-warning", BindingFlags.SYNC_CREATE); + }); + this.sync_preferred_players (); this.settings.changed["interested-media-players"].connect ( () => { this.sync_preferred_players (); }); - if (settings.get_boolean ("show-notify-osd-on-scroll")) { - List<string> caps = Notify.get_server_caps (); - if (caps.find_custom ("x-canonical-private-synchronous", strcmp) != null) { - this.notification = new Notify.Notification ("indicator-sound", "", ""); - this.notification.set_hint_string ("x-canonical-private-synchronous", "indicator-sound"); - } - } - sharedsettings.bind ("allow-amplified-volume", this, "allow-amplified-volume", SettingsBindFlags.GET); + + /* Hide the notification when the menu is shown */ + var shown_action = actions.lookup_action ("indicator-shown") as SimpleAction; + shown_action.change_state.connect ((state) => { + if (state.get_boolean()) { + try { + sync_notification.close(); + } catch (Error e) { + warning("Unable to close synchronous volume notification: %s", e.message); + } + } + }); } ~Service() { @@ -126,6 +137,9 @@ public class IndicatorSound.Service: Object { } set { + if (this.allow_amplified_volume == value) + return; + if (value) { /* from pulse/volume.h: #define PA_VOLUME_UI_MAX (pa_sw_volume_from_dB(+11.0)) */ this.max_volume = (double)PulseAudio.Volume.sw_from_dB(11.0) / PulseAudio.Volume.NORM; @@ -135,7 +149,7 @@ public class IndicatorSound.Service: Object { } /* Normalize volume, because the volume action's state is [0.0, 1.0], see create_volume_action() */ - this.actions.change_action_state ("volume", this.volume_control.get_volume () / this.max_volume); + this.actions.change_action_state ("volume", this.volume_control.volume / this.max_volume); } } @@ -144,6 +158,7 @@ public class IndicatorSound.Service: Object { { "scroll", activate_scroll_action, "i", null, null }, { "desktop-settings", activate_desktop_settings, null, null, null }, { "phone-settings", activate_phone_settings, null, null, null }, + { "indicator-shown", null, null, "@b false", null }, }; MainLoop loop; @@ -156,10 +171,10 @@ public class IndicatorSound.Service: Object { uint player_action_update_id; bool mute_blocks_sound; uint sound_was_blocked_timeout_id; - Notify.Notification notification; bool syncing_preferred_players = false; AccountsServiceUser? accounts_service = null; bool export_to_accounts_service = false; + private Notify.Notification sync_notification; /* Maximum volume as a scaling factor between the volume action's state and the value in * this.volume_control. See create_volume_action(). @@ -171,29 +186,8 @@ public class IndicatorSound.Service: Object { void activate_scroll_action (SimpleAction action, Variant? param) { int delta = param.get_int32(); /* positive for up, negative for down */ - double v = this.volume_control.get_volume () + volume_step_percentage * delta; - this.volume_control.set_volume (v.clamp (0.0, this.max_volume)); - - if (this.notification != null) { - string icon; - if (v <= 0.0) - icon = "notification-audio-volume-off"; - else if (v <= 0.3) - icon = "notification-audio-volume-low"; - else if (v <= 0.7) - icon = "notification-audio-volume-medium"; - else - icon = "notification-audio-volume-high"; - - this.notification.update ("indicator-sound", "", icon); - this.notification.set_hint_int32 ("value", ((int32) (100 * v / this.max_volume)).clamp (-1, 101)); - try { - this.notification.show (); - } - catch (Error e) { - warning ("unable to show notification: %s", e.message); - } - } + double v = this.volume_control.volume + volume_step_percentage * delta; + this.volume_control.volume = v.clamp (0.0, this.max_volume); } void activate_desktop_settings (SimpleAction action, Variant? param) { @@ -228,7 +222,7 @@ public class IndicatorSound.Service: Object { } void update_root_icon () { - double volume = this.volume_control.get_volume (); + double volume = this.volume_control.volume; string icon; if (this.volume_control.mute) icon = this.mute_blocks_sound ? "audio-volume-muted-blocking-panel" : "audio-volume-muted-panel"; @@ -258,6 +252,63 @@ public class IndicatorSound.Service: Object { root_action.set_state (builder.end()); } + /* TODO: Update these if the notification server leaves the bus and restarts */ + private bool check_sync_notification = false; + private bool support_sync_notification = false; + + void update_sync_notification () { + if (!check_sync_notification) { + List<string> caps = Notify.get_server_caps (); + if (caps.find_custom ("x-canonical-private-synchronous", strcmp) != null) { + support_sync_notification = true; + } + check_sync_notification = true; + } + + if (!support_sync_notification) + return; + + var shown_action = actions.lookup_action ("indicator-shown") as SimpleAction; + if (shown_action != null && shown_action.get_state().get_boolean()) + return; + + /* Determine Label */ + string volume_label = ""; + if (volume_control.high_volume) + volume_label = _("High volume"); + + /* Choose an icon */ + string icon = "audio-volume-muted"; + if (volume_control.volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume_control.volume <= 0.3) + icon = "audio-volume-low"; + else if (volume_control.volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + + /* Check tint */ + string tint = "false"; + if (volume_control.high_volume) + tint = "true"; + + /* Put it all into the notification */ + sync_notification.clear_hints (); + sync_notification.update (_("Volume"), volume_label, icon); + sync_notification.set_hint ("value", (int32)(volume_control.volume * 100.0)); + sync_notification.set_hint ("x-canonical-value-bar-tint", tint); + sync_notification.set_hint ("x-canonical-private-synchronous", "true"); + sync_notification.set_hint ("x-canonical-non-shaped-icon", "true"); + + /* Show it */ + try { + sync_notification.show (); + } catch (GLib.Error e) { + warning("Unable to send volume change notification: %s", e.message); + } + } + Action create_silent_mode_action () { bool silentNow = false; if (this.accounts_service != null) { @@ -332,15 +383,6 @@ public class IndicatorSound.Service: Object { return mute_action; } - void volume_changed (double volume) { - var volume_action = this.actions.lookup_action ("volume") as SimpleAction; - - /* Normalize volume, because the volume action's state is [0.0, 1.0], see create_volume_action() */ - volume_action.set_state (new Variant.double (volume / this.max_volume)); - - this.update_root_icon (); - } - Action create_volume_action () { /* The action's state is between be in [0.0, 1.0] instead of [0.0, * max_volume], so that we don't need to update the slider menu item @@ -349,23 +391,31 @@ public class IndicatorSound.Service: Object { * volume_control.set_volume(). */ - double volume = this.volume_control.get_volume () / this.max_volume; + double volume = this.volume_control.volume / this.max_volume; var volume_action = new SimpleAction.stateful ("volume", VariantType.INT32, new Variant.double (volume)); volume_action.change_state.connect ( (action, val) => { double v = val.get_double () * this.max_volume; - volume_control.set_volume (v.clamp (0.0, this.max_volume)); + volume_control.volume = v.clamp (0.0, this.max_volume); }); /* activating this action changes the volume by the amount given in the parameter */ volume_action.activate.connect ( (action, param) => { int delta = param.get_int32 (); - double v = volume_control.get_volume () + volume_step_percentage * delta; - volume_control.set_volume (v.clamp (0.0, this.max_volume)); + double v = volume_control.volume + volume_step_percentage * delta; + volume_control.volume = v.clamp (0.0, this.max_volume); }); - this.volume_control.volume_changed.connect (volume_changed); + this.volume_control.notify["volume"].connect (() => { + var vol_action = this.actions.lookup_action ("volume") as SimpleAction; + + /* Normalize volume, because the volume action's state is [0.0, 1.0], see create_volume_action() */ + vol_action.set_state (new Variant.double (this.volume_control.volume / this.max_volume)); + + this.update_root_icon (); + this.update_sync_notification (); + }); this.volume_control.bind_property ("ready", volume_action, "enabled", BindingFlags.SYNC_CREATE); @@ -373,14 +423,14 @@ public class IndicatorSound.Service: Object { } Action create_mic_volume_action () { - var volume_action = new SimpleAction.stateful ("mic-volume", null, new Variant.double (this.volume_control.get_mic_volume ())); + var volume_action = new SimpleAction.stateful ("mic-volume", null, new Variant.double (this.volume_control.mic_volume)); volume_action.change_state.connect ( (action, val) => { - volume_control.set_mic_volume (val.get_double ()); + volume_control.mic_volume = val.get_double (); }); - this.volume_control.mic_volume_changed.connect ( (volume) => { - volume_action.set_state (new Variant.double (volume)); + this.volume_control.notify["mic-volume"].connect ( () => { + volume_action.set_state (new Variant.double (this.volume_control.mic_volume)); }); this.volume_control.bind_property ("ready", volume_action, "enabled", BindingFlags.SYNC_CREATE); @@ -388,6 +438,17 @@ public class IndicatorSound.Service: Object { return volume_action; } + Action create_high_volume_action () { + var high_volume_action = new SimpleAction.stateful("high-volume", null, new Variant.boolean (this.volume_control.high_volume)); + + this.volume_control.notify["high-volume"].connect( () => { + high_volume_action.set_state(new Variant.boolean (this.volume_control.high_volume)); + update_sync_notification(); + }); + + return high_volume_action; + } + void bus_acquired (DBusConnection connection, string name) { try { connection.export_action_group ("/com/canonical/indicator/sound", this.actions); diff --git a/src/sound-menu.vala b/src/sound-menu.vala index f245a1f..96dd143 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -60,6 +60,7 @@ public class SoundMenu: Object root_item.set_attribute ("x-canonical-type", "s", "com.canonical.indicator.root"); root_item.set_attribute ("x-canonical-scroll-action", "s", "indicator.scroll"); root_item.set_attribute ("x-canonical-secondary-action", "s", "indicator.mute"); + root_item.set_attribute ("submenu-action", "s", "indicator.indicator-shown"); root_item.set_submenu (this.menu); this.root = new Menu (); @@ -93,12 +94,49 @@ public class SoundMenu: Object this.mic_volume_shown = true; } else if (!value && this.mic_volume_shown) { - this.volume_section.remove (this.volume_section.get_n_items () -1); + int location = -1; + while ((location = find_action(this.volume_section, "indicator.mic-volume")) != -1) { + this.volume_section.remove (location); + } this.mic_volume_shown = false; } } } + public bool show_high_volume_warning { + get { + return this.high_volume_warning_shown; + } + set { + if (value && !this.high_volume_warning_shown) { + /* NOTE: Action doesn't really exist, just used to find below when removing */ + var item = new MenuItem(_("High volume can damage your hearing."), "indicator.high-volume-warning-item"); + volume_section.append_item (item); + this.high_volume_warning_shown = true; + } + else if (!value && this.high_volume_warning_shown) { + int location = -1; + while ((location = find_action(this.volume_section, "indicator.high-volume-warning-item")) != -1) { + this.volume_section.remove (location); + } + this.high_volume_warning_shown = false; + } + } + } + + int find_action (Menu menu, string in_action) { + int n = menu.get_n_items (); + for (int i = 0; i < n; i++) { + string action; + menu.get_item_attribute (i, "action", "s", out action); + if (in_action == action) + return i; + } + + return -1; + } + + public void add_player (MediaPlayer player) { if (this.notify_handlers.contains (player)) return; @@ -141,6 +179,7 @@ public class SoundMenu: Object Menu volume_section; bool mic_volume_shown; bool settings_shown = false; + bool high_volume_warning_shown = false; bool hide_inactive; bool hide_players = false; HashTable<MediaPlayer, ulong> notify_handlers; diff --git a/src/volume-control.vala b/src/volume-control.vala index ad186a7..193e621 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -19,6 +19,7 @@ */ using PulseAudio; +using Notify; using Gee; [CCode(cname="pa_cvolume_set", cheader_filename = "pulse/volume.h")] @@ -67,9 +68,7 @@ public class VolumeControl : Object private uint _accountservice_volume_timer = 0; private bool _send_next_local_volume = false; private double _account_service_volume = 0.0; - - public signal void volume_changed (double v); - public signal void mic_volume_changed (double v); + private bool _active_port_headphone = false; /** true when connected to the pulse server */ public bool ready { get; set; } @@ -77,6 +76,13 @@ public class VolumeControl : Object /** true when a microphone is active **/ public bool active_mic { get; private set; default = false; } + /** true when high volume warnings should be shown */ + public bool high_volume { + get { + return this._volume > 0.75 && _active_port_headphone; + } + } + public VolumeControl () { if (loop == null) @@ -84,6 +90,7 @@ public class VolumeControl : Object _mute_cancellable = new Cancellable (); _volume_cancellable = new Cancellable (); + setup_accountsservice.begin (); this.reconnect_to_pulse (); @@ -149,6 +156,8 @@ public class VolumeControl : Object private void sink_info_cb_for_props (Context c, SinkInfo? i, int eol) { + bool old_high_volume = this.high_volume; + if (i == null) return; @@ -165,12 +174,29 @@ public class VolumeControl : Object this.notify_property ("is-playing"); } + /* Check if the current active port is headset/headphone */ + /* There is not easy way to check if the port is a headset/headphone besides + * checking for the port name. On touch (with the pulseaudio droid element) + * the headset/headphone port is called 'output-headset' and 'output-headphone'. + * On the desktop this is usually called 'analog-output-headphones' */ + if (i.active_port.name == "output-wired_headset" || + i.active_port.name == "output-wired_headphone" || + i.active_port.name == "analog-output-headphones") { + _active_port_headphone = true; + } else { + _active_port_headphone = false; + } + if (_pulse_use_stream_restore == false && _volume != volume_to_double (i.volume.max ())) { _volume = volume_to_double (i.volume.max ()); - volume_changed (_volume); + this.notify_property("volume"); start_local_volume_timer(); + } + + if (this.high_volume != old_high_volume) { + this.notify_property("high-volume"); } } @@ -182,7 +208,7 @@ public class VolumeControl : Object if (_mic_volume != volume_to_double (i.volume.values[0])) { _mic_volume = volume_to_double (i.volume.values[0]); - mic_volume_changed (_mic_volume); + this.notify_property ("mic-volume"); } } @@ -238,7 +264,7 @@ public class VolumeControl : Object if (volume != cvolume) { /* Someone else changed the volume for this role, reflect on the indicator */ _volume = volume_to_double (volume); - volume_changed (_volume); + this.notify_property("volume"); start_local_volume_timer(); } } @@ -281,7 +307,7 @@ public class VolumeControl : Object iter.next ("(uu)", &type, &volume); _volume = volume_to_double (volume); - volume_changed (_volume); + this.notify_property("volume"); start_local_volume_timer(); } catch (GLib.Error e) { warning ("unable to get volume for active role %s (%s)", sink_input_objp, e.message); @@ -498,7 +524,7 @@ public class VolumeControl : Object private void set_volume_success_cb (Context c, int success) { if ((bool)success) - volume_changed (_volume); + this.notify_property("volume"); } private void sink_info_set_volume_cb (Context c, SinkInfo? i, int eol) @@ -544,7 +570,7 @@ public class VolumeControl : Object new Variant ("(ssv)", "org.PulseAudio.Ext.StreamRestore1.RestoreEntry", "Volume", volume), null, DBusCallFlags.NONE, -1); - volume_changed (_volume); + this.notify_property("volume"); } catch (GLib.Error e) { lock (_pa_volume_sig_count) { _pa_volume_sig_count--; @@ -559,27 +585,29 @@ public class VolumeControl : Object return false; if (_volume != volume) { + var old_high_volume = this.high_volume; + _volume = volume; if (_pulse_use_stream_restore) set_volume_active_role.begin (); else context.get_server_info (server_info_cb_for_set_volume); + + this.notify_property("volume"); + + if (this.high_volume != old_high_volume) + this.notify_property("high-volume"); + return true; } else { return false; } } - public void set_volume (double volume) - { - if (set_volume_internal (volume)) - start_local_volume_timer(); - } - void set_mic_volume_success_cb (Context c, int success) { if ((bool)success) - mic_volume_changed (_mic_volume); + this.notify_property ("mic-volume"); } void set_mic_volume_get_server_info_cb (PulseAudio.Context c, PulseAudio.ServerInfo? i) { @@ -590,23 +618,28 @@ public class VolumeControl : Object } } - public void set_mic_volume (double volume) - { - return_if_fail (context.get_state () == Context.State.READY); - - _mic_volume = volume; - - context.get_server_info (set_mic_volume_get_server_info_cb); + public double volume { + get { + return _volume; + } + set { + if (set_volume_internal (value)) { + start_local_volume_timer(); + } + } } - public double get_volume () - { - return _volume; - } + public double mic_volume { + get { + return _mic_volume; + } + set { + return_if_fail (context.get_state () == Context.State.READY); - public double get_mic_volume () - { - return _mic_volume; + _mic_volume = value; + + context.get_server_info (set_mic_volume_get_server_info_cb); + } } /* PulseAudio Dbus (Stream Restore) logic */ @@ -689,7 +722,7 @@ public class VolumeControl : Object } /* AccountsService operations */ - private void accountsservice_props_changed_cb (DBusProxy proxy, Variant changed_properties, string[] invalidated_properties) + 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) { @@ -747,10 +780,14 @@ public class VolumeControl : Object // Get current values and listen for changes _user_proxy.g_properties_changed.connect (accountsservice_props_changed_cb); - var props_variant = yield _user_proxy.get_connection ().call (_user_proxy.get_name (), _user_proxy.get_object_path (), "org.freedesktop.DBus.Properties", "GetAll", new Variant ("(s)", _user_proxy.get_interface_name ()), null, DBusCallFlags.NONE, -1); - Variant props; - props_variant.get ("(@a{sv})", out props); - accountsservice_props_changed_cb(_user_proxy, props, null); + try { + var props_variant = yield _user_proxy.get_connection ().call (_user_proxy.get_name (), _user_proxy.get_object_path (), "org.freedesktop.DBus.Properties", "GetAll", new Variant ("(s)", _user_proxy.get_interface_name ()), null, DBusCallFlags.NONE, -1); + Variant props; + props_variant.get ("(@a{sv})", out props); + accountsservice_props_changed_cb(_user_proxy, props, null); + } catch (GLib.Error e) { + debug("Unable to get properties for user %s at first try: %s", username, e.message); + } } private void greeter_user_changed (string username) |