diff --git a/examples/config-keyboard.toml b/examples/config-keyboard.toml index 9a7ed7f..f9ca4f3 100644 --- a/examples/config-keyboard.toml +++ b/examples/config-keyboard.toml @@ -1,14 +1,13 @@ #SAMPLE CONFIG FILE FOR A GENERIC KEYBOARD -#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. -#You can find all the available keycodes in /usr/include/linux/input-event-codes.h +#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. Omit "/" if present. +#You can find the available keycodes in /usr/include/linux/input-event-codes.h #If you're not sure which keycode corresponds to which key, you can run 'evtest', select your device and press the corresponding key/button. -[bindings.keys] -#KEYS MOUSE/KEYBOARD BINDINGS +[remap] +#Examples of Key => Key(s) KEY_CAPSLOCK = ["KEY_LEFTCTRL"] KEY_LEFTCTRL = ["KEY_CAPSLOCK"] - -[combinations.keys] +#Examples of Modifier(s) + Key => Key(s) KEY_LEFTCTRL-KEY_LEFTSHIFT.KEY_Q = ["KEY_ESC"] KEY_LEFTSHIFT.KEY_UP = ["KEY_LEFTSHIFT", "KEY_PAGEUP"] KEY_LEFTSHIFT.KEY_DOWN = ["KEY_LEFTSHIFT", "KEY_PAGEDOWN"] diff --git a/examples/config-mouse.toml b/examples/config-mouse.toml index bdebb7c..ad7e682 100644 --- a/examples/config-mouse.toml +++ b/examples/config-mouse.toml @@ -1,22 +1,19 @@ #SAMPLE CONFIG FILE FOR A GENERIC MOUSE -#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. -#You can find all the available keycodes in /usr/include/linux/input-event-codes.h +#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. Omit "/" if present. +#You can find the available keycodes in /usr/include/linux/input-event-codes.h #If you're not sure which keycode corresponds to which key, you can run 'evtest', select your device and press the corresponding key/button. -#Axis values are hard coded instead, use SCROLL_WHEEL_UP and SCROLL_WHEEL_DOWN to rebind wheel movements. +#Relative and absolute axis events are hard coded, for example use SCROLL_WHEEL_UP and SCROLL_WHEEL_DOWN to rebind wheel movements. -[bindings.keys] -#MOUSE BUTTONS MOUSE/KEYBOARD BINDINGS +[remap] +#Example of Key => Key(s) BTN_RIGHT = ["KEY_LEFTCTRL", "KEY_C"] - -[bindings.axis] +#Example of Axis event => Key(s) SCROLL_WHEEL_UP = ["KEY_LEFTCTRL", "KEY_F"] SCROLL_WHEEL_DOWN = ["KEY_LEFTCTRL", "KEY_Q"] - -[combinations.keys] +#Examples of Modifier(s) + Key => Key(s) KEY_LEFTCTRL.BTN_RIGHT = ["KEY_SYSRQ"] KEY_LEFTCTRL-KEY_LEFTSHIFT.BTN_LEFT = ["KEY_LEFTSHIFT", "KEY_DELETE"] - -[combinations.axis] +#Examples of Modifier(s) + Axis event => Key(s) KEY_LEFTCTRL-KEY_LEFTSHIFT.SCROLL_WHEEL_UP = ["KEY_HOME"] KEY_LEFTCTRL-KEY_LEFTSHIFT.SCROLL_WHEEL_DOWN = ["KEY_END"] KEY_LEFTCTRL-KEY_LEFTSHIFT-KEY_LEFTALT.SCROLL_WHEEL_DOWN = ["KEY_LEFTALT", "KEY_F4"] diff --git a/examples/config-playstation.toml b/examples/config-playstation.toml index 9e4aed6..9cfd0fd 100644 --- a/examples/config-playstation.toml +++ b/examples/config-playstation.toml @@ -1,11 +1,12 @@ #SAMPLE CONFIG FILE FOR PLAYSTATION CONTROLLERS -#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. +#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. Omit "/" if present. #You can find all the available keycodes in /usr/include/linux/input-event-codes.h #If you're not sure which keycode corresponds to which key, you can run 'evtest', select your device and press the corresponding key/button. +#Relative and absolute axis events are hard coded, for example use RSTICK_UP, RSTICK_DOWN etc to rebind your analog stick. #This config file is tested for DualShock 3, DualShock 4 and DualSense controllers. When using a different controller, if no specific config file for your device is available, change the keycodes on the left according to those of your controller (evtest is your friend again). If your controller has a button to enable/disable analog sticks, make sure they're enabled. -[bindings.keys] -#CONTROLLER MOUSE/KEYBOARD BINDINGS +[remap] +#Examples of Button => Key(s) BTN_NORTH = ["KEY_LEFTMETA", "KEY_J"] #triangle BTN_EAST = ["KEY_ENTER"] #circle BTN_SOUTH = ["KEY_LEFTSHIFT"] #X @@ -17,19 +18,23 @@ BTN_SELECT = ["KEY_ESC"] #select BTN_THUMBR = ["KEY_LEFTMETA", "KEY_Q"] #R3 BTN_THUMBL = ["BTN_MIDDLE"] #L3 BTN_MODE = ["KEY_SPACE"] #PS button - -[bindings.axis] +#Examples of Axis events => Key(s) BTN_TL2 = ["KEY_LEFTMETA", "KEY_LEFTSHIFT", "KEY_K"] #L2 BTN_TR2 = ["KEY_LEFTMETA", "KEY_LEFTSHIFT", "KEY_L"] #R2 BTN_DPAD_UP = ["KEY_UP"] #directional pad up BTN_DPAD_RIGHT = ["KEY_RIGHT"] #directional pad right BTN_DPAD_DOWN = ["KEY_DOWN"] #directional pad down BTN_DPAD_LEFT = ["KEY_LEFT"] #directional pad left +RSTICK_UP = ["KEY_UP"] #right analog stick up +RSTICK_DOWN = ["KEY_DOWN"] #right analog stick down +RSTICK_LEFT = ["KEY_LEFT"] #right analog stick left +RSTICK_RIGHT = ["KEY_RIGHT"] #right analog stick right [settings] -CURSOR_SENSITIVITY = "6" #lower value is higher sensitivity, minimum 1 -SCROLL_SENSITIVITY = "6" #lower value is higher sensitivity, minimum 1 -CURSOR_STICK = "left" #left, right or none -SCROLL_STICK = "right" #left, right or none -DEADZONE = "5" #integer between 0 and 128, bigger number is wider deadzone, default 5 +LSTICK_SENSITIVITY = "6" #sensitivity when scrolling or moving cursor, lower value is higher sensitivity, minimum 1 +RSTICK_SENSITIVITY = "6" #sensitivity when scrolling or moving cursor, lower value is higher sensitivity, minimum 1 +LSTICK = "cursor" #cursor, scroll, bind or disabled +RSTICK = "bind" #cursor, scroll, bind or disabled +LSTICK_DEADZONE = "5" #integer between 0 and 128, bigger number is wider deadzone, default 5 +RSTICK_DEADZONE = "64" #integer between 0 and 128, bigger number is wider deadzone, default 5 GRAB_DEVICE = "false" #gain exclusivity on the device diff --git a/examples/config-ps2.toml b/examples/config-ps2.toml index df46f4e..44356cd 100644 --- a/examples/config-ps2.toml +++ b/examples/config-ps2.toml @@ -1,11 +1,12 @@ #SAMPLE CONFIG FILE FOR PLAYSTATION 2 CONTROLLERS -#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. +#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. Omit "/" if present. #You can find all the available keycodes in /usr/include/linux/input-event-codes.h #If you're not sure which keycode corresponds to which key, you can run 'evtest', select your device and press the corresponding key/button. +#Relative and absolute axis events are hard coded, for example use RSTICK_UP, RSTICK_DOWN etc to rebind your analog stick. #This config file is tested for DualShock 2 controllers. The "analog" LED on your controller has to be turned on or this daemon won't work properly. When using a different controller, if no specific config file for your device is available, change the keycodes on the left according to those of your controller (evtest is your friend again). -[bindings.keys] -#CONTROLLER MOUSE/KEYBOARD BINDINGS +[remap] +#Examples of Button => Key(s) BTN_TRIGGER = ["KEY_LEFTMETA", "KEY_J"] #triangle BTN_THUMB = ["KEY_ENTER"] #circle BTN_THUMB2 = ["KEY_LEFTSHIFT"] #X @@ -18,17 +19,21 @@ BTN_BASE4 = ["KEY_LEFTMETA", "KEY_D"] #start BTN_BASE3 = ["KEY_ESC"] #select BTN_BASE6 = ["KEY_LEFTMETA", "KEY_Q"] #R3 BTN_BASE5 = ["BTN_MIDDLE"] #L3 - -[bindings.axis] +#Examples of Axis events => Key(s) BTN_DPAD_UP = ["KEY_UP"] #directional pad up BTN_DPAD_RIGHT = ["KEY_RIGHT"] #directional pad right BTN_DPAD_DOWN = ["KEY_DOWN"] #directional pad down BTN_DPAD_LEFT = ["KEY_LEFT"] #directional pad left +RSTICK_UP = ["KEY_UP"] #right analog stick up +RSTICK_DOWN = ["KEY_DOWN"] #right analog stick down +RSTICK_LEFT = ["KEY_LEFT"] #right analog stick left +RSTICK_RIGHT = ["KEY_RIGHT"] #right analog stick right [settings] -CURSOR_SENSITIVITY = "6" #lower value is higher sensitivity, minimum 1 -SCROLL_SENSITIVITY = "6" #lower value is higher sensitivity, minimum 1 -CURSOR_STICK = "left" #left, right or none -SCROLL_STICK = "right" #left, right or none -DEADZONE = "5" #integer between 0 and 128, bigger number is wider deadzone, default 5 -GRAB_DEVICE = "false" #gain exclusivity on the device +LSTICK_SENSITIVITY = "6" #sensitivity when scrolling or moving cursor, lower value is higher sensitivity, minimum 1 +RSTICK_SENSITIVITY = "6" #sensitivity when scrolling or moving cursor, lower value is higher sensitivity, minimum 1 +LSTICK = "cursor" #cursor, scroll, bind or disabled +RSTICK = "bind" #cursor, scroll, bind or disabled +LSTICK_DEADZONE = "5" #integer between 0 and 128, bigger number is wider deadzone, default 5 +RSTICK_DEADZONE = "64" #integer between 0 and 128, bigger number is wider deadzone, default 5 +GRAB_DEVICE = "false" #gain exclusivity on the device \ No newline at end of file diff --git a/examples/config-stadia.toml b/examples/config-stadia.toml index 5a53b77..fcb2a53 100644 --- a/examples/config-stadia.toml +++ b/examples/config-stadia.toml @@ -1,11 +1,12 @@ #SAMPLE CONFIG FILE FOR GOOGLE STADIA CONTROLLERS -#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. +#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. Omit "/" if present. #You can find all the available keycodes in /usr/include/linux/input-event-codes.h #If you're not sure which keycode corresponds to which key, you can run 'evtest', select your device and press the corresponding key/button. +#Relative and absolute axis events are hard coded, for example use RSTICK_UP, RSTICK_DOWN etc to rebind your analog stick. #This config file is tested for Stadia controllers. When using a different controller, if no specific config file for your device is available, change the keycodes on the left according to those of your controller (evtest is your friend again). If your controller has a button to enable/disable analog sticks, make sure they're enabled. -[bindings.keys] -#CONTROLLER MOUSE/KEYBOARD BINDINGS +[remap] +#Examples of Button => Key(s) BTN_WEST = ["KEY_LEFTMETA", "KEY_J"] #Y BTN_EAST = ["KEY_ENTER"] #X BTN_SOUTH = ["KEY_LEFTSHIFT"] #A @@ -21,17 +22,21 @@ BTN_TRIGGER_HAPPY2 = ["BTN_LEFT"] #share/bubbles? BTN_THUMBR = ["KEY_LEFTMETA", "KEY_Q"] #R3 BTN_THUMBL = ["BTN_MIDDLE"] #L3 BTN_MODE = ["KEY_SPACE"] #Stadia button - -[bindings.axis] +#Examples of Axis events => Key(s) BTN_DPAD_UP = ["KEY_UP"] #directional pad up BTN_DPAD_RIGHT = ["KEY_RIGHT"] #directional pad right BTN_DPAD_DOWN = ["KEY_DOWN"] #directional pad down BTN_DPAD_LEFT = ["KEY_LEFT"] #directional pad left +RSTICK_UP = ["KEY_UP"] #right analog stick up +RSTICK_DOWN = ["KEY_DOWN"] #right analog stick down +RSTICK_LEFT = ["KEY_LEFT"] #right analog stick left +RSTICK_RIGHT = ["KEY_RIGHT"] #right analog stick right [settings] -CURSOR_SENSITIVITY = "6" #lower value is higher sensitivity, minimum 1 -SCROLL_SENSITIVITY = "6" #lower value is higher sensitivity, minimum 1 -CURSOR_STICK = "left" #left, right or none -SCROLL_STICK = "right" #left, right or none -DEADZONE = "5" #integer between 0 and 128, bigger number is wider deadzone, default 5 +LSTICK_SENSITIVITY = "6" #sensitivity when scrolling or moving cursor, lower value is higher sensitivity, minimum 1 +RSTICK_SENSITIVITY = "6" #sensitivity when scrolling or moving cursor, lower value is higher sensitivity, minimum 1 +LSTICK = "cursor" #cursor, scroll, bind or disabled +RSTICK = "bind" #cursor, scroll, bind or disabled +LSTICK_DEADZONE = "5" #integer between 0 and 128, bigger number is wider deadzone, default 5 +RSTICK_DEADZONE = "64" #integer between 0 and 128, bigger number is wider deadzone, default 5 GRAB_DEVICE = "false" #gain exclusivity on the device diff --git a/examples/config-switch.toml b/examples/config-switch.toml index 056595e..32df786 100644 --- a/examples/config-switch.toml +++ b/examples/config-switch.toml @@ -1,11 +1,12 @@ #SAMPLE CONFIG FILE FOR SWITCH JOYCONS -#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. +#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. Omit "/" if present. #You can find all the available keycodes in /usr/include/linux/input-event-codes.h #If you're not sure which keycode corresponds to which key, you can run 'evtest', select your device and press the corresponding key/button. +#Relative and absolute axis events are hard coded, for example use RSTICK_UP, RSTICK_DOWN etc to rebind your analog stick. #This config file is tested for Switch Joycons (Left and Right). When using a different controller, if no specific config file for your device is available, change the keycodes on the left according to those of your controller (evtest is your friend again). If your controller has a button to enable/disable analog sticks, make sure they're enabled. -[bindings.keys] -#CONTROLLER MOUSE/KEYBOARD BINDINGS +[remap] +#Examples of Button => Key(s) BTN_NORTH = ["KEY_LEFTMETA", "KEY_J"] #X BTN_EAST = ["KEY_ENTER"] #A BTN_SOUTH = ["KEY_LEFTSHIFT"] #B @@ -18,20 +19,24 @@ BTN_THUMBR = ["KEY_LEFTMETA", "KEY_Q"] #right stick press BTN_THUMBL = ["BTN_MIDDLE"] #left stick press BTN_MODE = ["KEY_SPACE"] #home BTN_Z = ["BTN_LEFT"] #capture - -[bindings.axis] +#Examples of Axis events => Key(s) BTN_TL2 = ["KEY_LEFTMETA", "KEY_LEFTSHIFT", "KEY_K"] #ZL (and SR on right joycon) BTN_TR2 = ["KEY_LEFTMETA", "KEY_LEFTSHIFT", "KEY_L"] #ZR (and SR on left joycon) BTN_DPAD_UP = ["KEY_UP"] #directional pad up BTN_DPAD_RIGHT = ["KEY_RIGHT"] #directional pad right BTN_DPAD_DOWN = ["KEY_DOWN"] #directional pad down BTN_DPAD_LEFT = ["KEY_LEFT"] #directional pad left +RSTICK_UP = ["KEY_UP"] #right analog stick up +RSTICK_DOWN = ["KEY_DOWN"] #right analog stick down +RSTICK_LEFT = ["KEY_LEFT"] #right analog stick left +RSTICK_RIGHT = ["KEY_RIGHT"] #right analog stick right [settings] -CURSOR_SENSITIVITY = "6" #lower value is higher sensitivity, minimum 1 -SCROLL_SENSITIVITY = "6" #lower value is higher sensitivity, minimum 1 -CURSOR_STICK = "left" #left, right or none -SCROLL_STICK = "right" #left, right or none -DEADZONE = "5" #integer between 0 and 128, bigger number is wider deadzone, default 5 -SIGNED_AXIS_VALUE = "true" #necessary for Xbox controllers and Switch joycons, use false for other controllers +LSTICK_SENSITIVITY = "6" #sensitivity when scrolling or moving cursor, lower value is higher sensitivity, minimum 1 +RSTICK_SENSITIVITY = "6" #sensitivity when scrolling or moving cursor, lower value is higher sensitivity, minimum 1 +LSTICK = "cursor" #cursor, scroll, bind or disabled +RSTICK = "bind" #cursor, scroll, bind or disabled +LSTICK_DEADZONE = "5" #integer between 0 and 128, bigger number is wider deadzone, default 5 +RSTICK_DEADZONE = "64" #integer between 0 and 128, bigger number is wider deadzone, default 5 +16_BIT_AXIS = "true" #necessary for Xbox controllers and Switch joycons, use false for other controllers GRAB_DEVICE = "false" #gain exclusivity on the device diff --git a/examples/config-xbox.toml b/examples/config-xbox.toml index 496e138..77f3bc6 100644 --- a/examples/config-xbox.toml +++ b/examples/config-xbox.toml @@ -1,11 +1,12 @@ #SAMPLE CONFIG FILE FOR XBOX CONTROLLERS -#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. +#Put this in ~/.config/makima and rename it to the exact name of the device as shown by the 'evtest' command, including spaces and capitalization. Omit "/" if present. #You can find all the available keycodes in /usr/include/linux/input-event-codes.h #If you're not sure which keycode corresponds to which key, you can run 'evtest', select your device and press the corresponding key/button. +#Relative and absolute axis events are hard coded, for example use RSTICK_UP, RSTICK_DOWN etc to rebind your analog stick. #This config file is tested for Xbox 360, Xbox One and Xbox Elite 2 controllers. When using a different controller, if no specific config file for your device is available, change the keycodes on the left according to those of your controller (evtest is your friend again). If your controller has a button to enable/disable analog sticks, make sure they're enabled. -[bindings.keys] -#CONTROLLER MOUSE/KEYBOARD BINDINGS +[remap] +#Examples of Button => Key(s) BTN_NORTH = ["KEY_LEFTMETA", "KEY_J"] #X BTN_EAST = ["KEY_ENTER"] #Y BTN_SOUTH = ["KEY_LEFTSHIFT"] #A @@ -17,20 +18,24 @@ BTN_SELECT = ["KEY_ESC"] #back BTN_THUMBR = ["KEY_LEFTMETA", "KEY_Q"] #RS BTN_THUMBL = ["BTN_MIDDLE"] #LS BTN_MODE = ["KEY_SPACE"] #Xbox button - -[bindings.axis] +#Examples of Axis events => Key(s) BTN_TR2 = ["KEY_LEFTMETA", "KEY_LEFTSHIFT", "KEY_L"] #RT BTN_TL2 = ["KEY_LEFTMETA", "KEY_LEFTSHIFT", "KEY_K"] #LT BTN_DPAD_UP = ["KEY_UP"] #directional pad up BTN_DPAD_RIGHT = ["KEY_RIGHT"] #directional pad right BTN_DPAD_DOWN = ["KEY_DOWN"] #directional pad down BTN_DPAD_LEFT = ["KEY_LEFT"] #directional pad left +RSTICK_UP = ["KEY_UP"] #right analog stick up +RSTICK_DOWN = ["KEY_DOWN"] #right analog stick down +RSTICK_LEFT = ["KEY_LEFT"] #right analog stick left +RSTICK_RIGHT = ["KEY_RIGHT"] #right analog stick right [settings] -CURSOR_SENSITIVITY = "6" #lower value is higher sensitivity, minimum 1 -SCROLL_SENSITIVITY = "6" #lower value is higher sensitivity, minimum 1 -CURSOR_STICK = "left" #left, right or none -SCROLL_STICK = "right" #left, right or none -DEADZONE = "5" #integer between 0 and 128, bigger number is wider deadzone, default 5 -SIGNED_AXIS_VALUE = "true" #necessary for Xbox controllers and Switch joycons, use false for other controllers +LSTICK_SENSITIVITY = "6" #sensitivity when scrolling or moving cursor, lower value is higher sensitivity, minimum 1 +RSTICK_SENSITIVITY = "6" #sensitivity when scrolling or moving cursor, lower value is higher sensitivity, minimum 1 +LSTICK = "cursor" #cursor, scroll, bind or disabled +RSTICK = "bind" #cursor, scroll, bind or disabled +LSTICK_DEADZONE = "5" #integer between 0 and 128, bigger number is wider deadzone, default 5 +RSTICK_DEADZONE = "64" #integer between 0 and 128, bigger number is wider deadzone, default 5 +16_BIT_AXIS = "true" #necessary for Xbox controllers and Switch joycons, use false for other controllers GRAB_DEVICE = "false" #gain exclusivity on the device diff --git a/src/config.rs b/src/config.rs index b4add64..831f297 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,46 +4,33 @@ use evdev::Key; use serde; -#[derive(serde::Deserialize, Debug, Clone, Default)] +#[derive(Default, Debug, Clone)] pub struct Bindings { - #[serde(default)] pub keys: HashMap>, - #[serde(default)] pub axis: HashMap>, } -#[derive(serde::Deserialize, Debug, Clone, Default)] +#[derive(Default, Debug, Clone)] pub struct Combinations { - #[serde(default)] pub keys: HashMap>>, - #[serde(default)] pub axis: HashMap>>, } -#[derive(serde::Deserialize, Debug, Clone, Default)] +#[derive(Default, Debug, Clone)] pub struct Modifiers { pub keys: HashMap, HashMap>>, pub axis: HashMap, HashMap>>, } -impl Modifiers { - pub fn new() -> Self { - let keys: HashMap, HashMap>> = HashMap::new(); - let axis: HashMap, HashMap>> = HashMap::new(); - Self { - keys: keys, - axis: axis - } - } -} - #[derive(serde::Deserialize, Debug, Clone)] pub struct Config { #[serde(skip)] pub name: String, #[serde(default)] + pub remap: HashMap>, + #[serde(skip)] pub bindings: Bindings, - #[serde(default)] + #[serde(skip)] pub combinations: Combinations, #[serde(skip)] pub modifiers: Modifiers, @@ -56,36 +43,81 @@ impl Config { let file_content: String = std::fs::read_to_string(file).unwrap(); let config: Config = toml::from_str(&file_content) .expect("Couldn't parse config file."); - let mut bindings: Bindings = config.bindings; - let combinations: Combinations = config.combinations; let settings: HashMap = config.settings; - + let remap: HashMap> = config.remap; + let mut bindings: Bindings = Default::default(); + let mut combinations: Combinations = Default::default(); + + let abs = [ + "DPAD_UP", + "DPAD_DOWN", + "DPAD_LEFT", + "DPAD_RIGHT", + "LSTICK_UP", + "LSTICK_DOWN", + "LSTICK_LEFT", + "LSTICK_RIGHT", + "RSTICK_UP", + "RSTICK_DOWN", + "RSTICK_LEFT", + "RSTICK_RIGHT", + "SCROLLWHEEL_UP", + "SCROLLWHEEL_DOWN", + "BTN_TL2", + "BTN_TR2", + ]; + for (input, output) in remap.clone().into_iter() { + if input.contains(".") { + let (mods, key) = input.split_once(".").unwrap(); + if abs.contains(&key) { + if !combinations.axis.contains_key(&mods.to_string()) { + combinations.axis.insert(mods.to_string(), HashMap::from([(key.to_string(), output)])); + } else { + combinations.axis.get_mut(mods).unwrap().insert(key.to_string(), output); + } + } else { + if !combinations.keys.contains_key(&mods.to_string()) { + combinations.keys.insert(mods.to_string(), HashMap::from([(Key::from_str(key).expect("Invalid KEY value."), output)])); + } else { + combinations.keys.get_mut(mods).unwrap().insert(Key::from_str(key).expect("Invalid KEY value."), output); + } + } + } else { + if abs.contains(&input.as_str()) { + bindings.axis.insert(input, output); + } else { + bindings.keys.insert(Key::from_str(input.as_str()).expect("Invalid KEY value."), output); + } + } + } + let empty_modmap = BTreeMap::from ([ - (Key::KEY_LEFTSHIFT, 0), - (Key::KEY_LEFTCTRL, 0), - (Key::KEY_LEFTALT, 0), - (Key::KEY_RIGHTSHIFT, 0), - (Key::KEY_RIGHTCTRL, 0), - (Key::KEY_RIGHTALT, 0), - (Key::KEY_LEFTMETA, 0) + (Key::KEY_LEFTSHIFT, 0), + (Key::KEY_LEFTCTRL, 0), + (Key::KEY_LEFTALT, 0), + (Key::KEY_RIGHTSHIFT, 0), + (Key::KEY_RIGHTCTRL, 0), + (Key::KEY_RIGHTALT, 0), + (Key::KEY_LEFTMETA, 0) ]); - let mut modifiers = Modifiers::new(); - for (mods, map) in combinations.keys.iter() { - let mods_vector = mods.split("-").map(str::to_string).collect::>(); + let mut modifiers: Modifiers = Default::default(); + + for (mods, key) in combinations.keys.iter() { + let mods_vector = mods.split("+").map(str::to_string).collect::>(); let mut modmap = empty_modmap.clone(); for modifier in mods_vector { modmap.insert(Key::from_str(&modifier).unwrap(), 1); } - modifiers.keys.insert(modmap, map.clone()); + modifiers.keys.insert(modmap, key.clone()); } - for (mods, map) in combinations.axis.iter() { - let mods_vector = mods.split("-").map(str::to_string).collect::>(); + for (mods, key) in combinations.axis.iter() { + let mods_vector = mods.split("+").map(str::to_string).collect::>(); let mut modmap = empty_modmap.clone(); for modifier in mods_vector { modmap.insert(Key::from_str(&modifier).unwrap(), 1); } - modifiers.axis.insert(modmap, map.clone()); + modifiers.axis.insert(modmap, key.clone()); } let mut pad_x: Vec = bindings.axis.get("BTN_DPAD_LEFT") @@ -123,6 +155,7 @@ impl Config { Self { name: file_name, + remap, bindings, combinations, modifiers, diff --git a/src/event_reader.rs b/src/event_reader.rs index 834aed7..f185bda 100644 --- a/src/event_reader.rs +++ b/src/event_reader.rs @@ -44,7 +44,6 @@ impl EventReader { let rstick_position = Arc::new(Mutex::new(position_vector.clone())); let device_is_connected: Arc> = Arc::new(Mutex::new(true)); let virt_dev = Arc::new(Mutex::new(VirtualDevices::new())); - let lstick_function = config.get(&"default".to_string()).unwrap() .settings.get("LSTICK").unwrap_or(&"cursor".to_string()).to_string(); let lstick_sensitivity: u64 = config.get(&"default".to_string()).unwrap() @@ -100,6 +99,8 @@ impl EventReader { } pub async fn event_loop(&self) { + let mut lstick_values = HashMap::from([("x", 0), ("y", 0)]); + let mut rstick_values = HashMap::from([("x", 0), ("y", 0)]); let mut stream = self.stream.lock().await; while let Some(Ok(event)) = stream.next().await { match (event.event_type(), RelativeAxisType(event.code()), AbsoluteAxisType(event.code())) { @@ -113,7 +114,7 @@ impl EventReader { _ => Option::None, }; if let Some(event_string) = event_string_option { - self.convert_axis_events(event, &event_string, true, false).await; + self.convert_axis_events(event, &event_string, true).await; } }, (_, _, AbsoluteAxisType::ABS_HAT0X) => { @@ -123,7 +124,7 @@ impl EventReader { 1 => "BTN_DPAD_RIGHT".to_string(), _ => "BTN_DPAD_X".to_string(), }; - self.convert_axis_events(event, &event_string, false, false).await; + self.convert_axis_events(event, &event_string, false).await; }, (_, _, AbsoluteAxisType::ABS_HAT0Y) => { let event_string: String = match event.value() { @@ -132,7 +133,7 @@ impl EventReader { 1 => "BTN_DPAD_DOWN".to_string(), _ => "BTN_DPAD_Y".to_string(), }; - self.convert_axis_events(event, &event_string, false, false).await; + self.convert_axis_events(event, &event_string, false).await; }, (EventType::ABSOLUTE, _, AbsoluteAxisType::ABS_X | AbsoluteAxisType::ABS_Y) => { if ["cursor", "scroll"].contains(&self.settings.lstick.function.as_str()) { @@ -148,17 +149,35 @@ impl EventReader { else if AbsoluteAxisType(event.code()) == AbsoluteAxisType::ABS_Y { "y" } else { "none" }; let event_string_option: Option = match clamped_value { - -1 if axis == "x" => Option::Some("LSTICK_LEFT".to_string()), - -1 if axis == "y" => Option::Some("LSTICK_UP".to_string()), - 0 if axis == "x" => Option::Some("LSTICK_X".to_string()), - 0 if axis == "y" => Option::Some("LSTICK_Y".to_string()), - 1 if axis == "x" => Option::Some("LSTICK_RIGHT".to_string()), - 1 if axis == "y" => Option::Some("LSTICK_DOWN".to_string()), + -1 if axis == "x" && lstick_values.get("x").unwrap() != &-1 => { + lstick_values.insert("x", -1); + Option::Some("LSTICK_LEFT".to_string()) + }, + -1 if axis == "y" && lstick_values.get("y").unwrap() != &-1 => { + lstick_values.insert("y", -1); + Option::Some("LSTICK_UP".to_string()) + }, + 0 if axis == "x" && lstick_values.get("x").unwrap() != &0 => { + lstick_values.insert("x", 0); + Option::Some("LSTICK_X".to_string()) + }, + 0 if axis == "y" && lstick_values.get("y").unwrap() != &0 => { + lstick_values.insert("y", 0); + Option::Some("LSTICK_Y".to_string()) + }, + 1 if axis == "x" && lstick_values.get("x").unwrap() != &1 => { + lstick_values.insert("x", 1); + Option::Some("LSTICK_RIGHT".to_string()) + }, + 1 if axis == "y" && lstick_values.get("y").unwrap() != &1 => { + lstick_values.insert("y", 1); + Option::Some("LSTICK_DOWN".to_string()) + }, _ => Option::None, }; if let Some(event_string) = event_string_option { let clamped_event = InputEvent::new_now(event.event_type(), event.code(), clamped_value); - self.convert_axis_events(clamped_event, &event_string, false, false).await; + self.convert_axis_events(clamped_event, &event_string, false).await; } } else { self.emit_default_event(event).await; @@ -178,27 +197,49 @@ impl EventReader { else if AbsoluteAxisType(event.code()) == AbsoluteAxisType::ABS_RY { "y" } else { "none" }; let event_string_option: Option = match clamped_value { - -1 if axis == "x" => Option::Some("RSTICK_LEFT".to_string()), - -1 if axis == "y" => Option::Some("RSTICK_UP".to_string()), - 0 if axis == "x" => Option::Some("RSTICK_X".to_string()), - 0 if axis == "y" => Option::Some("RSTICK_Y".to_string()), - 1 if axis == "x" => Option::Some("RSTICK_RIGHT".to_string()), - 1 if axis == "y" => Option::Some("RSTICK_DOWN".to_string()), + -1 if axis == "x" && rstick_values.get("x").unwrap() != &-1 => { + rstick_values.insert("x", -1); + Option::Some("RSTICK_LEFT".to_string()) + }, + -1 if axis == "y" && rstick_values.get("y").unwrap() != &-1 => { + rstick_values.insert("y", -1); + Option::Some("RSTICK_UP".to_string()) + }, + 0 if axis == "x" && rstick_values.get("x").unwrap() != &0 => { + rstick_values.insert("x", 0); + Option::Some("RSTICK_X".to_string()) + }, + 0 if axis == "y" && rstick_values.get("y").unwrap() != &0 => { + rstick_values.insert("y", 0); + Option::Some("RSTICK_Y".to_string()) + }, + 1 if axis == "x" && rstick_values.get("x").unwrap() != &1 => { + rstick_values.insert("x", 1); + Option::Some("RSTICK_RIGHT".to_string()) + }, + 1 if axis == "y" && rstick_values.get("y").unwrap() != &1 => { + rstick_values.insert("y", 1); + Option::Some("RSTICK_DOWN".to_string()) + }, _ => Option::None, }; if let Some(event_string) = event_string_option { let clamped_event = InputEvent::new_now(event.event_type(), event.code(), clamped_value); - self.convert_axis_events(clamped_event, &event_string, false, false).await; + self.convert_axis_events(clamped_event, &event_string, false).await; } } else { self.emit_default_event(event).await; } }, (EventType::ABSOLUTE, _, AbsoluteAxisType::ABS_Z) => { - self.convert_axis_events(event, &"BTN_TL2".to_string(), false, true).await; + let clamped_value = if event.value() > 0 { 1 } else { 0 }; + let clamped_event = InputEvent::new_now(event.event_type(), event.code(), clamped_value); + self.convert_axis_events(clamped_event, &"BTN_TL2".to_string(), false).await; }, (EventType::ABSOLUTE, _, AbsoluteAxisType::ABS_RZ) => { - self.convert_axis_events(event, &"BTN_TR2".to_string(), false, true).await; + let clamped_value = if event.value() > 0 { 1 } else { 0 }; + let clamped_event = InputEvent::new_now(event.event_type(), event.code(), clamped_value); + self.convert_axis_events(clamped_event, &"BTN_TR2".to_string(), false).await; }, _ => {self.emit_default_event(event).await;} } @@ -224,19 +265,12 @@ impl EventReader { } } - async fn convert_axis_events(&self, event: InputEvent, event_string: &String, send_zero: bool, clamp_value: bool) { + async fn convert_axis_events(&self, event: InputEvent, event_string: &String, send_zero: bool) { let path = self.config.get(&get_active_window(&self.current_desktop, &self.config).await).unwrap(); let modifiers = self.modifiers.lock().await.clone(); - let value = { - if clamp_value && event.value() > 1 { - 1 - } else { - event.value() - } - }; if let Some(event_hashmap) = path.modifiers.axis.get(&modifiers) { if let Some(event_list) = event_hashmap.get(event_string) { - self.emit_event_without_modifiers(event_list, &modifiers, value).await; + self.emit_event_without_modifiers(event_list, &modifiers, event.value()).await; if send_zero { self.emit_event_without_modifiers(event_list, &modifiers, 0).await; } @@ -245,7 +279,7 @@ impl EventReader { } if let Some(event_list) = path.bindings.axis.get(event_string) { println!("{:?}", event_list); - self.emit_event(event_list, value).await; + self.emit_event(event_list, event.value()).await; if send_zero { self.emit_event_without_modifiers(event_list, &modifiers, 0).await; }