pnmixer-rust/src/audio.rs

196 lines
5.5 KiB
Rust
Raw Normal View History

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-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-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-01 22:03:21 +00:00
pub handlers: Rc<RefCell<Vec<Box<Fn(AudioSignal, AudioUser)>>>>,
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> {
let handlers = Rc::new(RefCell::new(vec![]));
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
let myhandler = handlers.clone();
let ts = last_action_timestamp.clone();
let cb = Rc::new(move |event| {
Audio::on_alsa_event(&mut *ts.borrow_mut(),
&myhandler.borrow(),
event)
});
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");
let cb = self.acard.borrow().cb.clone();
2017-07-01 22:03:21 +00:00
let mut ac = self.acard.borrow_mut();
2017-07-01 23:35:12 +00:00
*ac = AlsaCard::new(card_name,
elem_name,
cb)?;
2017-06-30 23:56:32 +00:00
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
debug!("Setting vol to {:?} by user {:?}", 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
debug!("Increase vol by {:?} to {:?}", (new_vol - old_vol), new_vol);
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
debug!("Decrease vol by {:?} to {:?}", (new_vol - old_vol), new_vol);
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-06-30 20:08:08 +00:00
debug!("Setting mute to {} by user {:?}", mute, 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)>) {
self.handlers.borrow_mut().push(cb);
}
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-06-29 21:35:39 +00:00
2017-07-01 22:03:21 +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
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-07-01 22:03:21 +00:00
*last_action_timestamp = 0;
2017-06-30 19:10:33 +00:00
}
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-07-01 22:03:21 +00:00
Audio::invoke_handlers(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-28 15:53:19 +00:00
}