pnmixer-rust/src/audio.rs

268 lines
7.8 KiB
Rust
Raw Normal View History

2017-06-29 12:25:40 +00:00
use alsa::card::Card;
2017-06-29 12:55:07 +00:00
use alsa::mixer::{Mixer, Selem, SelemId};
2017-06-29 12:25:40 +00:00
use alsa::poll::PollDescriptors;
use alsa_sys;
use errors::*;
2017-06-29 21:35:39 +00:00
use glib;
2017-06-29 12:25:40 +00:00
use glib_sys;
2017-06-28 15:53:19 +00:00
use libc::c_uint;
use libc::pollfd;
2017-06-29 12:25:40 +00:00
use libc::size_t;
use myalsa::*;
2017-06-30 15:24:26 +00:00
use std::cell::Ref;
2017-06-30 19:10:33 +00:00
use std::cell::RefCell;
use std::mem;
2017-06-28 23:35:30 +00:00
use std::ptr;
2017-06-30 19:10:33 +00:00
use std::rc::Rc;
2017-06-28 23:35:30 +00:00
use std::u8;
2017-06-26 07:08:37 +00:00
2017-06-29 12:55:07 +00:00
2017-06-29 12:25:40 +00:00
// TODO: implement free/destructor
pub struct AlsaCard {
_cannot_construct: (),
pub card: Card,
pub mixer: Mixer,
pub selem_id: SelemId,
pub watch_ids: Vec<u32>,
2017-06-30 15:24:26 +00:00
pub last_action_timestamp: RefCell<i64>,
pub handlers: RefCell<Vec<Box<Fn(&AlsaCard, AudioSignal, AudioUser)>>>,
2017-06-29 12:25:40 +00:00
}
2017-06-29 12:55:07 +00:00
/* TODO: AlsaCard cleanup */
2017-06-29 12:25:40 +00:00
impl AlsaCard {
2017-06-30 19:10:33 +00:00
pub fn new(card_name: Option<String>,
elem_name: Option<String>)
-> Result<Rc<RefCell<AlsaCard>>> {
2017-06-29 12:25:40 +00:00
let card = {
match card_name {
Some(name) => get_alsa_card_by_name(name)?,
None => get_default_alsa_card(),
}
};
let mixer = get_mixer(&card)?;
2017-06-30 19:10:33 +00:00
let selem_id =
get_selem_by_name(&mixer,
elem_name.unwrap_or(String::from("Master")))
.unwrap()
.get_id();
2017-06-29 12:25:40 +00:00
let vec_pollfd = PollDescriptors::get(&mixer)?;
2017-06-30 19:10:33 +00:00
let acard = Rc::new(RefCell::new(AlsaCard {
_cannot_construct: (),
card: card,
mixer: mixer,
selem_id: selem_id,
watch_ids: vec![],
last_action_timestamp:
RefCell::new(0),
handlers: RefCell::new(vec![]),
}));
2017-06-30 15:24:26 +00:00
2017-06-29 12:25:40 +00:00
/* TODO: callback is registered here, which must be unregistered
2017-06-29 21:35:39 +00:00
* when the mixer is destroyed!!
* poll descriptors must be unwatched too */
2017-06-30 19:10:33 +00:00
let watch_ids = watch_poll_descriptors(vec_pollfd,
acard.clone().as_ptr());
acard.borrow_mut().watch_ids = watch_ids;
2017-06-30 15:24:26 +00:00
2017-06-30 19:10:33 +00:00
return Ok(acard.clone());
2017-06-28 12:55:38 +00:00
}
2017-06-29 12:55:07 +00:00
2017-06-29 12:25:40 +00:00
pub fn selem(&self) -> Selem {
return get_selems(&self.mixer)
2017-06-30 19:10:33 +00:00
.nth(self.selem_id.get_index() as usize)
.unwrap();
2017-06-29 12:25:40 +00:00
}
2017-06-26 07:08:37 +00:00
2017-06-29 12:55:07 +00:00
2017-06-29 12:25:40 +00:00
pub fn vol(&self) -> Result<f64> {
return get_vol(&self.selem());
}
2017-06-26 07:08:37 +00:00
2017-06-29 12:55:07 +00:00
2017-06-29 21:35:39 +00:00
pub fn set_vol(&self, new_vol: f64, user: AudioUser) -> Result<()> {
2017-06-30 15:24:26 +00:00
{
let mut rc = self.last_action_timestamp.borrow_mut();
*rc = glib::get_monotonic_time();
}
2017-06-29 21:35:39 +00:00
// TODO invoke handlers, make use of user
2017-06-30 20:08:08 +00:00
debug!("Setting vol to {:?} by user {:?}", new_vol, user);
2017-06-29 12:25:40 +00:00
return set_vol(&self.selem(), new_vol);
}
2017-06-26 07:08:37 +00:00
2017-06-29 12:55:07 +00:00
2017-06-29 12:25:40 +00:00
pub fn has_mute(&self) -> bool {
return has_mute(&self.selem());
}
2017-06-26 07:08:37 +00:00
2017-06-29 12:55:07 +00:00
2017-06-29 12:25:40 +00:00
pub fn get_mute(&self) -> Result<bool> {
return get_mute(&self.selem());
2017-06-26 07:08:37 +00:00
}
2017-06-29 12:55:07 +00:00
2017-06-29 21:35:39 +00:00
pub fn set_mute(&self, mute: bool, user: AudioUser) -> Result<()> {
let mut rc = self.last_action_timestamp.borrow_mut();
2017-06-30 15:24:26 +00:00
*rc = glib::get_monotonic_time();
2017-06-29 21:35:39 +00:00
// TODO invoke handlers, make use of user
2017-06-30 20:08:08 +00:00
debug!("Setting mute to {} by user {:?}", mute, user);
2017-06-29 12:25:40 +00:00
return set_mute(&self.selem(), mute);
}
2017-06-29 21:35:39 +00:00
fn on_alsa_event(&self, alsa_event: AlsaEvent) {
2017-06-30 19:10:33 +00:00
let last: i64 = *Ref::clone(&self.last_action_timestamp.borrow());
if last != 0 {
let now: i64 = glib::get_monotonic_time();
let delay: i64 = now - last;
if delay < 1000000 {
return;
}
2017-06-30 20:08:08 +00:00
debug!("Discarding last time stamp, too old");
2017-06-30 19:10:33 +00:00
*self.last_action_timestamp.borrow_mut() = 0;
}
2017-06-29 21:35:39 +00:00
/* external change */
match alsa_event {
// TODO: invoke handlers with AudioUserUnknown
2017-06-30 20:08:08 +00:00
AlsaEvent::AlsaCardError => debug!("AlsaCardError"),
AlsaEvent::AlsaCardDiconnected => debug!("AlsaCardDiconnected"),
2017-06-30 15:24:26 +00:00
AlsaEvent::AlsaCardValuesChanged => {
2017-06-30 20:08:08 +00:00
debug!("AlsaCardValuesChanged");
2017-06-30 19:10:33 +00:00
self.invoke_handlers(self::AudioSignal::AudioValuesChanged,
self::AudioUser::AudioUserUnknown);
2017-06-30 15:24:26 +00:00
}
2017-06-30 20:08:08 +00:00
e => warn!("Unhandled alsa event: {:?}", e),
2017-06-29 21:35:39 +00:00
}
}
2017-06-30 15:24:26 +00:00
fn invoke_handlers(&self, signal: AudioSignal, user: AudioUser) {
2017-06-30 20:08:08 +00:00
debug!("Invoking handlers for signal {:?} by user {:?}",
signal,
user);
2017-06-30 15:24:26 +00:00
let handlers = self.handlers.borrow();
let x: &Vec<Box<Fn(&AlsaCard, AudioSignal, AudioUser)>> = &*handlers;
2017-06-30 19:10:33 +00:00
for handler in x {
let unboxed = handler.as_ref();
unboxed(&self, signal, user);
}
2017-06-30 15:24:26 +00:00
}
2017-06-30 19:10:33 +00:00
pub fn connect_handler(&self,
cb: Box<Fn(&AlsaCard, AudioSignal, AudioUser)>) {
2017-06-30 15:24:26 +00:00
self.handlers.borrow_mut().push(cb);
}
2017-06-26 15:56:09 +00:00
}
2017-06-27 22:23:12 +00:00
2017-06-29 12:55:07 +00:00
2017-06-30 20:08:08 +00:00
#[derive(Clone, Copy, Debug)]
2017-06-29 12:25:40 +00:00
pub enum AudioUser {
AudioUserUnknown,
AudioUserPopup,
AudioUserTrayIcon,
AudioUserHotkeys,
2017-06-28 15:53:19 +00:00
}
2017-06-29 12:55:07 +00:00
2017-06-30 20:08:08 +00:00
#[derive(Clone, Copy, Debug)]
2017-06-30 15:24:26 +00:00
pub enum AudioSignal {
2017-06-29 12:25:40 +00:00
AudioNoCard,
AudioCardInitialized,
AudioCardCleanedUp,
AudioCardDisconnected,
AudioCardError,
AudioValuesChanged,
2017-06-27 22:23:12 +00:00
}
2017-06-30 15:24:26 +00:00
2017-06-30 20:08:08 +00:00
#[derive(Clone, Copy, Debug)]
2017-06-30 15:24:26 +00:00
pub enum AlsaEvent {
2017-06-29 21:35:39 +00:00
AlsaCardError,
AlsaCardDiconnected,
AlsaCardValuesChanged,
}
2017-06-27 22:23:12 +00:00
2017-06-30 19:10:33 +00:00
fn watch_poll_descriptors(polls: Vec<pollfd>,
acard: *mut AlsaCard)
-> Vec<c_uint> {
2017-06-28 15:53:19 +00:00
let mut watch_ids: Vec<c_uint> = vec![];
2017-06-30 15:24:26 +00:00
let acard_ptr =
2017-06-30 19:10:33 +00:00
unsafe { mem::transmute::<*mut AlsaCard, glib_sys::gpointer>(acard) };
2017-06-28 15:53:19 +00:00
for poll in polls {
2017-06-30 19:10:33 +00:00
let gioc: *mut glib_sys::GIOChannel =
unsafe { glib_sys::g_io_channel_unix_new(poll.fd) };
let id = unsafe {
glib_sys::g_io_add_watch(
2017-06-28 15:53:19 +00:00
gioc,
2017-06-28 23:35:30 +00:00
glib_sys::GIOCondition::from_bits(
2017-06-28 15:53:19 +00:00
glib_sys::G_IO_IN.bits() | glib_sys::G_IO_ERR.bits(),
2017-06-28 23:35:30 +00:00
).unwrap(),
2017-06-28 15:53:19 +00:00
Some(watch_cb),
2017-06-30 19:10:33 +00:00
acard_ptr,
)
};
watch_ids.push(id);
unsafe { glib_sys::g_io_channel_unref(gioc) }
2017-06-28 15:53:19 +00:00
}
2017-06-28 23:35:30 +00:00
return watch_ids;
2017-06-28 15:53:19 +00:00
}
2017-06-29 12:55:07 +00:00
2017-06-30 19:10:33 +00:00
extern "C" fn watch_cb(chan: *mut glib_sys::GIOChannel,
cond: glib_sys::GIOCondition,
data: glib_sys::gpointer)
-> glib_sys::gboolean {
2017-06-28 23:35:30 +00:00
2017-06-30 15:24:26 +00:00
let acard =
2017-06-30 19:10:33 +00:00
unsafe { mem::transmute::<glib_sys::gpointer, &AlsaCard>(data) };
2017-06-30 20:08:08 +00:00
unsafe {
2017-06-30 19:10:33 +00:00
let mixer_ptr =
2017-06-30 20:08:08 +00:00
mem::transmute::<&Mixer, &*mut alsa_sys::snd_mixer_t>(&acard.mixer);
alsa_sys::snd_mixer_handle_events(*mixer_ptr);
};
2017-06-28 23:35:30 +00:00
if cond == glib_sys::G_IO_ERR {
return false as glib_sys::gboolean;
}
2017-06-29 10:51:28 +00:00
let mut sread: size_t = 1;
2017-06-29 11:43:32 +00:00
let mut buf: Vec<u8> = vec![0; 256];
2017-06-28 23:35:30 +00:00
while sread > 0 {
2017-06-30 19:10:33 +00:00
let stat: glib_sys::GIOStatus =
unsafe {
glib_sys::g_io_channel_read_chars(chan,
buf.as_mut_ptr() as *mut u8,
256,
&mut sread as *mut size_t,
ptr::null_mut())
};
2017-06-29 11:43:32 +00:00
2017-06-28 23:35:30 +00:00
match stat {
2017-06-29 11:43:32 +00:00
glib_sys::G_IO_STATUS_AGAIN => {
2017-06-30 20:08:08 +00:00
debug!("G_IO_STATUS_AGAIN");
2017-06-29 21:35:39 +00:00
continue;
}
2017-06-30 20:08:08 +00:00
glib_sys::G_IO_STATUS_NORMAL => debug!("G_IO_STATUS_NORMAL"),
glib_sys::G_IO_STATUS_ERROR => debug!("G_IO_STATUS_ERROR"),
glib_sys::G_IO_STATUS_EOF => debug!("G_IO_STATUS_EOF"),
2017-06-28 23:35:30 +00:00
}
return true as glib_sys::gboolean;
2017-06-29 10:51:28 +00:00
}
2017-06-28 23:35:30 +00:00
2017-06-30 15:24:26 +00:00
acard.on_alsa_event(AlsaEvent::AlsaCardValuesChanged);
2017-06-28 15:53:19 +00:00
return true as glib_sys::gboolean;
}