From 78da613aa49ff337d90240fec3671014186e1080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Lidwin?= Date: Fri, 7 Feb 2025 13:21:49 +0100 Subject: [PATCH] refactor: use target type id instead of string --- src/dbus/interface/target/mod.rs | 14 +++++--------- src/input/target/dbus.rs | 8 +++++--- src/input/target/keyboard.rs | 9 ++++++--- src/input/target/mod.rs | 9 ++++----- src/input/target/mouse.rs | 7 ++++--- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/dbus/interface/target/mod.rs b/src/dbus/interface/target/mod.rs index 0f0f9ab..1035f8f 100644 --- a/src/dbus/interface/target/mod.rs +++ b/src/dbus/interface/target/mod.rs @@ -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 { @@ -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 diff --git a/src/input/target/dbus.rs b/src/input/target/dbus.rs index a9d5715..aed2026 100644 --- a/src/input/target/dbus.rs +++ b/src/input/target/dbus.rs @@ -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; @@ -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!( diff --git a/src/input/target/keyboard.rs b/src/input/target/keyboard.rs index eb2dbf1..c8e2a1a 100644 --- a/src/input/target/keyboard.rs +++ b/src/input/target/keyboard.rs @@ -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 { @@ -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(); diff --git a/src/input/target/mod.rs b/src/input/target/mod.rs index 2485802..4f17d4c 100644 --- a/src/input/target/mod.rs +++ b/src/input/target/mod.rs @@ -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!( @@ -429,7 +428,7 @@ impl TargetDriver self.dbus.clone(), dbus_path.clone(), client, - self.type_id.as_str().to_owned(), + self.type_id, ); log::debug!("Target device running: {dbus_path}"); diff --git a/src/input/target/mouse.rs b/src/input/target/mouse.rs index b9719ae..137d755 100644 --- a/src/input/target/mouse.rs +++ b/src/input/target/mouse.rs @@ -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. @@ -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