Refuxxor
This commit is contained in:
parent
055459fa7a
commit
5ec5a9a151
313
src/alsa_pn.rs
Normal file
313
src/alsa_pn.rs
Normal file
@ -0,0 +1,313 @@
|
|||||||
|
use alsa::card::Card;
|
||||||
|
use alsa::mixer::SelemChannelId::*;
|
||||||
|
use alsa::mixer::{Mixer, Selem, Elem, SelemId};
|
||||||
|
use alsa::poll::PollDescriptors;
|
||||||
|
use alsa;
|
||||||
|
use alsa_sys;
|
||||||
|
use errors::*;
|
||||||
|
use glib_sys;
|
||||||
|
use libc::c_int;
|
||||||
|
use libc::c_uint;
|
||||||
|
use libc::pollfd;
|
||||||
|
use libc::size_t;
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::iter::Map;
|
||||||
|
use std::mem;
|
||||||
|
use std::ptr;
|
||||||
|
use std::rc::Rc;
|
||||||
|
use std::u8;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum AlsaEvent {
|
||||||
|
AlsaCardError,
|
||||||
|
AlsaCardDiconnected,
|
||||||
|
AlsaCardValuesChanged,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub struct AlsaCard {
|
||||||
|
_cannot_construct: (),
|
||||||
|
pub card: Card,
|
||||||
|
pub mixer: Rc<Mixer>,
|
||||||
|
pub selem_id: SelemId,
|
||||||
|
pub watch_ids: RefCell<Vec<u32>>,
|
||||||
|
pub cb: Rc<Fn(AlsaEvent)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl AlsaCard {
|
||||||
|
pub fn new(card_name: Option<String>,
|
||||||
|
elem_name: Option<String>,
|
||||||
|
cb: Rc<Fn(AlsaEvent)>)
|
||||||
|
-> Result<Box<AlsaCard>> {
|
||||||
|
let card = {
|
||||||
|
match card_name {
|
||||||
|
Some(name) => get_alsa_card_by_name(name)?,
|
||||||
|
None => get_default_alsa_card(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mixer = Rc::new(get_mixer(&card)?);
|
||||||
|
let mixer2 = mixer.clone();
|
||||||
|
let selem_id =
|
||||||
|
get_selem_by_name(&mixer,
|
||||||
|
elem_name.unwrap_or(String::from("Master")))
|
||||||
|
.unwrap()
|
||||||
|
.get_id();
|
||||||
|
|
||||||
|
let acard = Box::new(AlsaCard {
|
||||||
|
_cannot_construct: (),
|
||||||
|
card: card,
|
||||||
|
mixer: mixer,
|
||||||
|
selem_id: selem_id,
|
||||||
|
watch_ids: RefCell::new(vec![]),
|
||||||
|
cb: cb,
|
||||||
|
});
|
||||||
|
|
||||||
|
let vec_pollfd = PollDescriptors::get(mixer2.as_ref())?;
|
||||||
|
/* TODO: callback is registered here, which must be unregistered
|
||||||
|
* when the card is destroyed!!
|
||||||
|
* poll descriptors must be unwatched too */
|
||||||
|
let watch_ids = AlsaCard::watch_poll_descriptors(vec_pollfd,
|
||||||
|
acard.as_ref());
|
||||||
|
*acard.watch_ids.borrow_mut() = watch_ids;
|
||||||
|
|
||||||
|
return Ok(acard);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn selem(&self) -> Selem {
|
||||||
|
return get_selems(&self.mixer)
|
||||||
|
.nth(self.selem_id.get_index() as usize)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_vol(&self) -> Result<f64> {
|
||||||
|
let selem = self.selem();
|
||||||
|
let range = selem.get_playback_volume_range();
|
||||||
|
let volume = selem.get_playback_volume(FrontRight).map(|v| {
|
||||||
|
return vol_to_percent(v, range);
|
||||||
|
});
|
||||||
|
|
||||||
|
return volume.from_err();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn set_vol(&self, new_vol: f64) -> Result<()> {
|
||||||
|
let selem = self.selem();
|
||||||
|
/* auto-unmute */
|
||||||
|
if self.get_mute()? {
|
||||||
|
self.set_mute(false)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let range = selem.get_playback_volume_range();
|
||||||
|
selem.set_playback_volume_all(percent_to_vol(new_vol, range))?;
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn has_mute(&self) -> bool {
|
||||||
|
let selem = self.selem();
|
||||||
|
return selem.has_playback_switch();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_mute(&self) -> Result<bool> {
|
||||||
|
let selem = self.selem();
|
||||||
|
let val = selem.get_playback_switch(FrontRight)?;
|
||||||
|
return Ok(val == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn set_mute(&self, mute: bool) -> Result<()> {
|
||||||
|
let selem = self.selem();
|
||||||
|
/* true -> mute, false -> unmute */
|
||||||
|
let _ = selem.set_playback_switch_all(!mute as i32)?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn watch_poll_descriptors(polls: Vec<pollfd>,
|
||||||
|
acard: &AlsaCard)
|
||||||
|
-> Vec<c_uint> {
|
||||||
|
let mut watch_ids: Vec<c_uint> = vec![];
|
||||||
|
let acard_ptr =
|
||||||
|
unsafe { mem::transmute::<&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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn unwatch_poll_descriptors(watch_ids: &Vec<u32>) {
|
||||||
|
for watch_id in watch_ids {
|
||||||
|
unsafe {
|
||||||
|
glib_sys::g_source_remove(*watch_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl Drop for AlsaCard {
|
||||||
|
// call Box::new(x), transmute the Box into a raw pointer, and then
|
||||||
|
// std::mem::forget
|
||||||
|
//
|
||||||
|
// if you unregister the callback, you should keep a raw pointer to the
|
||||||
|
// box
|
||||||
|
//
|
||||||
|
// For instance, `register` could return a raw pointer to the
|
||||||
|
// Box + a std::marker::PhantomData with the appropriate
|
||||||
|
// lifetime (if applicable)
|
||||||
|
//
|
||||||
|
// The struct could implement Drop, which unregisters the
|
||||||
|
// callback and frees the Box, by simply transmuting the
|
||||||
|
// raw pointer to a Box<T>
|
||||||
|
fn drop(&mut self) {
|
||||||
|
debug!("Destructing watch_ids: {:?}", self.watch_ids);
|
||||||
|
AlsaCard::unwatch_poll_descriptors(&self.watch_ids.borrow());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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::<glib_sys::gpointer, &AlsaCard>(data) };
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
let mixer_ptr =
|
||||||
|
mem::transmute::<&Mixer, &*mut alsa_sys::snd_mixer_t>(&acard.mixer);
|
||||||
|
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<u8> = 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 => {
|
||||||
|
debug!("G_IO_STATUS_AGAIN");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
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"),
|
||||||
|
}
|
||||||
|
return true as glib_sys::gboolean;
|
||||||
|
}
|
||||||
|
let cb = &acard.cb;
|
||||||
|
cb(AlsaEvent::AlsaCardValuesChanged);
|
||||||
|
|
||||||
|
return true as glib_sys::gboolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_default_alsa_card() -> Card {
|
||||||
|
return get_alsa_card_by_id(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_alsa_card_by_id(index: c_int) -> Card {
|
||||||
|
return Card::new(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_alsa_cards() -> alsa::card::Iter {
|
||||||
|
return alsa::card::Iter::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_alsa_card_by_name(name: String) -> Result<Card> {
|
||||||
|
for r_card in get_alsa_cards() {
|
||||||
|
let card = r_card?;
|
||||||
|
let card_name = card.get_name()?;
|
||||||
|
if name == card_name {
|
||||||
|
return Ok(card);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bail!("Not found a matching card named {}", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_mixer(card: &Card) -> Result<Mixer> {
|
||||||
|
return Mixer::new(&format!("hw:{}", card.get_index()), false).from_err();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_selem(elem: Elem) -> Selem {
|
||||||
|
/* in the ALSA API, there are currently only simple elements,
|
||||||
|
* so this unwrap() should be safe.
|
||||||
|
*http://www.alsa-project.org/alsa-doc/alsa-lib/group___mixer.html#enum-members */
|
||||||
|
return Selem::new(elem).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_selems(mixer: &Mixer) -> Map<alsa::mixer::Iter, fn(Elem) -> Selem> {
|
||||||
|
return mixer.iter().map(get_selem);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_selem_by_name(mixer: &Mixer, name: String) -> Result<Selem> {
|
||||||
|
for selem in get_selems(mixer) {
|
||||||
|
let n = selem.get_id()
|
||||||
|
.get_name()
|
||||||
|
.map(|y| String::from(y))?;
|
||||||
|
|
||||||
|
if n == name {
|
||||||
|
return Ok(selem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bail!("Not found a matching selem named {}", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn vol_to_percent(vol: i64, range: (i64, i64)) -> f64 {
|
||||||
|
let (min, max) = range;
|
||||||
|
return ((vol - min) as f64) / ((max - min) as f64) * 100.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn percent_to_vol(vol: f64, range: (i64, i64)) -> i64 {
|
||||||
|
let (min, max) = range;
|
||||||
|
let _v = vol / 100.0 * ((max - min) as f64) + (min as f64);
|
||||||
|
/* TODO: precision? Use direction. */
|
||||||
|
return _v as i64;
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
use gtk;
|
use gtk;
|
||||||
use audio::AlsaCard;
|
use audio::Audio;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use glade_helpers::*;
|
use glade_helpers::*;
|
||||||
@ -10,7 +10,7 @@ use glade_helpers::*;
|
|||||||
// TODO: glade stuff, config, alsacard
|
// TODO: glade stuff, config, alsacard
|
||||||
pub struct AppS {
|
pub struct AppS {
|
||||||
pub gui: Gui,
|
pub gui: Gui,
|
||||||
pub acard: Rc<RefCell<AlsaCard>>,
|
pub audio: Audio,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ impl AppS {
|
|||||||
let builder_popup_menu = gtk::Builder::new_from_string(include_str!("../data/ui/popup-menu.glade"));
|
let builder_popup_menu = gtk::Builder::new_from_string(include_str!("../data/ui/popup-menu.glade"));
|
||||||
return AppS {
|
return AppS {
|
||||||
gui: Gui::new(builder_popup_window, builder_popup_menu),
|
gui: Gui::new(builder_popup_window, builder_popup_menu),
|
||||||
acard: AlsaCard::new(None, Some(String::from("Master")))
|
audio: Audio::new(None, Some(String::from("Master")))
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
277
src/audio.rs
277
src/audio.rs
@ -1,21 +1,9 @@
|
|||||||
use alsa::card::Card;
|
|
||||||
use alsa::mixer::{Mixer, Selem, SelemId};
|
|
||||||
use alsa::poll::PollDescriptors;
|
|
||||||
use alsa_sys;
|
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use glib;
|
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::cell::RefCell;
|
||||||
use std::mem;
|
|
||||||
use std::ptr;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::u8;
|
|
||||||
use std::f64;
|
use std::f64;
|
||||||
|
use alsa_pn::*;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -39,87 +27,60 @@ pub enum AudioSignal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
pub struct Audio {
|
||||||
pub enum AlsaEvent {
|
|
||||||
AlsaCardError,
|
|
||||||
AlsaCardDiconnected,
|
|
||||||
AlsaCardValuesChanged,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// TODO: Audio struct? Separate more cleanly
|
|
||||||
// TODO: implement free/destructor
|
|
||||||
pub struct AlsaCard {
|
|
||||||
_cannot_construct: (),
|
_cannot_construct: (),
|
||||||
pub card: Card,
|
pub acard: RefCell<Box<AlsaCard>>,
|
||||||
pub mixer: Mixer,
|
|
||||||
pub selem_id: SelemId,
|
|
||||||
pub watch_ids: Vec<u32>,
|
|
||||||
pub last_action_timestamp: RefCell<i64>,
|
pub last_action_timestamp: RefCell<i64>,
|
||||||
pub handlers: RefCell<Vec<Box<Fn(AudioSignal, AudioUser)>>>,
|
pub handlers: Rc<RefCell<Vec<Box<Fn(AudioSignal, AudioUser)>>>>,
|
||||||
pub scroll_step: RefCell<u32>,
|
pub scroll_step: RefCell<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* TODO: AlsaCard cleanup */
|
impl Audio {
|
||||||
impl AlsaCard {
|
|
||||||
pub fn new(card_name: Option<String>,
|
pub fn new(card_name: Option<String>,
|
||||||
elem_name: Option<String>)
|
elem_name: Option<String>)
|
||||||
-> Result<Rc<RefCell<AlsaCard>>> {
|
-> Result<Audio> {
|
||||||
let card = {
|
|
||||||
match card_name {
|
let handlers = Rc::new(RefCell::new(vec![]));
|
||||||
Some(name) => get_alsa_card_by_name(name)?,
|
let last_action_timestamp = RefCell::new(0);
|
||||||
None => get_default_alsa_card(),
|
|
||||||
}
|
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(),
|
||||||
|
scroll_step: RefCell::new(5),
|
||||||
};
|
};
|
||||||
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)?;
|
|
||||||
|
|
||||||
// TODO: rework, should probably be a Box?
|
return Ok(audio);
|
||||||
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![]),
|
|
||||||
scroll_step: RefCell::new(3),
|
|
||||||
}));
|
|
||||||
|
|
||||||
/* 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.as_ptr());
|
|
||||||
acard.borrow_mut().watch_ids = watch_ids;
|
|
||||||
|
|
||||||
return Ok(acard.clone());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// pub fn switch_card(old_card: Rc<RefCell<AlsaCard>>,
|
pub fn switch_acard(&self,
|
||||||
// card_name: Option<String>,
|
card_name: Option<String>,
|
||||||
// elem_name: Option<String>)
|
elem_name: Option<String>)
|
||||||
// -> Result<Rc<RefCell<AlsaCard>>> {
|
-> Result<()> {
|
||||||
|
let mut ac = self.acard.borrow_mut();
|
||||||
|
let cb = self.acard
|
||||||
|
.borrow()
|
||||||
|
.cb
|
||||||
|
.clone();
|
||||||
|
*ac = AlsaCard::new(card_name, elem_name, cb)?;
|
||||||
|
|
||||||
// }
|
return Ok(());
|
||||||
|
|
||||||
|
|
||||||
pub fn selem(&self) -> Selem {
|
|
||||||
return get_selems(&self.mixer)
|
|
||||||
.nth(self.selem_id.get_index() as usize)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn vol(&self) -> Result<f64> {
|
pub fn vol(&self) -> Result<f64> {
|
||||||
return get_vol(&self.selem());
|
return self.acard.borrow().get_vol();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -131,7 +92,7 @@ impl AlsaCard {
|
|||||||
// TODO invoke handlers, make use of user
|
// TODO invoke handlers, make use of user
|
||||||
|
|
||||||
debug!("Setting vol to {:?} by user {:?}", new_vol, user);
|
debug!("Setting vol to {:?} by user {:?}", new_vol, user);
|
||||||
return set_vol(&self.selem(), new_vol);
|
return self.acard.borrow().set_vol(new_vol);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -164,12 +125,12 @@ impl AlsaCard {
|
|||||||
|
|
||||||
|
|
||||||
pub fn has_mute(&self) -> bool {
|
pub fn has_mute(&self) -> bool {
|
||||||
return has_mute(&self.selem());
|
return self.acard.borrow().has_mute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn get_mute(&self) -> Result<bool> {
|
pub fn get_mute(&self) -> Result<bool> {
|
||||||
return get_mute(&self.selem());
|
return self.acard.borrow().get_mute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -178,12 +139,32 @@ impl AlsaCard {
|
|||||||
*rc = glib::get_monotonic_time();
|
*rc = glib::get_monotonic_time();
|
||||||
// TODO invoke handlers, make use of user
|
// TODO invoke handlers, make use of user
|
||||||
debug!("Setting mute to {} by user {:?}", mute, user);
|
debug!("Setting mute to {} by user {:?}", mute, user);
|
||||||
return set_mute(&self.selem(), mute);
|
return self.acard.borrow().set_mute(mute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn on_alsa_event(&self, alsa_event: AlsaEvent) {
|
pub fn connect_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>) {
|
||||||
let last: i64 = *Ref::clone(&self.last_action_timestamp.borrow());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn on_alsa_event(last_action_timestamp: &mut i64,
|
||||||
|
handlers: &Vec<Box<Fn(AudioSignal, AudioUser)>>,
|
||||||
|
alsa_event: AlsaEvent) {
|
||||||
|
let last: i64 = *last_action_timestamp;
|
||||||
|
|
||||||
if last != 0 {
|
if last != 0 {
|
||||||
let now: i64 = glib::get_monotonic_time();
|
let now: i64 = glib::get_monotonic_time();
|
||||||
@ -192,7 +173,7 @@ impl AlsaCard {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
debug!("Discarding last time stamp, too old");
|
debug!("Discarding last time stamp, too old");
|
||||||
*self.last_action_timestamp.borrow_mut() = 0;
|
*last_action_timestamp = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* external change */
|
/* external change */
|
||||||
@ -202,136 +183,12 @@ impl AlsaCard {
|
|||||||
AlsaEvent::AlsaCardDiconnected => debug!("AlsaCardDiconnected"),
|
AlsaEvent::AlsaCardDiconnected => debug!("AlsaCardDiconnected"),
|
||||||
AlsaEvent::AlsaCardValuesChanged => {
|
AlsaEvent::AlsaCardValuesChanged => {
|
||||||
debug!("AlsaCardValuesChanged");
|
debug!("AlsaCardValuesChanged");
|
||||||
self.invoke_handlers(self::AudioSignal::AudioValuesChanged,
|
Audio::invoke_handlers(handlers,
|
||||||
self::AudioUser::AudioUserUnknown);
|
self::AudioSignal::AudioValuesChanged,
|
||||||
|
self::AudioUser::AudioUserUnknown);
|
||||||
}
|
}
|
||||||
e => warn!("Unhandled alsa event: {:?}", e),
|
e => warn!("Unhandled alsa event: {:?}", e),
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn invoke_handlers(&self, signal: AudioSignal, user: AudioUser) {
|
|
||||||
debug!("Invoking handlers for signal {:?} by user {:?}",
|
|
||||||
signal,
|
|
||||||
user);
|
|
||||||
let handlers = self.handlers.borrow();
|
|
||||||
let handlers_ref: &Vec<Box<Fn(AudioSignal, AudioUser)>> =
|
|
||||||
handlers.as_ref();
|
|
||||||
for handler in handlers_ref {
|
|
||||||
let unboxed = handler.as_ref();
|
|
||||||
unboxed(signal, user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn connect_handler(&self, cb: Box<Fn(AudioSignal, AudioUser)>) {
|
|
||||||
self.handlers.borrow_mut().push(cb);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl Drop for AlsaCard {
|
|
||||||
// call Box::new(x), transmute the Box into a raw pointer, and then
|
|
||||||
// std::mem::forget
|
|
||||||
//
|
|
||||||
// if you unregister the callback, you should keep a raw pointer to the
|
|
||||||
// box
|
|
||||||
//
|
|
||||||
// For instance, `register` could return a raw pointer to the
|
|
||||||
// Box + a std::marker::PhantomData with the appropriate
|
|
||||||
// lifetime (if applicable)
|
|
||||||
//
|
|
||||||
// The struct could implement Drop, which unregisters the
|
|
||||||
// callback and frees the Box, by simply transmuting the
|
|
||||||
// raw pointer to a Box<T>
|
|
||||||
fn drop(&mut self) {
|
|
||||||
debug!("Destructing watch_ids: {:?}", self.watch_ids);
|
|
||||||
unwatch_poll_descriptors(&self.watch_ids);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn watch_poll_descriptors(polls: Vec<pollfd>,
|
|
||||||
acard: *mut AlsaCard)
|
|
||||||
-> Vec<c_uint> {
|
|
||||||
let mut watch_ids: Vec<c_uint> = 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn unwatch_poll_descriptors(watch_ids: &Vec<u32>) {
|
|
||||||
for watch_id in watch_ids {
|
|
||||||
unsafe {
|
|
||||||
glib_sys::g_source_remove(*watch_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
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::<glib_sys::gpointer, &AlsaCard>(data) };
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
let mixer_ptr =
|
|
||||||
mem::transmute::<&Mixer, &*mut alsa_sys::snd_mixer_t>(&acard.mixer);
|
|
||||||
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<u8> = 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 => {
|
|
||||||
debug!("G_IO_STATUS_AGAIN");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
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"),
|
|
||||||
}
|
|
||||||
return true as glib_sys::gboolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
acard.on_alsa_event(AlsaEvent::AlsaCardValuesChanged);
|
|
||||||
|
|
||||||
return true as glib_sys::gboolean;
|
|
||||||
}
|
}
|
||||||
|
@ -18,4 +18,3 @@ macro_rules! create_builder_item {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,9 +28,9 @@ mod errors;
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod glade_helpers;
|
mod glade_helpers;
|
||||||
|
|
||||||
|
mod alsa_pn;
|
||||||
mod app_state;
|
mod app_state;
|
||||||
mod audio;
|
mod audio;
|
||||||
mod myalsa;
|
|
||||||
mod ui_entry;
|
mod ui_entry;
|
||||||
mod ui_popup_menu;
|
mod ui_popup_menu;
|
||||||
mod ui_popup_window;
|
mod ui_popup_window;
|
||||||
|
123
src/myalsa.rs
123
src/myalsa.rs
@ -1,123 +0,0 @@
|
|||||||
use alsa;
|
|
||||||
use alsa::card::Card;
|
|
||||||
use alsa::mixer::{Mixer, Selem, Elem};
|
|
||||||
use alsa::mixer::SelemChannelId::*;
|
|
||||||
use std::iter::Map;
|
|
||||||
use libc::c_int;
|
|
||||||
use errors::*;
|
|
||||||
use app_state;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_default_alsa_card() -> Card {
|
|
||||||
return get_alsa_card_by_id(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_alsa_card_by_id(index: c_int) -> Card {
|
|
||||||
return Card::new(index);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_alsa_cards() -> alsa::card::Iter {
|
|
||||||
return alsa::card::Iter::new();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_alsa_card_by_name(name: String) -> Result<Card> {
|
|
||||||
for r_card in get_alsa_cards() {
|
|
||||||
let card = r_card?;
|
|
||||||
let card_name = card.get_name()?;
|
|
||||||
if name == card_name {
|
|
||||||
return Ok(card);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bail!("Not found a matching card named {}", name);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_mixer(card: &Card) -> Result<Mixer> {
|
|
||||||
return Mixer::new(&format!("hw:{}", card.get_index()), false).from_err();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_selem(elem: Elem) -> Selem {
|
|
||||||
/* in the ALSA API, there are currently only simple elements,
|
|
||||||
* so this unwrap() should be safe.
|
|
||||||
*http://www.alsa-project.org/alsa-doc/alsa-lib/group___mixer.html#enum-members */
|
|
||||||
return Selem::new(elem).unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_selems(mixer: &Mixer) -> Map<alsa::mixer::Iter, fn(Elem) -> Selem> {
|
|
||||||
return mixer.iter().map(get_selem);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_selem_by_name(mixer: &Mixer, name: String) -> Result<Selem> {
|
|
||||||
for selem in get_selems(mixer) {
|
|
||||||
let n = selem.get_id()
|
|
||||||
.get_name()
|
|
||||||
.map(|y| String::from(y))?;
|
|
||||||
|
|
||||||
if n == name {
|
|
||||||
return Ok(selem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bail!("Not found a matching selem named {}", name);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn vol_to_percent(vol: i64, range: (i64, i64)) -> f64 {
|
|
||||||
let (min, max) = range;
|
|
||||||
return ((vol - min) as f64) / ((max - min) as f64) * 100.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn percent_to_vol(vol: f64, range: (i64, i64)) -> i64 {
|
|
||||||
let (min, max) = range;
|
|
||||||
let _v = vol / 100.0 * ((max - min) as f64) + (min as f64);
|
|
||||||
/* TODO: precision? Use direction. */
|
|
||||||
return _v as i64;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_vol(selem: &Selem) -> Result<f64> {
|
|
||||||
let range = selem.get_playback_volume_range();
|
|
||||||
let volume = selem.get_playback_volume(FrontRight).map(|v| {
|
|
||||||
return vol_to_percent(v, range);
|
|
||||||
});
|
|
||||||
|
|
||||||
return volume.from_err();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn set_vol(selem: &Selem, new_vol: f64) -> Result<()> {
|
|
||||||
/* auto-unmute */
|
|
||||||
if get_mute(selem)? {
|
|
||||||
set_mute(selem, false)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
let range = selem.get_playback_volume_range();
|
|
||||||
selem.set_playback_volume_all(percent_to_vol(new_vol, range))?;
|
|
||||||
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn has_mute(selem: &Selem) -> bool {
|
|
||||||
return selem.has_playback_switch();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn get_mute(selem: &Selem) -> Result<bool> {
|
|
||||||
let val = selem.get_playback_switch(FrontRight)?;
|
|
||||||
return Ok(val == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn set_mute(selem: &Selem, mute: bool) -> Result<()> {
|
|
||||||
/* true -> mute, false -> unmute */
|
|
||||||
let _ = selem.set_playback_switch_all(!mute as i32)?;
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
@ -1,6 +1,5 @@
|
|||||||
use app_state::*;
|
use app_state::*;
|
||||||
use audio::{AlsaCard, AudioSignal, AudioUser};
|
use audio::{AudioSignal, AudioUser};
|
||||||
use std::cell::RefCell;
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use ui_popup_menu::*;
|
use ui_popup_menu::*;
|
||||||
use ui_popup_window::*;
|
use ui_popup_window::*;
|
||||||
@ -11,7 +10,7 @@ use ui_tray_icon::*;
|
|||||||
pub fn init(appstate: Rc<AppS>) {
|
pub fn init(appstate: Rc<AppS>) {
|
||||||
{
|
{
|
||||||
let apps = appstate.clone();
|
let apps = appstate.clone();
|
||||||
appstate.acard.borrow().connect_handler(Box::new(move |s, u| {
|
appstate.audio.connect_handler(Box::new(move |s, u| {
|
||||||
match (s, u) {
|
match (s, u) {
|
||||||
(AudioSignal::AudioValuesChanged,
|
(AudioSignal::AudioValuesChanged,
|
||||||
AudioUser::AudioUserUnknown) => {
|
AudioUser::AudioUserUnknown) => {
|
||||||
|
@ -1,15 +1,6 @@
|
|||||||
use app_state::*;
|
use app_state::*;
|
||||||
use gdk;
|
|
||||||
use gdk_sys;
|
|
||||||
use glib;
|
|
||||||
use glib_sys;
|
|
||||||
use std::mem;
|
|
||||||
use gtk::prelude::*;
|
use gtk::prelude::*;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::cell::RefCell;
|
|
||||||
use libc;
|
|
||||||
use audio::AudioUser::*;
|
|
||||||
use errors::*;
|
|
||||||
use gtk;
|
use gtk;
|
||||||
|
|
||||||
|
|
||||||
|
@ -71,13 +71,13 @@ pub fn init_popup_window(appstate: Rc<AppS>) {
|
|||||||
fn on_popup_window_show(window: >k::Window,
|
fn on_popup_window_show(window: >k::Window,
|
||||||
appstate: &AppS,
|
appstate: &AppS,
|
||||||
toggle_signal: u64) {
|
toggle_signal: u64) {
|
||||||
let acard = appstate.acard.borrow();
|
let audio = &appstate.audio;
|
||||||
let popup_window = &appstate.gui.popup_window;
|
let popup_window = &appstate.gui.popup_window;
|
||||||
|
|
||||||
let cur_vol = try_w!(acard.vol());
|
let cur_vol = try_w!(audio.vol());
|
||||||
set_slider(&popup_window.vol_scale_adj, cur_vol);
|
set_slider(&popup_window.vol_scale_adj, cur_vol);
|
||||||
|
|
||||||
let muted = acard.get_mute();
|
let muted = audio.get_mute();
|
||||||
update_mute_check(&appstate, toggle_signal, muted);
|
update_mute_check(&appstate, toggle_signal, muted);
|
||||||
|
|
||||||
popup_window.vol_scale.grab_focus();
|
popup_window.vol_scale.grab_focus();
|
||||||
@ -118,22 +118,22 @@ fn on_popup_window_event(w: >k::Window,
|
|||||||
|
|
||||||
|
|
||||||
fn on_vol_scale_value_changed(appstate: &AppS) {
|
fn on_vol_scale_value_changed(appstate: &AppS) {
|
||||||
let acard = appstate.acard.borrow();
|
let audio = &appstate.audio;
|
||||||
|
|
||||||
let val = appstate.gui
|
let val = appstate.gui
|
||||||
.popup_window
|
.popup_window
|
||||||
.vol_scale
|
.vol_scale
|
||||||
.get_value();
|
.get_value();
|
||||||
|
|
||||||
try_w!(acard.set_vol(val, AudioUserPopup));
|
try_w!(audio.set_vol(val, AudioUserPopup));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn on_mute_check_toggled(appstate: &AppS) {
|
fn on_mute_check_toggled(appstate: &AppS) {
|
||||||
let acard = appstate.acard.borrow();
|
let audio = &appstate.audio;
|
||||||
|
|
||||||
let muted = try_w!(acard.get_mute());
|
let muted = try_w!(audio.get_mute());
|
||||||
let _ = try_w!(acard.set_mute(!muted, AudioUserPopup));
|
let _ = try_w!(audio.set_mute(!muted, AudioUserPopup));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -66,15 +66,15 @@ fn on_tray_icon_scroll_event(appstate: &AppS,
|
|||||||
event: &gdk::EventScroll)
|
event: &gdk::EventScroll)
|
||||||
-> bool {
|
-> bool {
|
||||||
|
|
||||||
|
let audio = &appstate.audio;
|
||||||
|
|
||||||
let scroll_dir: gdk::ScrollDirection = event.get_direction();
|
let scroll_dir: gdk::ScrollDirection = event.get_direction();
|
||||||
match scroll_dir {
|
match scroll_dir {
|
||||||
gdk::ScrollDirection::Up => {
|
gdk::ScrollDirection::Up => {
|
||||||
try_wr!(appstate.acard.borrow().increase_vol(AudioUserTrayIcon),
|
try_wr!(appstate.audio.increase_vol(AudioUserTrayIcon), false);
|
||||||
false);
|
|
||||||
}
|
}
|
||||||
gdk::ScrollDirection::Down => {
|
gdk::ScrollDirection::Down => {
|
||||||
try_wr!(appstate.acard.borrow().decrease_vol(AudioUserTrayIcon),
|
try_wr!(appstate.audio.decrease_vol(AudioUserTrayIcon), false);
|
||||||
false);
|
|
||||||
}
|
}
|
||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user