diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/alsa/src/card.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/alsa/src/card.rs')
-rw-r--r-- | third_party/rust/alsa/src/card.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/third_party/rust/alsa/src/card.rs b/third_party/rust/alsa/src/card.rs new file mode 100644 index 0000000000..93345f4bb7 --- /dev/null +++ b/third_party/rust/alsa/src/card.rs @@ -0,0 +1,54 @@ +//! Sound card enumeration +use libc::{c_int, c_char}; +use super::error::*; +use crate::alsa; +use std::ffi::CStr; + +/// An ALSA sound card, uniquely identified by its index. +#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Card(c_int); + +/// Iterate over existing sound cards. +pub struct Iter(c_int); + +impl Iter { + pub fn new() -> Iter { Iter(-1) } +} + +impl Iterator for Iter { + type Item = Result<Card>; + + fn next(&mut self) -> Option<Result<Card>> { + match acheck!(snd_card_next(&mut self.0)) { + Ok(_) if self.0 == -1 => None, + Ok(_) => Some(Ok(Card(self.0))), + Err(e) => Some(Err(e)), + } + } +} + +impl Card { + pub fn new(index: c_int) -> Card { Card(index) } + pub fn from_str(s: &CStr) -> Result<Card> { + acheck!(snd_card_get_index(s.as_ptr())).map(Card) + } + pub fn get_name(&self) -> Result<String> { + let mut c: *mut c_char = ::std::ptr::null_mut(); + acheck!(snd_card_get_name(self.0, &mut c)) + .and_then(|_| from_alloc("snd_card_get_name", c)) + } + pub fn get_longname(&self) -> Result<String> { + let mut c: *mut c_char = ::std::ptr::null_mut(); + acheck!(snd_card_get_longname(self.0, &mut c)) + .and_then(|_| from_alloc("snd_card_get_longname", c)) + } + + pub fn get_index(&self) -> c_int { self.0 } +} + +#[test] +fn print_cards() { + for a in Iter::new().map(|a| a.unwrap()) { + println!("Card #{}: {} ({})", a.get_index(), a.get_name().unwrap(), a.get_longname().unwrap()) + } +} |