feat: --silent flag to disable notifications

This commit is contained in:
2024-06-29 22:19:53 +02:00
parent 94338d2e37
commit 3f083d3ee5
2 changed files with 46 additions and 23 deletions

View File

@@ -1,19 +1,26 @@
mod instructions; mod instructions;
pub mod mixer; pub mod mixer;
pub mod playerctl;
pub mod pulseaudio; pub mod pulseaudio;
pub mod utils; pub mod utils;
pub mod playerctl;
use mixer::Mixer; use mixer::Mixer;
use pulseaudio::PulseInstruction; use pulseaudio::PulseInstruction;
use std::sync::mpsc::channel; use std::{env, sync::mpsc::channel};
fn main() { fn main() {
let mainloop = pulse::mainloop::standard::Mainloop::new().expect("Error getting PulseAudio main loop"); let mainloop =
pulse::mainloop::standard::Mainloop::new().expect("Error getting PulseAudio main loop");
let args: Vec<String> = env::args().collect();
let silent_mode = match args.iter().nth(1) {
Some(arg) => arg == "--silent",
None => false,
};
let (pulse_ix_tx, pulse_ix_rx) = channel::<PulseInstruction>(); let (pulse_ix_tx, pulse_ix_rx) = channel::<PulseInstruction>();
let mut mixer = Mixer::new(mainloop, pulse_ix_tx); let mut mixer = Mixer::new(mainloop, pulse_ix_tx, silent_mode);
mixer.run(pulse_ix_rx); mixer.run(pulse_ix_rx);
} }

View File

@@ -42,10 +42,15 @@ pub struct Mixer {
selected_index: Arc<Mutex<Option<usize>>>, selected_index: Arc<Mutex<Option<usize>>>,
mainloop: Mainloop, mainloop: Mainloop,
context: pulse::context::Context, context: pulse::context::Context,
silent_mode: bool,
} }
impl Mixer { impl Mixer {
pub fn new(mut mainloop: Mainloop, pulse_ix_tx: Sender<PulseInstruction>) -> Self { pub fn new(
mut mainloop: Mainloop,
pulse_ix_tx: Sender<PulseInstruction>,
silent_mode: bool,
) -> Self {
let mut context = let mut context =
pulse::context::Context::new(&mainloop, "Mixrs").expect("Error creating pulse context"); pulse::context::Context::new(&mainloop, "Mixrs").expect("Error creating pulse context");
@@ -105,6 +110,7 @@ impl Mixer {
selected_index, selected_index,
mainloop, mainloop,
context, context,
silent_mode,
} }
} }
@@ -423,7 +429,9 @@ impl Mixer {
.set_sink_input_volume( .set_sink_input_volume(
*sink_index, *sink_index,
&volume, &volume,
Some(Box::new(move |success| { match self.silent_mode {
true => None,
false => Some(Box::new(move |success| {
if success { if success {
let volume = volume_to_percentage(volume); let volume = volume_to_percentage(volume);
let _ = send_notification_with_progress( let _ = send_notification_with_progress(
@@ -432,6 +440,7 @@ impl Mixer {
); );
} }
})), })),
},
); );
} }
@@ -466,7 +475,9 @@ impl Mixer {
.set_sink_input_volume( .set_sink_input_volume(
*sink_index, *sink_index,
&volume, &volume,
Some(Box::new(move |success| { match self.silent_mode {
true => None,
false => Some(Box::new(move |success| {
if success { if success {
let volume = volume_to_percentage(volume); let volume = volume_to_percentage(volume);
let _ = send_notification_with_progress( let _ = send_notification_with_progress(
@@ -475,10 +486,15 @@ impl Mixer {
); );
} }
})), })),
},
); );
} }
pub fn get_current(&self) { pub fn get_current(&self) {
if self.silent_mode {
return;
}
let index_lock = self.selected_index.lock().unwrap(); let index_lock = self.selected_index.lock().unwrap();
let Some(index) = *index_lock else { let Some(index) = *index_lock else {