Finished do-not-purge feature. Closes #3

This feature is not yet tested on the Append:: functions since not all
functions are yet implemented. As soon as they get implemented, this
feature will be verified on them as well.

Furthermore, a Clear function is added.

Finally, in this commit, mouse_left, mouse_right, mouse_middle, and mouse_double got
abbreviated to left, right, middle, and double.
This commit is contained in:
Dennis Potter 2018-11-23 18:48:39 +01:00
parent 43f63c002b
commit 44c542d99a
3 changed files with 109 additions and 62 deletions

View File

@ -314,10 +314,10 @@ pub enum MouseButton {
impl MouseButton { impl MouseButton {
pub fn str_to_enum(mousebutton:&str) -> Option<MouseButton> { pub fn str_to_enum(mousebutton:&str) -> Option<MouseButton> {
match mousebutton { match mousebutton {
"mouse_left" => Some(MouseButton::MouseLeft), "left" => Some(MouseButton::MouseLeft),
"mouse_right" => Some(MouseButton::MouseRight), "right" => Some(MouseButton::MouseRight),
"mouse_middle" => Some(MouseButton::MouseMiddle), "middle" => Some(MouseButton::MouseMiddle),
"mouse_double" => Some(MouseButton::MouseDouble), "double" => Some(MouseButton::MouseDouble),
_ => None, _ => None,
} }
} }
@ -334,10 +334,10 @@ impl MouseButton {
pub fn enum_to_string(mousebutton:MouseButton) -> String { pub fn enum_to_string(mousebutton:MouseButton) -> String {
match mousebutton { match mousebutton {
MouseButton::MouseLeft => "mouse_left".to_string(), MouseButton::MouseLeft => "left".to_string(),
MouseButton::MouseRight => "mouse_right".to_string(), MouseButton::MouseRight => "right".to_string(),
MouseButton::MouseMiddle => "mouse_middle".to_string(), MouseButton::MouseMiddle => "middle".to_string(),
MouseButton::MouseDouble => "mouse_double".to_string(), MouseButton::MouseDouble => "double".to_string(),
} }
} }
} }

View File

@ -83,7 +83,7 @@ enum Set {
input: Vec<String>, input: Vec<String>,
}, },
/// Set a mousebutton to one or more pedals /// Set a mousebutton (left/right/middle/double) to one or more pedals
#[structopt(name = "mousebutton")] #[structopt(name = "mousebutton")]
SetMousebutton { SetMousebutton {
/// Specify pedal(s) to modify: [0 | 1 | 2] /// Specify pedal(s) to modify: [0 | 1 | 2]
@ -142,7 +142,7 @@ enum Append {
input: Vec<String>, input: Vec<String>,
}, },
/// Append a modifier to one or more pedals /// Append a modifier (ctrl/shift/alt/win) to one or more pedals
#[structopt(name = "modifier")] #[structopt(name = "modifier")]
AppendModifier { AppendModifier {
/// Specify pedal(s) to modify: [0 | 1 | 2] /// Specify pedal(s) to modify: [0 | 1 | 2]
@ -173,8 +173,54 @@ fn main() {
let mut pedals = pedal_operations::Pedals::new(); let mut pedals = pedal_operations::Pedals::new();
// Make sure that the application does not purge pedals that are not explicitly set
// by refreshing pedals that are not in use.
//
// When Command::Clear is set, refresh only the pedals that are NOT defined.
let mut unused_pedals: Vec<u8> = Vec::new();
match &opt.cmd {
Some(Command::Set { cmd }) => {
match cmd {
Set::SetKey { pedal, .. } |
Set::SetMousebutton { pedal, .. } |
Set::SetMousemovement { pedal, .. } => {
for number in 0..3 {
if !pedal.contains(&(number as u8)) {
unused_pedals.push(number);
}
}
}
}
},
Some(Command::Append { cmd }) => {
match cmd {
Append::AppendKey { pedal, .. } |
Append::AppendString { pedal, .. } |
Append::AppendModifier { pedal, .. } => {
for number in 0..3 {
if !pedal.contains(&(number as u8)) {
unused_pedals.push(number);
}
}
}
}
},
Some(Command::Clear { pedal }) => {
for number in 0..3 {
if !pedal.contains(&(number as u8)) {
unused_pedals.push(number);
}
}
}
_ => { /* Do nothing, statement below will cover this */ }
}
pedals.refresh_values(unused_pedals);
// All options that need the device to be open // All options that need the device to be open
match opt.cmd { match opt.cmd {
Some(Command::Append { cmd }) => { Some(Command::Append { cmd }) => {
match cmd { match cmd {
Append::AppendKey { pedal, input } => Append::AppendKey { pedal, input } =>
@ -208,16 +254,6 @@ fn main() {
{ {
check_length(&pedal, &input); check_length(&pedal, &input);
let mut unused_pedals: Vec<u8> = Vec::new();
for number in 0..3 {
if !pedal.contains(&(number as u8)) {
unused_pedals.push(number);
}
}
pedals.refresh_values(unused_pedals);
for (i, pedal) in pedal.iter().enumerate() { for (i, pedal) in pedal.iter().enumerate() {
pedals.set_key(*pedal as usize, input[i].as_str()); pedals.set_key(*pedal as usize, input[i].as_str());
} }
@ -235,7 +271,7 @@ fn main() {
{ {
if pedal.len() != x.len() || x.len() != y.len() || y.len() != w.len() { if pedal.len() != x.len() || x.len() != y.len() || y.len() != w.len() {
error!("You must define X, Y, and W for every pedal. If a direction is not needed, set it to 0!"); error!("You must define x, y, and w for every pedal. If a direction is not needed, set it to 0!");
} }
for (i, pedal) in pedal.iter().enumerate() { for (i, pedal) in pedal.iter().enumerate() {
@ -249,8 +285,8 @@ fn main() {
pedals.update_and_close(); pedals.update_and_close();
}, },
Some(Command::Clear { pedal }) => { Some(Command::Clear { .. }) => {
pedals.update_and_close();
}, },
Some(Command::Read {all: all_var, pedals: ped_list}) => { Some(Command::Read {all: all_var, pedals: ped_list}) => {

View File

@ -278,7 +278,7 @@ impl Pedals {
pub fn set_mousebutton(& mut self, ped:usize, mousebutton:&str) { pub fn set_mousebutton(& mut self, ped:usize, mousebutton:&str) {
let mousebutton = match key_operations::MouseButton::str_to_enum(mousebutton) { let mousebutton = match key_operations::MouseButton::str_to_enum(mousebutton) {
Some(x) => x, Some(x) => x,
None => error!("Unknown mousebutton! Please use one of the following: mouse_left, mouse_middle, mouse_right, mouse_double."), None => error!("Unknown mousebutton! Please use one of the following: left, middle, right, double."),
}; };
self.set_type(ped, Type::Mouse); self.set_type(ped, Type::Mouse);
@ -379,12 +379,11 @@ impl Pedals {
goodbye(); goodbye();
} }
/// Prevent the application from purging pedals that are not /// Prevent the application from purging pedals that are not explicitly set
/// explicitly set
pub fn refresh_values(& mut self, peds: Vec<u8>) { pub fn refresh_values(& mut self, peds: Vec<u8>) {
// First read from pedals that are defined in peds // First read from pedals that are defined in peds
for (i, ped) in peds.iter().enumerate() { for ped in peds.iter() {
// Read value from pedal and directly translate it to a key // Read value from pedal and directly translate it to a key
let mut key_value = self.read_pedal(ped); let mut key_value = self.read_pedal(ped);
@ -393,6 +392,18 @@ impl Pedals {
self.set_type(*ped as usize, Type::Key); self.set_type(*ped as usize, Type::Key);
self.ped_data[*ped as usize].data[3] = key_value[3]; self.ped_data[*ped as usize].data[3] = key_value[3];
}, },
Some(Type::Mouse) => {
self.set_type(*ped as usize, Type::Mouse);
self.ped_data[*ped as usize].data[4] = key_value[4];
},
Some(Type::MouseKey) => {
self.set_type(*ped as usize, Type::MouseKey);
self.ped_data[*ped as usize].data[3] = key_value[3];
self.ped_data[*ped as usize].data[4] = key_value[4];
},
Some(Type::String) => {
self.set_type(*ped as usize, Type::String);
},
None => error!("The key type which was returned by the pedal was invalid!"), None => error!("The key type which was returned by the pedal was invalid!"),
_ => {} _ => {}