Get sound devices.
Some checks failed
build Build build has failed
format Build format has succeeded
rust-clippy Build rust-clippy has failed
rust-test Build rust-test has failed

This commit is contained in:
Tom Alexander
2024-05-12 01:15:52 -04:00
parent 76c52b5737
commit d063a1bd4f
4 changed files with 826 additions and 0 deletions

45
src/client.rs Normal file
View File

@@ -0,0 +1,45 @@
use std::collections::HashMap;
#[derive(Default)]
pub(crate) struct State {
nodes: HashMap<u32, Node>,
links: HashMap<u32, Link>,
}
#[derive(Debug)]
struct Node {
id: u32,
name: Option<String>,
}
#[derive(Debug)]
struct Link {
id: u32,
output: u32,
input: u32,
}
impl State {
pub(crate) fn add_node<N: Into<String>>(&mut self, id: u32, name: Option<N>) {
let name = name.map(Into::into);
println!("Node Add {} {:?}", id, name);
let node = Node { id, name };
self.nodes.insert(id, node);
}
pub(crate) fn add_link(&mut self, id: u32, input: u32, output: u32) {
println!("Link Add {:?} {:?} {:?}", id, input, output);
let link = Link { id, input, output };
self.links.insert(id, link);
}
pub(crate) fn remove_id(&mut self, id: u32) {
if let Some(node) = self.nodes.remove(&id) {
println!("Node Remove {} {:?}", id, node);
} else if let Some(link) = self.links.remove(&id) {
println!("Link Remove {} {:?}", id, link);
} else {
eprintln!("Unrecognized remove: {}", id);
}
}
}

View File

@@ -1,3 +1,68 @@
use std::cell::RefCell;
use std::rc::Rc;
use pipewire::keys;
use pipewire::properties::properties;
use pipewire::{context::Context, main_loop::MainLoop};
use self::client::State;
mod client;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let proplist = properties! {*keys::APP_NAME => env!("CARGO_PKG_NAME")};
let mainloop = MainLoop::new(None)?;
let context = Context::with_properties(&mainloop, proplist)?;
let core = context.connect(None)?;
let registry = core.get_registry()?;
let state = Rc::new(RefCell::new(State::default()));
let state2 = state.clone();
let _listener = registry
.add_listener_local()
.global(move |global| {
let mut state = state.borrow_mut();
match &global.type_ {
pipewire::types::ObjectType::Node => {
if let Some(global_props) = global.props {
let node_id = global.id;
let name = global_props.get(&keys::NODE_NAME);
state.add_node(node_id, name);
} else {
eprintln!("Global Node Add without props {:?}", global);
}
}
pipewire::types::ObjectType::Link => {
if let Some(global_props) = global.props {
let link_id = global.id;
let input = global_props
.get(&keys::LINK_INPUT_NODE)
.map(|x| x.parse())
.map_or(Ok(None), |v| v.map(Some))
.unwrap()
.unwrap();
let output = global_props
.get(&keys::LINK_OUTPUT_NODE)
.map(|x| x.parse())
.map_or(Ok(None), |v| v.map(Some))
.unwrap()
.unwrap();
state.add_link(link_id, input, output);
} else {
eprintln!("Global Link Add without props {:?}", global);
}
}
_ => {}
};
})
.global_remove(move |id| {
let mut state = state2.borrow_mut();
state.remove_id(id);
})
.register();
mainloop.run();
Ok(())
}