summaryrefslogtreecommitdiffstats
path: root/third_party/rust/midir/examples/test_read_input.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/midir/examples/test_read_input.rs
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/midir/examples/test_read_input.rs')
-rw-r--r--third_party/rust/midir/examples/test_read_input.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/third_party/rust/midir/examples/test_read_input.rs b/third_party/rust/midir/examples/test_read_input.rs
new file mode 100644
index 0000000000..321ccc7d15
--- /dev/null
+++ b/third_party/rust/midir/examples/test_read_input.rs
@@ -0,0 +1,58 @@
+extern crate midir;
+
+use std::io::{stdin, stdout, Write};
+use std::error::Error;
+
+use midir::{MidiInput, Ignore};
+
+fn main() {
+ match run() {
+ Ok(_) => (),
+ Err(err) => println!("Error: {}", err)
+ }
+}
+
+fn run() -> Result<(), Box<dyn Error>> {
+ let mut input = String::new();
+
+ let mut midi_in = MidiInput::new("midir reading input")?;
+ midi_in.ignore(Ignore::None);
+
+ // Get an input port (read from console if multiple are available)
+ let in_ports = midi_in.ports();
+ let in_port = match in_ports.len() {
+ 0 => return Err("no input port found".into()),
+ 1 => {
+ println!("Choosing the only available input port: {}", midi_in.port_name(&in_ports[0]).unwrap());
+ &in_ports[0]
+ },
+ _ => {
+ println!("\nAvailable input ports:");
+ for (i, p) in in_ports.iter().enumerate() {
+ println!("{}: {}", i, midi_in.port_name(p).unwrap());
+ }
+ print!("Please select input port: ");
+ stdout().flush()?;
+ let mut input = String::new();
+ stdin().read_line(&mut input)?;
+ in_ports.get(input.trim().parse::<usize>()?)
+ .ok_or("invalid input port selected")?
+ }
+ };
+
+ println!("\nOpening connection");
+ let in_port_name = midi_in.port_name(in_port)?;
+
+ // _conn_in needs to be a named parameter, because it needs to be kept alive until the end of the scope
+ let _conn_in = midi_in.connect(in_port, "midir-read-input", move |stamp, message, _| {
+ println!("{}: {:?} (len = {})", stamp, message, message.len());
+ }, ())?;
+
+ println!("Connection open, reading input from '{}' (press enter to exit) ...", in_port_name);
+
+ input.clear();
+ stdin().read_line(&mut input)?; // wait for next enter key press
+
+ println!("Closing connection");
+ Ok(())
+}