Debian Codegraph @

third_party/rust/coremidi/examples/receive.rs

raw
1 extern crate coremidi;
2
3 use std::env;
4
5 fn main() {
6 let source_index = get_source_index();
7 println!("Source index: {}", source_index);
8
9 let source = coremidi::Source::from_index(source_index).unwrap();
10 println!("Source display name: {}", source.display_name().unwrap());
11
12 let client = coremidi::Client::new("Example Client").unwrap();
13
14 let callback = |packet_list: &coremidi::PacketList| {
15 println!("{}", packet_list);
16 };
17
18 let input_port = client.input_port("Example Port", callback).unwrap();
19 input_port.connect_source(&source).unwrap();
20
21 let mut input_line = String::new();
22 println!("Press Enter to Finish");
23 std::io::stdin()
24 .read_line(&mut input_line)
25 .expect("Failed to read line");
26
27 input_port.disconnect_source(&source).unwrap();
28 }
29
30 fn get_source_index() -> usize {
31 let mut args_iter = env::args();
32 let tool_name = args_iter
33 .next()
34 .and_then(|path| {
35 path.split(std::path::MAIN_SEPARATOR)
36 .last()
37 .map(|v| v.to_string())
38 })
39 .unwrap_or_else(|| "receive".to_string());
40
41 match args_iter.next() {
42 Some(arg) => match arg.parse::<usize>() {
43 Ok(index) => {
44 if index >= coremidi::Sources::count() {
45 println!("Source index out of range: {}", index);
46 std::process::exit(-1);
47 }
48 index
49 }
50 Err(_) => {
51 println!("Wrong source index: {}", arg);
52 std::process::exit(-1);
53 }
54 },
55 None => {
56 println!("Usage: {} <source-index>", tool_name);
57 println!();
58 println!("Available Sources:");
59 print_sources();
60 std::process::exit(-1);
61 }
62 }
63 }
64
65 fn print_sources() {
66 for (i, source) in coremidi::Sources.into_iter().enumerate() {
67 if let Some(display_name) = source.display_name() {
68 println!("[{}] {}", i, display_name)
69 }
70 }
71 }