diff options
author | Ted Gould <ted@gould.cx> | 2014-11-05 10:36:40 -0600 |
---|---|---|
committer | Ted Gould <ted@gould.cx> | 2014-11-05 10:36:40 -0600 |
commit | 3c350eabc0e9eef095933819fc168d8183630eb5 (patch) | |
tree | f48dfc67201d98ec7cc885dfa912c057f00e8572 /src | |
parent | ec3a83ffca91bc42c494979f4d686367d85ed216 (diff) | |
parent | 1e193a4b99e62b52fb759d00a8745f1c477d965f (diff) | |
download | ayatana-indicator-sound-3c350eabc0e9eef095933819fc168d8183630eb5.tar.gz ayatana-indicator-sound-3c350eabc0e9eef095933819fc168d8183630eb5.tar.bz2 ayatana-indicator-sound-3c350eabc0e9eef095933819fc168d8183630eb5.zip |
Warn on high audio levels when using headphones (LP: #123633, #1373404)
Diffstat (limited to 'src')
-rw-r--r-- | src/service.vala | 16 | ||||
-rw-r--r-- | src/sound-menu.vala | 40 | ||||
-rw-r--r-- | src/volume-control.vala | 75 |
3 files changed, 123 insertions, 8 deletions
diff --git a/src/service.vala b/src/service.vala index 4298ad7..7daa6db 100644 --- a/src/service.vala +++ b/src/service.vala @@ -49,6 +49,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_actions ()); 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,6 +61,10 @@ 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 (); @@ -174,6 +179,8 @@ public class IndicatorSound.Service: Object { double v = this.volume_control.get_volume () + volume_step_percentage * delta; this.volume_control.set_volume (v.clamp (0.0, this.max_volume)); + /* TODO: Don't want to mess up the desktop today, but we should remove this + scrolling change and merge that into volume control's notification */ if (this.notification != null) { string icon; if (v <= 0.0) @@ -388,6 +395,15 @@ public class IndicatorSound.Service: Object { return volume_action; } + Action create_high_volume_actions () { + 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))); + + 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..3881faf 100644 --- a/src/sound-menu.vala +++ b/src/sound-menu.vala @@ -93,12 +93,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 +178,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 dd1128c..295ebf5 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -69,6 +69,7 @@ public class VolumeControl : Object private bool _send_next_local_volume = false; private double _account_service_volume = 0.0; private Notify.Notification _notification; + private bool _active_port_headphone = false; public signal void volume_changed (double v); public signal void mic_volume_changed (double v); @@ -79,6 +80,9 @@ 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; set; } + public VolumeControl () { if (loop == null) @@ -158,6 +162,8 @@ public class VolumeControl : Object private void sink_info_cb_for_props (Context c, SinkInfo? i, int eol) { + bool old_active_port_headphone = this._active_port_headphone; + if (i == null) return; @@ -174,12 +180,27 @@ 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); start_local_volume_timer(); + } else if (this._active_port_headphone != old_active_port_headphone) { + volume_changed (_volume); } } @@ -583,6 +604,7 @@ public class VolumeControl : Object { /* Using this to detect whether we're on the phone or not */ if (_pulse_use_stream_restore) { +<<<<<<< TREE if (volume == 0.0) _notification.update (_("Volume"), "", "audio-volume-muted"); if (volume > 0.0 && volume <= 0.33) @@ -591,14 +613,53 @@ public class VolumeControl : Object _notification.update (_("Volume"), "", "audio-volume-medium"); if (volume > 0.66 && volume <= 1.0) _notification.update (_("Volume"), "", "audio-volume-high"); +======= + /* Watch for extreme */ + if (volume > 0.75 && _active_port_headphone) + high_volume = true; + else + high_volume = false; + + /* Determine Label */ + string volume_label = ""; + if (high_volume) + volume_label = _("High volume"); + + /* Choose an icon */ + string icon = "audio-volume-muted"; + if (volume <= 0.0) + icon = "audio-volume-muted"; + else if (volume <= 0.3) + icon = "audio-volume-low"; + else if (volume <= 0.7) + icon = "audio-volume-medium"; + else + icon = "audio-volume-high"; + + /* Choose a sound */ + string? sound = null; + if (!((_active_sink_input >= 0) && (_active_sink_input < _valid_roles.length) + && (_valid_roles[_active_sink_input] == "multimedia"))) + sound = "/usr/share/sounds/ubuntu/stereo/message.ogg"; + + /* Check tint */ + string tint = "false"; + if (high_volume) + tint = "true"; + + /* Put it all into the notification */ + _notification.clear_hints (); + _notification.update (_("Volume"), volume_label, icon); _notification.set_hint ("value", (int32)(volume * 100.0)); - if (_active_sink_input == -1 || _valid_roles[_active_sink_input] != "multimedia") { - /* No audio ping if we're playing multimedia */ - _notification.set_hint ("sound-file", "/usr/share/sounds/ubuntu/stereo/message.ogg"); - } else { - _notification.set_hint ("sound-file", null); - } - + /* TODO: Removing sound until we can get all the roles cleaned up for + when to play it. We expect this to come back, but in another landing. + _notification.set_hint ("sound-file", sound); + */ + _notification.set_hint ("x-canonical-value-bar-tint", tint); + _notification.set_hint ("x-canonical-private-synchronous", "true"); + _notification.set_hint ("x-canonical-non-shaped-icon", "true"); + + /* Show it */ try { _notification.show (); } catch (GLib.Error e) { |