1//! Sound card enumeration
2use libc::{c_int, c_char};
3use super::error::*;
4use crate::alsa;
5use std::ffi::CStr;
6
7/// An ALSA sound card, uniquely identified by its index.
8#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
9pub struct Card(c_int);
10
11/// Iterate over existing sound cards.
12pub struct Iter(c_int);
13
14impl Iter {
15 pub fn new() -> Iter { Iter(-1) }
16}
17
18impl Iterator for Iter {
19 type Item = Result<Card>;
20
21 fn next(&mut self) -> Option<Result<Card>> {
22 match acheck!(snd_card_next(&mut self.0)) {
23 Ok(_) if self.0 == -1 => None,
24 Ok(_) => Some(Ok(Card(self.0))),
25 Err(e: Error) => Some(Err(e)),
26 }
27 }
28}
29
30impl Card {
31 pub fn new(index: c_int) -> Card { Card(index) }
32 pub fn from_str(s: &CStr) -> Result<Card> {
33 acheck!(snd_card_get_index(s.as_ptr())).map(op:Card)
34 }
35 pub fn get_name(&self) -> Result<String> {
36 let mut c: *mut c_char = ::std::ptr::null_mut();
37 acheck!(snd_card_get_name(self.0, &mut c))
38 .and_then(|_| from_alloc(func:"snd_card_get_name", s:c))
39 }
40 pub fn get_longname(&self) -> Result<String> {
41 let mut c: *mut c_char = ::std::ptr::null_mut();
42 acheck!(snd_card_get_longname(self.0, &mut c))
43 .and_then(|_| from_alloc(func:"snd_card_get_longname", s:c))
44 }
45
46 pub fn get_index(&self) -> c_int { self.0 }
47}
48
49#[test]
50fn print_cards() {
51 for a: Card in Iter::new().map(|a: Result| a.unwrap()) {
52 println!("Card #{}: {} ({})", a.get_index(), a.get_name().unwrap(), a.get_longname().unwrap())
53 }
54}
55