use crate::alsa;
use super::error::*;
use std::{slice, ptr, fmt};
pub struct Output(*mut alsa::snd_output_t);
unsafe impl Send for Output {}
impl Drop for Output {
fn drop(&mut self) { unsafe { alsa::snd_output_close(self.0) }; }
}
impl Output {
pub fn buffer_open() -> Result<Output> {
let mut q = ptr::null_mut();
acheck!(snd_output_buffer_open(&mut q)).map(|_| Output(q))
}
pub fn buffer_string<T, F: FnOnce(&[u8]) -> T>(&self, f: F) -> T {
let b = unsafe {
let mut q = ptr::null_mut();
let s = alsa::snd_output_buffer_string(self.0, &mut q);
slice::from_raw_parts(q as *const u8, s as usize)
};
f(b)
}
}
impl fmt::Debug for Output {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Output(")?;
fmt::Display::fmt(self, f)?;
write!(f, ")")
}
}
impl fmt::Display for Output {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.buffer_string(|b| {
let s = String::from_utf8_lossy(b);
f.write_str(&*s)
})
}
}
pub fn output_handle(o: &Output) -> *mut alsa::snd_output_t { o.0 }