Skip to content

Commit

Permalink
Add two more config functions
Browse files Browse the repository at this point in the history
  • Loading branch information
diwic committed Nov 24, 2024
1 parent e560e9d commit 06d43d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
34 changes: 31 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
//! Configuration file API
//!
//! For now, just contains two functions regarding the global configuration
//! For now, just contains functions regarding the global configuration
//! stored as a cache inside alsa-lib. Calling `update_free_global` might help
//! against valgrind reporting memory leaks.
use crate::{alsa};
use super::error::*;
use super::Output;
use std::ptr;

pub fn update() -> Result<()> {
acheck!(snd_config_update()).map(|_| ())
pub fn update() -> Result<bool> {
acheck!(snd_config_update()).map(|x| x != 0)
}

pub fn update_free_global() -> Result<()> {
acheck!(snd_config_update_free_global()).map(|_| ())
}

/// [snd_config_t](https://alsa-project.org/alsa-doc/alsa-lib/group___config.html) wrapper
pub struct Config(*mut alsa::snd_config_t);

impl Drop for Config {
fn drop(&mut self) { unsafe { alsa::snd_config_unref(self.0) }; }
}

pub fn update_ref() -> Result<Config> {
let mut top = ptr::null_mut();
acheck!(snd_config_update_ref(&mut top)).map(|_| Config(top))
}

impl Config {
pub fn save(&self, o: &mut Output) -> Result<()> {
acheck!(snd_config_save(self.0, super::io::output_handle(o))).map(|_| ())
}
}

#[test]
fn config_save() {
let c = update_ref().unwrap();
let mut outp = Output::buffer_open().unwrap();
c.save(&mut outp).unwrap();
println!("== Config save ==\n{}", outp);
}
2 changes: 1 addition & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Output {
let b = unsafe {
let mut q = ptr::null_mut();
let s = alsa::snd_output_buffer_string(self.0, &mut q);
slice::from_raw_parts(q as *const u8, s as usize)
if s == 0 { &[] } else { slice::from_raw_parts(q as *const u8, s as usize) }
};
f(b)
}
Expand Down

0 comments on commit 06d43d7

Please sign in to comment.