@@ -22,9 +22,6 @@ const KEY_DELAY_MS: u64 = 40;
22
22
/// The amount of time required for system events, such as Esc
23
23
const SYS_DELAY_MS : u64 = 400 ;
24
24
25
- /// The name of the midi device we'll look to
26
- const MIDI_DEV_NAME : & str = "Launchkey Mini" ;
27
-
28
25
#[ derive( Debug , PartialEq ) ]
29
26
enum MidiEvent {
30
27
NoteOn ,
@@ -102,8 +99,10 @@ fn main() {
102
99
return ;
103
100
}
104
101
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
+ } ;
107
106
run ( device_name) . unwrap ( ) ;
108
107
}
109
108
@@ -200,8 +199,8 @@ fn midi_callback(_timestamp_us: u64, raw_message: &[u8], keygen: &mut enigo::Eni
200
199
println ! ( "Unhandled message for data: {}" , s) ;
201
200
}
202
201
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 ( ) ;
205
204
206
205
let mut device_idx: Option < usize > = None ;
207
206
@@ -211,34 +210,49 @@ fn run(midi_name: &str) -> Result<(), Box<Error>> {
211
210
let mut midi_in = MidiInput :: new ( "keyboard-tweak" ) ?;
212
211
midi_in. ignore ( Ignore :: None ) ;
213
212
213
+ // If the index of the device has changed, reset the connection
214
214
if let Some ( idx) = device_idx {
215
215
match midi_in. port_name ( idx) {
216
216
Err ( _) => {
217
217
device_idx = None ;
218
218
connection = None ;
219
219
}
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
+ }
223
227
} ,
224
228
}
225
229
} else {
226
230
device_idx = None ;
227
231
connection = None ;
228
232
} ;
229
233
234
+ // If there is no connection, try to create a new one.
230
235
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
+
235
241
for i in 0 ..midi_in. port_count ( ) {
236
242
match midi_in. port_name ( i) {
237
243
Err ( _) => ( ) ,
238
244
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
+ } ,
242
256
}
243
257
}
244
258
}
0 commit comments