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

Add some error handling to zbus, and unify dbus/zbus Error type #52

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 1 addition & 15 deletions src/platform/mpris/dbus/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,7 @@ use std::thread::{self, JoinHandle};
use std::time::Duration;

use crate::{MediaControlEvent, MediaMetadata, MediaPlayback, PlatformConfig};

/// A platform-specific error.
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("internal D-Bus error: {0}")]
DbusError(#[from] dbus::Error),
#[error("D-bus service thread not running. Run MediaControls::attach()")]
ThreadNotRunning,
// NOTE: For now this error is not very descriptive. For now we can't do much about it
// since the panic message returned by JoinHandle::join does not implement Debug/Display,
// thus we cannot print it, though perhaps there is another way. I will leave this error here,
// to at least be able to catch it, but it is preferable to have this thread *not panic* at all.
#[error("D-Bus service thread panicked")]
ThreadPanicked,
}
use super::super::Error;

/// A handle to OS media controls.
pub struct MediaControls {
Expand Down
2 changes: 1 addition & 1 deletion src/platform/mpris/dbus/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod interfaces;

mod controls;
pub use controls::{Error, MediaControls};
pub use controls::MediaControls;
23 changes: 23 additions & 0 deletions src/platform/mpris/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,31 @@ compile_error!("feature \"dbus\" and feature \"zbus\" are mutually exclusive");
mod zbus;
#[cfg(feature = "zbus")]
pub use self::zbus::*;
#[cfg(feature = "zbus")]
extern crate zbus as zbus_crate;

#[cfg(feature = "dbus")]
mod dbus;
#[cfg(feature = "dbus")]
pub use self::dbus::*;
#[cfg(feature = "dbus")]
extern crate dbus as dbus_crate;

/// A platform-specific error.
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("internal D-Bus error: {0}")]
#[cfg(feature = "dbus")]
DbusError(#[from] dbus_crate::Error),
#[error("internal D-Bus error: {0}")]
#[cfg(feature = "zbus")]
DbusError(#[from] zbus_crate::Error),
#[error("D-bus service thread not running. Run MediaControls::attach()")]
ThreadNotRunning,
// NOTE: For now this error is not very descriptive. For now we can't do much about it
// since the panic message returned by JoinHandle::join does not implement Debug/Display,
// thus we cannot print it, though perhaps there is another way. I will leave this error here,
// to at least be able to catch it, but it is preferable to have this thread *not panic* at all.
#[error("D-Bus service thread panicked")]
ThreadPanicked,
}
21 changes: 9 additions & 12 deletions src/platform/mpris/zbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ use crate::{
MediaControlEvent, MediaMetadata, MediaPlayback, MediaPosition, PlatformConfig, SeekDirection,
};

/// A platform-specific error.
#[derive(Debug)]
pub struct Error;
use super::Error;

/// A handle to OS media controls.
pub struct MediaControls {
Expand Down Expand Up @@ -109,34 +107,33 @@ impl MediaControls {
thread,
}) = self.thread.take()
{
event_channel.send(InternalEvent::Kill).unwrap();
thread.join().unwrap();
event_channel.send(InternalEvent::Kill).ok();
thread.join().map_err(|_| Error::ThreadPanicked)?;
}
Ok(())
}

/// Set the current playback status.
pub fn set_playback(&mut self, playback: MediaPlayback) -> Result<(), Error> {
self.send_internal_event(InternalEvent::ChangePlayback(playback));
self.send_internal_event(InternalEvent::ChangePlayback(playback))?;
Ok(())
}

/// Set the metadata of the currently playing media item.
pub fn set_metadata(&mut self, metadata: MediaMetadata) -> Result<(), Error> {
self.send_internal_event(InternalEvent::ChangeMetadata(metadata.into()));
self.send_internal_event(InternalEvent::ChangeMetadata(metadata.into()))?;
Ok(())
}

/// Set the volume level (0.0 - 1.0) (Only available on MPRIS)
pub fn set_volume(&mut self, volume: f64) -> Result<(), Error> {
self.send_internal_event(InternalEvent::ChangeVolume(volume));
self.send_internal_event(InternalEvent::ChangeVolume(volume))?;
Ok(())
}

// TODO: result
fn send_internal_event(&mut self, event: InternalEvent) {
let channel = &self.thread.as_ref().unwrap().event_channel;
channel.send(event).unwrap();
fn send_internal_event(&mut self, event: InternalEvent) -> Result<(), Error> {
let channel = &self.thread.as_ref().ok_or(Error::ThreadNotRunning)?.event_channel;
channel.send(event).map_err(|_| Error::ThreadPanicked)
}
}

Expand Down