Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(DBusTarget): translate analog trigger input to dbus events #174

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/input/event/dbus.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::str::FromStr;

use crate::input::capability::{Capability, Gamepad, GamepadAxis, GamepadButton, Keyboard, Touch};
use crate::input::capability::{
Capability, Gamepad, GamepadAxis, GamepadButton, GamepadTrigger, Keyboard, Touch,
};

use super::{native::NativeEvent, value::InputValue};

Expand Down Expand Up @@ -221,6 +223,14 @@ fn actions_from_capability(capability: Capability) -> Vec<Action> {
}
_ => vec![Action::None],
},
Gamepad::Trigger(trigger) => match trigger {
GamepadTrigger::LeftTrigger => vec![Action::L2],
GamepadTrigger::LeftTouchpadForce => vec![Action::None],
GamepadTrigger::LeftStickForce => vec![Action::None],
GamepadTrigger::RightTrigger => vec![Action::R2],
GamepadTrigger::RightTouchpadForce => vec![Action::None],
GamepadTrigger::RightStickForce => vec![Action::None],
},
_ => vec![Action::None],
},
Capability::Mouse(_) => vec![Action::None],
Expand Down Expand Up @@ -431,9 +441,7 @@ fn dbus_event_from_value(action: Action, input_value: InputValue) -> Option<DBus
y: _,
} => Some(input_value),
};
let Some(value) = value else {
return None;
};
let value = value?;

