From 78b734afc2cb908f8cf50ced43d398d35c363cce Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Mon, 17 Jun 2024 17:49:53 -0700 Subject: [PATCH] audio: Use volume subscription to detect changes Fixes https://github.com/pop-os/cosmic-applets/issues/301. Should be combined with the subscription code here to avoid duplication, and two things using pulse. --- Cargo.lock | 4 +-- Cargo.toml | 1 + cosmic-applet-audio/Cargo.toml | 1 + cosmic-applet-audio/src/lib.rs | 47 +++++++++++++++++++------------- cosmic-applet-battery/Cargo.toml | 4 +-- 5 files changed, 33 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b47e29c2..b28ea2da 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -929,6 +929,7 @@ dependencies = [ name = "cosmic-applet-audio" version = "0.1.1" dependencies = [ + "cosmic-settings-subscriptions", "cosmic-time", "i18n-embed 0.14.1", "i18n-embed-fl 0.8.0", @@ -964,7 +965,6 @@ dependencies = [ "tracing-log", "tracing-subscriber", "udev", - "upower_dbus", "zbus 4.2.2", ] @@ -5308,7 +5308,7 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67" [[package]] name = "smithay-client-toolkit" version = "0.18.0" -source = "git+https://github.com/smithay/client-toolkit?rev=3bed072#3bed072b966022f5f929d12f3aff089b1ace980b" +source = "git+https://github.com/smithay/client-toolkit//?rev=3bed072#3bed072b966022f5f929d12f3aff089b1ace980b" dependencies = [ "bitflags 2.5.0", "bytemuck", diff --git a/Cargo.toml b/Cargo.toml index 8d2cbeaf..40321a5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ cctk = { git = "https://github.com/pop-os/cosmic-protocols", package = "cosmic-c cosmic-protocols = { git = "https://github.com/pop-os/cosmic-protocols", default-features = false, features = [ "client", ], rev = "c8d3a1c" } +cosmic-settings-subscriptions = { git = "https://github.com/pop-os/cosmic-settings-subscriptions" } cosmic-time = { git = "https://github.com/pop-os/cosmic-time", default-features = false, features = [ "libcosmic", "once_cell", diff --git a/cosmic-applet-audio/Cargo.toml b/cosmic-applet-audio/Cargo.toml index df6a4ccf..f6a87902 100644 --- a/cosmic-applet-audio/Cargo.toml +++ b/cosmic-applet-audio/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" license = "GPL-3.0" [dependencies] +cosmic-settings-subscriptions.workspace = true cosmic-time.workspace = true i18n-embed-fl.workspace = true i18n-embed.workspace = true diff --git a/cosmic-applet-audio/src/lib.rs b/cosmic-applet-audio/src/lib.rs index 7e67ae1e..9dcc02b8 100644 --- a/cosmic-applet-audio/src/lib.rs +++ b/cosmic-applet-audio/src/lib.rs @@ -32,6 +32,7 @@ use cosmic::widget::Row; use cosmic::widget::{divider, icon}; use cosmic::Renderer; use cosmic::{Element, Theme}; +use cosmic_settings_subscriptions::pulse as sub_pulse; use cosmic_time::{anim, chain, id, once_cell::sync::Lazy, Instant, Timeline}; use iced::wayland::popup::{destroy_popup, get_popup}; use iced::widget::container; @@ -64,6 +65,10 @@ pub struct Audio { current_input: Option, outputs: Vec, inputs: Vec, + sink_mute: bool, + sink_volume: u32, + source_mute: bool, + source_volume: u32, pulse_state: PulseState, popup: Option, timeline: Timeline, @@ -78,19 +83,13 @@ impl Audio { } fn output_icon_name(&self) -> &'static str { - let Some(output) = self.current_output.as_ref() else { - return "audio-volume-muted-symbolic"; - }; - - let volume = output.volume.avg(); - let output_volume = volume_to_percent(volume); - if volume.is_muted() { + if self.sink_mute || self.sink_volume == 0 { "audio-volume-muted-symbolic" - } else if output_volume < 33.0 { + } else if self.sink_volume < 33 { "audio-volume-low-symbolic" - } else if output_volume < 66.0 { + } else if self.sink_volume < 66 { "audio-volume-medium-symbolic" - } else if output_volume <= 100.0 { + } else if self.sink_volume <= 100 { "audio-volume-high-symbolic" } else { "audio-volume-overamplified-symbolic" @@ -102,17 +101,11 @@ impl Audio { } fn input_icon_name(&self) -> &'static str { - let Some(input) = self.current_input.as_ref() else { - return "microphone-sensitivity-muted-symbolic"; - }; - - let volume = input.volume.avg(); - let input_volume = volume_to_percent(volume); - if volume.is_muted() || input_volume == 0.0 { + if self.source_mute || self.source_volume == 0 { "microphone-sensitivity-muted-symbolic" - } else if input_volume < 33.0 { + } else if self.source_volume < 33 { "microphone-sensitivity-low-symbolic" - } else if input_volume < 66.0 { + } else if self.source_volume < 66 { "microphone-sensitivity-medium-symbolic" } else { "microphone-sensitivity-high-symbolic" @@ -145,6 +138,7 @@ pub enum Message { MprisRequest(MprisRequest), Token(TokenUpdate), OpenSettings, + PulseSub(sub_pulse::Event), } impl Audio { @@ -540,6 +534,20 @@ impl cosmic::Application for Audio { cosmic::process::spawn(cmd); } }, + Message::PulseSub(event) => match event { + sub_pulse::Event::SinkVolume(value) => { + self.sink_volume = value; + } + sub_pulse::Event::SinkMute(value) => { + self.sink_mute = value; + } + sub_pulse::Event::SourceVolume(value) => { + self.source_volume = value; + } + sub_pulse::Event::SourceMute(value) => { + self.source_mute = value; + } + }, }; Command::none() @@ -559,6 +567,7 @@ impl cosmic::Application for Audio { }), mpris_subscription::mpris_subscription(0).map(Message::Mpris), activation_token_subscription(0).map(Message::Token), + sub_pulse::subscription().map(Message::PulseSub), ]) } diff --git a/cosmic-applet-battery/Cargo.toml b/cosmic-applet-battery/Cargo.toml index a8926942..edc8a0ce 100644 --- a/cosmic-applet-battery/Cargo.toml +++ b/cosmic-applet-battery/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" license = "GPL-3.0" [dependencies] +cosmic-settings-subscriptions.workspace = true cosmic-time.workspace = true drm = "0.11.1" futures.workspace = true @@ -19,6 +20,3 @@ tracing-subscriber.workspace = true tracing.workspace = true udev = "0.8" zbus.workspace = true -# TODO branch -upower_dbus = { git = "https://github.com/pop-os/dbus-settings-bindings", branch = "upower" } -cosmic-settings-subscriptions = { git = "https://github.com/pop-os/cosmic-settings-subscriptions" }