diff options
author | charles kerr <charlesk@canonical.com> | 2015-12-31 13:05:06 -0600 |
---|---|---|
committer | charles kerr <charlesk@canonical.com> | 2015-12-31 13:05:06 -0600 |
commit | cbdeea528675b55b634aa9b15f94e64d36c99924 (patch) | |
tree | a59ef2744abb62619e91f01da56833f64b93a07c /src | |
parent | 49bd2eb793b65f4458af05ac75aff0d0cdfd8f5c (diff) | |
download | ayatana-indicator-sound-cbdeea528675b55b634aa9b15f94e64d36c99924.tar.gz ayatana-indicator-sound-cbdeea528675b55b634aa9b15f94e64d36c99924.tar.bz2 ayatana-indicator-sound-cbdeea528675b55b634aa9b15f94e64d36c99924.zip |
simplify some code in volume-warning and volume-warning-pulse
Diffstat (limited to 'src')
-rw-r--r-- | src/volume-warning-pulse.vala | 36 | ||||
-rw-r--r-- | src/volume-warning.vala | 73 |
2 files changed, 46 insertions, 63 deletions
diff --git a/src/volume-warning-pulse.vala b/src/volume-warning-pulse.vala index 9e0664c..c0a1b1a 100644 --- a/src/volume-warning-pulse.vala +++ b/src/volume-warning-pulse.vala @@ -39,7 +39,7 @@ public class VolumeWarningPulse : VolumeWarning ~VolumeWarningPulse () { clear_timer (ref _pulse_reconnect_timer); clear_timer (ref _update_sink_timer); - clear_timer (ref _update_sink_inputs_timer); + clear_timer (ref _pending_sink_inputs_timer); pulse_disconnect (); } @@ -62,15 +62,11 @@ public class VolumeWarningPulse : VolumeWarning _pulse_context.set_sink_volume_by_index (index, cvol); } - /*** - **** PulseAudio: Tracking the active multimedia sink input - ***/ - private unowned PulseAudio.GLibMainLoop _pgloop = null; private PulseAudio.Context _pulse_context = null; private uint _pulse_reconnect_timer = 0; private uint _update_sink_timer = 0; - private uint _update_sink_inputs_timer = 0; + private uint _pending_sink_inputs_timer = 0; private GenericSet<uint32> _pending_sink_inputs = new GenericSet<uint32>(direct_hash, direct_equal); private uint soon_interval_msec = 500; @@ -105,29 +101,21 @@ public class VolumeWarningPulse : VolumeWarning private void set_multimedia_sink_index (uint32 index) { - if (index == PulseAudio.INVALID_INDEX) { - _multimedia_sink_index = PulseAudio.INVALID_INDEX; - multimedia_volume = PulseAudio.Volume.INVALID; - } else if (_multimedia_sink_index != index) { + if (_multimedia_sink_index != index) { _multimedia_sink_index = index; + multimedia_volume = PulseAudio.Volume.INVALID; - update_multimedia_volume_soon (); + if (index != PulseAudio.INVALID_INDEX) + update_multimedia_volume_soon (); } } /***/ private bool is_active_multimedia (SinkInputInfo i) { + return (i.corked == 0) && + (i.proplist.gets(PulseAudio.Proplist.PROP_MEDIA_ROLE) == "multimedia"); - if (i.corked != 0) - return false; - - var key = PulseAudio.Proplist.PROP_MEDIA_ROLE; - var media_role = i.proplist.gets (key); - if (media_role != "multimedia") - return false; - - return true; } private void clear_multimedia () { @@ -163,11 +151,11 @@ public class VolumeWarningPulse : VolumeWarning _pending_sink_inputs.add (index); - if (_update_sink_inputs_timer == 0) { - _update_sink_inputs_timer = Timeout.add (soon_interval_msec, () => { + if (_pending_sink_inputs_timer == 0) { + _pending_sink_inputs_timer = Timeout.add (soon_interval_msec, () => { + _pending_sink_inputs_timer = 0; _pending_sink_inputs.foreach ((i) => update_sink_input (i)); _pending_sink_inputs.remove_all (); - _update_sink_inputs_timer = 0; return Source.REMOVE; }); } @@ -261,7 +249,7 @@ public class VolumeWarningPulse : VolumeWarning _pulse_context = new PulseAudio.Context (_pgloop.get_api(), null, props); _pulse_context.set_state_callback (pulse_context_state_callback); - var server_string = Environment.get_variable ("PULSE_SERVER"); + unowned string server_string = Environment.get_variable ("PULSE_SERVER"); if (_pulse_context.connect (server_string, Context.Flags.NOFAIL, null) < 0) GLib.warning ("pa_context_connect() failed: %s\n", PulseAudio.strerror(_pulse_context.errno())); } diff --git a/src/volume-warning.vala b/src/volume-warning.vala index d7f0e79..3c0f1e6 100644 --- a/src/volume-warning.vala +++ b/src/volume-warning.vala @@ -48,13 +48,14 @@ public abstract class VolumeWarning : Object _options = options; - init_all_properties (); + init_high_volume (); + init_approved (); _notification.user_responded.connect ((n, r) => on_user_response (r)); } ~VolumeWarning () { - stop_all_timers (); + clear_timer (ref _approved_timer); } /*** @@ -82,34 +83,29 @@ public abstract class VolumeWarning : Object } } - /*** - **** - ***/ - private IndicatorSound.Options _options; - private void init_all_properties () - { - init_high_volume (); - init_approved (); - } - - private void stop_all_timers () - { - stop_approved_timer (); - } - /** *** HIGH VOLUME PROPERTY **/ private void init_high_volume () { - this.notify["multimedia-volume"].connect (() => update_high_volume ()); - this.notify["multimedia-active"].connect (() => update_high_volume ()); - this.notify["headphones-active"].connect (() => update_high_volume ()); - this.notify["high-volume-approved"].connect (() => update_high_volume ()); - _options.notify["loud-volume"].connect (() => update_high_volume ()); - _options.notify["loud-warning-enabled"].connect (() => update_high_volume ()); + const string self_keys[] = { + "multimedia-volume", + "multimedia-active", + "headphones-active", + "high-volume-approved" + }; + foreach (var key in self_keys) + this.notify[key].connect (() => update_high_volume ()); + + const string options_keys[] = { + "loud-volume", + "loud-warning-enabled" + }; + foreach (var key in options_keys) + _options.notify[key].connect (() => update_high_volume ()); + update_high_volume (); } @@ -158,23 +154,22 @@ public abstract class VolumeWarning : Object update_approved_timer (); } private void update_approved_timer () { - stop_approved_timer (); - if (_approved_at != 0) { - int64 expires_at = _approved_at + _approved_ttl_usec; - int64 now = GLib.get_monotonic_time (); - if (expires_at > now) { - var seconds_left = 1 + ((expires_at - now) / 1000000); - _approved_timer = Timeout.add_seconds ((uint)seconds_left, on_approved_timer); - } - } - } - private void stop_approved_timer () { + clear_timer (ref _approved_timer); - } - private bool on_approved_timer () { - _approved_timer = 0; - update_approved (); - return Source.REMOVE; + + if (_approved_at == 0) + return; + + int64 expires_at = _approved_at + _approved_ttl_usec; + int64 now = GLib.get_monotonic_time (); + if (expires_at > now) { + var seconds_left = 1 + ((expires_at - now) / 1000000); + _approved_timer = Timeout.add_seconds ((uint)seconds_left, () => { + _approved_timer = 0; + update_approved (); + return Source.REMOVE; + }); + } } private void update_approved () { var new_approved = calculate_approved (); |