2024-07-24 15:11:35 +00:00
|
|
|
//! TODO: Module level docs Volume
|
|
|
|
|
use clap::Subcommand;
|
2024-07-21 22:26:08 +00:00
|
|
|
use core::fmt;
|
|
|
|
|
use std::fmt::{Display, Result};
|
2024-07-21 19:58:56 +00:00
|
|
|
use super::Direction;
|
2024-07-24 15:11:35 +00:00
|
|
|
/// Defines the subcommands that can be used with the volume utility.
|
2024-07-21 22:26:08 +00:00
|
|
|
#[derive(Subcommand)]
|
|
|
|
|
pub enum Command {
|
|
|
|
|
/// Adjust volume Up/Down by 5%
|
|
|
|
|
Step {
|
|
|
|
|
/// The direction to adjust the volume
|
|
|
|
|
#[arg(value_enum)]
|
|
|
|
|
directon: Direction,
|
|
|
|
|
},
|
|
|
|
|
/// Adjust the volume to 0% or last volume
|
|
|
|
|
ToggleMute {
|
|
|
|
|
/// The value to set the volume to
|
|
|
|
|
#[arg()]
|
|
|
|
|
volume: u8,
|
|
|
|
|
},
|
|
|
|
|
/// Get the name of the current icon
|
|
|
|
|
CurrentIcon,
|
|
|
|
|
/// Get the current volume percent
|
|
|
|
|
CurrentValue,
|
|
|
|
|
}
|
2024-07-24 15:11:35 +00:00
|
|
|
/// Parses a command and passes it's arguments (if applicable) to the relevant method.
|
2024-07-21 22:26:08 +00:00
|
|
|
pub fn parse_command(command: Command) {
|
|
|
|
|
match command {
|
|
|
|
|
Command::CurrentValue => println!("{}", get_current_value()),
|
|
|
|
|
Command::CurrentIcon => println!("{}", get_current_icon()),
|
|
|
|
|
Command::Step { directon } => step(directon),
|
|
|
|
|
Command::ToggleMute { volume } => toggle_mute(volume),
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-24 15:11:35 +00:00
|
|
|
/// Increment or Deincrement the system volume by 5%
|
2024-07-21 22:26:08 +00:00
|
|
|
fn step(directon: Direction) { //onscroll
|
2024-07-21 19:58:56 +00:00
|
|
|
set(5, directon);
|
|
|
|
|
}
|
2024-07-24 15:11:35 +00:00
|
|
|
/// Takes [u8] (saved_volume) and replaces the current volume with the value.
|
|
|
|
|
/// The current volume is stored within the `mute_save` eww variable.
|
2024-07-21 22:26:08 +00:00
|
|
|
fn toggle_mute(saved_volume: u8) { //onclick
|
2024-07-21 19:58:56 +00:00
|
|
|
use super::update_eww_var;
|
|
|
|
|
|
|
|
|
|
let volume = get_current_value();
|
|
|
|
|
set(saved_volume, Direction::Absolute);
|
|
|
|
|
update_eww_var("mute_save", &volume.to_string());
|
|
|
|
|
|
|
|
|
|
std::process::exit(0);
|
|
|
|
|
}
|
2024-07-24 15:11:35 +00:00
|
|
|
/// Returns the current [Level] corresponding to the current volume value.
|
2024-07-21 22:26:08 +00:00
|
|
|
fn get_current_icon() -> Level { //for path var in image (add to defpoll)
|
2024-07-21 19:58:56 +00:00
|
|
|
match get_current_value() {
|
|
|
|
|
value if value <= 0 => Level::Mute,
|
|
|
|
|
value if value <= 25 => Level::Low,
|
|
|
|
|
value if value <= 75 => Level::Mid,
|
|
|
|
|
_ => Level::High,
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-24 15:11:35 +00:00
|
|
|
/// Returns the current volume value
|
2024-07-21 22:26:08 +00:00
|
|
|
fn get_current_value() -> u8 { //for value in circular-progrss (add to defpoll)
|
|
|
|
|
use std::process::Command;
|
|
|
|
|
|
2024-07-21 19:58:56 +00:00
|
|
|
let value = Command::new("amixer")
|
|
|
|
|
.arg("sget").arg("Master")
|
|
|
|
|
.output().unwrap().stdout;
|
|
|
|
|
let value = String::from_utf8(value).unwrap();
|
|
|
|
|
let value: Vec<&str> = value.split("[").collect();
|
2024-07-21 22:26:08 +00:00
|
|
|
let value = value[3].trim();
|
|
|
|
|
let value = value.get(0..value.len() -2).unwrap().replace("%", "");
|
|
|
|
|
|
|
|
|
|
value.parse().unwrap()
|
2024-07-21 19:58:56 +00:00
|
|
|
}
|
2024-07-24 15:11:35 +00:00
|
|
|
/// Sets the current volume to [u8] (value) with respect to the [Direction].
|
|
|
|
|
/// If direction = Up | Down : the volume steps by the value.
|
|
|
|
|
/// If direction = Absolute : the volume is set to the value%.
|
2024-07-21 19:58:56 +00:00
|
|
|
fn set(value: u8, directon: Direction) {
|
2024-07-21 22:26:08 +00:00
|
|
|
use std::process::Command;
|
|
|
|
|
|
2024-07-21 19:58:56 +00:00
|
|
|
let mut value = value.to_string();
|
|
|
|
|
value.push('%');
|
|
|
|
|
|
|
|
|
|
match directon {
|
|
|
|
|
Direction::Up => value.push('+'),
|
|
|
|
|
Direction::Down => value.push('-'),
|
|
|
|
|
Direction::Absolute => (),
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-21 22:26:08 +00:00
|
|
|
let _ = Command::new("amixer")
|
2024-07-21 19:58:56 +00:00
|
|
|
.arg("sset").arg("Master").arg(value).arg("> /dev/null")
|
|
|
|
|
.spawn();
|
|
|
|
|
}
|
2024-07-24 15:11:35 +00:00
|
|
|
/// Defines the levels the volume can be.
|
|
|
|
|
/// These levels correspond to the icons that can be displayed.
|
2024-07-21 19:58:56 +00:00
|
|
|
enum Level {
|
|
|
|
|
High,
|
|
|
|
|
Mid,
|
|
|
|
|
Low,
|
|
|
|
|
Mute,
|
|
|
|
|
}
|
2024-07-21 22:26:08 +00:00
|
|
|
impl Display for Level {
|
2024-07-24 15:11:35 +00:00
|
|
|
/// Formats [Level] to a lowercase string.
|
2024-07-21 22:26:08 +00:00
|
|
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result {
|
|
|
|
|
match self {
|
|
|
|
|
Level::Mute => write!(formatter, "mute"),
|
|
|
|
|
Level::Low => write!(formatter, "low"),
|
|
|
|
|
Level::Mid => write!(formatter, "mid"),
|
|
|
|
|
Level::High => write!(formatter, "high"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|