Skip to content

Commit

Permalink
refactor: use target type id instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
imLinguin authored and ShadowApex committed Feb 10, 2025
1 parent b3d00bd commit 78da613
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
14 changes: 5 additions & 9 deletions src/dbus/interface/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ pub mod touchscreen;
use zbus::fdo;
use zbus_macros::interface;

use crate::input::target::TargetDeviceTypeId;

/// The [TargetInterface] provides a DBus interface that can be exposed for managing
/// a target input device.
pub struct TargetInterface {
Expand All @@ -15,20 +17,14 @@ pub struct TargetInterface {
}

impl TargetInterface {
pub fn new(dev_name: String, device_type: String) -> TargetInterface {
pub fn new(device_type: &TargetDeviceTypeId) -> TargetInterface {
TargetInterface {
dev_name,
device_type,
dev_name: device_type.name().to_owned(),
device_type: device_type.as_str().to_owned(),
}
}
}

impl Default for TargetInterface {
fn default() -> Self {
Self::new("Gamepad".to_string(), "gamepad".to_string())
}
}

#[interface(name = "org.shadowblip.Input.Target")]
impl TargetInterface {
/// Name of the DBus device
Expand Down
8 changes: 5 additions & 3 deletions src/input/target/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ use crate::{
},
};

use super::{client::TargetDeviceClient, TargetInputDevice, TargetOutputDevice};
use super::{
client::TargetDeviceClient, TargetDeviceTypeId, TargetInputDevice, TargetOutputDevice,
};

/// The threshold for axis inputs to be considered "pressed"
const AXIS_THRESHOLD: f64 = 0.60;
Expand Down Expand Up @@ -300,12 +302,12 @@ impl TargetInputDevice for DBusDevice {
dbus: Connection,
path: String,
_client: TargetDeviceClient,
type_id: String,
type_id: TargetDeviceTypeId,
) {
log::debug!("Starting dbus interface: {path}");
self.dbus_path = Some(path.clone());
tokio::task::spawn(async move {
let generic_interface = TargetInterface::new("DBusDevice".into(), type_id);
let generic_interface = TargetInterface::new(&type_id);
let iface = TargetDBusInterface::new();
let object_server = dbus.object_server();
let (gen_result, result) = tokio::join!(
Expand Down
9 changes: 6 additions & 3 deletions src/input/target/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use crate::{
},
};

use super::{client::TargetDeviceClient, InputError, TargetInputDevice, TargetOutputDevice};
use super::{
client::TargetDeviceClient, InputError, TargetDeviceTypeId, TargetInputDevice,
TargetOutputDevice,
};

#[derive(Debug)]
pub struct KeyboardDevice {
Expand Down Expand Up @@ -221,11 +224,11 @@ impl TargetInputDevice for KeyboardDevice {
dbus: Connection,
path: String,
client: TargetDeviceClient,
type_id: String,
type_id: TargetDeviceTypeId,
) {
log::debug!("Starting dbus interface: {path}");
tokio::task::spawn(async move {
let generic_interface = TargetInterface::new("Keyboard".into(), type_id);
let generic_interface = TargetInterface::new(&type_id);
let iface = TargetKeyboardInterface::new(client);

let object_server = dbus.object_server();
Expand Down
9 changes: 4 additions & 5 deletions src/input/target/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,13 @@ pub trait TargetInputDevice {
dbus: Connection,
path: String,
client: TargetDeviceClient,
type_id: String,
type_id: TargetDeviceTypeId,
) {
log::debug!("Starting dbus interface: {path}");
log::trace!("Using device client: {client:?}");
tokio::task::spawn(async move {
let name = "Gamepad".to_string();
let generic_interface = TargetInterface::new(name.clone(), type_id);
let iface = TargetGamepadInterface::new(name);
let generic_interface = TargetInterface::new(&type_id);
let iface = TargetGamepadInterface::new(type_id.name().to_owned());

let object_server = dbus.object_server();
let (gen_result, result) = tokio::join!(
Expand Down Expand Up @@ -429,7 +428,7 @@ impl<T: TargetInputDevice + TargetOutputDevice + Send + 'static> TargetDriver<T>
self.dbus.clone(),
dbus_path.clone(),
client,
self.type_id.as_str().to_owned(),
self.type_id,
);

log::debug!("Target device running: {dbus_path}");
Expand Down
7 changes: 4 additions & 3 deletions src/input/target/mouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::{
};

use super::{
client::TargetDeviceClient, InputError, OutputError, TargetInputDevice, TargetOutputDevice,
client::TargetDeviceClient, InputError, OutputError, TargetDeviceTypeId, TargetInputDevice,
TargetOutputDevice,
};

/// Configuration of the target touchpad device.
Expand Down Expand Up @@ -143,11 +144,11 @@ impl TargetInputDevice for MouseDevice {
dbus: Connection,
path: String,
client: TargetDeviceClient,
type_id: String,
type_id: TargetDeviceTypeId,
) {
log::debug!("Starting dbus interface: {path}");
tokio::task::spawn(async move {
let generic_interface = TargetInterface::new("Mouse".into(), type_id);
let generic_interface = TargetInterface::new(&type_id);
let iface = TargetMouseInterface::new(client);

let gen_result = dbus
Expand Down

0 comments on commit 78da613

Please sign in to comment.