1use core::ptr;
18use libc::{c_void, c_int};
19use crate::alsa;
20use super::{Card, Direction};
21use super::error::*;
22use core::ffi::CStr;
23use ::alloc::ffi::CString;
24use ::alloc::string::String;
25
26#[derive(Debug)]
28pub struct HintIter(*mut *mut c_void, isize);
29
30impl Drop for HintIter {
31 fn drop(&mut self) { unsafe { alsa::snd_device_name_free_hint(self.0); }}
32}
33
34impl HintIter {
35 pub fn new(card: Option<&Card>, iface: &CStr) -> Result<HintIter> {
37 let mut p = ptr::null_mut();
38 let cnr = card.map(|c| c.get_index()).unwrap_or(-1) as c_int;
39 acheck!(snd_device_name_hint(cnr, iface.as_ptr(), &mut p))
40 .map(|_| HintIter(p, 0))
41 }
42
43 pub fn new_str(card: Option<&Card>, iface: &str) -> Result<HintIter> {
45 HintIter::new(card, &CString::new(iface).unwrap())
46 }
47}
48
49impl Iterator for HintIter {
50 type Item = Hint;
51 fn next(&mut self) -> Option<Hint> {
52 if self.0.is_null() { return None; }
53 let p = unsafe { *self.0.offset(self.1) };
54 if p.is_null() { return None; }
55 self.1 += 1;
56 Some(Hint::new(p))
57 }
58}
59
60
61#[derive(Debug, Clone)]
63pub struct Hint {
64 pub name: Option<String>,
65 pub desc: Option<String>,
66 pub direction: Option<Direction>,
67}
68
69impl Hint {
70 fn get_str(p: *const c_void, name: &str) -> Option<String> {
71 let name = CString::new(name).unwrap();
72 let c = unsafe { alsa::snd_device_name_get_hint(p, name.as_ptr()) };
73 from_alloc("snd_device_name_get_hint", c).ok()
74 }
75
76 fn new(p: *const c_void) -> Hint {
77 let d = Hint::get_str(p, "IOID").and_then(|x| match &*x {
78 "Input" => Some(Direction::Capture),
79 "Output" => Some(Direction::Playback),
80 _ => None,
81 });
82 Hint { name: Hint::get_str(p, "NAME"), desc: Hint::get_str(p, "DESC"), direction: d }
83 }
84}
85
86#[test]
87fn print_hints() {
88 extern crate std;
89 for t in &["pcm", "ctl", "rawmidi", "timer", "seq", "hwdep"] {
90 std::println!("{} devices:", t);
91 let i = HintIter::new(None, &*CString::new(*t).unwrap()).unwrap();
92 for a in i { std::println!(" {:?}", a) }
93 }
94}