2018-07-10 21:41:58 +00:00
|
|
|
//! Footswitch-RS
|
|
|
|
//!
|
|
|
|
//! `footswitch-rs` enables you to use footswitches of <xxx>
|
|
|
|
|
|
|
|
mod key_operations;
|
2018-07-18 12:26:11 +00:00
|
|
|
mod pedal_operations;
|
2018-07-10 21:41:58 +00:00
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate structopt;
|
|
|
|
extern crate hidapi;
|
|
|
|
extern crate users;
|
|
|
|
|
|
|
|
|
|
|
|
use std::path::PathBuf;
|
2018-07-11 10:02:10 +00:00
|
|
|
use std::process;
|
2018-07-10 21:41:58 +00:00
|
|
|
use structopt::StructOpt;
|
|
|
|
|
|
|
|
#[derive(StructOpt, Debug)]
|
|
|
|
#[structopt(name = "rust-footswitch")]
|
|
|
|
struct Opt {
|
|
|
|
/// Read all pedals
|
|
|
|
#[structopt(short = "r", long = "read")]
|
|
|
|
read: bool,
|
|
|
|
|
|
|
|
/// Prints a table of all keys with <listkeys> rows
|
|
|
|
#[structopt(short = "l", long = "listkeys")]
|
2018-07-11 10:02:10 +00:00
|
|
|
listkeys: Option<usize>,
|
2018-07-10 21:41:58 +00:00
|
|
|
|
|
|
|
/// Select pedal (left: 1, middle: 2, right: 3)
|
|
|
|
#[structopt(short = "p", long = "pedal", parse(from_os_str))]
|
|
|
|
output: Option<PathBuf>,
|
|
|
|
}
|
|
|
|
|
2018-07-10 21:36:20 +00:00
|
|
|
fn main() {
|
2018-07-18 12:26:11 +00:00
|
|
|
let pedals = pedal_operations::Pedals::new();
|
2018-07-10 21:41:58 +00:00
|
|
|
check_sudo();
|
|
|
|
|
|
|
|
let opt = Opt::from_args();
|
|
|
|
|
2018-07-11 10:02:10 +00:00
|
|
|
// All options that don't need the device to be open
|
|
|
|
// Print all keys and exit application
|
|
|
|
if let Some(x) = opt.listkeys {
|
|
|
|
key_operations::print_key_map(x);
|
|
|
|
process::exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open device
|
2018-07-23 22:16:28 +00:00
|
|
|
// This is the reason, the device is not part of the struct: https://github.com/Osspial/hidapi-rs/issues/16
|
|
|
|
// Maybe this can be fixed, as soon as this is merged into the crate: https://github.com/Osspial/hidapi-rs/pull/12
|
2018-07-10 21:41:58 +00:00
|
|
|
let vld_dev = [
|
|
|
|
(0x0c45u16, 0x7403u16),
|
|
|
|
(0x0c45 , 0x7404),
|
|
|
|
(0x413d , 0x2107)
|
|
|
|
];
|
|
|
|
|
2018-07-11 10:02:10 +00:00
|
|
|
let api = hidapi::HidApi::new().expect("Hidapi init failed!");
|
|
|
|
let mut dev_path = String::new();
|
|
|
|
|
|
|
|
for device in &api.devices() {
|
|
|
|
//println!("{}:{}, {}, {}", device.vendor_id, device.product_id, device.interface_number, device.path);
|
|
|
|
for val in vld_dev.iter() {
|
|
|
|
if *val == (device.vendor_id, device.product_id) && device.interface_number == 1 {
|
|
|
|
println!("Found device {:x}:{:x} ({})", device.vendor_id, device.product_id, device.path);
|
|
|
|
dev_path = device.path.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-10 21:41:58 +00:00
|
|
|
|
2018-07-11 10:02:10 +00:00
|
|
|
let dev = api.open_path(dev_path.as_str()).unwrap();
|
|
|
|
println!("Succesfully opened device.");
|
2018-07-10 21:41:58 +00:00
|
|
|
|
2018-07-11 10:02:10 +00:00
|
|
|
// All options that need the device to be open
|
|
|
|
if opt.read {
|
2018-07-23 21:52:10 +00:00
|
|
|
pedals.read_pedals(& dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
//ToDo: set right if condition
|
|
|
|
if true {
|
|
|
|
pedals.write_pedals(& dev);
|
2018-07-10 21:41:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if user is super user
|
|
|
|
fn check_sudo() {
|
|
|
|
if users::get_current_uid() != 0 {
|
|
|
|
panic!("Please execute this application as super user!");
|
|
|
|
}
|
|
|
|
}
|