From f4f0cbdf35c1881aea647e9bbbd920635bb667d7 Mon Sep 17 00:00:00 2001 From: cyber-sushi Date: Fri, 5 Apr 2024 22:32:15 +0200 Subject: [PATCH] Added deadzone configuration --- README.md | 1 + examples/config-playstation.toml | 1 + examples/config-ps2.toml | 1 + examples/config-stadia.toml | 1 + examples/config-switch.toml | 3 ++- examples/config-xbox.toml | 2 ++ src/event_reader.rs | 31 +++++++++++++++++-------------- 7 files changed, 25 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 12aa741..fbb8431 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,7 @@ There are currently 6 available settings: - `SCROLL_STICK` will set if 2D scrolling using your controller's analog sticks should be anabled, and which of the two sticks should move your cursor. Can be set to `"left"`, `"right"` or `"none"`. Defaults to `"right"` if not set. If both `CURSOR_STICK` and `SCROLL_STICK` are set to the same value, `CURSOR_STICK` will have priority. - `CURSOR_SENSITIVITY` will change the speed of your mouse cursor when it's being moved with an analog stick. Lower value is higher sensitivity, minimum `"1"`, suggested `"6"`. If this isn't set, cursor movement will be disabled. - `SCROLL_SENSITIVITY` will change the scroll speed when scrolling with an analog stick. Lower value is higher sensitivity, minimum `"1"`, suggested `"6"`. If this isn't set, scrolling will be disabled. +- `DEADZONE` will change how much your analog sticks should be tilted before their inputs are detected. Particularly useful for older devices that suffer from drifting. Use a value from `"0"` to `"128"`. - `SIGNED_AXIS_VALUE` is needed if you're using Xbox controllers and Switch Joy-Cons to properly calibrate the analog stick's sensitivity. Set to `"true"` if you're using those controllers. Can be left out otherwise. Example settings for a keyboard or mouse, notice that only the `GRAB_DEVICE` setting is needed in this case and you can leave everything else out: diff --git a/examples/config-playstation.toml b/examples/config-playstation.toml index 7fec42f..9e4aed6 100644 --- a/examples/config-playstation.toml +++ b/examples/config-playstation.toml @@ -31,4 +31,5 @@ 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 diff --git a/examples/config-ps2.toml b/examples/config-ps2.toml index 4c1480d..df46f4e 100644 --- a/examples/config-ps2.toml +++ b/examples/config-ps2.toml @@ -30,4 +30,5 @@ 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 diff --git a/examples/config-stadia.toml b/examples/config-stadia.toml index 9ba05c3..5a53b77 100644 --- a/examples/config-stadia.toml +++ b/examples/config-stadia.toml @@ -33,4 +33,5 @@ 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 diff --git a/examples/config-switch.toml b/examples/config-switch.toml index a49fcff..056595e 100644 --- a/examples/config-switch.toml +++ b/examples/config-switch.toml @@ -32,5 +32,6 @@ 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 -GRAB_DEVICE = "false" #gain exclusivity on the device +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 +GRAB_DEVICE = "false" #gain exclusivity on the device diff --git a/examples/config-xbox.toml b/examples/config-xbox.toml index 3af4b47..496e138 100644 --- a/examples/config-xbox.toml +++ b/examples/config-xbox.toml @@ -31,4 +31,6 @@ 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 +GRAB_DEVICE = "false" #gain exclusivity on the device diff --git a/src/event_reader.rs b/src/event_reader.rs index 06920a2..95d4301 100644 --- a/src/event_reader.rs +++ b/src/event_reader.rs @@ -66,6 +66,10 @@ impl EventReader { if let Some(axis_value) = self.config.get(&"default".to_string()).unwrap().settings.get("SIGNED_AXIS_VALUE") { has_signed_axis_value = axis_value.as_str(); } + let mut deadzone: i32 = 5; + if let Some(deadzone_str) = self.config.get(&"default".to_string()).unwrap().settings.get("DEADZONE") { + deadzone = deadzone_str.parse::().expect("Invalid value for DEADZONE, please use an integer between 0 and 128."); + } while let Some(Ok(event)) = stream.next().await { match (event.event_type(), RelativeAxisType(event.code()), AbsoluteAxisType(event.code())) { (EventType::KEY, _, _) => { @@ -103,22 +107,22 @@ impl EventReader { }, (EventType::ABSOLUTE, _, AbsoluteAxisType::ABS_X | AbsoluteAxisType::ABS_Y) => { if cursor_analog_mode == "left" { - let axis_value = self.get_axis_value(&has_signed_axis_value, &event).await; + let axis_value = self.get_axis_value(&has_signed_axis_value, &event, deadzone).await; let mut cursor_analog_position = self.cursor_analog_position.lock().await; cursor_analog_position[event.code() as usize] = axis_value; } else if scroll_analog_mode == "left" { - let axis_value = self.get_axis_value(&has_signed_axis_value, &event).await; + let axis_value = self.get_axis_value(&has_signed_axis_value, &event, deadzone).await; let mut scroll_analog_position = self.scroll_analog_position.lock().await; scroll_analog_position[event.code() as usize] = axis_value; } }, (EventType::ABSOLUTE, _, AbsoluteAxisType::ABS_RX | AbsoluteAxisType::ABS_RY) => { if cursor_analog_mode == "right" { - let axis_value = self.get_axis_value(&has_signed_axis_value, &event).await; + let axis_value = self.get_axis_value(&has_signed_axis_value, &event, deadzone).await; let mut cursor_analog_position = self.cursor_analog_position.lock().await; cursor_analog_position[event.code() as usize -3] = axis_value; } else if scroll_analog_mode == "right" { - let axis_value = self.get_axis_value(&has_signed_axis_value, &event).await; + let axis_value = self.get_axis_value(&has_signed_axis_value, &event, deadzone).await; let mut scroll_analog_position = self.scroll_analog_position.lock().await; scroll_analog_position[event.code() as usize -3] = axis_value; } @@ -234,17 +238,16 @@ impl EventReader { } } - async fn get_axis_value(&self, has_signed_axis_value: &str, event: &InputEvent) -> i32 { - let axis_value: i32 = match &has_signed_axis_value { - &"false" => { - let distance_from_center: i32 = event.value() as i32 - 128; - distance_from_center / 10 - } - _ => { - event.value() as i32 / 2000 - } + async fn get_axis_value(&self, has_signed_axis_value: &str, event: &InputEvent, deadzone: i32) -> i32 { + let distance_from_center: i32 = match &has_signed_axis_value { + &"false" => (event.value() as i32 - 128) * 200, + _ => event.value() as i32 }; - return axis_value + if distance_from_center.abs() <= deadzone * 200 { + 0 + } else { + (distance_from_center + 2000 - 1) / 2000 + } } async fn toggle_modifiers(&self, key: Key, value: i32) {