Get sound devices.
This commit is contained in:
45
src/client.rs
Normal file
45
src/client.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
65
src/main.rs
65
src/main.rs
@@ -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(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user