Skip to content

Commit

Permalink
refactor(TargetDevice): add target device traits
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowApex committed Jul 26, 2024
1 parent de268e5 commit b1dea06
Show file tree
Hide file tree
Showing 4 changed files with 449 additions and 69 deletions.
34 changes: 34 additions & 0 deletions src/input/event/native.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::{Duration, Instant};

use evdev::AbsoluteAxisCode;

use crate::input::capability::{Capability, Gamepad, GamepadButton};
Expand Down Expand Up @@ -130,3 +132,35 @@ impl From<EvdevEvent> for NativeEvent {
}
}
}

impl From<ScheduledNativeEvent> for NativeEvent {
fn from(value: ScheduledNativeEvent) -> Self {
value.event
}
}

/// A scheduled event represents an input event that should be sent sometime in
/// the future.
#[derive(Debug, Clone)]
pub struct ScheduledNativeEvent {
event: NativeEvent,
scheduled_time: Instant,
wait_time: Duration,
}

impl ScheduledNativeEvent {
/// Create a new scheduled event with the given time to wait before being
/// emitted.
pub fn new(event: NativeEvent, wait_time: Duration) -> Self {
Self {
event,
scheduled_time: Instant::now(),
wait_time,
}
}

/// Returns true when the scheduled event is ready to be emitted
pub fn is_ready(&self) -> bool {
self.scheduled_time.elapsed() > self.wait_time
}
}
11 changes: 4 additions & 7 deletions src/input/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use crate::input::target::touchscreen::TouchscreenDevice;
use crate::input::target::xb360::XBox360Controller;
use crate::input::target::xbox_elite::XboxEliteController;
use crate::input::target::xbox_series::XboxSeriesController;
use crate::input::target::TargetDeviceType;
use crate::input::target::TargetDevice;
use crate::input::target::TargetDeviceTypeId;
use crate::udev;
use crate::udev::device::UdevDevice;
Expand Down Expand Up @@ -356,17 +356,14 @@ impl Manager {
}

/// Create target input device to emulate based on the given device type.
async fn create_target_device(
&mut self,
kind: &str,
) -> Result<TargetDeviceType, Box<dyn Error>> {
async fn create_target_device(&mut self, kind: &str) -> Result<TargetDevice, Box<dyn Error>> {
log::trace!("Creating target device: {kind}");
let Ok(target_id) = TargetDeviceTypeId::try_from(kind) else {
return Err("Invalid target device ID".to_string().into());
};

// Create the target device to emulate based on the kind
let device = TargetDeviceType::from_type_id(target_id, self.dbus.clone());
let device = TargetDevice::from_type_id(target_id, self.dbus.clone());

Ok(device)
}
Expand All @@ -375,7 +372,7 @@ impl Manager {
/// to send events to the given targets.
async fn start_target_devices(
&mut self,
targets: Vec<TargetDeviceType>,
targets: Vec<TargetDevice>,
) -> Result<HashMap<String, TargetDeviceClient>, Box<dyn Error>> {
let mut target_devices = HashMap::new();
for mut target in targets {
Expand Down
55 changes: 55 additions & 0 deletions src/input/target/dbus_new.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use zbus::Connection;

use super::{TargetInputDevice, TargetOutputDevice};

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

/// The internal emulated device state for tracking analog input
#[derive(Debug, Clone, Default)]
struct State {
pressed_left: bool,
pressed_right: bool,
pressed_up: bool,
pressed_down: bool,
}

/// The [DBusDevice] is a virtual input device that can emit input events. It
/// is primarily used when a [CompositeDevice] is using input interception to
/// divert inputs to an overlay over DBus.
pub struct DBusDevice {
state: State,
conn: Connection,
}

impl DBusDevice {
// Create a new [DBusDevice] instance.
pub fn new(conn: Connection) -> Self {
Self {
state: State::default(),
conn,
}
}
}

impl TargetInputDevice for DBusDevice {
fn write_event(
&mut self,
event: crate::input::event::native::NativeEvent,
) -> Result<(), super::InputError> {
log::trace!("Discarding event: {event:?}");
Ok(())
}

fn get_capabilities(
&self,
) -> Result<Vec<crate::input::capability::Capability>, super::InputError> {
Ok(vec![])
}

fn stop(&self) -> Result<(), super::InputError> {
Ok(())
}
}

impl TargetOutputDevice for DBusDevice {}
Loading

0 comments on commit b1dea06

Please sign in to comment.