Skip to content

Commit

Permalink
fix(Dpad): Fix DPad mapping in device profiles.
Browse files Browse the repository at this point in the history
  • Loading branch information
pastaq committed Jul 16, 2024
1 parent 4de18fe commit 84e59d6
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 294 deletions.
3 changes: 0 additions & 3 deletions src/input/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,6 @@ pub enum GamepadAxis {
Hat1,
Hat2,
Hat3,
/// Axis input from two binary button inputs
Buttons(GamepadButton, GamepadButton),
}

impl fmt::Display for GamepadAxis {
Expand All @@ -530,7 +528,6 @@ impl fmt::Display for GamepadAxis {
GamepadAxis::Hat1 => write!(f, "Hat1"),
GamepadAxis::Hat2 => write!(f, "Hat2"),
GamepadAxis::Hat3 => write!(f, "Hat3"),
GamepadAxis::Buttons(_, _) => write!(f, "Buttons"),
}
}
}
Expand Down
25 changes: 0 additions & 25 deletions src/input/event/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,31 +218,6 @@ fn actions_from_capability(capability: Capability) -> Vec<Action> {
GamepadAxis::Hat0 => {
vec![Action::Left, Action::Right, Action::Up, Action::Down]
}
GamepadAxis::Buttons(negative, positive) => {
let mut dpad_actions = vec![];
// Match negative axis buttons (up and left)
match negative {
GamepadButton::DPadUp => {
dpad_actions.push(Action::Up);
}
GamepadButton::DPadLeft => {
dpad_actions.push(Action::Left);
}
_ => (),
};
// Match positive axis buttons (down and right)
match positive {
GamepadButton::DPadDown => {
dpad_actions.push(Action::Down);
}
GamepadButton::DPadRight => {
dpad_actions.push(Action::Right);
}
_ => (),
}

dpad_actions
}
_ => vec![Action::None],
},
_ => vec![Action::None],
Expand Down
37 changes: 20 additions & 17 deletions src/input/event/evdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,20 @@ impl EvdevEvent {
x: None,
y: Some(normal_value),
},
AbsoluteAxisCode::ABS_HAT0X => InputValue::Vector2 {
x: Some(normal_value),
y: None,
},
AbsoluteAxisCode::ABS_HAT0Y => InputValue::Vector2 {
x: None,
y: Some(normal_value),
},
AbsoluteAxisCode::ABS_HAT0X => {
if normal_value == 0.0 {
InputValue::Bool(false)
} else {
InputValue::Bool(true)
}
}
AbsoluteAxisCode::ABS_HAT0Y => {
if normal_value == 0.0 {
InputValue::Bool(false)
} else {
InputValue::Bool(true)
}
}
AbsoluteAxisCode::ABS_HAT1X => InputValue::Vector2 {
x: Some(normal_value),
y: None,
Expand Down Expand Up @@ -427,12 +433,12 @@ impl EvdevEvent {
AbsoluteAxisCode::ABS_RZ => {
Capability::Gamepad(Gamepad::Trigger(GamepadTrigger::RightTrigger))
}
AbsoluteAxisCode::ABS_HAT0X => Capability::Gamepad(Gamepad::Axis(
GamepadAxis::Buttons(GamepadButton::DPadLeft, GamepadButton::DPadRight),
)),
AbsoluteAxisCode::ABS_HAT0Y => Capability::Gamepad(Gamepad::Axis(
GamepadAxis::Buttons(GamepadButton::DPadUp, GamepadButton::DPadDown),
)),
AbsoluteAxisCode::ABS_HAT0X => {
Capability::Gamepad(Gamepad::Axis(GamepadAxis::Hat0))
}
AbsoluteAxisCode::ABS_HAT0Y => {
Capability::Gamepad(Gamepad::Axis(GamepadAxis::Hat0))
}
_ => Capability::NotImplemented,
},
EventType::RELATIVE => match RelativeAxisCode(code) {
Expand Down Expand Up @@ -635,9 +641,6 @@ fn event_codes_from_capability(capability: Capability) -> Vec<u16> {
GamepadAxis::Hat3 => {
vec![AbsoluteAxisCode::ABS_HAT3X.0, AbsoluteAxisCode::ABS_HAT3Y.0]
}
GamepadAxis::Buttons(_, _) => {
vec![AbsoluteAxisCode::ABS_HAT0X.0, AbsoluteAxisCode::ABS_HAT0Y.0]
}
},
Gamepad::Trigger(trigg) => match trigg {
GamepadTrigger::LeftTrigger => {
Expand Down
47 changes: 46 additions & 1 deletion src/input/event/native.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::input::capability::Capability;
use evdev::AbsoluteAxisCode;

use crate::input::capability::{Capability, Gamepad, GamepadButton};

use super::{evdev::EvdevEvent, value::InputValue};

Expand Down Expand Up @@ -71,6 +73,49 @@ impl NativeEvent {
pub fn pressed(&self) -> bool {
self.value.pressed()
}

pub fn from_evdev_raw(event: EvdevEvent, hat_state: Option<i32>) -> NativeEvent {
// If this is a Dpad input, figure out with button this event is for
let capability = if let Some(old_state) = hat_state {
let axis = AbsoluteAxisCode(event.as_input_event().code());
let value = event.as_input_event().value();

match axis {
AbsoluteAxisCode::ABS_HAT0X => match value {
-1 => Capability::Gamepad(Gamepad::Button(GamepadButton::DPadLeft)),
1 => Capability::Gamepad(Gamepad::Button(GamepadButton::DPadRight)),
0 => match old_state {
-1 => Capability::Gamepad(Gamepad::Button(GamepadButton::DPadLeft)),
1 => Capability::Gamepad(Gamepad::Button(GamepadButton::DPadRight)),
_ => Capability::NotImplemented,
},
_ => Capability::NotImplemented,
},
AbsoluteAxisCode::ABS_HAT0Y => match value {
-1 => Capability::Gamepad(Gamepad::Button(GamepadButton::DPadUp)),
1 => Capability::Gamepad(Gamepad::Button(GamepadButton::DPadDown)),
0 => match old_state {
-1 => Capability::Gamepad(Gamepad::Button(GamepadButton::DPadUp)),
1 => Capability::Gamepad(Gamepad::Button(GamepadButton::DPadDown)),
_ => Capability::NotImplemented,
},
_ => Capability::NotImplemented,
},

_ => Capability::NotImplemented,
}
} else {
event.as_capability()
};

let value = event.get_value();

NativeEvent {
capability,
value,
source_capability: None,
}
}
}

impl From<EvdevEvent> for NativeEvent {
Expand Down
47 changes: 43 additions & 4 deletions src/input/source/evdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::{
constants::BUS_PREFIX,
drivers::dualsense::hid_report::SetStatePackedOutputData,
input::{
capability::Capability,
capability::{Capability, Gamepad, GamepadAxis, GamepadButton},
composite_device::client::CompositeDeviceClient,
event::{evdev::EvdevEvent, Event},
event::{evdev::EvdevEvent, native::NativeEvent, Event},
output_event::OutputEvent,
},
procfs,
Expand All @@ -35,6 +35,7 @@ pub struct EventDevice {
rx: mpsc::Receiver<SourceCommand>,
ff_effects: HashMap<i16, FFEffect>,
ff_effects_dualsense: Option<i16>,
hat_state: HashMap<AbsoluteAxisCode, i32>,
}

impl EventDevice {
Expand All @@ -47,6 +48,7 @@ impl EventDevice {
rx,
ff_effects: HashMap::new(),
ff_effects_dualsense: None,
hat_state: HashMap::new(),
}
}

Expand Down Expand Up @@ -129,7 +131,7 @@ impl EventDevice {

/// Process incoming events and send them to the composite device.
async fn process_events(
&self,
&mut self,
events: Vec<InputEvent>,
axes_info: &HashMap<AbsoluteAxisCode, AbsInfo>,
) -> Result<(), Box<dyn Error>> {
Expand All @@ -150,15 +152,38 @@ impl EventDevice {
None
};

let state = if event.event_type() == EventType::ABSOLUTE {
let axis = AbsoluteAxisCode(event.code());

let state = match axis {
AbsoluteAxisCode::ABS_HAT0X | AbsoluteAxisCode::ABS_HAT0Y => {
let value = event.value();
let last_value = *self.hat_state.get(&axis).unwrap_or(&0);
self.hat_state
.entry(axis)
.and_modify(|v| *v = value)
.or_insert(value);
Some(last_value)
}
_ => None,
};
state
} else {
None
};

// Convert the event into an [EvdevEvent] and optionally include
// the axis information with min/max values
let mut evdev_event: EvdevEvent = event.into();
if let Some(info) = abs_info {
evdev_event.set_abs_info(*info);
}

// Convert the event into a [NativeEvent]
let native_event: NativeEvent = NativeEvent::from_evdev_raw(evdev_event, state);

// Send the event to the composite device
let event = Event::Evdev(evdev_event);
let event = Event::Native(native_event);
self.composite_device
.process_event(self.get_id(), event)
.await?;
Expand Down Expand Up @@ -432,6 +457,20 @@ impl EventDevice {
let input_event = InputEvent::new(event.0, axis.0, 0);
let evdev_event = EvdevEvent::from(input_event);
let cap = evdev_event.as_capability();
if cap == Capability::Gamepad(Gamepad::Axis(GamepadAxis::Hat0)) {
capabilities
.push(Capability::Gamepad(Gamepad::Button(GamepadButton::DPadUp)));
capabilities.push(Capability::Gamepad(Gamepad::Button(
GamepadButton::DPadDown,
)));
capabilities.push(Capability::Gamepad(Gamepad::Button(
GamepadButton::DPadLeft,
)));
capabilities.push(Capability::Gamepad(Gamepad::Button(
GamepadButton::DPadRight,
)));
continue;
}
capabilities.push(cap);
}
}
Expand Down
Loading

0 comments on commit 84e59d6

Please sign in to comment.