Skip to content

Commit e18fc31

Browse files
committed
main: if no device is specified, connect to the first one
Connect to the first device, because it's probably not the same as the one on my system. Signed-off-by: Sean Cross <[email protected]>
1 parent 800522b commit e18fc31

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

src/main.rs

+31-17
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ const KEY_DELAY_MS: u64 = 40;
2222
/// The amount of time required for system events, such as Esc
2323
const SYS_DELAY_MS: u64 = 400;
2424

25-
/// The name of the midi device we'll look to
26-
const MIDI_DEV_NAME: &str = "Launchkey Mini";
27-
2825
#[derive(Debug, PartialEq)]
2926
enum MidiEvent {
3027
NoteOn,
@@ -102,8 +99,10 @@ fn main() {
10299
return;
103100
}
104101

105-
let device_name = matches.value_of("device").unwrap_or(MIDI_DEV_NAME);
106-
println!("Attempting to connect to device {}", device_name);
102+
let device_name = match matches.value_of("device") {
103+
Some(s) => Some(s.to_owned()),
104+
None => None,
105+
};
107106
run(device_name).unwrap();
108107
}
109108

@@ -200,8 +199,8 @@ fn midi_callback(_timestamp_us: u64, raw_message: &[u8], keygen: &mut enigo::Eni
200199
println!("Unhandled message for data: {}", s);
201200
}
202201

203-
fn run(midi_name: &str) -> Result<(), Box<Error>> {
204-
let target_device_name = midi_name.to_owned();
202+
fn run(midi_name: Option<String>) -> Result<(), Box<Error>> {
203+
let mut target_device_name = midi_name.to_owned();
205204

206205
let mut device_idx: Option<usize> = None;
207206

@@ -211,34 +210,49 @@ fn run(midi_name: &str) -> Result<(), Box<Error>> {
211210
let mut midi_in = MidiInput::new("keyboard-tweak")?;
212211
midi_in.ignore(Ignore::None);
213212

213+
// If the index of the device has changed, reset the connection
214214
if let Some(idx) = device_idx {
215215
match midi_in.port_name(idx) {
216216
Err(_) => {
217217
device_idx = None;
218218
connection = None;
219219
}
220-
Ok(val) => if &val != &target_device_name {
221-
device_idx = None;
222-
connection = None;
220+
Ok(val) => {
221+
if let Some(ref name) = target_device_name {
222+
if &val != name {
223+
device_idx = None;
224+
connection = None;
225+
}
226+
}
223227
},
224228
}
225229
} else {
226230
device_idx = None;
227231
connection = None;
228232
};
229233

234+
// If there is no connection, try to create a new one.
230235
if connection.is_none() {
231-
println!(
232-
"Attempting to connect to MIDI device \"{}\". Detected devices:",
233-
target_device_name
234-
);
236+
match target_device_name {
237+
None => println!("Connecting to first available device"),
238+
Some(ref s) => println!("Looking for device {}", s),
239+
}
240+
235241
for i in 0..midi_in.port_count() {
236242
match midi_in.port_name(i) {
237243
Err(_) => (),
238244
Ok(name) => {
239-
if &name == &target_device_name {
240-
println!("Using device:{}", i);
241-
device_idx = Some(i);
245+
match target_device_name {
246+
Some(ref s) =>
247+
if &name == s {
248+
println!("Using device: {}", i);
249+
device_idx = Some(i);
250+
},
251+
None => {
252+
println!("Using device: {}", i);
253+
device_idx = Some(i);
254+
target_device_name = Some(name);
255+
},
242256
}
243257
}
244258
}

0 commit comments

Comments
 (0)