//! HCtl API - for mixer control and jack detection //! //! # Example //! Print all jacks and their status //! //! ``` //! for a in ::alsa::card::Iter::new().map(|x| x.unwrap()) { //! use std::ffi::CString; //! use alsa::hctl::HCtl; //! let h = HCtl::open(&CString::new(format!("hw:{}", a.get_index())).unwrap(), false).unwrap(); //! h.load().unwrap(); //! for b in h.elem_iter() { //! use alsa::ctl::ElemIface; //! let id = b.get_id().unwrap(); //! if id.get_interface() != ElemIface::Card { continue; } //! let name = id.get_name().unwrap(); //! if !name.ends_with(" Jack") { continue; } //! if name.ends_with(" Phantom Jack") { //! println!("{} is always present", &name[..name.len()-13]) //! } //! else { println!("{} is {}", &name[..name.len()-5], //! if b.read().unwrap().get_boolean(0).unwrap() { "plugged in" } else { "unplugged" }) //! } //! } //! } //! ```
usecrate::{alsa, Card}; use std::ffi::{CStr, CString}; usesuper::error::*; use std::ptr; usesuper::{ctl_int, poll}; use libc::{c_short, c_uint, c_int, pollfd};
impl Drop for HCtl { fn drop(&mutself) { unsafe { alsa::snd_hctl_close(self.0) }; }
}
impl HCtl { /// Wrapper around open that takes a &str instead of a &CStr pubfn new(c: &str, nonblock: bool) -> Result<HCtl> { Self::open(&CString::new(c).unwrap(), nonblock)
}
/// Open does not support async mode (it's not very Rustic anyway) /// Note: You probably want to call `load` afterwards. pubfn open(c: &CStr, nonblock: bool) -> Result<HCtl> { letmut r = ptr::null_mut(); let flags = if nonblock { 1 } else { 0 }; // FIXME: alsa::SND_CTL_NONBLOCK does not exist in alsa-sys
acheck!(snd_hctl_open(&mut r, c.as_ptr(), flags))
.map(|_| HCtl(r))
}
/// Wrapper around open. You probably want to call `load` afterwards. pubfn from_card(c: &Card, nonblock: bool) -> Result<HCtl> { let s = format!("hw:{}", c.get_index());
HCtl::new(&s, nonblock)
}
#[test] fn print_hctls() { for a insuper::card::Iter::new().map(|x| x.unwrap()) { use std::ffi::CString; let h = HCtl::open(&CString::new(format!("hw:{}", a.get_index())).unwrap(), false).unwrap();
h.load().unwrap();
println!("Card {}:", a.get_name().unwrap()); for b in h.elem_iter() {
println!(" {:?} - {:?}", b.get_id().unwrap(), b.read().unwrap());
}
}
}
#[test] fn print_jacks() { for a insuper::card::Iter::new().map(|x| x.unwrap()) { use std::ffi::CString; let h = HCtl::open(&CString::new(format!("hw:{}", a.get_index())).unwrap(), false).unwrap();
h.load().unwrap(); for b in h.elem_iter() { let id = b.get_id().unwrap(); if id.get_interface() != super::ctl_int::ElemIface::Card { continue; } let name = id.get_name().unwrap(); if !name.ends_with(" Jack") { continue; } if name.ends_with(" Phantom Jack") {
println!("{} is always present", &name[..name.len()-13])
} else { println!("{} is {}", &name[..name.len()-5], if b.read().unwrap().get_boolean(0).unwrap() { "plugged in" } else { "unplugged" })
}
}
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet am 2026-06-18)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.