use alsa::card::Card; use alsa::mixer::{Mixer, Selem, SelemId}; use alsa::poll::PollDescriptors; use alsa_sys; use errors::*; use glib; use glib_sys; use libc::c_uint; use libc::pollfd; use libc::size_t; use myalsa::*; use std::cell::Ref; use std::cell::RefCell; use std::mem; use std::ptr; use std::rc::Rc; use std::u8; // TODO: implement free/destructor pub struct AlsaCard { _cannot_construct: (), pub card: Card, pub mixer: Mixer, pub selem_id: SelemId, pub watch_ids: Vec, pub last_action_timestamp: RefCell, pub handlers: RefCell>>, } /* TODO: AlsaCard cleanup */ impl AlsaCard { pub fn new(card_name: Option, elem_name: Option) -> Result>> { let card = { match card_name { Some(name) => get_alsa_card_by_name(name)?, None => get_default_alsa_card(), } }; let mixer = get_mixer(&card)?; let selem_id = get_selem_by_name(&mixer, elem_name.unwrap_or(String::from("Master"))) .unwrap() .get_id(); let vec_pollfd = PollDescriptors::get(&mixer)?; 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![]), })); /* TODO: callback is registered here, which must be unregistered * when the mixer is destroyed!! * poll descriptors must be unwatched too */ let watch_ids = watch_poll_descriptors(vec_pollfd, acard.clone().as_ptr()); acard.borrow_mut().watch_ids = watch_ids; return Ok(acard.clone()); } pub fn selem(&self) -> Selem { return get_selems(&self.mixer) .nth(self.selem_id.get_index() as usize) .unwrap(); } pub fn vol(&self) -> Result { return get_vol(&self.selem()); } pub fn set_vol(&self, new_vol: f64, user: AudioUser) -> Result<()> { { let mut rc = self.last_action_timestamp.borrow_mut(); *rc = glib::get_monotonic_time(); } // TODO invoke handlers, make use of user return set_vol(&self.selem(), new_vol); } pub fn has_mute(&self) -> bool { return has_mute(&self.selem()); } pub fn get_mute(&self) -> Result { return get_mute(&self.selem()); } pub fn set_mute(&self, mute: bool, user: AudioUser) -> Result<()> { let mut rc = self.last_action_timestamp.borrow_mut(); *rc = glib::get_monotonic_time(); // TODO invoke handlers, make use of user return set_mute(&self.selem(), mute); } fn on_alsa_event(&self, alsa_event: AlsaEvent) { 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 { info!("Delay: {}", delay); return; } *self.last_action_timestamp.borrow_mut() = 0; } /* external change */ match alsa_event { // TODO: invoke handlers with AudioUserUnknown AlsaEvent::AlsaCardError => info!("AlsaCardError"), AlsaEvent::AlsaCardDiconnected => info!("AlsaCardDiconnected"), AlsaEvent::AlsaCardValuesChanged => { info!("AlsaCardValuesChanged"); self.invoke_handlers(self::AudioSignal::AudioValuesChanged, self::AudioUser::AudioUserUnknown); } } } fn invoke_handlers(&self, signal: AudioSignal, user: AudioUser) { let handlers = self.handlers.borrow(); let x: &Vec> = &*handlers; for handler in x { let unboxed = handler.as_ref(); unboxed(&self, signal, user); } } pub fn connect_handler(&self, cb: Box) { self.handlers.borrow_mut().push(cb); } } #[derive(Clone, Copy)] pub enum AudioUser { AudioUserUnknown, AudioUserPopup, AudioUserTrayIcon, AudioUserHotkeys, } #[derive(Clone, Copy)] pub enum AudioSignal { AudioNoCard, AudioCardInitialized, AudioCardCleanedUp, AudioCardDisconnected, AudioCardError, AudioValuesChanged, } #[derive(Clone, Copy)] pub enum AlsaEvent { AlsaCardError, AlsaCardDiconnected, AlsaCardValuesChanged, } fn watch_poll_descriptors(polls: Vec, acard: *mut AlsaCard) -> Vec { let mut watch_ids: Vec = vec![]; let acard_ptr = unsafe { mem::transmute::<*mut AlsaCard, glib_sys::gpointer>(acard) }; for poll in polls { 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( gioc, glib_sys::GIOCondition::from_bits( glib_sys::G_IO_IN.bits() | glib_sys::G_IO_ERR.bits(), ).unwrap(), Some(watch_cb), acard_ptr, ) }; watch_ids.push(id); unsafe { glib_sys::g_io_channel_unref(gioc) } } return watch_ids; } extern "C" fn watch_cb(chan: *mut glib_sys::GIOChannel, cond: glib_sys::GIOCondition, data: glib_sys::gpointer) -> glib_sys::gboolean { let acard = unsafe { mem::transmute::(data) }; { let mixer_ptr = unsafe { mem::transmute::<&Mixer, &*mut alsa_sys::snd_mixer_t>(&acard.mixer) }; unsafe { alsa_sys::snd_mixer_handle_events(*mixer_ptr); } } if cond == glib_sys::G_IO_ERR { return false as glib_sys::gboolean; } let mut sread: size_t = 1; let mut buf: Vec = vec![0; 256]; while sread > 0 { 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()) }; match stat { glib_sys::G_IO_STATUS_AGAIN => { info!("G_IO_STATUS_AGAIN"); continue; } glib_sys::G_IO_STATUS_NORMAL => info!("G_IO_STATUS_NORMAL"), glib_sys::G_IO_STATUS_ERROR => info!("G_IO_STATUS_ERROR"), glib_sys::G_IO_STATUS_EOF => info!("G_IO_STATUS_EOF"), } return true as glib_sys::gboolean; } acard.on_alsa_event(AlsaEvent::AlsaCardValuesChanged); return true as glib_sys::gboolean; }