Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: panic on mouse drag on volume sliders #653

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions cosmic-applet-audio/src/pulse.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

use std::{cell::RefCell, rc::Rc, thread};
use std::{cell::RefCell, mem, rc::Rc, thread};

extern crate libpulse_binding as pulse;

Expand Down Expand Up @@ -153,9 +153,19 @@

impl Connection {
pub fn send(&mut self, message: Message) {
self.0
.try_send(message)
.expect("Send message to PulseAudio server");
if let Err(e) = self.0.try_send(message) {
match e {
mpsc::error::TrySendError::Closed(_) => {
tracing::error!(
"Failed to send message: PulseAudio server communication closed"
);
panic!();
}
mpsc::error::TrySendError::Full(_) => {
tracing::warn!("Failed to send message to PulseAudio server: channel is full")
}
}
}
}
}

Expand Down Expand Up @@ -186,8 +196,8 @@
impl PulseHandle {
// Create pulse server thread, and bidirectional comms
pub fn new() -> Self {
let (to_pulse, mut to_pulse_recv) = tokio::sync::mpsc::channel(10);
let (from_pulse_send, from_pulse) = tokio::sync::mpsc::channel(10);
let (to_pulse, mut to_pulse_recv) = tokio::sync::mpsc::channel(50);
let (from_pulse_send, from_pulse) = tokio::sync::mpsc::channel(50);

// this thread should complete by pushing a completed message,
// or fail message. This should never complete/fail without pushing
Expand All @@ -208,7 +218,22 @@

loop {
// This is where the we match messages from the GUI to pass to the pulse server
if let Some(msg) = to_pulse_recv.recv().await {
let mut msgs = Vec::new();
while let Ok(msg) = to_pulse_recv.try_recv() {
msgs.push(msg);
}
// deduplicate Messages that do not rely on response
// Reverse to retain the last element instead of the first
msgs.reverse();
msgs.dedup_by(|a, b| match a {
Message::SetSinkVolumeByName(..) | Message::SetSourceVolumeByName(..) => {
mem::discriminant(a) == mem::discriminant(b)
}
_ => false,
});
msgs.reverse();

for msg in msgs {
match msg {
Message::GetDefaultSink => {
let server = match server.as_mut() {
Expand Down Expand Up @@ -419,7 +444,7 @@

#[derive(Clone, Debug)]
enum PulseServerError<'a> {
IterateErr(IterateResult),

Check warning on line 447 in cosmic-applet-audio/src/pulse.rs

View workflow job for this annotation

GitHub Actions / linting

field `0` is never read

warning: field `0` is never read --> cosmic-applet-audio/src/pulse.rs:447:16 | 447 | IterateErr(IterateResult), | ---------- ^^^^^^^^^^^^^ | | | field in this variant | = note: `#[warn(dead_code)]` on by default help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field | 447 | IterateErr(()), | ~~
ContextErr(pulse::context::State),
OperationErr(pulse::operation::State),
PAErr(PAErr),
Expand Down
Loading