From c9f434158769d7d81f4001cc8cc628373bf1041e Mon Sep 17 00:00:00 2001 From: 409 Date: Sun, 6 Oct 2024 04:14:44 +0200 Subject: [PATCH 1/3] refactor(instructions): `UpdateSinkInput` use channel and reduce nesting --- src/mixer.rs | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/mixer.rs b/src/mixer.rs index d58a4d5..dc42e69 100644 --- a/src/mixer.rs +++ b/src/mixer.rs @@ -294,17 +294,38 @@ impl Mixer { muted: sink_input.mute, }); }); + PulseInstruction::UpdateSinkInput(sink_index) => 'update_sink: { + let Some(sink_input_mixer_data) = self.sink_inputs.get_mut(&sink_index) + else { + break 'update_sink; + }; - while operation.get_state() == pulse::operation::State::Running { - iterate_mainloop(&mut self.mainloop); - } + let (new_sink_tx, new_sink_rx) = channel::>(); + let operation = self + .context + .borrow_mut() + .introspect() + .borrow_mut() + .get_sink_input_info(sink_index, move |r| { + let ListResult::Item(sink_input) = r else { + let _ = new_sink_tx.send(None); + return; + }; - let mut sink_input_lock = new_sink_input.lock().unwrap(); - if let Some(new_sink_input) = sink_input_lock.take() { - *sink_input_mixer_data = new_sink_input; - } - } - None => (), + let _ = new_sink_tx.send(Some(SinkInputMixerData { + name: get_sink_input_name(&sink_input).unwrap(), + volume: sink_input.volume.avg().0, + channels: sink_input.volume.len(), + muted: sink_input.mute, + })); + }); + + while operation.get_state() == pulse::operation::State::Running { + iterate_mainloop(&mut self.mainloop); + } + + if let Some(new_sink_input) = new_sink_rx.recv().ok().flatten() { + *sink_input_mixer_data = new_sink_input; } } } From 696ab0a0890ab2dea53b10c6ddea3482fbc845b1 Mon Sep 17 00:00:00 2001 From: 409 Date: Sun, 6 Oct 2024 04:16:19 +0200 Subject: [PATCH 2/3] fix(instructions): `RemoveSinkInput` switches selection sometimes --- src/mixer.rs | 62 +++++++++++++++------------------------------------- 1 file changed, 18 insertions(+), 44 deletions(-) diff --git a/src/mixer.rs b/src/mixer.rs index dc42e69..39abd3c 100644 --- a/src/mixer.rs +++ b/src/mixer.rs @@ -243,57 +243,31 @@ impl Mixer { } } } - PulseInstruction::RemoveSinkInput(sink_index) => { + PulseInstruction::RemoveSinkInput(sink_index) => 'remove_sink: { let selected_index_lock = self.selected_index.lock().unwrap(); - match *selected_index_lock { - Some(current_index) => { - drop(selected_index_lock); + let Some(selected_index) = *selected_index_lock else { + break 'remove_sink; + }; - if let Some(removed_sink_input_index) = - self.sink_inputs.keys().position(|k| *k == sink_index) - { - let current_key = - *self.sink_inputs.keys().nth(current_index).unwrap(); + drop(selected_index_lock); - if self.sink_inputs.remove(&sink_index).is_some() { - if sink_index == current_key - || removed_sink_input_index > current_index - { - self.select_previous(); - } - } - } + let Some(removed_sink_input_index) = + self.sink_inputs.keys().position(|k| *k == sink_index) + else { + break 'remove_sink; + }; + + let current_key = *self.sink_inputs.keys().nth(selected_index).unwrap(); + + if self.sink_inputs.remove(&sink_index).is_some() { + if sink_index == current_key + || removed_sink_input_index <= selected_index + { + self.select_previous(); } - - None => (), } } - PulseInstruction::UpdateSinkInput(sink_index) => { - match self.sink_inputs.get_mut(&sink_index) { - Some(sink_input_mixer_data) => { - let new_sink_input: Arc>> = - Arc::new(Mutex::new(None)); - let callback_new_sink_input = new_sink_input.clone(); - - let operation = self - .context - .borrow_mut() - .introspect() - .borrow_mut() - .get_sink_input_info(sink_index, move |r| { - let ListResult::Item(sink_input) = r else { - return; - }; - - *callback_new_sink_input.lock().unwrap() = - Some(SinkInputMixerData { - name: get_sink_input_name(&sink_input).unwrap(), - volume: sink_input.volume.avg().0, - channels: sink_input.volume.len(), - muted: sink_input.mute, - }); - }); PulseInstruction::UpdateSinkInput(sink_index) => 'update_sink: { let Some(sink_input_mixer_data) = self.sink_inputs.get_mut(&sink_index) else { From 25e19ed0244adca05e1b8a3412a627dbd8d85777 Mon Sep 17 00:00:00 2001 From: 409 Date: Sun, 6 Oct 2024 04:17:23 +0200 Subject: [PATCH 3/3] chore: bump version to `0.1.1` --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1a49051..ea32dab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -55,7 +55,7 @@ dependencies = [ [[package]] name = "mixrs" -version = "0.1.0" +version = "0.1.1" dependencies = [ "anyhow", "libpulse-binding", diff --git a/Cargo.toml b/Cargo.toml index 6d21a2a..9a62e82 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mixrs" -version = "0.1.0" +version = "0.1.1" edition = "2021" [dependencies]