Skip to content

Commit 06d43d7

Browse files
committed
Add two more config functions
1 parent e560e9d commit 06d43d7

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/config.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,43 @@
11
//! Configuration file API
22
//!
3-
//! For now, just contains two functions regarding the global configuration
3+
//! For now, just contains functions regarding the global configuration
44
//! stored as a cache inside alsa-lib. Calling `update_free_global` might help
55
//! against valgrind reporting memory leaks.
66
use crate::{alsa};
77
use super::error::*;
8+
use super::Output;
9+
use std::ptr;
810

9-
pub fn update() -> Result<()> {
10-
acheck!(snd_config_update()).map(|_| ())
11+
pub fn update() -> Result<bool> {
12+
acheck!(snd_config_update()).map(|x| x != 0)
1113
}
1214

1315
pub fn update_free_global() -> Result<()> {
1416
acheck!(snd_config_update_free_global()).map(|_| ())
17+
}
18+
19+
/// [snd_config_t](https://alsa-project.org/alsa-doc/alsa-lib/group___config.html) wrapper
20+
pub struct Config(*mut alsa::snd_config_t);
21+
22+
impl Drop for Config {
23+
fn drop(&mut self) { unsafe { alsa::snd_config_unref(self.0) }; }
24+
}
25+
26+
pub fn update_ref() -> Result<Config> {
27+
let mut top = ptr::null_mut();
28+
acheck!(snd_config_update_ref(&mut top)).map(|_| Config(top))
29+
}
30+
31+
impl Config {
32+
pub fn save(&self, o: &mut Output) -> Result<()> {
33+
acheck!(snd_config_save(self.0, super::io::output_handle(o))).map(|_| ())
34+
}
35+
}
36+
37+
#[test]
38+
fn config_save() {
39+
let c = update_ref().unwrap();
40+
let mut outp = Output::buffer_open().unwrap();
41+
c.save(&mut outp).unwrap();
42+
println!("== Config save ==\n{}", outp);
1543
}

src/io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ impl Output {
2222
let b = unsafe {
2323
let mut q = ptr::null_mut();
2424
let s = alsa::snd_output_buffer_string(self.0, &mut q);
25-
slice::from_raw_parts(q as *const u8, s as usize)
25+
if s == 0 { &[] } else { slice::from_raw_parts(q as *const u8, s as usize) }
2626
};
2727
f(b)
2828
}

0 commit comments

Comments
 (0)