1#![allow(dead_code)]
29use crate::{alsa, Card};
37use core::ffi::CStr;
38use ::alloc::ffi::CString;
39use super::error::*;
40use core::ptr;
41use super::{ctl_int, poll};
42use libc::{c_short, c_uint, c_int, pollfd};
43
44
45#[derive(Debug)]
47pub struct HCtl(*mut alsa::snd_hctl_t);
48
49unsafe impl Send for HCtl {}
50
51impl Drop for HCtl {
52 fn drop(&mut self) { unsafe { alsa::snd_hctl_close(self.0) }; }
53}
54
55impl HCtl {
56 pub fn new(c: &str, nonblock: bool) -> Result<HCtl> {
58 Self::open(&CString::new(c).unwrap(), nonblock)
59 }
60
61 pub fn open(c: &CStr, nonblock: bool) -> Result<HCtl> {
64 let mut r = ptr::null_mut();
65 let flags = if nonblock { 1 } else { 0 }; acheck!(snd_hctl_open(&mut r, c.as_ptr(), flags))
67 .map(|_| HCtl(r))
68 }
69
70 pub fn from_card(c: &Card, nonblock: bool) -> Result<HCtl> {
72 let s = ::alloc::format!("hw:{}", c.get_index());
73 HCtl::new(&s, nonblock)
74 }
75
76 pub fn load(&self) -> Result<()> { acheck!(snd_hctl_load(self.0)).map(|_| ()) }
77
78 pub fn elem_iter(&self) -> ElemIter<'_> { ElemIter(self, ptr::null_mut()) }
79
80 pub fn find_elem(&self, id: &ctl_int::ElemId) -> Option<Elem<'_>> {
81 let p = unsafe { alsa::snd_hctl_find_elem(self.0, ctl_int::elem_id_ptr(id)) };
82 if p.is_null() { None } else { Some(Elem(self, p)) }
83 }
84
85 pub fn handle_events(&self) -> Result<u32> {
86 acheck!(snd_hctl_handle_events(self.0)).map(|x| x as u32)
87 }
88
89 pub fn wait(&self, timeout_ms: Option<u32>) -> Result<bool> {
90 acheck!(snd_hctl_wait(self.0, timeout_ms.map(|x| x as c_int).unwrap_or(-1))).map(|i| i == 1) }
91}
92
93impl poll::Descriptors for HCtl {
94 fn count(&self) -> usize {
95 unsafe { alsa::snd_hctl_poll_descriptors_count(self.0) as usize }
96 }
97 fn fill(&self, p: &mut [pollfd]) -> Result<usize> {
98 let z = unsafe { alsa::snd_hctl_poll_descriptors(self.0, p.as_mut_ptr(), p.len() as c_uint) };
99 from_code("snd_hctl_poll_descriptors", z).map(|_| z as usize)
100 }
101 fn revents(&self, p: &[pollfd]) -> Result<poll::Flags> {
102 let mut r = 0;
103 let z = unsafe { alsa::snd_hctl_poll_descriptors_revents(self.0, p.as_ptr() as *mut pollfd, p.len() as c_uint, &mut r) };
104 from_code("snd_hctl_poll_descriptors_revents", z).map(|_| poll::Flags::from_bits_truncate(r as c_short))
105 }
106}
107
108#[derive(Debug)]
110pub struct ElemIter<'a>(&'a HCtl, *mut alsa::snd_hctl_elem_t);
111
112impl<'a> Iterator for ElemIter<'a> {
113 type Item = Elem<'a>;
114 fn next(&mut self) -> Option<Elem<'a>> {
115 self.1 = if self.1.is_null() { unsafe { alsa::snd_hctl_first_elem((self.0).0) }}
116 else { unsafe { alsa::snd_hctl_elem_next(self.1) }};
117 if self.1.is_null() { None }
118 else { Some(Elem(self.0, self.1)) }
119 }
120}
121
122
123#[derive(Debug)]
125pub struct Elem<'a>(&'a HCtl, *mut alsa::snd_hctl_elem_t);
126
127impl<'a> Elem<'a> {
128 pub fn get_id(&self) -> Result<ctl_int::ElemId> {
129 let v = ctl_int::elem_id_new()?;
130 unsafe { alsa::snd_hctl_elem_get_id(self.1, ctl_int::elem_id_ptr(&v)) };
131 Ok(v)
132 }
133 pub fn info(&self) -> Result<ctl_int::ElemInfo> {
134 let v = ctl_int::elem_info_new()?;
135 acheck!(snd_hctl_elem_info(self.1, ctl_int::elem_info_ptr(&v))).map(|_| v)
136 }
137 pub fn read(&self) -> Result<ctl_int::ElemValue> {
138 let i = self.info()?;
139 let v = ctl_int::elem_value_new(i.get_type(), i.get_count())?;
140 acheck!(snd_hctl_elem_read(self.1, ctl_int::elem_value_ptr(&v))).map(|_| v)
141 }
142
143 pub fn write(&self, v: &ctl_int::ElemValue) -> Result<bool> {
144 acheck!(snd_hctl_elem_write(self.1, ctl_int::elem_value_ptr(v))).map(|e| e > 0)
145 }
146}
147
148#[test]
149fn print_hctls() {
150 extern crate std;
151 for a in super::card::Iter::new().map(|x| x.unwrap()) {
152 use ::alloc::ffi::CString;
153 let h = HCtl::open(&CString::new(::alloc::format!("hw:{}", a.get_index())).unwrap(), false).unwrap();
154 h.load().unwrap();
155 std::println!("Card {}:", a.get_name().unwrap());
156 for b in h.elem_iter() {
157 std::println!(" {:?} - {:?}", b.get_id().unwrap(), b.read().unwrap());
158 }
159 }
160}
161
162#[test]
163fn print_jacks() {
164 extern crate std;
165 for a in super::card::Iter::new().map(|x| x.unwrap()) {
166 use ::alloc::ffi::CString;
167 let h = HCtl::open(&CString::new(::alloc::format!("hw:{}", a.get_index())).unwrap(), false).unwrap();
168 h.load().unwrap();
169 for b in h.elem_iter() {
170 let id = b.get_id().unwrap();
171 if id.get_interface() != super::ctl_int::ElemIface::Card { continue; }
172 let name = id.get_name().unwrap();
173 if !name.ends_with(" Jack") { continue; }
174 if name.ends_with(" Phantom Jack") {
175 std::println!("{} is always present", &name[..name.len()-13])
176 }
177 else { std::println!("{} is {}", &name[..name.len()-5],
178 if b.read().unwrap().get_boolean(0).unwrap() { "plugged in" } else { "unplugged" })
179 }
180 }
181 }
182}