Skip to content

Commit 34d7445

Browse files
committed
main: add argument parsing
Add the ability to process arguments, and configure the MIDI device to look for at runtime. Signed-off-by: Sean Cross <[email protected]>
1 parent 8f52173 commit 34d7445

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/main.rs

+33-12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
extern crate clap;
12
extern crate enigo;
23
extern crate midir;
34

@@ -6,6 +7,8 @@ use std::time::Duration;
67
use std::thread;
78
use std::fmt::Write;
89

10+
use clap::{App, Arg};
11+
912
use enigo::KeyboardControllable;
1013

1114
use midir::{Ignore, MidiInput, MidiInputConnection};
@@ -69,8 +72,31 @@ fn parse_message(message: &[u8]) -> Result<MidiMessage, MidiError> {
6972
}
7073

7174
fn main() {
72-
list_devices().unwrap();
73-
run(MIDI_DEV_NAME).unwrap();
75+
let matches = App::new("Midi Perform")
76+
.version("0.2.0")
77+
.author("Sean Cross <[email protected]>")
78+
.about("Accepts MIDI controller data and simulates keyboard presses")
79+
.arg(
80+
Arg::with_name("list")
81+
.short("l")
82+
.help("List available devices"),
83+
)
84+
.arg(
85+
Arg::with_name("device")
86+
.short("d")
87+
.help("Connect to specified device")
88+
.value_name("DEVICE"),
89+
)
90+
.get_matches();
91+
92+
if matches.is_present("list") {
93+
list_devices().expect("unable to list MIDI devices");
94+
return;
95+
}
96+
97+
let device_name = matches.value_of("DEVICE").unwrap_or(MIDI_DEV_NAME);
98+
println!("Attempting to connect to device {}", device_name);
99+
run(device_name).unwrap();
74100
}
75101

76102
fn midi_callback(_timestamp_us: u64, raw_message: &[u8], keygen: &mut enigo::Enigo) {
@@ -171,7 +197,6 @@ fn run(midi_name: &str) -> Result<(), Box<Error>> {
171197

172198
let mut device_idx: Option<usize> = None;
173199

174-
println!("Attempting to connect to {}", target_device_name);
175200
let mut connection: Option<MidiInputConnection<()>> = None;
176201

177202
loop {
@@ -195,10 +220,7 @@ fn run(midi_name: &str) -> Result<(), Box<Error>> {
195220
};
196221

197222
if connection.is_none() {
198-
println!(
199-
"Recreating midi connection. Looking for {}...",
200-
target_device_name
201-
);
223+
println!("Connecting to MIDI. Detected devices:");
202224
for i in 0..midi_in.port_count() {
203225
match midi_in.port_name(i) {
204226
Err(_) => (),
@@ -209,7 +231,7 @@ fn run(midi_name: &str) -> Result<(), Box<Error>> {
209231
}
210232
}
211233
}
212-
println!("{}: {}", i, midi_in.port_name(i)?);
234+
println!(" {}", midi_in.port_name(i)?);
213235
}
214236
}
215237

@@ -226,13 +248,12 @@ fn run(midi_name: &str) -> Result<(), Box<Error>> {
226248
) {
227249
Err(reason) => println!("Unable to connect to device: {:?}", reason),
228250
Ok(conn) => {
251+
println!("Connection established");
229252
connection = Some(conn);
230253
}
231254
}
232255
}
233256
}
234-
/*
235-
*/
236257
thread::sleep(Duration::from_secs(1));
237258
}
238259
}
@@ -241,9 +262,9 @@ fn list_devices() -> Result<(), Box<Error>> {
241262
let mut midi_in = MidiInput::new("keyboard-tweak")?;
242263
midi_in.ignore(Ignore::None);
243264

244-
println!("Available input ports:");
265+
println!("Available MIDI devices:");
245266
for i in 0..midi_in.port_count() {
246-
println!("{}: {}", i, midi_in.port_name(i)?);
267+
println!(" {}", midi_in.port_name(i)?);
247268
}
248269

249270
Ok(())

0 commit comments

Comments
 (0)