From 10a3263c82cbad86481f33a8b95cbf7467bbaf1a Mon Sep 17 00:00:00 2001 From: Tal <83217276+talwat@users.noreply.github.com> Date: Tue, 15 Oct 2024 14:28:26 +0200 Subject: [PATCH] fix: use `CString` instead of new rust syntax to fix older versions of rust throwing compilation errors --- src/player.rs | 19 +++++++++++++------ src/player/mpris.rs | 10 ++-------- src/player/ui.rs | 2 +- src/player/ui/components.rs | 6 +++--- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/player.rs b/src/player.rs index b5dd157..a428108 100644 --- a/src/player.rs +++ b/src/player.rs @@ -2,7 +2,7 @@ //! This also has the code for the underlying //! audio server which adds new tracks. -use std::{collections::VecDeque, sync::Arc, time::Duration}; +use std::{collections::VecDeque, ffi::CString, sync::Arc, time::Duration}; use arc_swap::ArcSwapOption; use downloader::Downloader; @@ -118,22 +118,29 @@ unsafe impl Sync for Player {} impl Player { /// This gets the output stream while also shutting up alsa with [libc]. fn silent_get_output_stream() -> eyre::Result<(OutputStream, OutputStreamHandle)> { + // Get the file descriptor to stderr from libc. extern "C" { static stderr: *mut libc::FILE; } - let mode = c"w".as_ptr(); - // This is a bit of an ugly hack that basically just uses `libc` to redirect alsa's // output to `/dev/null` so that it wont be shoved down our throats. - // SAFETY: Simple enough to be impossible to fail. Hopefully. - unsafe { freopen(c"/dev/null".as_ptr(), mode, stderr) }; + // The mode which to redirect terminal output with. + let mode = CString::new("w")?.as_ptr(); + // First redirect to /dev/null, which basically silences alsa. + let null = CString::new("/dev/null")?.as_ptr(); + // SAFETY: Simple enough to be impossible to fail. Hopefully. + unsafe { freopen(null, mode, stderr) }; + + // Make the OutputStream while stderr is still redirected to /dev/null. let (stream, handle) = OutputStream::try_default()?; + // Redirect back to the current terminal, so that other output isn't silenced. + let tty = CString::new("/dev/tty")?.as_ptr(); // SAFETY: See the first call to `freopen`. - unsafe { freopen(c"/dev/tty".as_ptr(), mode, stderr) }; + unsafe { freopen(tty, mode, stderr) }; Ok((stream, handle)) } diff --git a/src/player/mpris.rs b/src/player/mpris.rs index bcd624f..ddfde16 100644 --- a/src/player/mpris.rs +++ b/src/player/mpris.rs @@ -77,10 +77,7 @@ impl PlayerInterface for Player { } async fn pause(&self) -> fdo::Result<()> { - self.sender - .send(Messages::Pause) - .await - .map_err(|_| ERROR) + self.sender.send(Messages::Pause).await.map_err(|_| ERROR) } async fn play_pause(&self) -> fdo::Result<()> { @@ -95,10 +92,7 @@ impl PlayerInterface for Player { } async fn play(&self) -> fdo::Result<()> { - self.sender - .send(Messages::Play) - .await - .map_err(|_| ERROR) + self.sender.send(Messages::Play).await.map_err(|_| ERROR) } async fn seek(&self, _offset: Time) -> fdo::Result<()> { diff --git a/src/player/ui.rs b/src/player/ui.rs index 78d417c..dc92af8 100644 --- a/src/player/ui.rs +++ b/src/player/ui.rs @@ -131,7 +131,7 @@ async fn interface(player: Arc, minimalist: bool) -> eyre::Result<()> { VOLUME_TIMER.store(0, Ordering::Relaxed); } - let controls = components::controls(&player, WIDTH); + let controls = components::controls(WIDTH); let menu = if minimalist { vec![action, middle] diff --git a/src/player/ui/components.rs b/src/player/ui/components.rs index 8b33e10..e989903 100644 --- a/src/player/ui/components.rs +++ b/src/player/ui/components.rs @@ -103,9 +103,9 @@ pub fn action(player: &Player, width: usize) -> String { } /// Creates the bottom controls bar, and also spaces it properly. -pub fn controls(player: &Player, width: usize) -> String { - let play_pause = if player.sink.is_paused() { ["[p]", "lay "] } else { ["[p]", "ause"] }; - let controls = [["[s]", "kip"], play_pause, ["[q]", "uit"]]; +pub fn controls(width: usize) -> String { + let controls = [["[s]", "kip"], ["[p]", "ause"], ["[q]", "uit"]]; + let len: usize = controls.concat().iter().map(|x| x.len()).sum(); let controls = controls.map(|x| format!("{}{}", x[0].bold(), x[1]));