2017-06-29 12:25:40 +00:00
|
|
|
use errors::*;
|
2017-06-29 21:35:39 +00:00
|
|
|
use glib;
|
2017-07-01 23:35:12 +00:00
|
|
|
use std::cell::Cell;
|
2017-07-02 16:11:56 +00:00
|
|
|
use std::cell::Ref;
|
2017-06-30 19:10:33 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
2017-06-30 23:56:32 +00:00
|
|
|
use std::f64;
|
2017-07-01 22:03:21 +00:00
|
|
|
use alsa_pn::*;
|
2017-06-26 07:08:37 +00:00
|
|
|
|
|
|
|
|
2017-06-29 12:55:07 +00:00
|
|
|
|
2017-06-30 20:36:34 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum AudioUser {
|
|
|
|
AudioUserUnknown,
|
|
|
|
AudioUserPopup,
|
|
|
|
AudioUserTrayIcon,
|
|
|
|
AudioUserHotkeys,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum AudioSignal {
|
|
|
|
AudioNoCard,
|
|
|
|
AudioCardInitialized,
|
|
|
|
AudioCardCleanedUp,
|
|
|
|
AudioCardDisconnected,
|
|
|
|
AudioCardError,
|
|
|
|
AudioValuesChanged,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Handlers {
|
|
|
|
inner: Rc<RefCell<Vec<Box<Fn(AudioSignal, AudioUser)>>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl Handlers {
|
|
|
|
fn new() -> Handlers {
|
|
|
|
return Handlers { inner: Rc::new(RefCell::new(vec![])) };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn borrow(&self) -> Ref<Vec<Box<Fn(AudioSignal, AudioUser)>>> {
|
|
|
|
return self.inner.borrow();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn add_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>) {
|
|
|
|
self.inner.borrow_mut().push(cb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-01 22:03:21 +00:00
|
|
|
pub struct Audio {
|
2017-06-29 12:25:40 +00:00
|
|
|
_cannot_construct: (),
|
2017-07-01 22:03:21 +00:00
|
|
|
pub acard: RefCell<Box<AlsaCard>>,
|
2017-07-01 23:45:30 +00:00
|
|
|
pub last_action_timestamp: Rc<RefCell<i64>>,
|
2017-07-02 16:11:56 +00:00
|
|
|
pub handlers: Handlers,
|
2017-07-01 23:35:12 +00:00
|
|
|
pub scroll_step: Cell<u32>,
|
2017-06-29 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
2017-06-29 12:55:07 +00:00
|
|
|
|
2017-07-01 22:03:21 +00:00
|
|
|
impl Audio {
|
2017-06-30 19:10:33 +00:00
|
|
|
pub fn new(card_name: Option<String>,
|
|
|
|
elem_name: Option<String>)
|
2017-07-01 22:03:21 +00:00
|
|
|
-> Result<Audio> {
|
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
let handlers = Handlers::new();
|
2017-07-01 23:45:30 +00:00
|
|
|
let last_action_timestamp = Rc::new(RefCell::new(0));
|
2017-07-01 22:03:21 +00:00
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
let cb = {
|
|
|
|
let myhandler = handlers.clone();
|
|
|
|
let ts = last_action_timestamp.clone();
|
|
|
|
Rc::new(move |event| {
|
|
|
|
on_alsa_event(&mut *ts.borrow_mut(),
|
|
|
|
&myhandler.borrow(),
|
|
|
|
event)
|
|
|
|
})
|
|
|
|
};
|
2017-07-01 22:03:21 +00:00
|
|
|
|
|
|
|
let audio = Audio {
|
|
|
|
_cannot_construct: (),
|
|
|
|
acard: RefCell::new(AlsaCard::new(card_name, elem_name, cb)?),
|
|
|
|
last_action_timestamp: last_action_timestamp.clone(),
|
|
|
|
handlers: handlers.clone(),
|
2017-07-01 23:35:12 +00:00
|
|
|
scroll_step: Cell::new(5),
|
2017-06-29 12:25:40 +00:00
|
|
|
};
|
2017-06-28 12:55:38 +00:00
|
|
|
|
2017-07-01 22:03:21 +00:00
|
|
|
return Ok(audio);
|
|
|
|
}
|
2017-06-29 12:55:07 +00:00
|
|
|
|
2017-06-30 23:56:32 +00:00
|
|
|
|
2017-07-01 22:03:21 +00:00
|
|
|
pub fn switch_acard(&self,
|
|
|
|
card_name: Option<String>,
|
|
|
|
elem_name: Option<String>)
|
|
|
|
-> Result<()> {
|
2017-07-01 23:35:12 +00:00
|
|
|
debug!("Switching cards");
|
2017-07-02 16:11:56 +00:00
|
|
|
debug!("Old card name: {}", self.acard.borrow().card_name().unwrap());
|
|
|
|
debug!("Old chan name: {}", self.acard.borrow().chan_name().unwrap());
|
|
|
|
let cb = self.acard
|
|
|
|
.borrow()
|
|
|
|
.cb
|
|
|
|
.clone();
|
|
|
|
{
|
|
|
|
let mut ac = self.acard.borrow_mut();
|
|
|
|
*ac = AlsaCard::new(card_name, elem_name, cb)?;
|
|
|
|
}
|
|
|
|
debug!("Old card name: {}", self.acard.borrow().card_name().unwrap());
|
|
|
|
debug!("Old chan name: {}", self.acard.borrow().chan_name().unwrap());
|
2017-07-01 22:03:21 +00:00
|
|
|
return Ok(());
|
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> {
|
2017-07-01 22:03:21 +00:00
|
|
|
return self.acard.borrow().get_vol();
|
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 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
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
debug!("Setting vol on card {:?} and chan {:?} to {:?} by user {:?}",
|
|
|
|
self.acard.borrow().card_name().unwrap(),
|
|
|
|
self.acard.borrow().chan_name().unwrap(),
|
|
|
|
new_vol,
|
|
|
|
user);
|
2017-07-01 22:03:21 +00:00
|
|
|
return self.acard.borrow().set_vol(new_vol);
|
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-30 23:56:32 +00:00
|
|
|
pub fn increase_vol(&self, user: AudioUser) -> Result<()> {
|
|
|
|
{
|
|
|
|
let mut rc = self.last_action_timestamp.borrow_mut();
|
|
|
|
*rc = glib::get_monotonic_time();
|
|
|
|
}
|
|
|
|
let old_vol = self.vol()?;
|
2017-07-01 23:35:12 +00:00
|
|
|
let new_vol = f64::ceil(old_vol + (self.scroll_step.get() as f64));
|
2017-06-30 23:56:32 +00:00
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
debug!("Increase vol on card {:?} and chan {:?} by {:?} to {:?}",
|
|
|
|
self.acard.borrow().card_name().unwrap(),
|
|
|
|
self.acard.borrow().chan_name().unwrap(),
|
|
|
|
(new_vol - old_vol),
|
|
|
|
new_vol);
|
2017-06-30 23:56:32 +00:00
|
|
|
|
|
|
|
return self.set_vol(new_vol, user);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn decrease_vol(&self, user: AudioUser) -> Result<()> {
|
|
|
|
{
|
|
|
|
let mut rc = self.last_action_timestamp.borrow_mut();
|
|
|
|
*rc = glib::get_monotonic_time();
|
|
|
|
}
|
|
|
|
let old_vol = self.vol()?;
|
2017-07-01 23:35:12 +00:00
|
|
|
let new_vol = old_vol - (self.scroll_step.get() as f64);
|
2017-06-30 23:56:32 +00:00
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
debug!("Decrease vol on card {:?} and chan {:?} by {:?} to {:?}",
|
|
|
|
self.acard.borrow().card_name().unwrap(),
|
|
|
|
self.acard.borrow().chan_name().unwrap(),
|
|
|
|
(new_vol - old_vol),
|
|
|
|
new_vol);
|
2017-06-30 23:56:32 +00:00
|
|
|
|
|
|
|
return self.set_vol(new_vol, user);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-29 12:25:40 +00:00
|
|
|
pub fn has_mute(&self) -> bool {
|
2017-07-01 22:03:21 +00:00
|
|
|
return self.acard.borrow().has_mute();
|
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 get_mute(&self) -> Result<bool> {
|
2017-07-01 22:03:21 +00:00
|
|
|
return self.acard.borrow().get_mute();
|
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-07-02 16:11:56 +00:00
|
|
|
|
|
|
|
debug!("Setting mute to {} on card {:?} and chan {:?} by user {:?}",
|
|
|
|
mute,
|
|
|
|
self.acard.borrow().card_name().unwrap(),
|
|
|
|
self.acard.borrow().chan_name().unwrap(),
|
|
|
|
user);
|
|
|
|
|
2017-07-01 22:03:21 +00:00
|
|
|
return self.acard.borrow().set_mute(mute);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn connect_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>) {
|
2017-07-02 16:11:56 +00:00
|
|
|
self.handlers.add_handler(cb);
|
2017-07-01 22:03:21 +00:00
|
|
|
}
|
2017-07-02 16:11:56 +00:00
|
|
|
}
|
2017-07-01 22:03:21 +00:00
|
|
|
|
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
fn invoke_handlers(handlers: &Vec<Box<Fn(AudioSignal, AudioUser)>>,
|
|
|
|
signal: AudioSignal,
|
|
|
|
user: AudioUser) {
|
|
|
|
debug!("Invoking handlers for signal {:?} by user {:?}",
|
|
|
|
signal,
|
|
|
|
user);
|
|
|
|
for handler in handlers {
|
|
|
|
let unboxed = handler.as_ref();
|
|
|
|
unboxed(signal, user);
|
2017-06-29 12:25:40 +00:00
|
|
|
}
|
2017-07-02 16:11:56 +00:00
|
|
|
}
|
2017-06-29 21:35:39 +00:00
|
|
|
|
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
fn on_alsa_event(last_action_timestamp: &mut i64,
|
|
|
|
handlers: &Vec<Box<Fn(AudioSignal, AudioUser)>>,
|
|
|
|
alsa_event: AlsaEvent) {
|
|
|
|
let last: i64 = *last_action_timestamp;
|
2017-06-30 19:10:33 +00:00
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
debug!("Last: {}", last);
|
2017-06-29 21:35:39 +00:00
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
if last != 0 {
|
|
|
|
let now: i64 = glib::get_monotonic_time();
|
|
|
|
let delay: i64 = now - last;
|
|
|
|
if delay < 1000000 {
|
|
|
|
return;
|
2017-06-29 21:35:39 +00:00
|
|
|
}
|
2017-07-02 16:11:56 +00:00
|
|
|
debug!("Discarding last time stamp, too old");
|
|
|
|
*last_action_timestamp = 0;
|
|
|
|
}
|
2017-06-29 21:35:39 +00:00
|
|
|
|
2017-07-02 16:11:56 +00:00
|
|
|
/* external change */
|
|
|
|
match alsa_event {
|
|
|
|
// TODO: invoke handlers with AudioUserUnknown
|
|
|
|
AlsaEvent::AlsaCardError => debug!("AlsaCardError"),
|
|
|
|
AlsaEvent::AlsaCardDiconnected => debug!("AlsaCardDiconnected"),
|
|
|
|
AlsaEvent::AlsaCardValuesChanged => {
|
|
|
|
debug!("AlsaCardValuesChanged");
|
|
|
|
invoke_handlers(handlers,
|
|
|
|
self::AudioSignal::AudioValuesChanged,
|
|
|
|
self::AudioUser::AudioUserUnknown);
|
|
|
|
}
|
|
|
|
e => warn!("Unhandled alsa event: {:?}", e),
|
2017-06-29 21:35:39 +00:00
|
|
|
}
|
2017-07-02 16:11:56 +00:00
|
|
|
|
2017-06-28 15:53:19 +00:00
|
|
|
}
|