From 20290547f19b7bd6bcec2930140fe54074907440 Mon Sep 17 00:00:00 2001 From: Erwin Boskma Date: Wed, 24 Nov 2021 15:33:23 +0100 Subject: [PATCH] Refactoring, fixing volume calculation --- src/main.rs | 71 +++++++++++++++++++++++++++-------------------------- 1 file changed, 36 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1fd12ca..8d15eb9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ use std::error::Error; use argh::FromArgs; use notify_rust::{Hint, Notification}; -use pulse::volume::Volume; use pulsectl::controllers::{DeviceControl, SinkController}; /// Control the volume of the default sink @@ -47,7 +46,12 @@ struct GetArgs {} #[argh(subcommand, name = "set")] struct SetArgs { #[argh(positional)] - volume: u8, + volume: i32, +} + +enum Volume { + Muted, + Percent(i32), } fn main() -> Result<(), Box> { @@ -61,70 +65,67 @@ fn main() -> Result<(), Box> { match args.command { Subcommand::Up(_) => { handler.increase_device_volume_by_percent(default_device.index, 0.05); - notify_volume(default_device.volume.avg().0 as i32); } Subcommand::Down(_) => { handler.decrease_device_volume_by_percent(default_device.index, 0.05); - notify_volume(default_device.volume.avg().0 as i32); } Subcommand::Mute(_) => { handler.set_device_mute_by_index(default_device.index, !muted); - notify_muted(default_device.volume.avg().0 as i32, default_device.mute); } - Subcommand::Get(_) => println!("Volume: {}", default_device.volume.avg()), + Subcommand::Get(_) => { + println!("Volume: {}", default_device.volume.avg()); + return Ok(()); + } Subcommand::Set(SetArgs { volume }) => { let mut current_volume = default_device.volume; - let new_volume = Volume(volume_from_percent(volume) as u32); + let new_volume = pulse::volume::Volume(volume_from_percent(volume) as u32); let volumes = current_volume.set(2, new_volume); handler.set_device_volume_by_index(default_device.index, volumes); - notify_volume(default_device.volume.avg().0 as i32); } } + let v = if handler.get_default_device()?.mute { + Volume::Muted + } else { + Volume::Percent(volume_to_percent( + handler.get_default_device()?.volume.avg().0 as i32, + )) + }; + notify_volume(v); Ok(()) } -fn icon_name(volume: i32) -> String { +fn icon_name(volume: Volume) -> String { let name = match volume { - x if x <= 0 => "muted", - x if x <= 30 => "low", - x if x <= 70 => "medium", + Volume::Muted => "muted", + Volume::Percent(x) if x <= 33 => "low", + Volume::Percent(x) if x <= 67 => "medium", _ => "high", }; format!("audio-volume-{}-symbolic", name) } -fn notify_volume(new_volume: i32) { +fn notify_volume(new_volume: Volume) { + let value = dbg!(match new_volume { + Volume::Muted => 0, + Volume::Percent(new_volume) => volume_to_percent(new_volume), + }); Notification::new() .summary("Volume") .icon(icon_name(new_volume).as_str()) .appname("pamedia") - .hint(Hint::Custom( - "x-canonical-private-synchronous".into(), - "audio".into(), - )) - .hint(Hint::CustomInt("value".into(), new_volume)) + .hint(Hint::SoundName("audio-volume-change".into())) + .hint(Hint::Custom("synchronous".into(), "audio".into())) + .hint(Hint::CustomInt("value".into(), value)) .show() .expect("error showing notification"); } -fn notify_muted(volume: i32, muted: bool) { - let icon = icon_name(if muted { 0 } else { volume }); - - Notification::new() - .summary("Volume") - .icon(icon.as_str()) - .appname("pamedia") - .hint(Hint::Custom( - "x-canonical-private-synchronous".into(), - "audio".into(), - )) - .hint(Hint::CustomInt("value".into(), volume)) - .show() - .expect("error showing notification"); -} - -fn volume_from_percent(volume: u8) -> f64 { +fn volume_from_percent(volume: i32) -> f64 { (volume as f64) * (f64::from(pulse::volume::Volume::NORMAL.0) / 100.0) } + +fn volume_to_percent(volume: i32) -> i32 { + ((volume as f64) / (f64::from(pulse::volume::Volume::NORMAL.0)) * 100.0).round() as i32 +}