From 73e9aa2ecbb49d2597f2e07348ec039698c842f3 Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Wed, 16 Apr 2014 13:10:14 +0100 Subject: Added heuristic account services update timer --- src/volume-control.vala | 73 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 69 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/volume-control.vala b/src/volume-control.vala index 889c2d6..bd66224 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -47,6 +47,10 @@ public class VolumeControl : Object private GreeterListInterface _greeter_proxy; private Cancellable _mute_cancellable; private Cancellable _volume_cancellable; + private uint _local_volume_timer = 0; + 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); @@ -75,6 +79,8 @@ public class VolumeControl : Object Source.remove (_reconnect_timer); _reconnect_timer = 0; } + stop_local_volume_timer(); + stop_account_service_volume_timer(); } /* PulseAudio logic*/ @@ -193,7 +199,7 @@ public class VolumeControl : Object _reconnect_timer = Timeout.add_seconds (2, reconnect_timeout); break; - default: + default: this.ready = false; break; } @@ -335,7 +341,7 @@ public class VolumeControl : Object public void set_volume (double volume) { if (set_volume_internal (volume)) - sync_volume_to_accountsservice.begin (volume); + start_local_volume_timer(); } void set_mic_volume_success_cb (Context c, int success) @@ -377,8 +383,11 @@ public class VolumeControl : Object 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); + if (volume >= 0) { + _account_service_volume = volume; + // we need to wait for this to settle. + start_account_service_volume_timer(); + } } Variant mute_variant = changed_properties.lookup_value ("Muted", new VariantType ("b")); @@ -487,4 +496,60 @@ public class VolumeControl : Object warning ("unable to sync volume to AccountsService: %s", e.message); } } + + private void start_local_volume_timer() + { + // perform a slow sync with the accounts service. max at 1 per second. + + if (_local_volume_timer == 0) { + sync_volume_to_accountsservice.begin (_volume); + _local_volume_timer = Timeout.add_seconds (1, local_volume_changed_timeout); + } else { + _send_next_local_volume = true; + } + } + + private void stop_local_volume_timer() + { + if (_local_volume_timer != 0) { + Source.remove (_local_volume_timer); + _local_volume_timer = 0; + } + } + + bool local_volume_changed_timeout() + { + if (_send_next_local_volume == true) { + _send_next_local_volume = false; + + sync_volume_to_accountsservice.begin (_volume); + } + _local_volume_timer = 0; + return false; // G_SOURCE_REMOVE + } + + private void start_account_service_volume_timer() + { + stop_account_service_volume_timer(); + _accountservice_volume_timer = Timeout.add_seconds (2, accountservice_volume_changed_timeout); + } + + private void stop_account_service_volume_timer() + { + if (_accountservice_volume_timer != 0) { + Source.remove (_accountservice_volume_timer); + _accountservice_volume_timer = 0; + } + } + + bool accountservice_volume_changed_timeout () + { + // if the local value hasn't changed. + if (_local_volume_timer == 0) { + _accountservice_volume_timer = 0; + set_volume_internal (_account_service_volume); + return false; // G_SOURCE_REMOVE + } + return true; // G_SOURCE_CONTINUE + } } -- cgit v1.2.3 From ad57381fcfd07795691f50e4d51806fda8df4fa8 Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Wed, 16 Apr 2014 16:48:21 +0100 Subject: continue update timer if another change has come in --- src/volume-control.vala | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/volume-control.vala b/src/volume-control.vala index bd66224..8312cd6 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -523,6 +523,7 @@ public class VolumeControl : Object _send_next_local_volume = false; sync_volume_to_accountsservice.begin (_volume); + return true; // G_SOURCE_CONTINUE } _local_volume_timer = 0; return false; // G_SOURCE_REMOVE -- cgit v1.2.3 From 3c9c5f55e8a876fba36f2cd1f36cf2a5881f5846 Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Wed, 16 Apr 2014 21:55:04 +0100 Subject: review changes --- src/volume-control.vala | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/volume-control.vala b/src/volume-control.vala index 8312cd6..ccc81d1 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -501,6 +501,9 @@ public class VolumeControl : Object { // perform a slow sync with the accounts service. max at 1 per second. + // stop the AS update timer, as since we're going to be setting the volume. + stop_account_service_volume_timer(); + if (_local_volume_timer == 0) { sync_volume_to_accountsservice.begin (_volume); _local_volume_timer = Timeout.add_seconds (1, local_volume_changed_timeout); @@ -532,7 +535,7 @@ public class VolumeControl : Object private void start_account_service_volume_timer() { stop_account_service_volume_timer(); - _accountservice_volume_timer = Timeout.add_seconds (2, accountservice_volume_changed_timeout); + _accountservice_volume_timer = Timeout.add_seconds (1, accountservice_volume_changed_timeout); } private void stop_account_service_volume_timer() @@ -545,12 +548,8 @@ public class VolumeControl : Object bool accountservice_volume_changed_timeout () { - // if the local value hasn't changed. - if (_local_volume_timer == 0) { - _accountservice_volume_timer = 0; - set_volume_internal (_account_service_volume); - return false; // G_SOURCE_REMOVE - } - return true; // G_SOURCE_CONTINUE + _accountservice_volume_timer = 0; + set_volume_internal (_account_service_volume); + return false; // G_SOURCE_REMOVE } } -- cgit v1.2.3 From d0852028e0b47a1e2116d789b57475d27adb2591 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Fri, 18 Apr 2014 13:20:53 -0400 Subject: Consolidate local_volume_changed_timeout --- src/volume-control.vala | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/volume-control.vala b/src/volume-control.vala index ccc81d1..ebca606 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -522,13 +522,11 @@ public class VolumeControl : Object bool local_volume_changed_timeout() { - if (_send_next_local_volume == true) { + _local_volume_timer = 0; + if (_send_next_local_volume) { _send_next_local_volume = false; - - sync_volume_to_accountsservice.begin (_volume); - return true; // G_SOURCE_CONTINUE + start_local_volume_timer(); } - _local_volume_timer = 0; return false; // G_SOURCE_REMOVE } -- cgit v1.2.3 From d0c4fa8edbe45b99ee91d26f8fc5a16f08fa4c28 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Fri, 18 Apr 2014 13:41:32 -0400 Subject: And make it so we update from an AS notification immediately if we haven't been messing with local volume recently --- src/volume-control.vala | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/volume-control.vala b/src/volume-control.vala index ebca606..2a10917 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -532,8 +532,15 @@ public class VolumeControl : Object private void start_account_service_volume_timer() { - stop_account_service_volume_timer(); - _accountservice_volume_timer = Timeout.add_seconds (1, accountservice_volume_changed_timeout); + if (_accountservice_volume_timer == 0) { + // If we haven't been messing with local volume recently, apply immediately. + if (_local_volume_timer == 0 && !set_volume_internal (_account_service_volume)) { + return; + } + // Else check again in another second if needed. + // (if AS is throwing us lots of notifications, we update at most once a second) + _accountservice_volume_timer = Timeout.add_seconds (1, accountservice_volume_changed_timeout); + } } private void stop_account_service_volume_timer() @@ -547,7 +554,7 @@ public class VolumeControl : Object bool accountservice_volume_changed_timeout () { _accountservice_volume_timer = 0; - set_volume_internal (_account_service_volume); + start_account_service_volume_timer (); return false; // G_SOURCE_REMOVE } } -- cgit v1.2.3 From 05c6df41b18d44b9b3debb567dd551e356cd0694 Mon Sep 17 00:00:00 2001 From: Michael Terry Date: Fri, 18 Apr 2014 13:41:44 -0400 Subject: Whitespace change --- src/volume-control.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/volume-control.vala b/src/volume-control.vala index 2a10917..2efa186 100644 --- a/src/volume-control.vala +++ b/src/volume-control.vala @@ -525,7 +525,7 @@ public class VolumeControl : Object _local_volume_timer = 0; if (_send_next_local_volume) { _send_next_local_volume = false; - start_local_volume_timer(); + start_local_volume_timer (); } return false; // G_SOURCE_REMOVE } -- cgit v1.2.3