Skip to content

Commit

Permalink
Potential fix for libpulse locking issues, code cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aurailus committed Mar 13, 2021
1 parent 849a386 commit 4867c0d
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 35 deletions.
Binary file modified media/myxer_thumbnail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 4 additions & 7 deletions src/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ impl Card {
let index = self.data.index;
let pulse = self.pulse.as_ref().unwrap().clone();
self.combo_connect_id = Some(self.widgets.combo.connect_changed(move |combo| {
let val = combo.get_active_id().unwrap().as_str().to_owned();
pulse.borrow_mut().set_card_profile(index, val.as_str());
pulse.borrow_mut().set_card_profile(index, &String::from(combo.get_active_id().unwrap()));
}));
}

Expand All @@ -100,23 +99,21 @@ impl Card {

if data.name != self.data.name {
self.data.name = data.name.clone();
self.widgets.label.set_label(self.data.name.as_str());
self.widgets.label.set_label(&self.data.name);
}

if data.profiles.len() != self.data.profiles.len() {
self.disconnect();
self.data.profiles = data.profiles.clone();
self.widgets.combo.remove_all();
for (i, n) in data.profiles.iter() {
self.widgets.combo.append(Some(i.as_str()), n.as_str());
}
for (i, n) in &data.profiles { self.widgets.combo.append(Some(&i), &n); }
self.connect();
}

if data.active_profile != self.data.active_profile {
self.disconnect();
self.data.active_profile = data.active_profile.clone();
self.widgets.combo.set_active_id(Some(self.data.active_profile.as_str()));
self.widgets.combo.set_active_id(Some(&self.data.active_profile));
self.connect();
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/meter/sink_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ impl SinkMeter {
label.set_sensitive(false);
menu.pack_start(&label, true, true, 3);

for (i, v) in pulse.sinks.iter() {
for (i, v) in &pulse.sinks {
let button = gtk::ModelButton::new();
button.set_property_role(gtk::ButtonRole::Radio);
button.set_property_active(v.data.index == index);
let button_label = gtk::Label::new(Some(v.data.description.as_str()));
let button_label = gtk::Label::new(Some(&v.data.description));
button_label.set_ellipsize(pango::EllipsizeMode::End);
button_label.set_max_width_chars(18);
button.get_child().unwrap().downcast::<gtk::Box>().unwrap().add(&button_label);
Expand All @@ -129,7 +129,7 @@ impl SinkMeter {
}
}

root.get_children().iter().for_each(|i| i.show_all());
for child in &root.get_children() { child.show_all(); }
root.set_relative_to(Some(trigger));
root.popup();
}
Expand Down Expand Up @@ -159,7 +159,7 @@ impl Meter for SinkMeter {

if data.icon != self.data.icon {
self.data.icon = data.icon.clone();
self.widgets.icon.set_from_icon_name(Some(&self.data.icon.as_str()), gtk::IconSize::Dnd);
self.widgets.icon.set_from_icon_name(Some(&self.data.icon), gtk::IconSize::Dnd);
}

if data.name != self.data.name {
Expand All @@ -169,7 +169,7 @@ impl Meter for SinkMeter {

if data.description != self.data.description {
self.data.description = data.description.clone();
self.widgets.label.set_label(self.data.description.as_str());
self.widgets.label.set_label(&self.data.description);
}

if volume_changed || data.muted != self.data.muted {
Expand All @@ -188,7 +188,7 @@ impl Meter for SinkMeter {

let mut string = vol_scaled.to_string();
string.push_str("%");
self.widgets.status.set_label(string.as_str());
self.widgets.status.set_label(&string);

let status_ctx = self.widgets.status.get_style_context();
if self.data.muted { status_ctx.add_class("muted") }
Expand All @@ -210,7 +210,7 @@ impl Meter for SinkMeter {
}
}
else {
for s in self.widgets.scales_inner.get_children().iter() {
for s in &self.widgets.scales_inner.get_children() {
let s = s.clone().downcast::<gtk::Scale>().expect("Scales box has non-scale children.");
s.set_show_fill_level(false);
s.get_style_context().remove_class("visualizer");
Expand Down
14 changes: 7 additions & 7 deletions src/meter/source_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ impl SourceMeter {
label.set_sensitive(false);
menu.pack_start(&label, true, true, 3);

for (i, v) in pulse.sources.iter() {
for (i, v) in &pulse.sources {
let button = gtk::ModelButton::new();
button.set_property_role(gtk::ButtonRole::Radio);
button.set_property_active(v.data.index == index);
let button_label = gtk::Label::new(Some(v.data.description.as_str()));
let button_label = gtk::Label::new(Some(&v.data.description));
button_label.set_ellipsize(pango::EllipsizeMode::End);
button_label.set_max_width_chars(18);
button.get_child().unwrap().downcast::<gtk::Box>().unwrap().add(&button_label);
Expand All @@ -133,7 +133,7 @@ impl SourceMeter {
}
}

root.get_children().iter().for_each(|i| i.show_all());
for child in &root.get_children() { child.show_all(); }
root.set_relative_to(Some(trigger));
root.popup();
}
Expand Down Expand Up @@ -163,7 +163,7 @@ impl Meter for SourceMeter {

if data.icon != self.data.icon {
self.data.icon = data.icon.clone();
self.widgets.icon.set_from_icon_name(Some(&self.data.icon.as_str()), gtk::IconSize::Dnd);
self.widgets.icon.set_from_icon_name(Some(&self.data.icon), gtk::IconSize::Dnd);
}

if data.name != self.data.name {
Expand All @@ -173,7 +173,7 @@ impl Meter for SourceMeter {

if data.description != self.data.description {
self.data.description = data.description.clone();
self.widgets.label.set_label(self.data.description.as_str());
self.widgets.label.set_label(&self.data.description);
}

if volume_changed || data.muted != self.data.muted {
Expand All @@ -192,7 +192,7 @@ impl Meter for SourceMeter {

let mut string = vol_scaled.to_string();
string.push_str("%");
self.widgets.status.set_label(string.as_str());
self.widgets.status.set_label(&string);

let status_ctx = self.widgets.status.get_style_context();
if self.data.muted { status_ctx.add_class("muted") }
Expand All @@ -214,7 +214,7 @@ impl Meter for SourceMeter {
}
}
else {
for s in self.widgets.scales_inner.get_children().iter() {
for s in &self.widgets.scales_inner.get_children() {
let s = s.clone().downcast::<gtk::Scale>().expect("Scales box has non-scale children.");
s.set_show_fill_level(false);
s.get_style_context().remove_class("visualizer");
Expand Down
8 changes: 4 additions & 4 deletions src/meter/stream_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Meter for StreamMeter {

if data.icon != self.data.icon {
self.data.icon = data.icon.clone();
self.widgets.icon.set_from_icon_name(Some(&self.data.icon.as_str()), gtk::IconSize::Dnd);
self.widgets.icon.set_from_icon_name(Some(&self.data.icon), gtk::IconSize::Dnd);
}

if data.name != self.data.name {
Expand All @@ -97,7 +97,7 @@ impl Meter for StreamMeter {

if data.description != self.data.description {
self.data.description = data.description.clone();
self.widgets.label.set_label(self.data.description.as_str());
self.widgets.label.set_label(&self.data.description);
}

if volume_changed || data.muted != self.data.muted {
Expand All @@ -119,7 +119,7 @@ impl Meter for StreamMeter {

let mut string = vol_scaled.to_string();
string.push_str("%");
self.widgets.status.set_label(string.as_str());
self.widgets.status.set_label(&string);

let status_ctx = self.widgets.status.get_style_context();
if self.data.muted { status_ctx.add_class("muted") }
Expand All @@ -141,7 +141,7 @@ impl Meter for StreamMeter {
}
}
else {
for s in self.widgets.scales_inner.get_children().iter() {
for s in &self.widgets.scales_inner.get_children() {
let s = s.clone().downcast::<gtk::Scale>().expect("Scales box has non-scale children.");
s.set_show_fill_level(false);
s.get_style_context().remove_class("visualizer");
Expand Down
40 changes: 34 additions & 6 deletions src/pulse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ impl Pulse {

pub fn set_default_sink(&self, sink: u32) {
if let Some(sink) = self.sinks.get(&sink) {
self.context.borrow_mut().set_default_sink(sink.data.name.as_str(), |_|());
let mut mainloop = self.mainloop.borrow_mut();
mainloop.lock();
self.context.borrow_mut().set_default_sink(&sink.data.name, |_|());
mainloop.unlock();
}
}

Expand All @@ -197,7 +200,10 @@ impl Pulse {

pub fn set_default_source(&self, source: u32) {
if let Some(source) = self.sources.get(&source) {
self.context.borrow_mut().set_default_source(source.data.name.as_str(), |_|());
let mut mainloop = self.mainloop.borrow_mut();
mainloop.lock();
self.context.borrow_mut().set_default_source(&source.data.name, |_|());
mainloop.unlock();
}
}

Expand Down Expand Up @@ -234,13 +240,17 @@ impl Pulse {

pub fn set_volume(&self, t: StreamType, index: u32, volumes: ChannelVolumes) {
let mut introspect = self.context.borrow().introspect();

let mut mainloop = self.mainloop.borrow_mut();
mainloop.lock();

match t {
StreamType::Sink => drop(introspect.set_sink_volume_by_index(index, &volumes, None)),
StreamType::SinkInput => drop(introspect.set_sink_input_volume(index, &volumes, None)),
StreamType::Source => drop(introspect.set_source_volume_by_index(index, &volumes, None)),
StreamType::SourceOutput => drop(introspect.set_source_output_volume(index, &volumes, None))
};

mainloop.unlock();
}


Expand All @@ -254,12 +264,17 @@ impl Pulse {

pub fn set_muted(&self, t: StreamType, index: u32, muted: bool) {
let mut introspect = self.context.borrow().introspect();
let mut mainloop = self.mainloop.borrow_mut();
mainloop.lock();

match t {
StreamType::Sink => drop(introspect.set_sink_mute_by_index(index, muted, None)),
StreamType::SinkInput => drop(introspect.set_sink_input_mute(index, muted, None)),
StreamType::Source => drop(introspect.set_source_mute_by_index(index, muted, None)),
StreamType::SourceOutput => drop(introspect.set_source_output_mute(index, muted, None))
};

mainloop.unlock();
}


Expand All @@ -272,7 +287,10 @@ impl Pulse {

pub fn set_card_profile(&self, index: u32, profile: &str) {
let mut introspect = self.context.borrow().introspect();
let mut mainloop = self.mainloop.borrow_mut();
mainloop.lock();
introspect.set_card_profile_by_index(index, profile, None);
mainloop.unlock();
}


Expand Down Expand Up @@ -372,6 +390,8 @@ impl Pulse {
}
}

let mut mainloop = self.mainloop.borrow_mut();
mainloop.lock();
let mut context = self.context.borrow_mut();
let introspect = context.introspect();

Expand Down Expand Up @@ -421,6 +441,8 @@ impl Pulse {
_ => ()
};
})));

mainloop.unlock();
}


Expand Down Expand Up @@ -478,15 +500,15 @@ impl Pulse {
*/

fn update_default(&mut self, sink: String, source: String) {
for (i, v) in self.sinks.iter() {
for (i, v) in &self.sinks {
if v.data.name == sink {
self.default_sink = *i;
self.active_sink = *i;
break;
}
}

for (i, v) in self.sources.iter() {
for (i, v) in &self.sources {
if v.data.name == source {
self.default_source = *i;
self.active_source = *i;
Expand Down Expand Up @@ -517,7 +539,7 @@ impl Pulse {
if let Some(stream) = entry { stream.data = data; }
else {
let source_str = stream.monitor_index.to_string();
let monitor = self.create_monitor_stream(t, if t == StreamType::SinkInput { None } else { Some(source_str.as_str()) }, index);
let monitor = self.create_monitor_stream(t, if t == StreamType::SinkInput { None } else { Some(&source_str) }, index);
let data = StreamData { data, peak: 0, monitor, monitor_index: stream.monitor_index };
match t {
StreamType::Sink => self.sinks.insert(index, data),
Expand Down Expand Up @@ -546,10 +568,13 @@ impl Pulse {

if let Some(stream) = stream_opt {
let mut monitor = stream.monitor.borrow_mut();
let mut mainloop = self.mainloop.borrow_mut();
mainloop.lock();
if monitor.get_state().is_good() {
monitor.set_read_callback(None);
let _ = monitor.disconnect();
}
mainloop.unlock();
}

match t {
Expand Down Expand Up @@ -620,8 +645,11 @@ impl Pulse {
stream.set_monitor_stream(stream_index).unwrap();
}

let mut mainloop = self.mainloop.borrow_mut();
mainloop.lock();
stream.connect_record(source, Some(&attr),
StreamFlagSet::DONT_MOVE | StreamFlagSet::ADJUST_LATENCY | StreamFlagSet::PEAK_DETECT).unwrap();
mainloop.unlock();

let t = t.clone();
let sc = s.clone();
Expand Down
4 changes: 2 additions & 2 deletions src/window/myxer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl Myxer {
meters.sink.set_peak(if show { Some(sink.peak) } else { None });
}

for (index, input) in pulse.sink_inputs.iter() {
for (index, input) in &pulse.sink_inputs {
let sink_inputs_box = meters.sink_inputs_box.clone();

let meter = meters.sink_inputs.entry(*index).or_insert_with(|| StreamMeter::new(self.pulse.clone()));
Expand All @@ -278,7 +278,7 @@ impl Myxer {
meters.source.set_peak(if show { Some(source.peak) } else { None });
}

for (index, output) in pulse.source_outputs.iter() {
for (index, output) in &pulse.source_outputs {
let source_outputs_box = meters.source_outputs_box.clone();

let meter = meters.source_outputs.entry(*index).or_insert_with(|| StreamMeter::new(self.pulse.clone()));
Expand Down
2 changes: 1 addition & 1 deletion src/window/profiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl Profiles {

let pulse = self.pulse.borrow_mut();
let mut cards = self.cards.borrow_mut();
for (index, data) in pulse.cards.iter() {
for (index, data) in &pulse.cards {
let cards_box = cards.cards_box.clone();

let card = cards.cards.entry(*index).or_insert_with(|| Card::new(Some(self.pulse.clone())));
Expand Down
2 changes: 1 addition & 1 deletion src/window/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn style(window: &gtk::ApplicationWindow) {
s.push_str("@define-color ");
s.push_str(identifier);
s.push_str(" ");
s.push_str(colorsys::Rgb::new(color.red * 255.0, color.green * 255.0, color.blue * 255.0, None).to_css_string().as_str());
s.push_str(&colorsys::Rgb::new(color.red * 255.0, color.green * 255.0, color.blue * 255.0, None).to_css_string());
s.push_str(";\n");
};

Expand Down

0 comments on commit 4867c0d

Please sign in to comment.