Some(DBusEvent { action, value })
}
170 changes: 112 additions & 58 deletions src/input/target/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use zbus::Connection;
use crate::{
dbus::interface::target::dbus::TargetDBusInterface,
input::{
capability::{Capability, Gamepad},
capability::{Capability, Gamepad, GamepadButton},
event::{
dbus::{Action, DBusEvent},
native::NativeEvent,
Expand All @@ -18,6 +18,8 @@ use super::{client::TargetDeviceClient, TargetInputDevice, TargetOutputDevice};

/// The threshold for axis inputs to be considered "pressed"
const AXIS_THRESHOLD: f64 = 0.35;
/// The threshold for trigger inputs to be considered "pressed"
const TRIGGER_THRESHOLD: f64 = 0.75;

/// The internal emulated device state for tracking analog input
#[derive(Debug, Clone, Default)]
Expand All @@ -26,6 +28,10 @@ struct State {
pressed_right: bool,
pressed_up: bool,
pressed_down: bool,
pressed_l2: bool,
l2_value: Option<f64>,
pressed_r2: bool,
r2_value: Option<f64>,
}

/// The [DBusDevice] is a virtual input device that can emit input events. It
Expand Down Expand Up @@ -53,76 +59,124 @@ impl DBusDevice {
// Check to see if this is an axis event, which requires special
// handling.
let source_cap = event.as_capability();
let is_axis_event = match source_cap {
Capability::Gamepad(gamepad) => matches!(gamepad, Gamepad::Axis(_)),
_ => false,
};

let mut translated = vec![];
let events = DBusEvent::from_native_event(event);
for mut event in events {
if !is_axis_event {
translated.push(event);
continue;
}

// Axis input is a special case, where we need to keep track of the
// current state of the axis, and only emit events whenever the axis
// passes or falls below the defined threshold.
let include_event = match event.action {
Action::Left => {
if self.state.pressed_left && event.as_f64() < AXIS_THRESHOLD {
event.value = InputValue::Float(0.0);
self.state.pressed_left = false;
true
} else if !self.state.pressed_left && event.as_f64() > AXIS_THRESHOLD {
event.value = InputValue::Float(1.0);
self.state.pressed_left = true;
true
} else {
false
let include_event = if matches!(&source_cap, Capability::Gamepad(Gamepad::Axis(_))) {
match event.action {
Action::Left => {
if self.state.pressed_left && event.as_f64() < AXIS_THRESHOLD {
event.value = InputValue::Float(0.0);
self.state.pressed_left = false;
true
} else if !self.state.pressed_left && event.as_f64() > AXIS_THRESHOLD {
event.value = InputValue::Float(1.0);
self.state.pressed_left = true;
true
} else {
false
}
}
}
Action::Right => {
if self.state.pressed_right && event.as_f64() < AXIS_THRESHOLD {
event.value = InputValue::Float(0.0);
self.state.pressed_right = false;
true
} else if !self.state.pressed_right && event.as_f64() > AXIS_THRESHOLD {
event.value = InputValue::Float(1.0);
self.state.pressed_right = true;
true
} else {
false
Action::Right => {
if self.state.pressed_right && event.as_f64() < AXIS_THRESHOLD {
event.value = InputValue::Float(0.0);
self.state.pressed_right = false;
true
} else if !self.state.pressed_right && event.as_f64() > AXIS_THRESHOLD {
event.value = InputValue::Float(1.0);
self.state.pressed_right = true;
true
} else {
false
}
}
}
Action::Up => {
if self.state.pressed_up && event.as_f64() < AXIS_THRESHOLD {
event.value = InputValue::Float(0.0);
self.state.pressed_up = false;
true
} else if !self.state.pressed_up && event.as_f64() > AXIS_THRESHOLD {
event.value = InputValue::Float(1.0);
self.state.pressed_up = true;
true
} else {
false
Action::Up => {
if self.state.pressed_up && event.as_f64() < AXIS_THRESHOLD {
event.value = InputValue::Float(0.0);
self.state.pressed_up = false;
true
} else if !self.state.pressed_up && event.as_f64() > AXIS_THRESHOLD {
event.value = InputValue::Float(1.0);
self.state.pressed_up = true;
true
} else {
false
}
}
Action::Down => {
if self.state.pressed_down && event.as_f64() < AXIS_THRESHOLD {
event.value = InputValue::Float(0.0);
self.state.pressed_down = false;
true
} else if !self.state.pressed_down && event.as_f64() > AXIS_THRESHOLD {
event.value = InputValue::Float(1.0);
self.state.pressed_down = true;
true
} else {
false
}
}
_ => true,
}
Action::Down => {
if self.state.pressed_down && event.as_f64() < AXIS_THRESHOLD {
event.value = InputValue::Float(0.0);
self.state.pressed_down = false;
true
} else if !self.state.pressed_down && event.as_f64() > AXIS_THRESHOLD {
event.value = InputValue::Float(1.0);
self.state.pressed_down = true;
true
} else {
false
}
// Trigger input is also a special case, where we need to keep track of the
// current state of the trigger, and only emit events whenever the trigger
// passes or falls below the defined threshold.
else if matches!(&source_cap, Capability::Gamepad(Gamepad::Trigger(_))) {
match event.action {
Action::L2 => {
let value = event.as_f64();
self.state.l2_value = Some(value);
if self.state.pressed_l2 && value < TRIGGER_THRESHOLD {
event.value = InputValue::Float(0.0);
self.state.pressed_l2 = false;
true
} else if !self.state.pressed_l2 && value > TRIGGER_THRESHOLD {
event.value = InputValue::Float(1.0);
self.state.pressed_l2 = true;
true
} else {
false
}
}
Action::R2 => {
let value = event.as_f64();
self.state.r2_value = Some(value);
if self.state.pressed_r2 && value < TRIGGER_THRESHOLD {
event.value = InputValue::Float(0.0);
self.state.pressed_r2 = false;
true
} else if !self.state.pressed_r2 && value > TRIGGER_THRESHOLD {
event.value = InputValue::Float(1.0);
self.state.pressed_r2 = true;
true
} else {
false
}
}
_ => true,
}
_ => true,
}
// Trigger buttons should be ignored if analog trigger input is
// detected.
else if matches!(
&source_cap,
Capability::Gamepad(Gamepad::Button(GamepadButton::LeftTrigger))
) {
self.state.l2_value.is_none()
} else if matches!(
&source_cap,
Capability::Gamepad(Gamepad::Button(GamepadButton::RightTrigger))
) {
self.state.r2_value.is_none()
}
// All other translated events should be emitted
else {
true
};

if include_event {
Expand Down
Loading