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 b473d2b commit 8c5b174
Show file tree
Hide file tree
Showing 13 changed files with 1,985 additions and 2,592 deletions.
8 changes: 4 additions & 4 deletions rootfs/usr/share/inputplumber/devices/50-orangepi_neo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ source_devices:
- group: mouse
blocked: true
evdev:
name: { OPI0001:00 0911:5288 Touchpad, SYNA3602:00 0911:5288 Touchpad }
phys_path: { i2c-OPI0001:00, i2c-SYNA3602:00 }
name: "{OPI0001:00 0911:5288 Touchpad,SYNA3602:00 0911:5288 Touchpad}"
phys_path: "{i2c-OPI0001:00,i2c-SYNA3602:00}"
handler: event*
- group: mouse
blocked: true
evdev:
name: { OPI0002:00 0911:5288 Touchpad, SYNA3602:01 0911:5288 Touchpad }
phys_path: { i2c-OPI0002:00, i2c-SYNA3602:01 }
name: "{OPI0002:00 0911:5288 Touchpad,SYNA3602:01 0911:5288 Touchpad}"
phys_path: "{i2c-OPI0002:00,i2c-SYNA3602:01}"
handler: event*

## Gamepad Devices
Expand Down
44 changes: 44 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,45 @@ 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,
}
}

/// Create a new scheduled event with the given timestamp and wait time before
/// being emitted
pub fn new_with_time(event: NativeEvent, timestamp: Instant, wait_time: Duration) -> Self {
Self {
event,
scheduled_time: timestamp,
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
}
}
32 changes: 9 additions & 23 deletions src/input/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,7 @@ use crate::input::composite_device::CompositeDevice;
use crate::input::source::evdev;
use crate::input::source::hidraw;
use crate::input::source::iio;
use crate::input::target::dbus::DBusDevice;
use crate::input::target::dualsense;
use crate::input::target::dualsense::DualSenseDevice;
use crate::input::target::dualsense::DualSenseHardware;
use crate::input::target::keyboard::KeyboardDevice;
use crate::input::target::mouse::MouseDevice;
use crate::input::target::steam_deck::SteamDeckDevice;
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 +345,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,10 +361,10 @@ 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 {
for target in targets {
// Get the target device class to determine the DBus path to use for
// the device.
let device_class = target.dbus_device_class();
Expand All @@ -391,11 +377,10 @@ impl Manager {
};
target_devices.insert(path.clone(), client.clone());
self.target_devices.insert(path.clone(), client.clone());
target.listen_on_dbus(path.clone()).await?;

// Run the target device
tokio::spawn(async move {
if let Err(e) = target.run().await {
if let Err(e) = target.run(path.clone()).await {
log::error!("Failed to run target device {path}: {e:?}");
}
log::debug!("Target device closed at: {path}");
Expand Down Expand Up @@ -1382,7 +1367,7 @@ impl Manager {
name.clone(),
) else {
log::warn!(
"Unable to create UdevDevice from {base_path}/{name}"
"Unable to create UdevDevice from {base_path}/{name} to check initialization"
);
continue 'outer;
};
Expand Down Expand Up @@ -1535,7 +1520,8 @@ impl Manager {
CompositeDeviceConfig::from_yaml_file(file.path().display().to_string());
if device.is_err() {
log::warn!(
"Failed to parse composite device config: {}",
"Failed to parse composite device config '{}': {}",
file.path().display(),
device.unwrap_err()
);
continue;
Expand Down
Loading

0 comments on commit 8c5b174

Please sign in to comment.