This commit is contained in:
Ospald, Julian 2017-06-29 13:43:32 +02:00
parent d83209e67f
commit 537ab6bafe
2 changed files with 15 additions and 10 deletions

View File

@ -40,15 +40,16 @@ impl AlsaCard {
} }
}; };
let mixer = audio::get_mixer(&card)?; let mixer = audio::get_mixer(&card)?;
let rc_mixer = RefCell::new(audio::get_mixer(&card)?);
let selem_id = audio::get_selem_by_name( let selem_id = audio::get_selem_by_name(
&mixer, &mixer,
elem_name.unwrap_or(String::from("Master")), elem_name.unwrap_or(String::from("Master")),
).unwrap() ).unwrap()
.get_id(); .get_id();
let vec_pollfd = PollDescriptors::get(&mixer)?; let vec_pollfd = PollDescriptors::get(&mixer)?;
// let watch_ids = vec![];
let watch_ids = audio::watch_poll_descriptors(vec_pollfd, rc_mixer); /* TODO: callback is registered here, which must be unregistered
* when the mixer is destroyed!! */
let watch_ids = audio::watch_poll_descriptors(vec_pollfd, &mixer);
return Ok(AlsaCard { return Ok(AlsaCard {
_cannot_construct: (), _cannot_construct: (),

View File

@ -15,6 +15,7 @@ use std::convert::From;
use libc::pollfd; use libc::pollfd;
use app_state; use app_state;
use std::cell::RefCell; use std::cell::RefCell;
use std::rc::Rc;
use std::mem; use std::mem;
use std::ptr; use std::ptr;
use std::u8; use std::u8;
@ -126,12 +127,11 @@ pub fn set_mute(selem: &Selem, mute: bool) -> Result<()> {
pub fn watch_poll_descriptors( pub fn watch_poll_descriptors(
polls: Vec<pollfd>, polls: Vec<pollfd>,
mixer: RefCell<Mixer>, mixer: &Mixer,
) -> Vec<c_uint> { ) -> Vec<c_uint> {
let mut watch_ids: Vec<c_uint> = vec![]; let mut watch_ids: Vec<c_uint> = vec![];
let mixer = mixer.into_inner();
let mixer_ptr = unsafe { let mixer_ptr = unsafe {
mem::transmute::<Mixer, *mut alsa_sys::snd_mixer_t>(mixer) mem::transmute::<&Mixer, &*mut alsa_sys::snd_mixer_t>(mixer)
}; };
for poll in polls { for poll in polls {
unsafe { unsafe {
@ -143,7 +143,7 @@ pub fn watch_poll_descriptors(
glib_sys::G_IO_IN.bits() | glib_sys::G_IO_ERR.bits(), glib_sys::G_IO_IN.bits() | glib_sys::G_IO_ERR.bits(),
).unwrap(), ).unwrap(),
Some(watch_cb), Some(watch_cb),
mixer_ptr as glib_sys::gpointer, *mixer_ptr as glib_sys::gpointer,
)); ));
} }
} }
@ -168,20 +168,24 @@ extern fn watch_cb(
} }
let mut sread: size_t = 1; let mut sread: size_t = 1;
let mut buf: u8 = 0; let mut buf: Vec<u8> = vec![0; 256];
while sread > 0 { while sread > 0 {
let stat: glib_sys::GIOStatus = unsafe { let stat: glib_sys::GIOStatus = unsafe {
glib_sys::g_io_channel_read_chars( glib_sys::g_io_channel_read_chars(
chan, chan,
&mut buf as *mut u8, buf.as_mut_ptr() as *mut u8,
256, 256,
&mut sread as *mut size_t, &mut sread as *mut size_t,
ptr::null_mut(), ptr::null_mut(),
) )
}; };
match stat { match stat {
glib_sys::G_IO_STATUS_AGAIN => continue, glib_sys::G_IO_STATUS_AGAIN => {
println!("G_IO_STATUS_AGAIN");
continue
},
glib_sys::G_IO_STATUS_NORMAL => println!("G_IO_STATUS_NORMAL"), glib_sys::G_IO_STATUS_NORMAL => println!("G_IO_STATUS_NORMAL"),
glib_sys::G_IO_STATUS_ERROR => println!("G_IO_STATUS_ERROR"), glib_sys::G_IO_STATUS_ERROR => println!("G_IO_STATUS_ERROR"),
glib_sys::G_IO_STATUS_EOF => println!("G_IO_STATUS_EOF"), glib_sys::G_IO_STATUS_EOF => println!("G_IO_STATUS_EOF"),