fix: use CString instead of new rust syntax to fix older versions of rust throwing compilation errors

This commit is contained in:
Tal 2024-10-15 14:28:26 +02:00
parent 543aeee78c
commit 10a3263c82
4 changed files with 19 additions and 18 deletions

View File

@ -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))
}

View File

@ -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<()> {

View File

@ -131,7 +131,7 @@ async fn interface(player: Arc<Player>, 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]

View File

@ -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]));