From 4bb68a9c910029a0267c011fc675262412667f47 Mon Sep 17 00:00:00 2001 From: Denis Benato Date: Tue, 31 Dec 2024 03:38:52 +0100 Subject: [PATCH] improv(manager): remove duplicate code with minimal specialization in the manager component This makes it easier in the future to add new device types suc as led devices. --- src/input/manager.rs | 480 ++++++++----------------------------------- 1 file changed, 81 insertions(+), 399 deletions(-) diff --git a/src/input/manager.rs b/src/input/manager.rs index 269ac76..23c2d1f 100644 --- a/src/input/manager.rs +++ b/src/input/manager.rs @@ -782,280 +782,77 @@ impl Manager { // Check if this device matches any source udev configs of the running // CompositeDevice. - for source_device in config.source_devices.iter() { - log::trace!("Checking if existing composite device is missing udev device {id}"); - let Some(udev_config) = source_device.udev.as_ref() else { - continue; - }; - if !config.has_matching_udev(&device, udev_config) { - continue; - } - - // Check if the device has already been used in this config or not, - // stop here if the device must be unique. - if let Some(sources) = self.composite_device_sources.get(composite_device) { - for source in sources { - if source != source_device { - continue; - } - if let Some(ignored) = source_device.ignore { - if ignored { - log::debug!( - "Ignoring device {:?}, not adding to composite device: {}", - source_device, - composite_device - ); - break 'start; + match config.get_matching_device(&device) { + Some(source_device) => { + // Check if the device has already been used in this config or not, + // stop here if the device must be unique. + if let Some(sources) = self.composite_device_sources.get(composite_device) { + for source in sources { + if *source != source_device { + continue; } - } - if let Some(unique) = source_device.clone().unique { - if unique { + if let Some(ignored) = source_device.ignore { + if ignored { + log::debug!( + "Ignoring device {:?}, not adding to composite device: {composite_device}", + source_device + ); + break 'start; + } + } + if let Some(unique) = source_device.clone().unique { + if unique { + log::trace!( + "Found unique device {:?}, not adding to composite device {composite_device}", + source_device + ); + break 'start; + } + // Default to being unique + } else { log::trace!( - "Found unique device {:?}, not adding to composite device {}", - source_device, - composite_device + "Found unique device {:?}, not adding to composite device {composite_device}", + source_device ); break 'start; } - // Default to being unique - } else { - log::trace!( - "Found unique device {:?}, not adding to composite device {}", - source_device, - composite_device - ); - break 'start; } } - } - - log::info!("Found missing device, adding source device {id:?} to existing composite device: {composite_device:?}"); - let client = self.composite_devices.get(composite_device.as_str()); - if client.is_none() { - log::error!("No existing composite device found for key {composite_device:?}"); - continue; - } - self.add_device_to_composite_device(device, client.unwrap()) - .await?; - self.source_devices_used - .insert(id.clone(), composite_device.clone()); - let composite_id = composite_device.clone(); - if !self.composite_device_sources.contains_key(&composite_id) { - self.composite_device_sources - .insert(composite_id.clone(), Vec::new()); - } - let sources = self - .composite_device_sources - .get_mut(&composite_id) - .unwrap(); - sources.push(source_device.clone()); - self.source_devices.insert(id, source_device.clone()); - - return Ok(()); - } - - // TODO: Consolidate these - match device.subsystem().as_str() { - "input" => { - log::trace!( - "Checking if existing composite device is missing evdev device: {:?}", - device.name() - ); - for source_device in config.source_devices.iter() { - let Some(evdev_config) = source_device.evdev.as_ref() else { - log::trace!("Evdev section is empty"); - continue; - }; - if !config.has_matching_evdev(&device, evdev_config) { - continue; - } - // Check if the device has already been used in this config or not, stop here if the device must be unique. - if let Some(sources) = self.composite_device_sources.get(composite_device) { - for source in sources { - if source != source_device { - continue; - } - if let Some(ignored) = source_device.ignore { - if ignored { - log::debug!("Ignoring device {:?}, not adding to composite device: {}", source_device, composite_device); - break 'start; - } - } - if let Some(unique) = source_device.clone().unique { - if unique { - log::trace!("Found unique device {:?}, not adding to composite device {}", source_device, composite_device); - break 'start; - } - // Default to being unique - } else { - log::trace!("Found unique device {:?}, not adding to composite device {}", source_device, composite_device); - break 'start; - } + log::info!("Found missing {} device, adding source device {id} to existing composite device: {composite_device:?}", device.subsystem()); + match self.composite_devices.get(composite_device.as_str()) { + Some(client) => { + self.add_device_to_composite_device(device, client).await?; + self.source_devices_used + .insert(id.clone(), composite_device.clone()); + let composite_id = composite_device.clone(); + if !self.composite_device_sources.contains_key(&composite_id) { + self.composite_device_sources + .insert(composite_id.clone(), Vec::new()); } - } + let sources = self + .composite_device_sources + .get_mut(&composite_id) + .unwrap(); + sources.push(source_device.clone()); + self.source_devices.insert(id, source_device.clone()); - log::info!("Found missing device, adding source device {id:?} to existing composite device: {composite_device:?}"); - let client = self.composite_devices.get(composite_device.as_str()); - if client.is_none() { - log::error!( - "No existing composite device found for key {composite_device:?}" - ); - continue; - } - self.add_device_to_composite_device(device, client.unwrap()) - .await?; - self.source_devices_used - .insert(id.clone(), composite_device.clone()); - let composite_id = composite_device.clone(); - if !self.composite_device_sources.contains_key(&composite_id) { - self.composite_device_sources - .insert(composite_id.clone(), Vec::new()); + return Ok(()); } - let sources = self - .composite_device_sources - .get_mut(&composite_id) - .unwrap(); - sources.push(source_device.clone()); - self.source_devices.insert(id, source_device.clone()); - - return Ok(()); + None => log::error!( + "No existing composite device found for key {composite_device:?}" + ), } } - "hidraw" => { + None => { log::trace!( - "Checking if existing composite device is missing hidraw device: {:?}", - device.name() + "Device {id} does not match existing device: {:?}", + config.name ); - for source_device in config.source_devices.iter() { - let Some(hidraw_config) = source_device.hidraw.as_ref() else { - continue; - }; - if !config.has_matching_hidraw(&device, hidraw_config) { - continue; - } - - // Check if the device has already been used in this config or not, stop here if the device must be unique. - if let Some(sources) = self.composite_device_sources.get(composite_device) { - for source in sources { - if source != source_device { - continue; - } - if let Some(ignored) = source_device.ignore { - if ignored { - log::debug!("Ignoring device {:?}, not adding to composite device: {}", source_device, composite_device); - break 'start; - } - } - if let Some(unique) = source_device.clone().unique { - if unique { - log::trace!("Found unique device {:?}, not adding to composite device {}", source_device, composite_device); - break 'start; - } - } else { - log::trace!("Found unique device {:?}, not adding to composite device {}", source_device, composite_device); - break 'start; - } - } - } - - log::info!("Found missing device, adding source device {id} to existing composite device: {composite_device}"); - let handle = self.composite_devices.get(composite_device.as_str()); - if handle.is_none() { - log::error!( - "No existing composite device found for key {}", - composite_device.as_str() - ); - continue; - } - self.add_device_to_composite_device(device, handle.unwrap()) - .await?; - self.source_devices_used - .insert(id.clone(), composite_device.clone()); - let composite_id = composite_device.clone(); - if !self.composite_device_sources.contains_key(&composite_id) { - self.composite_device_sources - .insert(composite_id.clone(), Vec::new()); - } - let sources = self - .composite_device_sources - .get_mut(&composite_id) - .unwrap(); - sources.push(source_device.clone()); - - self.source_devices.insert(id, source_device.clone()); - return Ok(()); - } - } - "iio" => { - log::trace!("Checking if existing composite device is missing iio device"); - for source_device in config.source_devices.iter() { - let Some(iio_config) = source_device.iio.as_ref() else { - continue; - }; - if !config.has_matching_iio(&device, iio_config) { - continue; - } - - // Check if the device has already been used in this config or not, stop here if the device must be unique. - if let Some(sources) = self.composite_device_sources.get(composite_device) { - for source in sources { - if source != source_device { - continue; - } - if let Some(ignored) = source_device.ignore { - if ignored { - log::debug!("Ignoring device {:?}, not adding to composite device: {}", source_device, composite_device); - continue; - } - } - if let Some(unique) = source_device.clone().unique { - if unique { - log::trace!("Found unique device {:?}, not adding to composite device {}", source_device, composite_device); - break 'start; - } - } else { - log::trace!("Found unique device {:?}, not adding to composite device {}", source_device, composite_device); - break 'start; - } - } - } - - log::info!("Found missing device, adding source device {id} to existing composite device: {composite_device}"); - let handle = self.composite_devices.get(composite_device.as_str()); - if handle.is_none() { - log::error!( - "No existing composite device found for key {}", - composite_device.as_str() - ); - continue; - } - self.add_device_to_composite_device(device, handle.unwrap()) - .await?; - self.source_devices_used - .insert(id.clone(), composite_device.clone()); - let composite_id = composite_device.clone(); - if !self.composite_device_sources.contains_key(&composite_id) { - self.composite_device_sources - .insert(composite_id.clone(), Vec::new()); - } - let sources = self - .composite_device_sources - .get_mut(&composite_id) - .unwrap(); - sources.push(source_device.clone()); - - self.source_devices.insert(id, source_device.clone()); - return Ok(()); - } } - _ => (), - }; - log::trace!( - "Device {id} does not match existing device: {:?}", - config.name - ); + } } + log::debug!("No existing composite device matches device {id}."); // Check all CompositeDevice configs to see if this device creates @@ -1087,155 +884,40 @@ impl Manager { continue; } - // Check if this device matches any source udev configs - for source_device in config.source_devices.iter() { - let Some(udev_config) = source_device.udev.as_ref() else { - continue; - }; - if !config.has_matching_udev(&device, udev_config) { - continue; - } - - if let Some(ignored) = source_device.ignore { - if ignored { - log::trace!("Event device configured to ignore: {:?}", device); - return Ok(()); - } - } - log::info!("Found a matching udev device {id}, creating CompositeDevice"); - let dev = self - .create_composite_device_from_config(&config, device) - .await?; - - // Get the target input devices from the config - let target_devices_config = config.target_devices.clone(); - - // Create the composite deivce - self.start_composite_device( - dev, - config.clone(), - target_devices_config, - source_device.clone(), - ) - .await?; - - return Ok(()); - } - - let source_devices = config.source_devices.clone(); - match device.subsystem().as_str() { - "input" => { - for source_device in source_devices { - if source_device.evdev.is_none() { - continue; - } - // how to refrence source devices used by this config? - - if config.has_matching_evdev(&device, &source_device.clone().evdev.unwrap()) - { - if let Some(ignored) = source_device.ignore { - if ignored { - log::trace!("Event device configured to ignore: {:?}", device); - return Ok(()); - } - } - log::info!( - "Found a matching evdev device {id}, creating CompositeDevice" - ); - let dev = self - .create_composite_device_from_config(&config, device) - .await?; - - // Get the target input devices from the config - let target_devices_config = config.target_devices.clone(); - - // Create the composite deivce - self.start_composite_device( - dev, - config, - target_devices_config, - source_device.clone(), - ) - .await?; - + // Check if this device matches any source configs + match config.get_matching_device(&device) { + Some(source_device) => { + if let Some(ignored) = source_device.ignore { + if ignored { + log::trace!("Event device configured to ignore: {:?}", device); return Ok(()); } } - } - "hidraw" => { - for source_device in source_devices { - if source_device.hidraw.is_none() { - continue; - } - if config - .has_matching_hidraw(&device, &source_device.clone().hidraw.unwrap()) - { - if let Some(ignored) = source_device.ignore { - if ignored { - log::trace!("hidraw device configured to ignore: {:?}", device); - return Ok(()); - } - } - log::info!( - "Found a matching hidraw device {id}, creating CompositeDevice" - ); - let dev = self - .create_composite_device_from_config(&config, device) - .await?; - - // Get the target input devices from the config - let target_devices_config = config.target_devices.clone(); - - // Create the composite deivce - self.start_composite_device( - dev, - config, - target_devices_config, - source_device.clone(), - ) - .await?; + log::info!( + "Found a matching {} device {id}, creating CompositeDevice", + device.subsystem() + ); + let dev = self + .create_composite_device_from_config(&config, device) + .await?; - return Ok(()); - } - } - } - "iio" => { - for source_device in source_devices { - if source_device.iio.is_none() { - continue; - } - if config.has_matching_iio(&device, &source_device.clone().iio.unwrap()) { - if let Some(ignored) = source_device.ignore { - if ignored { - log::trace!("iio device configured to ignore: {:?}", device); - return Ok(()); - } - } - log::info!( - "Found a matching iio device {id}, creating CompositeDevice" - ); - let dev = self - .create_composite_device_from_config(&config, device.clone()) - .await?; - - // Get the target input devices from the config - let target_devices_config = config.target_devices.clone(); - - // Create the composite deivce - self.start_composite_device( - dev, - config, - target_devices_config, - source_device.clone(), - ) - .await?; + // Get the target input devices from the config + let target_devices_config = config.target_devices.clone(); - return Ok(()); - } - } + // Create the composite deivce + self.start_composite_device( + dev, + config.clone(), + target_devices_config, + source_device.clone(), + ) + .await?; + + return Ok(()); } - _ => (), + None => {} } + log::trace!("Device does not match config: {:?}", config.name); } log::debug!("No unused configs found for device.");