Skip to content

Commit

Permalink
fix(SourceDevice): code cleanup
Browse files Browse the repository at this point in the history
This commit does not modify how the software behaves, while refactoring some code:
  - remove a method and a big match that was used only once just to avoid calling .clone()
  - solve the SourceDevice scope creep that was in need (for no reason) to know specific internal details about sub-types
  • Loading branch information
NeroReflex committed Jan 23, 2025
1 parent 86849dc commit df4eda2
Show file tree
Hide file tree
Showing 5 changed files with 235 additions and 162 deletions.
2 changes: 1 addition & 1 deletion src/input/composite_device/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ impl CompositeDevice {

// Add the IIO IMU Dbus interface. We do this here because it needs the source
// device transmitter and this is the only place we can refrence it at the moment.
let device = source_device.get_device();
let device = source_device.get_device_ref().clone();
if let SourceDevice::Iio(_) = source_device {
SourceIioImuInterface::listen_on_dbus(self.conn.clone(), device.clone()).await?;
}
Expand Down
48 changes: 47 additions & 1 deletion src/input/source/evdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{

use self::{blocked::BlockedEventDevice, gamepad::GamepadEventDevice};

use super::{SourceDriver, SourceDriverOptions};
use super::{SourceDeviceCompatible, SourceDriver, SourceDriverOptions};

/// List of available drivers
enum DriverType {
Expand All @@ -27,6 +27,52 @@ pub enum EventDevice {
Gamepad(SourceDriver<GamepadEventDevice>),
}

impl SourceDeviceCompatible for EventDevice {
fn get_device_ref(&self) -> &UdevDevice {
match self {
EventDevice::Blocked(source_driver) => source_driver.info_ref(),
EventDevice::Gamepad(source_driver) => source_driver.info_ref(),
}
}

fn get_id(&self) -> String {
match self {
EventDevice::Blocked(source_driver) => source_driver.get_id(),
EventDevice::Gamepad(source_driver) => source_driver.get_id(),
}
}

fn client(&self) -> super::client::SourceDeviceClient {
match self {
EventDevice::Blocked(source_driver) => source_driver.client(),
EventDevice::Gamepad(source_driver) => source_driver.client(),
}
}

async fn run(self) -> Result<(), Box<dyn Error>> {
match self {
EventDevice::Blocked(source_driver) => source_driver.run().await,
EventDevice::Gamepad(source_driver) => source_driver.run().await,
}
}

fn get_capabilities(
&self,
) -> Result<Vec<crate::input::capability::Capability>, super::InputError> {
match self {
EventDevice::Blocked(source_driver) => source_driver.get_capabilities(),
EventDevice::Gamepad(source_driver) => source_driver.get_capabilities(),
}
}

fn get_device_path(&self) -> String {
match self {
EventDevice::Blocked(source_driver) => source_driver.get_device_path(),
EventDevice::Gamepad(source_driver) => source_driver.get_device_path(),
}
}
}

impl EventDevice {
pub fn new(
device_info: UdevDevice,
Expand Down
103 changes: 102 additions & 1 deletion src/input/source/hidraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use self::{
opineo::OrangePiNeoTouchpad, steam_deck::DeckController,
};

use super::{SourceDriver, SourceDriverOptions};
use super::{SourceDeviceCompatible, SourceDriver, SourceDriverOptions};

/// List of available drivers
enum DriverType {
Expand Down Expand Up @@ -64,6 +64,107 @@ pub enum HidRawDevice {
XpadUhid(SourceDriver<XpadUhid>),
}

impl SourceDeviceCompatible for HidRawDevice {
fn get_device_ref(&self) -> &UdevDevice {
match self {
HidRawDevice::DualSense(source_driver) => source_driver.info_ref(),
HidRawDevice::Fts3528Touchscreen(source_driver) => source_driver.info_ref(),
HidRawDevice::HoripadSteam(source_driver) => source_driver.info_ref(),
HidRawDevice::LegionGoDCombined(source_driver) => source_driver.info_ref(),
HidRawDevice::LegionGoDSplit(source_driver) => source_driver.info_ref(),
HidRawDevice::LegionGoFPS(source_driver) => source_driver.info_ref(),
HidRawDevice::LegionGoX(source_driver) => source_driver.info_ref(),
HidRawDevice::OrangePiNeo(source_driver) => source_driver.info_ref(),
HidRawDevice::RogAlly(source_driver) => source_driver.info_ref(),
HidRawDevice::SteamDeck(source_driver) => source_driver.info_ref(),
HidRawDevice::XpadUhid(source_driver) => source_driver.info_ref(),
}
}

fn get_id(&self) -> String {
match self {
HidRawDevice::DualSense(source_driver) => source_driver.get_id(),
HidRawDevice::Fts3528Touchscreen(source_driver) => source_driver.get_id(),
HidRawDevice::HoripadSteam(source_driver) => source_driver.get_id(),
HidRawDevice::LegionGoDCombined(source_driver) => source_driver.get_id(),
HidRawDevice::LegionGoDSplit(source_driver) => source_driver.get_id(),
HidRawDevice::LegionGoFPS(source_driver) => source_driver.get_id(),
HidRawDevice::LegionGoX(source_driver) => source_driver.get_id(),
HidRawDevice::OrangePiNeo(source_driver) => source_driver.get_id(),
HidRawDevice::RogAlly(source_driver) => source_driver.get_id(),
HidRawDevice::SteamDeck(source_driver) => source_driver.get_id(),
HidRawDevice::XpadUhid(source_driver) => source_driver.get_id(),
}
}

fn client(&self) -> super::client::SourceDeviceClient {
match self {
HidRawDevice::DualSense(source_driver) => source_driver.client(),
HidRawDevice::Fts3528Touchscreen(source_driver) => source_driver.client(),
HidRawDevice::HoripadSteam(source_driver) => source_driver.client(),
HidRawDevice::LegionGoDCombined(source_driver) => source_driver.client(),
HidRawDevice::LegionGoDSplit(source_driver) => source_driver.client(),
HidRawDevice::LegionGoFPS(source_driver) => source_driver.client(),
HidRawDevice::LegionGoX(source_driver) => source_driver.client(),
HidRawDevice::OrangePiNeo(source_driver) => source_driver.client(),
HidRawDevice::RogAlly(source_driver) => source_driver.client(),
HidRawDevice::SteamDeck(source_driver) => source_driver.client(),
HidRawDevice::XpadUhid(source_driver) => source_driver.client(),

}
}

async fn run(self) -> Result<(), Box<dyn Error>> {
match self {
HidRawDevice::DualSense(source_driver) => source_driver.run().await,
HidRawDevice::Fts3528Touchscreen(source_driver) => source_driver.run().await,
HidRawDevice::HoripadSteam(source_driver) => source_driver.run().await,
HidRawDevice::LegionGoDCombined(source_driver) => source_driver.run().await,
HidRawDevice::LegionGoDSplit(source_driver) => source_driver.run().await,
HidRawDevice::LegionGoFPS(source_driver) => source_driver.run().await,
HidRawDevice::LegionGoX(source_driver) => source_driver.run().await,
HidRawDevice::OrangePiNeo(source_driver) => source_driver.run().await,
HidRawDevice::RogAlly(source_driver) => source_driver.run().await,
HidRawDevice::SteamDeck(source_driver) => source_driver.run().await,
HidRawDevice::XpadUhid(source_driver) => source_driver.run().await,
}
}

fn get_capabilities(
&self,
) -> Result<Vec<crate::input::capability::Capability>, super::InputError> {
match self {
HidRawDevice::DualSense(source_driver) => source_driver.get_capabilities(),
HidRawDevice::Fts3528Touchscreen(source_driver) => source_driver.get_capabilities(),
HidRawDevice::HoripadSteam(source_driver) => source_driver.get_capabilities(),
HidRawDevice::LegionGoDCombined(source_driver) => source_driver.get_capabilities(),
HidRawDevice::LegionGoDSplit(source_driver) => source_driver.get_capabilities(),
HidRawDevice::LegionGoFPS(source_driver) => source_driver.get_capabilities(),
HidRawDevice::LegionGoX(source_driver) => source_driver.get_capabilities(),
HidRawDevice::OrangePiNeo(source_driver) => source_driver.get_capabilities(),
HidRawDevice::RogAlly(source_driver) => source_driver.get_capabilities(),
HidRawDevice::SteamDeck(source_driver) => source_driver.get_capabilities(),
HidRawDevice::XpadUhid(source_driver) => source_driver.get_capabilities(),
}
}

fn get_device_path(&self) -> String {
match self {
HidRawDevice::DualSense(source_driver) => source_driver.get_device_path(),
HidRawDevice::Fts3528Touchscreen(source_driver) => source_driver.get_device_path(),
HidRawDevice::HoripadSteam(source_driver) => source_driver.get_device_path(),
HidRawDevice::LegionGoDCombined(source_driver) => source_driver.get_device_path(),
HidRawDevice::LegionGoDSplit(source_driver) => source_driver.get_device_path(),
HidRawDevice::LegionGoFPS(source_driver) => source_driver.get_device_path(),
HidRawDevice::LegionGoX(source_driver) => source_driver.get_device_path(),
HidRawDevice::OrangePiNeo(source_driver) => source_driver.get_device_path(),
HidRawDevice::RogAlly(source_driver) => source_driver.get_device_path(),
HidRawDevice::SteamDeck(source_driver) => source_driver.get_device_path(),
HidRawDevice::XpadUhid(source_driver) => source_driver.get_device_path(),
}
}
}

impl HidRawDevice {
/// Create a new [HidRawDevice] associated with the given device and
/// composite device. The appropriate driver will be selected based on
Expand Down
48 changes: 47 additions & 1 deletion src/input/source/iio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{

use self::{accel_gyro_3d_new::AccelGyro3dImu, bmi_imu_new::BmiImu};

use super::SourceDriver;
use super::{SourceDeviceCompatible, SourceDriver};

/// List of available drivers
enum DriverType {
Expand All @@ -30,6 +30,52 @@ pub enum IioDevice {
AccelGryo3D(SourceDriver<AccelGyro3dImu>),
}

impl SourceDeviceCompatible for IioDevice {
fn get_device_ref(&self) -> &UdevDevice {
match self {
IioDevice::BmiImu(source_driver) => source_driver.info_ref(),
IioDevice::AccelGryo3D(source_driver) => source_driver.info_ref(),
}
}

fn get_id(&self) -> String {
match self {
IioDevice::BmiImu(source_driver) => source_driver.get_id(),
IioDevice::AccelGryo3D(source_driver) => source_driver.get_id(),
}
}

fn client(&self) -> super::client::SourceDeviceClient {
match self {
IioDevice::BmiImu(source_driver) => source_driver.client(),
IioDevice::AccelGryo3D(source_driver) => source_driver.client(),
}
}

async fn run(self) -> Result<(), Box<dyn Error>> {
match self {
IioDevice::BmiImu(source_driver) => source_driver.run().await,
IioDevice::AccelGryo3D(source_driver) => source_driver.run().await,
}
}

fn get_capabilities(
&self,
) -> Result<Vec<crate::input::capability::Capability>, super::InputError> {
match self {
IioDevice::BmiImu(source_driver) => source_driver.get_capabilities(),
IioDevice::AccelGryo3D(source_driver) => source_driver.get_capabilities(),
}
}

fn get_device_path(&self) -> String {
match self {
IioDevice::BmiImu(source_driver) => source_driver.get_device_path(),
IioDevice::AccelGryo3D(source_driver) => source_driver.get_device_path(),
}
}
}

impl IioDevice {
/// Create a new [IioDevice] associated with the given device and
/// composite device. The appropriate driver will be selected based on
Expand Down
Loading

0 comments on commit df4eda2

Please sign in to comment.